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
*wxPanelClassNameNR
= wxT("wxPanelClassNR");
143 const wxChar
*wxCanvasClassName
= wxT("wxCanvasClass");
144 const wxChar
*wxCanvasClassNameNR
= wxT("wxCanvasClassNR");
146 HICON wxSTD_FRAME_ICON
= (HICON
) NULL
;
147 HICON wxSTD_MDICHILDFRAME_ICON
= (HICON
) NULL
;
148 HICON wxSTD_MDIPARENTFRAME_ICON
= (HICON
) NULL
;
150 HICON wxDEFAULT_FRAME_ICON
= (HICON
) NULL
;
151 HICON wxDEFAULT_MDICHILDFRAME_ICON
= (HICON
) NULL
;
152 HICON wxDEFAULT_MDIPARENTFRAME_ICON
= (HICON
) NULL
;
154 HBRUSH wxDisableButtonBrush
= (HBRUSH
) 0;
156 LRESULT WXDLLEXPORT APIENTRY
wxWndProc(HWND
, UINT
, WPARAM
, LPARAM
);
158 // FIXME wxUSE_ON_FATAL_EXCEPTION is only supported for VC++ now because it
159 // needs compiler support for Win32 SEH. Others (especially Borland)
160 // probably have it too, but I'm not sure about how it works
162 #undef wxUSE_ON_FATAL_EXCEPTION
163 #define wxUSE_ON_FATAL_EXCEPTION 0
166 #if wxUSE_ON_FATAL_EXCEPTION
167 static bool gs_handleExceptions
= FALSE
;
170 // ===========================================================================
172 // ===========================================================================
174 // ---------------------------------------------------------------------------
176 // ---------------------------------------------------------------------------
178 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
180 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
181 EVT_IDLE(wxApp::OnIdle
)
182 EVT_END_SESSION(wxApp::OnEndSession
)
183 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession
)
187 bool wxApp::Initialize()
189 // Some people may wish to use this, but
190 // probably it shouldn't be here by default.
192 // wxRedirectIOToConsole();
195 wxBuffer
= new wxChar
[1500]; // FIXME
197 wxClassInfo::InitializeClasses();
200 wxGetResource(wxT("wxWindows"), wxT("OsVersion"), &wxOsVersion
);
204 wxPendingEventsLocker
= new wxCriticalSection
;
207 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
208 wxTheColourDatabase
->Initialize();
210 wxInitializeStockLists();
211 wxInitializeStockObjects();
213 #if wxUSE_WX_RESOURCES
214 wxInitializeResourceSystem();
217 wxBitmap::InitStandardHandlers();
219 #if defined(__WIN95__)
220 InitCommonControls();
227 // for OLE, enlarge message queue to be as large as possible
229 while (!SetMessageQueue(iMsg
) && (iMsg
-= 8))
232 // we need to initialize OLE library
233 if ( FAILED(::OleInitialize(NULL
)) )
234 wxLogError(_("Cannot initialize OLE"));
238 if (!Ctl3dRegister(wxhInstance
))
239 wxLogError(wxT("Cannot register CTL3D"));
241 Ctl3dAutoSubclass(wxhInstance
);
244 // VZ: these icons are not in wx.rc anyhow (but should they?)!
246 wxSTD_FRAME_ICON
= LoadIcon(wxhInstance
, wxT("wxSTD_FRAME"));
247 wxSTD_MDIPARENTFRAME_ICON
= LoadIcon(wxhInstance
, wxT("wxSTD_MDIPARENTFRAME"));
248 wxSTD_MDICHILDFRAME_ICON
= LoadIcon(wxhInstance
, wxT("wxSTD_MDICHILDFRAME"));
250 wxDEFAULT_FRAME_ICON
= LoadIcon(wxhInstance
, wxT("wxDEFAULT_FRAME"));
251 wxDEFAULT_MDIPARENTFRAME_ICON
= LoadIcon(wxhInstance
, wxT("wxDEFAULT_MDIPARENTFRAME"));
252 wxDEFAULT_MDICHILDFRAME_ICON
= LoadIcon(wxhInstance
, wxT("wxDEFAULT_MDICHILDFRAME"));
255 RegisterWindowClasses();
257 // Create the brush for disabling bitmap buttons
260 lb
.lbStyle
= BS_PATTERN
;
261 lb
.lbHatch
= (int)LoadBitmap( wxhInstance
, wxT("wxDISABLE_BUTTON_BITMAP") );
264 wxDisableButtonBrush
= ::CreateBrushIndirect( & lb
);
265 ::DeleteObject( (HGDIOBJ
)lb
.lbHatch
);
267 //else: wxWindows resources are probably not linked in
273 wxWinHandleList
= new wxList(wxKEY_INTEGER
);
275 // This is to foil optimizations in Visual C++ that throw out dummy.obj.
276 // PLEASE DO NOT ALTER THIS.
277 #if defined(__VISUALC__) && defined(__WIN16__) && !defined(WXMAKINGDLL)
278 extern char wxDummyChar
;
279 if (wxDummyChar
) wxDummyChar
++;
282 wxSetKeyboardHook(TRUE
);
284 wxModule::RegisterModules();
285 if (!wxModule::InitializeModules())
290 // ---------------------------------------------------------------------------
291 // RegisterWindowClasses
292 // ---------------------------------------------------------------------------
294 // TODO we should only register classes really used by the app. For this it
295 // would be enough to just delay the class registration until an attempt
296 // to create a window of this class is made.
297 bool wxApp::RegisterWindowClasses()
301 // for each class we register one with CS_(V|H)REDRAW style and one
302 // without for windows created with wxNO_FULL_REDRAW_ON_REPAINT flag
303 static const long styleNormal
= CS_HREDRAW
| CS_VREDRAW
| CS_DBLCLKS
;
304 static const long styleNoRedraw
= CS_DBLCLKS
;
306 // the fields which are common to all classes
307 wndclass
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
308 wndclass
.cbClsExtra
= 0;
309 wndclass
.cbWndExtra
= sizeof( DWORD
); // VZ: what is this DWORD used for?
310 wndclass
.hInstance
= wxhInstance
;
311 wndclass
.hIcon
= (HICON
) NULL
;
312 wndclass
.hCursor
= ::LoadCursor((HINSTANCE
)NULL
, IDC_ARROW
);
313 wndclass
.lpszMenuName
= NULL
;
315 // Register the frame window class.
316 wndclass
.hbrBackground
= (HBRUSH
)(COLOR_APPWORKSPACE
+ 1);
317 wndclass
.lpszClassName
= wxFrameClassName
;
318 wndclass
.style
= styleNormal
;
320 if ( !RegisterClass(&wndclass
) )
322 wxLogLastError("RegisterClass(frame)");
328 wndclass
.lpszClassName
= wxFrameClassNameNoRedraw
;
329 wndclass
.style
= styleNoRedraw
;
331 if ( !RegisterClass(&wndclass
) )
333 wxLogLastError("RegisterClass(no redraw frame)");
338 // Register the MDI frame window class.
339 wndclass
.hbrBackground
= (HBRUSH
)NULL
; // paint MDI frame ourselves
340 wndclass
.lpszClassName
= wxMDIFrameClassName
;
341 wndclass
.style
= styleNormal
;
343 if ( !RegisterClass(&wndclass
) )
345 wxLogLastError("RegisterClass(MDI parent)");
350 // "no redraw" MDI frame
351 wndclass
.lpszClassName
= wxMDIFrameClassNameNoRedraw
;
352 wndclass
.style
= styleNoRedraw
;
354 if ( !RegisterClass(&wndclass
) )
356 wxLogLastError("RegisterClass(no redraw MDI parent frame)");
361 // Register the MDI child frame window class.
362 wndclass
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+ 1);
363 wndclass
.lpszClassName
= wxMDIChildFrameClassName
;
364 wndclass
.style
= styleNormal
;
366 if ( !RegisterClass(&wndclass
) )
368 wxLogLastError("RegisterClass(MDI child)");
373 // "no redraw" MDI child frame
374 wndclass
.lpszClassName
= wxMDIChildFrameClassNameNoRedraw
;
375 wndclass
.style
= styleNoRedraw
;
377 if ( !RegisterClass(&wndclass
) )
379 wxLogLastError("RegisterClass(no redraw MDI child)");
384 // Register the panel window class.
385 wndclass
.hbrBackground
= (HBRUSH
) GetStockObject( LTGRAY_BRUSH
);
386 wndclass
.lpszClassName
= wxPanelClassName
;
387 wndclass
.style
= styleNormal
;
389 if ( !RegisterClass(&wndclass
) )
391 wxLogLastError("RegisterClass(panel)");
396 // Register the no redraw panel window class.
397 wndclass
.lpszClassName
= wxPanelClassNameNR
;
398 wndclass
.style
= styleNoRedraw
;
400 if ( !RegisterClass(&wndclass
) )
402 wxLogLastError("RegisterClass(no redraw panel)");
407 // Register the canvas and textsubwindow class name
408 wndclass
.hbrBackground
= (HBRUSH
)NULL
;
409 wndclass
.lpszClassName
= wxCanvasClassName
;
411 if ( !RegisterClass(&wndclass
) )
413 wxLogLastError("RegisterClass(canvas)");
418 wndclass
.lpszClassName
= wxCanvasClassNameNR
;
419 wndclass
.style
= styleNoRedraw
;
420 if ( !RegisterClass(&wndclass
) )
422 wxLogLastError("RegisterClass(no redraw canvas)");
430 // ---------------------------------------------------------------------------
431 // Convert Windows to argc, argv style
432 // ---------------------------------------------------------------------------
434 void wxApp::ConvertToStandardCommandArgs(char* lpCmdLine
)
438 wxString
cmdLine(lpCmdLine
);
441 // Get application name
442 wxChar name
[260]; // 260 is MAX_PATH value from windef.h
443 ::GetModuleFileName(wxhInstance
, name
, WXSIZEOF(name
));
448 wxStrcpy(name
, wxFileNameFromPath(name
));
449 wxStripExtension(name
);
450 wxTheApp
->SetAppName(name
);
453 // Treat strings enclosed in double-quotes as single arguments
455 int len
= cmdLine
.Length();
459 while ((i
< len
) && wxIsspace(cmdLine
.GetChar(i
)))
464 if (cmdLine
.GetChar(i
) == wxT('"')) // We found the start of a string
468 while ((i
< len
) && (cmdLine
.GetChar(i
) != wxT('"')))
471 wxString
arg(cmdLine
.Mid(first
, (i
- first
)));
477 i
++; // Skip past 2nd quote
479 else // Unquoted argument
482 while ((i
< len
) && !wxIsspace(cmdLine
.GetChar(i
)))
485 wxString
arg(cmdLine
.Mid(first
, (i
- first
)));
493 wxTheApp
->argv
= new wxChar
*[count
+ 1];
494 for (i
= 0; i
< count
; i
++)
496 wxString
arg(args
[i
]);
497 wxTheApp
->argv
[i
] = copystring((const wxChar
*)arg
);
499 wxTheApp
->argv
[count
] = NULL
; // argv[] is a NULL-terminated list
500 wxTheApp
->argc
= count
;
503 //// Cleans up any wxWindows internal structures left lying around
505 void wxApp::CleanUp()
510 // flush the logged messages if any and install a 'safer' log target: the
511 // default one (wxLogGui) can't be used after the resources are freed just
512 // below and the user suppliedo ne might be even more unsafe (using any
513 // wxWindows GUI function is unsafe starting from now)
514 wxLog::DontCreateOnDemand();
516 // this will flush the old messages if any
517 delete wxLog::SetActiveTarget(new wxLogStderr
);
520 // One last chance for pending objects to be cleaned up
521 wxTheApp
->DeletePendingObjects();
523 wxModule::CleanUpModules();
525 #if wxUSE_WX_RESOURCES
526 wxCleanUpResourceSystem();
528 // wxDefaultResourceTable->ClearTable();
531 wxDeleteStockObjects();
533 // Destroy all GDI lists, etc.
534 wxDeleteStockLists();
536 delete wxTheColourDatabase
;
537 wxTheColourDatabase
= NULL
;
539 wxBitmap::CleanUpHandlers();
544 //// WINDOWS-SPECIFIC CLEANUP
546 wxSetKeyboardHook(FALSE
);
552 if (wxSTD_FRAME_ICON
)
553 DestroyIcon(wxSTD_FRAME_ICON
);
554 if (wxSTD_MDICHILDFRAME_ICON
)
555 DestroyIcon(wxSTD_MDICHILDFRAME_ICON
);
556 if (wxSTD_MDIPARENTFRAME_ICON
)
557 DestroyIcon(wxSTD_MDIPARENTFRAME_ICON
);
559 if (wxDEFAULT_FRAME_ICON
)
560 DestroyIcon(wxDEFAULT_FRAME_ICON
);
561 if (wxDEFAULT_MDICHILDFRAME_ICON
)
562 DestroyIcon(wxDEFAULT_MDICHILDFRAME_ICON
);
563 if (wxDEFAULT_MDIPARENTFRAME_ICON
)
564 DestroyIcon(wxDEFAULT_MDIPARENTFRAME_ICON
);
566 if ( wxDisableButtonBrush
)
567 ::DeleteObject( wxDisableButtonBrush
);
574 Ctl3dUnregister(wxhInstance
);
578 delete wxWinHandleList
;
580 // GL: I'm annoyed ... I don't know where to put this and I don't want to
581 // create a module for that as it's part of the core.
582 delete wxPendingEvents
;
584 delete wxPendingEventsLocker
;
585 // If we don't do the following, we get an apparent memory leak.
586 ((wxEvtHandler
&) wxDefaultValidator
).ClearEventLocker();
589 wxClassInfo::CleanUpClasses();
594 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
595 // At this point we want to check if there are any memory
596 // blocks that aren't part of the wxDebugContext itself,
597 // as a special case. Then when dumping we need to ignore
598 // wxDebugContext, too.
599 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
601 wxLogMessage(wxT("There were memory leaks."));
602 wxDebugContext::Dump();
603 wxDebugContext::PrintStatistics();
605 // wxDebugContext::SetStream(NULL, NULL);
609 // do it as the very last thing because everything else can log messages
610 delete wxLog::SetActiveTarget(NULL
);
614 //----------------------------------------------------------------------
615 // Entry point helpers, used by wxPython
616 //----------------------------------------------------------------------
618 int WXDLLEXPORT
wxEntryStart( int WXUNUSED(argc
), char** WXUNUSED(argv
) )
620 return wxApp::Initialize();
623 int WXDLLEXPORT
wxEntryInitGui()
625 wxTheApp
->OnInitGui();
629 void WXDLLEXPORT
wxEntryCleanup()
635 #if !defined(_WINDLL) || (defined(_WINDLL) && defined(WXMAKINGDLL))
637 // temporarily disable this warning which would be generated in release builds
640 #pragma warning(disable: 4715) // not all control paths return a value
643 //----------------------------------------------------------------------
644 // Main wxWindows entry point
645 //----------------------------------------------------------------------
646 int wxEntry(WXHINSTANCE hInstance
,
647 WXHINSTANCE
WXUNUSED(hPrevInstance
),
652 // do check for memory leaks on program exit
653 // (another useful flag is _CRTDBG_DELAY_FREE_MEM_DF which doesn't free
654 // deallocated memory which may be used to simulate low-memory condition)
655 wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF
);
657 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
658 // This seems to be necessary since there are 'rogue'
659 // objects present at this point (perhaps global objects?)
660 // Setting a checkpoint will ignore them as far as the
661 // memory checking facility is concerned.
662 // Of course you may argue that memory allocated in globals should be
663 // checked, but this is a reasonable compromise.
664 wxDebugContext::SetCheckpoint();
668 // take everything into a try-except block to be able to call
669 // OnFatalException() if necessary
670 #if wxUSE_ON_FATAL_EXCEPTION
673 wxhInstance
= (HINSTANCE
) hInstance
;
675 if (!wxEntryStart(0,0))
678 // create the application object or ensure that one already exists
681 // The app may have declared a global application object, but we recommend
682 // the IMPLEMENT_APP macro is used instead, which sets an initializer
683 // function for delayed, dynamic app object construction.
684 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
685 wxT("No initializer - use IMPLEMENT_APP macro.") );
687 wxTheApp
= (*wxApp::GetInitializerFunction()) ();
690 wxCHECK_MSG( wxTheApp
, 0, wxT("You have to define an instance of wxApp!") );
692 // save the WinMain() parameters
693 wxTheApp
->ConvertToStandardCommandArgs(lpCmdLine
);
694 wxTheApp
->m_nCmdShow
= nCmdShow
;
696 // GUI-specific initialisation. In fact on Windows we don't have any,
697 // but this call is provided for compatibility across platforms.
700 // We really don't want timestamps by default, because it means
701 // we can't simply double-click on the error message and get to that
702 // line in the source. So VC++ at least, let's have a sensible default.
704 wxLog::SetTimestamp(NULL
);
709 if ( wxTheApp
->OnInit() )
713 retValue
= wxTheApp
->OnRun();
716 // We want to initialize, but not run or exit immediately.
719 //else: app initialization failed, so we skipped OnRun()
721 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
724 // Forcibly delete the window.
725 if ( topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
726 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
728 topWindow
->Close(TRUE
);
729 wxTheApp
->DeletePendingObjects();
734 wxTheApp
->SetTopWindow(NULL
);
744 #if wxUSE_ON_FATAL_EXCEPTION
746 __except ( gs_handleExceptions
? EXCEPTION_EXECUTE_HANDLER
747 : EXCEPTION_CONTINUE_SEARCH
) {
749 wxTheApp
->OnFatalException();
751 // using wxLog would be unsafe here
753 _("Unrecoverable program error detected: "
754 " the application will terminate."),
756 MB_APPLMODAL
| MB_ICONSTOP
| MB_OK
);
758 ::ExitProcess(3); // the same exit code as abort()
762 #endif // wxUSE_ON_FATAL_EXCEPTION
765 // restore warning state
767 #pragma warning(default: 4715) // not all control paths return a value
772 //----------------------------------------------------------------------
773 // Entry point for wxWindows + the App in a DLL
774 //----------------------------------------------------------------------
776 int wxEntry(WXHINSTANCE hInstance
)
778 wxhInstance
= (HINSTANCE
) hInstance
;
781 // The app may have declared a global application object, but we recommend
782 // the IMPLEMENT_APP macro is used instead, which sets an initializer function
783 // for delayed, dynamic app object construction.
786 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
787 "No initializer - use IMPLEMENT_APP macro." );
789 wxTheApp
= (* wxApp::GetInitializerFunction()) ();
792 wxCHECK_MSG( wxTheApp
, 0, "You have to define an instance of wxApp!" );
795 wxTheApp
->argv
= NULL
;
801 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
802 if ( topWindow
&& topWindow
->GetHWND())
804 topWindow
->Show(TRUE
);
811 //// Static member initialization
813 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
819 m_wantDebugOutput
= TRUE
;
823 m_printMode
= wxPRINT_WINDOWS
;
824 m_exitOnFrameDelete
= TRUE
;
830 // Delete command-line args
832 for (i
= 0; i
< argc
; i
++)
839 bool wxApp::Initialized()
846 #else // Assume initialized if DLL (no way of telling)
852 * Get and process a message, returning FALSE if WM_QUIT
853 * received (and also set the flag telling the app to exit the main loop)
856 bool wxApp::DoMessage()
858 BOOL rc
= ::GetMessage(&s_currentMsg
, (HWND
) NULL
, 0, 0);
868 // should never happen, but let's test for it nevertheless
869 wxLogLastError("GetMessage");
874 wxASSERT_MSG( wxThread::IsMain(),
875 wxT("only the main thread can process Windows messages") );
877 static bool s_hadGuiLock
= TRUE
;
878 static wxMsgArray s_aSavedMessages
;
880 // if a secondary thread owns is doing GUI calls, save all messages for
881 // later processing - we can't process them right now because it will
882 // lead to recursive library calls (and we're not reentrant)
883 if ( !wxGuiOwnedByMainThread() )
885 s_hadGuiLock
= FALSE
;
887 // leave out WM_COMMAND messages: too dangerous, sometimes
888 // the message will be processed twice
889 if ( !wxIsWaitingForThread() ||
890 s_currentMsg
.message
!= WM_COMMAND
)
892 s_aSavedMessages
.Add(s_currentMsg
);
899 // have we just regained the GUI lock? if so, post all of the saved
902 // FIXME of course, it's not _exactly_ the same as processing the
903 // messages normally - expect some things to break...
908 size_t count
= s_aSavedMessages
.Count();
909 for ( size_t n
= 0; n
< count
; n
++ )
911 MSG
& msg
= s_aSavedMessages
[n
];
913 if ( !ProcessMessage((WXMSG
*)&msg
) )
915 ::TranslateMessage(&msg
);
916 ::DispatchMessage(&msg
);
920 s_aSavedMessages
.Empty();
923 #endif // wxUSE_THREADS
925 // Process the message
926 if ( !ProcessMessage((WXMSG
*)&s_currentMsg
) )
928 ::TranslateMessage(&s_currentMsg
);
929 ::DispatchMessage(&s_currentMsg
);
937 * Keep trying to process messages until WM_QUIT
940 * If there are messages to be processed, they will all be
941 * processed and OnIdle will not be called.
942 * When there are no more messages, OnIdle is called.
943 * If OnIdle requests more time,
944 * it will be repeatedly called so long as there are no pending messages.
945 * A 'feature' of this is that once OnIdle has decided that no more processing
946 * is required, then it won't get processing time until further messages
947 * are processed (it'll sit in DoMessage).
950 int wxApp::MainLoop()
954 while ( m_keepGoing
)
957 wxMutexGuiLeaveOrEnter();
958 #endif // wxUSE_THREADS
960 while ( !Pending() && ProcessIdle() )
963 // a message came or no more idle processing to do
967 return s_currentMsg
.wParam
;
970 // Returns TRUE if more time is needed.
971 bool wxApp::ProcessIdle()
974 event
.SetEventObject(this);
977 return event
.MoreRequested();
980 void wxApp::ExitMainLoop()
982 // VZ: why not ::PostQuitMessage()?
986 bool wxApp::Pending()
988 return ::PeekMessage(&s_currentMsg
, 0, 0, 0, PM_NOREMOVE
) != 0;
991 void wxApp::Dispatch()
997 * Give all windows a chance to preprocess
998 * the message. Some may have accelerator tables, or have
1002 bool wxApp::ProcessMessage(WXMSG
*wxmsg
)
1004 MSG
*msg
= (MSG
*)wxmsg
;
1005 HWND hWnd
= msg
->hwnd
;
1006 wxWindow
*wndThis
= wxGetWindowFromHWND((WXHWND
)hWnd
);
1009 // we must relay WM_MOUSEMOVE events to the tooltip ctrl if we want it to
1010 // popup the tooltip bubbles
1011 if ( wndThis
&& (msg
->message
== WM_MOUSEMOVE
) )
1013 wxToolTip
*tt
= wndThis
->GetToolTip();
1016 tt
->RelayEvent(wxmsg
);
1019 #endif // wxUSE_TOOLTIPS
1021 // Try translations first; find the youngest window with
1022 // a translation table.
1024 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
1026 if ( wnd
->MSWTranslateMessage(wxmsg
) )
1030 // Anyone for a non-translation message? Try youngest descendants first.
1031 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
1033 if ( wnd
->MSWProcessMessage(wxmsg
) )
1040 void wxApp::OnIdle(wxIdleEvent
& event
)
1042 static bool s_inOnIdle
= FALSE
;
1044 // Avoid recursion (via ProcessEvent default case)
1050 // If there are pending events, we must process them: pending events
1051 // are either events to the threads other than main or events posted
1052 // with wxPostEvent() functions
1053 // GRG: I have moved this here so that all pending events are processed
1054 // before starting to delete any objects. This behaves better (in
1055 // particular, wrt wxPostEvent) and is coherent with wxGTK's current
1056 // behaviour. Changed Feb/2000 before 2.1.14
1057 ProcessPendingEvents();
1059 // 'Garbage' collection of windows deleted with Close().
1060 DeletePendingObjects();
1063 // flush the logged messages if any
1064 wxLog::FlushActive();
1067 // Send OnIdle events to all windows
1068 if ( SendIdleEvents() )
1070 // SendIdleEvents() returns TRUE if at least one window requested more
1072 event
.RequestMore(TRUE
);
1078 // Send idle event to all top-level windows
1079 bool wxApp::SendIdleEvents()
1081 bool needMore
= FALSE
;
1083 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
1086 wxWindow
* win
= node
->GetData();
1087 if (SendIdleEvents(win
))
1089 node
= node
->GetNext();
1095 // Send idle event to window and all subwindows
1096 bool wxApp::SendIdleEvents(wxWindow
* win
)
1098 bool needMore
= FALSE
;
1101 event
.SetEventObject(win
);
1102 win
->GetEventHandler()->ProcessEvent(event
);
1104 if (event
.MoreRequested())
1107 wxNode
* node
= win
->GetChildren().First();
1110 wxWindow
* win
= (wxWindow
*) node
->Data();
1111 if (SendIdleEvents(win
))
1114 node
= node
->Next();
1119 void wxApp::DeletePendingObjects()
1121 wxNode
*node
= wxPendingDelete
.First();
1124 wxObject
*obj
= (wxObject
*)node
->Data();
1128 if (wxPendingDelete
.Member(obj
))
1131 // Deleting one object may have deleted other pending
1132 // objects, so start from beginning of list again.
1133 node
= wxPendingDelete
.First();
1137 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
1140 GetTopWindow()->Close(TRUE
);
1143 // Default behaviour: close the application with prompts. The
1144 // user can veto the close, and therefore the end session.
1145 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
1149 if (!GetTopWindow()->Close(!event
.CanVeto()))
1155 int wxApp::GetComCtl32Version()
1158 static int s_verComCtl32
= -1;
1160 wxCRIT_SECT_DECLARE(csComCtl32
);
1161 wxCRIT_SECT_LOCKER(lock
, csComCtl32
);
1163 if ( s_verComCtl32
== -1 )
1165 // initally assume no comctl32.dll at all
1169 HMODULE hModuleComCtl32
= ::GetModuleHandle(wxT("COMCTL32"));
1171 // if so, then we can check for the version
1172 if ( hModuleComCtl32
)
1174 // try to use DllGetVersion() if available in _headers_
1175 #ifdef DLLVER_PLATFORM_WINDOWS // defined in shlwapi.h
1176 DLLGETVERSIONPROC pfnDllGetVersion
= (DLLGETVERSIONPROC
)
1177 ::GetProcAddress(hModuleComCtl32
, _T("DllGetVersion"));
1178 if ( pfnDllGetVersion
)
1181 dvi
.cbSize
= sizeof(dvi
);
1183 HRESULT hr
= (*pfnDllGetVersion
)(&dvi
);
1186 wxLogApiError(_T("DllGetVersion"), hr
);
1190 // this is incompatible with _WIN32_IE values, but
1191 // compatible with the other values returned by
1192 // GetComCtl32Version()
1193 s_verComCtl32
= 100*dvi
.dwMajorVersion
+
1198 // DllGetVersion() unavailable either during compile or
1199 // run-time, try to guess the version otherwise
1200 if ( !s_verComCtl32
)
1202 // InitCommonControlsEx is unique to 4.70 and later
1203 FARPROC theProc
= ::GetProcAddress
1206 #if defined(__BORLANDC__) && (__BORLANDC__ <= 0x520)
1207 "InitCommonControlsEx"
1209 _T("InitCommonControlsEx")
1215 // not found, must be 4.00
1216 s_verComCtl32
= 400;
1220 // many symbols appeared in comctl32 4.71, could use
1221 // any of them except may be DllInstall
1222 theProc
= ::GetProcAddress
1225 #if defined(__BORLANDC__) && (__BORLANDC__ <= 0x520)
1228 _T("InitializeFlatSB")
1233 // not found, must be 4.70
1234 s_verComCtl32
= 470;
1238 // found, must be 4.71
1239 s_verComCtl32
= 471;
1246 return s_verComCtl32
;
1251 wxLogError(_("Fatal error: exiting"));
1257 // Yield to incoming messages
1260 // disable log flushing from here because a call to wxYield() shouldn't
1261 // normally result in message boxes popping up &c
1264 // we don't want to process WM_QUIT from here - it should be processed in
1265 // the main event loop in order to stop it
1267 while ( PeekMessage(&msg
, (HWND
)0, 0, 0, PM_NOREMOVE
) &&
1268 msg
.message
!= WM_QUIT
)
1271 wxMutexGuiLeaveOrEnter();
1272 #endif // wxUSE_THREADS
1274 if ( !wxTheApp
->DoMessage() )
1278 // If they are pending events, we must process them.
1280 wxTheApp
->ProcessPendingEvents();
1282 // let the logs be flashed again
1288 bool wxHandleFatalExceptions(bool doit
)
1290 #if wxUSE_ON_FATAL_EXCEPTION
1291 // assume this can only be called from the main thread
1292 gs_handleExceptions
= doit
;
1296 wxFAIL_MSG(_T("set wxUSE_ON_FATAL_EXCEPTION to 1 to sue this function"));
1302 //-----------------------------------------------------------------------------
1304 //-----------------------------------------------------------------------------
1308 // Send the top window a dummy message so idle handler processing will
1309 // start up again. Doing it this way ensures that the idle handler
1310 // wakes up in the right thread (see also wxWakeUpMainThread() which does
1311 // the same for the main app thread only)
1312 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
1315 if ( !::PostMessage(GetHwndOf(topWindow
), WM_NULL
, 0, 0) )
1317 // should never happen
1318 wxLogLastError("PostMessage(WM_NULL)");
1323 //-----------------------------------------------------------------------------
1326 wxApp::GetStdIcon(int which
) const
1330 case wxICON_INFORMATION
:
1331 return wxIcon("wxICON_INFO");
1333 case wxICON_QUESTION
:
1334 return wxIcon("wxICON_QUESTION");
1336 case wxICON_EXCLAMATION
:
1337 return wxIcon("wxICON_WARNING");
1340 wxFAIL_MSG(wxT("requested non existent standard icon"));
1341 // still fall through
1344 return wxIcon("wxICON_ERROR");
1348 // For some reason, with MSVC++ 1.5, WinMain isn't linked in properly
1349 // if in a separate file. So include it here to ensure it's linked.
1350 #if (defined(__VISUALC__) && !defined(__WIN32__)) || (defined(__GNUWIN32__) && !defined(__TWIN32__) && !defined(WXMAKINGDLL))