2 Copyright (C) 1999-2014 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.
24 #include <cstdio> /* for snprintf, grrr */
26 #include <glibmm/miscutils.h>
27 #include <glib/gstdio.h>
29 #include "pbd/convert.h"
30 #include "pbd/failed_constructor.h"
31 #include "pbd/xml++.h"
32 #include "pbd/file_utils.h"
33 #include "pbd/error.h"
34 #include "pbd/stacktrace.h"
36 #include "gtkmm2ext/rgb_macros.h"
37 #include "gtkmm2ext/gtk_ui.h"
39 #include "ardour/filesystem_paths.h"
41 #include "ardour_ui.h"
42 #include "global_signals.h"
43 #include "ui_config.h"
49 using namespace ARDOUR;
50 using namespace ArdourCanvas;
52 static const char* ui_config_file_name = "ui_config";
53 static const char* default_ui_config_file_name = "default_ui_config";
54 UIConfiguration* UIConfiguration::_instance = 0;
56 static const double hue_width = 18.0;
58 UIConfiguration::UIConfiguration ()
60 #undef UI_CONFIG_VARIABLE
61 #define UI_CONFIG_VARIABLE(Type,var,name,val) var (name,val),
62 #define CANVAS_FONT_VARIABLE(var,name) var (name),
63 #include "ui_config_vars.h"
64 #include "canvas_vars.h"
65 #undef UI_CONFIG_VARIABLE
66 #undef CANVAS_FONT_VARIABLE
69 aliases_modified (false),
70 colors_modified (false),
71 modifiers_modified (false),
78 ARDOUR_UI_UTILS::ColorsChanged.connect (boost::bind (&UIConfiguration::colors_changed, this));
80 ParameterChanged.connect (sigc::mem_fun (*this, &UIConfiguration::parameter_changed));
82 /* force GTK theme setting, so that loading an RC file will work */
87 UIConfiguration::~UIConfiguration ()
92 UIConfiguration::colors_changed ()
96 /* In theory, one of these ought to work:
98 gtk_rc_reparse_all_for_settings (gtk_settings_get_default(), true);
99 gtk_rc_reset_styles (gtk_settings_get_default());
101 but in practice, neither of them do. So just reload the current
102 GTK RC file, which causes a reset of all styles and a redraw
105 parameter_changed ("ui-rc-file");
109 UIConfiguration::parameter_changed (string param)
113 if (param == "ui-rc-file") {
115 } else if (param == "color-file") {
123 UIConfiguration::reset_gtk_theme ()
127 ss << "gtk_color_scheme = \"" << hex;
129 for (ColorAliases::iterator g = color_aliases.begin(); g != color_aliases.end(); ++g) {
131 if (g->first.find ("gtk_") == 0) {
132 ColorAliases::const_iterator a = color_aliases.find (g->first);
133 const string gtk_name = g->first.substr (4);
134 ss << gtk_name << ":#" << std::setw (6) << setfill ('0') << (color (g->second) >> 8) << ';';
138 ss << '"' << dec << endl;
140 /* reset GTK color scheme */
142 Gtk::Settings::get_default()->property_gtk_color_scheme() = ss.str();
146 UIConfiguration::map_parameters (boost::function<void (std::string)>& functor)
148 #undef UI_CONFIG_VARIABLE
149 #define UI_CONFIG_VARIABLE(Type,var,Name,value) functor (Name);
150 #include "ui_config_vars.h"
151 #undef UI_CONFIG_VARIABLE
155 UIConfiguration::load_defaults ()
160 if (find_file (ardour_config_search_path(), default_ui_config_file_name, rcfile) ) {
163 info << string_compose (_("Loading default ui configuration file %1"), rcfile) << endmsg;
165 if (!tree.read (rcfile.c_str())) {
166 error << string_compose(_("cannot read default ui configuration file \"%1\""), rcfile) << endmsg;
168 if (set_state (*tree.root(), Stateful::loading_state_version)) {
169 error << string_compose(_("default ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
177 warning << string_compose (_("Could not find default UI configuration file %1"), default_ui_config_file_name) << endmsg;
181 /* reload color theme */
182 load_color_theme (false);
183 ARDOUR_UI_UTILS::ColorsChanged (); /* EMIT SIGNAL */
190 UIConfiguration::load_color_theme (bool allow_own)
198 basename += color_file.get();
199 basename += ".colors";
201 if (find_file (ardour_config_search_path(), basename, cfile)) {
207 basename = color_file.get();
208 basename += ".colors";
210 if (find_file (ardour_config_search_path(), basename, cfile)) {
219 info << string_compose (_("Loading color file %1"), cfile) << endmsg;
221 if (!tree.read (cfile.c_str())) {
222 error << string_compose(_("cannot read color file \"%1\""), cfile) << endmsg;
226 if (set_state (*tree.root(), Stateful::loading_state_version)) {
227 error << string_compose(_("color file \"%1\" not loaded successfully."), cfile) << endmsg;
231 ARDOUR_UI_UTILS::ColorsChanged ();
233 warning << string_compose (_("Color file %1 not found"), basename) << endmsg;
240 UIConfiguration::store_color_theme ()
243 LocaleGuard lg (X_("C"));
245 root = new XMLNode("Ardour");
247 XMLNode* parent = new XMLNode (X_("Colors"));
248 for (Colors::const_iterator i = colors.begin(); i != colors.end(); ++i) {
249 XMLNode* node = new XMLNode (X_("Color"));
250 node->add_property (X_("name"), i->first);
252 ss << "0x" << setw (8) << setfill ('0') << hex << i->second;
253 node->add_property (X_("value"), ss.str());
254 parent->add_child_nocopy (*node);
256 root->add_child_nocopy (*parent);
258 parent = new XMLNode (X_("ColorAliases"));
259 for (ColorAliases::const_iterator i = color_aliases.begin(); i != color_aliases.end(); ++i) {
260 XMLNode* node = new XMLNode (X_("ColorAlias"));
261 node->add_property (X_("name"), i->first);
262 node->add_property (X_("alias"), i->second);
263 parent->add_child_nocopy (*node);
265 root->add_child_nocopy (*parent);
267 parent = new XMLNode (X_("Modifiers"));
268 for (Modifiers::const_iterator i = modifiers.begin(); i != modifiers.end(); ++i) {
269 XMLNode* node = new XMLNode (X_("Modifier"));
270 node->add_property (X_("name"), i->first);
271 node->add_property (X_("modifier"), i->second.to_string());
272 parent->add_child_nocopy (*node);
274 root->add_child_nocopy (*parent);
277 std::string colorfile = Glib::build_filename (user_config_directory(), (string ("my-") + color_file.get() + ".colors"));
279 tree.set_root (root);
281 if (!tree.write (colorfile.c_str())){
282 error << string_compose (_("Color file %1 not saved"), colorfile) << endmsg;
290 UIConfiguration::load_state ()
296 if (find_file (ardour_config_search_path(), default_ui_config_file_name, rcfile)) {
300 info << string_compose (_("Loading default ui configuration file %1"), rcfile) << endmsg;
302 if (!tree.read (rcfile.c_str())) {
303 error << string_compose(_("cannot read default ui configuration file \"%1\""), rcfile) << endmsg;
307 if (set_state (*tree.root(), Stateful::loading_state_version)) {
308 error << string_compose(_("default ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
313 if (find_file (ardour_config_search_path(), ui_config_file_name, rcfile)) {
317 info << string_compose (_("Loading user ui configuration file %1"), rcfile) << endmsg;
319 if (!tree.read (rcfile)) {
320 error << string_compose(_("cannot read ui configuration file \"%1\""), rcfile) << endmsg;
324 if (set_state (*tree.root(), Stateful::loading_state_version)) {
325 error << string_compose(_("user ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
333 error << _("could not find any ui configuration file, canvas will look broken.") << endmsg;
340 UIConfiguration::save_state()
344 std::string rcfile = Glib::build_filename (user_config_directory(), ui_config_file_name);
348 tree.set_root (&get_state());
350 if (!tree.write (rcfile.c_str())){
351 error << string_compose (_("Config file %1 not saved"), rcfile) << endmsg;
358 if (aliases_modified || colors_modified || modifiers_modified) {
360 if (store_color_theme ()) {
361 error << string_compose (_("Color file %1 not saved"), color_file.get()) << endmsg;
365 aliases_modified = false;
366 colors_modified = false;
367 modifiers_modified = false;
375 UIConfiguration::get_state ()
378 LocaleGuard lg (X_("C"));
380 root = new XMLNode("Ardour");
382 root->add_child_nocopy (get_variables ("UI"));
383 root->add_child_nocopy (get_variables ("Canvas"));
386 root->add_child_copy (*_extra_xml);
393 UIConfiguration::get_variables (std::string which_node)
396 LocaleGuard lg (X_("C"));
398 node = new XMLNode (which_node);
400 #undef UI_CONFIG_VARIABLE
401 #undef CANVAS_FONT_VARIABLE
402 #define UI_CONFIG_VARIABLE(Type,var,Name,value) if (node->name() == "UI") { var.add_to_node (*node); }
403 #define CANVAS_FONT_VARIABLE(var,Name) if (node->name() == "Canvas") { var.add_to_node (*node); }
404 #include "ui_config_vars.h"
405 #include "canvas_vars.h"
406 #undef UI_CONFIG_VARIABLE
407 #undef CANVAS_FONT_VARIABLE
413 UIConfiguration::set_state (const XMLNode& root, int /*version*/)
415 /* this can load a generic UI configuration file or a colors file */
417 if (root.name() != "Ardour") {
421 Stateful::save_extra_xml (root);
423 XMLNodeList nlist = root.children();
424 XMLNodeConstIterator niter;
427 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
431 if (node->name() == "Canvas" || node->name() == "UI") {
432 set_variables (*node);
437 XMLNode* colors = find_named_node (root, X_("Colors"));
440 load_colors (*colors);
443 XMLNode* aliases = find_named_node (root, X_("ColorAliases"));
446 load_color_aliases (*aliases);
449 XMLNode* modifiers = find_named_node (root, X_("Modifiers"));
452 load_modifiers (*modifiers);
459 UIConfiguration::load_color_aliases (XMLNode const & node)
461 XMLNodeList const nlist = node.children();
462 XMLNodeConstIterator niter;
463 XMLProperty const *name;
464 XMLProperty const *alias;
466 color_aliases.clear ();
468 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
469 if ((*niter)->name() != X_("ColorAlias")) {
472 name = (*niter)->property (X_("name"));
473 alias = (*niter)->property (X_("alias"));
476 color_aliases.insert (make_pair (name->value(), alias->value()));
482 UIConfiguration::load_colors (XMLNode const & node)
484 XMLNodeList const nlist = node.children();
485 XMLNodeConstIterator niter;
486 XMLProperty const *name;
487 XMLProperty const *color;
491 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
492 if ((*niter)->name() != X_("Color")) {
495 name = (*niter)->property (X_("name"));
496 color = (*niter)->property (X_("value"));
499 ArdourCanvas::Color c;
500 c = strtoul (color->value().c_str(), 0, 16);
501 colors.insert (make_pair (name->value(), c));
507 UIConfiguration::load_modifiers (XMLNode const & node)
509 PBD::LocaleGuard lg ("C");
510 XMLNodeList const nlist = node.children();
511 XMLNodeConstIterator niter;
512 XMLProperty const *name;
513 XMLProperty const *mod;
517 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
518 if ((*niter)->name() != X_("Modifier")) {
522 name = (*niter)->property (X_("name"));
523 mod = (*niter)->property (X_("modifier"));
526 SVAModifier svam (mod->value());
527 modifiers.insert (make_pair (name->value(), svam));
533 UIConfiguration::set_variables (const XMLNode& node)
535 #undef UI_CONFIG_VARIABLE
536 #define UI_CONFIG_VARIABLE(Type,var,name,val) if (var.set_from_node (node)) { ParameterChanged (name); }
537 #define CANVAS_FONT_VARIABLE(var,name) if (var.set_from_node (node)) { ParameterChanged (name); }
538 #include "ui_config_vars.h"
539 #include "canvas_vars.h"
540 #undef UI_CONFIG_VARIABLE
541 #undef CANVAS_FONT_VARIABLE
544 ArdourCanvas::SVAModifier
545 UIConfiguration::modifier (string const & name) const
547 Modifiers::const_iterator m = modifiers.find (name);
548 if (m != modifiers.end()) {
551 return SVAModifier ();
555 UIConfiguration::color_mod (std::string const & colorname, std::string const & modifiername) const
557 return HSV (color (colorname)).mod (modifier (modifiername)).color ();
561 UIConfiguration::color_mod (const ArdourCanvas::Color& color, std::string const & modifiername) const
563 return HSV (color).mod (modifier (modifiername)).color ();
567 UIConfiguration::color (const std::string& name, bool* failed) const
569 ColorAliases::const_iterator e = color_aliases.find (name);
575 if (e != color_aliases.end ()) {
576 Colors::const_iterator rc = colors.find (e->second);
577 if (rc != colors.end()) {
581 /* not an alias, try directly */
582 Colors::const_iterator rc = colors.find (name);
583 if (rc != colors.end()) {
589 /* only show this message if the caller wasn't interested in
592 cerr << string_compose (_("Color %1 not found"), name) << endl;
599 return rgba_to_color ((g_random_int()%256)/255.0,
600 (g_random_int()%256)/255.0,
601 (g_random_int()%256)/255.0,
606 UIConfiguration::quantized (Color c) const
609 hsv.h = hue_width * (round (hsv.h/hue_width));
614 UIConfiguration::set_color (string const& name, ArdourCanvas::Color color)
616 Colors::iterator i = colors.find (name);
617 if (i == colors.end()) {
621 colors_modified = true;
623 ARDOUR_UI_UTILS::ColorsChanged (); /* EMIT SIGNAL */
627 UIConfiguration::set_alias (string const & name, string const & alias)
629 ColorAliases::iterator i = color_aliases.find (name);
630 if (i == color_aliases.end()) {
635 aliases_modified = true;
637 ARDOUR_UI_UTILS::ColorsChanged (); /* EMIT SIGNAL */
641 UIConfiguration::set_modifier (string const & name, SVAModifier svam)
643 Modifiers::iterator m = modifiers.find (name);
645 if (m == modifiers.end()) {
650 modifiers_modified = true;
652 ARDOUR_UI_UTILS::ColorsChanged (); /* EMIT SIGNAL */
656 UIConfiguration::load_rc_file (bool themechange, bool allow_own)
658 string basename = ui_rc_file.get();
659 std::string rc_file_path;
661 if (!find_file (ardour_config_search_path(), basename, rc_file_path)) {
662 warning << string_compose (_("Unable to find UI style file %1 in search path %2. %3 will look strange"),
663 basename, ardour_config_search_path().to_string(), PROGRAM_NAME)
668 info << "Loading ui configuration file " << rc_file_path << endmsg;
670 Gtkmm2ext::UI::instance()->load_rcfile (rc_file_path, themechange);