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..., but some
75 // compilers don't support it (missing headers, libs, ...)
76 #if defined(__GNUWIN32_OLD__) || defined(__SC__) || defined(__SALFORDC__)
80 #endif // broken compilers
89 #if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) || defined(__TWIN32__))
93 #include "wx/msw/msvcrt.h"
95 // ----------------------------------------------------------------------------
96 // conditional compilation
97 // ----------------------------------------------------------------------------
99 // The macro _WIN32_IE is defined by commctrl.h (unless it had already been
100 // defined before) and shows us what common control features are available
101 // during the compile time (it doesn't mean that they will be available during
102 // the run-time, use GetComCtl32Version() to test for them!). The possible
105 // 0x0200 for comctl32.dll 4.00 shipped with Win95/NT 4.0
106 // 0x0300 4.70 IE 3.x
107 // 0x0400 4.71 IE 4.0
108 // 0x0401 4.72 IE 4.01 and Win98
109 // 0x0500 5.00 IE 5.x and NT 5.0 (Win2000)
112 // minimal set of features by default
113 #define _WIN32_IE 0x0200
116 #if _WIN32_IE >= 0x0300
120 // ---------------------------------------------------------------------------
122 // ---------------------------------------------------------------------------
124 extern wxChar
*wxBuffer
;
125 extern wxChar
*wxOsVersion
;
126 extern wxList
*wxWinHandleList
;
127 extern wxList WXDLLEXPORT wxPendingDelete
;
128 extern void wxSetKeyboardHook(bool doIt
);
129 extern wxCursor
*g_globalCursor
;
132 wxApp
*wxTheApp
= NULL
;
134 // NB: all "NoRedraw" classes must have the same names as the "normal" classes
135 // with NR suffix - wxWindow::MSWCreate() supposes this
136 const wxChar
*wxFrameClassName
= wxT("wxFrameClass");
137 const wxChar
*wxFrameClassNameNoRedraw
= wxT("wxFrameClassNR");
138 const wxChar
*wxMDIFrameClassName
= wxT("wxMDIFrameClass");
139 const wxChar
*wxMDIFrameClassNameNoRedraw
= wxT("wxMDIFrameClassNR");
140 const wxChar
*wxMDIChildFrameClassName
= wxT("wxMDIChildFrameClass");
141 const wxChar
*wxMDIChildFrameClassNameNoRedraw
= wxT("wxMDIChildFrameClassNR");
142 const wxChar
*wxPanelClassName
= wxT("wxPanelClass");
143 const wxChar
*wxCanvasClassName
= wxT("wxCanvasClass");
145 HICON wxSTD_FRAME_ICON
= (HICON
) NULL
;
146 HICON wxSTD_MDICHILDFRAME_ICON
= (HICON
) NULL
;
147 HICON wxSTD_MDIPARENTFRAME_ICON
= (HICON
) NULL
;
149 HICON wxDEFAULT_FRAME_ICON
= (HICON
) NULL
;
150 HICON wxDEFAULT_MDICHILDFRAME_ICON
= (HICON
) NULL
;
151 HICON wxDEFAULT_MDIPARENTFRAME_ICON
= (HICON
) NULL
;
153 HBRUSH wxDisableButtonBrush
= (HBRUSH
) 0;
155 LRESULT WXDLLEXPORT APIENTRY
wxWndProc(HWND
, UINT
, WPARAM
, LPARAM
);
157 // ===========================================================================
159 // ===========================================================================
161 // ---------------------------------------------------------------------------
163 // ---------------------------------------------------------------------------
165 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
167 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
168 EVT_IDLE(wxApp::OnIdle
)
169 EVT_END_SESSION(wxApp::OnEndSession
)
170 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession
)
174 bool wxApp::Initialize()
176 // Some people may wish to use this, but
177 // probably it shouldn't be here by default.
179 // wxRedirectIOToConsole();
182 wxBuffer
= new wxChar
[1500]; // FIXME
184 wxClassInfo::InitializeClasses();
187 wxGetResource(wxT("wxWindows"), wxT("OsVersion"), &wxOsVersion
);
191 wxPendingEventsLocker
= new wxCriticalSection
;
194 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
195 wxTheColourDatabase
->Initialize();
197 wxInitializeStockLists();
198 wxInitializeStockObjects();
200 #if wxUSE_WX_RESOURCES
201 wxInitializeResourceSystem();
204 wxBitmap::InitStandardHandlers();
206 #if defined(__WIN95__)
207 InitCommonControls();
214 // for OLE, enlarge message queue to be as large as possible
216 while (!SetMessageQueue(iMsg
) && (iMsg
-= 8))
219 // we need to initialize OLE library
220 if ( FAILED(::OleInitialize(NULL
)) )
221 wxLogError(_("Cannot initialize OLE"));
225 if (!Ctl3dRegister(wxhInstance
))
226 wxLogError(wxT("Cannot register CTL3D"));
228 Ctl3dAutoSubclass(wxhInstance
);
231 g_globalCursor
= new wxCursor
;
233 // VZ: these icons are not in wx.rc anyhow (but should they?)!
235 wxSTD_FRAME_ICON
= LoadIcon(wxhInstance
, wxT("wxSTD_FRAME"));
236 wxSTD_MDIPARENTFRAME_ICON
= LoadIcon(wxhInstance
, wxT("wxSTD_MDIPARENTFRAME"));
237 wxSTD_MDICHILDFRAME_ICON
= LoadIcon(wxhInstance
, wxT("wxSTD_MDICHILDFRAME"));
239 wxDEFAULT_FRAME_ICON
= LoadIcon(wxhInstance
, wxT("wxDEFAULT_FRAME"));
240 wxDEFAULT_MDIPARENTFRAME_ICON
= LoadIcon(wxhInstance
, wxT("wxDEFAULT_MDIPARENTFRAME"));
241 wxDEFAULT_MDICHILDFRAME_ICON
= LoadIcon(wxhInstance
, wxT("wxDEFAULT_MDICHILDFRAME"));
244 RegisterWindowClasses();
246 // Create the brush for disabling bitmap buttons
249 lb
.lbStyle
= BS_PATTERN
;
250 lb
.lbHatch
= (int)LoadBitmap( wxhInstance
, wxT("wxDISABLE_BUTTON_BITMAP") );
253 wxDisableButtonBrush
= ::CreateBrushIndirect( & lb
);
254 ::DeleteObject( (HGDIOBJ
)lb
.lbHatch
);
256 //else: wxWindows resources are probably not linked in
262 wxWinHandleList
= new wxList(wxKEY_INTEGER
);
264 // This is to foil optimizations in Visual C++ that throw out dummy.obj.
265 // PLEASE DO NOT ALTER THIS.
266 #if defined(__VISUALC__) && !defined(WXMAKINGDLL)
267 extern char wxDummyChar
;
268 if (wxDummyChar
) wxDummyChar
++;
271 wxSetKeyboardHook(TRUE
);
273 wxModule::RegisterModules();
274 if (!wxModule::InitializeModules())
279 // ---------------------------------------------------------------------------
280 // RegisterWindowClasses
281 // ---------------------------------------------------------------------------
283 // TODO we should only register classes really used by the app. For this it
284 // would be enough to just delay the class registration until an attempt
285 // to create a window of this class is made.
286 bool wxApp::RegisterWindowClasses()
290 // for each class we register one with CS_(V|H)REDRAW style and one
291 // without for windows created with wxNO_FULL_REDRAW_ON_REPAINT flag
292 static const long styleNormal
= CS_HREDRAW
| CS_VREDRAW
| CS_DBLCLKS
;
293 static const long styleNoRedraw
= CS_DBLCLKS
;
295 // the fields which are common to all classes
296 wndclass
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
297 wndclass
.cbClsExtra
= 0;
298 wndclass
.cbWndExtra
= sizeof( DWORD
); // VZ: what is this DWORD used for?
299 wndclass
.hInstance
= wxhInstance
;
300 wndclass
.hIcon
= (HICON
) NULL
;
301 wndclass
.hCursor
= ::LoadCursor((HINSTANCE
)NULL
, IDC_ARROW
);
302 wndclass
.lpszMenuName
= NULL
;
304 // Register the frame window class.
305 wndclass
.hbrBackground
= (HBRUSH
)(COLOR_APPWORKSPACE
+ 1);
306 wndclass
.lpszClassName
= wxFrameClassName
;
307 wndclass
.style
= styleNormal
;
309 if ( !RegisterClass(&wndclass
) )
311 wxLogLastError("RegisterClass(frame)");
317 wndclass
.lpszClassName
= wxFrameClassNameNoRedraw
;
318 wndclass
.style
= styleNoRedraw
;
320 if ( !RegisterClass(&wndclass
) )
322 wxLogLastError("RegisterClass(no redraw frame)");
327 // Register the MDI frame window class.
328 wndclass
.hbrBackground
= (HBRUSH
)NULL
; // paint MDI frame ourselves
329 wndclass
.lpszClassName
= wxMDIFrameClassName
;
330 wndclass
.style
= styleNormal
;
332 if ( !RegisterClass(&wndclass
) )
334 wxLogLastError("RegisterClass(MDI parent)");
339 // "no redraw" MDI frame
340 wndclass
.lpszClassName
= wxMDIFrameClassNameNoRedraw
;
341 wndclass
.style
= styleNoRedraw
;
343 if ( !RegisterClass(&wndclass
) )
345 wxLogLastError("RegisterClass(no redraw MDI parent frame)");
350 // Register the MDI child frame window class.
351 wndclass
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+ 1);
352 wndclass
.lpszClassName
= wxMDIChildFrameClassName
;
353 wndclass
.style
= styleNormal
;
355 if ( !RegisterClass(&wndclass
) )
357 wxLogLastError("RegisterClass(MDI child)");
362 // "no redraw" MDI child frame
363 wndclass
.lpszClassName
= wxMDIChildFrameClassNameNoRedraw
;
364 wndclass
.style
= styleNoRedraw
;
366 if ( !RegisterClass(&wndclass
) )
368 wxLogLastError("RegisterClass(no redraw MDI child)");
373 // Register the panel window class.
374 wndclass
.hbrBackground
= (HBRUSH
) GetStockObject( LTGRAY_BRUSH
);
375 wndclass
.lpszClassName
= wxPanelClassName
;
376 wndclass
.style
= styleNormal
;
378 if ( !RegisterClass(&wndclass
) )
380 wxLogLastError("RegisterClass(panel)");
385 // Register the canvas and textsubwindow class name
386 wndclass
.hbrBackground
= (HBRUSH
)NULL
;
387 wndclass
.lpszClassName
= wxCanvasClassName
;
389 if ( !RegisterClass(&wndclass
) )
391 wxLogLastError("RegisterClass(canvas)");
399 // ---------------------------------------------------------------------------
400 // Convert Windows to argc, argv style
401 // ---------------------------------------------------------------------------
403 void wxApp::ConvertToStandardCommandArgs(char* lpCmdLine
)
407 wxString
cmdLine(lpCmdLine
);
410 // Get application name
411 wxChar name
[260]; // 260 is MAX_PATH value from windef.h
412 ::GetModuleFileName(wxhInstance
, name
, WXSIZEOF(name
));
417 wxStrcpy(name
, wxFileNameFromPath(name
));
418 wxStripExtension(name
);
419 wxTheApp
->SetAppName(name
);
422 // Treat strings enclosed in double-quotes as single arguments
424 int len
= cmdLine
.Length();
428 while ((i
< len
) && wxIsspace(cmdLine
.GetChar(i
)))
433 if (cmdLine
.GetChar(i
) == wxT('"')) // We found the start of a string
437 while ((i
< len
) && (cmdLine
.GetChar(i
) != wxT('"')))
440 wxString
arg(cmdLine
.Mid(first
, (i
- first
)));
446 i
++; // Skip past 2nd quote
448 else // Unquoted argument
451 while ((i
< len
) && !wxIsspace(cmdLine
.GetChar(i
)))
454 wxString
arg(cmdLine
.Mid(first
, (i
- first
)));
462 wxTheApp
->argv
= new wxChar
*[count
+ 1];
463 for (i
= 0; i
< count
; i
++)
465 wxString
arg(args
[i
]);
466 wxTheApp
->argv
[i
] = copystring((const wxChar
*)arg
);
468 wxTheApp
->argv
[count
] = NULL
; // argv[] is a NULL-terminated list
469 wxTheApp
->argc
= count
;
472 //// Cleans up any wxWindows internal structures left lying around
474 void wxApp::CleanUp()
479 // flush the logged messages if any and install a 'safer' log target: the
480 // default one (wxLogGui) can't be used after the resources are freed just
481 // below and the user suppliedo ne might be even more unsafe (using any
482 // wxWindows GUI function is unsafe starting from now)
483 wxLog::DontCreateOnDemand();
485 // this will flush the old messages if any
486 delete wxLog::SetActiveTarget(new wxLogStderr
);
489 // One last chance for pending objects to be cleaned up
490 wxTheApp
->DeletePendingObjects();
492 wxModule::CleanUpModules();
494 #if wxUSE_WX_RESOURCES
495 wxCleanUpResourceSystem();
497 // wxDefaultResourceTable->ClearTable();
500 // Indicate that the cursor can be freed, so that cursor won't be deleted
501 // by deleting the bitmap list before g_globalCursor goes out of scope
502 // (double deletion of the cursor).
503 wxSetCursor(wxNullCursor
);
504 delete g_globalCursor
;
505 g_globalCursor
= NULL
;
507 wxDeleteStockObjects();
509 // Destroy all GDI lists, etc.
510 wxDeleteStockLists();
512 delete wxTheColourDatabase
;
513 wxTheColourDatabase
= NULL
;
515 wxBitmap::CleanUpHandlers();
520 //// WINDOWS-SPECIFIC CLEANUP
522 wxSetKeyboardHook(FALSE
);
528 if (wxSTD_FRAME_ICON
)
529 DestroyIcon(wxSTD_FRAME_ICON
);
530 if (wxSTD_MDICHILDFRAME_ICON
)
531 DestroyIcon(wxSTD_MDICHILDFRAME_ICON
);
532 if (wxSTD_MDIPARENTFRAME_ICON
)
533 DestroyIcon(wxSTD_MDIPARENTFRAME_ICON
);
535 if (wxDEFAULT_FRAME_ICON
)
536 DestroyIcon(wxDEFAULT_FRAME_ICON
);
537 if (wxDEFAULT_MDICHILDFRAME_ICON
)
538 DestroyIcon(wxDEFAULT_MDICHILDFRAME_ICON
);
539 if (wxDEFAULT_MDIPARENTFRAME_ICON
)
540 DestroyIcon(wxDEFAULT_MDIPARENTFRAME_ICON
);
542 if ( wxDisableButtonBrush
)
543 ::DeleteObject( wxDisableButtonBrush
);
550 Ctl3dUnregister(wxhInstance
);
554 delete wxWinHandleList
;
556 // GL: I'm annoyed ... I don't know where to put this and I don't want to
557 // create a module for that as it's part of the core.
558 delete wxPendingEvents
;
560 delete wxPendingEventsLocker
;
561 // If we don't do the following, we get an apparent memory leak.
562 ((wxEvtHandler
&) wxDefaultValidator
).ClearEventLocker();
565 wxClassInfo::CleanUpClasses();
570 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
571 // At this point we want to check if there are any memory
572 // blocks that aren't part of the wxDebugContext itself,
573 // as a special case. Then when dumping we need to ignore
574 // wxDebugContext, too.
575 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
577 wxLogDebug(wxT("There were memory leaks."));
578 wxDebugContext::Dump();
579 wxDebugContext::PrintStatistics();
581 // wxDebugContext::SetStream(NULL, NULL);
585 // do it as the very last thing because everything else can log messages
586 delete wxLog::SetActiveTarget(NULL
);
590 #if !defined(_WINDLL) || (defined(_WINDLL) && defined(WXMAKINGDLL))
592 // temporarily disable this warning which would be generated in release builds
595 #pragma warning(disable: 4715) // not all control paths return a value
598 //// Main wxWindows entry point
599 int wxEntry(WXHINSTANCE hInstance
,
600 WXHINSTANCE
WXUNUSED(hPrevInstance
),
605 // do check for memory leaks on program exit
606 // (another useful flag is _CRTDBG_DELAY_FREE_MEM_DF which doesn't free
607 // deallocated memory which may be used to simulate low-memory condition)
608 wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF
);
610 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
611 // This seems to be necessary since there are 'rogue'
612 // objects present at this point (perhaps global objects?)
613 // Setting a checkpoint will ignore them as far as the
614 // memory checking facility is concerned.
615 // Of course you may argue that memory allocated in globals should be
616 // checked, but this is a reasonable compromise.
617 wxDebugContext::SetCheckpoint();
620 // take everything into a try-except block in release build
621 // FIXME other compilers must support Win32 SEH (structured exception
622 // handling) too, just find the appropriate keyword in their docs!
623 // Please note that it's _not_ the same as C++ exceptions!
624 #if !defined(__WXDEBUG__) && defined(__VISUALC__)
625 #define CATCH_PROGRAM_EXCEPTIONS
629 #undef CATCH_PROGRAM_EXCEPTIONS
631 wxhInstance
= (HINSTANCE
) hInstance
;
633 if (!wxApp::Initialize())
636 // create the application object or ensure that one already exists
639 // The app may have declared a global application object, but we recommend
640 // the IMPLEMENT_APP macro is used instead, which sets an initializer
641 // function for delayed, dynamic app object construction.
642 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
643 wxT("No initializer - use IMPLEMENT_APP macro.") );
645 wxTheApp
= (*wxApp::GetInitializerFunction()) ();
648 wxCHECK_MSG( wxTheApp
, 0, wxT("You have to define an instance of wxApp!") );
650 // save the WinMain() parameters
651 wxTheApp
->ConvertToStandardCommandArgs(lpCmdLine
);
652 wxTheApp
->m_nCmdShow
= nCmdShow
;
654 // GUI-specific initialisation. In fact on Windows we don't have any,
655 // but this call is provided for compatibility across platforms.
656 wxTheApp
->OnInitGui();
658 // We really don't want timestamps by default, because it means
659 // we can't simply double-click on the error message and get to that
660 // line in the source. So VC++ at least, let's have a sensible default.
662 wxLog::SetTimestamp(NULL
);
667 if ( wxTheApp
->OnInit() )
671 retValue
= wxTheApp
->OnRun();
674 // We want to initialize, but not run or exit immediately.
677 //else: app initialization failed, so we skipped OnRun()
679 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
682 // Forcibly delete the window.
683 if ( topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
684 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
686 topWindow
->Close(TRUE
);
687 wxTheApp
->DeletePendingObjects();
692 wxTheApp
->SetTopWindow(NULL
);
702 #ifdef CATCH_PROGRAM_EXCEPTIONS
704 __except ( EXCEPTION_EXECUTE_HANDLER
) {
707 wxTheApp->OnFatalException();
710 // using wxLog would be unsafe here
712 _("Unrecoverable program error detected: "
713 " the application will terminate."),
715 MB_APPLMODAL
| MB_ICONSTOP
| MB_OK
);
717 ::ExitProcess(3); // the same exit code as abort()
721 #endif // CATCH_PROGRAM_EXCEPTIONS
724 // restore warning state
726 #pragma warning(default: 4715) // not all control paths return a value
731 //// Entry point for DLLs
733 int wxEntry(WXHINSTANCE hInstance
)
735 wxhInstance
= (HINSTANCE
) hInstance
;
738 // The app may have declared a global application object, but we recommend
739 // the IMPLEMENT_APP macro is used instead, which sets an initializer function
740 // for delayed, dynamic app object construction.
743 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
744 "No initializer - use IMPLEMENT_APP macro." );
746 wxTheApp
= (* wxApp::GetInitializerFunction()) ();
749 wxCHECK_MSG( wxTheApp
, 0, "You have to define an instance of wxApp!" );
752 wxTheApp
->argv
= NULL
;
754 wxTheApp
->OnInitGui();
758 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
759 if ( topWindow
&& topWindow
->GetHWND())
761 topWindow
->Show(TRUE
);
768 //// Static member initialization
770 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
776 m_wantDebugOutput
= TRUE
;
780 m_printMode
= wxPRINT_WINDOWS
;
781 m_exitOnFrameDelete
= TRUE
;
787 // Delete command-line args
789 for (i
= 0; i
< argc
; i
++)
796 bool wxApp::Initialized()
804 #ifdef _WINDLL // Assume initialized if DLL (no way of telling)
810 * Get and process a message, returning FALSE if WM_QUIT
811 * received (and also set the flag telling the app to exit the main loop)
814 bool wxApp::DoMessage()
816 BOOL rc
= ::GetMessage(&s_currentMsg
, (HWND
) NULL
, 0, 0);
826 // should never happen, but let's test for it nevertheless
827 wxLogLastError("GetMessage");
832 wxASSERT_MSG( wxThread::IsMain(),
833 wxT("only the main thread can process Windows messages") );
835 static bool s_hadGuiLock
= TRUE
;
836 static wxMsgArray s_aSavedMessages
;
838 // if a secondary thread owns is doing GUI calls, save all messages for
839 // later processing - we can't process them right now because it will
840 // lead to recursive library calls (and we're not reentrant)
841 if ( !wxGuiOwnedByMainThread() )
843 s_hadGuiLock
= FALSE
;
845 // leave out WM_COMMAND messages: too dangerous, sometimes
846 // the message will be processed twice
847 if ( !wxIsWaitingForThread() ||
848 s_currentMsg
.message
!= WM_COMMAND
)
850 s_aSavedMessages
.Add(s_currentMsg
);
857 // have we just regained the GUI lock? if so, post all of the saved
860 // FIXME of course, it's not _exactly_ the same as processing the
861 // messages normally - expect some things to break...
866 size_t count
= s_aSavedMessages
.Count();
867 for ( size_t n
= 0; n
< count
; n
++ )
869 MSG
& msg
= s_aSavedMessages
[n
];
871 if ( !ProcessMessage((WXMSG
*)&msg
) )
873 ::TranslateMessage(&msg
);
874 ::DispatchMessage(&msg
);
878 s_aSavedMessages
.Empty();
881 #endif // wxUSE_THREADS
883 // Process the message
884 if ( !ProcessMessage((WXMSG
*)&s_currentMsg
) )
886 ::TranslateMessage(&s_currentMsg
);
887 ::DispatchMessage(&s_currentMsg
);
895 * Keep trying to process messages until WM_QUIT
898 * If there are messages to be processed, they will all be
899 * processed and OnIdle will not be called.
900 * When there are no more messages, OnIdle is called.
901 * If OnIdle requests more time,
902 * it will be repeatedly called so long as there are no pending messages.
903 * A 'feature' of this is that once OnIdle has decided that no more processing
904 * is required, then it won't get processing time until further messages
905 * are processed (it'll sit in DoMessage).
908 int wxApp::MainLoop()
912 while ( m_keepGoing
)
915 wxMutexGuiLeaveOrEnter();
916 #endif // wxUSE_THREADS
918 while ( !Pending() && ProcessIdle() )
921 // a message came or no more idle processing to do
925 return s_currentMsg
.wParam
;
928 // Returns TRUE if more time is needed.
929 bool wxApp::ProcessIdle()
932 event
.SetEventObject(this);
935 return event
.MoreRequested();
938 void wxApp::ExitMainLoop()
940 // VZ: why not ::PostQuitMessage()?
944 bool wxApp::Pending()
946 return ::PeekMessage(&s_currentMsg
, 0, 0, 0, PM_NOREMOVE
) != 0;
949 void wxApp::Dispatch()
955 * Give all windows a chance to preprocess
956 * the message. Some may have accelerator tables, or have
960 bool wxApp::ProcessMessage(WXMSG
*wxmsg
)
962 MSG
*msg
= (MSG
*)wxmsg
;
963 HWND hWnd
= msg
->hwnd
;
964 wxWindow
*wndThis
= wxGetWindowFromHWND((WXHWND
)hWnd
);
967 // we must relay WM_MOUSEMOVE events to the tooltip ctrl if we want it to
968 // popup the tooltip bubbles
969 if ( wndThis
&& (msg
->message
== WM_MOUSEMOVE
) )
971 wxToolTip
*tt
= wndThis
->GetToolTip();
974 tt
->RelayEvent(wxmsg
);
977 #endif // wxUSE_TOOLTIPS
979 // Try translations first; find the youngest window with
980 // a translation table.
982 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
984 if ( wnd
->MSWTranslateMessage(wxmsg
) )
988 // Anyone for a non-translation message? Try youngest descendants first.
989 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
991 if ( wnd
->MSWProcessMessage(wxmsg
) )
998 void wxApp::OnIdle(wxIdleEvent
& event
)
1000 static bool s_inOnIdle
= FALSE
;
1002 // Avoid recursion (via ProcessEvent default case)
1008 // If there are pending events, we must process them: pending events
1009 // are either events to the threads other than main or events posted
1010 // with wxPostEvent() functions
1011 // GRG: I have moved this here so that all pending events are processed
1012 // before starting to delete any objects. This behaves better (in
1013 // particular, wrt wxPostEvent) and is coherent with wxGTK's current
1014 // behaviour. Changed Feb/2000 before 2.1.14
1015 ProcessPendingEvents();
1017 // 'Garbage' collection of windows deleted with Close().
1018 DeletePendingObjects();
1021 // flush the logged messages if any
1022 wxLog
*pLog
= wxLog::GetActiveTarget();
1023 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
1027 // Send OnIdle events to all windows
1028 if ( SendIdleEvents() )
1030 // SendIdleEvents() returns TRUE if at least one window requested more
1032 event
.RequestMore(TRUE
);
1038 // Send idle event to all top-level windows
1039 bool wxApp::SendIdleEvents()
1041 bool needMore
= FALSE
;
1043 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
1046 wxWindow
* win
= node
->GetData();
1047 if (SendIdleEvents(win
))
1049 node
= node
->GetNext();
1055 // Send idle event to window and all subwindows
1056 bool wxApp::SendIdleEvents(wxWindow
* win
)
1058 bool needMore
= FALSE
;
1061 event
.SetEventObject(win
);
1062 win
->GetEventHandler()->ProcessEvent(event
);
1064 if (event
.MoreRequested())
1067 wxNode
* node
= win
->GetChildren().First();
1070 wxWindow
* win
= (wxWindow
*) node
->Data();
1071 if (SendIdleEvents(win
))
1074 node
= node
->Next();
1079 void wxApp::DeletePendingObjects()
1081 wxNode
*node
= wxPendingDelete
.First();
1084 wxObject
*obj
= (wxObject
*)node
->Data();
1088 if (wxPendingDelete
.Member(obj
))
1091 // Deleting one object may have deleted other pending
1092 // objects, so start from beginning of list again.
1093 node
= wxPendingDelete
.First();
1097 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
1100 GetTopWindow()->Close(TRUE
);
1103 // Default behaviour: close the application with prompts. The
1104 // user can veto the close, and therefore the end session.
1105 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
1109 if (!GetTopWindow()->Close(!event
.CanVeto()))
1115 int wxApp::GetComCtl32Version()
1118 static int s_verComCtl32
= -1;
1120 wxCRIT_SECT_DECLARE(csComCtl32
);
1121 wxCRIT_SECT_LOCKER(lock
, csComCtl32
);
1123 if ( s_verComCtl32
== -1 )
1125 // initally assume no comctl32.dll at all
1129 HMODULE hModuleComCtl32
= ::GetModuleHandle(wxT("COMCTL32"));
1131 // if so, then we can check for the version
1132 if ( hModuleComCtl32
)
1134 // try to use DllGetVersion() if available in _headers_
1135 #ifdef DLLVER_PLATFORM_WINDOWS // defined in shlwapi.h
1136 DLLGETVERSIONPROC pfnDllGetVersion
= (DLLGETVERSIONPROC
)
1137 ::GetProcAddress(hModuleComCtl32
, _T("DllGetVersion"));
1138 if ( pfnDllGetVersion
)
1141 dvi
.cbSize
= sizeof(dvi
);
1143 HRESULT hr
= (*pfnDllGetVersion
)(&dvi
);
1146 wxLogApiError(_T("DllGetVersion"), hr
);
1150 // this is incompatible with _WIN32_IE values, but
1151 // compatible with the other values returned by
1152 // GetComCtl32Version()
1153 s_verComCtl32
= 100*dvi
.dwMajorVersion
+
1158 // DllGetVersion() unavailable either during compile or
1159 // run-time, try to guess the version otherwise
1160 if ( !s_verComCtl32
)
1162 // InitCommonControlsEx is unique to 4.70 and later
1163 FARPROC theProc
= ::GetProcAddress
1166 #if defined(__BORLANDC__) && (__BORLANDC__ <= 0x520)
1167 "InitCommonControlsEx"
1169 _T("InitCommonControlsEx")
1175 // not found, must be 4.00
1176 s_verComCtl32
= 400;
1180 // many symbols appeared in comctl32 4.71, could use
1181 // any of them except may be DllInstall
1182 theProc
= ::GetProcAddress
1185 #if defined(__BORLANDC__) && (__BORLANDC__ <= 0x520)
1188 _T("InitializeFlatSB")
1193 // not found, must be 4.70
1194 s_verComCtl32
= 470;
1198 // found, must be 4.71
1199 s_verComCtl32
= 471;
1206 return s_verComCtl32
;
1211 wxLogError(_("Fatal error: exiting"));
1216 // Yield to incoming messages
1219 // we don't want to process WM_QUIT from here - it should be processed in
1220 // the main event loop in order to stop it
1223 while ( PeekMessage(&msg
, (HWND
)0, 0, 0, PM_NOREMOVE
) &&
1224 msg
.message
!= WM_QUIT
)
1227 wxMutexGuiLeaveOrEnter();
1228 #endif // wxUSE_THREADS
1230 if ( !wxTheApp
->DoMessage() )
1234 // If they are pending events, we must process them.
1235 wxTheApp
->ProcessPendingEvents();
1240 //-----------------------------------------------------------------------------
1242 //-----------------------------------------------------------------------------
1246 // Send the top window a dummy message so idle handler processing will
1247 // start up again. Doing it this way ensures that the idle handler
1248 // wakes up in the right thread (see also wxWakeUpMainThread() which does
1249 // the same for the main app thread only)
1250 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
1253 if ( !::PostMessage(GetHwndOf(topWindow
), WM_NULL
, 0, 0) )
1255 // should never happen
1256 wxLogLastError("PostMessage(WM_NULL)");
1261 //-----------------------------------------------------------------------------
1264 wxApp::GetStdIcon(int which
) const
1268 case wxICON_INFORMATION
:
1269 return wxIcon("wxICON_INFO");
1271 case wxICON_QUESTION
:
1272 return wxIcon("wxICON_QUESTION");
1274 case wxICON_EXCLAMATION
:
1275 return wxIcon("wxICON_WARNING");
1278 wxFAIL_MSG(wxT("requested non existent standard icon"));
1279 // still fall through
1282 return wxIcon("wxICON_ERROR");
1286 // For some reason, with MSVC++ 1.5, WinMain isn't linked in properly
1287 // if in a separate file. So include it here to ensure it's linked.
1288 #if (defined(__VISUALC__) && !defined(__WIN32__)) || (defined(__GNUWIN32__) && !defined(__TWIN32__) && !defined(WXMAKINGDLL))