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"
37 #include "wx/dynarray.h"
40 #include "wx/msw/private.h"
42 #include "wx/module.h"
45 #include "wx/thread.h"
47 // define the array of MSG strutures
48 WX_DECLARE_OBJARRAY(MSG
, wxMsgArray
);
50 #include "wx/arrimpl.cpp"
52 WX_DEFINE_OBJARRAY(wxMsgArray
);
53 #endif // wxUSE_THREADS
55 #if wxUSE_WX_RESOURCES
56 #include "wx/resource.h"
59 // To UG: there's no point in putting this #if here
60 // if you don't do the same for the Ole calls further down.
61 // Also, OLE is used not just for drag and drop (it's used by automatn.cpp).
62 // #if wxUSE_DRAG_AND_DROP
63 #if !defined(__GNUWIN32__) && !defined(__SC__) && !defined(__SALFORDC__)
72 #if (defined(__WIN95__) && !defined(__GNUWIN32__)) || defined(__TWIN32__)
76 // use debug CRT functions for memory leak detections in VC++ if we're not
77 // using wxWindows own methods
78 #if defined(__WXDEBUG__) && defined(_MSC_VER) && !wxUSE_GLOBAL_MEMORY_OPERATORS && !defined(__NO_VC_CRTDBG__)
79 #define wxUSE_VC_CRTDBG
81 #undef wxUSE_VC_CRTDBG
84 #ifdef wxUSE_VC_CRTDBG
85 // VC++ uses this macro as debug/release mode indicator
90 /* Need to undef new if including crtdbg.h */
97 #if defined(__WXDEBUG__) && wxUSE_GLOBAL_MEMORY_OPERATORS && wxUSE_DEBUG_NEW_ALWAYS
98 #define new new(__FILE__,__LINE__)
100 #endif // wxUSE_VC_CRTDBG
102 extern char *wxBuffer
;
103 extern char *wxOsVersion
;
104 extern wxList
*wxWinHandleList
;
105 extern wxList WXDLLEXPORT wxPendingDelete
;
106 extern void wxSetKeyboardHook(bool doIt
);
107 extern wxCursor
*g_globalCursor
;
109 HINSTANCE wxhInstance
= 0;
110 static MSG s_currentMsg
;
111 wxApp
*wxTheApp
= NULL
;
113 // @@ why not const? and not static?
114 char wxFrameClassName
[] = "wxFrameClass";
115 char wxMDIFrameClassName
[] = "wxMDIFrameClass";
116 char wxMDIChildFrameClassName
[] = "wxMDIChildFrameClass";
117 char wxPanelClassName
[] = "wxPanelClass";
118 char wxCanvasClassName
[] = "wxCanvasClass";
120 HICON wxSTD_FRAME_ICON
= (HICON
) NULL
;
121 HICON wxSTD_MDICHILDFRAME_ICON
= (HICON
) NULL
;
122 HICON wxSTD_MDIPARENTFRAME_ICON
= (HICON
) NULL
;
124 HICON wxDEFAULT_FRAME_ICON
= (HICON
) NULL
;
125 HICON wxDEFAULT_MDICHILDFRAME_ICON
= (HICON
) NULL
;
126 HICON wxDEFAULT_MDIPARENTFRAME_ICON
= (HICON
) NULL
;
128 HBRUSH wxDisableButtonBrush
= (HBRUSH
) 0;
130 LRESULT APIENTRY
wxWndProc(HWND
, UINT
, WPARAM
, LPARAM
);
132 #if !USE_SHARED_LIBRARY
133 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
135 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
136 EVT_IDLE(wxApp::OnIdle
)
137 EVT_END_SESSION(wxApp::OnEndSession
)
138 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession
)
142 long wxApp::sm_lastMessageTime
= 0;
144 #if defined(__WIN95__) && !defined(__TWIN32__)
145 #define wxUSE_RICHEDIT 1
147 #define wxUSE_RICHEDIT 0
151 static HINSTANCE gs_hRichEdit
= (HINSTANCE
) NULL
;
156 bool wxApp::Initialize()
158 // Some people may wish to use this, but
159 // probably it shouldn't be here by default.
161 // wxRedirectIOToConsole();
164 wxBuffer
= new char[1500];
166 #ifdef wxUSE_VC_CRTDBG
167 // do check for memory leaks on program exit
168 // (another useful flag is _CRTDBG_DELAY_FREE_MEM_DF which doesn't free
169 // deallocated memory which may be used to simulate low-memory condition)
170 _CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG
) | _CRTDBG_LEAK_CHECK_DF
);
171 #endif // debug build under MS VC++
173 wxClassInfo::InitializeClasses();
176 wxGetResource("wxWindows", "OsVersion", &wxOsVersion
);
179 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
180 wxTheColourDatabase
->Initialize();
182 wxInitializeStockLists();
183 wxInitializeStockObjects();
185 #if wxUSE_WX_RESOURCES
186 wxInitializeResourceSystem();
189 wxBitmap::InitStandardHandlers();
191 #if defined(__WIN95__)
192 InitCommonControls();
195 gs_hRichEdit
= LoadLibrary("RICHED32.DLL");
197 if (gs_hRichEdit
== (HINSTANCE
) NULL
)
199 wxMessageBox("Could not initialise Rich Edit DLL");
207 // for OLE, enlarge message queue to be as large as possible
208 while (!SetMessageQueue(iMsg
) && (iMsg
-= 8));
212 dwOleVer = CoBuildVersion();
214 // check the OLE library version
215 if (rmm != HIWORD(dwOleVer))
217 wxMessageBox("Incorrect version of OLE libraries.");
222 #if !defined(__GNUWIN32__) && !defined(__SC__) && !defined(__SALFORDC__)
223 // we need to initialize OLE library
224 if ( FAILED(::OleInitialize(NULL
)) )
225 wxFatalError(_("Cannot initialize OLE"));
229 if (!Ctl3dRegister(wxhInstance
))
230 wxFatalError("Cannot register CTL3D");
232 Ctl3dAutoSubclass(wxhInstance
);
235 g_globalCursor
= new wxCursor
;
237 wxSTD_FRAME_ICON
= LoadIcon(wxhInstance
, "wxSTD_FRAME");
238 wxSTD_MDIPARENTFRAME_ICON
= LoadIcon(wxhInstance
, "wxSTD_MDIPARENTFRAME");
239 wxSTD_MDICHILDFRAME_ICON
= LoadIcon(wxhInstance
, "wxSTD_MDICHILDFRAME");
241 wxDEFAULT_FRAME_ICON
= LoadIcon(wxhInstance
, "wxDEFAULT_FRAME");
242 wxDEFAULT_MDIPARENTFRAME_ICON
= LoadIcon(wxhInstance
, "wxDEFAULT_MDIPARENTFRAME");
243 wxDEFAULT_MDICHILDFRAME_ICON
= LoadIcon(wxhInstance
, "wxDEFAULT_MDICHILDFRAME");
245 RegisterWindowClasses();
247 // Create the brush for disabling bitmap buttons
250 lb
.lbStyle
= BS_PATTERN
;
251 lb
.lbHatch
= (int)LoadBitmap( wxhInstance
, "wxDISABLE_BUTTON_BITMAP" ) ;
252 wxDisableButtonBrush
= ::CreateBrushIndirect( & lb
) ;
253 ::DeleteObject( (HGDIOBJ
)lb
.lbHatch
) ;
259 wxWinHandleList
= new wxList(wxKEY_INTEGER
);
261 // This is to foil optimizations in Visual C++ that
262 // throw out dummy.obj.
263 // PLEASE DO NOT ALTER THIS.
264 #if defined(_MSC_VER) && !defined(WXMAKINGDLL)
265 extern char wxDummyChar
;
266 if (wxDummyChar
) wxDummyChar
++;
269 wxSetKeyboardHook(TRUE
);
271 wxModule::RegisterModules();
272 if (!wxModule::InitializeModules())
277 //// RegisterWindowClasses
279 bool wxApp::RegisterWindowClasses()
281 ///////////////////////////////////////////////////////////////////////
282 // Register the frame window class.
283 WNDCLASS wndclass
; // Structure used to register Windows class.
285 wndclass
.style
= CS_HREDRAW
| CS_VREDRAW
;
286 wndclass
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
287 wndclass
.cbClsExtra
= 0;
288 wndclass
.cbWndExtra
= sizeof( DWORD
); // was 4
289 wndclass
.hInstance
= wxhInstance
;
290 wndclass
.hIcon
= (HICON
) NULL
; // wxSTD_FRAME_ICON;
291 wndclass
.hCursor
= LoadCursor( (HINSTANCE
) NULL
, IDC_ARROW
);
292 wndclass
.hbrBackground
= (HBRUSH
)(COLOR_APPWORKSPACE
+1) ;
293 // wndclass.hbrBackground = GetStockObject( WHITE_BRUSH );
294 wndclass
.lpszMenuName
= NULL
;
295 #ifdef _MULTIPLE_INSTANCES
296 sprintf( wxFrameClassName
,"wxFrameClass%d", wxhInstance
);
298 wndclass
.lpszClassName
= wxFrameClassName
;
300 if (!RegisterClass( &wndclass
))
302 // wxFatalError("Can't register Frame Window class");
305 ///////////////////////////////////////////////////////////////////////
306 // Register the MDI frame window class.
307 WNDCLASS wndclass1
; // Structure used to register Windows class.
309 wndclass1
.style
= CS_HREDRAW
| CS_VREDRAW
;
310 wndclass1
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
311 wndclass1
.cbClsExtra
= 0;
312 wndclass1
.cbWndExtra
= sizeof( DWORD
); // was 4
313 wndclass1
.hInstance
= wxhInstance
;
314 wndclass1
.hIcon
= (HICON
) NULL
; // wxSTD_MDIPARENTFRAME_ICON;
315 wndclass1
.hCursor
= LoadCursor( (HINSTANCE
) NULL
, IDC_ARROW
);
316 // wndclass1.hbrBackground = (HBRUSH)(COLOR_APPWORKSPACE+1) ;
317 wndclass1
.hbrBackground
= (HBRUSH
) NULL
;
318 wndclass1
.lpszMenuName
= NULL
;
320 wndclass1
.lpszClassName
= wxMDIFrameClassName
;
321 if (!RegisterClass( &wndclass1
))
323 // wxFatalError("Can't register MDI Frame window class");
327 ///////////////////////////////////////////////////////////////////////
328 // Register the MDI child frame window class.
329 WNDCLASS wndclass4
; // Structure used to register Windows class.
331 wndclass4
.style
= CS_HREDRAW
| CS_VREDRAW
;
332 wndclass4
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
333 wndclass4
.cbClsExtra
= 0;
334 wndclass4
.cbWndExtra
= sizeof( DWORD
); // was 4
335 wndclass4
.hInstance
= wxhInstance
;
336 wndclass4
.hIcon
= (HICON
) NULL
; // wxSTD_MDICHILDFRAME_ICON;
337 wndclass4
.hCursor
= LoadCursor( (HINSTANCE
) NULL
, IDC_ARROW
);
338 // TODO: perhaps this should be NULL so that Windows doesn't
339 // paint the background itself (would OnEraseBackground duplicate
341 wndclass4
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+1) ;
342 // wndclass4.hbrBackground = NULL;
343 wndclass4
.lpszMenuName
= NULL
;
344 wndclass4
.lpszClassName
= wxMDIChildFrameClassName
;
346 if (!RegisterClass( &wndclass4
))
348 // wxFatalError("Can't register MDI child frame window class");
352 ///////////////////////////////////////////////////////////////////////
353 // Register the panel window class.
354 WNDCLASS wndclass2
; // Structure used to register Windows class.
355 memset(&wndclass2
, 0, sizeof(WNDCLASS
)); // start with NULL defaults
356 // Use CS_OWNDC to avoid messing about restoring the context
357 // for every graphic operation.
358 wndclass2
.style
= CS_HREDRAW
| CS_VREDRAW
;
359 wndclass2
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
360 wndclass2
.cbClsExtra
= 0;
361 wndclass2
.cbWndExtra
= sizeof( DWORD
); // was 4
362 wndclass2
.hInstance
= wxhInstance
;
363 wndclass2
.hIcon
= (HICON
) NULL
;
364 wndclass2
.hCursor
= (HCURSOR
) NULL
;
365 // wndclass2.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1) ;
366 wndclass2
.hbrBackground
= (HBRUSH
) GetStockObject( LTGRAY_BRUSH
);
367 wndclass2
.lpszMenuName
= NULL
;
368 wndclass2
.lpszClassName
= wxPanelClassName
;
369 if (!RegisterClass( &wndclass2
))
371 // wxFatalError("Can't register Panel Window class");
375 ///////////////////////////////////////////////////////////////////////
376 // Register the canvas and textsubwindow class name
377 WNDCLASS wndclass3
; // Structure used to register Windows class.
378 memset(&wndclass3
, 0, sizeof(WNDCLASS
)); // start with NULL defaults
379 // Use CS_OWNDC to avoid messing about restoring the context
380 // for every graphic operation.
381 // wndclass3.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS ;
383 wndclass3
.style
= CS_HREDRAW
| CS_VREDRAW
| CS_DBLCLKS
;
384 wndclass3
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
385 wndclass3
.cbClsExtra
= 0;
386 wndclass3
.cbWndExtra
= sizeof( DWORD
); // was 4
387 wndclass3
.hInstance
= wxhInstance
;
388 wndclass3
.hIcon
= (HICON
) NULL
;
389 wndclass3
.hCursor
= (HCURSOR
) NULL
;
390 // wndclass3.hbrBackground = (HBRUSH)(COLOR_WINDOW+1) ;
391 wndclass3
.hbrBackground
= (HBRUSH
) NULL
;
392 wndclass3
.lpszMenuName
= NULL
;
393 wndclass3
.lpszClassName
= wxCanvasClassName
;
394 if (!RegisterClass( &wndclass3
))
396 // wxFatalError("Can't register Canvas class");
403 //// Convert Windows to argc, argv style
405 void wxApp::ConvertToStandardCommandArgs(char* lpCmdLine
)
409 wxString
cmdLine(lpCmdLine
);
412 // Get application name
413 char name
[260]; // 260 is MAX_PATH value from windef.h
414 ::GetModuleFileName(wxhInstance
, name
, WXSIZEOF(name
));
416 // GNUWIN32 already fills in the first arg with the application name.
417 #if !defined(__GNUWIN32__)
422 strcpy(name
, wxFileNameFromPath(name
));
423 wxStripExtension(name
);
424 wxTheApp
->SetAppName(name
);
427 // Treat strings enclosed in double-quotes as single arguments
429 int len
= cmdLine
.Length();
433 while ((i
< len
) && isspace(cmdLine
.GetChar(i
)))
438 if (cmdLine
.GetChar(i
) == '"') // We found the start of a string
442 while ((i
< len
) && (cmdLine
.GetChar(i
) != '"'))
445 wxString
arg(cmdLine
.Mid(first
, (i
- first
)));
451 i
++; // Skip past 2nd quote
453 else // Unquoted argument
456 while ((i
< len
) && !isspace(cmdLine
.GetChar(i
)))
459 wxString
arg(cmdLine
.Mid(first
, (i
- first
)));
467 wxTheApp
->argv
= new char*[count
+ 1];
468 for (i
= 0; i
< count
; i
++)
470 wxString
arg(args
[i
]);
471 wxTheApp
->argv
[i
] = copystring((const char*)arg
);
473 wxTheApp
->argv
[count
] = NULL
; // argv[] is a NULL-terminated list
474 wxTheApp
->argc
= count
;
477 //// Cleans up any wxWindows internal structures left lying around
479 void wxApp::CleanUp()
482 wxModule::CleanUpModules();
484 #if wxUSE_WX_RESOURCES
485 wxCleanUpResourceSystem();
487 // wxDefaultResourceTable->ClearTable();
490 // Indicate that the cursor can be freed,
491 // so that cursor won't be deleted by deleting
492 // the bitmap list before g_globalCursor goes out
493 // of scope (double deletion of the cursor).
494 wxSetCursor(wxNullCursor
);
495 delete g_globalCursor
;
497 wxDeleteStockObjects() ;
499 // Destroy all GDI lists, etc.
500 wxDeleteStockLists();
502 delete wxTheColourDatabase
;
503 wxTheColourDatabase
= NULL
;
505 wxBitmap::CleanUpHandlers();
510 //// WINDOWS-SPECIFIC CLEANUP
512 wxSetKeyboardHook(FALSE
);
517 if (gs_hRichEdit
!= (HINSTANCE
) NULL
)
518 FreeLibrary(gs_hRichEdit
);
527 if (wxSTD_FRAME_ICON
)
528 DestroyIcon(wxSTD_FRAME_ICON
);
529 if (wxSTD_MDICHILDFRAME_ICON
)
530 DestroyIcon(wxSTD_MDICHILDFRAME_ICON
);
531 if (wxSTD_MDIPARENTFRAME_ICON
)
532 DestroyIcon(wxSTD_MDIPARENTFRAME_ICON
);
534 if (wxDEFAULT_FRAME_ICON
)
535 DestroyIcon(wxDEFAULT_FRAME_ICON
);
536 if (wxDEFAULT_MDICHILDFRAME_ICON
)
537 DestroyIcon(wxDEFAULT_MDICHILDFRAME_ICON
);
538 if (wxDEFAULT_MDIPARENTFRAME_ICON
)
539 DestroyIcon(wxDEFAULT_MDIPARENTFRAME_ICON
);
541 if ( wxDisableButtonBrush
)
542 ::DeleteObject( wxDisableButtonBrush
) ;
544 #if !defined(__GNUWIN32__) && !defined(__SC__) && !defined(__SALFORDC__)
549 Ctl3dUnregister(wxhInstance
);
553 delete wxWinHandleList
;
555 wxClassInfo::CleanUpClasses();
560 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
561 // At this point we want to check if there are any memory
562 // blocks that aren't part of the wxDebugContext itself,
563 // as a special case. Then when dumping we need to ignore
564 // wxDebugContext, too.
565 if (wxDebugContext::CountObjectsLeft() > 0)
567 wxLogDebug("There were memory leaks.");
568 wxDebugContext::Dump();
569 wxDebugContext::PrintStatistics();
571 // wxDebugContext::SetStream(NULL, NULL);
574 // do it as the very last thing because everything else can log messages
575 wxLog::DontCreateOnDemand();
576 delete wxLog::SetActiveTarget(NULL
);
579 #if !defined(_WINDLL) || (defined(_WINDLL) && defined(WXMAKINGDLL))
581 // temporarily disable this warning
583 #pragma warning(disable: 4715) // not all control paths return a value
586 //// Main wxWindows entry point
587 int wxEntry(WXHINSTANCE hInstance
,
588 WXHINSTANCE
WXUNUSED(hPrevInstance
),
593 // take everything into a try-except block in release build
594 // FIXME other compilers must support Win32 SEH (structured exception
595 // handling) too, just find the appropriate keyword in their docs!
596 // Please note that it's _not_ the same as C++ exceptions!
597 #if !defined(__WXDEBUG__) && defined(_MSC_VER)
598 #define CATCH_PROGRAM_EXCEPTIONS
602 #undef CATCH_PROGRAM_EXCEPTIONS
605 wxhInstance
= (HINSTANCE
) hInstance
;
607 if (!wxApp::Initialize())
610 // create the application object or ensure that one already exists
613 // The app may have declared a global application object, but we recommend
614 // the IMPLEMENT_APP macro is used instead, which sets an initializer
615 // function for delayed, dynamic app object construction.
616 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
617 "No initializer - use IMPLEMENT_APP macro." );
619 wxTheApp
= (*wxApp::GetInitializerFunction()) ();
622 wxCHECK_MSG( wxTheApp
, 0, "You have to define an instance of wxApp!" );
624 // save the WinMain() parameters
625 wxTheApp
->ConvertToStandardCommandArgs(lpCmdLine
);
626 wxTheApp
->m_nCmdShow
= nCmdShow
;
628 // GUI-specific initialisation. In fact on Windows we don't have any,
629 // but this call is provided for compatibility across platforms.
630 wxTheApp
->OnInitGui() ;
634 if ( wxTheApp
->OnInit() )
638 retValue
= wxTheApp
->OnRun();
641 //else: app initialization failed, so we skipped OnRun()
643 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
646 // Forcibly delete the window.
647 if ( topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
648 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
650 topWindow
->Close(TRUE
);
651 wxTheApp
->DeletePendingObjects();
656 wxTheApp
->SetTopWindow(NULL
);
662 // flush the logged messages if any
663 wxLog
*pLog
= wxLog::GetActiveTarget();
664 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
672 #ifdef CATCH_PROGRAM_EXCEPTIONS
674 __except ( EXCEPTION_EXECUTE_HANDLER
) {
677 wxTheApp->OnFatalException();
680 ::ExitProcess(3); // the same exit code as abort()
684 #endif // CATCH_PROGRAM_EXCEPTIONS
687 // restore warning state
689 #pragma warning(default: 4715) // not all control paths return a value
694 //// Entry point for DLLs
696 int wxEntry(WXHINSTANCE hInstance
)
698 wxhInstance
= (HINSTANCE
) hInstance
;
701 // The app may have declared a global application object, but we recommend
702 // the IMPLEMENT_APP macro is used instead, which sets an initializer function
703 // for delayed, dynamic app object construction.
707 if (!wxApp::GetInitializerFunction())
709 MessageBox(NULL
, "No initializer - use IMPLEMENT_APP macro.", "wxWindows Error", MB_APPLMODAL
| MB_ICONSTOP
| MB_OK
);
713 wxTheApp
= (* wxApp::GetInitializerFunction()) ();
717 MessageBox(NULL
, "You have to define an instance of wxApp!", "wxWindows Error", MB_APPLMODAL
| MB_ICONSTOP
| MB_OK
);
722 wxTheApp
->argv
= NULL
;
724 wxTheApp
->OnInitGui();
728 if (wxTheApp
->GetTopWindow() && wxTheApp
->GetTopWindow()->GetHWND()) {
729 wxTheApp
->GetTopWindow()->Show(TRUE
);
736 //// Static member initialization
738 wxAppInitializerFunction
wxApp::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
745 m_wantDebugOutput
= TRUE
;
750 m_printMode
= wxPRINT_WINDOWS
;
752 m_printMode
= wxPRINT_POSTSCRIPT
;
754 m_exitOnFrameDelete
= TRUE
;
760 // Delete command-line args
762 for (i
= 0; i
< argc
; i
++)
769 bool wxApp::Initialized()
777 #ifdef _WINDLL // Assume initialized if DLL (no way of telling)
783 * Get and process a message, returning FALSE if WM_QUIT
784 * received (and also set the flag telling the app to exit the main loop)
787 bool wxApp::DoMessage()
789 BOOL rc
= ::GetMessage(&s_currentMsg
, (HWND
) NULL
, 0, 0);
799 // should never happen, but let's test for it nevertheless
800 wxLogLastError("GetMessage");
805 wxASSERT_MSG( wxThread::IsMain(),
806 "only the main thread can process Windows messages" );
808 static bool s_hadGuiLock
= TRUE
;
809 static wxMsgArray s_aSavedMessages
;
811 // if a secondary thread owns is doing GUI calls, save all messages for
812 // later processing - we can't process them right now because it will
813 // lead to recursive library calls (and we're not reentrant)
814 if ( !wxGuiOwnedByMainThread() )
816 s_hadGuiLock
= FALSE
;
818 // leave out WM_COMMAND messages: too dangerous, sometimes
819 // the message will be processed twice
820 if ( !wxIsWaitingForThread() ||
821 s_currentMsg
.message
!= WM_COMMAND
)
823 s_aSavedMessages
.Add(s_currentMsg
);
830 // have we just regained the GUI lock? if so, post all of the saved
833 // FIXME of course, it's not _exactly_ the same as processing the
834 // messages normally - expect some things to break...
839 size_t count
= s_aSavedMessages
.Count();
840 for ( size_t n
= 0; n
< count
; n
++ )
842 MSG
& msg
= s_aSavedMessages
[n
];
844 if ( !ProcessMessage((WXMSG
*)&msg
) )
846 ::TranslateMessage(&msg
);
847 ::DispatchMessage(&msg
);
851 s_aSavedMessages
.Empty();
854 #endif // wxUSE_THREADS
856 // Process the message
857 if ( !ProcessMessage((WXMSG
*)&s_currentMsg
) )
859 ::TranslateMessage(&s_currentMsg
);
860 wxApp::sm_lastMessageTime
= s_currentMsg
.time
; /* MATTHEW: timeStamp impl. */
861 ::DispatchMessage(&s_currentMsg
);
869 * Keep trying to process messages until WM_QUIT
872 * If there are messages to be processed, they will all be
873 * processed and OnIdle will not be called.
874 * When there are no more messages, OnIdle is called.
875 * If OnIdle requests more time,
876 * it will be repeatedly called so long as there are no pending messages.
877 * A 'feature' of this is that once OnIdle has decided that no more processing
878 * is required, then it won't get processing time until further messages
879 * are processed (it'll sit in DoMessage).
882 int wxApp::MainLoop()
886 while ( m_keepGoing
)
889 wxMutexGuiLeaveOrEnter();
890 #endif // wxUSE_THREADS
892 while ( !::PeekMessage(&s_currentMsg
, 0, 0, 0, PM_NOREMOVE
) &&
900 return s_currentMsg
.wParam
;
903 // Returns TRUE if more time is needed.
904 bool wxApp::ProcessIdle()
907 event
.SetEventObject(this);
910 return event
.MoreRequested();
913 void wxApp::ExitMainLoop()
918 bool wxApp::Pending()
920 return (::PeekMessage(&s_currentMsg
, 0, 0, 0, PM_NOREMOVE
) != 0) ;
923 void wxApp::Dispatch()
929 * Give all windows a chance to preprocess
930 * the message. Some may have accelerator tables, or have
933 bool wxApp::ProcessMessage(WXMSG
*Msg
)
935 MSG
*msg
= (MSG
*)Msg
;
939 // Try translations first; find the youngest window with
940 // a translation table.
941 for (hWnd
= msg
->hwnd
; hWnd
!= (HWND
) NULL
; hWnd
= ::GetParent(hWnd
))
943 wxWindow
*wnd
= wxFindWinFromHandle((WXHWND
) hWnd
);
946 if (wnd
->MSWTranslateMessage(Msg
))
951 // Anyone for a non-translation message? Try youngest descendants first.
952 for (hWnd
= msg
->hwnd
; hWnd
!= (HWND
) NULL
; hWnd
= ::GetParent(hWnd
))
954 wxWindow
*wnd
= wxFindWinFromHandle((WXHWND
) hWnd
);
957 if (wnd
->MSWProcessMessage(Msg
))
964 void wxApp::OnIdle(wxIdleEvent
& event
)
966 static bool s_inOnIdle
= FALSE
;
968 // Avoid recursion (via ProcessEvent default case)
974 // 'Garbage' collection of windows deleted with Close().
975 DeletePendingObjects();
977 // flush the logged messages if any
978 wxLog
*pLog
= wxLog::GetActiveTarget();
979 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
982 // Send OnIdle events to all windows
983 if ( SendIdleEvents() )
985 // SendIdleEvents() returns TRUE if at least one window requested more
987 event
.RequestMore(TRUE
);
993 // Send idle event to all top-level windows
994 bool wxApp::SendIdleEvents()
996 bool needMore
= FALSE
;
997 wxNode
* node
= wxTopLevelWindows
.First();
1000 wxWindow
* win
= (wxWindow
*) node
->Data();
1001 if (SendIdleEvents(win
))
1004 node
= node
->Next();
1010 // Send idle event to window and all subwindows
1011 bool wxApp::SendIdleEvents(wxWindow
* win
)
1013 bool needMore
= FALSE
;
1016 event
.SetEventObject(win
);
1017 win
->GetEventHandler()->ProcessEvent(event
);
1019 if (event
.MoreRequested())
1022 wxNode
* node
= win
->GetChildren().First();
1025 wxWindow
* win
= (wxWindow
*) node
->Data();
1026 if (SendIdleEvents(win
))
1029 node
= node
->Next();
1034 void wxApp::DeletePendingObjects()
1036 wxNode
*node
= wxPendingDelete
.First();
1039 wxObject
*obj
= (wxObject
*)node
->Data();
1043 if (wxPendingDelete
.Member(obj
))
1046 // Deleting one object may have deleted other pending
1047 // objects, so start from beginning of list again.
1048 node
= wxPendingDelete
.First();
1052 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
1055 GetTopWindow()->Close(TRUE
);
1058 // Default behaviour: close the application with prompts. The
1059 // user can veto the close, and therefore the end session.
1060 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
1064 if (!GetTopWindow()->Close(!event
.CanVeto()))
1069 wxLog
* wxApp::CreateLogTarget()
1071 return new wxLogGui
;
1074 wxWindow
* wxApp::GetTopWindow() const
1078 else if (wxTopLevelWindows
.Number() > 0)
1079 return (wxWindow
*) wxTopLevelWindows
.First()->Data();
1084 int wxApp::GetComCtl32Version() const
1086 // have we loaded COMCTL32 yet?
1087 HMODULE theModule
= ::GetModuleHandle("COMCTL32");
1090 // if so, then we can check for the version
1093 // InitCommonControlsEx is unique to 4.7 and later
1094 FARPROC theProc
= ::GetProcAddress(theModule
, "InitCommonControlsEx");
1097 { // not found, must be 4.00
1102 // The following symbol are unique to 4.71
1104 // FlatSB_EnableScrollBar FlatSB_GetScrollInfo FlatSB_GetScrollPos
1105 // FlatSB_GetScrollProp FlatSB_GetScrollRange FlatSB_SetScrollInfo
1106 // FlatSB_SetScrollPos FlatSB_SetScrollProp FlatSB_SetScrollRange
1107 // FlatSB_ShowScrollBar
1108 // _DrawIndirectImageList _DuplicateImageList
1110 // UninitializeFlatSB
1111 // we could check for any of these - I chose DllInstall
1112 FARPROC theProc
= ::GetProcAddress(theModule
, "DllInstall");
1115 // not found, must be 4.70
1119 { // found, must be 4.71
1130 FatalAppExit(0, "Fatal error: exiting");
1133 // Yield to incoming messages
1137 // We want to go back to the main message loop
1138 // if we see a WM_QUIT. (?)
1139 while (PeekMessage(&msg
, NULL
, 0, 0, PM_NOREMOVE
) && msg
.message
!= WM_QUIT
)
1141 if ( !wxTheApp
->DoMessage() )
1148 HINSTANCE
wxGetInstance()
1153 // For some reason, with MSVC++ 1.5, WinMain isn't linked in properly
1154 // if in a separate file. So include it here to ensure it's linked.
1155 #if (defined(_MSC_VER) && !defined(__WIN32__)) || (defined(__GNUWIN32__) && !defined(__TWIN32__))