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
;
101 extern void wxSetKeyboardHook(bool doIt
);
102 extern wxCursor
*g_globalCursor
;
104 HINSTANCE wxhInstance
= 0;
106 wxApp
*wxTheApp
= NULL
;
108 // FIXME why not const? and not static?
110 // NB: all "NoRedraw" classes must have the same names as the "normal" classes
111 // with NR suffix - wxWindow::MSWCreate() supposes this
112 wxChar wxFrameClassName
[] = wxT("wxFrameClass");
113 wxChar wxFrameClassNameNoRedraw
[] = wxT("wxFrameClassNR");
114 wxChar wxMDIFrameClassName
[] = wxT("wxMDIFrameClass");
115 wxChar wxMDIFrameClassNameNoRedraw
[] = wxT("wxMDIFrameClassNR");
116 wxChar wxMDIChildFrameClassName
[] = wxT("wxMDIChildFrameClass");
117 wxChar wxMDIChildFrameClassNameNoRedraw
[] = wxT("wxMDIChildFrameClassNR");
118 wxChar wxPanelClassName
[] = wxT("wxPanelClass");
119 wxChar wxCanvasClassName
[] = wxT("wxCanvasClass");
121 HICON wxSTD_FRAME_ICON
= (HICON
) NULL
;
122 HICON wxSTD_MDICHILDFRAME_ICON
= (HICON
) NULL
;
123 HICON wxSTD_MDIPARENTFRAME_ICON
= (HICON
) NULL
;
125 HICON wxDEFAULT_FRAME_ICON
= (HICON
) NULL
;
126 HICON wxDEFAULT_MDICHILDFRAME_ICON
= (HICON
) NULL
;
127 HICON wxDEFAULT_MDIPARENTFRAME_ICON
= (HICON
) NULL
;
129 HBRUSH wxDisableButtonBrush
= (HBRUSH
) 0;
131 LRESULT APIENTRY
wxWndProc(HWND
, UINT
, WPARAM
, LPARAM
);
133 #if defined(__WIN95__) && !defined(__TWIN32__)
134 #define wxUSE_RICHEDIT 1
136 #define wxUSE_RICHEDIT 0
140 static HINSTANCE gs_hRichEdit
= (HINSTANCE
) NULL
;
143 // ===========================================================================
145 // ===========================================================================
147 // ---------------------------------------------------------------------------
149 // ---------------------------------------------------------------------------
151 #if !USE_SHARED_LIBRARY
152 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
154 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
155 EVT_IDLE(wxApp::OnIdle
)
156 EVT_END_SESSION(wxApp::OnEndSession
)
157 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession
)
162 bool wxApp::Initialize()
164 // Some people may wish to use this, but
165 // probably it shouldn't be here by default.
167 // wxRedirectIOToConsole();
170 wxBuffer
= new wxChar
[1500]; // FIXME
172 wxClassInfo::InitializeClasses();
175 wxGetResource(wxT("wxWindows"), wxT("OsVersion"), &wxOsVersion
);
179 wxPendingEventsLocker
= new wxCriticalSection
;
182 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
183 wxTheColourDatabase
->Initialize();
185 wxInitializeStockLists();
186 wxInitializeStockObjects();
188 #if wxUSE_WX_RESOURCES
189 wxInitializeResourceSystem();
192 wxBitmap::InitStandardHandlers();
194 #if defined(__WIN95__)
195 InitCommonControls();
198 gs_hRichEdit
= LoadLibrary(wxT("RICHED32.DLL"));
200 if (gs_hRichEdit
== (HINSTANCE
) NULL
)
202 wxLogError(_("Could not initialise Rich Edit DLL"));
204 #endif // wxUSE_RICHEDIT
211 // for OLE, enlarge message queue to be as large as possible
213 while (!SetMessageQueue(iMsg
) && (iMsg
-= 8))
216 // we need to initialize OLE library
217 if ( FAILED(::OleInitialize(NULL
)) )
218 wxLogError(_("Cannot initialize OLE"));
222 if (!Ctl3dRegister(wxhInstance
))
223 wxLogError(wxT("Cannot register CTL3D"));
225 Ctl3dAutoSubclass(wxhInstance
);
228 g_globalCursor
= new wxCursor
;
230 wxSTD_FRAME_ICON
= LoadIcon(wxhInstance
, wxT("wxSTD_FRAME"));
231 wxSTD_MDIPARENTFRAME_ICON
= LoadIcon(wxhInstance
, wxT("wxSTD_MDIPARENTFRAME"));
232 wxSTD_MDICHILDFRAME_ICON
= LoadIcon(wxhInstance
, wxT("wxSTD_MDICHILDFRAME"));
234 wxDEFAULT_FRAME_ICON
= LoadIcon(wxhInstance
, wxT("wxDEFAULT_FRAME"));
235 wxDEFAULT_MDIPARENTFRAME_ICON
= LoadIcon(wxhInstance
, wxT("wxDEFAULT_MDIPARENTFRAME"));
236 wxDEFAULT_MDICHILDFRAME_ICON
= LoadIcon(wxhInstance
, wxT("wxDEFAULT_MDICHILDFRAME"));
238 RegisterWindowClasses();
240 // Create the brush for disabling bitmap buttons
243 lb
.lbStyle
= BS_PATTERN
;
244 lb
.lbHatch
= (int)LoadBitmap( wxhInstance
, wxT("wxDISABLE_BUTTON_BITMAP") );
247 wxDisableButtonBrush
= ::CreateBrushIndirect( & lb
);
248 ::DeleteObject( (HGDIOBJ
)lb
.lbHatch
);
250 //else: wxWindows resources are probably not linked in
256 wxWinHandleList
= new wxList(wxKEY_INTEGER
);
258 // This is to foil optimizations in Visual C++ that throw out dummy.obj.
259 // PLEASE DO NOT ALTER THIS.
260 #if defined(__VISUALC__) && !defined(WXMAKINGDLL)
261 extern char wxDummyChar
;
262 if (wxDummyChar
) wxDummyChar
++;
265 wxSetKeyboardHook(TRUE
);
267 wxModule::RegisterModules();
268 if (!wxModule::InitializeModules())
273 // ---------------------------------------------------------------------------
274 // RegisterWindowClasses
275 // ---------------------------------------------------------------------------
277 // TODO we should only register classes really used by the app. For this it
278 // would be enough to just delay the class registration until an attempt
279 // to create a window of this class is made.
280 bool wxApp::RegisterWindowClasses()
284 // for each class we register one with CS_(V|H)REDRAW style and one
285 // without for windows created with wxNO_FULL_REDRAW_ON_REPAINT flag
286 static const long styleNormal
= CS_HREDRAW
| CS_VREDRAW
| CS_DBLCLKS
;
287 static const long styleNoRedraw
= CS_DBLCLKS
;
289 // the fields which are common to all classes
290 wndclass
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
291 wndclass
.cbClsExtra
= 0;
292 wndclass
.cbWndExtra
= sizeof( DWORD
); // VZ: what is this DWORD used for?
293 wndclass
.hInstance
= wxhInstance
;
294 wndclass
.hIcon
= (HICON
) NULL
;
295 wndclass
.hCursor
= ::LoadCursor((HINSTANCE
)NULL
, IDC_ARROW
);
296 wndclass
.lpszMenuName
= NULL
;
298 // Register the frame window class.
299 wndclass
.hbrBackground
= (HBRUSH
)(COLOR_APPWORKSPACE
+ 1);
300 wndclass
.lpszClassName
= wxFrameClassName
;
301 wndclass
.style
= styleNormal
;
303 if ( !RegisterClass(&wndclass
) )
305 wxLogLastError("RegisterClass(frame)");
311 wndclass
.lpszClassName
= wxFrameClassNameNoRedraw
;
312 wndclass
.style
= styleNoRedraw
;
314 if ( !RegisterClass(&wndclass
) )
316 wxLogLastError("RegisterClass(no redraw frame)");
321 // Register the MDI frame window class.
322 wndclass
.hbrBackground
= (HBRUSH
)NULL
; // paint MDI frame ourselves
323 wndclass
.lpszClassName
= wxMDIFrameClassName
;
324 wndclass
.style
= styleNormal
;
326 if ( !RegisterClass(&wndclass
) )
328 wxLogLastError("RegisterClass(MDI parent)");
333 // "no redraw" MDI frame
334 wndclass
.lpszClassName
= wxMDIFrameClassNameNoRedraw
;
335 wndclass
.style
= styleNoRedraw
;
337 if ( !RegisterClass(&wndclass
) )
339 wxLogLastError("RegisterClass(no redraw MDI parent frame)");
344 // Register the MDI child frame window class.
345 wndclass
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+ 1);
346 wndclass
.lpszClassName
= wxMDIChildFrameClassName
;
347 wndclass
.style
= styleNormal
;
349 if ( !RegisterClass(&wndclass
) )
351 wxLogLastError("RegisterClass(MDI child)");
356 // "no redraw" MDI child frame
357 wndclass
.lpszClassName
= wxMDIChildFrameClassNameNoRedraw
;
358 wndclass
.style
= styleNoRedraw
;
360 if ( !RegisterClass(&wndclass
) )
362 wxLogLastError("RegisterClass(no redraw MDI child)");
367 // Register the panel window class.
368 wndclass
.hbrBackground
= (HBRUSH
) GetStockObject( LTGRAY_BRUSH
);
369 wndclass
.lpszClassName
= wxPanelClassName
;
370 wndclass
.style
= styleNormal
;
372 if ( !RegisterClass(&wndclass
) )
374 wxLogLastError("RegisterClass(panel)");
379 // Register the canvas and textsubwindow class name
380 wndclass
.hbrBackground
= (HBRUSH
)NULL
;
381 wndclass
.lpszClassName
= wxCanvasClassName
;
383 if ( !RegisterClass(&wndclass
) )
385 wxLogLastError("RegisterClass(canvas)");
393 // ---------------------------------------------------------------------------
394 // Convert Windows to argc, argv style
395 // ---------------------------------------------------------------------------
397 void wxApp::ConvertToStandardCommandArgs(char* lpCmdLine
)
401 wxString
cmdLine(lpCmdLine
);
404 // Get application name
405 wxChar name
[260]; // 260 is MAX_PATH value from windef.h
406 ::GetModuleFileName(wxhInstance
, name
, WXSIZEOF(name
));
411 wxStrcpy(name
, wxFileNameFromPath(name
));
412 wxStripExtension(name
);
413 wxTheApp
->SetAppName(name
);
416 // Treat strings enclosed in double-quotes as single arguments
418 int len
= cmdLine
.Length();
422 while ((i
< len
) && wxIsspace(cmdLine
.GetChar(i
)))
427 if (cmdLine
.GetChar(i
) == wxT('"')) // We found the start of a string
431 while ((i
< len
) && (cmdLine
.GetChar(i
) != wxT('"')))
434 wxString
arg(cmdLine
.Mid(first
, (i
- first
)));
440 i
++; // Skip past 2nd quote
442 else // Unquoted argument
445 while ((i
< len
) && !wxIsspace(cmdLine
.GetChar(i
)))
448 wxString
arg(cmdLine
.Mid(first
, (i
- first
)));
456 wxTheApp
->argv
= new wxChar
*[count
+ 1];
457 for (i
= 0; i
< count
; i
++)
459 wxString
arg(args
[i
]);
460 wxTheApp
->argv
[i
] = copystring((const wxChar
*)arg
);
462 wxTheApp
->argv
[count
] = NULL
; // argv[] is a NULL-terminated list
463 wxTheApp
->argc
= count
;
466 //// Cleans up any wxWindows internal structures left lying around
468 void wxApp::CleanUp()
473 // flush the logged messages if any and install a 'safer' log target: the
474 // default one (wxLogGui) can't be used after the resources are freed just
475 // below and the user suppliedo ne might be even more unsafe (using any
476 // wxWindows GUI function is unsafe starting from now)
477 wxLog::DontCreateOnDemand();
479 // this will flush the old messages if any
480 delete wxLog::SetActiveTarget(new wxLogStderr
);
483 // One last chance for pending objects to be cleaned up
484 wxTheApp
->DeletePendingObjects();
486 wxModule::CleanUpModules();
488 #if wxUSE_WX_RESOURCES
489 wxCleanUpResourceSystem();
491 // wxDefaultResourceTable->ClearTable();
494 // Indicate that the cursor can be freed, so that cursor won't be deleted
495 // by deleting the bitmap list before g_globalCursor goes out of scope
496 // (double deletion of the cursor).
497 wxSetCursor(wxNullCursor
);
498 delete g_globalCursor
;
499 g_globalCursor
= NULL
;
501 wxDeleteStockObjects();
503 // Destroy all GDI lists, etc.
504 wxDeleteStockLists();
506 delete wxTheColourDatabase
;
507 wxTheColourDatabase
= NULL
;
509 wxBitmap::CleanUpHandlers();
514 //// WINDOWS-SPECIFIC CLEANUP
516 wxSetKeyboardHook(FALSE
);
521 if (gs_hRichEdit
!= (HINSTANCE
) NULL
)
522 FreeLibrary(gs_hRichEdit
);
531 if (wxSTD_FRAME_ICON
)
532 DestroyIcon(wxSTD_FRAME_ICON
);
533 if (wxSTD_MDICHILDFRAME_ICON
)
534 DestroyIcon(wxSTD_MDICHILDFRAME_ICON
);
535 if (wxSTD_MDIPARENTFRAME_ICON
)
536 DestroyIcon(wxSTD_MDIPARENTFRAME_ICON
);
538 if (wxDEFAULT_FRAME_ICON
)
539 DestroyIcon(wxDEFAULT_FRAME_ICON
);
540 if (wxDEFAULT_MDICHILDFRAME_ICON
)
541 DestroyIcon(wxDEFAULT_MDICHILDFRAME_ICON
);
542 if (wxDEFAULT_MDIPARENTFRAME_ICON
)
543 DestroyIcon(wxDEFAULT_MDIPARENTFRAME_ICON
);
545 if ( wxDisableButtonBrush
)
546 ::DeleteObject( wxDisableButtonBrush
);
553 Ctl3dUnregister(wxhInstance
);
557 delete wxWinHandleList
;
559 // GL: I'm annoyed ... I don't know where to put this and I don't want to
560 // create a module for that as it's part of the core.
561 delete wxPendingEvents
;
563 delete wxPendingEventsLocker
;
564 // If we don't do the following, we get an apparent memory leak.
565 ((wxEvtHandler
&) wxDefaultValidator
).ClearEventLocker();
568 wxClassInfo::CleanUpClasses();
573 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
574 // At this point we want to check if there are any memory
575 // blocks that aren't part of the wxDebugContext itself,
576 // as a special case. Then when dumping we need to ignore
577 // wxDebugContext, too.
578 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
580 wxLogDebug(wxT("There were memory leaks."));
581 wxDebugContext::Dump();
582 wxDebugContext::PrintStatistics();
584 // wxDebugContext::SetStream(NULL, NULL);
588 // do it as the very last thing because everything else can log messages
589 delete wxLog::SetActiveTarget(NULL
);
593 #if !defined(_WINDLL) || (defined(_WINDLL) && defined(WXMAKINGDLL))
595 // temporarily disable this warning which would be generated in release builds
598 #pragma warning(disable: 4715) // not all control paths return a value
601 //// Main wxWindows entry point
602 int wxEntry(WXHINSTANCE hInstance
,
603 WXHINSTANCE
WXUNUSED(hPrevInstance
),
608 // do check for memory leaks on program exit
609 // (another useful flag is _CRTDBG_DELAY_FREE_MEM_DF which doesn't free
610 // deallocated memory which may be used to simulate low-memory condition)
611 wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF
);
613 // take everything into a try-except block in release build
614 // FIXME other compilers must support Win32 SEH (structured exception
615 // handling) too, just find the appropriate keyword in their docs!
616 // Please note that it's _not_ the same as C++ exceptions!
617 #if !defined(__WXDEBUG__) && defined(__VISUALC__)
618 #define CATCH_PROGRAM_EXCEPTIONS
622 #undef CATCH_PROGRAM_EXCEPTIONS
625 wxhInstance
= (HINSTANCE
) hInstance
;
627 if (!wxApp::Initialize())
630 // create the application object or ensure that one already exists
633 // The app may have declared a global application object, but we recommend
634 // the IMPLEMENT_APP macro is used instead, which sets an initializer
635 // function for delayed, dynamic app object construction.
636 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
637 wxT("No initializer - use IMPLEMENT_APP macro.") );
639 wxTheApp
= (*wxApp::GetInitializerFunction()) ();
642 wxCHECK_MSG( wxTheApp
, 0, wxT("You have to define an instance of wxApp!") );
644 // save the WinMain() parameters
645 wxTheApp
->ConvertToStandardCommandArgs(lpCmdLine
);
646 wxTheApp
->m_nCmdShow
= nCmdShow
;
648 // GUI-specific initialisation. In fact on Windows we don't have any,
649 // but this call is provided for compatibility across platforms.
650 wxTheApp
->OnInitGui();
654 if ( wxTheApp
->OnInit() )
658 retValue
= wxTheApp
->OnRun();
661 // We want to initialize, but not run or exit immediately.
664 //else: app initialization failed, so we skipped OnRun()
666 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
669 // Forcibly delete the window.
670 if ( topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
671 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
673 topWindow
->Close(TRUE
);
674 wxTheApp
->DeletePendingObjects();
679 wxTheApp
->SetTopWindow(NULL
);
689 #ifdef CATCH_PROGRAM_EXCEPTIONS
691 __except ( EXCEPTION_EXECUTE_HANDLER
) {
694 wxTheApp->OnFatalException();
697 // using wxLog would be unsafe here
699 _("Unrecoverable program error detected: "
700 " the application will terminate."),
702 MB_APPLMODAL
| MB_ICONSTOP
| MB_OK
);
704 ::ExitProcess(3); // the same exit code as abort()
708 #endif // CATCH_PROGRAM_EXCEPTIONS
711 // restore warning state
713 #pragma warning(default: 4715) // not all control paths return a value
718 //// Entry point for DLLs
720 int wxEntry(WXHINSTANCE hInstance
)
722 wxhInstance
= (HINSTANCE
) hInstance
;
725 // The app may have declared a global application object, but we recommend
726 // the IMPLEMENT_APP macro is used instead, which sets an initializer function
727 // for delayed, dynamic app object construction.
730 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
731 "No initializer - use IMPLEMENT_APP macro." );
733 wxTheApp
= (* wxApp::GetInitializerFunction()) ();
736 wxCHECK_MSG( wxTheApp
, 0, "You have to define an instance of wxApp!" );
739 wxTheApp
->argv
= NULL
;
741 wxTheApp
->OnInitGui();
745 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
746 if ( topWindow
&& topWindow
->GetHWND())
748 topWindow
->Show(TRUE
);
755 //// Static member initialization
757 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
763 m_wantDebugOutput
= TRUE
;
767 m_printMode
= wxPRINT_WINDOWS
;
768 m_exitOnFrameDelete
= TRUE
;
774 // Delete command-line args
776 for (i
= 0; i
< argc
; i
++)
783 bool wxApp::Initialized()
791 #ifdef _WINDLL // Assume initialized if DLL (no way of telling)
797 * Get and process a message, returning FALSE if WM_QUIT
798 * received (and also set the flag telling the app to exit the main loop)
801 bool wxApp::DoMessage()
803 BOOL rc
= ::GetMessage(&s_currentMsg
, (HWND
) NULL
, 0, 0);
813 // should never happen, but let's test for it nevertheless
814 wxLogLastError("GetMessage");
819 wxASSERT_MSG( wxThread::IsMain(),
820 wxT("only the main thread can process Windows messages") );
822 static bool s_hadGuiLock
= TRUE
;
823 static wxMsgArray s_aSavedMessages
;
825 // if a secondary thread owns is doing GUI calls, save all messages for
826 // later processing - we can't process them right now because it will
827 // lead to recursive library calls (and we're not reentrant)
828 if ( !wxGuiOwnedByMainThread() )
830 s_hadGuiLock
= FALSE
;
832 // leave out WM_COMMAND messages: too dangerous, sometimes
833 // the message will be processed twice
834 if ( !wxIsWaitingForThread() ||
835 s_currentMsg
.message
!= WM_COMMAND
)
837 s_aSavedMessages
.Add(s_currentMsg
);
844 // have we just regained the GUI lock? if so, post all of the saved
847 // FIXME of course, it's not _exactly_ the same as processing the
848 // messages normally - expect some things to break...
853 size_t count
= s_aSavedMessages
.Count();
854 for ( size_t n
= 0; n
< count
; n
++ )
856 MSG
& msg
= s_aSavedMessages
[n
];
858 if ( !ProcessMessage((WXMSG
*)&msg
) )
860 ::TranslateMessage(&msg
);
861 ::DispatchMessage(&msg
);
865 s_aSavedMessages
.Empty();
868 #endif // wxUSE_THREADS
870 // Process the message
871 if ( !ProcessMessage((WXMSG
*)&s_currentMsg
) )
873 ::TranslateMessage(&s_currentMsg
);
874 ::DispatchMessage(&s_currentMsg
);
882 * Keep trying to process messages until WM_QUIT
885 * If there are messages to be processed, they will all be
886 * processed and OnIdle will not be called.
887 * When there are no more messages, OnIdle is called.
888 * If OnIdle requests more time,
889 * it will be repeatedly called so long as there are no pending messages.
890 * A 'feature' of this is that once OnIdle has decided that no more processing
891 * is required, then it won't get processing time until further messages
892 * are processed (it'll sit in DoMessage).
895 int wxApp::MainLoop()
899 while ( m_keepGoing
)
902 wxMutexGuiLeaveOrEnter();
903 #endif // wxUSE_THREADS
905 while ( !::PeekMessage(&s_currentMsg
, 0, 0, 0, PM_NOREMOVE
) &&
914 return s_currentMsg
.wParam
;
917 // Returns TRUE if more time is needed.
918 bool wxApp::ProcessIdle()
921 event
.SetEventObject(this);
924 return event
.MoreRequested();
927 void wxApp::ProcessPendingEvents()
930 // ensure that we're the only thread to modify the pending events list
931 wxCriticalSectionLocker
locker(*wxPendingEventsLocker
);
934 if ( !wxPendingEvents
)
937 wxNode
*node
= wxPendingEvents
->First();
940 wxEvtHandler
*handler
= (wxEvtHandler
*)node
->Data();
942 handler
->ProcessPendingEvents();
945 node
= wxPendingEvents
->First();
949 void wxApp::ExitMainLoop()
954 bool wxApp::Pending()
956 return (::PeekMessage(&s_currentMsg
, 0, 0, 0, PM_NOREMOVE
) != 0);
959 void wxApp::Dispatch()
965 * Give all windows a chance to preprocess
966 * the message. Some may have accelerator tables, or have
970 bool wxApp::ProcessMessage(WXMSG
*wxmsg
)
972 MSG
*msg
= (MSG
*)wxmsg
;
973 HWND hWnd
= msg
->hwnd
;
974 wxWindow
*wndThis
= wxFindWinFromHandle((WXHWND
)hWnd
), *wnd
;
976 // for some composite controls (like a combobox), wndThis might be NULL
977 // because the subcontrol is not a wxWindow, but only the control itself
978 // is - try to catch this case
979 while ( hWnd
&& !wndThis
)
981 hWnd
= ::GetParent(hWnd
);
982 wndThis
= wxFindWinFromHandle((WXHWND
)hWnd
);
985 // Try translations first; find the youngest window with
986 // a translation table.
987 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
989 if ( wnd
->MSWTranslateMessage(wxmsg
) )
993 // Anyone for a non-translation message? Try youngest descendants first.
994 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
996 if ( wnd
->MSWProcessMessage(wxmsg
) )
1003 void wxApp::OnIdle(wxIdleEvent
& event
)
1005 static bool s_inOnIdle
= FALSE
;
1007 // Avoid recursion (via ProcessEvent default case)
1013 // 'Garbage' collection of windows deleted with Close().
1014 DeletePendingObjects();
1017 // flush the logged messages if any
1018 wxLog
*pLog
= wxLog::GetActiveTarget();
1019 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
1023 // Send OnIdle events to all windows
1024 if ( SendIdleEvents() )
1026 // SendIdleEvents() returns TRUE if at least one window requested more
1028 event
.RequestMore(TRUE
);
1031 // If they are pending events, we must process them: pending events are
1032 // either events to the threads other than main or events posted with
1033 // wxPostEvent() functions
1034 ProcessPendingEvents();
1039 // Send idle event to all top-level windows
1040 bool wxApp::SendIdleEvents()
1042 bool needMore
= FALSE
;
1044 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
1047 wxWindow
* win
= node
->GetData();
1048 if (SendIdleEvents(win
))
1050 node
= node
->GetNext();
1056 // Send idle event to window and all subwindows
1057 bool wxApp::SendIdleEvents(wxWindow
* win
)
1059 bool needMore
= FALSE
;
1062 event
.SetEventObject(win
);
1063 win
->GetEventHandler()->ProcessEvent(event
);
1065 if (event
.MoreRequested())
1068 wxNode
* node
= win
->GetChildren().First();
1071 wxWindow
* win
= (wxWindow
*) node
->Data();
1072 if (SendIdleEvents(win
))
1075 node
= node
->Next();
1080 void wxApp::DeletePendingObjects()
1082 wxNode
*node
= wxPendingDelete
.First();
1085 wxObject
*obj
= (wxObject
*)node
->Data();
1089 if (wxPendingDelete
.Member(obj
))
1092 // Deleting one object may have deleted other pending
1093 // objects, so start from beginning of list again.
1094 node
= wxPendingDelete
.First();
1098 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
1101 GetTopWindow()->Close(TRUE
);
1104 // Default behaviour: close the application with prompts. The
1105 // user can veto the close, and therefore the end session.
1106 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
1110 if (!GetTopWindow()->Close(!event
.CanVeto()))
1115 int wxApp::GetComCtl32Version() const
1117 // have we loaded COMCTL32 yet?
1118 HMODULE theModule
= ::GetModuleHandle(wxT("COMCTL32"));
1121 // if so, then we can check for the version
1124 // InitCommonControlsEx is unique to 4.7 and later
1125 FARPROC theProc
= ::GetProcAddress(theModule
, "InitCommonControlsEx");
1128 { // not found, must be 4.00
1133 // The following symbol are unique to 4.71
1135 // FlatSB_EnableScrollBar FlatSB_GetScrollInfo FlatSB_GetScrollPos
1136 // FlatSB_GetScrollProp FlatSB_GetScrollRange FlatSB_SetScrollInfo
1137 // FlatSB_SetScrollPos FlatSB_SetScrollProp FlatSB_SetScrollRange
1138 // FlatSB_ShowScrollBar
1139 // _DrawIndirectImageList _DuplicateImageList
1141 // UninitializeFlatSB
1142 // we could check for any of these - I chose DllInstall
1143 FARPROC theProc
= ::GetProcAddress(theModule
, "DllInstall");
1146 // not found, must be 4.70
1150 { // found, must be 4.71
1160 wxLogError(_("Fatal error: exiting"));
1165 // Yield to incoming messages
1168 // we don't want to process WM_QUIT from here - it should be processed in
1169 // the main event loop in order to stop it
1172 while ( PeekMessage(&msg
, (HWND
)0, 0, 0, PM_NOREMOVE
) &&
1173 msg
.message
!= WM_QUIT
)
1175 if ( !wxTheApp
->DoMessage() )
1179 // If they are pending events, we must process them.
1180 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(wxT("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__))