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"
22 #pragma implementation "appbase.h"
25 // For compilers that support precompilation, includes "wx.h".
26 #include "wx/wxprec.h"
28 #if defined(__BORLANDC__)
36 #include "wx/gdicmn.h"
39 #include "wx/cursor.h"
41 #include "wx/palette.h"
43 #include "wx/dialog.h"
44 #include "wx/msgdlg.h"
46 #include "wx/dynarray.h"
47 # include "wx/wxchar.h"
52 #include "wx/module.h"
54 #include "wx/msw/private.h"
57 #include "wx/thread.h"
59 // define the array of MSG strutures
60 WX_DECLARE_OBJARRAY(MSG
, wxMsgArray
);
62 #include "wx/arrimpl.cpp"
64 WX_DEFINE_OBJARRAY(wxMsgArray
);
65 #endif // wxUSE_THREADS
67 #if wxUSE_WX_RESOURCES
68 #include "wx/resource.h"
71 // OLE is used for drag-and-drop, clipboard, OLE Automation...
72 #if defined(__GNUWIN32__) || defined(__SC__) || defined(__SALFORDC__)
76 #endif // broken compilers
85 #if (defined(__WIN95__) && !defined(__GNUWIN32__)) || defined(__TWIN32__) || defined(wxUSE_NORLANDER_HEADERS)
89 #include "wx/msw/msvcrt.h"
91 // ---------------------------------------------------------------------------
93 // ---------------------------------------------------------------------------
95 extern wxChar
*wxBuffer
;
96 extern wxChar
*wxOsVersion
;
97 extern wxList
*wxWinHandleList
;
98 extern wxList WXDLLEXPORT wxPendingDelete
;
100 extern wxList
*wxPendingEvents
;
101 extern wxCriticalSection
*wxPendingEventsLocker
;
103 extern void wxSetKeyboardHook(bool doIt
);
104 extern wxCursor
*g_globalCursor
;
106 HINSTANCE wxhInstance
= 0;
108 wxApp
*wxTheApp
= NULL
;
110 // FIXME why not const? and not static?
112 // NB: all "NoRedraw" classes must have the same names as the "normal" classes
113 // with NR suffix - wxWindow::MSWCreate() supposes this
114 wxChar wxFrameClassName
[] = _T("wxFrameClass");
115 wxChar wxFrameClassNameNoRedraw
[] = _T("wxFrameClassNR");
116 wxChar wxMDIFrameClassName
[] = _T("wxMDIFrameClass");
117 wxChar wxMDIFrameClassNameNoRedraw
[] = _T("wxMDIFrameClassNR");
118 wxChar wxMDIChildFrameClassName
[] = _T("wxMDIChildFrameClass");
119 wxChar wxMDIChildFrameClassNameNoRedraw
[] = _T("wxMDIChildFrameClassNR");
120 wxChar wxPanelClassName
[] = _T("wxPanelClass");
121 wxChar wxCanvasClassName
[] = _T("wxCanvasClass");
123 HICON wxSTD_FRAME_ICON
= (HICON
) NULL
;
124 HICON wxSTD_MDICHILDFRAME_ICON
= (HICON
) NULL
;
125 HICON wxSTD_MDIPARENTFRAME_ICON
= (HICON
) NULL
;
127 HICON wxDEFAULT_FRAME_ICON
= (HICON
) NULL
;
128 HICON wxDEFAULT_MDICHILDFRAME_ICON
= (HICON
) NULL
;
129 HICON wxDEFAULT_MDIPARENTFRAME_ICON
= (HICON
) NULL
;
131 HBRUSH wxDisableButtonBrush
= (HBRUSH
) 0;
133 LRESULT APIENTRY
wxWndProc(HWND
, UINT
, WPARAM
, LPARAM
);
135 #if defined(__WIN95__) && !defined(__TWIN32__)
136 #define wxUSE_RICHEDIT 1
138 #define wxUSE_RICHEDIT 0
142 static HINSTANCE gs_hRichEdit
= (HINSTANCE
) NULL
;
145 // ===========================================================================
147 // ===========================================================================
149 // ---------------------------------------------------------------------------
151 // ---------------------------------------------------------------------------
153 #if !USE_SHARED_LIBRARY
154 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
156 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
157 EVT_IDLE(wxApp::OnIdle
)
158 EVT_END_SESSION(wxApp::OnEndSession
)
159 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession
)
164 bool wxApp::Initialize()
166 // Some people may wish to use this, but
167 // probably it shouldn't be here by default.
169 // wxRedirectIOToConsole();
172 wxBuffer
= new wxChar
[1500]; // FIXME
174 wxClassInfo::InitializeClasses();
177 wxGetResource(_T("wxWindows"), _T("OsVersion"), &wxOsVersion
);
180 // I'm annoyed ... I don't know where to put this and I don't want to
181 // create a module for that as it's part of the core.
183 wxPendingEvents
= new wxList();
184 wxPendingEventsLocker
= new wxCriticalSection();
187 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
188 wxTheColourDatabase
->Initialize();
190 wxInitializeStockLists();
191 wxInitializeStockObjects();
193 #if wxUSE_WX_RESOURCES
194 wxInitializeResourceSystem();
197 wxBitmap::InitStandardHandlers();
199 #if defined(__WIN95__)
200 InitCommonControls();
203 gs_hRichEdit
= LoadLibrary(_T("RICHED32.DLL"));
205 if (gs_hRichEdit
== (HINSTANCE
) NULL
)
207 wxLogError(_("Could not initialise Rich Edit DLL"));
209 #endif // wxUSE_RICHEDIT
216 // for OLE, enlarge message queue to be as large as possible
218 while (!SetMessageQueue(iMsg
) && (iMsg
-= 8))
222 // we need to initialize OLE library
223 if ( FAILED(::OleInitialize(NULL
)) )
224 wxLogError(_("Cannot initialize OLE"));
228 if (!Ctl3dRegister(wxhInstance
))
229 wxLogError(_T("Cannot register CTL3D"));
231 Ctl3dAutoSubclass(wxhInstance
);
234 g_globalCursor
= new wxCursor
;
236 wxSTD_FRAME_ICON
= LoadIcon(wxhInstance
, _T("wxSTD_FRAME"));
237 wxSTD_MDIPARENTFRAME_ICON
= LoadIcon(wxhInstance
, _T("wxSTD_MDIPARENTFRAME"));
238 wxSTD_MDICHILDFRAME_ICON
= LoadIcon(wxhInstance
, _T("wxSTD_MDICHILDFRAME"));
240 wxDEFAULT_FRAME_ICON
= LoadIcon(wxhInstance
, _T("wxDEFAULT_FRAME"));
241 wxDEFAULT_MDIPARENTFRAME_ICON
= LoadIcon(wxhInstance
, _T("wxDEFAULT_MDIPARENTFRAME"));
242 wxDEFAULT_MDICHILDFRAME_ICON
= LoadIcon(wxhInstance
, _T("wxDEFAULT_MDICHILDFRAME"));
244 RegisterWindowClasses();
246 // Create the brush for disabling bitmap buttons
249 lb
.lbStyle
= BS_PATTERN
;
250 lb
.lbHatch
= (int)LoadBitmap( wxhInstance
, _T("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 bool wxApp::RegisterWindowClasses()
287 // for each class we register one with CS_(V|H)REDRAW style and one
288 // without for windows created with wxNO_FULL_REDRAW_ON_REPAINT flag
289 static const long styleNormal
= CS_HREDRAW
| CS_VREDRAW
| CS_DBLCLKS
;
290 static const long styleNoRedraw
= CS_DBLCLKS
;
292 // the fields which are common to all classes
293 wndclass
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
294 wndclass
.cbClsExtra
= 0;
295 wndclass
.cbWndExtra
= sizeof( DWORD
); // VZ: what is this DWORD used for?
296 wndclass
.hInstance
= wxhInstance
;
297 wndclass
.hIcon
= (HICON
) NULL
;
298 wndclass
.hCursor
= ::LoadCursor((HINSTANCE
)NULL
, IDC_ARROW
);
299 wndclass
.lpszMenuName
= NULL
;
301 // Register the frame window class.
302 wndclass
.hbrBackground
= (HBRUSH
)(COLOR_APPWORKSPACE
+ 1);
303 wndclass
.style
= styleNormal
;
304 wndclass
.lpszClassName
= wxFrameClassName
;
306 if ( !RegisterClass(&wndclass
) )
308 wxLogLastError("RegisterClass(frame)");
314 wndclass
.lpszClassName
= wxFrameClassNameNoRedraw
;
315 wndclass
.style
= styleNoRedraw
;
317 if ( !RegisterClass(&wndclass
) )
319 wxLogLastError("RegisterClass(no redraw frame)");
324 // Register the MDI frame window class.
325 wndclass
.hbrBackground
= (HBRUSH
)NULL
; // paint MDI frame ourselves
326 wndclass
.lpszClassName
= wxMDIFrameClassNameNoRedraw
;
328 if ( !RegisterClass(&wndclass
) )
330 wxLogLastError("RegisterClass(MDI parent)");
335 // "no redraw" MDI frame
336 wndclass
.lpszClassName
= wxMDIFrameClassName
;
337 wndclass
.style
= styleNoRedraw
;
339 if ( !RegisterClass(&wndclass
) )
341 wxLogLastError("RegisterClass(no redraw MDI parent frame)");
346 // Register the MDI child frame window class.
347 wndclass
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+ 1);
348 wndclass
.lpszClassName
= wxMDIChildFrameClassName
;
350 if ( !RegisterClass(&wndclass
) )
352 wxLogLastError("RegisterClass(MDI child)");
357 // "no redraw" MDI child frame
358 wndclass
.lpszClassName
= wxMDIChildFrameClassNameNoRedraw
;
359 wndclass
.style
= styleNoRedraw
;
361 if ( !RegisterClass(&wndclass
) )
363 wxLogLastError("RegisterClass(no redraw MDI child)");
368 // Register the panel window class.
369 wndclass
.hbrBackground
= (HBRUSH
) GetStockObject( LTGRAY_BRUSH
);
370 wndclass
.lpszClassName
= wxPanelClassName
;
372 if ( !RegisterClass(&wndclass
) )
374 wxLogLastError("RegisterClass(panel)");
379 // Register the canvas and textsubwindow class name
380 wndclass
.hbrBackground
= (HBRUSH
)NULL
;
381 wndclass
.lpszClassName
= wxCanvasClassName
;
383 if ( !RegisterClass(&wndclass
) )
385 wxLogLastError("RegisterClass(canvas)");
393 // ---------------------------------------------------------------------------
394 // Convert Windows to argc, argv style
395 // ---------------------------------------------------------------------------
397 void wxApp::ConvertToStandardCommandArgs(char* lpCmdLine
)
401 wxString
cmdLine(lpCmdLine
);
404 // Get application name
405 wxChar name
[260]; // 260 is MAX_PATH value from windef.h
406 ::GetModuleFileName(wxhInstance
, name
, WXSIZEOF(name
));
411 wxStrcpy(name
, wxFileNameFromPath(name
));
412 wxStripExtension(name
);
413 wxTheApp
->SetAppName(name
);
416 // Treat strings enclosed in double-quotes as single arguments
418 int len
= cmdLine
.Length();
422 while ((i
< len
) && wxIsspace(cmdLine
.GetChar(i
)))
427 if (cmdLine
.GetChar(i
) == _T('"')) // We found the start of a string
431 while ((i
< len
) && (cmdLine
.GetChar(i
) != _T('"')))
434 wxString
arg(cmdLine
.Mid(first
, (i
- first
)));
440 i
++; // Skip past 2nd quote
442 else // Unquoted argument
445 while ((i
< len
) && !wxIsspace(cmdLine
.GetChar(i
)))
448 wxString
arg(cmdLine
.Mid(first
, (i
- first
)));
456 wxTheApp
->argv
= new wxChar
*[count
+ 1];
457 for (i
= 0; i
< count
; i
++)
459 wxString
arg(args
[i
]);
460 wxTheApp
->argv
[i
] = copystring((const wxChar
*)arg
);
462 wxTheApp
->argv
[count
] = NULL
; // argv[] is a NULL-terminated list
463 wxTheApp
->argc
= count
;
466 //// Cleans up any wxWindows internal structures left lying around
468 void wxApp::CleanUp()
473 // flush the logged messages if any and install a 'safer' log target: the
474 // default one (wxLogGui) can't be used after the resources are freed just
475 // below and the user suppliedo ne might be even more unsafe (using any
476 // wxWindows GUI function is unsafe starting from now)
477 wxLog::DontCreateOnDemand();
479 // this will flush the old messages if any
480 delete wxLog::SetActiveTarget(new wxLogStderr
);
483 // One last chance for pending objects to be cleaned up
484 wxTheApp
->DeletePendingObjects();
486 wxModule::CleanUpModules();
488 #if wxUSE_WX_RESOURCES
489 wxCleanUpResourceSystem();
491 // wxDefaultResourceTable->ClearTable();
494 // Indicate that the cursor can be freed, so that cursor won't be deleted
495 // by deleting the bitmap list before g_globalCursor goes out of scope
496 // (double deletion of the cursor).
497 wxSetCursor(wxNullCursor
);
498 delete g_globalCursor
;
499 g_globalCursor
= NULL
;
501 wxDeleteStockObjects();
503 // Destroy all GDI lists, etc.
504 wxDeleteStockLists();
506 delete wxTheColourDatabase
;
507 wxTheColourDatabase
= NULL
;
509 wxBitmap::CleanUpHandlers();
514 //// WINDOWS-SPECIFIC CLEANUP
516 wxSetKeyboardHook(FALSE
);
521 if (gs_hRichEdit
!= (HINSTANCE
) NULL
)
522 FreeLibrary(gs_hRichEdit
);
531 if (wxSTD_FRAME_ICON
)
532 DestroyIcon(wxSTD_FRAME_ICON
);
533 if (wxSTD_MDICHILDFRAME_ICON
)
534 DestroyIcon(wxSTD_MDICHILDFRAME_ICON
);
535 if (wxSTD_MDIPARENTFRAME_ICON
)
536 DestroyIcon(wxSTD_MDIPARENTFRAME_ICON
);
538 if (wxDEFAULT_FRAME_ICON
)
539 DestroyIcon(wxDEFAULT_FRAME_ICON
);
540 if (wxDEFAULT_MDICHILDFRAME_ICON
)
541 DestroyIcon(wxDEFAULT_MDICHILDFRAME_ICON
);
542 if (wxDEFAULT_MDIPARENTFRAME_ICON
)
543 DestroyIcon(wxDEFAULT_MDIPARENTFRAME_ICON
);
545 if ( wxDisableButtonBrush
)
546 ::DeleteObject( wxDisableButtonBrush
);
553 Ctl3dUnregister(wxhInstance
);
557 delete wxWinHandleList
;
559 // GL: I'm annoyed ... I don't know where to put this and I don't want to
560 // create a module for that as it's part of the core.
562 delete wxPendingEvents
;
563 delete wxPendingEventsLocker
;
564 // If we don't do the following, we get an apparent memory leak.
565 ((wxEvtHandler
&) wxDefaultValidator
).ClearEventLocker();
568 wxClassInfo::CleanUpClasses();
573 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
574 // At this point we want to check if there are any memory
575 // blocks that aren't part of the wxDebugContext itself,
576 // as a special case. Then when dumping we need to ignore
577 // wxDebugContext, too.
578 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
580 wxLogDebug(_T("There were memory leaks."));
581 wxDebugContext::Dump();
582 wxDebugContext::PrintStatistics();
584 // wxDebugContext::SetStream(NULL, NULL);
588 // do it as the very last thing because everything else can log messages
589 delete wxLog::SetActiveTarget(NULL
);
593 #if !defined(_WINDLL) || (defined(_WINDLL) && defined(WXMAKINGDLL))
595 // temporarily disable this warning which would be generated in release builds
598 #pragma warning(disable: 4715) // not all control paths return a value
601 //// Main wxWindows entry point
602 int wxEntry(WXHINSTANCE hInstance
,
603 WXHINSTANCE
WXUNUSED(hPrevInstance
),
608 // do check for memory leaks on program exit
609 // (another useful flag is _CRTDBG_DELAY_FREE_MEM_DF which doesn't free
610 // deallocated memory which may be used to simulate low-memory condition)
611 wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF
);
613 // take everything into a try-except block in release build
614 // FIXME other compilers must support Win32 SEH (structured exception
615 // handling) too, just find the appropriate keyword in their docs!
616 // Please note that it's _not_ the same as C++ exceptions!
617 #if !defined(__WXDEBUG__) && defined(__VISUALC__)
618 #define CATCH_PROGRAM_EXCEPTIONS
622 #undef CATCH_PROGRAM_EXCEPTIONS
625 wxhInstance
= (HINSTANCE
) hInstance
;
627 if (!wxApp::Initialize())
630 // create the application object or ensure that one already exists
633 // The app may have declared a global application object, but we recommend
634 // the IMPLEMENT_APP macro is used instead, which sets an initializer
635 // function for delayed, dynamic app object construction.
636 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
637 _T("No initializer - use IMPLEMENT_APP macro.") );
639 wxTheApp
= (*wxApp::GetInitializerFunction()) ();
642 wxCHECK_MSG( wxTheApp
, 0, _T("You have to define an instance of wxApp!") );
644 // save the WinMain() parameters
645 wxTheApp
->ConvertToStandardCommandArgs(lpCmdLine
);
646 wxTheApp
->m_nCmdShow
= nCmdShow
;
648 // GUI-specific initialisation. In fact on Windows we don't have any,
649 // but this call is provided for compatibility across platforms.
650 wxTheApp
->OnInitGui();
654 if ( wxTheApp
->OnInit() )
658 retValue
= wxTheApp
->OnRun();
661 // We want to initialize, but not run or exit immediately.
664 //else: app initialization failed, so we skipped OnRun()
666 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
669 // Forcibly delete the window.
670 if ( topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
671 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
673 topWindow
->Close(TRUE
);
674 wxTheApp
->DeletePendingObjects();
679 wxTheApp
->SetTopWindow(NULL
);
689 #ifdef CATCH_PROGRAM_EXCEPTIONS
691 __except ( EXCEPTION_EXECUTE_HANDLER
) {
694 wxTheApp->OnFatalException();
697 // using wxLog would be unsafe here
699 _("Unrecoverable program error detected: "
700 " the application will terminate."),
702 MB_APPLMODAL
| MB_ICONSTOP
| MB_OK
);
704 ::ExitProcess(3); // the same exit code as abort()
708 #endif // CATCH_PROGRAM_EXCEPTIONS
711 // restore warning state
713 #pragma warning(default: 4715) // not all control paths return a value
718 //// Entry point for DLLs
720 int wxEntry(WXHINSTANCE hInstance
)
722 wxhInstance
= (HINSTANCE
) hInstance
;
725 // The app may have declared a global application object, but we recommend
726 // the IMPLEMENT_APP macro is used instead, which sets an initializer function
727 // for delayed, dynamic app object construction.
730 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
731 "No initializer - use IMPLEMENT_APP macro." );
733 wxTheApp
= (* wxApp::GetInitializerFunction()) ();
736 wxCHECK_MSG( wxTheApp
, 0, "You have to define an instance of wxApp!" );
739 wxTheApp
->argv
= NULL
;
741 wxTheApp
->OnInitGui();
745 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
746 if ( topWindow
&& topWindow
->GetHWND())
748 topWindow
->Show(TRUE
);
755 //// Static member initialization
757 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
763 m_wantDebugOutput
= TRUE
;
767 m_printMode
= wxPRINT_WINDOWS
;
768 m_exitOnFrameDelete
= TRUE
;
774 // Delete command-line args
776 for (i
= 0; i
< argc
; i
++)
783 bool wxApp::Initialized()
791 #ifdef _WINDLL // Assume initialized if DLL (no way of telling)
797 * Get and process a message, returning FALSE if WM_QUIT
798 * received (and also set the flag telling the app to exit the main loop)
801 bool wxApp::DoMessage()
803 BOOL rc
= ::GetMessage(&s_currentMsg
, (HWND
) NULL
, 0, 0);
813 // should never happen, but let's test for it nevertheless
814 wxLogLastError("GetMessage");
819 wxASSERT_MSG( wxThread::IsMain(),
820 _T("only the main thread can process Windows messages") );
822 static bool s_hadGuiLock
= TRUE
;
823 static wxMsgArray s_aSavedMessages
;
825 // if a secondary thread owns is doing GUI calls, save all messages for
826 // later processing - we can't process them right now because it will
827 // lead to recursive library calls (and we're not reentrant)
828 if ( !wxGuiOwnedByMainThread() )
830 s_hadGuiLock
= FALSE
;
832 // leave out WM_COMMAND messages: too dangerous, sometimes
833 // the message will be processed twice
834 if ( !wxIsWaitingForThread() ||
835 s_currentMsg
.message
!= WM_COMMAND
)
837 s_aSavedMessages
.Add(s_currentMsg
);
844 // have we just regained the GUI lock? if so, post all of the saved
847 // FIXME of course, it's not _exactly_ the same as processing the
848 // messages normally - expect some things to break...
853 size_t count
= s_aSavedMessages
.Count();
854 for ( size_t n
= 0; n
< count
; n
++ )
856 MSG
& msg
= s_aSavedMessages
[n
];
858 if ( !ProcessMessage((WXMSG
*)&msg
) )
860 ::TranslateMessage(&msg
);
861 ::DispatchMessage(&msg
);
865 s_aSavedMessages
.Empty();
868 #endif // wxUSE_THREADS
870 // Process the message
871 if ( !ProcessMessage((WXMSG
*)&s_currentMsg
) )
873 ::TranslateMessage(&s_currentMsg
);
874 ::DispatchMessage(&s_currentMsg
);
882 * Keep trying to process messages until WM_QUIT
885 * If there are messages to be processed, they will all be
886 * processed and OnIdle will not be called.
887 * When there are no more messages, OnIdle is called.
888 * If OnIdle requests more time,
889 * it will be repeatedly called so long as there are no pending messages.
890 * A 'feature' of this is that once OnIdle has decided that no more processing
891 * is required, then it won't get processing time until further messages
892 * are processed (it'll sit in DoMessage).
895 int wxApp::MainLoop()
899 while ( m_keepGoing
)
902 wxMutexGuiLeaveOrEnter();
903 #endif // wxUSE_THREADS
905 while ( !::PeekMessage(&s_currentMsg
, 0, 0, 0, PM_NOREMOVE
) &&
914 return s_currentMsg
.wParam
;
917 // Returns TRUE if more time is needed.
918 bool wxApp::ProcessIdle()
921 event
.SetEventObject(this);
924 return event
.MoreRequested();
928 void wxApp::ProcessPendingEvents()
930 wxNode
*node
= wxPendingEvents
->First();
931 wxCriticalSectionLocker
locker(*wxPendingEventsLocker
);
935 wxEvtHandler
*handler
= (wxEvtHandler
*)node
->Data();
937 handler
->ProcessPendingEvents();
940 node
= wxPendingEvents
->First();
946 void wxApp::ExitMainLoop()
951 bool wxApp::Pending()
953 return (::PeekMessage(&s_currentMsg
, 0, 0, 0, PM_NOREMOVE
) != 0);
956 void wxApp::Dispatch()
962 * Give all windows a chance to preprocess
963 * the message. Some may have accelerator tables, or have
967 bool wxApp::ProcessMessage(WXMSG
*wxmsg
)
969 MSG
*msg
= (MSG
*)wxmsg
;
970 HWND hWnd
= msg
->hwnd
;
971 wxWindow
*wndThis
= wxFindWinFromHandle((WXHWND
)hWnd
), *wnd
;
973 // for some composite controls (like a combobox), wndThis might be NULL
974 // because the subcontrol is not a wxWindow, but only the control itself
975 // is - try to catch this case
976 while ( hWnd
&& !wndThis
)
978 hWnd
= ::GetParent(hWnd
);
979 wndThis
= wxFindWinFromHandle((WXHWND
)hWnd
);
982 // Try translations first; find the youngest window with
983 // 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.
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()))
1111 int wxApp::GetComCtl32Version() const
1113 // have we loaded COMCTL32 yet?
1114 HMODULE theModule
= ::GetModuleHandle(_T("COMCTL32"));
1117 // if so, then we can check for the version
1120 // InitCommonControlsEx is unique to 4.7 and later
1121 FARPROC theProc
= ::GetProcAddress(theModule
, "InitCommonControlsEx");
1124 { // not found, must be 4.00
1129 // The following symbol are unique to 4.71
1131 // FlatSB_EnableScrollBar FlatSB_GetScrollInfo FlatSB_GetScrollPos
1132 // FlatSB_GetScrollProp FlatSB_GetScrollRange FlatSB_SetScrollInfo
1133 // FlatSB_SetScrollPos FlatSB_SetScrollProp FlatSB_SetScrollRange
1134 // FlatSB_ShowScrollBar
1135 // _DrawIndirectImageList _DuplicateImageList
1137 // UninitializeFlatSB
1138 // we could check for any of these - I chose DllInstall
1139 FARPROC theProc
= ::GetProcAddress(theModule
, "DllInstall");
1142 // not found, must be 4.70
1146 { // found, must be 4.71
1156 wxLogError(_("Fatal error: exiting"));
1161 // Yield to incoming messages
1165 // We want to go back to the main message loop
1166 // if we see a WM_QUIT. (?)
1168 while (PeekMessage(&msg
, (HWND
)NULL
, 0, 0, PM_NOREMOVE
) && msg
.message
!= WM_QUIT
)
1170 while (PeekMessage(&msg
, NULL
, 0, 0, PM_NOREMOVE
) && msg
.message
!= WM_QUIT
)
1173 if ( !wxTheApp
->DoMessage() )
1176 // If they are pending events, we must process them.
1178 wxTheApp
->ProcessPendingEvents();
1185 wxApp::GetStdIcon(int which
) const
1189 case wxICON_INFORMATION
:
1190 return wxIcon("wxICON_INFO");
1192 case wxICON_QUESTION
:
1193 return wxIcon("wxICON_QUESTION");
1195 case wxICON_EXCLAMATION
:
1196 return wxIcon("wxICON_WARNING");
1199 wxFAIL_MSG(_T("requested non existent standard icon"));
1200 // still fall through
1203 return wxIcon("wxICON_ERROR");
1208 HINSTANCE
wxGetInstance()
1213 void wxSetInstance(HINSTANCE hInst
)
1215 wxhInstance
= hInst
;
1218 // For some reason, with MSVC++ 1.5, WinMain isn't linked in properly
1219 // if in a separate file. So include it here to ensure it's linked.
1220 #if (defined(__VISUALC__) && !defined(__WIN32__)) || (defined(__GNUWIN32__) && !defined(__TWIN32__))