TGUI  1.3.0
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
26#ifndef TGUI_FILE_DIALOG_HPP
27#define TGUI_FILE_DIALOG_HPP
28
29
30#include <TGUI/CopiedSharedPtr.hpp>
31#include <TGUI/Widgets/Label.hpp>
32#include <TGUI/Widgets/Button.hpp>
33#include <TGUI/Widgets/EditBox.hpp>
34#include <TGUI/Widgets/ComboBox.hpp>
35#include <TGUI/Widgets/ListView.hpp>
36#include <TGUI/Widgets/ChildWindow.hpp>
37#include <TGUI/Widgets/Panel.hpp>
38#include <TGUI/Renderers/FileDialogRenderer.hpp>
39#include <TGUI/Filesystem.hpp>
40
41#if !TGUI_EXPERIMENTAL_USE_STD_MODULE
42 #include <tuple>
43#endif
44
46
47TGUI_MODULE_EXPORT namespace tgui
48{
49 class FileDialogIconLoader;
50
65 class TGUI_API FileDialog : public ChildWindow
66 {
67 public:
68
69 using Ptr = std::shared_ptr<FileDialog>;
70 using ConstPtr = std::shared_ptr<const FileDialog>;
71
72 static constexpr const char StaticWidgetType[] = "FileDialog";
73
74
82 FileDialog(const char* typeName = StaticWidgetType, bool initRenderer = true);
83
84
94 TGUI_NODISCARD static FileDialog::Ptr create(const String& title = "Open file", const String& confirmButtonText = "Open", bool allowCreateFolder = false);
95
96
100 FileDialog(const FileDialog& copy);
101
102
106 FileDialog(FileDialog&& copy) noexcept;
107
108
112 FileDialog& operator= (const FileDialog& right);
113
114
118 FileDialog& operator= (FileDialog&& right) noexcept;
119
120
128 TGUI_NODISCARD static FileDialog::Ptr copy(const FileDialog::ConstPtr& dialog);
129
130
135 TGUI_NODISCARD FileDialogRenderer* getSharedRenderer() override;
136 TGUI_NODISCARD const FileDialogRenderer* getSharedRenderer() const override;
137
143 TGUI_NODISCARD FileDialogRenderer* getRenderer() override;
144
145
163 TGUI_NODISCARD const std::vector<Filesystem::Path>& getSelectedPaths() const;
164
165
173 void setPath(const String& path);
174
175
183 void setPath(const Filesystem::Path& path);
184
185
191 TGUI_NODISCARD const Filesystem::Path& getPath() const;
192
193
199 void setFilename(const String& filename);
200
201
211 TGUI_NODISCARD const String& getFilename() const;
212
213
237 void setFileTypeFilters(const std::vector<std::pair<String, std::vector<String>>>& filters, std::size_t defaultFilterIndex = 0);
238
239
247 TGUI_NODISCARD const std::vector<std::pair<String, std::vector<String>>>& getFileTypeFilters() const;
248
249
257 TGUI_NODISCARD std::size_t getFileTypeFiltersIndex() const;
258
259
265 void setConfirmButtonText(const String& text = "Open");
266
267
273 TGUI_NODISCARD const String& getConfirmButtonText() const;
274
275
281 void setCancelButtonText(const String& text = "Cancel");
282
283
289 TGUI_NODISCARD const String& getCancelButtonText() const;
290
291
297 void setCreateFolderButtonText(const String& text = "Create Folder");
298
299
305 TGUI_NODISCARD const String& getCreateFolderButtonText() const;
306
312 void setAllowCreateFolder(bool allowCreateFolder);
313
319 TGUI_NODISCARD bool getAllowCreateFolder() const;
320
321
327 void setFilenameLabelText(const String& labelText = "Filename:");
328
329
335 TGUI_NODISCARD const String& getFilenameLabelText() const;
336
337
345 void setListViewColumnCaptions(const String& nameColumnText = "Name", const String& sizeColumnText = "Size", const String& modifiedColumnText = "Modified");
346
347
355 TGUI_NODISCARD std::tuple<String, String, String> getListViewColumnCaptions() const;
356
357
365 void setFileMustExist(bool enforceExistence);
366
367
373 TGUI_NODISCARD bool getFileMustExist() const;
374
375
384 void setSelectingDirectory(bool selectDirectories);
385
386
392 TGUI_NODISCARD bool getSelectingDirectory() const;
393
394
400 void setMultiSelect(bool multiSelect);
401
402
408 TGUI_NODISCARD bool getMultiSelect() const;
409
410
420 void setIconLoader(std::shared_ptr<FileDialogIconLoader> iconLoader);
421
422
428 TGUI_NODISCARD std::shared_ptr<FileDialogIconLoader> getIconLoader() const;
429
430
434 void keyPressed(const Event::KeyEvent& event) override;
435
445 bool canHandleKeyPress(const Event::KeyEvent& event) override;
446
450 void textEntered(char32_t key) override;
451
452
454 protected:
455
465 TGUI_NODISCARD Signal& getSignal(String signalName) override;
466
467
474 void rendererChanged(const String& property) override;
475
476
480 TGUI_NODISCARD std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
481
482
486 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
487
488
490 // This function is called every frame with the time passed since the last frame.
492 bool updateTime(Duration elapsedTime) override;
493
494
496 // Makes a copy of the widget
498 TGUI_NODISCARD Widget::Ptr clone() const override;
499
500
502 private:
503
505 // Changes the directory that is shown in the dialog
507 void changePath(const Filesystem::Path& path, bool updateHistory);
508
509
511 // Updates the back and forward buttons when the path history changes
513 void historyChanged();
514
515
517 // Adds the files to the list view in the order shosen by the user
519 void sortFilesInListView();
520
521
523 // Stores the selected files and closes the dialog
525 void filesSelected(std::vector<Filesystem::Path> filenames);
526
527
529 // Updates whether the open/save button is enabled or disabled
531 void updateConfirmButtonEnabled();
532
533
535 // Handles a press of the open/save button
537 void confirmButtonPressed();
538
540 // Adds a button the File Dialog which allows the user to create a new folder
542 void addCreateFolderButton();
543
545 // Creates a folder in a given directory
547 void createFolder(const String& name);
548
550 // Handles a press of the create folder button
552 void createCreateFolderDialog();
553
555 // Destroys the create folder dialog
557 void destroyCreateFolderDialog();
558
560 // Check if the name of a new folder is valid
562 bool isValidFolderName(const String& name);
563
565 // Initializes the widget pointers after copying or loading the dialog
567 void identifyChildWidgets();
568
569
571 // Connects the signals of the child widgets
573 void connectSignals();
574
575
577 public:
578
582 SignalFileDialogPaths onFileSelect = {"FileSelected"};
583
585 Signal onCancel = {"Cancelled"};
586
587
589 protected:
590
591 Button::Ptr m_buttonBack;
592 Button::Ptr m_buttonForward;
593 Button::Ptr m_buttonUp;
594 EditBox::Ptr m_editBoxPath;
595 ListView::Ptr m_listView;
596 Label::Ptr m_labelFilename;
597 EditBox::Ptr m_editBoxFilename;
598 ComboBox::Ptr m_comboBoxFileTypes;
599 Button::Ptr m_buttonCancel;
600 Button::Ptr m_buttonConfirm;
601 Button::Ptr m_buttonCreateFolder;
602 bool m_allowCreateFolder = false;
603
604 bool m_createFolderDialogOpen = false;
605
606 Filesystem::Path m_currentDirectory;
607 std::vector<Filesystem::FileInfo> m_filesInDirectory;
608 std::vector<Texture> m_fileIcons; // Same order as m_filesInDirectory
609 std::size_t m_sortColumnIndex = 0;
610 bool m_sortInversed = false;
611
612 std::vector<Filesystem::Path> m_pathHistory;
613 std::size_t m_pathHistoryIndex = 0;
614
615 bool m_fileMustExist = true;
616 bool m_selectingDirectory = false;
617 bool m_multiSelect = false;
618
619 std::vector<std::pair<String, std::vector<String>>> m_fileTypeFilters;
620 std::size_t m_selectedFileTypeFilter = 0;
621
622 std::shared_ptr<FileDialogIconLoader> m_iconLoader;
623
624 std::vector<Filesystem::Path> m_selectedFiles;
625 };
626
628}
629
631
632#endif // TGUI_FILE_DIALOG_HPP
std::shared_ptr< Button > Ptr
Shared widget pointer.
Definition Button.hpp:42
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:56
std::shared_ptr< EditBox > Ptr
Shared widget pointer.
Definition EditBox.hpp:51
Definition FileDialogRenderer.hpp:37
File dialog widget.
Definition FileDialog.hpp:66
static TGUI_NODISCARD FileDialog::Ptr create(const String &title="Open file", const String &confirmButtonText="Open", bool allowCreateFolder=false)
Creates a new file dialog widget.
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.
TGUI_NODISCARD bool getSelectingDirectory() const
Returns whether the file dialog is used for selecting a file or for selecting a directory.
void setFileMustExist(bool enforceExistence)
Changes whether the file should exist or whether the filename can be a non-existent file.
TGUI_NODISCARD const String & getFilename() const
Returns the filename that is entered in the filename edit box.
TGUI_NODISCARD std::tuple< String, String, String > getListViewColumnCaptions() const
Returns the names of the list view columns.
TGUI_NODISCARD const Filesystem::Path & getPath() const
Returns the directory that is currently being shown in the file dialog.
TGUI_NODISCARD Signal & getSignal(String signalName) override
Retrieves a signal based on its name.
TGUI_NODISCARD const String & getConfirmButtonText() const
Return the text of the open/save button.
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.
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
TGUI_NODISCARD FileDialogRenderer * getSharedRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
TGUI_NODISCARD const std::vector< Filesystem::Path > & getSelectedPaths() const
Returns the selected files/directories.
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:70
TGUI_NODISCARD Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
std::shared_ptr< FileDialog > Ptr
Shared widget pointer.
Definition FileDialog.hpp:69
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.
TGUI_NODISCARD bool getMultiSelect() const
Returns whether multiple files can be selected.
TGUI_NODISCARD bool getAllowCreateFolder() const
Return whether the file dialog allows the user to create a folder.
TGUI_NODISCARD const String & getFilenameLabelText() const
Return the text of the filename label.
static TGUI_NODISCARD FileDialog::Ptr copy(const FileDialog::ConstPtr &dialog)
Makes a copy of another file dialog.
void setIconLoader(std::shared_ptr< FileDialogIconLoader > iconLoader)
Sets a custom icon loader.
TGUI_NODISCARD 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 canHandleKeyPress(const Event::KeyEvent &event) override
Called by the parent of the widget to check if keyPressed would process the event.
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.
TGUI_NODISCARD std::size_t getFileTypeFiltersIndex() const
Returns the index of the currently selected file filter.
TGUI_NODISCARD 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.
TGUI_NODISCARD FileDialogRenderer * getRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
TGUI_NODISCARD const String & getCancelButtonText() const
Return the text of the cancel button.
TGUI_NODISCARD const String & getCreateFolderButtonText() const
Return the text of the create folder button.
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.
TGUI_NODISCARD std::shared_ptr< FileDialogIconLoader > getIconLoader() const
Gets the icon loader that is currently being used.
FileDialog(FileDialog &&copy) noexcept
Move constructor.
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)
FileDialog(const FileDialog &copy)
Copy constructor.
TGUI_NODISCARD bool getFileMustExist() const
Returns whether the file should exist or whether the filename can be a non-existent file.
Object to represent paths on a filesystem.
Definition Filesystem.hpp:59
std::shared_ptr< Label > Ptr
Shared widget pointer.
Definition Label.hpp:45
std::shared_ptr< ListView > Ptr
Shared widget pointer.
Definition ListView.hpp:51
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:839
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:62
Wrapper class to store strings.
Definition String.hpp:101
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition Widget.hpp:87
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:39
KeyPressed event parameters.
Definition Event.hpp:169