forked from rainmeter/rainmeter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDialogNewSkin.cpp
1940 lines (1645 loc) · 54.3 KB
/
DialogNewSkin.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
/* Copyright (C) 2011 Rainmeter Project Developers
*
* This Source Code Form is subject to the terms of the GNU General Public
* License; either version 2 of the License, or (at your option) any later
* version. If a copy of the GPL was not distributed with this file, You can
* obtain one at <https://www.gnu.org/licenses/gpl-2.0.html>. */
#include "StdAfx.h"
#include "../Common/MenuTemplate.h"
#include "../Common/PathUtil.h"
#include "Rainmeter.h"
#include "System.h"
#include "Util.h"
#include "resource.h"
#include "DialogNewSkin.h"
#include "DialogManage.h"
DialogNewSkin::CloseType DialogNewSkin::c_CloseAction = { false, 0 };
std::vector<std::wstring> DialogNewSkin::c_Templates;
WINDOWPLACEMENT DialogNewSkin::c_WindowPlacement = { 0 };
DialogNewSkin* DialogNewSkin::c_Dialog = nullptr;
DialogNewSkin::DialogNewSkin() : Dialog()
{
}
DialogNewSkin::~DialogNewSkin()
{
}
/*
** Opens by tab index
**
*/
void DialogNewSkin::Open(int tab)
{
if (!c_Dialog)
{
c_Dialog = new DialogNewSkin();
}
c_Dialog->ShowDialogWindow(
GetString(ID_STR_CREATENEWSKIN),
0, 0, 300, 250,
DS_CENTER | WS_POPUP | WS_MINIMIZEBOX | WS_CAPTION | WS_SYSMENU,
WS_EX_APPWINDOW | WS_EX_CONTROLPARENT | ((*GetString(ID_STR_ISRTL) == L'1') ? WS_EX_LAYOUTRTL : 0),
nullptr);
// Fake WM_NOTIFY to change tab
NMHDR nm = { 0 };
nm.code = TCN_SELCHANGE;
nm.idFrom = Id_Tab;
nm.hwndFrom = c_Dialog->GetControl(Id_Tab);
TabCtrl_SetCurSel(nm.hwndFrom, tab);
c_Dialog->OnNotify(0, (LPARAM)&nm);
const HWND& hwnd = c_Dialog->GetWindow();
GetWindowPlacement(hwnd, &c_WindowPlacement);
if (c_WindowPlacement.showCmd == SW_SHOWMINIMIZED)
{
ShowWindow(hwnd, SW_RESTORE);
}
SetForegroundWindow(hwnd);
}
void DialogNewSkin::Open(const WCHAR* tabName, const WCHAR* parent)
{
bool isOpen = c_Dialog ? true : false;
Open(tabName);
if (!isOpen && c_Dialog)
{
// "New" tab
if (_wcsicmp(tabName, L"New") == 0)
{
// |parent| represents the config (ie. "illustro\Clock")
c_Dialog->m_TabNew.SetParentFolder(parent);
}
}
}
void DialogNewSkin::Open(const WCHAR* name)
{
int tab = 0;
if (name)
{
if (_wcsicmp(name, L"Template") == 0)
{
tab = 1;
}
}
Open(tab);
}
void DialogNewSkin::ShowNewSkinDialog()
{
if (!c_Dialog)
{
Open();
}
}
Dialog::Tab& DialogNewSkin::GetActiveTab()
{
int sel = TabCtrl_GetCurSel(GetControl(Id_Tab));
if (sel == 0)
{
return m_TabNew;
}
else // if (sel == 1)
{
return m_TabTemplate;
}
}
INT_PTR DialogNewSkin::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_INITDIALOG:
return OnInitDialog(wParam, lParam);
case WM_ACTIVATE:
return OnActivate(wParam, lParam);
case WM_COMMAND:
return OnCommand(wParam, lParam);
case WM_NOTIFY:
return OnNotify(wParam, lParam);
case WM_CLOSE:
{
if (c_CloseAction.rootFolder && c_CloseAction.skins == 0)
{
// If a root folder was created (without any skins being created), attempt to create a skin using
// the selected template using the name of the root folder as the name of the skin. If the selected
// template is not longer valid, attempt to create a the skin using the default template.
const size_t pos = GetRainmeter().GetSkinPath().size();
std::wstring folder = c_Dialog->m_TabNew.GetParentFolder();
std::wstring file = folder.substr(pos);
PathUtil::RemoveTrailingBackslash(file);
file += L".ini";
folder += file;
const std::wstring& selectedTemplate = c_Dialog->m_TabNew.GetSelectedTemplate();
if (!selectedTemplate.empty())
{
// Copy template file
bool templateExists = true;
std::wstring templateFile = GetTemplateFolder();
templateFile += selectedTemplate;
templateFile += L".template";
if (_waccess_s(templateFile.c_str(), 0) != 0)
{
// Template file doesn't exist, so ask user if they would like a default template instead.
templateExists = false;
const std::wstring text = GetFormattedString(ID_STR_TEMPLATEDOESNOTEXIST, selectedTemplate.c_str());
if (GetRainmeter().ShowMessage(m_Window, text.c_str(), MB_ICONQUESTION | MB_YESNO) != IDYES)
{
// Cancel
break;
}
templateFile = folder;
c_Dialog->m_TabTemplate.CreateTemplate(templateFile);
if (templateFile.empty())
{
// Could not create the skin using the default template
const std::wstring text = GetFormattedString(ID_STR_CREATEFILEFAIL, file.c_str());
GetRainmeter().ShowMessage(m_Window, text.c_str(), MB_ICONERROR | MB_OK);
break;
}
}
// If the template exists, copy the template file to new location
if (templateExists)
{
if (!System::CopyFiles(templateFile, folder))
{
// Could not create the skin using the selected template
const std::wstring text = GetFormattedString(ID_STR_CREATEFILEFAIL, file.c_str());
GetRainmeter().ShowMessage(m_Window, text.c_str(), MB_ICONERROR | MB_OK);
break;
}
}
}
else
{
// Use default template
c_Dialog->m_TabTemplate.CreateTemplate(folder);
if (folder.empty())
{
// Could not create the skin
const std::wstring text = GetFormattedString(ID_STR_CREATEFILEFAIL, file.c_str());
GetRainmeter().ShowMessage(m_Window, text.c_str(), MB_ICONERROR | MB_OK);
break;
}
}
++c_CloseAction.skins; // increase skin count
}
// If any skins were created, refresh Rainmeter and select parent folder in the Manage dialog
if (c_CloseAction.skins > 0)
{
GetRainmeter().RefreshAll();
const size_t pos = GetRainmeter().GetSkinPath().size();
const std::wstring folder = c_Dialog->m_TabNew.GetParentFolder().substr(pos);
DialogManage::Open(L"Skins", folder.c_str(), nullptr);
}
// Reset any close action
c_CloseAction = { false, 0 };
GetWindowPlacement(m_Window, &c_WindowPlacement);
if (c_WindowPlacement.showCmd == SW_SHOWMINIMIZED)
{
c_WindowPlacement.showCmd = SW_SHOWNORMAL;
}
delete c_Dialog;
c_Dialog = nullptr;
}
return TRUE;
}
return FALSE;
}
INT_PTR DialogNewSkin::OnInitDialog(WPARAM wParam, LPARAM lParam)
{
static const ControlTemplate::Control s_Controls[] =
{
CT_BUTTON(Id_CloseButton, ID_STR_CLOSE,
243, 231, 50, 14,
WS_VISIBLE | WS_TABSTOP | BS_DEFPUSHBUTTON, 0),
CT_TAB(Id_Tab, 0,
6, 6, 288, 221,
WS_VISIBLE | WS_TABSTOP | TCS_FIXEDWIDTH, 0) // Last for correct tab order.
};
CreateControls(s_Controls, _countof(s_Controls), m_Font, GetString);
// Load template filenames
LoadTemplates();
HWND item = GetControl(Id_Tab);
m_TabNew.Create(m_Window);
m_TabTemplate.Create(m_Window);
TCITEM tci = { 0 };
tci.mask = TCIF_TEXT;
tci.pszText = GetString(ID_STR_NEWSKIN);
TabCtrl_InsertItem(item, 0, &tci);
tci.pszText = GetString(ID_STR_TEMPLATE);
TabCtrl_InsertItem(item, 1, &tci);
HICON hIcon = GetIcon(IDI_RAINMETER, true);
SendMessage(m_Window, WM_SETICON, ICON_SMALL, (LPARAM)hIcon); // Titlebar icon: 16x16
SendMessage(m_Window, WM_SETICON, ICON_BIG, (LPARAM)hIcon); // Taskbar icon: 32x32
item = GetControl(Id_CloseButton);
SendMessage(m_Window, WM_NEXTDLGCTL, (WPARAM)item, TRUE);
// Use arrows instead of plus/minus in the tree
item = m_TabNew.GetControl(TabNew::Id_ItemsTreeView);
SetWindowTheme(item, L"explorer", nullptr);
if (c_WindowPlacement.length == 0)
{
c_WindowPlacement.length = sizeof(WINDOWPLACEMENT);
GetWindowPlacement(m_Window, &c_WindowPlacement);
}
SetWindowPlacement(m_Window, &c_WindowPlacement);
return TRUE;
}
INT_PTR DialogNewSkin::OnCommand(WPARAM wParam, LPARAM lParam)
{
switch (LOWORD(wParam))
{
case Id_CloseButton:
PostMessage(m_Window, WM_CLOSE, 0, 0);
break;
default:
return FALSE;
}
return TRUE;
}
INT_PTR DialogNewSkin::OnNotify(WPARAM wParam, LPARAM lParam)
{
LPNMHDR nm = (LPNMHDR)lParam;
switch (nm->idFrom)
{
case Id_Tab:
if (nm->code == TCN_SELCHANGE)
{
// Disable all tab windows first
EnableWindow(m_TabNew.GetWindow(), FALSE);
EnableWindow(m_TabTemplate.GetWindow(), FALSE);
GetActiveTab().Activate();
}
break;
default:
return 1;
}
return 0;
}
const std::wstring& DialogNewSkin::GetTemplateFolder()
{
static std::wstring& folder = GetRainmeter().GetSettingsPath() + L"Templates\\";
return folder;
}
void DialogNewSkin::LoadTemplates()
{
c_Templates.clear();
const auto& templateFolder = GetTemplateFolder();
// If |NewSkin.template| exists in the settings path, move to Templates folder
const std::wstring oldTemplate = GetRainmeter().GetSettingsPath() + L"NewSkin.template";
if (_waccess_s(oldTemplate.c_str(), 0) == 0)
{
// Move file to new location
const std::wstring newTemplate = templateFolder + L"NewSkin.template";
System::CopyFiles(oldTemplate, newTemplate, true);
}
// Add template files
std::wstring files = templateFolder + L"*.template";
WIN32_FIND_DATA fd = { 0 };
HANDLE hSearch = FindFirstFile(files.c_str(), &fd);
if (hSearch != INVALID_HANDLE_VALUE)
{
do
{
if (_wcsicmp(PathFindExtension(fd.cFileName), L".template") == 0)
{
PathRemoveExtension(fd.cFileName);
c_Templates.emplace_back(fd.cFileName);
}
}
while (FindNextFile(hSearch, &fd));
FindClose(hSearch);
}
}
void DialogNewSkin::ValidateSelectedTemplate()
{
std::wstring& selected = c_Dialog->m_TabNew.GetSelectedTemplate();
bool found = false;
for (const auto& iter : c_Templates)
{
if (_wcsicmp(iter.c_str(), selected.c_str()) == 0)
{
// Selected template is valid
found = true;
break;
}
}
if (!found)
{
selected.clear();
}
WritePrivateProfileString(
L"Dialog_NewSkin",
L"SelectedTemplate",
selected.empty() ? NULL : selected.c_str(),
GetRainmeter().GetDataFile().c_str());
}
// -----------------------------------------------------------------------------------------------
//
// New tab
//
// -----------------------------------------------------------------------------------------------
std::vector<DialogNewSkin::TabNew::SortInfo> DialogNewSkin::TabNew::s_SortInfo;
DialogNewSkin::TabNew::TabNew() : Tab(),
m_IsRoot(true),
m_CanAddResourcesFolder(false),
m_InRenameMode(false),
m_TreeEdit(nullptr),
m_ParentPathTT(nullptr),
m_ImageList(nullptr)
{
}
DialogNewSkin::TabNew::~TabNew()
{
DestroyImageList();
}
void DialogNewSkin::TabNew::Create(HWND owner)
{
Tab::CreateTabWindow(15, 30, 270, 188, owner);
short buttonWidth = (short)_wtoi(GetString(ID_STR_NUM_BUTTONWIDTH));
buttonWidth += 10;
short column1 = (268 - buttonWidth);
static const ControlTemplate::Control s_Controls[] =
{
CT_LABEL(Id_ParentPathLabel, ID_STR_ELLIPSIS,
0, 0, 268, 14,
WS_VISIBLE | SS_CENTERIMAGE | SS_PATHELLIPSIS | SS_NOTIFY | WS_BORDER, 0),
CT_TREEVIEW(Id_ItemsTreeView, 0,
0, 19, 268 - buttonWidth - 10, 169,
WS_VISIBLE | WS_TABSTOP | TVS_HASBUTTONS | TVS_HASLINES | TVS_LINESATROOT | TVS_SHOWSELALWAYS | WS_VSCROLL, WS_EX_CLIENTEDGE),
CT_BUTTON(Id_AddFolderButton, ID_STR_ADDFOLDER,
column1, 19, buttonWidth, 14,
WS_VISIBLE | WS_TABSTOP, 0),
CT_BUTTON(Id_AddResourcesButton, ID_STR_ADDRESOURCES,
column1, 38, buttonWidth, 14,
WS_VISIBLE | WS_TABSTOP | WS_DISABLED, 0),
CT_BUTTON(Id_AddSkinButton, ID_STR_ADDSKIN,
column1, 57, buttonWidth, 14,
WS_VISIBLE | WS_TABSTOP | WS_DISABLED, 0),
CT_BUTTON(Id_TemplateDropDownList, ID_STR_TEMPLATEE,
column1, 76, buttonWidth, 14,
WS_VISIBLE | WS_TABSTOP, 0)
};
CreateControls(s_Controls, _countof(s_Controls), c_Dialog->m_Font, GetString);
// Create the tooltip for the parent path
m_ParentPathTT = CreateWindowEx(NULL, TOOLTIPS_CLASS, NULL,
WS_POPUP | TTS_ALWAYSTIP,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
m_Window, NULL,
GetModuleHandle(NULL), NULL);
// Add tooltip to window
UpdateParentPathTT(false);
// Get selected template for drop down menu
WCHAR buffer[MAX_PATH] = { 0 };
GetPrivateProfileString(L"Dialog_NewSkin", L"SelectedTemplate", L"", buffer, MAX_PATH, GetRainmeter().GetDataFile().c_str());
if (buffer && *buffer)
{
m_SelectedTemplate = buffer;
ValidateSelectedTemplate();
}
}
void DialogNewSkin::TabNew::Initialize()
{
// Draw arrow on button
HWND item = GetControl(Id_TemplateDropDownList);
Dialog::SetMenuButton(item);
// Load folder/.ini icons from shell32
HIMAGELIST hImageList = ImageList_Create(16, 16, ILC_COLOR32, 2, 10);
HMODULE hDLL = GetModuleHandle(L"shell32");
HICON hIcon = (HICON)LoadImage(hDLL, MAKEINTRESOURCE(4), IMAGE_ICON, 16, 16, LR_SHARED);
ImageList_AddIcon(hImageList, hIcon);
hIcon = (HICON)LoadImage(hDLL, MAKEINTRESOURCE(151), IMAGE_ICON, 16, 16, LR_SHARED);
ImageList_AddIcon(hImageList, hIcon);
// Apply icons and populate tree
item = GetControl(Id_ItemsTreeView);
TreeView_SetImageList(item, hImageList, TVSIL_NORMAL);
DestroyImageList();
m_ImageList = hImageList;
m_Initialized = true;
}
void DialogNewSkin::TabNew::DestroyImageList()
{
if (m_ImageList)
{
HWND item = GetControl(Id_ItemsTreeView);
ImageList_Destroy(TreeView_SetImageList(item, nullptr, TVSIL_STATE));
m_ImageList = nullptr;
}
}
INT_PTR DialogNewSkin::TabNew::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_COMMAND:
return OnCommand(wParam, lParam);
case WM_NOTIFY:
return OnNotify(wParam, lParam);
}
return FALSE;
}
INT_PTR DialogNewSkin::TabNew::OnCommand(WPARAM wParam, LPARAM lParam)
{
switch (LOWORD(wParam))
{
case Id_AddFolderButton:
AddTreeItem(true);
break;
case Id_AddSkinButton:
AddTreeItem(false);
break;
case Id_AddResourcesButton:
{
HWND tree = GetControl(Id_ItemsTreeView);
TVINSERTSTRUCT tvi = { 0 };
tvi.hInsertAfter = TVI_FIRST;
tvi.item.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_STATE | TVIF_PARAM;
tvi.item.iImage = tvi.item.iSelectedImage = 0;
tvi.item.pszText = L"@Resources";
tvi.item.state = tvi.item.stateMask = TVIS_BOLD;
tvi.hParent = TreeView_GetRoot(tree);
s_SortInfo.emplace_back(true, L"@Resources");
tvi.item.lParam = (LPARAM)(size_t)(s_SortInfo.size() - 1);
TreeView_InsertItem(tree, &tvi);
TreeView_Expand(tree, tvi.hParent, TVE_EXPAND);
TreeView_Select(tree, tvi.hParent, TVGN_CARET);
std::wstring resources = m_ParentFolder;
resources += L"@Resources\\";
BOOL exists = CreateDirectory(resources.c_str(), NULL);
if (!exists && GetLastError() == ERROR_PATH_NOT_FOUND)
{
const std::wstring text = GetFormattedString(ID_STR_CREATEFOLDERFAIL, L"@Resources");
GetRainmeter().ShowMessage(m_Window, text.c_str(), MB_ICONERROR | MB_OK);
TreeView_DeleteItem(tree, tvi.item.hItem);
return 0;
}
Button_Enable(GetControl(Id_AddResourcesButton), FALSE);
m_CanAddResourcesFolder = false;
}
break;
case Id_TemplateDropDownList:
{
static const MenuTemplate s_Menu[] =
{
MENU_ITEM(IDM_DEFAULT_TEMPLATE, ID_STR_USEDEFAULTTEMPLATE)
};
HMENU menu = MenuTemplate::CreateMenu(s_Menu, _countof(s_Menu), GetString);
if (menu)
{
TabTemplate::CreateTemplateMenu(menu, m_SelectedTemplate);
RECT r = { 0 };
GetWindowRect((HWND)lParam, &r);
// Show context menu
TrackPopupMenu(
menu,
TPM_RIGHTBUTTON | TPM_LEFTALIGN,
(*GetString(ID_STR_ISRTL) == L'1') ? r.right : r.left,
--r.bottom,
0,
m_Window,
nullptr
);
DestroyMenu(menu);
}
}
break;
case IDM_NEWSKINMENU_EXPAND:
{
HWND tree = GetControl(Id_ItemsTreeView);
HTREEITEM item = TreeView_GetSelection(tree);
TreeView_Expand(tree, item, TVE_TOGGLE);
}
break;
case IDM_NEWSKINMENU_OPENFOLDER:
{
HWND tree = GetControl(Id_ItemsTreeView);
std::wstring folder = m_ParentFolder + GetTreeSelectionPath(tree, false);
PathUtil::AppendBackslashIfMissing(folder);
CommandHandler::RunFile(folder.c_str());
}
break;
case IDM_NEWSKINMENU_EDITFOLDERNAME:
case IDM_NEWSKINMENU_EDITFILENAME:
{
m_InRenameMode = true;
HWND tree = GetControl(Id_ItemsTreeView);
// Make tree nodes editable
LONG_PTR style = GetWindowLongPtr(tree, GWL_STYLE) | TVS_EDITLABELS;
SetWindowLongPtr(tree, GWL_STYLE, style);
// Enter 'edit mode' of treeview edit control
TreeView_EditLabel(tree, TreeView_GetSelection(tree));
}
break;
case IDM_NEWSKINMENU_DELETEFOLDER:
{
HWND tree = GetControl(Id_ItemsTreeView);
std::wstring folder = m_ParentFolder + GetTreeSelectionPath(tree, false);
PathUtil::AppendBackslashIfMissing(folder);
std::wstring text = GetFormattedString(ID_STR_FOLDERDELETE, folder.c_str());
if (GetRainmeter().ShowMessage(m_Window, text.c_str(), MB_ICONQUESTION | MB_YESNO) != IDYES)
{
// Cancel
break;
}
if (!System::RemoveFolder(folder))
{
text = GetFormattedString(ID_STR_FOLDERDELETEFAIL, folder.c_str());
GetRainmeter().ShowMessage(m_Window, text.c_str(), MB_ICONERROR | MB_OK);
return 0;
}
HTREEITEM item = TreeView_GetSelection(tree);
// Subtract any newly created skins in the folder and its subfolders.
// Note: This only checks .ini skin files, not .inc files. Also any folders
// should only contain newly created items, not existing items.
UINT count = GetChildSkinCount(tree, item);
if (count > c_CloseAction.skins)
{
c_CloseAction.skins = 0;
}
else
{
c_CloseAction.skins -= count;
}
if (item == TreeView_GetRoot(tree))
{
// If deleting the 'root' item, all child skins and folders should be deleted,
// so no need to refresh Rainmeter or create any skins when the dialog is closed.
c_CloseAction = { false, 0 };
// Update the parent folder and label
m_ParentFolder = GetRainmeter().GetSkinPath();
UpdateParentPathLabel();
EnableWindow(GetControl(Id_AddResourcesButton), FALSE);
EnableWindow(GetControl(Id_AddSkinButton), FALSE);
}
TreeView_DeleteItem(tree, TreeView_GetSelection(tree));
}
break;
case IDM_NEWSKINMENU_DELETEFILE:
{
HWND tree = GetControl(Id_ItemsTreeView);
std::wstring file = m_ParentFolder + GetTreeSelectionPath(tree, true);
std::wstring text = GetFormattedString(ID_STR_FILEDELETE, file.c_str());
if (GetRainmeter().ShowMessage(m_Window, text.c_str(), MB_ICONQUESTION | MB_YESNO) != IDYES)
{
// Cancel
break;
}
if (!System::RemoveFile(file))
{
text = GetFormattedString(ID_STR_FOLDERDELETEFAIL, file.c_str());
GetRainmeter().ShowMessage(m_Window, text.c_str(), MB_ICONERROR | MB_OK);
return 0;
}
TreeView_DeleteItem(tree, TreeView_GetSelection(tree));
--c_CloseAction.skins;
}
break;
case IDM_NEWSKINMENU_EDITSKIN:
{
HWND tree = GetControl(Id_ItemsTreeView);
const std::wstring file = m_ParentFolder + GetTreeSelectionPath(tree, true);
CommandHandler::RunFile(GetRainmeter().GetSkinEditor().c_str(), file.c_str());
}
break;
default:
if (wParam == IDM_DEFAULT_TEMPLATE)
{
m_SelectedTemplate.clear();
ValidateSelectedTemplate();
break;
}
else if (wParam >= ID_TEMPLATE_FIRST && wParam <= ID_TEMPLATE_LAST)
{
size_t index = (size_t)wParam - ID_TEMPLATE_FIRST;
if (index < c_Templates.size())
{
m_SelectedTemplate = c_Templates[index];
}
else
{
m_SelectedTemplate.clear();
}
ValidateSelectedTemplate();
break;
}
return 1;
}
return 0;
}
INT_PTR DialogNewSkin::TabNew::OnNotify(WPARAM wParam, LPARAM lParam)
{
auto enterEditMode = [](HWND tree, HTREEITEM item) -> void
{
// Make tree nodes editable
LONG_PTR style = GetWindowLongPtr(tree, GWL_STYLE) | TVS_EDITLABELS;
SetWindowLongPtr(tree, GWL_STYLE, style);
// Enter 'edit mode' of treeview edit control
TreeView_EditLabel(tree, item);
};
LPNMHDR nm = (LPNMHDR)lParam;
switch (nm->code)
{
case NM_DBLCLK:
if (nm->idFrom == Id_ItemsTreeView)
{
POINT pt = System::GetCursorPosition();
TVHITTESTINFO ht = { 0 };
ht.pt = pt;
ScreenToClient(nm->hwndFrom, &ht.pt);
if (TreeView_HitTest(nm->hwndFrom, &ht) && !(ht.flags & TVHT_NOWHERE))
{
TreeView_SelectItem(nm->hwndFrom, ht.hItem);
TVITEM tvi = { 0 };
tvi.hItem = TreeView_GetSelection(nm->hwndFrom);
tvi.mask = TVIF_IMAGE;
if (TreeView_GetItem(nm->hwndFrom, &tvi))
{
if (tvi.iImage == 1)
{
// Double click on a .ini or .inc file
OnCommand(MAKEWPARAM(IDM_NEWSKINMENU_EDITSKIN, 0), 0);
}
}
}
}
break;
case NM_RCLICK:
if (nm->idFrom == Id_ItemsTreeView)
{
POINT pt = System::GetCursorPosition();
TVHITTESTINFO ht = { 0 };
ht.pt = pt;
ScreenToClient(nm->hwndFrom, &ht.pt);
if (TreeView_HitTest(nm->hwndFrom, &ht) && !(ht.flags & TVHT_NOWHERE))
{
TreeView_SelectItem(nm->hwndFrom, ht.hItem);
WCHAR buffer[MAX_PATH] = { 0 };
TVITEM tvi = { 0 };
tvi.hItem = TreeView_GetSelection(nm->hwndFrom);
tvi.mask = TVIF_STATE | TVIF_IMAGE | TVIF_CHILDREN | TVIF_TEXT;
tvi.pszText = buffer;
tvi.cchTextMax = _countof(buffer);
if (TreeView_GetItem(nm->hwndFrom, &tvi))
{
bool isNewItem = (tvi.state & TVIS_BOLD) != 0;
HMENU menu = nullptr;
if (tvi.iImage == 0)
{
// Folder menu.
static const MenuTemplate s_Menu[] =
{
MENU_ITEM(IDM_NEWSKINMENU_EXPAND, ID_STR_EXPAND),
MENU_ITEM(IDM_NEWSKINMENU_OPENFOLDER, ID_STR_OPENFOLDER)
};
MENUITEMINFO mii = { 0 };
mii.cbSize = sizeof(MENUITEMINFO);
mii.fMask = MIIM_STRING;
menu = MenuTemplate::CreateMenu(s_Menu, _countof(s_Menu), GetString);
SetMenuDefaultItem(menu, IDM_NEWSKINMENU_EXPAND, MF_BYCOMMAND);
// Allow for renaming of new items *only* if not in the skin registry.
// Also, do not allow editing of the @Resources folder.
if (isNewItem && (_wcsicmp(buffer, L"@Resources") != 0))
{
std::wstring folder = m_ParentFolder + GetTreeSelectionPath(nm->hwndFrom, false);
// Remove trailing slash (not needed for skin registry)
PathUtil::RemoveTrailingBackslash(folder);
folder = folder.substr(GetRainmeter().GetSkinPath().size());
if (!GetRainmeter().m_SkinRegistry.FindFolder(folder))
{
std::wstring text = GetString(ID_STR_RENAME);
AppendMenu(menu, MF_BYPOSITION, IDM_NEWSKINMENU_EDITFOLDERNAME, text.c_str());
text = GetString(ID_STR_DELETE);
AppendMenu(menu, MF_BYPOSITION, IDM_NEWSKINMENU_DELETEFOLDER, text.c_str());
}
}
if (tvi.state & TVIS_EXPANDED)
{
mii.dwTypeData = GetString(ID_STR_COLLAPSE);
SetMenuItemInfo(menu, IDM_NEWSKINMENU_EXPAND, MF_BYCOMMAND, &mii);
}
// Disable 'Expand' if folder has no children
if (tvi.cChildren == 0)
{
EnableMenuItem(menu, IDM_NEWSKINMENU_EXPAND, MF_BYCOMMAND | MF_GRAYED);
}
}
else
{
// Skin menu.
static const MenuTemplate s_Menu[] =
{
MENU_ITEM(IDM_NEWSKINMENU_EDITSKIN, ID_STR_EDITSKIN)
};
menu = MenuTemplate::CreateMenu(s_Menu, _countof(s_Menu), GetString);
SetMenuDefaultItem(menu, IDM_NEWSKINMENU_EDITSKIN, MF_BYCOMMAND);
// Allow for renaming of new items *only* if not in the skin registry
if (isNewItem)
{
std::wstring folder = m_ParentFolder + GetTreeSelectionPath(nm->hwndFrom, false);
folder = folder.substr(GetRainmeter().GetSkinPath().size());
// Remove trailing slash (not needed for skin registry)
PathUtil::RemoveTrailingBackslash(folder);
const std::wstring file = buffer;
const SkinRegistry::Indexes indexes =
GetRainmeter().m_SkinRegistry.FindIndexes(folder, file);
if (!indexes.IsValid())
{
std::wstring text = GetString(ID_STR_RENAME);
AppendMenu(menu, MF_BYPOSITION, IDM_NEWSKINMENU_EDITFILENAME, text.c_str());
text = GetString(ID_STR_DELETE);
AppendMenu(menu, MF_BYPOSITION, IDM_NEWSKINMENU_DELETEFILE, text.c_str());
}
}
}
// Show context menu
TrackPopupMenu(
menu,
TPM_RIGHTBUTTON | TPM_LEFTALIGN,
pt.x,
pt.y,
0,
m_Window,
nullptr
);
DestroyMenu(menu);
}
}
}
break;
case TVN_BEGINLABELEDIT:
{
HWND tree = GetControl(Id_ItemsTreeView);
m_TreeEdit = TreeView_GetEditControl(tree);
// Install subclass to capture key strokes for edit control
SetWindowSubclass(m_TreeEdit, &TreeEditSubclass, 1, 0);
SetFocus(m_TreeEdit);
// Fake set the text in the edit control, so that |TVN_ENDLABELEDIT| notification sees it
LPNMTVDISPINFO info = (LPNMTVDISPINFO)lParam;
SetWindowText(m_TreeEdit, info->item.pszText);
// Only select the skin name, not the extension
const size_t len = wcslen(info->item.pszText);
const WCHAR* ext = PathFindExtension(info->item.pszText);
if (len > 4 && ((_wcsicmp(ext, L".ini") == 0) || (_wcsicmp(ext, L".inc") == 0)))
{
Edit_SetSel(m_TreeEdit, 0, len - 4);
}
}
break;
case TVN_ENDLABELEDIT:
{
RemoveWindowSubclass(m_TreeEdit, &TreeEditSubclass, 1);
HWND tree = GetControl(Id_ItemsTreeView);
LPNMTVDISPINFO info = (LPNMTVDISPINFO)lParam;
// Make tree nodes non-editable
LONG_PTR style = GetWindowLongPtr(tree, GWL_STYLE) & ~TVS_EDITLABELS;
SetWindowLongPtr(tree, GWL_STYLE, style);
// The items text will be |NULL| if the edit control was cancelled.
// So delete the item from the tree unless the item is being renamed.
if (!info->item.pszText)
{
if (!m_InRenameMode)
{
TreeView_DeleteItem(tree, info->item.hItem);
}
m_InRenameMode = false;
return FALSE;
}
const UINT count = TreeView_GetCount(tree);
// Make sure the field is not empty
std::wstring name = info->item.pszText;
if (name.empty())
{
enterEditMode(tree, info->item.hItem);
return TRUE;
}
// Determine if the item is a folder or skin file (.ini)
TVITEM tvitem = { 0 };
tvitem.hItem = info->item.hItem;
tvitem.mask = TVIF_IMAGE | TVIF_TEXT | TVIF_PARAM;
TreeView_GetItem(tree, &tvitem);
const bool isFolder = tvitem.iImage == 0;
// Find out if item already exists in tree
if (DoesNodeExist(info->item.hItem, info->item.pszText, isFolder))
{
const UINT msg = isFolder ? ID_STR_FOLDEREXISTS : ID_STR_FILEEXISTS;
const std::wstring text = GetFormattedString(msg, name.c_str());
GetRainmeter().ShowMessage(m_Window, text.c_str(), MB_ICONWARNING | MB_OK);
enterEditMode(tree, info->item.hItem);
return TRUE;
}
std::wstring newItem = m_ParentFolder;
// Add selection to item
newItem += GetTreeSelectionPath(tree, false);
PathUtil::AppendBackslashIfMissing(newItem);
if (isFolder)
{
if (m_InRenameMode)
{
// |newItem| is now the 'old' path, so replace the last folder
// in the path with the |name| of the new folder
const std::wstring oldItem = newItem;
PathUtil::RemoveTrailingBackslash(newItem);
const size_t pos = newItem.find_last_of(L"\\");
if (pos == std::wstring::npos)
{
// Invalid path
m_InRenameMode = false;