core/vgui/impl/mfc/vgui_mfc.cxx
Go to the documentation of this file.
00001 // This is core/vgui/impl/mfc/vgui_mfc.cxx
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005 //:
00006 // \file
00007 // \brief See vgui_mfc.h for a description of this file.
00008 // \author awf@robots.ox.ac.uk
00009 // \date   July 2000
00010 //
00011 // \verbatim
00012 //  Modifications
00013 //   K.Y.McGaul  29-AUG-2001  Add destructor to remove Purify mem leak.
00014 //                            Tidy up indentation and documentation.
00015 //   16-NOV-2007 Gamze Tunali - added produce_dialog_extension()
00016 // \endverbatim
00017 //-----------------------------------------------------------------------------
00018 #include "vgui_mfc.h"
00019 
00020 #include <vgui/vgui_adaptor.h>
00021 #include "vgui_mfc_window.h"
00022 #include "vgui_mfc_dialog_impl.h"
00023 #include "vgui_mfc_dialog_extensions_impl.h"
00024 #include "vgui_mfc_utils.h"
00025 
00026 #define VC_EXTRALEAN  // Exclude rarely-used stuff from Windows headers
00027 
00028 #include <afxwin.h>         // MFC core and standard components
00029 #include <afxext.h>         // MFC extensions
00030 #include <afxdisp.h>        // MFC Automation classes
00031 #include <afxdtctl.h>       // MFC support for Internet Explorer 4 Common Controls
00032 #ifndef _AFX_NO_AFXCMN_SUPPORT
00033 #include <afxcmn.h>         // MFC support for Windows Common Controls
00034 #endif // _AFX_NO_AFXCMN_SUPPORT
00035 
00036 #include "vgui_mfc_app.h"
00037 
00038 #include <vcl_cassert.h>
00039 #include <vcl_iostream.h>
00040 #include <vgui/vgui_gl.h>
00041 
00042 static bool debug = false;
00043 
00044 vgui_mfc* vgui_mfc::instance()
00045 {
00046   static vgui_mfc instance_;
00047   return &instance_;
00048 }
00049 
00050 //---------------------------------------------------------------------------------
00051 //: Default constructor
00052 vgui_mfc::vgui_mfc()
00053   : theApp_( 0 )
00054 {
00055   utils = vgui_mfc_utils::instance();
00056 }
00057 
00058 //: Destructor
00059 vgui_mfc::~vgui_mfc()
00060 {
00061   for (unsigned i=0; i<windows_to_delete.size(); i++)
00062     delete windows_to_delete[i];
00063 }
00064 
00065 //--------------------------------------------------------------------------------
00066 //: Pure virtual function from vgui - returns the name of the GUI toolkit.
00067 vcl_string vgui_mfc::name() const { return "mfc"; }
00068 
00069 
00070 //--------------------------------------------------------------------------------
00071 //: Virtual function from vgui - initialise the implementation of vgui.
00072 void vgui_mfc::init(int &argc, char **argv)
00073 {
00074   if (debug) vcl_cerr << "vgui_mfc::init()\n";
00075 
00076   // If we are here, then we aren't trying to use vgui in an MFC
00077   // framework. That is, there shouldn't be another CWinApp
00078   // somewhere. So, create the vgui one.
00079   if ( AfxGetApp() )
00080     vcl_cerr << "vgui_mfc::init(): another CWinApp object exists!\n";
00081   else
00082     theApp_ = new vgui_mfc_app;
00083 
00084   //: Initialise MFC foundation classes
00085   if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), SW_SHOW))
00086   {
00087     // TODO: change error code to suit your needs
00088     vcl_cerr << _T("Fatal Error: MFC initialization failed") << vcl_endl;
00089     assert(0);
00090   }
00091   AfxGetApp()->InitInstance();
00092 }
00093 
00094 void vgui_mfc::uninit()
00095 {
00096   delete theApp_;
00097   theApp_ = 0;
00098 }
00099 
00100 
00101 //--------------------------------------------------------------------------------
00102 //: Virtual function from vgui - runs the event loop.
00103 void vgui_mfc::run()
00104 {
00105   if (debug) vcl_cerr << "vgui_mfc::run()\n";
00106 
00107   //: Start the main thread
00108   AfxGetThread()->Run();
00109   // Once the window has been closed Run() will terminate
00110   // and we clean-up the mess
00111   AfxWinTerm();
00112 }
00113 
00114 void vgui_mfc::run_one_event()
00115 {
00116   run_till_idle();
00117   MSG m_msg;
00118   AfxGetThread()->PumpMessage();
00119   ::PeekMessage(&m_msg, NULL, NULL, NULL, PM_NOREMOVE);
00120 }
00121 
00122 void vgui_mfc::run_till_idle()
00123 {
00124   MSG msg;
00125   while ( ::PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE ) )
00126   {
00127     if ( !AfxGetApp()->PumpMessage( ) )
00128     {
00129       ::PostQuitMessage(0);
00130       break;
00131     }
00132   }
00133 #if 0 // let MFC do its idle processing
00134   LONG lIdle = 0;
00135   while ( AfxGetApp()->OnIdle(lIdle++ ) ) ;
00136 #endif // 0
00137 }
00138 
00139 void vgui_mfc::flush()
00140 {
00141   glFlush();
00142   run_till_idle();
00143 }
00144 
00145 void vgui_mfc::quit()
00146 {
00147   // From MFC FAQ:
00148   // Same as double-clicking on main window close box.
00149   ASSERT(AfxGetMainWnd() != NULL);
00150   AfxGetMainWnd()->SendMessage(WM_CLOSE);
00151 }
00152 
00153 void vgui_mfc::add_event(const vgui_event& event) {}
00154 
00155 
00156 //--------------------------------------------------------------------------------
00157 //: Virtual function from vgui - creates a new window with a menubar.
00158 vgui_window* vgui_mfc::produce_window(int width, int height, const vgui_menu& menubar,
00159                                       const char* title)
00160 {
00161   vgui_window* a_window = new vgui_mfc_window(width, height, menubar, title);
00162   windows_to_delete.push_back(a_window);
00163   return a_window;
00164   //return new vgui_mfc_window(width, height, menubar, title);
00165 }
00166 
00167 
00168 //--------------------------------------------------------------------------------
00169 //: Virtual function from vgui - creates a new window.
00170 vgui_window* vgui_mfc::produce_window(int width, int height,
00171                                       const char* title)
00172 {
00173   vgui_window* a_window = new vgui_mfc_window(title, width, height);
00174   windows_to_delete.push_back(a_window);
00175   return a_window;
00176   //return new vgui_mfc_window(title,width, height);
00177 }
00178 
00179 
00180 //--------------------------------------------------------------------------------
00181 //: Virtual function from vgui - creates a new dialog box.
00182 vgui_dialog_impl* vgui_mfc::produce_dialog(const char* name)
00183 {
00184   return new vgui_mfc_dialog_impl(name);
00185   return 0;
00186 }
00187 
00188 //--------------------------------------------------------------------------------
00189 //: Virtual function from vgui - creates a new dialog box.
00190 vgui_dialog_extensions_impl* vgui_mfc::produce_dialog_extension(const char* name)
00191 {
00192   return new vgui_mfc_dialog_extensions_impl(name);
00193   return 0;
00194 }