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.
21 #include <gtkmm/box.h>
22 #include <gtkmm/alignment.h>
23 #include "gtkmm2ext/utils.h"
25 #include "ardour/dB.h"
26 #include "ardour/rc_configuration.h"
27 #include "ardour/session.h"
28 #include "ardour/types.h"
29 #include "ardour/utils.h"
31 #include "pbd/configuration.h"
32 #include "pbd/replace_all.h"
33 #include "pbd/strsplit.h"
35 #include "gui_thread.h"
36 #include "option_editor.h"
37 #include "public_editor.h"
43 using namespace Gtkmm2ext;
44 using namespace ARDOUR;
47 OptionEditorComponent::add_widget_to_page (OptionEditorPage* p, Gtk::Widget* w)
49 int const n = p->table.property_n_rows();
51 if (!_note.empty ()) {
55 p->table.resize (m, 3);
56 p->table.attach (*w, 1, 3, n, n + 1, FILL | EXPAND);
58 maybe_add_note (p, n + 1);
62 OptionEditorComponent::add_widgets_to_page (OptionEditorPage* p, Gtk::Widget* wa, Gtk::Widget* wb, bool expand)
64 int const n = p->table.property_n_rows();
66 if (!_note.empty ()) {
70 p->table.resize (m, 3);
71 p->table.attach (*wa, 1, 2, n, n + 1, FILL);
73 p->table.attach (*wb, 2, 3, n, n + 1, FILL | EXPAND);
75 Alignment* a = manage (new Alignment (0, 0.5, 0, 1.0));
77 p->table.attach (*a, 2, 3, n, n + 1, FILL | EXPAND);
79 maybe_add_note (p, n + 1);
83 OptionEditorComponent::maybe_add_note (OptionEditorPage* p, int n)
85 if (!_note.empty ()) {
86 Gtk::Label* l = manage (left_aligned_label (string_compose (X_("<i>%1</i>"), _note)));
87 l->set_use_markup (true);
88 l->set_line_wrap (true);
89 p->table.attach (*l, 1, 3, n, n + 1, FILL | EXPAND);
94 OptionEditorComponent::set_note (string const & n)
99 /*--------------------------*/
101 OptionEditorHeading::OptionEditorHeading (string const & h)
104 s << "<b>" << h << "</b>";
105 _label = manage (left_aligned_label (s.str()));
106 _label->set_use_markup (true);
109 /*--------------------------*/
112 OptionEditorHeading::add_to_page (OptionEditorPage* p)
114 int const n = p->table.property_n_rows();
115 if (!_note.empty ()) {
116 p->table.resize (n + 3, 3);
118 p->table.resize (n + 2, 3);
121 p->table.attach (*manage (new Label ("")), 0, 3, n, n + 1, FILL | EXPAND);
122 p->table.attach (*_label, 0, 3, n + 1, n + 2, FILL | EXPAND);
123 maybe_add_note (p, n + 2);
126 /*--------------------------*/
129 OptionEditorBlank::add_to_page (OptionEditorPage* p)
131 int const n = p->table.property_n_rows();
132 p->table.resize (n + 1, 3);
133 p->table.attach (_dummy, 2, 3, n, n + 1, FILL | EXPAND, SHRINK, 0, 0);
134 _dummy.set_size_request (-1, 1);
138 /*--------------------------*/
140 RcConfigDisplay::RcConfigDisplay (string const & i, string const & n, sigc::slot<string> g, char s)
145 _label = manage (right_aligned_label (n));
146 _info = manage (new Label);
147 _info-> set_line_wrap (true);
148 set_state_from_config ();
152 RcConfigDisplay::set_state_from_config ()
156 std::replace (p.begin(), p.end(), _sep, '\n');
162 RcConfigDisplay::parameter_changed (std::string const & p)
165 set_state_from_config ();
170 RcConfigDisplay::add_to_page (OptionEditorPage *p)
172 int const n = p->table.property_n_rows();
174 p->table.resize (m, 3);
175 p->table.attach (*_label, 1, 2, n, n + 1, FILL | EXPAND);
176 p->table.attach (*_info, 2, 3, n, n + 1, FILL | EXPAND);
179 /*--------------------------*/
181 RcActionButton::RcActionButton (std::string const & t, const Glib::SignalProxy0< void >::SlotType & slot, std::string const & l)
184 _button = manage (new Button (t));
185 _button->signal_clicked().connect (slot);
187 _label = manage (right_aligned_label (l));
192 RcActionButton::add_to_page (OptionEditorPage *p)
194 int const n = p->table.property_n_rows();
196 p->table.resize (m, 3);
198 p->table.attach (*_label, 1, 2, n, n + 1, FILL | EXPAND);
199 p->table.attach (*_button, 2, 3, n, n + 1, FILL | EXPAND);
201 p->table.attach (*_button, 1, 3, n, n + 1, FILL | EXPAND);
205 /*--------------------------*/
207 BoolOption::BoolOption (string const & i, string const & n, sigc::slot<bool> g, sigc::slot<bool, bool> s)
212 _button = manage (new CheckButton);
213 _label = manage (new Label);
214 _label->set_markup (n);
215 _button->add (*_label);
216 _button->set_active (_get ());
217 _button->signal_toggled().connect (sigc::mem_fun (*this, &BoolOption::toggled));
221 BoolOption::add_to_page (OptionEditorPage* p)
223 add_widget_to_page (p, _button);
227 BoolOption::set_state_from_config ()
229 _button->set_active (_get ());
233 BoolOption::toggled ()
235 if (!_set (_button->get_active ())) {
236 _button->set_active (_get ());
240 /*--------------------------*/
242 RouteDisplayBoolOption::RouteDisplayBoolOption (string const & i, string const & n, sigc::slot<bool> g, sigc::slot<bool, bool> s)
243 : BoolOption (i, n, g, s)
248 RouteDisplayBoolOption::toggled ()
251 BoolOption::toggled ();
254 /*--------------------------*/
256 EntryOption::EntryOption (string const & i, string const & n, sigc::slot<string> g, sigc::slot<bool, string> s)
261 _label = manage (left_aligned_label (n + ":"));
262 _entry = manage (new Entry);
263 _entry->signal_activate().connect (sigc::mem_fun (*this, &EntryOption::activated));
264 _entry->signal_focus_out_event().connect (sigc::mem_fun (*this, &EntryOption::focus_out));
265 _entry->signal_insert_text().connect (sigc::mem_fun (*this, &EntryOption::filter_text));
269 EntryOption::add_to_page (OptionEditorPage* p)
271 add_widgets_to_page (p, _label, _entry);
275 EntryOption::set_state_from_config ()
277 _entry->set_text (_get ());
281 EntryOption::set_sensitive (bool s)
283 _entry->set_sensitive (s);
287 EntryOption::filter_text (const Glib::ustring&, int*)
289 std::string text = _entry->get_text ();
290 for (size_t i = 0; i < _invalid.length(); ++i) {
291 text.erase (std::remove(text.begin(), text.end(), _invalid.at(i)), text.end());
293 if (text != _entry->get_text ()) {
294 _entry->set_text (text);
299 EntryOption::activated ()
301 _set (_entry->get_text ());
305 EntryOption::focus_out (GdkEventFocus*)
307 _set (_entry->get_text ());
311 /*--------------------------*/
312 HSliderOption::HSliderOption (
313 std::string const& i,
314 std::string const& n,
316 sigc::slot<bool, float> s,
317 double lower, double upper,
318 double step_increment,
319 double page_increment,
326 , _adj (lower, lower, upper, step_increment, page_increment, 0)
332 _label.set_alignment (0, 0.5);
333 _label.set_name ("OptionsLabel");
334 _adj.set_value (_get());
335 _adj.signal_value_changed().connect (sigc::mem_fun (*this, &HSliderOption::changed));
336 _hscale.set_update_policy (Gtk::UPDATE_DISCONTINUOUS);
340 HSliderOption::set_state_from_config ()
343 _adj.set_value (log10(_get()) / _mult);
345 _adj.set_value (_get() / _mult);
350 HSliderOption::changed ()
353 _set (pow (10, _adj.get_value () * _mult));
355 _set (_adj.get_value () * _mult);
360 HSliderOption::add_to_page (OptionEditorPage* p)
362 add_widgets_to_page (p, &_label, &_hscale);
366 HSliderOption::set_sensitive (bool yn)
368 _hscale.set_sensitive (yn);
371 /*--------------------------*/
373 ComboStringOption::ComboStringOption (
374 std::string const & i,
375 std::string const & n,
376 sigc::slot<std::string> g,
377 sigc::slot<bool, std::string> s
383 _label = Gtk::manage (new Gtk::Label (n + ":"));
384 _label->set_alignment (0, 0.5);
385 _combo = Gtk::manage (new Gtk::ComboBoxText);
386 _combo->signal_changed().connect (sigc::mem_fun (*this, &ComboStringOption::changed));
390 ComboStringOption::set_state_from_config () {
391 _combo->set_active_text (_get());
395 ComboStringOption::add_to_page (OptionEditorPage* p)
397 add_widgets_to_page (p, _label, _combo);
400 /** Set the allowed strings for this option
401 * @param strings a vector of allowed strings
404 ComboStringOption::set_popdown_strings (const std::vector<std::string>& strings) {
405 _combo->clear_items ();
406 for (std::vector<std::string>::const_iterator i = strings.begin(); i != strings.end(); ++i) {
407 _combo->append_text (*i);
412 ComboStringOption::clear () {
413 _combo->clear_items();
417 ComboStringOption::changed () {
418 _set (_combo->get_active_text ());
422 ComboStringOption::set_sensitive (bool yn) {
423 _combo->set_sensitive (yn);
426 /*--------------------------*/
428 /** Construct a BoolComboOption.
430 * @param n User-visible name.
431 * @param t Text to give for the variable being true.
432 * @param f Text to give for the variable being false.
433 * @param g Slot to get the variable's value.
434 * @param s Slot to set the variable's value.
436 BoolComboOption::BoolComboOption (
437 string const & i, string const & n, string const & t, string const & f,
438 sigc::slot<bool> g, sigc::slot<bool, bool> s
444 _label = manage (new Label (n + ":"));
445 _label->set_alignment (0, 0.5);
446 _combo = manage (new ComboBoxText);
448 /* option 0 is the false option */
449 _combo->append_text (f);
450 /* and option 1 is the true */
451 _combo->append_text (t);
453 _combo->signal_changed().connect (sigc::mem_fun (*this, &BoolComboOption::changed));
457 BoolComboOption::set_state_from_config ()
459 _combo->set_active (_get() ? 1 : 0);
463 BoolComboOption::add_to_page (OptionEditorPage* p)
465 add_widgets_to_page (p, _label, _combo);
469 BoolComboOption::changed ()
471 _set (_combo->get_active_row_number () == 0 ? false : true);
475 BoolComboOption::set_sensitive (bool yn)
477 _combo->set_sensitive (yn);
480 /*--------------------------*/
482 FaderOption::FaderOption (string const & i, string const & n, sigc::slot<gain_t> g, sigc::slot<bool, gain_t> s)
484 , _db_adjustment (gain_to_slider_position_with_max (1.0, Config->get_max_gain()), 0, 1, 0.01, 0.1)
488 _db_slider = manage (new ArdourWidgets::HSliderController (&_db_adjustment, boost::shared_ptr<PBD::Controllable>(), 220, 18));
490 _label.set_text (n + ":");
491 _label.set_alignment (0, 0.5);
492 _label.set_name (X_("OptionsLabel"));
494 _fader_centering_box.pack_start (*_db_slider, true, false);
496 _box.set_spacing (4);
497 _box.set_homogeneous (false);
498 _box.pack_start (_fader_centering_box, false, false);
499 _box.pack_start (_db_display, false, false);
500 _box.pack_start (*manage (new Label ("dB")), false, false);
503 set_size_request_to_display_given_text (_db_display, "-99.00", 12, 0);
505 _db_adjustment.signal_value_changed().connect (sigc::mem_fun (*this, &FaderOption::db_changed));
506 _db_display.signal_activate().connect (sigc::mem_fun (*this, &FaderOption::on_activate));
507 _db_display.signal_key_press_event().connect (sigc::mem_fun (*this, &FaderOption::on_key_press), false);
511 FaderOption::set_state_from_config ()
513 gain_t const val = _get ();
514 _db_adjustment.set_value (gain_to_slider_position_with_max (val, Config->get_max_gain ()));
519 snprintf (buf, sizeof (buf), "-inf");
521 snprintf (buf, sizeof (buf), "%.2f", accurate_coefficient_to_dB (val));
524 _db_display.set_text (buf);
528 FaderOption::db_changed ()
530 _set (slider_position_to_gain_with_max (_db_adjustment.get_value (), Config->get_max_gain()));
534 FaderOption::on_activate ()
536 float db_val = atof (_db_display.get_text ().c_str ());
537 gain_t coeff_val = dB_to_coefficient (db_val);
539 _db_adjustment.set_value (gain_to_slider_position_with_max (coeff_val, Config->get_max_gain ()));
543 FaderOption::on_key_press (GdkEventKey* ev)
545 if (ARDOUR_UI_UTILS::key_is_legal_for_numeric_entry (ev->keyval)) {
546 /* drop through to normal handling */
549 /* illegal key for gain entry */
554 FaderOption::add_to_page (OptionEditorPage* p)
556 add_widgets_to_page (p, &_label, &_box);
559 /*--------------------------*/
561 ClockOption::ClockOption (string const & i, string const & n, sigc::slot<std::string> g, sigc::slot<bool, std::string> s)
563 , _clock (X_("timecode-offset"), true, X_(""), true, false, true, false)
567 _label.set_text (n + ":");
568 _label.set_alignment (0, 0.5);
569 _label.set_name (X_("OptionsLabel"));
570 _clock.ValueChanged.connect (sigc::mem_fun (*this, &ClockOption::save_clock_time));
574 ClockOption::set_state_from_config ()
578 if (!Timecode::parse_timecode_format(_get(), TC)) {
579 _clock.set (0, true);
581 TC.rate = _session->samples_per_timecode_frame();
582 TC.drop = _session->timecode_drop_frames();
583 _session->timecode_to_sample(TC, when, false, false);
584 if (TC.negative) { when=-when; }
585 _clock.set (when, true);
589 ClockOption::save_clock_time ()
592 _session->sample_to_timecode(_clock.current_time(), TC, false, false);
593 _set (Timecode::timecode_format_time(TC));
597 ClockOption::add_to_page (OptionEditorPage* p)
599 add_widgets_to_page (p, &_label, &_clock);
603 ClockOption::set_session (Session* s)
606 _clock.set_session (s);
609 /*--------------------------*/
611 OptionEditorPage::OptionEditorPage ()
617 OptionEditorPage::OptionEditorPage (Gtk::Notebook& n, std::string const & t)
621 box.pack_start (table, false, false);
622 box.set_border_width (4);
623 n.append_page (box, t);
627 OptionEditorPage::init ()
629 table.set_spacings (4);
630 table.set_col_spacing (0, 32);
633 /*--------------------------*/
636 OptionEditorMiniPage::add_to_page (OptionEditorPage* p)
638 int const n = p->table.property_n_rows();
640 if (!_note.empty ()) {
643 p->table.resize (m, 3);
644 p->table.attach (box, 0, 3, n, n + 1, FILL | EXPAND, SHRINK, 0, 0);
645 maybe_add_note (p, n + 1);
648 /*--------------------------*/
650 /** Construct an OptionEditor.
651 * @param o Configuration to edit.
652 * @param t Title for the dialog.
654 OptionEditor::OptionEditor (PBD::Configuration* c)
656 , option_tree (TreeStore::create (option_columns))
657 , option_treeview (option_tree)
659 using namespace Notebook_Helpers;
661 _notebook.set_show_tabs (false);
662 _notebook.set_show_border (true);
663 _notebook.set_name ("OptionsNotebook");
665 option_treeview.append_column ("", option_columns.name);
666 option_treeview.set_enable_search(true);
667 option_treeview.set_search_column(0);
668 option_treeview.set_name ("OptionsTreeView");
669 option_treeview.set_headers_visible (false);
671 option_treeview.get_selection()->set_mode (Gtk::SELECTION_SINGLE);
672 option_treeview.get_selection()->signal_changed().connect (sigc::mem_fun (*this, &OptionEditor::treeview_row_selected));
674 /* Watch out for changes to parameters */
675 _config->ParameterChanged.connect (config_connection, invalidator (*this), boost::bind (&OptionEditor::parameter_changed, this, _1), gui_context());
678 OptionEditor::~OptionEditor ()
680 for (std::map<std::string, OptionEditorPage*>::iterator i = _pages.begin(); i != _pages.end(); ++i) {
681 for (std::list<OptionEditorComponent*>::iterator j = i->second->components.begin(); j != i->second->components.end(); ++j) {
688 /** Called when a configuration parameter has been changed.
689 * @param p Parameter name.
692 OptionEditor::parameter_changed (std::string const & p)
694 ENSURE_GUI_THREAD (*this, &OptionEditor::parameter_changed, p)
696 for (std::map<std::string, OptionEditorPage*>::iterator i = _pages.begin(); i != _pages.end(); ++i) {
697 for (std::list<OptionEditorComponent*>::iterator j = i->second->components.begin(); j != i->second->components.end(); ++j) {
698 (*j)->parameter_changed (p);
704 OptionEditor::treeview_row_selected ()
706 Glib::RefPtr<Gtk::TreeSelection> selection = option_treeview.get_selection();
707 TreeModel::iterator iter = selection->get_selected();
710 TreeModel::Row row = *iter;
711 Gtk::Widget* w = row[option_columns.widget];
713 _notebook.set_current_page (_notebook.page_num (*w));
719 OptionEditor::find_path_in_treemodel (std::string const & pn, bool create_missing)
721 /* split page name, which is actually a path, into each component */
723 std::vector<std::string> components;
724 split (pn, components, '/');
726 /* start with top level children */
728 TreeModel::Children children = option_tree->children();
729 TreeModel::iterator iter;
731 /* foreach path component ... */
733 for (std::vector<std::string>::const_iterator s = components.begin(); s != components.end(); ++s) {
735 for (iter = children.begin(); iter != children.end(); ++iter) {
736 TreeModel::Row row = *iter;
737 const std::string row_name = row[option_columns.name];
738 if (row_name == (*s)) {
743 if (iter == children.end()) {
744 /* the current component is missing; bail out or create it */
745 if (!create_missing) {
746 return option_tree->get_iter(TreeModel::Path(""));
748 iter = option_tree->append (children);
749 TreeModel::Row row = *iter;
750 row[option_columns.name] = *s;
751 row[option_columns.widget] = 0;
755 /* from now on, iter points to a valid row, either the one we found or a new one */
756 /* set children to the row's children to continue searching */
757 children = (*iter)->children ();
765 OptionEditor::add_path_to_treeview (std::string const & pn, Gtk::Widget& widget)
767 option_treeview.set_model (Glib::RefPtr<TreeStore>());
769 TreeModel::iterator row_iter = find_path_in_treemodel(pn, true);
773 TreeModel::Row row = *row_iter;
774 row[option_columns.widget] = &widget;
776 option_treeview.set_model (option_tree);
777 option_treeview.expand_all ();
780 /** Add a component to a given page.
781 * @param pn Page name (will be created if it doesn't already exist)
782 * @param o Component.
785 OptionEditor::add_option (std::string const & pn, OptionEditorComponent* o)
787 if (_pages.find (pn) == _pages.end()) {
788 OptionEditorPage* oep = new OptionEditorPage (_notebook, pn);
791 add_path_to_treeview (pn, oep->box);
794 OptionEditorPage* p = _pages[pn];
795 p->components.push_back (o);
798 o->set_state_from_config ();
802 * @param pn Page name (will be created if it doesn't already exist)
803 * @param w widget that fills the page
806 OptionEditor::add_page (std::string const & pn, Gtk::Widget& w)
808 if (_pages.find (pn) == _pages.end()) {
809 OptionEditorPage* oep = new OptionEditorPage (_notebook, pn);
811 add_path_to_treeview (pn, oep->box);
814 OptionEditorPage* p = _pages[pn];
815 p->box.pack_start (w, true, true);
819 OptionEditor::set_current_page (string const & p)
821 TreeModel::iterator row_iter = find_path_in_treemodel(p);
824 option_treeview.get_selection()->select(row_iter);
829 /*--------------------------*/
831 DirectoryOption::DirectoryOption (string const & i, string const & n, sigc::slot<string> g, sigc::slot<bool, string> s)
836 _file_chooser.set_action (Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER);
837 _file_chooser.signal_selection_changed().connect (sigc::mem_fun (*this, &DirectoryOption::selection_changed));
841 DirectoryOption::set_state_from_config ()
843 _file_chooser.set_current_folder (poor_mans_glob(_get ()));
847 DirectoryOption::add_to_page (OptionEditorPage* p)
849 Gtk::Label *label = manage (new Label (_name));
850 label->set_alignment (0, 0.5);
851 label->set_name (X_("OptionsLabel"));
852 add_widgets_to_page (p, label, &_file_chooser);
856 DirectoryOption::selection_changed ()
858 _set (poor_mans_glob(_file_chooser.get_filename ()));
861 /*--------------------------*/
863 OptionEditorContainer::OptionEditorContainer (PBD::Configuration* c, string const& str)
866 set_border_width (4);
867 Frame* f = manage (new Frame ());
869 f->set_shadow_type (Gtk::SHADOW_OUT);
870 f->set_border_width (0);
871 hpacker.pack_start (*f, false, false, 4);
872 hpacker.pack_start (notebook(), false, false);
873 pack_start (hpacker, true, true);
878 OptionEditorWindow::OptionEditorWindow (PBD::Configuration* c, string const& str)
882 container.set_border_width (4);
883 Frame* f = manage (new Frame ());
885 f->set_shadow_type (Gtk::SHADOW_OUT);
886 f->set_border_width (0);
887 hpacker.pack_start (*f, false, false);
888 hpacker.pack_start (notebook(), true, true, 4);
890 container.pack_start (hpacker, true, true);