From 58b98e8c5c08e88316f8810ec9114bbe2e725912 Mon Sep 17 00:00:00 2001 From: Robin Gareus Date: Sat, 27 May 2023 15:15:42 +0200 Subject: [PATCH] Prompter: add a reset to default option --- libs/widgets/prompter.cc | 29 +++++++++++++++++++++++++++++ libs/widgets/widgets/prompter.h | 12 +++++------- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/libs/widgets/prompter.cc b/libs/widgets/prompter.cc index a178cfc511..a6ef8fb876 100644 --- a/libs/widgets/prompter.cc +++ b/libs/widgets/prompter.cc @@ -18,6 +18,7 @@ */ #include +#include #include #include "pbd/whitespace.h" @@ -67,11 +68,17 @@ Prompter::init (bool with_cancel) entryLabel.set_line_wrap (true); entryLabel.set_name ("PrompterLabel"); + Widget* w = manage (new Gtk::Image (Gtk::Stock::REVERT_TO_SAVED, Gtk::ICON_SIZE_MENU)); + w->show (); + resetButton.add (*w); + resetButton.set_no_show_all (); + entryBox.set_homogeneous (false); entryBox.set_spacing (5); entryBox.set_border_width (10); entryBox.pack_start (entryLabel, false, false); entryBox.pack_start (entry, true, true); + entryBox.pack_start (resetButton, false, false); get_vbox()->pack_start (entryBox); show_all_children(); @@ -89,6 +96,26 @@ Prompter::set_allow_empty (bool yn) } } +void +Prompter::set_initial_text (std::string txt, bool allow_replace) +{ + entry.set_text (txt); + entry.select_region (0, entry.get_text_length()); + if (allow_replace) { + on_entry_changed (); + } + resetButton.set_sensitive (txt != default_text); +} + +void +Prompter::set_default_text (std::string const& txt) +{ + default_text = txt; + resetButton.show (); + resetButton.signal_clicked ().connect (sigc::track_obj([this] { entry.set_text (default_text);}, *this)); + resetButton.set_sensitive (entry.get_text() != default_text); +} + void Prompter::on_show () { @@ -149,4 +176,6 @@ Prompter::on_entry_changed () } else { set_response_sensitive (Gtk::RESPONSE_ACCEPT, false); } + + resetButton.set_sensitive (entry.get_text() != default_text); } diff --git a/libs/widgets/widgets/prompter.h b/libs/widgets/widgets/prompter.h index 7237007bac..1d4d58c9d8 100644 --- a/libs/widgets/widgets/prompter.h +++ b/libs/widgets/widgets/prompter.h @@ -22,6 +22,7 @@ #include #include +#include #include #include #include @@ -46,13 +47,8 @@ public: entryLabel.set_label (prompt); } - void set_initial_text (std::string txt, bool allow_replace = false) { - entry.set_text (txt); - entry.select_region (0, entry.get_text_length()); - if (allow_replace) { - on_entry_changed (); - } - } + void set_initial_text (std::string txt, bool allow_replace = false); + void set_default_text (std::string const& txt); void change_labels (std::string ok, std::string cancel); @@ -69,9 +65,11 @@ private: Gtk::Entry entry; Gtk::HBox entryBox; Gtk::Label entryLabel; + Gtk::Button resetButton; bool first_show; bool can_accept_from_entry; bool allow_empty; + std::string default_text; void init (bool with_cancel); void entry_activated ();