fix up file renaming code a little bit
[ArdourMidi.git] / gtk2_ardour / option_editor.cc
blob207a262dd9d994da8f7b6653120f1cb16d03d549
1 /*
2 Copyright (C) 2001-2009 Paul Davis
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.
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.
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.
20 #include <gtkmm/box.h>
21 #include <gtkmm/alignment.h>
22 #include "gtkmm2ext/utils.h"
23 #include "ardour/configuration.h"
24 #include "ardour/utils.h"
25 #include "ardour/dB.h"
26 #include "option_editor.h"
27 #include "gui_thread.h"
28 #include "utils.h"
29 #include "i18n.h"
31 using namespace std;
32 using namespace Gtk;
33 using namespace Gtkmm2ext;
34 using namespace ARDOUR;
36 void
37 OptionEditorComponent::add_widget_to_page (OptionEditorPage* p, Gtk::Widget* w)
39 int const n = p->table.property_n_rows();
40 p->table.resize (n + 1, 3);
41 p->table.attach (*w, 1, 3, n, n + 1, FILL | EXPAND);
44 void
45 OptionEditorComponent::add_widgets_to_page (OptionEditorPage* p, Gtk::Widget* wa, Gtk::Widget* wb)
47 int const n = p->table.property_n_rows();
48 p->table.resize (n + 1, 3);
49 p->table.attach (*wa, 1, 2, n, n + 1, FILL);
50 p->table.attach (*wb, 2, 3, n, n + 1, FILL | EXPAND);
53 OptionEditorHeading::OptionEditorHeading (string const & h)
55 std::stringstream s;
56 s << "<b>" << h << "</b>";
57 _label = manage (new Label (s.str()));
58 _label->set_alignment (0, 0.5);
59 _label->set_use_markup (true);
62 void
63 OptionEditorHeading::add_to_page (OptionEditorPage* p)
65 int const n = p->table.property_n_rows();
66 p->table.resize (n + 2, 3);
68 p->table.attach (*manage (new Label ("")), 0, 3, n, n + 1, FILL | EXPAND);
69 p->table.attach (*_label, 0, 3, n + 1, n + 2, FILL | EXPAND);
72 void
73 OptionEditorBox::add_to_page (OptionEditorPage* p)
75 add_widget_to_page (p, _box);
78 BoolOption::BoolOption (string const & i, string const & n, sigc::slot<bool> g, sigc::slot<bool, bool> s)
79 : Option (i, n),
80 _get (g),
81 _set (s)
83 _button = manage (new CheckButton (n));
84 _button->set_active (_get ());
85 _button->signal_toggled().connect (sigc::mem_fun (*this, &BoolOption::toggled));
88 void
89 BoolOption::add_to_page (OptionEditorPage* p)
91 add_widget_to_page (p, _button);
94 void
95 BoolOption::set_state_from_config ()
97 _button->set_active (_get ());
100 void
101 BoolOption::toggled ()
103 _set (_button->get_active ());
106 EntryOption::EntryOption (string const & i, string const & n, sigc::slot<string> g, sigc::slot<bool, string> s)
107 : Option (i, n),
108 _get (g),
109 _set (s)
111 _label = manage (new Label (n + ":"));
112 _label->set_alignment (1, 0.5);
113 _entry = manage (new Entry);
114 _entry->signal_activate().connect (sigc::mem_fun (*this, &EntryOption::activated));
117 void
118 EntryOption::add_to_page (OptionEditorPage* p)
120 add_widgets_to_page (p, _label, _entry);
123 void
124 EntryOption::set_state_from_config ()
126 _entry->set_text (_get ());
129 void
130 EntryOption::activated ()
132 _set (_entry->get_text ());
135 FaderOption::FaderOption (string const & i, string const & n, sigc::slot<gain_t> g, sigc::slot<bool, gain_t> s)
136 : Option (i, n)
137 // 0.781787 is the value needed for gain to be set to 0.
138 , _db_adjustment (0.781787, 0, 1, 0.01, 0.1)
139 , _get (g)
140 , _set (s)
142 _pix = ::get_icon (X_("fader_belt_h"));
143 if (_pix == 0) {
144 throw failed_constructor ();
147 _db_slider = manage (new HSliderController (_pix,
148 &_db_adjustment,
149 115,
150 false));
152 _label.set_text (n + ":");
153 _label.set_name (X_("OptionsLabel"));
155 _box.set_spacing (4);
156 _box.pack_start (*_db_slider, false, false);
157 _box.pack_start (_db_display, false, false);
158 _box.show_all ();
160 set_size_request_to_display_given_text (_db_display, "-99.0", 12, 12);
162 _db_adjustment.signal_value_changed().connect (sigc::mem_fun (*this, &FaderOption::db_changed));
165 void
166 FaderOption::set_state_from_config ()
168 gain_t const val = _get ();
169 _db_adjustment.set_value (gain_to_slider_position (val));
171 char buf[16];
173 if (val == 0.0) {
174 snprintf (buf, sizeof (buf), "-inf");
175 } else {
176 snprintf (buf, sizeof (buf), "%.2f", accurate_coefficient_to_dB (val));
179 _db_display.set_text (buf);
182 void
183 FaderOption::db_changed ()
185 _set (slider_position_to_gain (_db_adjustment.get_value ()));
188 void
189 FaderOption::add_to_page (OptionEditorPage* p)
191 add_widgets_to_page (p, &_label, &_box);
194 OptionEditorPage::OptionEditorPage (Gtk::Notebook& n, std::string const & t)
195 : table (1, 3)
197 table.set_spacings (4);
198 table.set_col_spacing (0, 32);
199 box.pack_start (table, false, false);
200 box.set_border_width (4);
201 n.append_page (box, t);
204 /** Construct an OptionEditor.
205 * @param o Configuration to edit.
206 * @param t Title for the dialog.
208 OptionEditor::OptionEditor (Configuration* c, std::string const & t)
209 : ArdourDialog (t, false), _config (c)
211 using namespace Notebook_Helpers;
213 set_default_size (300, 300);
214 set_wmclass (X_("ardour_preferences"), "Ardour");
216 set_name ("Preferences");
217 add_events (Gdk::KEY_PRESS_MASK | Gdk::KEY_RELEASE_MASK);
219 set_border_width (4);
221 get_vbox()->set_spacing (4);
222 get_vbox()->pack_start (_notebook);
224 _notebook.set_show_tabs (true);
225 _notebook.set_show_border (true);
226 _notebook.set_name ("OptionsNotebook");
228 show_all_children();
230 /* Watch out for changes to parameters */
231 _config->ParameterChanged.connect (config_connection, invalidator (*this), ui_bind (&OptionEditor::parameter_changed, this, _1), gui_context());
234 OptionEditor::~OptionEditor ()
236 for (std::map<std::string, OptionEditorPage*>::iterator i = _pages.begin(); i != _pages.end(); ++i) {
237 for (std::list<OptionEditorComponent*>::iterator j = i->second->components.begin(); j != i->second->components.end(); ++j) {
238 delete *j;
240 delete i->second;
244 /** Called when a configuration parameter has been changed.
245 * @param p Parameter name.
247 void
248 OptionEditor::parameter_changed (std::string const & p)
250 ENSURE_GUI_THREAD (*this, &OptionEditor::parameter_changed, p)
252 for (std::map<std::string, OptionEditorPage*>::iterator i = _pages.begin(); i != _pages.end(); ++i) {
253 for (std::list<OptionEditorComponent*>::iterator j = i->second->components.begin(); j != i->second->components.end(); ++j) {
254 (*j)->parameter_changed (p);
259 /** Add a component to a given page.
260 * @param pn Page name (will be created if it doesn't already exist)
261 * @param o Component.
263 void
264 OptionEditor::add_option (std::string const & pn, OptionEditorComponent* o)
266 if (_pages.find (pn) == _pages.end()) {
267 _pages[pn] = new OptionEditorPage (_notebook, pn);
270 OptionEditorPage* p = _pages[pn];
271 p->components.push_back (o);
273 o->add_to_page (p);
274 o->set_state_from_config ();