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 wxList
*wxWinHandleList
;
126 extern wxList WXDLLEXPORT wxPendingDelete
;
127 extern void wxSetKeyboardHook(bool doIt
);
130 wxApp
*wxTheApp
= NULL
;
132 // NB: all "NoRedraw" classes must have the same names as the "normal" classes
133 // with NR suffix - wxWindow::MSWCreate() supposes this
134 const wxChar
*wxFrameClassName
= wxT("wxFrameClass");
135 const wxChar
*wxFrameClassNameNoRedraw
= wxT("wxFrameClassNR");
136 const wxChar
*wxMDIFrameClassName
= wxT("wxMDIFrameClass");
137 const wxChar
*wxMDIFrameClassNameNoRedraw
= wxT("wxMDIFrameClassNR");
138 const wxChar
*wxMDIChildFrameClassName
= wxT("wxMDIChildFrameClass");
139 const wxChar
*wxMDIChildFrameClassNameNoRedraw
= wxT("wxMDIChildFrameClassNR");
140 const wxChar
*wxPanelClassName
= wxT("wxPanelClass");
141 const wxChar
*wxPanelClassNameNR
= wxT("wxPanelClassNR");
142 const wxChar
*wxCanvasClassName
= wxT("wxCanvasClass");
143 const wxChar
*wxCanvasClassNameNR
= wxT("wxCanvasClassNR");
145 HICON wxSTD_FRAME_ICON
= (HICON
) NULL
;
146 HICON wxSTD_MDICHILDFRAME_ICON
= (HICON
) NULL
;
147 HICON wxSTD_MDIPARENTFRAME_ICON
= (HICON
) NULL
;
149 HICON wxDEFAULT_FRAME_ICON
= (HICON
) NULL
;
150 HICON wxDEFAULT_MDICHILDFRAME_ICON
= (HICON
) NULL
;
151 HICON wxDEFAULT_MDIPARENTFRAME_ICON
= (HICON
) NULL
;
153 HBRUSH wxDisableButtonBrush
= (HBRUSH
) 0;
155 LRESULT WXDLLEXPORT APIENTRY
wxWndProc(HWND
, UINT
, WPARAM
, LPARAM
);
157 // FIXME wxUSE_ON_FATAL_EXCEPTION is only supported for VC++ now because it
158 // needs compiler support for Win32 SEH. Others (especially Borland)
159 // probably have it too, but I'm not sure about how it works
160 // JACS: get 'Cannot use __try in functions that require unwinding
161 // in Unicode mode, so disabling.
162 #if !defined(__VISUALC__) || defined(__WIN16__) || defined(UNICODE)
163 #undef wxUSE_ON_FATAL_EXCEPTION
164 #define wxUSE_ON_FATAL_EXCEPTION 0
167 #if wxUSE_ON_FATAL_EXCEPTION
168 static bool gs_handleExceptions
= FALSE
;
171 // ===========================================================================
173 // ===========================================================================
175 // ---------------------------------------------------------------------------
177 // ---------------------------------------------------------------------------
179 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
181 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
182 EVT_IDLE(wxApp::OnIdle
)
183 EVT_END_SESSION(wxApp::OnEndSession
)
184 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession
)
188 bool wxApp::Initialize()
190 // the first thing to do is to check if we're trying to run an Unicode
191 // program under Win9x - if so, abort right now as it has no chance to
194 if ( wxGetOsVersion() != wxWINDOWS_NT
)
196 // note that we can use MessageBoxW() as it's implemented even under
197 // Win9x - OTOH, we can't use wxGetTranslation() because the file APIs
198 // used by wxLocale are not
202 _T("This program uses Unicode and requires Windows NT/2000.\nProgram aborted."),
203 _T("wxWindows Fatal Error"),
209 #endif // wxUSE_UNICODE
211 // Some people may wish to use this, but
212 // probably it shouldn't be here by default.
214 // wxRedirectIOToConsole();
217 wxBuffer
= new wxChar
[1500]; // FIXME
219 wxClassInfo::InitializeClasses();
222 wxPendingEventsLocker
= new wxCriticalSection
;
225 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
226 wxTheColourDatabase
->Initialize();
228 wxInitializeStockLists();
229 wxInitializeStockObjects();
231 #if wxUSE_WX_RESOURCES
232 wxInitializeResourceSystem();
235 wxBitmap::InitStandardHandlers();
237 #if defined(__WIN95__)
238 InitCommonControls();
245 // for OLE, enlarge message queue to be as large as possible
247 while (!SetMessageQueue(iMsg
) && (iMsg
-= 8))
250 // we need to initialize OLE library
251 if ( FAILED(::OleInitialize(NULL
)) )
252 wxLogError(_("Cannot initialize OLE"));
256 if (!Ctl3dRegister(wxhInstance
))
257 wxLogError(wxT("Cannot register CTL3D"));
259 Ctl3dAutoSubclass(wxhInstance
);
262 // VZ: these icons are not in wx.rc anyhow (but should they?)!
264 wxSTD_FRAME_ICON
= LoadIcon(wxhInstance
, wxT("wxSTD_FRAME"));
265 wxSTD_MDIPARENTFRAME_ICON
= LoadIcon(wxhInstance
, wxT("wxSTD_MDIPARENTFRAME"));
266 wxSTD_MDICHILDFRAME_ICON
= LoadIcon(wxhInstance
, wxT("wxSTD_MDICHILDFRAME"));
268 wxDEFAULT_FRAME_ICON
= LoadIcon(wxhInstance
, wxT("wxDEFAULT_FRAME"));
269 wxDEFAULT_MDIPARENTFRAME_ICON
= LoadIcon(wxhInstance
, wxT("wxDEFAULT_MDIPARENTFRAME"));
270 wxDEFAULT_MDICHILDFRAME_ICON
= LoadIcon(wxhInstance
, wxT("wxDEFAULT_MDICHILDFRAME"));
273 RegisterWindowClasses();
275 // Create the brush for disabling bitmap buttons
278 lb
.lbStyle
= BS_PATTERN
;
279 lb
.lbHatch
= (int)LoadBitmap( wxhInstance
, wxT("wxDISABLE_BUTTON_BITMAP") );
282 wxDisableButtonBrush
= ::CreateBrushIndirect( & lb
);
283 ::DeleteObject( (HGDIOBJ
)lb
.lbHatch
);
285 //else: wxWindows resources are probably not linked in
291 wxWinHandleList
= new wxList(wxKEY_INTEGER
);
293 // This is to foil optimizations in Visual C++ that throw out dummy.obj.
294 // PLEASE DO NOT ALTER THIS.
295 #if defined(__VISUALC__) && defined(__WIN16__) && !defined(WXMAKINGDLL)
296 extern char wxDummyChar
;
297 if (wxDummyChar
) wxDummyChar
++;
300 wxSetKeyboardHook(TRUE
);
302 wxModule::RegisterModules();
303 if (!wxModule::InitializeModules())
308 // ---------------------------------------------------------------------------
309 // RegisterWindowClasses
310 // ---------------------------------------------------------------------------
312 // TODO we should only register classes really used by the app. For this it
313 // would be enough to just delay the class registration until an attempt
314 // to create a window of this class is made.
315 bool wxApp::RegisterWindowClasses()
319 // for each class we register one with CS_(V|H)REDRAW style and one
320 // without for windows created with wxNO_FULL_REDRAW_ON_REPAINT flag
321 static const long styleNormal
= CS_HREDRAW
| CS_VREDRAW
| CS_DBLCLKS
;
322 static const long styleNoRedraw
= CS_DBLCLKS
;
324 // the fields which are common to all classes
325 wndclass
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
326 wndclass
.cbClsExtra
= 0;
327 wndclass
.cbWndExtra
= sizeof( DWORD
); // VZ: what is this DWORD used for?
328 wndclass
.hInstance
= wxhInstance
;
329 wndclass
.hIcon
= (HICON
) NULL
;
330 wndclass
.hCursor
= ::LoadCursor((HINSTANCE
)NULL
, IDC_ARROW
);
331 wndclass
.lpszMenuName
= NULL
;
333 // Register the frame window class.
334 wndclass
.hbrBackground
= (HBRUSH
)(COLOR_APPWORKSPACE
+ 1);
335 wndclass
.lpszClassName
= wxFrameClassName
;
336 wndclass
.style
= styleNormal
;
338 if ( !RegisterClass(&wndclass
) )
340 wxLogLastError(wxT("RegisterClass(frame)"));
346 wndclass
.lpszClassName
= wxFrameClassNameNoRedraw
;
347 wndclass
.style
= styleNoRedraw
;
349 if ( !RegisterClass(&wndclass
) )
351 wxLogLastError(wxT("RegisterClass(no redraw frame)"));
356 // Register the MDI frame window class.
357 wndclass
.hbrBackground
= (HBRUSH
)NULL
; // paint MDI frame ourselves
358 wndclass
.lpszClassName
= wxMDIFrameClassName
;
359 wndclass
.style
= styleNormal
;
361 if ( !RegisterClass(&wndclass
) )
363 wxLogLastError(wxT("RegisterClass(MDI parent)"));
368 // "no redraw" MDI frame
369 wndclass
.lpszClassName
= wxMDIFrameClassNameNoRedraw
;
370 wndclass
.style
= styleNoRedraw
;
372 if ( !RegisterClass(&wndclass
) )
374 wxLogLastError(wxT("RegisterClass(no redraw MDI parent frame)"));
379 // Register the MDI child frame window class.
380 wndclass
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+ 1);
381 wndclass
.lpszClassName
= wxMDIChildFrameClassName
;
382 wndclass
.style
= styleNormal
;
384 if ( !RegisterClass(&wndclass
) )
386 wxLogLastError(wxT("RegisterClass(MDI child)"));
391 // "no redraw" MDI child frame
392 wndclass
.lpszClassName
= wxMDIChildFrameClassNameNoRedraw
;
393 wndclass
.style
= styleNoRedraw
;
395 if ( !RegisterClass(&wndclass
) )
397 wxLogLastError(wxT("RegisterClass(no redraw MDI child)"));
402 // Register the panel window class.
403 wndclass
.hbrBackground
= (HBRUSH
) GetStockObject( LTGRAY_BRUSH
);
404 wndclass
.lpszClassName
= wxPanelClassName
;
405 wndclass
.style
= styleNormal
;
407 if ( !RegisterClass(&wndclass
) )
409 wxLogLastError(wxT("RegisterClass(panel)"));
414 // Register the no redraw panel window class.
415 wndclass
.lpszClassName
= wxPanelClassNameNR
;
416 wndclass
.style
= styleNoRedraw
;
418 if ( !RegisterClass(&wndclass
) )
420 wxLogLastError(wxT("RegisterClass(no redraw panel)"));
425 // Register the canvas and textsubwindow class name
426 wndclass
.hbrBackground
= (HBRUSH
)NULL
;
427 wndclass
.lpszClassName
= wxCanvasClassName
;
429 if ( !RegisterClass(&wndclass
) )
431 wxLogLastError(wxT("RegisterClass(canvas)"));
436 wndclass
.lpszClassName
= wxCanvasClassNameNR
;
437 wndclass
.style
= styleNoRedraw
;
438 if ( !RegisterClass(&wndclass
) )
440 wxLogLastError(wxT("RegisterClass(no redraw canvas)"));
448 // ---------------------------------------------------------------------------
449 // Convert Windows to argc, argv style
450 // ---------------------------------------------------------------------------
452 void wxApp::ConvertToStandardCommandArgs(char* lpCmdLine
)
456 wxString
cmdLine(lpCmdLine
);
459 // Get application name
460 wxChar name
[260]; // 260 is MAX_PATH value from windef.h
461 ::GetModuleFileName(wxhInstance
, name
, WXSIZEOF(name
));
466 wxStrcpy(name
, wxFileNameFromPath(name
));
467 wxStripExtension(name
);
468 wxTheApp
->SetAppName(name
);
471 // Treat strings enclosed in double-quotes as single arguments
473 int len
= cmdLine
.Length();
477 while ((i
< len
) && wxIsspace(cmdLine
.GetChar(i
)))
482 if (cmdLine
.GetChar(i
) == wxT('"')) // We found the start of a string
486 while ((i
< len
) && (cmdLine
.GetChar(i
) != wxT('"')))
489 wxString
arg(cmdLine
.Mid(first
, (i
- first
)));
495 i
++; // Skip past 2nd quote
497 else // Unquoted argument
500 while ((i
< len
) && !wxIsspace(cmdLine
.GetChar(i
)))
503 wxString
arg(cmdLine
.Mid(first
, (i
- first
)));
511 wxTheApp
->argv
= new wxChar
*[count
+ 1];
512 for (i
= 0; i
< count
; i
++)
514 wxString
arg(args
[i
]);
515 wxTheApp
->argv
[i
] = copystring((const wxChar
*)arg
);
517 wxTheApp
->argv
[count
] = NULL
; // argv[] is a NULL-terminated list
518 wxTheApp
->argc
= count
;
521 //// Cleans up any wxWindows internal structures left lying around
523 void wxApp::CleanUp()
528 // flush the logged messages if any and install a 'safer' log target: the
529 // default one (wxLogGui) can't be used after the resources are freed just
530 // below and the user suppliedo ne might be even more unsafe (using any
531 // wxWindows GUI function is unsafe starting from now)
532 wxLog::DontCreateOnDemand();
534 // this will flush the old messages if any
535 delete wxLog::SetActiveTarget(new wxLogStderr
);
538 // One last chance for pending objects to be cleaned up
539 wxTheApp
->DeletePendingObjects();
541 wxModule::CleanUpModules();
543 #if wxUSE_WX_RESOURCES
544 wxCleanUpResourceSystem();
546 // wxDefaultResourceTable->ClearTable();
549 wxDeleteStockObjects();
551 // Destroy all GDI lists, etc.
552 wxDeleteStockLists();
554 delete wxTheColourDatabase
;
555 wxTheColourDatabase
= NULL
;
557 wxBitmap::CleanUpHandlers();
562 //// WINDOWS-SPECIFIC CLEANUP
564 wxSetKeyboardHook(FALSE
);
570 if (wxSTD_FRAME_ICON
)
571 DestroyIcon(wxSTD_FRAME_ICON
);
572 if (wxSTD_MDICHILDFRAME_ICON
)
573 DestroyIcon(wxSTD_MDICHILDFRAME_ICON
);
574 if (wxSTD_MDIPARENTFRAME_ICON
)
575 DestroyIcon(wxSTD_MDIPARENTFRAME_ICON
);
577 if (wxDEFAULT_FRAME_ICON
)
578 DestroyIcon(wxDEFAULT_FRAME_ICON
);
579 if (wxDEFAULT_MDICHILDFRAME_ICON
)
580 DestroyIcon(wxDEFAULT_MDICHILDFRAME_ICON
);
581 if (wxDEFAULT_MDIPARENTFRAME_ICON
)
582 DestroyIcon(wxDEFAULT_MDIPARENTFRAME_ICON
);
584 if ( wxDisableButtonBrush
)
585 ::DeleteObject( wxDisableButtonBrush
);
592 Ctl3dUnregister(wxhInstance
);
596 delete wxWinHandleList
;
598 // GL: I'm annoyed ... I don't know where to put this and I don't want to
599 // create a module for that as it's part of the core.
600 delete wxPendingEvents
;
602 delete wxPendingEventsLocker
;
603 // If we don't do the following, we get an apparent memory leak.
604 ((wxEvtHandler
&) wxDefaultValidator
).ClearEventLocker();
607 wxClassInfo::CleanUpClasses();
612 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
613 // At this point we want to check if there are any memory
614 // blocks that aren't part of the wxDebugContext itself,
615 // as a special case. Then when dumping we need to ignore
616 // wxDebugContext, too.
617 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
619 wxLogMessage(wxT("There were memory leaks."));
620 wxDebugContext::Dump();
621 wxDebugContext::PrintStatistics();
623 // wxDebugContext::SetStream(NULL, NULL);
627 // do it as the very last thing because everything else can log messages
628 delete wxLog::SetActiveTarget(NULL
);
632 //----------------------------------------------------------------------
633 // Entry point helpers, used by wxPython
634 //----------------------------------------------------------------------
636 int WXDLLEXPORT
wxEntryStart( int WXUNUSED(argc
), char** WXUNUSED(argv
) )
638 return wxApp::Initialize();
641 int WXDLLEXPORT
wxEntryInitGui()
643 wxTheApp
->OnInitGui();
647 void WXDLLEXPORT
wxEntryCleanup()
653 #if !defined(_WINDLL) || (defined(_WINDLL) && defined(WXMAKINGDLL))
655 // temporarily disable this warning which would be generated in release builds
658 #pragma warning(disable: 4715) // not all control paths return a value
661 //----------------------------------------------------------------------
662 // Main wxWindows entry point
663 //----------------------------------------------------------------------
664 int wxEntry(WXHINSTANCE hInstance
,
665 WXHINSTANCE
WXUNUSED(hPrevInstance
),
670 // do check for memory leaks on program exit
671 // (another useful flag is _CRTDBG_DELAY_FREE_MEM_DF which doesn't free
672 // deallocated memory which may be used to simulate low-memory condition)
673 wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF
);
675 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
676 // This seems to be necessary since there are 'rogue'
677 // objects present at this point (perhaps global objects?)
678 // Setting a checkpoint will ignore them as far as the
679 // memory checking facility is concerned.
680 // Of course you may argue that memory allocated in globals should be
681 // checked, but this is a reasonable compromise.
682 wxDebugContext::SetCheckpoint();
686 // take everything into a try-except block to be able to call
687 // OnFatalException() if necessary
688 #if wxUSE_ON_FATAL_EXCEPTION
691 wxhInstance
= (HINSTANCE
) hInstance
;
693 if (!wxEntryStart(0,0))
696 // create the application object or ensure that one already exists
699 // The app may have declared a global application object, but we recommend
700 // the IMPLEMENT_APP macro is used instead, which sets an initializer
701 // function for delayed, dynamic app object construction.
702 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
703 wxT("No initializer - use IMPLEMENT_APP macro.") );
705 wxTheApp
= (*wxApp::GetInitializerFunction()) ();
708 wxCHECK_MSG( wxTheApp
, 0, wxT("You have to define an instance of wxApp!") );
710 // save the WinMain() parameters
711 wxTheApp
->ConvertToStandardCommandArgs(lpCmdLine
);
712 wxTheApp
->m_nCmdShow
= nCmdShow
;
714 // GUI-specific initialisation. In fact on Windows we don't have any,
715 // but this call is provided for compatibility across platforms.
718 // We really don't want timestamps by default, because it means
719 // we can't simply double-click on the error message and get to that
720 // line in the source. So VC++ at least, let's have a sensible default.
722 wxLog::SetTimestamp(NULL
);
727 // it is common to create a modal dialog in OnInit() (to ask/notify the
728 // user about something) but it wouldn't work if we don't change the
729 // "exit on delete last frame" flag here as when this dialog is
730 // deleted, the app would terminate (it was the last top level window
731 // as the main frame wasn't created yet!), so disable this behaviour
733 bool exitOnLastFrameDelete
= wxTheApp
->GetExitOnFrameDelete();
734 wxTheApp
->SetExitOnFrameDelete(FALSE
);
737 retValue
= wxTheApp
->OnInit() ? 0 : -1;
739 // restore the old flag value
740 wxTheApp
->SetExitOnFrameDelete(exitOnLastFrameDelete
);
747 retValue
= wxTheApp
->OnRun();
751 // we want to initialize, but not run or exit immediately.
755 //else: app initialization failed, so we skipped OnRun()
757 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
760 // Forcibly delete the window.
761 if ( topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
762 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
764 topWindow
->Close(TRUE
);
765 wxTheApp
->DeletePendingObjects();
770 wxTheApp
->SetTopWindow(NULL
);
780 #if wxUSE_ON_FATAL_EXCEPTION
782 __except ( gs_handleExceptions
? EXCEPTION_EXECUTE_HANDLER
783 : EXCEPTION_CONTINUE_SEARCH
) {
786 // give the user a chance to do something special about this
787 wxTheApp
->OnFatalException();
790 ::ExitProcess(3); // the same exit code as abort()
794 #endif // wxUSE_ON_FATAL_EXCEPTION
797 // restore warning state
799 #pragma warning(default: 4715) // not all control paths return a value
804 //----------------------------------------------------------------------
805 // Entry point for wxWindows + the App in a DLL
806 //----------------------------------------------------------------------
808 int wxEntry(WXHINSTANCE hInstance
)
810 wxhInstance
= (HINSTANCE
) hInstance
;
813 // The app may have declared a global application object, but we recommend
814 // the IMPLEMENT_APP macro is used instead, which sets an initializer function
815 // for delayed, dynamic app object construction.
818 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
819 "No initializer - use IMPLEMENT_APP macro." );
821 wxTheApp
= (* wxApp::GetInitializerFunction()) ();
824 wxCHECK_MSG( wxTheApp
, 0, "You have to define an instance of wxApp!" );
827 wxTheApp
->argv
= NULL
;
833 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
834 if ( topWindow
&& topWindow
->GetHWND())
836 topWindow
->Show(TRUE
);
843 //// Static member initialization
845 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
851 m_wantDebugOutput
= TRUE
;
855 m_printMode
= wxPRINT_WINDOWS
;
856 m_exitOnFrameDelete
= TRUE
;
862 // Delete command-line args
864 for (i
= 0; i
< argc
; i
++)
871 bool wxApp::Initialized()
878 #else // Assume initialized if DLL (no way of telling)
884 * Get and process a message, returning FALSE if WM_QUIT
885 * received (and also set the flag telling the app to exit the main loop)
888 bool wxApp::DoMessage()
890 BOOL rc
= ::GetMessage(&s_currentMsg
, (HWND
) NULL
, 0, 0);
900 // should never happen, but let's test for it nevertheless
901 wxLogLastError(wxT("GetMessage"));
906 wxASSERT_MSG( wxThread::IsMain(),
907 wxT("only the main thread can process Windows messages") );
909 static bool s_hadGuiLock
= TRUE
;
910 static wxMsgArray s_aSavedMessages
;
912 // if a secondary thread owns is doing GUI calls, save all messages for
913 // later processing - we can't process them right now because it will
914 // lead to recursive library calls (and we're not reentrant)
915 if ( !wxGuiOwnedByMainThread() )
917 s_hadGuiLock
= FALSE
;
919 // leave out WM_COMMAND messages: too dangerous, sometimes
920 // the message will be processed twice
921 if ( !wxIsWaitingForThread() ||
922 s_currentMsg
.message
!= WM_COMMAND
)
924 s_aSavedMessages
.Add(s_currentMsg
);
931 // have we just regained the GUI lock? if so, post all of the saved
934 // FIXME of course, it's not _exactly_ the same as processing the
935 // messages normally - expect some things to break...
940 size_t count
= s_aSavedMessages
.Count();
941 for ( size_t n
= 0; n
< count
; n
++ )
943 MSG
& msg
= s_aSavedMessages
[n
];
945 if ( !ProcessMessage((WXMSG
*)&msg
) )
947 ::TranslateMessage(&msg
);
948 ::DispatchMessage(&msg
);
952 s_aSavedMessages
.Empty();
955 #endif // wxUSE_THREADS
957 // Process the message
958 if ( !ProcessMessage((WXMSG
*)&s_currentMsg
) )
960 ::TranslateMessage(&s_currentMsg
);
961 ::DispatchMessage(&s_currentMsg
);
969 * Keep trying to process messages until WM_QUIT
972 * If there are messages to be processed, they will all be
973 * processed and OnIdle will not be called.
974 * When there are no more messages, OnIdle is called.
975 * If OnIdle requests more time,
976 * it will be repeatedly called so long as there are no pending messages.
977 * A 'feature' of this is that once OnIdle has decided that no more processing
978 * is required, then it won't get processing time until further messages
979 * are processed (it'll sit in DoMessage).
982 int wxApp::MainLoop()
986 while ( m_keepGoing
)
989 wxMutexGuiLeaveOrEnter();
990 #endif // wxUSE_THREADS
992 while ( !Pending() && ProcessIdle() )
995 // a message came or no more idle processing to do
999 return s_currentMsg
.wParam
;
1002 // Returns TRUE if more time is needed.
1003 bool wxApp::ProcessIdle()
1006 event
.SetEventObject(this);
1007 ProcessEvent(event
);
1009 return event
.MoreRequested();
1012 void wxApp::ExitMainLoop()
1014 // VZ: why not ::PostQuitMessage()?
1015 m_keepGoing
= FALSE
;
1018 bool wxApp::Pending()
1020 return ::PeekMessage(&s_currentMsg
, 0, 0, 0, PM_NOREMOVE
) != 0;
1023 void wxApp::Dispatch()
1029 * Give all windows a chance to preprocess
1030 * the message. Some may have accelerator tables, or have
1031 * MDI complications.
1034 bool wxApp::ProcessMessage(WXMSG
*wxmsg
)
1036 MSG
*msg
= (MSG
*)wxmsg
;
1037 HWND hWnd
= msg
->hwnd
;
1038 wxWindow
*wndThis
= wxGetWindowFromHWND((WXHWND
)hWnd
);
1041 // we must relay WM_MOUSEMOVE events to the tooltip ctrl if we want it to
1042 // popup the tooltip bubbles
1043 if ( wndThis
&& (msg
->message
== WM_MOUSEMOVE
) )
1045 wxToolTip
*tt
= wndThis
->GetToolTip();
1048 tt
->RelayEvent(wxmsg
);
1051 #endif // wxUSE_TOOLTIPS
1053 // Try translations first; find the youngest window with
1054 // a translation table.
1057 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
1059 if ( wnd
->MSWTranslateMessage(wxmsg
) )
1063 // Anyone for a non-translation message? Try youngest descendants first.
1064 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
1066 if ( wnd
->MSWProcessMessage(wxmsg
) )
1073 void wxApp::OnIdle(wxIdleEvent
& event
)
1075 static bool s_inOnIdle
= FALSE
;
1077 // Avoid recursion (via ProcessEvent default case)
1083 // If there are pending events, we must process them: pending events
1084 // are either events to the threads other than main or events posted
1085 // with wxPostEvent() functions
1086 // GRG: I have moved this here so that all pending events are processed
1087 // before starting to delete any objects. This behaves better (in
1088 // particular, wrt wxPostEvent) and is coherent with wxGTK's current
1089 // behaviour. Changed Feb/2000 before 2.1.14
1090 ProcessPendingEvents();
1092 // 'Garbage' collection of windows deleted with Close().
1093 DeletePendingObjects();
1096 // flush the logged messages if any
1097 wxLog::FlushActive();
1100 // Send OnIdle events to all windows
1101 if ( SendIdleEvents() )
1103 // SendIdleEvents() returns TRUE if at least one window requested more
1105 event
.RequestMore(TRUE
);
1111 // Send idle event to all top-level windows
1112 bool wxApp::SendIdleEvents()
1114 bool needMore
= FALSE
;
1116 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
1119 wxWindow
* win
= node
->GetData();
1120 if (SendIdleEvents(win
))
1122 node
= node
->GetNext();
1128 // Send idle event to window and all subwindows
1129 bool wxApp::SendIdleEvents(wxWindow
* win
)
1131 bool needMore
= FALSE
;
1134 event
.SetEventObject(win
);
1135 win
->GetEventHandler()->ProcessEvent(event
);
1137 if (event
.MoreRequested())
1140 wxNode
* node
= win
->GetChildren().First();
1143 wxWindow
* win
= (wxWindow
*) node
->Data();
1144 if (SendIdleEvents(win
))
1147 node
= node
->Next();
1152 void wxApp::DeletePendingObjects()
1154 wxNode
*node
= wxPendingDelete
.First();
1157 wxObject
*obj
= (wxObject
*)node
->Data();
1161 if (wxPendingDelete
.Member(obj
))
1164 // Deleting one object may have deleted other pending
1165 // objects, so start from beginning of list again.
1166 node
= wxPendingDelete
.First();
1170 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
1173 GetTopWindow()->Close(TRUE
);
1176 // Default behaviour: close the application with prompts. The
1177 // user can veto the close, and therefore the end session.
1178 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
1182 if (!GetTopWindow()->Close(!event
.CanVeto()))
1188 int wxApp::GetComCtl32Version()
1191 static int s_verComCtl32
= -1;
1193 wxCRIT_SECT_DECLARE(csComCtl32
);
1194 wxCRIT_SECT_LOCKER(lock
, csComCtl32
);
1196 if ( s_verComCtl32
== -1 )
1198 // initally assume no comctl32.dll at all
1202 HMODULE hModuleComCtl32
= ::GetModuleHandle(wxT("COMCTL32"));
1204 // if so, then we can check for the version
1205 if ( hModuleComCtl32
)
1207 // try to use DllGetVersion() if available in _headers_
1208 #ifdef DLLVER_PLATFORM_WINDOWS // defined in shlwapi.h
1209 DLLGETVERSIONPROC pfnDllGetVersion
= (DLLGETVERSIONPROC
)
1210 ::GetProcAddress(hModuleComCtl32
, "DllGetVersion");
1211 if ( pfnDllGetVersion
)
1214 dvi
.cbSize
= sizeof(dvi
);
1216 HRESULT hr
= (*pfnDllGetVersion
)(&dvi
);
1219 wxLogApiError(_T("DllGetVersion"), hr
);
1223 // this is incompatible with _WIN32_IE values, but
1224 // compatible with the other values returned by
1225 // GetComCtl32Version()
1226 s_verComCtl32
= 100*dvi
.dwMajorVersion
+
1231 // DllGetVersion() unavailable either during compile or
1232 // run-time, try to guess the version otherwise
1233 if ( !s_verComCtl32
)
1235 // InitCommonControlsEx is unique to 4.70 and later
1236 FARPROC theProc
= ::GetProcAddress
1239 "InitCommonControlsEx"
1244 // not found, must be 4.00
1245 s_verComCtl32
= 400;
1249 // many symbols appeared in comctl32 4.71, could use
1250 // any of them except may be DllInstall
1251 theProc
= ::GetProcAddress
1258 // not found, must be 4.70
1259 s_verComCtl32
= 470;
1263 // found, must be 4.71
1264 s_verComCtl32
= 471;
1271 return s_verComCtl32
;
1276 wxLogError(_("Fatal error: exiting"));
1282 // Yield to incoming messages
1285 // disable log flushing from here because a call to wxYield() shouldn't
1286 // normally result in message boxes popping up &c
1289 // we don't want to process WM_QUIT from here - it should be processed in
1290 // the main event loop in order to stop it
1292 while ( PeekMessage(&msg
, (HWND
)0, 0, 0, PM_NOREMOVE
) &&
1293 msg
.message
!= WM_QUIT
)
1296 wxMutexGuiLeaveOrEnter();
1297 #endif // wxUSE_THREADS
1299 if ( !wxTheApp
->DoMessage() )
1303 // If they are pending events, we must process them.
1305 wxTheApp
->ProcessPendingEvents();
1307 // let the logs be flashed again
1313 bool wxHandleFatalExceptions(bool doit
)
1315 #if wxUSE_ON_FATAL_EXCEPTION
1316 // assume this can only be called from the main thread
1317 gs_handleExceptions
= doit
;
1321 wxFAIL_MSG(_T("set wxUSE_ON_FATAL_EXCEPTION to 1 to sue this function"));
1327 //-----------------------------------------------------------------------------
1329 //-----------------------------------------------------------------------------
1333 // Send the top window a dummy message so idle handler processing will
1334 // start up again. Doing it this way ensures that the idle handler
1335 // wakes up in the right thread (see also wxWakeUpMainThread() which does
1336 // the same for the main app thread only)
1337 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
1340 if ( !::PostMessage(GetHwndOf(topWindow
), WM_NULL
, 0, 0) )
1342 // should never happen
1343 wxLogLastError(wxT("PostMessage(WM_NULL)"));
1348 //-----------------------------------------------------------------------------
1351 wxApp::GetStdIcon(int which
) const
1355 case wxICON_INFORMATION
:
1356 return wxIcon("wxICON_INFO");
1358 case wxICON_QUESTION
:
1359 return wxIcon("wxICON_QUESTION");
1361 case wxICON_EXCLAMATION
:
1362 return wxIcon("wxICON_WARNING");
1365 wxFAIL_MSG(wxT("requested non existent standard icon"));
1366 // still fall through
1369 return wxIcon("wxICON_ERROR");
1373 // For some reason, with MSVC++ 1.5, WinMain isn't linked in properly
1374 // if in a separate file. So include it here to ensure it's linked.
1375 #if (defined(__VISUALC__) && !defined(__WIN32__)) || (defined(__GNUWIN32__) && !defined(__TWIN32__) && !defined(WXMAKINGDLL))