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__)
70 #if defined(__WIN95__) && !defined(__GNUWIN32__)
74 // use debug CRT functions for memory leak detections in VC++ if we're not
75 // using wxWindows own methods
76 #if defined(__WXDEBUG__) && defined(_MSC_VER) && !wxUSE_GLOBAL_MEMORY_OPERATORS && !defined(__NO_VC_CRTDBG__)
77 #define wxUSE_VC_CRTDBG
79 #undef wxUSE_VC_CRTDBG
82 #ifdef wxUSE_VC_CRTDBG
83 // VC++ uses this macro as debug/release mode indicator
88 /* Need to undef new if including crtdbg.h */
95 #if defined(__WXDEBUG__) && wxUSE_GLOBAL_MEMORY_OPERATORS && wxUSE_DEBUG_NEW_ALWAYS
96 #define new new(__FILE__,__LINE__)
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__)
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 (_MSC_VER >= 800) && !defined(WXMAKINGDLL)
253 #if defined(_MSC_VER) && defined(__WIN16__) && !defined(WXMAKINGDLL)
254 extern char wxDummyChar
;
255 if (wxDummyChar
) wxDummyChar
++;
258 wxSetKeyboardHook(TRUE
);
260 wxModule::RegisterModules();
261 if (!wxModule::InitializeModules())
266 //// RegisterWindowClasses
268 bool wxApp::RegisterWindowClasses()
270 ///////////////////////////////////////////////////////////////////////
271 // Register the frame window class.
272 WNDCLASS wndclass
; // Structure used to register Windows class.
274 wndclass
.style
= CS_HREDRAW
| CS_VREDRAW
;
275 wndclass
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
276 wndclass
.cbClsExtra
= 0;
277 wndclass
.cbWndExtra
= sizeof( DWORD
); // was 4
278 wndclass
.hInstance
= wxhInstance
;
279 wndclass
.hIcon
= NULL
; // wxSTD_FRAME_ICON;
280 wndclass
.hCursor
= LoadCursor( NULL
, IDC_ARROW
);
281 wndclass
.hbrBackground
= (HBRUSH
)(COLOR_APPWORKSPACE
+1) ;
282 // wndclass.hbrBackground = GetStockObject( WHITE_BRUSH );
283 wndclass
.lpszMenuName
= NULL
;
284 #ifdef _MULTIPLE_INSTANCES
285 sprintf( wxFrameClassName
,"wxFrameClass%d", wxhInstance
);
287 wndclass
.lpszClassName
= wxFrameClassName
;
289 if (!RegisterClass( &wndclass
))
291 // wxFatalError("Can't register Frame Window class");
294 ///////////////////////////////////////////////////////////////////////
295 // Register the MDI frame window class.
296 WNDCLASS wndclass1
; // Structure used to register Windows class.
298 wndclass1
.style
= CS_HREDRAW
| CS_VREDRAW
;
299 wndclass1
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
300 wndclass1
.cbClsExtra
= 0;
301 wndclass1
.cbWndExtra
= sizeof( DWORD
); // was 4
302 wndclass1
.hInstance
= wxhInstance
;
303 wndclass1
.hIcon
= NULL
; // wxSTD_MDIPARENTFRAME_ICON;
304 wndclass1
.hCursor
= LoadCursor( NULL
, IDC_ARROW
);
305 // wndclass1.hbrBackground = (HBRUSH)(COLOR_APPWORKSPACE+1) ;
306 wndclass1
.hbrBackground
= NULL
;
307 wndclass1
.lpszMenuName
= NULL
;
309 wndclass1
.lpszClassName
= wxMDIFrameClassName
;
310 if (!RegisterClass( &wndclass1
))
312 // wxFatalError("Can't register MDI Frame window class");
316 ///////////////////////////////////////////////////////////////////////
317 // Register the MDI child frame window class.
318 WNDCLASS wndclass4
; // Structure used to register Windows class.
320 wndclass4
.style
= CS_HREDRAW
| CS_VREDRAW
;
321 wndclass4
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
322 wndclass4
.cbClsExtra
= 0;
323 wndclass4
.cbWndExtra
= sizeof( DWORD
); // was 4
324 wndclass4
.hInstance
= wxhInstance
;
325 wndclass4
.hIcon
= NULL
; // wxSTD_MDICHILDFRAME_ICON;
326 wndclass4
.hCursor
= LoadCursor( NULL
, IDC_ARROW
);
327 // TODO: perhaps this should be NULL so that Windows doesn't
328 // paint the background itself (would OnEraseBackground duplicate
330 wndclass4
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+1) ;
331 // wndclass4.hbrBackground = NULL;
332 wndclass4
.lpszMenuName
= NULL
;
333 wndclass4
.lpszClassName
= wxMDIChildFrameClassName
;
335 if (!RegisterClass( &wndclass4
))
337 // wxFatalError("Can't register MDI child frame window class");
341 ///////////////////////////////////////////////////////////////////////
342 // Register the panel window class.
343 WNDCLASS wndclass2
; // Structure used to register Windows class.
344 memset(&wndclass2
, 0, sizeof(WNDCLASS
)); // start with NULL defaults
345 // Use CS_OWNDC to avoid messing about restoring the context
346 // for every graphic operation.
347 wndclass2
.style
= CS_HREDRAW
| CS_VREDRAW
;
348 wndclass2
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
349 wndclass2
.cbClsExtra
= 0;
350 wndclass2
.cbWndExtra
= sizeof( DWORD
); // was 4
351 wndclass2
.hInstance
= wxhInstance
;
352 wndclass2
.hIcon
= NULL
;
353 wndclass2
.hCursor
= NULL
;
354 // wndclass2.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1) ;
355 wndclass2
.hbrBackground
= (HBRUSH
) GetStockObject( LTGRAY_BRUSH
);
356 wndclass2
.lpszMenuName
= NULL
;
357 wndclass2
.lpszClassName
= wxPanelClassName
;
358 if (!RegisterClass( &wndclass2
))
360 // wxFatalError("Can't register Panel Window class");
364 ///////////////////////////////////////////////////////////////////////
365 // Register the canvas and textsubwindow class name
366 WNDCLASS wndclass3
; // Structure used to register Windows class.
367 memset(&wndclass3
, 0, sizeof(WNDCLASS
)); // start with NULL defaults
368 // Use CS_OWNDC to avoid messing about restoring the context
369 // for every graphic operation.
370 // wndclass3.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS ;
372 wndclass3
.style
= CS_HREDRAW
| CS_VREDRAW
| CS_DBLCLKS
;
373 wndclass3
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
374 wndclass3
.cbClsExtra
= 0;
375 wndclass3
.cbWndExtra
= sizeof( DWORD
); // was 4
376 wndclass3
.hInstance
= wxhInstance
;
377 wndclass3
.hIcon
= NULL
;
378 wndclass3
.hCursor
= NULL
;
379 // wndclass3.hbrBackground = (HBRUSH)(COLOR_WINDOW+1) ;
380 wndclass3
.hbrBackground
= NULL
;
381 wndclass3
.lpszMenuName
= NULL
;
382 wndclass3
.lpszClassName
= wxCanvasClassName
;
383 if (!RegisterClass( &wndclass3
))
385 // wxFatalError("Can't register Canvas class");
392 //// Convert Windows to argc, argv style
394 void wxApp::ConvertToStandardCommandArgs(char* lpCmdLine
)
398 wxString
cmdLine(lpCmdLine
);
401 // Get application name
402 char name
[260]; // 260 is MAX_PATH value from windef.h
403 ::GetModuleFileName(wxhInstance
, name
, WXSIZEOF(name
));
405 // GNUWIN32 already fills in the first arg with the application name.
406 #if !defined(__GNUWIN32__)
411 strcpy(name
, wxFileNameFromPath(name
));
412 wxStripExtension(name
);
413 wxTheApp
->SetAppName(name
);
416 // Treat strings enclosed in double-quotes as single arguments
418 int len
= cmdLine
.Length();
422 while ((i
< len
) && isspace(cmdLine
.GetChar(i
)))
427 if (cmdLine
.GetChar(i
) == '"') // We found the start of a string
431 while ((i
< len
) && (cmdLine
.GetChar(i
) != '"'))
434 wxString
arg(cmdLine
.Mid(first
, (i
- first
)));
440 i
++; // Skip past 2nd quote
442 else // Unquoted argument
445 while ((i
< len
) && !isspace(cmdLine
.GetChar(i
)))
448 wxString
arg(cmdLine
.Mid(first
, (i
- first
)));
456 wxTheApp
->argv
= new char*[count
+ 1];
457 for (i
= 0; i
< count
; i
++)
459 wxString
arg(args
[i
]);
460 wxTheApp
->argv
[i
] = copystring((const char*)arg
);
462 wxTheApp
->argv
[count
] = NULL
; // argv[] is a NULL-terminated list
463 wxTheApp
->argc
= count
;
466 //// Cleans up any wxWindows internal structures left lying around
468 void wxApp::CleanUp()
471 wxModule::CleanUpModules();
473 #if wxUSE_WX_RESOURCES
474 wxCleanUpResourceSystem();
476 // wxDefaultResourceTable->ClearTable();
479 // Indicate that the cursor can be freed,
480 // so that cursor won't be deleted by deleting
481 // the bitmap list before g_globalCursor goes out
482 // of scope (double deletion of the cursor).
483 wxSetCursor(wxNullCursor
);
484 delete g_globalCursor
;
486 wxDeleteStockObjects() ;
488 // Destroy all GDI lists, etc.
489 wxDeleteStockLists();
491 delete wxTheColourDatabase
;
492 wxTheColourDatabase
= NULL
;
494 wxBitmap::CleanUpHandlers();
499 //// WINDOWS-SPECIFIC CLEANUP
501 wxSetKeyboardHook(FALSE
);
504 if (gs_hRichEdit
!= NULL
)
505 FreeLibrary(gs_hRichEdit
);
512 if (wxSTD_FRAME_ICON
)
513 DestroyIcon(wxSTD_FRAME_ICON
);
514 if (wxSTD_MDICHILDFRAME_ICON
)
515 DestroyIcon(wxSTD_MDICHILDFRAME_ICON
);
516 if (wxSTD_MDIPARENTFRAME_ICON
)
517 DestroyIcon(wxSTD_MDIPARENTFRAME_ICON
);
519 if (wxDEFAULT_FRAME_ICON
)
520 DestroyIcon(wxDEFAULT_FRAME_ICON
);
521 if (wxDEFAULT_MDICHILDFRAME_ICON
)
522 DestroyIcon(wxDEFAULT_MDICHILDFRAME_ICON
);
523 if (wxDEFAULT_MDIPARENTFRAME_ICON
)
524 DestroyIcon(wxDEFAULT_MDIPARENTFRAME_ICON
);
526 if ( wxDisableButtonBrush
)
527 ::DeleteObject( wxDisableButtonBrush
) ;
529 #if !defined(__GNUWIN32__) && !defined(__SC__)
534 Ctl3dUnregister(wxhInstance
);
538 delete wxWinHandleList
;
540 wxClassInfo::CleanUpClasses();
545 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
546 // At this point we want to check if there are any memory
547 // blocks that aren't part of the wxDebugContext itself,
548 // as a special case. Then when dumping we need to ignore
549 // wxDebugContext, too.
550 if (wxDebugContext::CountObjectsLeft() > 0)
552 wxLogDebug("There were memory leaks.");
553 wxDebugContext::Dump();
554 wxDebugContext::PrintStatistics();
556 // wxDebugContext::SetStream(NULL, NULL);
559 // do it as the very last thing because everything else can log messages
560 wxLog::DontCreateOnDemand();
561 delete wxLog::SetActiveTarget(NULL
);
564 #if !defined(_WINDLL) || (defined(_WINDLL) && defined(WXMAKINGDLL))
566 //// Main wxWindows entry point
567 int wxEntry(WXHINSTANCE hInstance
,
568 WXHINSTANCE
WXUNUSED(hPrevInstance
),
573 #if !defined(__WXDEBUG__) && !defined(__BORLANDC__) && !defined(__WATCOMC__) // take everything into a try-except block in release build
577 wxhInstance
= (HINSTANCE
) hInstance
;
579 if (!wxApp::Initialize())
582 // create the application object or ensure that one already exists
585 // The app may have declared a global application object, but we recommend
586 // the IMPLEMENT_APP macro is used instead, which sets an initializer
587 // function for delayed, dynamic app object construction.
588 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
589 "No initializer - use IMPLEMENT_APP macro." );
591 wxTheApp
= (*wxApp::GetInitializerFunction()) ();
594 wxCHECK_MSG( wxTheApp
, 0, "You have to define an instance of wxApp!" );
596 // save the WinMain() parameters
597 wxTheApp
->ConvertToStandardCommandArgs(lpCmdLine
);
598 wxTheApp
->m_nCmdShow
= nCmdShow
;
600 // GUI-specific initialisation. In fact on Windows we don't have any,
601 // but this call is provided for compatibility across platforms.
602 wxTheApp
->OnInitGui() ;
606 if ( wxTheApp
->OnInit() )
610 retValue
= wxTheApp
->OnRun();
613 //else: app initialization failed, so we skipped OnRun()
615 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
618 // Forcibly delete the window.
619 if ( topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
620 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
622 topWindow
->Close(TRUE
);
623 wxTheApp
->DeletePendingObjects();
628 wxTheApp
->SetTopWindow(NULL
);
634 // flush the logged messages if any
635 wxLog
*pLog
= wxLog::GetActiveTarget();
636 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
643 #if !defined(__WXDEBUG__) && !defined(__BORLANDC__) && !defined(__WATCOMC__) // catch exceptions only in release build
645 except ( EXCEPTION_EXECUTE_HANDLER
) {
648 wxTheApp->OnFatalException();
651 ::ExitProcess(3); // the same exit code as abort()
659 //// Entry point for DLLs
661 int wxEntry(WXHINSTANCE hInstance
)
663 wxhInstance
= (HINSTANCE
) hInstance
;
666 // The app may have declared a global application object, but we recommend
667 // the IMPLEMENT_APP macro is used instead, which sets an initializer function
668 // for delayed, dynamic app object construction.
672 if (!wxApp::GetInitializerFunction())
674 MessageBox(NULL
, "No initializer - use IMPLEMENT_APP macro.", "wxWindows Error", MB_APPLMODAL
| MB_ICONSTOP
| MB_OK
);
678 wxTheApp
= (* wxApp::GetInitializerFunction()) ();
682 MessageBox(NULL
, "You have to define an instance of wxApp!", "wxWindows Error", MB_APPLMODAL
| MB_ICONSTOP
| MB_OK
);
687 wxTheApp
->argv
= NULL
;
689 wxTheApp
->OnInitGui();
693 if (wxTheApp
->GetTopWindow() && wxTheApp
->GetTopWindow()->GetHWND()) {
694 wxTheApp
->GetTopWindow()->Show(TRUE
);
701 //// Static member initialization
703 wxAppInitializerFunction
wxApp::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
710 m_wantDebugOutput
= TRUE
;
715 m_printMode
= wxPRINT_WINDOWS
;
717 m_printMode
= wxPRINT_POSTSCRIPT
;
719 m_exitOnFrameDelete
= TRUE
;
725 // Delete command-line args
727 for (i
= 0; i
< argc
; i
++)
734 bool wxApp::Initialized()
742 #ifdef _WINDLL // Assume initialized if DLL (no way of telling)
748 * Get and process a message, returning FALSE if WM_QUIT
749 * received (and also set the flag telling the app to exit the main loop)
752 bool wxApp::DoMessage()
754 BOOL rc
= ::GetMessage(&s_currentMsg
, (HWND
) NULL
, 0, 0);
764 // should never happen, but let's test for it nevertheless
765 wxLogLastError("GetMessage");
770 wxASSERT_MSG( wxThread::IsMain(),
771 "only the main thread can process Windows messages" );
773 static bool s_hadGuiLock
= TRUE
;
774 static wxMsgArray s_aSavedMessages
;
776 // if a secondary thread owns is doing GUI calls, save all messages for
777 // later processing - we can't process them right now because it will
778 // lead to recursive library calls (and we're not reentrant)
779 if ( !wxGuiOwnedByMainThread() )
781 s_hadGuiLock
= FALSE
;
783 s_aSavedMessages
.Add(s_currentMsg
);
789 // have we just regained the GUI lock? if so, post all of the saved
792 // FIXME of course, it's not _exactly_ the same as processing the
793 // messages normally - expect some things to break...
798 size_t count
= s_aSavedMessages
.Count();
799 for ( size_t n
= 0; n
< count
; n
++ )
801 MSG
& msg
= s_aSavedMessages
[n
];
803 if ( !ProcessMessage((WXMSG
*)&msg
) )
805 ::TranslateMessage(&msg
);
806 ::DispatchMessage(&msg
);
810 s_aSavedMessages
.Empty();
813 #endif // wxUSE_THREADS
815 // Process the message
816 if ( !ProcessMessage((WXMSG
*)&s_currentMsg
) )
818 ::TranslateMessage(&s_currentMsg
);
819 wxApp::sm_lastMessageTime
= s_currentMsg
.time
; /* MATTHEW: timeStamp impl. */
820 ::DispatchMessage(&s_currentMsg
);
828 * Keep trying to process messages until WM_QUIT
831 * If there are messages to be processed, they will all be
832 * processed and OnIdle will not be called.
833 * When there are no more messages, OnIdle is called.
834 * If OnIdle requests more time,
835 * it will be repeatedly called so long as there are no pending messages.
836 * A 'feature' of this is that once OnIdle has decided that no more processing
837 * is required, then it won't get processing time until further messages
838 * are processed (it'll sit in DoMessage).
841 int wxApp::MainLoop()
845 while ( m_keepGoing
)
848 wxMutexGuiLeaveOrEnter();
849 #endif // wxUSE_THREADS
851 while ( !::PeekMessage(&s_currentMsg
, 0, 0, 0, PM_NOREMOVE
) &&
859 return s_currentMsg
.wParam
;
862 // Returns TRUE if more time is needed.
863 bool wxApp::ProcessIdle()
866 event
.SetEventObject(this);
869 return event
.MoreRequested();
872 void wxApp::ExitMainLoop()
877 bool wxApp::Pending()
879 return (::PeekMessage(&s_currentMsg
, 0, 0, 0, PM_NOREMOVE
) != 0) ;
882 void wxApp::Dispatch()
888 * Give all windows a chance to preprocess
889 * the message. Some may have accelerator tables, or have
892 bool wxApp::ProcessMessage(WXMSG
*Msg
)
894 MSG
*msg
= (MSG
*)Msg
;
898 // Try translations first; find the youngest window with
899 // a translation table.
900 for (hWnd
= msg
->hwnd
; hWnd
!= NULL
; hWnd
= ::GetParent(hWnd
))
902 wxWindow
*wnd
= wxFindWinFromHandle((WXHWND
) hWnd
);
905 if (wnd
->MSWTranslateMessage(Msg
))
910 // Anyone for a non-translation message? Try youngest descendants first.
911 for (hWnd
= msg
->hwnd
; hWnd
!= NULL
; hWnd
= ::GetParent(hWnd
))
913 wxWindow
*wnd
= wxFindWinFromHandle((WXHWND
) hWnd
);
916 if (wnd
->MSWProcessMessage(Msg
))
923 void wxApp::OnIdle(wxIdleEvent
& event
)
925 static bool s_inOnIdle
= FALSE
;
927 // Avoid recursion (via ProcessEvent default case)
933 // 'Garbage' collection of windows deleted with Close().
934 DeletePendingObjects();
936 // flush the logged messages if any
937 wxLog
*pLog
= wxLog::GetActiveTarget();
938 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
941 // Send OnIdle events to all windows
942 if ( SendIdleEvents() )
944 // SendIdleEvents() returns TRUE if at least one window requested more
946 event
.RequestMore(TRUE
);
952 // Send idle event to all top-level windows
953 bool wxApp::SendIdleEvents()
955 bool needMore
= FALSE
;
956 wxNode
* node
= wxTopLevelWindows
.First();
959 wxWindow
* win
= (wxWindow
*) node
->Data();
960 if (SendIdleEvents(win
))
969 // Send idle event to window and all subwindows
970 bool wxApp::SendIdleEvents(wxWindow
* win
)
972 bool needMore
= FALSE
;
975 event
.SetEventObject(win
);
976 win
->GetEventHandler()->ProcessEvent(event
);
978 if (event
.MoreRequested())
981 wxNode
* node
= win
->GetChildren().First();
984 wxWindow
* win
= (wxWindow
*) node
->Data();
985 if (SendIdleEvents(win
))
993 void wxApp::DeletePendingObjects()
995 wxNode
*node
= wxPendingDelete
.First();
998 wxObject
*obj
= (wxObject
*)node
->Data();
1002 if (wxPendingDelete
.Member(obj
))
1005 // Deleting one object may have deleted other pending
1006 // objects, so start from beginning of list again.
1007 node
= wxPendingDelete
.First();
1011 void wxApp::OnEndSession(wxCloseEvent
& event
)
1014 GetTopWindow()->Close(TRUE
);
1017 // Default behaviour: close the application with prompts. The
1018 // user can veto the close, and therefore the end session.
1019 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
1023 if (!GetTopWindow()->Close(!event
.CanVeto()))
1028 wxLog
* wxApp::CreateLogTarget()
1030 return new wxLogGui
;
1033 wxWindow
* wxApp::GetTopWindow() const
1037 else if (wxTopLevelWindows
.Number() > 0)
1038 return (wxWindow
*) wxTopLevelWindows
.First()->Data();
1043 int wxApp::GetComCtl32Version() const
1045 // have we loaded COMCTL32 yet?
1046 HMODULE theModule
= ::GetModuleHandle("COMCTL32");
1049 // if so, then we can check for the version
1052 // InitCommonControlsEx is unique to 4.7 and later
1053 FARPROC theProc
= ::GetProcAddress(theModule
, "InitCommonControlsEx");
1056 { // not found, must be 4.00
1061 // The following symbol are unique to 4.71
1063 // FlatSB_EnableScrollBar FlatSB_GetScrollInfo FlatSB_GetScrollPos
1064 // FlatSB_GetScrollProp FlatSB_GetScrollRange FlatSB_SetScrollInfo
1065 // FlatSB_SetScrollPos FlatSB_SetScrollProp FlatSB_SetScrollRange
1066 // FlatSB_ShowScrollBar
1067 // _DrawIndirectImageList _DuplicateImageList
1069 // UninitializeFlatSB
1070 // we could check for any of these - I chose DllInstall
1071 FARPROC theProc
= ::GetProcAddress(theModule
, "DllInstall");
1074 // not found, must be 4.70
1078 { // found, must be 4.71
1089 FatalAppExit(0, "Fatal error: exiting");
1092 // Yield to incoming messages
1096 // We want to go back to the main message loop
1097 // if we see a WM_QUIT. (?)
1098 while (PeekMessage(&msg
, NULL
, 0, 0, PM_NOREMOVE
) && msg
.message
!= WM_QUIT
)
1100 if ( !wxTheApp
->DoMessage() )
1107 HINSTANCE
wxGetInstance()
1112 // For some reason, with MSVC++ 1.5, WinMain isn't linked in properly
1113 // if in a separate file. So include it here to ensure it's linked.
1114 #if (defined(_MSC_VER) && !defined(__WIN32__)) || defined(__GNUWIN32__)