aboutsummaryrefslogtreecommitdiff
path: root/Src/Plugins/General/gen_hotkeys/Configdlg.cpp
blob: d0572efe8fd521a0667abf159f3b3defbf1e81e1 (plain) (blame)
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
#include "ConfigDlg.h"
#include "gen_hotkeys.h"
#include "accelBlock.h"

#define ListView_InsertColumnW(hwnd, iCol, pcol) \
    (int)SNDMSG((hwnd), LVM_INSERTCOLUMNW, (WPARAM)(int)(iCol), (LPARAM)(const LV_COLUMNW *)(pcol))

#define ListView_InsertItemW(hwnd, pitem)   \
    (int)SNDMSG((hwnd), LVM_INSERTITEMW, 0, (LPARAM)(const LV_ITEMW *)(pitem))

#define ListView_SetItemTextW(hwndLV, i, iSubItem_, pszText_) \
{ LV_ITEMW _macro_lvi;\
  _macro_lvi.iSubItem = (iSubItem_);\
  _macro_lvi.pszText = (pszText_);\
  SNDMSG((hwndLV), LVM_SETITEMTEXTW, (WPARAM)(i), (LPARAM)(LV_ITEMW *)&_macro_lvi);\
}

#define ListView_SetItemW(hwnd, pitem) \
    (BOOL)SNDMSG((hwnd), LVM_SETITEMW, 0, (LPARAM)(LV_ITEMW *)(pitem))

void SetListItem(HWND hwHKList, HWND hwHK, HOTKEY_DATA *hk_data, int idx = -1, int failed = 0);

static AcceleratorBlocker *pAccelBlock = NULL;
static bool changed = false;

static int setCheckedStuff(HWND hwndDlg)
{
	int checked = SendMessage(GetDlgItem(hwndDlg, IDC_ENABLED), BM_GETCHECK, 0, 0) == BST_CHECKED;
	EnableWindow(GetDlgItem(hwndDlg,IDC_HKLIST),checked);
	EnableWindow(GetDlgItem(hwndDlg,IDC_COMBO),checked);
	EnableWindow(GetDlgItem(hwndDlg,IDC_HOTKEY),checked);
	EnableWindow(GetDlgItem(hwndDlg,IDC_ADD),checked);
	if (!checked)
	{
		EnableWindow(GetDlgItem(hwndDlg,IDC_SAVE),0);
		EnableWindow(GetDlgItem(hwndDlg,IDC_REMOVE),0);
	}
	EnableWindow(GetDlgItem(hwndDlg,IDC_DEFAULT),checked);
	return checked;
}

int ResizeComboBoxDropDown(HWND hwndDlg, UINT id, const char* str, int width){
	SIZE size = {0};
	HWND control = GetDlgItem(hwndDlg, id);
	HDC hdc = GetDC(control);
	// get and select parent dialog's font so that it'll calculate things correctly
	HFONT font = (HFONT)SendMessage(hwndDlg,WM_GETFONT,0,0), oldfont = (HFONT)SelectObject(hdc,font);
	GetTextExtentPoint32(hdc, str, lstrlen(str)+1, &size);

	int ret = width;
	if(size.cx > width)
	{
		SendDlgItemMessageW(hwndDlg, id, CB_SETDROPPEDWIDTH, size.cx, 0);
		ret = size.cx;
	}

	SelectObject(hdc, oldfont);
	ReleaseDC(control, hdc);
	return ret;
}

int ResizeComboBoxDropDownW(HWND hwndDlg, UINT id, const wchar_t *str, int width){
	SIZE size = {0};
	HWND control = GetDlgItem(hwndDlg, id);
	HDC hdc = GetDC(control);
	// get and select parent dialog's font so that it'll calculate things correctly
	HFONT font = (HFONT)SendMessage(hwndDlg,WM_GETFONT,0,0), oldfont = (HFONT)SelectObject(hdc,font);
	GetTextExtentPoint32W(hdc, str, (int)wcslen(str)+1, &size);

	int ret = width;
	if(size.cx > width)
	{
		SendDlgItemMessageW(hwndDlg, id, CB_SETDROPPEDWIDTH, size.cx, 0);
		ret = size.cx;
	}

	SelectObject(hdc, oldfont);
	ReleaseDC(control, hdc);
	return ret;
}

