From 5f3e3abd23f5ec503976885a073aae2c2a276ade Mon Sep 17 00:00:00 2001 From: George Hazan Date: Mon, 24 Nov 2014 14:19:23 +0000 Subject: - fix for thread control in Popup+; - fix for a very specific crash on exit in MTextControl; - code cleaning git-svn-id: http://svn.miranda-ng.org/main/trunk@11048 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- plugins/Popup/src/main.cpp | 11 ++- plugins/Popup/src/popup_thread.cpp | 21 ++---- plugins/Popup/src/popup_wnd2.cpp | 3 +- plugins/Popup/src/version.h | 16 ++-- plugins/mTextControl/src/main.cpp | 13 ++-- plugins/mTextControl/src/textcontrol.cpp | 126 ++++++++++--------------------- 6 files changed, 70 insertions(+), 120 deletions(-) diff --git a/plugins/Popup/src/main.cpp b/plugins/Popup/src/main.cpp index 3f97bf29f9..19a01e7224 100644 --- a/plugins/Popup/src/main.cpp +++ b/plugins/Popup/src/main.cpp @@ -319,7 +319,6 @@ MIRAPI PLUGININFOEX* MirandaPluginInfoEx(DWORD) return &pluginInfoEx; } -//ME_SYSTEM_PRESHUTDOWN event //called before the app goes into shutdown routine to make sure everyone is happy to exit static int OkToExit(WPARAM, LPARAM) { @@ -328,6 +327,13 @@ static int OkToExit(WPARAM, LPARAM) return 0; } +static int OnShutdown(WPARAM, LPARAM) +{ + UnloadPopupThread(); + UnloadPopupWnd2(); + return 0; +} + //===== Load ===== //Initializes the services provided and the link to those needed //Called when the plugin is loaded into Miranda @@ -367,6 +373,7 @@ MIRAPI int Load(void) HookEvent(ME_SYSTEM_MODULESLOADED, ModulesLoaded); HookEvent(ME_OPT_INITIALISE, OptionsInitialize); HookEvent(ME_SYSTEM_PRESHUTDOWN, OkToExit); + HookEvent(ME_SYSTEM_SHUTDOWN, OnShutdown); hbmNoAvatar = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_NOAVATAR)); @@ -437,8 +444,6 @@ MIRAPI int Unload(void) mir_free(PopupOptions.Effect); OptAdv_UnregisterVfx(); - UnloadPopupThread(); - UnloadPopupWnd2(); PopupHistoryUnload(); SrmmMenu_Unload(); diff --git a/plugins/Popup/src/popup_thread.cpp b/plugins/Popup/src/popup_thread.cpp index cede4d0a00..2c34d7e610 100644 --- a/plugins/Popup/src/popup_thread.cpp +++ b/plugins/Popup/src/popup_thread.cpp @@ -28,10 +28,10 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. // globals static int gIdleRequests = 0; static bool gTerminating = false; -static HANDLE hThreadMutex = NULL; static HWND gHwndManager = 0; static int gLockCount = 0; static volatile int nPopups = 0; +static HANDLE hThread = 0; static LIST popupList(3); @@ -203,15 +203,8 @@ static LRESULT CALLBACK PopupThreadManagerWndProc(HWND hwnd, UINT message, WPARA } // thread func -static void __cdecl PopupThread(void *arg) +static unsigned __stdcall PopupThread(void *arg) { - // grab the mutex - if ( WaitForSingleObject(hThreadMutex, INFINITE) != WAIT_OBJECT_0) { - // other thread is already running - _endthread(); - return; - } - // Create manager window DWORD err; WNDCLASSEX wcl; @@ -245,7 +238,7 @@ static void __cdecl PopupThread(void *arg) } DestroyWindow(gHwndManager); gHwndManager = NULL; - ReleaseMutex(hThreadMutex); + return 0; } ///////////////////////////////////////////////////////////////////////////////////////// @@ -253,8 +246,8 @@ static void __cdecl PopupThread(void *arg) void LoadPopupThread() { - hThreadMutex = CreateMutex(NULL, FALSE, NULL); - mir_forkthread(PopupThread, NULL); + unsigned threadId; + hThread = mir_forkthreadex(PopupThread, NULL, &threadId); } void StopPopupThread() @@ -265,8 +258,8 @@ void StopPopupThread() void UnloadPopupThread() { // We won't waint for thread to exit, Miranda's thread unsind mechanism will do that for us. - WaitForSingleObject(hThreadMutex, INFINITE); - CloseHandle(hThreadMutex); + WaitForSingleObject(hThread, INFINITE); + CloseHandle(hThread); for (int i=0; i < popupList.getCount(); ++i) delete popupList[i]; diff --git a/plugins/Popup/src/popup_wnd2.cpp b/plugins/Popup/src/popup_wnd2.cpp index cb2b350861..70c6fa820a 100644 --- a/plugins/Popup/src/popup_wnd2.cpp +++ b/plugins/Popup/src/popup_wnd2.cpp @@ -51,7 +51,6 @@ LRESULT CALLBACK MenuHostWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM bool LoadPopupWnd2() { bool res = true; - DWORD err; WNDCLASSEX wcl; wcl.cbSize = sizeof(wcl); @@ -67,7 +66,7 @@ bool LoadPopupWnd2() wcl.lpszClassName = _T(POPUP_WNDCLASS); wcl.hIconSm = (HICON)LoadImage(hInst, MAKEINTRESOURCE(IDI_POPUP), IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_DEFAULTCOLOR); g_wndClass.cPopupWnd2 = RegisterClassEx(&wcl); - err = GetLastError(); + DWORD err = GetLastError(); if (!g_wndClass.cPopupWnd2) { res = false; TCHAR msg[1024]; diff --git a/plugins/Popup/src/version.h b/plugins/Popup/src/version.h index 6651330150..445ddb4096 100644 --- a/plugins/Popup/src/version.h +++ b/plugins/Popup/src/version.h @@ -1,14 +1,14 @@ -#define __MAJOR_VERSION 2 -#define __MINOR_VERSION 1 -#define __RELEASE_NUM 1 -#define __BUILD_NUM 8 +#define __MAJOR_VERSION 2 +#define __MINOR_VERSION 1 +#define __RELEASE_NUM 1 +#define __BUILD_NUM 9 #include #define __PLUGIN_NAME "Popup plus" #define __FILENAME "Popup.dll" -#define __DESCRIPTION "Provides popup notification services for different plugins." -#define __AUTHOR "MPK, Merlin_de (Luca Santarelli, Victor Pavlychko)" +#define __DESCRIPTION "Provides popup notification services for different plugins." +#define __AUTHOR "MPK, Merlin_de (Luca Santarelli, Victor Pavlychko)" #define __AUTHOREMAIL "mp-king@web.de" -#define __AUTHORWEB "http://miranda-ng.org/p/Popup/" -#define __COPYRIGHT "© 2002 Luca Santarelli, 2004-2007 Victor Pavlychko, 2010 MPK, Merlin_de" +#define __AUTHORWEB "http://miranda-ng.org/p/Popup/" +#define __COPYRIGHT "© 2002 Luca Santarelli, 2004-2007 Victor Pavlychko, 2010 MPK, Merlin_de" diff --git a/plugins/mTextControl/src/main.cpp b/plugins/mTextControl/src/main.cpp index b24611b317..507fb5f684 100644 --- a/plugins/mTextControl/src/main.cpp +++ b/plugins/mTextControl/src/main.cpp @@ -25,7 +25,9 @@ HINSTANCE hInst = 0; int hLangpack; HMODULE hMsfteditDll = 0; -HRESULT (WINAPI *MyCreateTextServices)(IUnknown *punkOuter, ITextHost *pITextHost, IUnknown **ppUnk); + +typedef HRESULT (WINAPI *pfnMyCreateTextServices)(IUnknown *punkOuter, ITextHost *pITextHost, IUnknown **ppUnk); +pfnMyCreateTextServices MyCreateTextServices = NULL; PLUGININFOEX pluginInfoEx = { @@ -59,13 +61,8 @@ extern "C" __declspec(dllexport) int Load(void) MyCreateTextServices = 0; hMsfteditDll = LoadLibrary(_T("msftedit.dll")); - if (hMsfteditDll) { - MyCreateTextServices = (HRESULT (WINAPI *)( - IUnknown *punkOuter, - ITextHost *pITextHost, - IUnknown **ppUnk)) - GetProcAddress(hMsfteditDll, "CreateTextServices"); - } + if (hMsfteditDll) + MyCreateTextServices = (pfnMyCreateTextServices)GetProcAddress(hMsfteditDll, "CreateTextServices"); LoadEmfCache(); LoadRichEdit(); diff --git a/plugins/mTextControl/src/textcontrol.cpp b/plugins/mTextControl/src/textcontrol.cpp index 19b837349c..1cbee8c382 100644 --- a/plugins/mTextControl/src/textcontrol.cpp +++ b/plugins/mTextControl/src/textcontrol.cpp @@ -44,83 +44,60 @@ void MTextControl_RegisterClass() wcl.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH); wcl.lpszMenuName = NULL; wcl.lpszClassName = _T(MODULNAME); - wcl.hIconSm = 0; + wcl.hIconSm = 0; RegisterClassEx(&wcl); } LRESULT CALLBACK MTextControlWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { TextControlData *data = (TextControlData *)GetWindowLongPtr(hwnd, GWLP_USERDATA); - switch(msg) - { - case WM_CREATE: + switch (msg) { + case WM_CREATE: + data = new TextControlData; + data->text = 0; + data->mtext = 0; + data->htu = htuDefault; + SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)data); + PostMessage(hwnd, MTM_UPDATE, 0, 0); + return 0; + + case MTM_SETUSER: + data->htu = wParam ? (HANDLE)wParam : htuDefault; + // falldown, DefWindowProc won't process WM_USER ;) + + case WM_SETTEXT: + DefWindowProc(hwnd, msg, wParam, lParam); + // falldown + + case MTM_UPDATE: + if (data->text) delete[] data->text; + if (data->mtext) MTI_MTextDestroy(data->mtext); { - data = new TextControlData; - data->text = 0; - data->mtext = 0; - data->htu = htuDefault; - SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)data); - PostMessage(hwnd, MTM_UPDATE, 0, 0); - return 0; - } - - case MTM_SETUSER: - { - data->htu = wParam ? (HANDLE)wParam : htuDefault; - // falldown, DefWindowProc won't process WM_USER ;) - } - - case WM_SETTEXT: - { - DefWindowProc(hwnd, msg, wParam, lParam); - // falldown - } - - case MTM_UPDATE: - { - if (data->text) delete [] data->text; - if (data->mtext) MTI_MTextDestroy(data->mtext); - int textLength = GetWindowTextLength(hwnd); - data->text = new TCHAR[textLength+1]; - GetWindowText(hwnd, data->text, textLength+1); + data->text = new TCHAR[textLength + 1]; + GetWindowText(hwnd, data->text, textLength + 1); data->mtext = MTI_MTextCreateW(data->htu, data->text); - + RECT rc; GetClientRect(hwnd, &rc); MTI_MTextSetParent(data->mtext, hwnd, rc); InvalidateRect(hwnd, 0, TRUE); - - return TRUE; } + return TRUE; - case WM_PAINT: - { - return MTextControl_OnPaint(hwnd, wParam, lParam); - } + case WM_PAINT: + return MTextControl_OnPaint(hwnd, wParam, lParam); - case WM_ERASEBKGND: - { - HDC hdc = (HDC)wParam; - RECT rc; - GetClientRect(hwnd, &rc); - FillRect(hdc, &rc, GetSysColorBrush(COLOR_BTNFACE)); - return TRUE; - } - -// case WM_NCHITTEST: -// case WM_NCMOUSEMOVE: - case WM_MOUSEMOVE: -// case WM_LBUTTONDOWN: -// case WM_LBUTTONUP: -// case WM_RBUTTONDOWN: -// case WM_RBUTTONUP: - { - if (data && data->mtext) - return MTI_MTextSendMessage(hwnd, data->mtext, msg, wParam, lParam); - break; - } + case WM_ERASEBKGND: + RECT rc; + GetClientRect(hwnd, &rc); + FillRect((HDC)wParam, &rc, GetSysColorBrush(COLOR_BTNFACE)); + return TRUE; + case WM_MOUSEMOVE: + if (data && data->mtext) + return MTI_MTextSendMessage(hwnd, data->mtext, msg, wParam, lParam); + break; } return DefWindowProc(hwnd, msg, wParam, lParam); @@ -137,35 +114,17 @@ LRESULT MTextControl_OnPaint(HWND hwnd, WPARAM wParam, LPARAM lParam) GetClientRect(hwnd, &rc); FrameRect(hdc, &rc, (HBRUSH)GetStockObject(BLACK_BRUSH)); - SetTextColor(hdc, RGB(0,0,0)); + SetTextColor(hdc, RGB(0, 0, 0)); SetBkMode(hdc, TRANSPARENT); // Find the text to draw TextControlData *data = (TextControlData *)GetWindowLongPtr(hwnd, GWLP_USERDATA); - if (data->mtext) - { -/* - // Font-related stuff - LOGFONT lfText; - lfText.lfHeight = -11; //"8" in the font dialog - lfText.lfWidth = lfText.lfEscapement = lfText.lfOrientation = 0; - lfText.lfItalic = lfText.lfUnderline = lfText.lfStrikeOut = FALSE; - lfText.lfCharSet = DEFAULT_CHARSET; - lfText.lfOutPrecision = OUT_DEFAULT_PRECIS; - lfText.lfClipPrecision = CLIP_DEFAULT_PRECIS; - lfText.lfQuality = DEFAULT_QUALITY; - lfText.lfPitchAndFamily = DEFAULT_PITCH | FF_SWISS; - lstrcpy(lfText.lfFaceName,_T("Tahoma")); - lfText.lfWeight = FW_REGULAR; - HFONT hfntSave = (HFONT)SelectObject(hdc, CreateFontIndirect(&lfText)); -*/ - + if (data->mtext) { HFONT hfntSave = 0; HFONT hfnt = (HFONT)SendMessage(hwnd, WM_GETFONT, 0, 0); if (!hfnt) hfnt = (HFONT)SendMessage(GetParent(hwnd), WM_GETFONT, 0, 0); - if (hfnt) - { + if (hfnt) { LOGFONT lf; GetObject(hfnt, sizeof(lf), &lf); hfntSave = (HFONT)SelectObject(hdc, hfnt); @@ -184,11 +143,8 @@ LRESULT MTextControl_OnPaint(HWND hwnd, WPARAM wParam, LPARAM lParam) if (hfntSave) SelectObject(hdc, hfntSave); - -// DeleteObject(SelectObject(hdc, hfntSave)); - } - + // Release the device context EndPaint(hwnd, &ps); -- cgit v1.2.3