Back to home page

EIC code displayed by LXR

 
 

    


Warning, /include/Geant4/toolx/X11/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_X11_zb_viewer
0005 #define toolx_X11_zb_viewer
0006 
0007 #include "base_session"
0008 #include "pixwin"
0009 #include "simple_dispatcher"
0010 
0011 #include <tools/sg/zb_viewer>
0012 #include <tools/sg/device_interactor>
0013 
0014 namespace toolx {
0015 namespace X11 {
0016 
0017 class zb_viewer
0018 :public tools::sg::zb_viewer, protected pixwin {
0019   typedef tools::sg::zb_viewer parent;
0020   typedef pixwin parent_pixwin;
0021 private:
0022   class dispatcher : public simple_dispatcher {
0023     typedef simple_dispatcher parent;
0024   public:
0025     virtual bool dispatch(XEvent& a_event) {
0026       if(parent::dispatch(a_event)) return true;
0027       bool shift_modifier = a_event.xkey.state & ShiftMask;
0028       bool control_modifier = a_event.xkey.state & ControlMask;
0029       if(a_event.type==ButtonPress && a_event.xbutton.button==1) {
0030         if(!m_viewer.device_interactor()) return false;
0031         tools::sg::mouse_down_event event(a_event.xbutton.x,a_event.xbutton.y,shift_modifier,control_modifier);
0032         m_viewer.device_interactor()->mouse_press(event);
0033         return true;
0034       } else if(a_event.type==ButtonRelease && a_event.xbutton.button==1) {
0035         if(!m_viewer.device_interactor()) return false;
0036         tools::sg::mouse_up_event event(a_event.xbutton.x,a_event.xbutton.y,shift_modifier,control_modifier);
0037         m_viewer.device_interactor()->mouse_release(event);
0038         return true;
0039       } else if(a_event.type==MotionNotify) {
0040         if(!m_viewer.device_interactor()) return false;
0041         if((a_event.xmotion.state & Button1MotionMask)==Button1MotionMask) {
0042           tools::sg::mouse_move_event event(a_event.xmotion.x,a_event.xmotion.y,shift_modifier,control_modifier,0,0,false);
0043           m_viewer.device_interactor()->mouse_move(event);
0044         }
0045       } else if((a_event.type==ButtonPress)&&(a_event.xbutton.button==4)) { // mouse scrollwheel down :
0046         if(!m_viewer.device_interactor()) return false;
0047         tools::sg::wheel_rotate_event event(8,a_event.xbutton.x,a_event.xbutton.y,shift_modifier,control_modifier);  //8=cooking.
0048         m_viewer.device_interactor()->wheel_rotate(event);
0049         return true;
0050       } else if((a_event.type==ButtonPress)&&(a_event.xbutton.button==5)) { // mouse scrollwheel up :
0051         if(!m_viewer.device_interactor()) return false;
0052         tools::sg::wheel_rotate_event event(-8,a_event.xbutton.x,a_event.xbutton.y,shift_modifier,control_modifier);  //8=cooking.
0053         m_viewer.device_interactor()->wheel_rotate(event);
0054         return true;
0055       }
0056       return false;
0057     }
0058     virtual void win_render() {m_viewer.win_render();}
0059     virtual void set_size(unsigned int a_width,unsigned int a_height) {m_viewer.set_size(a_width,a_height);}
0060     virtual dispatcher* copy() const {return new dispatcher(*this);}
0061   public:
0062     dispatcher(zb_viewer& a_viewer)
0063     :parent(a_viewer.m_session,a_viewer.m_win)
0064     ,m_viewer(a_viewer){}
0065     virtual ~dispatcher(){}
0066   protected:
0067     dispatcher(const dispatcher& a_from)
0068     :parent(a_from)
0069     ,m_viewer(a_from.m_viewer)
0070     {}
0071     dispatcher& operator=(const dispatcher& a_from) {
0072       parent::operator=(a_from);
0073       return *this;
0074     }
0075   protected:
0076     zb_viewer& m_viewer;
0077   };
0078 
0079 public:
0080   virtual void set_size(unsigned int a_w,unsigned int a_h) {
0081     parent::set_size(a_w,a_h);
0082     free_XImage();
0083     alloc_XImage(a_w,a_h);
0084   }
0085 public:
0086   zb_viewer(base_session& a_session,
0087             int a_x = 0,int a_y = 0,
0088             unsigned int a_width = 500,unsigned int a_height = 500,
0089             const std::string& a_win_title = "")
0090   :parent(a_session.out(),a_width,a_height)
0091   ,parent_pixwin(a_session.out(),a_session.monitor(),a_session.display())
0092   ,m_session(a_session)
0093   ,m_win(0)
0094   ,m_interactor(0)
0095   {
0096     if(!m_session.display()) return; //throw
0097     m_win = m_session.create_window(a_win_title.c_str(),a_x,a_y,a_width,a_height);
0098     if(!m_win) return; //throw
0099     m_session.add_dispatcher(new dispatcher(*this));
0100   }
0101   virtual ~zb_viewer() {
0102     if(m_win) {
0103       m_session.remove_dispatchers_with_window(m_win);
0104       m_session.delete_window(m_win);
0105       m_session.sync();
0106     }
0107   }
0108 protected:
0109   zb_viewer(const zb_viewer& a_from)
0110   :parent(a_from)
0111   ,parent_pixwin(a_from)
0112   ,m_session(a_from.m_session)
0113   ,m_win(a_from.m_win)
0114   ,m_interactor(0)
0115   {}
0116   zb_viewer& operator=(const zb_viewer& a_from){
0117     parent::operator=(a_from);
0118     parent_pixwin::operator=(a_from);
0119     m_win = a_from.m_win;
0120     return *this;
0121   }
0122 public:
0123   bool has_window() const {return m_win?true:false;} //for SWIG
0124 
0125   Window window() const {return m_win;}
0126 
0127   bool show() {
0128     if(!m_win) return false;
0129     m_session.show_window(m_win);
0130     return true;
0131   }
0132 
0133   void win_render() {
0134     if(!m_win) return;
0135     if(!render(get_rgbas,false)) return;
0136     put_buffer(m_win,m_ww,m_wh,m_out_buffer.data());
0137     m_out_buffer.clear();
0138     m_session.sync();
0139   }
0140 
0141   void emit_win_render() {
0142     if(!m_win) return;
0143     m_session.post_expose(m_win);
0144   }
0145 
0146   bool window_size(unsigned int& a_w,unsigned int& a_h) {
0147     if(!m_win) {a_w = 0;a_h = 0;return false;}
0148     int width,height;
0149     if(!m_session.window_size(m_win,width,height)) {a_w = 0;a_h = 0;return false;}
0150     a_w = (unsigned int)width;
0151     a_h = (unsigned int)height;
0152     return true;
0153   }
0154   void render_area_size(unsigned int& a_w,unsigned int& a_h) {
0155     a_w = parent::width();
0156     a_h = parent::height();
0157   }
0158 
0159   void set_device_interactor(tools::sg::device_interactor* a_interactor) {m_interactor = a_interactor;}
0160 public:
0161   tools::sg::device_interactor* device_interactor() {return m_interactor;}
0162 protected:
0163   base_session& m_session;
0164   Window m_win;  
0165   tools::sg::device_interactor* m_interactor;
0166 };
0167 
0168 }}
0169 
0170 
0171 #endif
0172