1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "app.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
19 #if defined(__BORLANDC__)
27 #include "wx/gdicmn.h"
30 #include "wx/cursor.h"
32 #include "wx/palette.h"
34 #include "wx/dialog.h"
35 #include "wx/msgdlg.h"
38 #include "wx/msw/private.h"
40 #include "wx/module.h"
41 #include "wx/thread.h"
43 #if wxUSE_WX_RESOURCES
44 #include "wx/resource.h"
47 // To UG: there's no point in putting this #if here
48 // if you don't do the same for the Ole calls further down.
49 // Also, OLE is used not just for drag and drop (it's used by automatn.cpp).
50 // #if wxUSE_DRAG_AND_DROP
59 #if defined(__WIN95__) && !defined(__GNUWIN32__)
63 // use debug CRT functions for memory leak detections in VC++ if we're not
64 // using wxWindows own methods
65 #if defined(__WXDEBUG__) && defined(_MSC_VER) && !wxUSE_GLOBAL_MEMORY_OPERATORS && !defined(__NO_VC_CRTDBG__)
66 #define wxUSE_VC_CRTDBG
68 #undef wxUSE_VC_CRTDBG
71 #ifdef wxUSE_VC_CRTDBG
72 // VC++ uses this macro as debug/release mode indicator
77 /* Need to undef new if including crtdbg.h */
84 #if defined(__WXDEBUG__) && wxUSE_GLOBAL_MEMORY_OPERATORS && wxUSE_DEBUG_NEW_ALWAYS
85 #define new new(__FILE__,__LINE__)
90 extern char *wxBuffer
;
91 extern char *wxOsVersion
;
92 extern wxList
*wxWinHandleList
;
93 extern wxList wxPendingDelete
;
94 extern void wxSetKeyboardHook(bool doIt
);
95 extern wxCursor
*g_globalCursor
;
97 HINSTANCE wxhInstance
= 0;
98 static MSG s_currentMsg
;
99 wxApp
*wxTheApp
= NULL
;
101 // @@ why not const? and not static?
102 char wxFrameClassName
[] = "wxFrameClass";
103 char wxMDIFrameClassName
[] = "wxMDIFrameClass";
104 char wxMDIChildFrameClassName
[] = "wxMDIChildFrameClass";
105 char wxPanelClassName
[] = "wxPanelClass";
106 char wxCanvasClassName
[] = "wxCanvasClass";
108 HICON wxSTD_FRAME_ICON
= NULL
;
109 HICON wxSTD_MDICHILDFRAME_ICON
= NULL
;
110 HICON wxSTD_MDIPARENTFRAME_ICON
= NULL
;
112 HICON wxDEFAULT_FRAME_ICON
= NULL
;
113 HICON wxDEFAULT_MDICHILDFRAME_ICON
= NULL
;
114 HICON wxDEFAULT_MDIPARENTFRAME_ICON
= NULL
;
116 HBRUSH wxDisableButtonBrush
= 0;
118 LRESULT APIENTRY
wxWndProc(HWND
, UINT
, WPARAM
, LPARAM
);
120 #if !USE_SHARED_LIBRARY
121 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
123 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
124 EVT_IDLE(wxApp::OnIdle
)
125 EVT_END_SESSION(wxApp::OnEndSession
)
126 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession
)
130 long wxApp::sm_lastMessageTime
= 0;
133 static HINSTANCE gs_hRichEdit
= NULL
;
138 bool wxApp::Initialize()
140 // Some people may wish to use this, but
141 // probably it shouldn't be here by default.
143 // wxRedirectIOToConsole();
146 wxBuffer
= new char[1500];
148 #ifdef wxUSE_VC_CRTDBG
149 // do check for memory leaks on program exit
150 // (another useful flag is _CRTDBG_DELAY_FREE_MEM_DF which doesn't free
151 // deallocated memory which may be used to simulate low-memory condition)
152 _CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG
) | _CRTDBG_LEAK_CHECK_DF
);
153 #endif // debug build under MS VC++
155 wxClassInfo::InitializeClasses();
158 wxGetResource("wxWindows", "OsVersion", &wxOsVersion
);
161 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
162 wxTheColourDatabase
->Initialize();
164 wxInitializeStockLists();
165 wxInitializeStockObjects();
167 #if wxUSE_WX_RESOURCES
168 wxInitializeResourceSystem();
171 wxBitmap::InitStandardHandlers();
173 #if defined(__WIN95__)
174 InitCommonControls();
175 gs_hRichEdit
= LoadLibrary("RICHED32.DLL");
177 if (gs_hRichEdit
== NULL
)
179 wxMessageBox("Could not initialise Rich Edit DLL");
185 // for OLE, enlarge message queue to be as large as possible
186 while (!SetMessageQueue(iMsg
) && (iMsg
-= 8));
190 dwOleVer = CoBuildVersion();
192 // check the OLE library version
193 if (rmm != HIWORD(dwOleVer))
195 wxMessageBox("Incorrect version of OLE libraries.");
201 // we need to initialize OLE library
202 if ( FAILED(::OleInitialize(NULL
)) )
203 wxFatalError(_("Cannot initialize OLE"));
207 if (!Ctl3dRegister(wxhInstance
))
208 wxFatalError("Cannot register CTL3D");
210 Ctl3dAutoSubclass(wxhInstance
);
213 g_globalCursor
= new wxCursor
;
215 wxSTD_FRAME_ICON
= LoadIcon(wxhInstance
, "wxSTD_FRAME");
216 wxSTD_MDIPARENTFRAME_ICON
= LoadIcon(wxhInstance
, "wxSTD_MDIPARENTFRAME");
217 wxSTD_MDICHILDFRAME_ICON
= LoadIcon(wxhInstance
, "wxSTD_MDICHILDFRAME");
219 wxDEFAULT_FRAME_ICON
= LoadIcon(wxhInstance
, "wxDEFAULT_FRAME");
220 wxDEFAULT_MDIPARENTFRAME_ICON
= LoadIcon(wxhInstance
, "wxDEFAULT_MDIPARENTFRAME");
221 wxDEFAULT_MDICHILDFRAME_ICON
= LoadIcon(wxhInstance
, "wxDEFAULT_MDICHILDFRAME");
223 RegisterWindowClasses();
225 // Create the brush for disabling bitmap buttons
228 lb
.lbStyle
= BS_PATTERN
;
229 lb
.lbHatch
= (int)LoadBitmap( wxhInstance
, "wxDISABLE_BUTTON_BITMAP" ) ;
230 wxDisableButtonBrush
= ::CreateBrushIndirect( & lb
) ;
231 ::DeleteObject( (HGDIOBJ
)lb
.lbHatch
) ;
237 wxWinHandleList
= new wxList(wxKEY_INTEGER
);
239 // This is to foil optimizations in Visual C++ that
240 // throw out dummy.obj.
241 #if (_MSC_VER >= 800) && !defined(WXMAKINGDLL)
242 extern char wxDummyChar
;
243 if (wxDummyChar
) wxDummyChar
++;
246 wxSetKeyboardHook(TRUE
);
248 wxModule::RegisterModules();
249 if (!wxModule::InitializeModules())
254 //// RegisterWindowClasses
256 bool wxApp::RegisterWindowClasses()
258 ///////////////////////////////////////////////////////////////////////
259 // Register the frame window class.
260 WNDCLASS wndclass
; // Structure used to register Windows class.
262 wndclass
.style
= CS_HREDRAW
| CS_VREDRAW
;
263 wndclass
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
264 wndclass
.cbClsExtra
= 0;
265 wndclass
.cbWndExtra
= sizeof( DWORD
); // was 4
266 wndclass
.hInstance
= wxhInstance
;
267 wndclass
.hIcon
= NULL
; // wxSTD_FRAME_ICON;
268 wndclass
.hCursor
= LoadCursor( NULL
, IDC_ARROW
);
269 wndclass
.hbrBackground
= (HBRUSH
)(COLOR_APPWORKSPACE
+1) ;
270 // wndclass.hbrBackground = GetStockObject( WHITE_BRUSH );
271 wndclass
.lpszMenuName
= NULL
;
272 #ifdef _MULTIPLE_INSTANCES
273 sprintf( wxFrameClassName
,"wxFrameClass%d", wxhInstance
);
275 wndclass
.lpszClassName
= wxFrameClassName
;
277 if (!RegisterClass( &wndclass
))
279 // wxFatalError("Can't register Frame Window class");
282 ///////////////////////////////////////////////////////////////////////
283 // Register the MDI frame window class.
284 WNDCLASS wndclass1
; // Structure used to register Windows class.
286 wndclass1
.style
= CS_HREDRAW
| CS_VREDRAW
;
287 wndclass1
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
288 wndclass1
.cbClsExtra
= 0;
289 wndclass1
.cbWndExtra
= sizeof( DWORD
); // was 4
290 wndclass1
.hInstance
= wxhInstance
;
291 wndclass1
.hIcon
= NULL
; // wxSTD_MDIPARENTFRAME_ICON;
292 wndclass1
.hCursor
= LoadCursor( NULL
, IDC_ARROW
);
293 // wndclass1.hbrBackground = (HBRUSH)(COLOR_APPWORKSPACE+1) ;
294 wndclass1
.hbrBackground
= NULL
;
295 wndclass1
.lpszMenuName
= NULL
;
297 wndclass1
.lpszClassName
= wxMDIFrameClassName
;
298 if (!RegisterClass( &wndclass1
))
300 // wxFatalError("Can't register MDI Frame window class");
304 ///////////////////////////////////////////////////////////////////////
305 // Register the MDI child frame window class.
306 WNDCLASS wndclass4
; // Structure used to register Windows class.
308 wndclass4
.style
= CS_HREDRAW
| CS_VREDRAW
;
309 wndclass4
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
310 wndclass4
.cbClsExtra
= 0;
311 wndclass4
.cbWndExtra
= sizeof( DWORD
); // was 4
312 wndclass4
.hInstance
= wxhInstance
;
313 wndclass4
.hIcon
= NULL
; // wxSTD_MDICHILDFRAME_ICON;
314 wndclass4
.hCursor
= LoadCursor( NULL
, IDC_ARROW
);
315 // TODO: perhaps this should be NULL so that Windows doesn't
316 // paint the background itself (would OnEraseBackground duplicate
318 wndclass4
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+1) ;
319 // wndclass4.hbrBackground = NULL;
320 wndclass4
.lpszMenuName
= NULL
;
321 wndclass4
.lpszClassName
= wxMDIChildFrameClassName
;
323 if (!RegisterClass( &wndclass4
))
325 // wxFatalError("Can't register MDI child frame window class");
329 ///////////////////////////////////////////////////////////////////////
330 // Register the panel window class.
331 WNDCLASS wndclass2
; // Structure used to register Windows class.
332 memset(&wndclass2
, 0, sizeof(WNDCLASS
)); // start with NULL defaults
333 // Use CS_OWNDC to avoid messing about restoring the context
334 // for every graphic operation.
335 wndclass2
.style
= CS_HREDRAW
| CS_VREDRAW
;
336 wndclass2
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
337 wndclass2
.cbClsExtra
= 0;
338 wndclass2
.cbWndExtra
= sizeof( DWORD
); // was 4
339 wndclass2
.hInstance
= wxhInstance
;
340 wndclass2
.hIcon
= NULL
;
341 wndclass2
.hCursor
= NULL
;
342 // wndclass2.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1) ;
343 wndclass2
.hbrBackground
= (HBRUSH
) GetStockObject( LTGRAY_BRUSH
);
344 wndclass2
.lpszMenuName
= NULL
;
345 wndclass2
.lpszClassName
= wxPanelClassName
;
346 if (!RegisterClass( &wndclass2
))
348 // wxFatalError("Can't register Panel Window class");
352 ///////////////////////////////////////////////////////////////////////
353 // Register the canvas and textsubwindow class name
354 WNDCLASS wndclass3
; // Structure used to register Windows class.
355 memset(&wndclass3
, 0, sizeof(WNDCLASS
)); // start with NULL defaults
356 // Use CS_OWNDC to avoid messing about restoring the context
357 // for every graphic operation.
358 // wndclass3.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS ;
360 wndclass3
.style
= CS_HREDRAW
| CS_VREDRAW
| CS_DBLCLKS
;
361 wndclass3
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
362 wndclass3
.cbClsExtra
= 0;
363 wndclass3
.cbWndExtra
= sizeof( DWORD
); // was 4
364 wndclass3
.hInstance
= wxhInstance
;
365 wndclass3
.hIcon
= NULL
;
366 wndclass3
.hCursor
= NULL
;
367 // wndclass3.hbrBackground = (HBRUSH)(COLOR_WINDOW+1) ;
368 wndclass3
.hbrBackground
= NULL
;
369 wndclass3
.lpszMenuName
= NULL
;
370 wndclass3
.lpszClassName
= wxCanvasClassName
;
371 if (!RegisterClass( &wndclass3
))
373 // wxFatalError("Can't register Canvas class");
380 //// Convert Windows to argc, argv style
382 void wxApp::ConvertToStandardCommandArgs(char* lpCmdLine
)
386 wxString
cmdLine(lpCmdLine
);
389 // Get application name
390 char name
[260]; // 260 is MAX_PATH value from windef.h
391 ::GetModuleFileName(wxhInstance
, name
, WXSIZEOF(name
));
393 // GNUWIN32 already fills in the first arg with the application name.
394 #if !defined(__GNUWIN32__)
399 strcpy(name
, wxFileNameFromPath(name
));
400 wxStripExtension(name
);
401 wxTheApp
->SetAppName(name
);
404 // Treat strings enclosed in double-quotes as single arguments
406 int len
= cmdLine
.Length();
410 while ((i
< len
) && isspace(cmdLine
.GetChar(i
)))
415 if (cmdLine
.GetChar(i
) == '"') // We found the start of a string
419 while ((i
< len
) && (cmdLine
.GetChar(i
) != '"'))
422 wxString
arg(cmdLine
.Mid(first
, (i
- first
)));
428 i
++; // Skip past 2nd quote
430 else // Unquoted argument
433 while ((i
< len
) && !isspace(cmdLine
.GetChar(i
)))
436 wxString
arg(cmdLine
.Mid(first
, (i
- first
)));
444 wxTheApp
->argv
= new char*[count
+ 1];
445 for (i
= 0; i
< count
; i
++)
447 wxString
arg(args
[i
]);
448 wxTheApp
->argv
[i
] = copystring((const char*)arg
);
450 wxTheApp
->argv
[count
] = NULL
; // argv[] is a NULL-terminated list
451 wxTheApp
->argc
= count
;
454 //// Cleans up any wxWindows internal structures left lying around
456 void wxApp::CleanUp()
459 wxModule::CleanUpModules();
461 #if wxUSE_WX_RESOURCES
462 wxCleanUpResourceSystem();
464 // wxDefaultResourceTable->ClearTable();
467 // Indicate that the cursor can be freed,
468 // so that cursor won't be deleted by deleting
469 // the bitmap list before g_globalCursor goes out
470 // of scope (double deletion of the cursor).
471 wxSetCursor(wxNullCursor
);
472 delete g_globalCursor
;
474 wxDeleteStockObjects() ;
476 // Destroy all GDI lists, etc.
477 wxDeleteStockLists();
479 delete wxTheColourDatabase
;
480 wxTheColourDatabase
= NULL
;
482 wxBitmap::CleanUpHandlers();
487 //// WINDOWS-SPECIFIC CLEANUP
489 wxSetKeyboardHook(FALSE
);
492 if (gs_hRichEdit
!= NULL
)
493 FreeLibrary(gs_hRichEdit
);
500 if (wxSTD_FRAME_ICON
)
501 DestroyIcon(wxSTD_FRAME_ICON
);
502 if (wxSTD_MDICHILDFRAME_ICON
)
503 DestroyIcon(wxSTD_MDICHILDFRAME_ICON
);
504 if (wxSTD_MDIPARENTFRAME_ICON
)
505 DestroyIcon(wxSTD_MDIPARENTFRAME_ICON
);
507 if (wxDEFAULT_FRAME_ICON
)
508 DestroyIcon(wxDEFAULT_FRAME_ICON
);
509 if (wxDEFAULT_MDICHILDFRAME_ICON
)
510 DestroyIcon(wxDEFAULT_MDICHILDFRAME_ICON
);
511 if (wxDEFAULT_MDIPARENTFRAME_ICON
)
512 DestroyIcon(wxDEFAULT_MDIPARENTFRAME_ICON
);
514 if ( wxDisableButtonBrush
)
515 ::DeleteObject( wxDisableButtonBrush
) ;
522 Ctl3dUnregister(wxhInstance
);
526 delete wxWinHandleList
;
528 wxClassInfo::CleanUpClasses();
533 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
534 // At this point we want to check if there are any memory
535 // blocks that aren't part of the wxDebugContext itself,
536 // as a special case. Then when dumping we need to ignore
537 // wxDebugContext, too.
538 if (wxDebugContext::CountObjectsLeft() > 0)
540 wxLogDebug("There were memory leaks.");
541 wxDebugContext::Dump();
542 wxDebugContext::PrintStatistics();
544 // wxDebugContext::SetStream(NULL, NULL);
547 // do it as the very last thing because everything else can log messages
548 wxLog::DontCreateOnDemand();
549 delete wxLog::SetActiveTarget(NULL
);
552 #if !defined(_WINDLL) || (defined(_WINDLL) && defined(WXMAKINGDLL))
554 //// Main wxWindows entry point
555 int wxEntry(WXHINSTANCE hInstance
,
556 WXHINSTANCE
WXUNUSED(hPrevInstance
),
561 #if !defined(__WXDEBUG__) && !defined(__BORLANDC__) // take everything into a try-except block in release build
565 wxhInstance
= (HINSTANCE
) hInstance
;
567 if (!wxApp::Initialize())
570 // create the application object or ensure that one already exists
573 // The app may have declared a global application object, but we recommend
574 // the IMPLEMENT_APP macro is used instead, which sets an initializer
575 // function for delayed, dynamic app object construction.
576 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
577 "No initializer - use IMPLEMENT_APP macro." );
579 wxTheApp
= (*wxApp::GetInitializerFunction()) ();
582 wxCHECK_MSG( wxTheApp
, 0, "You have to define an instance of wxApp!" );
584 // save the WinMain() parameters
585 wxTheApp
->ConvertToStandardCommandArgs(lpCmdLine
);
586 wxTheApp
->m_nCmdShow
= nCmdShow
;
588 // GUI-specific initialisation. In fact on Windows we don't have any,
589 // but this call is provided for compatibility across platforms.
590 wxTheApp
->OnInitGui() ;
594 if ( wxTheApp
->OnInit() )
598 retValue
= wxTheApp
->OnRun();
601 //else: app initialization failed, so we skipped OnRun()
603 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
606 // Forcibly delete the window.
607 if ( topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
608 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
610 topWindow
->Close(TRUE
);
611 wxTheApp
->DeletePendingObjects();
616 wxTheApp
->SetTopWindow(NULL
);
622 // flush the logged messages if any
623 wxLog
*pLog
= wxLog::GetActiveTarget();
624 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
631 #if !defined(__WXDEBUG__) && !defined(__BORLANDC__) // catch exceptions only in release build
633 except ( EXCEPTION_EXECUTE_HANDLER
) {
636 wxTheApp->OnFatalException();
639 ::ExitProcess(3); // the same exit code as abort()
646 //// Entry point for DLLs
648 int wxEntry(WXHINSTANCE hInstance
)
650 wxhInstance
= (HINSTANCE
) hInstance
;
653 // The app may have declared a global application object, but we recommend
654 // the IMPLEMENT_APP macro is used instead, which sets an initializer function
655 // for delayed, dynamic app object construction.
659 if (!wxApp::GetInitializerFunction())
661 MessageBox(NULL
, "No initializer - use IMPLEMENT_APP macro.", "wxWindows Error", MB_APPLMODAL
| MB_ICONSTOP
| MB_OK
);
665 wxTheApp
= (* wxApp::GetInitializerFunction()) ();
669 MessageBox(NULL
, "You have to define an instance of wxApp!", "wxWindows Error", MB_APPLMODAL
| MB_ICONSTOP
| MB_OK
);
674 wxTheApp
->argv
= NULL
;
676 wxTheApp
->OnInitGui();
680 if (wxTheApp
->GetTopWindow() && wxTheApp
->GetTopWindow()->GetHWND()) {
681 wxTheApp
->GetTopWindow()->Show(TRUE
);
688 //// Static member initialization
690 wxAppInitializerFunction
wxApp::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
697 m_wantDebugOutput
= TRUE
;
702 m_printMode
= wxPRINT_WINDOWS
;
704 m_printMode
= wxPRINT_POSTSCRIPT
;
706 m_exitOnFrameDelete
= TRUE
;
712 // Delete command-line args
714 for (i
= 0; i
< argc
; i
++)
721 bool wxApp::Initialized()
729 #ifdef _WINDLL // Assume initialized if DLL (no way of telling)
735 * Get and process a message, returning FALSE if WM_QUIT
739 bool wxApp::DoMessage()
741 if (!::GetMessage(&s_currentMsg
, (HWND
) NULL
, 0, 0))
746 // Process the message
747 if (!ProcessMessage((WXMSG
*)&s_currentMsg
))
749 ::TranslateMessage(&s_currentMsg
);
750 wxApp::sm_lastMessageTime
= s_currentMsg
.time
; /* MATTHEW: timeStamp impl. */
751 ::DispatchMessage(&s_currentMsg
);
757 * Keep trying to process messages until WM_QUIT
760 * If there are messages to be processed, they will all be
761 * processed and OnIdle will not be called.
762 * When there are no more messages, OnIdle is called.
763 * If OnIdle requests more time,
764 * it will be repeatedly called so long as there are no pending messages.
765 * A 'feature' of this is that once OnIdle has decided that no more processing
766 * is required, then it won't get processing time until further messages
767 * are processed (it'll sit in DoMessage).
770 int wxApp::MainLoop()
775 while (!::PeekMessage(&s_currentMsg
, 0, 0, 0, PM_NOREMOVE
) &&
781 return s_currentMsg
.wParam
;
784 // Returns TRUE if more time is needed.
785 bool wxApp::ProcessIdle()
788 event
.SetEventObject(this);
791 return event
.MoreRequested();
794 void wxApp::ExitMainLoop()
799 bool wxApp::Pending()
801 return (::PeekMessage(&s_currentMsg
, 0, 0, 0, PM_NOREMOVE
) != 0) ;
804 void wxApp::Dispatch()
811 * Give all windows a chance to preprocess
812 * the message. Some may have accelerator tables, or have
815 bool wxApp::ProcessMessage(WXMSG
*Msg
)
817 MSG
*msg
= (MSG
*)Msg
;
821 // Try translations first; find the youngest window with
822 // a translation table.
823 for (hWnd
= msg
->hwnd
; hWnd
!= NULL
; hWnd
= ::GetParent(hWnd
))
825 wxWindow
*wnd
= wxFindWinFromHandle((WXHWND
) hWnd
);
828 if (wnd
->MSWTranslateMessage(Msg
))
833 // Anyone for a non-translation message? Try youngest descendants first.
834 for (hWnd
= msg
->hwnd
; hWnd
!= NULL
; hWnd
= ::GetParent(hWnd
))
836 wxWindow
*wnd
= wxFindWinFromHandle((WXHWND
) hWnd
);
839 if (wnd
->MSWProcessMessage(Msg
))
846 void wxApp::OnIdle(wxIdleEvent
& event
)
848 static bool s_inOnIdle
= FALSE
;
850 // Avoid recursion (via ProcessEvent default case)
856 // 'Garbage' collection of windows deleted with Close().
857 DeletePendingObjects();
859 // flush the logged messages if any
860 wxLog
*pLog
= wxLog::GetActiveTarget();
861 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
864 // Send OnIdle events to all windows
865 if ( SendIdleEvents() )
867 // SendIdleEvents() returns TRUE if at least one window requested more
869 event
.RequestMore(TRUE
);
872 // give a chance to all other threads to perform GUI calls
880 // Send idle event to all top-level windows
881 bool wxApp::SendIdleEvents()
883 bool needMore
= FALSE
;
884 wxNode
* node
= wxTopLevelWindows
.First();
887 wxWindow
* win
= (wxWindow
*) node
->Data();
888 if (SendIdleEvents(win
))
897 // Send idle event to window and all subwindows
898 bool wxApp::SendIdleEvents(wxWindow
* win
)
900 bool needMore
= FALSE
;
903 event
.SetEventObject(win
);
904 win
->GetEventHandler()->ProcessEvent(event
);
906 if (event
.MoreRequested())
909 wxNode
* node
= win
->GetChildren().First();
912 wxWindow
* win
= (wxWindow
*) node
->Data();
913 if (SendIdleEvents(win
))
921 void wxApp::DeletePendingObjects()
923 wxNode
*node
= wxPendingDelete
.First();
926 wxObject
*obj
= (wxObject
*)node
->Data();
930 if (wxPendingDelete
.Member(obj
))
933 // Deleting one object may have deleted other pending
934 // objects, so start from beginning of list again.
935 node
= wxPendingDelete
.First();
939 void wxApp::OnEndSession(wxCloseEvent
& event
)
942 GetTopWindow()->Close(TRUE
);
945 // Default behaviour: close the application with prompts. The
946 // user can veto the close, and therefore the end session.
947 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
951 if (!GetTopWindow()->Close(!event
.CanVeto()))
956 wxLog
* wxApp::CreateLogTarget()
961 wxWindow
* wxApp::GetTopWindow() const
965 else if (wxTopLevelWindows
.Number() > 0)
966 return (wxWindow
*) wxTopLevelWindows
.First()->Data();
971 int wxApp::GetComCtl32Version() const
973 // have we loaded COMCTL32 yet?
974 HMODULE theModule
= ::GetModuleHandle("COMCTL32");
977 // if so, then we can check for the version
980 // InitCommonControlsEx is unique to 4.7 and later
981 FARPROC theProc
= ::GetProcAddress(theModule
, "InitCommonControlsEx");
984 { // not found, must be 4.00
989 // The following symbol are unique to 4.71
991 // FlatSB_EnableScrollBar FlatSB_GetScrollInfo FlatSB_GetScrollPos
992 // FlatSB_GetScrollProp FlatSB_GetScrollRange FlatSB_SetScrollInfo
993 // FlatSB_SetScrollPos FlatSB_SetScrollProp FlatSB_SetScrollRange
994 // FlatSB_ShowScrollBar
995 // _DrawIndirectImageList _DuplicateImageList
997 // UninitializeFlatSB
998 // we could check for any of these - I chose DllInstall
999 FARPROC theProc
= ::GetProcAddress(theModule
, "DllInstall");
1002 // not found, must be 4.70
1006 { // found, must be 4.71
1017 FatalAppExit(0, "Fatal error: exiting");
1020 // Yield to incoming messages
1024 // We want to go back to the main message loop
1025 // if we see a WM_QUIT. (?)
1026 while (PeekMessage(&msg
, NULL
, 0, 0, PM_NOREMOVE
) && msg
.message
!= WM_QUIT
)
1028 if (!wxTheApp
->DoMessage())
1035 HINSTANCE
wxGetInstance()
1040 // For some reason, with MSVC++ 1.5, WinMain isn't linked in properly
1041 // if in a separate file. So include it here to ensure it's linked.
1042 #if (defined(_MSC_VER) && !defined(__WIN32__)) || defined(__GNUWIN32__)