1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "app.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
19 #if defined(__BORLANDC__)
27 #include "wx/gdicmn.h"
30 #include "wx/cursor.h"
32 #include "wx/palette.h"
34 #include "wx/dialog.h"
35 #include "wx/msgdlg.h"
38 #include "wx/msw/private.h"
40 #include "wx/module.h"
43 #include "wx/thread.h"
45 // define the array of MSG strutures
46 WX_DECLARE_OBJARRAY(MSG
, wxMsgArray
);
48 #include "wx/arrimpl.cpp"
50 WX_DEFINE_OBJARRAY(wxMsgArray
);
51 #endif // wxUSE_THREADS
53 #if wxUSE_WX_RESOURCES
54 #include "wx/resource.h"
57 // To UG: there's no point in putting this #if here
58 // if you don't do the same for the Ole calls further down.
59 // Also, OLE is used not just for drag and drop (it's used by automatn.cpp).
60 // #if wxUSE_DRAG_AND_DROP
61 #if !defined(__GNUWIN32__) && !defined(__SC__)
69 #if defined(__WIN95__) && !defined(__GNUWIN32__)
73 // use debug CRT functions for memory leak detections in VC++ if we're not
74 // using wxWindows own methods
75 #if defined(__WXDEBUG__) && defined(_MSC_VER) && !wxUSE_GLOBAL_MEMORY_OPERATORS && !defined(__NO_VC_CRTDBG__)
76 #define wxUSE_VC_CRTDBG
78 #undef wxUSE_VC_CRTDBG
81 #ifdef wxUSE_VC_CRTDBG
82 // VC++ uses this macro as debug/release mode indicator
87 /* Need to undef new if including crtdbg.h */
94 #if defined(__WXDEBUG__) && wxUSE_GLOBAL_MEMORY_OPERATORS && wxUSE_DEBUG_NEW_ALWAYS
95 #define new new(__FILE__,__LINE__)
100 extern char *wxBuffer
;
101 extern char *wxOsVersion
;
102 extern wxList
*wxWinHandleList
;
103 extern wxList WXDLLEXPORT wxPendingDelete
;
104 extern void wxSetKeyboardHook(bool doIt
);
105 extern wxCursor
*g_globalCursor
;
107 HINSTANCE wxhInstance
= 0;
108 static MSG s_currentMsg
;
109 wxApp
*wxTheApp
= NULL
;
111 // @@ why not const? and not static?
112 char wxFrameClassName
[] = "wxFrameClass";
113 char wxMDIFrameClassName
[] = "wxMDIFrameClass";
114 char wxMDIChildFrameClassName
[] = "wxMDIChildFrameClass";
115 char wxPanelClassName
[] = "wxPanelClass";
116 char wxCanvasClassName
[] = "wxCanvasClass";
118 HICON wxSTD_FRAME_ICON
= NULL
;
119 HICON wxSTD_MDICHILDFRAME_ICON
= NULL
;
120 HICON wxSTD_MDIPARENTFRAME_ICON
= NULL
;
122 HICON wxDEFAULT_FRAME_ICON
= NULL
;
123 HICON wxDEFAULT_MDICHILDFRAME_ICON
= NULL
;
124 HICON wxDEFAULT_MDIPARENTFRAME_ICON
= NULL
;
126 HBRUSH wxDisableButtonBrush
= 0;
128 LRESULT APIENTRY
wxWndProc(HWND
, UINT
, WPARAM
, LPARAM
);
130 #if !USE_SHARED_LIBRARY
131 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
133 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
134 EVT_IDLE(wxApp::OnIdle
)
135 EVT_END_SESSION(wxApp::OnEndSession
)
136 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession
)
140 long wxApp::sm_lastMessageTime
= 0;
143 static HINSTANCE gs_hRichEdit
= NULL
;
148 bool wxApp::Initialize()
150 // Some people may wish to use this, but
151 // probably it shouldn't be here by default.
153 // wxRedirectIOToConsole();
156 wxBuffer
= new char[1500];
158 #ifdef wxUSE_VC_CRTDBG
159 // do check for memory leaks on program exit
160 // (another useful flag is _CRTDBG_DELAY_FREE_MEM_DF which doesn't free
161 // deallocated memory which may be used to simulate low-memory condition)
162 _CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG
) | _CRTDBG_LEAK_CHECK_DF
);
163 #endif // debug build under MS VC++
165 wxClassInfo::InitializeClasses();
168 wxGetResource("wxWindows", "OsVersion", &wxOsVersion
);
171 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
172 wxTheColourDatabase
->Initialize();
174 wxInitializeStockLists();
175 wxInitializeStockObjects();
177 #if wxUSE_WX_RESOURCES
178 wxInitializeResourceSystem();
181 wxBitmap::InitStandardHandlers();
183 #if defined(__WIN95__)
184 InitCommonControls();
185 gs_hRichEdit
= LoadLibrary("RICHED32.DLL");
187 if (gs_hRichEdit
== NULL
)
189 wxMessageBox("Could not initialise Rich Edit DLL");
195 // for OLE, enlarge message queue to be as large as possible
196 while (!SetMessageQueue(iMsg
) && (iMsg
-= 8));
200 dwOleVer = CoBuildVersion();
202 // check the OLE library version
203 if (rmm != HIWORD(dwOleVer))
205 wxMessageBox("Incorrect version of OLE libraries.");
210 #if !defined(__GNUWIN32__) && !defined(__SC__)
211 // we need to initialize OLE library
212 if ( FAILED(::OleInitialize(NULL
)) )
213 wxFatalError(_("Cannot initialize OLE"));
217 if (!Ctl3dRegister(wxhInstance
))
218 wxFatalError("Cannot register CTL3D");
220 Ctl3dAutoSubclass(wxhInstance
);
223 g_globalCursor
= new wxCursor
;
225 wxSTD_FRAME_ICON
= LoadIcon(wxhInstance
, "wxSTD_FRAME");
226 wxSTD_MDIPARENTFRAME_ICON
= LoadIcon(wxhInstance
, "wxSTD_MDIPARENTFRAME");
227 wxSTD_MDICHILDFRAME_ICON
= LoadIcon(wxhInstance
, "wxSTD_MDICHILDFRAME");
229 wxDEFAULT_FRAME_ICON
= LoadIcon(wxhInstance
, "wxDEFAULT_FRAME");
230 wxDEFAULT_MDIPARENTFRAME_ICON
= LoadIcon(wxhInstance
, "wxDEFAULT_MDIPARENTFRAME");
231 wxDEFAULT_MDICHILDFRAME_ICON
= LoadIcon(wxhInstance
, "wxDEFAULT_MDICHILDFRAME");
233 RegisterWindowClasses();
235 // Create the brush for disabling bitmap buttons
238 lb
.lbStyle
= BS_PATTERN
;
239 lb
.lbHatch
= (int)LoadBitmap( wxhInstance
, "wxDISABLE_BUTTON_BITMAP" ) ;
240 wxDisableButtonBrush
= ::CreateBrushIndirect( & lb
) ;
241 ::DeleteObject( (HGDIOBJ
)lb
.lbHatch
) ;
247 wxWinHandleList
= new wxList(wxKEY_INTEGER
);
249 // This is to foil optimizations in Visual C++ that
250 // throw out dummy.obj.
251 #if (_MSC_VER >= 800) && !defined(WXMAKINGDLL)
252 extern char wxDummyChar
;
253 if (wxDummyChar
) wxDummyChar
++;
256 wxSetKeyboardHook(TRUE
);
258 wxModule::RegisterModules();
259 if (!wxModule::InitializeModules())
264 //// RegisterWindowClasses
266 bool wxApp::RegisterWindowClasses()
268 ///////////////////////////////////////////////////////////////////////
269 // Register the frame window class.
270 WNDCLASS wndclass
; // Structure used to register Windows class.
272 wndclass
.style
= CS_HREDRAW
| CS_VREDRAW
;
273 wndclass
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
274 wndclass
.cbClsExtra
= 0;
275 wndclass
.cbWndExtra
= sizeof( DWORD
); // was 4
276 wndclass
.hInstance
= wxhInstance
;
277 wndclass
.hIcon
= NULL
; // wxSTD_FRAME_ICON;
278 wndclass
.hCursor
= LoadCursor( NULL
, IDC_ARROW
);
279 wndclass
.hbrBackground
= (HBRUSH
)(COLOR_APPWORKSPACE
+1) ;
280 // wndclass.hbrBackground = GetStockObject( WHITE_BRUSH );
281 wndclass
.lpszMenuName
= NULL
;
282 #ifdef _MULTIPLE_INSTANCES
283 sprintf( wxFrameClassName
,"wxFrameClass%d", wxhInstance
);
285 wndclass
.lpszClassName
= wxFrameClassName
;
287 if (!RegisterClass( &wndclass
))
289 // wxFatalError("Can't register Frame Window class");
292 ///////////////////////////////////////////////////////////////////////
293 // Register the MDI frame window class.
294 WNDCLASS wndclass1
; // Structure used to register Windows class.
296 wndclass1
.style
= CS_HREDRAW
| CS_VREDRAW
;
297 wndclass1
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
298 wndclass1
.cbClsExtra
= 0;
299 wndclass1
.cbWndExtra
= sizeof( DWORD
); // was 4
300 wndclass1
.hInstance
= wxhInstance
;
301 wndclass1
.hIcon
= NULL
; // wxSTD_MDIPARENTFRAME_ICON;
302 wndclass1
.hCursor
= LoadCursor( NULL
, IDC_ARROW
);
303 // wndclass1.hbrBackground = (HBRUSH)(COLOR_APPWORKSPACE+1) ;
304 wndclass1
.hbrBackground
= NULL
;
305 wndclass1
.lpszMenuName
= NULL
;
307 wndclass1
.lpszClassName
= wxMDIFrameClassName
;
308 if (!RegisterClass( &wndclass1
))
310 // wxFatalError("Can't register MDI Frame window class");
314 ///////////////////////////////////////////////////////////////////////
315 // Register the MDI child frame window class.
316 WNDCLASS wndclass4
; // Structure used to register Windows class.
318 wndclass4
.style
= CS_HREDRAW
| CS_VREDRAW
;
319 wndclass4
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
320 wndclass4
.cbClsExtra
= 0;
321 wndclass4
.cbWndExtra
= sizeof( DWORD
); // was 4
322 wndclass4
.hInstance
= wxhInstance
;
323 wndclass4
.hIcon
= NULL
; // wxSTD_MDICHILDFRAME_ICON;
324 wndclass4
.hCursor
= LoadCursor( NULL
, IDC_ARROW
);
325 // TODO: perhaps this should be NULL so that Windows doesn't
326 // paint the background itself (would OnEraseBackground duplicate
328 wndclass4
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+1) ;
329 // wndclass4.hbrBackground = NULL;
330 wndclass4
.lpszMenuName
= NULL
;
331 wndclass4
.lpszClassName
= wxMDIChildFrameClassName
;
333 if (!RegisterClass( &wndclass4
))
335 // wxFatalError("Can't register MDI child frame window class");
339 ///////////////////////////////////////////////////////////////////////
340 // Register the panel window class.
341 WNDCLASS wndclass2
; // Structure used to register Windows class.
342 memset(&wndclass2
, 0, sizeof(WNDCLASS
)); // start with NULL defaults
343 // Use CS_OWNDC to avoid messing about restoring the context
344 // for every graphic operation.
345 wndclass2
.style
= CS_HREDRAW
| CS_VREDRAW
;
346 wndclass2
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
347 wndclass2
.cbClsExtra
= 0;
348 wndclass2
.cbWndExtra
= sizeof( DWORD
); // was 4
349 wndclass2
.hInstance
= wxhInstance
;
350 wndclass2
.hIcon
= NULL
;
351 wndclass2
.hCursor
= NULL
;
352 // wndclass2.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1) ;
353 wndclass2
.hbrBackground
= (HBRUSH
) GetStockObject( LTGRAY_BRUSH
);
354 wndclass2
.lpszMenuName
= NULL
;
355 wndclass2
.lpszClassName
= wxPanelClassName
;
356 if (!RegisterClass( &wndclass2
))
358 // wxFatalError("Can't register Panel Window class");
362 ///////////////////////////////////////////////////////////////////////
363 // Register the canvas and textsubwindow class name
364 WNDCLASS wndclass3
; // Structure used to register Windows class.
365 memset(&wndclass3
, 0, sizeof(WNDCLASS
)); // start with NULL defaults
366 // Use CS_OWNDC to avoid messing about restoring the context
367 // for every graphic operation.
368 // wndclass3.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS ;
370 wndclass3
.style
= CS_HREDRAW
| CS_VREDRAW
| CS_DBLCLKS
;
371 wndclass3
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
372 wndclass3
.cbClsExtra
= 0;
373 wndclass3
.cbWndExtra
= sizeof( DWORD
); // was 4
374 wndclass3
.hInstance
= wxhInstance
;
375 wndclass3
.hIcon
= NULL
;
376 wndclass3
.hCursor
= NULL
;
377 // wndclass3.hbrBackground = (HBRUSH)(COLOR_WINDOW+1) ;
378 wndclass3
.hbrBackground
= NULL
;
379 wndclass3
.lpszMenuName
= NULL
;
380 wndclass3
.lpszClassName
= wxCanvasClassName
;
381 if (!RegisterClass( &wndclass3
))
383 // wxFatalError("Can't register Canvas class");
390 //// Convert Windows to argc, argv style
392 void wxApp::ConvertToStandardCommandArgs(char* lpCmdLine
)
396 wxString
cmdLine(lpCmdLine
);
399 // Get application name
400 char name
[260]; // 260 is MAX_PATH value from windef.h
401 ::GetModuleFileName(wxhInstance
, name
, WXSIZEOF(name
));
403 // GNUWIN32 already fills in the first arg with the application name.
404 #if !defined(__GNUWIN32__)
409 strcpy(name
, wxFileNameFromPath(name
));
410 wxStripExtension(name
);
411 wxTheApp
->SetAppName(name
);
414 // Treat strings enclosed in double-quotes as single arguments
416 int len
= cmdLine
.Length();
420 while ((i
< len
) && isspace(cmdLine
.GetChar(i
)))
425 if (cmdLine
.GetChar(i
) == '"') // We found the start of a string
429 while ((i
< len
) && (cmdLine
.GetChar(i
) != '"'))
432 wxString
arg(cmdLine
.Mid(first
, (i
- first
)));
438 i
++; // Skip past 2nd quote
440 else // Unquoted argument
443 while ((i
< len
) && !isspace(cmdLine
.GetChar(i
)))
446 wxString
arg(cmdLine
.Mid(first
, (i
- first
)));
454 wxTheApp
->argv
= new char*[count
+ 1];
455 for (i
= 0; i
< count
; i
++)
457 wxString
arg(args
[i
]);
458 wxTheApp
->argv
[i
] = copystring((const char*)arg
);
460 wxTheApp
->argv
[count
] = NULL
; // argv[] is a NULL-terminated list
461 wxTheApp
->argc
= count
;
464 //// Cleans up any wxWindows internal structures left lying around
466 void wxApp::CleanUp()
469 wxModule::CleanUpModules();
471 #if wxUSE_WX_RESOURCES
472 wxCleanUpResourceSystem();
474 // wxDefaultResourceTable->ClearTable();
477 // Indicate that the cursor can be freed,
478 // so that cursor won't be deleted by deleting
479 // the bitmap list before g_globalCursor goes out
480 // of scope (double deletion of the cursor).
481 wxSetCursor(wxNullCursor
);
482 delete g_globalCursor
;
484 wxDeleteStockObjects() ;
486 // Destroy all GDI lists, etc.
487 wxDeleteStockLists();
489 delete wxTheColourDatabase
;
490 wxTheColourDatabase
= NULL
;
492 wxBitmap::CleanUpHandlers();
497 //// WINDOWS-SPECIFIC CLEANUP
499 wxSetKeyboardHook(FALSE
);
502 if (gs_hRichEdit
!= NULL
)
503 FreeLibrary(gs_hRichEdit
);
510 if (wxSTD_FRAME_ICON
)
511 DestroyIcon(wxSTD_FRAME_ICON
);
512 if (wxSTD_MDICHILDFRAME_ICON
)
513 DestroyIcon(wxSTD_MDICHILDFRAME_ICON
);
514 if (wxSTD_MDIPARENTFRAME_ICON
)
515 DestroyIcon(wxSTD_MDIPARENTFRAME_ICON
);
517 if (wxDEFAULT_FRAME_ICON
)
518 DestroyIcon(wxDEFAULT_FRAME_ICON
);
519 if (wxDEFAULT_MDICHILDFRAME_ICON
)
520 DestroyIcon(wxDEFAULT_MDICHILDFRAME_ICON
);
521 if (wxDEFAULT_MDIPARENTFRAME_ICON
)
522 DestroyIcon(wxDEFAULT_MDIPARENTFRAME_ICON
);
524 if ( wxDisableButtonBrush
)
525 ::DeleteObject( wxDisableButtonBrush
) ;
527 #if !defined(__GNUWIN32__) && !defined(__SC__)
532 Ctl3dUnregister(wxhInstance
);
536 delete wxWinHandleList
;
538 wxClassInfo::CleanUpClasses();
543 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
544 // At this point we want to check if there are any memory
545 // blocks that aren't part of the wxDebugContext itself,
546 // as a special case. Then when dumping we need to ignore
547 // wxDebugContext, too.
548 if (wxDebugContext::CountObjectsLeft() > 0)
550 wxLogDebug("There were memory leaks.");
551 wxDebugContext::Dump();
552 wxDebugContext::PrintStatistics();
554 // wxDebugContext::SetStream(NULL, NULL);
557 // do it as the very last thing because everything else can log messages
558 wxLog::DontCreateOnDemand();
559 delete wxLog::SetActiveTarget(NULL
);
562 #if !defined(_WINDLL) || (defined(_WINDLL) && defined(WXMAKINGDLL))
564 //// Main wxWindows entry point
565 int wxEntry(WXHINSTANCE hInstance
,
566 WXHINSTANCE
WXUNUSED(hPrevInstance
),
571 #if !defined(__WXDEBUG__) && !defined(__BORLANDC__) && !defined(__WATCOMC__) // take everything into a try-except block in release build
575 wxhInstance
= (HINSTANCE
) hInstance
;
577 if (!wxApp::Initialize())
580 // create the application object or ensure that one already exists
583 // The app may have declared a global application object, but we recommend
584 // the IMPLEMENT_APP macro is used instead, which sets an initializer
585 // function for delayed, dynamic app object construction.
586 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
587 "No initializer - use IMPLEMENT_APP macro." );
589 wxTheApp
= (*wxApp::GetInitializerFunction()) ();
592 wxCHECK_MSG( wxTheApp
, 0, "You have to define an instance of wxApp!" );
594 // save the WinMain() parameters
595 wxTheApp
->ConvertToStandardCommandArgs(lpCmdLine
);
596 wxTheApp
->m_nCmdShow
= nCmdShow
;
598 // GUI-specific initialisation. In fact on Windows we don't have any,
599 // but this call is provided for compatibility across platforms.
600 wxTheApp
->OnInitGui() ;
604 if ( wxTheApp
->OnInit() )
608 retValue
= wxTheApp
->OnRun();
611 //else: app initialization failed, so we skipped OnRun()
613 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
616 // Forcibly delete the window.
617 if ( topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
618 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
620 topWindow
->Close(TRUE
);
621 wxTheApp
->DeletePendingObjects();
626 wxTheApp
->SetTopWindow(NULL
);
632 // flush the logged messages if any
633 wxLog
*pLog
= wxLog::GetActiveTarget();
634 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
641 #if !defined(__WXDEBUG__) && !defined(__BORLANDC__) && !defined(__WATCOMC__) // catch exceptions only in release build
643 except ( EXCEPTION_EXECUTE_HANDLER
) {
646 wxTheApp->OnFatalException();
649 ::ExitProcess(3); // the same exit code as abort()
656 //// Entry point for DLLs
658 int wxEntry(WXHINSTANCE hInstance
)
660 wxhInstance
= (HINSTANCE
) hInstance
;
663 // The app may have declared a global application object, but we recommend
664 // the IMPLEMENT_APP macro is used instead, which sets an initializer function
665 // for delayed, dynamic app object construction.
669 if (!wxApp::GetInitializerFunction())
671 MessageBox(NULL
, "No initializer - use IMPLEMENT_APP macro.", "wxWindows Error", MB_APPLMODAL
| MB_ICONSTOP
| MB_OK
);
675 wxTheApp
= (* wxApp::GetInitializerFunction()) ();
679 MessageBox(NULL
, "You have to define an instance of wxApp!", "wxWindows Error", MB_APPLMODAL
| MB_ICONSTOP
| MB_OK
);
684 wxTheApp
->argv
= NULL
;
686 wxTheApp
->OnInitGui();
690 if (wxTheApp
->GetTopWindow() && wxTheApp
->GetTopWindow()->GetHWND()) {
691 wxTheApp
->GetTopWindow()->Show(TRUE
);
698 //// Static member initialization
700 wxAppInitializerFunction
wxApp::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
707 m_wantDebugOutput
= TRUE
;
712 m_printMode
= wxPRINT_WINDOWS
;
714 m_printMode
= wxPRINT_POSTSCRIPT
;
716 m_exitOnFrameDelete
= TRUE
;
722 // Delete command-line args
724 for (i
= 0; i
< argc
; i
++)
731 bool wxApp::Initialized()
739 #ifdef _WINDLL // Assume initialized if DLL (no way of telling)
745 * Get and process a message, returning FALSE if WM_QUIT
746 * received (and also set the flag telling the app to exit the main loop)
749 bool wxApp::DoMessage()
751 BOOL rc
= ::GetMessage(&s_currentMsg
, (HWND
) NULL
, 0, 0);
761 // should never happen, but let's test for it nevertheless
762 wxLogLastError("GetMessage");
767 wxASSERT_MSG( wxThread::IsMain(),
768 "only the main thread can process Windows messages" );
770 static bool s_hadGuiLock
= TRUE
;
771 static wxMsgArray s_aSavedMessages
;
773 // if a secondary thread owns is doing GUI calls, save all messages for
774 // later processing - we can't process them right now because it will
775 // lead to recursive library calls (and we're not reentrant)
776 if ( !wxGuiOwnedByMainThread() )
778 s_hadGuiLock
= FALSE
;
780 s_aSavedMessages
.Add(s_currentMsg
);
786 // have we just regained the GUI lock? if so, post all of the saved
789 // FIXME of course, it's not _exactly_ the same as processing the
790 // messages normally - expect some things to break...
795 size_t count
= s_aSavedMessages
.Count();
796 for ( size_t n
= 0; n
< count
; n
++ )
798 MSG
& msg
= s_aSavedMessages
[n
];
800 if ( !ProcessMessage((WXMSG
*)&msg
) )
802 ::TranslateMessage(&msg
);
803 ::DispatchMessage(&msg
);
807 s_aSavedMessages
.Empty();
810 #endif // wxUSE_THREADS
812 // Process the message
813 if ( !ProcessMessage((WXMSG
*)&s_currentMsg
) )
815 ::TranslateMessage(&s_currentMsg
);
816 wxApp::sm_lastMessageTime
= s_currentMsg
.time
; /* MATTHEW: timeStamp impl. */
817 ::DispatchMessage(&s_currentMsg
);
825 * Keep trying to process messages until WM_QUIT
828 * If there are messages to be processed, they will all be
829 * processed and OnIdle will not be called.
830 * When there are no more messages, OnIdle is called.
831 * If OnIdle requests more time,
832 * it will be repeatedly called so long as there are no pending messages.
833 * A 'feature' of this is that once OnIdle has decided that no more processing
834 * is required, then it won't get processing time until further messages
835 * are processed (it'll sit in DoMessage).
838 int wxApp::MainLoop()
842 while ( m_keepGoing
)
845 wxMutexGuiLeaveOrEnter();
846 #endif // wxUSE_THREADS
848 while ( !::PeekMessage(&s_currentMsg
, 0, 0, 0, PM_NOREMOVE
) &&
856 return s_currentMsg
.wParam
;
859 // Returns TRUE if more time is needed.
860 bool wxApp::ProcessIdle()
863 event
.SetEventObject(this);
866 return event
.MoreRequested();
869 void wxApp::ExitMainLoop()
874 bool wxApp::Pending()
876 return (::PeekMessage(&s_currentMsg
, 0, 0, 0, PM_NOREMOVE
) != 0) ;
879 void wxApp::Dispatch()
885 * Give all windows a chance to preprocess
886 * the message. Some may have accelerator tables, or have
889 bool wxApp::ProcessMessage(WXMSG
*Msg
)
891 MSG
*msg
= (MSG
*)Msg
;
895 // Try translations first; find the youngest window with
896 // a translation table.
897 for (hWnd
= msg
->hwnd
; hWnd
!= NULL
; hWnd
= ::GetParent(hWnd
))
899 wxWindow
*wnd
= wxFindWinFromHandle((WXHWND
) hWnd
);
902 if (wnd
->MSWTranslateMessage(Msg
))
907 // Anyone for a non-translation message? Try youngest descendants first.
908 for (hWnd
= msg
->hwnd
; hWnd
!= NULL
; hWnd
= ::GetParent(hWnd
))
910 wxWindow
*wnd
= wxFindWinFromHandle((WXHWND
) hWnd
);
913 if (wnd
->MSWProcessMessage(Msg
))
920 void wxApp::OnIdle(wxIdleEvent
& event
)
922 static bool s_inOnIdle
= FALSE
;
924 // Avoid recursion (via ProcessEvent default case)
930 // 'Garbage' collection of windows deleted with Close().
931 DeletePendingObjects();
933 // flush the logged messages if any
934 wxLog
*pLog
= wxLog::GetActiveTarget();
935 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
938 // Send OnIdle events to all windows
939 if ( SendIdleEvents() )
941 // SendIdleEvents() returns TRUE if at least one window requested more
943 event
.RequestMore(TRUE
);
949 // Send idle event to all top-level windows
950 bool wxApp::SendIdleEvents()
952 bool needMore
= FALSE
;
953 wxNode
* node
= wxTopLevelWindows
.First();
956 wxWindow
* win
= (wxWindow
*) node
->Data();
957 if (SendIdleEvents(win
))
966 // Send idle event to window and all subwindows
967 bool wxApp::SendIdleEvents(wxWindow
* win
)
969 bool needMore
= FALSE
;
972 event
.SetEventObject(win
);
973 win
->GetEventHandler()->ProcessEvent(event
);
975 if (event
.MoreRequested())
978 wxNode
* node
= win
->GetChildren().First();
981 wxWindow
* win
= (wxWindow
*) node
->Data();
982 if (SendIdleEvents(win
))
990 void wxApp::DeletePendingObjects()
992 wxNode
*node
= wxPendingDelete
.First();
995 wxObject
*obj
= (wxObject
*)node
->Data();
999 if (wxPendingDelete
.Member(obj
))
1002 // Deleting one object may have deleted other pending
1003 // objects, so start from beginning of list again.
1004 node
= wxPendingDelete
.First();
1008 void wxApp::OnEndSession(wxCloseEvent
& event
)
1011 GetTopWindow()->Close(TRUE
);
1014 // Default behaviour: close the application with prompts. The
1015 // user can veto the close, and therefore the end session.
1016 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
1020 if (!GetTopWindow()->Close(!event
.CanVeto()))
1025 wxLog
* wxApp::CreateLogTarget()
1027 return new wxLogGui
;
1030 wxWindow
* wxApp::GetTopWindow() const
1034 else if (wxTopLevelWindows
.Number() > 0)
1035 return (wxWindow
*) wxTopLevelWindows
.First()->Data();
1040 int wxApp::GetComCtl32Version() const
1042 // have we loaded COMCTL32 yet?
1043 HMODULE theModule
= ::GetModuleHandle("COMCTL32");
1046 // if so, then we can check for the version
1049 // InitCommonControlsEx is unique to 4.7 and later
1050 FARPROC theProc
= ::GetProcAddress(theModule
, "InitCommonControlsEx");
1053 { // not found, must be 4.00
1058 // The following symbol are unique to 4.71
1060 // FlatSB_EnableScrollBar FlatSB_GetScrollInfo FlatSB_GetScrollPos
1061 // FlatSB_GetScrollProp FlatSB_GetScrollRange FlatSB_SetScrollInfo
1062 // FlatSB_SetScrollPos FlatSB_SetScrollProp FlatSB_SetScrollRange
1063 // FlatSB_ShowScrollBar
1064 // _DrawIndirectImageList _DuplicateImageList
1066 // UninitializeFlatSB
1067 // we could check for any of these - I chose DllInstall
1068 FARPROC theProc
= ::GetProcAddress(theModule
, "DllInstall");
1071 // not found, must be 4.70
1075 { // found, must be 4.71
1086 FatalAppExit(0, "Fatal error: exiting");
1089 // Yield to incoming messages
1093 // We want to go back to the main message loop
1094 // if we see a WM_QUIT. (?)
1095 while (PeekMessage(&msg
, NULL
, 0, 0, PM_NOREMOVE
) && msg
.message
!= WM_QUIT
)
1097 if ( !wxTheApp
->DoMessage() )
1104 HINSTANCE
wxGetInstance()
1109 // For some reason, with MSVC++ 1.5, WinMain isn't linked in properly
1110 // if in a separate file. So include it here to ensure it's linked.
1111 #if (defined(_MSC_VER) && !defined(__WIN32__)) || defined(__GNUWIN32__)