X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/50165591a5f7de9566fc3bb4fe1ec7e4673d13be..4b1ae1539c549eb612e5d2b6876124138ce9cb4e:/src/msw/app.cpp diff --git a/src/msw/app.cpp b/src/msw/app.cpp index 7af9c7108a..b7ea221656 100644 --- a/src/msw/app.cpp +++ b/src/msw/app.cpp @@ -55,17 +55,6 @@ #include "wx/msw/private.h" -#if wxUSE_THREADS - #include "wx/thread.h" - - // define the array of MSG strutures - WX_DECLARE_OBJARRAY(MSG, wxMsgArray); - - #include "wx/arrimpl.cpp" - - WX_DEFINE_OBJARRAY(wxMsgArray); -#endif // wxUSE_THREADS - #if wxUSE_TOOLTIPS #include "wx/tooltip.h" #endif // wxUSE_TOOLTIPS @@ -87,9 +76,14 @@ #include "wx/msw/wrapcctl.h" +// For MB_TASKMODAL +#ifdef __WXWINCE__ +#include "wx/msw/wince/missing.h" +#endif + #if (!defined(__MINGW32__) || wxCHECK_W32API_VERSION( 2, 0 )) && \ !defined(__CYGWIN__) && !defined(__DIGITALMARS__) && !defined(__WXWINCE__) && \ - (!defined(_MSC_VER) || (_MSC_VER > 1100)) + (!defined(_MSC_VER) || (_MSC_VER > 1100)) #include #endif @@ -103,12 +97,15 @@ extern wxList WXDLLEXPORT wxPendingDelete; extern void wxSetKeyboardHook(bool doIt); #endif -MSG s_currentMsg; - // NB: all "NoRedraw" classes must have the same names as the "normal" classes // with NR suffix - wxWindow::MSWCreate() supposes this +#ifdef __WXWINCE__ + wxChar *wxCanvasClassName; + wxChar *wxCanvasClassNameNR; +#else const wxChar *wxCanvasClassName = wxT("wxWindowClass"); const wxChar *wxCanvasClassNameNR = wxT("wxWindowClassNR"); +#endif const wxChar *wxMDIFrameClassName = wxT("wxMDIFrameClass"); const wxChar *wxMDIFrameClassNameNoRedraw = wxT("wxMDIFrameClassNR"); const wxChar *wxMDIChildFrameClassName = wxT("wxMDIChildFrameClass"); @@ -200,12 +197,14 @@ void wxGUIAppTraits::AfterChildWaitLoop(void *dataOrig) bool wxGUIAppTraits::DoMessageFromThreadWait() { - return !wxTheApp || wxTheApp->DoMessage(); + // we should return false only if the app should exit, i.e. only if + // Dispatch() determines that the main event loop should terminate + return !wxTheApp || wxTheApp->Dispatch(); } wxToolkitInfo& wxGUIAppTraits::GetToolkitInfo() { - static wxToolkitInfo info; + static wxToolkitInfo info; wxToolkitInfo& baseInfo = wxAppTraits::GetToolkitInfo(); info.versionMajor = baseInfo.versionMajor; info.versionMinor = baseInfo.versionMinor; @@ -260,6 +259,20 @@ bool wxApp::Initialize(int& argc, wxChar **argv) // ensure that base cleanup is done if we return too early wxCallBaseCleanup callBaseCleanup(this); +#ifdef __WXWINCE__ + wxString tmp = GetAppName(); + tmp += wxT("ClassName"); + wxCanvasClassName = wxStrdup( tmp.c_str() ); + tmp += wxT("NR"); + wxCanvasClassNameNR = wxStrdup( tmp.c_str() ); + HWND hWnd = FindWindow( wxCanvasClassNameNR, NULL ); + if (hWnd) + { + SetForegroundWindow( (HWND)(((DWORD)hWnd)|0x01) ); + return false; + } +#endif + // the first thing to do is to check if we're trying to run an Unicode // program under Win9x w/o MSLU emulation layer - if so, abort right now // as it has no chance to work @@ -273,7 +286,7 @@ bool wxApp::Initialize(int& argc, wxChar **argv) ( NULL, _T("This program uses Unicode and requires Windows NT/2000/XP/CE.\nProgram aborted."), - _T("wxWindows Fatal Error"), + _T("wxWidgets Fatal Error"), MB_ICONERROR | MB_OK ); @@ -287,13 +300,6 @@ bool wxApp::Initialize(int& argc, wxChar **argv) #if wxUSE_OLE || wxUSE_DRAG_AND_DROP -#ifdef __WIN16__ - // for OLE, enlarge message queue to be as large as possible - int iMsg = 96; - while (!SetMessageQueue(iMsg) && (iMsg -= 8)) - ; -#endif // Win16 - #if wxUSE_OLE // we need to initialize OLE library #ifdef __WXWINCE__ @@ -328,7 +334,7 @@ bool wxApp::Initialize(int& argc, wxChar **argv) wxDisableButtonBrush = ::CreateBrushIndirect( & lb ); ::DeleteObject( (HGDIOBJ)lb.lbHatch ); } - //else: wxWindows resources are probably not linked in + //else: wxWidgets resources are probably not linked in #endif #if wxUSE_PENWINDOWS @@ -337,13 +343,6 @@ bool wxApp::Initialize(int& argc, wxChar **argv) wxWinHandleHash = new wxWinHashTable(wxKEY_INTEGER, 100); - // This is to foil optimizations in Visual C++ that throw out dummy.obj. - // PLEASE DO NOT ALTER THIS. -#if defined(__VISUALC__) && defined(__WIN16__) && !defined(WXMAKINGDLL) - extern char wxDummyChar; - if (wxDummyChar) wxDummyChar++; -#endif - #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__) wxSetKeyboardHook(TRUE); #endif @@ -497,6 +496,12 @@ bool wxApp::UnregisterWindowClasses() void wxApp::CleanUp() { + // all objects pending for deletion must be deleted first, otherwise we + // would crash when they use wxWinHandleHash (and UnregisterWindowClasses() + // call wouldn't succeed as long as any windows still exist), so call the + // base class method first and only then do our clean up + wxAppBase::CleanUp(); + #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__) wxSetKeyboardHook(FALSE); #endif @@ -528,8 +533,11 @@ void wxApp::CleanUp() delete wxWinHandleHash; wxWinHandleHash = NULL; - - wxAppBase::CleanUp(); + +#ifdef __WXWINCE__ + free( wxCanvasClassName ); + free( wxCanvasClassNameNR ); +#endif } // ---------------------------------------------------------------------------- @@ -556,247 +564,12 @@ wxApp::~wxApp() delete [] argv; } -bool wxApp::Initialized() -{ -#ifndef _WINDLL - if (GetTopWindow()) - return TRUE; - else - return FALSE; -#else // Assume initialized if DLL (no way of telling) - return TRUE; -#endif -} - -/* - * Get and process a message, returning FALSE if WM_QUIT - * received (and also set the flag telling the app to exit the main loop) - * - */ -bool wxApp::DoMessage() -{ - BOOL rc = ::GetMessage(&s_currentMsg, (HWND) NULL, 0, 0); - if ( rc == 0 ) - { - // got WM_QUIT - m_keepGoing = FALSE; - - return FALSE; - } - else if ( rc == -1 ) - { - // should never happen, but let's test for it nevertheless - wxLogLastError(wxT("GetMessage")); - } - else - { -#if wxUSE_THREADS - wxASSERT_MSG( wxThread::IsMain(), - wxT("only the main thread can process Windows messages") ); - - static bool s_hadGuiLock = TRUE; - static wxMsgArray s_aSavedMessages; - - // if a secondary thread owns is doing GUI calls, save all messages for - // later processing - we can't process them right now because it will - // lead to recursive library calls (and we're not reentrant) - if ( !wxGuiOwnedByMainThread() ) - { - s_hadGuiLock = FALSE; - - // leave out WM_COMMAND messages: too dangerous, sometimes - // the message will be processed twice - if ( !wxIsWaitingForThread() || - s_currentMsg.message != WM_COMMAND ) - { - s_aSavedMessages.Add(s_currentMsg); - } - - return TRUE; - } - else - { - // have we just regained the GUI lock? if so, post all of the saved - // messages - // - // FIXME of course, it's not _exactly_ the same as processing the - // messages normally - expect some things to break... - if ( !s_hadGuiLock ) - { - s_hadGuiLock = TRUE; - - size_t count = s_aSavedMessages.GetCount(); - for ( size_t n = 0; n < count; n++ ) - { - MSG& msg = s_aSavedMessages[n]; - - DoMessage((WXMSG *)&msg); - } - - s_aSavedMessages.Empty(); - } - } -#endif // wxUSE_THREADS - - // Process the message - DoMessage((WXMSG *)&s_currentMsg); - } - - return TRUE; -} - -void wxApp::DoMessage(WXMSG *pMsg) -{ - if ( !ProcessMessage(pMsg) ) - { - ::TranslateMessage((MSG *)pMsg); - ::DispatchMessage((MSG *)pMsg); - } -} - -/* - * Keep trying to process messages until WM_QUIT - * received. - * - * If there are messages to be processed, they will all be - * processed and OnIdle will not be called. - * When there are no more messages, OnIdle is called. - * If OnIdle requests more time, - * it will be repeatedly called so long as there are no pending messages. - * A 'feature' of this is that once OnIdle has decided that no more processing - * is required, then it won't get processing time until further messages - * are processed (it'll sit in DoMessage). - */ - -int wxApp::MainLoop() -{ - m_keepGoing = TRUE; - - while ( m_keepGoing ) - { -#if wxUSE_THREADS - wxMutexGuiLeaveOrEnter(); -#endif // wxUSE_THREADS - - while ( !Pending() && ProcessIdle() ) - ; - - // a message came or no more idle processing to do - DoMessage(); - } - - return s_currentMsg.wParam; -} - -void wxApp::ExitMainLoop() -{ - // this will set m_keepGoing to FALSE a bit later - ::PostQuitMessage(0); -} - -bool wxApp::Pending() -{ - return ::PeekMessage(&s_currentMsg, 0, 0, 0, PM_NOREMOVE) != 0; -} - -void wxApp::Dispatch() -{ - DoMessage(); -} - -/* - * Give all windows a chance to preprocess - * the message. Some may have accelerator tables, or have - * MDI complications. - */ - -bool wxApp::ProcessMessage(WXMSG *wxmsg) -{ - MSG *msg = (MSG *)wxmsg; - HWND hwnd = msg->hwnd; - wxWindow *wndThis = wxGetWindowFromHWND((WXHWND)hwnd); - - // this may happen if the event occured in a standard modeless dialog (the - // only example of which I know of is the find/replace dialog) - then call - // IsDialogMessage() to make TAB navigation in it work - if ( !wndThis ) - { - // we need to find the dialog containing this control as - // IsDialogMessage() just eats all the messages (i.e. returns TRUE for - // them) if we call it for the control itself - while ( hwnd && ::GetWindowLong(hwnd, GWL_STYLE) & WS_CHILD ) - { - hwnd = ::GetParent(hwnd); - } - - return hwnd && ::IsDialogMessage(hwnd, msg) != 0; - } - -#if wxUSE_TOOLTIPS - // we must relay WM_MOUSEMOVE events to the tooltip ctrl if we want it to - // popup the tooltip bubbles - if ( (msg->message == WM_MOUSEMOVE) ) - { - wxToolTip *tt = wndThis->GetToolTip(); - if ( tt ) - { - tt->RelayEvent(wxmsg); - } - } -#endif // wxUSE_TOOLTIPS - - // allow the window to prevent certain messages from being - // translated/processed (this is currently used by wxTextCtrl to always - // grab Ctrl-C/V/X, even if they are also accelerators in some parent) - if ( !wndThis->MSWShouldPreProcessMessage(wxmsg) ) - { - return FALSE; - } - - // try translations first: the accelerators override everything - wxWindow *wnd; - - for ( wnd = wndThis; wnd; wnd = wnd->GetParent() ) - { - if ( wnd->MSWTranslateMessage(wxmsg)) - return TRUE; - - // stop at first top level window, i.e. don't try to process the key - // strokes originating in a dialog using the accelerators of the parent - // frame - this doesn't make much sense - if ( wnd->IsTopLevel() ) - break; - } - - // now try the other hooks (kbd navigation is handled here): we start from - // wndThis->GetParent() because wndThis->MSWProcessMessage() was already - // called above - for ( wnd = wndThis->GetParent(); wnd; wnd = wnd->GetParent() ) - { - if ( wnd->MSWProcessMessage(wxmsg) ) - return TRUE; - } - - // no special preprocessing for this message, dispatch it normally - return FALSE; -} - -// this is a temporary hack and will be replaced by using wxEventLoop in the -// future -// -// it is needed to allow other event loops (currently only one: the modal -// dialog one) to reset the OnIdle() semaphore because otherwise OnIdle() -// wouldn't do anything while a modal dialog shown from OnIdle() call is shown. -bool wxIsInOnIdleFlag = FALSE; +// ---------------------------------------------------------------------------- +// wxApp idle handling +// ---------------------------------------------------------------------------- void wxApp::OnIdle(wxIdleEvent& event) { - // Avoid recursion (via ProcessEvent default case) - if ( wxIsInOnIdleFlag ) - return; - - wxIsInOnIdleFlag = TRUE; - wxAppBase::OnIdle(event); #if wxUSE_DC_CACHEING @@ -806,8 +579,6 @@ void wxApp::OnIdle(wxIdleEvent& event) if (!::GetKeyState(MK_LBUTTON) && !::GetKeyState(MK_MBUTTON) && !::GetKeyState(MK_RBUTTON)) wxDC::ClearCache(); #endif // wxUSE_DC_CACHEING - - wxIsInOnIdleFlag = FALSE; } void wxApp::WakeUpIdle() @@ -827,6 +598,10 @@ void wxApp::WakeUpIdle() } } +// ---------------------------------------------------------------------------- +// other wxApp event hanlders +// ---------------------------------------------------------------------------- + void wxApp::OnEndSession(wxCloseEvent& WXUNUSED(event)) { if (GetTopWindow()) @@ -844,6 +619,10 @@ void wxApp::OnQueryEndSession(wxCloseEvent& event) } } +// ---------------------------------------------------------------------------- +// miscellaneous +// ---------------------------------------------------------------------------- + /* static */ int wxApp::GetComCtl32Version() { @@ -871,6 +650,7 @@ int wxApp::GetComCtl32Version() // if so, then we can check for the version if ( dllComCtl32.IsLoaded() ) { +#ifdef DLLVER_PLATFORM_WINDOWS // try to use DllGetVersion() if available in _headers_ wxDYNLIB_FUNCTION( DLLGETVERSIONPROC, DllGetVersion, dllComCtl32 ); if ( pfnDllGetVersion ) @@ -892,6 +672,7 @@ int wxApp::GetComCtl32Version() dvi.dwMinorVersion; } } +#endif // if DllGetVersion() is unavailable either during compile or // run-time, try to guess the version otherwise @@ -963,7 +744,7 @@ bool wxApp::Yield(bool onlyIfNeeded) wxMutexGuiLeaveOrEnter(); #endif // wxUSE_THREADS - if ( !wxTheApp->DoMessage() ) + if ( !wxTheApp->Dispatch() ) break; } @@ -980,3 +761,43 @@ bool wxApp::Yield(bool onlyIfNeeded) return TRUE; } +#if wxUSE_EXCEPTIONS + +// ---------------------------------------------------------------------------- +// exception handling +// ---------------------------------------------------------------------------- + +bool wxApp::OnExceptionInMainLoop() +{ + // ask the user about what to do: use the Win32 API function here as it + // could be dangerous to use any wxWidgets code in this state + switch ( + ::MessageBox + ( + NULL, + _T("An unhandled exception occurred. Press \"Abort\" to \ +terminate the program,\r\n\ +\"Retry\" to exit the program normally and \"Ignore\" to try to continue."), + _T("Unhandled exception"), + MB_ABORTRETRYIGNORE | + MB_ICONERROR| + MB_TASKMODAL + ) + ) + { + case IDABORT: + throw; + + default: + wxFAIL_MSG( _T("unexpected MessageBox() return code") ); + // fall through + + case IDRETRY: + return false; + + case IDIGNORE: + return true; + } +} + +#endif // wxUSE_EXCEPTIONS