PBD::strip_whitespace_edges() returns the empty string if the passed string is
[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 "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 {
36         init ();
37 }
38
39 Prompter::Prompter (bool modal)
40         : Gtk::Dialog ("", modal)
41 {
42         init ();
43 }
44
45 void
46 Prompter::init ()
47 {
48         set_type_hint (Gdk::WINDOW_TYPE_HINT_DIALOG);
49         set_position (Gtk::WIN_POS_MOUSE);
50         set_name ("Prompter");
51         
52         add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
53
54         /* 
55            Alas a generic 'affirmative' button seems a bit useless sometimes.
56            You will have to add your own.
57            After adding, use :
58            set_response_sensitive (Gtk::RESPONSE_ACCEPT, false)
59            to prevent the RESPONSE_ACCEPT button from permitting blank strings.
60         */
61         
62         entryLabel.set_line_wrap (true);
63         entryLabel.set_name ("PrompterLabel");
64
65         entryBox.set_homogeneous (false);
66         entryBox.set_spacing (5);
67         entryBox.set_border_width (10);
68         entryBox.pack_start (entryLabel);
69         entryBox.pack_start (entry, false, false);
70
71         get_vbox()->pack_start (entryBox);
72         show_all_children();
73         entry.signal_key_release_event().connect (mem_fun (*this, &Prompter::maybe_allow_response));
74         entry.signal_activate().connect (bind (mem_fun (*this, &Prompter::response), Gtk::RESPONSE_ACCEPT));
75 }       
76
77 void
78 Prompter::change_labels (string okstr, string cancelstr)
79 {
80         // dynamic_cast<Gtk::Label*>(ok.get_child())->set_text (okstr);
81         // dynamic_cast<Gtk::Label*>(cancel.get_child())->set_text (cancelstr);
82 }
83
84 void
85 Prompter::get_result (string &str, bool strip)
86 {
87         str = entry.get_text ();
88         if (strip) {
89                 PBD::strip_whitespace_edges (str);
90         }
91 }
92
93 bool
94 Prompter::maybe_allow_response (GdkEventKey* ev)
95 {
96         /* 
97            This is set up so that entering text in the entry 
98            field makes the RESPONSE_ACCEPT button active. 
99            Of course if you haven't added a RESPONSE_ACCEPT 
100            button, nothing will happen at all.
101         */
102
103         if (entry.get_text() != "") {
104           set_response_sensitive (Gtk::RESPONSE_ACCEPT, true);
105           set_default_response (Gtk::RESPONSE_ACCEPT);
106         } else {
107           set_response_sensitive (Gtk::RESPONSE_ACCEPT, false);
108         }
109         return true;
110 }
111