TGUI 1.13
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/Config.hpp>
29
30#include <TGUI/Filesystem.hpp>
31#include <TGUI/Renderers/FileDialogRenderer.hpp>
32#include <TGUI/Widgets/Button.hpp>
33#include <TGUI/Widgets/ChildWindow.hpp>
34#include <TGUI/Widgets/ComboBox.hpp>
35#include <TGUI/Widgets/EditBox.hpp>
36#include <TGUI/Widgets/Label.hpp>
37#include <TGUI/Widgets/ListView.hpp>
38
39#include <tuple>
40
42
43namespace tgui
44{
46
61 class TGUI_API FileDialog : public ChildWindow
62 {
63 public:
64 using Ptr = std::shared_ptr<FileDialog>;
65 using ConstPtr = std::shared_ptr<const FileDialog>;
66
67 static constexpr char StaticWidgetType[] = "FileDialog";
68
76 explicit FileDialog(const char* typeName = StaticWidgetType, bool initRenderer = true);
77
87 [[nodiscard]] static FileDialog::Ptr create(const String& title = "Open file",
88 const String& confirmButtonText = "Open",
89 bool allowCreateFolder = false);
90
94 FileDialog(const FileDialog& other);
95
99 FileDialog(FileDialog&& other) noexcept;
100
104 FileDialog& operator=(const FileDialog& other);
105
109 FileDialog& operator=(FileDialog&& other) noexcept;
110
118 [[nodiscard]] static FileDialog::Ptr copy(const FileDialog::ConstPtr& dialog);
119
124 [[nodiscard]] FileDialogRenderer* getSharedRenderer() override;
125 [[nodiscard]] const FileDialogRenderer* getSharedRenderer() const override;
126
132 [[nodiscard]] FileDialogRenderer* getRenderer() override;
133
151 [[nodiscard]] const std::vector<Filesystem::Path>& getSelectedPaths() const;
152
160 void setPath(const String& path);
161
169 void setPath(const Filesystem::Path& path);
170
176 [[nodiscard]] const Filesystem::Path& getPath() const;
177
183 void setFilename(const String& filename);
184
194 [[nodiscard]] const String& getFilename() const;
195
219 void setFileTypeFilters(const std::vector<std::pair<String, std::vector<String>>>& filters,
220 std::size_t defaultFilterIndex = 0);
221
229 [[nodiscard]] const std::vector<std::pair<String, std::vector<String>>>& getFileTypeFilters() const;
230
238 [[nodiscard]] std::size_t getFileTypeFiltersIndex() const;
239
245 void setConfirmButtonText(const String& text = "Open");
246
252 [[nodiscard]] const String& getConfirmButtonText() const;
253
259 void setCancelButtonText(const String& text = "Cancel");
260
266 [[nodiscard]] const String& getCancelButtonText() const;
267
273 void setCreateFolderButtonText(const String& text = "Create Folder");
274
280 [[nodiscard]] const String& getCreateFolderButtonText() const;
281
287 void setAllowCreateFolder(bool allowCreateFolder);
288
294 [[nodiscard]] bool getAllowCreateFolder() const;
295
301 void setFilenameLabelText(const String& labelText = "Filename:");
302
308 [[nodiscard]] const String& getFilenameLabelText() const;
309
317 void setListViewColumnCaptions(const String& nameColumnText = "Name",
318 const String& sizeColumnText = "Size",
319 const String& modifiedColumnText = "Modified");
320
328 [[nodiscard]] std::tuple<String, String, String> getListViewColumnCaptions() const;
329
337 void setFileMustExist(bool enforceExistence);
338
344 [[nodiscard]] bool getFileMustExist() const;
345
354 void setSelectingDirectory(bool selectDirectories);
355
361 [[nodiscard]] bool getSelectingDirectory() const;
362
368 void setMultiSelect(bool multiSelect);
369
375 [[nodiscard]] bool getMultiSelect() const;
376
386 void setIconLoader(std::shared_ptr<FileDialogIconLoader> iconLoader);
387
393 [[nodiscard]] std::shared_ptr<FileDialogIconLoader> getIconLoader() const;
394
398 void keyPressed(const Event::KeyEvent& event) override;
399
409 bool canHandleKeyPress(const Event::KeyEvent& event) override;
410
414 void textEntered(char32_t key) override;
415
417
418 protected:
428 [[nodiscard]] Signal& getSignal(String signalName) override;
429
435 void rendererChanged(const String& property) override;
436
440 [[nodiscard]] std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
441
445 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
446
448 // This function is called every frame with the time passed since the last frame.
450 bool updateTime(Duration elapsedTime) override;
451
453 // Makes a copy of the widget
455 [[nodiscard]] Widget::Ptr clone() const override;
456
458
459 private:
461 // Changes the directory that is shown in the dialog
463 void changePath(const Filesystem::Path& path, bool updateHistory);
464
466 // Updates the back and forward buttons when the path history changes
468 void historyChanged();
469
471 // Adds the files to the list view in the order shosen by the user
473 void sortFilesInListView();
474
476 // Stores the selected files and closes the dialog
478 void filesSelected(std::vector<Filesystem::Path> selectedFiles);
479
481 // Updates whether the open/save button is enabled or disabled
483 void updateConfirmButtonEnabled();
484
486 // Handles a press of the open/save button
488 void confirmButtonPressed();
489
491 // Adds a button the File Dialog which allows the user to create a new folder
493 void addCreateFolderButton();
494
496 // Creates a folder in a given directory
498 void createFolder(const String& name);
499
501 // Handles a press of the create folder button
503 void createCreateFolderDialog();
504
506 // Destroys the create folder dialog
508 void destroyCreateFolderDialog();
509
511 // Check if the name of a new folder is valid
513 [[nodiscard]] static bool isValidFolderName(const String& name);
514
516 // Initializes the widget pointers after copying or loading the dialog
518 void identifyChildWidgets();
519
521 // Connects the signals of the child widgets
523 void connectSignals();
524
526
527 public:
532
534 Signal onCancel = {"Cancelled"};
535
537
538 protected:
539 Button::Ptr m_buttonBack;
540 Button::Ptr m_buttonForward;
541 Button::Ptr m_buttonUp;
542 EditBox::Ptr m_editBoxPath;
543 ListView::Ptr m_listView;
544 Label::Ptr m_labelFilename;
545 EditBox::Ptr m_editBoxFilename;
546 ComboBox::Ptr m_comboBoxFileTypes;
547 Button::Ptr m_buttonCancel;
548 Button::Ptr m_buttonConfirm;
549 Button::Ptr m_buttonCreateFolder;
550 bool m_allowCreateFolder = false;
551
552 bool m_createFolderDialogOpen = false;
553
554 Filesystem::Path m_currentDirectory;
555 std::vector<Filesystem::FileInfo> m_filesInDirectory;
556 std::vector<Texture> m_fileIcons; // Same order as m_filesInDirectory
557 std::size_t m_sortColumnIndex = 0;
558 bool m_sortInversed = false;
559
560 std::vector<Filesystem::Path> m_pathHistory;
561 std::size_t m_pathHistoryIndex = 0;
562
563 bool m_fileMustExist = true;
564 bool m_selectingDirectory = false;
565 bool m_multiSelect = false;
566
567 std::vector<std::pair<String, std::vector<String>>> m_fileTypeFilters;
568 std::size_t m_selectedFileTypeFilter = 0;
569
570 std::shared_ptr<FileDialogIconLoader> m_iconLoader;
571
572 std::vector<Filesystem::Path> m_selectedFiles;
573 };
574} // namespace tgui
575
576#endif // TGUI_FILE_DIALOG_HPP
std::shared_ptr< Button > Ptr
Shared widget pointer.
Definition Button.hpp:40
std::shared_ptr< ComboBox > Ptr
Shared widget pointer.
Definition ComboBox.hpp:50
Wrapper for durations.
Definition Duration.hpp:52
std::shared_ptr< EditBox > Ptr
Shared widget pointer.
Definition EditBox.hpp:48
Definition FileDialogIconLoader.hpp:43
Definition FileDialogRenderer.hpp:35
FileDialog(const FileDialog &other)
Copy constructor.
SignalFileDialogPaths onFileSelect
Definition FileDialog.hpp:531
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.
FileDialog(FileDialog &&other) noexcept
Move constructor.
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.
FileDialog & operator=(const FileDialog &other)
Overload of copy assignment operator.
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:65
std::shared_ptr< FileDialog > Ptr
Shared widget pointer.
Definition FileDialog.hpp:64
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.
FileDialog & operator=(FileDialog &&other) noexcept
Overload of move assignment operator.
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.
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.
static constexpr char StaticWidgetType[]
Type name of the widget.
Definition FileDialog.hpp:67
Signal onCancel
The child window was closed or the cancel button was pressed. This signal fires before onClose.
Definition FileDialog.hpp:534
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:55
std::shared_ptr< Label > Ptr
Shared widget pointer.
Definition Label.hpp:43
std::shared_ptr< ListView > Ptr
Shared widget pointer.
Definition ListView.hpp:47
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:798
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:59
Wrapper class to store strings.
Definition String.hpp:94
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition Widget.hpp:85
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:37
KeyPressed event parameters.
Definition Event.hpp:166