TGUI 1.12
Loading...
Searching...
No Matches
FileDialog.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2026 Bruno Van de Velde (vdv_b@tgui.eu)
5//
6// This software is provided 'as-is', without any express or implied warranty.
7// In no event will the authors be held liable for any damages arising from the use of this software.
8//
9// Permission is granted to anyone to use this software for any purpose,
10// including commercial applications, and to alter it and redistribute it freely,
11// subject to the following restrictions:
12//
13// 1. The origin of this software must not be misrepresented;
14// you must not claim that you wrote the original software.
15// If you use this software in a product, an acknowledgment
16// in the product documentation would be appreciated but is not required.
17//
18// 2. Altered source versions must be plainly marked as such,
19// and must not be misrepresented as being the original software.
20//
21// 3. This notice may not be removed or altered from any source distribution.
22//
24
25#ifndef TGUI_FILE_DIALOG_HPP
26#define TGUI_FILE_DIALOG_HPP
27
28#include <TGUI/Widgets/Label.hpp>
29#include <TGUI/Widgets/Button.hpp>
30#include <TGUI/Widgets/EditBox.hpp>
31#include <TGUI/Widgets/ComboBox.hpp>
32#include <TGUI/Widgets/ListView.hpp>
33#include <TGUI/Widgets/ChildWindow.hpp>
34#include <TGUI/Widgets/Panel.hpp>
35#include <TGUI/Renderers/FileDialogRenderer.hpp>
36#include <TGUI/Filesystem.hpp>
37#include <TGUI/Config.hpp>
38
39#include <tuple>
40
42
43namespace tgui
44{
46
61 class TGUI_API FileDialog : public ChildWindow
62 {
63 public:
64
65 using Ptr = std::shared_ptr<FileDialog>;
66 using ConstPtr = std::shared_ptr<const FileDialog>;
67
68 static constexpr const char StaticWidgetType[] = "FileDialog";
69
77 explicit FileDialog(const char* typeName = StaticWidgetType, bool initRenderer = true);
78
88 TGUI_NODISCARD static FileDialog::Ptr create(const String& title = "Open file", const String& confirmButtonText = "Open", bool allowCreateFolder = false);
89
93 FileDialog(const FileDialog& copy);
94
98 FileDialog(FileDialog&& copy) noexcept;
99
103 FileDialog& operator= (const FileDialog& right);
104
108 FileDialog& operator= (FileDialog&& right) noexcept;
109
117 TGUI_NODISCARD static FileDialog::Ptr copy(const FileDialog::ConstPtr& dialog);
118
123 TGUI_NODISCARD FileDialogRenderer* getSharedRenderer() override;
124 TGUI_NODISCARD const FileDialogRenderer* getSharedRenderer() const override;
125
131 TGUI_NODISCARD FileDialogRenderer* getRenderer() override;
132
150 TGUI_NODISCARD const std::vector<Filesystem::Path>& getSelectedPaths() const;
151
159 void setPath(const String& path);
160
168 void setPath(const Filesystem::Path& path);
169
175 TGUI_NODISCARD const Filesystem::Path& getPath() const;
176
182 void setFilename(const String& filename);
183
193 TGUI_NODISCARD const String& getFilename() const;
194
218 void setFileTypeFilters(const std::vector<std::pair<String, std::vector<String>>>& filters, std::size_t defaultFilterIndex = 0);
219
227 TGUI_NODISCARD const std::vector<std::pair<String, std::vector<String>>>& getFileTypeFilters() const;
228
236 TGUI_NODISCARD std::size_t getFileTypeFiltersIndex() const;
237
243 void setConfirmButtonText(const String& text = "Open");
244
250 TGUI_NODISCARD const String& getConfirmButtonText() const;
251
257 void setCancelButtonText(const String& text = "Cancel");
258
264 TGUI_NODISCARD const String& getCancelButtonText() const;
265
271 void setCreateFolderButtonText(const String& text = "Create Folder");
272
278 TGUI_NODISCARD const String& getCreateFolderButtonText() const;
279
285 void setAllowCreateFolder(bool allowCreateFolder);
286
292 TGUI_NODISCARD bool getAllowCreateFolder() const;
293
299 void setFilenameLabelText(const String& labelText = "Filename:");
300
306 TGUI_NODISCARD const String& getFilenameLabelText() const;
307
315 void setListViewColumnCaptions(const String& nameColumnText = "Name", const String& sizeColumnText = "Size", const String& modifiedColumnText = "Modified");
316
324 TGUI_NODISCARD std::tuple<String, String, String> getListViewColumnCaptions() const;
325
333 void setFileMustExist(bool enforceExistence);
334
340 TGUI_NODISCARD bool getFileMustExist() const;
341
350 void setSelectingDirectory(bool selectDirectories);
351
357 TGUI_NODISCARD bool getSelectingDirectory() const;
358
364 void setMultiSelect(bool multiSelect);
365
371 TGUI_NODISCARD bool getMultiSelect() const;
372
382 void setIconLoader(std::shared_ptr<FileDialogIconLoader> iconLoader);
383
389 TGUI_NODISCARD std::shared_ptr<FileDialogIconLoader> getIconLoader() const;
390
394 void keyPressed(const Event::KeyEvent& event) override;
395
405 bool canHandleKeyPress(const Event::KeyEvent& event) override;
406
410 void textEntered(char32_t key) override;
411
413 protected:
414
424 TGUI_NODISCARD Signal& getSignal(String signalName) override;
425
431 void rendererChanged(const String& property) override;
432
436 TGUI_NODISCARD std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
437
441 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
442
444 // This function is called every frame with the time passed since the last frame.
446 bool updateTime(Duration elapsedTime) override;
447
449 // Makes a copy of the widget
451 TGUI_NODISCARD Widget::Ptr clone() const override;
452
454 private:
455
457 // Changes the directory that is shown in the dialog
459 void changePath(const Filesystem::Path& path, bool updateHistory);
460
462 // Updates the back and forward buttons when the path history changes
464 void historyChanged();
465
467 // Adds the files to the list view in the order shosen by the user
469 void sortFilesInListView();
470
472 // Stores the selected files and closes the dialog
474 void filesSelected(std::vector<Filesystem::Path> filenames);
475
477 // Updates whether the open/save button is enabled or disabled
479 void updateConfirmButtonEnabled();
480
482 // Handles a press of the open/save button
484 void confirmButtonPressed();
485
487 // Adds a button the File Dialog which allows the user to create a new folder
489 void addCreateFolderButton();
490
492 // Creates a folder in a given directory
494 void createFolder(const String& name);
495
497 // Handles a press of the create folder button
499 void createCreateFolderDialog();
500
502 // Destroys the create folder dialog
504 void destroyCreateFolderDialog();
505
507 // Check if the name of a new folder is valid
509 TGUI_NODISCARD static bool isValidFolderName(const String& name);
510
512 // Initializes the widget pointers after copying or loading the dialog
514 void identifyChildWidgets();
515
517 // Connects the signals of the child widgets
519 void connectSignals();
520
522 public:
523
528
530 Signal onCancel = {"Cancelled"};
531
533 protected:
534
535 Button::Ptr m_buttonBack;
536 Button::Ptr m_buttonForward;
537 Button::Ptr m_buttonUp;
538 EditBox::Ptr m_editBoxPath;
539 ListView::Ptr m_listView;
540 Label::Ptr m_labelFilename;
541 EditBox::Ptr m_editBoxFilename;
542 ComboBox::Ptr m_comboBoxFileTypes;
543 Button::Ptr m_buttonCancel;
544 Button::Ptr m_buttonConfirm;
545 Button::Ptr m_buttonCreateFolder;
546 bool m_allowCreateFolder = false;
547
548 bool m_createFolderDialogOpen = false;
549
550 Filesystem::Path m_currentDirectory;
551 std::vector<Filesystem::FileInfo> m_filesInDirectory;
552 std::vector<Texture> m_fileIcons; // Same order as m_filesInDirectory
553 std::size_t m_sortColumnIndex = 0;
554 bool m_sortInversed = false;
555
556 std::vector<Filesystem::Path> m_pathHistory;
557 std::size_t m_pathHistoryIndex = 0;
558
559 bool m_fileMustExist = true;
560 bool m_selectingDirectory = false;
561 bool m_multiSelect = false;
562
563 std::vector<std::pair<String, std::vector<String>>> m_fileTypeFilters;
564 std::size_t m_selectedFileTypeFilter = 0;
565
566 std::shared_ptr<FileDialogIconLoader> m_iconLoader;
567
568 std::vector<Filesystem::Path> m_selectedFiles;
569 };
570
572}
573
575
576#endif // TGUI_FILE_DIALOG_HPP
std::shared_ptr< Button > Ptr
Shared widget pointer.
Definition Button.hpp:41
std::shared_ptr< ComboBox > Ptr
Shared widget pointer.
Definition ComboBox.hpp:52
Wrapper for durations.
Definition Duration.hpp:53
std::shared_ptr< EditBox > Ptr
Shared widget pointer.
Definition EditBox.hpp:49
Definition FileDialogIconLoader.hpp:43
Definition FileDialogRenderer.hpp:35
SignalFileDialogPaths onFileSelect
Definition FileDialog.hpp:527
void setCancelButtonText(const String &text="Cancel")
Changes the text of the cancel button (e.g. to display it in a different language).
void setMultiSelect(bool multiSelect)
Changes whether multiple files can be selected.
void setFileTypeFilters(const std::vector< std::pair< String, std::vector< String > > > &filters, std::size_t defaultFilterIndex=0)
Changes the file filters which the user can select to only show files of a certain type.
void setFileMustExist(bool enforceExistence)
Changes whether the file should exist or whether the filename can be a non-existent file.
std::size_t getFileTypeFiltersIndex() const
Returns the index of the currently selected file filter.
void setAllowCreateFolder(bool allowCreateFolder)
Adds or removes the create folder button.
void setConfirmButtonText(const String &text="Open")
Changes the text of the open/save button.
static FileDialog::Ptr create(const String &title="Open file", const String &confirmButtonText="Open", bool allowCreateFolder=false)
Creates a new file dialog widget.
std::unique_ptr< DataIO::Node > save(SavingRenderersMap &renderers) const override
Saves the widget as a tree node in order to save it to a file.
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
const String & getCreateFolderButtonText() const
Return the text of the create folder button.
const String & getConfirmButtonText() const
Return the text of the open/save button.
FileDialogRenderer * getSharedRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
bool getFileMustExist() const
Returns whether the file should exist or whether the filename can be a non-existent file.
void setPath(const String &path)
Changes the directory for which the files are to be displayed.
std::shared_ptr< const FileDialog > ConstPtr
Shared constant widget pointer.
Definition FileDialog.hpp:66
std::shared_ptr< FileDialog > Ptr
Shared widget pointer.
Definition FileDialog.hpp:65
const String & getCancelButtonText() const
Return the text of the cancel button.
std::shared_ptr< FileDialogIconLoader > getIconLoader() const
Gets the icon loader that is currently being used.
const String & getFilenameLabelText() const
Return the text of the filename label.
const std::vector< std::pair< String, std::vector< String > > > & getFileTypeFilters() const
Returns the file filters which the user can select to only show files of a certain type.
bool getAllowCreateFolder() const
Return whether the file dialog allows the user to create a folder.
void setPath(const Filesystem::Path &path)
Changes the directory for which the files are to be displayed.
void setCreateFolderButtonText(const String &text="Create Folder")
Changes the text of the create folder button.
std::tuple< String, String, String > getListViewColumnCaptions() const
Returns the names of the list view columns.
const String & getFilename() const
Returns the filename that is entered in the filename edit box.
bool getMultiSelect() const
Returns whether multiple files can be selected.
FileDialogRenderer * getRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
void setIconLoader(std::shared_ptr< FileDialogIconLoader > iconLoader)
Sets a custom icon loader.
bool canHandleKeyPress(const Event::KeyEvent &event) override
Called by the parent of the widget to check if keyPressed would process the event.
static FileDialog::Ptr copy(const FileDialog::ConstPtr &dialog)
Makes a copy of another file dialog.
void setSelectingDirectory(bool selectDirectories)
Changes whether the file dialog is used for selecting a file or for selecting a directory.
void rendererChanged(const String &property) override
Function called when one of the properties of the renderer is changed.
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
bool getSelectingDirectory() const
Returns whether the file dialog is used for selecting a file or for selecting a directory.
void setFilenameLabelText(const String &labelText="Filename:")
Changes the text of the filename label (e.g. to display it in a different language).
void setFilename(const String &filename)
Sets the filename that is shown at the bottom of the file dialog.
FileDialog(FileDialog &&copy) noexcept
Move constructor.
const std::vector< Filesystem::Path > & getSelectedPaths() const
Returns the selected files/directories.
void setListViewColumnCaptions(const String &nameColumnText="Name", const String &sizeColumnText="Size", const String &modifiedColumnText="Modified")
Changes the names of the list view columns (e.g. to display them in a different language).
Signal & getSignal(String signalName) override
Retrieves a signal based on its name.
FileDialog(const FileDialog &copy)
Copy constructor.
Signal onCancel
The child window was closed or the cancel button was pressed. This signal fires before onClose.
Definition FileDialog.hpp:530
static constexpr const char StaticWidgetType[]
Type name of the widget.
Definition FileDialog.hpp:68
const Filesystem::Path & getPath() const
Returns the directory that is currently being shown in the file dialog.
Object to represent paths on a filesystem.
Definition Filesystem.hpp:56
std::shared_ptr< Label > Ptr
Shared widget pointer.
Definition Label.hpp:44
std::shared_ptr< ListView > Ptr
Shared widget pointer.
Definition ListView.hpp:48
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:781
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:59
Wrapper class to store strings.
Definition String.hpp:93
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition Widget.hpp:87
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:36
KeyPressed event parameters.
Definition Event.hpp:168