HICON LoadLocalizedIcon(const UINT iconId) { LANGID langId = GetGUILanguage(); /* find group icon resource */ HRSRC res = FindResourceLang(RT_GROUP_ICON, MAKEINTRESOURCE(iconId), langId); if (res == NULL) return NULL; HGLOBAL resInfo = LoadResource(o.hInstance, res); if (resInfo == NULL) return NULL; int id = LookupIconIdFromDirectory(resInfo, TRUE); if (id == 0) return NULL; /* find the actual icon */ res = FindResourceLang(RT_ICON, MAKEINTRESOURCE(id), langId); if (res == NULL) return NULL; resInfo = LoadResource(o.hInstance, res); if (resInfo == NULL) return NULL; DWORD resSize = SizeofResource(o.hInstance, res); if (resSize == 0) return NULL; return CreateIconFromResource(resInfo, resSize, TRUE, 0x30000); }
static PTSTR __LoadLocalizedString(const UINT stringId, va_list args) { static TCHAR msg[512]; msg[0] = 0; LoadStringLang(stringId, GetGUILanguage(), msg, _countof(msg), args); return msg; }
int LoadLocalizedStringBuf(PTSTR buffer, int bufferSize, const UINT stringId, ...) { va_list args; va_start(args, stringId); int len = LoadStringLang(stringId, GetGUILanguage(), buffer, bufferSize, args); va_end(args); return len; }
LPCDLGTEMPLATE LocalizedDialogResource(const UINT dialogId) { /* find dialog resource */ HRSRC res = FindResourceLang(RT_DIALOG, MAKEINTRESOURCE(dialogId), GetGUILanguage()); if (res == NULL) return NULL; return LoadResource(o.hInstance, res); }
HWND CreateLocalizedDialogParam(const UINT dialogId, DLGPROC dialogFunc, const LPARAM param) { /* find dialog resource */ HRSRC res = FindResourceLang(RT_DIALOG, MAKEINTRESOURCE(dialogId), GetGUILanguage()); if (res == NULL) return NULL; HGLOBAL resInfo = LoadResource(o.hInstance, res); if (resInfo == NULL) return NULL; return CreateDialogIndirectParam(o.hInstance, resInfo, NULL, dialogFunc, param); }
INT_PTR CALLBACK GeneralSettingsDlgProc(HWND hwndDlg, UINT msg, UNUSED WPARAM wParam, LPARAM lParam) { LPPSHNOTIFY psn; langProcData langData = { .languages = GetDlgItem(hwndDlg, ID_CMB_LANGUAGE), .language = GetGUILanguage() }; switch(msg) { case WM_INITDIALOG: /* Populate UI language selection combo box */ EnumResourceLanguages( NULL, RT_STRING, MAKEINTRESOURCE(IDS_LANGUAGE_NAME / 16 + 1), (ENUMRESLANGPROC) FillLangListProc, (LONG_PTR) &langData ); /* If none of the available languages matched, select the fallback */ if (ComboBox_GetCurSel(langData.languages) == CB_ERR) ComboBox_SelectString(langData.languages, -1, LangListEntry(IDS_LANGUAGE_NAME, fallbackLangId)); /* Clear language id data for the selected item */ ComboBox_SetItemData(langData.languages, ComboBox_GetCurSel(langData.languages), 0); if (GetLaunchOnStartup()) Button_SetCheck(GetDlgItem(hwndDlg, ID_CHK_STARTUP), BST_CHECKED); break; case WM_NOTIFY: psn = (LPPSHNOTIFY) lParam; if (psn->hdr.code == (UINT) PSN_APPLY) { LANGID langId = (LANGID) ComboBox_GetItemData(langData.languages, ComboBox_GetCurSel(langData.languages)); if (langId != 0) SetGUILanguage(langId); SetLaunchOnStartup(Button_GetCheck(GetDlgItem(hwndDlg, ID_CHK_STARTUP)) == BST_CHECKED); SetWindowLongPtr(hwndDlg, DWLP_MSGRESULT, PSNRET_NOERROR); return TRUE; } break; } return FALSE; }
static HICON LoadLocalizedIconEx(const UINT iconId, int cxDesired, int cyDesired) { LANGID langId = GetGUILanguage(); HICON hIcon = (HICON) LoadImage (o.hInstance, MAKEINTRESOURCE(iconId), IMAGE_ICON, cxDesired, cyDesired, LR_DEFAULTSIZE|LR_SHARED); if (hIcon) return hIcon; else PrintDebug (L"Loading icon using LoadImage failed."); /* Fallback to CreateIconFromResource which always scales * from the first image in the resource */ /* find group icon resource */ HRSRC res = FindResourceLang(RT_GROUP_ICON, MAKEINTRESOURCE(iconId), langId); if (res == NULL) return NULL; HGLOBAL resInfo = LoadResource(o.hInstance, res); if (resInfo == NULL) return NULL; int id = LookupIconIdFromDirectory(resInfo, TRUE); if (id == 0) return NULL; /* find the actual icon */ res = FindResourceLang(RT_ICON, MAKEINTRESOURCE(id), langId); if (res == NULL) return NULL; resInfo = LoadResource(o.hInstance, res); if (resInfo == NULL) return NULL; DWORD resSize = SizeofResource(o.hInstance, res); if (resSize == 0) return NULL; /* Note: this uses the first icon in the resource and scales it */ hIcon = CreateIconFromResourceEx(resInfo, resSize, TRUE, 0x30000, cxDesired, cyDesired, LR_DEFAULTSIZE|LR_SHARED); return hIcon; }
int LocalizedTime(const time_t t, LPTSTR buf, size_t size) { /* Convert Unix timestamp to Win32 SYSTEMTIME */ FILETIME lft; SYSTEMTIME st; LONGLONG tmp = Int32x32To64(t, 10000000) + 116444736000000000; FILETIME ft = { .dwLowDateTime = (DWORD) tmp, .dwHighDateTime = tmp >> 32}; FileTimeToLocalFileTime(&ft, &lft); FileTimeToSystemTime(&lft, &st); int date_size = 0, time_size = 0; LCID locale = MAKELCID(GetGUILanguage(), SORT_DEFAULT); if (size > 0) { date_size = GetDateFormat(locale, DATE_SHORTDATE, &st, NULL, buf, size); if (date_size) buf[date_size - 1] = ' '; } if (size - date_size > 0) { time_size = GetTimeFormat(locale, TIME_NOSECONDS, &st, NULL, buf + date_size, size - date_size); } return date_size + time_size; } static int LoadStringLang(UINT stringId, LANGID langId, PTSTR buffer, int bufferSize, va_list args) { PWCH entry; PTSTR resBlockId = MAKEINTRESOURCE(stringId / 16 + 1); int resIndex = stringId & 15; /* find resource block for string */ HRSRC res = FindResourceLang(RT_STRING, resBlockId, langId); if (res == NULL) goto err; /* get pointer to first entry in resource block */ entry = (PWCH) LoadResource(o.hInstance, res); if (entry == NULL) goto err; /* search for string in block */ for (int i = 0; i < 16; i++) { /* skip over this entry */ if (i != resIndex) { entry += ((*entry) + 1); continue; } /* string does not exist */ if (i == resIndex && *entry == 0) break; /* string found, copy it */ PTSTR formatStr = (PTSTR) malloc((*entry + 1) * sizeof(TCHAR)); if (formatStr == NULL) break; formatStr[*entry] = 0; wcsncpy(formatStr, entry + 1, *entry); _vsntprintf(buffer, bufferSize, formatStr, args); buffer[bufferSize - 1] = 0; free(formatStr); return _tcslen(buffer); } err: /* not found, try again with the default language */ if (langId != fallbackLangId) return LoadStringLang(stringId, fallbackLangId, buffer, bufferSize, args); return 0; }
INT_PTR CALLBACK GeneralSettingsDlgProc(HWND hwndDlg, UINT msg, UNUSED WPARAM wParam, LPARAM lParam) { LPPSHNOTIFY psn; langProcData langData = { .languages = GetDlgItem(hwndDlg, ID_CMB_LANGUAGE), .language = GetGUILanguage() }; switch(msg) { case WM_INITDIALOG: /* Populate UI language selection combo box */ EnumResourceLanguages( NULL, RT_STRING, MAKEINTRESOURCE(IDS_LANGUAGE_NAME / 16 + 1), (ENUMRESLANGPROC) FillLangListProc, (LONG_PTR) &langData ); /* If none of the available languages matched, select the fallback */ if (ComboBox_GetCurSel(langData.languages) == CB_ERR) ComboBox_SelectString(langData.languages, -1, LangListEntry(IDS_LANGUAGE_NAME, fallbackLangId)); /* Clear language id data for the selected item */ ComboBox_SetItemData(langData.languages, ComboBox_GetCurSel(langData.languages), 0); if (GetLaunchOnStartup()) Button_SetCheck(GetDlgItem(hwndDlg, ID_CHK_STARTUP), BST_CHECKED); if (o.log_append) Button_SetCheck(GetDlgItem(hwndDlg, ID_CHK_LOG_APPEND), BST_CHECKED); if (o.silent_connection) Button_SetCheck(GetDlgItem(hwndDlg, ID_CHK_SILENT), BST_CHECKED); if (o.show_balloon == 0) CheckRadioButton (hwndDlg, ID_RB_BALLOON0, ID_RB_BALLOON2, ID_RB_BALLOON0); else if (o.show_balloon == 1) CheckRadioButton (hwndDlg, ID_RB_BALLOON0, ID_RB_BALLOON2, ID_RB_BALLOON1); else if (o.show_balloon == 2) CheckRadioButton (hwndDlg, ID_RB_BALLOON0, ID_RB_BALLOON2, ID_RB_BALLOON2); if (o.show_script_window) Button_SetCheck(GetDlgItem(hwndDlg, ID_CHK_SHOW_SCRIPT_WIN), BST_CHECKED); break; case WM_NOTIFY: psn = (LPPSHNOTIFY) lParam; if (psn->hdr.code == (UINT) PSN_APPLY) { LANGID langId = (LANGID) ComboBox_GetItemData(langData.languages, ComboBox_GetCurSel(langData.languages)); if (langId != 0) SetGUILanguage(langId); SetLaunchOnStartup(Button_GetCheck(GetDlgItem(hwndDlg, ID_CHK_STARTUP)) == BST_CHECKED); o.log_append = (Button_GetCheck(GetDlgItem(hwndDlg, ID_CHK_LOG_APPEND)) == BST_CHECKED); o.silent_connection = (Button_GetCheck(GetDlgItem(hwndDlg, ID_CHK_SILENT)) == BST_CHECKED); if (IsDlgButtonChecked(hwndDlg, ID_RB_BALLOON0)) o.show_balloon = 0; else if (IsDlgButtonChecked(hwndDlg, ID_RB_BALLOON2)) o.show_balloon = 2; else o.show_balloon = 1; o.show_script_window = (Button_GetCheck(GetDlgItem(hwndDlg, ID_CHK_SHOW_SCRIPT_WIN)) == BST_CHECKED); SaveRegistryKeys(); SetWindowLongPtr(hwndDlg, DWLP_MSGRESULT, PSNRET_NOERROR); return TRUE; } break; } return FALSE; }
static int __ShowLocalizedMsgEx(const UINT type, LPCTSTR caption, const UINT stringId, va_list args) { return MessageBoxEx(NULL, __LoadLocalizedString(stringId, args), caption, type | MB_SETFOREGROUND, GetGUILanguage()); }