X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/365b9f162ac22611baed1409cf249f55d6a91e93..be10c7f969bb460dc79bd8946d59bf54c98ac660:/src/msw/app.cpp?ds=inline diff --git a/src/msw/app.cpp b/src/msw/app.cpp index 196effa180..cadcdaaead 100644 --- a/src/msw/app.cpp +++ b/src/msw/app.cpp @@ -4,7 +4,6 @@ // Author: Julian Smart // Modified by: // Created: 04/01/98 -// RCS-ID: $Id$ // Copyright: (c) Julian Smart // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// @@ -121,13 +120,15 @@ extern void wxSetKeyboardHook(bool doIt); // see http://article.gmane.org/gmane.comp.lib.wxwidgets.devel/110282 struct ClassRegInfo { - // the base name of the class: this is used to construct the unique name in - // RegisterClassWithUniqueNames() - wxString basename; + ClassRegInfo(const wxChar *name) + : regname(name), + regnameNR(regname + wxApp::GetNoRedrawClassSuffix()) + { + } // the name of the registered class with and without CS_[HV]REDRAW styles - wxString regname, - regnameNR; + wxString regname; + wxString regnameNR; }; namespace @@ -170,12 +171,12 @@ void *wxGUIAppTraits::BeforeChildWaitLoop() focus/activation entirely when the child process terminates which would happen if we simply disabled everything using wxWindowDisabler. Indeed, remember that Windows will never activate a disabled window and when the - last childs window is closed and Windows looks for a window to activate + last child's window is closed and Windows looks for a window to activate all our windows are still disabled. There is no way to enable them in - time because we don't know when the childs windows are going to be - closed, so the solution we use here is to keep one special tiny frame + time because we don't know when the child's windows are going to be + closed, so the solution we use here is to keep one special tiny dialog enabled all the time. Then when the child terminates it will get - activated and when we close it below -- after reenabling all the other + activated and when we close it below -- after re-enabling all the other windows! -- the previously active window becomes activated again and everything is ok. */ @@ -184,27 +185,22 @@ void *wxGUIAppTraits::BeforeChildWaitLoop() // first disable all existing windows wxWindowDisabler *wd = new wxWindowDisabler; - // then create an "invisible" frame: it has minimal size, is positioned - // (hopefully) outside the screen and doesn't appear on the taskbar - wxWindow *winActive = new wxFrame + // then create an "invisible" dialog: it has minimal size, is positioned + // (hopefully) outside the screen and doesn't appear in the Alt-TAB list + // (unlike the frames, which is why we use a dialog here) + wxWindow *winActive = new wxDialog ( wxTheApp->GetTopWindow(), wxID_ANY, wxEmptyString, wxPoint(32600, 32600), - wxSize(1, 1), - wxDEFAULT_FRAME_STYLE | wxFRAME_NO_TASKBAR + wxSize(1, 1) ); winActive->Show(); return new ChildWaitLoopData(wd, winActive); } -void wxGUIAppTraits::AlwaysYield() -{ - wxYield(); -} - void wxGUIAppTraits::AfterChildWaitLoop(void *dataOrig) { wxEndBusyCursor(); @@ -213,15 +209,16 @@ void wxGUIAppTraits::AfterChildWaitLoop(void *dataOrig) delete data->wd; - // finally delete the dummy frame and, as wd has been already destroyed and - // the other windows reenabled, the activation is going to return to the - // window which had had it before + // finally delete the dummy dialog and, as wd has been already destroyed + // and the other windows reenabled, the activation is going to return to + // the window which had had it before data->winActive->Destroy(); // also delete the temporary data object itself delete data; } +#if wxUSE_THREADS bool wxGUIAppTraits::DoMessageFromThreadWait() { // we should return false only if the app should exit, i.e. only if @@ -236,14 +233,20 @@ bool wxGUIAppTraits::DoMessageFromThreadWait() return evtLoop->Dispatch(); } -DWORD wxGUIAppTraits::WaitForThread(WXHANDLE hThread) +DWORD wxGUIAppTraits::WaitForThread(WXHANDLE hThread, int flags) { - // if we don't have a running event loop, we shouldn't wait for the - // messages as we never remove them from the message queue and so we enter - // an infinite loop as MsgWaitForMultipleObjects() keeps returning - // WAIT_OBJECT_0 + 1 - if ( !wxEventLoop::GetActive() ) + // We only ever dispatch messages from the main thread and, additionally, + // even from the main thread we shouldn't wait for the message if we don't + // have a running event loop as we would never remove them from the message + // queue then and so we would enter an infinite loop as + // MsgWaitForMultipleObjects() keeps returning WAIT_OBJECT_0 + 1. + if ( flags == wxTHREAD_WAIT_BLOCK || + !wxIsMainThread() || + !wxEventLoop::GetActive() ) + { + // Simple blocking wait. return DoSimpleWaitForThread(hThread); + } return ::MsgWaitForMultipleObjects ( @@ -255,6 +258,7 @@ DWORD wxGUIAppTraits::WaitForThread(WXHANDLE hThread) QS_ALLPOSTMESSAGE ); } +#endif // wxUSE_THREADS wxPortId wxGUIAppTraits::GetToolkitVersion(int *majVer, int *minVer) const { @@ -299,6 +303,8 @@ wxEventLoopBase* wxGUIAppTraits::CreateEventLoop() #ifndef __WXWINCE__ +#if wxUSE_DYNLIB_CLASS + #include namespace @@ -337,7 +343,7 @@ public: { if ( !::FreeConsole() ) { - wxLogLastError(_T("FreeConsole")); + wxLogLastError(wxT("FreeConsole")); } } } @@ -396,7 +402,7 @@ private: GetConsoleCommandHistory_t m_pfnGetConsoleCommandHistory; GetConsoleCommandHistoryLength_t m_pfnGetConsoleCommandHistoryLength; - DECLARE_NO_COPY_CLASS(wxConsoleStderr) + wxDECLARE_NO_COPY_CLASS(wxConsoleStderr); }; bool wxConsoleStderr::DoInit() @@ -406,7 +412,7 @@ bool wxConsoleStderr::DoInit() if ( hStderr == INVALID_HANDLE_VALUE || !hStderr ) return false; - if ( !m_dllKernel32.Load(_T("kernel32.dll")) ) + if ( !m_dllKernel32.Load(wxT("kernel32.dll")) ) return false; typedef BOOL (WINAPI *AttachConsole_t)(DWORD dwProcessId); @@ -439,7 +445,7 @@ bool wxConsoleStderr::DoInit() if ( !::GetConsoleScreenBufferInfo(m_hStderr, &csbi) ) { - wxLogLastError(_T("GetConsoleScreenBufferInfo")); + wxLogLastError(wxT("GetConsoleScreenBufferInfo")); return false; } @@ -456,7 +462,7 @@ bool wxConsoleStderr::DoInit() if ( !::ReadConsoleOutputCharacterA(m_hStderr, buf, WXSIZEOF(buf), pos, &ret) ) { - wxLogLastError(_T("ReadConsoleOutputCharacterA")); + wxLogLastError(wxT("ReadConsoleOutputCharacterA")); return false; } } while ( wxStrncmp(" ", buf, WXSIZEOF(buf)) != 0 ); @@ -471,7 +477,7 @@ bool wxConsoleStderr::DoInit() if ( !::ReadConsoleOutputCharacterA(m_hStderr, m_data.data(), m_dataLen, pos, &ret) ) { - wxLogLastError(_T("ReadConsoleOutputCharacterA")); + wxLogLastError(wxT("ReadConsoleOutputCharacterA")); return false; } } @@ -482,7 +488,7 @@ bool wxConsoleStderr::DoInit() int wxConsoleStderr::GetCommandHistory(wxWxCharBuffer& buf) const { // these functions are internal and may only be called by cmd.exe - static const wxChar *CMD_EXE = _T("cmd.exe"); + static const wxChar *CMD_EXE = wxT("cmd.exe"); const int len = m_pfnGetConsoleCommandHistoryLength(CMD_EXE); if ( len ) @@ -499,7 +505,7 @@ int wxConsoleStderr::GetCommandHistory(wxWxCharBuffer& buf) const if ( len2 != len ) { - wxFAIL_MSG( _T("failed getting history?") ); + wxFAIL_MSG( wxT("failed getting history?") ); } } @@ -508,7 +514,7 @@ int wxConsoleStderr::GetCommandHistory(wxWxCharBuffer& buf) const bool wxConsoleStderr::IsHistoryUnchanged() const { - wxASSERT_MSG( m_ok == 1, _T("shouldn't be called if not initialized") ); + wxASSERT_MSG( m_ok == 1, wxT("shouldn't be called if not initialized") ); // get (possibly changed) command history wxWxCharBuffer history; @@ -522,13 +528,13 @@ bool wxConsoleStderr::IsHistoryUnchanged() const bool wxConsoleStderr::Write(const wxString& text) { wxASSERT_MSG( m_hStderr != INVALID_HANDLE_VALUE, - _T("should only be called if Init() returned true") ); + wxT("should only be called if Init() returned true") ); // get current position CONSOLE_SCREEN_BUFFER_INFO csbi; if ( !::GetConsoleScreenBufferInfo(m_hStderr, &csbi) ) { - wxLogLastError(_T("GetConsoleScreenBufferInfo")); + wxLogLastError(wxT("GetConsoleScreenBufferInfo")); return false; } @@ -538,21 +544,21 @@ bool wxConsoleStderr::Write(const wxString& text) if ( !::SetConsoleCursorPosition(m_hStderr, csbi.dwCursorPosition) ) { - wxLogLastError(_T("SetConsoleCursorPosition")); + wxLogLastError(wxT("SetConsoleCursorPosition")); return false; } DWORD ret; - if ( !::FillConsoleOutputCharacter(m_hStderr, _T(' '), m_dataLen, + if ( !::FillConsoleOutputCharacter(m_hStderr, wxT(' '), m_dataLen, csbi.dwCursorPosition, &ret) ) { - wxLogLastError(_T("FillConsoleOutputCharacter")); + wxLogLastError(wxT("FillConsoleOutputCharacter")); return false; } - if ( !::WriteConsole(m_hStderr, text.wx_str(), text.length(), &ret, NULL) ) + if ( !::WriteConsole(m_hStderr, text.t_str(), text.length(), &ret, NULL) ) { - wxLogLastError(_T("WriteConsole")); + wxLogLastError(wxT("WriteConsole")); return false; } @@ -575,6 +581,20 @@ bool wxGUIAppTraits::WriteToStderr(const wxString& text) return s_consoleStderr.IsOkToUse() && s_consoleStderr.Write(text); } +#else // !wxUSE_DYNLIB_CLASS + +bool wxGUIAppTraits::CanUseStderr() +{ + return false; +} + +bool wxGUIAppTraits::WriteToStderr(const wxString& WXUNUSED(text)) +{ + return false; +} + +#endif // wxUSE_DYNLIB_CLASS/!wxUSE_DYNLIB_CLASS + #endif // !__WXWINCE__ // =========================================================================== @@ -655,7 +675,7 @@ const wxChar *wxApp::GetRegisteredClassName(const wxChar *name, const size_t count = gs_regClassesInfo.size(); for ( size_t n = 0; n < count; n++ ) { - if ( gs_regClassesInfo[n].basename == name ) + if ( gs_regClassesInfo[n].regname == name ) return gs_regClassesInfo[n].regname.c_str(); } @@ -664,23 +684,14 @@ const wxChar *wxApp::GetRegisteredClassName(const wxChar *name, wxZeroMemory(wndclass); wndclass.lpfnWndProc = (WNDPROC)wxWndProc; - wndclass.hInstance = wxhInstance; + wndclass.hInstance = wxGetInstance(); wndclass.hCursor = ::LoadCursor(NULL, IDC_ARROW); wndclass.hbrBackground = (HBRUSH)wxUIntToPtr(bgBrushCol + 1); wndclass.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS | extraStyles; - ClassRegInfo regClass; - regClass.basename = name; - - // constuct a unique suffix to allow registering the class with the same - // base name in a main application using wxWidgets and a DLL using - // wxWidgets loaded into its address space: as gs_regClassesInfo variable - // is different in them, we're going to obtain a unique prefix by using its - // address here - regClass.regname = regClass.basename + - wxString::Format(wxT("@%p"), &gs_regClassesInfo); - wndclass.lpszClassName = regClass.regname.wx_str(); + ClassRegInfo regClass(name); + wndclass.lpszClassName = regClass.regname.t_str(); if ( !::RegisterClass(&wndclass) ) { wxLogLastError(wxString::Format(wxT("RegisterClass(%s)"), @@ -688,17 +699,13 @@ const wxChar *wxApp::GetRegisteredClassName(const wxChar *name, return NULL; } - // NB: remember that code elsewhere supposes that no redraw class names - // use the same names as normal classes with "NR" suffix so we must put - // "NR" at the end instead of using more natural basename+"NR"+suffix - regClass.regnameNR = regClass.regname + GetNoRedrawClassSuffix(); wndclass.style &= ~(CS_HREDRAW | CS_VREDRAW); - wndclass.lpszClassName = regClass.regnameNR.wx_str(); + wndclass.lpszClassName = regClass.regnameNR.t_str(); if ( !::RegisterClass(&wndclass) ) { wxLogLastError(wxString::Format(wxT("RegisterClass(%s)"), regClass.regname)); - ::UnregisterClass(regClass.regname.c_str(), wxhInstance); + ::UnregisterClass(regClass.regname.c_str(), wxGetInstance()); return NULL; } @@ -708,7 +715,7 @@ const wxChar *wxApp::GetRegisteredClassName(const wxChar *name, // function returns (it could be invalidated later if new elements are // added to the vector and it's reallocated but this shouldn't matter as // this pointer should be used right now, not stored) - return gs_regClassesInfo.back().regname.wx_str(); + return gs_regClassesInfo.back().regname.t_str(); } bool wxApp::IsRegisteredClassName(const wxString& name) @@ -730,13 +737,13 @@ void wxApp::UnregisterWindowClasses() for ( size_t n = 0; n < count; n++ ) { const ClassRegInfo& regClass = gs_regClassesInfo[n]; - if ( !::UnregisterClass(regClass.regname.c_str(), wxhInstance) ) + if ( !::UnregisterClass(regClass.regname.c_str(), wxGetInstance()) ) { wxLogLastError(wxString::Format(wxT("UnregisterClass(%s)"), regClass.regname)); } - if ( !::UnregisterClass(regClass.regnameNR.c_str(), wxhInstance) ) + if ( !::UnregisterClass(regClass.regnameNR.c_str(), wxGetInstance()) ) { wxLogLastError(wxString::Format(wxT("UnregisterClass(%s)"), regClass.regnameNR)); @@ -825,6 +832,10 @@ void wxApp::WakeUpIdle() } } } +#if wxUSE_THREADS + else + wxWakeUpMainThread(); +#endif // wxUSE_THREADS } // ---------------------------------------------------------------------------- @@ -891,7 +902,7 @@ int CallDllGetVersion(wxDynamicLibrary& dll) HRESULT hr = (*pfnDllGetVersion)(&dvi); if ( FAILED(hr) ) { - wxLogApiError(_T("DllGetVersion"), hr); + wxLogApiError(wxT("DllGetVersion"), hr); return 0; } @@ -919,7 +930,7 @@ int wxApp::GetComCtl32Version() // depending on the OS version and the presence of the manifest, it can // be either v5 or v6 and instead of trying to guess it just get the // handle of the already loaded version - wxLoadedDLL dllComCtl32(_T("comctl32.dll")); + wxLoadedDLL dllComCtl32(wxT("comctl32.dll")); if ( !dllComCtl32.IsLoaded() ) { s_verComCtl32 = 0; @@ -934,7 +945,7 @@ int wxApp::GetComCtl32Version() if ( !s_verComCtl32 ) { // InitCommonControlsEx is unique to 4.70 and later - void *pfn = dllComCtl32.GetSymbol(_T("InitCommonControlsEx")); + void *pfn = dllComCtl32.GetSymbol(wxT("InitCommonControlsEx")); if ( !pfn ) { // not found, must be 4.00 @@ -944,7 +955,7 @@ int wxApp::GetComCtl32Version() { // many symbols appeared in comctl32 4.71, could use any of // them except may be DllInstall() - pfn = dllComCtl32.GetSymbol(_T("InitializeFlatSB")); + pfn = dllComCtl32.GetSymbol(wxT("InitializeFlatSB")); if ( !pfn ) { // not found, must be 4.70 @@ -971,7 +982,7 @@ int wxApp::GetShell32Version() // we're prepared to handle the errors wxLogNull noLog; - wxDynamicLibrary dllShell32(_T("shell32.dll"), wxDL_VERBATIM); + wxDynamicLibrary dllShell32(wxT("shell32.dll"), wxDL_VERBATIM); if ( dllShell32.IsLoaded() ) { s_verShell32 = CallDllGetVersion(dllShell32); @@ -1011,176 +1022,6 @@ int wxApp::GetShell32Version() #endif // !__WXWINCE__ -// ---------------------------------------------------------------------------- -// Yield to incoming messages -// ---------------------------------------------------------------------------- - -WX_DECLARE_OBJARRAY(MSG, wxMSGArray); - -#include -WX_DEFINE_OBJARRAY(wxMSGArray); - -static wxMSGArray g_arrMSG; - -bool wxApp::DoYield(bool onlyIfNeeded, long eventsToProcess) -{ - if ( m_isInsideYield ) - { - if ( !onlyIfNeeded ) - { - wxFAIL_MSG( wxT("wxYield called recursively" ) ); - } - - return false; - } - - // set the flag and don't forget to reset it before returning - m_isInsideYield = true; - m_eventsToProcessInsideYield = eventsToProcess; - - wxON_BLOCK_EXIT_SET(m_isInsideYield, false); - -#if wxUSE_LOG - // disable log flushing from here because a call to wxYield() shouldn't - // normally result in message boxes popping up &c - wxLog::Suspend(); - - // ensure the logs will be flashed again when we exit - wxON_BLOCK_EXIT0(wxLog::Resume); -#endif // wxUSE_LOG - - // we don't want to process WM_QUIT from here - it should be processed in - // the main event loop in order to stop it - wxEventLoopGuarantor dummyLoopIfNeeded; - MSG msg; - while ( PeekMessage(&msg, (HWND)0, 0, 0, PM_NOREMOVE) && - msg.message != WM_QUIT ) - { -#if wxUSE_THREADS - wxMutexGuiLeaveOrEnter(); -#endif // wxUSE_THREADS - - if (msg.message == WM_PAINT) - { - // WM_PAINT messages are the last ones of the queue... - break; - } - - // choose a wxEventCategory for this Windows message - wxEventCategory cat; - switch (msg.message) - { - case WM_NCMOUSEMOVE: - case WM_NCLBUTTONDOWN: - case WM_NCLBUTTONUP: - case WM_NCLBUTTONDBLCLK: - case WM_NCRBUTTONDOWN: - case WM_NCRBUTTONUP: - case WM_NCRBUTTONDBLCLK: - case WM_NCMBUTTONDOWN: - case WM_NCMBUTTONUP: - case WM_NCMBUTTONDBLCLK: - - case WM_KEYDOWN: - case WM_KEYUP: - case WM_CHAR: - case WM_DEADCHAR: - case WM_SYSKEYDOWN: - case WM_SYSKEYUP: - case WM_SYSCHAR: - case WM_SYSDEADCHAR: - case WM_UNICHAR: - case WM_HOTKEY: - case WM_IME_STARTCOMPOSITION: - case WM_IME_ENDCOMPOSITION: - case WM_IME_COMPOSITION: - case WM_IME_KEYLAST: - case WM_COMMAND: - case WM_SYSCOMMAND: - - case WM_IME_SETCONTEXT: - case WM_IME_NOTIFY: - case WM_IME_CONTROL: - case WM_IME_COMPOSITIONFULL: - case WM_IME_SELECT: - case WM_IME_CHAR: - case WM_IME_KEYDOWN: - case WM_IME_KEYUP: - - case WM_MOUSEHOVER: -#ifdef WM_NCMOUSELEAVE - case WM_NCMOUSELEAVE: -#endif - case WM_MOUSELEAVE: - - case WM_CUT: - case WM_COPY: - case WM_PASTE: - case WM_CLEAR: - case WM_UNDO: - - case WM_MOUSEMOVE: - case WM_LBUTTONDOWN: - case WM_LBUTTONUP: - case WM_LBUTTONDBLCLK: - case WM_RBUTTONDOWN: - case WM_RBUTTONUP: - case WM_RBUTTONDBLCLK: - case WM_MBUTTONDOWN: - case WM_MBUTTONUP: - case WM_MBUTTONDBLCLK: - case WM_MOUSEWHEEL: - cat = wxEVT_CATEGORY_USER_INPUT; - break; - - case WM_TIMER: - cat = wxEVT_CATEGORY_TIMER; - break; - - default: - if (msg.message < WM_USER) - { - // 0;WM_USER-1 is the range of message IDs reserved for use - // by the system. - - // there are too many of these types of messages to handle - // them in this switch - cat = wxEVT_CATEGORY_UI; - } - else - cat = wxEVT_CATEGORY_UNKNOWN; - } - - // should we process this event now? - if (cat & eventsToProcess) - { - if ( !wxTheApp->Dispatch() ) - break; - } - else - { - // remove the message and store it - ::GetMessage(&msg, NULL, 0, 0); - g_arrMSG.Add(msg); - } - } - - // if there are pending events, we must process them. - ProcessPendingEvents(); - - // put back unprocessed events in the queue - DWORD id = GetCurrentThreadId(); - for (size_t i=0; i