/
FileDialog.cpp
executable file
·1429 lines (1179 loc) · 60.5 KB
/
FileDialog.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
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// TGUI - Texus' Graphical User Interface
// Copyright (C) 2012-2023 Bruno Van de Velde (vdv_b@tgui.eu)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <TGUI/Widgets/FileDialog.hpp>
#include <TGUI/FileDialogIconLoader.hpp>
#if TGUI_EXPERIMENTAL_USE_STD_MODULE
#ifdef TGUI_SYSTEM_WINDOWS
#include <time.h> // localtime_s
#endif
#else
#include <vector>
#include <map>
#include <ctime>
#endif
#ifdef TGUI_SYSTEM_WINDOWS
#include <TGUI/extlibs/IncludeWindows.hpp>
#endif
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
namespace tgui
{
#if TGUI_COMPILED_WITH_CPP_VER < 17
constexpr const char FileDialog::StaticWidgetType[];
#endif
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
FileDialog::FileDialog(const char* typeName, bool initRenderer) :
ChildWindow{typeName, false},
m_iconLoader(FileDialogIconLoader::createInstance())
{
setClientSize({600, 400});
m_buttonBack = Button::create();
m_buttonForward = Button::create();
m_buttonUp = Button::create();
m_editBoxPath = EditBox::create();
m_listView = ListView::create();
m_labelFilename = Label::create();
m_editBoxFilename = EditBox::create();
m_comboBoxFileTypes = ComboBox::create();
m_buttonCancel = Button::create();
m_buttonConfirm = Button::create();
m_buttonCreateFolder = Button::create();
m_buttonCancel->setText("Cancel");
m_buttonConfirm->setText("Open");
add(m_buttonBack, "#TGUI_INTERNAL$ButtonBack#");
add(m_buttonForward, "#TGUI_INTERNAL$ButtonForward#");
add(m_buttonUp, "#TGUI_INTERNAL$ButtonUp#");
add(m_editBoxPath, "#TGUI_INTERNAL$EditBoxPath#");
add(m_listView, "#TGUI_INTERNAL$ListView#");
add(m_labelFilename, "#TGUI_INTERNAL$LabelFilename#");
add(m_editBoxFilename, "#TGUI_INTERNAL$EditBoxFilename#");
add(m_comboBoxFileTypes, "#TGUI_INTERNAL$ComboBoxFileTypes#");
add(m_buttonCancel, "#TGUI_INTERNAL$ButtonCancel#");
add(m_buttonConfirm, "#TGUI_INTERNAL$ButtonConfirm#");
m_buttonConfirm->setOrigin(1, 1);
m_buttonCancel->setOrigin(1, 1);
m_comboBoxFileTypes->setOrigin(1, 1);
m_editBoxFilename->setOrigin(1, 1);
#ifdef TGUI_SYSTEM_WINDOWS
m_buttonCancel->setPosition("100% - 10", "100% - 10");
m_buttonConfirm->setPosition("#TGUI_INTERNAL$ButtonCancel#.left - 10", "100% - 10");
#else
m_buttonConfirm->setPosition("100% - 10", "100% - 10");
m_buttonCancel->setPosition("#TGUI_INTERNAL$ButtonConfirm#.left - 10", "100% - 10");
#endif
m_comboBoxFileTypes->setWidth("33%");
m_comboBoxFileTypes->setPosition("100% - 10", "#TGUI_INTERNAL$ButtonConfirm#.top - 15");
m_labelFilename->setPosition({10, "#TGUI_INTERNAL$ComboBoxFileTypes#.top + (#TGUI_INTERNAL$ComboBoxFileTypes#.height - #TGUI_INTERNAL$LabelFilename#.height) / 2"});
m_editBoxFilename->setPosition("#TGUI_INTERNAL$ComboBoxFileTypes#.left - 10", "#TGUI_INTERNAL$ComboBoxFileTypes#.y");
m_editBoxFilename->setWidth("#TGUI_INTERNAL$EditBoxFilename#.x - #TGUI_INTERNAL$LabelFilename#.width - #TGUI_INTERNAL$LabelFilename#.x - 5");
m_buttonBack->setPosition({10, 10});
m_buttonBack->setSize({"height", "#TGUI_INTERNAL$EditBoxPath#.height"});
m_buttonForward->setPosition({"#TGUI_INTERNAL$ButtonBack#.x + #TGUI_INTERNAL$ButtonBack#.width + 5", 10});
m_buttonForward->setSize({"height", "#TGUI_INTERNAL$EditBoxPath#.height"});
m_buttonUp->setPosition({"#TGUI_INTERNAL$ButtonForward#.x + #TGUI_INTERNAL$ButtonForward#.width + 15", 10});
m_buttonUp->setSize({"height", "#TGUI_INTERNAL$EditBoxPath#.height"});
m_editBoxPath->setPosition({"#TGUI_INTERNAL$ButtonUp#.x + #TGUI_INTERNAL$ButtonUp#.width + 5", 10});
m_editBoxPath->setWidth("100% - #TGUI_INTERNAL$EditBoxPath#.x - 10");
m_listView->setPosition({10, "#TGUI_INTERNAL$EditBoxPath#.y + #TGUI_INTERNAL$EditBoxPath#.height + 10"});
m_listView->setSize({"100% - 20", "#TGUI_INTERNAL$EditBoxFilename#.top - top - 10"});
m_listView->addColumn("Name", 325);
m_listView->addColumn("Size", 75, ListView::ColumnAlignment::Right);
m_listView->addColumn("Modified", 145, ListView::ColumnAlignment::Right);
if (m_iconLoader->supportsSystemIcons())
m_listView->setFixedIconSize({static_cast<float>(m_listView->getItemHeight()) * 0.8f, 0});
m_labelFilename->setText("Filename:");
m_editBoxPath->setText(m_currentDirectory.asString());
m_buttonBack->setText(U"\u2190");
m_buttonForward->setText(U"\u2192");
m_buttonUp->setText(U"\u2191");
if (initRenderer)
{
m_renderer = aurora::makeCopied<FileDialogRenderer>();
setRenderer(Theme::getDefault()->getRendererNoThrow(m_type));
}
setFileTypeFilters({});
setPath(Filesystem::getCurrentWorkingDirectory());
connectSignals();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
FileDialog::FileDialog(const FileDialog& other) :
ChildWindow {other},
onFileSelect {other.onFileSelect},
onCancel {other.onCancel},
m_currentDirectory {other.m_currentDirectory},
m_filesInDirectory {other.m_filesInDirectory},
m_fileIcons {other.m_fileIcons},
m_sortColumnIndex {other.m_sortColumnIndex},
m_sortInversed {other.m_sortInversed},
m_pathHistory {other.m_pathHistory},
m_pathHistoryIndex {other.m_pathHistoryIndex},
m_fileMustExist {other.m_fileMustExist},
m_selectingDirectory {other.m_selectingDirectory},
m_multiSelect {other.m_multiSelect},
m_fileTypeFilters {other.m_fileTypeFilters},
m_selectedFileTypeFilter{other.m_selectedFileTypeFilter},
m_iconLoader {FileDialogIconLoader::createInstance()},
m_selectedFiles {other.m_selectedFiles}
{
identifyChildWidgets();
connectSignals();
// Update the file list (to refresh icons)
changePath(m_currentDirectory, false);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
FileDialog::FileDialog(FileDialog&& other) noexcept :
ChildWindow {std::move(other)},
onFileSelect {std::move(other.onFileSelect)},
onCancel {std::move(other.onCancel)},
m_buttonBack {std::move(other.m_buttonBack)},
m_buttonForward {std::move(other.m_buttonForward)},
m_buttonUp {std::move(other.m_buttonUp)},
m_editBoxPath {std::move(other.m_editBoxPath)},
m_listView {std::move(other.m_listView)},
m_labelFilename {std::move(other.m_labelFilename)},
m_editBoxFilename {std::move(other.m_editBoxFilename)},
m_comboBoxFileTypes {std::move(other.m_comboBoxFileTypes)},
m_buttonCancel {std::move(other.m_buttonCancel)},
m_buttonConfirm {std::move(other.m_buttonConfirm)},
m_buttonCreateFolder {std::move(other.m_buttonCreateFolder)},
m_currentDirectory {std::move(other.m_currentDirectory)},
m_filesInDirectory {std::move(other.m_filesInDirectory)},
m_fileIcons {std::move(other.m_fileIcons)},
m_sortColumnIndex {std::move(other.m_sortColumnIndex)},
m_sortInversed {std::move(other.m_sortInversed)},
m_pathHistory {std::move(other.m_pathHistory)},
m_pathHistoryIndex {std::move(other.m_pathHistoryIndex)},
m_fileMustExist {std::move(other.m_fileMustExist)},
m_selectingDirectory {std::move(other.m_selectingDirectory)},
m_multiSelect {std::move(other.m_multiSelect)},
m_fileTypeFilters {std::move(other.m_fileTypeFilters)},
m_selectedFileTypeFilter{std::move(other.m_selectedFileTypeFilter)},
m_iconLoader {std::move(other.m_iconLoader)},
m_selectedFiles {std::move(other.m_selectedFiles)}
{
connectSignals();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
FileDialog& FileDialog::operator= (const FileDialog& other)
{
if (this != &other)
{
ChildWindow::operator=(other);
onFileSelect = other.onFileSelect;
onCancel = other.onCancel;
m_currentDirectory = other.m_currentDirectory;
m_filesInDirectory = other.m_filesInDirectory;
m_fileIcons = other.m_fileIcons;
m_sortColumnIndex = other.m_sortColumnIndex;
m_sortInversed = other.m_sortInversed;
m_pathHistory = other.m_pathHistory;
m_pathHistoryIndex = other.m_pathHistoryIndex;
m_fileMustExist = other.m_fileMustExist;
m_selectingDirectory = other.m_selectingDirectory;
m_multiSelect = other.m_multiSelect;
m_fileTypeFilters = other.m_fileTypeFilters;
m_selectedFileTypeFilter = other.m_selectedFileTypeFilter;
m_iconLoader = FileDialogIconLoader::createInstance();
m_selectedFiles = other.m_selectedFiles;
identifyChildWidgets();
connectSignals();
// Update the file list (to refresh icons)
changePath(m_currentDirectory, false);
}
return *this;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
FileDialog& FileDialog::operator= (FileDialog&& other) noexcept
{
if (this != &other)
{
onFileSelect = std::move(other.onFileSelect);
onCancel = std::move(other.onCancel);
m_buttonBack = std::move(other.m_buttonBack);
m_buttonForward = std::move(other.m_buttonForward);
m_buttonUp = std::move(other.m_buttonUp);
m_editBoxPath = std::move(other.m_editBoxPath);
m_listView = std::move(other.m_listView);
m_labelFilename = std::move(other.m_labelFilename);
m_editBoxFilename = std::move(other.m_editBoxFilename);
m_comboBoxFileTypes = std::move(other.m_comboBoxFileTypes);
m_buttonCancel = std::move(other.m_buttonCancel);
m_buttonConfirm = std::move(other.m_buttonConfirm);
m_buttonCreateFolder = std::move(other.m_buttonCreateFolder);
m_currentDirectory = std::move(other.m_currentDirectory);
m_filesInDirectory = std::move(other.m_filesInDirectory);
m_fileIcons = std::move(other.m_fileIcons);
m_sortColumnIndex = std::move(other.m_sortColumnIndex);
m_sortInversed = std::move(other.m_sortInversed);
m_pathHistory = std::move(other.m_pathHistory);
m_pathHistoryIndex = std::move(other.m_pathHistoryIndex);
m_fileMustExist = std::move(other.m_fileMustExist);
m_selectingDirectory = std::move(other.m_selectingDirectory);
m_multiSelect = std::move(other.m_multiSelect);
m_fileTypeFilters = std::move(other.m_fileTypeFilters);
m_selectedFileTypeFilter = std::move(other.m_selectedFileTypeFilter);
m_iconLoader = std::move(other.m_iconLoader);
m_selectedFiles = std::move(other.m_selectedFiles);
ChildWindow::operator=(std::move(other));
connectSignals();
}
return *this;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
FileDialog::Ptr FileDialog::create(const String& title, const String& confirmButtonText, bool allowCreateFolder)
{
auto fileDialog = std::make_shared<FileDialog>();
fileDialog->setTitle(title);
fileDialog->setConfirmButtonText(confirmButtonText);
fileDialog->setAllowCreateFolder(allowCreateFolder);
return fileDialog;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
FileDialog::Ptr FileDialog::copy(const FileDialog::ConstPtr& dialog)
{
if (dialog)
return std::static_pointer_cast<FileDialog>(dialog->clone());
else
return nullptr;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
FileDialogRenderer* FileDialog::getSharedRenderer()
{
return aurora::downcast<FileDialogRenderer*>(Widget::getSharedRenderer());
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
const FileDialogRenderer* FileDialog::getSharedRenderer() const
{
return aurora::downcast<const FileDialogRenderer*>(Widget::getSharedRenderer());
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
FileDialogRenderer* FileDialog::getRenderer()
{
return aurora::downcast<FileDialogRenderer*>(Widget::getRenderer());
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
const std::vector<Filesystem::Path>& FileDialog::getSelectedPaths() const
{
return m_selectedFiles;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void FileDialog::setPath(const String& path)
{
setPath(Filesystem::Path(path));
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void FileDialog::setPath(const Filesystem::Path& path)
{
m_pathHistoryIndex = 0;
m_pathHistory.clear();
m_pathHistory.push_back(path);
historyChanged();
changePath(path, false);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
const Filesystem::Path& FileDialog::getPath() const
{
return m_currentDirectory;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void FileDialog::setFilename(const String& filename)
{
m_editBoxFilename->setText(filename);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
const String& FileDialog::getFilename() const
{
return m_editBoxFilename->getText();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void FileDialog::setFileTypeFilters(const std::vector<std::pair<String, std::vector<String>>>& filters, std::size_t defaultIndex)
{
m_fileTypeFilters.clear();
for (const auto& filter : filters)
{
std::vector<String> expressions = filter.second;
for (auto& expression : expressions)
{
if (expression.empty() || (expression == U"*") || (expression == U"*.*"))
{
// The expression doesn't filter any files and shouldn't exist
expressions.clear();
break;
}
expression = expression.toLower();
}
m_fileTypeFilters.emplace_back(filter.first, expressions);
}
if (m_fileTypeFilters.size() == 0)
m_fileTypeFilters.emplace_back("All files (*)", std::vector<String>());
m_comboBoxFileTypes->removeAllItems();
for (const auto& filter : m_fileTypeFilters)
m_comboBoxFileTypes->addItem(filter.first);
if (defaultIndex > m_comboBoxFileTypes->getItemCount())
{
defaultIndex = m_comboBoxFileTypes->getItemCount() - 1;
TGUI_PRINT_WARNING("defaultIndex too high in FileDialog::setFileTypeFilters");
}
m_comboBoxFileTypes->setSelectedItemByIndex(defaultIndex);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
const std::vector<std::pair<String, std::vector<String>>>& FileDialog::getFileTypeFilters() const
{
return m_fileTypeFilters;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
std::size_t FileDialog::getFileTypeFiltersIndex() const
{
TGUI_ASSERT(m_comboBoxFileTypes->getSelectedItemIndex() >= 0, "File type combo box always need to have item selected");
return static_cast<std::size_t>(m_comboBoxFileTypes->getSelectedItemIndex());
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void FileDialog::setConfirmButtonText(const String& text)
{
m_buttonConfirm->setText(text);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
const String& FileDialog::getConfirmButtonText() const
{
return m_buttonConfirm->getText();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void FileDialog::setCancelButtonText(const String& text)
{
m_buttonCancel->setText(text);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
const String& FileDialog::getCancelButtonText() const
{
return m_buttonCancel->getText();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void FileDialog::setCreateFolderButtonText(const String& text)
{
m_buttonCreateFolder->setText(text);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
const String& FileDialog::getCreateFolderButtonText() const
{
return m_buttonCreateFolder->getText();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void FileDialog::setAllowCreateFolder(bool allowCreateFolder)
{
m_allowCreateFolder = allowCreateFolder;
if (nullptr == m_buttonCreateFolder->getParent() && m_allowCreateFolder)
addCreateFolderButton();
else
remove(m_buttonCreateFolder);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool FileDialog::getAllowCreateFolder() const
{
return m_allowCreateFolder;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void FileDialog::setFilenameLabelText(const String& labelText)
{
m_labelFilename->setText(labelText);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
const String& FileDialog::getFilenameLabelText() const
{
return m_labelFilename->getText();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void FileDialog::setListViewColumnCaptions(const String& nameColumnText, const String& sizeColumnText, const String& modifiedColumnText)
{
m_listView->setColumnText(0, nameColumnText);
m_listView->setColumnText(1, sizeColumnText);
m_listView->setColumnText(2, modifiedColumnText);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
std::tuple<String, String, String> FileDialog::getListViewColumnCaptions() const
{
return std::make_tuple(m_listView->getColumnText(0), m_listView->getColumnText(1), m_listView->getColumnText(2));
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void FileDialog::setFileMustExist(bool enforceExistence)
{
m_fileMustExist = enforceExistence;
updateConfirmButtonEnabled();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool FileDialog::getFileMustExist() const
{
return m_fileMustExist;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void FileDialog::setSelectingDirectory(bool selectDirectories)
{
m_selectingDirectory = selectDirectories;
if (selectDirectories)
{
m_comboBoxFileTypes->setVisible(false);
m_editBoxFilename->setPosition({"100% - 10", m_editBoxFilename->getPositionLayout().y});
}
else
{
m_comboBoxFileTypes->setVisible(true);
m_editBoxFilename->setPosition({"#TGUI_INTERNAL$ComboBoxFileTypes#.x - #TGUI_INTERNAL$ComboBoxFileTypes#.width - 10",
m_editBoxFilename->getPositionLayout().y});
}
changePath(m_currentDirectory, false);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool FileDialog::getSelectingDirectory() const
{
return m_selectingDirectory;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void FileDialog::setMultiSelect(bool multiSelect)
{
m_multiSelect = multiSelect;
m_listView->setMultiSelect(multiSelect);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool FileDialog::getMultiSelect() const
{
return m_multiSelect;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void FileDialog::setIconLoader(std::shared_ptr<FileDialogIconLoader> iconLoader)
{
TGUI_ASSERT(iconLoader != nullptr, "Icon loader can't be a nullptr");
m_iconLoader = std::move(iconLoader);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
std::shared_ptr<FileDialogIconLoader> FileDialog::getIconLoader() const
{
return m_iconLoader;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void FileDialog::keyPressed(const Event::KeyEvent& event)
{
if ((event.code == Event::KeyboardKey::Enter) && (!m_editBoxPath->isFocused()))
{
if (m_createFolderDialogOpen)
{
EditBox::Ptr folderNameEditBox = get<EditBox>("FolderNameEditBox");
createFolder(folderNameEditBox->getText());
destroyCreateFolderDialog();
return;
}
// Simulate a press on the confirm button
if (m_buttonConfirm->isEnabled())
confirmButtonPressed();
}
else if ((event.code == Event::KeyboardKey::Escape) && m_createFolderDialogOpen)
{
destroyCreateFolderDialog();
}
else
{
ChildWindow::keyPressed(event);
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void FileDialog::textEntered(char32_t key)
{
// We will only search through the list view if no other widget was focused. Pass the event to the focused widget otherwise.
if (m_focusedWidget && (m_focusedWidget != m_listView))
{
ChildWindow::textEntered(key);
return;
}
// We currently only search for the typed character. In the future we should remember previously typed characters too.
const String searchStr{key};
// Select the first item in the list view that starts with the search string (case-insensitive), starting the
// search from the currently selected item.
const std::size_t startIndex = static_cast<std::size_t>(std::max(0, m_listView->getSelectedItemIndex()));
const std::size_t itemCount = m_listView->getItemCount();
for (std::size_t i = 0; i < itemCount; ++i)
{
const std::size_t index = (startIndex + i) % itemCount;
const String& item = m_listView->getItem(index);
if (item.empty() || (item.length() < searchStr.length()))
continue;
if (item.startsWithIgnoreCase(searchStr))
{
m_listView->setSelectedItem(index);
break;
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool FileDialog::updateTime(Duration elapsedTime)
{
const bool childWidgetUpdated = ChildWindow::updateTime(elapsedTime);
if (!m_iconLoader->update())
return childWidgetUpdated;
m_fileIcons = m_iconLoader->retrieveFileIcons();
const int oldSelectedItem = m_listView->getSelectedItemIndex();
sortFilesInListView();
if (oldSelectedItem >= 0)
m_listView->setSelectedItem(static_cast<std::size_t>(oldSelectedItem));
return true;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void FileDialog::changePath(const Filesystem::Path& path, bool updateHistory)
{
if (updateHistory && (m_currentDirectory != path))
{
if (m_pathHistoryIndex + 1 < m_pathHistory.size())
m_pathHistory.erase(m_pathHistory.begin() + static_cast<std::ptrdiff_t>(m_pathHistoryIndex + 1), m_pathHistory.end());
m_pathHistory.push_back(path);
m_pathHistoryIndex = m_pathHistory.size() - 1;
historyChanged();
}
m_currentDirectory = path;
m_editBoxPath->setText(m_currentDirectory.asString());
m_filesInDirectory = Filesystem::listFilesInDirectory(path);
#ifdef TGUI_SYSTEM_WINDOWS
if (path.asString().empty())
{
m_listView->setHeaderVisible(false);
m_listView->setShowVerticalGridLines(false);
m_listView->removeAllItems();
wchar_t logicalDrives[MAX_PATH];
if (GetLogicalDriveStringsW(MAX_PATH, logicalDrives))
{
wchar_t* drive = logicalDrives;
while (*drive)
{
m_listView->setItemData(m_listView->addItem(Filesystem::Path(drive).asString()), true);
drive += std::wcslen(drive) + 1;
}
}
m_filesInDirectory.clear();
return;
}
#endif // TGUI_SYSTEM_WINDOWS
// If only directories should be shown then remove the files from the list
if (m_selectingDirectory)
{
m_filesInDirectory.erase(std::remove_if(m_filesInDirectory.begin(), m_filesInDirectory.end(),
[](const Filesystem::FileInfo& info){ return !info.directory; }), m_filesInDirectory.end());
}
m_fileIcons.clear();
if (m_iconLoader->hasGenericIcons())
{
m_fileIcons.reserve(m_filesInDirectory.size());
for (const auto& file : m_filesInDirectory)
m_fileIcons.push_back(m_iconLoader->getGenericFileIcon(file));
}
m_iconLoader->requestFileIcons(m_filesInDirectory);
m_listView->setVerticalScrollbarValue(0);
sortFilesInListView();
updateConfirmButtonEnabled();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void FileDialog::historyChanged()
{
m_buttonBack->setEnabled(m_pathHistoryIndex > 0);
m_buttonForward->setEnabled(m_pathHistoryIndex + 1 < m_pathHistory.size());
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void FileDialog::sortFilesInListView()
{
const auto oldScrollbarValue = m_listView->getVerticalScrollbarValue();
std::vector<std::pair<Filesystem::FileInfo, Texture>> items;
items.reserve(m_filesInDirectory.size());
if (!m_fileIcons.empty())
{
TGUI_ASSERT(m_filesInDirectory.size() == m_fileIcons.size(), "Icon count must match file count in FileDialog::sortFilesInListView");
for (std::size_t i = 0; i < m_filesInDirectory.size(); ++i)
items.emplace_back(m_filesInDirectory[i], m_fileIcons[i]);
}
else // There are no file icons
{
for (const auto& file : m_filesInDirectory)
items.emplace_back(file, Texture{});
}
std::sort(items.begin(), items.end(), [this](const auto& leftItem, const auto& rightItem){
const Filesystem::FileInfo& left = leftItem.first;
const Filesystem::FileInfo& right = rightItem.first;
if (m_sortColumnIndex == 2) // Sort by modification date
{
if (m_sortInversed)
return left.modificationTime < right.modificationTime;
else
return left.modificationTime > right.modificationTime;
}
else if (m_sortColumnIndex == 1) // Sort by file size
{
if (left.directory != right.directory)
return right.directory; // Place directories at the end of the list
else if (left.directory) // Both are directories, sort them alphabetically by filename since they have no size
return left.filename.toLower() < right.filename.toLower();
else // Both are files, sort them by file size
{
if (m_sortInversed)
return left.fileSize < right.fileSize;
else
return left.fileSize > right.fileSize;
}
}
else // Sort by filename
{
if (left.directory != right.directory)
return left.directory; // Place directories in front of files
else // Both are directories or both are files, so sort alphabetically
{
if (m_sortInversed)
return left.filename.toLower() > right.filename.toLower();
else
return left.filename.toLower() < right.filename.toLower();
}
}
});
if (!m_listView->getHeaderVisible())
{
m_listView->setHeaderVisible(true);
m_listView->setShowVerticalGridLines(true);
}
m_listView->removeAllItems();
for (const auto& item : items)
{
const Filesystem::FileInfo& file = item.first;
const Texture& icon = item.second;
// Filter the files
if (!file.directory && !m_fileTypeFilters[m_selectedFileTypeFilter].second.empty())
{
const String& lowercaseFilename = file.filename.toLower();
bool fileAccepted = false;
for (const auto& filter : m_fileTypeFilters[m_selectedFileTypeFilter].second)
{
TGUI_ASSERT(!filter.empty(), "FileDialog::sortFilesInListView can't have empty filter, they are removed in setFileTypeFilters");
if (filter[0] == '*')
{
if ((lowercaseFilename.length() >= filter.length() - 1)
&& (lowercaseFilename.compare(lowercaseFilename.length() - (filter.length() - 1),
filter.length() - 1, filter, 1, filter.length() - 1) == 0))
{
fileAccepted = true;
break;
}
}
// Check if the filter matches exactly with the filename
else if (lowercaseFilename == filter)
{
fileAccepted = true;
break;
}
}
if (!fileAccepted)
continue;
}
String fileSizeStr;
if (!file.directory)
{
if (file.fileSize == 0)
fileSizeStr = U"0.0 KB";
else if (file.fileSize < static_cast<std::uintmax_t>(100))
fileSizeStr = U"0.1 KB";
else if (file.fileSize < static_cast<std::uintmax_t>(1000)*1000)
fileSizeStr = String::fromNumberRounded(file.fileSize / 1000.f, 1) + U" KB";
else if (file.fileSize < static_cast<std::uintmax_t>(1000)*1000*1000)
fileSizeStr = String::fromNumberRounded(file.fileSize / 1000.f / 1000.f, 1) + U" MB";
else if (file.fileSize < static_cast<std::uintmax_t>(1000)*1000*1000*1000)
fileSizeStr = String::fromNumberRounded(file.fileSize / 1000.f / 1000.f / 1000.f, 1) + U" GB";
else if (file.fileSize < static_cast<std::uintmax_t>(1000)*1000*1000*1000*1000)
fileSizeStr = String::fromNumberRounded(file.fileSize / 1000.f / 1000.f / 1000.f / 1000.f, 1) + U" TB";
}
String modificationTimeStr;
bool modificationTimeConverted = false;
char buffer[19];
#if defined(TGUI_SYSTEM_WINDOWS) && defined(_MSC_VER)
std::tm TimeStructure;
if (localtime_s(&TimeStructure, &file.modificationTime) == 0)
{
if (std::strftime(&buffer[0], sizeof(buffer), "%e %b %Y %R", &TimeStructure) != 0)
modificationTimeConverted = true;
}
#elif defined(TGUI_SYSTEM_WINDOWS) && defined(__GNUC__) // MinGW doesn't support %e (day of the month without leading 0) and %R (same as %H:%M)
if (strftime(&buffer[0], sizeof(buffer), "%d %b %Y %H:%M", std::localtime(&file.modificationTime)) != 0)
modificationTimeConverted = true;
#else
if (strftime(&buffer[0], sizeof(buffer), "%e %b %Y %R", std::localtime(&file.modificationTime)) != 0)
modificationTimeConverted = true;
#endif
if (modificationTimeConverted)
modificationTimeStr = static_cast<char*>(buffer);
#if defined(TGUI_SYSTEM_WINDOWS)
// Hide .lnk and .url extensions
String filename = file.filename;
const auto filenameLen = file.filename.length();
if ((filename.length() > 4) && ((filename.compare(filenameLen - 4, 4, U".lnk", 4) == 0) || (filename.compare(filenameLen - 4, 4, U".url", 4) == 0)))
filename.erase(filenameLen - 4, 4);
const std::size_t itemIndex = m_listView->addItem({filename, fileSizeStr, modificationTimeStr});
#else
const std::size_t itemIndex = m_listView->addItem({file.filename, fileSizeStr, modificationTimeStr});
#endif
m_listView->setItemData(itemIndex, file.directory);
if (icon.getData())
m_listView->setItemIcon(itemIndex, icon);
}
m_listView->setVerticalScrollbarValue(oldScrollbarValue);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void FileDialog::filesSelected(std::vector<Filesystem::Path> selectedFiles)
{
m_selectedFiles = std::move(selectedFiles);
if (!m_selectedFiles.empty())
onFileSelect.emit(this, m_selectedFiles);
else
onCancel.emit(this);
close();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void FileDialog::updateConfirmButtonEnabled()
{
const String& filename = m_editBoxFilename->getText();
const bool enabled = ((filename != U".") && (filename != U"..")) // Always disabled for "." and ".." filenames
&& (m_selectingDirectory || !m_listView->getSelectedItemIndices().empty() || !filename.empty()) // Disabled when no file selected
&& (!m_fileMustExist || Filesystem::fileExists(m_currentDirectory / Filesystem::Path(filename))); // Disabled for non-existent files
if (enabled != m_buttonConfirm->isEnabled())
m_buttonConfirm->setEnabled(enabled);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void FileDialog::confirmButtonPressed()
{
if (m_multiSelect && (m_listView->getSelectedItemIndices().size() > 1))
{
const auto& indices = m_listView->getSelectedItemIndices();
std::vector<Filesystem::Path> paths;
for (const auto index : indices)
{
// Filter out directories when we are attempting to select files
if (!m_selectingDirectory && m_listView->getItemData<bool>(index))
continue;
paths.push_back(m_currentDirectory / m_listView->getItem(index));
}
filesSelected(std::move(paths));
}
else // Only one file selected
{
const auto path = m_currentDirectory / Filesystem::Path(m_editBoxFilename->getText());
// If we were looking for a file but a directory was selected then just enter that directory
if (!m_selectingDirectory && (m_listView->getSelectedItemIndices().size() == 1)
&& (m_listView->getItemData<bool>(*m_listView->getSelectedItemIndices().begin())))
{
changePath(m_currentDirectory / m_listView->getItem(*m_listView->getSelectedItemIndices().begin()), true);
m_editBoxFilename->setText(U"");
}
else if (!m_selectingDirectory && !m_editBoxFilename->getText().empty() && Filesystem::directoryExists(path))
{
changePath(path, true);
m_editBoxFilename->setText(U"");
}
else
filesSelected({path});
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void FileDialog::addCreateFolderButton()
{
m_buttonCreateFolder = Button::create();
m_buttonCreateFolder->setText("Create Folder");
m_buttonCreateFolder->setOrigin(0, 1);
m_buttonCreateFolder->setPosition("10", "100% - 10");
m_buttonCreateFolder->onPress.disconnectAll();
m_buttonCreateFolder->onPress([this]{ createCreateFolderDialog(); });
add(m_buttonCreateFolder, "#TGUI_INTERNAL%ButtonCreateFolder");
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void FileDialog::createFolder(const String& name)
{
Filesystem::createDirectory(m_currentDirectory / name);
changePath(m_currentDirectory, false);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void FileDialog::createCreateFolderDialog()
{
// Create a transparent background
Panel::Ptr background_panel = Panel::create({"100%", "100%"});
background_panel->getRenderer()->setBackgroundColor({0, 0, 0, 175});