-
Notifications
You must be signed in to change notification settings - Fork 1
/
localization.c
376 lines (292 loc) · 10.2 KB
/
localization.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
* OpenVPN-GUI -- A Windows GUI for OpenVPN.
*
* Copyright (C) 2009 Heiko Hund <[email protected]>
* 2011 Michael Berger <[email protected]>
*
* 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 (see the file COPYING included with this
* distribution); if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#define _WIN32_IE 0x0500
#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>
#include <prsht.h>
#include <tchar.h>
#include <stdio.h>
#include <stdarg.h>
#include "config.h"
#include "main.h"
#include "localization.h"
#include "openvpn-gui-res.h"
#include "options.h"
#include "registry.h"
#include "gui.h"
extern options_t o;
static const LANGID fallbackLangId = MAKELANGID(LANG_ENGLISH, SUBLANG_NEUTRAL);
static LANGID gui_language;
static HRSRC
FindResourceLang(PTSTR resType, PTSTR resId, LANGID langId)
{
HRSRC res;
/* try to find the resource in requested language */
res = FindResourceEx(o.hInstance, resType, resId, langId);
if (res)
return res;
/* try to find the resource in the default language */
res = FindResourceEx(o.hInstance, resType, resId, fallbackLangId);
if (res)
return res;
/* try to find the resource in any language */
return FindResource(o.hInstance, resId, resType);
}
static LANGID
GetGUILanguage(void)
{
if (gui_language != 0)
return gui_language;
HKEY regkey;
DWORD value = 0;
LONG status = RegOpenKeyEx(HKEY_CURRENT_USER, GUI_REGKEY_HKCU, 0, KEY_READ, ®key);
if (status == ERROR_SUCCESS)
GetRegistryValueNumeric(regkey, _T("ui_language"), &value);
gui_language = (LANGID) ( value != 0 ? value : LANGIDFROMLCID(GetSystemDefaultLCID()) );
InitMUILanguage(gui_language);
return gui_language;
}
static void
SetGUILanguage(LANGID langId)
{
HKEY regkey;
if (RegCreateKeyEx(HKEY_CURRENT_USER, GUI_REGKEY_HKCU, 0, NULL, 0,
KEY_WRITE, NULL, ®key, NULL) != ERROR_SUCCESS )
ShowLocalizedMsg(IDS_ERR_CREATE_REG_HKCU_KEY, GUI_REGKEY_HKCU);
SetRegistryValueNumeric(regkey, _T("ui_language"), langId);
InitMUILanguage(langId);
gui_language = langId;
}
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)
return 0;
/* get pointer to first entry in resource block */
entry = (PWCH) LoadResource(o.hInstance, res);
if (entry == NULL)
return 0;
/* 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);
}
return 0;
}
static PTSTR
__LoadLocalizedString(const UINT stringId, va_list args)
{
static TCHAR msg[512];
msg[0] = 0;
LoadStringLang(stringId, GetGUILanguage(), msg, _tsizeof(msg), args);
return msg;
}
PTSTR
LoadLocalizedString(const UINT stringId, ...)
{
va_list args;
va_start(args, stringId);
PTSTR str = __LoadLocalizedString(stringId, args);
va_end(args);
return str;
}
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;
}
void
ShowLocalizedMsg(const UINT stringId, ...)
{
va_list args;
va_start(args, stringId);
MessageBox(NULL, __LoadLocalizedString(stringId, args), _T(PACKAGE_NAME), MB_OK | MB_SETFOREGROUND);
va_end(args);
}
HICON
LoadLocalizedIcon(const UINT iconId, const int cxDesired, const int cyDesired)
{
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 = LookupIconIdFromDirectoryEx((PBYTE) resInfo, TRUE, cxDesired, cyDesired, LR_DEFAULTCOLOR);
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;
PBYTE resResource = (PBYTE) LockResource(resInfo);
if (resResource == NULL)
return NULL;
return CreateIconFromResourceEx(resResource, resSize, TRUE, 0x30000, cxDesired, cyDesired, LR_DEFAULTCOLOR);
}
LPCDLGTEMPLATE
LocalizedDialogResource(const UINT dialogId)
{
/* find dialog resource */
HRSRC res = FindResourceLang(RT_DIALOG, MAKEINTRESOURCE(dialogId), GetGUILanguage());
if (res == NULL)
return NULL;
return (LPCDLGTEMPLATE) LoadResource(o.hInstance, res);
}
INT_PTR
LocalizedDialogBoxParam(const UINT dialogId, DLGPROC dialogFunc, const LPARAM param)
{
LPCDLGTEMPLATE resInfo = LocalizedDialogResource(dialogId);
if (resInfo == NULL)
return -1;
return ExtendedDialogBoxIndirectParam(o.hInstance, resInfo, NULL, dialogFunc, param);
}
INT_PTR
LocalizedDialogBox(const UINT dialogId, DLGPROC dialogFunc)
{
return LocalizedDialogBoxParam(dialogId, dialogFunc, 0);
}
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 ExtendedCreateDialogIndirectParam(o.hInstance, (CONST DLGTEMPLATE *) resInfo, NULL, dialogFunc, param);
}
HWND
CreateLocalizedDialog(const UINT dialogId, DLGPROC dialogFunc)
{
return CreateLocalizedDialogParam(dialogId, dialogFunc, 0);
}
static PTSTR
LangListEntry(const UINT stringId, const LANGID langId, ...)
{
static TCHAR str[128];
va_list args;
va_start(args, langId);
LoadStringLang(stringId, langId, str, _tsizeof(str), args);
va_end(args);
return str;
}
typedef struct {
HWND languages;
LANGID language;
} langProcData;
static BOOL
FillLangListProc(HANDLE module, PTSTR type, PTSTR stringId, WORD langId, LONG_PTR lParam)
{
langProcData *data = (langProcData*) lParam;
int index = ComboBox_AddString(data->languages, LangListEntry(IDS_LANGUAGE_NAME, langId));
(void) ComboBox_SetItemData(data->languages, index, langId);
/* Select this item if it is the currently displayed language */
if (langId == data->language
|| (PRIMARYLANGID(langId) == PRIMARYLANGID(data->language)
&& ComboBox_GetCurSel(data->languages) == CB_ERR) )
(void) ComboBox_SetCurSel(data->languages, index);
return TRUE;
}
INT_PTR CALLBACK
LanguageSettingsDlgProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
HICON hIcon;
LPPSHNOTIFY psn;
langProcData langData;
langData.languages = GetDlgItem(hwndDlg, ID_CMB_LANGUAGE);
langData.language = GetGUILanguage();
switch(msg) {
case WM_INITDIALOG:
hIcon = LoadLocalizedIcon(ID_ICO_APP, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON));
if (hIcon)
SendMessage(GetParent(hwndDlg), WM_SETICON, (WPARAM) (ICON_BIG), (LPARAM) (hIcon));
hIcon = LoadLocalizedIcon(ID_ICO_APP, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON));
if (hIcon)
SendMessage(GetParent(hwndDlg), WM_SETICON, (WPARAM) (ICON_SMALL), (LPARAM) (hIcon));
/* 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)
(void) ComboBox_SelectString(langData.languages, -1,
LangListEntry(IDS_LANGUAGE_NAME, fallbackLangId));
/* Clear language id data for the selected item */
(void) ComboBox_SetItemData(langData.languages, ComboBox_GetCurSel(langData.languages), 0);
/* Center the propery sheet */
ClipOrCenterWindowToMonitor(GetParent(hwndDlg), MONITOR_CENTER | MONITOR_WORKAREA);
/* Set the global window handle for the settings dialog */
o.hwndSettings = GetParent(hwndDlg);
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);
SetWindowLongPtr(hwndDlg, DWLP_MSGRESULT, PSNRET_NOERROR);
return TRUE;
}
break;
}
return FALSE;
}