From 6b0e3e2f544d782b202e56cabee146ddd0fe4cfa Mon Sep 17 00:00:00 2001 From: George Hazan Date: Sun, 5 Jan 2014 21:56:14 +0000 Subject: memory leak fix in ChangeKeyboardLayout git-svn-id: http://svn.miranda-ng.org/main/trunk@7519 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- plugins/ChangeKeyboardLayout/src/Version.h | 24 +- plugins/ChangeKeyboardLayout/src/hook_events.cpp | 85 +- plugins/ChangeKeyboardLayout/src/main.cpp | 16 +- plugins/ChangeKeyboardLayout/src/options.cpp | 4 +- .../ChangeKeyboardLayout/src/text_operations.cpp | 865 ++++++++++----------- 5 files changed, 446 insertions(+), 548 deletions(-) (limited to 'plugins/ChangeKeyboardLayout/src') diff --git a/plugins/ChangeKeyboardLayout/src/Version.h b/plugins/ChangeKeyboardLayout/src/Version.h index b4c2219396..47741393be 100644 --- a/plugins/ChangeKeyboardLayout/src/Version.h +++ b/plugins/ChangeKeyboardLayout/src/Version.h @@ -1,14 +1,14 @@ -#define __MAJOR_VERSION 0 -#define __MINOR_VERSION 0 -#define __RELEASE_NUM 1 -#define __BUILD_NUM 5 +#define __MAJOR_VERSION 0 +#define __MINOR_VERSION 0 +#define __RELEASE_NUM 1 +#define __BUILD_NUM 6 -#define __FILEVERSION_STRING __MAJOR_VERSION,__MINOR_VERSION,__RELEASE_NUM,__BUILD_NUM +#define __FILEVERSION_STRING __MAJOR_VERSION,__MINOR_VERSION,__RELEASE_NUM,__BUILD_NUM -#define __PLUGIN_NAME "Change keyboard layout" -#define __FILENAME "ChangeKeyboardLayout.dll" -#define __DESCRIPTION "Plugin for changing keyboard layout of text (multilayout)." -#define __AUTHOR "Yasnovidyashii" -#define __AUTHOREMAIL "Yasnovidyashii@gmail.com" -#define __AUTHORWEB "http://miranda-ng.org/p/ChangeKeyboardLayout/" -#define __COPYRIGHT "© 2006-2009 Mikhail Yur'ev" +#define __PLUGIN_NAME "Change keyboard layout" +#define __FILENAME "ChangeKeyboardLayout.dll" +#define __DESCRIPTION "Plugin for changing keyboard layout of text (multilayout)." +#define __AUTHOR "Yasnovidyashii" +#define __AUTHOREMAIL "Yasnovidyashii@gmail.com" +#define __AUTHORWEB "http://miranda-ng.org/p/ChangeKeyboardLayout/" +#define __COPYRIGHT "© 2006-2009 Mikhail Yur'ev" diff --git a/plugins/ChangeKeyboardLayout/src/hook_events.cpp b/plugins/ChangeKeyboardLayout/src/hook_events.cpp index 0c350a8062..dfd8c6217a 100644 --- a/plugins/ChangeKeyboardLayout/src/hook_events.cpp +++ b/plugins/ChangeKeyboardLayout/src/hook_events.cpp @@ -2,27 +2,22 @@ INT_PTR APIChangeLayout(WPARAM wParam, LPARAM lParam) { - BOOL CurrentWord; - CurrentWord = moOptions.CurrentWordLayout; + BOOL CurrentWord = moOptions.CurrentWordLayout; return ChangeLayout((HWND)wParam, TOT_Layout, CurrentWord); } INT_PTR APIGetLayoutOfText(WPARAM wParam, LPARAM lParam) { - LPTSTR ptszInText; - ptszInText = (TCHAR*)lParam; + LPTSTR ptszInText = (TCHAR*)lParam; return (int) GetLayoutOfText(ptszInText); } INT_PTR APIChangeTextLayout(WPARAM wParam, LPARAM lParam) { - LPTSTR ptszInText; - CKLLayouts *ckllFromToLay; + LPTSTR ptszInText = (TCHAR*)wParam; + CKLLayouts *ckllFromToLay = (CKLLayouts*)lParam; - ptszInText = (TCHAR*)wParam; - ckllFromToLay = (CKLLayouts*)lParam; - - return (int) ChangeTextLayout(ptszInText, ckllFromToLay->hklFrom, ckllFromToLay->hklTo, ckllFromToLay->bTwoWay); + return (INT_PTR)ChangeTextLayout(ptszInText, ckllFromToLay->hklFrom, ckllFromToLay->hklTo, ckllFromToLay->bTwoWay); } void ReadMainOptions() @@ -55,7 +50,6 @@ void WriteMainOptions() db_set_b(NULL, ModuleName, "CaseOperations", moOptions.bCaseOperations); } - void ReadPopupOptions() { poOptions.bColourType = db_get_b(NULL, ModuleName, "ColourType", 0); @@ -80,7 +74,7 @@ void WritePopupOptions() void RegPopupActions() { - if ( ServiceExists(MS_POPUP_ADDPOPUP)) { + if (ServiceExists(MS_POPUP_ADDPOPUP)) { poOptions.paActions[0].cbSize = sizeof(POPUPACTION); strcpy(poOptions.paActions[0].lpzTitle, ModuleName); strcat(poOptions.paActions[0].lpzTitle, "/Copy to clipboard"); @@ -99,7 +93,7 @@ int OnIconsChanged(WPARAM wParam, LPARAM lParam) return 0; } -static IconItem iconList[] = +static IconItem iconList[] = { { LPGEN("Popup"), "ckl_popup_icon", IDI_POPUPICON }, { LPGEN("Copy to clipboard"), "ckl_copy_icon", IDI_COPYICON } @@ -107,32 +101,24 @@ static IconItem iconList[] = int ModulesLoaded(WPARAM wParam, LPARAM lParam) { - int i, iRes; - DBVARIANT dbv = {0}; LPCTSTR ptszEmptySting = _T(""); - LPTSTR ptszCurrLayout; - LPSTR ptszTemp; - - //Заполняем конфигурационные строки из базы. Если их там нет - генерируем. - for (i = 0; i < bLayNum; i++) { - ptszCurrLayout = GenerateLayoutString(hklLayouts[i]); - ptszTemp = GetNameOfLayout(hklLayouts[i]); - iRes = db_get_ts(NULL, ModuleName, ptszTemp, &dbv); - if (iRes != 0) + + // Заполняем конфигурационные строки из базы. Если их там нет - генерируем. + for (int i = 0; i < bLayNum; i++) { + LPTSTR ptszCurrLayout = GenerateLayoutString(hklLayouts[i]); + LPSTR ptszTemp = GetNameOfLayout(hklLayouts[i]); + ptrT tszValue(db_get_tsa(NULL, ModuleName, ptszTemp)); + if (tszValue == 0) ptszLayStrings[i] = ptszCurrLayout; - else - if(_tcscmp((dbv.ptszVal), ptszEmptySting) == 0) { - ptszLayStrings[i] = ptszCurrLayout; - mir_free(dbv.ptszVal); - } - else - { - ptszLayStrings[i] = dbv.ptszVal; - if(_tcscmp(ptszCurrLayout, ptszLayStrings[i]) == 0) - db_unset(NULL, ModuleName, ptszTemp); - mir_free(ptszCurrLayout); - } - mir_free(ptszTemp); + else if (!_tcscmp(tszValue, ptszEmptySting)) + ptszLayStrings[i] = ptszCurrLayout; + else { + ptszLayStrings[i] = tszValue.detouch(); + if (!_tcscmp(ptszCurrLayout, ptszLayStrings[i])) + db_unset(NULL, ModuleName, ptszTemp); + mir_free(ptszCurrLayout); + } + mir_free(ptszTemp); } // Прочитаем основные настройки @@ -148,7 +134,6 @@ int ModulesLoaded(WPARAM wParam, LPARAM lParam) // Хук на нажатие клавиши kbHook_All = SetWindowsHookEx(WH_KEYBOARD, (HOOKPROC)Keyboard_Hook, NULL, GetCurrentThreadId()); - // Зарегим сервисы hChangeLayout = CreateServiceFunction(MS_CKL_CHANGELAYOUT, APIChangeLayout); hGetLayoutOfText = CreateServiceFunction(MS_CKL_GETLAYOUTOFTEXT, APIGetLayoutOfText); hChangeTextLayout = CreateServiceFunction(MS_CKL_CHANGETEXTLAYOUT, APIChangeTextLayout); @@ -176,7 +161,7 @@ int OnOptionsInitialise(WPARAM wParam, LPARAM lParam) odp.pfnDlgProc = DlgMainProcOptions; Options_AddPage(wParam, &odp); - if ( ServiceExists(MS_POPUP_ADDPOPUP)) { + if (ServiceExists(MS_POPUP_ADDPOPUP)) { odp.pszTemplate = MAKEINTRESOURCEA(IDD_POPUP_OPTION_FORM); odp.pszGroup = LPGEN("Popups"); odp.pfnDlgProc = DlgPopupsProcOptions; @@ -187,16 +172,13 @@ int OnOptionsInitialise(WPARAM wParam, LPARAM lParam) LRESULT CALLBACK Keyboard_Hook(int code, WPARAM wParam, LPARAM lParam) { - DWORD lcode; - - if (code == HC_ACTION) - { - lcode = 0; - if ((GetKeyState(VK_SHIFT)&0x8000)) lcode |= HOTKEYF_SHIFT; - if ((GetKeyState(VK_CONTROL)&0x8000)) lcode |= HOTKEYF_CONTROL; - if ((GetKeyState(VK_MENU)&0x8000)) lcode |= HOTKEYF_ALT; - if ((GetKeyState(VK_LWIN)&0x8000)||(GetKeyState(VK_RWIN)&0x8000)) lcode |= HOTKEYF_EXT; - lcode = lcode<<8; + if (code == HC_ACTION) { + DWORD lcode = 0; + if ((GetKeyState(VK_SHIFT) & 0x8000)) lcode |= HOTKEYF_SHIFT; + if ((GetKeyState(VK_CONTROL) & 0x8000)) lcode |= HOTKEYF_CONTROL; + if ((GetKeyState(VK_MENU) & 0x8000)) lcode |= HOTKEYF_ALT; + if ((GetKeyState(VK_LWIN) & 0x8000) || (GetKeyState(VK_RWIN) & 0x8000)) lcode |= HOTKEYF_EXT; + lcode = lcode << 8; if ((wParam != VK_SHIFT) && (wParam != VK_MENU) && (wParam != VK_CONTROL) && (wParam != VK_LWIN) && (wParam != VK_RWIN)) lcode += wParam; @@ -222,10 +204,9 @@ LRESULT CALLBACK Keyboard_Hook(int code, WPARAM wParam, LPARAM lParam) int CALLBACK CKLPopupDlgProc(HWND hWnd, UINT uiMessage, WPARAM wParam, LPARAM lParam) { - LPTSTR ptszPopupText; + LPTSTR ptszPopupText = (LPTSTR)CallService(MS_POPUP_GETPLUGINDATA, (WPARAM)hWnd, (LPARAM)&ptszPopupText); - ptszPopupText = (LPTSTR)CallService(MS_POPUP_GETPLUGINDATA, (WPARAM)hWnd, (LPARAM)&ptszPopupText); - switch(uiMessage) { + switch (uiMessage) { case WM_COMMAND: if (HIWORD(wParam) == STN_CLICKED) { if (!IsBadStringPtr(ptszPopupText, MaxTextSize)) diff --git a/plugins/ChangeKeyboardLayout/src/main.cpp b/plugins/ChangeKeyboardLayout/src/main.cpp index 191d2dc9f7..f2ea678b30 100644 --- a/plugins/ChangeKeyboardLayout/src/main.cpp +++ b/plugins/ChangeKeyboardLayout/src/main.cpp @@ -31,7 +31,6 @@ HKL hklEng = (HKL)0x04090409; LPCTSTR ptszSeparators = _T(" \t\n\r"); HANDLE hOptionsInitialize; -HANDLE hModulesLoaded; BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) { @@ -49,25 +48,22 @@ extern "C" __declspec(dllexport) int Load(void) mir_getLP(&pluginInfoEx); ZeroMemory(hklLayouts, 20 * sizeof(HKL)); bLayNum = GetKeyboardLayoutList(20,hklLayouts); - if (bLayNum<2) + if (bLayNum < 2) return 1; - hOptionsInitialize = HookEvent(ME_OPT_INITIALISE, OnOptionsInitialise); - hModulesLoaded = HookEvent(ME_SYSTEM_MODULESLOADED,ModulesLoaded); + + HookEvent(ME_OPT_INITIALISE, OnOptionsInitialise); + HookEvent(ME_SYSTEM_MODULESLOADED, ModulesLoaded); return 0; } extern "C" __declspec(dllexport) int Unload(void) { - DWORD i; - - for (i = 0;icbBuff += cb; - esd->pbBuff = (PBYTE)realloc(esd->pbBuff, esd->cbBuff+sizeof(TCHAR)); - CopyMemory(esd->pbBuff+esd->iCurrent, pbBuff, cb); + esd->pbBuff = (PBYTE)realloc(esd->pbBuff, esd->cbBuff + sizeof(TCHAR)); + CopyMemory(esd->pbBuff + esd->iCurrent, pbBuff, cb); esd->iCurrent += cb; esd->pbBuff[esd->iCurrent] = 0; - - esd->pbBuff[esd->iCurrent+1] = 0; - + esd->pbBuff[esd->iCurrent + 1] = 0; + *pcb = cb; return 0; } -LPTSTR GeTStringFromStreamData(struct EditStreamData *esd) +LPTSTR GeTStringFromStreamData(EditStreamData *esd) { - LPTSTR ptszTemp, ptszOutText; DWORD i, k; - ptszOutText = (LPTSTR)mir_alloc(MaxTextSize*sizeof(TCHAR)); - ptszTemp = (TCHAR*)esd->pbBuff; + LPTSTR ptszOutText = (LPTSTR)mir_alloc(MaxTextSize*sizeof(TCHAR)); + LPTSTR ptszTemp = (TCHAR*)esd->pbBuff; - for (i = k = 0;i<_tcslen(ptszTemp);i++) - if ((ptszTemp[i] == 0x0A)||(ptszTemp[i] == 0x2028)) + for (i = k = 0; i < _tcslen(ptszTemp); i++) { + if ((ptszTemp[i] == 0x0A) || (ptszTemp[i] == 0x2028)) ptszOutText[k++] = 0x0D; - else if (ptszTemp[i] == 0x0D) - { - ptszOutText[k++] = 0x0D; - if (ptszTemp[i+1] == 0x0A) i++; - } - else - ptszOutText[k++] = ptszTemp[i]; - + else if (ptszTemp[i] == 0x0D) { + ptszOutText[k++] = 0x0D; + if (ptszTemp[i + 1] == 0x0A) i++; + } + else ptszOutText[k++] = ptszTemp[i]; + } + ptszOutText[k] = 0; return ptszOutText; } @@ -47,24 +48,16 @@ LPTSTR GeTStringFromStreamData(struct EditStreamData *esd) BOOL CopyTextToClipboard(LPTSTR ptszText) { - HGLOBAL hCopy; - - if (OpenClipboard(NULL)) - { - EmptyClipboard(); - hCopy = GlobalAlloc(GMEM_MOVEABLE, (_tcslen(ptszText)+1)*sizeof(TCHAR)); - _tcscpy((TCHAR*)GlobalLock(hCopy), ptszText); - GlobalUnlock(hCopy); - - - SetClipboardData(CF_UNICODETEXT, hCopy); - - - CloseClipboard(); - return TRUE; - } - else + if (!OpenClipboard(NULL)) return FALSE; + + EmptyClipboard(); + HGLOBAL hCopy = GlobalAlloc(GMEM_MOVEABLE, (_tcslen(ptszText) + 1)*sizeof(TCHAR)); + _tcscpy((TCHAR*)GlobalLock(hCopy), ptszText); + GlobalUnlock(hCopy); + SetClipboardData(CF_UNICODETEXT, hCopy); + CloseClipboard(); + return TRUE; } LPSTR GetNameOfLayout(HKL hklLayout) @@ -76,76 +69,68 @@ LPSTR GetNameOfLayout(HKL hklLayout) LPTSTR GetShortNameOfLayout(HKL hklLayout) { - DWORD dwLcid; TCHAR szLI[20]; - LPTSTR ptszLiShort; - - ptszLiShort = (LPTSTR)mir_alloc(3*sizeof(TCHAR)); - dwLcid = MAKELCID(hklLayout, 0); - GetLocaleInfo(dwLcid, LOCALE_SISO639LANGNAME , szLI, 10); + LPTSTR ptszLiShort = (LPTSTR)mir_alloc(3*sizeof(TCHAR)); + DWORD dwLcid = MAKELCID(hklLayout, 0); + GetLocaleInfo(dwLcid, LOCALE_SISO639LANGNAME, szLI, 10); ptszLiShort[0] = toupper(szLI[0]); ptszLiShort[1] = toupper(szLI[1]); ptszLiShort[2] = 0; - return ptszLiShort; } HKL GetNextLayout(HKL hklCurLay) { - DWORD i; - for(i = 0; i < bLayNum; i++) - if (hklLayouts[i] == hklCurLay) - return hklLayouts[(i+1)%(bLayNum)]; + for (DWORD i = 0; i < bLayNum; i++) + if (hklLayouts[i] == hklCurLay) + return hklLayouts[(i+1)%(bLayNum)]; return NULL; } LPTSTR GenerateLayoutString(HKL hklLayout) { - LPTSTR ptszLayStr, ptszTemp; - SHORT shVirtualKey; - UINT iScanCode; BYTE bState[256] = {0}; - DWORD i, j; - int iRes; - ptszLayStr = (LPTSTR)mir_alloc(100*sizeof(TCHAR)); - ptszTemp = (LPTSTR)mir_alloc(3*sizeof(TCHAR)); + LPTSTR ptszLayStr = (LPTSTR)mir_alloc(100 * sizeof(TCHAR)); + LPTSTR ptszTemp = (LPTSTR)mir_alloc(3 * sizeof(TCHAR)); ptszTemp[0] = 0; - for(i = 0;i<_tcslen(ptszKeybEng);i++) - { - shVirtualKey = VkKeyScanEx(ptszKeybEng[i], hklEng); - iScanCode = MapVirtualKeyEx(shVirtualKey&0x00FF, 0, hklEng); - - for (j = 0;j<256;j++) - bState[j] = 0; - - if (shVirtualKey&0x0100) bState[VK_SHIFT] = 0x80; - if (shVirtualKey&0x0200) bState[VK_CONTROL] = 0x80; - if (shVirtualKey&0x0400) bState[VK_MENU] = 0x80; - shVirtualKey = MapVirtualKeyEx(iScanCode, 1, hklLayout); - bState[shVirtualKey&0x00FF] = 0x80; + DWORD i; + for (i = 0; i < _tcslen(ptszKeybEng); i++) { + SHORT shVirtualKey = VkKeyScanEx(ptszKeybEng[i], hklEng); + UINT iScanCode = MapVirtualKeyEx(shVirtualKey & 0x00FF, 0, hklEng); - - iRes = ToUnicodeEx(shVirtualKey, iScanCode, bState, ptszTemp, 3, 0, hklLayout); - // Защита от дэд-кеев - if (iRes<0) ToUnicodeEx(shVirtualKey, iScanCode, bState, ptszTemp, 3, 0, hklLayout); - - - //Если нам вернули нулевой символ, или не вернули ничего, то присвоим "звоночек" - if (ptszTemp[0] == 0) ptszLayStr[i] = 3; else ptszLayStr[i] = ptszTemp[0]; + for (DWORD j = 0; j < 256; j++) + bState[j] = 0; + + if (shVirtualKey & 0x0100) bState[VK_SHIFT] = 0x80; + if (shVirtualKey & 0x0200) bState[VK_CONTROL] = 0x80; + if (shVirtualKey & 0x0400) bState[VK_MENU] = 0x80; + + shVirtualKey = MapVirtualKeyEx(iScanCode, 1, hklLayout); + bState[shVirtualKey & 0x00FF] = 0x80; + + int iRes = ToUnicodeEx(shVirtualKey, iScanCode, bState, ptszTemp, 3, 0, hklLayout); + // Защита от дэд-кеев + if (iRes < 0) + ToUnicodeEx(shVirtualKey, iScanCode, bState, ptszTemp, 3, 0, hklLayout); + + // Если нам вернули нулевой символ, или не вернули ничего, то присвоим "звоночек" + if (ptszTemp[0] == 0) + ptszLayStr[i] = 3; + else + ptszLayStr[i] = ptszTemp[0]; } ptszLayStr[i] = 0; - + mir_free(ptszTemp); return ptszLayStr; } LPTSTR GetLayoutString(HKL hklLayout) { - DWORD i; - for (i = 0;i=j) - ptszOutText[i] = ptszKeybNext[j]; - } - - if (TwoWay && (!Found)) - for (j = 0;(j<_tcslen(ptszKeybNext) && (!Found));j++) - if (ptszKeybNext[j] == ptszInText[i]) - { - Found = TRUE; - if (_tcslen(ptszKeybCur)>=j) - ptszOutText[i] = ptszKeybCur[j]; - } - }; + for (DWORD i = 0; i < _tcslen(ptszInText); i++) { + BOOL Found = FALSE; + for (DWORD j = 0; j < _tcslen(ptszKeybCur) && !Found; j++) + if (ptszKeybCur[j] == ptszInText[i]) { + Found = TRUE; + if (_tcslen(ptszKeybNext) >= j) + ptszOutText[i] = ptszKeybNext[j]; + } + + if (TwoWay && !Found) + for (DWORD j = 0; j < _tcslen(ptszKeybNext) && !Found; j++) + if (ptszKeybNext[j] == ptszInText[i]) { + Found = TRUE; + if (_tcslen(ptszKeybCur) >= j) + ptszOutText[i] = ptszKeybCur[j]; + } + } return ptszOutText; } HKL GetLayoutOfText(LPCTSTR ptszInText) { - DWORD i, j; - LPTSTR ptszKeybBuff; - DWORD dwCountSymbols, dwMaxSymbols, dwTemp; - HKL hklCurLay; + HKL hklCurLay = hklLayouts[0]; + LPTSTR ptszKeybBuff = ptszLayStrings[0]; + DWORD dwMaxSymbols = 0, dwTemp = 0; - hklCurLay = hklLayouts[0]; - ptszKeybBuff = ptszLayStrings[0]; - dwMaxSymbols = dwTemp = 0; + for (DWORD j = 0; j < _tcslen(ptszInText); j++) + if (_tcschr(ptszKeybBuff, ptszInText[j]) != NULL) + ++dwMaxSymbols; - for (j = 0;j<_tcslen(ptszInText);j++) - if (_tcschr(ptszKeybBuff, ptszInText[j]) != NULL) ++dwMaxSymbols; - - for(i = 1; i < bLayNum; i++) - { + for (DWORD i = 1; i < bLayNum; i++) { ptszKeybBuff = ptszLayStrings[i]; - dwCountSymbols = 0; + DWORD dwCountSymbols = 0; - for (j = 0;j<_tcslen(ptszInText);j++) - if (_tcschr(ptszKeybBuff, ptszInText[j]) != NULL) ++dwCountSymbols; + for (DWORD j = 0; j<_tcslen(ptszInText); j++) + if (_tcschr(ptszKeybBuff, ptszInText[j]) != NULL) + ++dwCountSymbols; if (dwCountSymbols == dwMaxSymbols) dwTemp = dwCountSymbols; - else if (dwCountSymbols>dwMaxSymbols) - { + else if (dwCountSymbols>dwMaxSymbols) { dwMaxSymbols = dwCountSymbols; hklCurLay = hklLayouts[i]; } @@ -257,7 +219,6 @@ int ChangeLayout(HWND hTextWnd, BYTE TextOperation, BOOL CurrentWord) HKL hklCurLay, hklToLay; LPTSTR ptszInText, ptszOutText, ptszMBox, ptszPopupText, ptszTemp; - IEVIEWEVENT ieEvent; CHARRANGE crSelection, crTemp; DWORD dwStartWord, dwEndWord; int i, iRes; @@ -265,377 +226,337 @@ int ChangeLayout(HWND hTextWnd, BYTE TextOperation, BOOL CurrentWord) BYTE WindowType = WTYPE_Unknown; BOOL WindowIsReadOnly, TwoWay; - EDITSTREAM esStream = {0}; - struct EditStreamData esdData; - - esStream.dwCookie = (DWORD)&esdData; - esStream.pfnCallback = EditStreamOutRtf; - if (hTextWnd == NULL) hTextWnd = GetFocus(); - if (hTextWnd != NULL) - { - // --------------Определяем тип окна----------------- - - ZeroMemory((void *)&ieEvent, sizeof(ieEvent)); - ieEvent.cbSize = sizeof(IEVIEWEVENT); - ieEvent.dwFlags = 0; - ieEvent.iType = IEE_GET_SELECTION; - //event.codepage = 1200; - - if (ServiceExists(MS_HPP_EG_EVENT)) - { - //То же самое для History++ - ieEvent.hwnd = hTextWnd; - ptszInText = (TCHAR*)CallService(MS_HPP_EG_EVENT, 0, (LPARAM)&ieEvent); + if (hTextWnd == NULL) + return 0; - if (!IsBadStringPtr(ptszInText, MaxTextSize)) WindowType = WTYPE_HistoryPP; - } + //--------------Определяем тип окна----------------- + IEVIEWEVENT ieEvent = { 0 }; + ieEvent.cbSize = sizeof(IEVIEWEVENT); + ieEvent.iType = IEE_GET_SELECTION; + + if (ServiceExists(MS_HPP_EG_EVENT)) { + // То же самое для History++ + ieEvent.hwnd = hTextWnd; + ptszInText = (TCHAR*)CallService(MS_HPP_EG_EVENT, 0, (LPARAM)&ieEvent); + + if (!IsBadStringPtr(ptszInText, MaxTextSize)) + WindowType = WTYPE_HistoryPP; + } + + if ((WindowType == WTYPE_Unknown) && (ServiceExists(MS_IEVIEW_EVENT))) { + // Извращенное определение хэндла IEView + ieEvent.hwnd = GetParent(GetParent(hTextWnd)); + + ptszInText = (TCHAR*)CallService(MS_IEVIEW_EVENT, 0, (LPARAM)&ieEvent); + if (!IsBadStringPtr(ptszInText, MaxTextSize)) + WindowType = WTYPE_IEView; + } - if ((WindowType == WTYPE_Unknown) && (ServiceExists(MS_IEVIEW_EVENT))) - { - // Извращенное определение хэндла IEView - ieEvent.hwnd = GetParent(GetParent(hTextWnd)); + if (WindowType == WTYPE_Unknown) { + ptszTemp = (LPTSTR)mir_alloc(255 * sizeof(TCHAR)); + i = GetClassName(hTextWnd, ptszTemp, 255); + ptszTemp[i] = 0; - ptszInText = (TCHAR*)CallService(MS_IEVIEW_EVENT, 0, (LPARAM)&ieEvent); - if (!IsBadStringPtr(ptszInText, MaxTextSize)) WindowType = WTYPE_IEView; + if (_tcsstr(CharUpper(ptszTemp), _T("RICHEDIT")) != NULL) { + WindowType = WTYPE_RichEdit; + SendMessage(hTextWnd, EM_EXGETSEL, 0, (LPARAM)&crSelection); } - - if (WindowType == WTYPE_Unknown) - { - ptszTemp = (LPTSTR)mir_alloc(255*sizeof(TCHAR)); - i = GetClassName(hTextWnd, ptszTemp, 255); - ptszTemp[i] = 0; - - if (_tcsstr(CharUpper(ptszTemp), _T("RICHEDIT")) != NULL ) - { - WindowType = WTYPE_RichEdit; - SendMessage(hTextWnd, EM_EXGETSEL, 0, (LPARAM)&crSelection); + mir_free(ptszTemp); + } + + if (WindowType == WTYPE_Unknown) { + SendMessage(hTextWnd, EM_GETSEL, (WPARAM)&crSelection.cpMin, (LPARAM)&crSelection.cpMax); + if ((SendMessage(hTextWnd, WM_GETDLGCODE, 0, 0)&(DLGC_HASSETSEL)) && (crSelection.cpMin >= 0)) + WindowType = WTYPE_Edit; + } + + // Получим текст из Рича или обычного Едита + if (WindowType == WTYPE_RichEdit || WindowType == WTYPE_Edit) { + dwStartWord = dwEndWord = -1; + SendMessage(hTextWnd, WM_SETREDRAW, FALSE, 0); + + // Бэкап выделения + crTemp = crSelection; + + // Если имеется выделенный текст, то получим его + if (crSelection.cpMin != crSelection.cpMax) { + if (WindowType == WTYPE_RichEdit) { + EditStreamData esdData; + EDITSTREAM esStream = { 0 }; + esStream.dwCookie = (DWORD)&esdData; + esStream.pfnCallback = EditStreamOutRtf; + if (SendMessage(hTextWnd, EM_STREAMOUT, SF_TEXT | SF_UNICODE | SFF_SELECTION, (LPARAM)&esStream) > 0) + ptszInText = GeTStringFromStreamData(&esdData); + else { + SendMessage(hTextWnd, EM_EXSETSEL, 0, (LPARAM)&crSelection); + SendMessage(hTextWnd, WM_SETREDRAW, TRUE, 0); + InvalidateRect(hTextWnd, NULL, FALSE); + return 1; + } + } + if (WindowType == WTYPE_Edit) { + ptszTemp = (LPTSTR)mir_alloc(MaxTextSize*sizeof(TCHAR)); + ptszInText = (LPTSTR)mir_alloc(MaxTextSize*sizeof(TCHAR)); + iRes = SendMessage(hTextWnd, WM_GETTEXT, (WPARAM)MaxTextSize, (LPARAM)ptszTemp); + if (!IsBadStringPtr(ptszInText, MaxTextSize) && (iRes > 0)) { + _tcsncpy(ptszInText, &ptszTemp[crSelection.cpMin], crSelection.cpMax - crSelection.cpMin); + ptszInText[crSelection.cpMax - crSelection.cpMin] = 0; + mir_free(ptszTemp); + } + else { + SendMessage(hTextWnd, EM_EXSETSEL, 0, (LPARAM)&crSelection); + SendMessage(hTextWnd, WM_SETREDRAW, TRUE, 0); + InvalidateRect(hTextWnd, NULL, FALSE); + return 1; + } } - mir_free(ptszTemp); - } - - if (WindowType == WTYPE_Unknown) - { - SendMessage(hTextWnd, EM_GETSEL, (WPARAM)&crSelection.cpMin, (LPARAM)&crSelection.cpMax); - if ((SendMessage(hTextWnd, WM_GETDLGCODE, 0, 0)&(DLGC_HASSETSEL)) && (crSelection.cpMin>=0)) - WindowType = WTYPE_Edit; } + // Если выделения нет, то получим нужный текст + else { + // Получаем весь текст в поле + if (WindowType == WTYPE_RichEdit) { + crTemp.cpMin = 0; + crTemp.cpMax = -1; + SendMessage(hTextWnd, EM_EXSETSEL, 0, (LPARAM)&crTemp); + + EditStreamData esdData; + EDITSTREAM esStream = { 0 }; + esStream.dwCookie = (DWORD)&esdData; + esStream.pfnCallback = EditStreamOutRtf; + if (SendMessage(hTextWnd, EM_STREAMOUT, SF_TEXT | SF_UNICODE | SFF_SELECTION, (LPARAM)&esStream) != 0) + ptszInText = GeTStringFromStreamData(&esdData); + else { + SendMessage(hTextWnd, EM_EXSETSEL, 0, (LPARAM)&crSelection); + SendMessage(hTextWnd, WM_SETREDRAW, TRUE, 0); + InvalidateRect(hTextWnd, NULL, FALSE); + return 1; + } + } + if (WindowType == WTYPE_Edit) { + ptszInText = (LPTSTR)mir_alloc(MaxTextSize*sizeof(TCHAR)); + iRes = SendMessage(hTextWnd, WM_GETTEXT, (WPARAM)MaxTextSize, (LPARAM)ptszInText); + if (!IsBadStringPtr(ptszInText, MaxTextSize) && (iRes > 0)) { + crTemp.cpMin = 0; + crTemp.cpMax = (int)_tcslen(ptszInText); + } + else { + SendMessage(hTextWnd, EM_EXSETSEL, 0, (LPARAM)&crSelection); + SendMessage(hTextWnd, WM_SETREDRAW, TRUE, 0); + InvalidateRect(hTextWnd, NULL, FALSE); + return 1; + } + } + // Получаем текущее слово + if (CurrentWord) { + for (dwStartWord = crSelection.cpMin; (dwStartWord > 0) && (_tcschr(ptszSeparators, ptszInText[dwStartWord - 1]) == NULL); dwStartWord--); + for (dwEndWord = crSelection.cpMin; (dwEndWord < (_tcslen(ptszInText))) && (_tcschr(ptszSeparators, ptszInText[dwEndWord]) == NULL); dwEndWord++); - //Получим текст из Рича или обычного Едита - if ((WindowType == WTYPE_RichEdit)||(WindowType == WTYPE_Edit)) - { - dwStartWord = dwEndWord = -1; - SendMessage(hTextWnd, WM_SETREDRAW, FALSE, 0); + crTemp.cpMin = dwStartWord; + crTemp.cpMax = dwEndWord; - //Бэкап выделения - crTemp = crSelection; - - // Если имеется выделенный текст, то получим его - if(crSelection.cpMin != crSelection.cpMax) - { - if (WindowType == WTYPE_RichEdit) - { - ZeroMemory(&esdData, sizeof(esdData)); - - if (SendMessage(hTextWnd, EM_STREAMOUT, SF_TEXT|SF_UNICODE|SFF_SELECTION, (LPARAM)&esStream)>0) - - ptszInText = GeTStringFromStreamData(&esdData); - else - { - SendMessage(hTextWnd, EM_EXSETSEL, 0, (LPARAM)&crSelection); - SendMessage(hTextWnd, WM_SETREDRAW, TRUE, 0); - InvalidateRect(hTextWnd, NULL, FALSE); - return 1; - } - } - if (WindowType == WTYPE_Edit) - { - ptszTemp = (LPTSTR)mir_alloc(MaxTextSize*sizeof(TCHAR)); - ptszInText = (LPTSTR)mir_alloc(MaxTextSize*sizeof(TCHAR)); - iRes = SendMessage(hTextWnd, WM_GETTEXT, (WPARAM)MaxTextSize, (LPARAM)ptszTemp); - if (!IsBadStringPtr(ptszInText, MaxTextSize) && (iRes>0)) - { - _tcsncpy(ptszInText, &ptszTemp[crSelection.cpMin], crSelection.cpMax-crSelection.cpMin); - ptszInText[crSelection.cpMax-crSelection.cpMin] = 0; - mir_free(ptszTemp); - } - else - { + if (WindowType == WTYPE_RichEdit) { + SendMessage(hTextWnd, EM_EXSETSEL, 0, (LPARAM)&crTemp); + + EditStreamData esdData; + EDITSTREAM esStream = { 0 }; + esStream.dwCookie = (DWORD)&esdData; + esStream.pfnCallback = EditStreamOutRtf; + if (SendMessage(hTextWnd, EM_STREAMOUT, SF_TEXT | SF_UNICODE | SFF_SELECTION, (LPARAM)&esStream) != 0) + ptszInText = GeTStringFromStreamData(&esdData); + else { SendMessage(hTextWnd, EM_EXSETSEL, 0, (LPARAM)&crSelection); SendMessage(hTextWnd, WM_SETREDRAW, TRUE, 0); InvalidateRect(hTextWnd, NULL, FALSE); return 1; } } - } - // Если выделения нет, то получим нужный текст - else - { - // Получаем весь текст в поле - if (WindowType == WTYPE_RichEdit) - { - ZeroMemory(&esdData, sizeof(esdData)); - crTemp.cpMin = 0; - crTemp.cpMax = -1; - SendMessage(hTextWnd, EM_EXSETSEL, 0, (LPARAM)&crTemp); - - if (SendMessage(hTextWnd, EM_STREAMOUT, SF_TEXT|SF_UNICODE|SFF_SELECTION, (LPARAM)&esStream) != 0) - - ptszInText = GeTStringFromStreamData(&esdData); - else - { - SendMessage(hTextWnd, EM_EXSETSEL, 0, (LPARAM)&crSelection); - SendMessage(hTextWnd, WM_SETREDRAW, TRUE, 0); - InvalidateRect(hTextWnd, NULL, FALSE); - return 1; - } - } - if (WindowType == WTYPE_Edit) - { - ptszInText = (LPTSTR)mir_alloc(MaxTextSize*sizeof(TCHAR)); - iRes = SendMessage(hTextWnd, WM_GETTEXT, (WPARAM)MaxTextSize, (LPARAM)ptszInText); - - if (!IsBadStringPtr(ptszInText, MaxTextSize) && (iRes>0)) - { - crTemp.cpMin = 0; - crTemp.cpMax = (int)_tcslen(ptszInText); - } - else - { + + if (WindowType == WTYPE_Edit) { + ptszTemp = (LPTSTR)mir_alloc(MaxTextSize*sizeof(TCHAR)); + _tcsncpy(ptszTemp, &ptszInText[crTemp.cpMin], crTemp.cpMax - crTemp.cpMin); + ptszTemp[crTemp.cpMax - crTemp.cpMin] = 0; + _tcscpy(ptszInText, ptszTemp); + mir_free(ptszTemp); + if (_tcslen(ptszInText) == 0) { + mir_free(ptszInText); SendMessage(hTextWnd, EM_EXSETSEL, 0, (LPARAM)&crSelection); SendMessage(hTextWnd, WM_SETREDRAW, TRUE, 0); InvalidateRect(hTextWnd, NULL, FALSE); return 1; } } - // Получаем текущее слово - if (CurrentWord) - { - for (dwStartWord = crSelection.cpMin;(dwStartWord>0) && (_tcschr(ptszSeparators, ptszInText[dwStartWord-1]) == NULL);dwStartWord--); - for (dwEndWord = crSelection.cpMin;(dwEndWord<(_tcslen(ptszInText))) && (_tcschr(ptszSeparators, ptszInText[dwEndWord]) == NULL);dwEndWord++); - - crTemp.cpMin = dwStartWord; - crTemp.cpMax = dwEndWord; - - if (WindowType == WTYPE_RichEdit) - { - SendMessage(hTextWnd, EM_EXSETSEL, 0, (LPARAM)&crTemp); - ZeroMemory(&esdData, sizeof(esdData)); - - if (SendMessage(hTextWnd, EM_STREAMOUT, SF_TEXT|SF_UNICODE|SFF_SELECTION, (LPARAM)&esStream) != 0) - - ptszInText = GeTStringFromStreamData(&esdData); - else - { - SendMessage(hTextWnd, EM_EXSETSEL, 0, (LPARAM)&crSelection); - SendMessage(hTextWnd, WM_SETREDRAW, TRUE, 0); - InvalidateRect(hTextWnd, NULL, FALSE); - return 1; - } - - } + } + } + } - if (WindowType == WTYPE_Edit) - { - ptszTemp = (LPTSTR)mir_alloc(MaxTextSize*sizeof(TCHAR)); - _tcsncpy(ptszTemp, &ptszInText[crTemp.cpMin], crTemp.cpMax-crTemp.cpMin); - ptszTemp[crTemp.cpMax-crTemp.cpMin] = 0; - _tcscpy(ptszInText, ptszTemp); - mir_free(ptszTemp); - if (_tcslen(ptszInText) == 0) - { - mir_free(ptszInText); - SendMessage(hTextWnd, EM_EXSETSEL, 0, (LPARAM)&crSelection); - SendMessage(hTextWnd, WM_SETREDRAW, TRUE, 0); - InvalidateRect(hTextWnd, NULL, FALSE); - return 1; - } - } + //---------------Выдаем результаты-------------------- + WindowIsReadOnly = FALSE; + if (WindowType == WTYPE_IEView || WindowType == WTYPE_HistoryPP) + WindowIsReadOnly = TRUE; + + // if ((SendMessage(hTextWnd, EM_GETOPTIONS, 0, 0)&ECO_READONLY)) + if (WindowType == WTYPE_RichEdit || WindowType == WTYPE_Edit) + if (GetWindowLongPtr(hTextWnd, GWL_STYLE) & ES_READONLY) + WindowIsReadOnly = TRUE; + + // Лог Иевью и ХисториПП в режиме эмуляции Иевью и поля только для чтения. + if (WindowType != WTYPE_Unknown && !IsBadStringPtr(ptszInText, MaxTextSize)) + if (WindowIsReadOnly) { + ptszMBox = (LPTSTR)mir_alloc(MaxTextSize*sizeof(TCHAR)); + ptszMBox[0] = 0; + + if (TextOperation == TOT_Layout) { + hklCurLay = GetLayoutOfText(ptszInText); + hklToLay = GetNextLayout(hklCurLay); + TwoWay = (moOptions.TwoWay) && (bLayNum == 2); + + if (bLayNum == 2) { + ptszMBox = ChangeTextLayout(ptszInText, hklCurLay, hklToLay, TwoWay); + } + else { + for (i = 0; i < bLayNum; i++) + if (hklLayouts[i] != hklCurLay) { + if (_tcslen(ptszMBox) != 0) + _tcscat(ptszMBox, _T("\n\n")); + ptszTemp = GetShortNameOfLayout(hklLayouts[i]); + _tcscat(ptszMBox, ptszTemp); + _tcscat(ptszMBox, _T(":\n")); + ptszOutText = ChangeTextLayout(ptszInText, hklCurLay, hklLayouts[i], FALSE); + _tcscat(ptszMBox, ptszOutText); + mir_free(ptszTemp); + mir_free(ptszOutText); } } } -// ---------------Выдаем результаты-------------------- - WindowIsReadOnly = FALSE; - if ((WindowType == WTYPE_IEView)||(WindowType == WTYPE_HistoryPP)) WindowIsReadOnly = TRUE; - - //if ((SendMessage(hTextWnd, EM_GETOPTIONS, 0, 0)&ECO_READONLY)) - if ((WindowType == WTYPE_RichEdit)||(WindowType == WTYPE_Edit)) - if (GetWindowLongPtr(hTextWnd, GWL_STYLE)&ES_READONLY) - WindowIsReadOnly = TRUE; - - // Лог Иевью и ХисториПП в режиме эмуляции Иевью и поля только для чтения. - if ((WindowType != WTYPE_Unknown) && (!IsBadStringPtr(ptszInText, MaxTextSize))) - if (WindowIsReadOnly) - { - ptszMBox = (LPTSTR)mir_alloc(MaxTextSize*sizeof(TCHAR)); - ptszMBox[0] = 0; - - if (TextOperation == TOT_Layout) - { - hklCurLay = GetLayoutOfText(ptszInText); - hklToLay = GetNextLayout(hklCurLay); - TwoWay = (moOptions.TwoWay) && (bLayNum == 2); - - if (bLayNum == 2) - { - ptszMBox = ChangeTextLayout(ptszInText, hklCurLay, hklToLay, TwoWay); - } - else - { - for (i = 0;i