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
207 // for OLE, enlarge message queue to be as large as possible
209 while (!SetMessageQueue(iMsg
) && (iMsg
-= 8));
212 // we need to initialize OLE library
213 if ( FAILED(::OleInitialize(NULL
)) )
214 wxLogError(_("Cannot initialize OLE"));
218 if (!Ctl3dRegister(wxhInstance
))
219 wxLogError(_T("Cannot register CTL3D"));
221 Ctl3dAutoSubclass(wxhInstance
);
224 g_globalCursor
= new wxCursor
;
226 wxSTD_FRAME_ICON
= LoadIcon(wxhInstance
, _T("wxSTD_FRAME"));
227 wxSTD_MDIPARENTFRAME_ICON
= LoadIcon(wxhInstance
, _T("wxSTD_MDIPARENTFRAME"));
228 wxSTD_MDICHILDFRAME_ICON
= LoadIcon(wxhInstance
, _T("wxSTD_MDICHILDFRAME"));
230 wxDEFAULT_FRAME_ICON
= LoadIcon(wxhInstance
, _T("wxDEFAULT_FRAME"));
231 wxDEFAULT_MDIPARENTFRAME_ICON
= LoadIcon(wxhInstance
, _T("wxDEFAULT_MDIPARENTFRAME"));
232 wxDEFAULT_MDICHILDFRAME_ICON
= LoadIcon(wxhInstance
, _T("wxDEFAULT_MDICHILDFRAME"));
234 RegisterWindowClasses();
236 // Create the brush for disabling bitmap buttons
239 lb
.lbStyle
= BS_PATTERN
;
240 lb
.lbHatch
= (int)LoadBitmap( wxhInstance
, _T("wxDISABLE_BUTTON_BITMAP") );
243 wxDisableButtonBrush
= ::CreateBrushIndirect( & lb
);
244 ::DeleteObject( (HGDIOBJ
)lb
.lbHatch
);
246 //else: wxWindows resources are probably not linked in
252 wxWinHandleList
= new wxList(wxKEY_INTEGER
);
254 // This is to foil optimizations in Visual C++ that throw out dummy.obj.
255 // PLEASE DO NOT ALTER THIS.
256 #if defined(__VISUALC__) && !defined(WXMAKINGDLL)
257 extern char wxDummyChar
;
258 if (wxDummyChar
) wxDummyChar
++;
261 wxSetKeyboardHook(TRUE
);
263 wxModule::RegisterModules();
264 if (!wxModule::InitializeModules())
269 // ---------------------------------------------------------------------------
270 // RegisterWindowClasses
271 // ---------------------------------------------------------------------------
273 bool wxApp::RegisterWindowClasses()
277 // the fields which are common to all classes
278 wndclass
.style
= CS_HREDRAW
| CS_VREDRAW
| CS_DBLCLKS
;
279 wndclass
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
280 wndclass
.cbClsExtra
= 0;
281 wndclass
.cbWndExtra
= sizeof( DWORD
); // what is this DWORD used for?
282 wndclass
.hInstance
= wxhInstance
;
283 wndclass
.hIcon
= (HICON
) NULL
;
284 wndclass
.hCursor
= ::LoadCursor((HINSTANCE
)NULL
, IDC_ARROW
);
285 wndclass
.lpszMenuName
= NULL
;
287 // Register the frame window class.
288 wndclass
.hbrBackground
= (HBRUSH
)(COLOR_APPWORKSPACE
+ 1);
289 #ifdef _MULTIPLE_INSTANCES
290 sprintf( wxFrameClassName
,"wxFrameClass%d", wxhInstance
);
292 wndclass
.lpszClassName
= wxFrameClassName
;
294 if ( !RegisterClass(&wndclass
) )
296 wxLogLastError("RegisterClass(frame)");
301 // Register the MDI frame window class.
302 wndclass
.hbrBackground
= (HBRUSH
)NULL
; // paint MDI frame ourselves
303 wndclass
.lpszClassName
= wxMDIFrameClassName
;
305 if ( !RegisterClass(&wndclass
) )
307 wxLogLastError("RegisterClass(MDI parent)");
312 // Register the MDI child frame window class.
313 wndclass
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+ 1);
314 wndclass
.lpszClassName
= wxMDIChildFrameClassName
;
316 if ( !RegisterClass(&wndclass
) )
318 wxLogLastError("RegisterClass(MDI child)");
323 // Register the panel window class.
324 wndclass
.hbrBackground
= (HBRUSH
) GetStockObject( LTGRAY_BRUSH
);
325 wndclass
.lpszClassName
= wxPanelClassName
;
327 if ( !RegisterClass(&wndclass
) )
329 wxLogLastError("RegisterClass(panel)");
334 // Register the canvas and textsubwindow class name
335 wndclass
.hbrBackground
= (HBRUSH
)NULL
;
336 wndclass
.lpszClassName
= wxCanvasClassName
;
338 if ( !RegisterClass(&wndclass
) )
340 wxLogLastError("RegisterClass(canvas)");
348 // ---------------------------------------------------------------------------
349 // Convert Windows to argc, argv style
350 // ---------------------------------------------------------------------------
352 void wxApp::ConvertToStandardCommandArgs(char* lpCmdLine
)
356 wxString
cmdLine(lpCmdLine
);
359 // Get application name
360 wxChar name
[260]; // 260 is MAX_PATH value from windef.h
361 ::GetModuleFileName(wxhInstance
, name
, WXSIZEOF(name
));
366 wxStrcpy(name
, wxFileNameFromPath(name
));
367 wxStripExtension(name
);
368 wxTheApp
->SetAppName(name
);
371 // Treat strings enclosed in double-quotes as single arguments
373 int len
= cmdLine
.Length();
377 while ((i
< len
) && wxIsspace(cmdLine
.GetChar(i
)))
382 if (cmdLine
.GetChar(i
) == _T('"')) // We found the start of a string
386 while ((i
< len
) && (cmdLine
.GetChar(i
) != _T('"')))
389 wxString
arg(cmdLine
.Mid(first
, (i
- first
)));
395 i
++; // Skip past 2nd quote
397 else // Unquoted argument
400 while ((i
< len
) && !wxIsspace(cmdLine
.GetChar(i
)))
403 wxString
arg(cmdLine
.Mid(first
, (i
- first
)));
411 wxTheApp
->argv
= new wxChar
*[count
+ 1];
412 for (i
= 0; i
< count
; i
++)
414 wxString
arg(args
[i
]);
415 wxTheApp
->argv
[i
] = copystring((const wxChar
*)arg
);
417 wxTheApp
->argv
[count
] = NULL
; // argv[] is a NULL-terminated list
418 wxTheApp
->argc
= count
;
421 //// Cleans up any wxWindows internal structures left lying around
423 void wxApp::CleanUp()
428 // flush the logged messages if any and install a 'safer' log target: the
429 // default one (wxLogGui) can't be used after the resources are freed just
430 // below and the user suppliedo ne might be even more unsafe (using any
431 // wxWindows GUI function is unsafe starting from now)
432 wxLog::DontCreateOnDemand();
434 // this will flush the old messages if any
435 delete wxLog::SetActiveTarget(new wxLogStderr
);
438 // One last chance for pending objects to be cleaned up
439 wxTheApp
->DeletePendingObjects();
441 wxModule::CleanUpModules();
443 #if wxUSE_WX_RESOURCES
444 wxCleanUpResourceSystem();
446 // wxDefaultResourceTable->ClearTable();
449 // Indicate that the cursor can be freed, so that cursor won't be deleted
450 // by deleting the bitmap list before g_globalCursor goes out of scope
451 // (double deletion of the cursor).
452 wxSetCursor(wxNullCursor
);
453 delete g_globalCursor
;
454 g_globalCursor
= NULL
;
456 wxDeleteStockObjects();
458 // Destroy all GDI lists, etc.
459 wxDeleteStockLists();
461 delete wxTheColourDatabase
;
462 wxTheColourDatabase
= NULL
;
464 wxBitmap::CleanUpHandlers();
469 //// WINDOWS-SPECIFIC CLEANUP
471 wxSetKeyboardHook(FALSE
);
476 if (gs_hRichEdit
!= (HINSTANCE
) NULL
)
477 FreeLibrary(gs_hRichEdit
);
486 if (wxSTD_FRAME_ICON
)
487 DestroyIcon(wxSTD_FRAME_ICON
);
488 if (wxSTD_MDICHILDFRAME_ICON
)
489 DestroyIcon(wxSTD_MDICHILDFRAME_ICON
);
490 if (wxSTD_MDIPARENTFRAME_ICON
)
491 DestroyIcon(wxSTD_MDIPARENTFRAME_ICON
);
493 if (wxDEFAULT_FRAME_ICON
)
494 DestroyIcon(wxDEFAULT_FRAME_ICON
);
495 if (wxDEFAULT_MDICHILDFRAME_ICON
)
496 DestroyIcon(wxDEFAULT_MDICHILDFRAME_ICON
);
497 if (wxDEFAULT_MDIPARENTFRAME_ICON
)
498 DestroyIcon(wxDEFAULT_MDIPARENTFRAME_ICON
);
500 if ( wxDisableButtonBrush
)
501 ::DeleteObject( wxDisableButtonBrush
);
508 Ctl3dUnregister(wxhInstance
);
512 delete wxWinHandleList
;
514 // GL: I'm annoyed ... I don't know where to put this and I don't want to
515 // create a module for that as it's part of the core.
517 delete wxPendingEvents
;
518 delete wxPendingEventsLocker
;
519 // If we don't do the following, we get an apparent memory leak.
520 ((wxEvtHandler
&) wxDefaultValidator
).ClearEventLocker();
523 wxClassInfo::CleanUpClasses();
528 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
529 // At this point we want to check if there are any memory
530 // blocks that aren't part of the wxDebugContext itself,
531 // as a special case. Then when dumping we need to ignore
532 // wxDebugContext, too.
533 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
535 wxLogDebug(_T("There were memory leaks."));
536 wxDebugContext::Dump();
537 wxDebugContext::PrintStatistics();
539 // wxDebugContext::SetStream(NULL, NULL);
543 // do it as the very last thing because everything else can log messages
544 delete wxLog::SetActiveTarget(NULL
);
548 #if !defined(_WINDLL) || (defined(_WINDLL) && defined(WXMAKINGDLL))
550 // temporarily disable this warning which would be generated in release builds
553 #pragma warning(disable: 4715) // not all control paths return a value
556 //// Main wxWindows entry point
557 int wxEntry(WXHINSTANCE hInstance
,
558 WXHINSTANCE
WXUNUSED(hPrevInstance
),
563 // do check for memory leaks on program exit
564 // (another useful flag is _CRTDBG_DELAY_FREE_MEM_DF which doesn't free
565 // deallocated memory which may be used to simulate low-memory condition)
566 wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF
);
568 // take everything into a try-except block in release build
569 // FIXME other compilers must support Win32 SEH (structured exception
570 // handling) too, just find the appropriate keyword in their docs!
571 // Please note that it's _not_ the same as C++ exceptions!
572 #if !defined(__WXDEBUG__) && defined(__VISUALC__)
573 #define CATCH_PROGRAM_EXCEPTIONS
577 #undef CATCH_PROGRAM_EXCEPTIONS
580 wxhInstance
= (HINSTANCE
) hInstance
;
582 if (!wxApp::Initialize())
585 // create the application object or ensure that one already exists
588 // The app may have declared a global application object, but we recommend
589 // the IMPLEMENT_APP macro is used instead, which sets an initializer
590 // function for delayed, dynamic app object construction.
591 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
592 _T("No initializer - use IMPLEMENT_APP macro.") );
594 wxTheApp
= (*wxApp::GetInitializerFunction()) ();
597 wxCHECK_MSG( wxTheApp
, 0, _T("You have to define an instance of wxApp!") );
599 // save the WinMain() parameters
600 wxTheApp
->ConvertToStandardCommandArgs(lpCmdLine
);
601 wxTheApp
->m_nCmdShow
= nCmdShow
;
603 // GUI-specific initialisation. In fact on Windows we don't have any,
604 // but this call is provided for compatibility across platforms.
605 wxTheApp
->OnInitGui();
609 if ( wxTheApp
->OnInit() )
613 retValue
= wxTheApp
->OnRun();
616 // We want to initialize, but not run or exit immediately.
619 //else: app initialization failed, so we skipped OnRun()
621 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
624 // Forcibly delete the window.
625 if ( topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
626 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
628 topWindow
->Close(TRUE
);
629 wxTheApp
->DeletePendingObjects();
634 wxTheApp
->SetTopWindow(NULL
);
644 #ifdef CATCH_PROGRAM_EXCEPTIONS
646 __except ( EXCEPTION_EXECUTE_HANDLER
) {
649 wxTheApp->OnFatalException();
652 // using wxLog would be unsafe here
654 _("Unrecoverable program error detected: "
655 " the application will terminate."),
657 MB_APPLMODAL
| MB_ICONSTOP
| MB_OK
);
659 ::ExitProcess(3); // the same exit code as abort()
663 #endif // CATCH_PROGRAM_EXCEPTIONS
666 // restore warning state
668 #pragma warning(default: 4715) // not all control paths return a value
673 //// Entry point for DLLs
675 int wxEntry(WXHINSTANCE hInstance
)
677 wxhInstance
= (HINSTANCE
) hInstance
;
680 // The app may have declared a global application object, but we recommend
681 // the IMPLEMENT_APP macro is used instead, which sets an initializer function
682 // for delayed, dynamic app object construction.
685 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
686 "No initializer - use IMPLEMENT_APP macro." );
688 wxTheApp
= (* wxApp::GetInitializerFunction()) ();
691 wxCHECK_MSG( wxTheApp
, 0, "You have to define an instance of wxApp!" );
694 wxTheApp
->argv
= NULL
;
696 wxTheApp
->OnInitGui();
700 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
701 if ( topWindow
&& topWindow
->GetHWND())
703 topWindow
->Show(TRUE
);
710 //// Static member initialization
712 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
718 m_wantDebugOutput
= TRUE
;
722 m_printMode
= wxPRINT_WINDOWS
;
723 m_exitOnFrameDelete
= TRUE
;
729 // Delete command-line args
731 for (i
= 0; i
< argc
; i
++)
738 bool wxApp::Initialized()
746 #ifdef _WINDLL // Assume initialized if DLL (no way of telling)
752 * Get and process a message, returning FALSE if WM_QUIT
753 * received (and also set the flag telling the app to exit the main loop)
756 bool wxApp::DoMessage()
758 BOOL rc
= ::GetMessage(&s_currentMsg
, (HWND
) NULL
, 0, 0);
768 // should never happen, but let's test for it nevertheless
769 wxLogLastError("GetMessage");
774 wxASSERT_MSG( wxThread::IsMain(),
775 _T("only the main thread can process Windows messages") );
777 static bool s_hadGuiLock
= TRUE
;
778 static wxMsgArray s_aSavedMessages
;
780 // if a secondary thread owns is doing GUI calls, save all messages for
781 // later processing - we can't process them right now because it will
782 // lead to recursive library calls (and we're not reentrant)
783 if ( !wxGuiOwnedByMainThread() )
785 s_hadGuiLock
= FALSE
;
787 // leave out WM_COMMAND messages: too dangerous, sometimes
788 // the message will be processed twice
789 if ( !wxIsWaitingForThread() ||
790 s_currentMsg
.message
!= WM_COMMAND
)
792 s_aSavedMessages
.Add(s_currentMsg
);
799 // have we just regained the GUI lock? if so, post all of the saved
802 // FIXME of course, it's not _exactly_ the same as processing the
803 // messages normally - expect some things to break...
808 size_t count
= s_aSavedMessages
.Count();
809 for ( size_t n
= 0; n
< count
; n
++ )
811 MSG
& msg
= s_aSavedMessages
[n
];
813 if ( !ProcessMessage((WXMSG
*)&msg
) )
815 ::TranslateMessage(&msg
);
816 ::DispatchMessage(&msg
);
820 s_aSavedMessages
.Empty();
823 #endif // wxUSE_THREADS
825 // Process the message
826 if ( !ProcessMessage((WXMSG
*)&s_currentMsg
) )
828 ::TranslateMessage(&s_currentMsg
);
829 ::DispatchMessage(&s_currentMsg
);
837 * Keep trying to process messages until WM_QUIT
840 * If there are messages to be processed, they will all be
841 * processed and OnIdle will not be called.
842 * When there are no more messages, OnIdle is called.
843 * If OnIdle requests more time,
844 * it will be repeatedly called so long as there are no pending messages.
845 * A 'feature' of this is that once OnIdle has decided that no more processing
846 * is required, then it won't get processing time until further messages
847 * are processed (it'll sit in DoMessage).
850 int wxApp::MainLoop()
854 while ( m_keepGoing
)
857 wxMutexGuiLeaveOrEnter();
858 #endif // wxUSE_THREADS
860 while ( !::PeekMessage(&s_currentMsg
, 0, 0, 0, PM_NOREMOVE
) &&
869 return s_currentMsg
.wParam
;
872 // Returns TRUE if more time is needed.
873 bool wxApp::ProcessIdle()
876 event
.SetEventObject(this);
879 return event
.MoreRequested();
883 void wxApp::ProcessPendingEvents()
885 wxNode
*node
= wxPendingEvents
->First();
886 wxCriticalSectionLocker
locker(*wxPendingEventsLocker
);
890 wxEvtHandler
*handler
= (wxEvtHandler
*)node
->Data();
892 handler
->ProcessPendingEvents();
895 node
= wxPendingEvents
->First();
901 void wxApp::ExitMainLoop()
906 bool wxApp::Pending()
908 return (::PeekMessage(&s_currentMsg
, 0, 0, 0, PM_NOREMOVE
) != 0);
911 void wxApp::Dispatch()
917 * Give all windows a chance to preprocess
918 * the message. Some may have accelerator tables, or have
922 bool wxApp::ProcessMessage(WXMSG
*wxmsg
)
924 MSG
*msg
= (MSG
*)wxmsg
;
925 HWND hWnd
= msg
->hwnd
;
926 wxWindow
*wndThis
= wxFindWinFromHandle((WXHWND
)hWnd
), *wnd
;
928 // for some composite controls (like a combobox), wndThis might be NULL
929 // because the subcontrol is not a wxWindow, but only the control itself
930 // is - try to catch this case
931 while ( hWnd
&& !wndThis
)
933 hWnd
= ::GetParent(hWnd
);
934 wndThis
= wxFindWinFromHandle((WXHWND
)hWnd
);
937 // Try translations first; find the youngest window with
938 // a translation table.
939 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
941 if ( wnd
->MSWTranslateMessage(wxmsg
) )
945 // Anyone for a non-translation message? Try youngest descendants first.
946 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
948 if ( wnd
->MSWProcessMessage(wxmsg
) )
955 void wxApp::OnIdle(wxIdleEvent
& event
)
957 static bool s_inOnIdle
= FALSE
;
959 // Avoid recursion (via ProcessEvent default case)
965 // 'Garbage' collection of windows deleted with Close().
966 DeletePendingObjects();
969 // flush the logged messages if any
970 wxLog
*pLog
= wxLog::GetActiveTarget();
971 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
975 // Send OnIdle events to all windows
976 if ( SendIdleEvents() )
978 // SendIdleEvents() returns TRUE if at least one window requested more
980 event
.RequestMore(TRUE
);
983 // If they are pending events, we must process them.
985 ProcessPendingEvents();
990 // Send idle event to all top-level windows
991 bool wxApp::SendIdleEvents()
993 bool needMore
= FALSE
;
995 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
998 wxWindow
* win
= node
->GetData();
999 if (SendIdleEvents(win
))
1001 node
= node
->GetNext();
1007 // Send idle event to window and all subwindows
1008 bool wxApp::SendIdleEvents(wxWindow
* win
)
1010 bool needMore
= FALSE
;
1013 event
.SetEventObject(win
);
1014 win
->GetEventHandler()->ProcessEvent(event
);
1016 if (event
.MoreRequested())
1019 wxNode
* node
= win
->GetChildren().First();
1022 wxWindow
* win
= (wxWindow
*) node
->Data();
1023 if (SendIdleEvents(win
))
1026 node
= node
->Next();
1031 void wxApp::DeletePendingObjects()
1033 wxNode
*node
= wxPendingDelete
.First();
1036 wxObject
*obj
= (wxObject
*)node
->Data();
1040 if (wxPendingDelete
.Member(obj
))
1043 // Deleting one object may have deleted other pending
1044 // objects, so start from beginning of list again.
1045 node
= wxPendingDelete
.First();
1049 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
1052 GetTopWindow()->Close(TRUE
);
1055 // Default behaviour: close the application with prompts. The
1056 // user can veto the close, and therefore the end session.
1057 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
1061 if (!GetTopWindow()->Close(!event
.CanVeto()))
1066 int wxApp::GetComCtl32Version() const
1068 // have we loaded COMCTL32 yet?
1069 HMODULE theModule
= ::GetModuleHandle(_T("COMCTL32"));
1072 // if so, then we can check for the version
1075 // InitCommonControlsEx is unique to 4.7 and later
1076 FARPROC theProc
= ::GetProcAddress(theModule
, "InitCommonControlsEx");
1079 { // not found, must be 4.00
1084 // The following symbol are unique to 4.71
1086 // FlatSB_EnableScrollBar FlatSB_GetScrollInfo FlatSB_GetScrollPos
1087 // FlatSB_GetScrollProp FlatSB_GetScrollRange FlatSB_SetScrollInfo
1088 // FlatSB_SetScrollPos FlatSB_SetScrollProp FlatSB_SetScrollRange
1089 // FlatSB_ShowScrollBar
1090 // _DrawIndirectImageList _DuplicateImageList
1092 // UninitializeFlatSB
1093 // we could check for any of these - I chose DllInstall
1094 FARPROC theProc
= ::GetProcAddress(theModule
, "DllInstall");
1097 // not found, must be 4.70
1101 { // found, must be 4.71
1111 wxLogError(_("Fatal error: exiting"));
1116 // Yield to incoming messages
1120 // We want to go back to the main message loop
1121 // if we see a WM_QUIT. (?)
1123 while (PeekMessage(&msg
, (HWND
)NULL
, 0, 0, PM_NOREMOVE
) && msg
.message
!= WM_QUIT
)
1125 while (PeekMessage(&msg
, NULL
, 0, 0, PM_NOREMOVE
) && msg
.message
!= WM_QUIT
)
1128 if ( !wxTheApp
->DoMessage() )
1131 // If they are pending events, we must process them.
1133 wxTheApp
->ProcessPendingEvents();
1140 wxApp::GetStdIcon(int which
) const
1144 case wxICON_INFORMATION
:
1145 return wxIcon("wxICON_INFO");
1147 case wxICON_QUESTION
:
1148 return wxIcon("wxICON_QUESTION");
1150 case wxICON_EXCLAMATION
:
1151 return wxIcon("wxICON_WARNING");
1154 wxFAIL_MSG(_T("requested non existent standard icon"));
1155 // still fall through
1158 return wxIcon("wxICON_ERROR");
1163 HINSTANCE
wxGetInstance()
1168 void wxSetInstance(HINSTANCE hInst
)
1170 wxhInstance
= hInst
;
1173 // For some reason, with MSVC++ 1.5, WinMain isn't linked in properly
1174 // if in a separate file. So include it here to ensure it's linked.
1175 #if (defined(__VISUALC__) && !defined(__WIN32__)) || (defined(__GNUWIN32__) && !defined(__TWIN32__))