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__)
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?
111 wxChar wxFrameClassName
[] = _T("wxFrameClass");
112 wxChar wxMDIFrameClassName
[] = _T("wxMDIFrameClass");
113 wxChar wxMDIChildFrameClassName
[] = _T("wxMDIChildFrameClass");
114 wxChar wxPanelClassName
[] = _T("wxPanelClass");
115 wxChar wxCanvasClassName
[] = _T("wxCanvasClass");
117 HICON wxSTD_FRAME_ICON
= (HICON
) NULL
;
118 HICON wxSTD_MDICHILDFRAME_ICON
= (HICON
) NULL
;
119 HICON wxSTD_MDIPARENTFRAME_ICON
= (HICON
) NULL
;
121 HICON wxDEFAULT_FRAME_ICON
= (HICON
) NULL
;
122 HICON wxDEFAULT_MDICHILDFRAME_ICON
= (HICON
) NULL
;
123 HICON wxDEFAULT_MDIPARENTFRAME_ICON
= (HICON
) NULL
;
125 HBRUSH wxDisableButtonBrush
= (HBRUSH
) 0;
127 LRESULT APIENTRY
wxWndProc(HWND
, UINT
, WPARAM
, LPARAM
);
129 #if defined(__WIN95__) && !defined(__TWIN32__)
130 #define wxUSE_RICHEDIT 1
132 #define wxUSE_RICHEDIT 0
136 static HINSTANCE gs_hRichEdit
= (HINSTANCE
) NULL
;
139 // ===========================================================================
141 // ===========================================================================
143 // ---------------------------------------------------------------------------
145 // ---------------------------------------------------------------------------
147 #if !USE_SHARED_LIBRARY
148 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
150 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
151 EVT_IDLE(wxApp::OnIdle
)
152 EVT_END_SESSION(wxApp::OnEndSession
)
153 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession
)
158 bool wxApp::Initialize()
160 // Some people may wish to use this, but
161 // probably it shouldn't be here by default.
163 // wxRedirectIOToConsole();
166 wxBuffer
= new wxChar
[1500]; // FIXME
168 wxClassInfo::InitializeClasses();
171 wxGetResource(_T("wxWindows"), _T("OsVersion"), &wxOsVersion
);
174 // I'm annoyed ... I don't know where to put this and I don't want to
175 // create a module for that as it's part of the core.
177 wxPendingEvents
= new wxList();
178 wxPendingEventsLocker
= new wxCriticalSection();
181 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
182 wxTheColourDatabase
->Initialize();
184 wxInitializeStockLists();
185 wxInitializeStockObjects();
187 #if wxUSE_WX_RESOURCES
188 wxInitializeResourceSystem();
191 wxBitmap::InitStandardHandlers();
193 #if defined(__WIN95__)
194 InitCommonControls();
197 gs_hRichEdit
= LoadLibrary(_T("RICHED32.DLL"));
199 if (gs_hRichEdit
== (HINSTANCE
) NULL
)
201 wxLogError(_("Could not initialise Rich Edit DLL"));
203 #endif // wxUSE_RICHEDIT
210 // for OLE, enlarge message queue to be as large as possible
212 while (!SetMessageQueue(iMsg
) && (iMsg
-= 8))
216 // we need to initialize OLE library
217 if ( FAILED(::OleInitialize(NULL
)) )
218 wxLogError(_("Cannot initialize OLE"));
222 if (!Ctl3dRegister(wxhInstance
))
223 wxLogError(_T("Cannot register CTL3D"));
225 Ctl3dAutoSubclass(wxhInstance
);
228 g_globalCursor
= new wxCursor
;
230 wxSTD_FRAME_ICON
= LoadIcon(wxhInstance
, _T("wxSTD_FRAME"));
231 wxSTD_MDIPARENTFRAME_ICON
= LoadIcon(wxhInstance
, _T("wxSTD_MDIPARENTFRAME"));
232 wxSTD_MDICHILDFRAME_ICON
= LoadIcon(wxhInstance
, _T("wxSTD_MDICHILDFRAME"));
234 wxDEFAULT_FRAME_ICON
= LoadIcon(wxhInstance
, _T("wxDEFAULT_FRAME"));
235 wxDEFAULT_MDIPARENTFRAME_ICON
= LoadIcon(wxhInstance
, _T("wxDEFAULT_MDIPARENTFRAME"));
236 wxDEFAULT_MDICHILDFRAME_ICON
= LoadIcon(wxhInstance
, _T("wxDEFAULT_MDICHILDFRAME"));
238 RegisterWindowClasses();
240 // Create the brush for disabling bitmap buttons
243 lb
.lbStyle
= BS_PATTERN
;
244 lb
.lbHatch
= (int)LoadBitmap( wxhInstance
, _T("wxDISABLE_BUTTON_BITMAP") );
247 wxDisableButtonBrush
= ::CreateBrushIndirect( & lb
);
248 ::DeleteObject( (HGDIOBJ
)lb
.lbHatch
);
250 //else: wxWindows resources are probably not linked in
256 wxWinHandleList
= new wxList(wxKEY_INTEGER
);
258 // This is to foil optimizations in Visual C++ that throw out dummy.obj.
259 // PLEASE DO NOT ALTER THIS.
260 #if defined(__VISUALC__) && !defined(WXMAKINGDLL)
261 extern char wxDummyChar
;
262 if (wxDummyChar
) wxDummyChar
++;
265 wxSetKeyboardHook(TRUE
);
267 wxModule::RegisterModules();
268 if (!wxModule::InitializeModules())
273 // ---------------------------------------------------------------------------
274 // RegisterWindowClasses
275 // ---------------------------------------------------------------------------
277 bool wxApp::RegisterWindowClasses()
281 // the fields which are common to all classes
282 wndclass
.style
= CS_HREDRAW
| CS_VREDRAW
| CS_DBLCLKS
;
283 wndclass
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
284 wndclass
.cbClsExtra
= 0;
285 wndclass
.cbWndExtra
= sizeof( DWORD
); // what is this DWORD used for?
286 wndclass
.hInstance
= wxhInstance
;
287 wndclass
.hIcon
= (HICON
) NULL
;
288 wndclass
.hCursor
= ::LoadCursor((HINSTANCE
)NULL
, IDC_ARROW
);
289 wndclass
.lpszMenuName
= NULL
;
291 // Register the frame window class.
292 wndclass
.hbrBackground
= (HBRUSH
)(COLOR_APPWORKSPACE
+ 1);
293 #ifdef _MULTIPLE_INSTANCES
294 sprintf( wxFrameClassName
,"wxFrameClass%d", wxhInstance
);
296 wndclass
.lpszClassName
= wxFrameClassName
;
298 if ( !RegisterClass(&wndclass
) )
300 wxLogLastError("RegisterClass(frame)");
305 // Register the MDI frame window class.
306 wndclass
.hbrBackground
= (HBRUSH
)NULL
; // paint MDI frame ourselves
307 wndclass
.lpszClassName
= wxMDIFrameClassName
;
309 if ( !RegisterClass(&wndclass
) )
311 wxLogLastError("RegisterClass(MDI parent)");
316 // Register the MDI child frame window class.
317 wndclass
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+ 1);
318 wndclass
.lpszClassName
= wxMDIChildFrameClassName
;
320 if ( !RegisterClass(&wndclass
) )
322 wxLogLastError("RegisterClass(MDI child)");
327 // Register the panel window class.
328 wndclass
.hbrBackground
= (HBRUSH
) GetStockObject( LTGRAY_BRUSH
);
329 wndclass
.lpszClassName
= wxPanelClassName
;
331 if ( !RegisterClass(&wndclass
) )
333 wxLogLastError("RegisterClass(panel)");
338 // Register the canvas and textsubwindow class name
339 wndclass
.hbrBackground
= (HBRUSH
)NULL
;
340 wndclass
.lpszClassName
= wxCanvasClassName
;
342 if ( !RegisterClass(&wndclass
) )
344 wxLogLastError("RegisterClass(canvas)");
352 // ---------------------------------------------------------------------------
353 // Convert Windows to argc, argv style
354 // ---------------------------------------------------------------------------
356 void wxApp::ConvertToStandardCommandArgs(char* lpCmdLine
)
360 wxString
cmdLine(lpCmdLine
);
363 // Get application name
364 wxChar name
[260]; // 260 is MAX_PATH value from windef.h
365 ::GetModuleFileName(wxhInstance
, name
, WXSIZEOF(name
));
370 wxStrcpy(name
, wxFileNameFromPath(name
));
371 wxStripExtension(name
);
372 wxTheApp
->SetAppName(name
);
375 // Treat strings enclosed in double-quotes as single arguments
377 int len
= cmdLine
.Length();
381 while ((i
< len
) && wxIsspace(cmdLine
.GetChar(i
)))
386 if (cmdLine
.GetChar(i
) == _T('"')) // We found the start of a string
390 while ((i
< len
) && (cmdLine
.GetChar(i
) != _T('"')))
393 wxString
arg(cmdLine
.Mid(first
, (i
- first
)));
399 i
++; // Skip past 2nd quote
401 else // Unquoted argument
404 while ((i
< len
) && !wxIsspace(cmdLine
.GetChar(i
)))
407 wxString
arg(cmdLine
.Mid(first
, (i
- first
)));
415 wxTheApp
->argv
= new wxChar
*[count
+ 1];
416 for (i
= 0; i
< count
; i
++)
418 wxString
arg(args
[i
]);
419 wxTheApp
->argv
[i
] = copystring((const wxChar
*)arg
);
421 wxTheApp
->argv
[count
] = NULL
; // argv[] is a NULL-terminated list
422 wxTheApp
->argc
= count
;
425 //// Cleans up any wxWindows internal structures left lying around
427 void wxApp::CleanUp()
432 // flush the logged messages if any and install a 'safer' log target: the
433 // default one (wxLogGui) can't be used after the resources are freed just
434 // below and the user suppliedo ne might be even more unsafe (using any
435 // wxWindows GUI function is unsafe starting from now)
436 wxLog::DontCreateOnDemand();
438 // this will flush the old messages if any
439 delete wxLog::SetActiveTarget(new wxLogStderr
);
442 // One last chance for pending objects to be cleaned up
443 wxTheApp
->DeletePendingObjects();
445 wxModule::CleanUpModules();
447 #if wxUSE_WX_RESOURCES
448 wxCleanUpResourceSystem();
450 // wxDefaultResourceTable->ClearTable();
453 // Indicate that the cursor can be freed, so that cursor won't be deleted
454 // by deleting the bitmap list before g_globalCursor goes out of scope
455 // (double deletion of the cursor).
456 wxSetCursor(wxNullCursor
);
457 delete g_globalCursor
;
458 g_globalCursor
= NULL
;
460 wxDeleteStockObjects();
462 // Destroy all GDI lists, etc.
463 wxDeleteStockLists();
465 delete wxTheColourDatabase
;
466 wxTheColourDatabase
= NULL
;
468 wxBitmap::CleanUpHandlers();
473 //// WINDOWS-SPECIFIC CLEANUP
475 wxSetKeyboardHook(FALSE
);
480 if (gs_hRichEdit
!= (HINSTANCE
) NULL
)
481 FreeLibrary(gs_hRichEdit
);
490 if (wxSTD_FRAME_ICON
)
491 DestroyIcon(wxSTD_FRAME_ICON
);
492 if (wxSTD_MDICHILDFRAME_ICON
)
493 DestroyIcon(wxSTD_MDICHILDFRAME_ICON
);
494 if (wxSTD_MDIPARENTFRAME_ICON
)
495 DestroyIcon(wxSTD_MDIPARENTFRAME_ICON
);
497 if (wxDEFAULT_FRAME_ICON
)
498 DestroyIcon(wxDEFAULT_FRAME_ICON
);
499 if (wxDEFAULT_MDICHILDFRAME_ICON
)
500 DestroyIcon(wxDEFAULT_MDICHILDFRAME_ICON
);
501 if (wxDEFAULT_MDIPARENTFRAME_ICON
)
502 DestroyIcon(wxDEFAULT_MDIPARENTFRAME_ICON
);
504 if ( wxDisableButtonBrush
)
505 ::DeleteObject( wxDisableButtonBrush
);
512 Ctl3dUnregister(wxhInstance
);
516 delete wxWinHandleList
;
518 // GL: I'm annoyed ... I don't know where to put this and I don't want to
519 // create a module for that as it's part of the core.
521 delete wxPendingEvents
;
522 delete wxPendingEventsLocker
;
523 // If we don't do the following, we get an apparent memory leak.
524 ((wxEvtHandler
&) wxDefaultValidator
).ClearEventLocker();
527 wxClassInfo::CleanUpClasses();
532 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
533 // At this point we want to check if there are any memory
534 // blocks that aren't part of the wxDebugContext itself,
535 // as a special case. Then when dumping we need to ignore
536 // wxDebugContext, too.
537 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
539 wxLogDebug(_T("There were memory leaks."));
540 wxDebugContext::Dump();
541 wxDebugContext::PrintStatistics();
543 // wxDebugContext::SetStream(NULL, NULL);
547 // do it as the very last thing because everything else can log messages
548 delete wxLog::SetActiveTarget(NULL
);
552 #if !defined(_WINDLL) || (defined(_WINDLL) && defined(WXMAKINGDLL))
554 // temporarily disable this warning which would be generated in release builds
557 #pragma warning(disable: 4715) // not all control paths return a value
560 //// Main wxWindows entry point
561 int wxEntry(WXHINSTANCE hInstance
,
562 WXHINSTANCE
WXUNUSED(hPrevInstance
),
567 // do check for memory leaks on program exit
568 // (another useful flag is _CRTDBG_DELAY_FREE_MEM_DF which doesn't free
569 // deallocated memory which may be used to simulate low-memory condition)
570 wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF
);
572 // take everything into a try-except block in release build
573 // FIXME other compilers must support Win32 SEH (structured exception
574 // handling) too, just find the appropriate keyword in their docs!
575 // Please note that it's _not_ the same as C++ exceptions!
576 #if !defined(__WXDEBUG__) && defined(__VISUALC__)
577 #define CATCH_PROGRAM_EXCEPTIONS
581 #undef CATCH_PROGRAM_EXCEPTIONS
584 wxhInstance
= (HINSTANCE
) hInstance
;
586 if (!wxApp::Initialize())
589 // create the application object or ensure that one already exists
592 // The app may have declared a global application object, but we recommend
593 // the IMPLEMENT_APP macro is used instead, which sets an initializer
594 // function for delayed, dynamic app object construction.
595 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
596 _T("No initializer - use IMPLEMENT_APP macro.") );
598 wxTheApp
= (*wxApp::GetInitializerFunction()) ();
601 wxCHECK_MSG( wxTheApp
, 0, _T("You have to define an instance of wxApp!") );
603 // save the WinMain() parameters
604 wxTheApp
->ConvertToStandardCommandArgs(lpCmdLine
);
605 wxTheApp
->m_nCmdShow
= nCmdShow
;
607 // GUI-specific initialisation. In fact on Windows we don't have any,
608 // but this call is provided for compatibility across platforms.
609 wxTheApp
->OnInitGui();
613 if ( wxTheApp
->OnInit() )
617 retValue
= wxTheApp
->OnRun();
620 // We want to initialize, but not run or exit immediately.
623 //else: app initialization failed, so we skipped OnRun()
625 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
628 // Forcibly delete the window.
629 if ( topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
630 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
632 topWindow
->Close(TRUE
);
633 wxTheApp
->DeletePendingObjects();
638 wxTheApp
->SetTopWindow(NULL
);
648 #ifdef CATCH_PROGRAM_EXCEPTIONS
650 __except ( EXCEPTION_EXECUTE_HANDLER
) {
653 wxTheApp->OnFatalException();
656 // using wxLog would be unsafe here
658 _("Unrecoverable program error detected: "
659 " the application will terminate."),
661 MB_APPLMODAL
| MB_ICONSTOP
| MB_OK
);
663 ::ExitProcess(3); // the same exit code as abort()
667 #endif // CATCH_PROGRAM_EXCEPTIONS
670 // restore warning state
672 #pragma warning(default: 4715) // not all control paths return a value
677 //// Entry point for DLLs
679 int wxEntry(WXHINSTANCE hInstance
)
681 wxhInstance
= (HINSTANCE
) hInstance
;
684 // The app may have declared a global application object, but we recommend
685 // the IMPLEMENT_APP macro is used instead, which sets an initializer function
686 // for delayed, dynamic app object construction.
689 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
690 "No initializer - use IMPLEMENT_APP macro." );
692 wxTheApp
= (* wxApp::GetInitializerFunction()) ();
695 wxCHECK_MSG( wxTheApp
, 0, "You have to define an instance of wxApp!" );
698 wxTheApp
->argv
= NULL
;
700 wxTheApp
->OnInitGui();
704 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
705 if ( topWindow
&& topWindow
->GetHWND())
707 topWindow
->Show(TRUE
);
714 //// Static member initialization
716 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
722 m_wantDebugOutput
= TRUE
;
726 m_printMode
= wxPRINT_WINDOWS
;
727 m_exitOnFrameDelete
= TRUE
;
733 // Delete command-line args
735 for (i
= 0; i
< argc
; i
++)
742 bool wxApp::Initialized()
750 #ifdef _WINDLL // Assume initialized if DLL (no way of telling)
756 * Get and process a message, returning FALSE if WM_QUIT
757 * received (and also set the flag telling the app to exit the main loop)
760 bool wxApp::DoMessage()
762 BOOL rc
= ::GetMessage(&s_currentMsg
, (HWND
) NULL
, 0, 0);
772 // should never happen, but let's test for it nevertheless
773 wxLogLastError("GetMessage");
778 wxASSERT_MSG( wxThread::IsMain(),
779 _T("only the main thread can process Windows messages") );
781 static bool s_hadGuiLock
= TRUE
;
782 static wxMsgArray s_aSavedMessages
;
784 // if a secondary thread owns is doing GUI calls, save all messages for
785 // later processing - we can't process them right now because it will
786 // lead to recursive library calls (and we're not reentrant)
787 if ( !wxGuiOwnedByMainThread() )
789 s_hadGuiLock
= FALSE
;
791 // leave out WM_COMMAND messages: too dangerous, sometimes
792 // the message will be processed twice
793 if ( !wxIsWaitingForThread() ||
794 s_currentMsg
.message
!= WM_COMMAND
)
796 s_aSavedMessages
.Add(s_currentMsg
);
803 // have we just regained the GUI lock? if so, post all of the saved
806 // FIXME of course, it's not _exactly_ the same as processing the
807 // messages normally - expect some things to break...
812 size_t count
= s_aSavedMessages
.Count();
813 for ( size_t n
= 0; n
< count
; n
++ )
815 MSG
& msg
= s_aSavedMessages
[n
];
817 if ( !ProcessMessage((WXMSG
*)&msg
) )
819 ::TranslateMessage(&msg
);
820 ::DispatchMessage(&msg
);
824 s_aSavedMessages
.Empty();
827 #endif // wxUSE_THREADS
829 // Process the message
830 if ( !ProcessMessage((WXMSG
*)&s_currentMsg
) )
832 ::TranslateMessage(&s_currentMsg
);
833 ::DispatchMessage(&s_currentMsg
);
841 * Keep trying to process messages until WM_QUIT
844 * If there are messages to be processed, they will all be
845 * processed and OnIdle will not be called.
846 * When there are no more messages, OnIdle is called.
847 * If OnIdle requests more time,
848 * it will be repeatedly called so long as there are no pending messages.
849 * A 'feature' of this is that once OnIdle has decided that no more processing
850 * is required, then it won't get processing time until further messages
851 * are processed (it'll sit in DoMessage).
854 int wxApp::MainLoop()
858 while ( m_keepGoing
)
861 wxMutexGuiLeaveOrEnter();
862 #endif // wxUSE_THREADS
864 while ( !::PeekMessage(&s_currentMsg
, 0, 0, 0, PM_NOREMOVE
) &&
873 return s_currentMsg
.wParam
;
876 // Returns TRUE if more time is needed.
877 bool wxApp::ProcessIdle()
880 event
.SetEventObject(this);
883 return event
.MoreRequested();
887 void wxApp::ProcessPendingEvents()
889 wxNode
*node
= wxPendingEvents
->First();
890 wxCriticalSectionLocker
locker(*wxPendingEventsLocker
);
894 wxEvtHandler
*handler
= (wxEvtHandler
*)node
->Data();
896 handler
->ProcessPendingEvents();
899 node
= wxPendingEvents
->First();
905 void wxApp::ExitMainLoop()
910 bool wxApp::Pending()
912 return (::PeekMessage(&s_currentMsg
, 0, 0, 0, PM_NOREMOVE
) != 0);
915 void wxApp::Dispatch()
921 * Give all windows a chance to preprocess
922 * the message. Some may have accelerator tables, or have
926 bool wxApp::ProcessMessage(WXMSG
*wxmsg
)
928 MSG
*msg
= (MSG
*)wxmsg
;
929 HWND hWnd
= msg
->hwnd
;
930 wxWindow
*wndThis
= wxFindWinFromHandle((WXHWND
)hWnd
), *wnd
;
932 // for some composite controls (like a combobox), wndThis might be NULL
933 // because the subcontrol is not a wxWindow, but only the control itself
934 // is - try to catch this case
935 while ( hWnd
&& !wndThis
)
937 hWnd
= ::GetParent(hWnd
);
938 wndThis
= wxFindWinFromHandle((WXHWND
)hWnd
);
941 // Try translations first; find the youngest window with
942 // a translation table.
943 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
945 if ( wnd
->MSWTranslateMessage(wxmsg
) )
949 // Anyone for a non-translation message? Try youngest descendants first.
950 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
952 if ( wnd
->MSWProcessMessage(wxmsg
) )
959 void wxApp::OnIdle(wxIdleEvent
& event
)
961 static bool s_inOnIdle
= FALSE
;
963 // Avoid recursion (via ProcessEvent default case)
969 // 'Garbage' collection of windows deleted with Close().
970 DeletePendingObjects();
973 // flush the logged messages if any
974 wxLog
*pLog
= wxLog::GetActiveTarget();
975 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
979 // Send OnIdle events to all windows
980 if ( SendIdleEvents() )
982 // SendIdleEvents() returns TRUE if at least one window requested more
984 event
.RequestMore(TRUE
);
987 // If they are pending events, we must process them.
989 ProcessPendingEvents();
994 // Send idle event to all top-level windows
995 bool wxApp::SendIdleEvents()
997 bool needMore
= FALSE
;
999 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
1002 wxWindow
* win
= node
->GetData();
1003 if (SendIdleEvents(win
))
1005 node
= node
->GetNext();
1011 // Send idle event to window and all subwindows
1012 bool wxApp::SendIdleEvents(wxWindow
* win
)
1014 bool needMore
= FALSE
;
1017 event
.SetEventObject(win
);
1018 win
->GetEventHandler()->ProcessEvent(event
);
1020 if (event
.MoreRequested())
1023 wxNode
* node
= win
->GetChildren().First();
1026 wxWindow
* win
= (wxWindow
*) node
->Data();
1027 if (SendIdleEvents(win
))
1030 node
= node
->Next();
1035 void wxApp::DeletePendingObjects()
1037 wxNode
*node
= wxPendingDelete
.First();
1040 wxObject
*obj
= (wxObject
*)node
->Data();
1044 if (wxPendingDelete
.Member(obj
))
1047 // Deleting one object may have deleted other pending
1048 // objects, so start from beginning of list again.
1049 node
= wxPendingDelete
.First();
1053 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
1056 GetTopWindow()->Close(TRUE
);
1059 // Default behaviour: close the application with prompts. The
1060 // user can veto the close, and therefore the end session.
1061 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
1065 if (!GetTopWindow()->Close(!event
.CanVeto()))
1070 int wxApp::GetComCtl32Version() const
1072 // have we loaded COMCTL32 yet?
1073 HMODULE theModule
= ::GetModuleHandle(_T("COMCTL32"));
1076 // if so, then we can check for the version
1079 // InitCommonControlsEx is unique to 4.7 and later
1080 FARPROC theProc
= ::GetProcAddress(theModule
, "InitCommonControlsEx");
1083 { // not found, must be 4.00
1088 // The following symbol are unique to 4.71
1090 // FlatSB_EnableScrollBar FlatSB_GetScrollInfo FlatSB_GetScrollPos
1091 // FlatSB_GetScrollProp FlatSB_GetScrollRange FlatSB_SetScrollInfo
1092 // FlatSB_SetScrollPos FlatSB_SetScrollProp FlatSB_SetScrollRange
1093 // FlatSB_ShowScrollBar
1094 // _DrawIndirectImageList _DuplicateImageList
1096 // UninitializeFlatSB
1097 // we could check for any of these - I chose DllInstall
1098 FARPROC theProc
= ::GetProcAddress(theModule
, "DllInstall");
1101 // not found, must be 4.70
1105 { // found, must be 4.71
1115 wxLogError(_("Fatal error: exiting"));
1120 // Yield to incoming messages
1124 // We want to go back to the main message loop
1125 // if we see a WM_QUIT. (?)
1127 while (PeekMessage(&msg
, (HWND
)NULL
, 0, 0, PM_NOREMOVE
) && msg
.message
!= WM_QUIT
)
1129 while (PeekMessage(&msg
, NULL
, 0, 0, PM_NOREMOVE
) && msg
.message
!= WM_QUIT
)
1132 if ( !wxTheApp
->DoMessage() )
1135 // If they are pending events, we must process them.
1137 wxTheApp
->ProcessPendingEvents();
1144 wxApp::GetStdIcon(int which
) const
1148 case wxICON_INFORMATION
:
1149 return wxIcon("wxICON_INFO");
1151 case wxICON_QUESTION
:
1152 return wxIcon("wxICON_QUESTION");
1154 case wxICON_EXCLAMATION
:
1155 return wxIcon("wxICON_WARNING");
1158 wxFAIL_MSG(_T("requested non existent standard icon"));
1159 // still fall through
1162 return wxIcon("wxICON_ERROR");
1167 HINSTANCE
wxGetInstance()
1172 void wxSetInstance(HINSTANCE hInst
)
1174 wxhInstance
= hInst
;
1177 // For some reason, with MSVC++ 1.5, WinMain isn't linked in properly
1178 // if in a separate file. So include it here to ensure it's linked.
1179 #if (defined(__VISUALC__) && !defined(__WIN32__)) || (defined(__GNUWIN32__) && !defined(__TWIN32__))