1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ===========================================================================
14 // ===========================================================================
16 // ---------------------------------------------------------------------------
18 // ---------------------------------------------------------------------------
21 #pragma implementation "app.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
27 #if defined(__BORLANDC__)
35 #include "wx/gdicmn.h"
38 #include "wx/cursor.h"
40 #include "wx/palette.h"
42 #include "wx/dialog.h"
43 #include "wx/msgdlg.h"
45 #include "wx/dynarray.h"
48 #include "wx/msw/private.h"
50 #include "wx/module.h"
53 #include "wx/thread.h"
55 // define the array of MSG strutures
56 WX_DECLARE_OBJARRAY(MSG
, wxMsgArray
);
58 #include "wx/arrimpl.cpp"
60 WX_DEFINE_OBJARRAY(wxMsgArray
);
61 #endif // wxUSE_THREADS
63 #if wxUSE_WX_RESOURCES
64 #include "wx/resource.h"
67 // OLE is used for drag-and-drop, clipboard, OLE Automation...
68 #if defined(__GNUWIN32__) || defined(__SC__) || defined(__SALFORDC__)
72 #endif // broken compilers
81 #if (defined(__WIN95__) && !defined(__GNUWIN32__)) || defined(__TWIN32__)
85 #include "wx/msw/msvcrt.h"
87 // ---------------------------------------------------------------------------
89 // ---------------------------------------------------------------------------
91 extern char *wxBuffer
;
92 extern char *wxOsVersion
;
93 extern wxList
*wxWinHandleList
;
94 extern wxList WXDLLEXPORT wxPendingDelete
;
96 extern wxList wxPendingEvents
;
97 extern wxCriticalSection wxPendingEventsLocker
;
99 extern void wxSetKeyboardHook(bool doIt
);
100 extern wxCursor
*g_globalCursor
;
102 HINSTANCE wxhInstance
= 0;
103 static MSG s_currentMsg
;
104 wxApp
*wxTheApp
= NULL
;
106 // FIXME why not const? and not static?
107 char wxFrameClassName
[] = "wxFrameClass";
108 char wxMDIFrameClassName
[] = "wxMDIFrameClass";
109 char wxMDIChildFrameClassName
[] = "wxMDIChildFrameClass";
110 char wxPanelClassName
[] = "wxPanelClass";
111 char wxCanvasClassName
[] = "wxCanvasClass";
113 HICON wxSTD_FRAME_ICON
= (HICON
) NULL
;
114 HICON wxSTD_MDICHILDFRAME_ICON
= (HICON
) NULL
;
115 HICON wxSTD_MDIPARENTFRAME_ICON
= (HICON
) NULL
;
117 HICON wxDEFAULT_FRAME_ICON
= (HICON
) NULL
;
118 HICON wxDEFAULT_MDICHILDFRAME_ICON
= (HICON
) NULL
;
119 HICON wxDEFAULT_MDIPARENTFRAME_ICON
= (HICON
) NULL
;
121 HBRUSH wxDisableButtonBrush
= (HBRUSH
) 0;
123 LRESULT APIENTRY
wxWndProc(HWND
, UINT
, WPARAM
, LPARAM
);
125 #if defined(__WIN95__) && !defined(__TWIN32__)
126 #define wxUSE_RICHEDIT 1
128 #define wxUSE_RICHEDIT 0
132 static HINSTANCE gs_hRichEdit
= (HINSTANCE
) NULL
;
135 // ===========================================================================
137 // ===========================================================================
139 // ---------------------------------------------------------------------------
141 // ---------------------------------------------------------------------------
143 #if !USE_SHARED_LIBRARY
144 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
146 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
147 EVT_IDLE(wxApp::OnIdle
)
148 EVT_END_SESSION(wxApp::OnEndSession
)
149 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession
)
153 long wxApp::sm_lastMessageTime
= 0;
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]; // FIXME
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();
188 gs_hRichEdit
= LoadLibrary("RICHED32.DLL");
190 if (gs_hRichEdit
== (HINSTANCE
) NULL
)
192 wxLogError(_("Could not initialise Rich Edit DLL"));
194 #endif // wxUSE_RICHEDIT
198 // for OLE, enlarge message queue to be as large as possible
200 while (!SetMessageQueue(iMsg
) && (iMsg
-= 8));
204 dwOleVer = CoBuildVersion();
206 // check the OLE library version
207 if (rmm != HIWORD(dwOleVer))
209 wxMessageBox("Incorrect version of OLE libraries.");
215 // we need to initialize OLE library
216 if ( FAILED(::OleInitialize(NULL
)) )
217 wxLogError(_("Cannot initialize OLE"));
221 if (!Ctl3dRegister(wxhInstance
))
222 wxLogError("Cannot register CTL3D");
224 Ctl3dAutoSubclass(wxhInstance
);
227 g_globalCursor
= new wxCursor
;
229 wxSTD_FRAME_ICON
= LoadIcon(wxhInstance
, "wxSTD_FRAME");
230 wxSTD_MDIPARENTFRAME_ICON
= LoadIcon(wxhInstance
, "wxSTD_MDIPARENTFRAME");
231 wxSTD_MDICHILDFRAME_ICON
= LoadIcon(wxhInstance
, "wxSTD_MDICHILDFRAME");
233 wxDEFAULT_FRAME_ICON
= LoadIcon(wxhInstance
, "wxDEFAULT_FRAME");
234 wxDEFAULT_MDIPARENTFRAME_ICON
= LoadIcon(wxhInstance
, "wxDEFAULT_MDIPARENTFRAME");
235 wxDEFAULT_MDICHILDFRAME_ICON
= LoadIcon(wxhInstance
, "wxDEFAULT_MDICHILDFRAME");
237 RegisterWindowClasses();
239 // Create the brush for disabling bitmap buttons
242 lb
.lbStyle
= BS_PATTERN
;
243 lb
.lbHatch
= (int)LoadBitmap( wxhInstance
, "wxDISABLE_BUTTON_BITMAP" ) ;
244 wxDisableButtonBrush
= ::CreateBrushIndirect( & lb
) ;
245 ::DeleteObject( (HGDIOBJ
)lb
.lbHatch
) ;
251 wxWinHandleList
= new wxList(wxKEY_INTEGER
);
253 // This is to foil optimizations in Visual C++ that throw out dummy.obj.
254 // PLEASE DO NOT ALTER THIS.
255 #if defined(__VISUALC__) && !defined(WXMAKINGDLL)
256 extern char wxDummyChar
;
257 if (wxDummyChar
) wxDummyChar
++;
260 wxSetKeyboardHook(TRUE
);
262 wxModule::RegisterModules();
263 if (!wxModule::InitializeModules())
268 //// RegisterWindowClasses
270 bool wxApp::RegisterWindowClasses()
272 ///////////////////////////////////////////////////////////////////////
273 // Register the frame window class.
274 WNDCLASS wndclass
; // Structure used to register Windows class.
276 wndclass
.style
= CS_HREDRAW
| CS_VREDRAW
| CS_DBLCLKS
;
277 wndclass
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
278 wndclass
.cbClsExtra
= 0;
279 wndclass
.cbWndExtra
= sizeof( DWORD
); // was 4
280 wndclass
.hInstance
= wxhInstance
;
281 wndclass
.hIcon
= (HICON
) NULL
; // wxSTD_FRAME_ICON;
282 wndclass
.hCursor
= LoadCursor( (HINSTANCE
) NULL
, IDC_ARROW
);
283 wndclass
.hbrBackground
= (HBRUSH
)(COLOR_APPWORKSPACE
+1) ;
284 // wndclass.hbrBackground = GetStockObject( WHITE_BRUSH );
285 wndclass
.lpszMenuName
= NULL
;
286 #ifdef _MULTIPLE_INSTANCES
287 sprintf( wxFrameClassName
,"wxFrameClass%d", wxhInstance
);
289 wndclass
.lpszClassName
= wxFrameClassName
;
291 if (!RegisterClass( &wndclass
))
293 // wxFatalError("Can't register Frame Window class");
296 ///////////////////////////////////////////////////////////////////////
297 // Register the MDI frame window class.
298 WNDCLASS wndclass1
; // Structure used to register Windows class.
300 wndclass1
.style
= CS_HREDRAW
| CS_VREDRAW
| CS_DBLCLKS
;
301 wndclass1
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
302 wndclass1
.cbClsExtra
= 0;
303 wndclass1
.cbWndExtra
= sizeof( DWORD
); // was 4
304 wndclass1
.hInstance
= wxhInstance
;
305 wndclass1
.hIcon
= (HICON
) NULL
; // wxSTD_MDIPARENTFRAME_ICON;
306 wndclass1
.hCursor
= LoadCursor( (HINSTANCE
) NULL
, IDC_ARROW
);
307 // wndclass1.hbrBackground = (HBRUSH)(COLOR_APPWORKSPACE+1) ;
308 wndclass1
.hbrBackground
= (HBRUSH
) NULL
;
309 wndclass1
.lpszMenuName
= NULL
;
311 wndclass1
.lpszClassName
= wxMDIFrameClassName
;
312 if (!RegisterClass( &wndclass1
))
314 // wxFatalError("Can't register MDI Frame window class");
318 ///////////////////////////////////////////////////////////////////////
319 // Register the MDI child frame window class.
320 WNDCLASS wndclass4
; // Structure used to register Windows class.
322 wndclass4
.style
= CS_HREDRAW
| CS_VREDRAW
| CS_DBLCLKS
;
323 wndclass4
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
324 wndclass4
.cbClsExtra
= 0;
325 wndclass4
.cbWndExtra
= sizeof( DWORD
); // was 4
326 wndclass4
.hInstance
= wxhInstance
;
327 wndclass4
.hIcon
= (HICON
) NULL
; // wxSTD_MDICHILDFRAME_ICON;
328 wndclass4
.hCursor
= LoadCursor( (HINSTANCE
) NULL
, IDC_ARROW
);
329 // TODO: perhaps this should be NULL so that Windows doesn't
330 // paint the background itself (would OnEraseBackground duplicate
332 wndclass4
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+1) ;
333 // wndclass4.hbrBackground = NULL;
334 wndclass4
.lpszMenuName
= NULL
;
335 wndclass4
.lpszClassName
= wxMDIChildFrameClassName
;
337 if (!RegisterClass( &wndclass4
))
339 // wxFatalError("Can't register MDI child frame window class");
343 ///////////////////////////////////////////////////////////////////////
344 // Register the panel window class.
345 WNDCLASS wndclass2
; // Structure used to register Windows class.
346 memset(&wndclass2
, 0, sizeof(WNDCLASS
)); // start with NULL defaults
347 wndclass2
.style
= CS_HREDRAW
| CS_VREDRAW
| CS_DBLCLKS
;
348 wndclass2
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
349 wndclass2
.cbClsExtra
= 0;
350 wndclass2
.cbWndExtra
= sizeof( DWORD
); // was 4
351 wndclass2
.hInstance
= wxhInstance
;
352 wndclass2
.hIcon
= (HICON
) NULL
;
353 wndclass2
.hCursor
= LoadCursor( (HINSTANCE
) NULL
, IDC_ARROW
);
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
= (HICON
) NULL
;
378 wndclass3
.hCursor
= LoadCursor( (HINSTANCE
) NULL
, IDC_ARROW
);
379 // wndclass3.hbrBackground = (HBRUSH)(COLOR_WINDOW+1) ;
380 wndclass3
.hbrBackground
= (HBRUSH
) 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
));
408 strcpy(name
, wxFileNameFromPath(name
));
409 wxStripExtension(name
);
410 wxTheApp
->SetAppName(name
);
413 // Treat strings enclosed in double-quotes as single arguments
415 int len
= cmdLine
.Length();
419 while ((i
< len
) && isspace(cmdLine
.GetChar(i
)))
424 if (cmdLine
.GetChar(i
) == '"') // We found the start of a string
428 while ((i
< len
) && (cmdLine
.GetChar(i
) != '"'))
431 wxString
arg(cmdLine
.Mid(first
, (i
- first
)));
437 i
++; // Skip past 2nd quote
439 else // Unquoted argument
442 while ((i
< len
) && !isspace(cmdLine
.GetChar(i
)))
445 wxString
arg(cmdLine
.Mid(first
, (i
- first
)));
453 wxTheApp
->argv
= new char*[count
+ 1];
454 for (i
= 0; i
< count
; i
++)
456 wxString
arg(args
[i
]);
457 wxTheApp
->argv
[i
] = copystring((const char*)arg
);
459 wxTheApp
->argv
[count
] = NULL
; // argv[] is a NULL-terminated list
460 wxTheApp
->argc
= count
;
463 //// Cleans up any wxWindows internal structures left lying around
465 void wxApp::CleanUp()
469 // flush the logged messages if any and install a 'safer' log target: the
470 // default one (wxLogGui) can't be used after the resources are freed just
471 // below and the user suppliedo ne might be even more unsafe (using any
472 // wxWindows GUI function is unsafe starting from now)
473 wxLog::DontCreateOnDemand();
475 // this will flush the old messages if any
476 delete wxLog::SetActiveTarget(new wxLogStderr
);
478 // One last chance for pending objects to be cleaned up
479 wxTheApp
->DeletePendingObjects();
481 wxModule::CleanUpModules();
483 #if wxUSE_WX_RESOURCES
484 wxCleanUpResourceSystem();
486 // wxDefaultResourceTable->ClearTable();
489 // Indicate that the cursor can be freed, so that cursor won't be deleted
490 // by deleting the bitmap list before g_globalCursor goes out of scope
491 // (double deletion of the cursor).
492 wxSetCursor(wxNullCursor
);
493 delete g_globalCursor
;
494 g_globalCursor
= NULL
;
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
) ;
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(TRUE
) > 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 delete wxLog::SetActiveTarget(NULL
);
577 #if !defined(_WINDLL) || (defined(_WINDLL) && defined(WXMAKINGDLL))
579 // temporarily disable this warning which would be generated in release builds
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 // do check for memory leaks on program exit
593 // (another useful flag is _CRTDBG_DELAY_FREE_MEM_DF which doesn't free
594 // deallocated memory which may be used to simulate low-memory condition)
595 wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF
);
597 // take everything into a try-except block in release build
598 // FIXME other compilers must support Win32 SEH (structured exception
599 // handling) too, just find the appropriate keyword in their docs!
600 // Please note that it's _not_ the same as C++ exceptions!
601 #if !defined(__WXDEBUG__) && defined(__VISUALC__)
602 #define CATCH_PROGRAM_EXCEPTIONS
606 #undef CATCH_PROGRAM_EXCEPTIONS
609 wxhInstance
= (HINSTANCE
) hInstance
;
611 if (!wxApp::Initialize())
614 // create the application object or ensure that one already exists
617 // The app may have declared a global application object, but we recommend
618 // the IMPLEMENT_APP macro is used instead, which sets an initializer
619 // function for delayed, dynamic app object construction.
620 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
621 "No initializer - use IMPLEMENT_APP macro." );
623 wxTheApp
= (*wxApp::GetInitializerFunction()) ();
626 wxCHECK_MSG( wxTheApp
, 0, "You have to define an instance of wxApp!" );
628 // save the WinMain() parameters
629 wxTheApp
->ConvertToStandardCommandArgs(lpCmdLine
);
630 wxTheApp
->m_nCmdShow
= nCmdShow
;
632 // GUI-specific initialisation. In fact on Windows we don't have any,
633 // but this call is provided for compatibility across platforms.
634 wxTheApp
->OnInitGui() ;
638 if ( wxTheApp
->OnInit() )
642 retValue
= wxTheApp
->OnRun();
645 // We want to initialize, but not run or exit immediately.
648 //else: app initialization failed, so we skipped OnRun()
650 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
653 // Forcibly delete the window.
654 if ( topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
655 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
657 topWindow
->Close(TRUE
);
658 wxTheApp
->DeletePendingObjects();
663 wxTheApp
->SetTopWindow(NULL
);
673 #ifdef CATCH_PROGRAM_EXCEPTIONS
675 __except ( EXCEPTION_EXECUTE_HANDLER
) {
678 wxTheApp->OnFatalException();
681 // using wxLog would be unsafe here
683 _("Unrecoverable program error detected: "
684 " the application will terminate."),
686 MB_APPLMODAL
| MB_ICONSTOP
| MB_OK
);
688 ::ExitProcess(3); // the same exit code as abort()
692 #endif // CATCH_PROGRAM_EXCEPTIONS
695 // restore warning state
697 #pragma warning(default: 4715) // not all control paths return a value
702 //// Entry point for DLLs
704 int wxEntry(WXHINSTANCE hInstance
)
706 wxhInstance
= (HINSTANCE
) hInstance
;
709 // The app may have declared a global application object, but we recommend
710 // the IMPLEMENT_APP macro is used instead, which sets an initializer function
711 // for delayed, dynamic app object construction.
714 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
715 "No initializer - use IMPLEMENT_APP macro." );
717 wxTheApp
= (* wxApp::GetInitializerFunction()) ();
720 wxCHECK_MSG( wxTheApp
, 0, "You have to define an instance of wxApp!" );
723 wxTheApp
->argv
= NULL
;
725 wxTheApp
->OnInitGui();
729 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
730 if ( topWindow
&& topWindow
->GetHWND())
732 topWindow
->Show(TRUE
);
739 //// Static member initialization
741 wxAppInitializerFunction
wxApp::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
748 m_wantDebugOutput
= TRUE
;
753 m_printMode
= wxPRINT_WINDOWS
;
755 m_printMode
= wxPRINT_POSTSCRIPT
;
757 m_exitOnFrameDelete
= TRUE
;
763 // Delete command-line args
765 for (i
= 0; i
< argc
; i
++)
772 bool wxApp::Initialized()
780 #ifdef _WINDLL // Assume initialized if DLL (no way of telling)
786 * Get and process a message, returning FALSE if WM_QUIT
787 * received (and also set the flag telling the app to exit the main loop)
790 bool wxApp::DoMessage()
792 BOOL rc
= ::GetMessage(&s_currentMsg
, (HWND
) NULL
, 0, 0);
802 // should never happen, but let's test for it nevertheless
803 wxLogLastError("GetMessage");
808 wxASSERT_MSG( wxThread::IsMain(),
809 "only the main thread can process Windows messages" );
811 static bool s_hadGuiLock
= TRUE
;
812 static wxMsgArray s_aSavedMessages
;
814 // if a secondary thread owns is doing GUI calls, save all messages for
815 // later processing - we can't process them right now because it will
816 // lead to recursive library calls (and we're not reentrant)
817 if ( !wxGuiOwnedByMainThread() )
819 s_hadGuiLock
= FALSE
;
821 // leave out WM_COMMAND messages: too dangerous, sometimes
822 // the message will be processed twice
823 if ( !wxIsWaitingForThread() ||
824 s_currentMsg
.message
!= WM_COMMAND
)
826 s_aSavedMessages
.Add(s_currentMsg
);
833 // have we just regained the GUI lock? if so, post all of the saved
836 // FIXME of course, it's not _exactly_ the same as processing the
837 // messages normally - expect some things to break...
842 size_t count
= s_aSavedMessages
.Count();
843 for ( size_t n
= 0; n
< count
; n
++ )
845 MSG
& msg
= s_aSavedMessages
[n
];
847 if ( !ProcessMessage((WXMSG
*)&msg
) )
849 ::TranslateMessage(&msg
);
850 ::DispatchMessage(&msg
);
854 s_aSavedMessages
.Empty();
857 #endif // wxUSE_THREADS
859 // Process the message
860 if ( !ProcessMessage((WXMSG
*)&s_currentMsg
) )
862 ::TranslateMessage(&s_currentMsg
);
863 wxApp::sm_lastMessageTime
= s_currentMsg
.time
; /* MATTHEW: timeStamp impl. */
864 ::DispatchMessage(&s_currentMsg
);
872 * Keep trying to process messages until WM_QUIT
875 * If there are messages to be processed, they will all be
876 * processed and OnIdle will not be called.
877 * When there are no more messages, OnIdle is called.
878 * If OnIdle requests more time,
879 * it will be repeatedly called so long as there are no pending messages.
880 * A 'feature' of this is that once OnIdle has decided that no more processing
881 * is required, then it won't get processing time until further messages
882 * are processed (it'll sit in DoMessage).
885 int wxApp::MainLoop()
889 while ( m_keepGoing
)
892 wxMutexGuiLeaveOrEnter();
893 #endif // wxUSE_THREADS
895 while ( !::PeekMessage(&s_currentMsg
, 0, 0, 0, PM_NOREMOVE
) &&
903 // If they are pending events, we must process them.
905 ProcessPendingEvents();
909 return s_currentMsg
.wParam
;
912 // Returns TRUE if more time is needed.
913 bool wxApp::ProcessIdle()
916 event
.SetEventObject(this);
919 return event
.MoreRequested();
923 void wxApp::ProcessPendingEvents()
925 wxNode
*node
= wxPendingEvents
.First();
926 wxCriticalSectionLocker
locker(wxPendingEventsLocker
);
930 wxEvtHandler
*handler
= (wxEvtHandler
*)node
->Data();
932 handler
->ProcessPendingEvents();
935 node
= wxPendingEvents
.First();
941 void wxApp::ExitMainLoop()
946 bool wxApp::Pending()
948 return (::PeekMessage(&s_currentMsg
, 0, 0, 0, PM_NOREMOVE
) != 0) ;
951 void wxApp::Dispatch()
957 * Give all windows a chance to preprocess
958 * the message. Some may have accelerator tables, or have
962 bool wxApp::ProcessMessage(WXMSG
*wxmsg
)
964 MSG
*msg
= (MSG
*)wxmsg
;
965 HWND hWnd
= msg
->hwnd
;
966 wxWindow
*wndThis
= wxFindWinFromHandle((WXHWND
)hWnd
), *wnd
;
968 // Try translations first; find the youngest window with
969 // a translation table.
970 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
972 if ( wnd
->MSWTranslateMessage(wxmsg
) )
976 // Anyone for a non-translation message? Try youngest descendants first.
977 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
979 if ( wnd
->MSWProcessMessage(wxmsg
) )
986 void wxApp::OnIdle(wxIdleEvent
& event
)
988 static bool s_inOnIdle
= FALSE
;
990 // Avoid recursion (via ProcessEvent default case)
996 // 'Garbage' collection of windows deleted with Close().
997 DeletePendingObjects();
999 // flush the logged messages if any
1000 wxLog
*pLog
= wxLog::GetActiveTarget();
1001 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
1004 // Send OnIdle events to all windows
1005 if ( SendIdleEvents() )
1007 // SendIdleEvents() returns TRUE if at least one window requested more
1009 event
.RequestMore(TRUE
);
1015 // Send idle event to all top-level windows
1016 bool wxApp::SendIdleEvents()
1018 bool needMore
= FALSE
;
1020 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
1023 wxWindow
* win
= node
->GetData();
1024 if (SendIdleEvents(win
))
1026 node
= node
->GetNext();
1032 // Send idle event to window and all subwindows
1033 bool wxApp::SendIdleEvents(wxWindow
* win
)
1035 bool needMore
= FALSE
;
1038 event
.SetEventObject(win
);
1039 win
->GetEventHandler()->ProcessEvent(event
);
1041 if (event
.MoreRequested())
1044 wxNode
* node
= win
->GetChildren().First();
1047 wxWindow
* win
= (wxWindow
*) node
->Data();
1048 if (SendIdleEvents(win
))
1051 node
= node
->Next();
1056 void wxApp::DeletePendingObjects()
1058 wxNode
*node
= wxPendingDelete
.First();
1061 wxObject
*obj
= (wxObject
*)node
->Data();
1065 if (wxPendingDelete
.Member(obj
))
1068 // Deleting one object may have deleted other pending
1069 // objects, so start from beginning of list again.
1070 node
= wxPendingDelete
.First();
1074 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
1077 GetTopWindow()->Close(TRUE
);
1080 // Default behaviour: close the application with prompts. The
1081 // user can veto the close, and therefore the end session.
1082 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
1086 if (!GetTopWindow()->Close(!event
.CanVeto()))
1091 wxLog
* wxApp::CreateLogTarget()
1093 return new wxLogGui
;
1096 wxWindow
* wxApp::GetTopWindow() const
1100 else if (wxTopLevelWindows
.GetCount() > 0)
1101 return wxTopLevelWindows
.GetFirst()->GetData();
1106 int wxApp::GetComCtl32Version() const
1108 // have we loaded COMCTL32 yet?
1109 HMODULE theModule
= ::GetModuleHandle("COMCTL32");
1112 // if so, then we can check for the version
1115 // InitCommonControlsEx is unique to 4.7 and later
1116 FARPROC theProc
= ::GetProcAddress(theModule
, "InitCommonControlsEx");
1119 { // not found, must be 4.00
1124 // The following symbol are unique to 4.71
1126 // FlatSB_EnableScrollBar FlatSB_GetScrollInfo FlatSB_GetScrollPos
1127 // FlatSB_GetScrollProp FlatSB_GetScrollRange FlatSB_SetScrollInfo
1128 // FlatSB_SetScrollPos FlatSB_SetScrollProp FlatSB_SetScrollRange
1129 // FlatSB_ShowScrollBar
1130 // _DrawIndirectImageList _DuplicateImageList
1132 // UninitializeFlatSB
1133 // we could check for any of these - I chose DllInstall
1134 FARPROC theProc
= ::GetProcAddress(theModule
, "DllInstall");
1137 // not found, must be 4.70
1141 { // found, must be 4.71
1151 wxLogError(_("Fatal error: exiting"));
1156 // Yield to incoming messages
1160 // We want to go back to the main message loop
1161 // if we see a WM_QUIT. (?)
1162 while (PeekMessage(&msg
, NULL
, 0, 0, PM_NOREMOVE
) && msg
.message
!= WM_QUIT
)
1164 if ( !wxTheApp
->DoMessage() )
1171 HINSTANCE
wxGetInstance()
1176 void wxSetInstance(HINSTANCE hInst
)
1178 wxhInstance
= hInst
;
1181 // For some reason, with MSVC++ 1.5, WinMain isn't linked in properly
1182 // if in a separate file. So include it here to ensure it's linked.
1183 #if (defined(__VISUALC__) && !defined(__WIN32__)) || (defined(__GNUWIN32__) && !defined(__TWIN32__))