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 // I'm annoyed ... I don't know where to put this and I don't want to create // a module for that as it's part of the core.
174 wxPendingEvents
= new wxList();
175 wxPendingEventsLocker
= new wxCriticalSection();
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 wxLogError(_("Could not initialise Rich Edit DLL"));
200 #endif // wxUSE_RICHEDIT
204 // for OLE, enlarge message queue to be as large as possible
206 while (!SetMessageQueue(iMsg
) && (iMsg
-= 8));
210 dwOleVer = CoBuildVersion();
212 // check the OLE library version
213 if (rmm != HIWORD(dwOleVer))
215 wxMessageBox("Incorrect version of OLE libraries.");
221 // we need to initialize OLE library
222 if ( FAILED(::OleInitialize(NULL
)) )
223 wxLogError(_("Cannot initialize OLE"));
227 if (!Ctl3dRegister(wxhInstance
))
228 wxLogError("Cannot register CTL3D");
230 Ctl3dAutoSubclass(wxhInstance
);
233 g_globalCursor
= new wxCursor
;
235 wxSTD_FRAME_ICON
= LoadIcon(wxhInstance
, "wxSTD_FRAME");
236 wxSTD_MDIPARENTFRAME_ICON
= LoadIcon(wxhInstance
, "wxSTD_MDIPARENTFRAME");
237 wxSTD_MDICHILDFRAME_ICON
= LoadIcon(wxhInstance
, "wxSTD_MDICHILDFRAME");
239 wxDEFAULT_FRAME_ICON
= LoadIcon(wxhInstance
, "wxDEFAULT_FRAME");
240 wxDEFAULT_MDIPARENTFRAME_ICON
= LoadIcon(wxhInstance
, "wxDEFAULT_MDIPARENTFRAME");
241 wxDEFAULT_MDICHILDFRAME_ICON
= LoadIcon(wxhInstance
, "wxDEFAULT_MDICHILDFRAME");
243 RegisterWindowClasses();
245 // Create the brush for disabling bitmap buttons
248 lb
.lbStyle
= BS_PATTERN
;
249 lb
.lbHatch
= (int)LoadBitmap( wxhInstance
, "wxDISABLE_BUTTON_BITMAP" ) ;
250 wxDisableButtonBrush
= ::CreateBrushIndirect( & lb
) ;
251 ::DeleteObject( (HGDIOBJ
)lb
.lbHatch
) ;
257 wxWinHandleList
= new wxList(wxKEY_INTEGER
);
259 // This is to foil optimizations in Visual C++ that throw out dummy.obj.
260 // PLEASE DO NOT ALTER THIS.
261 #if defined(__VISUALC__) && !defined(WXMAKINGDLL)
262 extern char wxDummyChar
;
263 if (wxDummyChar
) wxDummyChar
++;
266 wxSetKeyboardHook(TRUE
);
268 wxModule::RegisterModules();
269 if (!wxModule::InitializeModules())
274 //// RegisterWindowClasses
276 bool wxApp::RegisterWindowClasses()
278 ///////////////////////////////////////////////////////////////////////
279 // Register the frame window class.
280 WNDCLASS wndclass
; // Structure used to register Windows class.
282 wndclass
.style
= CS_HREDRAW
| CS_VREDRAW
| CS_DBLCLKS
;
283 wndclass
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
284 wndclass
.cbClsExtra
= 0;
285 wndclass
.cbWndExtra
= sizeof( DWORD
); // was 4
286 wndclass
.hInstance
= wxhInstance
;
287 wndclass
.hIcon
= (HICON
) NULL
; // wxSTD_FRAME_ICON;
288 wndclass
.hCursor
= LoadCursor( (HINSTANCE
) NULL
, IDC_ARROW
);
289 wndclass
.hbrBackground
= (HBRUSH
)(COLOR_APPWORKSPACE
+1) ;
290 // wndclass.hbrBackground = GetStockObject( WHITE_BRUSH );
291 wndclass
.lpszMenuName
= NULL
;
292 #ifdef _MULTIPLE_INSTANCES
293 sprintf( wxFrameClassName
,"wxFrameClass%d", wxhInstance
);
295 wndclass
.lpszClassName
= wxFrameClassName
;
297 if (!RegisterClass( &wndclass
))
299 // wxFatalError("Can't register Frame Window class");
302 ///////////////////////////////////////////////////////////////////////
303 // Register the MDI frame window class.
304 WNDCLASS wndclass1
; // Structure used to register Windows class.
306 wndclass1
.style
= CS_HREDRAW
| CS_VREDRAW
| CS_DBLCLKS
;
307 wndclass1
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
308 wndclass1
.cbClsExtra
= 0;
309 wndclass1
.cbWndExtra
= sizeof( DWORD
); // was 4
310 wndclass1
.hInstance
= wxhInstance
;
311 wndclass1
.hIcon
= (HICON
) NULL
; // wxSTD_MDIPARENTFRAME_ICON;
312 wndclass1
.hCursor
= LoadCursor( (HINSTANCE
) NULL
, IDC_ARROW
);
313 // wndclass1.hbrBackground = (HBRUSH)(COLOR_APPWORKSPACE+1) ;
314 wndclass1
.hbrBackground
= (HBRUSH
) NULL
;
315 wndclass1
.lpszMenuName
= NULL
;
317 wndclass1
.lpszClassName
= wxMDIFrameClassName
;
318 if (!RegisterClass( &wndclass1
))
320 // wxFatalError("Can't register MDI Frame window class");
324 ///////////////////////////////////////////////////////////////////////
325 // Register the MDI child frame window class.
326 WNDCLASS wndclass4
; // Structure used to register Windows class.
328 wndclass4
.style
= CS_HREDRAW
| CS_VREDRAW
| CS_DBLCLKS
;
329 wndclass4
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
330 wndclass4
.cbClsExtra
= 0;
331 wndclass4
.cbWndExtra
= sizeof( DWORD
); // was 4
332 wndclass4
.hInstance
= wxhInstance
;
333 wndclass4
.hIcon
= (HICON
) NULL
; // wxSTD_MDICHILDFRAME_ICON;
334 wndclass4
.hCursor
= LoadCursor( (HINSTANCE
) NULL
, IDC_ARROW
);
335 // TODO: perhaps this should be NULL so that Windows doesn't
336 // paint the background itself (would OnEraseBackground duplicate
338 wndclass4
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+1) ;
339 // wndclass4.hbrBackground = NULL;
340 wndclass4
.lpszMenuName
= NULL
;
341 wndclass4
.lpszClassName
= wxMDIChildFrameClassName
;
343 if (!RegisterClass( &wndclass4
))
345 // wxFatalError("Can't register MDI child frame window class");
349 ///////////////////////////////////////////////////////////////////////
350 // Register the panel window class.
351 WNDCLASS wndclass2
; // Structure used to register Windows class.
352 memset(&wndclass2
, 0, sizeof(WNDCLASS
)); // start with NULL defaults
353 wndclass2
.style
= CS_HREDRAW
| CS_VREDRAW
| CS_DBLCLKS
;
354 wndclass2
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
355 wndclass2
.cbClsExtra
= 0;
356 wndclass2
.cbWndExtra
= sizeof( DWORD
); // was 4
357 wndclass2
.hInstance
= wxhInstance
;
358 wndclass2
.hIcon
= (HICON
) NULL
;
359 wndclass2
.hCursor
= LoadCursor( (HINSTANCE
) NULL
, IDC_ARROW
);
360 // wndclass2.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1) ;
361 wndclass2
.hbrBackground
= (HBRUSH
) GetStockObject( LTGRAY_BRUSH
);
362 wndclass2
.lpszMenuName
= NULL
;
363 wndclass2
.lpszClassName
= wxPanelClassName
;
364 if (!RegisterClass( &wndclass2
))
366 // wxFatalError("Can't register Panel Window class");
370 ///////////////////////////////////////////////////////////////////////
371 // Register the canvas and textsubwindow class name
372 WNDCLASS wndclass3
; // Structure used to register Windows class.
373 memset(&wndclass3
, 0, sizeof(WNDCLASS
)); // start with NULL defaults
374 // Use CS_OWNDC to avoid messing about restoring the context
375 // for every graphic operation.
376 // wndclass3.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS ;
378 wndclass3
.style
= CS_HREDRAW
| CS_VREDRAW
| CS_DBLCLKS
;
379 wndclass3
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
380 wndclass3
.cbClsExtra
= 0;
381 wndclass3
.cbWndExtra
= sizeof( DWORD
); // was 4
382 wndclass3
.hInstance
= wxhInstance
;
383 wndclass3
.hIcon
= (HICON
) NULL
;
384 wndclass3
.hCursor
= LoadCursor( (HINSTANCE
) NULL
, IDC_ARROW
);
385 // wndclass3.hbrBackground = (HBRUSH)(COLOR_WINDOW+1) ;
386 wndclass3
.hbrBackground
= (HBRUSH
) NULL
;
387 wndclass3
.lpszMenuName
= NULL
;
388 wndclass3
.lpszClassName
= wxCanvasClassName
;
389 if (!RegisterClass( &wndclass3
))
391 // wxFatalError("Can't register Canvas class");
398 //// Convert Windows to argc, argv style
400 void wxApp::ConvertToStandardCommandArgs(char* lpCmdLine
)
404 wxString
cmdLine(lpCmdLine
);
407 // Get application name
408 char name
[260]; // 260 is MAX_PATH value from windef.h
409 ::GetModuleFileName(wxhInstance
, name
, WXSIZEOF(name
));
414 strcpy(name
, wxFileNameFromPath(name
));
415 wxStripExtension(name
);
416 wxTheApp
->SetAppName(name
);
419 // Treat strings enclosed in double-quotes as single arguments
421 int len
= cmdLine
.Length();
425 while ((i
< len
) && isspace(cmdLine
.GetChar(i
)))
430 if (cmdLine
.GetChar(i
) == '"') // We found the start of a string
434 while ((i
< len
) && (cmdLine
.GetChar(i
) != '"'))
437 wxString
arg(cmdLine
.Mid(first
, (i
- first
)));
443 i
++; // Skip past 2nd quote
445 else // Unquoted argument
448 while ((i
< len
) && !isspace(cmdLine
.GetChar(i
)))
451 wxString
arg(cmdLine
.Mid(first
, (i
- first
)));
459 wxTheApp
->argv
= new char*[count
+ 1];
460 for (i
= 0; i
< count
; i
++)
462 wxString
arg(args
[i
]);
463 wxTheApp
->argv
[i
] = copystring((const char*)arg
);
465 wxTheApp
->argv
[count
] = NULL
; // argv[] is a NULL-terminated list
466 wxTheApp
->argc
= count
;
469 //// Cleans up any wxWindows internal structures left lying around
471 void wxApp::CleanUp()
475 // flush the logged messages if any and install a 'safer' log target: the
476 // default one (wxLogGui) can't be used after the resources are freed just
477 // below and the user suppliedo ne might be even more unsafe (using any
478 // wxWindows GUI function is unsafe starting from now)
479 wxLog::DontCreateOnDemand();
481 // this will flush the old messages if any
482 delete wxLog::SetActiveTarget(new wxLogStderr
);
484 // One last chance for pending objects to be cleaned up
485 wxTheApp
->DeletePendingObjects();
487 wxModule::CleanUpModules();
489 #if wxUSE_WX_RESOURCES
490 wxCleanUpResourceSystem();
492 // wxDefaultResourceTable->ClearTable();
495 // Indicate that the cursor can be freed, so that cursor won't be deleted
496 // by deleting the bitmap list before g_globalCursor goes out of scope
497 // (double deletion of the cursor).
498 wxSetCursor(wxNullCursor
);
499 delete g_globalCursor
;
500 g_globalCursor
= NULL
;
502 wxDeleteStockObjects() ;
504 // Destroy all GDI lists, etc.
505 wxDeleteStockLists();
507 delete wxTheColourDatabase
;
508 wxTheColourDatabase
= NULL
;
510 wxBitmap::CleanUpHandlers();
515 //// WINDOWS-SPECIFIC CLEANUP
517 wxSetKeyboardHook(FALSE
);
522 if (gs_hRichEdit
!= (HINSTANCE
) NULL
)
523 FreeLibrary(gs_hRichEdit
);
532 if (wxSTD_FRAME_ICON
)
533 DestroyIcon(wxSTD_FRAME_ICON
);
534 if (wxSTD_MDICHILDFRAME_ICON
)
535 DestroyIcon(wxSTD_MDICHILDFRAME_ICON
);
536 if (wxSTD_MDIPARENTFRAME_ICON
)
537 DestroyIcon(wxSTD_MDIPARENTFRAME_ICON
);
539 if (wxDEFAULT_FRAME_ICON
)
540 DestroyIcon(wxDEFAULT_FRAME_ICON
);
541 if (wxDEFAULT_MDICHILDFRAME_ICON
)
542 DestroyIcon(wxDEFAULT_MDICHILDFRAME_ICON
);
543 if (wxDEFAULT_MDIPARENTFRAME_ICON
)
544 DestroyIcon(wxDEFAULT_MDIPARENTFRAME_ICON
);
546 if ( wxDisableButtonBrush
)
547 ::DeleteObject( wxDisableButtonBrush
) ;
554 Ctl3dUnregister(wxhInstance
);
558 delete wxWinHandleList
;
560 // GL: I'm annoyed ... I don't know where to put this and I don't want to
561 // create a module for that as it's part of the core.
563 delete wxPendingEvents
;
564 delete wxPendingEventsLocker
;
567 wxClassInfo::CleanUpClasses();
572 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
573 // At this point we want to check if there are any memory
574 // blocks that aren't part of the wxDebugContext itself,
575 // as a special case. Then when dumping we need to ignore
576 // wxDebugContext, too.
577 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
579 wxLogDebug("There were memory leaks.");
580 wxDebugContext::Dump();
581 wxDebugContext::PrintStatistics();
583 // wxDebugContext::SetStream(NULL, NULL);
586 // do it as the very last thing because everything else can log messages
587 delete wxLog::SetActiveTarget(NULL
);
590 #if !defined(_WINDLL) || (defined(_WINDLL) && defined(WXMAKINGDLL))
592 // temporarily disable this warning which would be generated in release builds
595 #pragma warning(disable: 4715) // not all control paths return a value
598 //// Main wxWindows entry point
599 int wxEntry(WXHINSTANCE hInstance
,
600 WXHINSTANCE
WXUNUSED(hPrevInstance
),
605 // do check for memory leaks on program exit
606 // (another useful flag is _CRTDBG_DELAY_FREE_MEM_DF which doesn't free
607 // deallocated memory which may be used to simulate low-memory condition)
608 wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF
);
610 // take everything into a try-except block in release build
611 // FIXME other compilers must support Win32 SEH (structured exception
612 // handling) too, just find the appropriate keyword in their docs!
613 // Please note that it's _not_ the same as C++ exceptions!
614 #if !defined(__WXDEBUG__) && defined(__VISUALC__)
615 #define CATCH_PROGRAM_EXCEPTIONS
619 #undef CATCH_PROGRAM_EXCEPTIONS
622 wxhInstance
= (HINSTANCE
) hInstance
;
624 if (!wxApp::Initialize())
627 // create the application object or ensure that one already exists
630 // The app may have declared a global application object, but we recommend
631 // the IMPLEMENT_APP macro is used instead, which sets an initializer
632 // function for delayed, dynamic app object construction.
633 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
634 "No initializer - use IMPLEMENT_APP macro." );
636 wxTheApp
= (*wxApp::GetInitializerFunction()) ();
639 wxCHECK_MSG( wxTheApp
, 0, "You have to define an instance of wxApp!" );
641 // save the WinMain() parameters
642 wxTheApp
->ConvertToStandardCommandArgs(lpCmdLine
);
643 wxTheApp
->m_nCmdShow
= nCmdShow
;
645 // GUI-specific initialisation. In fact on Windows we don't have any,
646 // but this call is provided for compatibility across platforms.
647 wxTheApp
->OnInitGui() ;
651 if ( wxTheApp
->OnInit() )
655 retValue
= wxTheApp
->OnRun();
658 // We want to initialize, but not run or exit immediately.
661 //else: app initialization failed, so we skipped OnRun()
663 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
666 // Forcibly delete the window.
667 if ( topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
668 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
670 topWindow
->Close(TRUE
);
671 wxTheApp
->DeletePendingObjects();
676 wxTheApp
->SetTopWindow(NULL
);
686 #ifdef CATCH_PROGRAM_EXCEPTIONS
688 __except ( EXCEPTION_EXECUTE_HANDLER
) {
691 wxTheApp->OnFatalException();
694 // using wxLog would be unsafe here
696 _("Unrecoverable program error detected: "
697 " the application will terminate."),
699 MB_APPLMODAL
| MB_ICONSTOP
| MB_OK
);
701 ::ExitProcess(3); // the same exit code as abort()
705 #endif // CATCH_PROGRAM_EXCEPTIONS
708 // restore warning state
710 #pragma warning(default: 4715) // not all control paths return a value
715 //// Entry point for DLLs
717 int wxEntry(WXHINSTANCE hInstance
)
719 wxhInstance
= (HINSTANCE
) hInstance
;
722 // The app may have declared a global application object, but we recommend
723 // the IMPLEMENT_APP macro is used instead, which sets an initializer function
724 // for delayed, dynamic app object construction.
727 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
728 "No initializer - use IMPLEMENT_APP macro." );
730 wxTheApp
= (* wxApp::GetInitializerFunction()) ();
733 wxCHECK_MSG( wxTheApp
, 0, "You have to define an instance of wxApp!" );
736 wxTheApp
->argv
= NULL
;
738 wxTheApp
->OnInitGui();
742 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
743 if ( topWindow
&& topWindow
->GetHWND())
745 topWindow
->Show(TRUE
);
752 //// Static member initialization
754 wxAppInitializerFunction
wxApp::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
761 m_wantDebugOutput
= TRUE
;
766 m_printMode
= wxPRINT_WINDOWS
;
768 m_printMode
= wxPRINT_POSTSCRIPT
;
770 m_exitOnFrameDelete
= TRUE
;
776 // Delete command-line args
778 for (i
= 0; i
< argc
; i
++)
785 bool wxApp::Initialized()
793 #ifdef _WINDLL // Assume initialized if DLL (no way of telling)
799 * Get and process a message, returning FALSE if WM_QUIT
800 * received (and also set the flag telling the app to exit the main loop)
803 bool wxApp::DoMessage()
805 BOOL rc
= ::GetMessage(&s_currentMsg
, (HWND
) NULL
, 0, 0);
815 // should never happen, but let's test for it nevertheless
816 wxLogLastError("GetMessage");
821 wxASSERT_MSG( wxThread::IsMain(),
822 "only the main thread can process Windows messages" );
824 static bool s_hadGuiLock
= TRUE
;
825 static wxMsgArray s_aSavedMessages
;
827 // if a secondary thread owns is doing GUI calls, save all messages for
828 // later processing - we can't process them right now because it will
829 // lead to recursive library calls (and we're not reentrant)
830 if ( !wxGuiOwnedByMainThread() )
832 s_hadGuiLock
= FALSE
;
834 // leave out WM_COMMAND messages: too dangerous, sometimes
835 // the message will be processed twice
836 if ( !wxIsWaitingForThread() ||
837 s_currentMsg
.message
!= WM_COMMAND
)
839 s_aSavedMessages
.Add(s_currentMsg
);
846 // have we just regained the GUI lock? if so, post all of the saved
849 // FIXME of course, it's not _exactly_ the same as processing the
850 // messages normally - expect some things to break...
855 size_t count
= s_aSavedMessages
.Count();
856 for ( size_t n
= 0; n
< count
; n
++ )
858 MSG
& msg
= s_aSavedMessages
[n
];
860 if ( !ProcessMessage((WXMSG
*)&msg
) )
862 ::TranslateMessage(&msg
);
863 ::DispatchMessage(&msg
);
867 s_aSavedMessages
.Empty();
870 #endif // wxUSE_THREADS
872 // Process the message
873 if ( !ProcessMessage((WXMSG
*)&s_currentMsg
) )
875 ::TranslateMessage(&s_currentMsg
);
876 wxApp::sm_lastMessageTime
= s_currentMsg
.time
; /* MATTHEW: timeStamp impl. */
877 ::DispatchMessage(&s_currentMsg
);
885 * Keep trying to process messages until WM_QUIT
888 * If there are messages to be processed, they will all be
889 * processed and OnIdle will not be called.
890 * When there are no more messages, OnIdle is called.
891 * If OnIdle requests more time,
892 * it will be repeatedly called so long as there are no pending messages.
893 * A 'feature' of this is that once OnIdle has decided that no more processing
894 * is required, then it won't get processing time until further messages
895 * are processed (it'll sit in DoMessage).
898 int wxApp::MainLoop()
902 while ( m_keepGoing
)
905 wxMutexGuiLeaveOrEnter();
906 #endif // wxUSE_THREADS
908 while ( !::PeekMessage(&s_currentMsg
, 0, 0, 0, PM_NOREMOVE
) &&
916 // If they are pending events, we must process them.
918 ProcessPendingEvents();
922 return s_currentMsg
.wParam
;
925 // Returns TRUE if more time is needed.
926 bool wxApp::ProcessIdle()
929 event
.SetEventObject(this);
932 return event
.MoreRequested();
936 void wxApp::ProcessPendingEvents()
938 wxNode
*node
= wxPendingEvents
->First();
939 wxCriticalSectionLocker
locker(*wxPendingEventsLocker
);
943 wxEvtHandler
*handler
= (wxEvtHandler
*)node
->Data();
945 handler
->ProcessPendingEvents();
948 node
= wxPendingEvents
->First();
954 void wxApp::ExitMainLoop()
959 bool wxApp::Pending()
961 return (::PeekMessage(&s_currentMsg
, 0, 0, 0, PM_NOREMOVE
) != 0) ;
964 void wxApp::Dispatch()
970 * Give all windows a chance to preprocess
971 * the message. Some may have accelerator tables, or have
975 bool wxApp::ProcessMessage(WXMSG
*wxmsg
)
977 MSG
*msg
= (MSG
*)wxmsg
;
978 HWND hWnd
= msg
->hwnd
;
979 wxWindow
*wndThis
= wxFindWinFromHandle((WXHWND
)hWnd
), *wnd
;
981 // Try translations first; find the youngest window with
982 // a translation table.
983 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
985 if ( wnd
->MSWTranslateMessage(wxmsg
) )
989 // Anyone for a non-translation message? Try youngest descendants first.
990 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
992 if ( wnd
->MSWProcessMessage(wxmsg
) )
999 void wxApp::OnIdle(wxIdleEvent
& event
)
1001 static bool s_inOnIdle
= FALSE
;
1003 // Avoid recursion (via ProcessEvent default case)
1009 // 'Garbage' collection of windows deleted with Close().
1010 DeletePendingObjects();
1012 // flush the logged messages if any
1013 wxLog
*pLog
= wxLog::GetActiveTarget();
1014 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
1017 // Send OnIdle events to all windows
1018 if ( SendIdleEvents() )
1020 // SendIdleEvents() returns TRUE if at least one window requested more
1022 event
.RequestMore(TRUE
);
1028 // Send idle event to all top-level windows
1029 bool wxApp::SendIdleEvents()
1031 bool needMore
= FALSE
;
1033 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
1036 wxWindow
* win
= node
->GetData();
1037 if (SendIdleEvents(win
))
1039 node
= node
->GetNext();
1045 // Send idle event to window and all subwindows
1046 bool wxApp::SendIdleEvents(wxWindow
* win
)
1048 bool needMore
= FALSE
;
1051 event
.SetEventObject(win
);
1052 win
->GetEventHandler()->ProcessEvent(event
);
1054 if (event
.MoreRequested())
1057 wxNode
* node
= win
->GetChildren().First();
1060 wxWindow
* win
= (wxWindow
*) node
->Data();
1061 if (SendIdleEvents(win
))
1064 node
= node
->Next();
1069 void wxApp::DeletePendingObjects()
1071 wxNode
*node
= wxPendingDelete
.First();
1074 wxObject
*obj
= (wxObject
*)node
->Data();
1078 if (wxPendingDelete
.Member(obj
))
1081 // Deleting one object may have deleted other pending
1082 // objects, so start from beginning of list again.
1083 node
= wxPendingDelete
.First();
1087 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
1090 GetTopWindow()->Close(TRUE
);
1093 // Default behaviour: close the application with prompts. The
1094 // user can veto the close, and therefore the end session.
1095 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
1099 if (!GetTopWindow()->Close(!event
.CanVeto()))
1104 wxLog
* wxApp::CreateLogTarget()
1106 return new wxLogGui
;
1109 wxWindow
* wxApp::GetTopWindow() const
1113 else if (wxTopLevelWindows
.GetCount() > 0)
1114 return wxTopLevelWindows
.GetFirst()->GetData();
1119 int wxApp::GetComCtl32Version() const
1121 // have we loaded COMCTL32 yet?
1122 HMODULE theModule
= ::GetModuleHandle("COMCTL32");
1125 // if so, then we can check for the version
1128 // InitCommonControlsEx is unique to 4.7 and later
1129 FARPROC theProc
= ::GetProcAddress(theModule
, "InitCommonControlsEx");
1132 { // not found, must be 4.00
1137 // The following symbol are unique to 4.71
1139 // FlatSB_EnableScrollBar FlatSB_GetScrollInfo FlatSB_GetScrollPos
1140 // FlatSB_GetScrollProp FlatSB_GetScrollRange FlatSB_SetScrollInfo
1141 // FlatSB_SetScrollPos FlatSB_SetScrollProp FlatSB_SetScrollRange
1142 // FlatSB_ShowScrollBar
1143 // _DrawIndirectImageList _DuplicateImageList
1145 // UninitializeFlatSB
1146 // we could check for any of these - I chose DllInstall
1147 FARPROC theProc
= ::GetProcAddress(theModule
, "DllInstall");
1150 // not found, must be 4.70
1154 { // found, must be 4.71
1164 wxLogError(_("Fatal error: exiting"));
1169 // Yield to incoming messages
1173 // We want to go back to the main message loop
1174 // if we see a WM_QUIT. (?)
1175 while (PeekMessage(&msg
, NULL
, 0, 0, PM_NOREMOVE
) && msg
.message
!= WM_QUIT
)
1177 if ( !wxTheApp
->DoMessage() )
1184 HINSTANCE
wxGetInstance()
1189 void wxSetInstance(HINSTANCE hInst
)
1191 wxhInstance
= hInst
;
1194 // For some reason, with MSVC++ 1.5, WinMain isn't linked in properly
1195 // if in a separate file. So include it here to ensure it's linked.
1196 #if (defined(__VISUALC__) && !defined(__WIN32__)) || (defined(__GNUWIN32__) && !defined(__TWIN32__))