//////////////////////////////////////////////////////////////////////////////////////// // Miranda NG: the free IM client for Microsoft* Windows* // // Copyright (C) 2012-22 Miranda NG team, // Copyright (c) 2000-09 Miranda ICQ/IM project, // all portions of this codebase are copyrighted to the people // listed in contributors.txt. // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License // as published by the Free Software Foundation; either version 2 // of the License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // you should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. // // part of tabSRMM messaging plugin for Miranda. // // (C) 2005-2010 by silvercircle _at_ gmail _dot_ com and contributors // // generic utility functions #include "stdafx.h" #define MWF_LOG_TEXTFORMAT 0x2000000 #define MSGDLGFONTCOUNT 22 OBJLIST Utils::rtf_clrs(10); MWindowList CWarning::hWindowList = nullptr; ///////////////////////////////////////////////////////////////////////////////////////// char* Utils::FilterEventMarkers(char *szText) { for (char *p = strstr(szText, "~-+"); p != nullptr; p = strstr(p, "~-+")) { char *pEnd = strstr(p + 3, "+-~"); if (pEnd == nullptr) break; strdel(p, (pEnd - p) + 3); } return szText; } wchar_t* Utils::FilterEventMarkers(wchar_t *wszText) { for (wchar_t *p = wcsstr(wszText, L"~-+"); p != nullptr; p = wcsstr(p, L"~-+")) { wchar_t *pEnd = wcsstr(p + 3, L"+-~"); if (pEnd == nullptr) break; strdelw(p, (pEnd - p) + 3); } return wszText; } ///////////////////////////////////////////////////////////////////////////////////////// void Utils::DoubleAmpersands(wchar_t *pszText, size_t len) { CMStringW text(pszText); text.Replace(L"&", L"&&"); mir_wstrncpy(pszText, text.c_str(), len); } ///////////////////////////////////////////////////////////////////////////////////////// // Get a preview of the text with an ellipsis appended(...) // // @param szText source text // @param iMaxLen max length of the preview // @return wchar_t* result (caller must mir_free() it) wchar_t* Utils::GetPreviewWithEllipsis(wchar_t *szText, size_t iMaxLen) { size_t uRequired; wchar_t *p = nullptr, cSaved; bool fEllipsis = false; if (mir_wstrlen(szText) <= iMaxLen) { uRequired = mir_wstrlen(szText) + 4; cSaved = 0; } else { p = &szText[iMaxLen - 1]; fEllipsis = true; while (p >= szText && *p != ' ') p--; if (p == szText) p = szText + iMaxLen - 1; cSaved = *p; *p = 0; uRequired = (p - szText) + 6; } wchar_t *szResult = reinterpret_cast(mir_alloc((uRequired + 1) * sizeof(wchar_t))); mir_snwprintf(szResult, (uRequired + 1), fEllipsis ? L"%s..." : L"%s", szText); if (p) *p = cSaved; return szResult; } ///////////////////////////////////////////////////////////////////////////////////////// // init default color table. the table may grow when using custom colors via bbcodes static const wchar_t *sttColorNames[] = { L"black", L"", L"", L"", L"", L"", L"", L"", L"", L"blue", L"cyan", L"magenta", L"green", L"yellow", L"red", L"white" }; void Utils::RTF_CTableInit() { int iTableSize; COLORREF *pTable = Srmm_GetColorTable(&iTableSize); for (int i = 0; i < iTableSize; i++) rtf_clrs.insert(new TRTFColorTable(sttColorNames[i], pTable[i])); } ///////////////////////////////////////////////////////////////////////////////////////// // add a color to the global rtf color table void Utils::RTF_ColorAdd(const wchar_t *tszColname) { wchar_t *stopped; COLORREF clr = wcstol(tszColname, &stopped, 16); rtf_clrs.insert(new TRTFColorTable(tszColname, RGB(GetBValue(clr), GetGValue(clr), GetRValue(clr)))); } ///////////////////////////////////////////////////////////////////////////////////////// // generic error popup dialog procedure LRESULT CALLBACK Utils::PopupDlgProcError(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { MCONTACT hContact = PUGetContact(hWnd); switch (message) { case WM_COMMAND: case WM_CONTEXTMENU: PostMessage(PluginConfig.g_hwndHotkeyHandler, DM_HANDLECLISTEVENT, hContact, 0); PUDeletePopup(hWnd); break; case WM_MOUSEWHEEL: break; case WM_SETCURSOR: break; } return DefWindowProc(hWnd, message, wParam, lParam); } ///////////////////////////////////////////////////////////////////////////////////////// // read a blob from db into the container settings structure // @param hContact: contact handle (0 = read global) // @param cs TContainerSettings* target structure // @return 0 on success, 1 failure (blob does not exist OR is not a valid private setting structure struct TOldContainerSettings { BOOL fPrivate; uint32_t dwFlags; uint32_t dwFlagsEx; uint32_t dwTransparency; uint32_t panelheight; int iSplitterY; wchar_t szTitleFormat[32]; uint16_t avatarMode; uint16_t ownAvatarMode; uint16_t autoCloseSeconds; uint8_t reserved[10]; }; int Utils::ReadContainerSettingsFromDB(const MCONTACT hContact, TContainerSettings *cs, const char *szKey) { CMStringA szSetting(szKey ? szKey : CNT_KEYNAME); int iSplitterX = db_get_dw(0, SRMSGMOD_T, szSetting + "_SplitterX", -1); if (iSplitterX == -1) { // nothing? try the old format DBVARIANT dbv = { 0 }; if (0 == db_get(hContact, SRMSGMOD_T, szSetting + "_Blob", &dbv)) { TOldContainerSettings oldBin = {}; if (dbv.type == DBVT_BLOB && dbv.cpbVal > 0 && dbv.cpbVal <= sizeof(oldBin)) { ::memcpy(&oldBin, (void*)dbv.pbVal, dbv.cpbVal); cs->flags.dw = oldBin.dwFlags; cs->flagsEx.dw = oldBin.dwFlagsEx; cs->dwTransparency = oldBin.dwTransparency; cs->panelheight = oldBin.panelheight; cs->iSplitterY = oldBin.iSplitterY; cs->iSplitterX = 35; wcsncpy_s(cs->szTitleFormat, oldBin.szTitleFormat, _TRUNCATE); cs->avatarMode = oldBin.avatarMode; cs->ownAvatarMode = oldBin.ownAvatarMode; cs->autoCloseSeconds = oldBin.autoCloseSeconds; cs->fPrivate = oldBin.fPrivate != 0; Utils::WriteContainerSettingsToDB(hContact, cs, szKey); db_unset(hContact, SRMSGMOD_T, szSetting); ::db_free(&dbv); return 0; } } cs->fPrivate = false; db_free(&dbv); return 1; } cs->flags.dw = db_get_dw(hContact, SRMSGMOD_T, szSetting + "_Flags", 0); cs->flagsEx.dw = db_get_dw(hContact, SRMSGMOD_T, szSetting + "_FlagsEx", 0); cs->dwTransparency = db_get_dw(hContact, SRMSGMOD_T, szSetting + "_Transparency", 0); cs->panelheight = db_get_dw(hContact, SRMSGMOD_T, szSetting + "_PanelY", 0); cs->iSplitterX = iSplitterX; cs->iSplitterY = db_get_dw(hContact, SRMSGMOD_T, szSetting + "_SplitterY", 0); cs->avatarMode = db_get_w(hContact, SRMSGMOD_T, szSetting + "_AvatarMode", 0); cs->ownAvatarMode = db_get_w(hContact, SRMSGMOD_T, szSetting + "_OwnAvatarMode", 0); cs->autoCloseSeconds = db_get_w(hContact, SRMSGMOD_T, szSetting + "_AutoCloseSecs", 0); cs->fPrivate = db_get_b(hContact, SRMSGMOD_T, szSetting + "_Private", 0) != 0; db_get_wstatic(hContact, SRMSGMOD_T, szSetting + "_Format", cs->szTitleFormat, _countof(cs->szTitleFormat)); return 0; } int Utils::WriteContainerSettingsToDB(const MCONTACT hContact, TContainerSettings *cs, const char *szKey) { CMStringA szSetting(szKey ? szKey : CNT_KEYNAME); db_set_dw(hContact, SRMSGMOD_T, szSetting + "_Flags", cs->flags.dw); db_set_dw(hContact, SRMSGMOD_T, szSetting + "_FlagsEx", cs->flagsEx.dw); db_set_dw(hContact, SRMSGMOD_T, szSetting + "_Transparency", cs->dwTransparency); db_set_dw(hContact, SRMSGMOD_T, szSetting + "_PanelY", cs->panelheight); db_set_dw(hContact, SRMSGMOD_T, szSetting + "_SplitterX", cs->iSplitterX); db_set_dw(hContact, SRMSGMOD_T, szSetting + "_SplitterY", cs->iSplitterY); db_set_ws(hContact, SRMSGMOD_T, szSetting + "_Format", cs->szTitleFormat); db_set_w(hContact, SRMSGMOD_T, szSetting + "_AvatarMode", cs->avatarMode); db_set_w(hContact, SRMSGMOD_T, szSetting + "_OwnAvatarMode", cs->ownAvatarMode); db_set_w(hContact, SRMSGMOD_T, szSetting + "_AutoCloseSecs", cs->autoCloseSeconds); db_set_b(hContact, SRMSGMOD_T, szSetting + "_Private", cs->fPrivate); return 0; } ///////////////////////////////////////////////////////////////////////////////////////// // calculate new width and height values for a user picture (avatar) // // @param: maxHeight - determines maximum height for the picture, width will // be scaled accordingly. void Utils::scaleAvatarHeightLimited(const HBITMAP hBm, double& dNewWidth, double& dNewHeight, LONG maxHeight) { BITMAP bm; double dAspect; GetObject(hBm, sizeof(bm), &bm); if (bm.bmHeight > bm.bmWidth) { if (bm.bmHeight > 0) dAspect = (double)(maxHeight) / (double)bm.bmHeight; else dAspect = 1.0; dNewWidth = (double)bm.bmWidth * dAspect; dNewHeight = (double)maxHeight; } else { if (bm.bmWidth > 0) dAspect = (double)(maxHeight) / (double)bm.bmWidth; else dAspect = 1.0; dNewHeight = (double)bm.bmHeight * dAspect; dNewWidth = (double)maxHeight; } } AVATARCACHEENTRY* Utils::loadAvatarFromAVS(const MCONTACT hContact) { if (!ServiceExists(MS_AV_GETAVATARBITMAP)) return nullptr; return (AVATARCACHEENTRY*)CallService(MS_AV_GETAVATARBITMAP, hContact, 0); } void Utils::sendContactMessage(MCONTACT hContact, UINT uMsg, WPARAM wParam, LPARAM lParam) { HWND h = Srmm_FindWindow(hContact); if (h != nullptr) PostMessage(h, uMsg, wParam, lParam); } void Utils::getIconSize(HICON hIcon, int& sizeX, int& sizeY) { ICONINFO ii; BITMAP bm; ::GetIconInfo(hIcon, &ii); ::GetObject(ii.hbmColor, sizeof(bm), &bm); sizeX = bm.bmWidth; sizeY = bm.bmHeight; ::DeleteObject(ii.hbmMask); ::DeleteObject(ii.hbmColor); } ///////////////////////////////////////////////////////////////////////////////////////// // add a menu item to a ownerdrawn menu. mii must be pre-initialized // // @param m menu handle // @param mii menu item info structure // @param hIcon the icon (0 allowed -> no icon) // @param szText menu item text (must NOT be 0) // @param uID the item command id // @param pos zero-based position index void Utils::addMenuItem(const HMENU& m, MENUITEMINFO &mii, HICON hIcon, const wchar_t *szText, UINT uID, UINT pos) { mii.wID = uID; mii.dwItemData = (ULONG_PTR)hIcon; mii.dwTypeData = const_cast(szText); mii.cch = (int)mir_wstrlen(mii.dwTypeData) + 1; ::InsertMenuItem(m, pos, TRUE, &mii); } ///////////////////////////////////////////////////////////////////////////////////////// // enable or disable a dialog control void Utils::enableDlgControl(const HWND hwnd, UINT id, bool fEnable) { ::EnableWindow(::GetDlgItem(hwnd, id), fEnable); } ///////////////////////////////////////////////////////////////////////////////////////// // show or hide a dialog control void Utils::showDlgControl(const HWND hwnd, UINT id, int showCmd) { ::ShowWindow(::GetDlgItem(hwnd, id), showCmd); } ///////////////////////////////////////////////////////////////////////////////////////// // stream function to write the contents of the message log to an rtf file uint32_t CALLBACK Utils::StreamOut(DWORD_PTR dwCookie, LPBYTE pbBuff, LONG cb, LONG * pcb) { wchar_t *szFilename = (wchar_t*)dwCookie; HANDLE hFile = CreateFile(szFilename, GENERIC_WRITE, FILE_SHARE_READ, nullptr, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); if (hFile != INVALID_HANDLE_VALUE) { SetFilePointer(hFile, 0, nullptr, FILE_END); FilterEventMarkers((char*)pbBuff); WriteFile(hFile, pbBuff, cb, (DWORD *)pcb, nullptr); *pcb = cb; CloseHandle(hFile); return 0; } return 1; } ///////////////////////////////////////////////////////////////////////////////////////// // extract a resource from the given module // tszPath must end with \ bool Utils::extractResource(const HMODULE h, const UINT uID, const wchar_t *tszName, const wchar_t *tszPath, const wchar_t *tszFilename, bool fForceOverwrite) { HRSRC hRes = FindResource(h, MAKEINTRESOURCE(uID), tszName); if (hRes) { HGLOBAL hResource = LoadResource(h, hRes); if (hResource) { char *pData = (char *)LockResource(hResource); DWORD dwSize = SizeofResource(g_plugin.getInst(), hRes), written = 0; wchar_t szFilename[MAX_PATH]; mir_snwprintf(szFilename, L"%s%s", tszPath, tszFilename); if (!fForceOverwrite) if (PathFileExists(szFilename)) return true; HANDLE hFile = CreateFile(szFilename, GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); if (hFile == INVALID_HANDLE_VALUE) return false; WriteFile(hFile, (void*)pData, dwSize, &written, nullptr); CloseHandle(hFile); } } return true; } ///////////////////////////////////////////////////////////////////////////////////////// // extract the clicked URL from a rich edit control. Return the URL as wchar_t* // caller MUST mir_free() the returned string // @param hwndRich - rich edit window handle // @return wchar_t* extracted URL wchar_t* Utils::extractURLFromRichEdit(const ENLINK* _e, const HWND hwndRich) { CHARRANGE sel = { 0 }; ::SendMessage(hwndRich, EM_EXGETSEL, 0, (LPARAM)&sel); if (sel.cpMin != sel.cpMax) return nullptr; TEXTRANGE tr; tr.chrg = _e->chrg; tr.lpstrText = (wchar_t*)mir_alloc(sizeof(wchar_t) * (tr.chrg.cpMax - tr.chrg.cpMin + 8)); ::SendMessage(hwndRich, EM_GETTEXTRANGE, 0, (LPARAM)&tr); if (wcschr(tr.lpstrText, '@') != nullptr && wcschr(tr.lpstrText, ':') == nullptr && wcschr(tr.lpstrText, '/') == nullptr) { mir_wstrncpy(tr.lpstrText, L"mailto:", 7); mir_wstrncpy(tr.lpstrText + 7, tr.lpstrText, tr.chrg.cpMax - tr.chrg.cpMin + 1); } return tr.lpstrText; } ///////////////////////////////////////////////////////////////////////////////////////// // filters out invalid characters from a string used as part of a file // or folder name. All invalid characters will be replaced by spaces. // // @param tszFilename - string to filter. void Utils::sanitizeFilename(wchar_t* tszFilename) { static wchar_t *forbiddenCharacters = L"%/\\':|\"<>?"; static size_t forbiddenCharactersLen = mir_wstrlen(forbiddenCharacters); for (size_t i = 0; i < forbiddenCharactersLen; i++) { wchar_t* szFound = nullptr; while ((szFound = wcschr(tszFilename, (int)forbiddenCharacters[i])) != nullptr) *szFound = ' '; } } ///////////////////////////////////////////////////////////////////////////////////////// // ensure that a path name ends on a trailing backslash // @param szPathname - pathname to check void Utils::ensureTralingBackslash(wchar_t *szPathname) { if (szPathname[mir_wstrlen(szPathname) - 1] != '\\') mir_wstrcat(szPathname, L"\\"); } ///////////////////////////////////////////////////////////////////////////////////////// // load a system library from the Windows system path and return its module // handle. // // return 0 and throw an exception if something goes wrong. HMODULE Utils::loadSystemLibrary(const wchar_t* szFilename) { wchar_t sysPathName[MAX_PATH + 2]; if (0 == ::GetSystemDirectoryW(sysPathName, MAX_PATH)) return nullptr; sysPathName[MAX_PATH - 1] = 0; if (mir_wstrlen(sysPathName) + mir_wstrlen(szFilename) >= MAX_PATH) return nullptr; mir_wstrcat(sysPathName, szFilename); HMODULE _h = LoadLibraryW(sysPathName); if (nullptr == _h) return nullptr; return _h; } ///////////////////////////////////////////////////////////////////////////////////////// // setting avatar's contact void Utils::setAvatarContact(HWND hWnd, MCONTACT hContact) { MCONTACT hSub = db_mc_getSrmmSub(hContact); SendMessage(hWnd, AVATAR_SETCONTACT, 0, (hSub) ? hSub : hContact); } ///////////////////////////////////////////////////////////////////////////////////////// // stub for copying data to clipboard size_t Utils::CopyToClipBoard(const wchar_t *str, const HWND hwndOwner) { if (!OpenClipboard(hwndOwner) || str == nullptr) return 0; size_t i = sizeof(wchar_t) * (mir_wstrlen(str) + 1); EmptyClipboard(); HGLOBAL hData = ::GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, i); memcpy((void*)GlobalLock(hData), str, i); GlobalUnlock(hData); SetClipboardData(CF_UNICODETEXT, hData); CloseClipboard(); return i; } ///////////////////////////////////////////////////////////////////////////////////////// HWND TSAPI GetTabWindow(HWND hwndTab, int i) { if (i < 0) return nullptr; TCITEM tci; tci.mask = TCIF_PARAM; return (TabCtrl_GetItem(hwndTab, i, &tci)) ? (HWND)tci.lParam : nullptr; } ///////////////////////////////////////////////////////////////////////////////////////// // file list handler int _DebugTraceW(const wchar_t *fmt, ...) { wchar_t debug[2048]; int ibsize = 2047; SYSTEMTIME st; va_list va; char tszTime[50]; va_start(va, fmt); GetLocalTime(&st); mir_snprintf(tszTime, "%02d.%02d.%04d - %02d:%02d:%02d.%04d: ", st.wDay, st.wMonth, st.wYear, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds); mir_vsnwprintf(debug, ibsize - 10, fmt, va); //#ifdef _DEBUG OutputDebugStringW(debug); //#else { char szLogFileName[MAX_PATH], szDataPath[MAX_PATH]; FILE *f; Profile_GetPathA(MAX_PATH, szDataPath); mir_snprintf(szLogFileName, "%s\\%s", szDataPath, "tabsrmm_debug.log"); f = fopen(szLogFileName, "a+"); if (f) { fputs(tszTime, f); fputs(T2Utf(debug), f); fputs("\n", f); fclose(f); } } //#endif return 0; } /* * output a notification message. * may accept a hContact to include the contacts nickname in the notification message... * the actual message is using printf() rules for formatting and passing the arguments... * * can display the message either as systray notification (baloon popup) or using the * popup plugin. */ int _DebugPopup(MCONTACT hContact, const wchar_t *fmt, ...) { va_list va; wchar_t debug[1024]; int ibsize = 1023; va_start(va, fmt); mir_vsnwprintf(debug, ibsize, fmt, va); wchar_t szTitle[128]; mir_snwprintf(szTitle, TranslateT("TabSRMM message (%s)"), (hContact != 0) ? Clist_GetContactDisplayName(hContact) : TranslateT("Global")); Clist_TrayNotifyW(nullptr, szTitle, debug, NIIF_INFO, 1000 * 4); return 0; } ///////////////////////////////////////////////////////////////////////////////////////// // implementation of the CWarning class // // IMPORTANT note to translators for translation of the warning dialogs: // // Make sure to NOT remove the pipe character ( | ) from the strings. This separates the // warning title from the actual warning text. // // Also, do NOT insert multiple | characters in the translated string. Not well-formatted // warnings cannot be translated and the plugin will show the untranslated versions. // // strings marked with a NOT TRANSLATABLE comment cannot be translated at all. This // will be used for important and critical error messages only. // // some strings are empty, this is intentional and used for error messages that share // the message with other possible error notifications (popups, tool tips etc.) // // Entries that do not use the LPGENW() macro are NOT TRANSLATABLE, so don't bother translating them. static wchar_t* warnings[] = { nullptr, LPGENW("Save file|Unable to save temporary file"), // WARN_SAVEFILE LPGENW("Edit user notes|You are editing the user notes. Click the button again or use the hotkey (default: Alt+N) to save the notes and return to normal messaging mode"), /* WARN_EDITUSERNOTES */ LPGENW("Missing component|The icon pack is missing. Please install it to the default icons folder.\n\nNo icons will be available"), /* WARN_ICONPACKMISSING */ LPGENW("Aero peek warning|You have enabled Aero Peek features and loaded a custom container window skin\n\nThis can result in minor visual anomalies in the live preview feature."), /* WARN_AEROPEEKSKIN */ LPGENW("File transfer problem|Sending the image by file transfer failed.\n\nPossible reasons: File transfers not supported, either you or the target contact is offline, or you are invisible and the target contact is not on your visibility list."), /* WARN_IMGSVC_MISSING */ LPGENW("Settings problem|The option \\b1 History -> Imitate IEView API\\b0 is enabled and the History++ plugin is active. This can cause problems when using IEView as message log viewer.\n\nShould I correct the option (a restart is required)?"), /* WARN_HPP_APICHECK */ LPGENW("Configuration issue|The unattended send feature is disabled. The \\b1 send later\\b0 and \\b1 send to multiple contacts\\b0 features depend on it.\n\nYou must enable it under \\b1Options -> Message sessions -> Advanced tweaks\\b0. Changing this option requires a restart."), /* WARN_NO_SENDLATER */ LPGENW("Closing Window|You are about to close a window with multiple tabs open.\n\nProceed?"), /* WARN_CLOSEWINDOW */ LPGENW("Closing options dialog|To reflect the changes done by importing a theme in the options dialog, the dialog must be closed after loading a theme \\b1 and unsaved changes might be lost\\b0 .\n\nDo you want to continue?"), /* WARN_OPTION_CLOSE */ LPGENW("Loading a theme|Loading a color and font theme can overwrite the settings defined by your skin.\n\nDo you want to continue?"), /* WARN_THEME_OVERWRITE */ }; CWarning::CWarning(const wchar_t *tszTitle, const wchar_t *tszText, const UINT uId, const uint32_t dwFlags) : m_szTitle(mir_wstrdup(tszTitle)), m_szText(mir_wstrdup(tszText)) { m_uId = uId; m_hFontCaption = nullptr; m_dwFlags = dwFlags; m_fIsModal = ((m_dwFlags & MB_YESNO || m_dwFlags & MB_YESNOCANCEL) ? true : false); } CWarning::~CWarning() { if (m_hFontCaption) ::DeleteObject(m_hFontCaption); } LRESULT CWarning::ShowDialog() const { if (!m_fIsModal) { ::CreateDialogParam(g_plugin.getInst(), MAKEINTRESOURCE(IDD_WARNING), nullptr, stubDlgProc, LPARAM(this)); return 0; } return ::DialogBoxParam(g_plugin.getInst(), MAKEINTRESOURCE(IDD_WARNING), nullptr, stubDlgProc, LPARAM(this)); } ///////////////////////////////////////////////////////////////////////////////////////// // send cancel message to all open warning dialogs so they are destroyed // before TabSRMM is unloaded. // // called by the OkToExit handler in globals.cpp void CWarning::destroyAll() { if (hWindowList) WindowList_Broadcast(hWindowList, WM_COMMAND, MAKEWPARAM(IDCANCEL, 0), 0); } ///////////////////////////////////////////////////////////////////////////////////////// // show a CWarning dialog using the id value. Check whether the user has chosen to // not show this message again. This has room for 64 different warning dialogs, which // should be enough in the first place. Extending it should not be too hard though. LRESULT CWarning::show(const int uId, uint32_t dwFlags, const wchar_t* tszTxt) { wchar_t* separator_pos = nullptr; if (nullptr == hWindowList) hWindowList = WindowList_Create(); // don't open new warnings when shutdown was initiated (modal ones will otherwise // block the shutdown) if (CMimAPI::m_shutDown) return -1; wchar_t *_s = nullptr; if (tszTxt) _s = const_cast(tszTxt); else { if (uId == -1) return -1; if (dwFlags & CWF_UNTRANSLATED) _s = TranslateW(warnings[uId]); else { // revert to untranslated warning when the translated message // is not well-formatted. _s = TranslateW(warnings[uId]); if (mir_wstrlen(_s) < 3 || nullptr == wcschr(_s, '|')) _s = TranslateW(warnings[uId]); } } if ((mir_wstrlen(_s) > 3) && ((separator_pos = wcschr(_s, '|')) != nullptr)) { if (uId >= 0 && !(dwFlags & CWF_NOALLOWHIDE)) { uint32_t val = M.GetDword("cWarningsL", 0); uint32_t mask = ((__int64)1L) << uId; if (mask & val) { bool bResult = (M.GetDword("cWarningsV", 0) & mask) != 0; if (dwFlags & MB_YESNO || dwFlags & MB_YESNOCANCEL) return (bResult) ? IDYES : IDNO; return IDOK; } } ptrW s(mir_wstrdup(_s)); separator_pos = wcschr(s, '|'); if (separator_pos) { *separator_pos = 0; CWarning *w = new CWarning(s, separator_pos + 1, uId, dwFlags); if (dwFlags & MB_YESNO || dwFlags & MB_YESNOCANCEL) return w->ShowDialog(); w->ShowDialog(); } } return -1; } ///////////////////////////////////////////////////////////////////////////////////////// // stub dlg procedure.Just register the object pointer in WM_INITDIALOG INT_PTR CALLBACK CWarning::stubDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { CWarning *w = reinterpret_cast(::GetWindowLongPtr(hwnd, GWLP_USERDATA)); if (w) return(w->dlgProc(hwnd, msg, wParam, lParam)); switch (msg) { case WM_INITDIALOG: w = reinterpret_cast(lParam); if (w) { ::SetWindowLongPtr(hwnd, GWLP_USERDATA, lParam); return(w->dlgProc(hwnd, msg, wParam, lParam)); } break; } return FALSE; } ///////////////////////////////////////////////////////////////////////////////////////// // dialog procedure for the warning dialog box INT_PTR CALLBACK CWarning::dlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { case WM_INITDIALOG: m_hwnd = hwnd; ::SetWindowTextW(hwnd, TranslateT("TabSRMM warning message")); ::Window_SetSkinIcon_IcoLib(hwnd, SKINICON_OTHER_MIRANDA); ::SendDlgItemMessage(hwnd, IDC_WARNTEXT, EM_AUTOURLDETECT, TRUE, 0); ::SendDlgItemMessage(hwnd, IDC_WARNTEXT, EM_SETEVENTMASK, 0, ENM_LINK); TranslateDialogDefault(hwnd); { CMStringW str(FORMAT, RTF_DEFAULT_HEADER, 0, 0, 0, 30 * 15); str.Append(m_szText); str.Append(L"}"); str.Replace(L"\n", L"\\line "); SETTEXTEX stx = { ST_SELECTION, CP_UTF8 }; ::SendDlgItemMessage(hwnd, IDC_WARNTEXT, EM_SETTEXTEX, (WPARAM)&stx, T2Utf(str)); ::SetDlgItemTextW(hwnd, IDC_CAPTION, m_szTitle); if (m_dwFlags & CWF_NOALLOWHIDE) Utils::showDlgControl(hwnd, IDC_DONTSHOWAGAIN, SW_HIDE); if (m_dwFlags & MB_YESNO || m_dwFlags & MB_YESNOCANCEL) { Utils::showDlgControl(hwnd, IDOK, SW_HIDE); ::SetFocus(::GetDlgItem(hwnd, IDCANCEL)); } else { Utils::showDlgControl(hwnd, IDCANCEL, SW_HIDE); Utils::showDlgControl(hwnd, IDYES, SW_HIDE); Utils::showDlgControl(hwnd, IDNO, SW_HIDE); ::SetFocus(::GetDlgItem(hwnd, IDOK)); } UINT uResId = 0; if (m_dwFlags & MB_ICONERROR || m_dwFlags & MB_ICONHAND) uResId = 32513; else if (m_dwFlags & MB_ICONEXCLAMATION || m_dwFlags & MB_ICONWARNING) uResId = 32515; else if (m_dwFlags & MB_ICONASTERISK || m_dwFlags & MB_ICONINFORMATION) uResId = 32516; else if (m_dwFlags & MB_ICONQUESTION) uResId = 32514; HICON hIcon; if (uResId) hIcon = reinterpret_cast(::LoadImage(nullptr, MAKEINTRESOURCE(uResId), IMAGE_ICON, 0, 0, LR_SHARED | LR_DEFAULTSIZE)); else hIcon = ::Skin_LoadIcon(SKINICON_EVENT_MESSAGE, true); ::SendDlgItemMessageW(hwnd, IDC_WARNICON, STM_SETICON, reinterpret_cast(hIcon), 0); if (!(m_dwFlags & MB_YESNO || m_dwFlags & MB_YESNOCANCEL)) ::ShowWindow(hwnd, SW_SHOWNORMAL); WindowList_Add(hWindowList, hwnd, (UINT_PTR)hwnd); } return TRUE; case WM_CTLCOLORSTATIC: { HWND hwndChild = reinterpret_cast(lParam); UINT id = ::GetDlgCtrlID(hwndChild); if (nullptr == m_hFontCaption) { HFONT hFont = reinterpret_cast(::SendDlgItemMessage(hwnd, IDC_CAPTION, WM_GETFONT, 0, 0)); LOGFONT lf = { 0 }; ::GetObject(hFont, sizeof(lf), &lf); lf.lfHeight = (int)((double)lf.lfHeight * 1.7f); m_hFontCaption = ::CreateFontIndirect(&lf); ::SendDlgItemMessage(hwnd, IDC_CAPTION, WM_SETFONT, (WPARAM)m_hFontCaption, FALSE); } if (IDC_CAPTION == id) { ::SetTextColor(reinterpret_cast(wParam), ::GetSysColor(COLOR_HIGHLIGHT)); ::SendMessage(hwndChild, WM_SETFONT, (WPARAM)m_hFontCaption, FALSE); } if (IDC_WARNGROUP != id && IDC_DONTSHOWAGAIN != id) { ::SetBkColor((HDC)wParam, ::GetSysColor(COLOR_WINDOW)); return reinterpret_cast(::GetSysColorBrush(COLOR_WINDOW)); } } break; case WM_COMMAND: switch (LOWORD(wParam)) { case IDOK: case IDYES: case IDNO: if (::IsDlgButtonChecked(hwnd, IDC_DONTSHOWAGAIN)) { uint32_t newVal = M.GetDword("cWarningsL", 0) | ((uint32_t)1L << m_uId); db_set_dw(0, SRMSGMOD_T, "cWarningsL", newVal); if (LOWORD(wParam) != IDNO) { newVal = M.GetDword("cWarningsV", 0) | ((uint32_t)1L << m_uId); db_set_dw(0, SRMSGMOD_T, "cWarningsV", newVal); } } __fallthrough; case IDCANCEL: if (!m_fIsModal && (IDOK == LOWORD(wParam) || IDCANCEL == LOWORD(wParam))) // modeless dialogs can receive a IDCANCEL from destroyAll() ::DestroyWindow(hwnd); else ::EndDialog(hwnd, LOWORD(wParam)); break; } break; case WM_NOTIFY: switch (((NMHDR*)lParam)->code) { case EN_LINK: switch (((ENLINK*)lParam)->msg) { case WM_LBUTTONUP: ENLINK *e = reinterpret_cast(lParam); const wchar_t *wszUrl = Utils::extractURLFromRichEdit(e, ::GetDlgItem(hwnd, IDC_WARNTEXT)); if (wszUrl) { Utils_OpenUrlW(wszUrl); mir_free(const_cast(wszUrl)); } } } break; case WM_DESTROY: ::SetWindowLongPtr(hwnd, GWLP_USERDATA, 0); delete this; WindowList_Remove(hWindowList, hwnd); Window_FreeIcon_IcoLib(hwnd); break; } return FALSE; }