forked from microsoft/PowerToys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPowerRenameUI.cpp
1284 lines (1098 loc) · 33.7 KB
/
PowerRenameUI.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 "pch.h"
#include "resource.h"
#include "PowerRenameUI.h"
#include "dpi_aware.h"
#include <commctrl.h>
#include <Shlobj.h>
#include <helpers.h>
#include <windowsx.h>
#include <thread>
#include <trace.h>
extern HINSTANCE g_hInst;
int g_rgnMatchModeResIDs[] = {
IDS_ENTIREITEMNAME,
IDS_NAMEONLY,
IDS_EXTENSIONONLY
};
enum
{
MATCHMODE_FULLNAME = 0,
MATCHMODE_NAMEONLY,
MATCHMODE_EXTENIONONLY
};
struct FlagCheckboxMap
{
DWORD flag;
DWORD id;
};
FlagCheckboxMap g_flagCheckboxMap[] = {
{ UseRegularExpressions, IDC_CHECK_USEREGEX },
{ ExcludeSubfolders, IDC_CHECK_EXCLUDESUBFOLDERS },
{ EnumerateItems, IDC_CHECK_ENUMITEMS },
{ ExcludeFiles, IDC_CHECK_EXCLUDEFILES },
{ CaseSensitive, IDC_CHECK_CASESENSITIVE },
{ MatchAllOccurences, IDC_CHECK_MATCHALLOCCURENCES },
{ ExcludeFolders, IDC_CHECK_EXCLUDEFOLDERS },
{ NameOnly, IDC_CHECK_NAMEONLY },
{ ExtensionOnly, IDC_CHECK_EXTENSIONONLY }
};
struct RepositionMap
{
DWORD id;
DWORD flags;
};
enum
{
Reposition_None = 0,
Reposition_X = 0x1,
Reposition_Y = 0x2,
Reposition_Width = 0x4,
Reposition_Height = 0x8
};
RepositionMap g_repositionMap[] = {
{ IDC_SEARCHREPLACEGROUP, Reposition_Width },
{ IDC_OPTIONSGROUP, Reposition_Width },
{ IDC_PREVIEWGROUP, Reposition_Width | Reposition_Height },
{ IDC_EDIT_SEARCHFOR, Reposition_Width },
{ IDC_EDIT_REPLACEWITH, Reposition_Width },
{ IDC_LIST_PREVIEW, Reposition_Width | Reposition_Height },
{ IDC_STATUS_MESSAGE, Reposition_Y },
{ ID_RENAME, Reposition_X | Reposition_Y },
{ ID_ABOUT, Reposition_X | Reposition_Y },
{ IDCANCEL, Reposition_X | Reposition_Y }
};
inline int RECT_WIDTH(RECT& r)
{
return r.right - r.left;
}
inline int RECT_HEIGHT(RECT& r)
{
return r.bottom - r.top;
}
CPowerRenameUI::CPowerRenameUI() :
m_refCount(1)
{
CSettingsInstance().Load();
(void)OleInitialize(nullptr);
ModuleAddRef();
}
// IUnknown
IFACEMETHODIMP CPowerRenameUI::QueryInterface(__in REFIID riid, __deref_out void** ppv)
{
static const QITAB qit[] = {
QITABENT(CPowerRenameUI, IPowerRenameUI),
QITABENT(CPowerRenameUI, IPowerRenameManagerEvents),
QITABENT(CPowerRenameUI, IDropTarget),
{ 0 },
};
return QISearch(this, qit, riid, ppv);
}
IFACEMETHODIMP_(ULONG)
CPowerRenameUI::AddRef()
{
return InterlockedIncrement(&m_refCount);
}
IFACEMETHODIMP_(ULONG)
CPowerRenameUI::Release()
{
long refCount = InterlockedDecrement(&m_refCount);
if (refCount == 0)
{
delete this;
}
return refCount;
}
HRESULT CPowerRenameUI::s_CreateInstance(_In_ IPowerRenameManager* psrm, _In_opt_ IUnknown* dataSource, _In_ bool enableDragDrop, _Outptr_ IPowerRenameUI** ppsrui)
{
*ppsrui = nullptr;
CPowerRenameUI* prui = new CPowerRenameUI();
HRESULT hr = prui ? S_OK : E_OUTOFMEMORY;
if (SUCCEEDED(hr))
{
// Pass the IPowerRenameManager to the IPowerRenameUI so it can subscribe to events
hr = prui->_Initialize(psrm, dataSource, enableDragDrop);
if (SUCCEEDED(hr))
{
hr = prui->QueryInterface(IID_PPV_ARGS(ppsrui));
}
prui->Release();
}
return hr;
}
// IPowerRenameUI
IFACEMETHODIMP CPowerRenameUI::Show(_In_opt_ HWND hwndParent)
{
return _DoModeless(hwndParent);
}
IFACEMETHODIMP CPowerRenameUI::Close()
{
_OnCloseDlg();
return S_OK;
}
IFACEMETHODIMP CPowerRenameUI::Update()
{
return S_OK;
}
IFACEMETHODIMP CPowerRenameUI::get_hwnd(_Out_ HWND* hwnd)
{
*hwnd = m_hwnd;
return S_OK;
}
IFACEMETHODIMP CPowerRenameUI::get_showUI(_Out_ bool* showUI)
{
// Let callers know that it is OK to show UI (ex: progress dialog, error dialog and conflict dialog UI)
*showUI = true;
return S_OK;
}
// IPowerRenameManagerEvents
IFACEMETHODIMP CPowerRenameUI::OnItemAdded(_In_ IPowerRenameItem*)
{
return S_OK;
}
IFACEMETHODIMP CPowerRenameUI::OnUpdate(_In_ IPowerRenameItem*)
{
UINT itemCount = 0;
if (m_spsrm)
{
m_spsrm->GetItemCount(&itemCount);
}
m_listview.RedrawItems(0, itemCount);
_UpdateCounts();
return S_OK;
}
IFACEMETHODIMP CPowerRenameUI::OnError(_In_ IPowerRenameItem*)
{
return S_OK;
}
IFACEMETHODIMP CPowerRenameUI::OnRegExStarted(_In_ DWORD threadId)
{
m_disableCountUpdate = true;
m_currentRegExId = threadId;
_UpdateCounts();
return S_OK;
}
IFACEMETHODIMP CPowerRenameUI::OnRegExCanceled(_In_ DWORD threadId)
{
if (m_currentRegExId == threadId)
{
m_disableCountUpdate = false;
_UpdateCounts();
}
return S_OK;
}
IFACEMETHODIMP CPowerRenameUI::OnRegExCompleted(_In_ DWORD threadId)
{
// Enable list view
if (m_currentRegExId == threadId)
{
m_disableCountUpdate = false;
_UpdateCounts();
}
return S_OK;
}
IFACEMETHODIMP CPowerRenameUI::OnRenameStarted()
{
// Disable controls
EnableWindow(m_hwnd, FALSE);
return S_OK;
}
IFACEMETHODIMP CPowerRenameUI::OnRenameCompleted()
{
// Enable controls
EnableWindow(m_hwnd, TRUE);
// Close the window
PostMessage(m_hwnd, WM_CLOSE, (WPARAM)0, (LPARAM)0);
return S_OK;
}
// IDropTarget
IFACEMETHODIMP CPowerRenameUI::DragEnter(_In_ IDataObject* pdtobj, DWORD /* grfKeyState */, POINTL pt, _Inout_ DWORD* pdwEffect)
{
if (m_spdth)
{
POINT ptT = { pt.x, pt.y };
m_spdth->DragEnter(m_hwnd, pdtobj, &ptT, *pdwEffect);
}
return S_OK;
}
IFACEMETHODIMP CPowerRenameUI::DragOver(DWORD /* grfKeyState */, POINTL pt, _Inout_ DWORD* pdwEffect)
{
if (m_spdth)
{
POINT ptT = { pt.x, pt.y };
m_spdth->DragOver(&ptT, *pdwEffect);
}
return S_OK;
}
IFACEMETHODIMP CPowerRenameUI::DragLeave()
{
if (m_spdth)
{
m_spdth->DragLeave();
}
return S_OK;
}
IFACEMETHODIMP CPowerRenameUI::Drop(_In_ IDataObject* pdtobj, DWORD, POINTL pt, _Inout_ DWORD* pdwEffect)
{
if (m_spdth)
{
POINT ptT = { pt.x, pt.y };
m_spdth->Drop(pdtobj, &ptT, *pdwEffect);
}
EnableWindow(GetDlgItem(m_hwnd, ID_RENAME), TRUE);
EnableWindow(m_hwndLV, TRUE);
// Populate the manager from the data object
if (m_spsrm)
{
_EnumerateItems(pdtobj);
}
return S_OK;
}
HRESULT CPowerRenameUI::_Initialize(_In_ IPowerRenameManager* psrm, _In_opt_ IUnknown* dataSource, _In_ bool enableDragDrop)
{
// Cache the rename manager
m_spsrm = psrm;
// Cache the data source for enumeration later
m_dataSource = dataSource;
m_enableDragDrop = enableDragDrop;
HRESULT hr = CoCreateInstance(CLSID_DragDropHelper, NULL, CLSCTX_INPROC, IID_PPV_ARGS(&m_spdth));
if (SUCCEEDED(hr))
{
// Subscribe to rename manager events
hr = m_spsrm->Advise(this, &m_cookie);
}
if (FAILED(hr))
{
_Cleanup();
}
return hr;
}
HRESULT CPowerRenameUI::_InitAutoComplete()
{
HRESULT hr = S_OK;
if (CSettingsInstance().GetMRUEnabled())
{
hr = CoCreateInstance(CLSID_AutoComplete, NULL, CLSCTX_INPROC, IID_PPV_ARGS(&m_spSearchAC));
if (SUCCEEDED(hr))
{
hr = CRenameMRUSearch_CreateInstance(&m_spSearchACL);
if (SUCCEEDED(hr))
{
hr = m_spSearchAC->Init(GetDlgItem(m_hwnd, IDC_EDIT_SEARCHFOR), m_spSearchACL, nullptr, nullptr);
if (SUCCEEDED(hr))
{
hr = m_spSearchAC->SetOptions(ACO_AUTOSUGGEST | ACO_AUTOAPPEND | ACO_UPDOWNKEYDROPSLIST);
}
}
}
if (SUCCEEDED(hr))
{
hr = CoCreateInstance(CLSID_AutoComplete, NULL, CLSCTX_INPROC, IID_PPV_ARGS(&m_spReplaceAC));
if (SUCCEEDED(hr))
{
hr = CRenameMRUReplace_CreateInstance(&m_spReplaceACL);
if (SUCCEEDED(hr))
{
hr = m_spReplaceAC->Init(GetDlgItem(m_hwnd, IDC_EDIT_REPLACEWITH), m_spReplaceACL, nullptr, nullptr);
if (SUCCEEDED(hr))
{
hr = m_spReplaceAC->SetOptions(ACO_AUTOSUGGEST | ACO_AUTOAPPEND | ACO_UPDOWNKEYDROPSLIST);
}
}
}
}
}
return hr;
}
void CPowerRenameUI::_Cleanup()
{
if (m_spsrm && m_cookie != 0)
{
m_spsrm->UnAdvise(m_cookie);
m_cookie = 0;
m_spsrm = nullptr;
}
m_dataSource = nullptr;
m_spdth = nullptr;
if (m_enableDragDrop)
{
RevokeDragDrop(m_hwnd);
}
m_hwnd = NULL;
}
void CPowerRenameUI::_EnumerateItems(_In_ IUnknown* pdtobj)
{
// Enumerate the data object and populate the manager
if (m_spsrm)
{
m_disableCountUpdate = true;
EnumerateDataObject(pdtobj, m_spsrm);
m_disableCountUpdate = false;
UINT itemCount = 0;
m_spsrm->GetItemCount(&itemCount);
m_listview.SetItemCount(itemCount);
_UpdateCounts();
}
}
HRESULT CPowerRenameUI::_ReadSettings()
{
// Check if we should read flags from settings
// or the defaults from the manager.
DWORD flags = 0;
if (CSettingsInstance().GetPersistState())
{
flags = CSettingsInstance().GetFlags();
m_spsrm->put_flags(flags);
SetDlgItemText(m_hwnd, IDC_EDIT_SEARCHFOR, CSettingsInstance().GetSearchText().c_str());
SetDlgItemText(m_hwnd, IDC_EDIT_REPLACEWITH, CSettingsInstance().GetReplaceText().c_str());
}
else
{
m_spsrm->get_flags(&flags);
}
_SetCheckboxesFromFlags(flags);
return S_OK;
}
HRESULT CPowerRenameUI::_WriteSettings()
{
// Check if we should store our settings
if (CSettingsInstance().GetPersistState())
{
DWORD flags = 0;
m_spsrm->get_flags(&flags);
CSettingsInstance().SetFlags(flags);
wchar_t buffer[CSettings::MAX_INPUT_STRING_LEN];
buffer[0] = L'\0';
GetDlgItemText(m_hwnd, IDC_EDIT_SEARCHFOR, buffer, ARRAYSIZE(buffer));
CSettingsInstance().SetSearchText(buffer);
if (CSettingsInstance().GetMRUEnabled() && m_spSearchACL)
{
CComPtr<IPowerRenameMRU> spSearchMRU;
if (SUCCEEDED(m_spSearchACL->QueryInterface(IID_PPV_ARGS(&spSearchMRU))))
{
spSearchMRU->AddMRUString(buffer);
}
}
buffer[0] = L'\0';
GetDlgItemText(m_hwnd, IDC_EDIT_REPLACEWITH, buffer, ARRAYSIZE(buffer));
CSettingsInstance().SetReplaceText(buffer);
if (CSettingsInstance().GetMRUEnabled() && m_spReplaceACL)
{
CComPtr<IPowerRenameMRU> spReplaceMRU;
if (SUCCEEDED(m_spReplaceACL->QueryInterface(IID_PPV_ARGS(&spReplaceMRU))))
{
spReplaceMRU->AddMRUString(buffer);
}
}
Trace::SettingsChanged();
}
return S_OK;
}
void CPowerRenameUI::_OnCloseDlg()
{
if (m_hwnd != NULL)
{
if (m_modeless)
{
DestroyWindow(m_hwnd);
}
else
{
EndDialog(m_hwnd, 1);
}
}
}
void CPowerRenameUI::_OnDestroyDlg()
{
_Cleanup();
if (m_modeless)
{
PostQuitMessage(0);
}
}
void CPowerRenameUI::_OnRename()
{
if (m_spsrm)
{
m_spsrm->Rename(m_hwnd);
}
// Persist the current settings. We only do this when
// a rename is actually performed. Not when the user
// closes/cancels the dialog.
_WriteSettings();
}
void CPowerRenameUI::_OnAbout()
{
// Launch github page
SHELLEXECUTEINFO info = { 0 };
info.cbSize = sizeof(SHELLEXECUTEINFO);
info.lpVerb = L"open";
info.lpFile = L"https://aka.ms/PowerToysOverview_PowerRename";
info.nShow = SW_SHOWDEFAULT;
ShellExecuteEx(&info);
}
HRESULT CPowerRenameUI::_DoModal(__in_opt HWND hwnd)
{
m_modeless = false;
HRESULT hr = S_OK;
INT_PTR ret = DialogBoxParam(g_hInst, MAKEINTRESOURCE(IDD_MAIN), hwnd, s_DlgProc, (LPARAM)this);
if (ret < 0)
{
hr = HRESULT_FROM_WIN32(GetLastError());
}
return hr;
}
void CPowerRenameUI::BecomeForegroundWindow()
{
static INPUT i = { INPUT_MOUSE, {} };
SendInput(1, &i, sizeof(i));
SetForegroundWindow(m_hwnd);
}
HRESULT CPowerRenameUI::_DoModeless(__in_opt HWND hwnd)
{
m_modeless = true;
HRESULT hr = S_OK;
if (NULL != CreateDialogParam(g_hInst, MAKEINTRESOURCE(IDD_MAIN), hwnd, s_DlgProc, (LPARAM)this))
{
ShowWindow(m_hwnd, SW_SHOWNORMAL);
BecomeForegroundWindow();
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
if (!IsDialogMessage(m_hwnd, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
DestroyWindow(m_hwnd);
m_hwnd = NULL;
}
else
{
hr = HRESULT_FROM_WIN32(GetLastError());
}
return hr;
}
INT_PTR CPowerRenameUI::_DlgProc(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
INT_PTR bRet = TRUE; // default for all handled cases in switch below
switch (uMsg)
{
case WM_INITDIALOG:
_OnInitDlg();
break;
case WM_COMMAND:
_OnCommand(wParam, lParam);
break;
case WM_NOTIFY:
bRet = _OnNotify(wParam, lParam);
break;
case WM_THEMECHANGED:
_OnSize(SIZE_RESTORED);
break;
case WM_SIZE:
_OnSize(wParam);
break;
case WM_GETMINMAXINFO:
_OnGetMinMaxInfo(lParam);
break;
case WM_CLOSE:
_OnCloseDlg();
break;
case WM_DESTROY:
_OnDestroyDlg();
break;
default:
bRet = FALSE;
}
return bRet;
}
void CPowerRenameUI::_OnInitDlg()
{
m_hwndLV = GetDlgItem(m_hwnd, IDC_LIST_PREVIEW);
m_listview.Init(m_hwndLV);
if (m_dataSource)
{
// Populate the manager from the data object
_EnumerateItems(m_dataSource);
}
// Initialize from stored settings. Do this now in case we have
// restored a previous search or replace text that needs to be
// evaluated against the items we just enumerated.
_ReadSettings();
// Load the main icon
LoadIconWithScaleDown(g_hInst, MAKEINTRESOURCE(IDI_RENAME), 32, 32, &m_iconMain);
// Update the icon associated with our main app window
SendMessage(m_hwnd, WM_SETICON, (WPARAM)ICON_SMALL, (LPARAM)m_iconMain);
SendMessage(m_hwnd, WM_SETICON, (WPARAM)ICON_BIG, (LPARAM)m_iconMain);
// TODO: put this behind a setting?
if (m_enableDragDrop)
{
RegisterDragDrop(m_hwnd, this);
}
RECT rc = { 0 };
GetWindowRect(m_hwnd, &rc);
m_initialWidth = RECT_WIDTH(rc);
m_initialHeight = RECT_HEIGHT(rc);
UINT dummy = 0;
DPIAware::GetScreenDPIForWindow(m_hwnd, m_initialDPI, dummy);
for (UINT u = 0; u < ARRAYSIZE(g_repositionMap); u++)
{
_CollectItemPosition(g_repositionMap[u].id);
}
_InitAutoComplete();
// Disable rename button by default. It will be enabled in _UpdateCounts if
// there are times to be renamed
EnableWindow(GetDlgItem(m_hwnd, ID_RENAME), FALSE);
// Update UI elements that depend on number of items selected or to be renamed
_UpdateCounts();
m_initialized = true;
}
void CPowerRenameUI::_OnCommand(_In_ WPARAM wParam, _In_ LPARAM lParam)
{
switch (LOWORD(wParam))
{
case IDOK:
case ID_RENAME:
_OnRename();
break;
case ID_ABOUT:
_OnAbout();
break;
case IDCANCEL:
_OnCloseDlg();
break;
case IDC_EDIT_REPLACEWITH:
case IDC_EDIT_SEARCHFOR:
if (GET_WM_COMMAND_CMD(wParam, lParam) == EN_CHANGE)
{
_OnSearchReplaceChanged();
}
break;
case IDC_CHECK_CASESENSITIVE:
case IDC_CHECK_ENUMITEMS:
case IDC_CHECK_EXCLUDEFILES:
case IDC_CHECK_EXCLUDEFOLDERS:
case IDC_CHECK_EXCLUDESUBFOLDERS:
case IDC_CHECK_MATCHALLOCCURENCES:
case IDC_CHECK_USEREGEX:
case IDC_CHECK_EXTENSIONONLY:
case IDC_CHECK_NAMEONLY:
if (BN_CLICKED == HIWORD(wParam))
{
_ValidateFlagCheckbox(LOWORD(wParam));
_GetFlagsFromCheckboxes();
}
break;
}
}
BOOL CPowerRenameUI::_OnNotify(_In_ WPARAM wParam, _In_ LPARAM lParam)
{
bool ret = FALSE;
LPNMHDR pnmdr = (LPNMHDR)lParam;
LPNMLISTVIEW pnmlv = (LPNMLISTVIEW)pnmdr;
NMLVEMPTYMARKUP* pnmMarkup = NULL;
if (pnmdr)
{
BOOL checked = FALSE;
switch (pnmdr->code)
{
case HDN_ITEMSTATEICONCLICK:
if (m_spsrm)
{
m_listview.ToggleAll(m_spsrm, (!(((LPNMHEADER)lParam)->pitem->fmt & HDF_CHECKED)));
_UpdateCounts();
}
break;
case LVN_GETEMPTYMARKUP:
pnmMarkup = (NMLVEMPTYMARKUP*)lParam;
pnmMarkup->dwFlags = EMF_CENTERED;
LoadString(g_hInst, IDS_LISTVIEW_EMPTY, pnmMarkup->szMarkup, ARRAYSIZE(pnmMarkup->szMarkup));
ret = TRUE;
break;
case LVN_BEGINLABELEDIT:
ret = TRUE;
break;
case LVN_KEYDOWN:
if (m_spsrm)
{
m_listview.OnKeyDown(m_spsrm, (LV_KEYDOWN*)pnmdr);
_UpdateCounts();
}
break;
case LVN_GETDISPINFO:
if (m_spsrm)
{
m_listview.GetDisplayInfo(m_spsrm, (LV_DISPINFO*)pnmlv);
}
break;
case NM_CLICK: {
if (m_spsrm)
{
m_listview.OnClickList(m_spsrm, (NM_LISTVIEW*)pnmdr);
_UpdateCounts();
}
break;
}
}
}
return ret;
}
void CPowerRenameUI::_OnGetMinMaxInfo(_In_ LPARAM lParam)
{
if (m_initialWidth)
{
// Prevent resizing the dialog less than the original size
MINMAXINFO* pMinMaxInfo = reinterpret_cast<MINMAXINFO*>(lParam);
pMinMaxInfo->ptMinTrackSize.x = m_initialWidth;
pMinMaxInfo->ptMinTrackSize.y = m_initialHeight;
}
}
void CPowerRenameUI::_OnSize(_In_ WPARAM wParam)
{
if ((wParam == SIZE_RESTORED || wParam == SIZE_MAXIMIZED) && m_initialWidth)
{
for (UINT u = 0; u < ARRAYSIZE(g_repositionMap); u++)
{
_MoveControl(g_repositionMap[u].id, g_repositionMap[u].flags);
}
m_listview.OnSize();
}
}
void CPowerRenameUI::_MoveControl(_In_ DWORD id, _In_ DWORD repositionFlags)
{
HWND hwnd = GetDlgItem(m_hwnd, id);
UINT flags = SWP_NOOWNERZORDER | SWP_NOZORDER | SWP_NOACTIVATE;
if (!((repositionFlags & Reposition_X) || (repositionFlags & Reposition_Y)))
{
flags |= SWP_NOMOVE;
}
if (!((repositionFlags & Reposition_Width) || (repositionFlags & Reposition_Height)))
{
flags |= SWP_NOSIZE;
}
RECT rc = { 0 };
GetWindowRect(m_hwnd, &rc);
int mainWindowWidth = rc.right - rc.left;
int mainWindowHeight = rc.bottom - rc.top;
RECT rcWindow = { 0 };
GetWindowRect(hwnd, &rcWindow);
MapWindowPoints(HWND_DESKTOP, GetParent(hwnd), (LPPOINT)&rcWindow, 2);
int x = rcWindow.left;
int y = rcWindow.top;
int width = rcWindow.right - rcWindow.left;
int height = rcWindow.bottom - rcWindow.top;
UINT currentDPI = 0, dummy;
DPIAware::GetScreenDPIForWindow(m_hwnd, currentDPI, dummy);
float scale = (float)currentDPI / m_initialDPI;
switch (id)
{
case IDC_EDIT_SEARCHFOR:
case IDC_EDIT_REPLACEWITH:
width = mainWindowWidth - static_cast<int>(m_itemsPositioning.searchReplaceWidthDiff * scale);
break;
case IDC_PREVIEWGROUP:
height = mainWindowHeight - static_cast<int>(m_itemsPositioning.previewGroupHeightDiff * scale);
case IDC_SEARCHREPLACEGROUP:
case IDC_OPTIONSGROUP:
width = mainWindowWidth - static_cast<int>(m_itemsPositioning.groupsWidthDiff * scale);
break;
case IDC_LIST_PREVIEW:
width = mainWindowWidth - static_cast<int>(m_itemsPositioning.listPreviewWidthDiff * scale);
height = mainWindowHeight - static_cast<int>(m_itemsPositioning.listPreviewHeightDiff * scale);
break;
case IDC_STATUS_MESSAGE:
y = mainWindowHeight - static_cast<int>(m_itemsPositioning.statusMessageYDiff * scale);
break;
case ID_RENAME:
x = mainWindowWidth - static_cast<int>(m_itemsPositioning.renameButtonXDiff * scale);
y = mainWindowHeight - static_cast<int>(m_itemsPositioning.renameButtonYDiff * scale);
break;
case ID_ABOUT:
x = mainWindowWidth - static_cast<int>(m_itemsPositioning.helpButtonXDiff * scale);
y = mainWindowHeight - static_cast<int>(m_itemsPositioning.helpButtonYDiff * scale);
break;
case IDCANCEL:
x = mainWindowWidth - static_cast<int>(m_itemsPositioning.cancelButtonXDiff * scale);
y = mainWindowHeight - static_cast<int>(m_itemsPositioning.cancelButtonYDiff * scale);
break;
}
SetWindowPos(hwnd, NULL, x, y, width, height, flags);
RedrawWindow(hwnd, NULL, NULL, RDW_INVALIDATE);
}
void CPowerRenameUI::_OnSearchReplaceChanged()
{
// Pass updated search and replace terms to the IPowerRenameRegEx handler
CComPtr<IPowerRenameRegEx> spRegEx;
if (m_spsrm && SUCCEEDED(m_spsrm->get_renameRegEx(&spRegEx)))
{
wchar_t buffer[CSettings::MAX_INPUT_STRING_LEN];
buffer[0] = L'\0';
GetDlgItemText(m_hwnd, IDC_EDIT_SEARCHFOR, buffer, ARRAYSIZE(buffer));
spRegEx->put_searchTerm(buffer);
buffer[0] = L'\0';
GetDlgItemText(m_hwnd, IDC_EDIT_REPLACEWITH, buffer, ARRAYSIZE(buffer));
spRegEx->put_replaceTerm(buffer);
}
}
DWORD CPowerRenameUI::_GetFlagsFromCheckboxes()
{
DWORD flags = 0;
for (int i = 0; i < ARRAYSIZE(g_flagCheckboxMap); i++)
{
if (Button_GetCheck(GetDlgItem(m_hwnd, g_flagCheckboxMap[i].id)) == BST_CHECKED)
{
flags |= g_flagCheckboxMap[i].flag;
}
}
// Ensure we update flags
if (m_spsrm)
{
m_spsrm->put_flags(flags);
}
return flags;
}
void CPowerRenameUI::_SetCheckboxesFromFlags(_In_ DWORD flags)
{
for (int i = 0; i < ARRAYSIZE(g_flagCheckboxMap); i++)
{
Button_SetCheck(GetDlgItem(m_hwnd, g_flagCheckboxMap[i].id), flags & g_flagCheckboxMap[i].flag);
}
}
void CPowerRenameUI::_ValidateFlagCheckbox(_In_ DWORD checkBoxId)
{
if (checkBoxId == IDC_CHECK_NAMEONLY)
{
if (Button_GetCheck(GetDlgItem(m_hwnd, IDC_CHECK_NAMEONLY)) == BST_CHECKED)
{
Button_SetCheck(GetDlgItem(m_hwnd, IDC_CHECK_EXTENSIONONLY), FALSE);
}
}
else if (checkBoxId == IDC_CHECK_EXTENSIONONLY)
{
if (Button_GetCheck(GetDlgItem(m_hwnd, IDC_CHECK_EXTENSIONONLY)) == BST_CHECKED)
{
Button_SetCheck(GetDlgItem(m_hwnd, IDC_CHECK_NAMEONLY), FALSE);
}
}
}
void CPowerRenameUI::_UpdateCounts()
{
// This method is CPU intensive. We disable it during certain operations
// for performance reasons.
if (m_disableCountUpdate)
{
return;
}
UINT selectedCount = 0;
UINT renamingCount = 0;
if (m_spsrm)
{
m_spsrm->GetSelectedItemCount(&selectedCount);
m_spsrm->GetRenameItemCount(&renamingCount);
}
if (m_selectedCount != selectedCount ||
m_renamingCount != renamingCount)
{
m_selectedCount = selectedCount;
m_renamingCount = renamingCount;
// Update selected and rename count label
wchar_t countsLabelFormat[100] = { 0 };
LoadString(g_hInst, IDS_COUNTSLABELFMT, countsLabelFormat, ARRAYSIZE(countsLabelFormat));
wchar_t countsLabel[100] = { 0 };
StringCchPrintf(countsLabel, ARRAYSIZE(countsLabel), countsLabelFormat, selectedCount, renamingCount);
SetDlgItemText(m_hwnd, IDC_STATUS_MESSAGE, countsLabel);
// Update Rename button state
EnableWindow(GetDlgItem(m_hwnd, ID_RENAME), (renamingCount > 0));
}
}
void CPowerRenameUI::_CollectItemPosition(_In_ DWORD id)
{
HWND hwnd = GetDlgItem(m_hwnd, id);
RECT rcWindow = { 0 };
GetWindowRect(hwnd, &rcWindow);
MapWindowPoints(HWND_DESKTOP, GetParent(hwnd), (LPPOINT)&rcWindow, 2);
int itemWidth = rcWindow.right - rcWindow.left;
int itemHeight = rcWindow.bottom - rcWindow.top;
switch (id)
{
case IDC_EDIT_SEARCHFOR:
/* IDC_EDIT_REPLACEWITH uses same value*/
m_itemsPositioning.searchReplaceWidthDiff = m_initialWidth - itemWidth;
break;
case IDC_PREVIEWGROUP:
m_itemsPositioning.previewGroupHeightDiff = m_initialHeight - itemHeight;
break;
case IDC_SEARCHREPLACEGROUP:
/* IDC_OPTIONSGROUP uses same value */
m_itemsPositioning.groupsWidthDiff = m_initialWidth - itemWidth;
break;
case IDC_LIST_PREVIEW:
m_itemsPositioning.listPreviewWidthDiff = m_initialWidth - itemWidth;
m_itemsPositioning.listPreviewHeightDiff = m_initialHeight - itemHeight;
break;
case IDC_STATUS_MESSAGE:
m_itemsPositioning.statusMessageYDiff = m_initialHeight - rcWindow.top;
break;
case ID_RENAME:
m_itemsPositioning.renameButtonXDiff = m_initialWidth - rcWindow.left;
m_itemsPositioning.renameButtonYDiff = m_initialHeight - rcWindow.top;
break;
case ID_ABOUT:
m_itemsPositioning.helpButtonXDiff = m_initialWidth - rcWindow.left;
m_itemsPositioning.helpButtonYDiff = m_initialHeight - rcWindow.top;
break;
case IDCANCEL:
m_itemsPositioning.cancelButtonXDiff = m_initialWidth - rcWindow.left;
m_itemsPositioning.cancelButtonYDiff = m_initialHeight - rcWindow.top;
break;
}
}
void CPowerRenameListView::Init(_In_ HWND hwndLV)
{
if (hwndLV)
{
m_hwndLV = hwndLV;
EnableWindow(m_hwndLV, TRUE);
// Set the standard styles
DWORD dwLVStyle = (DWORD)GetWindowLongPtr(m_hwndLV, GWL_STYLE);