de18be29dfac2566153c4c149da9dd9505a7d0c3
[ardour.git] / prompter.cc
1 /*
2  * Copyright (C) 1999 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2017 Robin Gareus <robin@gareus.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19
20 #include <string>
21 #include <gtkmm/stock.h>
22
23 #include "pbd/whitespace.h"
24 #include "widgets/prompter.h"
25
26 #include "pbd/i18n.h"
27
28 using namespace std;
29 using namespace ArdourWidgets;
30
31 Prompter::Prompter (Gtk::Window& parent, bool modal)
32         : Gtk::Dialog ("", parent, modal)
33         , first_show (true)
34         , can_accept_from_entry (false)
35 {
36         init ();
37 }
38
39 Prompter::Prompter (bool modal)
40         : Gtk::Dialog ("", modal)
41         , first_show (true)
42         , can_accept_from_entry (false)
43 {
44         init ();
45 }
46
47 void
48 Prompter::init ()
49 {
50         set_type_hint (Gdk::WINDOW_TYPE_HINT_DIALOG);
51         set_position (Gtk::WIN_POS_MOUSE);
52         set_name ("Prompter");
53
54         add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
55
56         /*
57            Alas a generic 'affirmative' button seems a bit useless sometimes.
58            You will have to add your own.
59            After adding, use :
60            set_response_sensitive (Gtk::RESPONSE_ACCEPT, false)
61            to prevent the RESPONSE_ACCEPT button from permitting blank strings.
62         */
63
64         entryLabel.set_line_wrap (true);
65         entryLabel.set_name ("PrompterLabel");
66
67         entryBox.set_homogeneous (false);
68         entryBox.set_spacing (5);
69         entryBox.set_border_width (10);
70         entryBox.pack_start (entryLabel, false, false);
71         entryBox.pack_start (entry, true, true);
72
73         get_vbox()->pack_start (entryBox);
74         show_all_children();
75 }
76
77 void
78 Prompter::on_show ()
79 {
80         /* don't connect to signals till shown, so that we don't change the
81            response sensitivity etc. when the setup of the dialog sets the text.
82         */
83
84         if (first_show) {
85                 entry.signal_changed().connect (mem_fun (*this, &Prompter::on_entry_changed));
86                 entry.signal_activate().connect (mem_fun (*this, &Prompter::entry_activated));
87                 can_accept_from_entry = !entry.get_text().empty();
88                 first_show = false;
89         }
90
91         Dialog::on_show ();
92 }
93
94 void
95 Prompter::change_labels (string /*okstr*/, string /*cancelstr*/)
96 {
97         // dynamic_cast<Gtk::Label*>(ok.get_child())->set_text (okstr);
98         // dynamic_cast<Gtk::Label*>(cancel.get_child())->set_text (cancelstr);
99 }
100
101 void
102 Prompter::get_result (string &str, bool strip)
103 {
104         str = entry.get_text ();
105         if (strip) {
106                 PBD::strip_whitespace_edges (str);
107         }
108 }
109
110 void
111 Prompter::entry_activated ()
112 {
113         if (can_accept_from_entry) {
114                 response (Gtk::RESPONSE_ACCEPT);
115         } else {
116                 response (Gtk::RESPONSE_CANCEL);
117         }
118 }
119
120 void
121 Prompter::on_entry_changed ()
122 {
123         /*
124            This is set up so that entering text in the entry
125            field makes the RESPONSE_ACCEPT button active.
126            Of course if you haven't added a RESPONSE_ACCEPT
127            button, nothing will happen at all.
128         */
129
130         if (!entry.get_text().empty()) {
131                 set_response_sensitive (Gtk::RESPONSE_ACCEPT, true);
132                 set_default_response (Gtk::RESPONSE_ACCEPT);
133                 can_accept_from_entry = true;
134         } else {
135                 set_response_sensitive (Gtk::RESPONSE_ACCEPT, false);
136         }
137 }