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 ( !::PeekMessage(&s_currentMsg
, 0, 0, 0, PM_NOREMOVE
) &&
928 return s_currentMsg
.wParam
;
931 // Returns TRUE if more time is needed.
932 bool wxApp::ProcessIdle()
935 event
.SetEventObject(this);
938 return event
.MoreRequested();
941 void wxApp::ExitMainLoop()
946 bool wxApp::Pending()
948 return (::PeekMessage(&s_currentMsg
, 0, 0, 0, PM_NOREMOVE
) != 0);
951 void wxApp::Dispatch()
957 * Give all windows a chance to preprocess
958 * the message. Some may have accelerator tables, or have
962 bool wxApp::ProcessMessage(WXMSG
*wxmsg
)
964 MSG
*msg
= (MSG
*)wxmsg
;
965 HWND hWnd
= msg
->hwnd
;
966 wxWindow
*wndThis
= wxGetWindowFromHWND((WXHWND
)hWnd
);
969 // we must relay WM_MOUSEMOVE events to the tooltip ctrl if we want it to
970 // popup the tooltip bubbles
971 if ( wndThis
&& (msg
->message
== WM_MOUSEMOVE
) )
973 wxToolTip
*tt
= wndThis
->GetToolTip();
976 tt
->RelayEvent(wxmsg
);
979 #endif // wxUSE_TOOLTIPS
981 // Try translations first; find the youngest window with
982 // a translation table.
984 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
986 if ( wnd
->MSWTranslateMessage(wxmsg
) )
990 // Anyone for a non-translation message? Try youngest descendants first.
991 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
993 if ( wnd
->MSWProcessMessage(wxmsg
) )
1000 void wxApp::OnIdle(wxIdleEvent
& event
)
1002 static bool s_inOnIdle
= FALSE
;
1004 // Avoid recursion (via ProcessEvent default case)
1010 // 'Garbage' collection of windows deleted with Close().
1011 DeletePendingObjects();
1014 // flush the logged messages if any
1015 wxLog
*pLog
= wxLog::GetActiveTarget();
1016 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
1020 // Send OnIdle events to all windows
1021 if ( SendIdleEvents() )
1023 // SendIdleEvents() returns TRUE if at least one window requested more
1025 event
.RequestMore(TRUE
);
1028 // If they are pending events, we must process them: pending events are
1029 // either events to the threads other than main or events posted with
1030 // wxPostEvent() functions
1031 ProcessPendingEvents();
1036 // Send idle event to all top-level windows
1037 bool wxApp::SendIdleEvents()
1039 bool needMore
= FALSE
;
1041 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
1044 wxWindow
* win
= node
->GetData();
1045 if (SendIdleEvents(win
))
1047 node
= node
->GetNext();
1053 // Send idle event to window and all subwindows
1054 bool wxApp::SendIdleEvents(wxWindow
* win
)
1056 bool needMore
= FALSE
;
1059 event
.SetEventObject(win
);
1060 win
->GetEventHandler()->ProcessEvent(event
);
1062 if (event
.MoreRequested())
1065 wxNode
* node
= win
->GetChildren().First();
1068 wxWindow
* win
= (wxWindow
*) node
->Data();
1069 if (SendIdleEvents(win
))
1072 node
= node
->Next();
1077 void wxApp::DeletePendingObjects()
1079 wxNode
*node
= wxPendingDelete
.First();
1082 wxObject
*obj
= (wxObject
*)node
->Data();
1086 if (wxPendingDelete
.Member(obj
))
1089 // Deleting one object may have deleted other pending
1090 // objects, so start from beginning of list again.
1091 node
= wxPendingDelete
.First();
1095 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
1098 GetTopWindow()->Close(TRUE
);
1101 // Default behaviour: close the application with prompts. The
1102 // user can veto the close, and therefore the end session.
1103 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
1107 if (!GetTopWindow()->Close(!event
.CanVeto()))
1113 int wxApp::GetComCtl32Version()
1116 static int s_verComCtl32
= -1;
1118 wxCRIT_SECT_DECLARE(csComCtl32
);
1119 wxCRIT_SECT_LOCKER(lock
, csComCtl32
);
1121 if ( s_verComCtl32
== -1 )
1123 // initally assume no comctl32.dll at all
1127 HMODULE hModuleComCtl32
= ::GetModuleHandle(wxT("COMCTL32"));
1129 // if so, then we can check for the version
1130 if ( hModuleComCtl32
)
1132 // try to use DllGetVersion() if available in _headers_
1133 #ifdef DLLVER_PLATFORM_WINDOWS // defined in shlwapi.h
1134 DLLGETVERSIONPROC pfnDllGetVersion
= (DLLGETVERSIONPROC
)
1135 ::GetProcAddress(hModuleComCtl32
, _T("DllGetVersion"));
1136 if ( pfnDllGetVersion
)
1139 dvi
.cbSize
= sizeof(dvi
);
1141 HRESULT hr
= (*pfnDllGetVersion
)(&dvi
);
1144 wxLogApiError(_T("DllGetVersion"), hr
);
1148 // this is incompatible with _WIN32_IE values, but
1149 // compatible with the other values returned by
1150 // GetComCtl32Version()
1151 s_verComCtl32
= 100*dvi
.dwMajorVersion
+
1156 // DllGetVersion() unavailable either during compile or
1157 // run-time, try to guess the version otherwise
1158 if ( !s_verComCtl32
)
1160 // InitCommonControlsEx is unique to 4.70 and later
1161 FARPROC theProc
= ::GetProcAddress
1164 #if defined(__BORLANDC__) && (__BORLANDC__ <= 0x520)
1165 "InitCommonControlsEx"
1167 _T("InitCommonControlsEx")
1173 // not found, must be 4.00
1174 s_verComCtl32
= 400;
1178 // many symbols appeared in comctl32 4.71, could use
1179 // any of them except may be DllInstall
1180 theProc
= ::GetProcAddress
1183 #if defined(__BORLANDC__) && (__BORLANDC__ <= 0x520)
1186 _T("InitializeFlatSB")
1191 // not found, must be 4.70
1192 s_verComCtl32
= 470;
1196 // found, must be 4.71
1197 s_verComCtl32
= 471;
1204 return s_verComCtl32
;
1209 wxLogError(_("Fatal error: exiting"));
1214 // Yield to incoming messages
1217 // we don't want to process WM_QUIT from here - it should be processed in
1218 // the main event loop in order to stop it
1221 while ( PeekMessage(&msg
, (HWND
)0, 0, 0, PM_NOREMOVE
) &&
1222 msg
.message
!= WM_QUIT
)
1224 if ( !wxTheApp
->DoMessage() )
1228 // If they are pending events, we must process them.
1229 wxTheApp
->ProcessPendingEvents();
1234 //-----------------------------------------------------------------------------
1236 //-----------------------------------------------------------------------------
1240 // Send the top window a dummy message so idle handler processing will
1241 // start up again. Doing it this way ensures that the idle handler
1242 // wakes up in the right thread (see also wxWakeUpMainThread() which does
1243 // the same for the main app thread only)
1244 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
1247 if ( !::PostMessage(GetHwndOf(topWindow
), WM_NULL
, 0, 0) )
1249 // should never happen
1250 wxLogLastError("PostMessage(WM_NULL)");
1255 //-----------------------------------------------------------------------------
1258 wxApp::GetStdIcon(int which
) const
1262 case wxICON_INFORMATION
:
1263 return wxIcon("wxICON_INFO");
1265 case wxICON_QUESTION
:
1266 return wxIcon("wxICON_QUESTION");
1268 case wxICON_EXCLAMATION
:
1269 return wxIcon("wxICON_WARNING");
1272 wxFAIL_MSG(wxT("requested non existent standard icon"));
1273 // still fall through
1276 return wxIcon("wxICON_ERROR");
1280 // For some reason, with MSVC++ 1.5, WinMain isn't linked in properly
1281 // if in a separate file. So include it here to ensure it's linked.
1282 #if (defined(__VISUALC__) && !defined(__WIN32__)) || (defined(__GNUWIN32__) && !defined(__TWIN32__) && !defined(WXMAKINGDLL))