X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=libs%2Fgtkmm2ext%2Fprompter.cc;h=1f5e13b923c7679659e758bc360c673989f7a08a;hb=bff1fcbe4813346b72e8afcc1e7ea4d3c3f598bd;hp=5dd3367380dd3dcfd17a99964520aad49991581e;hpb=79650335d11f2b3c0a0ae51ad971a36cdb28c108;p=ardour.git diff --git a/libs/gtkmm2ext/prompter.cc b/libs/gtkmm2ext/prompter.cc index 5dd3367380..1f5e13b923 100644 --- a/libs/gtkmm2ext/prompter.cc +++ b/libs/gtkmm2ext/prompter.cc @@ -1,5 +1,5 @@ /* - Copyright (C) 1999 Paul Barton-Davis + Copyright (C) 1999 Paul Barton-Davis This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -20,6 +20,8 @@ #include +#include + #include #include @@ -30,12 +32,16 @@ using namespace Gtkmm2ext; Prompter::Prompter (Gtk::Window& parent, bool modal) : Gtk::Dialog ("", parent, modal) + , first_show (true) + , can_accept_from_entry (false) { init (); } Prompter::Prompter (bool modal) : Gtk::Dialog ("", modal) + , first_show (true) + , can_accept_from_entry (false) { init (); } @@ -43,39 +49,91 @@ Prompter::Prompter (bool modal) void Prompter::init () { + set_type_hint (Gdk::WINDOW_TYPE_HINT_DIALOG); set_position (Gtk::WIN_POS_MOUSE); set_name ("Prompter"); - set_default_response (Gtk::RESPONSE_ACCEPT); - add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); - add_button (Gtk::Stock::OK, Gtk::RESPONSE_ACCEPT); - + + /* + Alas a generic 'affirmative' button seems a bit useless sometimes. + You will have to add your own. + After adding, use : + set_response_sensitive (Gtk::RESPONSE_ACCEPT, false) + to prevent the RESPONSE_ACCEPT button from permitting blank strings. + */ + entryLabel.set_line_wrap (true); entryLabel.set_name ("PrompterLabel"); entryBox.set_homogeneous (false); entryBox.set_spacing (5); entryBox.set_border_width (10); - entryBox.pack_start (entryLabel); - entryBox.pack_start (entry, false, false); + entryBox.pack_start (entryLabel, false, false); + entryBox.pack_start (entry, true, true); get_vbox()->pack_start (entryBox); show_all_children(); +} - entry.signal_activate().connect (bind (mem_fun (*this, &Prompter::response), Gtk::RESPONSE_ACCEPT)); -} +void +Prompter::on_show () +{ + /* don't connect to signals till shown, so that we don't change the + response sensitivity etc. when the setup of the dialog sets the text. + */ + + if (first_show) { + entry.signal_changed().connect (mem_fun (*this, &Prompter::on_entry_changed)); + entry.signal_activate().connect (mem_fun (*this, &Prompter::entry_activated)); + can_accept_from_entry = !entry.get_text().empty(); + first_show = false; + } + + Dialog::on_show (); +} void -Prompter::change_labels (string okstr, string cancelstr) +Prompter::change_labels (string /*okstr*/, string /*cancelstr*/) { // dynamic_cast(ok.get_child())->set_text (okstr); // dynamic_cast(cancel.get_child())->set_text (cancelstr); } void -Prompter::get_result (string &str) - +Prompter::get_result (string &str, bool strip) { str = entry.get_text (); + if (strip) { + PBD::strip_whitespace_edges (str); + } +} + +void +Prompter::entry_activated () +{ + if (can_accept_from_entry) { + response (Gtk::RESPONSE_ACCEPT); + } else { + response (Gtk::RESPONSE_CANCEL); + } +} + +void +Prompter::on_entry_changed () +{ + /* + This is set up so that entering text in the entry + field makes the RESPONSE_ACCEPT button active. + Of course if you haven't added a RESPONSE_ACCEPT + button, nothing will happen at all. + */ + + if (!entry.get_text().empty()) { + set_response_sensitive (Gtk::RESPONSE_ACCEPT, true); + set_default_response (Gtk::RESPONSE_ACCEPT); + can_accept_from_entry = true; + } else { + set_response_sensitive (Gtk::RESPONSE_ACCEPT, false); + } }