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__)) || defined(__TWIN32__)
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
= (HICON
) NULL
;
120 HICON wxSTD_MDICHILDFRAME_ICON
= (HICON
) NULL
;
121 HICON wxSTD_MDIPARENTFRAME_ICON
= (HICON
) NULL
;
123 HICON wxDEFAULT_FRAME_ICON
= (HICON
) NULL
;
124 HICON wxDEFAULT_MDICHILDFRAME_ICON
= (HICON
) NULL
;
125 HICON wxDEFAULT_MDIPARENTFRAME_ICON
= (HICON
) NULL
;
127 HBRUSH wxDisableButtonBrush
= (HBRUSH
) 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;
143 #if defined(__WIN95__) && !defined(__TWIN32__)
144 #define wxUSE_RICHEDIT 1
146 #define wxUSE_RICHEDIT 0
150 static HINSTANCE gs_hRichEdit
= (HINSTANCE
) NULL
;
155 bool wxApp::Initialize()
157 // Some people may wish to use this, but
158 // probably it shouldn't be here by default.
160 // wxRedirectIOToConsole();
163 wxBuffer
= new char[1500];
165 #ifdef wxUSE_VC_CRTDBG
166 // do check for memory leaks on program exit
167 // (another useful flag is _CRTDBG_DELAY_FREE_MEM_DF which doesn't free
168 // deallocated memory which may be used to simulate low-memory condition)
169 _CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG
) | _CRTDBG_LEAK_CHECK_DF
);
170 #endif // debug build under MS VC++
172 wxClassInfo::InitializeClasses();
175 wxGetResource("wxWindows", "OsVersion", &wxOsVersion
);
178 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
179 wxTheColourDatabase
->Initialize();
181 wxInitializeStockLists();
182 wxInitializeStockObjects();
184 #if wxUSE_WX_RESOURCES
185 wxInitializeResourceSystem();
188 wxBitmap::InitStandardHandlers();
190 #if defined(__WIN95__)
191 InitCommonControls();
194 gs_hRichEdit
= LoadLibrary("RICHED32.DLL");
196 if (gs_hRichEdit
== (HINSTANCE
) NULL
)
198 wxMessageBox("Could not initialise Rich Edit DLL");
206 // for OLE, enlarge message queue to be as large as possible
207 while (!SetMessageQueue(iMsg
) && (iMsg
-= 8));
211 dwOleVer = CoBuildVersion();
213 // check the OLE library version
214 if (rmm != HIWORD(dwOleVer))
216 wxMessageBox("Incorrect version of OLE libraries.");
221 #if !defined(__GNUWIN32__) && !defined(__SC__) && !defined(__SALFORDC__)
222 // we need to initialize OLE library
223 if ( FAILED(::OleInitialize(NULL
)) )
224 wxFatalError(_("Cannot initialize OLE"));
228 if (!Ctl3dRegister(wxhInstance
))
229 wxFatalError("Cannot register CTL3D");
231 Ctl3dAutoSubclass(wxhInstance
);
234 g_globalCursor
= new wxCursor
;
236 wxSTD_FRAME_ICON
= LoadIcon(wxhInstance
, "wxSTD_FRAME");
237 wxSTD_MDIPARENTFRAME_ICON
= LoadIcon(wxhInstance
, "wxSTD_MDIPARENTFRAME");
238 wxSTD_MDICHILDFRAME_ICON
= LoadIcon(wxhInstance
, "wxSTD_MDICHILDFRAME");
240 wxDEFAULT_FRAME_ICON
= LoadIcon(wxhInstance
, "wxDEFAULT_FRAME");
241 wxDEFAULT_MDIPARENTFRAME_ICON
= LoadIcon(wxhInstance
, "wxDEFAULT_MDIPARENTFRAME");
242 wxDEFAULT_MDICHILDFRAME_ICON
= LoadIcon(wxhInstance
, "wxDEFAULT_MDICHILDFRAME");
244 RegisterWindowClasses();
246 // Create the brush for disabling bitmap buttons
249 lb
.lbStyle
= BS_PATTERN
;
250 lb
.lbHatch
= (int)LoadBitmap( wxhInstance
, "wxDISABLE_BUTTON_BITMAP" ) ;
251 wxDisableButtonBrush
= ::CreateBrushIndirect( & lb
) ;
252 ::DeleteObject( (HGDIOBJ
)lb
.lbHatch
) ;
258 wxWinHandleList
= new wxList(wxKEY_INTEGER
);
260 // This is to foil optimizations in Visual C++ that
261 // throw out dummy.obj.
262 #if defined(_MSC_VER) && !defined(WXMAKINGDLL)
263 extern char wxDummyChar
;
264 if (wxDummyChar
) wxDummyChar
++;
267 wxSetKeyboardHook(TRUE
);
269 wxModule::RegisterModules();
270 if (!wxModule::InitializeModules())
275 //// RegisterWindowClasses
277 bool wxApp::RegisterWindowClasses()
279 ///////////////////////////////////////////////////////////////////////
280 // Register the frame window class.
281 WNDCLASS wndclass
; // Structure used to register Windows class.
283 wndclass
.style
= CS_HREDRAW
| CS_VREDRAW
;
284 wndclass
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
285 wndclass
.cbClsExtra
= 0;
286 wndclass
.cbWndExtra
= sizeof( DWORD
); // was 4
287 wndclass
.hInstance
= wxhInstance
;
288 wndclass
.hIcon
= (HICON
) NULL
; // wxSTD_FRAME_ICON;
289 wndclass
.hCursor
= LoadCursor( (HINSTANCE
) NULL
, IDC_ARROW
);
290 wndclass
.hbrBackground
= (HBRUSH
)(COLOR_APPWORKSPACE
+1) ;
291 // wndclass.hbrBackground = GetStockObject( WHITE_BRUSH );
292 wndclass
.lpszMenuName
= NULL
;
293 #ifdef _MULTIPLE_INSTANCES
294 sprintf( wxFrameClassName
,"wxFrameClass%d", wxhInstance
);
296 wndclass
.lpszClassName
= wxFrameClassName
;
298 if (!RegisterClass( &wndclass
))
300 // wxFatalError("Can't register Frame Window class");
303 ///////////////////////////////////////////////////////////////////////
304 // Register the MDI frame window class.
305 WNDCLASS wndclass1
; // Structure used to register Windows class.
307 wndclass1
.style
= CS_HREDRAW
| CS_VREDRAW
;
308 wndclass1
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
309 wndclass1
.cbClsExtra
= 0;
310 wndclass1
.cbWndExtra
= sizeof( DWORD
); // was 4
311 wndclass1
.hInstance
= wxhInstance
;
312 wndclass1
.hIcon
= (HICON
) NULL
; // wxSTD_MDIPARENTFRAME_ICON;
313 wndclass1
.hCursor
= LoadCursor( (HINSTANCE
) NULL
, IDC_ARROW
);
314 // wndclass1.hbrBackground = (HBRUSH)(COLOR_APPWORKSPACE+1) ;
315 wndclass1
.hbrBackground
= (HBRUSH
) NULL
;
316 wndclass1
.lpszMenuName
= NULL
;
318 wndclass1
.lpszClassName
= wxMDIFrameClassName
;
319 if (!RegisterClass( &wndclass1
))
321 // wxFatalError("Can't register MDI Frame window class");
325 ///////////////////////////////////////////////////////////////////////
326 // Register the MDI child frame window class.
327 WNDCLASS wndclass4
; // Structure used to register Windows class.
329 wndclass4
.style
= CS_HREDRAW
| CS_VREDRAW
;
330 wndclass4
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
331 wndclass4
.cbClsExtra
= 0;
332 wndclass4
.cbWndExtra
= sizeof( DWORD
); // was 4
333 wndclass4
.hInstance
= wxhInstance
;
334 wndclass4
.hIcon
= (HICON
) NULL
; // wxSTD_MDICHILDFRAME_ICON;
335 wndclass4
.hCursor
= LoadCursor( (HINSTANCE
) NULL
, IDC_ARROW
);
336 // TODO: perhaps this should be NULL so that Windows doesn't
337 // paint the background itself (would OnEraseBackground duplicate
339 wndclass4
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+1) ;
340 // wndclass4.hbrBackground = NULL;
341 wndclass4
.lpszMenuName
= NULL
;
342 wndclass4
.lpszClassName
= wxMDIChildFrameClassName
;
344 if (!RegisterClass( &wndclass4
))
346 // wxFatalError("Can't register MDI child frame window class");
350 ///////////////////////////////////////////////////////////////////////
351 // Register the panel window class.
352 WNDCLASS wndclass2
; // Structure used to register Windows class.
353 memset(&wndclass2
, 0, sizeof(WNDCLASS
)); // start with NULL defaults
354 // Use CS_OWNDC to avoid messing about restoring the context
355 // for every graphic operation.
356 wndclass2
.style
= CS_HREDRAW
| CS_VREDRAW
;
357 wndclass2
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
358 wndclass2
.cbClsExtra
= 0;
359 wndclass2
.cbWndExtra
= sizeof( DWORD
); // was 4
360 wndclass2
.hInstance
= wxhInstance
;
361 wndclass2
.hIcon
= (HICON
) NULL
;
362 wndclass2
.hCursor
= (HCURSOR
) NULL
;
363 // wndclass2.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1) ;
364 wndclass2
.hbrBackground
= (HBRUSH
) GetStockObject( LTGRAY_BRUSH
);
365 wndclass2
.lpszMenuName
= NULL
;
366 wndclass2
.lpszClassName
= wxPanelClassName
;
367 if (!RegisterClass( &wndclass2
))
369 // wxFatalError("Can't register Panel Window class");
373 ///////////////////////////////////////////////////////////////////////
374 // Register the canvas and textsubwindow class name
375 WNDCLASS wndclass3
; // Structure used to register Windows class.
376 memset(&wndclass3
, 0, sizeof(WNDCLASS
)); // start with NULL defaults
377 // Use CS_OWNDC to avoid messing about restoring the context
378 // for every graphic operation.
379 // wndclass3.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS ;
381 wndclass3
.style
= CS_HREDRAW
| CS_VREDRAW
| CS_DBLCLKS
;
382 wndclass3
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
383 wndclass3
.cbClsExtra
= 0;
384 wndclass3
.cbWndExtra
= sizeof( DWORD
); // was 4
385 wndclass3
.hInstance
= wxhInstance
;
386 wndclass3
.hIcon
= (HICON
) NULL
;
387 wndclass3
.hCursor
= (HCURSOR
) NULL
;
388 // wndclass3.hbrBackground = (HBRUSH)(COLOR_WINDOW+1) ;
389 wndclass3
.hbrBackground
= (HBRUSH
) NULL
;
390 wndclass3
.lpszMenuName
= NULL
;
391 wndclass3
.lpszClassName
= wxCanvasClassName
;
392 if (!RegisterClass( &wndclass3
))
394 // wxFatalError("Can't register Canvas class");
401 //// Convert Windows to argc, argv style
403 void wxApp::ConvertToStandardCommandArgs(char* lpCmdLine
)
407 wxString
cmdLine(lpCmdLine
);
410 // Get application name
411 char name
[260]; // 260 is MAX_PATH value from windef.h
412 ::GetModuleFileName(wxhInstance
, name
, WXSIZEOF(name
));
414 // GNUWIN32 already fills in the first arg with the application name.
415 #if !defined(__GNUWIN32__)
420 strcpy(name
, wxFileNameFromPath(name
));
421 wxStripExtension(name
);
422 wxTheApp
->SetAppName(name
);
425 // Treat strings enclosed in double-quotes as single arguments
427 int len
= cmdLine
.Length();
431 while ((i
< len
) && isspace(cmdLine
.GetChar(i
)))
436 if (cmdLine
.GetChar(i
) == '"') // We found the start of a string
440 while ((i
< len
) && (cmdLine
.GetChar(i
) != '"'))
443 wxString
arg(cmdLine
.Mid(first
, (i
- first
)));
449 i
++; // Skip past 2nd quote
451 else // Unquoted argument
454 while ((i
< len
) && !isspace(cmdLine
.GetChar(i
)))
457 wxString
arg(cmdLine
.Mid(first
, (i
- first
)));
465 wxTheApp
->argv
= new char*[count
+ 1];
466 for (i
= 0; i
< count
; i
++)
468 wxString
arg(args
[i
]);
469 wxTheApp
->argv
[i
] = copystring((const char*)arg
);
471 wxTheApp
->argv
[count
] = NULL
; // argv[] is a NULL-terminated list
472 wxTheApp
->argc
= count
;
475 //// Cleans up any wxWindows internal structures left lying around
477 void wxApp::CleanUp()
480 wxModule::CleanUpModules();
482 #if wxUSE_WX_RESOURCES
483 wxCleanUpResourceSystem();
485 // wxDefaultResourceTable->ClearTable();
488 // Indicate that the cursor can be freed,
489 // so that cursor won't be deleted by deleting
490 // the bitmap list before g_globalCursor goes out
491 // of scope (double deletion of the cursor).
492 wxSetCursor(wxNullCursor
);
493 delete g_globalCursor
;
495 wxDeleteStockObjects() ;
497 // Destroy all GDI lists, etc.
498 wxDeleteStockLists();
500 delete wxTheColourDatabase
;
501 wxTheColourDatabase
= NULL
;
503 wxBitmap::CleanUpHandlers();
508 //// WINDOWS-SPECIFIC CLEANUP
510 wxSetKeyboardHook(FALSE
);
515 if (gs_hRichEdit
!= (HINSTANCE
) NULL
)
516 FreeLibrary(gs_hRichEdit
);
525 if (wxSTD_FRAME_ICON
)
526 DestroyIcon(wxSTD_FRAME_ICON
);
527 if (wxSTD_MDICHILDFRAME_ICON
)
528 DestroyIcon(wxSTD_MDICHILDFRAME_ICON
);
529 if (wxSTD_MDIPARENTFRAME_ICON
)
530 DestroyIcon(wxSTD_MDIPARENTFRAME_ICON
);
532 if (wxDEFAULT_FRAME_ICON
)
533 DestroyIcon(wxDEFAULT_FRAME_ICON
);
534 if (wxDEFAULT_MDICHILDFRAME_ICON
)
535 DestroyIcon(wxDEFAULT_MDICHILDFRAME_ICON
);
536 if (wxDEFAULT_MDIPARENTFRAME_ICON
)
537 DestroyIcon(wxDEFAULT_MDIPARENTFRAME_ICON
);
539 if ( wxDisableButtonBrush
)
540 ::DeleteObject( wxDisableButtonBrush
) ;
542 #if !defined(__GNUWIN32__) && !defined(__SC__) && !defined(__SALFORDC__)
547 Ctl3dUnregister(wxhInstance
);
551 delete wxWinHandleList
;
553 wxClassInfo::CleanUpClasses();
558 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
559 // At this point we want to check if there are any memory
560 // blocks that aren't part of the wxDebugContext itself,
561 // as a special case. Then when dumping we need to ignore
562 // wxDebugContext, too.
563 if (wxDebugContext::CountObjectsLeft() > 0)
565 wxLogDebug("There were memory leaks.");
566 wxDebugContext::Dump();
567 wxDebugContext::PrintStatistics();
569 // wxDebugContext::SetStream(NULL, NULL);
572 // do it as the very last thing because everything else can log messages
573 wxLog::DontCreateOnDemand();
574 delete wxLog::SetActiveTarget(NULL
);
577 #if !defined(_WINDLL) || (defined(_WINDLL) && defined(WXMAKINGDLL))
579 // temporarily disable this warning
581 #pragma warning(disable: 4715) // not all control paths return a value
584 //// Main wxWindows entry point
585 int wxEntry(WXHINSTANCE hInstance
,
586 WXHINSTANCE
WXUNUSED(hPrevInstance
),
591 // take everything into a try-except block in release build
592 // FIXME other compilers must support Win32 SEH (structured exception
593 // handling) too, just find the appropriate keyword in their docs!
594 // Please note that it's _not_ the same as C++ exceptions!
595 #if !defined(__WXDEBUG__) && defined(_MSC_VER)
596 #define CATCH_PROGRAM_EXCEPTIONS
600 #undef CATCH_PROGRAM_EXCEPTIONS
603 wxhInstance
= (HINSTANCE
) hInstance
;
605 if (!wxApp::Initialize())
608 // create the application object or ensure that one already exists
611 // The app may have declared a global application object, but we recommend
612 // the IMPLEMENT_APP macro is used instead, which sets an initializer
613 // function for delayed, dynamic app object construction.
614 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
615 "No initializer - use IMPLEMENT_APP macro." );
617 wxTheApp
= (*wxApp::GetInitializerFunction()) ();
620 wxCHECK_MSG( wxTheApp
, 0, "You have to define an instance of wxApp!" );
622 // save the WinMain() parameters
623 wxTheApp
->ConvertToStandardCommandArgs(lpCmdLine
);
624 wxTheApp
->m_nCmdShow
= nCmdShow
;
626 // GUI-specific initialisation. In fact on Windows we don't have any,
627 // but this call is provided for compatibility across platforms.
628 wxTheApp
->OnInitGui() ;
632 if ( wxTheApp
->OnInit() )
636 retValue
= wxTheApp
->OnRun();
639 //else: app initialization failed, so we skipped OnRun()
641 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
644 // Forcibly delete the window.
645 if ( topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
646 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
648 topWindow
->Close(TRUE
);
649 wxTheApp
->DeletePendingObjects();
654 wxTheApp
->SetTopWindow(NULL
);
660 // flush the logged messages if any
661 wxLog
*pLog
= wxLog::GetActiveTarget();
662 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
670 #ifdef CATCH_PROGRAM_EXCEPTIONS
672 __except ( EXCEPTION_EXECUTE_HANDLER
) {
675 wxTheApp->OnFatalException();
678 ::ExitProcess(3); // the same exit code as abort()
682 #endif // CATCH_PROGRAM_EXCEPTIONS
685 // restore warning state
687 #pragma warning(default: 4715) // not all control paths return a value
692 //// Entry point for DLLs
694 int wxEntry(WXHINSTANCE hInstance
)
696 wxhInstance
= (HINSTANCE
) hInstance
;
699 // The app may have declared a global application object, but we recommend
700 // the IMPLEMENT_APP macro is used instead, which sets an initializer function
701 // for delayed, dynamic app object construction.
705 if (!wxApp::GetInitializerFunction())
707 MessageBox(NULL
, "No initializer - use IMPLEMENT_APP macro.", "wxWindows Error", MB_APPLMODAL
| MB_ICONSTOP
| MB_OK
);
711 wxTheApp
= (* wxApp::GetInitializerFunction()) ();
715 MessageBox(NULL
, "You have to define an instance of wxApp!", "wxWindows Error", MB_APPLMODAL
| MB_ICONSTOP
| MB_OK
);
720 wxTheApp
->argv
= NULL
;
722 wxTheApp
->OnInitGui();
726 if (wxTheApp
->GetTopWindow() && wxTheApp
->GetTopWindow()->GetHWND()) {
727 wxTheApp
->GetTopWindow()->Show(TRUE
);
734 //// Static member initialization
736 wxAppInitializerFunction
wxApp::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
743 m_wantDebugOutput
= TRUE
;
748 m_printMode
= wxPRINT_WINDOWS
;
750 m_printMode
= wxPRINT_POSTSCRIPT
;
752 m_exitOnFrameDelete
= TRUE
;
758 // Delete command-line args
760 for (i
= 0; i
< argc
; i
++)
767 bool wxApp::Initialized()
775 #ifdef _WINDLL // Assume initialized if DLL (no way of telling)
781 * Get and process a message, returning FALSE if WM_QUIT
782 * received (and also set the flag telling the app to exit the main loop)
785 bool wxApp::DoMessage()
787 BOOL rc
= ::GetMessage(&s_currentMsg
, (HWND
) NULL
, 0, 0);
797 // should never happen, but let's test for it nevertheless
798 wxLogLastError("GetMessage");
803 wxASSERT_MSG( wxThread::IsMain(),
804 "only the main thread can process Windows messages" );
806 static bool s_hadGuiLock
= TRUE
;
807 static wxMsgArray s_aSavedMessages
;
809 // if a secondary thread owns is doing GUI calls, save all messages for
810 // later processing - we can't process them right now because it will
811 // lead to recursive library calls (and we're not reentrant)
812 if ( !wxGuiOwnedByMainThread() )
814 s_hadGuiLock
= FALSE
;
816 // leave out WM_COMMAND messages: too dangerous, sometimes
817 // the message will be processed twice
818 if ( !wxIsWaitingForThread() ||
819 s_currentMsg
.message
!= WM_COMMAND
)
821 s_aSavedMessages
.Add(s_currentMsg
);
828 // have we just regained the GUI lock? if so, post all of the saved
831 // FIXME of course, it's not _exactly_ the same as processing the
832 // messages normally - expect some things to break...
837 size_t count
= s_aSavedMessages
.Count();
838 for ( size_t n
= 0; n
< count
; n
++ )
840 MSG
& msg
= s_aSavedMessages
[n
];
842 if ( !ProcessMessage((WXMSG
*)&msg
) )
844 ::TranslateMessage(&msg
);
845 ::DispatchMessage(&msg
);
849 s_aSavedMessages
.Empty();
852 #endif // wxUSE_THREADS
854 // Process the message
855 if ( !ProcessMessage((WXMSG
*)&s_currentMsg
) )
857 ::TranslateMessage(&s_currentMsg
);
858 wxApp::sm_lastMessageTime
= s_currentMsg
.time
; /* MATTHEW: timeStamp impl. */
859 ::DispatchMessage(&s_currentMsg
);
867 * Keep trying to process messages until WM_QUIT
870 * If there are messages to be processed, they will all be
871 * processed and OnIdle will not be called.
872 * When there are no more messages, OnIdle is called.
873 * If OnIdle requests more time,
874 * it will be repeatedly called so long as there are no pending messages.
875 * A 'feature' of this is that once OnIdle has decided that no more processing
876 * is required, then it won't get processing time until further messages
877 * are processed (it'll sit in DoMessage).
880 int wxApp::MainLoop()
884 while ( m_keepGoing
)
887 wxMutexGuiLeaveOrEnter();
888 #endif // wxUSE_THREADS
890 while ( !::PeekMessage(&s_currentMsg
, 0, 0, 0, PM_NOREMOVE
) &&
898 return s_currentMsg
.wParam
;
901 // Returns TRUE if more time is needed.
902 bool wxApp::ProcessIdle()
905 event
.SetEventObject(this);
908 return event
.MoreRequested();
911 void wxApp::ExitMainLoop()
916 bool wxApp::Pending()
918 return (::PeekMessage(&s_currentMsg
, 0, 0, 0, PM_NOREMOVE
) != 0) ;
921 void wxApp::Dispatch()
927 * Give all windows a chance to preprocess
928 * the message. Some may have accelerator tables, or have
931 bool wxApp::ProcessMessage(WXMSG
*Msg
)
933 MSG
*msg
= (MSG
*)Msg
;
937 // Try translations first; find the youngest window with
938 // a translation table.
939 for (hWnd
= msg
->hwnd
; hWnd
!= (HWND
) NULL
; hWnd
= ::GetParent(hWnd
))
941 wxWindow
*wnd
= wxFindWinFromHandle((WXHWND
) hWnd
);
944 if (wnd
->MSWTranslateMessage(Msg
))
949 // Anyone for a non-translation message? Try youngest descendants first.
950 for (hWnd
= msg
->hwnd
; hWnd
!= (HWND
) NULL
; hWnd
= ::GetParent(hWnd
))
952 wxWindow
*wnd
= wxFindWinFromHandle((WXHWND
) hWnd
);
955 if (wnd
->MSWProcessMessage(Msg
))
962 void wxApp::OnIdle(wxIdleEvent
& event
)
964 static bool s_inOnIdle
= FALSE
;
966 // Avoid recursion (via ProcessEvent default case)
972 // 'Garbage' collection of windows deleted with Close().
973 DeletePendingObjects();
975 // flush the logged messages if any
976 wxLog
*pLog
= wxLog::GetActiveTarget();
977 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
980 // Send OnIdle events to all windows
981 if ( SendIdleEvents() )
983 // SendIdleEvents() returns TRUE if at least one window requested more
985 event
.RequestMore(TRUE
);
991 // Send idle event to all top-level windows
992 bool wxApp::SendIdleEvents()
994 bool needMore
= FALSE
;
995 wxNode
* node
= wxTopLevelWindows
.First();
998 wxWindow
* win
= (wxWindow
*) node
->Data();
999 if (SendIdleEvents(win
))
1002 node
= node
->Next();
1008 // Send idle event to window and all subwindows
1009 bool wxApp::SendIdleEvents(wxWindow
* win
)
1011 bool needMore
= FALSE
;
1014 event
.SetEventObject(win
);
1015 win
->GetEventHandler()->ProcessEvent(event
);
1017 if (event
.MoreRequested())
1020 wxNode
* node
= win
->GetChildren().First();
1023 wxWindow
* win
= (wxWindow
*) node
->Data();
1024 if (SendIdleEvents(win
))
1027 node
= node
->Next();
1032 void wxApp::DeletePendingObjects()
1034 wxNode
*node
= wxPendingDelete
.First();
1037 wxObject
*obj
= (wxObject
*)node
->Data();
1041 if (wxPendingDelete
.Member(obj
))
1044 // Deleting one object may have deleted other pending
1045 // objects, so start from beginning of list again.
1046 node
= wxPendingDelete
.First();
1050 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
1053 GetTopWindow()->Close(TRUE
);
1056 // Default behaviour: close the application with prompts. The
1057 // user can veto the close, and therefore the end session.
1058 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
1062 if (!GetTopWindow()->Close(!event
.CanVeto()))
1067 wxLog
* wxApp::CreateLogTarget()
1069 return new wxLogGui
;
1072 wxWindow
* wxApp::GetTopWindow() const
1076 else if (wxTopLevelWindows
.Number() > 0)
1077 return (wxWindow
*) wxTopLevelWindows
.First()->Data();
1082 int wxApp::GetComCtl32Version() const
1084 // have we loaded COMCTL32 yet?
1085 HMODULE theModule
= ::GetModuleHandle("COMCTL32");
1088 // if so, then we can check for the version
1091 // InitCommonControlsEx is unique to 4.7 and later
1092 FARPROC theProc
= ::GetProcAddress(theModule
, "InitCommonControlsEx");
1095 { // not found, must be 4.00
1100 // The following symbol are unique to 4.71
1102 // FlatSB_EnableScrollBar FlatSB_GetScrollInfo FlatSB_GetScrollPos
1103 // FlatSB_GetScrollProp FlatSB_GetScrollRange FlatSB_SetScrollInfo
1104 // FlatSB_SetScrollPos FlatSB_SetScrollProp FlatSB_SetScrollRange
1105 // FlatSB_ShowScrollBar
1106 // _DrawIndirectImageList _DuplicateImageList
1108 // UninitializeFlatSB
1109 // we could check for any of these - I chose DllInstall
1110 FARPROC theProc
= ::GetProcAddress(theModule
, "DllInstall");
1113 // not found, must be 4.70
1117 { // found, must be 4.71
1128 FatalAppExit(0, "Fatal error: exiting");
1131 // Yield to incoming messages
1135 // We want to go back to the main message loop
1136 // if we see a WM_QUIT. (?)
1137 while (PeekMessage(&msg
, NULL
, 0, 0, PM_NOREMOVE
) && msg
.message
!= WM_QUIT
)
1139 if ( !wxTheApp
->DoMessage() )
1146 HINSTANCE
wxGetInstance()
1151 // For some reason, with MSVC++ 1.5, WinMain isn't linked in properly
1152 // if in a separate file. So include it here to ensure it's linked.
1153 #if (defined(_MSC_VER) && !defined(__WIN32__)) || (defined(__GNUWIN32__) && !defined(__TWIN32__))