BOOL CALLBACK ConfigProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) 
{
	HWND hwHKList = GetDlgItem(hwndDlg, IDC_HKLIST);
	HWND hwCombo = GetDlgItem(hwndDlg, IDC_COMBO);
	HWND hwHK = GetDlgItem(hwndDlg, IDC_HOTKEY);

	switch (uMsg)
	{
		case WM_INITDIALOG:
		{
			int colwidth1 = -1, colwidth2 = -1;
			if (hwHKList && hwHK)
			{
				RECT r;

				SendMessage(hwHKList, WM_SETREDRAW, FALSE, 0);
				ListView_SetExtendedListViewStyle(hwHKList, LVS_EX_FULLROWSELECT | LVS_EX_INFOTIP);

				GetClientRect(hwHKList, &r);

				colwidth2 = GetPrivateProfileIntW(L"gen_hotkeys", L"col2", -1, g_iniFile);
				LVCOLUMNW lc = {LVCF_TEXT | LVCF_WIDTH, 0,
							    (colwidth2==-1?((r.right / 2) - GetSystemMetrics(SM_CYHSCROLL)):colwidth2),
							    WASABI_API_LNGSTRINGW(IDS_GHK_HOTKEY_COLUMN), 0, 0
							  };

				ListView_InsertColumnW(hwHKList, 0, &lc);

				lc.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;
				colwidth1 = GetPrivateProfileIntW(L"gen_hotkeys", L"col1", -1, g_iniFile);
				lc.cx = (colwidth1==-1?(r.right - lc.cx - GetSystemMetrics(SM_CYHSCROLL)):colwidth1);
				lc.pszText = WASABI_API_LNGSTRINGW(IDS_GHK_ACTION_COLUMN);
				lc.iSubItem = 1;

				ListView_InsertColumnW(hwHKList, 0, &lc);
			}

			changed = false;
			SubclassEditBox(hwHK);
			if (NULL != hwHK && NULL == pAccelBlock)
				pAccelBlock = new AcceleratorBlocker(hwHK);
			
			HBITMAP hBitmap = LoadBitmap(psPlugin.hDllInstance, MAKEINTRESOURCE(IDB_LVSTATE));

			HIMAGELIST hImageList = ImageList_Create(14, 14, ILC_COLOR8|ILC_MASK, 1, 0);
			ImageList_AddMasked(hImageList, hBitmap, RGB(252, 254, 252));
			ListView_SetImageList(hwHKList, hImageList, LVSIL_STATE);
			DeleteObject(hBitmap);

			ListView_SetItemCount(hwHKList, g_dwHotkeys);

			// We don't want our hot keys while in the config
			for (size_t i = 0; i < g_dwHotkeys; i++)
			{
				SetLastError(0);
				UnregisterHotkey(g_hotkeys + i);
				SetListItem(hwHKList, hwHK, &g_hotkeys[i].hkd, -1, g_hotkeys[i].failed);
			}
			hotkeysClear();

			if(colwidth1==-1)
				ListView_SetColumnWidth(hwHKList, 0, LVSCW_AUTOSIZE);
			if(colwidth2==-1)
				ListView_SetColumnWidth(hwHKList, 1, LVSCW_AUTOSIZE);

			// clear
			SendMessage(hwHK, wmHKCtlSet, 0, 0);

			ListView_SetItemState(hwHKList, -1, 0, LVIS_SELECTED);

			SendMessage(hwHKList, WM_SETREDRAW, TRUE, 0);

			int width = 0;
			for (size_t i = 0; i < GetCommandsNum(); i++)
			{
				bool unicode = 0;
				char *cmdname = GetCommandName((unsigned int)i, &unicode);
				if (*cmdname)
				{
					LRESULT pos;
					if (unicode)
					{
						pos = SendMessageW(hwCombo, CB_ADDSTRING, 0, (LPARAM) cmdname);
						width = ResizeComboBoxDropDownW(hwndDlg, IDC_COMBO, (wchar_t*)cmdname, width);
					}
					else
					{
						pos = SendMessageA(hwCombo, CB_ADDSTRING, 0, (LPARAM) cmdname);
						width = ResizeComboBoxDropDown(hwndDlg, IDC_COMBO, cmdname, width);
					}
					SendMessage(hwCombo, CB_SETITEMDATA, pos, i);
				}
			}

			// Set the enabled checkbox
			CheckDlgButton(hwndDlg,IDC_ENABLED,GetPrivateProfileIntW(L"gen_hotkeys", L"enabled", 0, g_iniFile));
			CheckDlgButton(hwndDlg,IDC_ENABLED_WM_APPCOMMAND,GetPrivateProfileIntW(L"gen_hotkeys", L"appcommand", 0, g_iniFile));
			setCheckedStuff(hwndDlg);

			if (NULL != WASABI_API_APP)
				WASABI_API_APP->DirectMouseWheel_EnableConvertToMouseWheel(hwHKList, TRUE);

			break;
		}

		case WM_COMMAND:
		{
			// we don't want accelerator keys working when the hotkey window is focused
			// that could be a problem if someone wants to bind Alt+D for example...
			if (GetFocus() == hwHK)
				break;

			switch (LOWORD(wParam))
			{
				case IDC_ADD:
				{
					HOTKEY_DATA hkd;
					DWORD dwHotKey = (unsigned long)SendMessage(hwHK, wmHKCtlGet, 0, 0);
					UINT iCommand = (unsigned int)SendMessage(hwCombo, CB_GETITEMDATA, SendMessage(hwCombo, CB_GETCURSEL, 0, 0), 0);

					if (!dwHotKey || iCommand == CB_ERR)
						break;

					hkd.dwHotKey = dwHotKey;
					hkd.iCommand = iCommand;

					changed = true;
					SetListItem(hwHKList, hwHK, &hkd);
					break;
				}

				case IDC_REMOVE:
				{
					int i = ListView_GetNextItem(hwHKList, -1, LVNI_SELECTED);
					while (i != -1)
					{
						LVITEM lvi = {LVIF_PARAM, i};
						ListView_GetItem(hwHKList, &lvi);
						HOTKEY_DATA *hkd = (HOTKEY_DATA *) lvi.lParam;
						delete hkd;
            
						changed = true;
						ListView_DeleteItem(hwHKList, i);
						i = ListView_GetNextItem(hwHKList, -1, LVNI_SELECTED);
					}
					EnableWindow(GetDlgItem(hwndDlg, IDC_REMOVE), 0);
					break;
				}

				case IDC_SAVE:
				{
					if (ListView_GetSelectedCount(hwHKList) != 1)
						break;

					int iSel = ListView_GetNextItem(hwHKList, -1, LVNI_SELECTED);

					LVITEMW lvi = {LVIF_PARAM, iSel};
					ListView_GetItem(hwHKList, &lvi);
					HOTKEY_DATA *hkd = (HOTKEY_DATA *) lvi.lParam;
					if (hkd)
					{
						DWORD dwHotKey = (unsigned long)SendMessage(hwHK, wmHKCtlGet, 0, 0);
						UINT iCommand = (unsigned int)SendMessage(hwCombo, CB_GETITEMDATA, SendMessage(hwCombo, CB_GETCURSEL, 0, 0), 0);

						if (!dwHotKey || iCommand == CB_ERR)
							break;

						hkd->dwHotKey = dwHotKey;
						hkd->iCommand = iCommand;

						changed = true;
						SetListItem(hwHKList, hwHK, hkd, iSel);
					}
					break;
				}

				case IDC_ENABLED:
					if (setCheckedStuff(hwndDlg))
					{
						DWORD dwSelected = ListView_GetSelectedCount(hwHKList);
						EnableWindow(GetDlgItem(hwndDlg, IDC_SAVE), dwSelected == 1);
						EnableWindow(GetDlgItem(hwndDlg, IDC_REMOVE), dwSelected);
						changed = true;
					}
					break;

				case IDC_DEFAULT:
				{
					DWORD i;
					SendMessage(hwndDlg, WM_SETREDRAW, FALSE, 0);

					DWORD dwCount = (DWORD)ListView_GetItemCount(hwHKList);
					if (dwCount)
					{
						for (i = 0; i < dwCount; i++)
						{
							LVITEM lvi = {LVIF_PARAM,(int)i};
							ListView_GetItem(hwHKList, &lvi);
							HOTKEY_DATA *hkd = (HOTKEY_DATA *) lvi.lParam;
							delete hkd;
						}
					}

					changed = true;
					ListView_DeleteAllItems(hwHKList);

					for (i = 0; i < DEFHKDS_NUM; i++)
					{
						SetListItem(hwHKList, hwHK, g_defhkds + i);
					}

					// restore the size of the columns on a reset as well
					ListView_SetColumnWidth(hwHKList, 0, LVSCW_AUTOSIZE);
					ListView_SetColumnWidth(hwHKList, 1, LVSCW_AUTOSIZE);

					ListView_SetItemState(hwHKList, -1, 0, LVIS_SELECTED);
					SendMessage(hwndDlg, WM_SETREDRAW, TRUE, 0);
					InvalidateRect(hwndDlg, 0, 0);
					break;
				}
			}
			break;
		}

		case WM_NOTIFY:
		{
			LPNMHDR nmhdr = (LPNMHDR) lParam;
			if (nmhdr && nmhdr->idFrom == IDC_HKLIST)
			{
				if (nmhdr->code == LVN_ITEMCHANGED)
				{
					LPNMLISTVIEW pnmv = (LPNMLISTVIEW) lParam;
					DWORD dwSelected = ListView_GetSelectedCount(hwHKList);
					EnableWindow(GetDlgItem(hwndDlg, IDC_SAVE), dwSelected == 1);
					EnableWindow(GetDlgItem(hwndDlg, IDC_REMOVE), dwSelected);

					SendMessage(hwHK, wmHKCtlSet, 0, 0);
					SendMessage(hwCombo, CB_SETCURSEL, -1, 0);

					if (dwSelected == 1 && (pnmv->uNewState & LVIS_SELECTED))
					{
						HOTKEY_DATA *hkd = (HOTKEY_DATA *) pnmv->lParam;
						if (hkd)
						{
							SendMessage(hwHK, wmHKCtlSet, hkd->dwHotKey, 0);
							bool unicode = 0;
							char *cmd = GetCommandName(hkd->iCommand, &unicode);
							if (*cmd)
							{
								if(unicode) SendMessageW(hwCombo, CB_SELECTSTRING, -1, (LPARAM) cmd);
								else SendMessageA(hwCombo, CB_SELECTSTRING, -1, (LPARAM) cmd);
							}
						}
					}
				}
				else if(nmhdr->code == LVN_KEYDOWN)
				{
					LPNMLVKEYDOWN pnmv = (LPNMLVKEYDOWN) lParam;
					if(pnmv->wVKey == VK_DELETE)
					{
						changed = true;
						SendMessage(hwndDlg,WM_COMMAND,MAKEWPARAM(IDC_REMOVE,0),0);
					}
					else if(pnmv->wVKey == 'A' && GetAsyncKeyState(VK_CONTROL))
					{
						for(int i = 0; i < ListView_GetItemCount(hwHKList); i++)
						{
							ListView_SetItemState(hwHKList,i,LVIS_SELECTED,LVIS_SELECTED);
						}
					}
				}
			}
		}
			break;

		case WM_DESTROY:
		{
			int checked = (SendMessage(GetDlgItem(hwndDlg, IDC_ENABLED), BM_GETCHECK, 0, 0) == BST_CHECKED);
			writePrivateProfileInt(L"enabled", checked);
			writePrivateProfileInt(L"appcommand",
								   (SendMessage(GetDlgItem(hwndDlg, IDC_ENABLED_WM_APPCOMMAND), BM_GETCHECK, 0, 0) == BST_CHECKED));
			hotkeysClear();

			int iCount = ListView_GetItemCount(hwHKList);
			HOTKEY_DATA *hkds = NULL;
			if (iCount)
			{
				hkds = new HOTKEY_DATA[iCount]; // TODO: could alloca this
				memset(hkds, 0, iCount * sizeof(HOTKEY_DATA));
			}
			if (hkds || !iCount)
			{ 
				for (size_t i = 0; i < (unsigned int)iCount; i++)
				{
					LVITEM lvi = {LVIF_PARAM, (int)i};
					ListView_GetItem(hwHKList, &lvi);
					HOTKEY_DATA *hkd = (HOTKEY_DATA *) lvi.lParam;
					if (hkd)
					{
						hkds[i] = *hkd;
						delete hkd;
					}
				}

		        hotkeysLoad(hkds, iCount, checked);
				if (changed) hotkeysSave(hkds, iCount);
				delete [] hkds;
			}
			writePrivateProfileInt(L"col1", ListView_GetColumnWidth(hwHKList,0));
			writePrivateProfileInt(L"col2", ListView_GetColumnWidth(hwHKList,1));
			RegisterShellHookWindow(psPlugin.hwndParent);
			if (NULL != pAccelBlock && hwHK == pAccelBlock->GetHwnd())
			{
				delete(pAccelBlock);
				pAccelBlock = NULL;
			}

			if (NULL != WASABI_API_APP)
				WASABI_API_APP->DirectMouseWheel_EnableConvertToMouseWheel(hwHKList, FALSE);
		}
			break;
	}
	return FALSE;
}

