Geant4 Cross Reference |
1 // Copyright (C) 2010, Guy Barrand. All rights 2 // See the file tools.license for terms. 3 4 #ifndef toolx_Xt_sg_viewer 5 #define toolx_Xt_sg_viewer 6 7 #include "session" 8 9 #include "../sg/GL_viewer" 10 11 #include "OpenGLArea" 12 13 #include <X11/Shell.h> 14 #include <X11/StringDefs.h> 15 #include <X11/IntrinsicP.h> 16 17 #include <tools/num2s> 18 #include <tools/sg/device_interactor> 19 #include <tools/sg/keys> 20 21 namespace toolx { 22 namespace Xt { 23 24 class sg_viewer : public sg::GL_viewer { 25 typedef sg::GL_viewer parent; 26 public: 27 sg_viewer(session& a_session, 28 int a_x = 0,int a_y = 0, 29 unsigned int a_width = 500,unsigne 30 const std::string& a_win_title = " 31 :parent(a_session.out(),a_width,a_height) 32 ,m_session(a_session) 33 ,m_shell(0) 34 ,m_glarea(0) 35 ,m_interactor(0) 36 { 37 Widget app_widget = a_session.get_app_widg 38 if(!app_widget) return; 39 40 std::string sgeom; 41 tools::numas(a_width,sgeom); 42 sgeom += "x"; 43 tools::numas(a_height,sgeom); 44 sgeom += "+"; 45 tools::numas(a_x,sgeom); 46 sgeom += "+"; 47 tools::numas(a_y,sgeom); 48 49 Arg args[2]; 50 XtSetArg(args[0],XtNgeometry,XtNewString(s 51 XtSetArg(args[1],XtNborderWidth,0); 52 53 m_shell = ::XtAppCreateShell((char*)a_win_ 54 (char*)"sg_vi 55 topLevelShell 56 ::XtSetMappedWhenManaged(m_shell,True); 57 58 m_glarea = ::XtCreateManagedWidget("glarea 59 ::XtAddCallback(m_glarea,XoNpaintCallback, 60 ::XtAddCallback(m_glarea,XoNeventCallback, 61 62 ::XtAddCallback(m_shell,XtNdestroyCallback 63 64 ::XtRealizeWidget(m_shell); 65 66 //Atom WM_DELETE_WINDOW_atom = ::XInternAtom 67 //::XSetWMProtocols(XtDisplay(m_shell),XtWin 68 } 69 virtual ~sg_viewer() { 70 if(m_shell) { 71 ::XtRemoveCallback(m_glarea,XoNpaintCall 72 ::XtRemoveCallback(m_shell,XtNdestroyCal 73 ::XtDestroyWidget(m_shell); 74 } 75 m_shell = 0; 76 m_glarea = 0; 77 } 78 protected: 79 sg_viewer(const sg_viewer& a_from) 80 :parent(a_from) 81 ,m_session(a_from.m_session) 82 ,m_shell(0) 83 ,m_glarea(0) 84 ,m_interactor(0) 85 {} 86 sg_viewer& operator=(const sg_viewer& a_from 87 parent::operator=(a_from); 88 return *this; 89 } 90 public: 91 bool has_window() const {return m_glarea?tru 92 93 bool show() { 94 if(!m_shell) return false; 95 ::XtRealizeWidget(m_shell); 96 ::XtMapWidget(m_shell); 97 // Raise window : 98 if(XtIsWidget(m_shell) && XtIsRealized(m_s 99 Display* display = XtDisplay(m_shell); 100 Atom atom = ::XInternAtom(display,"WM_DE 101 ::XSetWMProtocols(display,XtWindow(m_she 102 ::XRaiseWindow(display,XtWindow(m_shell) 103 } 104 return true; 105 } 106 107 void win_render() { 108 if(m_glarea) OpenGLArea::paint(m_glarea); 109 } 110 111 public: 112 void set_device_interactor(tools::sg::device 113 protected: 114 static void paint_cbk(Widget a_widget,XtPoin 115 unsigned int width = a_widget->core.width; 116 unsigned int height = a_widget->core.heigh 117 //::printf("debug : toolx::Xt::sg_viewer:: 118 sg_viewer* _this = (sg_viewer*)a_tag; 119 _this->set_size(width,height); 120 _this->render(); 121 } 122 static void event_cbk(Widget,XtPointer a_tag 123 sg_viewer* _this = (sg_viewer*)a_tag; 124 if(!_this->m_interactor) return; 125 XoAnyCallbackStruct* data = (XoAnyCallback 126 XEvent* xevent = data->event; 127 switch( xevent->type ) { 128 case KeyPress:{ 129 KeySym key_sym; 130 ::XLookupString(&(xevent->xkey),NULL,0,& 131 tools::sg::key_down_event event(_this->c 132 _this->m_interactor->key_press(event); 133 }return; 134 case KeyRelease:{ 135 KeySym key_sym; 136 ::XLookupString(&(xevent->xkey),NULL,0,& 137 tools::sg::key_up_event event(_this->con 138 _this->m_interactor->key_release(event); 139 }return; 140 case ButtonPress:{ 141 if(xevent->xbutton.button==Button4) { / 142 tools::sg::wheel_rotate_event event(8) 143 _this->m_interactor->wheel_rotate(even 144 } else if(xevent->xbutton.button==Button 145 tools::sg::wheel_rotate_event event(-8 146 _this->m_interactor->wheel_rotate(even 147 } else { 148 tools::sg::mouse_down_event event(xeve 149 _this->m_interactor->mouse_press(event 150 } 151 }return; 152 case ButtonRelease:{ 153 tools::sg::mouse_up_event event(xevent-> 154 _this->m_interactor->mouse_release(event 155 }return; 156 case MotionNotify:{ 157 tools::sg::mouse_move_event event(xevent 158 _this->m_interactor->mouse_move(event); 159 }return; 160 default:return;} 161 } 162 static void destroy_shell_callback(Widget,Xt 163 sg_viewer* _this = (sg_viewer*)a_tag; 164 _this->m_shell = 0; 165 _this->m_glarea = 0; 166 } 167 protected: 168 tools::key_code convert(KeySym a_key) { 169 if(a_key==XK_Shift_L) return tools::sg::ke 170 if(a_key==XK_Shift_R) return tools::sg::ke 171 return (tools::key_code)a_key; 172 } 173 protected: 174 session& m_session; 175 Widget m_shell; 176 Widget m_glarea; 177 tools::sg::device_interactor* m_interactor; 178 }; 179 180 }} 181 182 #endif 183