Debian Codegraph @

android/app/jni/extern/igfd/ImGuiFileDialog.cpp

raw
1 // This is an independent project of an individual developer. Dear PVS-Studio, please check it.
2 // PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
3
4 /*
5 MIT License
6
7 Copyright (c) 2019-2020 Stephane Cuillerdier (aka aiekick)
8
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
15
16 The above copyright notice and this permission notice shall be included in all
17 copies or substantial portions of the Software.
18
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 SOFTWARE.
26 */
27
28 #ifndef IMGUI_DEFINE_MATH_OPERATORS
29 #define IMGUI_DEFINE_MATH_OPERATORS
30 #endif // IMGUI_DEFINE_MATH_OPERATORS
31
32 #include "ImGuiFileDialog.h"
33 #include "../../src/ta-log.h"
34
35 #ifdef __cplusplus
36
37 #include <cfloat>
38 #include <cstring> // stricmp / strcasecmp
39 #include <cstdarg> // variadic
40 #include <sstream>
41 #include <iomanip>
42 #include <ctime>
43 #include <sys/stat.h>
44 #include <cstdio>
45 #if defined (__EMSCRIPTEN__) // EMSCRIPTEN
46 #include <emscripten.h>
47 #endif // EMSCRIPTEN
48 #ifdef WIN32
49 #define stricmp _stricmp
50 #include <cctype>
51 #include "dirent/dirent.h" // directly open the dirent file attached to this lib
52 #define PATH_SEP '\\'
53 #define PATH_SEP_STR "\\"
54 #ifndef PATH_MAX
55 #define PATH_MAX 260
56 #endif // PATH_MAX
57 #elif defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__APPLE__) || defined (__EMSCRIPTEN__) || defined(__HAIKU__)
58 #define UNIX
59 #define stricmp strcasecmp
60 #include <sys/types.h>
61 #include <dirent.h>
62 #define PATH_SEP '/'
63 #define PATH_SEP_STR "/"
64 #endif // defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__APPLE__)
65
66 #include "imgui.h"
67 #include "imgui_internal.h"
68 #include <IconsFontAwesome4.h>
69
70 #include <cstdlib>
71 #include <algorithm>
72 #include <iostream>
73
74 #define DOUBLE_CLICKED ((singleClickSel && ImGui::IsMouseReleased(0)) || (!singleClickSel && ImGui::IsMouseDoubleClicked(0)))
75
76 #ifdef USE_THUMBNAILS
77 #ifndef DONT_DEFINE_AGAIN__STB_IMAGE_IMPLEMENTATION
78 #ifndef STB_IMAGE_IMPLEMENTATION
79 #define STB_IMAGE_IMPLEMENTATION
80 #endif // STB_IMAGE_IMPLEMENTATION
81 #endif // DONT_DEFINE_AGAIN__STB_IMAGE_IMPLEMENTATION
82 #include "stb/stb_image.h"
83 #ifndef DONT_DEFINE_AGAIN__STB_IMAGE_RESIZE_IMPLEMENTATION
84 #ifndef STB_IMAGE_RESIZE_IMPLEMENTATION
85 #define STB_IMAGE_RESIZE_IMPLEMENTATION
86 #endif // STB_IMAGE_RESIZE_IMPLEMENTATION
87 #endif // DONT_DEFINE_AGAIN__STB_IMAGE_RESIZE_IMPLEMENTATION
88 #include "stb/stb_image_resize.h"
89 #endif // USE_THUMBNAILS
90
91 namespace IGFD
92 {
93 // float comparisons
94 #ifndef IS_FLOAT_DIFFERENT
95 #define IS_FLOAT_DIFFERENT(a,b) (fabs((a) - (b)) > FLT_EPSILON)
96 #endif // IS_FLOAT_DIFFERENT
97 #ifndef IS_FLOAT_EQUAL
98 #define IS_FLOAT_EQUAL(a,b) (fabs((a) - (b)) < FLT_EPSILON)
99 #endif // IS_FLOAT_EQUAL
100 // width of filter combobox
101 #ifndef FILTER_COMBO_WIDTH
102 #define FILTER_COMBO_WIDTH 150.0f
103 #endif // FILTER_COMBO_WIDTH
104 #ifndef IMGUI_BUTTON
105 #define IMGUI_BUTTON ImGui::Button
106 #endif // IMGUI_BUTTON
107 // locales
108 #ifndef createDirButtonString
109 #define createDirButtonString ICON_FA_PLUS
110 #endif // createDirButtonString
111 #ifndef resetButtonString
112 #define resetButtonString ICON_FA_REPEAT
113 #endif // resetButtonString
114 #ifndef homeButtonString
115 #define homeButtonString ICON_FA_HOME
116 #endif // bomeButtonString
117 #ifndef drivesButtonString
118 #define drivesButtonString ICON_FA_HDD_O
119 #endif // drivesButtonString
120 #ifndef parentDirString
121 #define parentDirString ICON_FA_CHEVRON_UP
122 #endif // parentDirString
123 #ifndef editPathButtonString
124 #define editPathButtonString ICON_FA_PENCIL
125 #endif // editPathButtonString
126 #ifdef USE_THUMBNAILS
127 #ifndef tableHeaderFileThumbnailsString
128 #define tableHeaderFileThumbnailsString "Thumbnails"
129 #endif // tableHeaderFileThumbnailsString
130 #ifndef DisplayMode_FilesList_ButtonString
131 #define DisplayMode_FilesList_ButtonString "FL"
132 #endif // DisplayMode_FilesList_ButtonString
133 #ifndef DisplayMode_FilesList_ButtonHelp
134 #define DisplayMode_FilesList_ButtonHelp "File List"
135 #endif // DisplayMode_FilesList_ButtonHelp
136 #ifndef DisplayMode_ThumbailsList_ButtonString
137 #define DisplayMode_ThumbailsList_ButtonString "TL"
138 #endif // DisplayMode_ThumbailsList_ButtonString
139 #ifndef DisplayMode_ThumbailsList_ButtonHelp
140 #define DisplayMode_ThumbailsList_ButtonHelp "Thumbnails List"
141 #endif // DisplayMode_ThumbailsList_ButtonHelp
142 #ifndef DisplayMode_ThumbailsGrid_ButtonString
143 #define DisplayMode_ThumbailsGrid_ButtonString "TG"
144 #endif // DisplayMode_ThumbailsGrid_ButtonString
145 #ifndef DisplayMode_ThumbailsGrid_ButtonHelp
146 #define DisplayMode_ThumbailsGrid_ButtonHelp "Thumbnails Grid"
147 #endif // DisplayMode_ThumbailsGrid_ButtonHelp
148 #ifndef DisplayMode_ThumbailsList_ImageHeight
149 #define DisplayMode_ThumbailsList_ImageHeight 32.0f
150 #endif // DisplayMode_ThumbailsList_ImageHeight
151 #ifndef IMGUI_RADIO_BUTTON
152 inline bool inRadioButton(const char* vLabel, bool vToggled)
153 {
154 bool pressed = false;
155
156 if (vToggled)
157 {
158 ImVec4 bua = ImGui::GetStyleColorVec4(ImGuiCol_ButtonActive);
159 ImVec4 te = ImGui::GetStyleColorVec4(ImGuiCol_Text);
160 ImGui::PushStyleColor(ImGuiCol_Button, te);
161 ImGui::PushStyleColor(ImGuiCol_ButtonActive, te);
162 ImGui::PushStyleColor(ImGuiCol_ButtonHovered, te);
163 ImGui::PushStyleColor(ImGuiCol_Text, bua);
164 }
165
166 pressed = IMGUI_BUTTON(vLabel);
167
168 if (vToggled)
169 {
170 ImGui::PopStyleColor(4); //-V112
171 }
172
173 return pressed;
174 }
175 #define IMGUI_RADIO_BUTTON inRadioButton
176 #endif // IMGUI_RADIO_BUTTON
177 #endif // USE_THUMBNAILS
178 #ifdef USE_BOOKMARK
179 #ifndef defaultBookmarkPaneWith
180 #define defaultBookmarkPaneWith 150.0f
181 #endif // defaultBookmarkPaneWith
182 #ifndef bookmarksButtonString
183 #define bookmarksButtonString "Bookmark"
184 #endif // bookmarksButtonString
185 #ifndef bookmarksButtonHelpString
186 #define bookmarksButtonHelpString "Bookmark"
187 #endif // bookmarksButtonHelpString
188 #ifndef addBookmarkButtonString
189 #define addBookmarkButtonString "+"
190 #endif // addBookmarkButtonString
191 #ifndef removeBookmarkButtonString
192 #define removeBookmarkButtonString "-"
193 #endif // removeBookmarkButtonString
194 #ifndef IMGUI_TOGGLE_BUTTON
195 inline bool inToggleButton(const char* vLabel, bool* vToggled)
196 {
197 bool pressed = false;
198
199 if (vToggled && *vToggled)
200 {
201 ImVec4 bua = ImGui::GetStyleColorVec4(ImGuiCol_ButtonActive);
202 //ImVec4 buh = ImGui::GetStyleColorVec4(ImGuiCol_ButtonHovered);
203 //ImVec4 bu = ImGui::GetStyleColorVec4(ImGuiCol_Button);
204 ImVec4 te = ImGui::GetStyleColorVec4(ImGuiCol_Text);
205 ImGui::PushStyleColor(ImGuiCol_Button, te);
206 ImGui::PushStyleColor(ImGuiCol_ButtonActive, te);
207 ImGui::PushStyleColor(ImGuiCol_ButtonHovered, te);
208 ImGui::PushStyleColor(ImGuiCol_Text, bua);
209 }
210
211 pressed = IMGUI_BUTTON(vLabel);
212
213 if (vToggled && *vToggled)
214 {
215 ImGui::PopStyleColor(4); //-V112
216 }
217
218 if (vToggled && pressed)
219 *vToggled = !*vToggled;
220
221 return pressed;
222 }
223 #define IMGUI_TOGGLE_BUTTON inToggleButton
224 #endif // IMGUI_TOGGLE_BUTTON
225 #endif // USE_BOOKMARK
226
227 /////////////////////////////////////////////////////////////////////////////////////
228 //// INLINE FUNCTIONS ///////////////////////////////////////////////////////////////
229 /////////////////////////////////////////////////////////////////////////////////////
230
231 inline int inAlphaSort(const struct dirent** a, const struct dirent** b)
232 {
233 return strcoll((*a)->d_name, (*b)->d_name);
234 }
235
236 /////////////////////////////////////////////////////////////////////////////////////
237 //// FILE EXTENTIONS INFOS //////////////////////////////////////////////////////////
238 /////////////////////////////////////////////////////////////////////////////////////
239
240 IGFD::FileStyle::FileStyle()
241 : color(0, 0, 0, 0)
242 {
243
244 }
245
246 IGFD::FileStyle::FileStyle(const FileStyle& vStyle)
247 {
248 color = vStyle.color;
249 icon = vStyle.icon;
250 font = vStyle.font;
251 flags = vStyle.flags;
252 }
253
254 IGFD::FileStyle::FileStyle(const ImVec4& vColor, const std::string& vIcon, ImFont* vFont)
255 : color(vColor), icon(vIcon), font(vFont)
256 {
257
258 }
259
260 /////////////////////////////////////////////////////////////////////////////////////
261 //// FILE INFOS /////////////////////////////////////////////////////////////////////
262 /////////////////////////////////////////////////////////////////////////////////////
263
264 // https://github.com/ocornut/imgui/issues/1720
265 bool IGFD::Utils::Splitter(bool split_vertically, float thickness, float* size1, float* size2, float min_size1, float min_size2, float splitter_long_axis_size)
266 {
267 using namespace ImGui;
268 ImGuiContext& g = *GImGui;
269 ImGuiWindow* window = g.CurrentWindow;
270 ImGuiID id = window->GetID("##Splitter");
271 ImRect bb;
272 bb.Min = window->DC.CursorPos + (split_vertically ? ImVec2(*size1, 0.0f) : ImVec2(0.0f, *size1));
273 bb.Max = bb.Min + CalcItemSize(split_vertically ? ImVec2(thickness, splitter_long_axis_size) : ImVec2(splitter_long_axis_size, thickness), 0.0f, 0.0f);
274 return SplitterBehavior(bb, id, split_vertically ? ImGuiAxis_X : ImGuiAxis_Y, size1, size2, min_size1, min_size2, 1.0f);
275 }
276
277 #ifdef WIN32
278 bool IGFD::Utils::WReplaceString(std::wstring& str, const std::wstring& oldStr, const std::wstring& newStr)
279 {
280 bool found = false;
281 size_t pos = 0;
282 while ((pos = str.find(oldStr, pos)) != std::wstring::npos)
283 {
284 found = true;
285 str.replace(pos, oldStr.length(), newStr);
286 pos += newStr.length();
287 }
288 return found;
289 }
290
291 std::vector<std::wstring> IGFD::Utils::WSplitStringToVector(const std::wstring& text, char delimiter, bool pushEmpty)
292 {
293 std::vector<std::wstring> arr;
294 if (!text.empty())
295 {
296 std::wstring::size_type start = 0;
297 std::wstring::size_type end = text.find(delimiter, start);
298 while (end != std::wstring::npos)
299 {
300 std::wstring token = text.substr(start, end - start);
301 if (!token.empty() || (token.empty() && pushEmpty)) //-V728
302 arr.push_back(token);
303 start = end + 1;
304 end = text.find(delimiter, start);
305 }
306 std::wstring token = text.substr(start);
307 if (!token.empty() || (token.empty() && pushEmpty)) //-V728
308 arr.push_back(token);
309 }
310 return arr;
311 }
312
313 std::wstring IGFD::Utils::string_to_wstring(const std::string& str)
314 {
315 std::wstring ret;
316 if (!str.empty())
317 {
318 size_t sz = std::mbstowcs(nullptr, str.c_str(), str.size());
319 if (sz)
320 {
321 ret.resize(sz);
322 std::mbstowcs((wchar_t*)ret.data(), str.c_str(), sz);
323 }
324 }
325 return ret;
326 }
327
328 std::string IGFD::Utils::wstring_to_string(const std::wstring& str)
329 {
330 std::string ret;
331 if (!str.empty())
332 {
333 size_t sz = std::wcstombs(nullptr, str.c_str(), str.size());
334 if (sz)
335 {
336 ret.resize(sz);
337 std::wcstombs((char*)ret.data(), str.c_str(), sz);
338 }
339 }
340 return ret;
341 }
342 #endif // WIN32
343
344 bool IGFD::Utils::ReplaceString(std::string& str, const std::string& oldStr, const std::string& newStr)
345 {
346 bool found = false;
347 size_t pos = 0;
348 while ((pos = str.find(oldStr, pos)) != std::string::npos)
349 {
350 found = true;
351 str.replace(pos, oldStr.length(), newStr);
352 pos += newStr.length();
353 }
354 return found;
355 }
356
357 std::vector<std::string> IGFD::Utils::SplitStringToVector(const std::string& text, char delimiter, bool pushEmpty)
358 {
359 std::vector<std::string> arr;
360 if (!text.empty())
361 {
362 size_t start = 0;
363 size_t end = text.find(delimiter, start);
364 while (end != std::string::npos)
365 {
366 auto token = text.substr(start, end - start);
367 if (!token.empty() || (token.empty() && pushEmpty)) //-V728
368 arr.push_back(token);
369 start = end + 1;
370 end = text.find(delimiter, start);
371 }
372 auto token = text.substr(start);
373 if (!token.empty() || (token.empty() && pushEmpty)) //-V728
374 arr.push_back(token);
375 }
376 return arr;
377 }
378
379 std::vector<std::string> IGFD::Utils::GetDrivesList()
380 {
381 std::vector<std::string> res;
382
383 #ifdef WIN32
384 const DWORD mydrives = 2048;
385 char lpBuffer[2048];
386 #define mini(a,b) (((a) < (b)) ? (a) : (b))
387 const DWORD countChars = mini(GetLogicalDriveStringsA(mydrives, lpBuffer), 2047);
388 #undef mini
389 if (countChars > 0)
390 {
391 std::string var = std::string(lpBuffer, (size_t)countChars);
392 IGFD::Utils::ReplaceString(var, "\\", "");
393 res = IGFD::Utils::SplitStringToVector(var, '\0', false);
394 }
395 #endif // WIN32
396
397 return res;
398 }
399
400 bool IGFD::Utils::IsDirectoryExist(const std::string& name)
401 {
402 bool bExists = false;
403
404 if (!name.empty())
405 {
406 DIR* pDir = nullptr;
407 pDir = opendir(name.c_str());
408 if (pDir != nullptr)
409 {
410 bExists = true;
411 (void)closedir(pDir);
412 }
413 }
414
415 return bExists; // this is not a directory!
416 }
417
418 bool IGFD::Utils::CreateDirectoryIfNotExist(const std::string& name)
419 {
420 bool res = false;
421
422 if (!name.empty())
423 {
424 if (!IsDirectoryExist(name))
425 {
426 #ifdef WIN32
427 std::wstring wname = IGFD::Utils::string_to_wstring(name);
428 if (CreateDirectoryW(wname.c_str(), nullptr))
429 {
430 res = true;
431 }
432 #elif defined(__EMSCRIPTEN__)
433 std::string str = std::string("FS.mkdir('") + name + "');";
434 emscripten_run_script(str.c_str());
435 res = true;
436 #elif defined(UNIX)
437 char buffer[PATH_MAX] = {};
438 snprintf(buffer, PATH_MAX, "mkdir -p %s", name.c_str());
439 const int dir_err = std::system(buffer);
440 if (dir_err != -1)
441 {
442 res = true;
443 }
444 #endif // WIN32
445 if (!res) {
446 std::cout << "Error creating directory " << name << std::endl;
447 }
448 }
449 }
450
451 return res;
452 }
453
454 IGFD::Utils::PathStruct IGFD::Utils::ParsePathFileName(const std::string& vPathFileName)
455 {
456 PathStruct res;
457
458 if (!vPathFileName.empty())
459 {
460 std::string pfn = vPathFileName;
461 std::string separator(1u, PATH_SEP);
462 IGFD::Utils::ReplaceString(pfn, "\\", separator);
463 IGFD::Utils::ReplaceString(pfn, "/", separator);
464
465 size_t lastSlash = pfn.find_last_of(separator);
466 if (lastSlash != std::string::npos)
467 {
468 res.name = pfn.substr(lastSlash + 1);
469 res.path = pfn.substr(0, lastSlash);
470 res.isOk = true;
471 }
472
473 size_t lastPoint = pfn.find_last_of('.');
474 if (lastPoint != std::string::npos)
475 {
476 if (!res.isOk)
477 {
478 res.name = pfn;
479 res.isOk = true;
480 }
481 res.ext = pfn.substr(lastPoint + 1);
482 IGFD::Utils::ReplaceString(res.name, "." + res.ext, "");
483 }
484
485 if (res.path.empty()) {
486 res.path=separator;
487 }
488
489 if (!res.isOk)
490 {
491 res.name = std::move(pfn);
492 res.isOk = true;
493 }
494 }
495
496 return res;
497 }
498 void IGFD::Utils::AppendToBuffer(char* vBuffer, size_t vBufferLen, const std::string& vStr)
499 {
500 std::string st = vStr;
501 size_t len = vBufferLen - 1u;
502 size_t slen = strlen(vBuffer);
503
504 if (!st.empty() && st != "\n")
505 {
506 IGFD::Utils::ReplaceString(st, "\n", "");
507 IGFD::Utils::ReplaceString(st, "\r", "");
508 }
509 vBuffer[slen] = '\0';
510 std::string str = std::string(vBuffer);
511 //if (!str.empty()) str += "\n";
512 str += vStr;
513 if (len > str.size()) len = str.size();
514 #ifdef MSVC
515 strncpy_s(vBuffer, vBufferLen, str.c_str(), len);
516 #else // MSVC
517 strncpy(vBuffer, str.c_str(), len);
518 #endif // MSVC
519 vBuffer[len] = '\0';
520 }
521
522 void IGFD::Utils::ResetBuffer(char* vBuffer)
523 {
524 vBuffer[0] = '\0';
525 }
526
527 void IGFD::Utils::SetBuffer(char* vBuffer, size_t vBufferLen, const std::string& vStr)
528 {
529 ResetBuffer(vBuffer);
530 AppendToBuffer(vBuffer, vBufferLen, vStr);
531 }
532
533 /////////////////////////////////////////////////////////////////////////////////////
534 //// FILE INFOS /////////////////////////////////////////////////////////////////////
535 /////////////////////////////////////////////////////////////////////////////////////
536
537 bool IGFD::FileInfos::IsTagFound(const std::string& vTag) const
538 {
539 if (!vTag.empty())
540 {
541 if (fileNameExt_optimized == "..") return true;
542
543 return
544 fileNameExt_optimized.find(vTag) != std::string::npos || // first try wihtout case and accents
545 fileNameExt.find(vTag) != std::string::npos; // second if searched with case and accents
546 }
547
548 // if tag is empty => its a special case but all is found
549 return true;
550 }
551
552 /////////////////////////////////////////////////////////////////////////////////////
553 //// SEARCH MANAGER /////////////////////////////////////////////////////////////////
554 /////////////////////////////////////////////////////////////////////////////////////
555
556 void IGFD::SearchManager::Clear()
557 {
558 puSearchTag.clear();
559 IGFD::Utils::ResetBuffer(puSearchBuffer);
560 }
561
562 void IGFD::SearchManager::DrawSearchBar(FileDialogInternal& vFileDialogInternal)
563 {
564 // search field
565 if (IMGUI_BUTTON(resetButtonString "##BtnImGuiFileDialogSearchField"))
566 {
567 Clear();
568 vFileDialogInternal.puFileManager.ApplyFilteringOnFileList(vFileDialogInternal);
569 }
570 if (ImGui::IsItemHovered())
571 ImGui::SetTooltip("%s",FileDialog::Instance()->buttonResetSearchString);
572 ImGui::SameLine();
573 ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x);
574 bool edited = ImGui::InputTextWithHint("##InputImGuiFileDialogSearchField", FileDialog::Instance()->searchString, puSearchBuffer, MAX_FILE_DIALOG_NAME_BUFFER);
575 if (ImGui::GetItemID() == ImGui::GetActiveID())
576 puSearchInputIsActive = true;
577 ImGui::PopItemWidth();
578 if (edited)
579 {
580 puSearchTag = puSearchBuffer;
581 vFileDialogInternal.puFileManager.ApplyFilteringOnFileList(vFileDialogInternal);
582 }
583 }
584
585 /////////////////////////////////////////////////////////////////////////////////////
586 //// FILTER INFOS ///////////////////////////////////////////////////////////////////
587 /////////////////////////////////////////////////////////////////////////////////////
588
589 void IGFD::FilterManager::FilterInfos::clear()
590 {
591 filter.clear();
592 collectionfilters.clear();
593 }
594
595 bool IGFD::FilterManager::FilterInfos::empty() const
596 {
597 return filter.empty() && collectionfilters.empty();
598 }
599
600 bool IGFD::FilterManager::FilterInfos::exist(const std::string& vFilter) const
601 {
602 return filter == vFilter || (collectionfilters.find(vFilter) != collectionfilters.end());
603 }
604
605 /////////////////////////////////////////////////////////////////////////////////////
606 //// FILTER MANAGER /////////////////////////////////////////////////////////////////
607 /////////////////////////////////////////////////////////////////////////////////////
608
609 void IGFD::FilterManager::ParseFilters(const char* vFilters)
610 {
611 prParsedFilters.clear();
612
613 if (vFilters)
614 puDLGFilters = vFilters; // file mode
615 else
616 puDLGFilters.clear(); // directory mode
617
618 if (!puDLGFilters.empty())
619 {
620 // ".*,.cpp,.h,.hpp"
621 // "Source files{.cpp,.h,.hpp},Image files{.png,.gif,.jpg,.jpeg},.md"
622
623 bool currentFilterFound = false;
624
625 size_t nan = std::string::npos;
626 size_t p = 0, lp = 0;
627 while ((p = puDLGFilters.find_first_of("{,", p)) != nan)
628 {
629 FilterInfos infos;
630
631 if (puDLGFilters[p] == '{') // {
632 {
633 infos.filter = puDLGFilters.substr(lp, p - lp);
634 p++;
635 lp = puDLGFilters.find('}', p);
636 if (lp != nan)
637 {
638 std::string fs = puDLGFilters.substr(p, lp - p);
639 auto arr = IGFD::Utils::SplitStringToVector(fs, ',', false);
640 for (auto a : arr)
641 {
642 infos.firstFilter=a;
643 infos.collectionfilters.emplace(a);
644 }
645 }
646 p = lp + 1;
647 }
648 else // ,
649 {
650 infos.filter = puDLGFilters.substr(lp, p - lp);
651 p++;
652 }
653
654 if (!currentFilterFound && prSelectedFilter.filter == infos.filter)
655 {
656 currentFilterFound = true;
657 prSelectedFilter = infos;
658 }
659
660 lp = p;
661 if (!infos.empty())
662 prParsedFilters.emplace_back(infos);
663 }
664
665 std::string token = puDLGFilters.substr(lp);
666 if (!token.empty())
667 {
668 FilterInfos infos;
669 infos.filter = std::move(token);
670 prParsedFilters.emplace_back(infos);
671 }
672
673 if (!currentFilterFound)
674 if (!prParsedFilters.empty())
675 prSelectedFilter = *prParsedFilters.begin();
676 }
677 }
678
679 void IGFD::FilterManager::SetSelectedFilterWithExt(const std::string& vFilter)
680 {
681 if (!prParsedFilters.empty())
682 {
683 if (!vFilter.empty())
684 {
685 // std::map<std::string, FilterInfos>
686 for (const auto& infos : prParsedFilters)
687 {
688 if (vFilter == infos.filter)
689 {
690 prSelectedFilter = infos;
691 }
692 else
693 {
694 // maybe this ext is in an extention so we will
695 // explore the collections is they are existing
696 for (const auto& filter : infos.collectionfilters)
697 {
698 if (vFilter == filter)
699 {
700 prSelectedFilter = infos;
701 }
702 }
703 }
704 }
705 }
706
707 if (prSelectedFilter.empty())
708 prSelectedFilter = *prParsedFilters.begin();
709 }
710 }
711
712 void IGFD::FilterManager::SetFileStyle(const IGFD_FileStyleFlags& vFlags, const char* vCriteria, const FileStyle& vInfos)
713 {
714 std::string _criteria;
715 if (vCriteria)
716 _criteria = std::string(vCriteria);
717 prFilesStyle[vFlags][_criteria] = std::make_shared<FileStyle>(vInfos);
718 prFilesStyle[vFlags][_criteria]->flags = vFlags;
719 }
720
721 // will be called internally
722 // will not been exposed to IGFD API
723 bool IGFD::FilterManager::prFillFileStyle(FileInfos& vFileInfos) const
724 {
725 if (!prFilesStyle.empty())
726 {
727 for (const auto& _flag : prFilesStyle)
728 {
729 for (const auto& _file : _flag.second)
730 {
731 if (_flag.first & IGFD_FileStyleByTypeDir && vFileInfos.fileType == 'd')
732 {
733 if (_file.first.empty()) // for all dirs
734 {
735 vFileInfos.fileStyle = _file.second;
736 }
737 else if (_file.first == vFileInfos.fileNameExt) // for dirs who are equal to style criteria
738 {
739 vFileInfos.fileStyle = _file.second;
740 }
741 }
742 else if (_flag.first & IGFD_FileStyleByTypeFile && vFileInfos.fileType == 'f')
743 {
744 if (_file.first.empty()) // for all files
745 {
746 vFileInfos.fileStyle = _file.second;
747 }
748 else if (_file.first == vFileInfos.fileNameExt) // for files who are equal to style criteria
749 {
750 vFileInfos.fileStyle = _file.second;
751 }
752 }
753 else if (_flag.first & IGFD_FileStyleByTypeLink && vFileInfos.fileType == 'l')
754 {
755 if (_file.first.empty()) // for all links
756 {
757 vFileInfos.fileStyle = _file.second;
758 }
759 else if (_file.first == vFileInfos.fileNameExt) // for links who are equal to style criteria
760 {
761 vFileInfos.fileStyle = _file.second;
762 }
763 }
764
765 if (_flag.first & IGFD_FileStyleByExtention)
766 {
767 if (_file.first == vFileInfos.fileExt)
768 {
769 vFileInfos.fileStyle = _file.second;
770 }
771
772 // can make sense for some dirs like the hidden by ex ".git"
773 if (_flag.first & IGFD_FileStyleByTypeDir && vFileInfos.fileType == 'd')
774 {
775 if (_file.first == vFileInfos.fileExt)
776 {
777 vFileInfos.fileStyle = _file.second;
778 }
779 }
780 else if (_flag.first & IGFD_FileStyleByTypeFile && vFileInfos.fileType == 'f')
781 {
782 if (_file.first == vFileInfos.fileExt)
783 {
784 vFileInfos.fileStyle = _file.second;
785 }
786 }
787 else if (_flag.first & IGFD_FileStyleByTypeLink && vFileInfos.fileType == 'l')
788 {
789 if (_file.first == vFileInfos.fileExt)
790 {
791 vFileInfos.fileStyle = _file.second;
792 }
793 }
794 }
795 if (_flag.first & IGFD_FileStyleByFullName)
796 {
797 if (_file.first == vFileInfos.fileNameExt)
798 {
799 vFileInfos.fileStyle = _file.second;
800 }
801
802 if (_flag.first & IGFD_FileStyleByTypeDir && vFileInfos.fileType == 'd')
803 {
804 if (_file.first == vFileInfos.fileNameExt)
805 {
806 vFileInfos.fileStyle = _file.second;
807 }
808 }
809 else if (_flag.first & IGFD_FileStyleByTypeFile && vFileInfos.fileType == 'f')
810 {
811 if (_file.first == vFileInfos.fileNameExt)
812 {
813 vFileInfos.fileStyle = _file.second;
814 }
815 }
816 else if (_flag.first & IGFD_FileStyleByTypeLink && vFileInfos.fileType == 'l')
817 {
818 if (_file.first == vFileInfos.fileNameExt)
819 {
820 vFileInfos.fileStyle = _file.second;
821 }
822 }
823 }
824 if (_flag.first & IGFD_FileStyleByContainedInFullName)
825 {
826 if (vFileInfos.fileNameExt.find(_file.first) != std::string::npos)
827 {
828 vFileInfos.fileStyle = _file.second;
829 }
830
831 if (_flag.first & IGFD_FileStyleByTypeDir && vFileInfos.fileType == 'd')
832 {
833 if (vFileInfos.fileNameExt.find(_file.first) != std::string::npos)
834 {
835 vFileInfos.fileStyle = _file.second;
836 }
837 }
838 else if (_flag.first & IGFD_FileStyleByTypeFile && vFileInfos.fileType == 'f')
839 {
840 if (vFileInfos.fileNameExt.find(_file.first) != std::string::npos)
841 {
842 vFileInfos.fileStyle = _file.second;
843 }
844 }
845 else if (_flag.first & IGFD_FileStyleByTypeLink && vFileInfos.fileType == 'l')
846 {
847 if (vFileInfos.fileNameExt.find(_file.first) != std::string::npos)
848 {
849 vFileInfos.fileStyle = _file.second;
850 }
851 }
852 }
853
854 if (vFileInfos.fileStyle.use_count())
855 return true;
856 }
857 }
858 }
859
860 return false;
861 }
862
863 void IGFD::FilterManager::SetFileStyle(const IGFD_FileStyleFlags& vFlags, const char* vCriteria, const ImVec4& vColor, const std::string& vIcon, ImFont* vFont)
864 {
865 std::string _criteria;
866 if (vCriteria)
867 _criteria = std::string(vCriteria);
868 prFilesStyle[vFlags][_criteria] = std::make_shared<FileStyle>(vColor, vIcon, vFont);
869 prFilesStyle[vFlags][_criteria]->flags = vFlags;
870 }
871
872 // todo : to refactor this fucking function
873 bool IGFD::FilterManager::GetFileStyle(const IGFD_FileStyleFlags& vFlags, const std::string& vCriteria, ImVec4* vOutColor, std::string* vOutIcon, ImFont **vOutFont)
874 {
875 if (vOutColor)
876 {
877 if (!prFilesStyle.empty())
878 {
879 if (prFilesStyle.find(vFlags) != prFilesStyle.end()) // found
880 {
881 if (vFlags & IGFD_FileStyleByContainedInFullName)
882 {
883 // search for vCriteria who are containing the criteria
884 for (const auto& _file : prFilesStyle.at(vFlags))
885 {
886 if (vCriteria.find(_file.first) != std::string::npos)
887 {
888 if (_file.second.use_count())
889 {
890 *vOutColor = _file.second->color;
891 if (vOutIcon)
892 *vOutIcon = _file.second->icon;
893 if (vOutFont)
894 *vOutFont = _file.second->font;
895 return true;
896 }
897 }
898 }
899 }
900 else
901 {
902 if (prFilesStyle.at(vFlags).find(vCriteria) != prFilesStyle.at(vFlags).end()) // found
903 {
904 *vOutColor = prFilesStyle[vFlags][vCriteria]->color;
905 if (vOutIcon)
906 *vOutIcon = prFilesStyle[vFlags][vCriteria]->icon;
907 if (vOutFont)
908 *vOutFont = prFilesStyle[vFlags][vCriteria]->font;
909 return true;
910 }
911 }
912 }
913 else
914 {
915 // search for flag composition
916 for (const auto& _flag : prFilesStyle)
917 {
918 if (_flag.first & vFlags)
919 {
920 if (_flag.first & IGFD_FileStyleByContainedInFullName)
921 {
922 // search for vCriteria who are containing the criteria
923 for (const auto& _file : prFilesStyle.at(_flag.first))
924 {
925 if (vCriteria.find(_file.first) != std::string::npos)
926 {
927 if (_file.second.use_count())
928 {
929 *vOutColor = _file.second->color;
930 if (vOutIcon)
931 *vOutIcon = _file.second->icon;
932 if (vOutFont)
933 *vOutFont = _file.second->font;
934 return true;
935 }
936 }
937 }
938 }
939 else
940 {
941 if (prFilesStyle.at(_flag.first).find(vCriteria) != prFilesStyle.at(_flag.first).end()) // found
942 {
943 *vOutColor = prFilesStyle[_flag.first][vCriteria]->color;
944 if (vOutIcon)
945 *vOutIcon = prFilesStyle[_flag.first][vCriteria]->icon;
946 if (vOutFont)
947 *vOutFont = prFilesStyle[_flag.first][vCriteria]->font;
948 return true;
949 }
950 }
951 }
952 }
953 }
954 }
955 }
956 return false;
957 }
958
959 void IGFD::FilterManager::ClearFilesStyle()
960 {
961 prFilesStyle.clear();
962 }
963
964 bool IGFD::FilterManager::IsCoveredByFilters(const std::string& vTag) const
965 {
966 if (!puDLGFilters.empty() && !prSelectedFilter.empty())
967 {
968 // check if current file extention is covered by current filter
969 // we do that here, for avoid doing that during filelist display
970 // for better fps
971 if (prSelectedFilter.exist(vTag) || prSelectedFilter.firstFilter == ".*")
972 {
973 return true;
974 }
975 }
976
977 return false;
978 }
979
980 bool IGFD::FilterManager::DrawFilterComboBox(FileDialogInternal& vFileDialogInternal)
981 {
982 // combobox of filters
983 if (!puDLGFilters.empty())
984 {
985 ImGui::SameLine();
986
987 bool needToApllyNewFilter = false;
988
989 ImGui::PushItemWidth(FILTER_COMBO_WIDTH*FileDialog::Instance()->DpiScale);
990 if (ImGui::BeginCombo("##Filters", prSelectedFilter.filter.c_str(), ImGuiComboFlags_None))
991 {
992 intptr_t i = 0;
993 for (const auto& filter : prParsedFilters)
994 {
995 const bool item_selected = (filter.filter == prSelectedFilter.filter);
996 ImGui::PushID((void*)(intptr_t)i++);
997 if (ImGui::Selectable(filter.filter.c_str(), item_selected))
998 {
999 prSelectedFilter = filter;
1000 needToApllyNewFilter = true;
1001 }
1002 ImGui::PopID();
1003 }
1004
1005 ImGui::EndCombo();
1006 }
1007 ImGui::PopItemWidth();
1008
1009 if (needToApllyNewFilter)
1010 {
1011 vFileDialogInternal.puFileManager.OpenCurrentPath(vFileDialogInternal);
1012 }
1013
1014 return needToApllyNewFilter;
1015 }
1016
1017 return false;
1018 }
1019
1020 IGFD::FilterManager::FilterInfos IGFD::FilterManager::GetSelectedFilter()
1021 {
1022 return prSelectedFilter;
1023 }
1024
1025 std::string IGFD::FilterManager::ReplaceExtentionWithCurrentFilter(const std::string& vFile) const
1026 {
1027 auto result = vFile;
1028
1029 if (!result.empty())
1030 {
1031 // if not a collection we can replace the filter by the extention we want
1032 if (prSelectedFilter.collectionfilters.empty())
1033 {
1034 size_t lastPoint = vFile.find_last_of('.');
1035 if (lastPoint != std::string::npos)
1036 {
1037 result = result.substr(0, lastPoint);
1038 }
1039
1040 result += prSelectedFilter.filter;
1041 }
1042 }
1043
1044 return result;
1045 }
1046
1047 void IGFD::FilterManager::SetDefaultFilterIfNotDefined()
1048 {
1049 if (prSelectedFilter.empty() && // no filter selected
1050 !prParsedFilters.empty()) // filter exist
1051 prSelectedFilter = *prParsedFilters.begin(); // we take the first filter
1052 }
1053
1054 /////////////////////////////////////////////////////////////////////////////////////
1055 //// FILE MANAGER ///////////////////////////////////////////////////////////////////
1056 /////////////////////////////////////////////////////////////////////////////////////
1057
1058 IGFD::FileManager::FileManager()
1059 {
1060 puFsRoot = std::string(1u, PATH_SEP);
1061 puSortingDirection[0]=true;
1062 for (int i=1; i<4; i++) {
1063 puSortingDirection[i]=false;
1064 }
1065 invalidFile.isValid=false;
1066 }
1067
1068 void IGFD::FileManager::OpenCurrentPath(const FileDialogInternal& vFileDialogInternal)
1069 {
1070 puShowDrives = false;
1071 ClearComposer();
1072 ClearFileLists();
1073 if (puDLGDirectoryMode) // directory mode
1074 SetDefaultFileName(".");
1075 else
1076 SetDefaultFileName("");
1077 logV("IGFD: OpenCurrentPath()");
1078 ScanDir(vFileDialogInternal, GetCurrentPath());
1079 }
1080
1081 void IGFD::FileManager::SortFields(const FileDialogInternal& vFileDialogInternal, const SortingFieldEnum& vSortingField, const bool vCanChangeOrder)
1082 {
1083 logV("IGFD: SortFields()");
1084 if (vSortingField != SortingFieldEnum::FIELD_NONE)
1085 {
1086 puHeaderFileName = FileDialog::Instance()->tableHeaderFileNameString;
1087 puHeaderFileType = FileDialog::Instance()->tableHeaderFileTypeString;
1088 puHeaderFileSize = FileDialog::Instance()->tableHeaderFileSizeString;
1089 puHeaderFileDate = FileDialog::Instance()->tableHeaderFileDateString;
1090 #ifdef USE_THUMBNAILS
1091 puHeaderFileThumbnails = tableHeaderFileThumbnailsString;
1092 #endif // #ifdef USE_THUMBNAILS
1093 } else {
1094 logV("IGFD: sorting by NONE!");
1095 }
1096
1097 if (prFileList.empty()) {
1098 logV("IGFD: with an empty file list?");
1099 }
1100
1101 if (vSortingField == SortingFieldEnum::FIELD_FILENAME)
1102 {
1103 logV("IGFD: sorting by name");
1104 if (vCanChangeOrder && puSortingField == vSortingField) {
1105 //printf("Change the sorting\n");
1106 puSortingDirection[0] = !puSortingDirection[0];
1107 }
1108
1109 if (puSortingDirection[0])
1110 {
1111 #ifdef USE_CUSTOM_SORTING_ICON
1112 puHeaderFileName = tableHeaderDescendingIcon + puHeaderFileName;
1113 #endif // USE_CUSTOM_SORTING_ICON
1114 std::sort(prFileList.begin(), prFileList.end(),
1115 [](const FileInfos& a, const FileInfos& b) -> bool
1116 {
1117 // this code fail in c:\\Users with the link "All users". got a invalid comparator
1118 /*
1119 // use code from https://github.com/jackm97/ImGuiFileDialog/commit/bf40515f5a1de3043e60562dc1a494ee7ecd3571
1120 // strict ordering for file/directory types beginning in '.'
1121 // common on Linux platforms
1122 if (a.fileNameExt[0] == '.' && b.fileNameExt[0] != '.')
1123 return false;
1124 if (a.fileNameExt[0] != '.' && b.fileNameExt[0] == '.')
1125 return true;
1126 if (a.fileNameExt[0] == '.' && b.fileNameExt[0] == '.')
1127 {
1128 return (stricmp(a.fileNameExt.c_str(), b.fileNameExt.c_str()) < 0); // sort in insensitive case
1129 }
1130 */
1131 if (a.fileType != b.fileType) return (a.fileType == 'd'); // directory in first
1132 return (stricmp(a.fileNameExt.c_str(), b.fileNameExt.c_str()) < 0); // sort in insensitive case
1133 });
1134 }
1135 else
1136 {
1137 #ifdef USE_CUSTOM_SORTING_ICON
1138 puHeaderFileName = tableHeaderAscendingIcon + puHeaderFileName;
1139 #endif // USE_CUSTOM_SORTING_ICON
1140 std::sort(prFileList.begin(), prFileList.end(),
1141 [](const FileInfos& a, const FileInfos& b) -> bool
1142 {
1143 // this code fail in c:\\Users with the link "All users". got a invalid comparator
1144 /*
1145 // use code from https://github.com/jackm97/ImGuiFileDialog/commit/bf40515f5a1de3043e60562dc1a494ee7ecd3571
1146 // strict ordering for file/directory types beginning in '.'
1147 // common on Linux platforms
1148 if (a.fileNameExt[0] == '.' && b.fileNameExt[0] != '.')
1149 return false;
1150 if (a.fileNameExt[0] != '.' && b.fileNameExt[0] == '.')
1151 return true;
1152 if (a.fileNameExt[0] == '.' && b.fileNameExt[0] == '.')
1153 {
1154 return (stricmp(a.fileNameExt.c_str(), b.fileNameExt.c_str()) > 0); // sort in insensitive case
1155 }
1156 */
1157 return (stricmp(a.fileNameExt.c_str(), b.fileNameExt.c_str()) > 0); // sort in insensitive case
1158 });
1159 }
1160 }
1161 else if (vSortingField == SortingFieldEnum::FIELD_TYPE)
1162 {
1163 logV("IGFD: sorting by type");
1164 if (vCanChangeOrder && puSortingField == vSortingField)
1165 puSortingDirection[1] = !puSortingDirection[1];
1166
1167 if (puSortingDirection[1])
1168 {
1169 #ifdef USE_CUSTOM_SORTING_ICON
1170 puHeaderFileType = tableHeaderDescendingIcon + puHeaderFileType;
1171 #endif // USE_CUSTOM_SORTING_ICON
1172 std::sort(prFileList.begin(), prFileList.end(),
1173 [](const FileInfos& a, const FileInfos& b) -> bool
1174 {
1175 if (a.fileType != b.fileType) return (a.fileType == 'd'); // directory in first
1176 return (a.fileExt < b.fileExt); // else
1177 });
1178 }
1179 else
1180 {
1181 #ifdef USE_CUSTOM_SORTING_ICON
1182 puHeaderFileType = tableHeaderAscendingIcon + puHeaderFileType;
1183 #endif // USE_CUSTOM_SORTING_ICON
1184 std::sort(prFileList.begin(), prFileList.end(),
1185 [](const FileInfos& a, const FileInfos& b) -> bool
1186 {
1187 if (a.fileType != b.fileType) return (a.fileType != 'd'); // directory in last
1188 return (a.fileExt > b.fileExt); // else
1189 });
1190 }
1191 }
1192 else if (vSortingField == SortingFieldEnum::FIELD_SIZE)
1193 {
1194 logV("IGFD: sorting by size");
1195 if (vCanChangeOrder && puSortingField == vSortingField)
1196 puSortingDirection[2] = !puSortingDirection[2];
1197
1198 if (puSortingDirection[2])
1199 {
1200 #ifdef USE_CUSTOM_SORTING_ICON
1201 puHeaderFileSize = tableHeaderDescendingIcon + puHeaderFileSize;
1202 #endif // USE_CUSTOM_SORTING_ICON
1203 std::sort(prFileList.begin(), prFileList.end(),
1204 [](const FileInfos& a, const FileInfos& b) -> bool
1205 {
1206 if (a.fileType != b.fileType) return (a.fileType == 'd'); // directory in first
1207 return (a.fileSize < b.fileSize); // else
1208 });
1209 }
1210 else
1211 {
1212 #ifdef USE_CUSTOM_SORTING_ICON
1213 puHeaderFileSize = tableHeaderAscendingIcon + puHeaderFileSize;
1214 #endif // USE_CUSTOM_SORTING_ICON
1215 std::sort(prFileList.begin(), prFileList.end(),
1216 [](const FileInfos& a, const FileInfos& b) -> bool
1217 {
1218 if (a.fileType != b.fileType) return (a.fileType != 'd'); // directory in last
1219 return (a.fileSize > b.fileSize); // else
1220 });
1221 }
1222 }
1223 else if (vSortingField == SortingFieldEnum::FIELD_DATE)
1224 {
1225 logV("IGFD: sorting by date");
1226 if (vCanChangeOrder && puSortingField == vSortingField)
1227 puSortingDirection[3] = !puSortingDirection[3];
1228
1229 if (puSortingDirection[3])
1230 {
1231 #ifdef USE_CUSTOM_SORTING_ICON
1232 puHeaderFileDate = tableHeaderDescendingIcon + puHeaderFileDate;
1233 #endif // USE_CUSTOM_SORTING_ICON
1234 std::sort(prFileList.begin(), prFileList.end(),
1235 [](const FileInfos& a, const FileInfos& b) -> bool
1236 {
1237 if (a.fileType != b.fileType) return (a.fileType == 'd'); // directory in first
1238 return (a.fileModifDate < b.fileModifDate); // else
1239 });
1240 }
1241 else
1242 {
1243 #ifdef USE_CUSTOM_SORTING_ICON
1244 puHeaderFileDate = tableHeaderAscendingIcon + puHeaderFileDate;
1245 #endif // USE_CUSTOM_SORTING_ICON
1246 std::sort(prFileList.begin(), prFileList.end(),
1247 [](const FileInfos& a, const FileInfos& b) -> bool
1248 {
1249 if (a.fileType != b.fileType) return (a.fileType != 'd'); // directory in last
1250 return (a.fileModifDate > b.fileModifDate); // else
1251 });
1252 }
1253 }
1254 #ifdef USE_THUMBNAILS
1255 else if (vSortingField == SortingFieldEnum::FIELD_THUMBNAILS)
1256 {
1257 if (vCanChangeOrder && puSortingField == vSortingField)
1258 puSortingDirection[4] = !puSortingDirection[4];
1259
1260 // we will compare thumbnails by :
1261 // 1) width
1262 // 2) height
1263
1264 if (puSortingDirection[4])
1265 {
1266 #ifdef USE_CUSTOM_SORTING_ICON
1267 puHeaderFileThumbnails = tableHeaderDescendingIcon + puHeaderFileThumbnails;
1268 #endif // USE_CUSTOM_SORTING_ICON
1269 std::sort(prFileList.begin(), prFileList.end(),
1270 [](const FileInfos& a, const FileInfos& b) -> bool
1271 {
1272 if (a.fileType != b.fileType) return (a.fileType == 'd'); // directory in first
1273 if (a.thumbnailInfo.textureWidth == b.thumbnailInfo.textureWidth)
1274 return (a.thumbnailInfo.textureHeight < b.thumbnailInfo.textureHeight);
1275 return (a.thumbnailInfo.textureWidth < b.thumbnailInfo.textureWidth);
1276 });
1277 }
1278
1279 else
1280 {
1281 #ifdef USE_CUSTOM_SORTING_ICON
1282 puHeaderFileThumbnails = tableHeaderAscendingIcon + puHeaderFileThumbnails;
1283 #endif // USE_CUSTOM_SORTING_ICON
1284 std::sort(prFileList.begin(), prFileList.end(),
1285 [](const FileInfos& a, const FileInfos& b) -> bool
1286 {
1287 if (a.fileType != b.fileType) return (a.fileType != 'd'); // directory in last
1288 if (a.thumbnailInfo.textureWidth == b.thumbnailInfo.textureWidth)
1289 return (a.thumbnailInfo.textureHeight > b.thumbnailInfo.textureHeight);
1290 return (a.thumbnailInfo.textureWidth > b.thumbnailInfo.textureWidth);
1291 });
1292 }
1293 }
1294 #endif // USE_THUMBNAILS
1295
1296 if (vSortingField != SortingFieldEnum::FIELD_NONE)
1297 {
1298 puSortingField = vSortingField;
1299 }
1300
1301 logV("IGFD: applying filtering on file list");
1302
1303 ApplyFilteringOnFileList(vFileDialogInternal);
1304 }
1305
1306 void IGFD::FileManager::ClearFileLists()
1307 {
1308 prFilteredFileList.clear();
1309 prFileList.clear();
1310 }
1311
1312 std::string IGFD::FileManager::prOptimizeFilenameForSearchOperations(const std::string& vFileNameExt)
1313 {
1314 auto fileNameExt = vFileNameExt;
1315 // convert to lower case
1316 for (char& c : fileNameExt)
1317 c = (char)std::tolower(c);
1318 return fileNameExt;
1319 }
1320
1321 void IGFD::FileManager::AddFile(const FileDialogInternal& vFileDialogInternal, const std::string& vPath, const std::string& vFileName, char vFileType, void* ent)
1322 {
1323 FileInfos infos;
1324
1325 #ifdef _WIN32
1326 struct dirent* dent=(struct dirent*)ent;
1327 #endif
1328
1329 infos.filePath = vPath;
1330 infos.fileNameExt = vFileName;
1331 infos.fileNameExt_optimized = prOptimizeFilenameForSearchOperations(infos.fileNameExt);
1332 infos.fileType = vFileType;
1333
1334 if (infos.fileNameExt.empty() || ((infos.fileNameExt == "." || infos.fileNameExt == "..") && !vFileDialogInternal.puFilterManager.puDLGFilters.empty())) return; // filename empty or filename is the current dir '.' //-V807
1335 if (infos.fileNameExt != ".." && (vFileDialogInternal.puDLGflags & ImGuiFileDialogFlags_DontShowHiddenFiles) && infos.fileNameExt[0] == '.') // dont show hidden files
1336 if (!vFileDialogInternal.puFilterManager.puDLGFilters.empty() || (vFileDialogInternal.puFilterManager.puDLGFilters.empty() && infos.fileNameExt != ".")) // except "." if in directory mode //-V728
1337 return;
1338
1339 if (infos.fileType == 'f' ||
1340 infos.fileType == 'l') // link can have the same extention of a file
1341 {
1342 size_t lpt = infos.fileNameExt.find_last_of('.');
1343 if (lpt != std::string::npos)
1344 {
1345 infos.fileExt = infos.fileNameExt.substr(lpt);
1346 }
1347
1348 for (char& i: infos.fileExt) {
1349 if (i>='A' && i<='Z') i+='a'-'A';
1350 }
1351
1352 if (!vFileDialogInternal.puFilterManager.IsCoveredByFilters(infos.fileExt))
1353 {
1354 return;
1355 }
1356 }
1357
1358 #ifdef _WIN32
1359 SYSTEMTIME systemTime;
1360 SYSTEMTIME localTime;
1361 char timebuf[100];
1362
1363 infos.fileSize=dent->dwin_size;
1364 if (FileTimeToSystemTime(&dent->dwin_mtime,&systemTime)==TRUE) {
1365 if (SystemTimeToTzSpecificLocalTime(NULL,&systemTime,&localTime)==TRUE) {
1366 snprintf(timebuf,99,"%d/%.2d/%.2d %.2d:%.2d",localTime.wYear,localTime.wMonth,localTime.wDay,localTime.wHour,localTime.wMinute);
1367 } else {
1368 snprintf(timebuf,99,"%d/%.2d/%.2d %.2d:%.2d",systemTime.wYear,systemTime.wMonth,systemTime.wDay,systemTime.wHour,systemTime.wMinute);
1369 }
1370 infos.fileModifDate=timebuf;
1371 } else {
1372 infos.fileModifDate="???";
1373 }
1374 #endif
1375
1376 vFileDialogInternal.puFilterManager.prFillFileStyle(infos);
1377
1378 prCompleteFileInfos(infos);
1379 prFileList.push_back(infos);
1380 }
1381
1382 void IGFD::FileManager::ScanDir(const FileDialogInternal& vFileDialogInternal, const std::string& vPath)
1383 {
1384 std::string path = vPath;
1385 logV("IGFD: ScanDir(%s)",vPath);
1386
1387 if (prCurrentPathDecomposition.empty())
1388 {
1389 logV("IGFD: the current path decomposition is empty. setting.");
1390 SetCurrentDir(path);
1391 }
1392
1393 if (!prCurrentPathDecomposition.empty())
1394 {
1395 logV("IGFD: the current path decomposition is not empty. trying.");
1396 #ifdef WIN32
1397 if (path == puFsRoot)
1398 path += std::string(1u, PATH_SEP);
1399 #endif // WIN32
1400
1401 ClearFileLists();
1402
1403 struct dirent** files = nullptr;
1404 int n = scandir(path.c_str(), &files, nullptr, inAlphaSort);
1405 logV("IGFD: %d entries in directory",n);
1406 if (n>0)
1407 {
1408 int i;
1409
1410 for (i = 0; i < n; i++)
1411 {
1412 struct dirent* ent = files[i];
1413 std::string where = path + std::string(PATH_SEP_STR) + std::string(ent->d_name);
1414 char fileType = 'f';
1415 #if defined(HAVE_DIRENT_TYPE) || defined(_WIN32)
1416 if (ent->d_type != DT_UNKNOWN)
1417 {
1418 switch (ent->d_type)
1419 {
1420 case DT_REG:
1421 fileType = 'f'; break;
1422 case DT_DIR:
1423 fileType = 'd'; break;
1424 case DT_LNK: {
1425 #ifdef _WIN32
1426 fileType = 'f';
1427 #else
1428 DIR* dirTest = opendir(where.c_str());
1429 if (dirTest == NULL)
1430 {
1431 if (errno == ENOTDIR)
1432 {
1433 fileType = 'f';
1434 }
1435 else
1436 {
1437 // why does 'l' make it crash?
1438 fileType = 'f';
1439 }
1440 }
1441 else
1442 {
1443 fileType = 'd';
1444 closedir(dirTest);
1445 }
1446 #endif
1447 break;
1448 }
1449 default:
1450 fileType = 'f'; break;
1451 }
1452 }
1453 else
1454 #endif // HAVE_DIRENT_TYPE
1455 {
1456 #ifndef _WIN32
1457 struct stat filestat;
1458 if (stat(where.c_str(), &filestat) == 0)
1459 {
1460 if (S_ISDIR(filestat.st_mode))
1461 {
1462 fileType = 'd';
1463 }
1464 else
1465 {
1466 fileType = 'f';
1467 }
1468 }
1469 #endif
1470 }
1471
1472 auto fileNameExt = ent->d_name;
1473
1474 AddFile(vFileDialogInternal, path, fileNameExt, fileType, ent);
1475 }
1476
1477 for (i = 0; i < n; i++)
1478 {
1479 free(files[i]);
1480 }
1481
1482 free(files);
1483 } else {
1484 logV("IGFD: it's empty");
1485 }
1486
1487 logV("IGFD: sorting fields...");
1488 SortFields(vFileDialogInternal, puSortingField, false);
1489 } else {
1490 logE("IGFD: current path decomposition is empty!");
1491 }
1492
1493 fileListActuallyEmpty=prFileList.empty();
1494 }
1495
1496 bool IGFD::FileManager::GetDrives()
1497 {
1498 auto drives = IGFD::Utils::GetDrivesList();
1499 if (!drives.empty())
1500 {
1501 prCurrentPath.clear();
1502 prCurrentPathDecomposition.clear();
1503 ClearFileLists();
1504 for (auto& drive : drives)
1505 {
1506 FileInfos info;
1507 info.fileNameExt = drive;
1508 info.fileNameExt_optimized = prOptimizeFilenameForSearchOperations(drive);
1509 info.fileType = 'd';
1510
1511 if (!info.fileNameExt.empty())
1512 {
1513 prFileList.push_back(info);
1514 }
1515 }
1516 puShowDrives = true;
1517 return true;
1518 }
1519 return false;
1520 }
1521
1522 bool IGFD::FileManager::IsComposerEmpty()
1523 {
1524 return prCurrentPathDecomposition.empty();
1525 }
1526
1527 size_t IGFD::FileManager::GetComposerSize()
1528 {
1529 return prCurrentPathDecomposition.size();
1530 }
1531
1532 bool IGFD::FileManager::IsFileListEmpty()
1533 {
1534 return prFileList.empty();
1535 }
1536
1537 size_t IGFD::FileManager::GetFullFileListSize()
1538 {
1539 return prFileList.size();
1540 }
1541
1542 const FileInfos& IGFD::FileManager::GetFullFileAt(size_t vIdx)
1543 {
1544 if (vIdx < prFileList.size())
1545 return prFileList[vIdx];
1546 return invalidFile;
1547 }
1548
1549 bool IGFD::FileManager::IsFilteredListEmpty()
1550 {
1551 return prFilteredFileList.empty();
1552 }
1553
1554 size_t IGFD::FileManager::GetFilteredListSize()
1555 {
1556 return prFilteredFileList.size();
1557 }
1558
1559 const FileInfos& IGFD::FileManager::GetFilteredFileAt(size_t vIdx)
1560 {
1561 if (vIdx < prFilteredFileList.size())
1562 return prFilteredFileList[vIdx];
1563 return invalidFile;
1564 }
1565
1566 bool IGFD::FileManager::IsFileNameSelected(const std::string& vFileName)
1567 {
1568 return prSelectedFileNames.find(vFileName) != prSelectedFileNames.end();
1569 }
1570
1571 std::string IGFD::FileManager::GetBack()
1572 {
1573 return prCurrentPathDecomposition.back();
1574 }
1575
1576 void IGFD::FileManager::ClearComposer()
1577 {
1578 prCurrentPathDecomposition.clear();
1579 }
1580
1581 void IGFD::FileManager::ClearAll()
1582 {
1583 ClearComposer();
1584 ClearFileLists();
1585 }
1586
1587 void IGFD::FileManager::ApplyFilteringOnFileList(const FileDialogInternal& vFileDialogInternal)
1588 {
1589 prFilteredFileList.clear();
1590 for (const FileInfos& file : prFileList)
1591 {
1592 bool show = true;
1593 if (!file.IsTagFound(vFileDialogInternal.puSearchManager.puSearchTag)) // if search tag
1594 show = false;
1595 if (puDLGDirectoryMode && file.fileType != 'd') // directory mode
1596 show = false;
1597 if (show)
1598 prFilteredFileList.push_back(file);
1599 }
1600 }
1601
1602 std::string IGFD::FileManager::prRoundNumber(double vvalue, int n)
1603 {
1604 std::stringstream tmp;
1605 tmp << std::setprecision(n) << std::fixed << vvalue;
1606 return tmp.str();
1607 }
1608
1609 std::string IGFD::FileManager::prFormatFileSize(size_t vByteSize)
1610 {
1611 if (vByteSize != 0)
1612 {
1613 static double lo = 1024.0;
1614 static double ko = 1024.0 * 1024.0;
1615 static double mo = 1024.0 * 1024.0 * 1024.0;
1616
1617 auto v = (double)vByteSize;
1618
1619 if (v < lo)
1620 return prRoundNumber(v, 0); // octet
1621 else if (v < ko)
1622 return prRoundNumber(v / lo, 2) + "K"; // ko
1623 else if (v < mo)
1624 return prRoundNumber(v / ko, 2) + "M"; // Mo
1625 else
1626 return prRoundNumber(v / mo, 2) + "G"; // Go
1627 }
1628
1629 return "";
1630 }
1631
1632 void IGFD::FileManager::prCompleteFileInfos(FileInfos& vInfos)
1633 {
1634 if (vInfos.fileNameExt != "." &&
1635 vInfos.fileNameExt != "..")
1636 {
1637 // _stat struct :
1638 //dev_t st_dev; /* ID of device containing file */
1639 //ino_t st_ino; /* inode number */
1640 //mode_t st_mode; /* protection */
1641 //nlink_t st_nlink; /* number of hard links */
1642 //uid_t st_uid; /* user ID of owner */
1643 //gid_t st_gid; /* group ID of owner */
1644 //dev_t st_rdev; /* device ID (if special file) */
1645 //off_t st_size; /* total size, in bytes */
1646 //blksize_t st_blksize; /* blocksize for file system I/O */
1647 //blkcnt_t st_blocks; /* number of 512B blocks allocated */
1648 //time_t st_atime; /* time of last access - not sure out of ntfs */
1649 //time_t st_mtime; /* time of last modification - not sure out of ntfs */
1650 //time_t st_ctime; /* time of last status change - not sure out of ntfs */
1651
1652 #ifdef _WIN32
1653 if (vInfos.fileType != 'd')
1654 {
1655 vInfos.formatedFileSize = prFormatFileSize(vInfos.fileSize);
1656 }
1657 #else
1658 std::string fpn;
1659
1660 if (vInfos.fileType == 'f' || vInfos.fileType == 'l' || vInfos.fileType == 'd') // file
1661 fpn = vInfos.filePath + std::string(1u, PATH_SEP) + vInfos.fileNameExt;
1662
1663 struct stat statInfos = {};
1664 char timebuf[100];
1665 int result = stat(fpn.c_str(), &statInfos);
1666 if (result!=-1)
1667 {
1668 if (vInfos.fileType != 'd')
1669 {
1670 vInfos.fileSize = (size_t)statInfos.st_size;
1671 vInfos.formatedFileSize = prFormatFileSize(vInfos.fileSize);
1672 }
1673
1674 size_t len = 0;
1675 #ifdef MSVC
1676 struct tm _tm;
1677 errno_t err = localtime_s(&_tm, &statInfos.st_mtime);
1678 if (!err) len = strftime(timebuf, 99, FileDialog::Instance()->DateTimeFormat, &_tm);
1679 #else // MSVC
1680 struct tm* _tm = localtime(&statInfos.st_mtime);
1681 if (_tm) len = strftime(timebuf, 99, FileDialog::Instance()->DateTimeFormat, _tm);
1682 #endif // MSVC
1683 if (len)
1684 {
1685 vInfos.fileModifDate = std::string(timebuf, len);
1686 }
1687 } else {
1688 vInfos.fileSize=0;
1689 vInfos.formatedFileSize = prFormatFileSize(vInfos.fileSize);
1690 vInfos.fileModifDate="???";
1691 }
1692 #endif
1693 }
1694 }
1695
1696 void IGFD::FileManager::prRemoveFileNameInSelection(const std::string& vFileName)
1697 {
1698 prSelectedFileNames.erase(vFileName);
1699
1700 if (prSelectedFileNames.size() == 1)
1701 {
1702 snprintf(puFileNameBuffer, MAX_FILE_DIALOG_NAME_BUFFER, "%s", vFileName.c_str());
1703 }
1704 else
1705 {
1706 snprintf(puFileNameBuffer, MAX_FILE_DIALOG_NAME_BUFFER, "%zu files Selected", prSelectedFileNames.size());
1707 }
1708 }
1709
1710 void IGFD::FileManager::prAddFileNameInSelection(const std::string& vFileName, bool vSetLastSelectionFileName)
1711 {
1712 prSelectedFileNames.emplace(vFileName);
1713
1714 if (prSelectedFileNames.size() == 1)
1715 {
1716 snprintf(puFileNameBuffer, MAX_FILE_DIALOG_NAME_BUFFER, "%s", vFileName.c_str());
1717 }
1718 else
1719 {
1720 snprintf(puFileNameBuffer, MAX_FILE_DIALOG_NAME_BUFFER, "%zu files Selected", prSelectedFileNames.size());
1721 }
1722
1723 if (vSetLastSelectionFileName)
1724 prLastSelectedFileName = vFileName;
1725 }
1726
1727 void IGFD::FileManager::SetCurrentDir(const std::string& vPath)
1728 {
1729 std::string path = vPath;
1730 #ifdef WIN32
1731 if (puFsRoot == path)
1732 path += std::string(1u, PATH_SEP);
1733 #endif // WIN32
1734
1735 DIR* dir = opendir(path.c_str());
1736 if (dir == nullptr)
1737 {
1738 path = ".";
1739 dir = opendir(path.c_str());
1740 }
1741
1742 if (dir != nullptr)
1743 {
1744 #ifdef WIN32
1745 DWORD numchar = 0;
1746 // numchar = GetFullPathNameA(path.c_str(), PATH_MAX, real_path, nullptr);
1747 std::wstring wpath = IGFD::Utils::string_to_wstring(path);
1748 numchar = GetFullPathNameW(wpath.c_str(), 0, nullptr, nullptr);
1749 std::wstring fpath(numchar, 0);
1750 GetFullPathNameW(wpath.c_str(), numchar, (wchar_t*)fpath.data(), nullptr);
1751 std::string real_path = IGFD::Utils::wstring_to_string(fpath);
1752 if (real_path.back() == '\0') // for fix issue we can have with std::string concatenation.. if there is a \0 at end
1753 real_path = real_path.substr(0, real_path.size() - 1U);
1754 if (!real_path.empty())
1755 #elif defined(UNIX) // UNIX is LINUX or APPLE
1756 char real_path[PATH_MAX];
1757 char* numchar = realpath(path.c_str(), real_path);
1758 if (numchar != nullptr)
1759 #endif // WIN32
1760 {
1761 prCurrentPath = std::move(real_path);
1762 if (prCurrentPath[prCurrentPath.size() - 1] == PATH_SEP)
1763 {
1764 prCurrentPath = prCurrentPath.substr(0, prCurrentPath.size() - 1);
1765 }
1766 IGFD::Utils::SetBuffer(puInputPathBuffer, MAX_PATH_BUFFER_SIZE, prCurrentPath);
1767 prCurrentPathDecomposition = IGFD::Utils::SplitStringToVector(prCurrentPath, PATH_SEP, false);
1768 #ifdef UNIX // UNIX is LINUX or APPLE
1769 prCurrentPathDecomposition.insert(prCurrentPathDecomposition.begin(), std::string(1u, PATH_SEP));
1770 #endif // UNIX
1771 if (!prCurrentPathDecomposition.empty())
1772 {
1773 #ifdef WIN32
1774 puFsRoot = prCurrentPathDecomposition[0];
1775 #endif // WIN32
1776 }
1777 }
1778 closedir(dir);
1779 }
1780 }
1781
1782 bool IGFD::FileManager::CreateDir(const std::string& vPath)
1783 {
1784 bool res = false;
1785
1786 if (!vPath.empty())
1787 {
1788 std::string path = prCurrentPath + std::string(1u, PATH_SEP) + vPath;
1789
1790 res = IGFD::Utils::CreateDirectoryIfNotExist(path);
1791 }
1792
1793 return res;
1794 }
1795
1796 void IGFD::FileManager::ComposeNewPath(std::vector<std::string>::iterator vIter)
1797 {
1798 std::string res;
1799
1800 while (true)
1801 {
1802 if (!res.empty())
1803 {
1804 #ifdef WIN32
1805 res = *vIter + std::string(1u, PATH_SEP) + res;
1806 #elif defined(UNIX) // UNIX is LINUX or APPLE
1807 if (*vIter == puFsRoot)
1808 res = *vIter + res;
1809 else
1810 res = *vIter + PATH_SEP + res;
1811 #endif // WIN32
1812 }
1813 else
1814 res = *vIter;
1815
1816 if (vIter == prCurrentPathDecomposition.begin())
1817 {
1818 #if defined(UNIX) // UNIX is LINUX or APPLE
1819 if (res[0] != PATH_SEP)
1820 res = PATH_SEP + res;
1821 #endif // defined(UNIX)
1822 break;
1823 }
1824
1825 --vIter;
1826 }
1827
1828 prCurrentPath = std::move(res);
1829 }
1830
1831 bool IGFD::FileManager::SetPathOnParentDirectoryIfAny()
1832 {
1833 if (prCurrentPathDecomposition.size() > 1)
1834 {
1835 ComposeNewPath(prCurrentPathDecomposition.end() - 2);
1836 return true;
1837 }
1838 return false;
1839 }
1840
1841 std::string IGFD::FileManager::GetCurrentPath()
1842 {
1843 if (prCurrentPath.empty())
1844 prCurrentPath = ".";
1845 return prCurrentPath;
1846 }
1847
1848 void IGFD::FileManager::SetCurrentPath(const std::string& vCurrentPath)
1849 {
1850 if (vCurrentPath.empty())
1851 prCurrentPath = ".";
1852 else
1853 prCurrentPath = vCurrentPath;
1854 }
1855
1856 bool IGFD::FileManager::IsFileExist(const std::string& vFile, const std::string& vFileExt)
1857 {
1858 std::ifstream docFile(vFile, std::ios::in);
1859 logV("IGFD: IsFileExist(%s)",vFile);
1860 if (docFile.is_open())
1861 {
1862 docFile.close();
1863 return true;
1864 }
1865
1866 std::ifstream docFileE(vFile+vFileExt, std::ios::in);
1867 logV("IGFD: IsFileExist(%s)",vFile+vFileExt);
1868 if (docFileE.is_open())
1869 {
1870 docFileE.close();
1871 return true;
1872 }
1873 return false;
1874 }
1875
1876 void IGFD::FileManager::SetDefaultFileName(const std::string& vFileName)
1877 {
1878 puDLGDefaultFileName = vFileName;
1879 IGFD::Utils::SetBuffer(puFileNameBuffer, MAX_FILE_DIALOG_NAME_BUFFER, vFileName);
1880 }
1881
1882 bool IGFD::FileManager::SelectDirectory(const FileInfos& vInfos)
1883 {
1884 bool pathClick = false;
1885
1886 if (vInfos.fileNameExt == "..")
1887 {
1888 pathClick = SetPathOnParentDirectoryIfAny();
1889 }
1890 else
1891 {
1892 std::string newPath;
1893
1894 if (puShowDrives)
1895 {
1896 newPath = vInfos.fileNameExt + std::string(1u, PATH_SEP);
1897 }
1898 else
1899 {
1900 #ifdef __linux__
1901 if (puFsRoot == prCurrentPath)
1902 newPath = prCurrentPath + vInfos.fileNameExt;
1903 else
1904 #endif // __linux__
1905 newPath = prCurrentPath + std::string(1u, PATH_SEP) + vInfos.fileNameExt;
1906 }
1907
1908 if (IGFD::Utils::IsDirectoryExist(newPath))
1909 {
1910 if (puShowDrives)
1911 {
1912 prCurrentPath = vInfos.fileNameExt;
1913 puFsRoot = prCurrentPath;
1914 }
1915 else
1916 {
1917 prCurrentPath = newPath; //-V820
1918 }
1919 pathClick = true;
1920 }
1921 }
1922
1923 return pathClick;
1924 }
1925
1926 void IGFD::FileManager::SelectFileName(const FileDialogInternal& vFileDialogInternal, const FileInfos& vInfos)
1927 {
1928 if (ImGui::GetIO().KeyCtrl)
1929 {
1930 if (puDLGcountSelectionMax == 0) // infinite selection
1931 {
1932 if (prSelectedFileNames.find(vInfos.fileNameExt) == prSelectedFileNames.end()) // not found +> add
1933 {
1934 prAddFileNameInSelection(vInfos.fileNameExt, true);
1935 }
1936 else // found +> remove
1937 {
1938 prRemoveFileNameInSelection(vInfos.fileNameExt);
1939 }
1940 }
1941 else // selection limited by size
1942 {
1943 if (prSelectedFileNames.size() < puDLGcountSelectionMax)
1944 {
1945 if (prSelectedFileNames.find(vInfos.fileNameExt) == prSelectedFileNames.end()) // not found +> add
1946 {
1947 prAddFileNameInSelection(vInfos.fileNameExt, true);
1948 }
1949 else // found +> remove
1950 {
1951 prRemoveFileNameInSelection(vInfos.fileNameExt);
1952 }
1953 }
1954 }
1955 }
1956 else if (ImGui::GetIO().KeyShift)
1957 {
1958 if (puDLGcountSelectionMax != 1)
1959 {
1960 prSelectedFileNames.clear();
1961 // we will iterate filelist and get the last selection after the start selection
1962 bool startMultiSelection = false;
1963 std::string fileNameToSelect = vInfos.fileNameExt;
1964 std::string savedLastSelectedFileName; // for invert selection mode
1965 for (const FileInfos& file : prFileList)
1966 {
1967 bool canTake = true;
1968 if (!file.IsTagFound(vFileDialogInternal.puSearchManager.puSearchTag)) canTake = false;
1969 if (canTake) // if not filtered, we will take files who are filtered by the dialog
1970 {
1971 if (file.fileNameExt == prLastSelectedFileName)
1972 {
1973 startMultiSelection = true;
1974 prAddFileNameInSelection(prLastSelectedFileName, false);
1975 }
1976 else if (startMultiSelection)
1977 {
1978 if (puDLGcountSelectionMax == 0) // infinite selection
1979 {
1980 prAddFileNameInSelection(file.fileNameExt, false);
1981 }
1982 else // selection limited by size
1983 {
1984 if (prSelectedFileNames.size() < puDLGcountSelectionMax)
1985 {
1986 prAddFileNameInSelection(file.fileNameExt, false);
1987 }
1988 else
1989 {
1990 startMultiSelection = false;
1991 if (!savedLastSelectedFileName.empty())
1992 prLastSelectedFileName = savedLastSelectedFileName;
1993 break;
1994 }
1995 }
1996 }
1997
1998 if (file.fileNameExt == fileNameToSelect)
1999 {
2000 if (!startMultiSelection) // we are before the last Selected FileName, so we must inverse
2001 {
2002 savedLastSelectedFileName = prLastSelectedFileName;
2003 prLastSelectedFileName = fileNameToSelect;
2004 fileNameToSelect = savedLastSelectedFileName;
2005 startMultiSelection = true;
2006 prAddFileNameInSelection(prLastSelectedFileName, false);
2007 }
2008 else
2009 {
2010 startMultiSelection = false;
2011 if (!savedLastSelectedFileName.empty())
2012 prLastSelectedFileName = savedLastSelectedFileName;
2013 break;
2014 }
2015 }
2016 }
2017 }
2018 }
2019 }
2020 else
2021 {
2022 prSelectedFileNames.clear();
2023 IGFD::Utils::ResetBuffer(puFileNameBuffer);
2024 prAddFileNameInSelection(vInfos.fileNameExt, true);
2025 }
2026 }
2027
2028 void IGFD::FileManager::DrawDirectoryCreation(const FileDialogInternal& vFileDialogInternal)
2029 {
2030 if (vFileDialogInternal.puDLGflags & ImGuiFileDialogFlags_DisableCreateDirectoryButton)
2031 return;
2032
2033 if (IMGUI_BUTTON(createDirButtonString))
2034 {
2035 if (!prCreateDirectoryMode)
2036 {
2037 prCreateDirectoryMode = true;
2038 IGFD::Utils::ResetBuffer(puDirectoryNameBuffer);
2039 }
2040 }
2041 if (ImGui::IsItemHovered())
2042 ImGui::SetTooltip("%s",FileDialog::Instance()->buttonCreateDirString);
2043
2044 if (prCreateDirectoryMode)
2045 {
2046 ImGui::SameLine();
2047
2048 ImGui::PushItemWidth(100.0f);
2049 ImGui::InputText("##DirectoryFileName", puDirectoryNameBuffer, MAX_FILE_DIALOG_NAME_BUFFER);
2050 ImGui::PopItemWidth();
2051
2052 ImGui::SameLine();
2053
2054 if (IMGUI_BUTTON(FileDialog::Instance()->okButtonString))
2055 {
2056 std::string newDir = std::string(puDirectoryNameBuffer);
2057 if (CreateDir(newDir))
2058 {
2059 SetCurrentPath(prCurrentPath + std::string(1u, PATH_SEP) + newDir);
2060 OpenCurrentPath(vFileDialogInternal);
2061 }
2062
2063 prCreateDirectoryMode = false;
2064 }
2065
2066 ImGui::SameLine();
2067
2068 if (IMGUI_BUTTON(FileDialog::Instance()->cancelButtonString))
2069 {
2070 prCreateDirectoryMode = false;
2071 }
2072 }
2073 }
2074
2075 void IGFD::FileManager::DrawPathComposer(const FileDialogInternal& vFileDialogInternal)
2076 {
2077 if (IMGUI_BUTTON(homeButtonString))
2078 {
2079 SetCurrentPath(FileDialog::Instance()->homePath);
2080 OpenCurrentPath(vFileDialogInternal);
2081 }
2082 if (ImGui::IsItemHovered())
2083 ImGui::SetTooltip("%s",FileDialog::Instance()->buttonResetPathString);
2084
2085 ImGui::SameLine();
2086 if (IMGUI_BUTTON(parentDirString))
2087 {
2088 if (SetPathOnParentDirectoryIfAny()) {
2089 OpenCurrentPath(vFileDialogInternal);
2090 }
2091 }
2092 if (ImGui::IsItemHovered())
2093 ImGui::SetTooltip("%s",FileDialog::Instance()->buttonParentDirString);
2094
2095 #ifdef WIN32
2096 ImGui::SameLine();
2097
2098 if (IMGUI_BUTTON(drivesButtonString))
2099 {
2100 puDrivesClicked = true;
2101 }
2102 if (ImGui::IsItemHovered())
2103 ImGui::SetTooltip("%s",FileDialog::Instance()->buttonDriveString);
2104 #endif // WIN32
2105
2106 ImGui::SameLine();
2107
2108 if (IMGUI_BUTTON(editPathButtonString))
2109 {
2110 puInputPathActivated = true;
2111 }
2112 if (ImGui::IsItemHovered())
2113 ImGui::SetTooltip("%s",FileDialog::Instance()->buttonEditPathString);
2114
2115 ImGui::SameLine();
2116
2117 ImGui::SeparatorEx(ImGuiSeparatorFlags_Vertical);
2118
2119 // show current path
2120 if (!prCurrentPathDecomposition.empty())
2121 {
2122 ImGui::SameLine();
2123
2124 if (puInputPathActivated)
2125 {
2126 ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x);
2127 ImGui::InputText("##pathedition", puInputPathBuffer, MAX_PATH_BUFFER_SIZE);
2128 ImGui::PopItemWidth();
2129 }
2130 else
2131 {
2132 int _id = 0;
2133 for (auto itPathDecomp = prCurrentPathDecomposition.begin();
2134 itPathDecomp != prCurrentPathDecomposition.end(); ++itPathDecomp)
2135 {
2136 if (itPathDecomp != prCurrentPathDecomposition.begin())
2137 ImGui::SameLine();
2138 ImGui::PushID(_id++);
2139 bool click = ImGui::ButtonEx((*itPathDecomp).c_str(),ImVec2(0,0),ImGuiButtonFlags_NoHashTextHide);
2140 ImGui::PopID();
2141 if (click)
2142 {
2143 ComposeNewPath(itPathDecomp);
2144 puPathClicked = true;
2145 break;
2146 }
2147 // activate input for path
2148 if (ImGui::IsItemClicked(ImGuiMouseButton_Right))
2149 {
2150 ComposeNewPath(itPathDecomp);
2151 IGFD::Utils::SetBuffer(puInputPathBuffer, MAX_PATH_BUFFER_SIZE, prCurrentPath);
2152 puInputPathActivated = true;
2153 break;
2154 }
2155 }
2156 }
2157 }
2158 }
2159
2160 std::string IGFD::FileManager::GetResultingPath()
2161 {
2162 std::string path = prCurrentPath;
2163
2164 if (puDLGDirectoryMode) // if directory mode
2165 {
2166 std::string selectedDirectory = puFileNameBuffer;
2167 if (!selectedDirectory.empty() &&
2168 selectedDirectory != ".")
2169 path += std::string(1u, PATH_SEP) + selectedDirectory;
2170 }
2171
2172 return path;
2173 }
2174
2175 std::string IGFD::FileManager::GetResultingFileName(FileDialogInternal& vFileDialogInternal)
2176 {
2177 if (!puDLGDirectoryMode) // if not directory mode
2178 {
2179 return puFileNameBuffer; //vFileDialogInternal.puFilterManager.ReplaceExtentionWithCurrentFilter(std::string(puFileNameBuffer));
2180 }
2181
2182 return ""; // directory mode
2183 }
2184
2185 std::string IGFD::FileManager::GetResultingFilePathName(FileDialogInternal& vFileDialogInternal)
2186 {
2187 std::string result = GetResultingPath();
2188
2189 std::string filename = GetResultingFileName(vFileDialogInternal);
2190 if (!filename.empty())
2191 {
2192 #ifdef UNIX
2193 if (puFsRoot != result)
2194 #endif // UNIX
2195 result += std::string(1u, PATH_SEP);
2196
2197 result += filename;
2198 }
2199
2200 return result;
2201 }
2202
2203 std::map<std::string, std::string> IGFD::FileManager::GetResultingSelection()
2204 {
2205 std::map<std::string, std::string> res;
2206
2207 for (auto& selectedFileName : prSelectedFileNames)
2208 {
2209 std::string result = GetResultingPath();
2210
2211 #ifdef UNIX
2212 if (puFsRoot != result)
2213 #endif // UNIX
2214 result += std::string(1u, PATH_SEP);
2215
2216 result += selectedFileName;
2217
2218 res[selectedFileName] = result;
2219 }
2220
2221 return res;
2222 }
2223
2224 /////////////////////////////////////////////////////////////////////////////////////
2225 //// FILE DIALOG INTERNAL ///////////////////////////////////////////////////////////
2226 /////////////////////////////////////////////////////////////////////////////////////
2227
2228 void IGFD::FileDialogInternal::NewFrame()
2229 {
2230 puCanWeContinue = true; // reset flag for possibily validate the dialog
2231 puIsOk = false; // reset dialog result
2232 puFileManager.puDrivesClicked = false;
2233 puFileManager.puPathClicked = false;
2234
2235 puNeedToExitDialog = false;
2236
2237 #ifdef USE_DIALOG_EXIT_WITH_KEY
2238 if (ImGui::IsKeyPressed(IGFD_EXIT_KEY))
2239 {
2240 // we do that here with the data's defined at the last frame
2241 // because escape key can quit input activation and at the end of the frame all flag will be false
2242 // so we will detect nothing
2243 if (!(puFileManager.puInputPathActivated ||
2244 puSearchManager.puSearchInputIsActive ||
2245 puFileInputIsActive ||
2246 puFileListViewIsActive))
2247 {
2248 puNeedToExitDialog = true; // need to quit dialog
2249 }
2250 }
2251 else
2252 #endif
2253 {
2254 puSearchManager.puSearchInputIsActive = false;
2255 puFileInputIsActive = false;
2256 puFileListViewIsActive = false;
2257 }
2258 }
2259
2260 void IGFD::FileDialogInternal::EndFrame()
2261 {
2262 // directory change
2263 if (puFileManager.puPathClicked)
2264 {
2265 puFileManager.OpenCurrentPath(*this);
2266 }
2267
2268 if (puFileManager.puDrivesClicked)
2269 {
2270 if (puFileManager.GetDrives())
2271 {
2272 puFileManager.ApplyFilteringOnFileList(*this);
2273 }
2274 }
2275
2276 if (puFileManager.puInputPathActivated)
2277 {
2278 auto gio = ImGui::GetIO();
2279 if (ImGui::IsKeyReleased(ImGuiKey_Enter))
2280 {
2281 puFileManager.SetCurrentPath(std::string(puFileManager.puInputPathBuffer));
2282 puFileManager.OpenCurrentPath(*this);
2283 puFileManager.puInputPathActivated = false;
2284 }
2285 if (ImGui::IsKeyReleased(ImGuiKey_Escape))
2286 {
2287 puFileManager.puInputPathActivated = false;
2288 }
2289 }
2290 }
2291
2292 void IGFD::FileDialogInternal::ResetForNewDialog()
2293 {
2294 puFileManager.fileListActuallyEmpty=false;
2295 }
2296
2297 /////////////////////////////////////////////////////////////////////////////////////
2298 //// THUMBNAIL FEATURE //////////////////////////////////////////////////////////////
2299 /////////////////////////////////////////////////////////////////////////////////////
2300
2301 IGFD::ThumbnailFeature::ThumbnailFeature()
2302 {
2303 #ifdef USE_THUMBNAILS
2304 prDisplayMode = DisplayModeEnum::FILE_LIST;
2305 #endif
2306 }
2307
2308 IGFD::ThumbnailFeature::~ThumbnailFeature() = default;
2309
2310 void IGFD::ThumbnailFeature::NewThumbnailFrame(FileDialogInternal& vFileDialogInternal)
2311 {
2312 (void)vFileDialogInternal;
2313
2314 #ifdef USE_THUMBNAILS
2315 prStartThumbnailFileDatasExtraction();
2316 #endif
2317 }
2318
2319 void IGFD::ThumbnailFeature::EndThumbnailFrame(FileDialogInternal& vFileDialogInternal)
2320 {
2321 #ifdef USE_THUMBNAILS
2322 prClearThumbnails(vFileDialogInternal);
2323 #endif
2324 }
2325
2326 void IGFD::ThumbnailFeature::QuitThumbnailFrame(FileDialogInternal& vFileDialogInternal)
2327 {
2328 #ifdef USE_THUMBNAILS
2329 prStopThumbnailFileDatasExtraction();
2330 prClearThumbnails(vFileDialogInternal);
2331 #endif
2332 }
2333
2334 #ifdef USE_THUMBNAILS
2335 void IGFD::ThumbnailFeature::prStartThumbnailFileDatasExtraction()
2336 {
2337 const bool res = prThumbnailGenerationThread.use_count() && prThumbnailGenerationThread->joinable();
2338 if (!res)
2339 {
2340 prIsWorking = true;
2341 prCountFiles = 0U;
2342 prThumbnailGenerationThread = std::shared_ptr<std::thread>(
2343 new std::thread(&IGFD::ThumbnailFeature::prThreadThumbnailFileDatasExtractionFunc, this),
2344 [this](std::thread* obj)
2345 {
2346 prIsWorking = false;
2347 if (obj)
2348 obj->join();
2349 });
2350 }
2351 }
2352
2353 bool IGFD::ThumbnailFeature::prStopThumbnailFileDatasExtraction()
2354 {
2355 const bool res = prThumbnailGenerationThread.use_count() && prThumbnailGenerationThread->joinable();
2356 if (res)
2357 {
2358 prThumbnailGenerationThread.reset();
2359 }
2360
2361 return res;
2362 }
2363
2364 void IGFD::ThumbnailFeature::prThreadThumbnailFileDatasExtractionFunc()
2365 {
2366 prCountFiles = 0U;
2367 prIsWorking = true;
2368
2369 // infinite loop while is thread working
2370 while(prIsWorking)
2371 {
2372 if (!prThumbnailFileDatasToGet.empty())
2373 {
2374 std::shared_ptr<FileInfos> file = nullptr;
2375 prThumbnailFileDatasToGetMutex.lock();
2376 //get the first file in the list
2377 file = (*prThumbnailFileDatasToGet.begin());
2378 prThumbnailFileDatasToGetMutex.unlock();
2379
2380 // retrieve datas of the texture file if its an image file
2381 if (file.use_count())
2382 {
2383 if (file->fileType == 'f') //-V522
2384 {
2385 if (file->fileExt == ".png"
2386 || file->fileExt == ".bmp"
2387 || file->fileExt == ".tga"
2388 || file->fileExt == ".jpg" || file->fileExt == ".jpeg"
2389 || file->fileExt == ".gif"
2390 || file->fileExt == ".psd"
2391 || file->fileExt == ".pic"
2392 || file->fileExt == ".ppm" || file->fileExt == ".pgm"
2393 //|| file->fileExt == ".hdr" => format float so in few times
2394 )
2395 {
2396 auto fpn = file->filePath + std::string(1u, PATH_SEP) + file->fileNameExt;
2397
2398 int w = 0;
2399 int h = 0;
2400 int chans = 0;
2401 uint8_t *datas = stbi_load(fpn.c_str(), &w, &h, &chans, STBI_rgb_alpha);
2402 if (datas)
2403 {
2404 if (w && h)
2405 {
2406 // resize with respect to glyph ratio
2407 const float ratioX = (float)w / (float)h;
2408 const float newX = DisplayMode_ThumbailsList_ImageHeight * ratioX;
2409 float newY = w / ratioX;
2410 if (newX < w)
2411 newY = DisplayMode_ThumbailsList_ImageHeight;
2412
2413 const auto newWidth = (int)newX;
2414 const auto newHeight = (int)newY;
2415 const auto newBufSize = (size_t)(newWidth * newHeight * 4U); //-V112 //-V1028
2416 auto resizedData = new uint8_t[newBufSize];
2417
2418 const int resizeSucceeded = stbir_resize_uint8(
2419 datas, w, h, 0,
2420 resizedData, newWidth, newHeight, 0,
2421 4); //-V112
2422
2423 if (resizeSucceeded)
2424 {
2425 auto th = &file->thumbnailInfo;
2426
2427 th->textureFileDatas = resizedData;
2428 th->textureWidth = newWidth;
2429 th->textureHeight = newHeight;
2430 th->textureChannels = 4; //-V112
2431
2432 // we set that at least, because will launch the gpu creation of the texture in the main thread
2433 th->isReadyToUpload = true;
2434
2435 // need gpu loading
2436 prAddThumbnailToCreate(file);
2437 }
2438 }
2439 else
2440 {
2441 printf("image loading fail : w:%i h:%i c:%i\n", w, h, 4); //-V112
2442 }
2443
2444 stbi_image_free(datas);
2445 }
2446 }
2447 }
2448
2449 // peu importe le resultat on vire le fichicer
2450 // remove form this list
2451 // write => thread concurency issues
2452 prThumbnailFileDatasToGetMutex.lock();
2453 prThumbnailFileDatasToGet.pop_front();
2454 prThumbnailFileDatasToGetMutex.unlock();
2455 }
2456 }
2457 }
2458 }
2459
2460 inline void inVariadicProgressBar(float fraction, const ImVec2& size_arg, const char* fmt, ...)
2461 {
2462 va_list args;
2463 va_start(args, fmt);
2464 char TempBuffer[512];
2465 const int w = vsnprintf(TempBuffer, 511, fmt, args);
2466 va_end(args);
2467 if (w)
2468 {
2469 ImGui::ProgressBar(fraction, size_arg, TempBuffer);
2470 }
2471 }
2472
2473 void IGFD::ThumbnailFeature::prDrawThumbnailGenerationProgress()
2474 {
2475 if (prThumbnailGenerationThread.use_count() && prThumbnailGenerationThread->joinable())
2476 {
2477 if (!prThumbnailFileDatasToGet.empty())
2478 {
2479 const auto p = (float)((double)prCountFiles / (double)prThumbnailFileDatasToGet.size()); // read => no thread concurency issues
2480 inVariadicProgressBar(p, ImVec2(50, 0), "%u/%u", prCountFiles, (uint32_t)prThumbnailFileDatasToGet.size()); // read => no thread concurency issues
2481 ImGui::SameLine();
2482 }
2483 }
2484 }
2485
2486 void IGFD::ThumbnailFeature::prAddThumbnailToLoad(const std::shared_ptr<FileInfos>& vFileInfos)
2487 {
2488 if (vFileInfos.use_count())
2489 {
2490 if (vFileInfos->fileType == 'f')
2491 {
2492 if (vFileInfos->fileExt == ".png"
2493 || vFileInfos->fileExt == ".bmp"
2494 || vFileInfos->fileExt == ".tga"
2495 || vFileInfos->fileExt == ".jpg" || vFileInfos->fileExt == ".jpeg"
2496 || vFileInfos->fileExt == ".gif"
2497 || vFileInfos->fileExt == ".psd"
2498 || vFileInfos->fileExt == ".pic"
2499 || vFileInfos->fileExt == ".ppm" || vFileInfos->fileExt == ".pgm"
2500 //|| file->fileExt == ".hdr" => format float so in few times
2501 )
2502 {
2503 // write => thread concurency issues
2504 prThumbnailFileDatasToGetMutex.lock();
2505 prThumbnailFileDatasToGet.push_back(vFileInfos);
2506 vFileInfos->thumbnailInfo.isLoadingOrLoaded = true;
2507 prThumbnailFileDatasToGetMutex.unlock();
2508 }
2509 }
2510 }
2511 }
2512
2513 void IGFD::ThumbnailFeature::prAddThumbnailToCreate(const std::shared_ptr<FileInfos>& vFileInfos)
2514 {
2515 if (vFileInfos.use_count())
2516 {
2517 // write => thread concurency issues
2518 prThumbnailToCreateMutex.lock();
2519 prThumbnailToCreate.push_back(vFileInfos);
2520 prThumbnailToCreateMutex.unlock();
2521 }
2522 }
2523
2524 void IGFD::ThumbnailFeature::prAddThumbnailToDestroy(const IGFD_Thumbnail_Info& vIGFD_Thumbnail_Info)
2525 {
2526 // write => thread concurency issues
2527 prThumbnailToDestroyMutex.lock();
2528 prThumbnailToDestroy.push_back(vIGFD_Thumbnail_Info);
2529 prThumbnailToDestroyMutex.unlock();
2530 }
2531
2532 void IGFD::ThumbnailFeature::prDrawDisplayModeToolBar()
2533 {
2534 if (IMGUI_RADIO_BUTTON(DisplayMode_FilesList_ButtonString,
2535 prDisplayMode == DisplayModeEnum::FILE_LIST))
2536 prDisplayMode = DisplayModeEnum::FILE_LIST;
2537 if (ImGui::IsItemHovered()) ImGui::SetTooltip(DisplayMode_FilesList_ButtonHelp);
2538 ImGui::SameLine();
2539 if (IMGUI_RADIO_BUTTON(DisplayMode_ThumbailsList_ButtonString,
2540 prDisplayMode == DisplayModeEnum::THUMBNAILS_LIST))
2541 prDisplayMode = DisplayModeEnum::THUMBNAILS_LIST;
2542 if (ImGui::IsItemHovered()) ImGui::SetTooltip(DisplayMode_ThumbailsList_ButtonHelp);
2543 ImGui::SameLine();
2544 /* todo
2545 if (IMGUI_RADIO_BUTTON(DisplayMode_ThumbailsGrid_ButtonString,
2546 prDisplayMode == DisplayModeEnum::THUMBNAILS_GRID))
2547 prDisplayMode = DisplayModeEnum::THUMBNAILS_GRID;
2548 if (ImGui::IsItemHovered()) ImGui::SetTooltip(DisplayMode_ThumbailsGrid_ButtonHelp);
2549 ImGui::SameLine();
2550 */
2551 prDrawThumbnailGenerationProgress();
2552 }
2553
2554 void IGFD::ThumbnailFeature::prClearThumbnails(FileDialogInternal& vFileDialogInternal)
2555 {
2556 // directory wil be changed so the file list will be erased
2557 if (vFileDialogInternal.puFileManager.puPathClicked)
2558 {
2559 size_t count = vFileDialogInternal.puFileManager.GetFullFileListSize();
2560 for (size_t idx = 0U; idx < count; idx++)
2561 {
2562 auto file = vFileDialogInternal.puFileManager.GetFullFileAt(idx);
2563 if (file.use_count())
2564 {
2565 if (file->thumbnailInfo.isReadyToDisplay) //-V522
2566 {
2567 prAddThumbnailToDestroy(file->thumbnailInfo);
2568 }
2569 }
2570 }
2571 }
2572 }
2573
2574 void IGFD::ThumbnailFeature::SetCreateThumbnailCallback(const CreateThumbnailFun& vCreateThumbnailFun)
2575 {
2576 prCreateThumbnailFun = vCreateThumbnailFun;
2577 }
2578
2579 void IGFD::ThumbnailFeature::SetDestroyThumbnailCallback(const DestroyThumbnailFun& vCreateThumbnailFun)
2580 {
2581 prDestroyThumbnailFun = vCreateThumbnailFun;
2582 }
2583
2584 void IGFD::ThumbnailFeature::ManageGPUThumbnails()
2585 {
2586 if (prCreateThumbnailFun)
2587 {
2588 if (!prThumbnailToCreate.empty())
2589 {
2590 for (const auto& file : prThumbnailToCreate)
2591 {
2592 if (file.use_count())
2593 {
2594 prCreateThumbnailFun(&file->thumbnailInfo);
2595 }
2596 }
2597 prThumbnailToCreateMutex.lock();
2598 prThumbnailToCreate.clear();
2599 prThumbnailToCreateMutex.unlock();
2600 }
2601 }
2602 else
2603 {
2604 printf("No Callback found for create texture\nYou need to define the callback with a call to SetCreateThumbnailCallback\n");
2605 }
2606
2607 if (prDestroyThumbnailFun)
2608 {
2609 if (!prThumbnailToDestroy.empty())
2610 {
2611 for (auto thumbnail : prThumbnailToDestroy)
2612 {
2613 prDestroyThumbnailFun(&thumbnail);
2614 }
2615 prThumbnailToDestroyMutex.lock();
2616 prThumbnailToDestroy.clear();
2617 prThumbnailToDestroyMutex.unlock();
2618 }
2619 }
2620 else
2621 {
2622 printf("No Callback found for destroy texture\nYou need to define the callback with a call to SetCreateThumbnailCallback\n");
2623 }
2624 }
2625
2626 #endif // USE_THUMBNAILS
2627
2628 /////////////////////////////////////////////////////////////////////////////////////
2629 //// BOOKMARK FEATURE ///////////////////////////////////////////////////////////////
2630 /////////////////////////////////////////////////////////////////////////////////////
2631
2632 IGFD::BookMarkFeature::BookMarkFeature()
2633 {
2634 #ifdef USE_BOOKMARK
2635 prBookmarkWidth = defaultBookmarkPaneWith;
2636 #endif // USE_BOOKMARK
2637 }
2638
2639 #ifdef USE_BOOKMARK
2640 void IGFD::BookMarkFeature::prDrawBookmarkButton()
2641 {
2642 IMGUI_TOGGLE_BUTTON(bookmarksButtonString, &prBookmarkPaneShown);
2643
2644 if (ImGui::IsItemHovered())
2645 ImGui::SetTooltip(bookmarksButtonHelpString);
2646 }
2647 bool IGFD::BookMarkFeature::prDrawBookmarkPane(FileDialogInternal& vFileDialogInternal, const ImVec2& vSize)
2648 {
2649 bool res = false;
2650
2651 ImGui::BeginChild("##bookmarkpane", vSize);
2652
2653 static int selectedBookmarkForEdition = -1;
2654
2655 if (IMGUI_BUTTON(addBookmarkButtonString "##ImGuiFileDialogAddBookmark"))
2656 {
2657 if (!vFileDialogInternal.puFileManager.IsComposerEmpty())
2658 {
2659 BookmarkStruct bookmark;
2660 bookmark.name = vFileDialogInternal.puFileManager.GetBack();
2661 bookmark.path = vFileDialogInternal.puFileManager.GetCurrentPath();
2662 prBookmarks.push_back(bookmark);
2663 }
2664 }
2665 if (selectedBookmarkForEdition >= 0 &&
2666 selectedBookmarkForEdition < (int)prBookmarks.size())
2667 {
2668 ImGui::SameLine();
2669 if (IMGUI_BUTTON(removeBookmarkButtonString "##ImGuiFileDialogAddBookmark"))
2670 {
2671 prBookmarks.erase(prBookmarks.begin() + selectedBookmarkForEdition);
2672 if (selectedBookmarkForEdition == (int)prBookmarks.size())
2673 selectedBookmarkForEdition--;
2674 }
2675
2676 if (selectedBookmarkForEdition >= 0 &&
2677 selectedBookmarkForEdition < (int)prBookmarks.size())
2678 {
2679 ImGui::SameLine();
2680
2681 ImGui::PushItemWidth(vSize.x - ImGui::GetCursorPosX());
2682 if (ImGui::InputText("##ImGuiFileDialogBookmarkEdit", prBookmarkEditBuffer, MAX_FILE_DIALOG_NAME_BUFFER))
2683 {
2684 prBookmarks[(size_t)selectedBookmarkForEdition].name = std::string(prBookmarkEditBuffer);
2685 }
2686 ImGui::PopItemWidth();
2687 }
2688 }
2689
2690 ImGui::Separator();
2691
2692 if (!prBookmarks.empty())
2693 {
2694 prBookmarkClipper.Begin((int)prBookmarks.size(), ImGui::GetTextLineHeightWithSpacing());
2695 while (prBookmarkClipper.Step())
2696 {
2697 for (int i = prBookmarkClipper.DisplayStart; i < prBookmarkClipper.DisplayEnd; i++)
2698 {
2699 if (i < 0) continue;
2700 const BookmarkStruct& bookmark = prBookmarks[(size_t)i];
2701 ImGui::PushID(i);
2702 if (ImGui::Selectable(bookmark.name.c_str(), selectedBookmarkForEdition == i,
2703 ImGuiSelectableFlags_AllowDoubleClick) |
2704 (selectedBookmarkForEdition == -1 &&
2705 bookmark.path == vFileDialogInternal.puFileManager.GetCurrentPath())) // select if path is current
2706 {
2707 selectedBookmarkForEdition = i;
2708 IGFD::Utils::ResetBuffer(prBookmarkEditBuffer);
2709 IGFD::Utils::AppendToBuffer(prBookmarkEditBuffer, MAX_FILE_DIALOG_NAME_BUFFER, bookmark.name);
2710
2711 if (ImGui::IsMouseDoubleClicked(0)) // apply path
2712 {
2713 vFileDialogInternal.puFileManager.SetCurrentPath(bookmark.path);
2714 vFileDialogInternal.puFileManager.OpenCurrentPath(vFileDialogInternal);
2715 res = true;
2716 }
2717 }
2718 ImGui::PopID();
2719 if (ImGui::IsItemHovered())
2720 ImGui::SetTooltip("%s", bookmark.path.c_str()); //-V111
2721 }
2722 }
2723 prBookmarkClipper.End();
2724 }
2725
2726 ImGui::EndChild();
2727
2728 return res;
2729 }
2730
2731 std::string IGFD::BookMarkFeature::SerializeBookmarks()
2732 {
2733 std::string res;
2734
2735 size_t idx = 0;
2736 for (auto& it : prBookmarks)
2737 {
2738 if (idx++ != 0)
2739 res += "##"; // ## because reserved by imgui, so an input text cant have ##
2740 res += it.name + "##" + it.path;
2741 }
2742
2743 return res;
2744 }
2745
2746 void IGFD::BookMarkFeature::DeserializeBookmarks(const std::string& vBookmarks)
2747 {
2748 if (!vBookmarks.empty())
2749 {
2750 prBookmarks.clear();
2751 auto arr = IGFD::Utils::SplitStringToVector(vBookmarks, '#', false);
2752 for (size_t i = 0; i < arr.size(); i += 2)
2753 {
2754 BookmarkStruct bookmark;
2755 bookmark.name = arr[i];
2756 if (i + 1 < arr.size()) // for avoid crash if arr size is impair due to user mistake after edition
2757 {
2758 // if bad format we jump this bookmark
2759 bookmark.path = arr[i + 1];
2760 prBookmarks.push_back(bookmark);
2761 }
2762 }
2763 }
2764 }
2765 #endif // USE_BOOKMARK
2766
2767 /////////////////////////////////////////////////////////////////////////////////////
2768 //// KEY EXPLORER FEATURE ///////////////////////////////////////////////////////////
2769 /////////////////////////////////////////////////////////////////////////////////////
2770
2771 KeyExplorerFeature::KeyExplorerFeature() = default;
2772
2773 #ifdef USE_EXPLORATION_BY_KEYS
2774 bool IGFD::KeyExplorerFeature::prLocateItem_Loop(FileDialogInternal& vFileDialogInternal, ImWchar vC)
2775 {
2776 bool found = false;
2777
2778 auto& fdi = vFileDialogInternal.puFileManager;
2779 if (!fdi.IsFilteredListEmpty())
2780 {
2781 auto countFiles = fdi.GetFilteredListSize();
2782 for (size_t i = prLocateFileByInputChar_lastFileIdx; i < countFiles; i++)
2783 {
2784 auto nfo = fdi.GetFilteredFileAt(i);
2785 if (nfo.use_count())
2786 {
2787 if (nfo->fileNameExt_optimized[0] == vC || // lower case search //-V522
2788 nfo->fileNameExt[0] == vC) // maybe upper case search
2789 {
2790 //float p = ((float)i) * ImGui::GetTextLineHeightWithSpacing();
2791 float p = (float)((double)i / (double)countFiles) * ImGui::GetScrollMaxY();
2792 ImGui::SetScrollY(p);
2793 prLocateFileByInputChar_lastFound = true;
2794 prLocateFileByInputChar_lastFileIdx = i;
2795 prStartFlashItem(prLocateFileByInputChar_lastFileIdx);
2796
2797 auto infos = fdi.GetFilteredFileAt(prLocateFileByInputChar_lastFileIdx);
2798 if (infos.use_count())
2799 {
2800 if (infos->fileType == 'd') //-V522
2801 {
2802 if (fdi.puDLGDirectoryMode) // directory chooser
2803 {
2804 fdi.SelectFileName(vFileDialogInternal, infos);
2805 }
2806 }
2807 else
2808 {
2809 fdi.SelectFileName(vFileDialogInternal, infos);
2810 }
2811
2812 found = true;
2813 break;
2814 }
2815 }
2816 }
2817 }
2818 }
2819
2820 return found;
2821 }
2822
2823 void IGFD::KeyExplorerFeature::prLocateByInputKey(FileDialogInternal& vFileDialogInternal)
2824 {
2825 ImGuiContext& g = *GImGui;
2826 auto& fdi = vFileDialogInternal.puFileManager;
2827 if (!g.ActiveId && !fdi.IsFilteredListEmpty())
2828 {
2829 auto& queueChar = ImGui::GetIO().InputQueueCharacters;
2830 auto countFiles = fdi.GetFilteredListSize();
2831
2832 // point by char
2833 if (!queueChar.empty())
2834 {
2835 ImWchar c = queueChar.back();
2836 if (prLocateFileByInputChar_InputQueueCharactersSize != queueChar.size())
2837 {
2838 if (c == prLocateFileByInputChar_lastChar) // next file starting with same char until
2839 {
2840 if (prLocateFileByInputChar_lastFileIdx < countFiles - 1U)
2841 prLocateFileByInputChar_lastFileIdx++;
2842 else
2843 prLocateFileByInputChar_lastFileIdx = 0;
2844 }
2845
2846 if (!prLocateItem_Loop(vFileDialogInternal, c))
2847 {
2848 // not found, loop again from 0 this time
2849 prLocateFileByInputChar_lastFileIdx = 0;
2850 prLocateItem_Loop(vFileDialogInternal, c);
2851 }
2852
2853 prLocateFileByInputChar_lastChar = c;
2854 }
2855 }
2856
2857 prLocateFileByInputChar_InputQueueCharactersSize = queueChar.size();
2858 }
2859 }
2860
2861 void IGFD::KeyExplorerFeature::prExploreWithkeys(FileDialogInternal& vFileDialogInternal, ImGuiID vListViewID)
2862 {
2863 auto& fdi = vFileDialogInternal.puFileManager;
2864 if (!fdi.IsFilteredListEmpty())
2865 {
2866 bool canWeExplore = false;
2867 bool hasNav = (ImGui::GetIO().ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard);
2868
2869 ImGuiContext& g = *GImGui;
2870 if (!hasNav && !g.ActiveId) // no nav and no activated inputs
2871 canWeExplore = true;
2872
2873 if (g.NavId && g.NavId == vListViewID)
2874 {
2875 if (ImGui::IsKeyPressedMap(IGFD_KEY_ENTER) ||
2876 ImGui::IsKeyPressedMap(ImGuiKey_KeyPadEnter) ||
2877 ImGui::IsKeyPressedMap(ImGuiKey_Space))
2878 {
2879 ImGui::ActivateItem(vListViewID);
2880 ImGui::SetActiveID(vListViewID, g.CurrentWindow);
2881 }
2882 }
2883
2884 if (vListViewID == g.LastActiveId-1) // if listview id is the last acticated nav id (ImGui::ActivateItem(vListViewID);)
2885 canWeExplore = true;
2886
2887 if (canWeExplore)
2888 {
2889 if (ImGui::IsKeyPressedMap(ImGuiKey_Escape))
2890 {
2891 ImGui::ClearActiveID();
2892 g.LastActiveId = 0;
2893 }
2894
2895 auto countFiles = fdi.GetFilteredListSize();
2896
2897 // explore
2898 bool exploreByKey = false;
2899 bool enterInDirectory = false;
2900 bool exitDirectory = false;
2901
2902 if ((hasNav && ImGui::IsKeyPressedMap(ImGuiKey_UpArrow)) || (!hasNav && ImGui::IsKeyPressed(IGFD_KEY_UP)))
2903 {
2904 exploreByKey = true;
2905 if (prLocateFileByInputChar_lastFileIdx > 0)
2906 prLocateFileByInputChar_lastFileIdx--;
2907 else
2908 prLocateFileByInputChar_lastFileIdx = countFiles - 1U;
2909 }
2910 else if ((hasNav && ImGui::IsKeyPressedMap(ImGuiKey_DownArrow)) || (!hasNav && ImGui::IsKeyPressed(IGFD_KEY_DOWN)))
2911 {
2912 exploreByKey = true;
2913 if (prLocateFileByInputChar_lastFileIdx < countFiles - 1U)
2914 prLocateFileByInputChar_lastFileIdx++;
2915 else
2916 prLocateFileByInputChar_lastFileIdx = 0U;
2917 }
2918 else if (ImGui::IsKeyReleased(IGFD_KEY_ENTER))
2919 {
2920 exploreByKey = true;
2921 enterInDirectory = true;
2922 }
2923 else if (ImGui::IsKeyReleased(IGFD_KEY_BACKSPACE))
2924 {
2925 exploreByKey = true;
2926 exitDirectory = true;
2927 }
2928
2929 if (exploreByKey)
2930 {
2931 //float totalHeight = prFilteredFileList.size() * ImGui::GetTextLineHeightWithSpacing();
2932 float p = (float)((double)prLocateFileByInputChar_lastFileIdx / (double)(countFiles - 1U)) * ImGui::GetScrollMaxY();// seems not udpated in tables version outside tables
2933 //float p = ((float)locateFileByInputChar_lastFileIdx) * ImGui::GetTextLineHeightWithSpacing();
2934 ImGui::SetScrollY(p);
2935 prStartFlashItem(prLocateFileByInputChar_lastFileIdx);
2936
2937 auto infos = fdi.GetFilteredFileAt(prLocateFileByInputChar_lastFileIdx);
2938 if (infos.use_count())
2939 {
2940 if (infos->fileType == 'd') //-V522
2941 {
2942 if (!fdi.puDLGDirectoryMode || enterInDirectory)
2943 {
2944 if (enterInDirectory)
2945 {
2946 if (fdi.SelectDirectory(infos))
2947 {
2948 // changement de repertoire
2949 vFileDialogInternal.puFileManager.OpenCurrentPath(vFileDialogInternal);
2950 if (prLocateFileByInputChar_lastFileIdx > countFiles - 1U)
2951 {
2952 prLocateFileByInputChar_lastFileIdx = 0;
2953 }
2954 }
2955 }
2956 }
2957 else // directory chooser
2958 {
2959 fdi.SelectFileName(vFileDialogInternal, infos);
2960 }
2961 }
2962 else
2963 {
2964 fdi.SelectFileName(vFileDialogInternal, infos);
2965 }
2966
2967 if (exitDirectory)
2968 {
2969 auto nfo = std::make_shared<FileInfos>();
2970 nfo->fileNameExt = "..";
2971
2972 if (fdi.SelectDirectory(nfo))
2973 {
2974 // changement de repertoire
2975 vFileDialogInternal.puFileManager.OpenCurrentPath(vFileDialogInternal);
2976 if (prLocateFileByInputChar_lastFileIdx > countFiles - 1U)
2977 {
2978 prLocateFileByInputChar_lastFileIdx = 0;
2979 }
2980 }
2981 #ifdef WIN32
2982 else
2983 {
2984 if (fdi.GetComposerSize() == 1U)
2985 {
2986 if (fdi.GetDrives())
2987 {
2988 fdi.ApplyFilteringOnFileList(vFileDialogInternal);
2989 }
2990 }
2991 }
2992 #endif // WIN32
2993 }
2994 }
2995 }
2996 }
2997 }
2998 }
2999
3000 bool IGFD::KeyExplorerFeature::prFlashableSelectable(const char* label, bool selected,
3001 ImGuiSelectableFlags flags, bool vFlashing, const ImVec2& size_arg)
3002 {
3003 using namespace ImGui;
3004
3005 ImGuiWindow* window = GetCurrentWindow();
3006 if (window->SkipItems)
3007 return false;
3008
3009 ImGuiContext& g = *GImGui;
3010 const ImGuiStyle& style = g.Style;
3011
3012 // Submit label or explicit size to ItemSize(), whereas ItemAdd() will submit a larger/spanning rectangle.
3013 ImGuiID id = window->GetID(label);
3014 ImVec2 label_size = CalcTextSize(label, nullptr, true);
3015 ImVec2 size(size_arg.x != 0.0f ? size_arg.x : label_size.x, size_arg.y != 0.0f ? size_arg.y : label_size.y); //-V550
3016 ImVec2 pos = window->DC.CursorPos;
3017 pos.y += window->DC.CurrLineTextBaseOffset;
3018 ItemSize(size, 0.0f);
3019
3020 // Fill horizontal space
3021 // We don't support (size < 0.0f) in Selectable() because the ItemSpacing extension would make explicitly right-aligned sizes not visibly match other widgets.
3022 const bool span_all_columns = (flags & ImGuiSelectableFlags_SpanAllColumns) != 0;
3023 const float min_x = span_all_columns ? window->ParentWorkRect.Min.x : pos.x;
3024 const float max_x = span_all_columns ? window->ParentWorkRect.Max.x : window->WorkRect.Max.x;
3025 if (fabs(size_arg.x) < FLT_EPSILON || (flags & ImGuiSelectableFlags_SpanAvailWidth))
3026 size.x = ImMax(label_size.x, max_x - min_x);
3027
3028 // Text stays at the submission position, but bounding box may be extended on both sides
3029 const ImVec2 text_min = pos;
3030 const ImVec2 text_max(min_x + size.x, pos.y + size.y);
3031
3032 // Selectables are meant to be tightly packed together with no click-gap, so we extend their box to cover spacing between selectable.
3033 ImRect bb(min_x, pos.y, text_max.x, text_max.y);
3034 if ((flags & ImGuiSelectableFlags_NoPadWithHalfSpacing) == 0)
3035 {
3036 const float spacing_x = span_all_columns ? 0.0f : style.ItemSpacing.x;
3037 const float spacing_y = style.ItemSpacing.y;
3038 const float spacing_L = IM_FLOOR(spacing_x * 0.50f);
3039 const float spacing_U = IM_FLOOR(spacing_y * 0.50f);
3040 bb.Min.x -= spacing_L;
3041 bb.Min.y -= spacing_U;
3042 bb.Max.x += (spacing_x - spacing_L);
3043 bb.Max.y += (spacing_y - spacing_U);
3044 }
3045 //if (g.IO.KeyCtrl) { GetForegroundDrawList()->AddRect(bb.Min, bb.Max, IM_COL32(0, 255, 0, 255)); }
3046
3047 // Modify ClipRect for the ItemAdd(), faster than doing a PushColumnsBackground/PushTableBackground for every Selectable..
3048 const float backup_clip_rect_min_x = window->ClipRect.Min.x;
3049 const float backup_clip_rect_max_x = window->ClipRect.Max.x;
3050 if (span_all_columns)
3051 {
3052 window->ClipRect.Min.x = window->ParentWorkRect.Min.x;
3053 window->ClipRect.Max.x = window->ParentWorkRect.Max.x;
3054 }
3055
3056 bool item_add;
3057 const bool disabled_item = (flags & ImGuiSelectableFlags_Disabled) != 0;
3058 if (disabled_item)
3059 {
3060 ImGuiItemFlags backup_item_flags = g.CurrentItemFlags;
3061 g.CurrentItemFlags |= ImGuiItemFlags_Disabled;
3062 item_add = ItemAdd(bb, id);
3063 g.CurrentItemFlags = backup_item_flags;
3064 }
3065 else
3066 {
3067 item_add = ItemAdd(bb, id);
3068 }
3069
3070 if (span_all_columns)
3071 {
3072 window->ClipRect.Min.x = backup_clip_rect_min_x;
3073 window->ClipRect.Max.x = backup_clip_rect_max_x;
3074 }
3075
3076 if (!item_add)
3077 return false;
3078
3079 const bool disabled_global = (g.CurrentItemFlags & ImGuiItemFlags_Disabled) != 0;
3080 if (disabled_item && !disabled_global) // Only testing this as an optimization
3081 BeginDisabled(true);
3082
3083 // FIXME: We can standardize the behavior of those two, we could also keep the fast path of override ClipRect + full push on render only,
3084 // which would be advantageous since most selectable are not selected.
3085 if (span_all_columns && window->DC.CurrentColumns)
3086 PushColumnsBackground();
3087 else if (span_all_columns && g.CurrentTable)
3088 TablePushBackgroundChannel();
3089
3090 // We use NoHoldingActiveID on menus so user can click and _hold_ on a menu then drag to browse child entries
3091 ImGuiButtonFlags button_flags = 0;
3092 if (flags & ImGuiSelectableFlags_NoHoldingActiveID) { button_flags |= ImGuiButtonFlags_NoHoldingActiveId; }
3093 if (flags & ImGuiSelectableFlags_SelectOnClick) { button_flags |= ImGuiButtonFlags_PressedOnClick; }
3094 if (flags & ImGuiSelectableFlags_SelectOnRelease) { button_flags |= ImGuiButtonFlags_PressedOnRelease; }
3095 if (flags & ImGuiSelectableFlags_AllowDoubleClick) { button_flags |= ImGuiButtonFlags_PressedOnClickRelease | ImGuiButtonFlags_PressedOnDoubleClick; }
3096 if (flags & ImGuiSelectableFlags_AllowItemOverlap) { button_flags |= ImGuiButtonFlags_AllowItemOverlap; }
3097
3098 const bool was_selected = selected;
3099 bool hovered, held;
3100 bool pressed = ButtonBehavior(bb, id, &hovered, &held, button_flags);
3101
3102 // Auto-select when moved into
3103 // - This will be more fully fleshed in the range-select branch
3104 // - This is not exposed as it won't nicely work with some user side handling of shift/control
3105 // - We cannot do 'if (g.NavJustMovedToId != id) { selected = false; pressed = was_selected; }' for two reasons
3106 // - (1) it would require focus scope to be set, need exposing PushFocusScope() or equivalent (e.g. BeginSelection() calling PushFocusScope())
3107 // - (2) usage will fail with clipped items
3108 // The multi-select API aim to fix those issues, e.g. may be replaced with a BeginSelection() API.
3109 if ((flags & ImGuiSelectableFlags_SelectOnNav) && g.NavJustMovedToId != 0 && g.NavJustMovedToFocusScopeId == window->DC.NavFocusScopeIdCurrent)
3110 if (g.NavJustMovedToId == id)
3111 selected = pressed = true;
3112
3113 // Update NavId when clicking or when Hovering (this doesn't happen on most widgets), so navigation can be resumed with gamepad/keyboard
3114 if (pressed || (hovered && (flags & ImGuiSelectableFlags_SetNavIdOnHover)))
3115 {
3116 if (!g.NavDisableMouseHover && g.NavWindow == window && g.NavLayer == window->DC.NavLayerCurrent)
3117 {
3118 SetNavID(id, window->DC.NavLayerCurrent, window->DC.NavFocusScopeIdCurrent, ImRect(bb.Min - window->Pos, bb.Max - window->Pos));
3119 g.NavDisableHighlight = true;
3120 }
3121 }
3122 if (pressed)
3123 MarkItemEdited(id);
3124
3125 if (flags & ImGuiSelectableFlags_AllowItemOverlap)
3126 SetItemAllowOverlap();
3127
3128 // In this branch, Selectable() cannot toggle the selection so this will never trigger.
3129 if (selected != was_selected) //-V547
3130 g.LastItemData.StatusFlags |= ImGuiItemStatusFlags_ToggledSelection;
3131
3132 // Render
3133 if ((held && (flags & ImGuiSelectableFlags_DrawHoveredWhenHeld)) || vFlashing)
3134 hovered = true;
3135 if (hovered || selected)
3136 {
3137 const ImU32 col = GetColorU32((held && hovered) ? ImGuiCol_HeaderActive : hovered ? ImGuiCol_HeaderHovered : ImGuiCol_Header);
3138 RenderFrame(bb.Min, bb.Max, col, false, 0.0f);
3139 }
3140 RenderNavHighlight(bb, id, ImGuiNavHighlightFlags_TypeThin | ImGuiNavHighlightFlags_NoRounding);
3141
3142 if (span_all_columns && window->DC.CurrentColumns)
3143 PopColumnsBackground();
3144 else if (span_all_columns && g.CurrentTable)
3145 TablePopBackgroundChannel();
3146
3147 RenderTextClipped(text_min, text_max, label, nullptr, &label_size, style.SelectableTextAlign, &bb);
3148
3149 // Automatically close popups
3150 if (pressed && (window->Flags & ImGuiWindowFlags_Popup) && !(flags & ImGuiSelectableFlags_DontClosePopups) && !(g.LastItemData.InFlags & ImGuiItemFlags_SelectableDontClosePopup))
3151 CloseCurrentPopup();
3152
3153 if (disabled_item && !disabled_global)
3154 EndDisabled();
3155
3156 IMGUI_TEST_ENGINE_ITEM_INFO(id, label, g.LastItemData.StatusFlags);
3157 return pressed; //-V1020
3158 }
3159
3160 void IGFD::KeyExplorerFeature::prStartFlashItem(size_t vIdx)
3161 {
3162 prFlashAlpha = 1.0f;
3163 prFlashedItem = vIdx;
3164 }
3165
3166 bool IGFD::KeyExplorerFeature::prBeginFlashItem(size_t vIdx)
3167 {
3168 bool res = false;
3169
3170 if (prFlashedItem == vIdx &&
3171 std::abs(prFlashAlpha - 0.0f) > 0.00001f)
3172 {
3173 prFlashAlpha -= prFlashAlphaAttenInSecs * ImGui::GetIO().DeltaTime;
3174 if (prFlashAlpha < 0.0f) prFlashAlpha = 0.0f;
3175
3176 ImVec4 hov = ImGui::GetStyleColorVec4(ImGuiCol_HeaderHovered);
3177 hov.w = prFlashAlpha;
3178 ImGui::PushStyleColor(ImGuiCol_HeaderHovered, hov);
3179 res = true;
3180 }
3181
3182 return res;
3183 }
3184
3185 void IGFD::KeyExplorerFeature::prEndFlashItem()
3186 {
3187 ImGui::PopStyleColor();
3188 }
3189
3190 void IGFD::KeyExplorerFeature::SetFlashingAttenuationInSeconds(float vAttenValue)
3191 {
3192 prFlashAlphaAttenInSecs = 1.0f / ImMax(vAttenValue, 0.01f);
3193 }
3194 #endif // USE_EXPLORATION_BY_KEYS
3195
3196 /////////////////////////////////////////////////////////////////////////////////////
3197 //// FILE DIALOG CONSTRUCTOR / DESTRUCTOR ///////////////////////////////////////////
3198 /////////////////////////////////////////////////////////////////////////////////////
3199
3200 IGFD::FileDialog::FileDialog() : BookMarkFeature(), KeyExplorerFeature(), ThumbnailFeature() {DpiScale=1.0f; singleClickSel=false; mobileMode=false;}
3201 IGFD::FileDialog::~FileDialog() = default;
3202
3203 //////////////////////////////////////////////////////////////////////////////////////////////////
3204 ///// FILE DIALOG STANDARD DIALOG ////////////////////////////////////////////////////////////////
3205 //////////////////////////////////////////////////////////////////////////////////////////////////
3206
3207 // path and fileNameExt can be specified
3208 void IGFD::FileDialog::OpenDialog(
3209 const std::string& vKey,
3210 const std::string& vTitle,
3211 const char* vFilters,
3212 const std::string& vPath,
3213 const std::string& vFileName,
3214 const int& vCountSelectionMax,
3215 UserDatas vUserDatas,
3216 ImGuiFileDialogFlags vFlags,
3217 SelectFun vSelectFun)
3218 {
3219 if (prFileDialogInternal.puShowDialog) // if already opened, quit
3220 return;
3221
3222 prFileDialogInternal.ResetForNewDialog();
3223
3224 prFileDialogInternal.puDLGkey = vKey;
3225 prFileDialogInternal.puDLGtitle = vTitle;
3226 prFileDialogInternal.puDLGuserDatas = vUserDatas;
3227 prFileDialogInternal.puDLGflags = vFlags;
3228 prFileDialogInternal.puDLGselFun = vSelectFun;
3229 prFileDialogInternal.puDLGoptionsPane = nullptr;
3230 prFileDialogInternal.puDLGoptionsPaneWidth = 0.0f;
3231 prFileDialogInternal.puDLGmodal = false;
3232
3233 prFileDialogInternal.puFilterManager.puDLGdefaultExt.clear();
3234 prFileDialogInternal.puFilterManager.ParseFilters(vFilters);
3235
3236 prFileDialogInternal.puFileManager.puDLGDirectoryMode = (vFilters == nullptr);
3237 if (vPath.empty())
3238 prFileDialogInternal.puFileManager.puDLGpath = prFileDialogInternal.puFileManager.GetCurrentPath();
3239 else
3240 prFileDialogInternal.puFileManager.puDLGpath = vPath;
3241 prFileDialogInternal.puFileManager.SetCurrentPath(vPath);
3242 prFileDialogInternal.puFileManager.puDLGcountSelectionMax = (size_t)vCountSelectionMax;
3243 prFileDialogInternal.puFileManager.SetDefaultFileName(vFileName);
3244
3245 prFileDialogInternal.puFileManager.ClearAll();
3246
3247 prFileDialogInternal.puShowDialog = true; // open dialog
3248 }
3249
3250 // path and filename are obtained from filePathName
3251 void IGFD::FileDialog::OpenDialog(
3252 const std::string& vKey,
3253 const std::string& vTitle,
3254 const char* vFilters,
3255 const std::string& vFilePathName,
3256 const int& vCountSelectionMax,
3257 UserDatas vUserDatas,
3258 ImGuiFileDialogFlags vFlags,
3259 SelectFun vSelectFun)
3260 {
3261 if (prFileDialogInternal.puShowDialog) // if already opened, quit
3262 return;
3263
3264 prFileDialogInternal.ResetForNewDialog();
3265
3266 prFileDialogInternal.puDLGkey = vKey;
3267 prFileDialogInternal.puDLGtitle = vTitle;
3268 prFileDialogInternal.puDLGoptionsPane = nullptr;
3269 prFileDialogInternal.puDLGoptionsPaneWidth = 0.0f;
3270 prFileDialogInternal.puDLGuserDatas = vUserDatas;
3271 prFileDialogInternal.puDLGflags = vFlags;
3272 prFileDialogInternal.puDLGselFun = vSelectFun;
3273 prFileDialogInternal.puDLGmodal = false;
3274
3275 auto ps = IGFD::Utils::ParsePathFileName(vFilePathName);
3276 if (ps.isOk)
3277 {
3278 prFileDialogInternal.puFileManager.puDLGpath = ps.path;
3279 prFileDialogInternal.puFileManager.SetDefaultFileName("");
3280 prFileDialogInternal.puFilterManager.puDLGdefaultExt = "." + ps.ext;
3281 }
3282 else
3283 {
3284 prFileDialogInternal.puFileManager.puDLGpath = prFileDialogInternal.puFileManager.GetCurrentPath();
3285 prFileDialogInternal.puFileManager.SetDefaultFileName("");
3286 prFileDialogInternal.puFilterManager.puDLGdefaultExt.clear();
3287 }
3288
3289 prFileDialogInternal.puFilterManager.ParseFilters(vFilters);
3290 prFileDialogInternal.puFilterManager.SetSelectedFilterWithExt(
3291 prFileDialogInternal.puFilterManager.puDLGdefaultExt);
3292
3293 prFileDialogInternal.puFileManager.SetCurrentPath(prFileDialogInternal.puFileManager.puDLGpath);
3294
3295 prFileDialogInternal.puFileManager.puDLGDirectoryMode = (vFilters == nullptr);
3296 prFileDialogInternal.puFileManager.puDLGcountSelectionMax = vCountSelectionMax; //-V101
3297
3298 prFileDialogInternal.puFileManager.ClearAll();
3299
3300 prFileDialogInternal.puShowDialog = true;
3301 }
3302
3303 // with pane
3304 // path and fileNameExt can be specified
3305 void IGFD::FileDialog::OpenDialog(
3306 const std::string& vKey,
3307 const std::string& vTitle,
3308 const char* vFilters,
3309 const std::string& vPath,
3310 const std::string& vFileName,
3311 const PaneFun& vSidePane,
3312 const float& vSidePaneWidth,
3313 const int& vCountSelectionMax,
3314 UserDatas vUserDatas,
3315 ImGuiFileDialogFlags vFlags,
3316 SelectFun vSelectFun)
3317 {
3318 if (prFileDialogInternal.puShowDialog) // if already opened, quit
3319 return;
3320
3321 prFileDialogInternal.ResetForNewDialog();
3322
3323 prFileDialogInternal.puDLGkey = vKey;
3324 prFileDialogInternal.puDLGtitle = vTitle;
3325 prFileDialogInternal.puDLGuserDatas = vUserDatas;
3326 prFileDialogInternal.puDLGflags = vFlags;
3327 prFileDialogInternal.puDLGselFun = vSelectFun;
3328 prFileDialogInternal.puDLGoptionsPane = vSidePane;
3329 prFileDialogInternal.puDLGoptionsPaneWidth = vSidePaneWidth;
3330 prFileDialogInternal.puDLGmodal = false;
3331
3332 prFileDialogInternal.puFilterManager.puDLGdefaultExt.clear();
3333 prFileDialogInternal.puFilterManager.ParseFilters(vFilters);
3334
3335 prFileDialogInternal.puFileManager.puDLGcountSelectionMax = (size_t)vCountSelectionMax;
3336 prFileDialogInternal.puFileManager.puDLGDirectoryMode = (vFilters == nullptr);
3337 if (vPath.empty())
3338 prFileDialogInternal.puFileManager.puDLGpath = prFileDialogInternal.puFileManager.GetCurrentPath();
3339 else
3340 prFileDialogInternal.puFileManager.puDLGpath = vPath;
3341
3342 prFileDialogInternal.puFileManager.SetCurrentPath(prFileDialogInternal.puFileManager.puDLGpath);
3343
3344 prFileDialogInternal.puFileManager.SetDefaultFileName(vFileName);
3345
3346 prFileDialogInternal.puFileManager.ClearAll();
3347
3348 prFileDialogInternal.puShowDialog = true; // open dialog
3349 }
3350
3351 // with pane
3352 // path and filename are obtained from filePathName
3353 void IGFD::FileDialog::OpenDialog(
3354 const std::string& vKey,
3355 const std::string& vTitle,
3356 const char* vFilters,
3357 const std::string& vFilePathName,
3358 const PaneFun& vSidePane,
3359 const float& vSidePaneWidth,
3360 const int& vCountSelectionMax,
3361 UserDatas vUserDatas,
3362 ImGuiFileDialogFlags vFlags,
3363 SelectFun vSelectFun)
3364 {
3365 if (prFileDialogInternal.puShowDialog) // if already opened, quit
3366 return;
3367
3368 prFileDialogInternal.ResetForNewDialog();
3369
3370 prFileDialogInternal.puDLGkey = vKey;
3371 prFileDialogInternal.puDLGtitle = vTitle;
3372 prFileDialogInternal.puDLGoptionsPane = vSidePane;
3373 prFileDialogInternal.puDLGoptionsPaneWidth = vSidePaneWidth;
3374 prFileDialogInternal.puDLGuserDatas = vUserDatas;
3375 prFileDialogInternal.puDLGflags = vFlags;
3376 prFileDialogInternal.puDLGselFun = vSelectFun;
3377 prFileDialogInternal.puDLGmodal = false;
3378
3379 auto ps = IGFD::Utils::ParsePathFileName(vFilePathName);
3380 if (ps.isOk)
3381 {
3382 prFileDialogInternal.puFileManager.puDLGpath = ps.path;
3383 prFileDialogInternal.puFileManager.SetDefaultFileName(vFilePathName);
3384 prFileDialogInternal.puFilterManager.puDLGdefaultExt = "." + ps.ext;
3385 }
3386 else
3387 {
3388 prFileDialogInternal.puFileManager.puDLGpath = prFileDialogInternal.puFileManager.GetCurrentPath();
3389 prFileDialogInternal.puFileManager.SetDefaultFileName("");
3390 prFileDialogInternal.puFilterManager.puDLGdefaultExt.clear();
3391 }
3392
3393 prFileDialogInternal.puFileManager.SetCurrentPath(prFileDialogInternal.puFileManager.puDLGpath);
3394
3395 prFileDialogInternal.puFileManager.puDLGcountSelectionMax = vCountSelectionMax; //-V101
3396 prFileDialogInternal.puFileManager.puDLGDirectoryMode = (vFilters == nullptr);
3397 prFileDialogInternal.puFilterManager.ParseFilters(vFilters);
3398 prFileDialogInternal.puFilterManager.SetSelectedFilterWithExt(
3399 prFileDialogInternal.puFilterManager.puDLGdefaultExt);
3400
3401 prFileDialogInternal.puFileManager.ClearAll();
3402
3403 prFileDialogInternal.puShowDialog = true;
3404 }
3405
3406 //////////////////////////////////////////////////////////////////////////////////////////////////
3407 ///// FILE DIALOG MODAL DIALOG ///////////////////////////////////////////////////////////////////
3408 //////////////////////////////////////////////////////////////////////////////////////////////////
3409
3410 void IGFD::FileDialog::OpenModal(
3411 const std::string& vKey,
3412 const std::string& vTitle,
3413 const char* vFilters,
3414 const std::string& vPath,
3415 const std::string& vFileName,
3416 const int& vCountSelectionMax,
3417 UserDatas vUserDatas,
3418 ImGuiFileDialogFlags vFlags,
3419 SelectFun vSelectFun)
3420 {
3421 if (prFileDialogInternal.puShowDialog) // if already opened, quit
3422 return;
3423
3424 OpenDialog(
3425 vKey, vTitle, vFilters,
3426 vPath, vFileName,
3427 vCountSelectionMax, vUserDatas, vFlags, vSelectFun);
3428
3429 prFileDialogInternal.puDLGmodal = true;
3430 }
3431
3432 void IGFD::FileDialog::OpenModal(
3433 const std::string& vKey,
3434 const std::string& vTitle,
3435 const char* vFilters,
3436 const std::string& vFilePathName,
3437 const int& vCountSelectionMax,
3438 UserDatas vUserDatas,
3439 ImGuiFileDialogFlags vFlags,
3440 SelectFun vSelectFun)
3441 {
3442 if (prFileDialogInternal.puShowDialog) // if already opened, quit
3443 return;
3444
3445 OpenDialog(
3446 vKey, vTitle, vFilters,
3447 vFilePathName,
3448 vCountSelectionMax, vUserDatas, vFlags, vSelectFun);
3449
3450 prFileDialogInternal.puDLGmodal = true;
3451 }
3452
3453 // with pane
3454 // path and fileNameExt can be specified
3455 void IGFD::FileDialog::OpenModal(
3456 const std::string& vKey,
3457 const std::string& vTitle,
3458 const char* vFilters,
3459 const std::string& vPath,
3460 const std::string& vFileName,
3461 const PaneFun& vSidePane,
3462 const float& vSidePaneWidth,
3463 const int& vCountSelectionMax,
3464 UserDatas vUserDatas,
3465 ImGuiFileDialogFlags vFlags,
3466 SelectFun vSelectFun)
3467 {
3468 if (prFileDialogInternal.puShowDialog) // if already opened, quit
3469 return;
3470
3471 OpenDialog(
3472 vKey, vTitle, vFilters,
3473 vPath, vFileName,
3474 vSidePane, vSidePaneWidth,
3475 vCountSelectionMax, vUserDatas, vFlags, vSelectFun);
3476
3477 prFileDialogInternal.puDLGmodal = true;
3478 }
3479
3480 // with pane
3481 // path and filename are obtained from filePathName
3482 void IGFD::FileDialog::OpenModal(
3483 const std::string& vKey,
3484 const std::string& vTitle,
3485 const char* vFilters,
3486 const std::string& vFilePathName,
3487 const PaneFun& vSidePane,
3488 const float& vSidePaneWidth,
3489 const int& vCountSelectionMax,
3490 UserDatas vUserDatas,
3491 ImGuiFileDialogFlags vFlags,
3492 SelectFun vSelectFun)
3493 {
3494 if (prFileDialogInternal.puShowDialog) // if already opened, quit
3495 return;
3496
3497 OpenDialog(
3498 vKey, vTitle, vFilters,
3499 vFilePathName,
3500 vSidePane, vSidePaneWidth,
3501 vCountSelectionMax, vUserDatas, vFlags, vSelectFun);
3502
3503 prFileDialogInternal.puDLGmodal = true;
3504 }
3505
3506 //////////////////////////////////////////////////////////////////////////////////////////////////
3507 ///// FILE DIALOG DISPLAY FUNCTION ///////////////////////////////////////////////////////////////
3508 //////////////////////////////////////////////////////////////////////////////////////////////////
3509
3510 bool IGFD::FileDialog::Display(const std::string& vKey, ImGuiWindowFlags vFlags, ImVec2 vMinSize, ImVec2 vMaxSize)
3511 {
3512 bool res = false;
3513
3514 if (prFileDialogInternal.puShowDialog && prFileDialogInternal.puDLGkey == vKey)
3515 {
3516 if (prFileDialogInternal.puUseCustomLocale)
3517 setlocale(prFileDialogInternal.puLocaleCategory, prFileDialogInternal.puLocaleBegin.c_str());
3518
3519 auto& fdFile = prFileDialogInternal.puFileManager;
3520 auto& fdFilter = prFileDialogInternal.puFilterManager;
3521
3522 static ImGuiWindowFlags flags;
3523
3524 // to be sure than only one dialog is displayed per frame
3525 ImGuiContext& g = *GImGui;
3526 if (g.FrameCount == prFileDialogInternal.puLastImGuiFrameCount) // one instance was displayed this frame before for this key +> quit
3527 return res;
3528 prFileDialogInternal.puLastImGuiFrameCount = g.FrameCount; // mark this instance as used this frame
3529
3530 std::string name = prFileDialogInternal.puDLGtitle + "##" + prFileDialogInternal.puDLGkey;
3531 if (prFileDialogInternal.puName != name)
3532 {
3533 fdFile.ClearComposer();
3534 fdFile.ClearFileLists();
3535 flags = vFlags;
3536 }
3537
3538 NewFrame();
3539
3540 #ifdef IMGUI_HAS_VIEWPORT
3541 if (!ImGui::GetIO().ConfigViewportsNoDecoration)
3542 {
3543 // https://github.com/ocornut/imgui/issues/4534
3544 ImGuiWindowClass window_class;
3545 window_class.ViewportFlagsOverrideClear = ImGuiViewportFlags_NoDecoration;
3546 ImGui::SetNextWindowClass(&window_class);
3547 }
3548 #endif // IMGUI_HAS_VIEWPORT
3549
3550 ImGui::SetNextWindowSizeConstraints(vMinSize, vMaxSize);
3551
3552 bool beg = false;
3553 if (prFileDialogInternal.puDLGmodal &&
3554 !prFileDialogInternal.puOkResultToConfirm) // disable modal because the confirm dialog for overwrite is a new modal
3555 {
3556 ImGui::OpenPopup(name.c_str());
3557 beg = ImGui::BeginPopupModal(name.c_str(), (bool*)nullptr,
3558 flags | ImGuiWindowFlags_NoScrollbar);
3559 }
3560 else
3561 {
3562 beg = ImGui::Begin(name.c_str(), (bool*)nullptr, flags | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoScrollWithMouse | ImGuiWindowFlags_NoDocking);
3563 }
3564 if (beg)
3565 {
3566 ImGui::SetWindowPos(ImVec2((ImGui::GetMainViewport()->Size.x-ImGui::GetWindowWidth())*0.5f,(ImGui::GetMainViewport()->Size.y-ImGui::GetWindowHeight())*0.5f));
3567 if (ImGui::GetWindowSize().x<vMinSize.x || ImGui::GetWindowSize().y<vMinSize.y) {
3568 ImGui::SetWindowSize(vMinSize,ImGuiCond_Always);
3569 }
3570 #ifdef IMGUI_HAS_VIEWPORT
3571 // if decoration is enabled we disable the resizing feature of imgui for avoid crash with SDL2 and GLFW3
3572 if (ImGui::GetIO().ConfigViewportsNoDecoration)
3573 {
3574 flags = vFlags;
3575 }
3576 else
3577 {
3578 auto win = ImGui::GetCurrentWindowRead();
3579 if (win->Viewport->Idx != 0)
3580 flags |= ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoTitleBar;
3581 else
3582 flags = vFlags;
3583 }
3584 #endif // IMGUI_HAS_VIEWPORT
3585
3586 prFileDialogInternal.puName = name; //-V820
3587 puAnyWindowsHovered |= ImGui::IsWindowHovered();
3588
3589 if (fdFile.puDLGpath.empty())
3590 fdFile.puDLGpath = "."; // defaut path is '.'
3591
3592 fdFilter.SetDefaultFilterIfNotDefined();
3593
3594 // init list of files
3595 if (fdFile.IsFileListEmpty() && !fdFile.puShowDrives && !fdFile.fileListActuallyEmpty)
3596 {
3597 IGFD::Utils::ReplaceString(fdFile.puDLGDefaultFileName, fdFile.puDLGpath, ""); // local path
3598 if (!fdFile.puDLGDefaultFileName.empty())
3599 {
3600 fdFile.SetDefaultFileName(fdFile.puDLGDefaultFileName);
3601 fdFilter.SetSelectedFilterWithExt(fdFilter.puDLGdefaultExt);
3602 } else if (fdFile.puDLGDirectoryMode) { // directory mode
3603 fdFile.SetDefaultFileName(".");
3604 }
3605 logV("IGFD: fdFile.IsFileListEmpty() and !fdFile.puShowDrives");
3606 fdFile.ScanDir(prFileDialogInternal, fdFile.puDLGpath);
3607 }
3608
3609 // draw dialog parts
3610 prDrawHeader(); // bookmark, directory, path
3611 res = prDrawContent(); // bookmark, files view, side pane
3612 bool res1 = prDrawFooter(); // file field, filter combobox, ok/cancel buttons
3613 if (!res) res=res1;
3614
3615 EndFrame();
3616
3617 // for display in dialog center, the confirm to overwrite dlg
3618 prFileDialogInternal.puDialogCenterPos = ImGui::GetCurrentWindowRead()->ContentRegionRect.GetCenter();
3619
3620 // when the confirm to overwrite dialog will appear we need to
3621 // disable the modal mode of the main file dialog
3622 // see prOkResultToConfirm under
3623 if (prFileDialogInternal.puDLGmodal &&
3624 !prFileDialogInternal.puOkResultToConfirm)
3625 ImGui::EndPopup();
3626 }
3627
3628 // same things here regarding prOkResultToConfirm
3629 if (!prFileDialogInternal.puDLGmodal || prFileDialogInternal.puOkResultToConfirm)
3630 ImGui::End();
3631
3632 // confirm the result and show the confirm to overwrite dialog if needed
3633 res = prConfirm_Or_OpenOverWriteFileDialog_IfNeeded(res, vFlags);
3634
3635 if (prFileDialogInternal.puUseCustomLocale)
3636 setlocale(prFileDialogInternal.puLocaleCategory, prFileDialogInternal.puLocaleEnd.c_str());
3637 }
3638
3639 return res;
3640 }
3641
3642 void IGFD::FileDialog::NewFrame()
3643 {
3644 prFileDialogInternal.NewFrame();
3645 NewThumbnailFrame(prFileDialogInternal);
3646 }
3647
3648 void IGFD::FileDialog::EndFrame()
3649 {
3650 EndThumbnailFrame(prFileDialogInternal);
3651 prFileDialogInternal.EndFrame();
3652
3653 }
3654 void IGFD::FileDialog::QuitFrame()
3655 {
3656 QuitThumbnailFrame(prFileDialogInternal);
3657 }
3658
3659 void IGFD::FileDialog::prDrawHeader()
3660 {
3661 #ifdef USE_BOOKMARK
3662 prDrawBookmarkButton();
3663 ImGui::SameLine();
3664 #endif // USE_BOOKMARK
3665
3666 prFileDialogInternal.puFileManager.DrawDirectoryCreation(prFileDialogInternal);
3667 ImGui::SameLine();
3668 ImGui::SeparatorEx(ImGuiSeparatorFlags_Vertical);
3669 ImGui::SameLine();
3670 prFileDialogInternal.puFileManager.DrawPathComposer(prFileDialogInternal);
3671
3672 #ifdef USE_THUMBNAILS
3673 if (!(prFileDialogInternal.puDLGflags & ImGuiFileDialogFlags_DisableThumbnailMode))
3674 {
3675 prDrawDisplayModeToolBar();
3676 ImGui::SameLine();
3677 ImGui::SeparatorEx(ImGuiSeparatorFlags_Vertical);
3678 ImGui::SameLine();
3679 }
3680 #endif // USE_THUMBNAILS
3681
3682 prFileDialogInternal.puSearchManager.DrawSearchBar(prFileDialogInternal);
3683 }
3684
3685 bool IGFD::FileDialog::prDrawContent()
3686 {
3687 bool escape = false;
3688 ImVec2 size = ImGui::GetContentRegionAvail() - ImVec2(0.0f, prFileDialogInternal.puFooterHeight);
3689
3690 #ifdef USE_BOOKMARK
3691 if (prBookmarkPaneShown)
3692 {
3693 //size.x -= prBookmarkWidth;
3694 float otherWidth = size.x - prBookmarkWidth;
3695 ImGui::PushID("##splitterbookmark");
3696 IGFD::Utils::Splitter(true, 4.0f,
3697 &prBookmarkWidth, &otherWidth, 10.0f,
3698 10.0f + prFileDialogInternal.puDLGoptionsPaneWidth, size.y);
3699 ImGui::PopID();
3700 size.x -= otherWidth;
3701 prDrawBookmarkPane(prFileDialogInternal, size);
3702 ImGui::SameLine();
3703 }
3704 #endif // USE_BOOKMARK
3705
3706 size.x = ImGui::GetContentRegionAvail().x - prFileDialogInternal.puDLGoptionsPaneWidth;
3707
3708 if (prFileDialogInternal.puDLGoptionsPane)
3709 {
3710 ImGui::PushID("##splittersidepane");
3711 IGFD::Utils::Splitter(true, 4.0f, &size.x, &prFileDialogInternal.puDLGoptionsPaneWidth, 10.0f, 10.0f, size.y);
3712 ImGui::PopID();
3713 }
3714
3715 #ifdef USE_THUMBNAILS
3716 if (prFileDialogInternal.puDLGflags & ImGuiFileDialogFlags_DisableThumbnailMode)
3717 {
3718 prDrawFileListView(size);
3719 }
3720 else
3721 {
3722 switch (prDisplayMode)
3723 {
3724 case DisplayModeEnum::FILE_LIST:
3725 prDrawFileListView(size);
3726 break;
3727 case DisplayModeEnum::THUMBNAILS_LIST:
3728 prDrawThumbnailsListView(size);
3729 break;
3730 case DisplayModeEnum::THUMBNAILS_GRID:
3731 prDrawThumbnailsGridView(size);
3732 }
3733 }
3734 #else
3735 escape = prDrawFileListView(size);
3736 #endif // USE_THUMBNAILS
3737
3738 if (prFileDialogInternal.puDLGoptionsPane)
3739 {
3740 prDrawSidePane(size.y);
3741 }
3742
3743 return escape;
3744 }
3745
3746 int IGFD::FileDialog::isFileNameValid(const char* n) {
3747 #ifdef _WIN32
3748 char lower[6];
3749 int len=strlen(n);
3750
3751 for (int i=0; i<6; i++) {
3752 lower[i]=0;
3753 }
3754
3755 if (len>=3) {
3756 if (len>5) len=5;
3757 for (int i=0; i<len; i++) {
3758 if (n[i]>='A' && n[i]<='Z') {
3759 lower[i]=n[i]+('a'-'A');
3760 } else {
3761 lower[i]=n[i];
3762 }
3763 }
3764 if ((memcmp(lower,"con",3)==0 ||
3765 memcmp(lower,"prn",3)==0 ||
3766 memcmp(lower,"aux",3)==0 ||
3767 memcmp(lower,"nul",3)==0) &&
3768 (len==3 || lower[3]=='.')) {
3769 return 2;
3770 }
3771 if (len>=4) {
3772 if ((memcmp(lower,"com",3) ||
3773 memcmp(lower,"lpt",3)) &&
3774 (lower[3]>='1' && lower[3]<='9') &&
3775 (len==4 || lower[4]=='.')) {
3776 return 2;
3777 }
3778 }
3779 }
3780 #endif
3781 for (const unsigned char* i=(const unsigned char*)n; *i; i++) {
3782 #ifdef _WIN32
3783 if (*i<32) {
3784 return 3;
3785 }
3786 #endif
3787
3788 switch (*i) {
3789 #ifdef _WIN32
3790 case '<':
3791 case '>':
3792 case ':':
3793 case '"':
3794 case '\\':
3795 case '|':
3796 case '?':
3797 case '*':
3798 #endif
3799 case '/':
3800 return 1;
3801 }
3802 }
3803 return 0;
3804 }
3805
3806 bool IGFD::FileDialog::prDrawFooter()
3807 {
3808 auto& fdFile = prFileDialogInternal.puFileManager;
3809
3810 float posY = ImGui::GetCursorPos().y; // height of last bar calc
3811
3812 ImGui::AlignTextToFramePadding();
3813 if (!fdFile.puDLGDirectoryMode)
3814 ImGui::Text("%s",FileDialog::Instance()->fileNameString);
3815 else // directory chooser
3816 ImGui::Text("%s",FileDialog::Instance()->dirNameString);
3817
3818 ImGui::SameLine();
3819
3820 // Input file fields
3821 float width = ImGui::GetContentRegionAvail().x;
3822 // fix this! fix this! fix this!
3823 if (!fdFile.puDLGDirectoryMode)
3824 width -= FILTER_COMBO_WIDTH*DpiScale;
3825 ImGui::PushItemWidth(width);
3826 ImGui::InputText("##FileName", fdFile.puFileNameBuffer, MAX_FILE_DIALOG_NAME_BUFFER);
3827 if (ImGui::GetItemID() == ImGui::GetActiveID())
3828 prFileDialogInternal.puFileInputIsActive = true;
3829 ImGui::PopItemWidth();
3830
3831 // combobox of filters
3832 prFileDialogInternal.puFilterManager.DrawFilterComboBox(prFileDialogInternal);
3833
3834 bool res = false;
3835
3836 // OK Button
3837 bool notEmpty=strlen(fdFile.puFileNameBuffer);
3838 int fileValid=isFileNameValid(fdFile.puFileNameBuffer);
3839 if (!(prFileDialogInternal.puDLGflags&ImGuiFileDialogFlags_ConfirmOverwrite)) fileValid=0;
3840 ImGui::BeginDisabled(!(prFileDialogInternal.puCanWeContinue && notEmpty && fileValid==0));
3841 std::string okbs=std::string(FileDialog::Instance()->okButtonString)+"##validationdialog";
3842 if (IMGUI_BUTTON(okbs.c_str()))
3843 {
3844 prFileDialogInternal.puIsOk = true;
3845 res = true;
3846 }
3847 ImGui::EndDisabled();
3848 if (!(prFileDialogInternal.puCanWeContinue && notEmpty && fileValid==0)) {
3849 if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled)) {
3850 if (!notEmpty) {
3851 if (prFileDialogInternal.puDLGflags&ImGuiFileDialogFlags_ConfirmOverwrite) {
3852 ImGui::SetTooltip("file name is empty");
3853 } else {
3854 ImGui::SetTooltip("select a file");
3855 }
3856 } else if (!prFileDialogInternal.puCanWeContinue) {
3857 ImGui::SetTooltip("we can't continue - this is most likely a bug!");
3858 } else switch (fileValid) {
3859 case 1:
3860 #ifdef _WIN32
3861 ImGui::SetTooltip("invalid characters in file name\nmake sure there aren't any of these:\n < > : \" / \\ | ? *");
3862 #else
3863 ImGui::SetTooltip("invalid characters in file name\nmake sure there aren't any slashes (/)");
3864 #endif
3865 break;
3866 case 2:
3867 ImGui::SetTooltip("this file name is reserved by the system");
3868 break;
3869 case 3:
3870 ImGui::SetTooltip("non-printable characters in file name\nhow did you manage to type these?");
3871 break;
3872 default:
3873 ImGui::SetTooltip("bug! how did you do this? please fill an issue report now...");
3874 break;
3875 }
3876 }
3877 }
3878
3879 ImGui::SameLine();
3880
3881 // Cancel Button
3882 std::string cbs=std::string(cancelButtonString)+"##validationdialog";
3883 if (IMGUI_BUTTON(cbs.c_str()) ||
3884 prFileDialogInternal.puNeedToExitDialog) // dialog exit asked
3885 {
3886 prFileDialogInternal.puIsOk = false;
3887 res = true;
3888 }
3889
3890 prFileDialogInternal.puFooterHeight = ImGui::GetCursorPosY() - posY;
3891
3892 return res;
3893 }
3894
3895 // returns 0 if not break loop, 1 if break loop, 2 if exit dialog
3896 int IGFD::FileDialog::prSelectableItem(int vidx, const FileInfos& vInfos, bool vSelected, const char* vFmt, ...)
3897 {
3898 auto& fdi = prFileDialogInternal.puFileManager;
3899
3900 static ImGuiSelectableFlags selectableFlags = ImGuiSelectableFlags_AllowDoubleClick |
3901 ImGuiSelectableFlags_SpanAllColumns | ImGuiSelectableFlags_SpanAvailWidth | ImGuiSelectableFlags_NoHashTextHide;
3902
3903 // TODO BUG?!
3904 // YES BUG: THIS JUST CRASHED FOR SOME REASON
3905 va_list args;
3906 va_start(args, vFmt);
3907 vsnprintf(fdi.puVariadicBuffer, MAX_FILE_DIALOG_NAME_BUFFER, vFmt, args);
3908 va_end(args);
3909
3910 float h = /*mobileMode?(ImGui::GetFontSize()+10.0f*DpiScale):*/0.0f;
3911 #ifdef USE_THUMBNAILS
3912 if (prDisplayMode == DisplayModeEnum::THUMBNAILS_LIST)
3913 h = DisplayMode_ThumbailsList_ImageHeight;
3914 #endif // USE_THUMBNAILS
3915 #ifdef USE_EXPLORATION_BY_KEYS
3916 bool flashed = prBeginFlashItem((size_t)vidx);
3917 bool res = prFlashableSelectable(fdi.puVariadicBuffer, vSelected, selectableFlags,
3918 flashed, ImVec2(-1.0f, h));
3919 if (flashed)
3920 prEndFlashItem();
3921 #else // USE_EXPLORATION_BY_KEYS
3922 (void)vidx; // remove a warnings ofr unused var
3923
3924 bool res = ImGui::Selectable(fdi.puVariadicBuffer, vSelected, selectableFlags, ImVec2(-1.0f, h));
3925 #endif // USE_EXPLORATION_BY_KEYS
3926 if (res)
3927 {
3928 if (vInfos.fileType == 'd')
3929 {
3930 bool isSelectingDir=false;
3931 // nav system, selectebale cause open directory or select directory
3932 if (ImGui::GetIO().ConfigFlags & ImGuiConfigFlags_NavEnableKeyboard)
3933 {
3934 if (fdi.puDLGDirectoryMode) // directory chooser
3935 {
3936 fdi.SelectFileName(prFileDialogInternal, vInfos);
3937 }
3938 else
3939 {
3940 fdi.puPathClicked = fdi.SelectDirectory(vInfos);
3941 isSelectingDir=true;
3942 }
3943 }
3944 else // no nav system => classic behavior
3945 {
3946 if (DOUBLE_CLICKED) // 0 -> left mouse button double click
3947 {
3948 isSelectingDir=true;
3949 fdi.puPathClicked = fdi.SelectDirectory(vInfos);
3950 }
3951 else if (fdi.puDLGDirectoryMode) // directory chooser
3952 {
3953 fdi.SelectFileName(prFileDialogInternal, vInfos);
3954 }
3955 }
3956
3957 return isSelectingDir; // needToBreakTheloop
3958 }
3959 else
3960 {
3961 if (DOUBLE_CLICKED) {
3962 fdi.SelectFileName(prFileDialogInternal, vInfos);
3963 prFileDialogInternal.puIsOk = true;
3964 return 2;
3965 } else {
3966 fdi.SelectFileName(prFileDialogInternal, vInfos);
3967 if (prFileDialogInternal.puDLGselFun!=NULL) {
3968 std::string argPath;
3969 for (auto& i: GetSelection()) {
3970 argPath=i.second;
3971 }
3972 if (!argPath.empty()) {
3973 prFileDialogInternal.puDLGselFun(argPath.c_str());
3974 }
3975 }
3976 }
3977 }
3978 }
3979
3980 return 0;
3981 }
3982
3983 void IGFD::FileDialog::prBeginFileColorIconStyle(const FileInfos& vFileInfos, bool& vOutShowColor, std::string& vOutStr, ImFont** vOutFont)
3984 {
3985 vOutStr.clear();
3986 vOutShowColor = false;
3987
3988 if (vFileInfos.fileStyle.use_count()) //-V807 //-V522
3989 {
3990 vOutShowColor = true;
3991
3992 *vOutFont = vFileInfos.fileStyle->font;
3993 }
3994
3995 if (vOutShowColor && !vFileInfos.fileStyle->icon.empty()) vOutStr = vFileInfos.fileStyle->icon;
3996 else if (vFileInfos.fileType == 'd') vOutStr = dirEntryString;
3997 else if (vFileInfos.fileType == 'l') vOutStr = linkEntryString;
3998 else if (vFileInfos.fileType == 'f') vOutStr = fileEntryString;
3999
4000 vOutStr += " " + vFileInfos.fileNameExt;
4001
4002 if (vOutShowColor)
4003 ImGui::PushStyleColor(ImGuiCol_Text, vFileInfos.fileStyle->color);
4004 if (*vOutFont)
4005 ImGui::PushFont(*vOutFont);
4006 }
4007
4008 void IGFD::FileDialog::prEndFileColorIconStyle(const bool& vShowColor, ImFont* vFont)
4009 {
4010 if (vFont)
4011 ImGui::PopFont();
4012 if (vShowColor)
4013 ImGui::PopStyleColor();
4014 }
4015
4016 bool IGFD::FileDialog::prDrawFileListView(ImVec2 vSize)
4017 {
4018 bool escape = false;
4019 auto& fdi = prFileDialogInternal.puFileManager;
4020
4021 ImGui::PushID(this);
4022
4023 static ImGuiTableFlags flags = ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_RowBg |
4024 ImGuiTableFlags_Hideable | ImGuiTableFlags_ScrollY |
4025 ImGuiTableFlags_NoHostExtendY
4026 #ifndef USE_CUSTOM_SORTING_ICON
4027 | ImGuiTableFlags_Sortable
4028 #endif // USE_CUSTOM_SORTING_ICON
4029 ;
4030 auto listViewID = ImGui::GetID("##FileDialog_fileTable");
4031 if (ImGui::BeginTableEx("##FileDialog_fileTable", listViewID, 4, flags, vSize, 0.0f)) //-V112
4032 {
4033 ImGui::TableSetupScrollFreeze(0, 1); // Make header always visible
4034 ImGui::TableSetupColumn(fdi.puHeaderFileName.c_str(), ImGuiTableColumnFlags_WidthStretch, -1, 0);
4035 ImGui::TableSetupColumn(fdi.puHeaderFileType.c_str(), ImGuiTableColumnFlags_WidthFixed |
4036 ((prFileDialogInternal.puDLGflags & ImGuiFileDialogFlags_HideColumnType) ? ImGuiTableColumnFlags_DefaultHide : 0), -1, 1);
4037 ImGui::TableSetupColumn(fdi.puHeaderFileSize.c_str(), ImGuiTableColumnFlags_WidthFixed |
4038 ((prFileDialogInternal.puDLGflags & ImGuiFileDialogFlags_HideColumnSize) ? ImGuiTableColumnFlags_DefaultHide : 0), -1, 2);
4039 ImGui::TableSetupColumn(fdi.puHeaderFileDate.c_str(), ImGuiTableColumnFlags_WidthFixed |
4040 ((prFileDialogInternal.puDLGflags & ImGuiFileDialogFlags_HideColumnDate) ? ImGuiTableColumnFlags_DefaultHide : 0), -1, 3);
4041
4042 #ifndef USE_CUSTOM_SORTING_ICON
4043 // Sort our data if sort specs have been changed!
4044 if (ImGuiTableSortSpecs* sorts_specs = ImGui::TableGetSortSpecs())
4045 {
4046 if (sorts_specs->SpecsDirty && !fdi.IsFileListEmpty())
4047 {
4048 if (sorts_specs->Specs->ColumnUserID == 0)
4049 fdi.SortFields(prFileDialogInternal, IGFD::FileManager::SortingFieldEnum::FIELD_FILENAME, true);
4050 else if (sorts_specs->Specs->ColumnUserID == 1)
4051 fdi.SortFields(prFileDialogInternal, IGFD::FileManager::SortingFieldEnum::FIELD_TYPE, true);
4052 else if (sorts_specs->Specs->ColumnUserID == 2)
4053 fdi.SortFields(prFileDialogInternal, IGFD::FileManager::SortingFieldEnum::FIELD_SIZE, true);
4054 else //if (sorts_specs->Specs->ColumnUserID == 3) => alwayd true for the moment, to uncomment if we add a fourth column
4055 fdi.SortFields(prFileDialogInternal, IGFD::FileManager::SortingFieldEnum::FIELD_DATE, true);
4056
4057 sorts_specs->SpecsDirty = false;
4058 }
4059 }
4060
4061 ImGui::TableHeadersRow();
4062 #else // USE_CUSTOM_SORTING_ICON
4063 ImGui::TableNextRow(ImGuiTableRowFlags_Headers);
4064 for (int column = 0; column < 4; column++) //-V112
4065 {
4066 ImGui::TableSetColumnIndex(column);
4067 const char* column_name = ImGui::TableGetColumnName(column); // Retrieve name passed to TableSetupColumn()
4068 ImGui::PushID(column);
4069 ImGui::TableHeader(column_name);
4070 ImGui::PopID();
4071 if (ImGui::IsItemClicked())
4072 {
4073 if (column == 0)
4074 fdi.SortFields(prFileDialogInternal, IGFD::FileManager::SortingFieldEnum::FIELD_FILENAME, true);
4075 else if (column == 1)
4076 fdi.SortFields(prFileDialogInternal, IGFD::FileManager::SortingFieldEnum::FIELD_TYPE, true);
4077 else if (column == 2)
4078 fdi.SortFields(prFileDialogInternal, IGFD::FileManager::SortingFieldEnum::FIELD_SIZE, true);
4079 else //if (column == 3) => alwayd true for the moment, to uncomment if we add a fourth column
4080 fdi.SortFields(prFileDialogInternal, IGFD::FileManager::SortingFieldEnum::FIELD_DATE, true);
4081 }
4082 }
4083 #endif // USE_CUSTOM_SORTING_ICON
4084 if (!fdi.IsFilteredListEmpty())
4085 {
4086 std::string _str;
4087 ImFont* _font = nullptr;
4088 bool _showColor = false;
4089
4090 prFileListClipper.Begin((int)fdi.GetFilteredListSize(), ImGui::GetTextLineHeightWithSpacing());
4091 while (prFileListClipper.Step())
4092 {
4093 for (int i = prFileListClipper.DisplayStart; i < prFileListClipper.DisplayEnd; i++)
4094 {
4095 if (i < 0) continue;
4096
4097 const FileInfos& infos = fdi.GetFilteredFileAt((size_t)i);
4098 if (!infos.isValid)
4099 continue;
4100
4101 prBeginFileColorIconStyle(infos, _showColor, _str, &_font);
4102
4103 bool selected = fdi.IsFileNameSelected(infos.fileNameExt); // found
4104
4105 ImGui::TableNextRow();
4106
4107 int needToBreakTheloop = false;
4108
4109 if (ImGui::TableNextColumn()) // file name
4110 {
4111 // TODO BUG?!?!?!
4112 // YES BUG
4113 needToBreakTheloop = prSelectableItem(i, infos, selected, "%s", _str.c_str());
4114 if (needToBreakTheloop==2) escape=true;
4115 }
4116 if (ImGui::TableNextColumn()) // file type
4117 {
4118 ImGui::Text("%s", infos.fileExt.c_str());
4119 }
4120 if (ImGui::TableNextColumn()) // file size
4121 {
4122 if (infos.fileType != 'd')
4123 {
4124 ImGui::Text("%s ", infos.formatedFileSize.c_str());
4125 }
4126 else
4127 {
4128 ImGui::Text("%s","");
4129 }
4130 }
4131 if (ImGui::TableNextColumn()) // file date + time
4132 {
4133 ImGui::Text("%s", infos.fileModifDate.c_str());
4134 }
4135
4136 prEndFileColorIconStyle(_showColor, _font);
4137
4138 if (needToBreakTheloop==1)
4139 break;
4140 }
4141 }
4142 prFileListClipper.End();
4143 }
4144
4145 #ifdef USE_EXPLORATION_BY_KEYS
4146 if (!fdi.puInputPathActivated)
4147 {
4148 prLocateByInputKey(prFileDialogInternal);
4149 prExploreWithkeys(prFileDialogInternal, listViewID);
4150 }
4151 #endif // USE_EXPLORATION_BY_KEYS
4152
4153 ImGuiContext& g = *GImGui;
4154 if (g.LastActiveId - 1 == listViewID || g.LastActiveId == listViewID)
4155 {
4156 prFileDialogInternal.puFileListViewIsActive = true;
4157 }
4158
4159 ImGui::EndTable();
4160 }
4161
4162 ImGui::PopID();
4163 return escape;
4164 }
4165
4166 #ifdef USE_THUMBNAILS
4167 void IGFD::FileDialog::prDrawThumbnailsListView(ImVec2 vSize)
4168 {
4169 auto& fdi = prFileDialogInternal.puFileManager;
4170
4171 ImGui::PushID(this);
4172
4173 static ImGuiTableFlags flags = ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_RowBg |
4174 ImGuiTableFlags_Hideable | ImGuiTableFlags_ScrollY |
4175 ImGuiTableFlags_NoHostExtendY
4176 #ifndef USE_CUSTOM_SORTING_ICON
4177 | ImGuiTableFlags_Sortable
4178 #endif // USE_CUSTOM_SORTING_ICON
4179 ;
4180 auto listViewID = ImGui::GetID("##FileDialog_fileTable");
4181 if (ImGui::BeginTableEx("##FileDialog_fileTable", listViewID, 5, flags, vSize, 0.0f))
4182 {
4183 ImGui::TableSetupScrollFreeze(0, 1); // Make header always visible
4184 ImGui::TableSetupColumn(fdi.puHeaderFileName.c_str(), ImGuiTableColumnFlags_WidthStretch, -1, 0);
4185 ImGui::TableSetupColumn(fdi.puHeaderFileType.c_str(), ImGuiTableColumnFlags_WidthFixed |
4186 ((prFileDialogInternal.puDLGflags & ImGuiFileDialogFlags_HideColumnType) ? ImGuiTableColumnFlags_DefaultHide : 0), -1, 1);
4187 ImGui::TableSetupColumn(fdi.puHeaderFileSize.c_str(), ImGuiTableColumnFlags_WidthFixed |
4188 ((prFileDialogInternal.puDLGflags & ImGuiFileDialogFlags_HideColumnSize) ? ImGuiTableColumnFlags_DefaultHide : 0), -1, 2);
4189 ImGui::TableSetupColumn(fdi.puHeaderFileDate.c_str(), ImGuiTableColumnFlags_WidthFixed |
4190 ((prFileDialogInternal.puDLGflags & ImGuiFileDialogFlags_HideColumnDate) ? ImGuiTableColumnFlags_DefaultHide : 0), -1, 3);
4191 // not needed to have an option for hide the thumbnails since this is why this view is used
4192 ImGui::TableSetupColumn(fdi.puHeaderFileThumbnails.c_str(), ImGuiTableColumnFlags_WidthFixed, -1, 4); //-V112
4193
4194 #ifndef USE_CUSTOM_SORTING_ICON
4195 // Sort our data if sort specs have been changed!
4196 if (ImGuiTableSortSpecs* sorts_specs = ImGui::TableGetSortSpecs())
4197 {
4198 if (sorts_specs->SpecsDirty && !fdi.IsFileListEmpty())
4199 {
4200 if (sorts_specs->Specs->ColumnUserID == 0)
4201 fdi.SortFields(prFileDialogInternal, IGFD::FileManager::SortingFieldEnum::FIELD_FILENAME, true);
4202 else if (sorts_specs->Specs->ColumnUserID == 1)
4203 fdi.SortFields(prFileDialogInternal, IGFD::FileManager::SortingFieldEnum::FIELD_TYPE, true);
4204 else if (sorts_specs->Specs->ColumnUserID == 2)
4205 fdi.SortFields(prFileDialogInternal, IGFD::FileManager::SortingFieldEnum::FIELD_SIZE, true);
4206 else if (sorts_specs->Specs->ColumnUserID == 3)
4207 fdi.SortFields(prFileDialogInternal, IGFD::FileManager::SortingFieldEnum::FIELD_DATE, true);
4208 else // if (sorts_specs->Specs->ColumnUserID == 4) = > always true for the moment, to uncomment if we add another column
4209 fdi.SortFields(prFileDialogInternal, IGFD::FileManager::SortingFieldEnum::FIELD_THUMBNAILS, true);
4210 sorts_specs->SpecsDirty = false;
4211 }
4212 }
4213
4214 ImGui::TableHeadersRow();
4215 #else // USE_CUSTOM_SORTING_ICON
4216 ImGui::TableNextRow(ImGuiTableRowFlags_Headers);
4217 for (int column = 0; column < 5; column++)
4218 {
4219 ImGui::TableSetColumnIndex(column);
4220 const char* column_name = ImGui::TableGetColumnName(column); // Retrieve name passed to TableSetupColumn()
4221 ImGui::PushID(column);
4222 ImGui::TableHeader(column_name);
4223 ImGui::PopID();
4224 if (ImGui::IsItemClicked())
4225 {
4226 if (column == 0)
4227 fdi.SortFields(prFileDialogInternal, IGFD::FileManager::SortingFieldEnum::FIELD_FILENAME, true);
4228 else if (column == 1)
4229 fdi.SortFields(prFileDialogInternal, IGFD::FileManager::SortingFieldEnum::FIELD_TYPE, true);
4230 else if (column == 2)
4231 fdi.SortFields(prFileDialogInternal, IGFD::FileManager::SortingFieldEnum::FIELD_SIZE, true);
4232 else if (column == 3)
4233 fdi.SortFields(prFileDialogInternal, IGFD::FileManager::SortingFieldEnum::FIELD_DATE, true);
4234 else // if (column == 4) = > always true for the moment, to uncomment if we add another column
4235 fdi.SortFields(prFileDialogInternal, IGFD::FileManager::SortingFieldEnum::FIELD_THUMBNAILS, true);
4236 }
4237 }
4238 #endif // USE_CUSTOM_SORTING_ICON
4239 if (!fdi.IsFilteredListEmpty())
4240 {
4241 std::string _str;
4242 ImFont* _font = nullptr;
4243 bool _showColor = false;
4244
4245 ImGuiContext& g = *GImGui;
4246 const float itemHeight = ImMax(g.FontSize, DisplayMode_ThumbailsList_ImageHeight) + g.Style.ItemSpacing.y;
4247
4248 prFileListClipper.Begin((int)fdi.GetFilteredListSize(), itemHeight);
4249 while (prFileListClipper.Step())
4250 {
4251 for (int i = prFileListClipper.DisplayStart; i < prFileListClipper.DisplayEnd; i++)
4252 {
4253 if (i < 0) continue;
4254
4255 auto infos = fdi.GetFilteredFileAt((size_t)i);
4256 if (!infos.use_count())
4257 continue;
4258
4259 prBeginFileColorIconStyle(infos, _showColor, _str, &_font);
4260
4261 bool selected = fdi.IsFileNameSelected(infos->fileNameExt); // found
4262
4263 ImGui::TableNextRow();
4264
4265 bool needToBreakTheloop = false;
4266
4267 if (ImGui::TableNextColumn()) // file name
4268 {
4269 needToBreakTheloop = prSelectableItem(i, infos, selected, _str.c_str());
4270 }
4271 if (ImGui::TableNextColumn()) // file type
4272 {
4273 ImGui::Text("%s", infos->fileExt.c_str());
4274 }
4275 if (ImGui::TableNextColumn()) // file size
4276 {
4277 if (infos->fileType != 'd')
4278 {
4279 ImGui::Text("%s ", infos->formatedFileSize.c_str());
4280 }
4281 else
4282 {
4283 ImGui::Text("");
4284 }
4285 }
4286 if (ImGui::TableNextColumn()) // file date + time
4287 {
4288 ImGui::Text("%s", infos->fileModifDate.c_str());
4289 }
4290 if (ImGui::TableNextColumn()) // file thumbnails
4291 {
4292 auto th = &infos->thumbnailInfo;
4293
4294 if (!th->isLoadingOrLoaded)
4295 {
4296 prAddThumbnailToLoad(infos);
4297 }
4298 if (th->isReadyToDisplay &&
4299 th->textureID)
4300 {
4301 ImGui::Image((ImTextureID)th->textureID,
4302 ImVec2((float)th->textureWidth,
4303 (float)th->textureHeight));
4304 }
4305 }
4306
4307 prEndFileColorIconStyle(_showColor, _font);
4308
4309 if (needToBreakTheloop)
4310 break;
4311 }
4312 }
4313 prFileListClipper.End();
4314 }
4315
4316 #ifdef USE_EXPLORATION_BY_KEYS
4317 if (!fdi.puInputPathActivated)
4318 {
4319 prLocateByInputKey(prFileDialogInternal);
4320 prExploreWithkeys(prFileDialogInternal, listViewID);
4321 }
4322 #endif // USE_EXPLORATION_BY_KEYS
4323
4324 ImGuiContext& g = *GImGui;
4325 if (g.LastActiveId - 1 == listViewID || g.LastActiveId == listViewID)
4326 {
4327 prFileDialogInternal.puFileListViewIsActive = true;
4328 }
4329
4330 ImGui::EndTable();
4331 }
4332
4333 ImGui::PopID();
4334 }
4335
4336 void IGFD::FileDialog::prDrawThumbnailsGridView(ImVec2 vSize)
4337 {
4338 if (ImGui::BeginChild("##thumbnailsGridsFiles", vSize))
4339 {
4340 // todo
4341 }
4342
4343 ImGui::EndChild();
4344 }
4345
4346 #endif
4347
4348 void IGFD::FileDialog::prDrawSidePane(float vHeight)
4349 {
4350 ImGui::SameLine();
4351
4352 ImGui::BeginChild("##FileTypes", ImVec2(0, vHeight));
4353
4354 prFileDialogInternal.puDLGoptionsPane(
4355 prFileDialogInternal.puFilterManager.GetSelectedFilter().filter.c_str(),
4356 prFileDialogInternal.puDLGuserDatas, &prFileDialogInternal.puCanWeContinue);
4357
4358 ImGui::EndChild();
4359 }
4360
4361 void IGFD::FileDialog::Close()
4362 {
4363 prFileDialogInternal.puDLGkey.clear();
4364 prFileDialogInternal.puShowDialog = false;
4365 }
4366
4367 bool IGFD::FileDialog::WasOpenedThisFrame(const std::string& vKey) const
4368 {
4369 bool res = prFileDialogInternal.puShowDialog && prFileDialogInternal.puDLGkey == vKey;
4370 if (res)
4371 {
4372 ImGuiContext& g = *GImGui;
4373 res &= prFileDialogInternal.puLastImGuiFrameCount == g.FrameCount; // return true if a dialog was displayed in this frame
4374 }
4375 return res;
4376 }
4377
4378 bool IGFD::FileDialog::WasOpenedThisFrame() const
4379 {
4380 bool res = prFileDialogInternal.puShowDialog;
4381 if (res)
4382 {
4383 ImGuiContext& g = *GImGui;
4384 res &= prFileDialogInternal.puLastImGuiFrameCount == g.FrameCount; // return true if a dialog was displayed in this frame
4385 }
4386 return res;
4387 }
4388
4389 bool IGFD::FileDialog::IsOpened(const std::string& vKey) const
4390 {
4391 return (prFileDialogInternal.puShowDialog && prFileDialogInternal.puDLGkey == vKey);
4392 }
4393
4394 bool IGFD::FileDialog::IsOpened() const
4395 {
4396 return prFileDialogInternal.puShowDialog;
4397 }
4398
4399 std::string IGFD::FileDialog::GetOpenedKey() const
4400 {
4401 if (prFileDialogInternal.puShowDialog)
4402 return prFileDialogInternal.puDLGkey;
4403 return "";
4404 }
4405
4406 std::string IGFD::FileDialog::GetFilePathName()
4407 {
4408 return prFileDialogInternal.puFileManager.GetResultingFilePathName(prFileDialogInternal);
4409 }
4410
4411 std::string IGFD::FileDialog::GetCurrentPath()
4412 {
4413 return prFileDialogInternal.puFileManager.GetResultingPath();
4414 }
4415
4416 std::string IGFD::FileDialog::GetCurrentFileName()
4417 {
4418 return prFileDialogInternal.puFileManager.GetResultingFileName(prFileDialogInternal);
4419 }
4420
4421 std::string IGFD::FileDialog::GetCurrentFilter()
4422 {
4423 return prFileDialogInternal.puFilterManager.GetSelectedFilter().filter;
4424 }
4425
4426 std::map<std::string, std::string> IGFD::FileDialog::GetSelection()
4427 {
4428 return prFileDialogInternal.puFileManager.GetResultingSelection();
4429 }
4430
4431 UserDatas IGFD::FileDialog::GetUserDatas() const
4432 {
4433 return prFileDialogInternal.puDLGuserDatas;
4434 }
4435
4436 bool IGFD::FileDialog::IsOk() const
4437 {
4438 return prFileDialogInternal.puIsOk;
4439 }
4440
4441 void IGFD::FileDialog::SetFileStyle(const IGFD_FileStyleFlags& vFlags, const char* vCriteria, const FileStyle& vInfos)
4442 {
4443 prFileDialogInternal.puFilterManager.SetFileStyle(vFlags, vCriteria, vInfos);
4444 }
4445
4446 void IGFD::FileDialog::SetFileStyle(const IGFD_FileStyleFlags& vFlags, const char* vCriteria, const ImVec4& vColor, const std::string& vIcon, ImFont* vFont)
4447 {
4448 prFileDialogInternal.puFilterManager.SetFileStyle(vFlags, vCriteria, vColor, vIcon, vFont);
4449 }
4450
4451 bool IGFD::FileDialog::GetFileStyle(const IGFD_FileStyleFlags& vFlags, const std::string& vCriteria, ImVec4* vOutColor, std::string* vOutIcon, ImFont **vOutFont)
4452 {
4453 return prFileDialogInternal.puFilterManager.GetFileStyle(vFlags, vCriteria, vOutColor, vOutIcon, vOutFont);
4454 }
4455
4456 void IGFD::FileDialog::ClearFilesStyle()
4457 {
4458 prFileDialogInternal.puFilterManager.ClearFilesStyle();
4459 }
4460
4461 void IGFD::FileDialog::SetLocales(const int& vLocaleCategory, const std::string& vLocaleBegin, const std::string& vLocaleEnd)
4462 {
4463 prFileDialogInternal.puUseCustomLocale = true;
4464 prFileDialogInternal.puLocaleBegin = vLocaleBegin;
4465 prFileDialogInternal.puLocaleEnd = vLocaleEnd;
4466 }
4467
4468 //////////////////////////////////////////////////////////////////////////////
4469 //// OVERWRITE DIALOG ////////////////////////////////////////////////////////
4470 //////////////////////////////////////////////////////////////////////////////
4471
4472 bool IGFD::FileDialog::prConfirm_Or_OpenOverWriteFileDialog_IfNeeded(bool vLastAction, ImGuiWindowFlags vFlags)
4473 {
4474 // if confirmation => return true for confirm the overwrite et quit the dialog
4475 // if cancel => return false && set IsOk to false for keep inside the dialog
4476
4477 // if IsOk == false => return false for quit the dialog
4478 if (!prFileDialogInternal.puIsOk && vLastAction)
4479 {
4480 QuitFrame();
4481 return true;
4482 }
4483
4484 // if IsOk == true && no check of overwrite => return true for confirm the dialog
4485 if (prFileDialogInternal.puIsOk && vLastAction && !(prFileDialogInternal.puDLGflags & ImGuiFileDialogFlags_ConfirmOverwrite))
4486 {
4487 QuitFrame();
4488 return true;
4489 }
4490
4491 // if IsOk == true && check of overwrite => return false and show confirm to overwrite dialog
4492 if ((prFileDialogInternal.puOkResultToConfirm || (prFileDialogInternal.puIsOk && vLastAction)) &&
4493 (prFileDialogInternal.puDLGflags & ImGuiFileDialogFlags_ConfirmOverwrite))
4494 {
4495 if (prFileDialogInternal.puIsOk) // catched only one time
4496 {
4497 if (!prFileDialogInternal.puFileManager.IsFileExist(GetFilePathName(),prFileDialogInternal.puFilterManager.GetSelectedFilter().firstFilter)) // not existing => quit dialog
4498 {
4499 QuitFrame();
4500 return true;
4501 }
4502 else // existing => confirm dialog to open
4503 {
4504 prFileDialogInternal.puIsOk = false;
4505 prFileDialogInternal.puOkResultToConfirm = true;
4506 }
4507 }
4508
4509 std::string name = std::string(OverWriteDialogTitleString) + "##" + prFileDialogInternal.puDLGtitle + prFileDialogInternal.puDLGkey + "OverWriteDialog";
4510
4511 bool res = false;
4512
4513 ImGui::OpenPopup(name.c_str());
4514 if (ImGui::BeginPopupModal(name.c_str(), (bool*)0,
4515 vFlags | ImGuiWindowFlags_AlwaysAutoResize |
4516 ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove))
4517 {
4518 ImGui::SetWindowPos(prFileDialogInternal.puDialogCenterPos - ImGui::GetWindowSize() * 0.5f); // next frame needed for GetWindowSize to work
4519
4520 ImGui::Text("%s", OverWriteDialogMessageString);
4521
4522 if (IMGUI_BUTTON(OverWriteDialogConfirmButtonString))
4523 {
4524 prFileDialogInternal.puOkResultToConfirm = false;
4525 prFileDialogInternal.puIsOk = true;
4526 res = true;
4527 ImGui::CloseCurrentPopup();
4528 }
4529
4530 ImGui::SameLine();
4531
4532 if (IMGUI_BUTTON(OverWriteDialogCancelButtonString))
4533 {
4534 prFileDialogInternal.puOkResultToConfirm = false;
4535 prFileDialogInternal.puIsOk = false;
4536 res = false;
4537 ImGui::CloseCurrentPopup();
4538 }
4539
4540 ImGui::EndPopup();
4541 }
4542
4543 if (res)
4544 {
4545 QuitFrame();
4546 }
4547 return res;
4548 }
4549
4550 return false;
4551 }
4552 }
4553
4554 #endif // __cplusplus
4555
4556 /////////////////////////////////////////////////////////////////
4557 ///// C Interface ///////////////////////////////////////////////
4558 /////////////////////////////////////////////////////////////////
4559
4560 // Return an initialized IGFD_Selection_Pair
4561 IMGUIFILEDIALOG_API IGFD_Selection_Pair IGFD_Selection_Pair_Get(void)
4562 {
4563 IGFD_Selection_Pair res = {};
4564 res.fileName = nullptr;
4565 res.filePathName = nullptr;
4566 return res;
4567 }
4568
4569 // destroy only the content of vSelection_Pair
4570 IMGUIFILEDIALOG_API void IGFD_Selection_Pair_DestroyContent(IGFD_Selection_Pair* vSelection_Pair)
4571 {
4572 if (vSelection_Pair)
4573 {
4574 delete[] vSelection_Pair->fileName;
4575 delete[] vSelection_Pair->filePathName;
4576 }
4577 }
4578
4579 // Return an initialized IGFD_Selection
4580 IMGUIFILEDIALOG_API IGFD_Selection IGFD_Selection_Get(void)
4581 {
4582 return { nullptr, 0U };
4583 }
4584
4585 // destroy only the content of vSelection
4586 IMGUIFILEDIALOG_API void IGFD_Selection_DestroyContent(IGFD_Selection* vSelection)
4587 {
4588 if (vSelection)
4589 {
4590 if (vSelection->table)
4591 {
4592 for (size_t i = 0U; i < vSelection->count; i++)
4593 {
4594 IGFD_Selection_Pair_DestroyContent(&vSelection->table[i]);
4595 }
4596 delete[] vSelection->table;
4597 }
4598 vSelection->count = 0U;
4599 }
4600 }
4601
4602 // create an instance of ImGuiFileDialog
4603 IMGUIFILEDIALOG_API ImGuiFileDialog* IGFD_Create(void)
4604 {
4605 return new ImGuiFileDialog();
4606 }
4607
4608 // destroy the instance of ImGuiFileDialog
4609 IMGUIFILEDIALOG_API void IGFD_Destroy(ImGuiFileDialog* vContext)
4610 {
4611 if (vContext)
4612 {
4613 delete vContext;
4614 vContext = nullptr;
4615 }
4616 }
4617
4618 // standard dialog
4619 IMGUIFILEDIALOG_API void IGFD_OpenDialog(
4620 ImGuiFileDialog* vContext,
4621 const char* vKey,
4622 const char* vTitle,
4623 const char* vFilters,
4624 const char* vPath,
4625 const char* vFileName,
4626 const int vCountSelectionMax,
4627 void* vUserDatas,
4628 ImGuiFileDialogFlags flags)
4629 {
4630 if (vContext)
4631 {
4632 vContext->OpenDialog(
4633 vKey, vTitle, vFilters, vPath, vFileName,
4634 vCountSelectionMax, vUserDatas, flags);
4635 }
4636 }
4637
4638 IMGUIFILEDIALOG_API void IGFD_OpenDialog2(
4639 ImGuiFileDialog* vContext,
4640 const char* vKey,
4641 const char* vTitle,
4642 const char* vFilters,
4643 const char* vFilePathName,
4644 const int vCountSelectionMax,
4645 void* vUserDatas,
4646 ImGuiFileDialogFlags flags)
4647 {
4648 if (vContext)
4649 {
4650 vContext->OpenDialog(
4651 vKey, vTitle, vFilters, vFilePathName,
4652 vCountSelectionMax, vUserDatas, flags);
4653 }
4654 }
4655
4656 IMGUIFILEDIALOG_API void IGFD_OpenPaneDialog(
4657 ImGuiFileDialog* vContext,
4658 const char* vKey,
4659 const char* vTitle,
4660 const char* vFilters,
4661 const char* vPath,
4662 const char* vFileName,
4663 IGFD_PaneFun vSidePane,
4664 const float vSidePaneWidth,
4665 const int vCountSelectionMax,
4666 void* vUserDatas,
4667 ImGuiFileDialogFlags flags)
4668 {
4669 if (vContext)
4670 {
4671 vContext->OpenDialog(
4672 vKey, vTitle, vFilters,
4673 vPath, vFileName,
4674 vSidePane, vSidePaneWidth,
4675 vCountSelectionMax, vUserDatas, flags);
4676 }
4677 }
4678
4679 IMGUIFILEDIALOG_API void IGFD_OpenPaneDialog2(
4680 ImGuiFileDialog* vContext,
4681 const char* vKey,
4682 const char* vTitle,
4683 const char* vFilters,
4684 const char* vFilePathName,
4685 IGFD_PaneFun vSidePane,
4686 const float vSidePaneWidth,
4687 const int vCountSelectionMax,
4688 void* vUserDatas,
4689 ImGuiFileDialogFlags flags)
4690 {
4691 if (vContext)
4692 {
4693 vContext->OpenDialog(
4694 vKey, vTitle, vFilters,
4695 vFilePathName,
4696 vSidePane, vSidePaneWidth,
4697 vCountSelectionMax, vUserDatas, flags);
4698 }
4699 }
4700
4701 // modal dialog
4702 IMGUIFILEDIALOG_API void IGFD_OpenModal(
4703 ImGuiFileDialog* vContext,
4704 const char* vKey,
4705 const char* vTitle,
4706 const char* vFilters,
4707 const char* vPath,
4708 const char* vFileName,
4709 const int vCountSelectionMax,
4710 void* vUserDatas,
4711 ImGuiFileDialogFlags flags)
4712 {
4713 if (vContext)
4714 {
4715 vContext->OpenModal(
4716 vKey, vTitle, vFilters, vPath, vFileName,
4717 vCountSelectionMax, vUserDatas, flags);
4718 }
4719 }
4720
4721 IMGUIFILEDIALOG_API void IGFD_OpenModal2(
4722 ImGuiFileDialog* vContext,
4723 const char* vKey,
4724 const char* vTitle,
4725 const char* vFilters,
4726 const char* vFilePathName,
4727 const int vCountSelectionMax,
4728 void* vUserDatas,
4729 ImGuiFileDialogFlags flags)
4730 {
4731 if (vContext)
4732 {
4733 vContext->OpenModal(
4734 vKey, vTitle, vFilters, vFilePathName,
4735 vCountSelectionMax, vUserDatas, flags);
4736 }
4737 }
4738
4739 IMGUIFILEDIALOG_API void IGFD_OpenPaneModal(
4740 ImGuiFileDialog* vContext,
4741 const char* vKey,
4742 const char* vTitle,
4743 const char* vFilters,
4744 const char* vPath,
4745 const char* vFileName,
4746 IGFD_PaneFun vSidePane,
4747 const float vSidePaneWidth,
4748 const int vCountSelectionMax,
4749 void* vUserDatas,
4750 ImGuiFileDialogFlags flags)
4751 {
4752 if (vContext)
4753 {
4754 vContext->OpenModal(
4755 vKey, vTitle, vFilters,
4756 vPath, vFileName,
4757 vSidePane, vSidePaneWidth,
4758 vCountSelectionMax, vUserDatas, flags);
4759 }
4760 }
4761
4762 IMGUIFILEDIALOG_API void IGFD_OpenPaneModal2(
4763 ImGuiFileDialog* vContext,
4764 const char* vKey,
4765 const char* vTitle,
4766 const char* vFilters,
4767 const char* vFilePathName,
4768 IGFD_PaneFun vSidePane,
4769 const float vSidePaneWidth,
4770 const int vCountSelectionMax,
4771 void* vUserDatas,
4772 ImGuiFileDialogFlags flags)
4773 {
4774 if (vContext)
4775 {
4776 vContext->OpenModal(
4777 vKey, vTitle, vFilters,
4778 vFilePathName,
4779 vSidePane, vSidePaneWidth,
4780 vCountSelectionMax, vUserDatas, flags);
4781 }
4782 }
4783
4784 IMGUIFILEDIALOG_API bool IGFD_DisplayDialog(ImGuiFileDialog* vContext,
4785 const char* vKey, ImGuiWindowFlags vFlags, ImVec2 vMinSize, ImVec2 vMaxSize)
4786 {
4787 if (vContext)
4788 {
4789 return vContext->Display(vKey, vFlags, vMinSize, vMaxSize);
4790 }
4791
4792 return false;
4793 }
4794
4795 IMGUIFILEDIALOG_API void IGFD_CloseDialog(ImGuiFileDialog* vContext)
4796 {
4797 if (vContext)
4798 {
4799 vContext->Close();
4800 }
4801 }
4802
4803 IMGUIFILEDIALOG_API bool IGFD_IsOk(ImGuiFileDialog* vContext)
4804 {
4805 if (vContext)
4806 {
4807 return vContext->IsOk();
4808 }
4809
4810 return false;
4811 }
4812
4813 IMGUIFILEDIALOG_API bool IGFD_WasKeyOpenedThisFrame(ImGuiFileDialog* vContext,
4814 const char* vKey)
4815 {
4816 if (vContext)
4817 {
4818 vContext->WasOpenedThisFrame(vKey);
4819 }
4820
4821 return false;
4822 }
4823
4824 IMGUIFILEDIALOG_API bool IGFD_WasOpenedThisFrame(ImGuiFileDialog* vContext)
4825 {
4826 if (vContext)
4827 {
4828 vContext->WasOpenedThisFrame();
4829 }
4830
4831 return false;
4832 }
4833
4834 IMGUIFILEDIALOG_API bool IGFD_IsKeyOpened(ImGuiFileDialog* vContext,
4835 const char* vCurrentOpenedKey)
4836 {
4837 if (vContext)
4838 {
4839 vContext->IsOpened(vCurrentOpenedKey);
4840 }
4841
4842 return false;
4843 }
4844
4845 IMGUIFILEDIALOG_API bool IGFD_IsOpened(ImGuiFileDialog* vContext)
4846 {
4847 if (vContext)
4848 {
4849 vContext->IsOpened();
4850 }
4851
4852 return false;
4853 }
4854
4855 IMGUIFILEDIALOG_API IGFD_Selection IGFD_GetSelection(ImGuiFileDialog* vContext)
4856 {
4857 IGFD_Selection res = IGFD_Selection_Get();
4858
4859 if (vContext)
4860 {
4861 auto sel = vContext->GetSelection();
4862 if (!sel.empty())
4863 {
4864 res.count = sel.size();
4865 res.table = new IGFD_Selection_Pair[res.count];
4866
4867 size_t idx = 0U;
4868 for (const auto& s : sel)
4869 {
4870 IGFD_Selection_Pair* pair = res.table + idx++;
4871
4872 // fileNameExt
4873 if (!s.first.empty())
4874 {
4875 size_t siz = s.first.size() + 1U;
4876 pair->fileName = new char[siz];
4877 #ifndef MSVC
4878 strncpy(pair->fileName, s.first.c_str(), siz);
4879 #else
4880 strncpy_s(pair->fileName, siz, s.first.c_str(), siz);
4881 #endif
4882 pair->fileName[siz - 1U] = '\0';
4883 }
4884
4885 // filePathName
4886 if (!s.second.empty())
4887 {
4888 size_t siz = s.first.size() + 1U;
4889 pair->filePathName = new char[siz];
4890 #ifndef MSVC
4891 strncpy(pair->filePathName, s.first.c_str(), siz);
4892 #else
4893 strncpy_s(pair->filePathName, siz, s.first.c_str(), siz);
4894 #endif
4895 pair->filePathName[siz - 1U] = '\0';
4896 }
4897 }
4898
4899 return res;
4900 }
4901 }
4902
4903 return res;
4904 }
4905
4906 IMGUIFILEDIALOG_API char* IGFD_GetFilePathName(ImGuiFileDialog* vContext)
4907 {
4908 char* res = nullptr;
4909
4910 if (vContext)
4911 {
4912 auto s = vContext->GetFilePathName();
4913 if (!s.empty())
4914 {
4915 size_t siz = s.size() + 1U;
4916 res = new char[siz];
4917 #ifndef MSVC
4918 strncpy(res, s.c_str(), siz);
4919 #else
4920 strncpy_s(res, siz, s.c_str(), siz);
4921 #endif
4922 res[siz - 1U] = '\0';
4923 }
4924 }
4925
4926 return res;
4927 }
4928
4929 IMGUIFILEDIALOG_API char* IGFD_GetCurrentFileName(ImGuiFileDialog* vContext)
4930 {
4931 char* res = nullptr;
4932
4933 if (vContext)
4934 {
4935 auto s = vContext->GetCurrentFileName();
4936 if (!s.empty())
4937 {
4938 size_t siz = s.size() + 1U;
4939 res = new char[siz];
4940 #ifndef MSVC
4941 strncpy(res, s.c_str(), siz);
4942 #else
4943 strncpy_s(res, siz, s.c_str(), siz);
4944 #endif
4945 res[siz - 1U] = '\0';
4946 }
4947 }
4948
4949 return res;
4950 }
4951
4952 IMGUIFILEDIALOG_API char* IGFD_GetCurrentPath(ImGuiFileDialog* vContext)
4953 {
4954 char* res = nullptr;
4955
4956 if (vContext)
4957 {
4958 auto s = vContext->GetCurrentPath();
4959 if (!s.empty())
4960 {
4961 size_t siz = s.size() + 1U;
4962 res = new char[siz];
4963 #ifndef MSVC
4964 strncpy(res, s.c_str(), siz);
4965 #else
4966 strncpy_s(res, siz, s.c_str(), siz);
4967 #endif
4968 res[siz - 1U] = '\0';
4969 }
4970 }
4971
4972 return res;
4973 }
4974
4975 IMGUIFILEDIALOG_API char* IGFD_GetCurrentFilter(ImGuiFileDialog* vContext)
4976 {
4977 char* res = nullptr;
4978
4979 if (vContext)
4980 {
4981 auto s = vContext->GetCurrentFilter();
4982 if (!s.empty())
4983 {
4984 size_t siz = s.size() + 1U;
4985 res = new char[siz];
4986 #ifndef MSVC
4987 strncpy(res, s.c_str(), siz);
4988 #else
4989 strncpy_s(res, siz, s.c_str(), siz);
4990 #endif
4991 res[siz - 1U] = '\0';
4992 }
4993 }
4994
4995 return res;
4996 }
4997
4998 IMGUIFILEDIALOG_API void* IGFD_GetUserDatas(ImGuiFileDialog* vContext)
4999 {
5000 if (vContext)
5001 {
5002 return vContext->GetUserDatas();
5003 }
5004
5005 return nullptr;
5006 }
5007
5008 IMGUIFILEDIALOG_API void IGFD_SetFileStyle(ImGuiFileDialog* vContext,
5009 IGFD_FileStyleFlags vFlags, const char* vCriteria, ImVec4 vColor, const char* vIcon, ImFont* vFont) //-V813
5010 {
5011 if (vContext)
5012 {
5013 vContext->SetFileStyle(vFlags, vCriteria, vColor, vIcon, vFont);
5014 }
5015 }
5016
5017 IMGUIFILEDIALOG_API void IGFD_SetFileStyle2(ImGuiFileDialog* vContext,
5018 IGFD_FileStyleFlags vFlags, const char* vCriteria, float vR, float vG, float vB, float vA, const char* vIcon, ImFont* vFont)
5019 {
5020 if (vContext)
5021 {
5022 vContext->SetFileStyle(vFlags, vCriteria, ImVec4(vR, vG, vB, vA), vIcon, vFont);
5023 }
5024 }
5025
5026 IMGUIFILEDIALOG_API bool IGFD_GetFileStyle(ImGuiFileDialog* vContext,
5027 IGFD_FileStyleFlags vFlags, const char* vCriteria, ImVec4* vOutColor, char** vOutIcon, ImFont** vOutFont)
5028 {
5029 if (vContext)
5030 {
5031 std::string icon;
5032 bool res = vContext->GetFileStyle(vFlags, vCriteria, vOutColor, &icon, vOutFont);
5033 if (!icon.empty() && vOutIcon)
5034 {
5035 size_t siz = icon.size() + 1U;
5036 *vOutIcon = new char[siz];
5037 #ifndef MSVC
5038 strncpy(*vOutIcon, icon.c_str(), siz);
5039 #else
5040 strncpy_s(*vOutIcon, siz, icon.c_str(), siz);
5041 #endif
5042 (*vOutIcon)[siz - 1U] = '\0';
5043 }
5044 return res;
5045 }
5046
5047 return false;
5048 }
5049
5050 IMGUIFILEDIALOG_API void IGFD_ClearFilesStyle(ImGuiFileDialog* vContext)
5051 {
5052 if (vContext)
5053 {
5054 vContext->ClearFilesStyle();
5055 }
5056 }
5057
5058 IMGUIFILEDIALOG_API void SetLocales(ImGuiFileDialog* vContext, const int vCategory, const char* vBeginLocale, const char* vEndLocale)
5059 {
5060 if (vContext)
5061 {
5062 vContext->SetLocales(vCategory, (vBeginLocale ? vBeginLocale : ""), (vEndLocale ? vEndLocale : ""));
5063 }
5064 }
5065
5066 #ifdef USE_EXPLORATION_BY_KEYS
5067 IMGUIFILEDIALOG_API void IGFD_SetFlashingAttenuationInSeconds(ImGuiFileDialog* vContext, float vAttenValue)
5068 {
5069 if (vContext)
5070 {
5071 vContext->SetFlashingAttenuationInSeconds(vAttenValue);
5072 }
5073 }
5074 #endif
5075
5076 #ifdef USE_BOOKMARK
5077 IMGUIFILEDIALOG_API char* IGFD_SerializeBookmarks(ImGuiFileDialog* vContext)
5078 {
5079 char* res = nullptr;
5080
5081 if (vContext)
5082 {
5083 auto s = vContext->SerializeBookmarks();
5084 if (!s.empty())
5085 {
5086 size_t siz = s.size() + 1U;
5087 res = new char[siz];
5088 #ifndef MSVC
5089 strncpy(res, s.c_str(), siz);
5090 #else
5091 strncpy_s(res, siz, s.c_str(), siz);
5092 #endif
5093 res[siz - 1U] = '\0';
5094 }
5095 }
5096
5097 return res;
5098 }
5099
5100 IMGUIFILEDIALOG_API void IGFD_DeserializeBookmarks(ImGuiFileDialog* vContext, const char* vBookmarks)
5101 {
5102 if (vContext)
5103 {
5104 vContext->DeserializeBookmarks(vBookmarks);
5105 }
5106 }
5107 #endif
5108
5109 #ifdef USE_THUMBNAILS
5110 IMGUIFILEDIALOG_API void SetCreateThumbnailCallback(ImGuiFileDialog* vContext, const IGFD_CreateThumbnailFun vCreateThumbnailFun)
5111 {
5112 if (vContext)
5113 {
5114 vContext->SetCreateThumbnailCallback(vCreateThumbnailFun);
5115 }
5116 }
5117
5118 IMGUIFILEDIALOG_API void SetDestroyThumbnailCallback(ImGuiFileDialog* vContext, const IGFD_DestroyThumbnailFun vDestroyThumbnailFun)
5119 {
5120 if (vContext)
5121 {
5122 vContext->SetDestroyThumbnailCallback(vDestroyThumbnailFun);
5123 }
5124 }
5125
5126 IMGUIFILEDIALOG_API void ManageGPUThumbnails(ImGuiFileDialog* vContext)
5127 {
5128 if (vContext)
5129 {
5130 vContext->ManageGPUThumbnails();
5131 }
5132 }
5133 #endif // USE_THUMBNAILS