Warning, /include/Geant4/toolx/Xt/zb_viewer is written in an unsupported language. File is not indexed.
0001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
0002 // See the file tools.license for terms.
0003
0004 #ifndef toolx_Xt_zb_viewer
0005 #define toolx_Xt_zb_viewer
0006
0007 #include "session"
0008 #include "ImageArea"
0009 #include "../X11/pixwin"
0010
0011 #include <X11/Shell.h>
0012 #include <X11/StringDefs.h>
0013 #include <X11/IntrinsicP.h>
0014
0015 #include <tools/sg/zb_viewer>
0016 #include <tools/num2s>
0017 #include <tools/sg/device_interactor>
0018 #include <tools/sg/keys>
0019
0020 namespace toolx {
0021 namespace Xt {
0022
0023 class zb_viewer: public tools::sg::zb_viewer {
0024 typedef tools::sg::zb_viewer parent;
0025 public:
0026 zb_viewer(session& a_session,
0027 int a_x = 0,int a_y = 0,
0028 unsigned int a_width = 500,unsigned int a_height = 500,
0029 const std::string& a_win_title = "")
0030 :parent(a_session.out(),a_width,a_height)
0031 ,m_session(a_session)
0032 ,m_shell(0)
0033 ,m_image_area(0)
0034 ,m_pixwin(0)
0035 ,m_interactor(0)
0036 {
0037 Widget app_widget = a_session.get_app_widget();
0038 if(!app_widget) return;
0039
0040 std::string sgeom;
0041 tools::numas(a_width,sgeom);
0042 sgeom += "x";
0043 tools::numas(a_height,sgeom);
0044 sgeom += "+";
0045 tools::numas(a_x,sgeom);
0046 sgeom += "+";
0047 tools::numas(a_y,sgeom);
0048
0049 Arg args[2];
0050 XtSetArg(args[0],XtNgeometry,XtNewString(sgeom.c_str()));
0051 XtSetArg(args[1],XtNborderWidth,0);
0052
0053 m_shell = ::XtAppCreateShell((char*)a_win_title.c_str(),
0054 (char*)"zb_viewer_shell",
0055 topLevelShellWidgetClass,XtDisplay(app_widget),args,2);
0056 ::XtSetMappedWhenManaged(m_shell,True);
0057
0058 m_image_area = ::XtCreateManagedWidget("imagearea",ImageArea::widget_class(),m_shell,args,0);
0059 ::XtAddCallback(m_image_area,ImageArea::XoN_resizeCallback(),resize_cbk,(XtPointer)this);
0060 ::XtAddCallback(m_image_area,ImageArea::XoN_paintCallback(),paint_cbk,(XtPointer)this);
0061 ::XtAddCallback(m_image_area,ImageArea::XoN_eventCallback(),event_cbk,(XtPointer)this);
0062
0063 ::XtAddCallback(m_shell,XtNdestroyCallback,destroy_shell_callback,(XtPointer)this);
0064
0065 ::XtRealizeWidget(m_shell);
0066
0067 {int iscreen;
0068 Screen* screen = XtScreen(m_image_area);
0069 iscreen = XScreenNumberOfScreen(screen);
0070 m_pixwin = new toolx::X11::pixwin(a_session.out(),iscreen,XtDisplay(app_widget));
0071 m_pixwin->set_size(m_image_area->core.width,m_image_area->core.height);}
0072 }
0073 virtual ~zb_viewer() {
0074 delete m_pixwin;
0075 if(m_shell) {
0076 ::XtRemoveCallback(m_image_area,ImageArea::XoN_paintCallback(),paint_cbk,(XtPointer)this);
0077 ::XtRemoveCallback(m_shell,XtNdestroyCallback,destroy_shell_callback,(XtPointer)this);
0078 ::XtDestroyWidget(m_shell);
0079 }
0080 m_shell = 0;
0081 m_image_area = 0;
0082 }
0083 protected:
0084 zb_viewer(const zb_viewer& a_from)
0085 :parent(a_from)
0086 ,m_session(a_from.m_session)
0087 ,m_shell(0)
0088 ,m_image_area(0)
0089 ,m_interactor(0)
0090 {}
0091 zb_viewer& operator=(const zb_viewer& a_from){
0092 parent::operator=(a_from);
0093 return *this;
0094 }
0095 public:
0096 bool has_window() const {return m_image_area?true:false;}
0097
0098 bool show() {
0099 if(!m_shell) return false;
0100 ::XtRealizeWidget(m_shell);
0101 ::XtMapWidget(m_shell);
0102 // Raise window :
0103 if(XtIsWidget(m_shell) && XtIsRealized(m_shell) ) {
0104 Display* display = XtDisplay(m_shell);
0105 Atom atom = ::XInternAtom(display,"WM_DELETE_WINDOW",False);
0106 ::XSetWMProtocols(display,XtWindow(m_shell),&atom,1);
0107 ::XRaiseWindow(display,XtWindow(m_shell));
0108 }
0109 return true;
0110 }
0111
0112 void win_render() {
0113 if(!m_image_area) return;
0114 ImageArea::paint(m_image_area);
0115 m_session.sync();
0116 }
0117
0118 void emit_win_render() {
0119 if(!m_image_area) return;
0120 if(!post_expose(m_image_area)) {}
0121 }
0122
0123 bool window_size(unsigned int& a_w,unsigned int& a_h) {
0124 if(!m_image_area) {a_w = 0;a_h = 0;return false;}
0125 a_w = m_image_area->core.width;
0126 a_h = m_image_area->core.height;
0127 return true;
0128 }
0129 void render_area_size(unsigned int& a_w,unsigned int& a_h) {
0130 a_w = parent::width();
0131 a_h = parent::height();
0132 }
0133
0134 void set_device_interactor(tools::sg::device_interactor* a_interactor) {m_interactor = a_interactor;} //we do not have ownership.
0135 protected:
0136 static void resize_cbk(Widget a_widget,XtPointer a_tag,XtPointer){
0137 if(!XtIsRealized(a_widget)) return;
0138 unsigned int width = a_widget->core.width;
0139 unsigned int height = a_widget->core.height;
0140 zb_viewer* _this = (zb_viewer*)a_tag;
0141 _this->set_size(width,height);
0142 _this->m_pixwin->set_size(width,height);
0143 }
0144 static void paint_cbk(Widget a_widget,XtPointer a_tag,XtPointer){
0145 if(!XtIsRealized(a_widget)) return;
0146 unsigned int width = a_widget->core.width;
0147 unsigned int height = a_widget->core.height;
0148 zb_viewer* _this = (zb_viewer*)a_tag;
0149 if(!_this->render(tools::sg::zb_viewer::get_rgbas,false)) return; //rgbas because Qt wants an image 32 bits aligned.
0150 _this->m_pixwin->put_buffer(XtWindow(a_widget),width,height,_this->out_buffer().data());
0151 _this->out_buffer_clear();
0152 }
0153 static void event_cbk(Widget,XtPointer a_tag,XtPointer a_data){
0154 zb_viewer* _this = (zb_viewer*)a_tag;
0155 if(!_this->m_interactor) return;
0156 ImageArea::XoAnyCallbackStruct* data = (ImageArea::XoAnyCallbackStruct*)a_data;
0157 XEvent* xevent = data->event;
0158 switch( xevent->type ) {
0159 case KeyPress:{
0160 KeySym key_sym;
0161 ::XLookupString(&(xevent->xkey),NULL,0,&key_sym,NULL);
0162 tools::sg::key_down_event event(_this->convert_key(key_sym));
0163 _this->m_interactor->key_press(event);
0164 }return;
0165 case KeyRelease:{
0166 KeySym key_sym;
0167 ::XLookupString(&(xevent->xkey),NULL,0,&key_sym,NULL);
0168 tools::sg::key_up_event event(_this->convert_key(key_sym));
0169 _this->m_interactor->key_release(event);
0170 }return;
0171 case ButtonPress:{
0172 bool shift_modifier = xevent->xkey.state & ShiftMask;
0173 bool control_modifier = xevent->xkey.state & ControlMask;
0174 if(xevent->xbutton.button==Button4) { //4=wheel down, or move down double touch on trackpad = zoom in.
0175 tools::sg::wheel_rotate_event event(8,xevent->xbutton.x,xevent->xbutton.y,shift_modifier,control_modifier); //8=cooking.
0176 _this->m_interactor->wheel_rotate(event);
0177 } else if(xevent->xbutton.button==Button5) { //5=wheel up, or move up double touch on trackpad = zoom out.
0178 tools::sg::wheel_rotate_event event(-8,xevent->xbutton.x,xevent->xbutton.y,shift_modifier,control_modifier); //8=cooking.
0179 _this->m_interactor->wheel_rotate(event);
0180 } else if(xevent->xbutton.button==Button1) {
0181 tools::sg::mouse_down_event event(xevent->xbutton.x,xevent->xbutton.y,shift_modifier,control_modifier);
0182 _this->m_interactor->mouse_press(event);
0183 }
0184 }return;
0185 case ButtonRelease:{
0186 if(xevent->xbutton.button==Button1) {
0187 bool shift_modifier = xevent->xkey.state & ShiftMask;
0188 bool control_modifier = xevent->xkey.state & ControlMask;
0189 tools::sg::mouse_up_event event(xevent->xbutton.x,xevent->xbutton.y,shift_modifier,control_modifier);
0190 _this->m_interactor->mouse_release(event);
0191 }
0192 }return;
0193 case MotionNotify:{
0194 if((xevent->xmotion.state & Button1MotionMask)==Button1MotionMask) {
0195 bool shift_modifier = xevent->xkey.state & ShiftMask;
0196 bool control_modifier = xevent->xkey.state & ControlMask;
0197 tools::sg::mouse_move_event event(xevent->xmotion.x,xevent->xmotion.y,shift_modifier,control_modifier,0,0,false);
0198 _this->m_interactor->mouse_move(event);
0199 }
0200 }return;
0201 default:return;}
0202 }
0203 static void destroy_shell_callback(Widget,XtPointer a_tag,XtPointer) {
0204 zb_viewer* _this = (zb_viewer*)a_tag;
0205 _this->m_shell = 0;
0206 _this->m_image_area = 0;
0207 }
0208 protected:
0209 tools::key_code convert_key(KeySym a_key) {
0210 if(a_key==XK_Shift_L) return tools::sg::key_shift();
0211 if(a_key==XK_Shift_R) return tools::sg::key_shift();
0212 return (tools::key_code)a_key;
0213 }
0214 protected:
0215 session& m_session;
0216 Widget m_shell;
0217 Widget m_image_area;
0218 toolx::X11::pixwin* m_pixwin;
0219 tools::sg::device_interactor* m_interactor;
0220 };
0221
0222 }}
0223
0224
0225 #endif
0226