Geant4 Cross Reference |
1 // Copyright (C) 2010, Guy Barrand. All rights 1 // Copyright (C) 2010, Guy Barrand. All rights reserved. 2 // See the file tools.license for terms. 2 // See the file tools.license for terms. 3 3 4 #ifndef toolx_X11_sg_viewer 4 #ifndef toolx_X11_sg_viewer 5 #define toolx_X11_sg_viewer 5 #define toolx_X11_sg_viewer 6 6 7 // not pure X11 version. The class inherits to 7 // not pure X11 version. The class inherits tools::sg::viewer that uses GL. 8 8 9 #include "session" 9 #include "session" 10 10 11 #include "../sg/GL_viewer" 11 #include "../sg/GL_viewer" 12 12 13 #include "simple_dispatcher" 13 #include "simple_dispatcher" 14 14 15 namespace tools{namespace sg{class device_inte 15 namespace tools{namespace sg{class device_interactor;}} 16 16 17 namespace toolx { 17 namespace toolx { 18 namespace X11 { 18 namespace X11 { 19 19 20 class sg_viewer : public sg::GL_viewer { 20 class sg_viewer : public sg::GL_viewer { 21 typedef sg::GL_viewer parent; 21 typedef sg::GL_viewer parent; 22 private: 22 private: 23 class dispatcher : public simple_dispatcher 23 class dispatcher : public simple_dispatcher { 24 typedef simple_dispatcher parent; 24 typedef simple_dispatcher parent; 25 public: 25 public: 26 virtual void win_render() {m_sg_viewer.win 26 virtual void win_render() {m_sg_viewer.win_render();} 27 virtual void set_size(unsigned int a_width 27 virtual void set_size(unsigned int a_width,unsigned int a_height) {m_sg_viewer.set_size(a_width,a_height);} 28 virtual dispatcher* copy() const {return n 28 virtual dispatcher* copy() const {return new dispatcher(*this);} 29 public: 29 public: 30 dispatcher(sg_viewer& a_sg_viewer) 30 dispatcher(sg_viewer& a_sg_viewer) 31 :parent(a_sg_viewer.m_session,a_sg_viewer. 31 :parent(a_sg_viewer.m_session,a_sg_viewer.m_win) 32 ,m_sg_viewer(a_sg_viewer){} 32 ,m_sg_viewer(a_sg_viewer){} 33 virtual ~dispatcher(){} 33 virtual ~dispatcher(){} 34 protected: 34 protected: 35 dispatcher(const dispatcher& a_from) 35 dispatcher(const dispatcher& a_from) 36 :parent(a_from) 36 :parent(a_from) 37 ,m_sg_viewer(a_from.m_sg_viewer) 37 ,m_sg_viewer(a_from.m_sg_viewer) 38 {} 38 {} 39 dispatcher& operator=(const dispatcher& a_ 39 dispatcher& operator=(const dispatcher& a_from) { 40 parent::operator=(a_from); 40 parent::operator=(a_from); 41 return *this; 41 return *this; 42 } 42 } 43 protected: 43 protected: 44 sg_viewer& m_sg_viewer; 44 sg_viewer& m_sg_viewer; 45 }; 45 }; 46 46 47 public: 47 public: 48 sg_viewer(session& a_session, 48 sg_viewer(session& a_session, 49 int a_x = 0,int a_y = 0, 49 int a_x = 0,int a_y = 0, 50 unsigned int a_width = 500,unsigne 50 unsigned int a_width = 500,unsigned int a_height = 500, 51 const std::string& a_win_title = " 51 const std::string& a_win_title = "") 52 :parent(a_session.out(),a_width,a_height) 52 :parent(a_session.out(),a_width,a_height) 53 ,m_session(a_session) 53 ,m_session(a_session) 54 ,m_win(0) 54 ,m_win(0) 55 { 55 { 56 if(!m_session.display()) return; //throw 56 if(!m_session.display()) return; //throw 57 m_win = m_session.create_window(a_win_titl 57 m_win = m_session.create_window(a_win_title.c_str(),a_x,a_y,a_width,a_height); 58 if(!m_win) return; //throw 58 if(!m_win) return; //throw 59 m_session.add_dispatcher(new dispatcher(*t 59 m_session.add_dispatcher(new dispatcher(*this)); 60 } 60 } 61 virtual ~sg_viewer() { 61 virtual ~sg_viewer() { 62 if(m_win) { 62 if(m_win) { 63 m_session.remove_dispatchers_with_window 63 m_session.remove_dispatchers_with_window(m_win); 64 m_session.delete_window(m_win); 64 m_session.delete_window(m_win); 65 m_session.sync(); 65 m_session.sync(); 66 } 66 } 67 } 67 } 68 public: 68 public: 69 sg_viewer(const sg_viewer& a_from) 69 sg_viewer(const sg_viewer& a_from) 70 :parent(a_from) 70 :parent(a_from) 71 ,m_session(a_from.m_session) 71 ,m_session(a_from.m_session) 72 ,m_win(a_from.m_win) 72 ,m_win(a_from.m_win) 73 {} 73 {} 74 sg_viewer& operator=(const sg_viewer& a_from 74 sg_viewer& operator=(const sg_viewer& a_from){ 75 parent::operator=(a_from); 75 parent::operator=(a_from); 76 m_win = a_from.m_win; 76 m_win = a_from.m_win; 77 return *this; 77 return *this; 78 } 78 } 79 public: 79 public: 80 bool has_window() const {return m_win?true:f 80 bool has_window() const {return m_win?true:false;} //for SWIG 81 81 82 Window window() const {return m_win;} 82 Window window() const {return m_win;} 83 83 84 bool show() { 84 bool show() { 85 if(!m_win) return false; 85 if(!m_win) return false; 86 m_session.show_window(m_win); 86 m_session.show_window(m_win); 87 return true; 87 return true; 88 } 88 } 89 89 90 void win_render() { 90 void win_render() { 91 if(!m_win) return; 91 if(!m_win) return; 92 if(::glXMakeCurrent(m_session.display(),m_ 92 if(::glXMakeCurrent(m_session.display(),m_win,m_session.context())==False){ 93 m_session.out() << "toolx::X11::sg_viewe 93 m_session.out() << "toolx::X11::sg_viewer::win_render : glXMakeCurrent failed." << std::endl; 94 return; 94 return; 95 } 95 } 96 render(); //viewer::render() 96 render(); //viewer::render() 97 ::glXSwapBuffers(m_session.display(),m_win 97 ::glXSwapBuffers(m_session.display(),m_win); 98 if(::glXMakeCurrent(m_session.display(),No 98 if(::glXMakeCurrent(m_session.display(),None,NULL)==False){ 99 m_session.out() << "toolx::X11::sg_viewe 99 m_session.out() << "toolx::X11::sg_viewer::win_render : glXMakeCurrent(None,NULL) failed." << std::endl; 100 } 100 } 101 } 101 } 102 102 103 public: 103 public: 104 void set_device_interactor(tools::sg::device 104 void set_device_interactor(tools::sg::device_interactor*) {} 105 protected: 105 protected: 106 session& m_session; 106 session& m_session; 107 Window m_win; 107 Window m_win; 108 }; 108 }; 109 109 110 }} 110 }} 111 111 112 #endif 112 #endif 113 113