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"
70 // OLE is used for drag-and-drop, clipboard, OLE Automation...
71 #ifndef wxUSE_NORLANDER_HEADERS
72 #if defined(__GNUWIN32__) || defined(__SC__) || defined(__SALFORDC__)
76 #endif // broken compilers
86 #if (defined(__WIN95__) && !defined(__GNUWIN32__)) || defined(__TWIN32__) || defined(wxUSE_NORLANDER_HEADERS)
90 #include "wx/msw/msvcrt.h"
92 // ---------------------------------------------------------------------------
94 // ---------------------------------------------------------------------------
96 extern wxChar
*wxBuffer
;
97 extern wxChar
*wxOsVersion
;
98 extern wxList
*wxWinHandleList
;
99 extern wxList WXDLLEXPORT wxPendingDelete
;
100 extern void wxSetKeyboardHook(bool doIt
);
101 extern wxCursor
*g_globalCursor
;
103 HINSTANCE wxhInstance
= 0;
105 wxApp
*wxTheApp
= NULL
;
107 // FIXME why not const? and not static?
109 // NB: all "NoRedraw" classes must have the same names as the "normal" classes
110 // with NR suffix - wxWindow::MSWCreate() supposes this
111 wxChar wxFrameClassName
[] = wxT("wxFrameClass");
112 wxChar wxFrameClassNameNoRedraw
[] = wxT("wxFrameClassNR");
113 wxChar wxMDIFrameClassName
[] = wxT("wxMDIFrameClass");
114 wxChar wxMDIFrameClassNameNoRedraw
[] = wxT("wxMDIFrameClassNR");
115 wxChar wxMDIChildFrameClassName
[] = wxT("wxMDIChildFrameClass");
116 wxChar wxMDIChildFrameClassNameNoRedraw
[] = wxT("wxMDIChildFrameClassNR");
117 wxChar wxPanelClassName
[] = wxT("wxPanelClass");
118 wxChar wxCanvasClassName
[] = wxT("wxCanvasClass");
120 HICON wxSTD_FRAME_ICON
= (HICON
) NULL
;
121 HICON wxSTD_MDICHILDFRAME_ICON
= (HICON
) NULL
;
122 HICON wxSTD_MDIPARENTFRAME_ICON
= (HICON
) NULL
;
124 HICON wxDEFAULT_FRAME_ICON
= (HICON
) NULL
;
125 HICON wxDEFAULT_MDICHILDFRAME_ICON
= (HICON
) NULL
;
126 HICON wxDEFAULT_MDIPARENTFRAME_ICON
= (HICON
) NULL
;
128 HBRUSH wxDisableButtonBrush
= (HBRUSH
) 0;
130 LRESULT WXDLLEXPORT APIENTRY
wxWndProc(HWND
, UINT
, WPARAM
, LPARAM
);
132 #if defined(__WIN95__) && !defined(__TWIN32__)
133 #define wxUSE_RICHEDIT 1
135 #define wxUSE_RICHEDIT 0
139 static HINSTANCE gs_hRichEdit
= (HINSTANCE
) NULL
;
142 // ===========================================================================
144 // ===========================================================================
146 // ---------------------------------------------------------------------------
148 // ---------------------------------------------------------------------------
150 #if !USE_SHARED_LIBRARY
151 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
153 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
154 EVT_IDLE(wxApp::OnIdle
)
155 EVT_END_SESSION(wxApp::OnEndSession
)
156 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession
)
161 bool wxApp::Initialize()
163 // Some people may wish to use this, but
164 // probably it shouldn't be here by default.
166 // wxRedirectIOToConsole();
169 wxBuffer
= new wxChar
[1500]; // FIXME
171 wxClassInfo::InitializeClasses();
174 wxGetResource(wxT("wxWindows"), wxT("OsVersion"), &wxOsVersion
);
178 wxPendingEventsLocker
= new wxCriticalSection
;
181 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
182 wxTheColourDatabase
->Initialize();
184 wxInitializeStockLists();
185 wxInitializeStockObjects();
187 #if wxUSE_WX_RESOURCES
188 wxInitializeResourceSystem();
191 wxBitmap::InitStandardHandlers();
193 #if defined(__WIN95__)
194 InitCommonControls();
197 gs_hRichEdit
= LoadLibrary(wxT("RICHED32.DLL"));
199 if (gs_hRichEdit
== (HINSTANCE
) NULL
)
201 wxLogError(_("Could not initialise Rich Edit DLL"));
203 #endif // wxUSE_RICHEDIT
210 // for OLE, enlarge message queue to be as large as possible
212 while (!SetMessageQueue(iMsg
) && (iMsg
-= 8))
215 // we need to initialize OLE library
216 if ( FAILED(::OleInitialize(NULL
)) )
217 wxLogError(_("Cannot initialize OLE"));
221 if (!Ctl3dRegister(wxhInstance
))
222 wxLogError(wxT("Cannot register CTL3D"));
224 Ctl3dAutoSubclass(wxhInstance
);
227 g_globalCursor
= new wxCursor
;
229 wxSTD_FRAME_ICON
= LoadIcon(wxhInstance
, wxT("wxSTD_FRAME"));
230 wxSTD_MDIPARENTFRAME_ICON
= LoadIcon(wxhInstance
, wxT("wxSTD_MDIPARENTFRAME"));
231 wxSTD_MDICHILDFRAME_ICON
= LoadIcon(wxhInstance
, wxT("wxSTD_MDICHILDFRAME"));
233 wxDEFAULT_FRAME_ICON
= LoadIcon(wxhInstance
, wxT("wxDEFAULT_FRAME"));
234 wxDEFAULT_MDIPARENTFRAME_ICON
= LoadIcon(wxhInstance
, wxT("wxDEFAULT_MDIPARENTFRAME"));
235 wxDEFAULT_MDICHILDFRAME_ICON
= LoadIcon(wxhInstance
, wxT("wxDEFAULT_MDICHILDFRAME"));
237 RegisterWindowClasses();
239 // Create the brush for disabling bitmap buttons
242 lb
.lbStyle
= BS_PATTERN
;
243 lb
.lbHatch
= (int)LoadBitmap( wxhInstance
, wxT("wxDISABLE_BUTTON_BITMAP") );
246 wxDisableButtonBrush
= ::CreateBrushIndirect( & lb
);
247 ::DeleteObject( (HGDIOBJ
)lb
.lbHatch
);
249 //else: wxWindows resources are probably not linked in
255 wxWinHandleList
= new wxList(wxKEY_INTEGER
);
257 // This is to foil optimizations in Visual C++ that throw out dummy.obj.
258 // PLEASE DO NOT ALTER THIS.
259 #if defined(__VISUALC__) && !defined(WXMAKINGDLL)
260 extern char wxDummyChar
;
261 if (wxDummyChar
) wxDummyChar
++;
264 wxSetKeyboardHook(TRUE
);
266 wxModule::RegisterModules();
267 if (!wxModule::InitializeModules())
272 // ---------------------------------------------------------------------------
273 // RegisterWindowClasses
274 // ---------------------------------------------------------------------------
276 // TODO we should only register classes really used by the app. For this it
277 // would be enough to just delay the class registration until an attempt
278 // to create a window of this class is made.
279 bool wxApp::RegisterWindowClasses()
283 // for each class we register one with CS_(V|H)REDRAW style and one
284 // without for windows created with wxNO_FULL_REDRAW_ON_REPAINT flag
285 static const long styleNormal
= CS_HREDRAW
| CS_VREDRAW
| CS_DBLCLKS
;
286 static const long styleNoRedraw
= CS_DBLCLKS
;
288 // the fields which are common to all classes
289 wndclass
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
290 wndclass
.cbClsExtra
= 0;
291 wndclass
.cbWndExtra
= sizeof( DWORD
); // VZ: what is this DWORD used for?
292 wndclass
.hInstance
= wxhInstance
;
293 wndclass
.hIcon
= (HICON
) NULL
;
294 wndclass
.hCursor
= ::LoadCursor((HINSTANCE
)NULL
, IDC_ARROW
);
295 wndclass
.lpszMenuName
= NULL
;
297 // Register the frame window class.
298 wndclass
.hbrBackground
= (HBRUSH
)(COLOR_APPWORKSPACE
+ 1);
299 wndclass
.lpszClassName
= wxFrameClassName
;
300 wndclass
.style
= styleNormal
;
302 if ( !RegisterClass(&wndclass
) )
304 wxLogLastError("RegisterClass(frame)");
310 wndclass
.lpszClassName
= wxFrameClassNameNoRedraw
;
311 wndclass
.style
= styleNoRedraw
;
313 if ( !RegisterClass(&wndclass
) )
315 wxLogLastError("RegisterClass(no redraw frame)");
320 // Register the MDI frame window class.
321 wndclass
.hbrBackground
= (HBRUSH
)NULL
; // paint MDI frame ourselves
322 wndclass
.lpszClassName
= wxMDIFrameClassName
;
323 wndclass
.style
= styleNormal
;
325 if ( !RegisterClass(&wndclass
) )
327 wxLogLastError("RegisterClass(MDI parent)");
332 // "no redraw" MDI frame
333 wndclass
.lpszClassName
= wxMDIFrameClassNameNoRedraw
;
334 wndclass
.style
= styleNoRedraw
;
336 if ( !RegisterClass(&wndclass
) )
338 wxLogLastError("RegisterClass(no redraw MDI parent frame)");
343 // Register the MDI child frame window class.
344 wndclass
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+ 1);
345 wndclass
.lpszClassName
= wxMDIChildFrameClassName
;
346 wndclass
.style
= styleNormal
;
348 if ( !RegisterClass(&wndclass
) )
350 wxLogLastError("RegisterClass(MDI child)");
355 // "no redraw" MDI child frame
356 wndclass
.lpszClassName
= wxMDIChildFrameClassNameNoRedraw
;
357 wndclass
.style
= styleNoRedraw
;
359 if ( !RegisterClass(&wndclass
) )
361 wxLogLastError("RegisterClass(no redraw MDI child)");
366 // Register the panel window class.
367 wndclass
.hbrBackground
= (HBRUSH
) GetStockObject( LTGRAY_BRUSH
);
368 wndclass
.lpszClassName
= wxPanelClassName
;
369 wndclass
.style
= styleNormal
;
371 if ( !RegisterClass(&wndclass
) )
373 wxLogLastError("RegisterClass(panel)");
378 // Register the canvas and textsubwindow class name
379 wndclass
.hbrBackground
= (HBRUSH
)NULL
;
380 wndclass
.lpszClassName
= wxCanvasClassName
;
382 if ( !RegisterClass(&wndclass
) )
384 wxLogLastError("RegisterClass(canvas)");
392 // ---------------------------------------------------------------------------
393 // Convert Windows to argc, argv style
394 // ---------------------------------------------------------------------------
396 void wxApp::ConvertToStandardCommandArgs(char* lpCmdLine
)
400 wxString
cmdLine(lpCmdLine
);
403 // Get application name
404 wxChar name
[260]; // 260 is MAX_PATH value from windef.h
405 ::GetModuleFileName(wxhInstance
, name
, WXSIZEOF(name
));
410 wxStrcpy(name
, wxFileNameFromPath(name
));
411 wxStripExtension(name
);
412 wxTheApp
->SetAppName(name
);
415 // Treat strings enclosed in double-quotes as single arguments
417 int len
= cmdLine
.Length();
421 while ((i
< len
) && wxIsspace(cmdLine
.GetChar(i
)))
426 if (cmdLine
.GetChar(i
) == wxT('"')) // We found the start of a string
430 while ((i
< len
) && (cmdLine
.GetChar(i
) != wxT('"')))
433 wxString
arg(cmdLine
.Mid(first
, (i
- first
)));
439 i
++; // Skip past 2nd quote
441 else // Unquoted argument
444 while ((i
< len
) && !wxIsspace(cmdLine
.GetChar(i
)))
447 wxString
arg(cmdLine
.Mid(first
, (i
- first
)));
455 wxTheApp
->argv
= new wxChar
*[count
+ 1];
456 for (i
= 0; i
< count
; i
++)
458 wxString
arg(args
[i
]);
459 wxTheApp
->argv
[i
] = copystring((const wxChar
*)arg
);
461 wxTheApp
->argv
[count
] = NULL
; // argv[] is a NULL-terminated list
462 wxTheApp
->argc
= count
;
465 //// Cleans up any wxWindows internal structures left lying around
467 void wxApp::CleanUp()
472 // flush the logged messages if any and install a 'safer' log target: the
473 // default one (wxLogGui) can't be used after the resources are freed just
474 // below and the user suppliedo ne might be even more unsafe (using any
475 // wxWindows GUI function is unsafe starting from now)
476 wxLog::DontCreateOnDemand();
478 // this will flush the old messages if any
479 delete wxLog::SetActiveTarget(new wxLogStderr
);
482 // One last chance for pending objects to be cleaned up
483 wxTheApp
->DeletePendingObjects();
485 wxModule::CleanUpModules();
487 #if wxUSE_WX_RESOURCES
488 wxCleanUpResourceSystem();
490 // wxDefaultResourceTable->ClearTable();
493 // Indicate that the cursor can be freed, so that cursor won't be deleted
494 // by deleting the bitmap list before g_globalCursor goes out of scope
495 // (double deletion of the cursor).
496 wxSetCursor(wxNullCursor
);
497 delete g_globalCursor
;
498 g_globalCursor
= NULL
;
500 wxDeleteStockObjects();
502 // Destroy all GDI lists, etc.
503 wxDeleteStockLists();
505 delete wxTheColourDatabase
;
506 wxTheColourDatabase
= NULL
;
508 wxBitmap::CleanUpHandlers();
513 //// WINDOWS-SPECIFIC CLEANUP
515 wxSetKeyboardHook(FALSE
);
520 if (gs_hRichEdit
!= (HINSTANCE
) NULL
)
521 FreeLibrary(gs_hRichEdit
);
530 if (wxSTD_FRAME_ICON
)
531 DestroyIcon(wxSTD_FRAME_ICON
);
532 if (wxSTD_MDICHILDFRAME_ICON
)
533 DestroyIcon(wxSTD_MDICHILDFRAME_ICON
);
534 if (wxSTD_MDIPARENTFRAME_ICON
)
535 DestroyIcon(wxSTD_MDIPARENTFRAME_ICON
);
537 if (wxDEFAULT_FRAME_ICON
)
538 DestroyIcon(wxDEFAULT_FRAME_ICON
);
539 if (wxDEFAULT_MDICHILDFRAME_ICON
)
540 DestroyIcon(wxDEFAULT_MDICHILDFRAME_ICON
);
541 if (wxDEFAULT_MDIPARENTFRAME_ICON
)
542 DestroyIcon(wxDEFAULT_MDIPARENTFRAME_ICON
);
544 if ( wxDisableButtonBrush
)
545 ::DeleteObject( wxDisableButtonBrush
);
552 Ctl3dUnregister(wxhInstance
);
556 delete wxWinHandleList
;
558 // GL: I'm annoyed ... I don't know where to put this and I don't want to
559 // create a module for that as it's part of the core.
560 delete wxPendingEvents
;
562 delete wxPendingEventsLocker
;
563 // If we don't do the following, we get an apparent memory leak.
564 ((wxEvtHandler
&) wxDefaultValidator
).ClearEventLocker();
567 wxClassInfo::CleanUpClasses();
572 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
573 // At this point we want to check if there are any memory
574 // blocks that aren't part of the wxDebugContext itself,
575 // as a special case. Then when dumping we need to ignore
576 // wxDebugContext, too.
577 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
579 wxLogDebug(wxT("There were memory leaks."));
580 wxDebugContext::Dump();
581 wxDebugContext::PrintStatistics();
583 // wxDebugContext::SetStream(NULL, NULL);
587 // do it as the very last thing because everything else can log messages
588 delete wxLog::SetActiveTarget(NULL
);
592 #if !defined(_WINDLL) || (defined(_WINDLL) && defined(WXMAKINGDLL))
594 // temporarily disable this warning which would be generated in release builds
597 #pragma warning(disable: 4715) // not all control paths return a value
600 //// Main wxWindows entry point
601 int wxEntry(WXHINSTANCE hInstance
,
602 WXHINSTANCE
WXUNUSED(hPrevInstance
),
607 // do check for memory leaks on program exit
608 // (another useful flag is _CRTDBG_DELAY_FREE_MEM_DF which doesn't free
609 // deallocated memory which may be used to simulate low-memory condition)
610 wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF
);
612 // take everything into a try-except block in release build
613 // FIXME other compilers must support Win32 SEH (structured exception
614 // handling) too, just find the appropriate keyword in their docs!
615 // Please note that it's _not_ the same as C++ exceptions!
616 #if !defined(__WXDEBUG__) && defined(__VISUALC__)
617 #define CATCH_PROGRAM_EXCEPTIONS
621 #undef CATCH_PROGRAM_EXCEPTIONS
623 wxhInstance
= (HINSTANCE
) hInstance
;
625 if (!wxApp::Initialize())
628 // create the application object or ensure that one already exists
631 // The app may have declared a global application object, but we recommend
632 // the IMPLEMENT_APP macro is used instead, which sets an initializer
633 // function for delayed, dynamic app object construction.
634 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
635 wxT("No initializer - use IMPLEMENT_APP macro.") );
637 wxTheApp
= (*wxApp::GetInitializerFunction()) ();
640 wxCHECK_MSG( wxTheApp
, 0, wxT("You have to define an instance of wxApp!") );
642 // save the WinMain() parameters
643 wxTheApp
->ConvertToStandardCommandArgs(lpCmdLine
);
644 wxTheApp
->m_nCmdShow
= nCmdShow
;
646 // GUI-specific initialisation. In fact on Windows we don't have any,
647 // but this call is provided for compatibility across platforms.
648 wxTheApp
->OnInitGui();
650 // We really don't want timestamps by default, because it means
651 // we can't simply double-click on the error message and get to that
652 // line in the source. So VC++ at least, let's have a sensible default.
654 wxLog::SetTimestamp(NULL
);
659 if ( wxTheApp
->OnInit() )
663 retValue
= wxTheApp
->OnRun();
666 // We want to initialize, but not run or exit immediately.
669 //else: app initialization failed, so we skipped OnRun()
671 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
674 // Forcibly delete the window.
675 if ( topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
676 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
678 topWindow
->Close(TRUE
);
679 wxTheApp
->DeletePendingObjects();
684 wxTheApp
->SetTopWindow(NULL
);
694 #ifdef CATCH_PROGRAM_EXCEPTIONS
696 __except ( EXCEPTION_EXECUTE_HANDLER
) {
699 wxTheApp->OnFatalException();
702 // using wxLog would be unsafe here
704 _("Unrecoverable program error detected: "
705 " the application will terminate."),
707 MB_APPLMODAL
| MB_ICONSTOP
| MB_OK
);
709 ::ExitProcess(3); // the same exit code as abort()
713 #endif // CATCH_PROGRAM_EXCEPTIONS
716 // restore warning state
718 #pragma warning(default: 4715) // not all control paths return a value
723 //// Entry point for DLLs
725 int wxEntry(WXHINSTANCE hInstance
)
727 wxhInstance
= (HINSTANCE
) hInstance
;
730 // The app may have declared a global application object, but we recommend
731 // the IMPLEMENT_APP macro is used instead, which sets an initializer function
732 // for delayed, dynamic app object construction.
735 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
736 "No initializer - use IMPLEMENT_APP macro." );
738 wxTheApp
= (* wxApp::GetInitializerFunction()) ();
741 wxCHECK_MSG( wxTheApp
, 0, "You have to define an instance of wxApp!" );
744 wxTheApp
->argv
= NULL
;
746 wxTheApp
->OnInitGui();
750 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
751 if ( topWindow
&& topWindow
->GetHWND())
753 topWindow
->Show(TRUE
);
760 //// Static member initialization
762 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
768 m_wantDebugOutput
= TRUE
;
772 m_printMode
= wxPRINT_WINDOWS
;
773 m_exitOnFrameDelete
= TRUE
;
779 // Delete command-line args
781 for (i
= 0; i
< argc
; i
++)
788 bool wxApp::Initialized()
796 #ifdef _WINDLL // Assume initialized if DLL (no way of telling)
802 * Get and process a message, returning FALSE if WM_QUIT
803 * received (and also set the flag telling the app to exit the main loop)
806 bool wxApp::DoMessage()
808 BOOL rc
= ::GetMessage(&s_currentMsg
, (HWND
) NULL
, 0, 0);
818 // should never happen, but let's test for it nevertheless
819 wxLogLastError("GetMessage");
824 wxASSERT_MSG( wxThread::IsMain(),
825 wxT("only the main thread can process Windows messages") );
827 static bool s_hadGuiLock
= TRUE
;
828 static wxMsgArray s_aSavedMessages
;
830 // if a secondary thread owns is doing GUI calls, save all messages for
831 // later processing - we can't process them right now because it will
832 // lead to recursive library calls (and we're not reentrant)
833 if ( !wxGuiOwnedByMainThread() )
835 s_hadGuiLock
= FALSE
;
837 // leave out WM_COMMAND messages: too dangerous, sometimes
838 // the message will be processed twice
839 if ( !wxIsWaitingForThread() ||
840 s_currentMsg
.message
!= WM_COMMAND
)
842 s_aSavedMessages
.Add(s_currentMsg
);
849 // have we just regained the GUI lock? if so, post all of the saved
852 // FIXME of course, it's not _exactly_ the same as processing the
853 // messages normally - expect some things to break...
858 size_t count
= s_aSavedMessages
.Count();
859 for ( size_t n
= 0; n
< count
; n
++ )
861 MSG
& msg
= s_aSavedMessages
[n
];
863 if ( !ProcessMessage((WXMSG
*)&msg
) )
865 ::TranslateMessage(&msg
);
866 ::DispatchMessage(&msg
);
870 s_aSavedMessages
.Empty();
873 #endif // wxUSE_THREADS
875 // Process the message
876 if ( !ProcessMessage((WXMSG
*)&s_currentMsg
) )
878 ::TranslateMessage(&s_currentMsg
);
879 ::DispatchMessage(&s_currentMsg
);
887 * Keep trying to process messages until WM_QUIT
890 * If there are messages to be processed, they will all be
891 * processed and OnIdle will not be called.
892 * When there are no more messages, OnIdle is called.
893 * If OnIdle requests more time,
894 * it will be repeatedly called so long as there are no pending messages.
895 * A 'feature' of this is that once OnIdle has decided that no more processing
896 * is required, then it won't get processing time until further messages
897 * are processed (it'll sit in DoMessage).
900 int wxApp::MainLoop()
904 while ( m_keepGoing
)
907 wxMutexGuiLeaveOrEnter();
908 #endif // wxUSE_THREADS
910 while ( !::PeekMessage(&s_currentMsg
, 0, 0, 0, PM_NOREMOVE
) &&
919 return s_currentMsg
.wParam
;
922 // Returns TRUE if more time is needed.
923 bool wxApp::ProcessIdle()
926 event
.SetEventObject(this);
929 return event
.MoreRequested();
932 void wxApp::ExitMainLoop()
937 bool wxApp::Pending()
939 return (::PeekMessage(&s_currentMsg
, 0, 0, 0, PM_NOREMOVE
) != 0);
942 void wxApp::Dispatch()
948 * Give all windows a chance to preprocess
949 * the message. Some may have accelerator tables, or have
953 bool wxApp::ProcessMessage(WXMSG
*wxmsg
)
955 MSG
*msg
= (MSG
*)wxmsg
;
956 HWND hWnd
= msg
->hwnd
;
957 wxWindow
*wndThis
= wxFindWinFromHandle((WXHWND
)hWnd
), *wnd
;
959 // for some composite controls (like a combobox), wndThis might be NULL
960 // because the subcontrol is not a wxWindow, but only the control itself
961 // is - try to catch this case
962 while ( hWnd
&& !wndThis
)
964 hWnd
= ::GetParent(hWnd
);
965 wndThis
= wxFindWinFromHandle((WXHWND
)hWnd
);
968 // Try translations first; find the youngest window with
969 // a translation table.
970 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
972 if ( wnd
->MSWTranslateMessage(wxmsg
) )
976 // Anyone for a non-translation message? Try youngest descendants first.
977 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
979 if ( wnd
->MSWProcessMessage(wxmsg
) )
986 void wxApp::OnIdle(wxIdleEvent
& event
)
988 static bool s_inOnIdle
= FALSE
;
990 // Avoid recursion (via ProcessEvent default case)
996 // 'Garbage' collection of windows deleted with Close().
997 DeletePendingObjects();
1000 // flush the logged messages if any
1001 wxLog
*pLog
= wxLog::GetActiveTarget();
1002 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
1006 // Send OnIdle events to all windows
1007 if ( SendIdleEvents() )
1009 // SendIdleEvents() returns TRUE if at least one window requested more
1011 event
.RequestMore(TRUE
);
1014 // If they are pending events, we must process them: pending events are
1015 // either events to the threads other than main or events posted with
1016 // wxPostEvent() functions
1017 ProcessPendingEvents();
1022 // Send idle event to all top-level windows
1023 bool wxApp::SendIdleEvents()
1025 bool needMore
= FALSE
;
1027 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
1030 wxWindow
* win
= node
->GetData();
1031 if (SendIdleEvents(win
))
1033 node
= node
->GetNext();
1039 // Send idle event to window and all subwindows
1040 bool wxApp::SendIdleEvents(wxWindow
* win
)
1042 bool needMore
= FALSE
;
1045 event
.SetEventObject(win
);
1046 win
->GetEventHandler()->ProcessEvent(event
);
1048 if (event
.MoreRequested())
1051 wxNode
* node
= win
->GetChildren().First();
1054 wxWindow
* win
= (wxWindow
*) node
->Data();
1055 if (SendIdleEvents(win
))
1058 node
= node
->Next();
1063 void wxApp::DeletePendingObjects()
1065 wxNode
*node
= wxPendingDelete
.First();
1068 wxObject
*obj
= (wxObject
*)node
->Data();
1072 if (wxPendingDelete
.Member(obj
))
1075 // Deleting one object may have deleted other pending
1076 // objects, so start from beginning of list again.
1077 node
= wxPendingDelete
.First();
1081 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
1084 GetTopWindow()->Close(TRUE
);
1087 // Default behaviour: close the application with prompts. The
1088 // user can veto the close, and therefore the end session.
1089 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
1093 if (!GetTopWindow()->Close(!event
.CanVeto()))
1098 int wxApp::GetComCtl32Version() const
1100 // have we loaded COMCTL32 yet?
1101 HMODULE theModule
= ::GetModuleHandle(wxT("COMCTL32"));
1104 // if so, then we can check for the version
1107 // InitCommonControlsEx is unique to 4.7 and later
1108 FARPROC theProc
= ::GetProcAddress(theModule
, "InitCommonControlsEx");
1111 { // not found, must be 4.00
1116 // The following symbol are unique to 4.71
1118 // FlatSB_EnableScrollBar FlatSB_GetScrollInfo FlatSB_GetScrollPos
1119 // FlatSB_GetScrollProp FlatSB_GetScrollRange FlatSB_SetScrollInfo
1120 // FlatSB_SetScrollPos FlatSB_SetScrollProp FlatSB_SetScrollRange
1121 // FlatSB_ShowScrollBar
1122 // _DrawIndirectImageList _DuplicateImageList
1124 // UninitializeFlatSB
1125 // we could check for any of these - I chose DllInstall
1126 FARPROC theProc
= ::GetProcAddress(theModule
, "DllInstall");
1129 // not found, must be 4.70
1133 { // found, must be 4.71
1143 wxLogError(_("Fatal error: exiting"));
1148 // Yield to incoming messages
1151 // we don't want to process WM_QUIT from here - it should be processed in
1152 // the main event loop in order to stop it
1155 while ( PeekMessage(&msg
, (HWND
)0, 0, 0, PM_NOREMOVE
) &&
1156 msg
.message
!= WM_QUIT
)
1158 if ( !wxTheApp
->DoMessage() )
1162 // If they are pending events, we must process them.
1163 wxTheApp
->ProcessPendingEvents();
1169 wxApp::GetStdIcon(int which
) const
1173 case wxICON_INFORMATION
:
1174 return wxIcon("wxICON_INFO");
1176 case wxICON_QUESTION
:
1177 return wxIcon("wxICON_QUESTION");
1179 case wxICON_EXCLAMATION
:
1180 return wxIcon("wxICON_WARNING");
1183 wxFAIL_MSG(wxT("requested non existent standard icon"));
1184 // still fall through
1187 return wxIcon("wxICON_ERROR");
1192 HINSTANCE
wxGetInstance()
1197 void wxSetInstance(HINSTANCE hInst
)
1199 wxhInstance
= hInst
;
1202 // For some reason, with MSVC++ 1.5, WinMain isn't linked in properly
1203 // if in a separate file. So include it here to ensure it's linked.
1204 #if (defined(__VISUALC__) && !defined(__WIN32__)) || (defined(__GNUWIN32__) && !defined(__TWIN32__))