core/vgui/impl/win32/vgui_win32_adaptor.h
Go to the documentation of this file.
00001 // This is core/vgui/impl/win32/vgui_win32_adaptor.h
00002 #ifndef vgui_win32_adaptor_h_
00003 #define vgui_win32_adaptor_h_
00004 //:
00005 // \file
00006 // \brief The Win32 Application Programming Interfaces (API) implementation of vgui_adaptor.
00007 // \author Lianqing Yu
00008 // \date July 29, 2009              Initial version
00009 //
00010 // \todo TODO issues:
00011 // 1. idle_slot.
00012 // 2. Should the timer be created in the constructor, in case post_timer is
00013 //    never called? Qt impl. does this but MFC impl. does not.
00014 // 3. How to implement post_message?
00015 
00016 #include <windows.h>
00017 
00018 #include <vgui/vgui_adaptor.h>
00019 #include <vgui/internals/vgui_adaptor_mixin.h>
00020 #include <vgui/impl/win32/vgui_win32_cmdtarget.h>
00021 #include <vcl_map.h>
00022 
00023 class vgui_win32_window;
00024 class vgui_win32_internal_timer;
00025 
00026 class vgui_win32_adaptor : public vgui_adaptor, public vgui_adaptor_mixin,
00027                            public vgui_win32_cmdtarget
00028 {
00029  public:
00030   typedef vgui_adaptor_mixin mixin;
00031 
00032   vgui_win32_adaptor(HWND hwnd, vgui_window *win = 0);
00033   ~vgui_win32_adaptor();
00034 
00035   // Set the size of rendering area. This is convinient for tableaus that
00036   // cannot receive WM_SIZE message.
00037   void set_width(unsigned w) { width = w; }
00038   void set_height(unsigned h) { height = h; }
00039 
00040   // Return width of rendering area, NOT the width of the viewport.
00041   virtual unsigned get_width() const { return width; }
00042 
00043   // Return height of rendering area, NOT the height of the viewport.
00044   virtual unsigned get_height() const { return height; }
00045 
00046   // These methods are called by vgui_adaptor (in its capacity as a base class)
00047   // when a post arrives.
00048 
00049   // Create a timer (id) to dispatch WM_TIMER event every tm milliseconds.
00050   virtual void post_timer(float tm, int id);
00051   // Redraw the rendering area.
00052   virtual void post_redraw();
00053   // Redraw the overlay buffer
00054   virtual void post_overlay_redraw();
00055   virtual void post_idle_request();
00056   //virtual void post_message(char const *, void const *);
00057   virtual void post_destroy();
00058 
00059   // kill an existing timer
00060   virtual void kill_timer(int timer);
00061 
00062   // Bind the given modifier/button combination to the popup menu.
00063   virtual void bind_popups(vgui_modifier m, vgui_button b)
00064   { mixin::popup_modifier = m; mixin::popup_button = b; }
00065 
00066   // Return the modifier/button which pops up the popup menu.
00067   virtual void get_popup_bindings(vgui_modifier &m, vgui_button &b) const
00068   { m = mixin::popup_modifier; b = mixin::popup_button; }
00069 
00070 
00071   // getting the window.
00072   virtual vgui_window *get_window() const { return win_; } 
00073 
00074   // various buffer behaviour.
00075   virtual void swap_buffers() { SwapBuffers(hdc_); }
00076   virtual void make_current() { wglMakeCurrent(hdc_, hglrc_); }
00077 
00078   // Message handling function
00079   virtual BOOL OnCmdMsg(UINT message, WPARAM wParam, LPARAM lParam);
00080   // Called within message processing loop.
00081   void menu_dispatcher(int menuId);
00082 
00083   // Message callback functions
00084   void OnCreate();
00085   //void OnDestroy();
00086   void OnSize(WPARAM wParam, LPARAM lParam);
00087   void OnPaint();
00088   void OnTimer(WPARAM wParam, LPARAM lParam);
00089   void OnHScroll(UINT message, WPARAM wParam, LPARAM lParam);
00090   void OnVScroll(UINT message, WPARAM wParam, LPARAM lParam);
00091   void OnKeyDown(WPARAM wParam, LPARAM lParam);
00092   void OnKeyUp(WPARAM wParam, LPARAM lParam);
00093   //void OnChar();
00094   void OnLButtonDown(WPARAM wParam, LPARAM lParam);
00095   void OnLButtonUp(WPARAM wParam, LPARAM lParam);
00096   void OnMButtonDown(WPARAM wParam, LPARAM lParam);
00097   void OnMButtonUp(WPARAM wParam, LPARAM lParam);
00098   void OnRButtonDown(WPARAM wParam, LPARAM lParam);
00099   void OnRButtonUp(WPARAM wParam, LPARAM lParam);
00100   void OnMouseMove(WPARAM wParam, LPARAM lParam);
00101   void OnMouseWheel(WPARAM wParam, LPARAM lParam);
00102 
00103  protected:
00104   // Translate a win32 message into the corresponding VGUI event.
00105   vgui_event translate_message(WPARAM wParam, LPARAM lParam, 
00106                                vgui_event_type evtype = vgui_EVENT_NULL);
00107   // Translate a win32 key into the corresponding VGUI key
00108   void translate_key(UINT nChar, UINT nFlags, int *key, int *ascii_char);
00109   // Handle mouse event
00110   void domouse(vgui_event_type t, vgui_button b, UINT nFlags, int x, int y);
00111 
00112   HWND   hwnd_;  // main window handle
00113   HGLRC  hglrc_; // OpenGL rendering context
00114   HDC    hdc_;   // device context 
00115   int    tid_;   // timer identifier
00116 
00117   vgui_window *win_; // the window that contains this adaptor
00118 
00119   // map of timers currently in use.
00120   vcl_map<unsigned int, vgui_win32_internal_timer> timers_;
00121 
00122   static vgui_menu last_popup;
00123   vcl_vector<vgui_command_sptr> popup_callbacks; // commands called by popup menu items
00124 
00125  private:
00126   HGLRC setup_for_gl(HDC);
00127 
00128   // True while a redraw event has been requested but not implemented.
00129   bool redraw_posted_;
00130 
00131   // True while a overlay redraw event has been requested but not implemented.
00132   bool overlay_redraw_posted_;
00133 
00134   // True while an idle time has been requested but not implemented.
00135   bool idle_request_posted_;
00136 
00137   DECLARE_MESSAGE_MAP()
00138 };
00139 
00140 class vgui_win32_internal_timer
00141 {
00142  public:
00143   vgui_win32_internal_timer() : timer_id(0), callback_ptr(0) {}
00144   vgui_win32_internal_timer(unsigned int id, void *p) 
00145   : timer_id(id), callback_ptr(p) {}
00146 
00147   unsigned int timer_id;
00148   void* callback_ptr;
00149 };
00150 
00151 #endif // vgui_win32_adaptor_h_