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 ) {}
00053 virtual void on_key_up(GLWindow& , int ) {}
00055 virtual void on_mouse_move(GLWindow& , ImageRef , int ) {}
00057 virtual void on_mouse_down(GLWindow& , ImageRef , int , int ) {}
00059 virtual void on_mouse_up(GLWindow& , ImageRef , int , int ) {}
00061 virtual void on_resize(GLWindow& , ImageRef ) {}
00063 virtual void on_event(GLWindow& , int ) {}
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
00100 GLWindow(const ImageRef& size, int bpp=24, const std::string& title="GLWindow", const std::string& display="") {
00101 init(size, bpp, title, display);
00102 }
00104 GLWindow(const ImageRef& size, const std::string& title, int bpp=24, const std::string& display="") {
00105 init(size, bpp, title, display);
00106 }
00107
00108 ~GLWindow();
00110 ImageRef size() const;
00112 void set_size(const ImageRef &);
00114 ImageRef position() const;
00116 void set_position(const ImageRef &);
00118 void set_cursor_position(const ImageRef& where);
00120 ImageRef cursor_position() const;
00122 void show_cursor(bool show=true);
00124 void hide_cursor() { show_cursor(false); }
00126 std::string title() const;
00128 void set_title(const std::string& title);
00130 void swap_buffers();
00132 void handle_events(EventHandler& handler);
00134 void get_events(std::vector<Event>& events);
00136 void get_events(EventSummary& summary);
00138 bool has_events() const;
00140 void activate();
00142 void make_current() { activate(); }
00143
00144 struct State;
00145 private:
00146 State* state;
00147 void init(const ImageRef& size, int bpp, const std::string& title, const std::string& display);
00148 };
00149
00150
00151 }
00152
00153
00154 #endif