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"
22 #pragma implementation "appbase.h"
25 // For compilers that support precompilation, includes "wx.h".
26 #include "wx/wxprec.h"
28 #if defined(__BORLANDC__)
36 #include "wx/gdicmn.h"
39 #include "wx/cursor.h"
41 #include "wx/palette.h"
43 #include "wx/dialog.h"
44 #include "wx/msgdlg.h"
46 #include "wx/dynarray.h"
47 # include "wx/wxchar.h"
52 #include "wx/module.h"
54 #include "wx/msw/private.h"
57 #include "wx/thread.h"
59 // define the array of MSG strutures
60 WX_DECLARE_OBJARRAY(MSG
, wxMsgArray
);
62 #include "wx/arrimpl.cpp"
64 WX_DEFINE_OBJARRAY(wxMsgArray
);
65 #endif // wxUSE_THREADS
67 #if wxUSE_WX_RESOURCES
68 #include "wx/resource.h"
71 // OLE is used for drag-and-drop, clipboard, OLE Automation...
72 #ifndef wxUSE_NORLANDER_HEADERS
73 #if defined(__GNUWIN32__) || defined(__SC__) || defined(__SALFORDC__)
77 #endif // broken compilers
87 #if (defined(__WIN95__) && !defined(__GNUWIN32__)) || defined(__TWIN32__) || defined(wxUSE_NORLANDER_HEADERS)
91 #include "wx/msw/msvcrt.h"
93 // ---------------------------------------------------------------------------
95 // ---------------------------------------------------------------------------
97 extern wxChar
*wxBuffer
;
98 extern wxChar
*wxOsVersion
;
99 extern wxList
*wxWinHandleList
;
100 extern wxList WXDLLEXPORT wxPendingDelete
;
102 extern wxList
*wxPendingEvents
;
103 extern wxCriticalSection
*wxPendingEventsLocker
;
105 extern void wxSetKeyboardHook(bool doIt
);
106 extern wxCursor
*g_globalCursor
;
108 HINSTANCE wxhInstance
= 0;
110 wxApp
*wxTheApp
= NULL
;
112 // FIXME why not const? and not static?
114 // NB: all "NoRedraw" classes must have the same names as the "normal" classes
115 // with NR suffix - wxWindow::MSWCreate() supposes this
116 wxChar wxFrameClassName
[] = wxT("wxFrameClass");
117 wxChar wxFrameClassNameNoRedraw
[] = wxT("wxFrameClassNR");
118 wxChar wxMDIFrameClassName
[] = wxT("wxMDIFrameClass");
119 wxChar wxMDIFrameClassNameNoRedraw
[] = wxT("wxMDIFrameClassNR");
120 wxChar wxMDIChildFrameClassName
[] = wxT("wxMDIChildFrameClass");
121 wxChar wxMDIChildFrameClassNameNoRedraw
[] = wxT("wxMDIChildFrameClassNR");
122 wxChar wxPanelClassName
[] = wxT("wxPanelClass");
123 wxChar wxCanvasClassName
[] = wxT("wxCanvasClass");
125 HICON wxSTD_FRAME_ICON
= (HICON
) NULL
;
126 HICON wxSTD_MDICHILDFRAME_ICON
= (HICON
) NULL
;
127 HICON wxSTD_MDIPARENTFRAME_ICON
= (HICON
) NULL
;
129 HICON wxDEFAULT_FRAME_ICON
= (HICON
) NULL
;
130 HICON wxDEFAULT_MDICHILDFRAME_ICON
= (HICON
) NULL
;
131 HICON wxDEFAULT_MDIPARENTFRAME_ICON
= (HICON
) NULL
;
133 HBRUSH wxDisableButtonBrush
= (HBRUSH
) 0;
135 LRESULT APIENTRY
wxWndProc(HWND
, UINT
, WPARAM
, LPARAM
);
137 #if defined(__WIN95__) && !defined(__TWIN32__)
138 #define wxUSE_RICHEDIT 1
140 #define wxUSE_RICHEDIT 0
144 static HINSTANCE gs_hRichEdit
= (HINSTANCE
) NULL
;
147 // ===========================================================================
149 // ===========================================================================
151 // ---------------------------------------------------------------------------
153 // ---------------------------------------------------------------------------
155 #if !USE_SHARED_LIBRARY
156 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
158 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
159 EVT_IDLE(wxApp::OnIdle
)
160 EVT_END_SESSION(wxApp::OnEndSession
)
161 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession
)
166 bool wxApp::Initialize()
168 // Some people may wish to use this, but
169 // probably it shouldn't be here by default.
171 // wxRedirectIOToConsole();
174 wxBuffer
= new wxChar
[1500]; // FIXME
176 wxClassInfo::InitializeClasses();
179 wxGetResource(wxT("wxWindows"), wxT("OsVersion"), &wxOsVersion
);
182 // I'm annoyed ... I don't know where to put this and I don't want to
183 // create a module for that as it's part of the core.
185 wxPendingEvents
= new wxList();
186 wxPendingEventsLocker
= new wxCriticalSection();
189 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
190 wxTheColourDatabase
->Initialize();
192 wxInitializeStockLists();
193 wxInitializeStockObjects();
195 #if wxUSE_WX_RESOURCES
196 wxInitializeResourceSystem();
199 wxBitmap::InitStandardHandlers();
201 #if defined(__WIN95__)
202 InitCommonControls();
205 gs_hRichEdit
= LoadLibrary(wxT("RICHED32.DLL"));
207 if (gs_hRichEdit
== (HINSTANCE
) NULL
)
209 wxLogError(_("Could not initialise Rich Edit DLL"));
211 #endif // wxUSE_RICHEDIT
218 // for OLE, enlarge message queue to be as large as possible
220 while (!SetMessageQueue(iMsg
) && (iMsg
-= 8))
223 // we need to initialize OLE library
224 if ( FAILED(::OleInitialize(NULL
)) )
225 wxLogError(_("Cannot initialize OLE"));
229 if (!Ctl3dRegister(wxhInstance
))
230 wxLogError(wxT("Cannot register CTL3D"));
232 Ctl3dAutoSubclass(wxhInstance
);
235 g_globalCursor
= new wxCursor
;
237 wxSTD_FRAME_ICON
= LoadIcon(wxhInstance
, wxT("wxSTD_FRAME"));
238 wxSTD_MDIPARENTFRAME_ICON
= LoadIcon(wxhInstance
, wxT("wxSTD_MDIPARENTFRAME"));
239 wxSTD_MDICHILDFRAME_ICON
= LoadIcon(wxhInstance
, wxT("wxSTD_MDICHILDFRAME"));
241 wxDEFAULT_FRAME_ICON
= LoadIcon(wxhInstance
, wxT("wxDEFAULT_FRAME"));
242 wxDEFAULT_MDIPARENTFRAME_ICON
= LoadIcon(wxhInstance
, wxT("wxDEFAULT_MDIPARENTFRAME"));
243 wxDEFAULT_MDICHILDFRAME_ICON
= LoadIcon(wxhInstance
, wxT("wxDEFAULT_MDICHILDFRAME"));
245 RegisterWindowClasses();
247 // Create the brush for disabling bitmap buttons
250 lb
.lbStyle
= BS_PATTERN
;
251 lb
.lbHatch
= (int)LoadBitmap( wxhInstance
, wxT("wxDISABLE_BUTTON_BITMAP") );
254 wxDisableButtonBrush
= ::CreateBrushIndirect( & lb
);
255 ::DeleteObject( (HGDIOBJ
)lb
.lbHatch
);
257 //else: wxWindows resources are probably not linked in
263 wxWinHandleList
= new wxList(wxKEY_INTEGER
);
265 // This is to foil optimizations in Visual C++ that throw out dummy.obj.
266 // PLEASE DO NOT ALTER THIS.
267 #if defined(__VISUALC__) && !defined(WXMAKINGDLL)
268 extern char wxDummyChar
;
269 if (wxDummyChar
) wxDummyChar
++;
272 wxSetKeyboardHook(TRUE
);
274 wxModule::RegisterModules();
275 if (!wxModule::InitializeModules())
280 // ---------------------------------------------------------------------------
281 // RegisterWindowClasses
282 // ---------------------------------------------------------------------------
284 // TODO we should only register classes really used by the app. For this it
285 // would be enough to just delay the class registration until an attempt
286 // to create a window of this class is made.
287 bool wxApp::RegisterWindowClasses()
291 // for each class we register one with CS_(V|H)REDRAW style and one
292 // without for windows created with wxNO_FULL_REDRAW_ON_REPAINT flag
293 static const long styleNormal
= CS_HREDRAW
| CS_VREDRAW
| CS_DBLCLKS
;
294 static const long styleNoRedraw
= CS_DBLCLKS
;
296 // the fields which are common to all classes
297 wndclass
.lpfnWndProc
= (WNDPROC
)wxWndProc
;
298 wndclass
.cbClsExtra
= 0;
299 wndclass
.cbWndExtra
= sizeof( DWORD
); // VZ: what is this DWORD used for?
300 wndclass
.hInstance
= wxhInstance
;
301 wndclass
.hIcon
= (HICON
) NULL
;
302 wndclass
.hCursor
= ::LoadCursor((HINSTANCE
)NULL
, IDC_ARROW
);
303 wndclass
.lpszMenuName
= NULL
;
305 // Register the frame window class.
306 wndclass
.hbrBackground
= (HBRUSH
)(COLOR_APPWORKSPACE
+ 1);
307 wndclass
.lpszClassName
= wxFrameClassName
;
308 wndclass
.style
= styleNormal
;
310 if ( !RegisterClass(&wndclass
) )
312 wxLogLastError("RegisterClass(frame)");
318 wndclass
.lpszClassName
= wxFrameClassNameNoRedraw
;
319 wndclass
.style
= styleNoRedraw
;
321 if ( !RegisterClass(&wndclass
) )
323 wxLogLastError("RegisterClass(no redraw frame)");
328 // Register the MDI frame window class.
329 wndclass
.hbrBackground
= (HBRUSH
)NULL
; // paint MDI frame ourselves
330 wndclass
.lpszClassName
= wxMDIFrameClassName
;
331 wndclass
.style
= styleNormal
;
333 if ( !RegisterClass(&wndclass
) )
335 wxLogLastError("RegisterClass(MDI parent)");
340 // "no redraw" MDI frame
341 wndclass
.lpszClassName
= wxMDIFrameClassNameNoRedraw
;
342 wndclass
.style
= styleNoRedraw
;
344 if ( !RegisterClass(&wndclass
) )
346 wxLogLastError("RegisterClass(no redraw MDI parent frame)");
351 // Register the MDI child frame window class.
352 wndclass
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+ 1);
353 wndclass
.lpszClassName
= wxMDIChildFrameClassName
;
354 wndclass
.style
= styleNormal
;
356 if ( !RegisterClass(&wndclass
) )
358 wxLogLastError("RegisterClass(MDI child)");
363 // "no redraw" MDI child frame
364 wndclass
.lpszClassName
= wxMDIChildFrameClassNameNoRedraw
;
365 wndclass
.style
= styleNoRedraw
;
367 if ( !RegisterClass(&wndclass
) )
369 wxLogLastError("RegisterClass(no redraw MDI child)");
374 // Register the panel window class.
375 wndclass
.hbrBackground
= (HBRUSH
) GetStockObject( LTGRAY_BRUSH
);
376 wndclass
.lpszClassName
= wxPanelClassName
;
377 wndclass
.style
= styleNormal
;
379 if ( !RegisterClass(&wndclass
) )
381 wxLogLastError("RegisterClass(panel)");
386 // Register the canvas and textsubwindow class name
387 wndclass
.hbrBackground
= (HBRUSH
)NULL
;
388 wndclass
.lpszClassName
= wxCanvasClassName
;
390 if ( !RegisterClass(&wndclass
) )
392 wxLogLastError("RegisterClass(canvas)");
400 // ---------------------------------------------------------------------------
401 // Convert Windows to argc, argv style
402 // ---------------------------------------------------------------------------
404 void wxApp::ConvertToStandardCommandArgs(char* lpCmdLine
)
408 wxString
cmdLine(lpCmdLine
);
411 // Get application name
412 wxChar name
[260]; // 260 is MAX_PATH value from windef.h
413 ::GetModuleFileName(wxhInstance
, name
, WXSIZEOF(name
));
418 wxStrcpy(name
, wxFileNameFromPath(name
));
419 wxStripExtension(name
);
420 wxTheApp
->SetAppName(name
);
423 // Treat strings enclosed in double-quotes as single arguments
425 int len
= cmdLine
.Length();
429 while ((i
< len
) && wxIsspace(cmdLine
.GetChar(i
)))
434 if (cmdLine
.GetChar(i
) == wxT('"')) // We found the start of a string
438 while ((i
< len
) && (cmdLine
.GetChar(i
) != wxT('"')))
441 wxString
arg(cmdLine
.Mid(first
, (i
- first
)));
447 i
++; // Skip past 2nd quote
449 else // Unquoted argument
452 while ((i
< len
) && !wxIsspace(cmdLine
.GetChar(i
)))
455 wxString
arg(cmdLine
.Mid(first
, (i
- first
)));
463 wxTheApp
->argv
= new wxChar
*[count
+ 1];
464 for (i
= 0; i
< count
; i
++)
466 wxString
arg(args
[i
]);
467 wxTheApp
->argv
[i
] = copystring((const wxChar
*)arg
);
469 wxTheApp
->argv
[count
] = NULL
; // argv[] is a NULL-terminated list
470 wxTheApp
->argc
= count
;
473 //// Cleans up any wxWindows internal structures left lying around
475 void wxApp::CleanUp()
480 // flush the logged messages if any and install a 'safer' log target: the
481 // default one (wxLogGui) can't be used after the resources are freed just
482 // below and the user suppliedo ne might be even more unsafe (using any
483 // wxWindows GUI function is unsafe starting from now)
484 wxLog::DontCreateOnDemand();
486 // this will flush the old messages if any
487 delete wxLog::SetActiveTarget(new wxLogStderr
);
490 // One last chance for pending objects to be cleaned up
491 wxTheApp
->DeletePendingObjects();
493 wxModule::CleanUpModules();
495 #if wxUSE_WX_RESOURCES
496 wxCleanUpResourceSystem();
498 // wxDefaultResourceTable->ClearTable();
501 // Indicate that the cursor can be freed, so that cursor won't be deleted
502 // by deleting the bitmap list before g_globalCursor goes out of scope
503 // (double deletion of the cursor).
504 wxSetCursor(wxNullCursor
);
505 delete g_globalCursor
;
506 g_globalCursor
= NULL
;
508 wxDeleteStockObjects();
510 // Destroy all GDI lists, etc.
511 wxDeleteStockLists();
513 delete wxTheColourDatabase
;
514 wxTheColourDatabase
= NULL
;
516 wxBitmap::CleanUpHandlers();
521 //// WINDOWS-SPECIFIC CLEANUP
523 wxSetKeyboardHook(FALSE
);
528 if (gs_hRichEdit
!= (HINSTANCE
) NULL
)
529 FreeLibrary(gs_hRichEdit
);
538 if (wxSTD_FRAME_ICON
)
539 DestroyIcon(wxSTD_FRAME_ICON
);
540 if (wxSTD_MDICHILDFRAME_ICON
)
541 DestroyIcon(wxSTD_MDICHILDFRAME_ICON
);
542 if (wxSTD_MDIPARENTFRAME_ICON
)
543 DestroyIcon(wxSTD_MDIPARENTFRAME_ICON
);
545 if (wxDEFAULT_FRAME_ICON
)
546 DestroyIcon(wxDEFAULT_FRAME_ICON
);
547 if (wxDEFAULT_MDICHILDFRAME_ICON
)
548 DestroyIcon(wxDEFAULT_MDICHILDFRAME_ICON
);
549 if (wxDEFAULT_MDIPARENTFRAME_ICON
)
550 DestroyIcon(wxDEFAULT_MDIPARENTFRAME_ICON
);
552 if ( wxDisableButtonBrush
)
553 ::DeleteObject( wxDisableButtonBrush
);
560 Ctl3dUnregister(wxhInstance
);
564 delete wxWinHandleList
;
566 // GL: I'm annoyed ... I don't know where to put this and I don't want to
567 // create a module for that as it's part of the core.
569 delete wxPendingEvents
;
570 delete wxPendingEventsLocker
;
571 // If we don't do the following, we get an apparent memory leak.
572 ((wxEvtHandler
&) wxDefaultValidator
).ClearEventLocker();
575 wxClassInfo::CleanUpClasses();
580 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
581 // At this point we want to check if there are any memory
582 // blocks that aren't part of the wxDebugContext itself,
583 // as a special case. Then when dumping we need to ignore
584 // wxDebugContext, too.
585 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
587 wxLogDebug(wxT("There were memory leaks."));
588 wxDebugContext::Dump();
589 wxDebugContext::PrintStatistics();
591 // wxDebugContext::SetStream(NULL, NULL);
595 // do it as the very last thing because everything else can log messages
596 delete wxLog::SetActiveTarget(NULL
);
600 #if !defined(_WINDLL) || (defined(_WINDLL) && defined(WXMAKINGDLL))
602 // temporarily disable this warning which would be generated in release builds
605 #pragma warning(disable: 4715) // not all control paths return a value
608 //// Main wxWindows entry point
609 int wxEntry(WXHINSTANCE hInstance
,
610 WXHINSTANCE
WXUNUSED(hPrevInstance
),
615 // do check for memory leaks on program exit
616 // (another useful flag is _CRTDBG_DELAY_FREE_MEM_DF which doesn't free
617 // deallocated memory which may be used to simulate low-memory condition)
618 wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF
);
620 // take everything into a try-except block in release build
621 // FIXME other compilers must support Win32 SEH (structured exception
622 // handling) too, just find the appropriate keyword in their docs!
623 // Please note that it's _not_ the same as C++ exceptions!
624 #if !defined(__WXDEBUG__) && defined(__VISUALC__)
625 #define CATCH_PROGRAM_EXCEPTIONS
629 #undef CATCH_PROGRAM_EXCEPTIONS
632 wxhInstance
= (HINSTANCE
) hInstance
;
634 if (!wxApp::Initialize())
637 // create the application object or ensure that one already exists
640 // The app may have declared a global application object, but we recommend
641 // the IMPLEMENT_APP macro is used instead, which sets an initializer
642 // function for delayed, dynamic app object construction.
643 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
644 wxT("No initializer - use IMPLEMENT_APP macro.") );
646 wxTheApp
= (*wxApp::GetInitializerFunction()) ();
649 wxCHECK_MSG( wxTheApp
, 0, wxT("You have to define an instance of wxApp!") );
651 // save the WinMain() parameters
652 wxTheApp
->ConvertToStandardCommandArgs(lpCmdLine
);
653 wxTheApp
->m_nCmdShow
= nCmdShow
;
655 // GUI-specific initialisation. In fact on Windows we don't have any,
656 // but this call is provided for compatibility across platforms.
657 wxTheApp
->OnInitGui();
661 if ( wxTheApp
->OnInit() )
665 retValue
= wxTheApp
->OnRun();
668 // We want to initialize, but not run or exit immediately.
671 //else: app initialization failed, so we skipped OnRun()
673 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
676 // Forcibly delete the window.
677 if ( topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
678 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
680 topWindow
->Close(TRUE
);
681 wxTheApp
->DeletePendingObjects();
686 wxTheApp
->SetTopWindow(NULL
);
696 #ifdef CATCH_PROGRAM_EXCEPTIONS
698 __except ( EXCEPTION_EXECUTE_HANDLER
) {
701 wxTheApp->OnFatalException();
704 // using wxLog would be unsafe here
706 _("Unrecoverable program error detected: "
707 " the application will terminate."),
709 MB_APPLMODAL
| MB_ICONSTOP
| MB_OK
);
711 ::ExitProcess(3); // the same exit code as abort()
715 #endif // CATCH_PROGRAM_EXCEPTIONS
718 // restore warning state
720 #pragma warning(default: 4715) // not all control paths return a value
725 //// Entry point for DLLs
727 int wxEntry(WXHINSTANCE hInstance
)
729 wxhInstance
= (HINSTANCE
) hInstance
;
732 // The app may have declared a global application object, but we recommend
733 // the IMPLEMENT_APP macro is used instead, which sets an initializer function
734 // for delayed, dynamic app object construction.
737 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
738 "No initializer - use IMPLEMENT_APP macro." );
740 wxTheApp
= (* wxApp::GetInitializerFunction()) ();
743 wxCHECK_MSG( wxTheApp
, 0, "You have to define an instance of wxApp!" );
746 wxTheApp
->argv
= NULL
;
748 wxTheApp
->OnInitGui();
752 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
753 if ( topWindow
&& topWindow
->GetHWND())
755 topWindow
->Show(TRUE
);
762 //// Static member initialization
764 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
770 m_wantDebugOutput
= TRUE
;
774 m_printMode
= wxPRINT_WINDOWS
;
775 m_exitOnFrameDelete
= TRUE
;
781 // Delete command-line args
783 for (i
= 0; i
< argc
; i
++)
790 bool wxApp::Initialized()
798 #ifdef _WINDLL // Assume initialized if DLL (no way of telling)
804 * Get and process a message, returning FALSE if WM_QUIT
805 * received (and also set the flag telling the app to exit the main loop)
808 bool wxApp::DoMessage()
810 BOOL rc
= ::GetMessage(&s_currentMsg
, (HWND
) NULL
, 0, 0);
820 // should never happen, but let's test for it nevertheless
821 wxLogLastError("GetMessage");
826 wxASSERT_MSG( wxThread::IsMain(),
827 wxT("only the main thread can process Windows messages") );
829 static bool s_hadGuiLock
= TRUE
;
830 static wxMsgArray s_aSavedMessages
;
832 // if a secondary thread owns is doing GUI calls, save all messages for
833 // later processing - we can't process them right now because it will
834 // lead to recursive library calls (and we're not reentrant)
835 if ( !wxGuiOwnedByMainThread() )
837 s_hadGuiLock
= FALSE
;
839 // leave out WM_COMMAND messages: too dangerous, sometimes
840 // the message will be processed twice
841 if ( !wxIsWaitingForThread() ||
842 s_currentMsg
.message
!= WM_COMMAND
)
844 s_aSavedMessages
.Add(s_currentMsg
);
851 // have we just regained the GUI lock? if so, post all of the saved
854 // FIXME of course, it's not _exactly_ the same as processing the
855 // messages normally - expect some things to break...
860 size_t count
= s_aSavedMessages
.Count();
861 for ( size_t n
= 0; n
< count
; n
++ )
863 MSG
& msg
= s_aSavedMessages
[n
];
865 if ( !ProcessMessage((WXMSG
*)&msg
) )
867 ::TranslateMessage(&msg
);
868 ::DispatchMessage(&msg
);
872 s_aSavedMessages
.Empty();
875 #endif // wxUSE_THREADS
877 // Process the message
878 if ( !ProcessMessage((WXMSG
*)&s_currentMsg
) )
880 ::TranslateMessage(&s_currentMsg
);
881 ::DispatchMessage(&s_currentMsg
);
889 * Keep trying to process messages until WM_QUIT
892 * If there are messages to be processed, they will all be
893 * processed and OnIdle will not be called.
894 * When there are no more messages, OnIdle is called.
895 * If OnIdle requests more time,
896 * it will be repeatedly called so long as there are no pending messages.
897 * A 'feature' of this is that once OnIdle has decided that no more processing
898 * is required, then it won't get processing time until further messages
899 * are processed (it'll sit in DoMessage).
902 int wxApp::MainLoop()
906 while ( m_keepGoing
)
909 wxMutexGuiLeaveOrEnter();
910 #endif // wxUSE_THREADS
912 while ( !::PeekMessage(&s_currentMsg
, 0, 0, 0, PM_NOREMOVE
) &&
921 return s_currentMsg
.wParam
;
924 // Returns TRUE if more time is needed.
925 bool wxApp::ProcessIdle()
928 event
.SetEventObject(this);
931 return event
.MoreRequested();
935 void wxApp::ProcessPendingEvents()
937 wxNode
*node
= wxPendingEvents
->First();
938 wxCriticalSectionLocker
locker(*wxPendingEventsLocker
);
942 wxEvtHandler
*handler
= (wxEvtHandler
*)node
->Data();
944 handler
->ProcessPendingEvents();
947 node
= wxPendingEvents
->First();
953 void wxApp::ExitMainLoop()
958 bool wxApp::Pending()
960 return (::PeekMessage(&s_currentMsg
, 0, 0, 0, PM_NOREMOVE
) != 0);
963 void wxApp::Dispatch()
969 * Give all windows a chance to preprocess
970 * the message. Some may have accelerator tables, or have
974 bool wxApp::ProcessMessage(WXMSG
*wxmsg
)
976 MSG
*msg
= (MSG
*)wxmsg
;
977 HWND hWnd
= msg
->hwnd
;
978 wxWindow
*wndThis
= wxFindWinFromHandle((WXHWND
)hWnd
), *wnd
;
980 // for some composite controls (like a combobox), wndThis might be NULL
981 // because the subcontrol is not a wxWindow, but only the control itself
982 // is - try to catch this case
983 while ( hWnd
&& !wndThis
)
985 hWnd
= ::GetParent(hWnd
);
986 wndThis
= wxFindWinFromHandle((WXHWND
)hWnd
);
989 // Try translations first; find the youngest window with
990 // a translation table.
991 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
993 if ( wnd
->MSWTranslateMessage(wxmsg
) )
997 // Anyone for a non-translation message? Try youngest descendants first.
998 for ( wnd
= wndThis
; wnd
; wnd
= wnd
->GetParent() )
1000 if ( wnd
->MSWProcessMessage(wxmsg
) )
1007 void wxApp::OnIdle(wxIdleEvent
& event
)
1009 static bool s_inOnIdle
= FALSE
;
1011 // Avoid recursion (via ProcessEvent default case)
1017 // 'Garbage' collection of windows deleted with Close().
1018 DeletePendingObjects();
1021 // flush the logged messages if any
1022 wxLog
*pLog
= wxLog::GetActiveTarget();
1023 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
1027 // Send OnIdle events to all windows
1028 if ( SendIdleEvents() )
1030 // SendIdleEvents() returns TRUE if at least one window requested more
1032 event
.RequestMore(TRUE
);
1035 // If they are pending events, we must process them.
1037 ProcessPendingEvents();
1042 // Send idle event to all top-level windows
1043 bool wxApp::SendIdleEvents()
1045 bool needMore
= FALSE
;
1047 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
1050 wxWindow
* win
= node
->GetData();
1051 if (SendIdleEvents(win
))
1053 node
= node
->GetNext();
1059 // Send idle event to window and all subwindows
1060 bool wxApp::SendIdleEvents(wxWindow
* win
)
1062 bool needMore
= FALSE
;
1065 event
.SetEventObject(win
);
1066 win
->GetEventHandler()->ProcessEvent(event
);
1068 if (event
.MoreRequested())
1071 wxNode
* node
= win
->GetChildren().First();
1074 wxWindow
* win
= (wxWindow
*) node
->Data();
1075 if (SendIdleEvents(win
))
1078 node
= node
->Next();
1083 void wxApp::DeletePendingObjects()
1085 wxNode
*node
= wxPendingDelete
.First();
1088 wxObject
*obj
= (wxObject
*)node
->Data();
1092 if (wxPendingDelete
.Member(obj
))
1095 // Deleting one object may have deleted other pending
1096 // objects, so start from beginning of list again.
1097 node
= wxPendingDelete
.First();
1101 void wxApp::OnEndSession(wxCloseEvent
& WXUNUSED(event
))
1104 GetTopWindow()->Close(TRUE
);
1107 // Default behaviour: close the application with prompts. The
1108 // user can veto the close, and therefore the end session.
1109 void wxApp::OnQueryEndSession(wxCloseEvent
& event
)
1113 if (!GetTopWindow()->Close(!event
.CanVeto()))
1118 int wxApp::GetComCtl32Version() const
1120 // have we loaded COMCTL32 yet?
1121 HMODULE theModule
= ::GetModuleHandle(wxT("COMCTL32"));
1124 // if so, then we can check for the version
1127 // InitCommonControlsEx is unique to 4.7 and later
1128 FARPROC theProc
= ::GetProcAddress(theModule
, "InitCommonControlsEx");
1131 { // not found, must be 4.00
1136 // The following symbol are unique to 4.71
1138 // FlatSB_EnableScrollBar FlatSB_GetScrollInfo FlatSB_GetScrollPos
1139 // FlatSB_GetScrollProp FlatSB_GetScrollRange FlatSB_SetScrollInfo
1140 // FlatSB_SetScrollPos FlatSB_SetScrollProp FlatSB_SetScrollRange
1141 // FlatSB_ShowScrollBar
1142 // _DrawIndirectImageList _DuplicateImageList
1144 // UninitializeFlatSB
1145 // we could check for any of these - I chose DllInstall
1146 FARPROC theProc
= ::GetProcAddress(theModule
, "DllInstall");
1149 // not found, must be 4.70
1153 { // found, must be 4.71
1163 wxLogError(_("Fatal error: exiting"));
1168 // Yield to incoming messages
1172 // We want to go back to the main message loop
1173 // if we see a WM_QUIT. (?)
1175 while (PeekMessage(&msg
, (HWND
)NULL
, 0, 0, PM_NOREMOVE
) && msg
.message
!= WM_QUIT
)
1177 while (PeekMessage(&msg
, NULL
, 0, 0, PM_NOREMOVE
) && msg
.message
!= WM_QUIT
)
1180 if ( !wxTheApp
->DoMessage() )
1183 // If they are pending events, we must process them.
1185 wxTheApp
->ProcessPendingEvents();
1192 wxApp::GetStdIcon(int which
) const
1196 case wxICON_INFORMATION
:
1197 return wxIcon("wxICON_INFO");
1199 case wxICON_QUESTION
:
1200 return wxIcon("wxICON_QUESTION");
1202 case wxICON_EXCLAMATION
:
1203 return wxIcon("wxICON_WARNING");
1206 wxFAIL_MSG(wxT("requested non existent standard icon"));
1207 // still fall through
1210 return wxIcon("wxICON_ERROR");
1215 HINSTANCE
wxGetInstance()
1220 void wxSetInstance(HINSTANCE hInst
)
1222 wxhInstance
= hInst
;
1225 // For some reason, with MSVC++ 1.5, WinMain isn't linked in properly
1226 // if in a separate file. So include it here to ensure it's linked.
1227 #if (defined(__VISUALC__) && !defined(__WIN32__)) || (defined(__GNUWIN32__) && !defined(__TWIN32__))