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