Geant4 Cross Reference |
1 // Copyright (C) 2010, Guy Barrand. All rights reserved. 2 // See the file tools.license for terms. 3 4 #ifndef toolx_X11_zb_viewer 5 #define toolx_X11_zb_viewer 6 7 #include "base_session" 8 #include "pixwin" 9 #include "simple_dispatcher" 10 11 #include <tools/sg/zb_viewer> 12 #include <tools/sg/device_interactor> 13 14 namespace toolx { 15 namespace X11 { 16 17 class zb_viewer 18 :public tools::sg::zb_viewer, protected pixwin { 19 typedef tools::sg::zb_viewer parent; 20 typedef pixwin parent_pixwin; 21 private: 22 class dispatcher : public simple_dispatcher { 23 typedef simple_dispatcher parent; 24 public: 25 virtual bool dispatch(XEvent& a_event) { 26 if(parent::dispatch(a_event)) return true; 27 if(a_event.type==ButtonPress && a_event.xbutton.button==1) { 28 if(!m_viewer.device_interactor()) return false; 29 tools::sg::mouse_down_event event(a_event.xbutton.x,a_event.xbutton.y); 30 m_viewer.device_interactor()->mouse_press(event); 31 return true; 32 } else if(a_event.type==ButtonRelease && a_event.xbutton.button==1) { 33 if(!m_viewer.device_interactor()) return false; 34 tools::sg::mouse_up_event event(a_event.xbutton.x,a_event.xbutton.y); 35 m_viewer.device_interactor()->mouse_release(event); 36 return true; 37 } else if(a_event.type==MotionNotify) { 38 if(!m_viewer.device_interactor()) return false; 39 if((a_event.xmotion.state & Button1MotionMask)==Button1MotionMask) { 40 tools::sg::mouse_move_event event(a_event.xmotion.x,a_event.xmotion.y,0,0,false); 41 m_viewer.device_interactor()->mouse_move(event); 42 } 43 } else if((a_event.type==ButtonPress)&&(a_event.xbutton.button==4)) { // mouse scrollwheel down : 44 if(!m_viewer.device_interactor()) return false; 45 tools::sg::wheel_rotate_event event(8); //8=cooking. 46 m_viewer.device_interactor()->wheel_rotate(event); 47 return true; 48 } else if((a_event.type==ButtonPress)&&(a_event.xbutton.button==5)) { // mouse scrollwheel up : 49 if(!m_viewer.device_interactor()) return false; 50 tools::sg::wheel_rotate_event event(-8); //8=cooking. 51 m_viewer.device_interactor()->wheel_rotate(event); 52 return true; 53 } 54 return false; 55 } 56 virtual void win_render() {m_viewer.win_render();} 57 virtual void set_size(unsigned int a_width,unsigned int a_height) {m_viewer.set_size(a_width,a_height);} 58 virtual dispatcher* copy() const {return new dispatcher(*this);} 59 public: 60 dispatcher(zb_viewer& a_viewer) 61 :parent(a_viewer.m_session,a_viewer.m_win) 62 ,m_viewer(a_viewer){} 63 virtual ~dispatcher(){} 64 protected: 65 dispatcher(const dispatcher& a_from) 66 :parent(a_from) 67 ,m_viewer(a_from.m_viewer) 68 {} 69 dispatcher& operator=(const dispatcher& a_from) { 70 parent::operator=(a_from); 71 return *this; 72 } 73 protected: 74 zb_viewer& m_viewer; 75 }; 76 77 public: 78 virtual void set_size(unsigned int a_w,unsigned int a_h) { 79 parent::set_size(a_w,a_h); 80 free_XImage(); 81 alloc_XImage(a_w,a_h); 82 } 83 public: 84 zb_viewer(base_session& a_session, 85 int a_x = 0,int a_y = 0, 86 unsigned int a_width = 500,unsigned int a_height = 500, 87 const std::string& a_win_title = "") 88 :parent(a_session.out(),a_width,a_height) 89 ,parent_pixwin(a_session.out(),a_session.monitor(),a_session.display()) 90 ,m_session(a_session) 91 ,m_win(0) 92 ,m_interactor(0) 93 { 94 if(!m_session.display()) return; //throw 95 m_win = m_session.create_window(a_win_title.c_str(),a_x,a_y,a_width,a_height); 96 if(!m_win) return; //throw 97 m_session.add_dispatcher(new dispatcher(*this)); 98 } 99 virtual ~zb_viewer() { 100 if(m_win) { 101 m_session.remove_dispatchers_with_window(m_win); 102 m_session.delete_window(m_win); 103 m_session.sync(); 104 } 105 } 106 protected: 107 zb_viewer(const zb_viewer& a_from) 108 :parent(a_from) 109 ,parent_pixwin(a_from) 110 ,m_session(a_from.m_session) 111 ,m_win(a_from.m_win) 112 ,m_interactor(0) 113 {} 114 zb_viewer& operator=(const zb_viewer& a_from){ 115 parent::operator=(a_from); 116 parent_pixwin::operator=(a_from); 117 m_win = a_from.m_win; 118 return *this; 119 } 120 public: 121 bool has_window() const {return m_win?true:false;} //for SWIG 122 123 Window window() const {return m_win;} 124 125 bool show() { 126 if(!m_win) return false; 127 m_session.show_window(m_win); 128 return true; 129 } 130 131 void win_render() { 132 if(!m_win) return; 133 if(!render(get_rgbas,false)) return; 134 put_buffer(m_win,m_ww,m_wh,tools::vec_data(m_out_buffer)); 135 m_out_buffer.clear(); 136 } 137 138 void set_device_interactor(tools::sg::device_interactor* a_interactor) {m_interactor = a_interactor;} 139 public: 140 tools::sg::device_interactor* device_interactor() {return m_interactor;} 141 protected: 142 base_session& m_session; 143 Window m_win; 144 tools::sg::device_interactor* m_interactor; 145 }; 146 147 }} 148 149 150 #endif 151