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"
22 #pragma implementation "appbase.h"
25 // For compilers that support precompilation, includes "wx.h".
26 #include "wx/wxprec.h"
28 #if defined(__BORLANDC__)
36 #include "wx/gdicmn.h"
39 #include "wx/cursor.h"
41 #include "wx/palette.h"
43 #include "wx/dialog.h"
44 #include "wx/msgdlg.h"
46 #include "wx/dynarray.h"
47 # include "wx/wxchar.h"
52 #include "wx/module.h"
54 #include "wx/msw/private.h"
57 #include "wx/thread.h"
59 // define the array of MSG strutures
60 WX_DECLARE_OBJARRAY(MSG
, wxMsgArray
);
62 #include "wx/arrimpl.cpp"
64 WX_DEFINE_OBJARRAY(wxMsgArray
);
65 #endif // wxUSE_THREADS
67 #if wxUSE_WX_RESOURCES
68 #include "wx/resource.h"
71 // OLE is used for drag-and-drop, clipboard, OLE Automation...
72 #ifndef wxUSE_NORLANDER_HEADERS
73 #if defined(__GNUWIN32__) || defined(__SC__) || defined(__SALFORDC__)
77 #endif // broken compilers
87 #if (defined(__WIN95__) && !defined(__GNUWIN32__)) || defined(__TWIN32__) || defined(wxUSE_NORLANDER_HEADERS)
91 #include "wx/msw/msvcrt.h"
93 // ---------------------------------------------------------------------------
95 // ---------------------------------------------------------------------------
97 extern wxChar
*wxBuffer
;
98 extern wxChar
*wxOsVersion
;
99 extern wxList
*wxWinHandleList
;
100 extern wxList WXDLLEXPORT wxPendingDelete
;
102 extern wxList
*wxPendingEvents
;
103 extern wxCriticalSection
*wxPendingEventsLocker
;
105 extern void wxSetKeyboardHook(bool doIt
);
106 extern wxCursor
*g_globalCursor
;
108 HINSTANCE wxhInstance
= 0;
110 wxApp
*wxTheApp
= NULL
;
112 // FIXME why not const? and not static?
114 // NB: all "NoRedraw" classes must have the same names as the "normal" classes
115 // with NR suffix - wxWindow::MSWCreate() supposes this
116 wxChar wxFrameClassName
[] = _T("wxFrameClass");
117 wxChar wxFrameClassNameNoRedraw
[] = _T("wxFrameClassNR");
118 wxChar wxMDIFrameClassName
[] = _T("wxMDIFrameClass");
119 wxChar wxMDIFrameClassNameNoRedraw
[] = _T("wxMDIFrameClassNR");
120 wxChar wxMDIChildFrameClassName
[] = _T("wxMDIChildFrameClass");
121 wxChar wxMDIChildFrameClassNameNoRedraw
[] = _T("wxMDIChildFrameClassNR");
122 wxChar wxPanelClassName
[] = _T("wxPanelClass");
123 wxChar wxCanvasClassName
[] = _T("wxCanvasClass");
125 HICON wxSTD_FRAME_ICON
= (HICON
) NULL
;
126 HICON wxSTD_MDICHILDFRAME_ICON
= (HICON
) NULL
;
127 HICON wxSTD_MDIPARENTFRAME_ICON
= (HICON
) NULL
;
129 HICON wxDEFAULT_FRAME_ICON
= (HICON
) NULL
;
130 HICON wxDEFAULT_MDICHILDFRAME_ICON
= (HICON
) NULL
;
131 HICON wxDEFAULT_MDIPARENTFRAME_ICON
= (HICON
) NULL
;
133 HBRUSH wxDisableButtonBrush
= (HBRUSH
) 0;
135 LRESULT APIENTRY
wxWndProc(HWND
, UINT
, WPARAM
, LPARAM
);
137 #if defined(__WIN95__) && !defined(__TWIN32__)
138 #define wxUSE_RICHEDIT 1
140 #define wxUSE_RICHEDIT 0
144 static HINSTANCE gs_hRichEdit
= (HINSTANCE
) NULL
;
147 // ===========================================================================
149 // ===========================================================================
151 // ---------------------------------------------------------------------------
153 // ---------------------------------------------------------------------------
155 #if !USE_SHARED_LIBRARY
156 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
158 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
159 EVT_IDLE(wxApp::OnIdle
)
160 EVT_END_SESSION(wxApp::OnEndSession
)
161 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession
)
166 bool wxApp::Initialize()
168 // Some people may wish to use this, but
169 // probably it shouldn't be here by default.
171 // wxRedirectIOToConsole();
174 wxBuffer
= new wxChar
[1500]; // FIXME
176 wxClassInfo::InitializeClasses();
179 wxGetResource(_T("wxWindows"), _T("OsVersion"), &wxOsVersion
);
182 // I'm annoyed ... I don't know where to put this and I don't want to
183 // create a module for that as it's part of the core.
185 wxPendingEvents
= new wxList();
186 wxPendingEventsLocker
= new wxCriticalSection();
189 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
190 wxTheColourDatabase
->Initialize();
192 wxInitializeStockLists();
193 wxInitializeStockObjects();
195 #if wxUSE_WX_RESOURCES
196 wxInitializeResourceSystem();
199 wxBitmap::InitStandardHandlers();
201 #if defined(__WIN95__)
202 InitCommonControls();
205 gs_hRichEdit
= LoadLibrary(_T("RICHED32.DLL"));
207 if (gs_hRichEdit
== (HINSTANCE
) NULL
)
209 wxLogError(_("Could not initialise Rich Edit DLL"));
211 #endif // wxUSE_RICHEDIT
218 // for OLE, enlarge message queue to be as large as possible
220 while (!SetMessageQueue(iMsg
) && (iMsg
-= 8))
223 // we need to initialize OLE library
224 if ( FAILED(::OleInitialize(NULL
)) )
225 wxLogError(_("Cannot initialize OLE"));
229 if (!Ctl3dRegister(wxhInstance
))
230 wxLogError(_T("Cannot register CTL3D"));
232 Ctl3dAutoSubclass(wxhInstance
);
235 g_globalCursor
= new wxCursor
;
237 wxSTD_FRAME_ICON
= LoadIcon(wxhInstance
, _T("wxSTD_FRAME"));
238 wxSTD_MDIPARENTFRAME_ICON
= LoadIcon(wxhInstance
, _T("wxSTD_MDIPARENTFRAME"));
239 wxSTD_MDICHILDFRAME_ICON
= LoadIcon(wxhInstance
, _T("wxSTD_MDICHILDFRAME"));
241 wxDEFAULT_FRAME_ICON
= LoadIcon(wxhInstance
, _T("wxDEFAULT_FRAME"));
242 wxDEFAULT_MDIPARENTFRAME_ICON
= LoadIcon(wxhInstance
, _T("wxDEFAULT_MDIPARENTFRAME"));
243 wxDEFAULT_MDICHILDFRAME_ICON
= LoadIcon(wxhInstance
, _T("wxDEFAULT_MDICHILDFRAME"));
245 RegisterWindowClasses();
247 // Create the brush for disabling bitmap buttons
250 lb
.lbStyle
= BS_PATTERN
;
251 lb
.lbHatch
= (int)LoadBitmap( wxhInstance
, _T("wxDISABLE_BUTTON_BITMAP") );
254 wxDisableButtonBrush
= ::CreateBrushIndirect( & lb
);
255 ::DeleteObject( (HGDIOBJ
)lb
.lbHatch
);
257 //else: wxWindows resources are probably not linked in
263 wxWinHandleList
= new wxList(wxKEY_INTEGER
);
265 // This is to foil optimizations in Visual C++ that throw out dummy.obj.
266 // PLEASE DO NOT ALTER THIS.
267 #if defined(__VISUALC__) && !defined(WXMAKINGDLL)
268 extern char wxDummyChar
;
269 if (wxDummyChar
) wxDummyChar
++;
272 wxSetKeyboardHook(TRUE
);
274 wxModule::RegisterModules();
275 if (!wxModule::InitializeModules())
280 // ---------------------------------------------------------------------------
281 // RegisterWindowClasses
282 // ---------------------------------------------------------------------------
284 bool wxApp::RegisterWindowClasses()
288 // for each class we register one with CS_(V|H)REDRAW style and one
289 // without for windows created with wxNO_FULL_REDRAW_ON_REPAINT flag
290 static const long styleNormal
= CS_HREDRAW
| CS_VREDRAW
| CS_DBLCLKS
;
291 static const long styleNoRedraw
= CS_DBLCLKS
;
293 // the fields which are common to all classes
294 wndclass
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
295 wndclass
.cbClsExtra
= 0;
296 wndclass
.cbWndExtra
= sizeof( DWORD
); // VZ: what is this DWORD used for?
297 wndclass
.hInstance
= wxhInstance
;
298 wndclass
.hIcon
= (HICON
) NULL
;
299 wndclass
.hCursor
= ::LoadCursor((HINSTANCE
)NULL
, IDC_ARROW
);
300 wndclass
.lpszMenuName
= NULL
;
302 // Register the frame window class.
303 wndclass
.hbrBackground
= (HBRUSH
)(COLOR_APPWORKSPACE
+ 1);
304 wndclass
.style
= styleNormal
;
305 wndclass
.lpszClassName
= wxFrameClassName
;
307 if ( !RegisterClass(&wndclass
) )
309 wxLogLastError("RegisterClass(frame)");
315 wndclass
.lpszClassName
= wxFrameClassNameNoRedraw
;
316 wndclass
.style
= styleNoRedraw
;
318 if ( !RegisterClass(&wndclass
) )
320 wxLogLastError("RegisterClass(no redraw frame)");
325 // Register the MDI frame window class.
326 wndclass
.hbrBackground
= (HBRUSH
)NULL
; // paint MDI frame ourselves
327 wndclass
.lpszClassName
= wxMDIFrameClassNameNoRedraw
;
329 if ( !RegisterClass(&wndclass
) )
331 wxLogLastError("RegisterClass(MDI parent)");
336 // "no redraw" MDI frame
337 wndclass
.lpszClassName
= wxMDIFrameClassName
;
338 wndclass
.style
= styleNoRedraw
;
340 if ( !RegisterClass(&wndclass
) )
342 wxLogLastError("RegisterClass(no redraw MDI parent frame)");
347 // Register the MDI child frame window class.
348 wndclass
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+ 1);
349 wndclass
.lpszClassName
= wxMDIChildFrameClassName
;
351 if ( !RegisterClass(&wndclass
) )
353 wxLogLastError("RegisterClass(MDI child)");
358 // "no redraw" MDI child frame
359 wndclass
.lpszClassName
= wxMDIChildFrameClassNameNoRedraw
;
360 wndclass
.style
= styleNoRedraw
;
362 if ( !RegisterClass(&wndclass
) )
364 wxLogLastError("RegisterClass(no redraw MDI child)");
369 // Register the panel window class.
370 wndclass
.hbrBackground
= (HBRUSH
) GetStockObject( LTGRAY_BRUSH
);
371 wndclass
.lpszClassName
= wxPanelClassName
;
373 if ( !RegisterClass(&wndclass
) )
375 wxLogLastError("RegisterClass(panel)");
380 // Register the canvas and textsubwindow class name
381 wndclass
.hbrBackground
= (HBRUSH
)NULL
;
382 wndclass
.lpszClassName
= wxCanvasClassName
;
384 if ( !RegisterClass(&wndclass
) )
386 wxLogLastError("RegisterClass(canvas)");
394 // ---------------------------------------------------------------------------
395 // Convert Windows to argc, argv style
396 // ---------------------------------------------------------------------------
398 void wxApp::ConvertToStandardCommandArgs(char* lpCmdLine
)
402 wxString
cmdLine(lpCmdLine
);
405 // Get application name
406 wxChar name
[260]; // 260 is MAX_PATH value from windef.h
407 ::GetModuleFileName(wxhInstance
, name
, WXSIZEOF(name
));
412 wxStrcpy(name
, wxFileNameFromPath(name
));
413 wxStripExtension(name
);
414 wxTheApp
->SetAppName(name
);
417 // Treat strings enclosed in double-quotes as single arguments
419 int len
= cmdLine
.Length();
423 while ((i
< len
) && wxIsspace(cmdLine
.GetChar(i
)))
428 if (cmdLine
.GetChar(i
) == _T('"')) // We found the start of a string
432 while ((i
< len
) && (cmdLine
.GetChar(i
) != _T('"')))
435 wxString
arg(cmdLine
.Mid(first
, (i
- first
)));
441 i
++; // Skip past 2nd quote
443 else // Unquoted argument
446 while ((i
< len
) && !wxIsspace(cmdLine
.GetChar(i
)))
449 wxString
arg(cmdLine
.Mid(first
, (i
- first
)));
457 wxTheApp
->argv
= new wxChar
*[count
+ 1];
458 for (i
= 0; i
< count
; i
++)
460 wxString
arg(args
[i
]);
461 wxTheApp
->argv
[i
] = copystring((const wxChar
*)arg
);
463 wxTheApp
->argv
[count
] = NULL
; // argv[] is a NULL-terminated list
464 wxTheApp
->argc
= count
;
467 //// Cleans up any wxWindows internal structures left lying around
469 void wxApp::CleanUp()
474 // flush the logged messages if any and install a 'safer' log target: the
475 // default one (wxLogGui) can't be used after the resources are freed just
476 // below and the user suppliedo ne might be even more unsafe (using any
477 // wxWindows GUI function is unsafe starting from now)
478 wxLog::DontCreateOnDemand();
480 // this will flush the old messages if any
481 delete wxLog::SetActiveTarget(new wxLogStderr
);
484 // One last chance for pending objects to be cleaned up
485 wxTheApp
->DeletePendingObjects();
487 wxModule::CleanUpModules();
489 #if wxUSE_WX_RESOURCES
490 wxCleanUpResourceSystem();
492 // wxDefaultResourceTable->ClearTable();
495 // Indicate that the cursor can be freed, so that cursor won't be deleted
496 // by deleting the bitmap list before g_globalCursor goes out of scope
497 // (double deletion of the cursor).
498 wxSetCursor(wxNullCursor
);
499 delete g_globalCursor
;
500 g_globalCursor
= NULL
;
502 wxDeleteStockObjects();
504 // Destroy all GDI lists, etc.
505 wxDeleteStockLists();
507 delete wxTheColourDatabase
;
508 wxTheColourDatabase
= NULL
;
510 wxBitmap::CleanUpHandlers();
515 //// WINDOWS-SPECIFIC CLEANUP
517 wxSetKeyboardHook(FALSE
);
522 if (gs_hRichEdit
!= (HINSTANCE
) NULL
)
523 FreeLibrary(gs_hRichEdit
);
532 if (wxSTD_FRAME_ICON
)
533 DestroyIcon(wxSTD_FRAME_ICON
);
534 if (wxSTD_MDICHILDFRAME_ICON
)
535 DestroyIcon(wxSTD_MDICHILDFRAME_ICON
);
536 if (wxSTD_MDIPARENTFRAME_ICON
)
537 DestroyIcon(wxSTD_MDIPARENTFRAME_ICON
);
539 if (wxDEFAULT_FRAME_ICON
)
540 DestroyIcon(wxDEFAULT_FRAME_ICON
);
541 if (wxDEFAULT_MDICHILDFRAME_ICON
)
542 DestroyIcon(wxDEFAULT_MDICHILDFRAME_ICON
);
543 if (wxDEFAULT_MDIPARENTFRAME_ICON
)
544 DestroyIcon(wxDEFAULT_MDIPARENTFRAME_ICON
);
546 if ( wxDisableButtonBrush
)
547 ::DeleteObject( wxDisableButtonBrush
);
554 Ctl3dUnregister(wxhInstance
);
558 delete wxWinHandleList
;
560 // GL: I'm annoyed ... I don't know where to put this and I don't want to
561 // create a module for that as it's part of the core.
563 delete wxPendingEvents
;
564 delete wxPendingEventsLocker
;
565 // If we don't do the following, we get an apparent memory leak.
566 ((wxEvtHandler
&) wxDefaultValidator
).ClearEventLocker();
569 wxClassInfo::CleanUpClasses();
574 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
575 // At this point we want to check if there are any memory
576 // blocks that aren't part of the wxDebugContext itself,
577 // as a special case. Then when dumping we need to ignore
578 // wxDebugContext, too.
579 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
581 wxLogDebug(_T("There were memory leaks."));
582 wxDebugContext::Dump();
583 wxDebugContext::PrintStatistics();
585 // wxDebugContext::SetStream(NULL, NULL);
589 // do it as the very last thing because everything else can log messages
590 delete wxLog::SetActiveTarget(NULL
);
594 #if !defined(_WINDLL) || (defined(_WINDLL) && defined(WXMAKINGDLL))
596 // temporarily disable this warning which would be generated in release builds
599 #pragma warning(disable: 4715) // not all control paths return a value
602 //// Main wxWindows entry point
603 int wxEntry(WXHINSTANCE hInstance
,
604 WXHINSTANCE
WXUNUSED(hPrevInstance
),
609 // do check for memory leaks on program exit
610 // (another useful flag is _CRTDBG_DELAY_FREE_MEM_DF which doesn't free
611 // deallocated memory which may be used to simulate low-memory condition)
612 wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF
);
614 // take everything into a try-except block in release build
615 // FIXME other compilers must support Win32 SEH (structured exception
616 // handling) too, just find the appropriate keyword in their docs!
617 // Please note that it's _not_ the same as C++ exceptions!
618 #if !defined(__WXDEBUG__) && defined(__VISUALC__)
619 #define CATCH_PROGRAM_EXCEPTIONS
623 #undef CATCH_PROGRAM_EXCEPTIONS
626 wxhInstance
= (HINSTANCE
) hInstance
;
628 if (!wxApp::Initialize())
631 // create the application object or ensure that one already exists
634 // The app may have declared a global application object, but we recommend
635 // the IMPLEMENT_APP macro is used instead, which sets an initializer
636 // function for delayed, dynamic app object construction.
637 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
638 _T("No initializer - use IMPLEMENT_APP macro.") );
640 wxTheApp
= (*wxApp::GetInitializerFunction()) ();
643 wxCHECK_MSG( wxTheApp
, 0, _T("You have to define an instance of wxApp!") );
645 // save the WinMain() parameters
646 wxTheApp
->ConvertToStandardCommandArgs(lpCmdLine
);
647 wxTheApp
->m_nCmdShow
= nCmdShow
;
649 // GUI-specific initialisation. In fact on Windows we don't have any,
650 // but this call is provided for compatibility across platforms.
651 wxTheApp
->OnInitGui();
655 if ( wxTheApp
->OnInit() )
659 retValue
= wxTheApp
->OnRun();
662 // We want to initialize, but not run or exit immediately.
665 //else: app initialization failed, so we skipped OnRun()
667 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
670 // Forcibly delete the window.
671 if ( topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
672 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
674 topWindow
->Close(TRUE
);
675 wxTheApp
->DeletePendingObjects();
680 wxTheApp
->SetTopWindow(NULL
);
690 #ifdef CATCH_PROGRAM_EXCEPTIONS
692 __except ( EXCEPTION_EXECUTE_HANDLER
) {
695 wxTheApp->OnFatalException();
698 // using wxLog would be unsafe here
700 _("Unrecoverable program error detected: "
701 " the application will terminate."),
703 MB_APPLMODAL
| MB_ICONSTOP
| MB_OK
);
705 ::ExitProcess(3); // the same exit code as abort()
709 #endif // CATCH_PROGRAM_EXCEPTIONS
712 // restore warning state
714 #pragma warning(default: 4715) // not all control paths return a value
719 //// Entry point for DLLs
721 int wxEntry(WXHINSTANCE hInstance
)
723 wxhInstance
= (HINSTANCE
) hInstance
;
726 // The app may have declared a global application object, but we recommend
727 // the IMPLEMENT_APP macro is used instead, which sets an initializer function
728 // for delayed, dynamic app object construction.
731 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
732 "No initializer - use IMPLEMENT_APP macro." );
734 wxTheApp
= (* wxApp::GetInitializerFunction()) ();
737 wxCHECK_MSG( wxTheApp
, 0, "You have to define an instance of wxApp!" );
740 wxTheApp
->argv
= NULL
;
742 wxTheApp
->OnInitGui();
746 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
747 if ( topWindow
&& topWindow
->GetHWND())
749 topWindow
->Show(TRUE
);
756 //// Static member initialization
758 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
764 m_wantDebugOutput
= TRUE
;
768 m_printMode
= wxPRINT_WINDOWS
;
769 m_exitOnFrameDelete
= TRUE
;
775 // Delete command-line args
777 for (i
= 0; i
< argc
; i
++)
784 bool wxApp::Initialized()
792 #ifdef _WINDLL // Assume initialized if DLL (no way of telling)
798 * Get and process a message, returning FALSE if WM_QUIT
799 * received (and also set the flag telling the app to exit the main loop)
802 bool wxApp::DoMessage()
804 BOOL rc
= ::GetMessage(&s_currentMsg
, (HWND
) NULL
, 0, 0);
814 // should never happen, but let's test for it nevertheless
815 wxLogLastError("GetMessage");
820 wxASSERT_MSG( wxThread::IsMain(),
821 _T("only the main thread can process Windows messages") );
823 static bool s_hadGuiLock
= TRUE
;
824 static wxMsgArray s_aSavedMessages
;
826 // if a secondary thread owns is doing GUI calls, save all messages for
827 // later processing - we can't process them right now because it will
828 // lead to recursive library calls (and we're not reentrant)
829 if ( !wxGuiOwnedByMainThread() )
831 s_hadGuiLock
= FALSE
;
833 // leave out WM_COMMAND messages: too dangerous, sometimes
834 // the message will be processed twice
835 if ( !wxIsWaitingForThread() ||
836 s_currentMsg
.message
!= WM_COMMAND
)
838 s_aSavedMessages
.Add(s_currentMsg
);
845 // have we just regained the GUI lock? if so, post all of the saved
848 // FIXME of course, it's not _exactly_ the same as processing the
849 // messages normally - expect some things to break...
854 size_t count
= s_aSavedMessages
.Count();
855 for ( size_t n
= 0; n
< count
; n
++ )
857 MSG
& msg
= s_aSavedMessages
[n
];
859 if ( !ProcessMessage((WXMSG
*)&msg
) )
861 ::TranslateMessage(&msg
);
862 ::DispatchMessage(&msg
);
866 s_aSavedMessages
.Empty();
869 #endif // wxUSE_THREADS
871 // Process the message
872 if ( !ProcessMessage((WXMSG
*)&s_currentMsg
) )
874 ::TranslateMessage(&s_currentMsg
);
875 ::DispatchMessage(&s_currentMsg
);
883 * Keep trying to process messages until WM_QUIT
886 * If there are messages to be processed, they will all be
887 * processed and OnIdle will not be called.
888 * When there are no more messages, OnIdle is called.
889 * If OnIdle requests more time,
890 * it will be repeatedly called so long as there are no pending messages.
891 * A 'feature' of this is that once OnIdle has decided that no more processing
892 * is required, then it won't get processing time until further messages
893 * are processed (it'll sit in DoMessage).
896 int wxApp::MainLoop()
900 while ( m_keepGoing
)
903 wxMutexGuiLeaveOrEnter();
904 #endif // wxUSE_THREADS
906 while ( !::PeekMessage(&s_currentMsg
, 0, 0, 0, PM_NOREMOVE
) &&
915 return s_currentMsg
.wParam
;
918 // Returns TRUE if more time is needed.
919 bool wxApp::ProcessIdle()
922 event
.SetEventObject(this);
925 return event
.MoreRequested();
929 void wxApp::ProcessPendingEvents()
931 wxNode
*node
= wxPendingEvents
->First();
932 wxCriticalSectionLocker
locker(*wxPendingEventsLocker
);
936 wxEvtHandler
*handler
= (wxEvtHandler
*)node
->Data();
938 handler
->ProcessPendingEvents();
941 node
= wxPendingEvents
->First();
947 void wxApp::ExitMainLoop()
952 bool wxApp::Pending()
954 return (::PeekMessage(&s_currentMsg
, 0, 0, 0, PM_NOREMOVE
) != 0);
957 void wxApp::Dispatch()
963 * Give all windows a chance to preprocess
964 * the message. Some may have accelerator tables, or have
968 bool wxApp::ProcessMessage(WXMSG
*wxmsg
)
970 MSG
*msg
= (MSG
*)wxmsg
;
971 HWND hWnd
= msg
->hwnd
;
972 wxWindow
*wndThis
= wxFindWinFromHandle((WXHWND
)hWnd
), *wnd
;
974 // for some composite controls (like a combobox), wndThis might be NULL
975 // because the subcontrol is not a wxWindow, but only the control itself
976 // is - try to catch this case
977 while ( hWnd
&& !wndThis
)
979 hWnd
= ::GetParent(hWnd
);
980 wndThis
= wxFindWinFromHandle((WXHWND
)hWnd
);
983 // Try translations first; find the youngest window with
984 // a translation table.
985 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
987 if ( wnd
->MSWTranslateMessage(wxmsg
) )
991 // Anyone for a non-translation message? Try youngest descendants first.
992 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
994 if ( wnd
->MSWProcessMessage(wxmsg
) )
1001 void wxApp::OnIdle(wxIdleEvent
& event
)
1003 static bool s_inOnIdle
= FALSE
;
1005 // Avoid recursion (via ProcessEvent default case)
1011 // 'Garbage' collection of windows deleted with Close().
1012 DeletePendingObjects();
1015 // flush the logged messages if any
1016 wxLog
*pLog
= wxLog::GetActiveTarget();
1017 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
1021 // Send OnIdle events to all windows
1022 if ( SendIdleEvents() )
1024 // SendIdleEvents() returns TRUE if at least one window requested more
1026 event
.RequestMore(TRUE
);
1029 // If they are pending events, we must process them.
1031 ProcessPendingEvents();
1036 // Send idle event to all top-level windows
1037 bool wxApp::SendIdleEvents()
1039 bool needMore
= FALSE
;
1041 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
1044 wxWindow
* win
= node
->GetData();
1045 if (SendIdleEvents(win
))
1047 node
= node
->GetNext();
1053 // Send idle event to window and all subwindows
1054 bool wxApp::SendIdleEvents(wxWindow
* win
)
1056 bool needMore
= FALSE
;
1059 event
.SetEventObject(win
);
1060 win
->GetEventHandler()->ProcessEvent(event
);
1062 if (event
.MoreRequested())
1065 wxNode
* node
= win
->GetChildren().First();
1068 wxWindow
* win
= (wxWindow
*) node
->Data();
1069 if (SendIdleEvents(win
))
1072 node
= node
->Next();
1077 void wxApp::DeletePendingObjects()
1079 wxNode
*node
= wxPendingDelete
.First();
1082 wxObject
*obj
= (wxObject
*)node
->Data();
1086 if (wxPendingDelete
.Member(obj
))
1089 // Deleting one object may have deleted other pending
1090 // objects, so start from beginning of list again.
1091 node
= wxPendingDelete
.First();
1095 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
1098 GetTopWindow()->Close(TRUE
);
1101 // Default behaviour: close the application with prompts. The
1102 // user can veto the close, and therefore the end session.
1103 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
1107 if (!GetTopWindow()->Close(!event
.CanVeto()))
1112 int wxApp::GetComCtl32Version() const
1114 // have we loaded COMCTL32 yet?
1115 HMODULE theModule
= ::GetModuleHandle(_T("COMCTL32"));
1118 // if so, then we can check for the version
1121 // InitCommonControlsEx is unique to 4.7 and later
1122 FARPROC theProc
= ::GetProcAddress(theModule
, "InitCommonControlsEx");
1125 { // not found, must be 4.00
1130 // The following symbol are unique to 4.71
1132 // FlatSB_EnableScrollBar FlatSB_GetScrollInfo FlatSB_GetScrollPos
1133 // FlatSB_GetScrollProp FlatSB_GetScrollRange FlatSB_SetScrollInfo
1134 // FlatSB_SetScrollPos FlatSB_SetScrollProp FlatSB_SetScrollRange
1135 // FlatSB_ShowScrollBar
1136 // _DrawIndirectImageList _DuplicateImageList
1138 // UninitializeFlatSB
1139 // we could check for any of these - I chose DllInstall
1140 FARPROC theProc
= ::GetProcAddress(theModule
, "DllInstall");
1143 // not found, must be 4.70
1147 { // found, must be 4.71
1157 wxLogError(_("Fatal error: exiting"));
1162 // Yield to incoming messages
1166 // We want to go back to the main message loop
1167 // if we see a WM_QUIT. (?)
1169 while (PeekMessage(&msg
, (HWND
)NULL
, 0, 0, PM_NOREMOVE
) && msg
.message
!= WM_QUIT
)
1171 while (PeekMessage(&msg
, NULL
, 0, 0, PM_NOREMOVE
) && msg
.message
!= WM_QUIT
)
1174 if ( !wxTheApp
->DoMessage() )
1177 // If they are pending events, we must process them.
1179 wxTheApp
->ProcessPendingEvents();
1186 wxApp::GetStdIcon(int which
) const
1190 case wxICON_INFORMATION
:
1191 return wxIcon("wxICON_INFO");
1193 case wxICON_QUESTION
:
1194 return wxIcon("wxICON_QUESTION");
1196 case wxICON_EXCLAMATION
:
1197 return wxIcon("wxICON_WARNING");
1200 wxFAIL_MSG(_T("requested non existent standard icon"));
1201 // still fall through
1204 return wxIcon("wxICON_ERROR");
1209 HINSTANCE
wxGetInstance()
1214 void wxSetInstance(HINSTANCE hInst
)
1216 wxhInstance
= hInst
;
1219 // For some reason, with MSVC++ 1.5, WinMain isn't linked in properly
1220 // if in a separate file. So include it here to ensure it's linked.
1221 #if (defined(__VISUALC__) && !defined(__WIN32__)) || (defined(__GNUWIN32__) && !defined(__TWIN32__))