core/vgui/impl/mfc/vgui_mfc_adaptor.h
Go to the documentation of this file.
00001 // This is core/vgui/impl/mfc/vgui_mfc_adaptor.h
00002 #ifndef AFX_VGUI_MFC_ADAPTOR_H__6BE405F7_0910_4C83_A175_5E8FBDCE88A5__INCLUDED_
00003 #define AFX_VGUI_MFC_ADAPTOR_H__6BE405F7_0910_4C83_A175_5E8FBDCE88A5__INCLUDED_
00004 //:
00005 // \file
00006 // \brief  The MFC implementation of vgui_adaptor.
00007 // \author RRG, Oxford University
00008 //
00009 //  Contains class vgui_mfc_adaptor
00010 //
00011 // \verbatim
00012 //  Modifications
00013 //   13-08-2000  Marko Bacic, Oxford RRG - Fixed textures
00014 //   14-08-2000  FSM, Oxford RRG - Fixed double buffering/rubber banding issues
00015 //   14-08-2000  Marko Bacic, Oxford RRG - Added right popup menu
00016 //   30-08-2000  Marko Bacic, Oxford RRG - Support for Windows/MFC acceleration
00017 //   06-02-2001  AWF, Oxford RRG - Make acceleration work...
00018 //   02-03-2001  K.Y.McGaul - Add shift & ctrl modifiers to key press/release
00019 //                            events.
00020 //                          - Edited and added Doxygen comments.
00021 //   14-09-2001  K.Y.McGaul - Moved stuff in OnDestroy to destructor.
00022 //   18-09-2001  K.Y.McGaul - Major changes to allow for multiple adaptors in
00023 //                            an application.  In particular using
00024 //                            setup_adaptor we can now specify which window
00025 //                            our adaptor is in.  If you don't call
00026 //                            setup_adaptor it is assumed to be the main
00027 //                            window.
00028 //   07-07-2003  M.R.Johnson - Disabled MFC acceleration by default so that new
00029 //                             overlay code will work (which requires double
00030 //                             buffering). Overlays necessary to enable new
00031 //                             highlighting mechanism in vgui tableaux.
00032 //   07-21-2003  A.Perera   - Update the overlay mechanism for Windows, since copying
00033 //                            the frame buffer to memory is *slow*.
00034 // \endverbatim
00035 
00036 #if _MSC_VER > 1000
00037 #pragma once
00038 #endif // _MSC_VER > 1000
00039 
00040 #include <afxwin.h>
00041 #include <vgui/vgui_adaptor.h>
00042 #include <vgui/vgui_gl.h>
00043 #include <vgui/internals/vgui_adaptor_mixin.h>
00044 
00045 //: MFC implementation of vgui_adaptor.
00046 //
00047 //  The adaptor allows you to draw an OpenGL area in your MFC application.
00048 //
00049 //  If this adaptor is not being used inside the main window of the application
00050 //  then you will need to call setup_adaptor().  See vgui_mfc_dialog_impl where
00051 //  this is done, when an OpenGL area is used in a dialog box.
00052 class vgui_mfc_adaptor : public CView, public vgui_adaptor, public vgui_adaptor_mixin
00053 {
00054  public:
00055   //: Constructor used by dynamic creation.
00056   //  kym - this used to be protected, but I changed it to public so I can add
00057   //  adaptors to dialog boxes.
00058   vgui_mfc_adaptor();
00059   DECLARE_DYNCREATE(vgui_mfc_adaptor)
00060 
00061   //: Device context for this adaptor.
00062   CDC* m_pDC;
00063 
00064   HBITMAP m_pDC_default_bitmap;
00065 
00066   typedef vgui_adaptor_mixin mixin;
00067 
00068   // 0. vgui_adaptor implementations
00069   //--------------------------------
00070   //: MFC implementation from vgui_adaptor -  returns width of rendering area.
00071   unsigned int get_width() const { return m_width; }
00072 
00073   //: MFC implementation from vgui_adaptor - returns height of rendering area.
00074   unsigned int get_height() const { return m_height; }
00075 
00076   //: MFC implementation from vgui_adaptor - redraws rendering area.
00077   void post_redraw();
00078 
00079   //: MFC implementation from vgui_adaptor - redraws overlay buffer.
00080   void post_overlay_redraw();
00081 
00082   //: MFC impl. from vgui_adaptor - swap buffers if using double buffering.
00083   void swap_buffers();
00084 
00085   //: MFC impl. from vgui_adaptor - make this the current GL rendering context.
00086   void make_current();
00087 
00088   //: MFC implementation from vgui_adaptor  - redraw everything now.
00089   void draw();
00090 
00091   // Do any idle processing that needs to be done.
00092   // Return true if idle processing is not complete
00093   bool do_idle();
00094 
00095   // 0.5 fsm stuff
00096   //--------------
00097   //: Redraws the OpenGL area.
00098   void service_redraws();
00099 
00100   //: Change the default popup menu to the given one (not yet implemented).
00101   void set_default_popup(vgui_menu);
00102 
00103   //: Sets timer to dispatch WM_TIMER event to a mainframe every time milliseconds
00104   void post_timer(float,int);
00105 
00106   //: Stop timer
00107   void kill_timer(int id);
00108 
00109   //: Flags than a child requests idle processing
00110   void post_idle_request() { idle_request_posted_ = true; }
00111 
00112   //: Return the default popup menu (not yet implemented).
00113   vgui_menu get_popup();
00114 
00115   //: MFC impl. from vgui_adaptor - set button & modifier to display popup.
00116   void bind_popups(vgui_modifier m, vgui_button b) {
00117     mixin::popup_modifier = m;
00118     mixin::popup_button = b;
00119   }
00120 
00121   //: MFC impl. from vgui_adaptor - get button & modifier which display popup.
00122   void get_popup_bindings(vgui_modifier &m,vgui_button &b) const {
00123     m = mixin::popup_modifier;
00124     b = mixin::popup_button;
00125   }
00126 
00127   virtual vgui_window* get_window() const { return win_; }
00128   void set_window( vgui_window* win ) { win_ = win; }
00129 
00130   // kym stuff
00131   //----------
00132   //:
00133   //  If your adaptor is not in the main window of the application call this
00134   //  function. The first parameter tells it which window this adaptor is
00135   //  associated with, the other parameters are so we can go back to our old
00136   //  rendering context when this adaptor is deleted.
00137   void setup_adaptor(CWnd* this_cwnd, HDC OldDC, HGLRC oldContext);
00138 
00139 
00140  private:
00141   void create_bitmap( int cx, int cy,
00142                       CDC*& out_pDC,
00143                       HBITMAP& defaultBitmapForDC );
00144 
00145   HGLRC setup_for_gl( CDC* pDC, DWORD dwFlags );
00146 
00147   //: Device context for the auxiliary buffer used for simulating overlays
00148   CDC* m_pDC_aux;
00149   //: True if the aux buffer is a copy of the main GL buffer
00150   bool aux_dc_valid_;
00151 
00152   HBITMAP m_pDC_aux_default_bitmap;
00153 
00154   //: True while a redraw event has been requested but not implemented.
00155   bool redraw_posted_;
00156 
00157   //: True while an overlay redraw event has been requested but not implemented.
00158   bool overlay_redraw_posted_;
00159 
00160   //: True while an idle time has been requested but not implemented.
00161   bool idle_request_posted_;
00162 
00163  protected:
00164   //: The window associated with this adaptor if it is not the main window.
00165   CWnd* m_pCWnd;
00166 
00167   //: The previous device context to this one.
00168   HDC hOldDC;
00169 
00170   //: The previous rendering context to this one.
00171   HGLRC hOldRC;
00172 
00173   // 1. MFC stuff -  ClassWizard generated virtual function overrides
00174   //-------------
00175  protected:
00176   //: Called by MFC when a draw event is required - overridden to draw this view
00177   virtual void OnDraw(CDC* pDC);
00178 
00179   //: Called by MFC before the creation of the window attached to this object.
00180   virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
00181 
00182 // Implementation
00183  public:
00184   virtual ~vgui_mfc_adaptor();
00185  protected:
00186 #ifdef _DEBUG
00187   virtual void AssertValid() const;
00188   virtual void Dump(CDumpContext& dc) const;
00189 #endif
00190 
00191   // Generated message map functions
00192   //--------------------------------
00193   //: Handles mouse press/release events.
00194   void domouse(vgui_event_type e, UINT nFlags, CPoint point, vgui_button b);
00195 
00196   //: The Win-OpenGL resource context.
00197   HGLRC m_hRC;
00198 
00199   //: Width of rendering area.
00200   int m_width;
00201 
00202   //: Height of rendering area.
00203   int m_height;
00204 
00205   vgui_window* win_;
00206 
00207   static vgui_menu last_popup;
00208 
00209   //: Called by MFC when the application requests the creation of a window.
00210   afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
00211 
00212   //: Called by MFC when the main window has been destroyed.
00213   //  Note, this function is not called when your adaptor is destroyed
00214   //  inside a non-main window (eg. a dialog box).
00215   afx_msg void OnDestroy();
00216 
00217   //: Called by MFC when the application requests part of the window is redrawn.
00218   afx_msg void OnPaint();
00219 
00220   //: Called by MFC when the background needs erasing.
00221   afx_msg BOOL OnEraseBkgnd(CDC* pDC);
00222 
00223   //: Called by MFC when the application is resized.
00224   afx_msg void OnSize(UINT nType, int cx, int cy);
00225 
00226   //: Create the corresponding vgui_event from an MFC event.
00227   vgui_event generate_vgui_event(UINT nChar, UINT nRepCnt, UINT nFlags, vgui_event_type evttype);
00228 
00229   //: Called by MFC when a key is pressed inside the application.
00230   afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
00231 
00232   //: Called by MFC when a key is released inside the application.
00233   afx_msg void OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags);
00234 
00235   //: Called by MFC when the left mouse button is pressed inside the application
00236   afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
00237 
00238   //: Called by MFC when left mouse button is released inside the application.
00239   afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
00240 
00241   //: Called by MFC when the mouse is moved inside the application.
00242   afx_msg void OnMouseMove(UINT nFlags, CPoint point);
00243 
00244   //: Called by MFC when right mouse button is pressed inside the application.
00245   afx_msg void OnRButtonDown(UINT nFlags, CPoint point);
00246 
00247   //: Called by MFC when right mouse button is released inside the application.
00248   afx_msg void OnRButtonUp(UINT nFlags, CPoint point);
00249 
00250   //: Called by MFC when a user rotates a mouse wheel.
00251   afx_msg BOOL OnMouseWheel(UINT nFlags, short zDelta, CPoint pt);
00252 
00253   //: Called by MFC when middle mouse button is pressed inside the application.
00254   afx_msg void OnMButtonDown(UINT nFlags, CPoint point);
00255 
00256   //: Called by MFC when middle mouse button is released inside the application.
00257   afx_msg void OnMButtonUp(UINT nFlags, CPoint point);
00258 
00259   DECLARE_MESSAGE_MAP()
00260 };
00261 
00262 /////////////////////////////////////////////////////////////////////////////
00263 
00264 //{{AFX_INSERT_LOCATION}}
00265 // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
00266 
00267 #endif // AFX_VGUI_MFC_ADAPTOR_H__6BE405F7_0910_4C83_A175_5E8FBDCE88A5__INCLUDED_