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"
71 #include "wx/tooltip.h"
72 #endif // wxUSE_TOOLTIPS
74 // OLE is used for drag-and-drop, clipboard, OLE Automation...
75 #ifndef wxUSE_NORLANDER_HEADERS
76 #if defined(__GNUWIN32__) || defined(__SC__) || defined(__SALFORDC__)
80 #endif // broken compilers
90 #if (defined(__WIN95__) && !defined(__GNUWIN32__)) || defined(__TWIN32__) || defined(wxUSE_NORLANDER_HEADERS)
94 #include "wx/msw/msvcrt.h"
96 // ----------------------------------------------------------------------------
97 // conditional compilation
98 // ----------------------------------------------------------------------------
100 // The macro _WIN32_IE is defined by commctrl.h (unless it had already been
101 // defined before) and shows us what common control features are available
102 // during the compile time (it doesn't mean that they will be available during
103 // the run-time, use GetComCtl32Version() to test for them!). The possible
106 // 0x0200 for comctl32.dll 4.00 shipped with Win95/NT 4.0
107 // 0x0300 4.70 IE 3.x
108 // 0x0400 4.71 IE 4.0
109 // 0x0401 4.72 IE 4.01 and Win98
110 // 0x0500 5.00 IE 5.x and NT 5.0 (Win2000)
113 // minimal set of features by default
114 #define _WIN32_IE 0x0200
117 #if _WIN32_IE >= 0x0300
121 // ---------------------------------------------------------------------------
123 // ---------------------------------------------------------------------------
125 extern wxChar
*wxBuffer
;
126 extern wxChar
*wxOsVersion
;
127 extern wxList
*wxWinHandleList
;
128 extern wxList WXDLLEXPORT wxPendingDelete
;
129 extern void wxSetKeyboardHook(bool doIt
);
130 extern wxCursor
*g_globalCursor
;
133 wxApp
*wxTheApp
= NULL
;
135 // NB: all "NoRedraw" classes must have the same names as the "normal" classes
136 // with NR suffix - wxWindow::MSWCreate() supposes this
137 const wxChar
*wxFrameClassName
= wxT("wxFrameClass");
138 const wxChar
*wxFrameClassNameNoRedraw
= wxT("wxFrameClassNR");
139 const wxChar
*wxMDIFrameClassName
= wxT("wxMDIFrameClass");
140 const wxChar
*wxMDIFrameClassNameNoRedraw
= wxT("wxMDIFrameClassNR");
141 const wxChar
*wxMDIChildFrameClassName
= wxT("wxMDIChildFrameClass");
142 const wxChar
*wxMDIChildFrameClassNameNoRedraw
= wxT("wxMDIChildFrameClassNR");
143 const wxChar
*wxPanelClassName
= wxT("wxPanelClass");
144 const wxChar
*wxCanvasClassName
= wxT("wxCanvasClass");
146 HICON wxSTD_FRAME_ICON
= (HICON
) NULL
;
147 HICON wxSTD_MDICHILDFRAME_ICON
= (HICON
) NULL
;
148 HICON wxSTD_MDIPARENTFRAME_ICON
= (HICON
) NULL
;
150 HICON wxDEFAULT_FRAME_ICON
= (HICON
) NULL
;
151 HICON wxDEFAULT_MDICHILDFRAME_ICON
= (HICON
) NULL
;
152 HICON wxDEFAULT_MDIPARENTFRAME_ICON
= (HICON
) NULL
;
154 HBRUSH wxDisableButtonBrush
= (HBRUSH
) 0;
156 LRESULT WXDLLEXPORT APIENTRY
wxWndProc(HWND
, UINT
, WPARAM
, LPARAM
);
158 // ===========================================================================
160 // ===========================================================================
162 // ---------------------------------------------------------------------------
164 // ---------------------------------------------------------------------------
166 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
168 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
169 EVT_IDLE(wxApp::OnIdle
)
170 EVT_END_SESSION(wxApp::OnEndSession
)
171 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession
)
175 bool wxApp::Initialize()
177 // Some people may wish to use this, but
178 // probably it shouldn't be here by default.
180 // wxRedirectIOToConsole();
183 wxBuffer
= new wxChar
[1500]; // FIXME
185 wxClassInfo::InitializeClasses();
188 wxGetResource(wxT("wxWindows"), wxT("OsVersion"), &wxOsVersion
);
192 wxPendingEventsLocker
= new wxCriticalSection
;
195 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
196 wxTheColourDatabase
->Initialize();
198 wxInitializeStockLists();
199 wxInitializeStockObjects();
201 #if wxUSE_WX_RESOURCES
202 wxInitializeResourceSystem();
205 wxBitmap::InitStandardHandlers();
207 #if defined(__WIN95__)
208 InitCommonControls();
215 // for OLE, enlarge message queue to be as large as possible
217 while (!SetMessageQueue(iMsg
) && (iMsg
-= 8))
220 // we need to initialize OLE library
221 if ( FAILED(::OleInitialize(NULL
)) )
222 wxLogError(_("Cannot initialize OLE"));
226 if (!Ctl3dRegister(wxhInstance
))
227 wxLogError(wxT("Cannot register CTL3D"));
229 Ctl3dAutoSubclass(wxhInstance
);
232 g_globalCursor
= new wxCursor
;
234 // VZ: these icons are not in wx.rc anyhow (but should they?)!
236 wxSTD_FRAME_ICON
= LoadIcon(wxhInstance
, wxT("wxSTD_FRAME"));
237 wxSTD_MDIPARENTFRAME_ICON
= LoadIcon(wxhInstance
, wxT("wxSTD_MDIPARENTFRAME"));
238 wxSTD_MDICHILDFRAME_ICON
= LoadIcon(wxhInstance
, wxT("wxSTD_MDICHILDFRAME"));
240 wxDEFAULT_FRAME_ICON
= LoadIcon(wxhInstance
, wxT("wxDEFAULT_FRAME"));
241 wxDEFAULT_MDIPARENTFRAME_ICON
= LoadIcon(wxhInstance
, wxT("wxDEFAULT_MDIPARENTFRAME"));
242 wxDEFAULT_MDICHILDFRAME_ICON
= LoadIcon(wxhInstance
, wxT("wxDEFAULT_MDICHILDFRAME"));
245 RegisterWindowClasses();
247 // Create the brush for disabling bitmap buttons
250 lb
.lbStyle
= BS_PATTERN
;
251 lb
.lbHatch
= (int)LoadBitmap( wxhInstance
, wxT("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 // TODO we should only register classes really used by the app. For this it
285 // would be enough to just delay the class registration until an attempt
286 // to create a window of this class is made.
287 bool wxApp::RegisterWindowClasses()
291 // for each class we register one with CS_(V|H)REDRAW style and one
292 // without for windows created with wxNO_FULL_REDRAW_ON_REPAINT flag
293 static const long styleNormal
= CS_HREDRAW
| CS_VREDRAW
| CS_DBLCLKS
;
294 static const long styleNoRedraw
= CS_DBLCLKS
;
296 // the fields which are common to all classes
297 wndclass
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
298 wndclass
.cbClsExtra
= 0;
299 wndclass
.cbWndExtra
= sizeof( DWORD
); // VZ: what is this DWORD used for?
300 wndclass
.hInstance
= wxhInstance
;
301 wndclass
.hIcon
= (HICON
) NULL
;
302 wndclass
.hCursor
= ::LoadCursor((HINSTANCE
)NULL
, IDC_ARROW
);
303 wndclass
.lpszMenuName
= NULL
;
305 // Register the frame window class.
306 wndclass
.hbrBackground
= (HBRUSH
)(COLOR_APPWORKSPACE
+ 1);
307 wndclass
.lpszClassName
= wxFrameClassName
;
308 wndclass
.style
= styleNormal
;
310 if ( !RegisterClass(&wndclass
) )
312 wxLogLastError("RegisterClass(frame)");
318 wndclass
.lpszClassName
= wxFrameClassNameNoRedraw
;
319 wndclass
.style
= styleNoRedraw
;
321 if ( !RegisterClass(&wndclass
) )
323 wxLogLastError("RegisterClass(no redraw frame)");
328 // Register the MDI frame window class.
329 wndclass
.hbrBackground
= (HBRUSH
)NULL
; // paint MDI frame ourselves
330 wndclass
.lpszClassName
= wxMDIFrameClassName
;
331 wndclass
.style
= styleNormal
;
333 if ( !RegisterClass(&wndclass
) )
335 wxLogLastError("RegisterClass(MDI parent)");
340 // "no redraw" MDI frame
341 wndclass
.lpszClassName
= wxMDIFrameClassNameNoRedraw
;
342 wndclass
.style
= styleNoRedraw
;
344 if ( !RegisterClass(&wndclass
) )
346 wxLogLastError("RegisterClass(no redraw MDI parent frame)");
351 // Register the MDI child frame window class.
352 wndclass
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+ 1);
353 wndclass
.lpszClassName
= wxMDIChildFrameClassName
;
354 wndclass
.style
= styleNormal
;
356 if ( !RegisterClass(&wndclass
) )
358 wxLogLastError("RegisterClass(MDI child)");
363 // "no redraw" MDI child frame
364 wndclass
.lpszClassName
= wxMDIChildFrameClassNameNoRedraw
;
365 wndclass
.style
= styleNoRedraw
;
367 if ( !RegisterClass(&wndclass
) )
369 wxLogLastError("RegisterClass(no redraw MDI child)");
374 // Register the panel window class.
375 wndclass
.hbrBackground
= (HBRUSH
) GetStockObject( LTGRAY_BRUSH
);
376 wndclass
.lpszClassName
= wxPanelClassName
;
377 wndclass
.style
= styleNormal
;
379 if ( !RegisterClass(&wndclass
) )
381 wxLogLastError("RegisterClass(panel)");
386 // Register the canvas and textsubwindow class name
387 wndclass
.hbrBackground
= (HBRUSH
)NULL
;
388 wndclass
.lpszClassName
= wxCanvasClassName
;
390 if ( !RegisterClass(&wndclass
) )
392 wxLogLastError("RegisterClass(canvas)");
400 // ---------------------------------------------------------------------------
401 // Convert Windows to argc, argv style
402 // ---------------------------------------------------------------------------
404 void wxApp::ConvertToStandardCommandArgs(char* lpCmdLine
)
408 wxString
cmdLine(lpCmdLine
);
411 // Get application name
412 wxChar name
[260]; // 260 is MAX_PATH value from windef.h
413 ::GetModuleFileName(wxhInstance
, name
, WXSIZEOF(name
));
418 wxStrcpy(name
, wxFileNameFromPath(name
));
419 wxStripExtension(name
);
420 wxTheApp
->SetAppName(name
);
423 // Treat strings enclosed in double-quotes as single arguments
425 int len
= cmdLine
.Length();
429 while ((i
< len
) && wxIsspace(cmdLine
.GetChar(i
)))
434 if (cmdLine
.GetChar(i
) == wxT('"')) // We found the start of a string
438 while ((i
< len
) && (cmdLine
.GetChar(i
) != wxT('"')))
441 wxString
arg(cmdLine
.Mid(first
, (i
- first
)));
447 i
++; // Skip past 2nd quote
449 else // Unquoted argument
452 while ((i
< len
) && !wxIsspace(cmdLine
.GetChar(i
)))
455 wxString
arg(cmdLine
.Mid(first
, (i
- first
)));
463 wxTheApp
->argv
= new wxChar
*[count
+ 1];
464 for (i
= 0; i
< count
; i
++)
466 wxString
arg(args
[i
]);
467 wxTheApp
->argv
[i
] = copystring((const wxChar
*)arg
);
469 wxTheApp
->argv
[count
] = NULL
; // argv[] is a NULL-terminated list
470 wxTheApp
->argc
= count
;
473 //// Cleans up any wxWindows internal structures left lying around
475 void wxApp::CleanUp()
480 // flush the logged messages if any and install a 'safer' log target: the
481 // default one (wxLogGui) can't be used after the resources are freed just
482 // below and the user suppliedo ne might be even more unsafe (using any
483 // wxWindows GUI function is unsafe starting from now)
484 wxLog::DontCreateOnDemand();
486 // this will flush the old messages if any
487 delete wxLog::SetActiveTarget(new wxLogStderr
);
490 // One last chance for pending objects to be cleaned up
491 wxTheApp
->DeletePendingObjects();
493 wxModule::CleanUpModules();
495 #if wxUSE_WX_RESOURCES
496 wxCleanUpResourceSystem();
498 // wxDefaultResourceTable->ClearTable();
501 // Indicate that the cursor can be freed, so that cursor won't be deleted
502 // by deleting the bitmap list before g_globalCursor goes out of scope
503 // (double deletion of the cursor).
504 wxSetCursor(wxNullCursor
);
505 delete g_globalCursor
;
506 g_globalCursor
= NULL
;
508 wxDeleteStockObjects();
510 // Destroy all GDI lists, etc.
511 wxDeleteStockLists();
513 delete wxTheColourDatabase
;
514 wxTheColourDatabase
= NULL
;
516 wxBitmap::CleanUpHandlers();
521 //// WINDOWS-SPECIFIC CLEANUP
523 wxSetKeyboardHook(FALSE
);
529 if (wxSTD_FRAME_ICON
)
530 DestroyIcon(wxSTD_FRAME_ICON
);
531 if (wxSTD_MDICHILDFRAME_ICON
)
532 DestroyIcon(wxSTD_MDICHILDFRAME_ICON
);
533 if (wxSTD_MDIPARENTFRAME_ICON
)
534 DestroyIcon(wxSTD_MDIPARENTFRAME_ICON
);
536 if (wxDEFAULT_FRAME_ICON
)
537 DestroyIcon(wxDEFAULT_FRAME_ICON
);
538 if (wxDEFAULT_MDICHILDFRAME_ICON
)
539 DestroyIcon(wxDEFAULT_MDICHILDFRAME_ICON
);
540 if (wxDEFAULT_MDIPARENTFRAME_ICON
)
541 DestroyIcon(wxDEFAULT_MDIPARENTFRAME_ICON
);
543 if ( wxDisableButtonBrush
)
544 ::DeleteObject( wxDisableButtonBrush
);
551 Ctl3dUnregister(wxhInstance
);
555 delete wxWinHandleList
;
557 // GL: I'm annoyed ... I don't know where to put this and I don't want to
558 // create a module for that as it's part of the core.
559 delete wxPendingEvents
;
561 delete wxPendingEventsLocker
;
562 // If we don't do the following, we get an apparent memory leak.
563 ((wxEvtHandler
&) wxDefaultValidator
).ClearEventLocker();
566 wxClassInfo::CleanUpClasses();
571 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
572 // At this point we want to check if there are any memory
573 // blocks that aren't part of the wxDebugContext itself,
574 // as a special case. Then when dumping we need to ignore
575 // wxDebugContext, too.
576 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
578 wxLogDebug(wxT("There were memory leaks."));
579 wxDebugContext::Dump();
580 wxDebugContext::PrintStatistics();
582 // wxDebugContext::SetStream(NULL, NULL);
586 // do it as the very last thing because everything else can log messages
587 delete wxLog::SetActiveTarget(NULL
);
591 #if !defined(_WINDLL) || (defined(_WINDLL) && defined(WXMAKINGDLL))
593 // temporarily disable this warning which would be generated in release builds
596 #pragma warning(disable: 4715) // not all control paths return a value
599 //// Main wxWindows entry point
600 int wxEntry(WXHINSTANCE hInstance
,
601 WXHINSTANCE
WXUNUSED(hPrevInstance
),
606 // do check for memory leaks on program exit
607 // (another useful flag is _CRTDBG_DELAY_FREE_MEM_DF which doesn't free
608 // deallocated memory which may be used to simulate low-memory condition)
609 wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF
);
611 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
612 // This seems to be necessary since there are 'rogue'
613 // objects present at this point (perhaps global objects?)
614 // Setting a checkpoint will ignore them as far as the
615 // memory checking facility is concerned.
616 // Of course you may argue that memory allocated in globals should be
617 // checked, but this is a reasonable compromise.
618 wxDebugContext::SetCheckpoint();
621 // take everything into a try-except block in release build
622 // FIXME other compilers must support Win32 SEH (structured exception
623 // handling) too, just find the appropriate keyword in their docs!
624 // Please note that it's _not_ the same as C++ exceptions!
625 #if !defined(__WXDEBUG__) && defined(__VISUALC__)
626 #define CATCH_PROGRAM_EXCEPTIONS
630 #undef CATCH_PROGRAM_EXCEPTIONS
632 wxhInstance
= (HINSTANCE
) hInstance
;
634 if (!wxApp::Initialize())
637 // create the application object or ensure that one already exists
640 // The app may have declared a global application object, but we recommend
641 // the IMPLEMENT_APP macro is used instead, which sets an initializer
642 // function for delayed, dynamic app object construction.
643 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
644 wxT("No initializer - use IMPLEMENT_APP macro.") );
646 wxTheApp
= (*wxApp::GetInitializerFunction()) ();
649 wxCHECK_MSG( wxTheApp
, 0, wxT("You have to define an instance of wxApp!") );
651 // save the WinMain() parameters
652 wxTheApp
->ConvertToStandardCommandArgs(lpCmdLine
);
653 wxTheApp
->m_nCmdShow
= nCmdShow
;
655 // GUI-specific initialisation. In fact on Windows we don't have any,
656 // but this call is provided for compatibility across platforms.
657 wxTheApp
->OnInitGui();
659 // We really don't want timestamps by default, because it means
660 // we can't simply double-click on the error message and get to that
661 // line in the source. So VC++ at least, let's have a sensible default.
663 wxLog::SetTimestamp(NULL
);
668 if ( wxTheApp
->OnInit() )
672 retValue
= wxTheApp
->OnRun();
675 // We want to initialize, but not run or exit immediately.
678 //else: app initialization failed, so we skipped OnRun()
680 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
683 // Forcibly delete the window.
684 if ( topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
685 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
687 topWindow
->Close(TRUE
);
688 wxTheApp
->DeletePendingObjects();
693 wxTheApp
->SetTopWindow(NULL
);
703 #ifdef CATCH_PROGRAM_EXCEPTIONS
705 __except ( EXCEPTION_EXECUTE_HANDLER
) {
708 wxTheApp->OnFatalException();
711 // using wxLog would be unsafe here
713 _("Unrecoverable program error detected: "
714 " the application will terminate."),
716 MB_APPLMODAL
| MB_ICONSTOP
| MB_OK
);
718 ::ExitProcess(3); // the same exit code as abort()
722 #endif // CATCH_PROGRAM_EXCEPTIONS
725 // restore warning state
727 #pragma warning(default: 4715) // not all control paths return a value
732 //// Entry point for DLLs
734 int wxEntry(WXHINSTANCE hInstance
)
736 wxhInstance
= (HINSTANCE
) hInstance
;
739 // The app may have declared a global application object, but we recommend
740 // the IMPLEMENT_APP macro is used instead, which sets an initializer function
741 // for delayed, dynamic app object construction.
744 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
745 "No initializer - use IMPLEMENT_APP macro." );
747 wxTheApp
= (* wxApp::GetInitializerFunction()) ();
750 wxCHECK_MSG( wxTheApp
, 0, "You have to define an instance of wxApp!" );
753 wxTheApp
->argv
= NULL
;
755 wxTheApp
->OnInitGui();
759 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
760 if ( topWindow
&& topWindow
->GetHWND())
762 topWindow
->Show(TRUE
);
769 //// Static member initialization
771 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
777 m_wantDebugOutput
= TRUE
;
781 m_printMode
= wxPRINT_WINDOWS
;
782 m_exitOnFrameDelete
= TRUE
;
788 // Delete command-line args
790 for (i
= 0; i
< argc
; i
++)
797 bool wxApp::Initialized()
805 #ifdef _WINDLL // Assume initialized if DLL (no way of telling)
811 * Get and process a message, returning FALSE if WM_QUIT
812 * received (and also set the flag telling the app to exit the main loop)
815 bool wxApp::DoMessage()
817 BOOL rc
= ::GetMessage(&s_currentMsg
, (HWND
) NULL
, 0, 0);
827 // should never happen, but let's test for it nevertheless
828 wxLogLastError("GetMessage");
833 wxASSERT_MSG( wxThread::IsMain(),
834 wxT("only the main thread can process Windows messages") );
836 static bool s_hadGuiLock
= TRUE
;
837 static wxMsgArray s_aSavedMessages
;
839 // if a secondary thread owns is doing GUI calls, save all messages for
840 // later processing - we can't process them right now because it will
841 // lead to recursive library calls (and we're not reentrant)
842 if ( !wxGuiOwnedByMainThread() )
844 s_hadGuiLock
= FALSE
;
846 // leave out WM_COMMAND messages: too dangerous, sometimes
847 // the message will be processed twice
848 if ( !wxIsWaitingForThread() ||
849 s_currentMsg
.message
!= WM_COMMAND
)
851 s_aSavedMessages
.Add(s_currentMsg
);
858 // have we just regained the GUI lock? if so, post all of the saved
861 // FIXME of course, it's not _exactly_ the same as processing the
862 // messages normally - expect some things to break...
867 size_t count
= s_aSavedMessages
.Count();
868 for ( size_t n
= 0; n
< count
; n
++ )
870 MSG
& msg
= s_aSavedMessages
[n
];
872 if ( !ProcessMessage((WXMSG
*)&msg
) )
874 ::TranslateMessage(&msg
);
875 ::DispatchMessage(&msg
);
879 s_aSavedMessages
.Empty();
882 #endif // wxUSE_THREADS
884 // Process the message
885 if ( !ProcessMessage((WXMSG
*)&s_currentMsg
) )
887 ::TranslateMessage(&s_currentMsg
);
888 ::DispatchMessage(&s_currentMsg
);
896 * Keep trying to process messages until WM_QUIT
899 * If there are messages to be processed, they will all be
900 * processed and OnIdle will not be called.
901 * When there are no more messages, OnIdle is called.
902 * If OnIdle requests more time,
903 * it will be repeatedly called so long as there are no pending messages.
904 * A 'feature' of this is that once OnIdle has decided that no more processing
905 * is required, then it won't get processing time until further messages
906 * are processed (it'll sit in DoMessage).
909 int wxApp::MainLoop()
913 while ( m_keepGoing
)
916 wxMutexGuiLeaveOrEnter();
917 #endif // wxUSE_THREADS
919 while ( !Pending() && ProcessIdle() )
922 // a message came or no more idle processing to do
926 return s_currentMsg
.wParam
;
929 // Returns TRUE if more time is needed.
930 bool wxApp::ProcessIdle()
933 event
.SetEventObject(this);
936 return event
.MoreRequested();
939 void wxApp::ExitMainLoop()
941 // VZ: why not ::PostQuitMessage()?
945 bool wxApp::Pending()
947 return ::PeekMessage(&s_currentMsg
, 0, 0, 0, PM_NOREMOVE
) != 0;
950 void wxApp::Dispatch()
956 * Give all windows a chance to preprocess
957 * the message. Some may have accelerator tables, or have
961 bool wxApp::ProcessMessage(WXMSG
*wxmsg
)
963 MSG
*msg
= (MSG
*)wxmsg
;
964 HWND hWnd
= msg
->hwnd
;
965 wxWindow
*wndThis
= wxGetWindowFromHWND((WXHWND
)hWnd
);
968 // we must relay WM_MOUSEMOVE events to the tooltip ctrl if we want it to
969 // popup the tooltip bubbles
970 if ( wndThis
&& (msg
->message
== WM_MOUSEMOVE
) )
972 wxToolTip
*tt
= wndThis
->GetToolTip();
975 tt
->RelayEvent(wxmsg
);
978 #endif // wxUSE_TOOLTIPS
980 // Try translations first; find the youngest window with
981 // a translation table.
983 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
985 if ( wnd
->MSWTranslateMessage(wxmsg
) )
989 // Anyone for a non-translation message? Try youngest descendants first.
990 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
992 if ( wnd
->MSWProcessMessage(wxmsg
) )
999 void wxApp::OnIdle(wxIdleEvent
& event
)
1001 static bool s_inOnIdle
= FALSE
;
1003 // Avoid recursion (via ProcessEvent default case)
1009 // 'Garbage' collection of windows deleted with Close().
1010 DeletePendingObjects();
1013 // flush the logged messages if any
1014 wxLog
*pLog
= wxLog::GetActiveTarget();
1015 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
1019 // Send OnIdle events to all windows
1020 if ( SendIdleEvents() )
1022 // SendIdleEvents() returns TRUE if at least one window requested more
1024 event
.RequestMore(TRUE
);
1027 // If they are pending events, we must process them: pending events are
1028 // either events to the threads other than main or events posted with
1029 // wxPostEvent() functions
1030 ProcessPendingEvents();
1035 // Send idle event to all top-level windows
1036 bool wxApp::SendIdleEvents()
1038 bool needMore
= FALSE
;
1040 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
1043 wxWindow
* win
= node
->GetData();
1044 if (SendIdleEvents(win
))
1046 node
= node
->GetNext();
1052 // Send idle event to window and all subwindows
1053 bool wxApp::SendIdleEvents(wxWindow
* win
)
1055 bool needMore
= FALSE
;
1058 event
.SetEventObject(win
);
1059 win
->GetEventHandler()->ProcessEvent(event
);
1061 if (event
.MoreRequested())
1064 wxNode
* node
= win
->GetChildren().First();
1067 wxWindow
* win
= (wxWindow
*) node
->Data();
1068 if (SendIdleEvents(win
))
1071 node
= node
->Next();
1076 void wxApp::DeletePendingObjects()
1078 wxNode
*node
= wxPendingDelete
.First();
1081 wxObject
*obj
= (wxObject
*)node
->Data();
1085 if (wxPendingDelete
.Member(obj
))
1088 // Deleting one object may have deleted other pending
1089 // objects, so start from beginning of list again.
1090 node
= wxPendingDelete
.First();
1094 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
1097 GetTopWindow()->Close(TRUE
);
1100 // Default behaviour: close the application with prompts. The
1101 // user can veto the close, and therefore the end session.
1102 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
1106 if (!GetTopWindow()->Close(!event
.CanVeto()))
1112 int wxApp::GetComCtl32Version()
1115 static int s_verComCtl32
= -1;
1117 wxCRIT_SECT_DECLARE(csComCtl32
);
1118 wxCRIT_SECT_LOCKER(lock
, csComCtl32
);
1120 if ( s_verComCtl32
== -1 )
1122 // initally assume no comctl32.dll at all
1126 HMODULE hModuleComCtl32
= ::GetModuleHandle(wxT("COMCTL32"));
1128 // if so, then we can check for the version
1129 if ( hModuleComCtl32
)
1131 // try to use DllGetVersion() if available in _headers_
1132 #ifdef DLLVER_PLATFORM_WINDOWS // defined in shlwapi.h
1133 DLLGETVERSIONPROC pfnDllGetVersion
= (DLLGETVERSIONPROC
)
1134 ::GetProcAddress(hModuleComCtl32
, _T("DllGetVersion"));
1135 if ( pfnDllGetVersion
)
1138 dvi
.cbSize
= sizeof(dvi
);
1140 HRESULT hr
= (*pfnDllGetVersion
)(&dvi
);
1143 wxLogApiError(_T("DllGetVersion"), hr
);
1147 // this is incompatible with _WIN32_IE values, but
1148 // compatible with the other values returned by
1149 // GetComCtl32Version()
1150 s_verComCtl32
= 100*dvi
.dwMajorVersion
+
1155 // DllGetVersion() unavailable either during compile or
1156 // run-time, try to guess the version otherwise
1157 if ( !s_verComCtl32
)
1159 // InitCommonControlsEx is unique to 4.70 and later
1160 FARPROC theProc
= ::GetProcAddress
1163 #if defined(__BORLANDC__) && (__BORLANDC__ <= 0x520)
1164 "InitCommonControlsEx"
1166 _T("InitCommonControlsEx")
1172 // not found, must be 4.00
1173 s_verComCtl32
= 400;
1177 // many symbols appeared in comctl32 4.71, could use
1178 // any of them except may be DllInstall
1179 theProc
= ::GetProcAddress
1182 #if defined(__BORLANDC__) && (__BORLANDC__ <= 0x520)
1185 _T("InitializeFlatSB")
1190 // not found, must be 4.70
1191 s_verComCtl32
= 470;
1195 // found, must be 4.71
1196 s_verComCtl32
= 471;
1203 return s_verComCtl32
;
1208 wxLogError(_("Fatal error: exiting"));
1213 // Yield to incoming messages
1216 // we don't want to process WM_QUIT from here - it should be processed in
1217 // the main event loop in order to stop it
1220 while ( PeekMessage(&msg
, (HWND
)0, 0, 0, PM_NOREMOVE
) &&
1221 msg
.message
!= WM_QUIT
)
1224 wxMutexGuiLeaveOrEnter();
1225 #endif // wxUSE_THREADS
1227 if ( !wxTheApp
->DoMessage() )
1231 // If they are pending events, we must process them.
1232 wxTheApp
->ProcessPendingEvents();
1237 //-----------------------------------------------------------------------------
1239 //-----------------------------------------------------------------------------
1243 // Send the top window a dummy message so idle handler processing will
1244 // start up again. Doing it this way ensures that the idle handler
1245 // wakes up in the right thread (see also wxWakeUpMainThread() which does
1246 // the same for the main app thread only)
1247 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
1250 if ( !::PostMessage(GetHwndOf(topWindow
), WM_NULL
, 0, 0) )
1252 // should never happen
1253 wxLogLastError("PostMessage(WM_NULL)");
1258 //-----------------------------------------------------------------------------
1261 wxApp::GetStdIcon(int which
) const
1265 case wxICON_INFORMATION
:
1266 return wxIcon("wxICON_INFO");
1268 case wxICON_QUESTION
:
1269 return wxIcon("wxICON_QUESTION");
1271 case wxICON_EXCLAMATION
:
1272 return wxIcon("wxICON_WARNING");
1275 wxFAIL_MSG(wxT("requested non existent standard icon"));
1276 // still fall through
1279 return wxIcon("wxICON_ERROR");
1283 // For some reason, with MSVC++ 1.5, WinMain isn't linked in properly
1284 // if in a separate file. So include it here to ensure it's linked.
1285 #if (defined(__VISUALC__) && !defined(__WIN32__)) || (defined(__GNUWIN32__) && !defined(__TWIN32__) && !defined(WXMAKINGDLL))