From e26e1f0f6d11b5a679de8f4eb8e0554427e1ee1a Mon Sep 17 00:00:00 2001 From: Laurenz Reinthaler Date: Mon, 10 Apr 2023 21:45:00 +0200 Subject: [PATCH] Disable create folder confirm button when folder name is invalid --- include/TGUI/Widgets/FileDialog.hpp | 7 ++++++- src/Widgets/FileDialog.cpp | 25 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/include/TGUI/Widgets/FileDialog.hpp b/include/TGUI/Widgets/FileDialog.hpp index 78eed47df..ffa55a6b2 100644 --- a/include/TGUI/Widgets/FileDialog.hpp +++ b/include/TGUI/Widgets/FileDialog.hpp @@ -533,7 +533,7 @@ TGUI_MODULE_EXPORT namespace tgui ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Creates a folder in a given directory ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - void createFolder(const tgui::String& name); + void createFolder(const String& name); ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Handles a press of the create folder button @@ -545,6 +545,11 @@ TGUI_MODULE_EXPORT namespace tgui ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void destroyCreateFolderDialog(); + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + // Check if the name of a new folder is valid + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + bool isValidFolderName(const tgui::String& name); + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Initializes the widget pointers after copying or loading the dialog ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/Widgets/FileDialog.cpp b/src/Widgets/FileDialog.cpp index 2a491eeb6..ad146be1c 100755 --- a/src/Widgets/FileDialog.cpp +++ b/src/Widgets/FileDialog.cpp @@ -1015,10 +1015,17 @@ namespace tgui Button::Ptr confirm_button = Button::create("Confirm"); confirm_button->setPosition("75%", "75%"); confirm_button->setOrigin(0.5f, 0.5f); + confirm_button->setEnabled(false); + + folder_name_edit_box->onTextChange([this, folder_name_edit_box, confirm_button] { + bool isValid = isValidFolderName(folder_name_edit_box->getText()); + confirm_button->setEnabled(isValid); + } ); cancel_button->onPress([this] { destroyCreateFolderDialog(); } ); + confirm_button->onPress([this, folder_name_edit_box] { createFolder(folder_name_edit_box->getText()); destroyCreateFolderDialog(); @@ -1047,6 +1054,23 @@ namespace tgui ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + bool FileDialog::isValidFolderName(const tgui::String& name) + { + if (name.empty() || (name == U".") || (name == U"..")) + return false; + +#ifdef TGUI_SYSTEM_WINDOWS + if (name.find_first_of(U"\\/:*?\"<>|") != tgui::String::npos) +#else + if (name.contains(U'/')) +#endif + return false; + + return true; + } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + void FileDialog::identifyChildWidgets() { m_buttonBack = get