Fix non-buffered PA backend. Wait for backend to become active
[ardour.git] / libs / widgets / 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 #include <gtkmm/stock.h>
23
24 #include "pbd/whitespace.h"
25 #include "widgets/prompter.h"
26
27 #include "pbd/i18n.h"
28
29 using namespace std;
30 using namespace ArdourWidgets;
31
32 Prompter::Prompter (Gtk::Window& parent, bool modal)
33         : Gtk::Dialog ("", parent, modal)
34         , first_show (true)
35         , can_accept_from_entry (false)
36 {
37         init ();
38 }
39
40 Prompter::Prompter (bool modal)
41         : Gtk::Dialog ("", modal)
42         , first_show (true)
43         , can_accept_from_entry (false)
44 {
45         init ();
46 }
47
48 void
49 Prompter::init ()
50 {
51         set_type_hint (Gdk::WINDOW_TYPE_HINT_DIALOG);
52         set_position (Gtk::WIN_POS_MOUSE);
53         set_name ("Prompter");
54
55         add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
56
57         /*
58            Alas a generic 'affirmative' button seems a bit useless sometimes.
59            You will have to add your own.
60            After adding, use :
61            set_response_sensitive (Gtk::RESPONSE_ACCEPT, false)
62            to prevent the RESPONSE_ACCEPT button from permitting blank strings.
63         */
64
65         entryLabel.set_line_wrap (true);
66         entryLabel.set_name ("PrompterLabel");
67
68         entryBox.set_homogeneous (false);
69         entryBox.set_spacing (5);
70         entryBox.set_border_width (10);
71         entryBox.pack_start (entryLabel, false, false);
72         entryBox.pack_start (entry, true, true);
73
74         get_vbox()->pack_start (entryBox);
75         show_all_children();
76 }
77
78 void
79 Prompter::on_show ()
80 {
81         /* don't connect to signals till shown, so that we don't change the
82            response sensitivity etc. when the setup of the dialog sets the text.
83         */
84
85         if (first_show) {
86                 entry.signal_changed().connect (mem_fun (*this, &Prompter::on_entry_changed));
87                 entry.signal_activate().connect (mem_fun (*this, &Prompter::entry_activated));
88                 can_accept_from_entry = !entry.get_text().empty();
89                 first_show = false;
90         }
91
92         Dialog::on_show ();
93 }
94
95 void
96 Prompter::change_labels (string /*okstr*/, string /*cancelstr*/)
97 {
98         // dynamic_cast<Gtk::Label*>(ok.get_child())->set_text (okstr);
99         // dynamic_cast<Gtk::Label*>(cancel.get_child())->set_text (cancelstr);
100 }
101
102 void
103 Prompter::get_result (string &str, bool strip)
104 {
105         str = entry.get_text ();
106         if (strip) {
107                 PBD::strip_whitespace_edges (str);
108         }
109 }
110
111 void
112 Prompter::entry_activated ()
113 {
114         if (can_accept_from_entry) {
115                 response (Gtk::RESPONSE_ACCEPT);
116         } else {
117                 response (Gtk::RESPONSE_CANCEL);
118         }
119 }
120
121 void
122 Prompter::on_entry_changed ()
123 {
124         /*
125            This is set up so that entering text in the entry
126            field makes the RESPONSE_ACCEPT button active.
127            Of course if you haven't added a RESPONSE_ACCEPT
128            button, nothing will happen at all.
129         */
130
131         if (!entry.get_text().empty()) {
132                 set_response_sensitive (Gtk::RESPONSE_ACCEPT, true);
133                 set_default_response (Gtk::RESPONSE_ACCEPT);
134                 can_accept_from_entry = true;
135         } else {
136                 set_response_sensitive (Gtk::RESPONSE_ACCEPT, false);
137         }
138 }