cvd/glwindow.h

00001 #ifndef GLWINDOW_H
00002 #define GLWINDOW_H
00003 
00004 #include <string>
00005 #include <vector>
00006 #include <map>
00007 #include <cvd/image_ref.h>
00008 
00009 namespace CVD {
00010 
00011     namespace Exceptions
00012     {
00015     namespace GLWindow
00016     {
00019         struct All: public CVD::Exceptions::All{
00020         };
00021 
00024         struct CreationError: public All
00025         {
00026         CreationError(std::string w); 
00027         };
00028 
00031         struct RuntimeError: public All
00032         {
00033         RuntimeError(std::string w); 
00034         };
00035     }
00036     }
00037 
00039     class GLWindow {
00040     public:
00042     enum MouseButton { BUTTON_LEFT=1, BUTTON_MIDDLE=2, BUTTON_RIGHT=4, BUTTON_MOD_CTRL=8, BUTTON_MOD_SHIFT=0x10, BUTTON_WHEEL_UP=0x20, BUTTON_WHEEL_DOWN=0x40 };
00044     enum EventType { EVENT_CLOSE, EVENT_EXPOSE };
00045 
00047     class EventHandler {
00048     public:
00049         virtual ~EventHandler() {}
00051         virtual void on_key_down(GLWindow&, int /*key*/) {}
00053         virtual void on_key_up(GLWindow& /*win*/, int /*key*/) {}
00055         virtual void on_mouse_move(GLWindow& /*win*/, ImageRef /*where*/, int /*state*/) {}
00057         virtual void on_mouse_down(GLWindow& /*win*/, ImageRef /*where*/, int /*state*/, int /*button*/) {}
00059         virtual void on_mouse_up(GLWindow& /*win*/, ImageRef /*where*/, int /*state*/, int /*button*/) {}
00061         virtual void on_resize(GLWindow& /*win*/, ImageRef /*size*/) {}
00063         virtual void on_event(GLWindow& /*win*/, int /*event*/) {}
00064     };
00065 
00066     struct Event {
00067         enum Type { KEY_DOWN, KEY_UP, MOUSE_MOVE, MOUSE_DOWN, MOUSE_UP, RESIZE, EVENT };
00068         Type type;
00069         int which, state;
00070         ImageRef where, size;
00071     };
00072 
00074     struct EventSummary {
00075         EventSummary() : cursor(-1,-1), cursor_moved(false) {}
00077         std::map<int,int> key_down, key_up;
00078         typedef std::map<int,int>::const_iterator key_iterator;
00080         std::map<int,std::pair<ImageRef,int> > mouse_down, mouse_up;
00081         typedef std::map<int,std::pair<ImageRef,int> >::const_iterator mouse_iterator;
00083         std::map<int,int> events;
00085         void clear() { *this = EventSummary(); }
00087         bool should_quit() const;
00089         ImageRef cursor;
00091         bool cursor_moved;
00092     };
00093 
00096     GLWindow(const ImageRef& size, int bpp=24, const std::string& title="GLWindow") {
00097       init(size, bpp, title);
00098     }
00099     GLWindow(const ImageRef& size, const std::string& title, int bpp=24) {
00100       init(size, bpp, title);
00101     }
00102 
00103     ~GLWindow();
00105     ImageRef size() const;
00107     void set_size(const ImageRef &);
00109     ImageRef position() const;
00111     void set_position(const ImageRef &);
00113     void set_cursor_position(const ImageRef& where);
00115     ImageRef cursor_position() const;
00117     void show_cursor(bool show=true);
00119     void hide_cursor() { show_cursor(false); }
00121     std::string title() const;
00123     void set_title(const std::string& title);
00125     void swap_buffers();
00127     void handle_events(EventHandler& handler);
00129     void get_events(std::vector<Event>& events);
00131     void get_events(EventSummary& summary);
00133     bool has_events() const;
00135     void activate();
00137     void make_current() { activate(); }
00138 
00139     struct State;
00140     private:
00141     State* state;
00142     void init(const ImageRef& size, int bpp, const std::string& title);
00143     };
00144 
00145 
00146 }
00147 
00148 
00149 #endif

Generated on Wed Feb 18 10:23:01 2009 for CVD by  doxygen 1.5.3