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 // VZ: these icons are not in wx.rc anyhow (but should they?)!
231 wxSTD_FRAME_ICON
= LoadIcon(wxhInstance
, wxT("wxSTD_FRAME"));
232 wxSTD_MDIPARENTFRAME_ICON
= LoadIcon(wxhInstance
, wxT("wxSTD_MDIPARENTFRAME"));
233 wxSTD_MDICHILDFRAME_ICON
= LoadIcon(wxhInstance
, wxT("wxSTD_MDICHILDFRAME"));
235 wxDEFAULT_FRAME_ICON
= LoadIcon(wxhInstance
, wxT("wxDEFAULT_FRAME"));
236 wxDEFAULT_MDIPARENTFRAME_ICON
= LoadIcon(wxhInstance
, wxT("wxDEFAULT_MDIPARENTFRAME"));
237 wxDEFAULT_MDICHILDFRAME_ICON
= LoadIcon(wxhInstance
, wxT("wxDEFAULT_MDICHILDFRAME"));
240 RegisterWindowClasses();
242 // Create the brush for disabling bitmap buttons
245 lb
.lbStyle
= BS_PATTERN
;
246 lb
.lbHatch
= (int)LoadBitmap( wxhInstance
, wxT("wxDISABLE_BUTTON_BITMAP") );
249 wxDisableButtonBrush
= ::CreateBrushIndirect( & lb
);
250 ::DeleteObject( (HGDIOBJ
)lb
.lbHatch
);
252 //else: wxWindows resources are probably not linked in
258 wxWinHandleList
= new wxList(wxKEY_INTEGER
);
260 // This is to foil optimizations in Visual C++ that throw out dummy.obj.
261 // PLEASE DO NOT ALTER THIS.
262 #if defined(__VISUALC__) && !defined(WXMAKINGDLL)
263 extern char wxDummyChar
;
264 if (wxDummyChar
) wxDummyChar
++;
267 wxSetKeyboardHook(TRUE
);
269 wxModule::RegisterModules();
270 if (!wxModule::InitializeModules())
275 // ---------------------------------------------------------------------------
276 // RegisterWindowClasses
277 // ---------------------------------------------------------------------------
279 // TODO we should only register classes really used by the app. For this it
280 // would be enough to just delay the class registration until an attempt
281 // to create a window of this class is made.
282 bool wxApp::RegisterWindowClasses()
286 // for each class we register one with CS_(V|H)REDRAW style and one
287 // without for windows created with wxNO_FULL_REDRAW_ON_REPAINT flag
288 static const long styleNormal
= CS_HREDRAW
| CS_VREDRAW
| CS_DBLCLKS
;
289 static const long styleNoRedraw
= CS_DBLCLKS
;
291 // the fields which are common to all classes
292 wndclass
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
293 wndclass
.cbClsExtra
= 0;
294 wndclass
.cbWndExtra
= sizeof( DWORD
); // VZ: what is this DWORD used for?
295 wndclass
.hInstance
= wxhInstance
;
296 wndclass
.hIcon
= (HICON
) NULL
;
297 wndclass
.hCursor
= ::LoadCursor((HINSTANCE
)NULL
, IDC_ARROW
);
298 wndclass
.lpszMenuName
= NULL
;
300 // Register the frame window class.
301 wndclass
.hbrBackground
= (HBRUSH
)(COLOR_APPWORKSPACE
+ 1);
302 wndclass
.lpszClassName
= wxFrameClassName
;
303 wndclass
.style
= styleNormal
;
305 if ( !RegisterClass(&wndclass
) )
307 wxLogLastError("RegisterClass(frame)");
313 wndclass
.lpszClassName
= wxFrameClassNameNoRedraw
;
314 wndclass
.style
= styleNoRedraw
;
316 if ( !RegisterClass(&wndclass
) )
318 wxLogLastError("RegisterClass(no redraw frame)");
323 // Register the MDI frame window class.
324 wndclass
.hbrBackground
= (HBRUSH
)NULL
; // paint MDI frame ourselves
325 wndclass
.lpszClassName
= wxMDIFrameClassName
;
326 wndclass
.style
= styleNormal
;
328 if ( !RegisterClass(&wndclass
) )
330 wxLogLastError("RegisterClass(MDI parent)");
335 // "no redraw" MDI frame
336 wndclass
.lpszClassName
= wxMDIFrameClassNameNoRedraw
;
337 wndclass
.style
= styleNoRedraw
;
339 if ( !RegisterClass(&wndclass
) )
341 wxLogLastError("RegisterClass(no redraw MDI parent frame)");
346 // Register the MDI child frame window class.
347 wndclass
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+ 1);
348 wndclass
.lpszClassName
= wxMDIChildFrameClassName
;
349 wndclass
.style
= styleNormal
;
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
;
372 wndclass
.style
= styleNormal
;
374 if ( !RegisterClass(&wndclass
) )
376 wxLogLastError("RegisterClass(panel)");
381 // Register the canvas and textsubwindow class name
382 wndclass
.hbrBackground
= (HBRUSH
)NULL
;
383 wndclass
.lpszClassName
= wxCanvasClassName
;
385 if ( !RegisterClass(&wndclass
) )
387 wxLogLastError("RegisterClass(canvas)");
395 // ---------------------------------------------------------------------------
396 // Convert Windows to argc, argv style
397 // ---------------------------------------------------------------------------
399 void wxApp::ConvertToStandardCommandArgs(char* lpCmdLine
)
403 wxString
cmdLine(lpCmdLine
);
406 // Get application name
407 wxChar name
[260]; // 260 is MAX_PATH value from windef.h
408 ::GetModuleFileName(wxhInstance
, name
, WXSIZEOF(name
));
413 wxStrcpy(name
, wxFileNameFromPath(name
));
414 wxStripExtension(name
);
415 wxTheApp
->SetAppName(name
);
418 // Treat strings enclosed in double-quotes as single arguments
420 int len
= cmdLine
.Length();
424 while ((i
< len
) && wxIsspace(cmdLine
.GetChar(i
)))
429 if (cmdLine
.GetChar(i
) == wxT('"')) // We found the start of a string
433 while ((i
< len
) && (cmdLine
.GetChar(i
) != wxT('"')))
436 wxString
arg(cmdLine
.Mid(first
, (i
- first
)));
442 i
++; // Skip past 2nd quote
444 else // Unquoted argument
447 while ((i
< len
) && !wxIsspace(cmdLine
.GetChar(i
)))
450 wxString
arg(cmdLine
.Mid(first
, (i
- first
)));
458 wxTheApp
->argv
= new wxChar
*[count
+ 1];
459 for (i
= 0; i
< count
; i
++)
461 wxString
arg(args
[i
]);
462 wxTheApp
->argv
[i
] = copystring((const wxChar
*)arg
);
464 wxTheApp
->argv
[count
] = NULL
; // argv[] is a NULL-terminated list
465 wxTheApp
->argc
= count
;
468 //// Cleans up any wxWindows internal structures left lying around
470 void wxApp::CleanUp()
475 // flush the logged messages if any and install a 'safer' log target: the
476 // default one (wxLogGui) can't be used after the resources are freed just
477 // below and the user suppliedo ne might be even more unsafe (using any
478 // wxWindows GUI function is unsafe starting from now)
479 wxLog::DontCreateOnDemand();
481 // this will flush the old messages if any
482 delete wxLog::SetActiveTarget(new wxLogStderr
);
485 // One last chance for pending objects to be cleaned up
486 wxTheApp
->DeletePendingObjects();
488 wxModule::CleanUpModules();
490 #if wxUSE_WX_RESOURCES
491 wxCleanUpResourceSystem();
493 // wxDefaultResourceTable->ClearTable();
496 // Indicate that the cursor can be freed, so that cursor won't be deleted
497 // by deleting the bitmap list before g_globalCursor goes out of scope
498 // (double deletion of the cursor).
499 wxSetCursor(wxNullCursor
);
500 delete g_globalCursor
;
501 g_globalCursor
= NULL
;
503 wxDeleteStockObjects();
505 // Destroy all GDI lists, etc.
506 wxDeleteStockLists();
508 delete wxTheColourDatabase
;
509 wxTheColourDatabase
= NULL
;
511 wxBitmap::CleanUpHandlers();
516 //// WINDOWS-SPECIFIC CLEANUP
518 wxSetKeyboardHook(FALSE
);
523 if (gs_hRichEdit
!= (HINSTANCE
) NULL
)
524 FreeLibrary(gs_hRichEdit
);
533 if (wxSTD_FRAME_ICON
)
534 DestroyIcon(wxSTD_FRAME_ICON
);
535 if (wxSTD_MDICHILDFRAME_ICON
)
536 DestroyIcon(wxSTD_MDICHILDFRAME_ICON
);
537 if (wxSTD_MDIPARENTFRAME_ICON
)
538 DestroyIcon(wxSTD_MDIPARENTFRAME_ICON
);
540 if (wxDEFAULT_FRAME_ICON
)
541 DestroyIcon(wxDEFAULT_FRAME_ICON
);
542 if (wxDEFAULT_MDICHILDFRAME_ICON
)
543 DestroyIcon(wxDEFAULT_MDICHILDFRAME_ICON
);
544 if (wxDEFAULT_MDIPARENTFRAME_ICON
)
545 DestroyIcon(wxDEFAULT_MDIPARENTFRAME_ICON
);
547 if ( wxDisableButtonBrush
)
548 ::DeleteObject( wxDisableButtonBrush
);
555 Ctl3dUnregister(wxhInstance
);
559 delete wxWinHandleList
;
561 // GL: I'm annoyed ... I don't know where to put this and I don't want to
562 // create a module for that as it's part of the core.
563 delete wxPendingEvents
;
565 delete wxPendingEventsLocker
;
566 // If we don't do the following, we get an apparent memory leak.
567 ((wxEvtHandler
&) wxDefaultValidator
).ClearEventLocker();
570 wxClassInfo::CleanUpClasses();
575 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
576 // At this point we want to check if there are any memory
577 // blocks that aren't part of the wxDebugContext itself,
578 // as a special case. Then when dumping we need to ignore
579 // wxDebugContext, too.
580 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
582 wxLogDebug(wxT("There were memory leaks."));
583 wxDebugContext::Dump();
584 wxDebugContext::PrintStatistics();
586 // wxDebugContext::SetStream(NULL, NULL);
590 // do it as the very last thing because everything else can log messages
591 delete wxLog::SetActiveTarget(NULL
);
595 #if !defined(_WINDLL) || (defined(_WINDLL) && defined(WXMAKINGDLL))
597 // temporarily disable this warning which would be generated in release builds
600 #pragma warning(disable: 4715) // not all control paths return a value
603 //// Main wxWindows entry point
604 int wxEntry(WXHINSTANCE hInstance
,
605 WXHINSTANCE
WXUNUSED(hPrevInstance
),
610 // do check for memory leaks on program exit
611 // (another useful flag is _CRTDBG_DELAY_FREE_MEM_DF which doesn't free
612 // deallocated memory which may be used to simulate low-memory condition)
613 wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF
);
615 // take everything into a try-except block in release build
616 // FIXME other compilers must support Win32 SEH (structured exception
617 // handling) too, just find the appropriate keyword in their docs!
618 // Please note that it's _not_ the same as C++ exceptions!
619 #if !defined(__WXDEBUG__) && defined(__VISUALC__)
620 #define CATCH_PROGRAM_EXCEPTIONS
624 #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 wxT("No initializer - use IMPLEMENT_APP macro.") );
640 wxTheApp
= (*wxApp::GetInitializerFunction()) ();
643 wxCHECK_MSG( wxTheApp
, 0, wxT("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();
653 // We really don't want timestamps by default, because it means
654 // we can't simply double-click on the error message and get to that
655 // line in the source. So VC++ at least, let's have a sensible default.
657 wxLog::SetTimestamp(NULL
);
662 if ( wxTheApp
->OnInit() )
666 retValue
= wxTheApp
->OnRun();
669 // We want to initialize, but not run or exit immediately.
672 //else: app initialization failed, so we skipped OnRun()
674 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
677 // Forcibly delete the window.
678 if ( topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
679 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
681 topWindow
->Close(TRUE
);
682 wxTheApp
->DeletePendingObjects();
687 wxTheApp
->SetTopWindow(NULL
);
697 #ifdef CATCH_PROGRAM_EXCEPTIONS
699 __except ( EXCEPTION_EXECUTE_HANDLER
) {
702 wxTheApp->OnFatalException();
705 // using wxLog would be unsafe here
707 _("Unrecoverable program error detected: "
708 " the application will terminate."),
710 MB_APPLMODAL
| MB_ICONSTOP
| MB_OK
);
712 ::ExitProcess(3); // the same exit code as abort()
716 #endif // CATCH_PROGRAM_EXCEPTIONS
719 // restore warning state
721 #pragma warning(default: 4715) // not all control paths return a value
726 //// Entry point for DLLs
728 int wxEntry(WXHINSTANCE hInstance
)
730 wxhInstance
= (HINSTANCE
) hInstance
;
733 // The app may have declared a global application object, but we recommend
734 // the IMPLEMENT_APP macro is used instead, which sets an initializer function
735 // for delayed, dynamic app object construction.
738 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
739 "No initializer - use IMPLEMENT_APP macro." );
741 wxTheApp
= (* wxApp::GetInitializerFunction()) ();
744 wxCHECK_MSG( wxTheApp
, 0, "You have to define an instance of wxApp!" );
747 wxTheApp
->argv
= NULL
;
749 wxTheApp
->OnInitGui();
753 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
754 if ( topWindow
&& topWindow
->GetHWND())
756 topWindow
->Show(TRUE
);
763 //// Static member initialization
765 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
771 m_wantDebugOutput
= TRUE
;
775 m_printMode
= wxPRINT_WINDOWS
;
776 m_exitOnFrameDelete
= TRUE
;
782 // Delete command-line args
784 for (i
= 0; i
< argc
; i
++)
791 bool wxApp::Initialized()
799 #ifdef _WINDLL // Assume initialized if DLL (no way of telling)
805 * Get and process a message, returning FALSE if WM_QUIT
806 * received (and also set the flag telling the app to exit the main loop)
809 bool wxApp::DoMessage()
811 BOOL rc
= ::GetMessage(&s_currentMsg
, (HWND
) NULL
, 0, 0);
821 // should never happen, but let's test for it nevertheless
822 wxLogLastError("GetMessage");
827 wxASSERT_MSG( wxThread::IsMain(),
828 wxT("only the main thread can process Windows messages") );
830 static bool s_hadGuiLock
= TRUE
;
831 static wxMsgArray s_aSavedMessages
;
833 // if a secondary thread owns is doing GUI calls, save all messages for
834 // later processing - we can't process them right now because it will
835 // lead to recursive library calls (and we're not reentrant)
836 if ( !wxGuiOwnedByMainThread() )
838 s_hadGuiLock
= FALSE
;
840 // leave out WM_COMMAND messages: too dangerous, sometimes
841 // the message will be processed twice
842 if ( !wxIsWaitingForThread() ||
843 s_currentMsg
.message
!= WM_COMMAND
)
845 s_aSavedMessages
.Add(s_currentMsg
);
852 // have we just regained the GUI lock? if so, post all of the saved
855 // FIXME of course, it's not _exactly_ the same as processing the
856 // messages normally - expect some things to break...
861 size_t count
= s_aSavedMessages
.Count();
862 for ( size_t n
= 0; n
< count
; n
++ )
864 MSG
& msg
= s_aSavedMessages
[n
];
866 if ( !ProcessMessage((WXMSG
*)&msg
) )
868 ::TranslateMessage(&msg
);
869 ::DispatchMessage(&msg
);
873 s_aSavedMessages
.Empty();
876 #endif // wxUSE_THREADS
878 // Process the message
879 if ( !ProcessMessage((WXMSG
*)&s_currentMsg
) )
881 ::TranslateMessage(&s_currentMsg
);
882 ::DispatchMessage(&s_currentMsg
);
890 * Keep trying to process messages until WM_QUIT
893 * If there are messages to be processed, they will all be
894 * processed and OnIdle will not be called.
895 * When there are no more messages, OnIdle is called.
896 * If OnIdle requests more time,
897 * it will be repeatedly called so long as there are no pending messages.
898 * A 'feature' of this is that once OnIdle has decided that no more processing
899 * is required, then it won't get processing time until further messages
900 * are processed (it'll sit in DoMessage).
903 int wxApp::MainLoop()
907 while ( m_keepGoing
)
910 wxMutexGuiLeaveOrEnter();
911 #endif // wxUSE_THREADS
913 while ( !::PeekMessage(&s_currentMsg
, 0, 0, 0, PM_NOREMOVE
) &&
922 return s_currentMsg
.wParam
;
925 // Returns TRUE if more time is needed.
926 bool wxApp::ProcessIdle()
929 event
.SetEventObject(this);
932 return event
.MoreRequested();
935 void wxApp::ExitMainLoop()
940 bool wxApp::Pending()
942 return (::PeekMessage(&s_currentMsg
, 0, 0, 0, PM_NOREMOVE
) != 0);
945 void wxApp::Dispatch()
951 * Give all windows a chance to preprocess
952 * the message. Some may have accelerator tables, or have
956 bool wxApp::ProcessMessage(WXMSG
*wxmsg
)
958 MSG
*msg
= (MSG
*)wxmsg
;
959 HWND hWnd
= msg
->hwnd
;
960 wxWindow
*wndThis
= wxFindWinFromHandle((WXHWND
)hWnd
), *wnd
;
962 // for some composite controls (like a combobox), wndThis might be NULL
963 // because the subcontrol is not a wxWindow, but only the control itself
964 // is - try to catch this case
965 while ( hWnd
&& !wndThis
)
967 hWnd
= ::GetParent(hWnd
);
968 wndThis
= wxFindWinFromHandle((WXHWND
)hWnd
);
971 // Try translations first; find the youngest window with
972 // a translation table.
973 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
975 if ( wnd
->MSWTranslateMessage(wxmsg
) )
979 // Anyone for a non-translation message? Try youngest descendants first.
980 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
982 if ( wnd
->MSWProcessMessage(wxmsg
) )
989 void wxApp::OnIdle(wxIdleEvent
& event
)
991 static bool s_inOnIdle
= FALSE
;
993 // Avoid recursion (via ProcessEvent default case)
999 // 'Garbage' collection of windows deleted with Close().
1000 DeletePendingObjects();
1003 // flush the logged messages if any
1004 wxLog
*pLog
= wxLog::GetActiveTarget();
1005 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
1009 // Send OnIdle events to all windows
1010 if ( SendIdleEvents() )
1012 // SendIdleEvents() returns TRUE if at least one window requested more
1014 event
.RequestMore(TRUE
);
1017 // If they are pending events, we must process them: pending events are
1018 // either events to the threads other than main or events posted with
1019 // wxPostEvent() functions
1020 ProcessPendingEvents();
1025 // Send idle event to all top-level windows
1026 bool wxApp::SendIdleEvents()
1028 bool needMore
= FALSE
;
1030 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
1033 wxWindow
* win
= node
->GetData();
1034 if (SendIdleEvents(win
))
1036 node
= node
->GetNext();
1042 // Send idle event to window and all subwindows
1043 bool wxApp::SendIdleEvents(wxWindow
* win
)
1045 bool needMore
= FALSE
;
1048 event
.SetEventObject(win
);
1049 win
->GetEventHandler()->ProcessEvent(event
);
1051 if (event
.MoreRequested())
1054 wxNode
* node
= win
->GetChildren().First();
1057 wxWindow
* win
= (wxWindow
*) node
->Data();
1058 if (SendIdleEvents(win
))
1061 node
= node
->Next();
1066 void wxApp::DeletePendingObjects()
1068 wxNode
*node
= wxPendingDelete
.First();
1071 wxObject
*obj
= (wxObject
*)node
->Data();
1075 if (wxPendingDelete
.Member(obj
))
1078 // Deleting one object may have deleted other pending
1079 // objects, so start from beginning of list again.
1080 node
= wxPendingDelete
.First();
1084 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
1087 GetTopWindow()->Close(TRUE
);
1090 // Default behaviour: close the application with prompts. The
1091 // user can veto the close, and therefore the end session.
1092 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
1096 if (!GetTopWindow()->Close(!event
.CanVeto()))
1101 int wxApp::GetComCtl32Version() const
1103 // have we loaded COMCTL32 yet?
1104 HMODULE theModule
= ::GetModuleHandle(wxT("COMCTL32"));
1107 // if so, then we can check for the version
1110 // InitCommonControlsEx is unique to 4.7 and later
1111 FARPROC theProc
= ::GetProcAddress(theModule
, "InitCommonControlsEx");
1114 { // not found, must be 4.00
1119 // The following symbol are unique to 4.71
1121 // FlatSB_EnableScrollBar FlatSB_GetScrollInfo FlatSB_GetScrollPos
1122 // FlatSB_GetScrollProp FlatSB_GetScrollRange FlatSB_SetScrollInfo
1123 // FlatSB_SetScrollPos FlatSB_SetScrollProp FlatSB_SetScrollRange
1124 // FlatSB_ShowScrollBar
1125 // _DrawIndirectImageList _DuplicateImageList
1127 // UninitializeFlatSB
1128 // we could check for any of these - I chose DllInstall
1129 FARPROC theProc
= ::GetProcAddress(theModule
, "DllInstall");
1132 // not found, must be 4.70
1136 { // found, must be 4.71
1146 wxLogError(_("Fatal error: exiting"));
1151 // Yield to incoming messages
1154 // we don't want to process WM_QUIT from here - it should be processed in
1155 // the main event loop in order to stop it
1158 while ( PeekMessage(&msg
, (HWND
)0, 0, 0, PM_NOREMOVE
) &&
1159 msg
.message
!= WM_QUIT
)
1161 if ( !wxTheApp
->DoMessage() )
1165 // If they are pending events, we must process them.
1166 wxTheApp
->ProcessPendingEvents();
1171 //-----------------------------------------------------------------------------
1173 //-----------------------------------------------------------------------------
1177 // Send the top window a dummy message so idle handler processing will
1178 // start up again. Doing it this way ensures that the idle handler
1179 // wakes up in the right thread.
1180 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
1182 HWND hWnd
= (HWND
)topWindow
->GetHWND();
1183 ::PostMessage(hWnd
, WM_NULL
, 0, 0);
1187 //-----------------------------------------------------------------------------
1190 wxApp::GetStdIcon(int which
) const
1194 case wxICON_INFORMATION
:
1195 return wxIcon("wxICON_INFO");
1197 case wxICON_QUESTION
:
1198 return wxIcon("wxICON_QUESTION");
1200 case wxICON_EXCLAMATION
:
1201 return wxIcon("wxICON_WARNING");
1204 wxFAIL_MSG(wxT("requested non existent standard icon"));
1205 // still fall through
1208 return wxIcon("wxICON_ERROR");
1213 HINSTANCE
wxGetInstance()
1218 void wxSetInstance(HINSTANCE hInst
)
1220 wxhInstance
= hInst
;
1223 // For some reason, with MSVC++ 1.5, WinMain isn't linked in properly
1224 // if in a separate file. So include it here to ensure it's linked.
1225 #if (defined(__VISUALC__) && !defined(__WIN32__)) || (defined(__GNUWIN32__) && !defined(__TWIN32__))