-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImGuiFileDialog.cpp
3516 lines (3084 loc) · 94.2 KB
/
ImGuiFileDialog.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
// This is an independent project of an individual developer. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
/*
MIT License
Copyright (c) 2019-2020 Stephane Cuillerdier (aka aiekick)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "ImGuiFileDialog.h"
#ifdef __cplusplus
#include "imgui/imgui.h"
#include <float.h>
#include <string.h> // stricmp / strcasecmp
#include <sstream>
#include <iomanip>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>
#if defined (__EMSCRIPTEN__) // EMSCRIPTEN
#include <emscripten.h>
#endif // EMSCRIPTEN
#if defined(__WIN32__) || defined(_WIN32)
#ifndef WIN32
#define WIN32
#endif // WIN32
#define stat _stat
#define stricmp _stricmp
#include <cctype>
#include "dirent.h" // directly open the dirent file attached to this lib
#define PATH_SEP '\\'
#ifndef PATH_MAX
#define PATH_MAX 260
#endif // PATH_MAX
#elif defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__APPLE__) || defined (__EMSCRIPTEN__)
#define UNIX
#define stricmp strcasecmp
#include <sys/types.h>
#include <dirent.h>
#define PATH_SEP '/'
#endif // defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__APPLE__)
#include "imgui/imgui.h"
#ifndef IMGUI_DEFINE_MATH_OPERATORS
#define IMGUI_DEFINE_MATH_OPERATORS
#endif // IMGUI_DEFINE_MATH_OPERATORS
#include "imgui/imgui_internal.h"
#include <cstdlib>
#include <algorithm>
#include <iostream>
namespace IGFD
{
// float comparisons
#ifndef IS_FLOAT_DIFFERENT
#define IS_FLOAT_DIFFERENT(a,b) (fabs((a) - (b)) > FLT_EPSILON)
#endif // IS_FLOAT_DIFFERENT
#ifndef IS_FLOAT_EQUAL
#define IS_FLOAT_EQUAL(a,b) (fabs((a) - (b)) < FLT_EPSILON)
#endif // IS_FLOAT_EQUAL
// width of filter combobox
#ifndef FILTER_COMBO_WIDTH
#define FILTER_COMBO_WIDTH 150.0f
#endif // FILTER_COMBO_WIDTH
// for lets you define your button widget
// if you have like me a special bi-color button
#ifndef IMGUI_PATH_BUTTON
#define IMGUI_PATH_BUTTON ImGui::Button
#endif // IMGUI_PATH_BUTTON
#ifndef IMGUI_BUTTON
#define IMGUI_BUTTON ImGui::Button
#endif // IMGUI_BUTTON
// locales
#ifndef createDirButtonString
#define createDirButtonString "+"
#endif // createDirButtonString
#ifndef okButtonString
#define okButtonString "OK"
#endif // okButtonString
#ifndef cancelButtonString
#define cancelButtonString "Cancel"
#endif // cancelButtonString
#ifndef resetButtonString
#define resetButtonString "R"
#endif // resetButtonString
#ifndef drivesButtonString
#define drivesButtonString "Drives"
#endif // drivesButtonString
#ifndef searchString
#define searchString "Search :"
#endif // searchString
#ifndef dirEntryString
#define dirEntryString "[Dir]"
#endif // dirEntryString
#ifndef linkEntryString
#define linkEntryString "[Link]"
#endif // linkEntryString
#ifndef fileEntryString
#define fileEntryString "[File]"
#endif // fileEntryString
#ifndef fileNameString
#define fileNameString "File Name :"
#endif // fileNameString
#ifndef dirNameString
#define dirNameString "Directory Path :"
#endif // dirNameString
#ifndef buttonResetSearchString
#define buttonResetSearchString "Reset search"
#endif // buttonResetSearchString
#ifndef buttonDriveString
#define buttonDriveString "Drives"
#endif // buttonDriveString
#ifndef buttonResetPathString
#define buttonResetPathString "Reset to current directory"
#endif // buttonResetPathString
#ifndef buttonCreateDirString
#define buttonCreateDirString "Create Directory"
#endif // buttonCreateDirString
#ifndef tableHeaderAscendingIcon
#define tableHeaderAscendingIcon "A|"
#endif // tableHeaderAscendingIcon
#ifndef tableHeaderDescendingIcon
#define tableHeaderDescendingIcon "D|"
#endif // tableHeaderDescendingIcon
#ifndef tableHeaderFileNameString
#define tableHeaderFileNameString "File name"
#endif // tableHeaderFileNameString
#ifndef tableHeaderFileTypeString
#define tableHeaderFileTypeString "Type"
#endif // tableHeaderFileTypeString
#ifndef tableHeaderFileSizeString
#define tableHeaderFileSizeString "Size"
#endif // tableHeaderFileSizeString
#ifndef tableHeaderFileDateString
#define tableHeaderFileDateString "Date"
#endif // tableHeaderFileDateString
#ifndef OverWriteDialogTitleString
#define OverWriteDialogTitleString "The file Already Exist !"
#endif // OverWriteDialogTitleString
#ifndef OverWriteDialogMessageString
#define OverWriteDialogMessageString "Would you like to OverWrite it ?"
#endif // OverWriteDialogMessageString
#ifndef OverWriteDialogConfirmButtonString
#define OverWriteDialogConfirmButtonString "Confirm"
#endif // OverWriteDialogConfirmButtonString
#ifndef OverWriteDialogCancelButtonString
#define OverWriteDialogCancelButtonString "Cancel"
#endif // OverWriteDialogCancelButtonString
// see strftime functionin <ctime> for customize
#ifndef DateTimeFormat
#define DateTimeFormat "%Y/%m/%d %H:%M"
#endif // DateTimeFormat
#ifdef USE_BOOKMARK
#ifndef defaultBookmarkPaneWith
#define defaultBookmarkPaneWith 150.0f
#endif // defaultBookmarkPaneWith
#ifndef bookmarksButtonString
#define bookmarksButtonString "Bookmark"
#endif // bookmarksButtonString
#ifndef bookmarksButtonHelpString
#define bookmarksButtonHelpString "Bookmark"
#endif // bookmarksButtonHelpString
#ifndef addBookmarkButtonString
#define addBookmarkButtonString "+"
#endif // addBookmarkButtonString
#ifndef removeBookmarkButtonString
#define removeBookmarkButtonString "-"
#endif // removeBookmarkButtonString
#ifndef IMGUI_TOGGLE_BUTTON
inline bool ToggleButton(const char* vLabel, bool* vToggled)
{
bool pressed = false;
if (vToggled && *vToggled)
{
ImVec4 bua = ImGui::GetStyleColorVec4(ImGuiCol_ButtonActive);
//ImVec4 buh = ImGui::GetStyleColorVec4(ImGuiCol_ButtonHovered);
//ImVec4 bu = ImGui::GetStyleColorVec4(ImGuiCol_Button);
ImVec4 te = ImGui::GetStyleColorVec4(ImGuiCol_Text);
ImGui::PushStyleColor(ImGuiCol_Button, te);
ImGui::PushStyleColor(ImGuiCol_ButtonActive, te);
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, te);
ImGui::PushStyleColor(ImGuiCol_Text, bua);
}
pressed = IMGUI_BUTTON(vLabel);
if (vToggled && *vToggled)
{
ImGui::PopStyleColor(4); //-V112
}
if (vToggled && pressed)
*vToggled = !*vToggled;
return pressed;
}
#define IMGUI_TOGGLE_BUTTON ToggleButton
#endif // IMGUI_TOGGLE_BUTTON
#endif // USE_BOOKMARK
// https://github.com/ocornut/imgui/issues/1720
bool Splitter(bool split_vertically, float thickness, float* size1, float* size2, float min_size1, float min_size2, float splitter_long_axis_size = -1.0f)
{
using namespace ImGui;
ImGuiContext& g = *GImGui;
ImGuiWindow* window = g.CurrentWindow;
ImGuiID id = window->GetID("##Splitter");
ImRect bb;
bb.Min = window->DC.CursorPos + (split_vertically ? ImVec2(*size1, 0.0f) : ImVec2(0.0f, *size1));
bb.Max = bb.Min + CalcItemSize(split_vertically ? ImVec2(thickness, splitter_long_axis_size) : ImVec2(splitter_long_axis_size, thickness), 0.0f, 0.0f);
return SplitterBehavior(bb, id, split_vertically ? ImGuiAxis_X : ImGuiAxis_Y, size1, size2, min_size1, min_size2, 1.0f);
}
static std::string s_fs_root = std::string(1u, PATH_SEP);
inline int alphaSort(const struct dirent** a, const struct dirent** b)
{
return strcoll((*a)->d_name, (*b)->d_name);
}
#ifdef WIN32
inline bool wreplaceString(std::wstring& str, const std::wstring& oldStr, const std::wstring& newStr)
{
bool found = false;
size_t pos = 0;
while ((pos = str.find(oldStr, pos)) != std::wstring::npos)
{
found = true;
str.replace(pos, oldStr.length(), newStr);
pos += newStr.length();
}
return found;
}
inline std::vector<std::wstring> wsplitStringToVector(const std::wstring& text, char delimiter, bool pushEmpty)
{
std::vector<std::wstring> arr;
if (!text.empty())
{
std::wstring::size_type start = 0;
std::wstring::size_type end = text.find(delimiter, start);
while (end != std::wstring::npos)
{
std::wstring token = text.substr(start, end - start);
if (!token.empty() || (token.empty() && pushEmpty)) //-V728
arr.push_back(token);
start = end + 1;
end = text.find(delimiter, start);
}
std::wstring token = text.substr(start);
if (!token.empty() || (token.empty() && pushEmpty)) //-V728
arr.push_back(token);
}
return arr;
}
#endif
inline bool replaceString(std::string& str, const std::string& oldStr, const std::string& newStr)
{
bool found = false;
size_t pos = 0;
while ((pos = str.find(oldStr, pos)) != std::string::npos)
{
found = true;
str.replace(pos, oldStr.length(), newStr);
pos += newStr.length();
}
return found;
}
inline std::vector<std::string> splitStringToVector(const std::string& text, char delimiter, bool pushEmpty)
{
std::vector<std::string> arr;
if (!text.empty())
{
std::string::size_type start = 0;
std::string::size_type end = text.find(delimiter, start);
while (end != std::string::npos)
{
std::string token = text.substr(start, end - start);
if (!token.empty() || (token.empty() && pushEmpty)) //-V728
arr.push_back(token);
start = end + 1;
end = text.find(delimiter, start);
}
std::string token = text.substr(start);
if (!token.empty() || (token.empty() && pushEmpty)) //-V728
arr.push_back(token);
}
return arr;
}
inline std::vector<std::string> GetDrivesList()
{
std::vector<std::string> res;
#ifdef WIN32
const DWORD mydrives = 2048;
char lpBuffer[2048];
#define mini(a,b) (((a) < (b)) ? (a) : (b))
const DWORD countChars = mini(GetLogicalDriveStringsA(mydrives, lpBuffer), 2047);
#undef mini
if (countChars > 0)
{
std::string var = std::string(lpBuffer, (size_t)countChars);
replaceString(var, "\\", "");
res = splitStringToVector(var, '\0', false);
}
#endif // WIN32
return res;
}
inline bool IsDirectoryExist(const std::string& name)
{
bool bExists = false;
if (!name.empty())
{
DIR* pDir = nullptr;
pDir = opendir(name.c_str());
if (pDir != nullptr)
{
bExists = true;
(void)closedir(pDir);
}
}
return bExists; // this is not a directory!
}
#ifdef WIN32
inline std::wstring wGetString(const char* str)
{
std::wstring ret;
size_t sz;
if (!dirent_mbstowcs_s(&sz, nullptr, 0, str, 0))
{
ret.resize(sz);
dirent_mbstowcs_s(nullptr, (wchar_t*)ret.data(), sz, str, sz - 1);
}
return ret;
}
#endif
inline bool CreateDirectoryIfNotExist(const std::string& name)
{
bool res = false;
if (!name.empty())
{
if (!IsDirectoryExist(name))
{
#ifdef WIN32
std::wstring wname = wGetString(name.c_str());
if (CreateDirectoryW(wname.c_str(), nullptr))
{
res = true;
}
#elif defined(__EMSCRIPTEN__)
std::string str = std::string("FS.mkdir('") + name + "');";
emscripten_run_script(str.c_str());
res = true;
#elif defined(UNIX)
char buffer[PATH_MAX] = {};
snprintf(buffer, PATH_MAX, "mkdir -p %s", name.c_str());
const int dir_err = std::system(buffer);
if (dir_err != -1)
{
res = true;
}
#endif // defined(UNIX)
if (!res) {
std::cout << "Error creating directory " << name << std::endl;
}
}
}
return res;
}
struct PathStruct
{
std::string path;
std::string name;
std::string ext;
bool isOk;
PathStruct()
{
isOk = false;
}
};
inline PathStruct ParsePathFileName(const std::string& vPathFileName)
{
PathStruct res;
if (!vPathFileName.empty())
{
std::string pfn = vPathFileName;
std::string separator(1u, PATH_SEP);
replaceString(pfn, "\\", separator);
replaceString(pfn, "/", separator);
size_t lastSlash = pfn.find_last_of(separator);
if (lastSlash != std::string::npos)
{
res.name = pfn.substr(lastSlash + 1);
res.path = pfn.substr(0, lastSlash);
res.isOk = true;
}
size_t lastPoint = pfn.find_last_of('.');
if (lastPoint != std::string::npos)
{
if (!res.isOk)
{
res.name = pfn;
res.isOk = true;
}
res.ext = pfn.substr(lastPoint + 1);
replaceString(res.name, "." + res.ext, "");
}
}
return res;
}
inline void AppendToBuffer(char* vBuffer, size_t vBufferLen, const std::string& vStr)
{
std::string st = vStr;
size_t len = vBufferLen - 1u;
size_t slen = strlen(vBuffer);
if (!st.empty() && st != "\n")
{
replaceString(st, "\n", "");
replaceString(st, "\r", "");
}
vBuffer[slen] = '\0';
std::string str = std::string(vBuffer);
//if (!str.empty()) str += "\n";
str += vStr;
if (len > str.size()) len = str.size();
#ifdef MSVC
strncpy_s(vBuffer, vBufferLen, str.c_str(), len);
#else // MSVC
strncpy(vBuffer, str.c_str(), len);
#endif // MSVC
vBuffer[len] = '\0';
}
inline void ResetBuffer(char* vBuffer)
{
vBuffer[0] = '\0';
}
inline void SetBuffer(char* vBuffer, size_t vBufferLen, const std::string& vStr)
{
ResetBuffer(vBuffer);
AppendToBuffer(vBuffer, vBufferLen, vStr);
}
IGFD::FileDialog::FileDialog()
{
m_AnyWindowsHovered = false;
m_IsOk = false;
m_ShowDialog = false;
m_ShowDrives = false;
m_CreateDirectoryMode = false;
dlg_optionsPane = nullptr;
dlg_optionsPaneWidth = 250;
dlg_filters = "";
dlg_userDatas = 0;
#ifdef USE_BOOKMARK
m_BookmarkPaneShown = false;
m_BookmarkWidth = defaultBookmarkPaneWith;
#endif // USE_BOOKMARK
}
IGFD::FileDialog::~FileDialog() = default;
//////////////////////////////////////////////////////////////////////////////////////////////////
///// CUSTOM SELECTABLE (Flashing Support) ///////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
#ifdef USE_EXPLORATION_BY_KEYS
bool IGFD::FileDialog::FlashableSelectable(const char* label, bool selected,
ImGuiSelectableFlags flags, bool vFlashing, const ImVec2& size_arg)
{
using namespace ImGui;
ImGuiWindow* window = GetCurrentWindow();
if (window->SkipItems)
return false;
ImGuiContext& g = *GImGui;
const ImGuiStyle& style = g.Style;
// Submit label or explicit size to ItemSize(), whereas ItemAdd() will submit a larger/spanning rectangle.
ImGuiID id = window->GetID(label);
ImVec2 label_size = CalcTextSize(label, NULL, true);
ImVec2 size(size_arg.x != 0.0f ? size_arg.x : label_size.x, size_arg.y != 0.0f ? size_arg.y : label_size.y);
ImVec2 pos = window->DC.CursorPos;
pos.y += window->DC.CurrLineTextBaseOffset;
ItemSize(size, 0.0f);
// Fill horizontal space
// We don't support (size < 0.0f) in Selectable() because the ItemSpacing extension would make explicitly right-aligned sizes not visibly match other widgets.
const bool span_all_columns = (flags & ImGuiSelectableFlags_SpanAllColumns) != 0;
const float min_x = span_all_columns ? window->ParentWorkRect.Min.x : pos.x;
const float max_x = span_all_columns ? window->ParentWorkRect.Max.x : window->WorkRect.Max.x;
if (size_arg.x == 0.0f || (flags & ImGuiSelectableFlags_SpanAvailWidth))
size.x = ImMax(label_size.x, max_x - min_x);
// Text stays at the submission position, but bounding box may be extended on both sides
const ImVec2 text_min = pos;
const ImVec2 text_max(min_x + size.x, pos.y + size.y);
// Selectables are meant to be tightly packed together with no click-gap, so we extend their box to cover spacing between selectable.
ImRect bb(min_x, pos.y, text_max.x, text_max.y);
if ((flags & ImGuiSelectableFlags_NoPadWithHalfSpacing) == 0)
{
const float spacing_x = span_all_columns ? 0.0f : style.ItemSpacing.x;
const float spacing_y = style.ItemSpacing.y;
const float spacing_L = IM_FLOOR(spacing_x * 0.50f);
const float spacing_U = IM_FLOOR(spacing_y * 0.50f);
bb.Min.x -= spacing_L;
bb.Min.y -= spacing_U;
bb.Max.x += (spacing_x - spacing_L);
bb.Max.y += (spacing_y - spacing_U);
}
//if (g.IO.KeyCtrl) { GetForegroundDrawList()->AddRect(bb.Min, bb.Max, IM_COL32(0, 255, 0, 255)); }
// Modify ClipRect for the ItemAdd(), faster than doing a PushColumnsBackground/PushTableBackground for every Selectable..
const float backup_clip_rect_min_x = window->ClipRect.Min.x;
const float backup_clip_rect_max_x = window->ClipRect.Max.x;
if (span_all_columns)
{
window->ClipRect.Min.x = window->ParentWorkRect.Min.x;
window->ClipRect.Max.x = window->ParentWorkRect.Max.x;
}
bool item_add;
const bool disabled_item = (flags & ImGuiSelectableFlags_Disabled) != 0;
if (disabled_item)
{
ImGuiItemFlags backup_item_flags = g.CurrentItemFlags;
g.CurrentItemFlags |= ImGuiItemFlags_Disabled;
item_add = ItemAdd(bb, id);
g.CurrentItemFlags = backup_item_flags;
}
else
{
item_add = ItemAdd(bb, id);
}
if (span_all_columns)
{
window->ClipRect.Min.x = backup_clip_rect_min_x;
window->ClipRect.Max.x = backup_clip_rect_max_x;
}
if (!item_add)
return false;
const bool disabled_global = (g.CurrentItemFlags & ImGuiItemFlags_Disabled) != 0;
if (disabled_item && !disabled_global)
PushDisabled(true);
// FIXME: We can standardize the behavior of those two, we could also keep the fast path of override ClipRect + full push on render only,
// which would be advantageous since most selectable are not selected.
if (span_all_columns && window->DC.CurrentColumns)
PushColumnsBackground();
else if (span_all_columns && g.CurrentTable)
TablePushBackgroundChannel();
// We use NoHoldingActiveID on menus so user can click and _hold_ on a menu then drag to browse child entries
ImGuiButtonFlags button_flags = 0;
if (flags & ImGuiSelectableFlags_NoHoldingActiveID) { button_flags |= ImGuiButtonFlags_NoHoldingActiveId; }
if (flags & ImGuiSelectableFlags_SelectOnClick) { button_flags |= ImGuiButtonFlags_PressedOnClick; }
if (flags & ImGuiSelectableFlags_SelectOnRelease) { button_flags |= ImGuiButtonFlags_PressedOnRelease; }
if (flags & ImGuiSelectableFlags_AllowDoubleClick) { button_flags |= ImGuiButtonFlags_PressedOnClickRelease | ImGuiButtonFlags_PressedOnDoubleClick; }
if (flags & ImGuiSelectableFlags_AllowItemOverlap) { button_flags |= ImGuiButtonFlags_AllowItemOverlap; }
const bool was_selected = selected;
bool hovered, held;
bool pressed = ButtonBehavior(bb, id, &hovered, &held, button_flags);
// Auto-select when moved into
// - This will be more fully fleshed in the range-select branch
// - This is not exposed as it won't nicely work with some user side handling of shift/control
// - We cannot do 'if (g.NavJustMovedToId != id) { selected = false; pressed = was_selected; }' for two reasons
// - (1) it would require focus scope to be set, need exposing PushFocusScope() or equivalent (e.g. BeginSelection() calling PushFocusScope())
// - (2) usage will fail with clipped items
// The multi-select API aim to fix those issues, e.g. may be replaced with a BeginSelection() API.
if ((flags & ImGuiSelectableFlags_SelectOnNav) && g.NavJustMovedToId != 0 && g.NavJustMovedToFocusScopeId == window->DC.NavFocusScopeIdCurrent)
if (g.NavJustMovedToId == id)
selected = pressed = true;
// Update NavId when clicking or when Hovering (this doesn't happen on most widgets), so navigation can be resumed with gamepad/keyboard
if (pressed || (hovered && (flags & ImGuiSelectableFlags_SetNavIdOnHover)))
{
if (!g.NavDisableMouseHover && g.NavWindow == window && g.NavLayer == window->DC.NavLayerCurrent)
{
SetNavID(id, window->DC.NavLayerCurrent, window->DC.NavFocusScopeIdCurrent, ImRect(bb.Min - window->Pos, bb.Max - window->Pos));
g.NavDisableHighlight = true;
}
}
if (pressed)
MarkItemEdited(id);
if (flags & ImGuiSelectableFlags_AllowItemOverlap)
SetItemAllowOverlap();
// In this branch, Selectable() cannot toggle the selection so this will never trigger.
if (selected != was_selected) //-V547
window->DC.LastItemStatusFlags |= ImGuiItemStatusFlags_ToggledSelection;
// Render
if (held && (flags & ImGuiSelectableFlags_DrawHoveredWhenHeld) || vFlashing)
hovered = true;
if (hovered || selected)
{
const ImU32 col = GetColorU32((held && hovered) ? ImGuiCol_HeaderActive : hovered ? ImGuiCol_HeaderHovered : ImGuiCol_Header);
RenderFrame(bb.Min, bb.Max, col, false, 0.0f);
}
RenderNavHighlight(bb, id, ImGuiNavHighlightFlags_TypeThin | ImGuiNavHighlightFlags_NoRounding);
if (span_all_columns && window->DC.CurrentColumns)
PopColumnsBackground();
else if (span_all_columns && g.CurrentTable)
TablePopBackgroundChannel();
RenderTextClipped(text_min, text_max, label, NULL, &label_size, style.SelectableTextAlign, &bb);
// Automatically close popups
if (pressed && (window->Flags & ImGuiWindowFlags_Popup) && !(flags & ImGuiSelectableFlags_DontClosePopups) && !(g.CurrentItemFlags & ImGuiItemFlags_SelectableDontClosePopup))
CloseCurrentPopup();
if (disabled_item && !disabled_global)
PopDisabled();
IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.LastItemStatusFlags);
return pressed;
}
#endif // USE_EXPLORATION_BY_KEYS
//////////////////////////////////////////////////////////////////////////////////////////////////
///// STANDARD DIALOG ////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
// path and fileName can be specified
void IGFD::FileDialog::OpenDialog(
const std::string& vKey,
const std::string& vTitle,
const char *vFilters,
const std::string& vPath,
const std::string& vFileName,
const int& vCountSelectionMax,
UserDatas vUserDatas,
ImGuiFileDialogFlags vFlags)
{
if (m_ShowDialog) // if already opened, quit
return;
dlg_key = vKey;
dlg_title = vTitle;
dlg_path = vPath;
dlg_userDatas = vUserDatas;
dlg_flags = vFlags;
dlg_optionsPane = nullptr;
dlg_optionsPaneWidth = 0.0f;
dlg_countSelectionMax = vCountSelectionMax;
dlg_modal = false;
dlg_defaultExt.clear();
ParseFilters(vFilters);
SetDefaultFileName(vFileName);
SetPath(m_CurrentPath);
m_ShowDialog = true; // open dialog
#ifdef USE_BOOKMARK
m_BookmarkPaneShown = false;
#endif // USE_BOOKMARK
}
// path and filename are obtained from filePathName
void IGFD::FileDialog::OpenDialog(
const std::string& vKey,
const std::string& vTitle,
const char *vFilters,
const std::string& vFilePathName,
const int& vCountSelectionMax,
UserDatas vUserDatas,
ImGuiFileDialogFlags vFlags)
{
if (m_ShowDialog) // if already opened, quit
return;
dlg_key = vKey;
dlg_title = vTitle;
auto ps = ParsePathFileName(vFilePathName);
if (ps.isOk)
{
dlg_path = ps.path;
SetDefaultFileName(ps.name + "." + ps.ext);
m_SelectedFileNames.clear();
m_SelectedFileNames.emplace(ps.name + "." + ps.ext);
dlg_defaultExt = "." + ps.ext;
}
else
{
dlg_path = ".";
SetDefaultFileName("");
dlg_defaultExt.clear();
}
dlg_optionsPane = nullptr;
dlg_optionsPaneWidth = 0.0f;
dlg_userDatas = vUserDatas;
dlg_flags = vFlags;
dlg_countSelectionMax = vCountSelectionMax; //-V101
dlg_modal = false;
ParseFilters(vFilters);
SetSelectedFilterWithExt(dlg_defaultExt);
SetPath(m_CurrentPath);
m_ShowDialog = true;
#ifdef USE_BOOKMARK
m_BookmarkPaneShown = false;
#endif // USE_BOOKMARK
}
// with pane
// path and fileName can be specified
void IGFD::FileDialog::OpenDialog(
const std::string& vKey,
const std::string& vTitle,
const char* vFilters,
const std::string& vPath,
const std::string& vFileName,
const PaneFun& vSidePane,
const float& vSidePaneWidth,
const int& vCountSelectionMax,
UserDatas vUserDatas,
ImGuiFileDialogFlags vFlags)
{
if (m_ShowDialog) // if already opened, quit
return;
dlg_key = vKey;
dlg_title = vTitle;
dlg_path = vPath;
dlg_userDatas = vUserDatas;
dlg_flags = vFlags;
dlg_optionsPane = vSidePane;
dlg_optionsPaneWidth = vSidePaneWidth;
dlg_countSelectionMax = vCountSelectionMax;
dlg_modal = false;
dlg_defaultExt.clear();
ParseFilters(vFilters);
SetDefaultFileName(vFileName);
SetPath(m_CurrentPath);
m_ShowDialog = true; // open dialog
#ifdef USE_BOOKMARK
m_BookmarkPaneShown = false;
#endif // USE_BOOKMARK
}
// with pane
// path and filename are obtained from filePathName
void IGFD::FileDialog::OpenDialog(
const std::string& vKey,
const std::string& vTitle,
const char* vFilters,
const std::string& vFilePathName,
const PaneFun& vSidePane,
const float& vSidePaneWidth,
const int& vCountSelectionMax,
UserDatas vUserDatas,
ImGuiFileDialogFlags vFlags)
{
if (m_ShowDialog) // if already opened, quit
return;
dlg_key = vKey;
dlg_title = vTitle;
auto ps = ParsePathFileName(vFilePathName);
if (ps.isOk)
{
dlg_path = ps.path;
SetDefaultFileName(ps.name + "." + ps.ext);
m_SelectedFileNames.clear();
m_SelectedFileNames.emplace(ps.name + "." + ps.ext);
dlg_defaultExt = "." + ps.ext;
}
else
{
dlg_path = ".";
SetDefaultFileName("");
dlg_defaultExt.clear();
}
dlg_optionsPane = vSidePane;
dlg_optionsPaneWidth = vSidePaneWidth;
dlg_userDatas = vUserDatas;
dlg_flags = vFlags;
dlg_countSelectionMax = vCountSelectionMax; //-V101
dlg_modal = false;
ParseFilters(vFilters);
SetSelectedFilterWithExt(dlg_defaultExt);
SetPath(m_CurrentPath);
m_ShowDialog = true;
#ifdef USE_BOOKMARK
m_BookmarkPaneShown = false;
#endif // USE_BOOKMARK
}
//////////////////////////////////////////////////////////////////////////////////////////////////
///// MODAL DIALOG ///////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
void IGFD::FileDialog::OpenModal(
const std::string& vKey,
const std::string& vTitle,
const char* vFilters,
const std::string& vPath,
const std::string& vFileName,
const int& vCountSelectionMax,
UserDatas vUserDatas,
ImGuiFileDialogFlags vFlags)
{
if (m_ShowDialog) // if already opened, quit
return;
OpenDialog(
vKey, vTitle, vFilters,
vPath, vFileName,
vCountSelectionMax, vUserDatas, vFlags);
dlg_modal = true;
}
void IGFD::FileDialog::OpenModal(
const std::string& vKey,
const std::string& vTitle,
const char *vFilters,
const std::string& vFilePathName,
const int& vCountSelectionMax,
UserDatas vUserDatas,
ImGuiFileDialogFlags vFlags)
{
if (m_ShowDialog) // if already opened, quit
return;
OpenDialog(
vKey, vTitle, vFilters,
vFilePathName,
vCountSelectionMax, vUserDatas, vFlags);
dlg_modal = true;
}
// with pane
// path and fileName can be specified
void IGFD::FileDialog::OpenModal(
const std::string& vKey,
const std::string& vTitle,
const char* vFilters,
const std::string& vPath,
const std::string& vFileName,
const PaneFun& vSidePane,
const float& vSidePaneWidth,
const int& vCountSelectionMax,
UserDatas vUserDatas,
ImGuiFileDialogFlags vFlags)
{
if (m_ShowDialog) // if already opened, quit
return;
OpenDialog(
vKey, vTitle, vFilters,
vPath, vFileName,
vSidePane, vSidePaneWidth,
vCountSelectionMax, vUserDatas, vFlags);
dlg_modal = true;
}
// with pane
// path and filename are obtained from filePathName
void IGFD::FileDialog::OpenModal(
const std::string& vKey,
const std::string& vTitle,
const char* vFilters,
const std::string& vFilePathName,
const PaneFun& vSidePane,
const float& vSidePaneWidth,
const int& vCountSelectionMax,
UserDatas vUserDatas,
ImGuiFileDialogFlags vFlags)
{
if (m_ShowDialog) // if already opened, quit
return;
OpenDialog(
vKey, vTitle, vFilters,
vFilePathName,
vSidePane, vSidePaneWidth,
vCountSelectionMax, vUserDatas, vFlags);
dlg_modal = true;
}
//////////////////////////////////////////////////////////////////////////////////////////////////
///// MAIN FUNCTION //////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
bool IGFD::FileDialog::Display(const std::string& vKey, ImGuiWindowFlags vFlags, ImVec2 vMinSize, ImVec2 vMaxSize)
{
if (m_ShowDialog && dlg_key == vKey)
{
bool res = false;
// to be sure than only one dialog is displayed per frame
ImGuiContext& g = *GImGui;
if (g.FrameCount == m_LastImGuiFrameCount) // one instance was displayed this frame before for this key +> quit
return res;
m_LastImGuiFrameCount = g.FrameCount; // mark this instance as used this frame
std::string name = dlg_title + "##" + dlg_key;
if (m_Name != name)
{
m_FileList.clear();
m_CurrentPath_Decomposition.clear();
}
m_IsOk = false; // reset dialog result
m_WantToQuit = false; // reset var used for start the dialog quit process from anywhere
ResetEvents();
ImGui::SetNextWindowSizeConstraints(vMinSize, vMaxSize);
bool beg = false;
if (dlg_modal &&
!m_OkResultToConfirm) // disable modal because the confirm dialog for overwrite is a new modal
{
ImGui::OpenPopup(name.c_str());
beg = ImGui::BeginPopupModal(name.c_str(), (bool*)nullptr,
vFlags | ImGuiWindowFlags_NoScrollbar);
}
else
{
beg = ImGui::Begin(name.c_str(), (bool*)nullptr, vFlags | ImGuiWindowFlags_NoScrollbar);
}
if (beg)
{
m_Name = name; //-V820
m_AnyWindowsHovered |= ImGui::IsWindowHovered();