use new syntax for connecting to backend signals that enforces explicit connection...
[ardour.git] / gtk2_ardour / option_editor.cc
1  /*
2     Copyright (C) 2001-2009 Paul 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 */
19
20 #include <gtkmm/box.h>
21 #include <gtkmm/alignment.h>
22 #include "ardour/configuration.h"
23 #include "option_editor.h"
24 #include "gui_thread.h"
25 #include "i18n.h"
26
27 using namespace std;
28 using namespace Gtk;
29 using namespace ARDOUR;
30
31 void
32 OptionEditorComponent::add_widget_to_page (OptionEditorPage* p, Gtk::Widget* w)
33 {
34         int const n = p->table.property_n_rows();
35         p->table.resize (n + 1, 3);
36         p->table.attach (*w, 1, 3, n, n + 1, FILL | EXPAND);
37 }
38
39 void
40 OptionEditorComponent::add_widgets_to_page (OptionEditorPage* p, Gtk::Widget* wa, Gtk::Widget* wb)
41 {
42         int const n = p->table.property_n_rows();
43         p->table.resize (n + 1, 3);
44         p->table.attach (*wa, 1, 2, n, n + 1, FILL);
45         p->table.attach (*wb, 2, 3, n, n + 1, FILL | EXPAND);
46 }
47
48 OptionEditorHeading::OptionEditorHeading (string const & h)
49 {
50         std::stringstream s;
51         s << "<b>" << h << "</b>";
52         _label = manage (new Label (s.str()));
53         _label->set_alignment (0, 0.5);
54         _label->set_use_markup (true);
55 }
56
57 void
58 OptionEditorHeading::add_to_page (OptionEditorPage* p)
59 {
60         int const n = p->table.property_n_rows();
61         p->table.resize (n + 2, 3);
62
63         p->table.attach (*manage (new Label ("")), 0, 3, n, n + 1, FILL | EXPAND);
64         p->table.attach (*_label, 0, 3, n + 1, n + 2, FILL | EXPAND);
65 }
66
67 void
68 OptionEditorBox::add_to_page (OptionEditorPage* p)
69 {
70         add_widget_to_page (p, _box);
71 }
72
73 BoolOption::BoolOption (string const & i, string const & n, sigc::slot<bool> g, sigc::slot<bool, bool> s)
74         : Option (i, n),
75           _get (g),
76           _set (s)
77 {
78         _button = manage (new CheckButton (n));
79         _button->set_active (_get ());
80         _button->signal_toggled().connect (sigc::mem_fun (*this, &BoolOption::toggled));
81 }
82
83 void
84 BoolOption::add_to_page (OptionEditorPage* p)
85 {
86         add_widget_to_page (p, _button);
87 }
88
89 void
90 BoolOption::set_state_from_config ()
91 {
92         _button->set_active (_get ());
93 }
94
95 void
96 BoolOption::toggled ()
97 {
98         _set (_button->get_active ());
99 }
100
101 EntryOption::EntryOption (string const & i, string const & n, sigc::slot<string> g, sigc::slot<bool, string> s)
102         : Option (i, n),
103           _get (g),
104           _set (s)
105 {
106         _label = manage (new Label (n + ":"));
107         _label->set_alignment (1, 0.5);
108         _entry = manage (new Entry);
109         _entry->signal_activate().connect (sigc::mem_fun (*this, &EntryOption::activated));
110 }
111
112 void
113 EntryOption::add_to_page (OptionEditorPage* p)
114 {
115         add_widgets_to_page (p, _label, _entry);
116 }
117
118 void
119 EntryOption::set_state_from_config ()
120 {
121         _entry->set_text (_get ());
122 }
123
124 void
125 EntryOption::activated ()
126 {
127         _set (_entry->get_text ());
128 }
129
130 OptionEditorPage::OptionEditorPage (Gtk::Notebook& n, std::string const & t)
131         : table (1, 3)
132 {
133         table.set_spacings (4);
134         table.set_col_spacing (0, 32);
135         box.pack_start (table, false, false);
136         box.set_border_width (4);
137         n.append_page (box, t);
138 }
139
140 /** Construct an OptionEditor.
141  *  @param o Configuration to edit.
142  *  @param t Title for the dialog.
143  */
144 OptionEditor::OptionEditor (Configuration* c, std::string const & t)
145         : ArdourDialog (t, false), _config (c)
146 {
147         using namespace Notebook_Helpers;
148
149         set_default_size (300, 300);
150         set_wmclass (X_("ardour_preferences"), "Ardour");
151
152         set_name ("Preferences");
153         add_events (Gdk::KEY_PRESS_MASK | Gdk::KEY_RELEASE_MASK);
154
155         set_border_width (4);
156
157         get_vbox()->set_spacing (4);
158         get_vbox()->pack_start (_notebook);
159
160         _notebook.set_show_tabs (true);
161         _notebook.set_show_border (true);
162         _notebook.set_name ("OptionsNotebook");
163
164         show_all_children();
165
166         /* Watch out for changes to parameters */
167         _config->ParameterChanged.connect (config_connection, boost::bind (&OptionEditor::parameter_changed, this, _1));
168 }
169
170 OptionEditor::~OptionEditor ()
171 {
172         for (std::map<std::string, OptionEditorPage*>::iterator i = _pages.begin(); i != _pages.end(); ++i) {
173                 for (std::list<OptionEditorComponent*>::iterator j = i->second->components.begin(); j != i->second->components.end(); ++j) {
174                         delete *j;
175                 }
176                 delete i->second;
177         }
178 }
179
180 /** Called when a configuration parameter has been changed.
181  *  @param p Parameter name.
182  */
183 void
184 OptionEditor::parameter_changed (std::string const & p)
185 {
186         ENSURE_GUI_THREAD (*this, &OptionEditor::parameter_changed, p)
187
188         for (std::map<std::string, OptionEditorPage*>::iterator i = _pages.begin(); i != _pages.end(); ++i) {
189                 for (std::list<OptionEditorComponent*>::iterator j = i->second->components.begin(); j != i->second->components.end(); ++j) {
190                         (*j)->parameter_changed (p);
191                 }
192         }
193 }
194
195 /** Add a component to a given page.
196  *  @param pn Page name (will be created if it doesn't already exist)
197  *  @param o Component.
198  */
199 void
200 OptionEditor::add_option (std::string const & pn, OptionEditorComponent* o)
201 {
202         if (_pages.find (pn) == _pages.end()) {
203                 _pages[pn] = new OptionEditorPage (_notebook, pn);
204         }
205
206         OptionEditorPage* p = _pages[pn];
207         p->components.push_back (o);
208
209         o->add_to_page (p);
210         o->set_state_from_config ();
211 }