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"
39 #include "wx/msw/private.h"
41 #include "wx/module.h"
44 #include "wx/thread.h"
46 // define the array of MSG strutures
47 WX_DECLARE_OBJARRAY(MSG
, wxMsgArray
);
49 #include "wx/arrimpl.cpp"
51 WX_DEFINE_OBJARRAY(wxMsgArray
);
52 #endif // wxUSE_THREADS
54 #if wxUSE_WX_RESOURCES
55 #include "wx/resource.h"
58 // To UG: there's no point in putting this #if here
59 // if you don't do the same for the Ole calls further down.
60 // Also, OLE is used not just for drag and drop (it's used by automatn.cpp).
61 // #if wxUSE_DRAG_AND_DROP
62 #if !defined(__GNUWIN32__) && !defined(__SC__) && !defined(__SALFORDC__)
71 #if defined(__WIN95__) && !defined(__GNUWIN32__)
75 // use debug CRT functions for memory leak detections in VC++ if we're not
76 // using wxWindows own methods
77 #if defined(__WXDEBUG__) && defined(_MSC_VER) && !wxUSE_GLOBAL_MEMORY_OPERATORS && !defined(__NO_VC_CRTDBG__)
78 #define wxUSE_VC_CRTDBG
80 #undef wxUSE_VC_CRTDBG
83 #ifdef wxUSE_VC_CRTDBG
84 // VC++ uses this macro as debug/release mode indicator
89 /* Need to undef new if including crtdbg.h */
96 #if defined(__WXDEBUG__) && wxUSE_GLOBAL_MEMORY_OPERATORS && wxUSE_DEBUG_NEW_ALWAYS
97 #define new new(__FILE__,__LINE__)
99 #endif // wxUSE_VC_CRTDBG
101 extern char *wxBuffer
;
102 extern char *wxOsVersion
;
103 extern wxList
*wxWinHandleList
;
104 extern wxList WXDLLEXPORT wxPendingDelete
;
105 extern void wxSetKeyboardHook(bool doIt
);
106 extern wxCursor
*g_globalCursor
;
108 HINSTANCE wxhInstance
= 0;
109 static MSG s_currentMsg
;
110 wxApp
*wxTheApp
= NULL
;
112 // @@ why not const? and not static?
113 char wxFrameClassName
[] = "wxFrameClass";
114 char wxMDIFrameClassName
[] = "wxMDIFrameClass";
115 char wxMDIChildFrameClassName
[] = "wxMDIChildFrameClass";
116 char wxPanelClassName
[] = "wxPanelClass";
117 char wxCanvasClassName
[] = "wxCanvasClass";
119 HICON wxSTD_FRAME_ICON
= NULL
;
120 HICON wxSTD_MDICHILDFRAME_ICON
= NULL
;
121 HICON wxSTD_MDIPARENTFRAME_ICON
= NULL
;
123 HICON wxDEFAULT_FRAME_ICON
= NULL
;
124 HICON wxDEFAULT_MDICHILDFRAME_ICON
= NULL
;
125 HICON wxDEFAULT_MDIPARENTFRAME_ICON
= NULL
;
127 HBRUSH wxDisableButtonBrush
= 0;
129 LRESULT APIENTRY
wxWndProc(HWND
, UINT
, WPARAM
, LPARAM
);
131 #if !USE_SHARED_LIBRARY
132 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
134 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
135 EVT_IDLE(wxApp::OnIdle
)
136 EVT_END_SESSION(wxApp::OnEndSession
)
137 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession
)
141 long wxApp::sm_lastMessageTime
= 0;
144 static HINSTANCE gs_hRichEdit
= NULL
;
149 bool wxApp::Initialize()
151 // Some people may wish to use this, but
152 // probably it shouldn't be here by default.
154 // wxRedirectIOToConsole();
157 wxBuffer
= new char[1500];
159 #ifdef wxUSE_VC_CRTDBG
160 // do check for memory leaks on program exit
161 // (another useful flag is _CRTDBG_DELAY_FREE_MEM_DF which doesn't free
162 // deallocated memory which may be used to simulate low-memory condition)
163 _CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG
) | _CRTDBG_LEAK_CHECK_DF
);
164 #endif // debug build under MS VC++
166 wxClassInfo::InitializeClasses();
169 wxGetResource("wxWindows", "OsVersion", &wxOsVersion
);
172 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
173 wxTheColourDatabase
->Initialize();
175 wxInitializeStockLists();
176 wxInitializeStockObjects();
178 #if wxUSE_WX_RESOURCES
179 wxInitializeResourceSystem();
182 wxBitmap::InitStandardHandlers();
184 #if defined(__WIN95__)
185 InitCommonControls();
186 gs_hRichEdit
= LoadLibrary("RICHED32.DLL");
188 if (gs_hRichEdit
== NULL
)
190 wxMessageBox("Could not initialise Rich Edit DLL");
196 // for OLE, enlarge message queue to be as large as possible
197 while (!SetMessageQueue(iMsg
) && (iMsg
-= 8));
201 dwOleVer = CoBuildVersion();
203 // check the OLE library version
204 if (rmm != HIWORD(dwOleVer))
206 wxMessageBox("Incorrect version of OLE libraries.");
211 #if !defined(__GNUWIN32__) && !defined(__SC__) && !defined(__SALFORDC__)
212 // we need to initialize OLE library
213 if ( FAILED(::OleInitialize(NULL
)) )
214 wxFatalError(_("Cannot initialize OLE"));
218 if (!Ctl3dRegister(wxhInstance
))
219 wxFatalError("Cannot register CTL3D");
221 Ctl3dAutoSubclass(wxhInstance
);
224 g_globalCursor
= new wxCursor
;
226 wxSTD_FRAME_ICON
= LoadIcon(wxhInstance
, "wxSTD_FRAME");
227 wxSTD_MDIPARENTFRAME_ICON
= LoadIcon(wxhInstance
, "wxSTD_MDIPARENTFRAME");
228 wxSTD_MDICHILDFRAME_ICON
= LoadIcon(wxhInstance
, "wxSTD_MDICHILDFRAME");
230 wxDEFAULT_FRAME_ICON
= LoadIcon(wxhInstance
, "wxDEFAULT_FRAME");
231 wxDEFAULT_MDIPARENTFRAME_ICON
= LoadIcon(wxhInstance
, "wxDEFAULT_MDIPARENTFRAME");
232 wxDEFAULT_MDICHILDFRAME_ICON
= LoadIcon(wxhInstance
, "wxDEFAULT_MDICHILDFRAME");
234 RegisterWindowClasses();
236 // Create the brush for disabling bitmap buttons
239 lb
.lbStyle
= BS_PATTERN
;
240 lb
.lbHatch
= (int)LoadBitmap( wxhInstance
, "wxDISABLE_BUTTON_BITMAP" ) ;
241 wxDisableButtonBrush
= ::CreateBrushIndirect( & lb
) ;
242 ::DeleteObject( (HGDIOBJ
)lb
.lbHatch
) ;
248 wxWinHandleList
= new wxList(wxKEY_INTEGER
);
250 // This is to foil optimizations in Visual C++ that
251 // throw out dummy.obj.
252 #if defined(_MSC_VER) && !defined(WXMAKINGDLL)
253 extern char wxDummyChar
;
254 if (wxDummyChar
) wxDummyChar
++;
257 wxSetKeyboardHook(TRUE
);
259 wxModule::RegisterModules();
260 if (!wxModule::InitializeModules())
265 //// RegisterWindowClasses
267 bool wxApp::RegisterWindowClasses()
269 ///////////////////////////////////////////////////////////////////////
270 // Register the frame window class.
271 WNDCLASS wndclass
; // Structure used to register Windows class.
273 wndclass
.style
= CS_HREDRAW
| CS_VREDRAW
;
274 wndclass
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
275 wndclass
.cbClsExtra
= 0;
276 wndclass
.cbWndExtra
= sizeof( DWORD
); // was 4
277 wndclass
.hInstance
= wxhInstance
;
278 wndclass
.hIcon
= NULL
; // wxSTD_FRAME_ICON;
279 wndclass
.hCursor
= LoadCursor( NULL
, IDC_ARROW
);
280 wndclass
.hbrBackground
= (HBRUSH
)(COLOR_APPWORKSPACE
+1) ;
281 // wndclass.hbrBackground = GetStockObject( WHITE_BRUSH );
282 wndclass
.lpszMenuName
= NULL
;
283 #ifdef _MULTIPLE_INSTANCES
284 sprintf( wxFrameClassName
,"wxFrameClass%d", wxhInstance
);
286 wndclass
.lpszClassName
= wxFrameClassName
;
288 if (!RegisterClass( &wndclass
))
290 // wxFatalError("Can't register Frame Window class");
293 ///////////////////////////////////////////////////////////////////////
294 // Register the MDI frame window class.
295 WNDCLASS wndclass1
; // Structure used to register Windows class.
297 wndclass1
.style
= CS_HREDRAW
| CS_VREDRAW
;
298 wndclass1
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
299 wndclass1
.cbClsExtra
= 0;
300 wndclass1
.cbWndExtra
= sizeof( DWORD
); // was 4
301 wndclass1
.hInstance
= wxhInstance
;
302 wndclass1
.hIcon
= NULL
; // wxSTD_MDIPARENTFRAME_ICON;
303 wndclass1
.hCursor
= LoadCursor( NULL
, IDC_ARROW
);
304 // wndclass1.hbrBackground = (HBRUSH)(COLOR_APPWORKSPACE+1) ;
305 wndclass1
.hbrBackground
= NULL
;
306 wndclass1
.lpszMenuName
= NULL
;
308 wndclass1
.lpszClassName
= wxMDIFrameClassName
;
309 if (!RegisterClass( &wndclass1
))
311 // wxFatalError("Can't register MDI Frame window class");
315 ///////////////////////////////////////////////////////////////////////
316 // Register the MDI child frame window class.
317 WNDCLASS wndclass4
; // Structure used to register Windows class.
319 wndclass4
.style
= CS_HREDRAW
| CS_VREDRAW
;
320 wndclass4
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
321 wndclass4
.cbClsExtra
= 0;
322 wndclass4
.cbWndExtra
= sizeof( DWORD
); // was 4
323 wndclass4
.hInstance
= wxhInstance
;
324 wndclass4
.hIcon
= NULL
; // wxSTD_MDICHILDFRAME_ICON;
325 wndclass4
.hCursor
= LoadCursor( NULL
, IDC_ARROW
);
326 // TODO: perhaps this should be NULL so that Windows doesn't
327 // paint the background itself (would OnEraseBackground duplicate
329 wndclass4
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+1) ;
330 // wndclass4.hbrBackground = NULL;
331 wndclass4
.lpszMenuName
= NULL
;
332 wndclass4
.lpszClassName
= wxMDIChildFrameClassName
;
334 if (!RegisterClass( &wndclass4
))
336 // wxFatalError("Can't register MDI child frame window class");
340 ///////////////////////////////////////////////////////////////////////
341 // Register the panel window class.
342 WNDCLASS wndclass2
; // Structure used to register Windows class.
343 memset(&wndclass2
, 0, sizeof(WNDCLASS
)); // start with NULL defaults
344 // Use CS_OWNDC to avoid messing about restoring the context
345 // for every graphic operation.
346 wndclass2
.style
= CS_HREDRAW
| CS_VREDRAW
;
347 wndclass2
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
348 wndclass2
.cbClsExtra
= 0;
349 wndclass2
.cbWndExtra
= sizeof( DWORD
); // was 4
350 wndclass2
.hInstance
= wxhInstance
;
351 wndclass2
.hIcon
= NULL
;
352 wndclass2
.hCursor
= NULL
;
353 // wndclass2.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1) ;
354 wndclass2
.hbrBackground
= (HBRUSH
) GetStockObject( LTGRAY_BRUSH
);
355 wndclass2
.lpszMenuName
= NULL
;
356 wndclass2
.lpszClassName
= wxPanelClassName
;
357 if (!RegisterClass( &wndclass2
))
359 // wxFatalError("Can't register Panel Window class");
363 ///////////////////////////////////////////////////////////////////////
364 // Register the canvas and textsubwindow class name
365 WNDCLASS wndclass3
; // Structure used to register Windows class.
366 memset(&wndclass3
, 0, sizeof(WNDCLASS
)); // start with NULL defaults
367 // Use CS_OWNDC to avoid messing about restoring the context
368 // for every graphic operation.
369 // wndclass3.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS ;
371 wndclass3
.style
= CS_HREDRAW
| CS_VREDRAW
| CS_DBLCLKS
;
372 wndclass3
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
373 wndclass3
.cbClsExtra
= 0;
374 wndclass3
.cbWndExtra
= sizeof( DWORD
); // was 4
375 wndclass3
.hInstance
= wxhInstance
;
376 wndclass3
.hIcon
= NULL
;
377 wndclass3
.hCursor
= NULL
;
378 // wndclass3.hbrBackground = (HBRUSH)(COLOR_WINDOW+1) ;
379 wndclass3
.hbrBackground
= NULL
;
380 wndclass3
.lpszMenuName
= NULL
;
381 wndclass3
.lpszClassName
= wxCanvasClassName
;
382 if (!RegisterClass( &wndclass3
))
384 // wxFatalError("Can't register Canvas class");
391 //// Convert Windows to argc, argv style
393 void wxApp::ConvertToStandardCommandArgs(char* lpCmdLine
)
397 wxString
cmdLine(lpCmdLine
);
400 // Get application name
401 char name
[260]; // 260 is MAX_PATH value from windef.h
402 ::GetModuleFileName(wxhInstance
, name
, WXSIZEOF(name
));
404 // GNUWIN32 already fills in the first arg with the application name.
405 #if !defined(__GNUWIN32__)
410 strcpy(name
, wxFileNameFromPath(name
));
411 wxStripExtension(name
);
412 wxTheApp
->SetAppName(name
);
415 // Treat strings enclosed in double-quotes as single arguments
417 int len
= cmdLine
.Length();
421 while ((i
< len
) && isspace(cmdLine
.GetChar(i
)))
426 if (cmdLine
.GetChar(i
) == '"') // We found the start of a string
430 while ((i
< len
) && (cmdLine
.GetChar(i
) != '"'))
433 wxString
arg(cmdLine
.Mid(first
, (i
- first
)));
439 i
++; // Skip past 2nd quote
441 else // Unquoted argument
444 while ((i
< len
) && !isspace(cmdLine
.GetChar(i
)))
447 wxString
arg(cmdLine
.Mid(first
, (i
- first
)));
455 wxTheApp
->argv
= new char*[count
+ 1];
456 for (i
= 0; i
< count
; i
++)
458 wxString
arg(args
[i
]);
459 wxTheApp
->argv
[i
] = copystring((const char*)arg
);
461 wxTheApp
->argv
[count
] = NULL
; // argv[] is a NULL-terminated list
462 wxTheApp
->argc
= count
;
465 //// Cleans up any wxWindows internal structures left lying around
467 void wxApp::CleanUp()
470 wxModule::CleanUpModules();
472 #if wxUSE_WX_RESOURCES
473 wxCleanUpResourceSystem();
475 // wxDefaultResourceTable->ClearTable();
478 // Indicate that the cursor can be freed,
479 // so that cursor won't be deleted by deleting
480 // the bitmap list before g_globalCursor goes out
481 // of scope (double deletion of the cursor).
482 wxSetCursor(wxNullCursor
);
483 delete g_globalCursor
;
485 wxDeleteStockObjects() ;
487 // Destroy all GDI lists, etc.
488 wxDeleteStockLists();
490 delete wxTheColourDatabase
;
491 wxTheColourDatabase
= NULL
;
493 wxBitmap::CleanUpHandlers();
498 //// WINDOWS-SPECIFIC CLEANUP
500 wxSetKeyboardHook(FALSE
);
503 if (gs_hRichEdit
!= NULL
)
504 FreeLibrary(gs_hRichEdit
);
511 if (wxSTD_FRAME_ICON
)
512 DestroyIcon(wxSTD_FRAME_ICON
);
513 if (wxSTD_MDICHILDFRAME_ICON
)
514 DestroyIcon(wxSTD_MDICHILDFRAME_ICON
);
515 if (wxSTD_MDIPARENTFRAME_ICON
)
516 DestroyIcon(wxSTD_MDIPARENTFRAME_ICON
);
518 if (wxDEFAULT_FRAME_ICON
)
519 DestroyIcon(wxDEFAULT_FRAME_ICON
);
520 if (wxDEFAULT_MDICHILDFRAME_ICON
)
521 DestroyIcon(wxDEFAULT_MDICHILDFRAME_ICON
);
522 if (wxDEFAULT_MDIPARENTFRAME_ICON
)
523 DestroyIcon(wxDEFAULT_MDIPARENTFRAME_ICON
);
525 if ( wxDisableButtonBrush
)
526 ::DeleteObject( wxDisableButtonBrush
) ;
528 #if !defined(__GNUWIN32__) && !defined(__SC__) && !defined(__SALFORDC__)
533 Ctl3dUnregister(wxhInstance
);
537 delete wxWinHandleList
;
539 wxClassInfo::CleanUpClasses();
544 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
545 // At this point we want to check if there are any memory
546 // blocks that aren't part of the wxDebugContext itself,
547 // as a special case. Then when dumping we need to ignore
548 // wxDebugContext, too.
549 if (wxDebugContext::CountObjectsLeft() > 0)
551 wxLogDebug("There were memory leaks.");
552 wxDebugContext::Dump();
553 wxDebugContext::PrintStatistics();
555 // wxDebugContext::SetStream(NULL, NULL);
558 // do it as the very last thing because everything else can log messages
559 wxLog::DontCreateOnDemand();
560 delete wxLog::SetActiveTarget(NULL
);
563 #if !defined(_WINDLL) || (defined(_WINDLL) && defined(WXMAKINGDLL))
565 // temporarily disable this warning
567 #pragma warning(disable: 4715) // not all control paths return a value
570 //// Main wxWindows entry point
571 int wxEntry(WXHINSTANCE hInstance
,
572 WXHINSTANCE
WXUNUSED(hPrevInstance
),
577 // take everything into a try-except block in release build
578 // FIXME other compilers must support Win32 SEH (structured exception
579 // handling) too, just find the appropriate keyword in their docs!
580 // Please note that it's _not_ the same as C++ exceptions!
581 #if !defined(__WXDEBUG__) && defined(_MSC_VER)
582 #define CATCH_PROGRAM_EXCEPTIONS
586 #undef CATCH_PROGRAM_EXCEPTIONS
589 wxhInstance
= (HINSTANCE
) hInstance
;
591 if (!wxApp::Initialize())
594 // create the application object or ensure that one already exists
597 // The app may have declared a global application object, but we recommend
598 // the IMPLEMENT_APP macro is used instead, which sets an initializer
599 // function for delayed, dynamic app object construction.
600 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
601 "No initializer - use IMPLEMENT_APP macro." );
603 wxTheApp
= (*wxApp::GetInitializerFunction()) ();
606 wxCHECK_MSG( wxTheApp
, 0, "You have to define an instance of wxApp!" );
608 // save the WinMain() parameters
609 wxTheApp
->ConvertToStandardCommandArgs(lpCmdLine
);
610 wxTheApp
->m_nCmdShow
= nCmdShow
;
612 // GUI-specific initialisation. In fact on Windows we don't have any,
613 // but this call is provided for compatibility across platforms.
614 wxTheApp
->OnInitGui() ;
618 if ( wxTheApp
->OnInit() )
622 retValue
= wxTheApp
->OnRun();
625 //else: app initialization failed, so we skipped OnRun()
627 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
630 // Forcibly delete the window.
631 if ( topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
632 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
634 topWindow
->Close(TRUE
);
635 wxTheApp
->DeletePendingObjects();
640 wxTheApp
->SetTopWindow(NULL
);
646 // flush the logged messages if any
647 wxLog
*pLog
= wxLog::GetActiveTarget();
648 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
656 #ifdef CATCH_PROGRAM_EXCEPTIONS
658 __except ( EXCEPTION_EXECUTE_HANDLER
) {
661 wxTheApp->OnFatalException();
664 ::ExitProcess(3); // the same exit code as abort()
668 #endif // CATCH_PROGRAM_EXCEPTIONS
671 // restore warning state
673 #pragma warning(default: 4715) // not all control paths return a value
678 //// Entry point for DLLs
680 int wxEntry(WXHINSTANCE hInstance
)
682 wxhInstance
= (HINSTANCE
) hInstance
;
685 // The app may have declared a global application object, but we recommend
686 // the IMPLEMENT_APP macro is used instead, which sets an initializer function
687 // for delayed, dynamic app object construction.
691 if (!wxApp::GetInitializerFunction())
693 MessageBox(NULL
, "No initializer - use IMPLEMENT_APP macro.", "wxWindows Error", MB_APPLMODAL
| MB_ICONSTOP
| MB_OK
);
697 wxTheApp
= (* wxApp::GetInitializerFunction()) ();
701 MessageBox(NULL
, "You have to define an instance of wxApp!", "wxWindows Error", MB_APPLMODAL
| MB_ICONSTOP
| MB_OK
);
706 wxTheApp
->argv
= NULL
;
708 wxTheApp
->OnInitGui();
712 if (wxTheApp
->GetTopWindow() && wxTheApp
->GetTopWindow()->GetHWND()) {
713 wxTheApp
->GetTopWindow()->Show(TRUE
);
720 //// Static member initialization
722 wxAppInitializerFunction
wxApp::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
729 m_wantDebugOutput
= TRUE
;
734 m_printMode
= wxPRINT_WINDOWS
;
736 m_printMode
= wxPRINT_POSTSCRIPT
;
738 m_exitOnFrameDelete
= TRUE
;
744 // Delete command-line args
746 for (i
= 0; i
< argc
; i
++)
753 bool wxApp::Initialized()
761 #ifdef _WINDLL // Assume initialized if DLL (no way of telling)
767 * Get and process a message, returning FALSE if WM_QUIT
768 * received (and also set the flag telling the app to exit the main loop)
771 bool wxApp::DoMessage()
773 BOOL rc
= ::GetMessage(&s_currentMsg
, (HWND
) NULL
, 0, 0);
783 // should never happen, but let's test for it nevertheless
784 wxLogLastError("GetMessage");
789 wxASSERT_MSG( wxThread::IsMain(),
790 "only the main thread can process Windows messages" );
792 static bool s_hadGuiLock
= TRUE
;
793 static wxMsgArray s_aSavedMessages
;
795 // if a secondary thread owns is doing GUI calls, save all messages for
796 // later processing - we can't process them right now because it will
797 // lead to recursive library calls (and we're not reentrant)
798 if ( !wxGuiOwnedByMainThread() )
800 s_hadGuiLock
= FALSE
;
802 // leave out WM_COMMAND messages: too dangerous, sometimes
803 // the message will be processed twice
804 if ( !wxIsWaitingForThread() ||
805 s_currentMsg
.message
!= WM_COMMAND
)
807 s_aSavedMessages
.Add(s_currentMsg
);
814 // have we just regained the GUI lock? if so, post all of the saved
817 // FIXME of course, it's not _exactly_ the same as processing the
818 // messages normally - expect some things to break...
823 size_t count
= s_aSavedMessages
.Count();
824 for ( size_t n
= 0; n
< count
; n
++ )
826 MSG
& msg
= s_aSavedMessages
[n
];
828 if ( !ProcessMessage((WXMSG
*)&msg
) )
830 ::TranslateMessage(&msg
);
831 ::DispatchMessage(&msg
);
835 s_aSavedMessages
.Empty();
838 #endif // wxUSE_THREADS
840 // Process the message
841 if ( !ProcessMessage((WXMSG
*)&s_currentMsg
) )
843 ::TranslateMessage(&s_currentMsg
);
844 wxApp::sm_lastMessageTime
= s_currentMsg
.time
; /* MATTHEW: timeStamp impl. */
845 ::DispatchMessage(&s_currentMsg
);
853 * Keep trying to process messages until WM_QUIT
856 * If there are messages to be processed, they will all be
857 * processed and OnIdle will not be called.
858 * When there are no more messages, OnIdle is called.
859 * If OnIdle requests more time,
860 * it will be repeatedly called so long as there are no pending messages.
861 * A 'feature' of this is that once OnIdle has decided that no more processing
862 * is required, then it won't get processing time until further messages
863 * are processed (it'll sit in DoMessage).
866 int wxApp::MainLoop()
870 while ( m_keepGoing
)
873 wxMutexGuiLeaveOrEnter();
874 #endif // wxUSE_THREADS
876 while ( !::PeekMessage(&s_currentMsg
, 0, 0, 0, PM_NOREMOVE
) &&
884 return s_currentMsg
.wParam
;
887 // Returns TRUE if more time is needed.
888 bool wxApp::ProcessIdle()
891 event
.SetEventObject(this);
894 return event
.MoreRequested();
897 void wxApp::ExitMainLoop()
902 bool wxApp::Pending()
904 return (::PeekMessage(&s_currentMsg
, 0, 0, 0, PM_NOREMOVE
) != 0) ;
907 void wxApp::Dispatch()
913 * Give all windows a chance to preprocess
914 * the message. Some may have accelerator tables, or have
917 bool wxApp::ProcessMessage(WXMSG
*Msg
)
919 MSG
*msg
= (MSG
*)Msg
;
923 // Try translations first; find the youngest window with
924 // a translation table.
925 for (hWnd
= msg
->hwnd
; hWnd
!= NULL
; hWnd
= ::GetParent(hWnd
))
927 wxWindow
*wnd
= wxFindWinFromHandle((WXHWND
) hWnd
);
930 if (wnd
->MSWTranslateMessage(Msg
))
935 // Anyone for a non-translation message? Try youngest descendants first.
936 for (hWnd
= msg
->hwnd
; hWnd
!= NULL
; hWnd
= ::GetParent(hWnd
))
938 wxWindow
*wnd
= wxFindWinFromHandle((WXHWND
) hWnd
);
941 if (wnd
->MSWProcessMessage(Msg
))
948 void wxApp::OnIdle(wxIdleEvent
& event
)
950 static bool s_inOnIdle
= FALSE
;
952 // Avoid recursion (via ProcessEvent default case)
958 // 'Garbage' collection of windows deleted with Close().
959 DeletePendingObjects();
961 // flush the logged messages if any
962 wxLog
*pLog
= wxLog::GetActiveTarget();
963 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
966 // Send OnIdle events to all windows
967 if ( SendIdleEvents() )
969 // SendIdleEvents() returns TRUE if at least one window requested more
971 event
.RequestMore(TRUE
);
977 // Send idle event to all top-level windows
978 bool wxApp::SendIdleEvents()
980 bool needMore
= FALSE
;
981 wxNode
* node
= wxTopLevelWindows
.First();
984 wxWindow
* win
= (wxWindow
*) node
->Data();
985 if (SendIdleEvents(win
))
994 // Send idle event to window and all subwindows
995 bool wxApp::SendIdleEvents(wxWindow
* win
)
997 bool needMore
= FALSE
;
1000 event
.SetEventObject(win
);
1001 win
->GetEventHandler()->ProcessEvent(event
);
1003 if (event
.MoreRequested())
1006 wxNode
* node
= win
->GetChildren().First();
1009 wxWindow
* win
= (wxWindow
*) node
->Data();
1010 if (SendIdleEvents(win
))
1013 node
= node
->Next();
1018 void wxApp::DeletePendingObjects()
1020 wxNode
*node
= wxPendingDelete
.First();
1023 wxObject
*obj
= (wxObject
*)node
->Data();
1027 if (wxPendingDelete
.Member(obj
))
1030 // Deleting one object may have deleted other pending
1031 // objects, so start from beginning of list again.
1032 node
= wxPendingDelete
.First();
1036 void wxApp::OnEndSession(wxCloseEvent
& event
)
1039 GetTopWindow()->Close(TRUE
);
1042 // Default behaviour: close the application with prompts. The
1043 // user can veto the close, and therefore the end session.
1044 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
1048 if (!GetTopWindow()->Close(!event
.CanVeto()))
1053 wxLog
* wxApp::CreateLogTarget()
1055 return new wxLogGui
;
1058 wxWindow
* wxApp::GetTopWindow() const
1062 else if (wxTopLevelWindows
.Number() > 0)
1063 return (wxWindow
*) wxTopLevelWindows
.First()->Data();
1068 int wxApp::GetComCtl32Version() const
1070 // have we loaded COMCTL32 yet?
1071 HMODULE theModule
= ::GetModuleHandle("COMCTL32");
1074 // if so, then we can check for the version
1077 // InitCommonControlsEx is unique to 4.7 and later
1078 FARPROC theProc
= ::GetProcAddress(theModule
, "InitCommonControlsEx");
1081 { // not found, must be 4.00
1086 // The following symbol are unique to 4.71
1088 // FlatSB_EnableScrollBar FlatSB_GetScrollInfo FlatSB_GetScrollPos
1089 // FlatSB_GetScrollProp FlatSB_GetScrollRange FlatSB_SetScrollInfo
1090 // FlatSB_SetScrollPos FlatSB_SetScrollProp FlatSB_SetScrollRange
1091 // FlatSB_ShowScrollBar
1092 // _DrawIndirectImageList _DuplicateImageList
1094 // UninitializeFlatSB
1095 // we could check for any of these - I chose DllInstall
1096 FARPROC theProc
= ::GetProcAddress(theModule
, "DllInstall");
1099 // not found, must be 4.70
1103 { // found, must be 4.71
1114 FatalAppExit(0, "Fatal error: exiting");
1117 // Yield to incoming messages
1121 // We want to go back to the main message loop
1122 // if we see a WM_QUIT. (?)
1123 while (PeekMessage(&msg
, NULL
, 0, 0, PM_NOREMOVE
) && msg
.message
!= WM_QUIT
)
1125 if ( !wxTheApp
->DoMessage() )
1132 HINSTANCE
wxGetInstance()
1137 // For some reason, with MSVC++ 1.5, WinMain isn't linked in properly
1138 // if in a separate file. So include it here to ensure it's linked.
1139 #if (defined(_MSC_VER) && !defined(__WIN32__)) || defined(__GNUWIN32__)