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"
48 #include "wx/msw/private.h"
50 #include "wx/module.h"
53 #include "wx/thread.h"
55 // define the array of MSG strutures
56 WX_DECLARE_OBJARRAY(MSG
, wxMsgArray
);
58 #include "wx/arrimpl.cpp"
60 WX_DEFINE_OBJARRAY(wxMsgArray
);
61 #endif // wxUSE_THREADS
63 #if wxUSE_WX_RESOURCES
64 #include "wx/resource.h"
67 // OLE is used for drag-and-drop, clipboard, OLE Automation...
68 #if defined(__GNUWIN32__) || defined(__SC__) || defined(__SALFORDC__)
72 #endif // broken compilers
81 #if (defined(__WIN95__) && !defined(__GNUWIN32__)) || defined(__TWIN32__)
85 #include "wx/msw/msvcrt.h"
87 // ---------------------------------------------------------------------------
89 // ---------------------------------------------------------------------------
91 extern char *wxBuffer
;
92 extern char *wxOsVersion
;
93 extern wxList
*wxWinHandleList
;
94 extern wxList WXDLLEXPORT wxPendingDelete
;
96 extern wxList
*wxPendingEvents
;
97 extern wxCriticalSection
*wxPendingEventsLocker
;
99 extern void wxSetKeyboardHook(bool doIt
);
100 extern wxCursor
*g_globalCursor
;
102 HINSTANCE wxhInstance
= 0;
104 wxApp
*wxTheApp
= NULL
;
106 // FIXME why not const? and not static?
107 char wxFrameClassName
[] = "wxFrameClass";
108 char wxMDIFrameClassName
[] = "wxMDIFrameClass";
109 char wxMDIChildFrameClassName
[] = "wxMDIChildFrameClass";
110 char wxPanelClassName
[] = "wxPanelClass";
111 char wxCanvasClassName
[] = "wxCanvasClass";
113 HICON wxSTD_FRAME_ICON
= (HICON
) NULL
;
114 HICON wxSTD_MDICHILDFRAME_ICON
= (HICON
) NULL
;
115 HICON wxSTD_MDIPARENTFRAME_ICON
= (HICON
) NULL
;
117 HICON wxDEFAULT_FRAME_ICON
= (HICON
) NULL
;
118 HICON wxDEFAULT_MDICHILDFRAME_ICON
= (HICON
) NULL
;
119 HICON wxDEFAULT_MDIPARENTFRAME_ICON
= (HICON
) NULL
;
121 HBRUSH wxDisableButtonBrush
= (HBRUSH
) 0;
123 LRESULT APIENTRY
wxWndProc(HWND
, UINT
, WPARAM
, LPARAM
);
125 #if defined(__WIN95__) && !defined(__TWIN32__)
126 #define wxUSE_RICHEDIT 1
128 #define wxUSE_RICHEDIT 0
132 static HINSTANCE gs_hRichEdit
= (HINSTANCE
) NULL
;
135 // ===========================================================================
137 // ===========================================================================
139 // ---------------------------------------------------------------------------
141 // ---------------------------------------------------------------------------
143 #if !USE_SHARED_LIBRARY
144 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
146 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
147 EVT_IDLE(wxApp::OnIdle
)
148 EVT_END_SESSION(wxApp::OnEndSession
)
149 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession
)
154 bool wxApp::Initialize()
156 // Some people may wish to use this, but
157 // probably it shouldn't be here by default.
159 // wxRedirectIOToConsole();
162 wxBuffer
= new char[1500]; // FIXME
164 wxClassInfo::InitializeClasses();
167 wxGetResource("wxWindows", "OsVersion", &wxOsVersion
);
170 // I'm annoyed ... I don't know where to put this and I don't want to
171 // create a module for that as it's part of the core.
173 wxPendingEvents
= new wxList();
174 wxPendingEventsLocker
= new wxCriticalSection();
177 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
178 wxTheColourDatabase
->Initialize();
180 wxInitializeStockLists();
181 wxInitializeStockObjects();
183 #if wxUSE_WX_RESOURCES
184 wxInitializeResourceSystem();
187 wxBitmap::InitStandardHandlers();
189 #if defined(__WIN95__)
190 InitCommonControls();
193 gs_hRichEdit
= LoadLibrary("RICHED32.DLL");
195 if (gs_hRichEdit
== (HINSTANCE
) NULL
)
197 wxLogError(_("Could not initialise Rich Edit DLL"));
199 #endif // wxUSE_RICHEDIT
203 // for OLE, enlarge message queue to be as large as possible
205 while (!SetMessageQueue(iMsg
) && (iMsg
-= 8));
208 // we need to initialize OLE library
209 if ( FAILED(::OleInitialize(NULL
)) )
210 wxLogError(_("Cannot initialize OLE"));
214 if (!Ctl3dRegister(wxhInstance
))
215 wxLogError("Cannot register CTL3D");
217 Ctl3dAutoSubclass(wxhInstance
);
220 g_globalCursor
= new wxCursor
;
222 wxSTD_FRAME_ICON
= LoadIcon(wxhInstance
, "wxSTD_FRAME");
223 wxSTD_MDIPARENTFRAME_ICON
= LoadIcon(wxhInstance
, "wxSTD_MDIPARENTFRAME");
224 wxSTD_MDICHILDFRAME_ICON
= LoadIcon(wxhInstance
, "wxSTD_MDICHILDFRAME");
226 wxDEFAULT_FRAME_ICON
= LoadIcon(wxhInstance
, "wxDEFAULT_FRAME");
227 wxDEFAULT_MDIPARENTFRAME_ICON
= LoadIcon(wxhInstance
, "wxDEFAULT_MDIPARENTFRAME");
228 wxDEFAULT_MDICHILDFRAME_ICON
= LoadIcon(wxhInstance
, "wxDEFAULT_MDICHILDFRAME");
230 RegisterWindowClasses();
232 // Create the brush for disabling bitmap buttons
235 lb
.lbStyle
= BS_PATTERN
;
236 lb
.lbHatch
= (int)LoadBitmap( wxhInstance
, "wxDISABLE_BUTTON_BITMAP" );
239 wxDisableButtonBrush
= ::CreateBrushIndirect( & lb
);
240 ::DeleteObject( (HGDIOBJ
)lb
.lbHatch
);
242 //else: wxWindows resources are probably not linked in
248 wxWinHandleList
= new wxList(wxKEY_INTEGER
);
250 // This is to foil optimizations in Visual C++ that throw out dummy.obj.
251 // PLEASE DO NOT ALTER THIS.
252 #if defined(__VISUALC__) && !defined(WXMAKINGDLL)
253 extern char wxDummyChar
;
254 if (wxDummyChar
) wxDummyChar
++;
257 wxSetKeyboardHook(TRUE
);
259 wxModule::RegisterModules();
260 if (!wxModule::InitializeModules())
265 // ---------------------------------------------------------------------------
266 // RegisterWindowClasses
267 // ---------------------------------------------------------------------------
269 bool wxApp::RegisterWindowClasses()
273 // the fields which are common to all classes
274 wndclass
.style
= CS_HREDRAW
| CS_VREDRAW
| CS_DBLCLKS
;
275 wndclass
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
276 wndclass
.cbClsExtra
= 0;
277 wndclass
.cbWndExtra
= sizeof( DWORD
); // what is this DWORD used for?
278 wndclass
.hInstance
= wxhInstance
;
279 wndclass
.hIcon
= (HICON
) NULL
;
280 wndclass
.hCursor
= ::LoadCursor((HINSTANCE
)NULL
, IDC_ARROW
);
281 wndclass
.lpszMenuName
= NULL
;
283 // Register the frame window class.
284 wndclass
.hbrBackground
= (HBRUSH
)(COLOR_APPWORKSPACE
+ 1);
285 #ifdef _MULTIPLE_INSTANCES
286 sprintf( wxFrameClassName
,"wxFrameClass%d", wxhInstance
);
288 wndclass
.lpszClassName
= wxFrameClassName
;
290 if ( !RegisterClass(&wndclass
) )
292 wxLogLastError("RegisterClass(frame)");
297 // Register the MDI frame window class.
298 wndclass
.hbrBackground
= (HBRUSH
)NULL
; // paint MDI frame ourselves
299 wndclass
.lpszClassName
= wxMDIFrameClassName
;
301 if ( !RegisterClass(&wndclass
) )
303 wxLogLastError("RegisterClass(MDI parent)");
308 // Register the MDI child frame window class.
309 wndclass
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+ 1);
310 wndclass
.lpszClassName
= wxMDIChildFrameClassName
;
312 if ( !RegisterClass(&wndclass
) )
314 wxLogLastError("RegisterClass(MDI child)");
319 // Register the panel window class.
320 wndclass
.hbrBackground
= (HBRUSH
) GetStockObject( LTGRAY_BRUSH
);
321 wndclass
.lpszClassName
= wxPanelClassName
;
323 if ( !RegisterClass(&wndclass
) )
325 wxLogLastError("RegisterClass(panel)");
330 // Register the canvas and textsubwindow class name
331 wndclass
.hbrBackground
= (HBRUSH
)NULL
;
332 wndclass
.lpszClassName
= wxCanvasClassName
;
334 if ( !RegisterClass(&wndclass
) )
336 wxLogLastError("RegisterClass(canvas)");
344 // ---------------------------------------------------------------------------
345 // Convert Windows to argc, argv style
346 // ---------------------------------------------------------------------------
348 void wxApp::ConvertToStandardCommandArgs(char* lpCmdLine
)
352 wxString
cmdLine(lpCmdLine
);
355 // Get application name
356 char name
[260]; // 260 is MAX_PATH value from windef.h
357 ::GetModuleFileName(wxhInstance
, name
, WXSIZEOF(name
));
362 strcpy(name
, wxFileNameFromPath(name
));
363 wxStripExtension(name
);
364 wxTheApp
->SetAppName(name
);
367 // Treat strings enclosed in double-quotes as single arguments
369 int len
= cmdLine
.Length();
373 while ((i
< len
) && isspace(cmdLine
.GetChar(i
)))
378 if (cmdLine
.GetChar(i
) == '"') // We found the start of a string
382 while ((i
< len
) && (cmdLine
.GetChar(i
) != '"'))
385 wxString
arg(cmdLine
.Mid(first
, (i
- first
)));
391 i
++; // Skip past 2nd quote
393 else // Unquoted argument
396 while ((i
< len
) && !isspace(cmdLine
.GetChar(i
)))
399 wxString
arg(cmdLine
.Mid(first
, (i
- first
)));
407 wxTheApp
->argv
= new char*[count
+ 1];
408 for (i
= 0; i
< count
; i
++)
410 wxString
arg(args
[i
]);
411 wxTheApp
->argv
[i
] = copystring((const char*)arg
);
413 wxTheApp
->argv
[count
] = NULL
; // argv[] is a NULL-terminated list
414 wxTheApp
->argc
= count
;
417 //// Cleans up any wxWindows internal structures left lying around
419 void wxApp::CleanUp()
424 // flush the logged messages if any and install a 'safer' log target: the
425 // default one (wxLogGui) can't be used after the resources are freed just
426 // below and the user suppliedo ne might be even more unsafe (using any
427 // wxWindows GUI function is unsafe starting from now)
428 wxLog::DontCreateOnDemand();
430 // this will flush the old messages if any
431 delete wxLog::SetActiveTarget(new wxLogStderr
);
434 // One last chance for pending objects to be cleaned up
435 wxTheApp
->DeletePendingObjects();
437 wxModule::CleanUpModules();
439 #if wxUSE_WX_RESOURCES
440 wxCleanUpResourceSystem();
442 // wxDefaultResourceTable->ClearTable();
445 // Indicate that the cursor can be freed, so that cursor won't be deleted
446 // by deleting the bitmap list before g_globalCursor goes out of scope
447 // (double deletion of the cursor).
448 wxSetCursor(wxNullCursor
);
449 delete g_globalCursor
;
450 g_globalCursor
= NULL
;
452 wxDeleteStockObjects();
454 // Destroy all GDI lists, etc.
455 wxDeleteStockLists();
457 delete wxTheColourDatabase
;
458 wxTheColourDatabase
= NULL
;
460 wxBitmap::CleanUpHandlers();
465 //// WINDOWS-SPECIFIC CLEANUP
467 wxSetKeyboardHook(FALSE
);
472 if (gs_hRichEdit
!= (HINSTANCE
) NULL
)
473 FreeLibrary(gs_hRichEdit
);
482 if (wxSTD_FRAME_ICON
)
483 DestroyIcon(wxSTD_FRAME_ICON
);
484 if (wxSTD_MDICHILDFRAME_ICON
)
485 DestroyIcon(wxSTD_MDICHILDFRAME_ICON
);
486 if (wxSTD_MDIPARENTFRAME_ICON
)
487 DestroyIcon(wxSTD_MDIPARENTFRAME_ICON
);
489 if (wxDEFAULT_FRAME_ICON
)
490 DestroyIcon(wxDEFAULT_FRAME_ICON
);
491 if (wxDEFAULT_MDICHILDFRAME_ICON
)
492 DestroyIcon(wxDEFAULT_MDICHILDFRAME_ICON
);
493 if (wxDEFAULT_MDIPARENTFRAME_ICON
)
494 DestroyIcon(wxDEFAULT_MDIPARENTFRAME_ICON
);
496 if ( wxDisableButtonBrush
)
497 ::DeleteObject( wxDisableButtonBrush
);
504 Ctl3dUnregister(wxhInstance
);
508 delete wxWinHandleList
;
510 // GL: I'm annoyed ... I don't know where to put this and I don't want to
511 // create a module for that as it's part of the core.
513 delete wxPendingEvents
;
514 delete wxPendingEventsLocker
;
515 // If we don't do the following, we get an apparent memory leak.
516 ((wxEvtHandler
&) wxDefaultValidator
).ClearEventLocker();
519 wxClassInfo::CleanUpClasses();
524 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
525 // At this point we want to check if there are any memory
526 // blocks that aren't part of the wxDebugContext itself,
527 // as a special case. Then when dumping we need to ignore
528 // wxDebugContext, too.
529 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
531 wxLogDebug("There were memory leaks.");
532 wxDebugContext::Dump();
533 wxDebugContext::PrintStatistics();
535 // wxDebugContext::SetStream(NULL, NULL);
539 // do it as the very last thing because everything else can log messages
540 delete wxLog::SetActiveTarget(NULL
);
544 #if !defined(_WINDLL) || (defined(_WINDLL) && defined(WXMAKINGDLL))
546 // temporarily disable this warning which would be generated in release builds
549 #pragma warning(disable: 4715) // not all control paths return a value
552 //// Main wxWindows entry point
553 int wxEntry(WXHINSTANCE hInstance
,
554 WXHINSTANCE
WXUNUSED(hPrevInstance
),
559 // do check for memory leaks on program exit
560 // (another useful flag is _CRTDBG_DELAY_FREE_MEM_DF which doesn't free
561 // deallocated memory which may be used to simulate low-memory condition)
562 wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF
);
564 // take everything into a try-except block in release build
565 // FIXME other compilers must support Win32 SEH (structured exception
566 // handling) too, just find the appropriate keyword in their docs!
567 // Please note that it's _not_ the same as C++ exceptions!
568 #if !defined(__WXDEBUG__) && defined(__VISUALC__)
569 #define CATCH_PROGRAM_EXCEPTIONS
573 #undef CATCH_PROGRAM_EXCEPTIONS
576 wxhInstance
= (HINSTANCE
) hInstance
;
578 if (!wxApp::Initialize())
581 // create the application object or ensure that one already exists
584 // The app may have declared a global application object, but we recommend
585 // the IMPLEMENT_APP macro is used instead, which sets an initializer
586 // function for delayed, dynamic app object construction.
587 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
588 "No initializer - use IMPLEMENT_APP macro." );
590 wxTheApp
= (*wxApp::GetInitializerFunction()) ();
593 wxCHECK_MSG( wxTheApp
, 0, "You have to define an instance of wxApp!" );
595 // save the WinMain() parameters
596 wxTheApp
->ConvertToStandardCommandArgs(lpCmdLine
);
597 wxTheApp
->m_nCmdShow
= nCmdShow
;
599 // GUI-specific initialisation. In fact on Windows we don't have any,
600 // but this call is provided for compatibility across platforms.
601 wxTheApp
->OnInitGui();
605 if ( wxTheApp
->OnInit() )
609 retValue
= wxTheApp
->OnRun();
612 // We want to initialize, but not run or exit immediately.
615 //else: app initialization failed, so we skipped OnRun()
617 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
620 // Forcibly delete the window.
621 if ( topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
622 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
624 topWindow
->Close(TRUE
);
625 wxTheApp
->DeletePendingObjects();
630 wxTheApp
->SetTopWindow(NULL
);
640 #ifdef CATCH_PROGRAM_EXCEPTIONS
642 __except ( EXCEPTION_EXECUTE_HANDLER
) {
645 wxTheApp->OnFatalException();
648 // using wxLog would be unsafe here
650 _("Unrecoverable program error detected: "
651 " the application will terminate."),
653 MB_APPLMODAL
| MB_ICONSTOP
| MB_OK
);
655 ::ExitProcess(3); // the same exit code as abort()
659 #endif // CATCH_PROGRAM_EXCEPTIONS
662 // restore warning state
664 #pragma warning(default: 4715) // not all control paths return a value
669 //// Entry point for DLLs
671 int wxEntry(WXHINSTANCE hInstance
)
673 wxhInstance
= (HINSTANCE
) hInstance
;
676 // The app may have declared a global application object, but we recommend
677 // the IMPLEMENT_APP macro is used instead, which sets an initializer function
678 // for delayed, dynamic app object construction.
681 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
682 "No initializer - use IMPLEMENT_APP macro." );
684 wxTheApp
= (* wxApp::GetInitializerFunction()) ();
687 wxCHECK_MSG( wxTheApp
, 0, "You have to define an instance of wxApp!" );
690 wxTheApp
->argv
= NULL
;
692 wxTheApp
->OnInitGui();
696 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
697 if ( topWindow
&& topWindow
->GetHWND())
699 topWindow
->Show(TRUE
);
706 //// Static member initialization
708 wxAppInitializerFunction
wxApp::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
714 m_wantDebugOutput
= TRUE
;
718 m_printMode
= wxPRINT_WINDOWS
;
719 m_exitOnFrameDelete
= TRUE
;
725 // Delete command-line args
727 for (i
= 0; i
< argc
; i
++)
734 bool wxApp::Initialized()
742 #ifdef _WINDLL // Assume initialized if DLL (no way of telling)
748 * Get and process a message, returning FALSE if WM_QUIT
749 * received (and also set the flag telling the app to exit the main loop)
752 bool wxApp::DoMessage()
754 BOOL rc
= ::GetMessage(&s_currentMsg
, (HWND
) NULL
, 0, 0);
764 // should never happen, but let's test for it nevertheless
765 wxLogLastError("GetMessage");
770 wxASSERT_MSG( wxThread::IsMain(),
771 "only the main thread can process Windows messages" );
773 static bool s_hadGuiLock
= TRUE
;
774 static wxMsgArray s_aSavedMessages
;
776 // if a secondary thread owns is doing GUI calls, save all messages for
777 // later processing - we can't process them right now because it will
778 // lead to recursive library calls (and we're not reentrant)
779 if ( !wxGuiOwnedByMainThread() )
781 s_hadGuiLock
= FALSE
;
783 // leave out WM_COMMAND messages: too dangerous, sometimes
784 // the message will be processed twice
785 if ( !wxIsWaitingForThread() ||
786 s_currentMsg
.message
!= WM_COMMAND
)
788 s_aSavedMessages
.Add(s_currentMsg
);
795 // have we just regained the GUI lock? if so, post all of the saved
798 // FIXME of course, it's not _exactly_ the same as processing the
799 // messages normally - expect some things to break...
804 size_t count
= s_aSavedMessages
.Count();
805 for ( size_t n
= 0; n
< count
; n
++ )
807 MSG
& msg
= s_aSavedMessages
[n
];
809 if ( !ProcessMessage((WXMSG
*)&msg
) )
811 ::TranslateMessage(&msg
);
812 ::DispatchMessage(&msg
);
816 s_aSavedMessages
.Empty();
819 #endif // wxUSE_THREADS
821 // Process the message
822 if ( !ProcessMessage((WXMSG
*)&s_currentMsg
) )
824 ::TranslateMessage(&s_currentMsg
);
825 ::DispatchMessage(&s_currentMsg
);
833 * Keep trying to process messages until WM_QUIT
836 * If there are messages to be processed, they will all be
837 * processed and OnIdle will not be called.
838 * When there are no more messages, OnIdle is called.
839 * If OnIdle requests more time,
840 * it will be repeatedly called so long as there are no pending messages.
841 * A 'feature' of this is that once OnIdle has decided that no more processing
842 * is required, then it won't get processing time until further messages
843 * are processed (it'll sit in DoMessage).
846 int wxApp::MainLoop()
850 while ( m_keepGoing
)
853 wxMutexGuiLeaveOrEnter();
854 #endif // wxUSE_THREADS
856 while ( !::PeekMessage(&s_currentMsg
, 0, 0, 0, PM_NOREMOVE
) &&
865 return s_currentMsg
.wParam
;
868 // Returns TRUE if more time is needed.
869 bool wxApp::ProcessIdle()
872 event
.SetEventObject(this);
875 return event
.MoreRequested();
879 void wxApp::ProcessPendingEvents()
881 wxNode
*node
= wxPendingEvents
->First();
882 wxCriticalSectionLocker
locker(*wxPendingEventsLocker
);
886 wxEvtHandler
*handler
= (wxEvtHandler
*)node
->Data();
888 handler
->ProcessPendingEvents();
891 node
= wxPendingEvents
->First();
897 void wxApp::ExitMainLoop()
902 bool wxApp::Pending()
904 return (::PeekMessage(&s_currentMsg
, 0, 0, 0, PM_NOREMOVE
) != 0);
907 void wxApp::Dispatch()
913 * Give all windows a chance to preprocess
914 * the message. Some may have accelerator tables, or have
918 bool wxApp::ProcessMessage(WXMSG
*wxmsg
)
920 MSG
*msg
= (MSG
*)wxmsg
;
921 HWND hWnd
= msg
->hwnd
;
922 wxWindow
*wndThis
= wxFindWinFromHandle((WXHWND
)hWnd
), *wnd
;
924 // Try translations first; find the youngest window with
925 // a translation table.
926 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
928 if ( wnd
->MSWTranslateMessage(wxmsg
) )
932 // Anyone for a non-translation message? Try youngest descendants first.
933 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
935 if ( wnd
->MSWProcessMessage(wxmsg
) )
942 void wxApp::OnIdle(wxIdleEvent
& event
)
944 static bool s_inOnIdle
= FALSE
;
946 // Avoid recursion (via ProcessEvent default case)
952 // 'Garbage' collection of windows deleted with Close().
953 DeletePendingObjects();
956 // flush the logged messages if any
957 wxLog
*pLog
= wxLog::GetActiveTarget();
958 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
962 // Send OnIdle events to all windows
963 if ( SendIdleEvents() )
965 // SendIdleEvents() returns TRUE if at least one window requested more
967 event
.RequestMore(TRUE
);
970 // If they are pending events, we must process them.
972 ProcessPendingEvents();
977 // Send idle event to all top-level windows
978 bool wxApp::SendIdleEvents()
980 bool needMore
= FALSE
;
982 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
985 wxWindow
* win
= node
->GetData();
986 if (SendIdleEvents(win
))
988 node
= node
->GetNext();
994 // Send idle event to window and all subwindows
995 bool wxApp::SendIdleEvents(wxWindow
* win
)
997 bool needMore
= FALSE
;
1000 event
.SetEventObject(win
);
1001 win
->GetEventHandler()->ProcessEvent(event
);
1003 if (event
.MoreRequested())
1006 wxNode
* node
= win
->GetChildren().First();
1009 wxWindow
* win
= (wxWindow
*) node
->Data();
1010 if (SendIdleEvents(win
))
1013 node
= node
->Next();
1018 void wxApp::DeletePendingObjects()
1020 wxNode
*node
= wxPendingDelete
.First();
1023 wxObject
*obj
= (wxObject
*)node
->Data();
1027 if (wxPendingDelete
.Member(obj
))
1030 // Deleting one object may have deleted other pending
1031 // objects, so start from beginning of list again.
1032 node
= wxPendingDelete
.First();
1036 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
1039 GetTopWindow()->Close(TRUE
);
1042 // Default behaviour: close the application with prompts. The
1043 // user can veto the close, and therefore the end session.
1044 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
1048 if (!GetTopWindow()->Close(!event
.CanVeto()))
1053 wxLog
* wxApp::CreateLogTarget()
1055 return new wxLogGui
;
1058 wxWindow
* wxApp::GetTopWindow() const
1062 else if (wxTopLevelWindows
.GetCount() > 0)
1063 return wxTopLevelWindows
.GetFirst()->GetData();
1068 int wxApp::GetComCtl32Version() const
1070 // have we loaded COMCTL32 yet?
1071 HMODULE theModule
= ::GetModuleHandle("COMCTL32");
1074 // if so, then we can check for the version
1077 // InitCommonControlsEx is unique to 4.7 and later
1078 FARPROC theProc
= ::GetProcAddress(theModule
, "InitCommonControlsEx");
1081 { // not found, must be 4.00
1086 // The following symbol are unique to 4.71
1088 // FlatSB_EnableScrollBar FlatSB_GetScrollInfo FlatSB_GetScrollPos
1089 // FlatSB_GetScrollProp FlatSB_GetScrollRange FlatSB_SetScrollInfo
1090 // FlatSB_SetScrollPos FlatSB_SetScrollProp FlatSB_SetScrollRange
1091 // FlatSB_ShowScrollBar
1092 // _DrawIndirectImageList _DuplicateImageList
1094 // UninitializeFlatSB
1095 // we could check for any of these - I chose DllInstall
1096 FARPROC theProc
= ::GetProcAddress(theModule
, "DllInstall");
1099 // not found, must be 4.70
1103 { // found, must be 4.71
1113 wxLogError(_("Fatal error: exiting"));
1118 // Yield to incoming messages
1122 // We want to go back to the main message loop
1123 // if we see a WM_QUIT. (?)
1125 while (PeekMessage(&msg
, (HWND
)NULL
, 0, 0, PM_NOREMOVE
) && msg
.message
!= WM_QUIT
)
1127 while (PeekMessage(&msg
, NULL
, 0, 0, PM_NOREMOVE
) && msg
.message
!= WM_QUIT
)
1130 if ( !wxTheApp
->DoMessage() )
1133 // If they are pending events, we must process them.
1135 wxTheApp
->ProcessPendingEvents();
1141 HINSTANCE
wxGetInstance()
1146 void wxSetInstance(HINSTANCE hInst
)
1148 wxhInstance
= hInst
;
1151 // For some reason, with MSVC++ 1.5, WinMain isn't linked in properly
1152 // if in a separate file. So include it here to ensure it's linked.
1153 #if (defined(__VISUALC__) && !defined(__WIN32__)) || (defined(__GNUWIN32__) && !defined(__TWIN32__))