xref: /reactos/dll/cpl/input/key_settings_dialog.c (revision 84344399)
1 /*
2 * PROJECT:         input.dll
3 * FILE:            dll/cpl/input/key_settings_dialog.c
4 * PURPOSE:         input.dll
5 * PROGRAMMER:      Dmitry Chapyshev (dmitry@reactos.org)
6 */
7 
8 #include "input.h"
9 
10 static KEY_SETTINGS _KeySettings = { 0 };
11 
12 
13 DWORD
14 ReadAttributes(VOID)
15 {
16     DWORD dwAttributes = 0;
17     HKEY hKey;
18 
19     if (RegOpenKeyExW(HKEY_CURRENT_USER,
20                       L"Keyboard Layout",
21                       0,
22                       KEY_QUERY_VALUE,
23                       &hKey) == ERROR_SUCCESS)
24     {
25         DWORD dwSize = sizeof(dwAttributes);
26 
27         RegQueryValueExW(hKey,
28                          L"Attributes",
29                          NULL, NULL,
30                          (LPBYTE)&dwAttributes,
31                          &dwSize);
32 
33         RegCloseKey(hKey);
34     }
35 
36     return dwAttributes;
37 }
38 
39 static VOID
40 ReadKeysSettings(VOID)
41 {
42     HKEY hKey;
43 
44     _KeySettings.dwAttributes = ReadAttributes();
45 
46     if (RegOpenKeyExW(HKEY_CURRENT_USER,
47                       L"Keyboard Layout\\Toggle",
48                       0,
49                       KEY_QUERY_VALUE,
50                       &hKey) == ERROR_SUCCESS)
51     {
52         WCHAR szBuffer[MAX_PATH];
53         DWORD dwSize;
54 
55         dwSize = sizeof(szBuffer);
56 
57         if (RegQueryValueExW(hKey,
58                              L"Language Hotkey",
59                              NULL, NULL,
60                              (LPBYTE)szBuffer, &dwSize) == ERROR_SUCCESS)
61         {
62             _KeySettings.dwLanguage = _wtoi(szBuffer);
63         }
64 
65         dwSize = sizeof(szBuffer);
66 
67         if (RegQueryValueExW(hKey,
68                              L"Layout Hotkey",
69                              NULL, NULL,
70                              (LPBYTE)szBuffer, &dwSize) == ERROR_SUCCESS)
71         {
72             _KeySettings.dwLayout = _wtoi(szBuffer);
73         }
74 
75         RegCloseKey(hKey);
76     }
77 }
78 
79 
80 static VOID
81 WriteKeysSettings(VOID)
82 {
83     HKEY hKey;
84 
85     if (RegOpenKeyExW(HKEY_CURRENT_USER,
86                       L"Keyboard Layout",
87                       0,
88                       KEY_SET_VALUE,
89                       &hKey) == ERROR_SUCCESS)
90     {
91         RegSetValueExW(hKey,
92                        L"Attributes",
93                        0,
94                        REG_DWORD,
95                        (LPBYTE)&_KeySettings.dwAttributes,
96                        sizeof(DWORD));
97 
98         RegCloseKey(hKey);
99     }
100 
101     if (RegOpenKeyExW(HKEY_CURRENT_USER,
102                       L"Keyboard Layout\\Toggle",
103                       0,
104                       KEY_SET_VALUE,
105                       &hKey) == ERROR_SUCCESS)
106     {
107         WCHAR szBuffer[MAX_PATH];
108 
109         StringCchPrintfW(szBuffer, ARRAYSIZE(szBuffer), L"%lu", _KeySettings.dwLanguage);
110 
111         RegSetValueExW(hKey,
112                        L"Hotkey",
113                        0,
114                        REG_SZ,
115                        (LPBYTE)szBuffer,
116                        (wcslen(szBuffer) + 1) * sizeof(WCHAR));
117 
118         RegSetValueExW(hKey,
119                        L"Language Hotkey",
120                        0,
121                        REG_SZ,
122                        (LPBYTE)szBuffer,
123                        (wcslen(szBuffer) + 1) * sizeof(WCHAR));
124 
125         StringCchPrintfW(szBuffer, ARRAYSIZE(szBuffer), L"%lu", _KeySettings.dwLayout);
126 
127         RegSetValueExW(hKey,
128                        L"Layout Hotkey",
129                        0,
130                        REG_SZ,
131                        (LPBYTE)szBuffer,
132                        (wcslen(szBuffer) + 1) * sizeof(WCHAR));
133 
134         RegCloseKey(hKey);
135     }
136 
137     /* Notice system of change hotkeys parameters */
138     SystemParametersInfoW(SPI_SETLANGTOGGLE, 0, NULL, 0);
139 
140     /* Notice system of change CapsLock mode parameters */
141     ActivateKeyboardLayout(GetKeyboardLayout(0), KLF_RESET | _KeySettings.dwAttributes);
142 }
143 
144 
145 static VOID
146 UpdateKeySettingsListView(HWND hwndList)
147 {
148     WCHAR szBuffer[MAX_STR_LEN];
149     LV_ITEM item;
150     INT iItemIndex;
151 
152     ListView_DeleteAllItems(hwndList);
153 
154     ZeroMemory(&item, sizeof(item));
155 
156     LoadStringW(hApplet, IDS_SWITCH_BET_INLANG, szBuffer, ARRAYSIZE(szBuffer));
157     item.mask = LVIF_TEXT | LVIF_PARAM | LVIF_STATE;
158     item.pszText = szBuffer;
159     item.iItem = 0;
160 
161     iItemIndex = ListView_InsertItem(hwndList, &item);
162 
163     if (_KeySettings.dwLanguage == 1)
164     {
165         LoadStringW(hApplet, IDS_LEFT_ALT_SHIFT, szBuffer, ARRAYSIZE(szBuffer));
166     }
167     else if (_KeySettings.dwLanguage == 2)
168     {
169         LoadStringW(hApplet, IDS_CTRL_SHIFT, szBuffer, ARRAYSIZE(szBuffer));
170     }
171     else
172     {
173         LoadStringW(hApplet, IDS_NONE, szBuffer, ARRAYSIZE(szBuffer));
174     }
175 
176     ListView_SetItemText(hwndList, iItemIndex, 1, szBuffer);
177 }
178 
179 
180 static VOID
181 OnInitKeySettingsDialog(HWND hwndDlg)
182 {
183     LV_COLUMN column;
184     HWND hwndList;
185 
186     ReadKeysSettings();
187 
188     if (_KeySettings.dwAttributes & KLF_SHIFTLOCK)
189     {
190         CheckDlgButton(hwndDlg, IDC_PRESS_SHIFT_KEY_RB, BST_CHECKED);
191         CheckDlgButton(hwndDlg, IDC_PRESS_CL_KEY_RB, BST_UNCHECKED);
192     }
193     else
194     {
195         CheckDlgButton(hwndDlg, IDC_PRESS_SHIFT_KEY_RB, BST_UNCHECKED);
196         CheckDlgButton(hwndDlg, IDC_PRESS_CL_KEY_RB, BST_CHECKED);
197     }
198 
199     hwndList = GetDlgItem(hwndDlg, IDC_KEY_LISTVIEW);
200 
201     ListView_SetExtendedListViewStyle(hwndList, LVS_EX_FULLROWSELECT);
202 
203     ZeroMemory(&column, sizeof(column));
204 
205     column.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;
206 
207     column.fmt      = LVCFMT_LEFT;
208     column.iSubItem = 0;
209     column.pszText  = L"";
210     column.cx       = 210;
211     ListView_InsertColumn(hwndList, 0, &column);
212 
213     column.fmt      = LVCFMT_RIGHT;
214     column.cx       = 145;
215     column.iSubItem = 1;
216     column.pszText  = L"";
217     ListView_InsertColumn(hwndList, 1, &column);
218 
219     UpdateKeySettingsListView(hwndList);
220 }
221 
222 
223 INT_PTR CALLBACK
224 KeySettingsDialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
225 {
226     UNREFERENCED_PARAMETER(lParam);
227 
228     switch (uMsg)
229     {
230         case WM_INITDIALOG:
231             OnInitKeySettingsDialog(hwndDlg);
232             return TRUE;
233 
234         case WM_COMMAND:
235         {
236             switch (LOWORD(wParam))
237             {
238                 case IDC_CHANGE_KEY_SEQ_BTN:
239                 {
240                     if (DialogBoxParamW(hApplet,
241                                         MAKEINTRESOURCEW(IDD_CHANGE_KEY_SEQ),
242                                         hwndDlg,
243                                         ChangeKeySeqDialogProc,
244                                         (LPARAM)&_KeySettings) == IDOK)
245                     {
246                         UpdateKeySettingsListView(GetDlgItem(hwndDlg, IDC_KEY_LISTVIEW));
247                     }
248                 }
249                 break;
250 
251                 case IDOK:
252                 {
253                     if (IsDlgButtonChecked(hwndDlg, IDC_PRESS_CL_KEY_RB) == BST_CHECKED)
254                     {
255                         _KeySettings.dwAttributes &= ~KLF_SHIFTLOCK;
256                     }
257                     else
258                     {
259                         _KeySettings.dwAttributes |= KLF_SHIFTLOCK;
260                     }
261 
262                     WriteKeysSettings();
263                     EndDialog(hwndDlg, LOWORD(wParam));
264                 }
265                 break;
266 
267                 case IDCANCEL:
268                 {
269                     EndDialog(hwndDlg, LOWORD(wParam));
270                 }
271                 break;
272             }
273         }
274         break;
275     }
276 
277     return FALSE;
278 }
279