// idx = -1 for insertion
void SetListItem(HWND hwHKList, HWND hwHK, HOTKEY_DATA *hk_data, int idx, int failed)
{
	wchar_t szHK[1024] = {L""};

	SendMessage(hwHK, wmHKCtlSet, hk_data->dwHotKey, 0);
	GetWindowTextW(hwHK, szHK, sizeof(szHK));

	HOTKEY_DATA *hk_data_allocated = 0;

	if (idx < 0)
		hk_data_allocated = new HOTKEY_DATA; 

	if (idx >= 0 || hk_data_allocated)
	{
		if (idx < 0)
			*hk_data_allocated = *hk_data;

		int pos = ListView_GetItemCount(hwHKList);

		if (idx >= 0)
			pos = idx;

		// Add item to the list
		bool unicode = 0;
		LVITEM lvi = {
						LVIF_PARAM | LVIF_TEXT | LVIF_STATE,
						pos,
						0,
						LVIS_SELECTED | INDEXTOSTATEIMAGEMASK(failed ? (UINT)1 : 0) | LVIS_FOCUSED,
						LVIS_SELECTED | LVIS_STATEIMAGEMASK | LVIS_FOCUSED,
						GetCommandName(hk_data->iCommand, &unicode),
						0,
						0,
						(LPARAM) hk_data_allocated
					};

		char tmp[1024] = {0};

		if (!lvi.pszText || !*lvi.pszText)
		{
			if (hk_data->szCommand)
			{
				StringCchPrintf(tmp, 1024, "[%s]", hk_data->szCommand);
				lvi.pszText = tmp;
			}
		}
		if (!lvi.pszText || !*lvi.pszText)
			lvi.pszText = WASABI_API_LNGSTRING(IDS_GHK_ACTION_NOT_FOUND);

		if (idx >= 0)
		{
			lvi.mask ^= LVIF_PARAM;
			if(unicode) ListView_SetItemW(hwHKList, &lvi);
			else ListView_SetItem(hwHKList, &lvi);
		}
		else
		{
			ListView_SetItemState(hwHKList, -1, 0, LVIS_SELECTED | LVIS_FOCUSED);
			if(unicode) ListView_InsertItemW(hwHKList, &lvi);
			else ListView_InsertItem(hwHKList, &lvi);
		}
		ListView_SetItemTextW(hwHKList, pos, 1, szHK);
	}
}