1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ===========================================================================
14 // ===========================================================================
16 // ---------------------------------------------------------------------------
18 // ---------------------------------------------------------------------------
21 #pragma implementation "app.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
27 #if defined(__BORLANDC__)
35 #include "wx/gdicmn.h"
38 #include "wx/cursor.h"
40 #include "wx/palette.h"
42 #include "wx/dialog.h"
43 #include "wx/msgdlg.h"
45 #include "wx/dynarray.h"
46 #include "wx/wxchar.h"
51 #include "wx/module.h"
53 #include "wx/msw/private.h"
56 #include "wx/thread.h"
58 // define the array of MSG strutures
59 WX_DECLARE_OBJARRAY(MSG
, wxMsgArray
);
61 #include "wx/arrimpl.cpp"
63 WX_DEFINE_OBJARRAY(wxMsgArray
);
64 #endif // wxUSE_THREADS
66 #if wxUSE_WX_RESOURCES
67 #include "wx/resource.h"
71 #include "wx/tooltip.h"
72 #endif // wxUSE_TOOLTIPS
74 // OLE is used for drag-and-drop, clipboard, OLE Automation..., but some
75 // compilers don't support it (missing headers, libs, ...)
76 #if defined(__GNUWIN32_OLD__) || defined(__SC__) || defined(__SALFORDC__)
80 #endif // broken compilers
89 #if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) || defined(__TWIN32__))
93 #include "wx/msw/msvcrt.h"
95 // ----------------------------------------------------------------------------
96 // conditional compilation
97 // ----------------------------------------------------------------------------
99 // The macro _WIN32_IE is defined by commctrl.h (unless it had already been
100 // defined before) and shows us what common control features are available
101 // during the compile time (it doesn't mean that they will be available during
102 // the run-time, use GetComCtl32Version() to test for them!). The possible
105 // 0x0200 for comctl32.dll 4.00 shipped with Win95/NT 4.0
106 // 0x0300 4.70 IE 3.x
107 // 0x0400 4.71 IE 4.0
108 // 0x0401 4.72 IE 4.01 and Win98
109 // 0x0500 5.00 IE 5.x and NT 5.0 (Win2000)
112 // minimal set of features by default
113 #define _WIN32_IE 0x0200
116 #if _WIN32_IE >= 0x0300
120 // ---------------------------------------------------------------------------
122 // ---------------------------------------------------------------------------
124 extern wxChar
*wxBuffer
;
125 extern wxChar
*wxOsVersion
;
126 extern wxList
*wxWinHandleList
;
127 extern wxList WXDLLEXPORT wxPendingDelete
;
128 extern void wxSetKeyboardHook(bool doIt
);
131 wxApp
*wxTheApp
= NULL
;
133 // NB: all "NoRedraw" classes must have the same names as the "normal" classes
134 // with NR suffix - wxWindow::MSWCreate() supposes this
135 const wxChar
*wxFrameClassName
= wxT("wxFrameClass");
136 const wxChar
*wxFrameClassNameNoRedraw
= wxT("wxFrameClassNR");
137 const wxChar
*wxMDIFrameClassName
= wxT("wxMDIFrameClass");
138 const wxChar
*wxMDIFrameClassNameNoRedraw
= wxT("wxMDIFrameClassNR");
139 const wxChar
*wxMDIChildFrameClassName
= wxT("wxMDIChildFrameClass");
140 const wxChar
*wxMDIChildFrameClassNameNoRedraw
= wxT("wxMDIChildFrameClassNR");
141 const wxChar
*wxPanelClassName
= wxT("wxPanelClass");
142 const wxChar
*wxCanvasClassName
= wxT("wxCanvasClass");
144 HICON wxSTD_FRAME_ICON
= (HICON
) NULL
;
145 HICON wxSTD_MDICHILDFRAME_ICON
= (HICON
) NULL
;
146 HICON wxSTD_MDIPARENTFRAME_ICON
= (HICON
) NULL
;
148 HICON wxDEFAULT_FRAME_ICON
= (HICON
) NULL
;
149 HICON wxDEFAULT_MDICHILDFRAME_ICON
= (HICON
) NULL
;
150 HICON wxDEFAULT_MDIPARENTFRAME_ICON
= (HICON
) NULL
;
152 HBRUSH wxDisableButtonBrush
= (HBRUSH
) 0;
154 LRESULT WXDLLEXPORT APIENTRY
wxWndProc(HWND
, UINT
, WPARAM
, LPARAM
);
156 // ===========================================================================
158 // ===========================================================================
160 // ---------------------------------------------------------------------------
162 // ---------------------------------------------------------------------------
164 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
166 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
167 EVT_IDLE(wxApp::OnIdle
)
168 EVT_END_SESSION(wxApp::OnEndSession
)
169 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession
)
173 bool wxApp::Initialize()
175 // Some people may wish to use this, but
176 // probably it shouldn't be here by default.
178 // wxRedirectIOToConsole();
181 wxBuffer
= new wxChar
[1500]; // FIXME
183 wxClassInfo::InitializeClasses();
186 wxGetResource(wxT("wxWindows"), wxT("OsVersion"), &wxOsVersion
);
190 wxPendingEventsLocker
= new wxCriticalSection
;
193 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
194 wxTheColourDatabase
->Initialize();
196 wxInitializeStockLists();
197 wxInitializeStockObjects();
199 #if wxUSE_WX_RESOURCES
200 wxInitializeResourceSystem();
203 wxBitmap::InitStandardHandlers();
205 #if defined(__WIN95__)
206 InitCommonControls();
213 // for OLE, enlarge message queue to be as large as possible
215 while (!SetMessageQueue(iMsg
) && (iMsg
-= 8))
218 // we need to initialize OLE library
219 if ( FAILED(::OleInitialize(NULL
)) )
220 wxLogError(_("Cannot initialize OLE"));
224 if (!Ctl3dRegister(wxhInstance
))
225 wxLogError(wxT("Cannot register CTL3D"));
227 Ctl3dAutoSubclass(wxhInstance
);
230 // VZ: these icons are not in wx.rc anyhow (but should they?)!
232 wxSTD_FRAME_ICON
= LoadIcon(wxhInstance
, wxT("wxSTD_FRAME"));
233 wxSTD_MDIPARENTFRAME_ICON
= LoadIcon(wxhInstance
, wxT("wxSTD_MDIPARENTFRAME"));
234 wxSTD_MDICHILDFRAME_ICON
= LoadIcon(wxhInstance
, wxT("wxSTD_MDICHILDFRAME"));
236 wxDEFAULT_FRAME_ICON
= LoadIcon(wxhInstance
, wxT("wxDEFAULT_FRAME"));
237 wxDEFAULT_MDIPARENTFRAME_ICON
= LoadIcon(wxhInstance
, wxT("wxDEFAULT_MDIPARENTFRAME"));
238 wxDEFAULT_MDICHILDFRAME_ICON
= LoadIcon(wxhInstance
, wxT("wxDEFAULT_MDICHILDFRAME"));
241 RegisterWindowClasses();
243 // Create the brush for disabling bitmap buttons
246 lb
.lbStyle
= BS_PATTERN
;
247 lb
.lbHatch
= (int)LoadBitmap( wxhInstance
, wxT("wxDISABLE_BUTTON_BITMAP") );
250 wxDisableButtonBrush
= ::CreateBrushIndirect( & lb
);
251 ::DeleteObject( (HGDIOBJ
)lb
.lbHatch
);
253 //else: wxWindows resources are probably not linked in
259 wxWinHandleList
= new wxList(wxKEY_INTEGER
);
261 // This is to foil optimizations in Visual C++ that throw out dummy.obj.
262 // PLEASE DO NOT ALTER THIS.
263 #if defined(__VISUALC__) && !defined(WXMAKINGDLL)
264 extern char wxDummyChar
;
265 if (wxDummyChar
) wxDummyChar
++;
268 wxSetKeyboardHook(TRUE
);
270 wxModule::RegisterModules();
271 if (!wxModule::InitializeModules())
276 // ---------------------------------------------------------------------------
277 // RegisterWindowClasses
278 // ---------------------------------------------------------------------------
280 // TODO we should only register classes really used by the app. For this it
281 // would be enough to just delay the class registration until an attempt
282 // to create a window of this class is made.
283 bool wxApp::RegisterWindowClasses()
287 // for each class we register one with CS_(V|H)REDRAW style and one
288 // without for windows created with wxNO_FULL_REDRAW_ON_REPAINT flag
289 static const long styleNormal
= CS_HREDRAW
| CS_VREDRAW
| CS_DBLCLKS
;
290 static const long styleNoRedraw
= CS_DBLCLKS
;
292 // the fields which are common to all classes
293 wndclass
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
294 wndclass
.cbClsExtra
= 0;
295 wndclass
.cbWndExtra
= sizeof( DWORD
); // VZ: what is this DWORD used for?
296 wndclass
.hInstance
= wxhInstance
;
297 wndclass
.hIcon
= (HICON
) NULL
;
298 wndclass
.hCursor
= ::LoadCursor((HINSTANCE
)NULL
, IDC_ARROW
);
299 wndclass
.lpszMenuName
= NULL
;
301 // Register the frame window class.
302 wndclass
.hbrBackground
= (HBRUSH
)(COLOR_APPWORKSPACE
+ 1);
303 wndclass
.lpszClassName
= wxFrameClassName
;
304 wndclass
.style
= styleNormal
;
306 if ( !RegisterClass(&wndclass
) )
308 wxLogLastError("RegisterClass(frame)");
314 wndclass
.lpszClassName
= wxFrameClassNameNoRedraw
;
315 wndclass
.style
= styleNoRedraw
;
317 if ( !RegisterClass(&wndclass
) )
319 wxLogLastError("RegisterClass(no redraw frame)");
324 // Register the MDI frame window class.
325 wndclass
.hbrBackground
= (HBRUSH
)NULL
; // paint MDI frame ourselves
326 wndclass
.lpszClassName
= wxMDIFrameClassName
;
327 wndclass
.style
= styleNormal
;
329 if ( !RegisterClass(&wndclass
) )
331 wxLogLastError("RegisterClass(MDI parent)");
336 // "no redraw" MDI frame
337 wndclass
.lpszClassName
= wxMDIFrameClassNameNoRedraw
;
338 wndclass
.style
= styleNoRedraw
;
340 if ( !RegisterClass(&wndclass
) )
342 wxLogLastError("RegisterClass(no redraw MDI parent frame)");
347 // Register the MDI child frame window class.
348 wndclass
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+ 1);
349 wndclass
.lpszClassName
= wxMDIChildFrameClassName
;
350 wndclass
.style
= styleNormal
;
352 if ( !RegisterClass(&wndclass
) )
354 wxLogLastError("RegisterClass(MDI child)");
359 // "no redraw" MDI child frame
360 wndclass
.lpszClassName
= wxMDIChildFrameClassNameNoRedraw
;
361 wndclass
.style
= styleNoRedraw
;
363 if ( !RegisterClass(&wndclass
) )
365 wxLogLastError("RegisterClass(no redraw MDI child)");
370 // Register the panel window class.
371 wndclass
.hbrBackground
= (HBRUSH
) GetStockObject( LTGRAY_BRUSH
);
372 wndclass
.lpszClassName
= wxPanelClassName
;
373 wndclass
.style
= styleNormal
;
375 if ( !RegisterClass(&wndclass
) )
377 wxLogLastError("RegisterClass(panel)");
382 // Register the canvas and textsubwindow class name
383 wndclass
.hbrBackground
= (HBRUSH
)NULL
;
384 wndclass
.lpszClassName
= wxCanvasClassName
;
386 if ( !RegisterClass(&wndclass
) )
388 wxLogLastError("RegisterClass(canvas)");
396 // ---------------------------------------------------------------------------
397 // Convert Windows to argc, argv style
398 // ---------------------------------------------------------------------------
400 void wxApp::ConvertToStandardCommandArgs(char* lpCmdLine
)
404 wxString
cmdLine(lpCmdLine
);
407 // Get application name
408 wxChar name
[260]; // 260 is MAX_PATH value from windef.h
409 ::GetModuleFileName(wxhInstance
, name
, WXSIZEOF(name
));
414 wxStrcpy(name
, wxFileNameFromPath(name
));
415 wxStripExtension(name
);
416 wxTheApp
->SetAppName(name
);
419 // Treat strings enclosed in double-quotes as single arguments
421 int len
= cmdLine
.Length();
425 while ((i
< len
) && wxIsspace(cmdLine
.GetChar(i
)))
430 if (cmdLine
.GetChar(i
) == wxT('"')) // We found the start of a string
434 while ((i
< len
) && (cmdLine
.GetChar(i
) != wxT('"')))
437 wxString
arg(cmdLine
.Mid(first
, (i
- first
)));
443 i
++; // Skip past 2nd quote
445 else // Unquoted argument
448 while ((i
< len
) && !wxIsspace(cmdLine
.GetChar(i
)))
451 wxString
arg(cmdLine
.Mid(first
, (i
- first
)));
459 wxTheApp
->argv
= new wxChar
*[count
+ 1];
460 for (i
= 0; i
< count
; i
++)
462 wxString
arg(args
[i
]);
463 wxTheApp
->argv
[i
] = copystring((const wxChar
*)arg
);
465 wxTheApp
->argv
[count
] = NULL
; // argv[] is a NULL-terminated list
466 wxTheApp
->argc
= count
;
469 //// Cleans up any wxWindows internal structures left lying around
471 void wxApp::CleanUp()
476 // flush the logged messages if any and install a 'safer' log target: the
477 // default one (wxLogGui) can't be used after the resources are freed just
478 // below and the user suppliedo ne might be even more unsafe (using any
479 // wxWindows GUI function is unsafe starting from now)
480 wxLog::DontCreateOnDemand();
482 // this will flush the old messages if any
483 delete wxLog::SetActiveTarget(new wxLogStderr
);
486 // One last chance for pending objects to be cleaned up
487 wxTheApp
->DeletePendingObjects();
489 wxModule::CleanUpModules();
491 #if wxUSE_WX_RESOURCES
492 wxCleanUpResourceSystem();
494 // wxDefaultResourceTable->ClearTable();
497 wxDeleteStockObjects();
499 // Destroy all GDI lists, etc.
500 wxDeleteStockLists();
502 delete wxTheColourDatabase
;
503 wxTheColourDatabase
= NULL
;
505 wxBitmap::CleanUpHandlers();
510 //// WINDOWS-SPECIFIC CLEANUP
512 wxSetKeyboardHook(FALSE
);
518 if (wxSTD_FRAME_ICON
)
519 DestroyIcon(wxSTD_FRAME_ICON
);
520 if (wxSTD_MDICHILDFRAME_ICON
)
521 DestroyIcon(wxSTD_MDICHILDFRAME_ICON
);
522 if (wxSTD_MDIPARENTFRAME_ICON
)
523 DestroyIcon(wxSTD_MDIPARENTFRAME_ICON
);
525 if (wxDEFAULT_FRAME_ICON
)
526 DestroyIcon(wxDEFAULT_FRAME_ICON
);
527 if (wxDEFAULT_MDICHILDFRAME_ICON
)
528 DestroyIcon(wxDEFAULT_MDICHILDFRAME_ICON
);
529 if (wxDEFAULT_MDIPARENTFRAME_ICON
)
530 DestroyIcon(wxDEFAULT_MDIPARENTFRAME_ICON
);
532 if ( wxDisableButtonBrush
)
533 ::DeleteObject( wxDisableButtonBrush
);
540 Ctl3dUnregister(wxhInstance
);
544 delete wxWinHandleList
;
546 // GL: I'm annoyed ... I don't know where to put this and I don't want to
547 // create a module for that as it's part of the core.
548 delete wxPendingEvents
;
550 delete wxPendingEventsLocker
;
551 // If we don't do the following, we get an apparent memory leak.
552 ((wxEvtHandler
&) wxDefaultValidator
).ClearEventLocker();
555 wxClassInfo::CleanUpClasses();
560 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
561 // At this point we want to check if there are any memory
562 // blocks that aren't part of the wxDebugContext itself,
563 // as a special case. Then when dumping we need to ignore
564 // wxDebugContext, too.
565 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
567 wxLogMessage(wxT("There were memory leaks."));
568 wxDebugContext::Dump();
569 wxDebugContext::PrintStatistics();
571 // wxDebugContext::SetStream(NULL, NULL);
575 // do it as the very last thing because everything else can log messages
576 delete wxLog::SetActiveTarget(NULL
);
580 #if !defined(_WINDLL) || (defined(_WINDLL) && defined(WXMAKINGDLL))
582 // temporarily disable this warning which would be generated in release builds
585 #pragma warning(disable: 4715) // not all control paths return a value
588 //// Main wxWindows entry point
589 int wxEntry(WXHINSTANCE hInstance
,
590 WXHINSTANCE
WXUNUSED(hPrevInstance
),
595 // do check for memory leaks on program exit
596 // (another useful flag is _CRTDBG_DELAY_FREE_MEM_DF which doesn't free
597 // deallocated memory which may be used to simulate low-memory condition)
598 wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF
);
600 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
601 // This seems to be necessary since there are 'rogue'
602 // objects present at this point (perhaps global objects?)
603 // Setting a checkpoint will ignore them as far as the
604 // memory checking facility is concerned.
605 // Of course you may argue that memory allocated in globals should be
606 // checked, but this is a reasonable compromise.
607 wxDebugContext::SetCheckpoint();
610 // take everything into a try-except block in release build
611 // FIXME other compilers must support Win32 SEH (structured exception
612 // handling) too, just find the appropriate keyword in their docs!
613 // Please note that it's _not_ the same as C++ exceptions!
614 #if !defined(__WXDEBUG__) && defined(__VISUALC__)
615 #define CATCH_PROGRAM_EXCEPTIONS
619 #undef CATCH_PROGRAM_EXCEPTIONS
621 wxhInstance
= (HINSTANCE
) hInstance
;
623 if (!wxApp::Initialize())
626 // create the application object or ensure that one already exists
629 // The app may have declared a global application object, but we recommend
630 // the IMPLEMENT_APP macro is used instead, which sets an initializer
631 // function for delayed, dynamic app object construction.
632 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
633 wxT("No initializer - use IMPLEMENT_APP macro.") );
635 wxTheApp
= (*wxApp::GetInitializerFunction()) ();
638 wxCHECK_MSG( wxTheApp
, 0, wxT("You have to define an instance of wxApp!") );
640 // save the WinMain() parameters
641 wxTheApp
->ConvertToStandardCommandArgs(lpCmdLine
);
642 wxTheApp
->m_nCmdShow
= nCmdShow
;
644 // GUI-specific initialisation. In fact on Windows we don't have any,
645 // but this call is provided for compatibility across platforms.
646 wxTheApp
->OnInitGui();
648 // We really don't want timestamps by default, because it means
649 // we can't simply double-click on the error message and get to that
650 // line in the source. So VC++ at least, let's have a sensible default.
652 wxLog::SetTimestamp(NULL
);
657 if ( wxTheApp
->OnInit() )
661 retValue
= wxTheApp
->OnRun();
664 // We want to initialize, but not run or exit immediately.
667 //else: app initialization failed, so we skipped OnRun()
669 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
672 // Forcibly delete the window.
673 if ( topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
674 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
676 topWindow
->Close(TRUE
);
677 wxTheApp
->DeletePendingObjects();
682 wxTheApp
->SetTopWindow(NULL
);
692 #ifdef CATCH_PROGRAM_EXCEPTIONS
694 __except ( EXCEPTION_EXECUTE_HANDLER
) {
697 wxTheApp->OnFatalException();
700 // using wxLog would be unsafe here
702 _("Unrecoverable program error detected: "
703 " the application will terminate."),
705 MB_APPLMODAL
| MB_ICONSTOP
| MB_OK
);
707 ::ExitProcess(3); // the same exit code as abort()
711 #endif // CATCH_PROGRAM_EXCEPTIONS
714 // restore warning state
716 #pragma warning(default: 4715) // not all control paths return a value
721 //// Entry point for DLLs
723 int wxEntry(WXHINSTANCE hInstance
)
725 wxhInstance
= (HINSTANCE
) hInstance
;
728 // The app may have declared a global application object, but we recommend
729 // the IMPLEMENT_APP macro is used instead, which sets an initializer function
730 // for delayed, dynamic app object construction.
733 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
734 "No initializer - use IMPLEMENT_APP macro." );
736 wxTheApp
= (* wxApp::GetInitializerFunction()) ();
739 wxCHECK_MSG( wxTheApp
, 0, "You have to define an instance of wxApp!" );
742 wxTheApp
->argv
= NULL
;
744 wxTheApp
->OnInitGui();
748 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
749 if ( topWindow
&& topWindow
->GetHWND())
751 topWindow
->Show(TRUE
);
758 //// Static member initialization
760 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
766 m_wantDebugOutput
= TRUE
;
770 m_printMode
= wxPRINT_WINDOWS
;
771 m_exitOnFrameDelete
= TRUE
;
777 // Delete command-line args
779 for (i
= 0; i
< argc
; i
++)
786 bool wxApp::Initialized()
794 #ifdef _WINDLL // Assume initialized if DLL (no way of telling)
800 * Get and process a message, returning FALSE if WM_QUIT
801 * received (and also set the flag telling the app to exit the main loop)
804 bool wxApp::DoMessage()
806 BOOL rc
= ::GetMessage(&s_currentMsg
, (HWND
) NULL
, 0, 0);
816 // should never happen, but let's test for it nevertheless
817 wxLogLastError("GetMessage");
822 wxASSERT_MSG( wxThread::IsMain(),
823 wxT("only the main thread can process Windows messages") );
825 static bool s_hadGuiLock
= TRUE
;
826 static wxMsgArray s_aSavedMessages
;
828 // if a secondary thread owns is doing GUI calls, save all messages for
829 // later processing - we can't process them right now because it will
830 // lead to recursive library calls (and we're not reentrant)
831 if ( !wxGuiOwnedByMainThread() )
833 s_hadGuiLock
= FALSE
;
835 // leave out WM_COMMAND messages: too dangerous, sometimes
836 // the message will be processed twice
837 if ( !wxIsWaitingForThread() ||
838 s_currentMsg
.message
!= WM_COMMAND
)
840 s_aSavedMessages
.Add(s_currentMsg
);
847 // have we just regained the GUI lock? if so, post all of the saved
850 // FIXME of course, it's not _exactly_ the same as processing the
851 // messages normally - expect some things to break...
856 size_t count
= s_aSavedMessages
.Count();
857 for ( size_t n
= 0; n
< count
; n
++ )
859 MSG
& msg
= s_aSavedMessages
[n
];
861 if ( !ProcessMessage((WXMSG
*)&msg
) )
863 ::TranslateMessage(&msg
);
864 ::DispatchMessage(&msg
);
868 s_aSavedMessages
.Empty();
871 #endif // wxUSE_THREADS
873 // Process the message
874 if ( !ProcessMessage((WXMSG
*)&s_currentMsg
) )
876 ::TranslateMessage(&s_currentMsg
);
877 ::DispatchMessage(&s_currentMsg
);
885 * Keep trying to process messages until WM_QUIT
888 * If there are messages to be processed, they will all be
889 * processed and OnIdle will not be called.
890 * When there are no more messages, OnIdle is called.
891 * If OnIdle requests more time,
892 * it will be repeatedly called so long as there are no pending messages.
893 * A 'feature' of this is that once OnIdle has decided that no more processing
894 * is required, then it won't get processing time until further messages
895 * are processed (it'll sit in DoMessage).
898 int wxApp::MainLoop()
902 while ( m_keepGoing
)
905 wxMutexGuiLeaveOrEnter();
906 #endif // wxUSE_THREADS
908 while ( !Pending() && ProcessIdle() )
911 // a message came or no more idle processing to do
915 return s_currentMsg
.wParam
;
918 // Returns TRUE if more time is needed.
919 bool wxApp::ProcessIdle()
922 event
.SetEventObject(this);
925 return event
.MoreRequested();
928 void wxApp::ExitMainLoop()
930 // VZ: why not ::PostQuitMessage()?
934 bool wxApp::Pending()
936 return ::PeekMessage(&s_currentMsg
, 0, 0, 0, PM_NOREMOVE
) != 0;
939 void wxApp::Dispatch()
945 * Give all windows a chance to preprocess
946 * the message. Some may have accelerator tables, or have
950 bool wxApp::ProcessMessage(WXMSG
*wxmsg
)
952 MSG
*msg
= (MSG
*)wxmsg
;
953 HWND hWnd
= msg
->hwnd
;
954 wxWindow
*wndThis
= wxGetWindowFromHWND((WXHWND
)hWnd
);
957 // we must relay WM_MOUSEMOVE events to the tooltip ctrl if we want it to
958 // popup the tooltip bubbles
959 if ( wndThis
&& (msg
->message
== WM_MOUSEMOVE
) )
961 wxToolTip
*tt
= wndThis
->GetToolTip();
964 tt
->RelayEvent(wxmsg
);
967 #endif // wxUSE_TOOLTIPS
969 // Try translations first; find the youngest window with
970 // a translation table.
972 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
974 if ( wnd
->MSWTranslateMessage(wxmsg
) )
978 // Anyone for a non-translation message? Try youngest descendants first.
979 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
981 if ( wnd
->MSWProcessMessage(wxmsg
) )
988 void wxApp::OnIdle(wxIdleEvent
& event
)
990 static bool s_inOnIdle
= FALSE
;
992 // Avoid recursion (via ProcessEvent default case)
998 // If there are pending events, we must process them: pending events
999 // are either events to the threads other than main or events posted
1000 // with wxPostEvent() functions
1001 // GRG: I have moved this here so that all pending events are processed
1002 // before starting to delete any objects. This behaves better (in
1003 // particular, wrt wxPostEvent) and is coherent with wxGTK's current
1004 // behaviour. Changed Feb/2000 before 2.1.14
1005 ProcessPendingEvents();
1007 // 'Garbage' collection of windows deleted with Close().
1008 DeletePendingObjects();
1011 // flush the logged messages if any
1012 wxLog::FlushActive();
1015 // Send OnIdle events to all windows
1016 if ( SendIdleEvents() )
1018 // SendIdleEvents() returns TRUE if at least one window requested more
1020 event
.RequestMore(TRUE
);
1026 // Send idle event to all top-level windows
1027 bool wxApp::SendIdleEvents()
1029 bool needMore
= FALSE
;
1031 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
1034 wxWindow
* win
= node
->GetData();
1035 if (SendIdleEvents(win
))
1037 node
= node
->GetNext();
1043 // Send idle event to window and all subwindows
1044 bool wxApp::SendIdleEvents(wxWindow
* win
)
1046 bool needMore
= FALSE
;
1049 event
.SetEventObject(win
);
1050 win
->GetEventHandler()->ProcessEvent(event
);
1052 if (event
.MoreRequested())
1055 wxNode
* node
= win
->GetChildren().First();
1058 wxWindow
* win
= (wxWindow
*) node
->Data();
1059 if (SendIdleEvents(win
))
1062 node
= node
->Next();
1067 void wxApp::DeletePendingObjects()
1069 wxNode
*node
= wxPendingDelete
.First();
1072 wxObject
*obj
= (wxObject
*)node
->Data();
1076 if (wxPendingDelete
.Member(obj
))
1079 // Deleting one object may have deleted other pending
1080 // objects, so start from beginning of list again.
1081 node
= wxPendingDelete
.First();
1085 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
1088 GetTopWindow()->Close(TRUE
);
1091 // Default behaviour: close the application with prompts. The
1092 // user can veto the close, and therefore the end session.
1093 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
1097 if (!GetTopWindow()->Close(!event
.CanVeto()))
1103 int wxApp::GetComCtl32Version()
1106 static int s_verComCtl32
= -1;
1108 wxCRIT_SECT_DECLARE(csComCtl32
);
1109 wxCRIT_SECT_LOCKER(lock
, csComCtl32
);
1111 if ( s_verComCtl32
== -1 )
1113 // initally assume no comctl32.dll at all
1117 HMODULE hModuleComCtl32
= ::GetModuleHandle(wxT("COMCTL32"));
1119 // if so, then we can check for the version
1120 if ( hModuleComCtl32
)
1122 // try to use DllGetVersion() if available in _headers_
1123 #ifdef DLLVER_PLATFORM_WINDOWS // defined in shlwapi.h
1124 DLLGETVERSIONPROC pfnDllGetVersion
= (DLLGETVERSIONPROC
)
1125 ::GetProcAddress(hModuleComCtl32
, _T("DllGetVersion"));
1126 if ( pfnDllGetVersion
)
1129 dvi
.cbSize
= sizeof(dvi
);
1131 HRESULT hr
= (*pfnDllGetVersion
)(&dvi
);
1134 wxLogApiError(_T("DllGetVersion"), hr
);
1138 // this is incompatible with _WIN32_IE values, but
1139 // compatible with the other values returned by
1140 // GetComCtl32Version()
1141 s_verComCtl32
= 100*dvi
.dwMajorVersion
+
1146 // DllGetVersion() unavailable either during compile or
1147 // run-time, try to guess the version otherwise
1148 if ( !s_verComCtl32
)
1150 // InitCommonControlsEx is unique to 4.70 and later
1151 FARPROC theProc
= ::GetProcAddress
1154 #if defined(__BORLANDC__) && (__BORLANDC__ <= 0x520)
1155 "InitCommonControlsEx"
1157 _T("InitCommonControlsEx")
1163 // not found, must be 4.00
1164 s_verComCtl32
= 400;
1168 // many symbols appeared in comctl32 4.71, could use
1169 // any of them except may be DllInstall
1170 theProc
= ::GetProcAddress
1173 #if defined(__BORLANDC__) && (__BORLANDC__ <= 0x520)
1176 _T("InitializeFlatSB")
1181 // not found, must be 4.70
1182 s_verComCtl32
= 470;
1186 // found, must be 4.71
1187 s_verComCtl32
= 471;
1194 return s_verComCtl32
;
1199 wxLogError(_("Fatal error: exiting"));
1204 // Yield to incoming messages
1207 // disable log flushing from here because a call to wxYield() shouldn't
1208 // normally result in message boxes popping up &c
1211 // we don't want to process WM_QUIT from here - it should be processed in
1212 // the main event loop in order to stop it
1214 while ( PeekMessage(&msg
, (HWND
)0, 0, 0, PM_NOREMOVE
) &&
1215 msg
.message
!= WM_QUIT
)
1218 wxMutexGuiLeaveOrEnter();
1219 #endif // wxUSE_THREADS
1221 if ( !wxTheApp
->DoMessage() )
1225 // If they are pending events, we must process them.
1226 wxTheApp
->ProcessPendingEvents();
1228 // let the logs be flashed again
1234 //-----------------------------------------------------------------------------
1236 //-----------------------------------------------------------------------------
1240 // Send the top window a dummy message so idle handler processing will
1241 // start up again. Doing it this way ensures that the idle handler
1242 // wakes up in the right thread (see also wxWakeUpMainThread() which does
1243 // the same for the main app thread only)
1244 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
1247 if ( !::PostMessage(GetHwndOf(topWindow
), WM_NULL
, 0, 0) )
1249 // should never happen
1250 wxLogLastError("PostMessage(WM_NULL)");
1255 //-----------------------------------------------------------------------------
1258 wxApp::GetStdIcon(int which
) const
1262 case wxICON_INFORMATION
:
1263 return wxIcon("wxICON_INFO");
1265 case wxICON_QUESTION
:
1266 return wxIcon("wxICON_QUESTION");
1268 case wxICON_EXCLAMATION
:
1269 return wxIcon("wxICON_WARNING");
1272 wxFAIL_MSG(wxT("requested non existent standard icon"));
1273 // still fall through
1276 return wxIcon("wxICON_ERROR");
1280 // For some reason, with MSVC++ 1.5, WinMain isn't linked in properly
1281 // if in a separate file. So include it here to ensure it's linked.
1282 #if (defined(__VISUALC__) && !defined(__WIN32__)) || (defined(__GNUWIN32__) && !defined(__TWIN32__) && !defined(WXMAKINGDLL))