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" );
237 wxDisableButtonBrush
= ::CreateBrushIndirect( & lb
);
238 ::DeleteObject( (HGDIOBJ
)lb
.lbHatch
);
244 wxWinHandleList
= new wxList(wxKEY_INTEGER
);
246 // This is to foil optimizations in Visual C++ that throw out dummy.obj.
247 // PLEASE DO NOT ALTER THIS.
248 #if defined(__VISUALC__) && !defined(WXMAKINGDLL)
249 extern char wxDummyChar
;
250 if (wxDummyChar
) wxDummyChar
++;
253 wxSetKeyboardHook(TRUE
);
255 wxModule::RegisterModules();
256 if (!wxModule::InitializeModules())
261 // ---------------------------------------------------------------------------
262 // RegisterWindowClasses
263 // ---------------------------------------------------------------------------
265 bool wxApp::RegisterWindowClasses()
269 // the fields which are common to all classes
270 wndclass
.style
= CS_HREDRAW
| CS_VREDRAW
| CS_DBLCLKS
;
271 wndclass
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
272 wndclass
.cbClsExtra
= 0;
273 wndclass
.cbWndExtra
= sizeof( DWORD
); // what is this DWORD used for?
274 wndclass
.hInstance
= wxhInstance
;
275 wndclass
.hIcon
= (HICON
) NULL
;
276 wndclass
.hCursor
= ::LoadCursor((HINSTANCE
)NULL
, IDC_ARROW
);
277 wndclass
.lpszMenuName
= NULL
;
279 // Register the frame window class.
280 wndclass
.hbrBackground
= (HBRUSH
)(COLOR_APPWORKSPACE
+ 1);
281 #ifdef _MULTIPLE_INSTANCES
282 sprintf( wxFrameClassName
,"wxFrameClass%d", wxhInstance
);
284 wndclass
.lpszClassName
= wxFrameClassName
;
286 if ( !RegisterClass(&wndclass
) )
288 wxLogLastError("RegisterClass(frame)");
293 // Register the MDI frame window class.
294 wndclass
.hbrBackground
= (HBRUSH
)NULL
; // paint MDI frame ourselves
295 wndclass
.lpszClassName
= wxMDIFrameClassName
;
297 if ( !RegisterClass(&wndclass
) )
299 wxLogLastError("RegisterClass(MDI parent)");
304 // Register the MDI child frame window class.
305 wndclass
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+ 1);
306 wndclass
.lpszClassName
= wxMDIChildFrameClassName
;
308 if ( !RegisterClass(&wndclass
) )
310 wxLogLastError("RegisterClass(MDI child)");
315 // Register the panel window class.
316 wndclass
.hbrBackground
= (HBRUSH
) GetStockObject( LTGRAY_BRUSH
);
317 wndclass
.lpszClassName
= wxPanelClassName
;
319 if ( !RegisterClass(&wndclass
) )
321 wxLogLastError("RegisterClass(panel)");
326 // Register the canvas and textsubwindow class name
327 wndclass
.hbrBackground
= (HBRUSH
)NULL
;
328 wndclass
.lpszClassName
= wxCanvasClassName
;
330 if ( !RegisterClass(&wndclass
) )
332 wxLogLastError("RegisterClass(canvas)");
340 // ---------------------------------------------------------------------------
341 // Convert Windows to argc, argv style
342 // ---------------------------------------------------------------------------
344 void wxApp::ConvertToStandardCommandArgs(char* lpCmdLine
)
348 wxString
cmdLine(lpCmdLine
);
351 // Get application name
352 char name
[260]; // 260 is MAX_PATH value from windef.h
353 ::GetModuleFileName(wxhInstance
, name
, WXSIZEOF(name
));
358 strcpy(name
, wxFileNameFromPath(name
));
359 wxStripExtension(name
);
360 wxTheApp
->SetAppName(name
);
363 // Treat strings enclosed in double-quotes as single arguments
365 int len
= cmdLine
.Length();
369 while ((i
< len
) && isspace(cmdLine
.GetChar(i
)))
374 if (cmdLine
.GetChar(i
) == '"') // We found the start of a string
378 while ((i
< len
) && (cmdLine
.GetChar(i
) != '"'))
381 wxString
arg(cmdLine
.Mid(first
, (i
- first
)));
387 i
++; // Skip past 2nd quote
389 else // Unquoted argument
392 while ((i
< len
) && !isspace(cmdLine
.GetChar(i
)))
395 wxString
arg(cmdLine
.Mid(first
, (i
- first
)));
403 wxTheApp
->argv
= new char*[count
+ 1];
404 for (i
= 0; i
< count
; i
++)
406 wxString
arg(args
[i
]);
407 wxTheApp
->argv
[i
] = copystring((const char*)arg
);
409 wxTheApp
->argv
[count
] = NULL
; // argv[] is a NULL-terminated list
410 wxTheApp
->argc
= count
;
413 //// Cleans up any wxWindows internal structures left lying around
415 void wxApp::CleanUp()
420 // flush the logged messages if any and install a 'safer' log target: the
421 // default one (wxLogGui) can't be used after the resources are freed just
422 // below and the user suppliedo ne might be even more unsafe (using any
423 // wxWindows GUI function is unsafe starting from now)
424 wxLog::DontCreateOnDemand();
426 // this will flush the old messages if any
427 delete wxLog::SetActiveTarget(new wxLogStderr
);
430 // One last chance for pending objects to be cleaned up
431 wxTheApp
->DeletePendingObjects();
433 wxModule::CleanUpModules();
435 #if wxUSE_WX_RESOURCES
436 wxCleanUpResourceSystem();
438 // wxDefaultResourceTable->ClearTable();
441 // Indicate that the cursor can be freed, so that cursor won't be deleted
442 // by deleting the bitmap list before g_globalCursor goes out of scope
443 // (double deletion of the cursor).
444 wxSetCursor(wxNullCursor
);
445 delete g_globalCursor
;
446 g_globalCursor
= NULL
;
448 wxDeleteStockObjects();
450 // Destroy all GDI lists, etc.
451 wxDeleteStockLists();
453 delete wxTheColourDatabase
;
454 wxTheColourDatabase
= NULL
;
456 wxBitmap::CleanUpHandlers();
461 //// WINDOWS-SPECIFIC CLEANUP
463 wxSetKeyboardHook(FALSE
);
468 if (gs_hRichEdit
!= (HINSTANCE
) NULL
)
469 FreeLibrary(gs_hRichEdit
);
478 if (wxSTD_FRAME_ICON
)
479 DestroyIcon(wxSTD_FRAME_ICON
);
480 if (wxSTD_MDICHILDFRAME_ICON
)
481 DestroyIcon(wxSTD_MDICHILDFRAME_ICON
);
482 if (wxSTD_MDIPARENTFRAME_ICON
)
483 DestroyIcon(wxSTD_MDIPARENTFRAME_ICON
);
485 if (wxDEFAULT_FRAME_ICON
)
486 DestroyIcon(wxDEFAULT_FRAME_ICON
);
487 if (wxDEFAULT_MDICHILDFRAME_ICON
)
488 DestroyIcon(wxDEFAULT_MDICHILDFRAME_ICON
);
489 if (wxDEFAULT_MDIPARENTFRAME_ICON
)
490 DestroyIcon(wxDEFAULT_MDIPARENTFRAME_ICON
);
492 if ( wxDisableButtonBrush
)
493 ::DeleteObject( wxDisableButtonBrush
);
500 Ctl3dUnregister(wxhInstance
);
504 delete wxWinHandleList
;
506 // GL: I'm annoyed ... I don't know where to put this and I don't want to
507 // create a module for that as it's part of the core.
509 delete wxPendingEvents
;
510 delete wxPendingEventsLocker
;
511 // If we don't do the following, we get an apparent memory leak.
512 ((wxEvtHandler
&) wxDefaultValidator
).ClearEventLocker();
515 wxClassInfo::CleanUpClasses();
520 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
521 // At this point we want to check if there are any memory
522 // blocks that aren't part of the wxDebugContext itself,
523 // as a special case. Then when dumping we need to ignore
524 // wxDebugContext, too.
525 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
527 wxLogDebug("There were memory leaks.");
528 wxDebugContext::Dump();
529 wxDebugContext::PrintStatistics();
531 // wxDebugContext::SetStream(NULL, NULL);
535 // do it as the very last thing because everything else can log messages
536 delete wxLog::SetActiveTarget(NULL
);
540 #if !defined(_WINDLL) || (defined(_WINDLL) && defined(WXMAKINGDLL))
542 // temporarily disable this warning which would be generated in release builds
545 #pragma warning(disable: 4715) // not all control paths return a value
548 //// Main wxWindows entry point
549 int wxEntry(WXHINSTANCE hInstance
,
550 WXHINSTANCE
WXUNUSED(hPrevInstance
),
555 // do check for memory leaks on program exit
556 // (another useful flag is _CRTDBG_DELAY_FREE_MEM_DF which doesn't free
557 // deallocated memory which may be used to simulate low-memory condition)
558 wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF
);
560 // take everything into a try-except block in release build
561 // FIXME other compilers must support Win32 SEH (structured exception
562 // handling) too, just find the appropriate keyword in their docs!
563 // Please note that it's _not_ the same as C++ exceptions!
564 #if !defined(__WXDEBUG__) && defined(__VISUALC__)
565 #define CATCH_PROGRAM_EXCEPTIONS
569 #undef CATCH_PROGRAM_EXCEPTIONS
572 wxhInstance
= (HINSTANCE
) hInstance
;
574 if (!wxApp::Initialize())
577 // create the application object or ensure that one already exists
580 // The app may have declared a global application object, but we recommend
581 // the IMPLEMENT_APP macro is used instead, which sets an initializer
582 // function for delayed, dynamic app object construction.
583 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
584 "No initializer - use IMPLEMENT_APP macro." );
586 wxTheApp
= (*wxApp::GetInitializerFunction()) ();
589 wxCHECK_MSG( wxTheApp
, 0, "You have to define an instance of wxApp!" );
591 // save the WinMain() parameters
592 wxTheApp
->ConvertToStandardCommandArgs(lpCmdLine
);
593 wxTheApp
->m_nCmdShow
= nCmdShow
;
595 // GUI-specific initialisation. In fact on Windows we don't have any,
596 // but this call is provided for compatibility across platforms.
597 wxTheApp
->OnInitGui();
601 if ( wxTheApp
->OnInit() )
605 retValue
= wxTheApp
->OnRun();
608 // We want to initialize, but not run or exit immediately.
611 //else: app initialization failed, so we skipped OnRun()
613 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
616 // Forcibly delete the window.
617 if ( topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
618 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
620 topWindow
->Close(TRUE
);
621 wxTheApp
->DeletePendingObjects();
626 wxTheApp
->SetTopWindow(NULL
);
636 #ifdef CATCH_PROGRAM_EXCEPTIONS
638 __except ( EXCEPTION_EXECUTE_HANDLER
) {
641 wxTheApp->OnFatalException();
644 // using wxLog would be unsafe here
646 _("Unrecoverable program error detected: "
647 " the application will terminate."),
649 MB_APPLMODAL
| MB_ICONSTOP
| MB_OK
);
651 ::ExitProcess(3); // the same exit code as abort()
655 #endif // CATCH_PROGRAM_EXCEPTIONS
658 // restore warning state
660 #pragma warning(default: 4715) // not all control paths return a value
665 //// Entry point for DLLs
667 int wxEntry(WXHINSTANCE hInstance
)
669 wxhInstance
= (HINSTANCE
) hInstance
;
672 // The app may have declared a global application object, but we recommend
673 // the IMPLEMENT_APP macro is used instead, which sets an initializer function
674 // for delayed, dynamic app object construction.
677 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
678 "No initializer - use IMPLEMENT_APP macro." );
680 wxTheApp
= (* wxApp::GetInitializerFunction()) ();
683 wxCHECK_MSG( wxTheApp
, 0, "You have to define an instance of wxApp!" );
686 wxTheApp
->argv
= NULL
;
688 wxTheApp
->OnInitGui();
692 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
693 if ( topWindow
&& topWindow
->GetHWND())
695 topWindow
->Show(TRUE
);
702 //// Static member initialization
704 wxAppInitializerFunction
wxApp::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
710 m_wantDebugOutput
= TRUE
;
714 m_printMode
= wxPRINT_WINDOWS
;
715 m_exitOnFrameDelete
= TRUE
;
721 // Delete command-line args
723 for (i
= 0; i
< argc
; i
++)
730 bool wxApp::Initialized()
738 #ifdef _WINDLL // Assume initialized if DLL (no way of telling)
744 * Get and process a message, returning FALSE if WM_QUIT
745 * received (and also set the flag telling the app to exit the main loop)
748 bool wxApp::DoMessage()
750 BOOL rc
= ::GetMessage(&s_currentMsg
, (HWND
) NULL
, 0, 0);
760 // should never happen, but let's test for it nevertheless
761 wxLogLastError("GetMessage");
766 wxASSERT_MSG( wxThread::IsMain(),
767 "only the main thread can process Windows messages" );
769 static bool s_hadGuiLock
= TRUE
;
770 static wxMsgArray s_aSavedMessages
;
772 // if a secondary thread owns is doing GUI calls, save all messages for
773 // later processing - we can't process them right now because it will
774 // lead to recursive library calls (and we're not reentrant)
775 if ( !wxGuiOwnedByMainThread() )
777 s_hadGuiLock
= FALSE
;
779 // leave out WM_COMMAND messages: too dangerous, sometimes
780 // the message will be processed twice
781 if ( !wxIsWaitingForThread() ||
782 s_currentMsg
.message
!= WM_COMMAND
)
784 s_aSavedMessages
.Add(s_currentMsg
);
791 // have we just regained the GUI lock? if so, post all of the saved
794 // FIXME of course, it's not _exactly_ the same as processing the
795 // messages normally - expect some things to break...
800 size_t count
= s_aSavedMessages
.Count();
801 for ( size_t n
= 0; n
< count
; n
++ )
803 MSG
& msg
= s_aSavedMessages
[n
];
805 if ( !ProcessMessage((WXMSG
*)&msg
) )
807 ::TranslateMessage(&msg
);
808 ::DispatchMessage(&msg
);
812 s_aSavedMessages
.Empty();
815 #endif // wxUSE_THREADS
817 // Process the message
818 if ( !ProcessMessage((WXMSG
*)&s_currentMsg
) )
820 ::TranslateMessage(&s_currentMsg
);
821 ::DispatchMessage(&s_currentMsg
);
829 * Keep trying to process messages until WM_QUIT
832 * If there are messages to be processed, they will all be
833 * processed and OnIdle will not be called.
834 * When there are no more messages, OnIdle is called.
835 * If OnIdle requests more time,
836 * it will be repeatedly called so long as there are no pending messages.
837 * A 'feature' of this is that once OnIdle has decided that no more processing
838 * is required, then it won't get processing time until further messages
839 * are processed (it'll sit in DoMessage).
842 int wxApp::MainLoop()
846 while ( m_keepGoing
)
849 wxMutexGuiLeaveOrEnter();
850 #endif // wxUSE_THREADS
852 while ( !::PeekMessage(&s_currentMsg
, 0, 0, 0, PM_NOREMOVE
) &&
861 return s_currentMsg
.wParam
;
864 // Returns TRUE if more time is needed.
865 bool wxApp::ProcessIdle()
868 event
.SetEventObject(this);
871 return event
.MoreRequested();
875 void wxApp::ProcessPendingEvents()
877 wxNode
*node
= wxPendingEvents
->First();
878 wxCriticalSectionLocker
locker(*wxPendingEventsLocker
);
882 wxEvtHandler
*handler
= (wxEvtHandler
*)node
->Data();
884 handler
->ProcessPendingEvents();
887 node
= wxPendingEvents
->First();
893 void wxApp::ExitMainLoop()
898 bool wxApp::Pending()
900 return (::PeekMessage(&s_currentMsg
, 0, 0, 0, PM_NOREMOVE
) != 0);
903 void wxApp::Dispatch()
909 * Give all windows a chance to preprocess
910 * the message. Some may have accelerator tables, or have
914 bool wxApp::ProcessMessage(WXMSG
*wxmsg
)
916 MSG
*msg
= (MSG
*)wxmsg
;
917 HWND hWnd
= msg
->hwnd
;
918 wxWindow
*wndThis
= wxFindWinFromHandle((WXHWND
)hWnd
), *wnd
;
920 // Try translations first; find the youngest window with
921 // a translation table.
922 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
924 if ( wnd
->MSWTranslateMessage(wxmsg
) )
928 // Anyone for a non-translation message? Try youngest descendants first.
929 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
931 if ( wnd
->MSWProcessMessage(wxmsg
) )
938 void wxApp::OnIdle(wxIdleEvent
& event
)
940 static bool s_inOnIdle
= FALSE
;
942 // Avoid recursion (via ProcessEvent default case)
948 // 'Garbage' collection of windows deleted with Close().
949 DeletePendingObjects();
952 // flush the logged messages if any
953 wxLog
*pLog
= wxLog::GetActiveTarget();
954 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
958 // Send OnIdle events to all windows
959 if ( SendIdleEvents() )
961 // SendIdleEvents() returns TRUE if at least one window requested more
963 event
.RequestMore(TRUE
);
966 // If they are pending events, we must process them.
968 ProcessPendingEvents();
973 // Send idle event to all top-level windows
974 bool wxApp::SendIdleEvents()
976 bool needMore
= FALSE
;
978 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
981 wxWindow
* win
= node
->GetData();
982 if (SendIdleEvents(win
))
984 node
= node
->GetNext();
990 // Send idle event to window and all subwindows
991 bool wxApp::SendIdleEvents(wxWindow
* win
)
993 bool needMore
= FALSE
;
996 event
.SetEventObject(win
);
997 win
->GetEventHandler()->ProcessEvent(event
);
999 if (event
.MoreRequested())
1002 wxNode
* node
= win
->GetChildren().First();
1005 wxWindow
* win
= (wxWindow
*) node
->Data();
1006 if (SendIdleEvents(win
))
1009 node
= node
->Next();
1014 void wxApp::DeletePendingObjects()
1016 wxNode
*node
= wxPendingDelete
.First();
1019 wxObject
*obj
= (wxObject
*)node
->Data();
1023 if (wxPendingDelete
.Member(obj
))
1026 // Deleting one object may have deleted other pending
1027 // objects, so start from beginning of list again.
1028 node
= wxPendingDelete
.First();
1032 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
1035 GetTopWindow()->Close(TRUE
);
1038 // Default behaviour: close the application with prompts. The
1039 // user can veto the close, and therefore the end session.
1040 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
1044 if (!GetTopWindow()->Close(!event
.CanVeto()))
1049 wxLog
* wxApp::CreateLogTarget()
1051 return new wxLogGui
;
1054 wxWindow
* wxApp::GetTopWindow() const
1058 else if (wxTopLevelWindows
.GetCount() > 0)
1059 return wxTopLevelWindows
.GetFirst()->GetData();
1064 int wxApp::GetComCtl32Version() const
1066 // have we loaded COMCTL32 yet?
1067 HMODULE theModule
= ::GetModuleHandle("COMCTL32");
1070 // if so, then we can check for the version
1073 // InitCommonControlsEx is unique to 4.7 and later
1074 FARPROC theProc
= ::GetProcAddress(theModule
, "InitCommonControlsEx");
1077 { // not found, must be 4.00
1082 // The following symbol are unique to 4.71
1084 // FlatSB_EnableScrollBar FlatSB_GetScrollInfo FlatSB_GetScrollPos
1085 // FlatSB_GetScrollProp FlatSB_GetScrollRange FlatSB_SetScrollInfo
1086 // FlatSB_SetScrollPos FlatSB_SetScrollProp FlatSB_SetScrollRange
1087 // FlatSB_ShowScrollBar
1088 // _DrawIndirectImageList _DuplicateImageList
1090 // UninitializeFlatSB
1091 // we could check for any of these - I chose DllInstall
1092 FARPROC theProc
= ::GetProcAddress(theModule
, "DllInstall");
1095 // not found, must be 4.70
1099 { // found, must be 4.71
1109 wxLogError(_("Fatal error: exiting"));
1114 // Yield to incoming messages
1118 // We want to go back to the main message loop
1119 // if we see a WM_QUIT. (?)
1120 while (PeekMessage(&msg
, NULL
, 0, 0, PM_NOREMOVE
) && msg
.message
!= WM_QUIT
)
1122 if ( !wxTheApp
->DoMessage() )
1125 // If they are pending events, we must process them.
1127 wxTheApp
->ProcessPendingEvents();
1133 HINSTANCE
wxGetInstance()
1138 void wxSetInstance(HINSTANCE hInst
)
1140 wxhInstance
= hInst
;
1143 // For some reason, with MSVC++ 1.5, WinMain isn't linked in properly
1144 // if in a separate file. So include it here to ensure it's linked.
1145 #if (defined(__VISUALC__) && !defined(__WIN32__)) || (defined(__GNUWIN32__) && !defined(__TWIN32__))