TGUI  1.6.1
Loading...
Searching...
No Matches
FileDialog.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2024 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
38#if !TGUI_EXPERIMENTAL_USE_STD_MODULE
39 #include <tuple>
40#endif
41
43
44TGUI_MODULE_EXPORT namespace tgui
45{
46 class FileDialogIconLoader;
47
62 class TGUI_API FileDialog : public ChildWindow
63 {
64 public:
65
66 using Ptr = std::shared_ptr<FileDialog>;
67 using ConstPtr = std::shared_ptr<const FileDialog>;
68
69 static constexpr const char StaticWidgetType[] = "FileDialog";
70
78 FileDialog(const char* typeName = StaticWidgetType, bool initRenderer = true);
79
89 TGUI_NODISCARD static FileDialog::Ptr create(const String& title = "Open file", const String& confirmButtonText = "Open", bool allowCreateFolder = false);
90
94 FileDialog(const FileDialog& copy);
95
99 FileDialog(FileDialog&& copy) noexcept;
100
104 FileDialog& operator= (const FileDialog& right);
105
109 FileDialog& operator= (FileDialog&& right) noexcept;
110
118 TGUI_NODISCARD static FileDialog::Ptr copy(const FileDialog::ConstPtr& dialog);
119
124 TGUI_NODISCARD FileDialogRenderer* getSharedRenderer() override;
125 TGUI_NODISCARD const FileDialogRenderer* getSharedRenderer() const override;
126
132 TGUI_NODISCARD FileDialogRenderer* getRenderer() override;
133
151 TGUI_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 TGUI_NODISCARD const Filesystem::Path& getPath() const;
177
183 void setFilename(const String& filename);
184
194 TGUI_NODISCARD const String& getFilename() const;
195
219 void setFileTypeFilters(const std::vector<std::pair<String, std::vector<String>>>& filters, std::size_t defaultFilterIndex = 0);
220
228 TGUI_NODISCARD const std::vector<std::pair<String, std::vector<String>>>& getFileTypeFilters() const;
229
237 TGUI_NODISCARD std::size_t getFileTypeFiltersIndex() const;
238
244 void setConfirmButtonText(const String& text = "Open");
245
251 TGUI_NODISCARD const String& getConfirmButtonText() const;
252
258 void setCancelButtonText(const String& text = "Cancel");
259
265 TGUI_NODISCARD const String& getCancelButtonText() const;
266
272 void setCreateFolderButtonText(const String& text = "Create Folder");
273
279 TGUI_NODISCARD const String& getCreateFolderButtonText() const;
280
286 void setAllowCreateFolder(bool allowCreateFolder);
287
293 TGUI_NODISCARD bool getAllowCreateFolder() const;
294
300 void setFilenameLabelText(const String& labelText = "Filename:");
301
307 TGUI_NODISCARD const String& getFilenameLabelText() const;
308
316 void setListViewColumnCaptions(const String& nameColumnText = "Name", const String& sizeColumnText = "Size", const String& modifiedColumnText = "Modified");
317
325 TGUI_NODISCARD std::tuple<String, String, String> getListViewColumnCaptions() const;
326
334 void setFileMustExist(bool enforceExistence);
335
341 TGUI_NODISCARD bool getFileMustExist() const;
342
351 void setSelectingDirectory(bool selectDirectories);
352
358 TGUI_NODISCARD bool getSelectingDirectory() const;
359
365 void setMultiSelect(bool multiSelect);
366
372 TGUI_NODISCARD bool getMultiSelect() const;
373
383 void setIconLoader(std::shared_ptr<FileDialogIconLoader> iconLoader);
384
390 TGUI_NODISCARD std::shared_ptr<FileDialogIconLoader> getIconLoader() const;
391
395 void keyPressed(const Event::KeyEvent& event) override;
396
406 bool canHandleKeyPress(const Event::KeyEvent& event) override;
407
411 void textEntered(char32_t key) override;
412
414 protected:
415
425 TGUI_NODISCARD Signal& getSignal(String signalName) override;
426
432 void rendererChanged(const String& property) override;
433
437 TGUI_NODISCARD std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
438
442 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
443
445 // This function is called every frame with the time passed since the last frame.
447 bool updateTime(Duration elapsedTime) override;
448
450 // Makes a copy of the widget
452 TGUI_NODISCARD Widget::Ptr clone() const override;
453
455 private:
456
458 // Changes the directory that is shown in the dialog
460 void changePath(const Filesystem::Path& path, bool updateHistory);
461
463 // Updates the back and forward buttons when the path history changes
465 void historyChanged();
466
468 // Adds the files to the list view in the order shosen by the user
470 void sortFilesInListView();
471
473 // Stores the selected files and closes the dialog
475 void filesSelected(std::vector<Filesystem::Path> filenames);
476
478 // Updates whether the open/save button is enabled or disabled
480 void updateConfirmButtonEnabled();
481
483 // Handles a press of the open/save button
485 void confirmButtonPressed();
486
488 // Adds a button the File Dialog which allows the user to create a new folder
490 void addCreateFolderButton();
491
493 // Creates a folder in a given directory
495 void createFolder(const String& name);
496
498 // Handles a press of the create folder button
500 void createCreateFolderDialog();
501
503 // Destroys the create folder dialog
505 void destroyCreateFolderDialog();
506
508 // Check if the name of a new folder is valid
510 bool isValidFolderName(const String& name);
511
513 // Initializes the widget pointers after copying or loading the dialog
515 void identifyChildWidgets();
516
518 // Connects the signals of the child widgets
520 void connectSignals();
521
523 public:
524
528 SignalFileDialogPaths onFileSelect = {"FileSelected"};
529
531 Signal onCancel = {"Cancelled"};
532
534 protected:
535
536 Button::Ptr m_buttonBack;
537 Button::Ptr m_buttonForward;
538 Button::Ptr m_buttonUp;
539 EditBox::Ptr m_editBoxPath;
540 ListView::Ptr m_listView;
541 Label::Ptr m_labelFilename;
542 EditBox::Ptr m_editBoxFilename;
543 ComboBox::Ptr m_comboBoxFileTypes;
544 Button::Ptr m_buttonCancel;
545 Button::Ptr m_buttonConfirm;
546 Button::Ptr m_buttonCreateFolder;
547 bool m_allowCreateFolder = false;
548
549 bool m_createFolderDialogOpen = false;
550
551 Filesystem::Path m_currentDirectory;
552 std::vector<Filesystem::FileInfo> m_filesInDirectory;
553 std::vector<Texture> m_fileIcons; // Same order as m_filesInDirectory
554 std::size_t m_sortColumnIndex = 0;
555 bool m_sortInversed = false;
556
557 std::vector<Filesystem::Path> m_pathHistory;
558 std::size_t m_pathHistoryIndex = 0;
559
560 bool m_fileMustExist = true;
561 bool m_selectingDirectory = false;
562 bool m_multiSelect = false;
563
564 std::vector<std::pair<String, std::vector<String>>> m_fileTypeFilters;
565 std::size_t m_selectedFileTypeFilter = 0;
566
567 std::shared_ptr<FileDialogIconLoader> m_iconLoader;
568
569 std::vector<Filesystem::Path> m_selectedFiles;
570 };
571
573}
574
576
577#endif // TGUI_FILE_DIALOG_HPP
std::shared_ptr< Button > Ptr
Shared widget pointer.
Definition Button.hpp:41
Child window widget.
Definition ChildWindow.hpp:45
std::shared_ptr< ComboBox > Ptr
Shared widget pointer.
Definition ComboBox.hpp:52
Wrapper for durations.
Definition Duration.hpp:55
std::shared_ptr< EditBox > Ptr
Shared widget pointer.
Definition EditBox.hpp:51
Definition FileDialogRenderer.hpp:35
File dialog widget.
Definition FileDialog.hpp:63
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:67
std::shared_ptr< FileDialog > Ptr
Shared widget pointer.
Definition FileDialog.hpp:66
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.
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:58
std::shared_ptr< Label > Ptr
Shared widget pointer.
Definition Label.hpp:44
std::shared_ptr< ListView > Ptr
Shared widget pointer.
Definition ListView.hpp:50
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:783
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:61
Wrapper class to store strings.
Definition String.hpp:96
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition Widget.hpp:86
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:38
KeyPressed event parameters.
Definition Event.hpp:168