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 #if 0 && defined(_MSC_VER) && !defined(WXMAKINGDLL)
264 extern char wxDummyChar
;
265 if (wxDummyChar
) wxDummyChar
++;
268 wxSetKeyboardHook(TRUE
);
270 wxModule::RegisterModules();
271 if (!wxModule::InitializeModules())
276 //// RegisterWindowClasses
278 bool wxApp::RegisterWindowClasses()
280 ///////////////////////////////////////////////////////////////////////
281 // Register the frame window class.
282 WNDCLASS wndclass
; // Structure used to register Windows class.
284 wndclass
.style
= CS_HREDRAW
| CS_VREDRAW
;
285 wndclass
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
286 wndclass
.cbClsExtra
= 0;
287 wndclass
.cbWndExtra
= sizeof( DWORD
); // was 4
288 wndclass
.hInstance
= wxhInstance
;
289 wndclass
.hIcon
= (HICON
) NULL
; // wxSTD_FRAME_ICON;
290 wndclass
.hCursor
= LoadCursor( (HINSTANCE
) NULL
, IDC_ARROW
);
291 wndclass
.hbrBackground
= (HBRUSH
)(COLOR_APPWORKSPACE
+1) ;
292 // wndclass.hbrBackground = GetStockObject( WHITE_BRUSH );
293 wndclass
.lpszMenuName
= NULL
;
294 #ifdef _MULTIPLE_INSTANCES
295 sprintf( wxFrameClassName
,"wxFrameClass%d", wxhInstance
);
297 wndclass
.lpszClassName
= wxFrameClassName
;
299 if (!RegisterClass( &wndclass
))
301 // wxFatalError("Can't register Frame Window class");
304 ///////////////////////////////////////////////////////////////////////
305 // Register the MDI frame window class.
306 WNDCLASS wndclass1
; // Structure used to register Windows class.
308 wndclass1
.style
= CS_HREDRAW
| CS_VREDRAW
;
309 wndclass1
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
310 wndclass1
.cbClsExtra
= 0;
311 wndclass1
.cbWndExtra
= sizeof( DWORD
); // was 4
312 wndclass1
.hInstance
= wxhInstance
;
313 wndclass1
.hIcon
= (HICON
) NULL
; // wxSTD_MDIPARENTFRAME_ICON;
314 wndclass1
.hCursor
= LoadCursor( (HINSTANCE
) NULL
, IDC_ARROW
);
315 // wndclass1.hbrBackground = (HBRUSH)(COLOR_APPWORKSPACE+1) ;
316 wndclass1
.hbrBackground
= (HBRUSH
) NULL
;
317 wndclass1
.lpszMenuName
= NULL
;
319 wndclass1
.lpszClassName
= wxMDIFrameClassName
;
320 if (!RegisterClass( &wndclass1
))
322 // wxFatalError("Can't register MDI Frame window class");
326 ///////////////////////////////////////////////////////////////////////
327 // Register the MDI child frame window class.
328 WNDCLASS wndclass4
; // Structure used to register Windows class.
330 wndclass4
.style
= CS_HREDRAW
| CS_VREDRAW
;
331 wndclass4
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
332 wndclass4
.cbClsExtra
= 0;
333 wndclass4
.cbWndExtra
= sizeof( DWORD
); // was 4
334 wndclass4
.hInstance
= wxhInstance
;
335 wndclass4
.hIcon
= (HICON
) NULL
; // wxSTD_MDICHILDFRAME_ICON;
336 wndclass4
.hCursor
= LoadCursor( (HINSTANCE
) NULL
, IDC_ARROW
);
337 // TODO: perhaps this should be NULL so that Windows doesn't
338 // paint the background itself (would OnEraseBackground duplicate
340 wndclass4
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+1) ;
341 // wndclass4.hbrBackground = NULL;
342 wndclass4
.lpszMenuName
= NULL
;
343 wndclass4
.lpszClassName
= wxMDIChildFrameClassName
;
345 if (!RegisterClass( &wndclass4
))
347 // wxFatalError("Can't register MDI child frame window class");
351 ///////////////////////////////////////////////////////////////////////
352 // Register the panel window class.
353 WNDCLASS wndclass2
; // Structure used to register Windows class.
354 memset(&wndclass2
, 0, sizeof(WNDCLASS
)); // start with NULL defaults
355 // Use CS_OWNDC to avoid messing about restoring the context
356 // for every graphic operation.
357 wndclass2
.style
= CS_HREDRAW
| CS_VREDRAW
;
358 wndclass2
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
359 wndclass2
.cbClsExtra
= 0;
360 wndclass2
.cbWndExtra
= sizeof( DWORD
); // was 4
361 wndclass2
.hInstance
= wxhInstance
;
362 wndclass2
.hIcon
= (HICON
) NULL
;
363 wndclass2
.hCursor
= (HCURSOR
) NULL
;
364 // wndclass2.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1) ;
365 wndclass2
.hbrBackground
= (HBRUSH
) GetStockObject( LTGRAY_BRUSH
);
366 wndclass2
.lpszMenuName
= NULL
;
367 wndclass2
.lpszClassName
= wxPanelClassName
;
368 if (!RegisterClass( &wndclass2
))
370 // wxFatalError("Can't register Panel Window class");
374 ///////////////////////////////////////////////////////////////////////
375 // Register the canvas and textsubwindow class name
376 WNDCLASS wndclass3
; // Structure used to register Windows class.
377 memset(&wndclass3
, 0, sizeof(WNDCLASS
)); // start with NULL defaults
378 // Use CS_OWNDC to avoid messing about restoring the context
379 // for every graphic operation.
380 // wndclass3.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS ;
382 wndclass3
.style
= CS_HREDRAW
| CS_VREDRAW
| CS_DBLCLKS
;
383 wndclass3
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
384 wndclass3
.cbClsExtra
= 0;
385 wndclass3
.cbWndExtra
= sizeof( DWORD
); // was 4
386 wndclass3
.hInstance
= wxhInstance
;
387 wndclass3
.hIcon
= (HICON
) NULL
;
388 wndclass3
.hCursor
= (HCURSOR
) NULL
;
389 // wndclass3.hbrBackground = (HBRUSH)(COLOR_WINDOW+1) ;
390 wndclass3
.hbrBackground
= (HBRUSH
) NULL
;
391 wndclass3
.lpszMenuName
= NULL
;
392 wndclass3
.lpszClassName
= wxCanvasClassName
;
393 if (!RegisterClass( &wndclass3
))
395 // wxFatalError("Can't register Canvas class");
402 //// Convert Windows to argc, argv style
404 void wxApp::ConvertToStandardCommandArgs(char* lpCmdLine
)
408 wxString
cmdLine(lpCmdLine
);
411 // Get application name
412 char name
[260]; // 260 is MAX_PATH value from windef.h
413 ::GetModuleFileName(wxhInstance
, name
, WXSIZEOF(name
));
415 // GNUWIN32 already fills in the first arg with the application name.
416 #if !defined(__GNUWIN32__)
421 strcpy(name
, wxFileNameFromPath(name
));
422 wxStripExtension(name
);
423 wxTheApp
->SetAppName(name
);
426 // Treat strings enclosed in double-quotes as single arguments
428 int len
= cmdLine
.Length();
432 while ((i
< len
) && isspace(cmdLine
.GetChar(i
)))
437 if (cmdLine
.GetChar(i
) == '"') // We found the start of a string
441 while ((i
< len
) && (cmdLine
.GetChar(i
) != '"'))
444 wxString
arg(cmdLine
.Mid(first
, (i
- first
)));
450 i
++; // Skip past 2nd quote
452 else // Unquoted argument
455 while ((i
< len
) && !isspace(cmdLine
.GetChar(i
)))
458 wxString
arg(cmdLine
.Mid(first
, (i
- first
)));
466 wxTheApp
->argv
= new char*[count
+ 1];
467 for (i
= 0; i
< count
; i
++)
469 wxString
arg(args
[i
]);
470 wxTheApp
->argv
[i
] = copystring((const char*)arg
);
472 wxTheApp
->argv
[count
] = NULL
; // argv[] is a NULL-terminated list
473 wxTheApp
->argc
= count
;
476 //// Cleans up any wxWindows internal structures left lying around
478 void wxApp::CleanUp()
481 wxModule::CleanUpModules();
483 #if wxUSE_WX_RESOURCES
484 wxCleanUpResourceSystem();
486 // wxDefaultResourceTable->ClearTable();
489 // Indicate that the cursor can be freed,
490 // so that cursor won't be deleted by deleting
491 // the bitmap list before g_globalCursor goes out
492 // of scope (double deletion of the cursor).
493 wxSetCursor(wxNullCursor
);
494 delete g_globalCursor
;
496 wxDeleteStockObjects() ;
498 // Destroy all GDI lists, etc.
499 wxDeleteStockLists();
501 delete wxTheColourDatabase
;
502 wxTheColourDatabase
= NULL
;
504 wxBitmap::CleanUpHandlers();
509 //// WINDOWS-SPECIFIC CLEANUP
511 wxSetKeyboardHook(FALSE
);
516 if (gs_hRichEdit
!= (HINSTANCE
) NULL
)
517 FreeLibrary(gs_hRichEdit
);
526 if (wxSTD_FRAME_ICON
)
527 DestroyIcon(wxSTD_FRAME_ICON
);
528 if (wxSTD_MDICHILDFRAME_ICON
)
529 DestroyIcon(wxSTD_MDICHILDFRAME_ICON
);
530 if (wxSTD_MDIPARENTFRAME_ICON
)
531 DestroyIcon(wxSTD_MDIPARENTFRAME_ICON
);
533 if (wxDEFAULT_FRAME_ICON
)
534 DestroyIcon(wxDEFAULT_FRAME_ICON
);
535 if (wxDEFAULT_MDICHILDFRAME_ICON
)
536 DestroyIcon(wxDEFAULT_MDICHILDFRAME_ICON
);
537 if (wxDEFAULT_MDIPARENTFRAME_ICON
)
538 DestroyIcon(wxDEFAULT_MDIPARENTFRAME_ICON
);
540 if ( wxDisableButtonBrush
)
541 ::DeleteObject( wxDisableButtonBrush
) ;
543 #if !defined(__GNUWIN32__) && !defined(__SC__) && !defined(__SALFORDC__)
548 Ctl3dUnregister(wxhInstance
);
552 delete wxWinHandleList
;
554 wxClassInfo::CleanUpClasses();
559 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
560 // At this point we want to check if there are any memory
561 // blocks that aren't part of the wxDebugContext itself,
562 // as a special case. Then when dumping we need to ignore
563 // wxDebugContext, too.
564 if (wxDebugContext::CountObjectsLeft() > 0)
566 wxLogDebug("There were memory leaks.");
567 wxDebugContext::Dump();
568 wxDebugContext::PrintStatistics();
570 // wxDebugContext::SetStream(NULL, NULL);
573 // do it as the very last thing because everything else can log messages
574 wxLog::DontCreateOnDemand();
575 delete wxLog::SetActiveTarget(NULL
);
578 #if !defined(_WINDLL) || (defined(_WINDLL) && defined(WXMAKINGDLL))
580 // temporarily disable this warning
582 #pragma warning(disable: 4715) // not all control paths return a value
585 //// Main wxWindows entry point
586 int wxEntry(WXHINSTANCE hInstance
,
587 WXHINSTANCE
WXUNUSED(hPrevInstance
),
592 // take everything into a try-except block in release build
593 // FIXME other compilers must support Win32 SEH (structured exception
594 // handling) too, just find the appropriate keyword in their docs!
595 // Please note that it's _not_ the same as C++ exceptions!
596 #if !defined(__WXDEBUG__) && defined(_MSC_VER)
597 #define CATCH_PROGRAM_EXCEPTIONS
601 #undef CATCH_PROGRAM_EXCEPTIONS
604 wxhInstance
= (HINSTANCE
) hInstance
;
606 if (!wxApp::Initialize())
609 // create the application object or ensure that one already exists
612 // The app may have declared a global application object, but we recommend
613 // the IMPLEMENT_APP macro is used instead, which sets an initializer
614 // function for delayed, dynamic app object construction.
615 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
616 "No initializer - use IMPLEMENT_APP macro." );
618 wxTheApp
= (*wxApp::GetInitializerFunction()) ();
621 wxCHECK_MSG( wxTheApp
, 0, "You have to define an instance of wxApp!" );
623 // save the WinMain() parameters
624 wxTheApp
->ConvertToStandardCommandArgs(lpCmdLine
);
625 wxTheApp
->m_nCmdShow
= nCmdShow
;
627 // GUI-specific initialisation. In fact on Windows we don't have any,
628 // but this call is provided for compatibility across platforms.
629 wxTheApp
->OnInitGui() ;
633 if ( wxTheApp
->OnInit() )
637 retValue
= wxTheApp
->OnRun();
640 //else: app initialization failed, so we skipped OnRun()
642 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
645 // Forcibly delete the window.
646 if ( topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
647 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
649 topWindow
->Close(TRUE
);
650 wxTheApp
->DeletePendingObjects();
655 wxTheApp
->SetTopWindow(NULL
);
661 // flush the logged messages if any
662 wxLog
*pLog
= wxLog::GetActiveTarget();
663 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
671 #ifdef CATCH_PROGRAM_EXCEPTIONS
673 __except ( EXCEPTION_EXECUTE_HANDLER
) {
676 wxTheApp->OnFatalException();
679 ::ExitProcess(3); // the same exit code as abort()
683 #endif // CATCH_PROGRAM_EXCEPTIONS
686 // restore warning state
688 #pragma warning(default: 4715) // not all control paths return a value
693 //// Entry point for DLLs
695 int wxEntry(WXHINSTANCE hInstance
)
697 wxhInstance
= (HINSTANCE
) hInstance
;
700 // The app may have declared a global application object, but we recommend
701 // the IMPLEMENT_APP macro is used instead, which sets an initializer function
702 // for delayed, dynamic app object construction.
706 if (!wxApp::GetInitializerFunction())
708 MessageBox(NULL
, "No initializer - use IMPLEMENT_APP macro.", "wxWindows Error", MB_APPLMODAL
| MB_ICONSTOP
| MB_OK
);
712 wxTheApp
= (* wxApp::GetInitializerFunction()) ();
716 MessageBox(NULL
, "You have to define an instance of wxApp!", "wxWindows Error", MB_APPLMODAL
| MB_ICONSTOP
| MB_OK
);
721 wxTheApp
->argv
= NULL
;
723 wxTheApp
->OnInitGui();
727 if (wxTheApp
->GetTopWindow() && wxTheApp
->GetTopWindow()->GetHWND()) {
728 wxTheApp
->GetTopWindow()->Show(TRUE
);
735 //// Static member initialization
737 wxAppInitializerFunction
wxApp::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
744 m_wantDebugOutput
= TRUE
;
749 m_printMode
= wxPRINT_WINDOWS
;
751 m_printMode
= wxPRINT_POSTSCRIPT
;
753 m_exitOnFrameDelete
= TRUE
;
759 // Delete command-line args
761 for (i
= 0; i
< argc
; i
++)
768 bool wxApp::Initialized()
776 #ifdef _WINDLL // Assume initialized if DLL (no way of telling)
782 * Get and process a message, returning FALSE if WM_QUIT
783 * received (and also set the flag telling the app to exit the main loop)
786 bool wxApp::DoMessage()
788 BOOL rc
= ::GetMessage(&s_currentMsg
, (HWND
) NULL
, 0, 0);
798 // should never happen, but let's test for it nevertheless
799 wxLogLastError("GetMessage");
804 wxASSERT_MSG( wxThread::IsMain(),
805 "only the main thread can process Windows messages" );
807 static bool s_hadGuiLock
= TRUE
;
808 static wxMsgArray s_aSavedMessages
;
810 // if a secondary thread owns is doing GUI calls, save all messages for
811 // later processing - we can't process them right now because it will
812 // lead to recursive library calls (and we're not reentrant)
813 if ( !wxGuiOwnedByMainThread() )
815 s_hadGuiLock
= FALSE
;
817 // leave out WM_COMMAND messages: too dangerous, sometimes
818 // the message will be processed twice
819 if ( !wxIsWaitingForThread() ||
820 s_currentMsg
.message
!= WM_COMMAND
)
822 s_aSavedMessages
.Add(s_currentMsg
);
829 // have we just regained the GUI lock? if so, post all of the saved
832 // FIXME of course, it's not _exactly_ the same as processing the
833 // messages normally - expect some things to break...
838 size_t count
= s_aSavedMessages
.Count();
839 for ( size_t n
= 0; n
< count
; n
++ )
841 MSG
& msg
= s_aSavedMessages
[n
];
843 if ( !ProcessMessage((WXMSG
*)&msg
) )
845 ::TranslateMessage(&msg
);
846 ::DispatchMessage(&msg
);
850 s_aSavedMessages
.Empty();
853 #endif // wxUSE_THREADS
855 // Process the message
856 if ( !ProcessMessage((WXMSG
*)&s_currentMsg
) )
858 ::TranslateMessage(&s_currentMsg
);
859 wxApp::sm_lastMessageTime
= s_currentMsg
.time
; /* MATTHEW: timeStamp impl. */
860 ::DispatchMessage(&s_currentMsg
);
868 * Keep trying to process messages until WM_QUIT
871 * If there are messages to be processed, they will all be
872 * processed and OnIdle will not be called.
873 * When there are no more messages, OnIdle is called.
874 * If OnIdle requests more time,
875 * it will be repeatedly called so long as there are no pending messages.
876 * A 'feature' of this is that once OnIdle has decided that no more processing
877 * is required, then it won't get processing time until further messages
878 * are processed (it'll sit in DoMessage).
881 int wxApp::MainLoop()
885 while ( m_keepGoing
)
888 wxMutexGuiLeaveOrEnter();
889 #endif // wxUSE_THREADS
891 while ( !::PeekMessage(&s_currentMsg
, 0, 0, 0, PM_NOREMOVE
) &&
899 return s_currentMsg
.wParam
;
902 // Returns TRUE if more time is needed.
903 bool wxApp::ProcessIdle()
906 event
.SetEventObject(this);
909 return event
.MoreRequested();
912 void wxApp::ExitMainLoop()
917 bool wxApp::Pending()
919 return (::PeekMessage(&s_currentMsg
, 0, 0, 0, PM_NOREMOVE
) != 0) ;
922 void wxApp::Dispatch()
928 * Give all windows a chance to preprocess
929 * the message. Some may have accelerator tables, or have
932 bool wxApp::ProcessMessage(WXMSG
*Msg
)
934 MSG
*msg
= (MSG
*)Msg
;
938 // Try translations first; find the youngest window with
939 // a translation table.
940 for (hWnd
= msg
->hwnd
; hWnd
!= (HWND
) NULL
; hWnd
= ::GetParent(hWnd
))
942 wxWindow
*wnd
= wxFindWinFromHandle((WXHWND
) hWnd
);
945 if (wnd
->MSWTranslateMessage(Msg
))
950 // Anyone for a non-translation message? Try youngest descendants first.
951 for (hWnd
= msg
->hwnd
; hWnd
!= (HWND
) NULL
; hWnd
= ::GetParent(hWnd
))
953 wxWindow
*wnd
= wxFindWinFromHandle((WXHWND
) hWnd
);
956 if (wnd
->MSWProcessMessage(Msg
))
963 void wxApp::OnIdle(wxIdleEvent
& event
)
965 static bool s_inOnIdle
= FALSE
;
967 // Avoid recursion (via ProcessEvent default case)
973 // 'Garbage' collection of windows deleted with Close().
974 DeletePendingObjects();
976 // flush the logged messages if any
977 wxLog
*pLog
= wxLog::GetActiveTarget();
978 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
981 // Send OnIdle events to all windows
982 if ( SendIdleEvents() )
984 // SendIdleEvents() returns TRUE if at least one window requested more
986 event
.RequestMore(TRUE
);
992 // Send idle event to all top-level windows
993 bool wxApp::SendIdleEvents()
995 bool needMore
= FALSE
;
996 wxNode
* node
= wxTopLevelWindows
.First();
999 wxWindow
* win
= (wxWindow
*) node
->Data();
1000 if (SendIdleEvents(win
))
1003 node
= node
->Next();
1009 // Send idle event to window and all subwindows
1010 bool wxApp::SendIdleEvents(wxWindow
* win
)
1012 bool needMore
= FALSE
;
1015 event
.SetEventObject(win
);
1016 win
->GetEventHandler()->ProcessEvent(event
);
1018 if (event
.MoreRequested())
1021 wxNode
* node
= win
->GetChildren().First();
1024 wxWindow
* win
= (wxWindow
*) node
->Data();
1025 if (SendIdleEvents(win
))
1028 node
= node
->Next();
1033 void wxApp::DeletePendingObjects()
1035 wxNode
*node
= wxPendingDelete
.First();
1038 wxObject
*obj
= (wxObject
*)node
->Data();
1042 if (wxPendingDelete
.Member(obj
))
1045 // Deleting one object may have deleted other pending
1046 // objects, so start from beginning of list again.
1047 node
= wxPendingDelete
.First();
1051 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
1054 GetTopWindow()->Close(TRUE
);
1057 // Default behaviour: close the application with prompts. The
1058 // user can veto the close, and therefore the end session.
1059 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
1063 if (!GetTopWindow()->Close(!event
.CanVeto()))
1068 wxLog
* wxApp::CreateLogTarget()
1070 return new wxLogGui
;
1073 wxWindow
* wxApp::GetTopWindow() const
1077 else if (wxTopLevelWindows
.Number() > 0)
1078 return (wxWindow
*) wxTopLevelWindows
.First()->Data();
1083 int wxApp::GetComCtl32Version() const
1085 // have we loaded COMCTL32 yet?
1086 HMODULE theModule
= ::GetModuleHandle("COMCTL32");
1089 // if so, then we can check for the version
1092 // InitCommonControlsEx is unique to 4.7 and later
1093 FARPROC theProc
= ::GetProcAddress(theModule
, "InitCommonControlsEx");
1096 { // not found, must be 4.00
1101 // The following symbol are unique to 4.71
1103 // FlatSB_EnableScrollBar FlatSB_GetScrollInfo FlatSB_GetScrollPos
1104 // FlatSB_GetScrollProp FlatSB_GetScrollRange FlatSB_SetScrollInfo
1105 // FlatSB_SetScrollPos FlatSB_SetScrollProp FlatSB_SetScrollRange
1106 // FlatSB_ShowScrollBar
1107 // _DrawIndirectImageList _DuplicateImageList
1109 // UninitializeFlatSB
1110 // we could check for any of these - I chose DllInstall
1111 FARPROC theProc
= ::GetProcAddress(theModule
, "DllInstall");
1114 // not found, must be 4.70
1118 { // found, must be 4.71
1129 FatalAppExit(0, "Fatal error: exiting");
1132 // Yield to incoming messages
1136 // We want to go back to the main message loop
1137 // if we see a WM_QUIT. (?)
1138 while (PeekMessage(&msg
, NULL
, 0, 0, PM_NOREMOVE
) && msg
.message
!= WM_QUIT
)
1140 if ( !wxTheApp
->DoMessage() )
1147 HINSTANCE
wxGetInstance()
1152 // For some reason, with MSVC++ 1.5, WinMain isn't linked in properly
1153 // if in a separate file. So include it here to ensure it's linked.
1154 #if (defined(_MSC_VER) && !defined(__WIN32__)) || (defined(__GNUWIN32__) && !defined(__TWIN32__))