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