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