From 4f73f17cecae8f7991424d7a820c83774c7c311e Mon Sep 17 00:00:00 2001 From: Birunthan Mohanathas Date: Sat, 1 Oct 2011 17:39:09 +0000 Subject: [PATCH] Added ability to change language in Manage Settings. --- Application/Application.cpp | 1 - Library/DialogManage.cpp | 68 +++++++++++++++++++++++++++++++++++++ Library/Rainmeter.cpp | 46 ++++++++++++++++++++----- Library/Rainmeter.h | 2 ++ Library/resource.h | 1 + Rainmeter.sln | 3 ++ 6 files changed, 111 insertions(+), 10 deletions(-) diff --git a/Application/Application.cpp b/Application/Application.cpp index 1ed5c4f0..becf0a7c 100644 --- a/Application/Application.cpp +++ b/Application/Application.cpp @@ -101,7 +101,6 @@ int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmd HMODULE module = GetModuleHandle(L"Rainmeter.dll"); if (module == NULL || Initialize(hWnd, module, lpCmdLine) == 1) { - MessageBox(NULL, L"Unable to load Rainmeter.dll", L"Rainmeter", MB_OK | MB_TOPMOST | MB_ICONERROR); DestroyWindow(hWnd); return RetError; } diff --git a/Library/DialogManage.cpp b/Library/DialogManage.cpp index bdccc71e..1630ed68 100644 --- a/Library/DialogManage.cpp +++ b/Library/DialogManage.cpp @@ -1629,6 +1629,53 @@ void CDialogManage::CTabSettings::Initialize() { m_Initialized = true; + // Scan for languages + HWND item = GetDlgItem(m_Window, IDC_MANAGESETTINGS_LANGUAGE_COMBOBOX); + WIN32_FIND_DATA fd; // Data structure describes the file found + HANDLE hSearch; // Search handle returned by FindFirstFile + + std::wstring files = Rainmeter->GetPath() + L"Languages\\*.dll"; + hSearch = FindFirstFile(files.c_str(), &fd); + do + { + if (hSearch == INVALID_HANDLE_VALUE) break; // No more files found + + WCHAR* pos = wcschr(fd.cFileName, L'.'); + if (pos) + { + LCID lcid = (LCID)wcstoul(fd.cFileName, &pos, 10); + if (GetLocaleInfo(lcid, LOCALE_SENGLISHLANGUAGENAME, fd.cFileName, MAX_PATH) > 0) + { + // Strip brackets in language name + std::wstring text = fd.cFileName; + std::wstring::size_type pos = 0; + while ((pos = text.find_first_of(L"()")) != std::wstring::npos) + { + text.erase(pos, 1); + } + + text += L" ("; + GetLocaleInfo(lcid, LOCALE_SNATIVELANGUAGENAME, fd.cFileName, MAX_PATH); + + // Some native language names don't start with a uppercase char.. + fd.cFileName[0] = towupper(fd.cFileName[0]); + text += fd.cFileName; + text += L")"; + + int index = ComboBox_AddString(item, text.c_str()); + ComboBox_SetItemData(item, index, (LPARAM)lcid); + + if (lcid == Rainmeter->GetResourceLCID()) + { + ComboBox_SetCurSel(item, index); + } + } + } + } + while (FindNextFile(hSearch, &fd)); + + FindClose(hSearch); + Button_SetCheck(GetDlgItem(m_Window, IDC_MANAGESETTINGS_CHECKUPDATES_CHECKBOX), !Rainmeter->GetDisableVersionCheck()); Button_SetCheck(GetDlgItem(m_Window, IDC_MANAGESETTINGS_LOCKSKINS_CHECKBOX), Rainmeter->GetDisableDragging()); Button_SetCheck(GetDlgItem(m_Window, IDC_MANAGESETTINGS_LOGTOFILE_CHECKBOX), Rainmeter->GetLogging()); @@ -1660,6 +1707,27 @@ INT_PTR CDialogManage::CTabSettings::OnCommand(WPARAM wParam, LPARAM lParam) { switch (LOWORD(wParam)) { + case IDC_MANAGESETTINGS_LANGUAGE_COMBOBOX: + if (HIWORD(wParam) == CBN_SELCHANGE) + { + WCHAR buffer[16]; + int sel = ComboBox_GetCurSel((HWND)lParam); + LCID lcid = ComboBox_GetItemData((HWND)lParam, sel); + _ultow(lcid, buffer, 10); + WritePrivateProfileString(L"Rainmeter", L"Language", buffer, Rainmeter->GetIniFile().c_str()); + + std::wstring resource = Rainmeter->GetPath() + L"Languages\\"; + resource += buffer; + resource += L".dll"; + FreeLibrary(Rainmeter->m_ResourceInstance); + Rainmeter->m_ResourceInstance = LoadLibraryEx(resource.c_str(), NULL, DONT_RESOLVE_DLL_REFERENCES | LOAD_LIBRARY_AS_DATAFILE); + Rainmeter->m_ResourceLCID = lcid; + + SendMessage(c_Dialog->GetWindow(), WM_DELAYED_CLOSE, 0, 0); + ExecuteBang(L"!Manage Settings"); // Delayed execute + } + break; + case IDC_MANAGESETTINGS_CHECKUPDATES_CHECKBOX: Rainmeter->SetDisableVersionCheck(!Rainmeter->GetDisableVersionCheck()); break; diff --git a/Library/Rainmeter.cpp b/Library/Rainmeter.cpp index 420b7553..89fc654c 100644 --- a/Library/Rainmeter.cpp +++ b/Library/Rainmeter.cpp @@ -155,7 +155,7 @@ int Initialize(HWND hWnd, HINSTANCE hInstance, LPCWSTR lpCmdLine) } catch (CError& error) { - MessageBox(hWnd, error.GetString().c_str(), APPNAME, MB_OK | MB_TOPMOST | MB_ICONEXCLAMATION); + MessageBox(hWnd, error.GetString().c_str(), APPNAME, MB_OK | MB_TOPMOST | MB_ICONERROR); } return result; @@ -968,13 +968,6 @@ int CRainmeter::Initialize(HWND hParent, HINSTANCE hInstance, LPCWSTR szPath) m_Path = tmpSzPath; - wcscat(tmpSzPath, L"Languages\\English.dll"); - m_ResourceInstance = LoadLibraryEx(tmpSzPath, NULL, DONT_RESOLVE_DLL_REFERENCES | LOAD_LIBRARY_AS_DATAFILE); - if (!m_ResourceInstance) - { - throw CError(L"Unable to load English.dll"); - } - InitalizeLitestep(); bool bDefaultIniLocation = false; @@ -1055,6 +1048,42 @@ int CRainmeter::Initialize(HWND hParent, HINSTANCE hInstance, LPCWSTR szPath) m_StatsFile += L".stats"; } + // Determine the language resource to load + std::wstring resource = m_Path + L"Languages\\"; + if (GetPrivateProfileString(L"Rainmeter", L"Language", L"", tmpSzPath, MAX_LINE_LENGTH, m_IniFile.c_str()) > 0) + { + m_ResourceLCID = wcstoul(tmpSzPath, NULL, 10); + } + else + { + // Use whatever the user selected for the installer + DWORD lang; + DWORD size = sizeof(DWORD); + if (SHGetValue(HKEY_LOCAL_MACHINE, L"Software\\Rainmeter", L"Language", NULL, &lang, &size) == ERROR_SUCCESS) + { + _ultow(lang, tmpSzPath, 10); + m_ResourceLCID = lang; + } + } + resource += tmpSzPath; + resource += L".dll"; + + m_ResourceInstance = LoadLibraryEx(resource.c_str(), NULL, DONT_RESOLVE_DLL_REFERENCES | LOAD_LIBRARY_AS_DATAFILE); + if (!m_ResourceInstance) + { + resource.insert(0, L"Unable to load language: "); + Log(LOG_ERROR, resource.c_str()); + + // Try English + resource = m_Path + L"Languages\\1033.dll"; + m_ResourceInstance = LoadLibraryEx(resource.c_str(), NULL, DONT_RESOLVE_DLL_REFERENCES | LOAD_LIBRARY_AS_DATAFILE); + m_ResourceLCID = 1033; + if (!m_ResourceInstance) + { + throw CError(L"Unable to load language library"); + } + } + // Read Logging settings beforehand m_Logging = 0!=GetPrivateProfileInt(L"Rainmeter", L"Logging", 0, m_IniFile.c_str()); m_Debug = 0!=GetPrivateProfileInt(L"Rainmeter", L"Debug", 0, m_IniFile.c_str()); @@ -1070,7 +1099,6 @@ int CRainmeter::Initialize(HWND hParent, HINSTANCE hInstance, LPCWSTR szPath) m_SkinPath += L"Skins\\"; // Read the skin folder from the ini file - tmpSzPath[0] = L'\0'; if (GetPrivateProfileString(L"Rainmeter", L"SkinPath", L"", tmpSzPath, MAX_LINE_LENGTH, m_IniFile.c_str()) > 0) { m_SkinPath = tmpSzPath; diff --git a/Library/Rainmeter.h b/Library/Rainmeter.h index 19242296..97da6699 100644 --- a/Library/Rainmeter.h +++ b/Library/Rainmeter.h @@ -130,6 +130,7 @@ public: HINSTANCE GetInstance() { return m_Instance; } HINSTANCE GetResourceInstance() { return m_ResourceInstance; } + LCID GetResourceLCID() { return m_ResourceLCID; } bool GetDebug() { return m_Debug; } @@ -264,6 +265,7 @@ private: HINSTANCE m_Instance; HMODULE m_ResourceInstance; + LCID m_ResourceLCID; ULONG_PTR m_GDIplusToken; diff --git a/Library/resource.h b/Library/resource.h index a9054010..618b78c8 100644 --- a/Library/resource.h +++ b/Library/resource.h @@ -78,6 +78,7 @@ #define IDC_MANAGESETTINGS_VERBOSELOGGING_CHECKBOX 1056 #define IDC_MANAGESETTINGS_SHOWLOGFILE_BUTTON 1057 #define IDC_MANAGESETTINGS_DELETELOGFILE_BUTTON 1058 +#define IDC_MANAGESETTINGS_LANGUAGE_COMBOBOX 1059 #define ID_STR_ISRTL 2000 #define ID_STR_UPDATEAVAILABLE 2001 diff --git a/Rainmeter.sln b/Rainmeter.sln index a6f6d632..54d2de03 100644 --- a/Rainmeter.sln +++ b/Rainmeter.sln @@ -5,6 +5,9 @@ EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Language", "Language\Language.vcxproj", "{6BE6F228-B741-4DA9-9FBC-E9F2A7BD483A}" EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Library", "Library\Library.vcxproj", "{BE9D2400-7F1C-49D6-8498-5CE495491AD6}" + ProjectSection(ProjectDependencies) = postProject + {6BE6F228-B741-4DA9-9FBC-E9F2A7BD483A} = {6BE6F228-B741-4DA9-9FBC-E9F2A7BD483A} + EndProjectSection EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "PluginAdvancedCPU", "Plugins\PluginAdvancedCPU\PluginAdvancedCPU.vcxproj", "{EE8EC522-8430-4B46-86A3-D943D77F9E4B}" EndProject