-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfiguration.cpp
1886 lines (1543 loc) · 48.6 KB
/
configuration.cpp
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "stdafx.h"
#include "resource.h"
#include "recaps.h"
#include "configuration.h"
#include "settings.h"
std::map<HKL, KeyboardLayoutInfo> g_keyboardInfo;
std::map<UHK, std::map<HKL, UHK>> g_hotkeyInfo;
UHK g_uhkCurrentGroup = 0;
UHK g_uhkLastHotkey = 0;
DWORD g_dwKeysCount = 0;
DWORD g_dwModifiers = 0;
int g_nHeightDelta = 0;
int g_nLocalesDelta = 0;
int g_nGroupsDelta = 0;
bool g_bCustomizingOn = false;
HICON g_hHKIcon = NULL;
////////////////////////////////////////////////////////////////////////////
//
// HotKey configuration dialog
//
static HBRUSH st_hbrBack = 0;
static UHK st_uhkCustHotkeyOrg = 0;
static UHK st_uhkCustHotkey = 0;
static DWORD st_dwPrevKeysCount = 0;
struct HotkeyDialogInit {
UHK uhk;
const TCHAR* szCaption;
const TCHAR* szDisableCaption;
HotkeyDialogInit(UHK _uhk, const TCHAR* sz, const TCHAR* szDis = NULL)
: uhk(_uhk), szCaption(sz), szDisableCaption(szDis) {}
};
DWORD VkToModBit(DWORD vkCode)
{
switch (vkCode) {
case VK_LSHIFT: return LEFT_LSHIFT;
case VK_RSHIFT: return LEFT_RSHIFT;
case VK_LCONTROL: return LEFT_LCTRL;
case VK_RCONTROL: return LEFT_RCTRL;
case VK_LMENU: return LEFT_LALT;
case VK_RMENU: return LEFT_RALT;
case VK_LWIN: return LEFT_LWIN;
case VK_RWIN: return LEFT_RWIN;
case VK_APPS: return LEFT_MENU;
default:
break;
}
return 0;
}
void ExpandHotkeyName(std::basic_string<TCHAR>& name, UHK uhk)
{
if (0 == uhk.hotkeys.ulHotKey) {
name = _T("Disabled");
return;
}
const TCHAR* aszModifiers[] = {
_T("L.Shift"), // LEFT_LSHIFT = 1 << 0
_T("R.Shift"), // LEFT_RSHIFT = 1 << 1
_T("L.Control"), // LEFT_LCTRL = 1 << 2
_T("R.Control"), // LEFT_RCTRL = 1 << 3
_T("L.Alt"), // LEFT_LALT = 1 << 4
_T("R.Alt"), // LEFT_RALT = 1 << 5
_T("L.Win"), // LEFT_LWIN = 1 << 6
_T("R.Win"), // LEFT_RWIN = 1 << 7
_T("Menu") // LEFT_MENU = 1 << 8
};
DWORD dwModifiers = uhk.bits.btMods;
dwModifiers &= ~VkToModBit(uhk.bits.btVK);
int i = 0;
for (DWORD mask = 1; mask != LEFT_MAX; mask <<= 1, i++)
if (0 != (dwModifiers & mask)) {
name += aszModifiers[i];
name += _T(" + ");
}
int index = -1;
switch (uhk.bits.btVK) {
case VK_LSHIFT: index = 0; break;
case VK_RSHIFT: index = 1; break;
case VK_LCONTROL: index = 2; break;
case VK_RCONTROL: index = 3; break;
case VK_LMENU: index = 4; break;
case VK_RMENU: index = 5; break;
case VK_LWIN: index = 6; break;
case VK_RWIN: index = 7; break;
case VK_APPS: index = 8; break;
default:
break;
}
if (-1 == index) {
std::vector<TCHAR> buffer(64);
DWORD dwScanCode = 0 == uhk.bits.btSK
? MapVirtualKey(uhk.bits.btVK, MAPVK_VK_TO_VSC) : uhk.bits.btSK;
if (GetKeyNameText(dwScanCode << 16 | uhk.bits.bExtKey << 24, &buffer[0], ( int )buffer.size())) {
name += &buffer[0];
} else {
_sntprintf(&buffer[0], 64, _T("GetKeyNameText error %d"), GetLastError());
OutputDebugString(&buffer[0]);
_sntprintf(&buffer[0], 64, _T("VK:%#x; SK:%#x"), uhk.bits.btVK, dwScanCode);
name += &buffer[0];
}
} else
name += aszModifiers[index];
}
static BOOL OnInitShortCutDialog(HWND hwnd, HWND /*hwndFocus*/, LPARAM lParam)
{
SetTimer(hwnd, 1, 100, NULL);
st_hbrBack = CreateSolidBrush(RGB(0, 255, 0));
SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)g_hHKIcon);
const HotkeyDialogInit* lpInit = (const HotkeyDialogInit*)lParam;
if (NULL != lpInit && NULL != lpInit->szCaption)
SetWindowText(hwnd, lpInit->szCaption);
if (NULL != lpInit && NULL != lpInit->szDisableCaption)
SetWindowText(GetDlgItem(hwnd, IDC_DISABLE), lpInit->szDisableCaption);
st_uhkCustHotkeyOrg =
st_uhkCustHotkey = lpInit->uhk;
std::basic_string<TCHAR> hotkeyName;
ExpandHotkeyName(hotkeyName, st_uhkCustHotkey);
SetWindowText(GetDlgItem(hwnd, IDC_HOTKEY_STATIC), hotkeyName.c_str());
g_bCustomizingOn = true;
return TRUE;
}
static bool OnApply(bool bDisable)
{
if (!bDisable) {
std::map<UHK, std::map<HKL, UHK>>::iterator
i = g_hotkeyInfo.find(st_uhkCustHotkey);
if (st_uhkCustHotkeyOrg.hotkeys.ulHotKey != st_uhkCustHotkey.hotkeys.ulHotKey
&& i != g_hotkeyInfo.end()) {
MessageBox(NULL, _T("The hotkey is already in use. Please choose another one."),
_T("Recaps"), MB_OK | MB_ICONWARNING | MB_TASKMODAL | MB_TOPMOST);
return false;
}
switch (st_uhkCustHotkey.bits.btVK) {
case VK_LSHIFT:
case VK_RSHIFT:
case VK_LCONTROL:
case VK_RCONTROL:
case VK_LMENU:
case VK_RMENU:
case VK_LWIN:
case VK_RWIN:
case VK_APPS:
st_uhkCustHotkey.bits.bKeyDown = 0;
break;
default:
st_uhkCustHotkey.bits.bKeyDown = 1;
break;
}
} else
st_uhkCustHotkey.hotkeys.ulHotKey = 0;
#if 1
TCHAR ch[100] = { 0 };
_stprintf(ch, _T("Set Hotkey %08x\n"), st_uhkCustHotkey.ulKey);
OutputDebugString(ch);
#endif
return true;
}
static void OnShortCutCommand(
HWND hwnd, int id, HWND /*hwndCtl*/, UINT /*codeNotify*/)
{
bool bDisable = false;
switch (id) {
case IDC_DISABLE:
bDisable = true;
// no break;
case IDOK:
if (OnApply(bDisable))
EndDialog(hwnd, IDOK);
break;
case IDCANCEL:
EndDialog(hwnd, IDCANCEL);
break;
}
}
static void OnShortCutTimer(HWND hwnd, UINT /*id*/)
{
static DWORD st_dwPrevModifiers = g_dwModifiers;
DWORD dwChanged = st_dwPrevModifiers ^ g_dwModifiers;
st_dwPrevModifiers = g_dwModifiers;
if (dwChanged) {
UINT idControl = IDC_LEFT_SHIFT_STATIC;
for (DWORD mask = 1; mask != LEFT_MAX; mask <<= 1, idControl++)
if (0 != (dwChanged & mask))
InvalidateRect(GetDlgItem(hwnd, idControl), NULL, FALSE);
}
if (st_dwPrevKeysCount != g_dwKeysCount) {
/*#ifdef _DEBUG
TCHAR ch[100] = { 0 };
_stprintf(ch, _T("%08x => %08x\n"), g_adwPrevHotkey[4], g_uhkLastHotkey);
OutputDebugString(ch);
#endif */
// copy but preserve HK flags
st_uhkCustHotkey.bits.bKeyDown = g_uhkLastHotkey.bits.bKeyDown;
st_uhkCustHotkey.bits.bExtKey = g_uhkLastHotkey.bits.bExtKey;
st_uhkCustHotkey.hotkeys.ulHotKey = g_uhkLastHotkey.hotkeys.ulHotKey;
std::basic_string<TCHAR> hotkeyName;
ExpandHotkeyName(hotkeyName, st_uhkCustHotkey);
SetWindowText(GetDlgItem(hwnd, IDC_HOTKEY_STATIC), hotkeyName.c_str());
InvalidateRect(GetDlgItem(hwnd, IDC_HOTKEY_STATIC), NULL, FALSE);
}
st_dwPrevKeysCount = g_dwKeysCount;
}
static void OnShortCutClose(HWND hwnd)
{
DeleteObject(st_hbrBack);
KillTimer(hwnd, 1);
}
HBRUSH OnShortCutColorStatic(HWND hwnd, HDC hdc, HWND hwndChild, int /*type*/)
{
UINT idControl = GetDlgCtrlID(hwndChild);
bool bHilite = false;
if (IDC_HOTKEY_STATIC == idControl) {
bHilite = !g_bCustomizingOn;
} else if (!(idControl < IDC_LEFT_SHIFT_STATIC
|| idControl > IDC_MENU_STATIC))
bHilite = 0 != (g_dwModifiers & (1 << (idControl - IDC_LEFT_SHIFT_STATIC)));
if (bHilite) {
SetBkMode(hdc, TRANSPARENT);
return st_hbrBack;
}
return FORWARD_WM_CTLCOLORMSGBOX(hwnd, hdc, hwndChild, DefDlgProc);
}
static INT_PTR __stdcall ShortCutDialogProc(HWND hDlg, UINT message,
WPARAM wParam, LPARAM lParam)
{
switch (message) {
HANDLE_MSG(hDlg, WM_INITDIALOG, OnInitShortCutDialog);
HANDLE_MSG(hDlg, WM_COMMAND, OnShortCutCommand);
HANDLE_MSG(hDlg, WM_TIMER, OnShortCutTimer);
HANDLE_MSG(hDlg, WM_CLOSE, OnShortCutClose);
HANDLE_MSG(hDlg, WM_CTLCOLORSTATIC, OnShortCutColorStatic);
default: break;
}
return FALSE;
}
////////////////////////////////////////////////////////////////////////////
//
// Settings dialog
//
/* LRESULT Cls_OnSizing(HWND hwnd, UINT edge, const LPRECT lprc) */
#define HANDLE_WM_SIZING(hwnd, wParam, lParam, fn) \
((fn)((hwnd), (UINT)(wParam), (LPRECT)(lParam)))
#define FORWARD_WM_SIZING(hwnd, edge, lprc, fn) \
(void)(fn)((hwnd), WM_SIZING, (WPARAM)(UINT)(edge), (LPARAM)(LPRECT)(lprc))
enum {
eHotkeysList = 0,
eLocalesList,
eGroupsList,
eHintStatic,
};
enum {
HTSPLIT_TOP = 1000,
HTSPLIT_BOTTOM,
};
static struct {
UINT uiId;
HWND hwnd;
RECT rc;
} st_aControls[] = {
{ IDC_LIST_HOTKEYS, 0 },
{ IDC_LIST_LANGUAGES, 0 },
{ IDC_LIST_GROUPS, 0 },
{ IDC_PAYPAL_LINK, 0 },
{ IDCANCEL, 0 }
};
static HIMAGELIST st_hImageList16;
static SIZE st_szInitDlgSize = { 0 };
static int st_nInitHotkeysHeight = 0;
static int st_nInitLocalesHeight = 0;
static int st_nInitGroupsHeight = 0;
static UINT st_uiCapturedHitTest = 0;
static int st_nCapturedY = 0;
const int cnDrawTextOffset = 6;
static BOOL InitHotkeysList(HWND hwnd)
{
HWND hWndList = GetDlgItem(hwnd, IDC_LIST_HOTKEYS);
DWORD dwStyleEx = LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT
| LVS_EX_AUTOSIZECOLUMNS | LVS_EX_BORDERSELECT;
ListView_SetExtendedListViewStyleEx(hWndList, dwStyleEx, dwStyleEx);
ListView_SetBkColor(hWndList, GetSysColor(COLOR_BTNFACE));
ListView_SetTextBkColor(hWndList, GetSysColor(COLOR_BTNFACE));
// init columns
struct {
const TCHAR* pszName;
int iWidth;
} aColumns[] = {
{ _T("Action"), 200 },
{ _T("Hotkey"), 220 },
};
LVCOLUMN lvc = { 0 };
lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
for (int i = 0; i < sizeof(aColumns) / sizeof(aColumns[0]); i++) {
lvc.iSubItem = i;
lvc.pszText = (TCHAR*)aColumns[i].pszName;
lvc.cx = aColumns[i].iWidth;
if ( i < 1 )
lvc.fmt = LVCFMT_LEFT; // Left-aligned column.
else
lvc.fmt = LVCFMT_RIGHT; // Right-aligned column.
if (ListView_InsertColumn(hWndList, i, &lvc) == -1)
return FALSE;
}
// Add items for the actions
struct {
const TCHAR* pszName;
UHK uhk;
} aActionItems[] = {
{ _T("Cycle Switch"), 0 },
{ _T("Convert Inplace"), 0 },
{ _T("Eject CD"), 0 },
{ _T("Toggle Caps Lock"), 0 },
};
/* aActionItems[0].uhk.bCycleHK = 1;
aActionItems[1].uhk.bRecodeHK = 1;
aActionItems[2].uhk.bEjectHK = 1;
aActionItems[3].uhk.bCapsHK = 1;*/
aActionItems[0].uhk.bits.btHKType = eHKTCycle;
aActionItems[1].uhk.bits.btHKType = eHKTRecode;
aActionItems[2].uhk.bits.btHKType = eHKTEject;
aActionItems[3].uhk.bits.btHKType = eHKTCaps;
for (std::map<UHK, std::map<HKL, UHK>>::iterator i = g_hotkeyInfo.begin();
i != g_hotkeyInfo.end(); i++) {
/* if (i->first.bCycleHK)
aActionItems[0].uhk = i->first.ulKey;
if (i->first.bRecodeHK)
aActionItems[1].uhk = i->first.ulKey;
if (i->first.bEjectHK)
aActionItems[2].uhk = i->first.ulKey;
// if (i->first.bCapsHK)
// aActionItems[3].uhk = i->first.ulKey; */
switch (i->first.bits.btHKType) {
case eHKTCycle: aActionItems[0].uhk = i->first.ulKey; break;
case eHKTRecode: aActionItems[1].uhk = i->first.ulKey; break;
case eHKTEject: aActionItems[2].uhk = i->first.ulKey; break;
//case eHKTCaps: aActionItems[3].uhk = i->first.ulKey; break;
}
}
aActionItems[3].uhk = UHK(VK_CAPITAL,
( unsigned char )MapVirtualKey(VK_CAPITAL, MAPVK_VK_TO_VSC), 0, true, false);
// aActionItems[3].uhk.bCapsHK = 1;
aActionItems[3].uhk.bits.btHKType = eHKTCaps;
if (aActionItems[0].uhk.bits.btVK == VK_CAPITAL)
aActionItems[3].uhk.bits.btMods = LEFT_LALT;
LVITEM lvi = { 0 };
lvi.mask = LVIF_TEXT | LVIF_PARAM;
// stock hotkeys
for (int i = 0; i < _countof(aActionItems); i++) {
lvi.mask |= LVIF_PARAM;
lvi.iItem = i;
lvi.iSubItem = 0;
lvi.pszText = (LPTSTR)aActionItems[i].pszName;
lvi.lParam = (LPARAM)aActionItems[i].uhk.ulKey;
if (ListView_InsertItem(hWndList, &lvi) == -1)
return FALSE;
lvi.mask &= ~(LVIF_PARAM);
lvi.iSubItem = 1;
std::basic_string<TCHAR> name;
ExpandHotkeyName(name, aActionItems[i].uhk);
lvi.pszText = (LPTSTR)name.c_str();
if (ListView_SetItem(hWndList, &lvi) == -1)
return FALSE;
}
// key remaps
int idx = _countof(aActionItems);
lvi.mask = LVIF_TEXT | LVIF_PARAM;
lvi.iImage = -1;
for (std::map<UHK, std::map<HKL, UHK>>::iterator i = g_hotkeyInfo.begin();
i != g_hotkeyInfo.end(); i++) {
// if (!i->first.bRemapHK)
if (eHKTRemap != i->first.bits.btHKType)
continue;
lvi.mask |= LVIF_PARAM;
lvi.iItem = idx++;
lvi.iSubItem = 0;
std::basic_string<TCHAR> name;
ExpandHotkeyName(name, i->first);
lvi.pszText = (LPTSTR)name.c_str();
lvi.lParam = (LPARAM)i->first.ulKey;
if (ListView_InsertItem(hWndList, &lvi) == -1)
return FALSE;
lvi.mask &= ~(LVIF_PARAM);
lvi.iSubItem = 1;
UHK uhk(i->second.empty() ? 0 : i->second.begin()->second);
name.clear();
ExpandHotkeyName(name, uhk);
lvi.pszText = (LPTSTR)name.c_str();
if (ListView_SetItem(hWndList, &lvi) == -1)
return FALSE;
}
#if 0 // disable remaps
// <new remap> entry
UHK uhk(0);
// uhk.bRemapHK = 1;
uhk.btHKType = eHKTRemap;
lvi.mask = LVIF_TEXT | LVIF_PARAM;
lvi.iItem = idx;
lvi.iSubItem = 0;
lvi.pszText = (LPTSTR)_T("<new remap>");
lvi.lParam = (LPARAM)uhk.ulKey;
if (ListView_InsertItem(hWndList, &lvi) == -1)
return FALSE;
#endif
return TRUE;
}
static BOOL InitLanguagesList(HWND hwnd)
{
HWND hWndList = GetDlgItem(hwnd, IDC_LIST_LANGUAGES);
DWORD dwStyleEx = LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT
| LVS_EX_AUTOSIZECOLUMNS | LVS_EX_BORDERSELECT;
ListView_SetExtendedListViewStyleEx(hWndList, dwStyleEx, dwStyleEx);
ListView_SetBkColor(hWndList, GetSysColor(COLOR_BTNFACE));
ListView_SetTextBkColor(hWndList, GetSysColor(COLOR_BTNFACE));
// init columns
struct {
const TCHAR* pszName;
int iWidth;
} aColumns[] = {
{ _T("+"), 20 },
{ _T("Flag"), 50 },
{ _T("Input Language"), 200 },
{ _T("Scroll LED"), 70 },
{ _T("Overlay hint"), 100 }
};
LVCOLUMN lvc = { 0 };
lvc.mask = LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
for (int i = 0; i < sizeof(aColumns) / sizeof(aColumns[0]); i++) {
lvc.iSubItem = i;
lvc.pszText = (TCHAR*)aColumns[i].pszName;
lvc.cx = aColumns[i].iWidth;
if (ListView_InsertColumn(hWndList, i, &lvc) == -1)
return FALSE;
}
// Add items for the languages
LVITEM lvi = { 0 };
for (std::map<HKL, KeyboardLayoutInfo>::iterator i = g_keyboardInfo.begin();
i != g_keyboardInfo.end(); i++) {
lvi.mask = LVIF_PARAM;
lvi.iItem = ( int )std::distance(g_keyboardInfo.begin(), i);
lvi.iSubItem = 0;
lvi.lParam = (LPARAM)i->first;
if (ListView_InsertItem(hWndList, &lvi) == -1)
return FALSE;
lvi.mask = LVIF_TEXT;
lvi.iSubItem = 2;
lvi.iImage = 0;
lvi.pszText = (LPTSTR)i->second.name.c_str();
if (ListView_SetItem(hWndList, &lvi) == -1)
return FALSE;
}
return FALSE;
}
static BOOL InitGroupsList(HWND hwnd)
{
HWND hWndList = GetDlgItem(hwnd, IDC_LIST_GROUPS);
DWORD dwStyleEx = LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT
| LVS_EX_AUTOSIZECOLUMNS | LVS_EX_BORDERSELECT;
ListView_SetExtendedListViewStyleEx(hWndList, dwStyleEx, dwStyleEx);
ListView_SetBkColor(hWndList, GetSysColor(COLOR_BTNFACE));
ListView_SetTextBkColor(hWndList, GetSysColor(COLOR_BTNFACE));
// Prepare imagelist
//HWND hWndHeader =
ListView_GetHeader(hWndList);
// init columns
LVCOLUMN lvc = { 0 };
lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
// lvc.iSubItem = i;
lvc.pszText = (TCHAR*)_T("Group Hotkey");
lvc.cx = 150;
if (ListView_InsertColumn(hWndList, 0, &lvc) == -1)
return FALSE;
int nWidth = (420 - 150) / ( int )g_keyboardInfo.size();
if (nWidth < 50)
nWidth = 50;
for (std::map<HKL, KeyboardLayoutInfo>::iterator i = g_keyboardInfo.begin();
i != g_keyboardInfo.end(); i++) {
lvc.iSubItem = 1 + ( int )std::distance(g_keyboardInfo.begin(), i);
std::basic_string<TCHAR> str(i->second.id);
//str += _T(" *");
lvc.pszText = (TCHAR*)str.c_str();
lvc.cx = nWidth;
lvc.fmt = LVCFMT_CENTER; // Right-aligned column.
if (ListView_InsertColumn(hWndList,
1 + std::distance(g_keyboardInfo.begin(), i), &lvc) == -1)
return FALSE;
}
// Add items for hotkeys
LVITEM lvi = { 0 };
// Initialize LVITEM members that are common to all items.
lvi.mask = LVIF_TEXT | LVIF_PARAM;
lvi.iImage = -1;
for (std::map<UHK, std::map<HKL, UHK>>::iterator i = g_hotkeyInfo.begin();
i != g_hotkeyInfo.end(); i++) {
if (eHKTGroup != i->first.bits.btHKType)
continue;
lvi.iItem = ( int )std::distance(g_hotkeyInfo.begin(), i);
lvi.iSubItem = 0;
std::basic_string<TCHAR> name;
ExpandHotkeyName(name, i->first);
lvi.pszText = (LPTSTR)name.c_str();
lvi.lParam = (LPARAM)i->first.ulKey;
if (ListView_InsertItem(hWndList, &lvi) == -1)
return FALSE;
}
lvi.iItem = ( int )g_hotkeyInfo.size();
lvi.iSubItem = 0;
lvi.iImage = ( int )g_hotkeyInfo.size();
lvi.pszText = (LPTSTR)_T("<add new group>");
lvi.lParam = (LPARAM)0;
if (ListView_InsertItem(hWndList, &lvi) == -1)
return FALSE;
return FALSE;
}
static void ReposControls(HWND hwnd,
int nHeightDelta, int nLocalesDelta, int nGroupsDelta)
{
const int cnCount = sizeof(st_aControls) / sizeof(*st_aControls);
for (int i = 0; i < cnCount; i++) {
GetWindowRect(st_aControls[i].hwnd, &st_aControls[i].rc);
MapWindowPoints(HWND_DESKTOP, hwnd, (LPPOINT)&st_aControls[i].rc, 2);
}
int nDelta = nHeightDelta - nLocalesDelta - nGroupsDelta;
st_aControls[eHotkeysList].rc.bottom += nDelta;
st_aControls[eLocalesList].rc.top += nDelta;
nDelta += nLocalesDelta;
st_aControls[eLocalesList].rc.bottom += nDelta;
st_aControls[eGroupsList].rc.top += nDelta;
nDelta += nGroupsDelta;
st_aControls[eGroupsList].rc.bottom += nDelta;
for (int i = eHintStatic; i < cnCount; i++) {
st_aControls[i].rc.top += nDelta;
st_aControls[i].rc.bottom += nDelta;
}
for (int i = 0; i < cnCount; i++) {
SetWindowPos(st_aControls[i].hwnd, NULL,
st_aControls[i].rc.left, st_aControls[i].rc.top,
st_aControls[i].rc.right - st_aControls[i].rc.left,
st_aControls[i].rc.bottom - st_aControls[i].rc.top, SWP_NOZORDER);
InvalidateRect(st_aControls[i].hwnd, NULL, TRUE);
}
}
static BOOL OnInitConfigureDialog(HWND hwnd, HWND /*hwndFocus*/, LPARAM /*lParam*/)
{
SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)g_hIcon);
st_hImageList16 = ImageList_Create(16, 16, ILC_MASK, ( int )g_keyboardInfo.size(), 0);
for (std::map<HKL, KeyboardLayoutInfo>::iterator i = g_keyboardInfo.begin();
i != g_keyboardInfo.end(); i++)
ImageList_AddIcon(st_hImageList16,
i->second.inUse ? i->second.iconColor : i->second.iconGray);
ImageList_AddIcon(st_hImageList16,
LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_CHECK)));
InitLanguagesList(hwnd);
InitHotkeysList(hwnd);
InitGroupsList(hwnd);
RECT rc;
GetWindowRect(hwnd, &rc);
st_szInitDlgSize.cx = rc.right - rc.left;
st_szInitDlgSize.cy = rc.bottom - rc.top;
SetWindowPos(hwnd, NULL, rc.left, rc.top - g_nHeightDelta / 2,
rc.right - rc.left, rc.bottom - rc.top + g_nHeightDelta, SWP_NOZORDER);
for (int i = 0; i < sizeof(st_aControls) / sizeof(*st_aControls); i++) {
st_aControls[i].hwnd = GetDlgItem(hwnd, st_aControls[i].uiId);
GetWindowRect(st_aControls[i].hwnd, &st_aControls[i].rc);
}
st_nInitHotkeysHeight = st_aControls[eHotkeysList].rc.bottom
- st_aControls[eHotkeysList].rc.top;
st_nInitLocalesHeight = st_aControls[eLocalesList].rc.bottom
- st_aControls[eLocalesList].rc.top;
st_nInitGroupsHeight = st_aControls[eGroupsList].rc.bottom
- st_aControls[eGroupsList].rc.top;
ReposControls(hwnd, g_nHeightDelta, g_nLocalesDelta, g_nGroupsDelta);
return TRUE;
}
static LRESULT OnHotkeysClick(HWND hwnd, NMHDR* pnmhdr)
{
do {
LPNMITEMACTIVATE lpnma = (LPNMITEMACTIVATE)pnmhdr;
LVHITTESTINFO lvhti = { 0 };
lvhti.pt = lpnma->ptAction;
if (-1 == ListView_SubItemHitTest(pnmhdr->hwndFrom, &lvhti))
break;
LVITEM lvi = { 0 };
lvi.mask = LVIF_PARAM | LVIF_TEXT;
lvi.iItem = lvhti.iItem;
std::vector<TCHAR> text(128);
lvi.pszText = &text[0];
lvi.cchTextMax = ( int )text.size();
ListView_GetItem(pnmhdr->hwndFrom, &lvi);
UHK uhk = (UHK)(( unsigned long )lvi.lParam);
// if (1 != lvhti.iSubItem && !uhk.bRemapHK) // Name and not remapping hotkey
if (1 != lvhti.iSubItem && eHKTRemap != uhk.bits.btHKType) // Name and not remapping hotkey
break;
// if (uhk.bCapsHK) { // Caps Lock hotkey is not editable.
if (eHKTCaps == uhk.bits.btHKType) { // Caps Lock hotkey is not editable.
MessageBox(NULL,
_T("A 'Toggle Capslock key' hotkey is not editable.\nChange the 'Cycle Switch' one instead."),
_T("Recaps"), MB_OK | MB_ICONINFORMATION | MB_TASKMODAL | MB_TOPMOST);
break;
}
std::basic_string<TCHAR> caption(&text[0]);
HotkeyDialogInit InitData(uhk, caption.c_str());
// if (uhk.bRemapHK) {
if (eHKTRemap == uhk.bits.btHKType) {
switch (lvhti.iSubItem) {
case 1: {
std::map<UHK, std::map<HKL, UHK>>::iterator
i = g_hotkeyInfo.find(uhk);
if (i == g_hotkeyInfo.end() || i->second.empty())
continue;
InitData.uhk = i->second.begin()->second;
InitData.szCaption = _T("Remap Target hotkey");
break;
}
case 0:
InitData.szCaption = _T("Remap Source hotkey");
break;
default:
continue;
}
} else {
caption.insert(0, _T("Action hotkey: "));
InitData.szCaption = caption.c_str();
}
g_bCustomizingOn = true;
st_dwPrevKeysCount = g_dwKeysCount;
INT_PTR nResult = DialogBoxParam(g_hInstance,
MAKEINTRESOURCE(IDD_SELECT_HOTKEY_DIALOG),
hwnd, ShortCutDialogProc, (LPARAM)&InitData);
g_bCustomizingOn = false;
if (IDOK != nResult)
break;
// if (st_uhkCustHotkey.bRemapHK
if (eHKTRemap == st_uhkCustHotkey.bits.btHKType
&& 0 == st_uhkCustHotkey.hotkeys.ulHotKey
&& 0 == lvhti.iSubItem) { // Deleting remaping hotkey
std::map<UHK, std::map<HKL, UHK>>::iterator
i = g_hotkeyInfo.find(uhk);
if (i != g_hotkeyInfo.end())
g_hotkeyInfo.erase(i);
ListView_DeleteItem(pnmhdr->hwndFrom, lvhti.iItem);
break;
}
// param - exclude if remap target
// if (!(uhk.bRemapHK && 1 == lvhti.iSubItem)) {
if (!(eHKTRemap == uhk.bits.btHKType && 1 == lvhti.iSubItem)) {
lvi.mask = LVIF_PARAM;
lvi.iItem = lvhti.iItem;
lvi.iSubItem = 0;
lvi.lParam = (LPARAM)st_uhkCustHotkey.ulKey;
// if (uhk.bRemapHK && 0 == uhk.ulHotKey) { // new
if (eHKTRemap == uhk.bits.btHKType && 0 == uhk.hotkeys.ulHotKey) { // new
ListView_InsertItem(pnmhdr->hwndFrom, &lvi);
lvi.mask = LVIF_TEXT;
lvi.iSubItem = 1;
std::basic_string<TCHAR> hotkeyName;
ExpandHotkeyName(hotkeyName, UHK(0));
lvi.pszText = (LPTSTR)hotkeyName.c_str();
ListView_SetItem(pnmhdr->hwndFrom, &lvi);
} else
ListView_SetItem(pnmhdr->hwndFrom, &lvi);
}
// text
lvi.mask = LVIF_TEXT;
lvi.iItem = lvhti.iItem;
lvi.iSubItem = lvhti.iSubItem;
std::basic_string<TCHAR> hotkeyName;
ExpandHotkeyName(hotkeyName, st_uhkCustHotkey);
lvi.pszText = (LPTSTR)hotkeyName.c_str();
ListView_SetItem(pnmhdr->hwndFrom, &lvi);
//if (st_uhkCustHotkey.bCycleHK) {
if (eHKTCycle == st_uhkCustHotkey.bits.btHKType) {
lvi.iItem = 3;
if (st_uhkCustHotkey.bits.btVK == VK_CAPITAL)
lvi.pszText = (LPTSTR)_T("L.Alt + Caps Lock");
else
lvi.pszText = (LPTSTR)_T("Caps Lock");
ListView_SetItem(pnmhdr->hwndFrom, &lvi);
}
if (st_uhkCustHotkey.hotkeys.ulHotKey != InitData.uhk.hotkeys.ulHotKey) {
std::map<UHK, std::map<HKL, UHK>>::iterator
i = g_hotkeyInfo.find(uhk);
// if (uhk.bRemapHK && 1 == lvhti.iSubItem) {
if (eHKTRemap == uhk.bits.btHKType && 1 == lvhti.iSubItem) {
if (i != g_hotkeyInfo.end())
if (!i->second.empty())
i->second.begin()->second = st_uhkCustHotkey;
// } else if (uhk.bRemapHK && 0 == uhk.ulHotKey) { // new
} else if (eHKTRemap == uhk.bits.btHKType && 0 == uhk.hotkeys.ulHotKey) { // new
std::map<HKL, UHK> remap;
remap.insert(std::pair<HKL, UHK>(0, 0));
g_hotkeyInfo.insert(
std::pair<UHK, std::map<HKL, UHK>>(st_uhkCustHotkey, remap));
} else {
// write back to map
if (i != g_hotkeyInfo.end()) {
std::map<HKL, UHK> remap;
if (!i->second.empty())
remap.insert(std::pair<HKL, UHK>(0, i->second.begin()->second));
g_hotkeyInfo.erase(i);
g_hotkeyInfo.insert(
std::pair<UHK, std::map<HKL, UHK>>(st_uhkCustHotkey, remap));
}
}
}
} while (false);
return 0;
}
static LRESULT OnLanguagesClick(HWND hwnd, NMHDR* pnmhdr)
{
LPNMITEMACTIVATE lpnma = (LPNMITEMACTIVATE)pnmhdr;
LVHITTESTINFO lvhti = { 0 };
lvhti.pt = lpnma->ptAction;
if (-1 != ListView_SubItemHitTest(pnmhdr->hwndFrom, &lvhti)) {
LVITEM lvi = { 0 };
lvi.mask = LVIF_PARAM;
lvi.iItem = lvhti.iItem;
ListView_GetItem(pnmhdr->hwndFrom, &lvi);
HKL hkl = (HKL)(lvi.lParam);
lvi.mask = LVIF_TEXT;
lvi.pszText = (LPTSTR)"";
lvi.iItem = lvhti.iItem;
lvi.iSubItem = lvhti.iSubItem;
bool bUpdateIcon = false;
switch (lvhti.iSubItem) {
case 0: // Active
g_keyboardInfo[hkl].inUse = !g_keyboardInfo[hkl].inUse;
for (std::map<UHK, std::map<HKL, UHK>>::iterator
hk_i = g_hotkeyInfo.begin(); hk_i != g_hotkeyInfo.end(); hk_i++)
for (std::map<HKL, UHK>::iterator i = hk_i->second.begin();
i != hk_i->second.end(); i++)
if (hkl == i->first)
i->second = g_keyboardInfo[hkl].inUse;
bUpdateIcon = true;
break;
case 1: // Flag
lvi.mask = LVIF_IMAGE;
lvi.iImage = lvhti.iItem;
if (LoadExternalIcon(&g_keyboardInfo[hkl].iconColor,
g_keyboardInfo[hkl].name.c_str())) {
g_keyboardInfo[hkl].iconGray
= CreateGrayIcon(g_keyboardInfo[hkl].iconColor);
bUpdateIcon = true;
}
break;
case 2: // Name
lvi.mask = 0;
break;
case 3: // Scroll LED
g_keyboardInfo[hkl].useLED = !g_keyboardInfo[hkl].useLED;
break;
case 4: // Overlay
g_keyboardInfo[hkl].showIcon = !g_keyboardInfo[hkl].showIcon;
break;
}
if (bUpdateIcon) {
ImageList_ReplaceIcon(st_hImageList16, lvhti.iItem,
g_keyboardInfo[hkl].inUse
? g_keyboardInfo[hkl].iconColor : g_keyboardInfo[hkl].iconGray);
InvalidateRect(GetDlgItem(hwnd, IDC_LIST_GROUPS), NULL, TRUE);
}
ListView_SetItem(pnmhdr->hwndFrom, &lvi);
}
return 0;
}
static LRESULT OnGroupsClick(HWND hwnd, NMHDR* pnmhdr)
{
LPNMITEMACTIVATE lpnma = (LPNMITEMACTIVATE)pnmhdr;
LVHITTESTINFO lvhti = { 0 };
lvhti.pt = lpnma->ptAction;
if (-1 != ListView_SubItemHitTest(pnmhdr->hwndFrom, &lvhti)) {
LVITEM lvi_ = { 0 };
lvi_.mask = LVIF_PARAM | LVIF_TEXT;
std::vector<TCHAR> text(128);
lvi_.pszText = &text[0];
lvi_.cchTextMax = ( int )text.size();
lvi_.iItem = lvhti.iItem;
ListView_GetItem(pnmhdr->hwndFrom, &lvi_);
UHK uhk = (UHK)(( unsigned long )lvi_.lParam);
std::basic_string<TCHAR> caption(_T("Group hotkey: "));
caption += &text[0];
lvi_.mask = LVIF_TEXT;
lvi_.pszText = (LPTSTR)_T("");
lvi_.iItem = lvhti.iItem;
lvi_.iSubItem = lvhti.iSubItem;
if (0 == lvhti.iSubItem) { // Hotkey
lvi_.mask = 0;
HotkeyDialogInit InitData(uhk, caption.c_str(), _T("Delete"));
g_bCustomizingOn = true;
st_dwPrevKeysCount = g_dwKeysCount;
do {
if (IDOK != DialogBoxParam(g_hInstance,
MAKEINTRESOURCE(IDD_SELECT_HOTKEY_DIALOG),
hwnd, ShortCutDialogProc, (LPARAM)&InitData))
break;
if (st_uhkCustHotkey.hotkeys.ulHotKey == uhk.hotkeys.ulHotKey)
break;
if (0 == st_uhkCustHotkey.hotkeys.ulHotKey) { // Disabled
std::map<UHK, std::map<HKL, UHK>>::iterator
i = g_hotkeyInfo.find(uhk);
if (i != g_hotkeyInfo.end())
g_hotkeyInfo.erase(i);
ListView_DeleteItem(pnmhdr->hwndFrom, lvhti.iItem);
break;
}
LVITEM lvi = { 0 };
lvi.mask = LVIF_PARAM | LVIF_TEXT;
lvi.iItem = lvhti.iItem;
lvi.iSubItem = 0;
lvi.lParam = (LPARAM)st_uhkCustHotkey.ulKey;
std::basic_string<TCHAR> hotkeyName;
ExpandHotkeyName(hotkeyName, st_uhkCustHotkey);