Silence GCC warnings.
[reactos.git] / reactos / dll / cpl / input / add.c
1 /*
2 *
3 * PROJECT: input.dll
4 * FILE: dll/win32/input/add.c
5 * PURPOSE: input.dll
6 * PROGRAMMER: Dmitry Chapyshev (dmitry@reactos.org)
7 * Colin Finck
8 * UPDATE HISTORY:
9 * 06-09-2007 Created
10 */
11
12 #include "resource.h"
13 #include "input.h"
14
15 static HWND hLangList;
16 static HWND hLayoutList;
17
18 static VOID
19 SelectLayoutByLang(VOID)
20 {
21 TCHAR Layout[MAX_PATH], Lang[MAX_PATH], LangID[CCH_LAYOUT_ID + 1];
22 INT iIndex;
23 LCID Lcid;
24
25 iIndex = SendMessage(hLangList, CB_GETCURSEL, 0, 0);
26 Lcid = SendMessage(hLangList, CB_GETITEMDATA, iIndex, 0);
27
28 GetLocaleInfo(MAKELCID(Lcid, SORT_DEFAULT), LOCALE_ILANGUAGE, Lang, sizeof(Lang) / sizeof(TCHAR));
29
30 wsprintf(LangID, _T("0000%s"), Lang);
31
32 if (GetLayoutName(LangID, Layout))
33 {
34 SendMessage(hLayoutList, CB_SELECTSTRING,
35 (WPARAM) -1, (LPARAM)Layout);
36 }
37 }
38
39 INT
40 GetLayoutCount(LPTSTR szLang)
41 {
42 HKEY hKey;
43 TCHAR szLayoutID[3 + 1], szPreload[CCH_LAYOUT_ID + 1], szLOLang[MAX_PATH];
44 DWORD dwIndex = 0, dwType, dwSize;
45 UINT Count = 0, i, j;
46
47 if (RegOpenKeyEx(HKEY_CURRENT_USER, _T("Keyboard Layout\\Preload"),
48 0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS)
49 {
50 dwSize = sizeof(szLayoutID);
51
52 while (RegEnumValue(hKey, dwIndex, szLayoutID, &dwSize, NULL, &dwType, NULL, NULL) == ERROR_SUCCESS)
53 {
54 dwSize = sizeof(szPreload);
55 RegQueryValueEx(hKey, szLayoutID, NULL, NULL, (LPBYTE)szPreload, &dwSize);
56
57 for (i = 4, j = 0; i < _tcslen(szPreload)+1; i++, j++)
58 szLOLang[j] = szPreload[i];
59
60 if (_tcscmp(szLOLang, szLang) == 0) Count += 1;
61
62 dwSize = sizeof(szLayoutID);
63 dwIndex++;
64 }
65
66 RegCloseKey(hKey);
67 }
68
69 return Count;
70 }
71
72 static VOID
73 AddNewLayout(HWND hwndDlg)
74 {
75 TCHAR NewLayout[CCH_ULONG_DEC + 1], Lang[MAX_PATH],
76 LangID[CCH_LAYOUT_ID + 1], Layout[MAX_PATH],
77 SubPath[CCH_LAYOUT_ID + 1], szMessage[MAX_PATH];
78 INT iLayout, iLang;
79 HKEY hKey, hSubKey;
80 DWORD cValues;
81 PTSTR pts;
82 LCID lcid;
83
84 iLayout = SendMessage(hLayoutList, CB_GETCURSEL, 0, 0);
85 if (iLayout == CB_ERR) return;
86
87 if (RegOpenKeyEx(HKEY_CURRENT_USER, _T("Keyboard Layout\\Preload"), 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS)
88 {
89 if (RegQueryInfoKey(hKey, NULL, NULL, NULL, NULL, NULL, NULL, &cValues, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
90 {
91 _ultot(cValues + 1, NewLayout, 10);
92
93 iLang = SendMessage(hLangList, CB_GETCURSEL, 0, 0);
94 lcid = SendMessage(hLangList, CB_GETITEMDATA, iLang, 0);
95 pts = (PTSTR) SendMessage(hLayoutList, CB_GETITEMDATA, iLayout, 0);
96
97 GetLocaleInfo(MAKELCID(lcid, SORT_DEFAULT), LOCALE_ILANGUAGE, Lang, sizeof(Lang) / sizeof(TCHAR));
98 wsprintf(LangID, _T("0000%s"), Lang);
99
100 if (IsLayoutExists(pts, LangID))
101 {
102 LoadString(hApplet, IDS_LAYOUT_EXISTS2, szMessage, sizeof(szMessage) / sizeof(TCHAR));
103 MessageBox(hwndDlg, szMessage, NULL, MB_OK | MB_ICONINFORMATION);
104
105 RegCloseKey(hKey);
106 return;
107 }
108
109 if (GetLayoutName(LangID, Layout))
110 {
111 if ((SendMessage(hLayoutList, CB_SELECTSTRING, (WPARAM) -1, (LPARAM)Layout) != CB_ERR)&&
112 (GetLayoutCount(Lang) >= 1))
113 {
114 wsprintf(SubPath, _T("d%03d%s"), GetLayoutCount(Lang)-1, Lang);
115 }
116 else SubPath[0] = '\0';
117 }
118
119 if (_tcslen(SubPath) != 0)
120 {
121 if (RegCreateKeyEx(HKEY_CURRENT_USER, _T("Keyboard Layout\\Substitutes"), 0, NULL,
122 REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS,
123 NULL, &hSubKey, NULL) == ERROR_SUCCESS)
124 {
125 if (RegSetValueEx(hSubKey, SubPath, 0, REG_SZ, (LPBYTE)pts,
126 (DWORD)((CCH_LAYOUT_ID + 1) * sizeof(TCHAR))) != ERROR_SUCCESS)
127 {
128 RegCloseKey(hSubKey);
129 RegCloseKey(hKey);
130 return;
131 }
132 RegCloseKey(hSubKey);
133 }
134 lstrcpy(pts, SubPath);
135 }
136
137 if (RegSetValueEx(hKey,
138 NewLayout,
139 0,
140 REG_SZ,
141 (LPBYTE)pts,
142 (DWORD)((CCH_LAYOUT_ID + 1) * sizeof(TCHAR))) == ERROR_SUCCESS)
143 {
144 UpdateLayoutsList();
145 }
146 }
147 RegCloseKey(hKey);
148 }
149 }
150
151 VOID
152 CreateKeyboardLayoutList(HWND hItemsList)
153 {
154 HKEY hKey;
155 PTSTR pstrLayoutID;
156 TCHAR szLayoutID[CCH_LAYOUT_ID + 1], KeyName[MAX_PATH];
157 DWORD dwIndex = 0;
158 DWORD dwSize;
159
160 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("System\\CurrentControlSet\\Control\\Keyboard Layouts"), 0, KEY_ENUMERATE_SUB_KEYS, &hKey) == ERROR_SUCCESS)
161 {
162 dwSize = sizeof(szLayoutID) / sizeof(TCHAR);
163
164 while (RegEnumKeyEx(hKey, dwIndex, szLayoutID, &dwSize, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
165 {
166 GetLayoutName(szLayoutID, KeyName);
167
168 INT iIndex = (INT) SendMessage(hItemsList, CB_ADDSTRING, 0, (LPARAM)KeyName);
169
170 pstrLayoutID = (PTSTR)HeapAlloc(hProcessHeap, 0, sizeof(szLayoutID));
171 lstrcpy(pstrLayoutID, szLayoutID);
172 SendMessage(hItemsList, CB_SETITEMDATA, iIndex, (LPARAM)pstrLayoutID);
173
174 // FIXME!
175 if (_tcscmp(szLayoutID, _T("00000409")) == 0)
176 {
177 SendMessage(hItemsList, CB_SETCURSEL, (WPARAM)iIndex, (LPARAM)0);
178 }
179
180 dwIndex++;
181
182 dwSize = sizeof(szLayoutID) / sizeof(TCHAR);
183 }
184
185 RegCloseKey(hKey);
186 }
187 }
188
189 /* Language enumerate procedure */
190 static BOOL CALLBACK
191 LanguagesEnumProc(LPTSTR lpLanguage)
192 {
193 LCID Lcid;
194 TCHAR Lang[1024];
195 INT Index;
196
197 Lcid = _tcstoul(lpLanguage, NULL, 16);
198
199 GetLocaleInfo(Lcid, LOCALE_SLANGUAGE, Lang, sizeof(Lang));
200 Index = (INT)SendMessage(hLangList, CB_ADDSTRING,
201 0, (LPARAM)Lang);
202
203 SendMessage(hLangList, CB_SETITEMDATA,
204 Index, (LPARAM)Lcid);
205
206 // FIXME!
207 if (Lcid == 0x0409)
208 {
209 SendMessage(hLangList, CB_SELECTSTRING,
210 (WPARAM) -1, (LPARAM)Lang);
211 }
212
213 return TRUE;
214 }
215
216 INT_PTR CALLBACK
217 AddDlgProc(HWND hDlg,
218 UINT message,
219 WPARAM wParam,
220 LPARAM lParam)
221 {
222 UNREFERENCED_PARAMETER(lParam);
223
224 switch (message)
225 {
226 case WM_INITDIALOG:
227 {
228 hLangList = GetDlgItem(hDlg, IDC_INPUT_LANG_COMBO);
229 hLayoutList = GetDlgItem(hDlg, IDC_KEYBOARD_LO_COMBO);
230 EnumSystemLocales(LanguagesEnumProc, LCID_INSTALLED);
231 CreateKeyboardLayoutList(hLayoutList);
232 }
233 break;
234
235 case WM_COMMAND:
236 {
237 switch (LOWORD(wParam))
238 {
239 case IDC_INPUT_LANG_COMBO:
240 {
241 if (HIWORD(wParam) == CBN_SELCHANGE)
242 {
243 SelectLayoutByLang();
244 }
245 }
246 break;
247
248 case IDOK:
249 {
250 AddNewLayout(hDlg);
251 EndDialog(hDlg, LOWORD(wParam));
252 }
253 break;
254
255 case IDCANCEL:
256 {
257 EndDialog(hDlg, LOWORD(wParam));
258 }
259 }
260 }
261 break;
262
263 case WM_DESTROY:
264 {
265 INT iCount;
266
267 for(iCount = SendMessage(hLayoutList, CB_GETCOUNT, 0, 0); --iCount >= 0;)
268 HeapFree(hProcessHeap, 0, (LPVOID)SendMessage(hLayoutList, CB_GETITEMDATA, iCount, 0));
269 }
270 break;
271 }
272
273 return FALSE;
274 }
275
276 /* EOF */