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
);
133 // the handle to richedit DLL and the version of the DLL loaded
134 static HINSTANCE gs_hRichEdit
= (HINSTANCE
)NULL
;
135 static int gs_verRichEdit
= -1;
138 // ===========================================================================
140 // ===========================================================================
142 // ---------------------------------------------------------------------------
144 // ---------------------------------------------------------------------------
146 #if !USE_SHARED_LIBRARY
147 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
149 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
150 EVT_IDLE(wxApp::OnIdle
)
151 EVT_END_SESSION(wxApp::OnEndSession
)
152 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession
)
157 bool wxApp::Initialize()
159 // Some people may wish to use this, but
160 // probably it shouldn't be here by default.
162 // wxRedirectIOToConsole();
165 wxBuffer
= new wxChar
[1500]; // FIXME
167 wxClassInfo::InitializeClasses();
170 wxGetResource(wxT("wxWindows"), wxT("OsVersion"), &wxOsVersion
);
174 wxPendingEventsLocker
= new wxCriticalSection
;
177 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
178 wxTheColourDatabase
->Initialize();
180 wxInitializeStockLists();
181 wxInitializeStockObjects();
183 #if wxUSE_WX_RESOURCES
184 wxInitializeResourceSystem();
187 wxBitmap::InitStandardHandlers();
189 #if defined(__WIN95__)
190 InitCommonControls();
197 // for OLE, enlarge message queue to be as large as possible
199 while (!SetMessageQueue(iMsg
) && (iMsg
-= 8))
202 // we need to initialize OLE library
203 if ( FAILED(::OleInitialize(NULL
)) )
204 wxLogError(_("Cannot initialize OLE"));
208 if (!Ctl3dRegister(wxhInstance
))
209 wxLogError(wxT("Cannot register CTL3D"));
211 Ctl3dAutoSubclass(wxhInstance
);
214 g_globalCursor
= new wxCursor
;
216 // VZ: these icons are not in wx.rc anyhow (but should they?)!
218 wxSTD_FRAME_ICON
= LoadIcon(wxhInstance
, wxT("wxSTD_FRAME"));
219 wxSTD_MDIPARENTFRAME_ICON
= LoadIcon(wxhInstance
, wxT("wxSTD_MDIPARENTFRAME"));
220 wxSTD_MDICHILDFRAME_ICON
= LoadIcon(wxhInstance
, wxT("wxSTD_MDICHILDFRAME"));
222 wxDEFAULT_FRAME_ICON
= LoadIcon(wxhInstance
, wxT("wxDEFAULT_FRAME"));
223 wxDEFAULT_MDIPARENTFRAME_ICON
= LoadIcon(wxhInstance
, wxT("wxDEFAULT_MDIPARENTFRAME"));
224 wxDEFAULT_MDICHILDFRAME_ICON
= LoadIcon(wxhInstance
, wxT("wxDEFAULT_MDICHILDFRAME"));
227 RegisterWindowClasses();
229 // Create the brush for disabling bitmap buttons
232 lb
.lbStyle
= BS_PATTERN
;
233 lb
.lbHatch
= (int)LoadBitmap( wxhInstance
, wxT("wxDISABLE_BUTTON_BITMAP") );
236 wxDisableButtonBrush
= ::CreateBrushIndirect( & lb
);
237 ::DeleteObject( (HGDIOBJ
)lb
.lbHatch
);
239 //else: wxWindows resources are probably not linked in
245 wxWinHandleList
= new wxList(wxKEY_INTEGER
);
247 // This is to foil optimizations in Visual C++ that throw out dummy.obj.
248 // PLEASE DO NOT ALTER THIS.
249 #if defined(__VISUALC__) && !defined(WXMAKINGDLL)
250 extern char wxDummyChar
;
251 if (wxDummyChar
) wxDummyChar
++;
254 wxSetKeyboardHook(TRUE
);
256 wxModule::RegisterModules();
257 if (!wxModule::InitializeModules())
262 // ---------------------------------------------------------------------------
263 // RegisterWindowClasses
264 // ---------------------------------------------------------------------------
266 // TODO we should only register classes really used by the app. For this it
267 // would be enough to just delay the class registration until an attempt
268 // to create a window of this class is made.
269 bool wxApp::RegisterWindowClasses()
273 // for each class we register one with CS_(V|H)REDRAW style and one
274 // without for windows created with wxNO_FULL_REDRAW_ON_REPAINT flag
275 static const long styleNormal
= CS_HREDRAW
| CS_VREDRAW
| CS_DBLCLKS
;
276 static const long styleNoRedraw
= CS_DBLCLKS
;
278 // the fields which are common to all classes
279 wndclass
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
280 wndclass
.cbClsExtra
= 0;
281 wndclass
.cbWndExtra
= sizeof( DWORD
); // VZ: what is this DWORD used for?
282 wndclass
.hInstance
= wxhInstance
;
283 wndclass
.hIcon
= (HICON
) NULL
;
284 wndclass
.hCursor
= ::LoadCursor((HINSTANCE
)NULL
, IDC_ARROW
);
285 wndclass
.lpszMenuName
= NULL
;
287 // Register the frame window class.
288 wndclass
.hbrBackground
= (HBRUSH
)(COLOR_APPWORKSPACE
+ 1);
289 wndclass
.lpszClassName
= wxFrameClassName
;
290 wndclass
.style
= styleNormal
;
292 if ( !RegisterClass(&wndclass
) )
294 wxLogLastError("RegisterClass(frame)");
300 wndclass
.lpszClassName
= wxFrameClassNameNoRedraw
;
301 wndclass
.style
= styleNoRedraw
;
303 if ( !RegisterClass(&wndclass
) )
305 wxLogLastError("RegisterClass(no redraw frame)");
310 // Register the MDI frame window class.
311 wndclass
.hbrBackground
= (HBRUSH
)NULL
; // paint MDI frame ourselves
312 wndclass
.lpszClassName
= wxMDIFrameClassName
;
313 wndclass
.style
= styleNormal
;
315 if ( !RegisterClass(&wndclass
) )
317 wxLogLastError("RegisterClass(MDI parent)");
322 // "no redraw" MDI frame
323 wndclass
.lpszClassName
= wxMDIFrameClassNameNoRedraw
;
324 wndclass
.style
= styleNoRedraw
;
326 if ( !RegisterClass(&wndclass
) )
328 wxLogLastError("RegisterClass(no redraw MDI parent frame)");
333 // Register the MDI child frame window class.
334 wndclass
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+ 1);
335 wndclass
.lpszClassName
= wxMDIChildFrameClassName
;
336 wndclass
.style
= styleNormal
;
338 if ( !RegisterClass(&wndclass
) )
340 wxLogLastError("RegisterClass(MDI child)");
345 // "no redraw" MDI child frame
346 wndclass
.lpszClassName
= wxMDIChildFrameClassNameNoRedraw
;
347 wndclass
.style
= styleNoRedraw
;
349 if ( !RegisterClass(&wndclass
) )
351 wxLogLastError("RegisterClass(no redraw MDI child)");
356 // Register the panel window class.
357 wndclass
.hbrBackground
= (HBRUSH
) GetStockObject( LTGRAY_BRUSH
);
358 wndclass
.lpszClassName
= wxPanelClassName
;
359 wndclass
.style
= styleNormal
;
361 if ( !RegisterClass(&wndclass
) )
363 wxLogLastError("RegisterClass(panel)");
368 // Register the canvas and textsubwindow class name
369 wndclass
.hbrBackground
= (HBRUSH
)NULL
;
370 wndclass
.lpszClassName
= wxCanvasClassName
;
372 if ( !RegisterClass(&wndclass
) )
374 wxLogLastError("RegisterClass(canvas)");
382 // ---------------------------------------------------------------------------
383 // Convert Windows to argc, argv style
384 // ---------------------------------------------------------------------------
386 void wxApp::ConvertToStandardCommandArgs(char* lpCmdLine
)
390 wxString
cmdLine(lpCmdLine
);
393 // Get application name
394 wxChar name
[260]; // 260 is MAX_PATH value from windef.h
395 ::GetModuleFileName(wxhInstance
, name
, WXSIZEOF(name
));
400 wxStrcpy(name
, wxFileNameFromPath(name
));
401 wxStripExtension(name
);
402 wxTheApp
->SetAppName(name
);
405 // Treat strings enclosed in double-quotes as single arguments
407 int len
= cmdLine
.Length();
411 while ((i
< len
) && wxIsspace(cmdLine
.GetChar(i
)))
416 if (cmdLine
.GetChar(i
) == wxT('"')) // We found the start of a string
420 while ((i
< len
) && (cmdLine
.GetChar(i
) != wxT('"')))
423 wxString
arg(cmdLine
.Mid(first
, (i
- first
)));
429 i
++; // Skip past 2nd quote
431 else // Unquoted argument
434 while ((i
< len
) && !wxIsspace(cmdLine
.GetChar(i
)))
437 wxString
arg(cmdLine
.Mid(first
, (i
- first
)));
445 wxTheApp
->argv
= new wxChar
*[count
+ 1];
446 for (i
= 0; i
< count
; i
++)
448 wxString
arg(args
[i
]);
449 wxTheApp
->argv
[i
] = copystring((const wxChar
*)arg
);
451 wxTheApp
->argv
[count
] = NULL
; // argv[] is a NULL-terminated list
452 wxTheApp
->argc
= count
;
455 //// Cleans up any wxWindows internal structures left lying around
457 void wxApp::CleanUp()
462 // flush the logged messages if any and install a 'safer' log target: the
463 // default one (wxLogGui) can't be used after the resources are freed just
464 // below and the user suppliedo ne might be even more unsafe (using any
465 // wxWindows GUI function is unsafe starting from now)
466 wxLog::DontCreateOnDemand();
468 // this will flush the old messages if any
469 delete wxLog::SetActiveTarget(new wxLogStderr
);
472 // One last chance for pending objects to be cleaned up
473 wxTheApp
->DeletePendingObjects();
475 wxModule::CleanUpModules();
477 #if wxUSE_WX_RESOURCES
478 wxCleanUpResourceSystem();
480 // wxDefaultResourceTable->ClearTable();
483 // Indicate that the cursor can be freed, so that cursor won't be deleted
484 // by deleting the bitmap list before g_globalCursor goes out of scope
485 // (double deletion of the cursor).
486 wxSetCursor(wxNullCursor
);
487 delete g_globalCursor
;
488 g_globalCursor
= NULL
;
490 wxDeleteStockObjects();
492 // Destroy all GDI lists, etc.
493 wxDeleteStockLists();
495 delete wxTheColourDatabase
;
496 wxTheColourDatabase
= NULL
;
498 wxBitmap::CleanUpHandlers();
503 //// WINDOWS-SPECIFIC CLEANUP
505 wxSetKeyboardHook(FALSE
);
508 if (gs_hRichEdit
!= (HINSTANCE
) NULL
)
509 FreeLibrary(gs_hRichEdit
);
516 if (wxSTD_FRAME_ICON
)
517 DestroyIcon(wxSTD_FRAME_ICON
);
518 if (wxSTD_MDICHILDFRAME_ICON
)
519 DestroyIcon(wxSTD_MDICHILDFRAME_ICON
);
520 if (wxSTD_MDIPARENTFRAME_ICON
)
521 DestroyIcon(wxSTD_MDIPARENTFRAME_ICON
);
523 if (wxDEFAULT_FRAME_ICON
)
524 DestroyIcon(wxDEFAULT_FRAME_ICON
);
525 if (wxDEFAULT_MDICHILDFRAME_ICON
)
526 DestroyIcon(wxDEFAULT_MDICHILDFRAME_ICON
);
527 if (wxDEFAULT_MDIPARENTFRAME_ICON
)
528 DestroyIcon(wxDEFAULT_MDIPARENTFRAME_ICON
);
530 if ( wxDisableButtonBrush
)
531 ::DeleteObject( wxDisableButtonBrush
);
538 Ctl3dUnregister(wxhInstance
);
542 delete wxWinHandleList
;
544 // GL: I'm annoyed ... I don't know where to put this and I don't want to
545 // create a module for that as it's part of the core.
546 delete wxPendingEvents
;
548 delete wxPendingEventsLocker
;
549 // If we don't do the following, we get an apparent memory leak.
550 ((wxEvtHandler
&) wxDefaultValidator
).ClearEventLocker();
553 wxClassInfo::CleanUpClasses();
558 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
559 // At this point we want to check if there are any memory
560 // blocks that aren't part of the wxDebugContext itself,
561 // as a special case. Then when dumping we need to ignore
562 // wxDebugContext, too.
563 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
565 wxLogDebug(wxT("There were memory leaks."));
566 wxDebugContext::Dump();
567 wxDebugContext::PrintStatistics();
569 // wxDebugContext::SetStream(NULL, NULL);
573 // do it as the very last thing because everything else can log messages
574 delete wxLog::SetActiveTarget(NULL
);
578 #if !defined(_WINDLL) || (defined(_WINDLL) && defined(WXMAKINGDLL))
580 // temporarily disable this warning which would be generated in release builds
583 #pragma warning(disable: 4715) // not all control paths return a value
586 //// Main wxWindows entry point
587 int wxEntry(WXHINSTANCE hInstance
,
588 WXHINSTANCE
WXUNUSED(hPrevInstance
),
593 // do check for memory leaks on program exit
594 // (another useful flag is _CRTDBG_DELAY_FREE_MEM_DF which doesn't free
595 // deallocated memory which may be used to simulate low-memory condition)
596 wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF
);
598 // take everything into a try-except block in release build
599 // FIXME other compilers must support Win32 SEH (structured exception
600 // handling) too, just find the appropriate keyword in their docs!
601 // Please note that it's _not_ the same as C++ exceptions!
602 #if !defined(__WXDEBUG__) && defined(__VISUALC__)
603 #define CATCH_PROGRAM_EXCEPTIONS
607 #undef CATCH_PROGRAM_EXCEPTIONS
609 wxhInstance
= (HINSTANCE
) hInstance
;
611 if (!wxApp::Initialize())
614 // create the application object or ensure that one already exists
617 // The app may have declared a global application object, but we recommend
618 // the IMPLEMENT_APP macro is used instead, which sets an initializer
619 // function for delayed, dynamic app object construction.
620 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
621 wxT("No initializer - use IMPLEMENT_APP macro.") );
623 wxTheApp
= (*wxApp::GetInitializerFunction()) ();
626 wxCHECK_MSG( wxTheApp
, 0, wxT("You have to define an instance of wxApp!") );
628 // save the WinMain() parameters
629 wxTheApp
->ConvertToStandardCommandArgs(lpCmdLine
);
630 wxTheApp
->m_nCmdShow
= nCmdShow
;
632 // GUI-specific initialisation. In fact on Windows we don't have any,
633 // but this call is provided for compatibility across platforms.
634 wxTheApp
->OnInitGui();
636 // We really don't want timestamps by default, because it means
637 // we can't simply double-click on the error message and get to that
638 // line in the source. So VC++ at least, let's have a sensible default.
640 wxLog::SetTimestamp(NULL
);
645 if ( wxTheApp
->OnInit() )
649 retValue
= wxTheApp
->OnRun();
652 // We want to initialize, but not run or exit immediately.
655 //else: app initialization failed, so we skipped OnRun()
657 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
660 // Forcibly delete the window.
661 if ( topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
662 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
664 topWindow
->Close(TRUE
);
665 wxTheApp
->DeletePendingObjects();
670 wxTheApp
->SetTopWindow(NULL
);
680 #ifdef CATCH_PROGRAM_EXCEPTIONS
682 __except ( EXCEPTION_EXECUTE_HANDLER
) {
685 wxTheApp->OnFatalException();
688 // using wxLog would be unsafe here
690 _("Unrecoverable program error detected: "
691 " the application will terminate."),
693 MB_APPLMODAL
| MB_ICONSTOP
| MB_OK
);
695 ::ExitProcess(3); // the same exit code as abort()
699 #endif // CATCH_PROGRAM_EXCEPTIONS
702 // restore warning state
704 #pragma warning(default: 4715) // not all control paths return a value
709 //// Entry point for DLLs
711 int wxEntry(WXHINSTANCE hInstance
)
713 wxhInstance
= (HINSTANCE
) hInstance
;
716 // The app may have declared a global application object, but we recommend
717 // the IMPLEMENT_APP macro is used instead, which sets an initializer function
718 // for delayed, dynamic app object construction.
721 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
722 "No initializer - use IMPLEMENT_APP macro." );
724 wxTheApp
= (* wxApp::GetInitializerFunction()) ();
727 wxCHECK_MSG( wxTheApp
, 0, "You have to define an instance of wxApp!" );
730 wxTheApp
->argv
= NULL
;
732 wxTheApp
->OnInitGui();
736 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
737 if ( topWindow
&& topWindow
->GetHWND())
739 topWindow
->Show(TRUE
);
746 //// Static member initialization
748 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
754 m_wantDebugOutput
= TRUE
;
758 m_printMode
= wxPRINT_WINDOWS
;
759 m_exitOnFrameDelete
= TRUE
;
765 // Delete command-line args
767 for (i
= 0; i
< argc
; i
++)
774 bool wxApp::Initialized()
782 #ifdef _WINDLL // Assume initialized if DLL (no way of telling)
788 * Get and process a message, returning FALSE if WM_QUIT
789 * received (and also set the flag telling the app to exit the main loop)
792 bool wxApp::DoMessage()
794 BOOL rc
= ::GetMessage(&s_currentMsg
, (HWND
) NULL
, 0, 0);
804 // should never happen, but let's test for it nevertheless
805 wxLogLastError("GetMessage");
810 wxASSERT_MSG( wxThread::IsMain(),
811 wxT("only the main thread can process Windows messages") );
813 static bool s_hadGuiLock
= TRUE
;
814 static wxMsgArray s_aSavedMessages
;
816 // if a secondary thread owns is doing GUI calls, save all messages for
817 // later processing - we can't process them right now because it will
818 // lead to recursive library calls (and we're not reentrant)
819 if ( !wxGuiOwnedByMainThread() )
821 s_hadGuiLock
= FALSE
;
823 // leave out WM_COMMAND messages: too dangerous, sometimes
824 // the message will be processed twice
825 if ( !wxIsWaitingForThread() ||
826 s_currentMsg
.message
!= WM_COMMAND
)
828 s_aSavedMessages
.Add(s_currentMsg
);
835 // have we just regained the GUI lock? if so, post all of the saved
838 // FIXME of course, it's not _exactly_ the same as processing the
839 // messages normally - expect some things to break...
844 size_t count
= s_aSavedMessages
.Count();
845 for ( size_t n
= 0; n
< count
; n
++ )
847 MSG
& msg
= s_aSavedMessages
[n
];
849 if ( !ProcessMessage((WXMSG
*)&msg
) )
851 ::TranslateMessage(&msg
);
852 ::DispatchMessage(&msg
);
856 s_aSavedMessages
.Empty();
859 #endif // wxUSE_THREADS
861 // Process the message
862 if ( !ProcessMessage((WXMSG
*)&s_currentMsg
) )
864 ::TranslateMessage(&s_currentMsg
);
865 ::DispatchMessage(&s_currentMsg
);
873 * Keep trying to process messages until WM_QUIT
876 * If there are messages to be processed, they will all be
877 * processed and OnIdle will not be called.
878 * When there are no more messages, OnIdle is called.
879 * If OnIdle requests more time,
880 * it will be repeatedly called so long as there are no pending messages.
881 * A 'feature' of this is that once OnIdle has decided that no more processing
882 * is required, then it won't get processing time until further messages
883 * are processed (it'll sit in DoMessage).
886 int wxApp::MainLoop()
890 while ( m_keepGoing
)
893 wxMutexGuiLeaveOrEnter();
894 #endif // wxUSE_THREADS
896 while ( !::PeekMessage(&s_currentMsg
, 0, 0, 0, PM_NOREMOVE
) &&
905 return s_currentMsg
.wParam
;
908 // Returns TRUE if more time is needed.
909 bool wxApp::ProcessIdle()
912 event
.SetEventObject(this);
915 return event
.MoreRequested();
918 void wxApp::ExitMainLoop()
923 bool wxApp::Pending()
925 return (::PeekMessage(&s_currentMsg
, 0, 0, 0, PM_NOREMOVE
) != 0);
928 void wxApp::Dispatch()
934 * Give all windows a chance to preprocess
935 * the message. Some may have accelerator tables, or have
939 bool wxApp::ProcessMessage(WXMSG
*wxmsg
)
941 MSG
*msg
= (MSG
*)wxmsg
;
942 HWND hWnd
= msg
->hwnd
;
943 wxWindow
*wndThis
= wxFindWinFromHandle((WXHWND
)hWnd
), *wnd
;
945 // for some composite controls (like a combobox), wndThis might be NULL
946 // because the subcontrol is not a wxWindow, but only the control itself
947 // is - try to catch this case
948 while ( hWnd
&& !wndThis
)
950 hWnd
= ::GetParent(hWnd
);
951 wndThis
= wxFindWinFromHandle((WXHWND
)hWnd
);
954 // Try translations first; find the youngest window with
955 // a translation table.
956 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
958 if ( wnd
->MSWTranslateMessage(wxmsg
) )
962 // Anyone for a non-translation message? Try youngest descendants first.
963 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
965 if ( wnd
->MSWProcessMessage(wxmsg
) )
972 void wxApp::OnIdle(wxIdleEvent
& event
)
974 static bool s_inOnIdle
= FALSE
;
976 // Avoid recursion (via ProcessEvent default case)
982 // 'Garbage' collection of windows deleted with Close().
983 DeletePendingObjects();
986 // flush the logged messages if any
987 wxLog
*pLog
= wxLog::GetActiveTarget();
988 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
992 // Send OnIdle events to all windows
993 if ( SendIdleEvents() )
995 // SendIdleEvents() returns TRUE if at least one window requested more
997 event
.RequestMore(TRUE
);
1000 // If they are pending events, we must process them: pending events are
1001 // either events to the threads other than main or events posted with
1002 // wxPostEvent() functions
1003 ProcessPendingEvents();
1008 // Send idle event to all top-level windows
1009 bool wxApp::SendIdleEvents()
1011 bool needMore
= FALSE
;
1013 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
1016 wxWindow
* win
= node
->GetData();
1017 if (SendIdleEvents(win
))
1019 node
= node
->GetNext();
1025 // Send idle event to window and all subwindows
1026 bool wxApp::SendIdleEvents(wxWindow
* win
)
1028 bool needMore
= FALSE
;
1031 event
.SetEventObject(win
);
1032 win
->GetEventHandler()->ProcessEvent(event
);
1034 if (event
.MoreRequested())
1037 wxNode
* node
= win
->GetChildren().First();
1040 wxWindow
* win
= (wxWindow
*) node
->Data();
1041 if (SendIdleEvents(win
))
1044 node
= node
->Next();
1049 void wxApp::DeletePendingObjects()
1051 wxNode
*node
= wxPendingDelete
.First();
1054 wxObject
*obj
= (wxObject
*)node
->Data();
1058 if (wxPendingDelete
.Member(obj
))
1061 // Deleting one object may have deleted other pending
1062 // objects, so start from beginning of list again.
1063 node
= wxPendingDelete
.First();
1067 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
1070 GetTopWindow()->Close(TRUE
);
1073 // Default behaviour: close the application with prompts. The
1074 // user can veto the close, and therefore the end session.
1075 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
1079 if (!GetTopWindow()->Close(!event
.CanVeto()))
1087 bool wxApp::InitRichEdit(int version
)
1089 wxCHECK_MSG( version
>= 1 && version
<= 3, FALSE
,
1090 _T("incorrect richedit control version requested") );
1092 if ( version
<= gs_verRichEdit
)
1094 // we've already got this or better
1100 ::FreeLibrary(gs_hRichEdit
);
1103 // always try load riched20.dll first - like this we won't have to reload
1104 // it later if we're first asked for RE 1 and then for RE 2 or 3
1105 wxString dllname
= _T("riched20.dll");
1106 gs_hRichEdit
= ::LoadLibrary(dllname
);
1107 if ( !gs_hRichEdit
&& (version
== 1) )
1109 // fall back to RE 1
1110 dllname
= _T("riched32.dll");
1111 gs_hRichEdit
= ::LoadLibrary(dllname
);
1114 if ( !gs_hRichEdit
)
1116 wxLogSysError(_("Could not load Rich Edit DLL '%s'"), dllname
.c_str());
1118 gs_verRichEdit
= -1;
1123 gs_verRichEdit
= version
;
1128 #endif // wxUSE_RICHEDIT
1131 int wxApp::GetComCtl32Version()
1133 // TODO should use DllGetVersion() instead of this hack
1136 static int s_verComCtl32
= -1; // MT-FIXME
1138 if ( s_verComCtl32
== -1 )
1142 // have we loaded COMCTL32 yet?
1143 HMODULE theModule
= ::GetModuleHandle(wxT("COMCTL32"));
1145 // if so, then we can check for the version
1148 // InitCommonControlsEx is unique to 4.7 and later
1149 FARPROC theProc
= ::GetProcAddress(theModule
,
1150 _T("InitCommonControlsEx"));
1153 { // not found, must be 4.00
1154 s_verComCtl32
= 400;
1158 // The following symbol are unique to 4.71
1160 // FlatSB_EnableScrollBar FlatSB_GetScrollInfo FlatSB_GetScrollPos
1161 // FlatSB_GetScrollProp FlatSB_GetScrollRange FlatSB_SetScrollInfo
1162 // FlatSB_SetScrollPos FlatSB_SetScrollProp FlatSB_SetScrollRange
1163 // FlatSB_ShowScrollBar
1164 // _DrawIndirectImageList _DuplicateImageList
1166 // UninitializeFlatSB
1167 // we could check for any of these - I chose DllInstall
1168 FARPROC theProc
= ::GetProcAddress(theModule
, _T("DllInstall"));
1171 // not found, must be 4.70
1172 s_verComCtl32
= 470;
1175 { // found, must be 4.71
1176 s_verComCtl32
= 471;
1182 return s_verComCtl32
;
1187 wxLogError(_("Fatal error: exiting"));
1192 // Yield to incoming messages
1195 // we don't want to process WM_QUIT from here - it should be processed in
1196 // the main event loop in order to stop it
1199 while ( PeekMessage(&msg
, (HWND
)0, 0, 0, PM_NOREMOVE
) &&
1200 msg
.message
!= WM_QUIT
)
1202 if ( !wxTheApp
->DoMessage() )
1206 // If they are pending events, we must process them.
1207 wxTheApp
->ProcessPendingEvents();
1212 //-----------------------------------------------------------------------------
1214 //-----------------------------------------------------------------------------
1218 // Send the top window a dummy message so idle handler processing will
1219 // start up again. Doing it this way ensures that the idle handler
1220 // wakes up in the right thread.
1221 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
1223 HWND hWnd
= (HWND
)topWindow
->GetHWND();
1224 ::PostMessage(hWnd
, WM_NULL
, 0, 0);
1228 //-----------------------------------------------------------------------------
1231 wxApp::GetStdIcon(int which
) const
1235 case wxICON_INFORMATION
:
1236 return wxIcon("wxICON_INFO");
1238 case wxICON_QUESTION
:
1239 return wxIcon("wxICON_QUESTION");
1241 case wxICON_EXCLAMATION
:
1242 return wxIcon("wxICON_WARNING");
1245 wxFAIL_MSG(wxT("requested non existent standard icon"));
1246 // still fall through
1249 return wxIcon("wxICON_ERROR");
1254 HINSTANCE
wxGetInstance()
1259 void wxSetInstance(HINSTANCE hInst
)
1261 wxhInstance
= hInst
;
1264 // For some reason, with MSVC++ 1.5, WinMain isn't linked in properly
1265 // if in a separate file. So include it here to ensure it's linked.
1266 #if (defined(__VISUALC__) && !defined(__WIN32__)) || (defined(__GNUWIN32__) && !defined(__TWIN32__))