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 <cairo/cairo.h>
28 #include <glibmm/miscutils.h>
29 #include <glib/gstdio.h>
31 #include "pbd/convert.h"
32 #include "pbd/failed_constructor.h"
33 #include "pbd/xml++.h"
34 #include "pbd/file_utils.h"
35 #include "pbd/error.h"
36 #include "pbd/stacktrace.h"
38 #include "gtkmm2ext/rgb_macros.h"
39 #include "gtkmm2ext/gtk_ui.h"
41 #include "ardour/filesystem_paths.h"
43 #include "ardour_ui.h"
44 #include "global_signals.h"
45 #include "ui_config.h"
51 using namespace ARDOUR;
52 using namespace ArdourCanvas;
54 static const char* ui_config_file_name = "ui_config";
55 static const char* default_ui_config_file_name = "default_ui_config";
56 UIConfiguration* UIConfiguration::_instance = 0;
58 static const double hue_width = 18.0;
60 UIConfiguration::UIConfiguration ()
62 #undef UI_CONFIG_VARIABLE
63 #define UI_CONFIG_VARIABLE(Type,var,name,val) var (name,val),
64 #define CANVAS_FONT_VARIABLE(var,name) var (name),
65 #include "ui_config_vars.h"
66 #include "canvas_vars.h"
67 #undef UI_CONFIG_VARIABLE
68 #undef CANVAS_FONT_VARIABLE
71 aliases_modified (false),
72 colors_modified (false),
73 modifiers_modified (false),
80 ARDOUR_UI_UTILS::ColorsChanged.connect (boost::bind (&UIConfiguration::colors_changed, this));
82 ParameterChanged.connect (sigc::mem_fun (*this, &UIConfiguration::parameter_changed));
85 UIConfiguration::~UIConfiguration ()
90 UIConfiguration::colors_changed ()
94 /* In theory, one of these ought to work:
96 gtk_rc_reparse_all_for_settings (gtk_settings_get_default(), true);
97 gtk_rc_reset_styles (gtk_settings_get_default());
99 but in practice, neither of them do. So just reload the current
100 GTK RC file, which causes a reset of all styles and a redraw
103 parameter_changed ("ui-rc-file");
107 UIConfiguration::parameter_changed (string param)
111 if (param == "ui-rc-file") {
113 } else if (param == "color-file") {
121 UIConfiguration::reset_gtk_theme ()
125 ss << "gtk_color_scheme = \"" << hex;
127 for (ColorAliases::iterator g = color_aliases.begin(); g != color_aliases.end(); ++g) {
129 if (g->first.find ("gtk_") == 0) {
130 ColorAliases::const_iterator a = color_aliases.find (g->first);
131 const string gtk_name = g->first.substr (4);
132 ss << gtk_name << ":#" << std::setw (6) << setfill ('0') << (color (g->second) >> 8) << ';';
136 ss << '"' << dec << endl;
138 /* reset GTK color scheme */
140 Gtk::Settings::get_default()->property_gtk_color_scheme() = ss.str();
144 UIConfiguration::map_parameters (boost::function<void (std::string)>& functor)
146 #undef UI_CONFIG_VARIABLE
147 #define UI_CONFIG_VARIABLE(Type,var,Name,value) functor (Name);
148 #include "ui_config_vars.h"
149 #undef UI_CONFIG_VARIABLE
153 UIConfiguration::pre_gui_init ()
155 #ifdef CAIRO_SUPPORTS_FORCE_BUGGY_GRADIENTS_ENVIRONMENT_VARIABLE
156 if (get_buggy_gradients()) {
157 g_setenv ("FORCE_BUGGY_GRADIENTS", "1", 1);
164 UIConfiguration::post_gui_init ()
171 UIConfiguration::load_defaults ()
176 if (find_file (ardour_config_search_path(), default_ui_config_file_name, rcfile) ) {
179 info << string_compose (_("Loading default ui configuration file %1"), rcfile) << endmsg;
181 if (!tree.read (rcfile.c_str())) {
182 error << string_compose(_("cannot read default ui configuration file \"%1\""), rcfile) << endmsg;
184 if (set_state (*tree.root(), Stateful::loading_state_version)) {
185 error << string_compose(_("default ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
193 warning << string_compose (_("Could not find default UI configuration file %1"), default_ui_config_file_name) << endmsg;
198 /* reload color theme */
199 load_color_theme (false);
200 ARDOUR_UI_UTILS::ColorsChanged (); /* EMIT SIGNAL */
207 UIConfiguration::load_color_theme (bool allow_own)
215 basename += color_file.get();
216 basename += ".colors";
218 if (find_file (ardour_config_search_path(), basename, cfile)) {
224 basename = color_file.get();
225 basename += ".colors";
227 if (find_file (ardour_config_search_path(), basename, cfile)) {
236 info << string_compose (_("Loading color file %1"), cfile) << endmsg;
238 if (!tree.read (cfile.c_str())) {
239 error << string_compose(_("cannot read color file \"%1\""), cfile) << endmsg;
243 if (set_state (*tree.root(), Stateful::loading_state_version)) {
244 error << string_compose(_("color file \"%1\" not loaded successfully."), cfile) << endmsg;
248 ARDOUR_UI_UTILS::ColorsChanged ();
250 warning << string_compose (_("Color file %1 not found"), basename) << endmsg;
257 UIConfiguration::store_color_theme ()
260 LocaleGuard lg (X_("C"));
262 root = new XMLNode("Ardour");
264 XMLNode* parent = new XMLNode (X_("Colors"));
265 for (Colors::const_iterator i = colors.begin(); i != colors.end(); ++i) {
266 XMLNode* node = new XMLNode (X_("Color"));
267 node->add_property (X_("name"), i->first);
269 ss << "0x" << setw (8) << setfill ('0') << hex << i->second;
270 node->add_property (X_("value"), ss.str());
271 parent->add_child_nocopy (*node);
273 root->add_child_nocopy (*parent);
275 parent = new XMLNode (X_("ColorAliases"));
276 for (ColorAliases::const_iterator i = color_aliases.begin(); i != color_aliases.end(); ++i) {
277 XMLNode* node = new XMLNode (X_("ColorAlias"));
278 node->add_property (X_("name"), i->first);
279 node->add_property (X_("alias"), i->second);
280 parent->add_child_nocopy (*node);
282 root->add_child_nocopy (*parent);
284 parent = new XMLNode (X_("Modifiers"));
285 for (Modifiers::const_iterator i = modifiers.begin(); i != modifiers.end(); ++i) {
286 XMLNode* node = new XMLNode (X_("Modifier"));
287 node->add_property (X_("name"), i->first);
288 node->add_property (X_("modifier"), i->second.to_string());
289 parent->add_child_nocopy (*node);
291 root->add_child_nocopy (*parent);
294 std::string colorfile = Glib::build_filename (user_config_directory(), (string ("my-") + color_file.get() + ".colors"));
296 tree.set_root (root);
298 if (!tree.write (colorfile.c_str())){
299 error << string_compose (_("Color file %1 not saved"), colorfile) << endmsg;
307 UIConfiguration::load_state ()
313 if (find_file (ardour_config_search_path(), default_ui_config_file_name, rcfile)) {
317 info << string_compose (_("Loading default ui configuration file %1"), rcfile) << endmsg;
319 if (!tree.read (rcfile.c_str())) {
320 error << string_compose(_("cannot read default ui configuration file \"%1\""), rcfile) << endmsg;
324 if (set_state (*tree.root(), Stateful::loading_state_version)) {
325 error << string_compose(_("default ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
330 if (find_file (ardour_config_search_path(), ui_config_file_name, rcfile)) {
334 info << string_compose (_("Loading user ui configuration file %1"), rcfile) << endmsg;
336 if (!tree.read (rcfile)) {
337 error << string_compose(_("cannot read ui configuration file \"%1\""), rcfile) << endmsg;
341 if (set_state (*tree.root(), Stateful::loading_state_version)) {
342 error << string_compose(_("user ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
350 error << _("could not find any ui configuration file, canvas will look broken.") << endmsg;
357 UIConfiguration::save_state()
361 std::string rcfile = Glib::build_filename (user_config_directory(), ui_config_file_name);
365 tree.set_root (&get_state());
367 if (!tree.write (rcfile.c_str())){
368 error << string_compose (_("Config file %1 not saved"), rcfile) << endmsg;
375 if (aliases_modified || colors_modified || modifiers_modified) {
377 if (store_color_theme ()) {
378 error << string_compose (_("Color file %1 not saved"), color_file.get()) << endmsg;
382 aliases_modified = false;
383 colors_modified = false;
384 modifiers_modified = false;
392 UIConfiguration::get_state ()
395 LocaleGuard lg (X_("C"));
397 root = new XMLNode("Ardour");
399 root->add_child_nocopy (get_variables ("UI"));
400 root->add_child_nocopy (get_variables ("Canvas"));
403 root->add_child_copy (*_extra_xml);
410 UIConfiguration::get_variables (std::string which_node)
413 LocaleGuard lg (X_("C"));
415 node = new XMLNode (which_node);
417 #undef UI_CONFIG_VARIABLE
418 #undef CANVAS_FONT_VARIABLE
419 #define UI_CONFIG_VARIABLE(Type,var,Name,value) if (node->name() == "UI") { var.add_to_node (*node); }
420 #define CANVAS_FONT_VARIABLE(var,Name) if (node->name() == "Canvas") { var.add_to_node (*node); }
421 #include "ui_config_vars.h"
422 #include "canvas_vars.h"
423 #undef UI_CONFIG_VARIABLE
424 #undef CANVAS_FONT_VARIABLE
430 UIConfiguration::set_state (const XMLNode& root, int /*version*/)
432 /* this can load a generic UI configuration file or a colors file */
434 if (root.name() != "Ardour") {
438 Stateful::save_extra_xml (root);
440 XMLNodeList nlist = root.children();
441 XMLNodeConstIterator niter;
444 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
448 if (node->name() == "Canvas" || node->name() == "UI") {
449 set_variables (*node);
454 XMLNode* colors = find_named_node (root, X_("Colors"));
457 load_colors (*colors);
460 XMLNode* aliases = find_named_node (root, X_("ColorAliases"));
463 load_color_aliases (*aliases);
466 XMLNode* modifiers = find_named_node (root, X_("Modifiers"));
469 load_modifiers (*modifiers);
476 UIConfiguration::load_color_aliases (XMLNode const & node)
478 XMLNodeList const nlist = node.children();
479 XMLNodeConstIterator niter;
480 XMLProperty const *name;
481 XMLProperty const *alias;
483 color_aliases.clear ();
485 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
486 if ((*niter)->name() != X_("ColorAlias")) {
489 name = (*niter)->property (X_("name"));
490 alias = (*niter)->property (X_("alias"));
493 color_aliases.insert (make_pair (name->value(), alias->value()));
499 UIConfiguration::load_colors (XMLNode const & node)
501 XMLNodeList const nlist = node.children();
502 XMLNodeConstIterator niter;
503 XMLProperty const *name;
504 XMLProperty const *color;
508 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
509 if ((*niter)->name() != X_("Color")) {
512 name = (*niter)->property (X_("name"));
513 color = (*niter)->property (X_("value"));
516 ArdourCanvas::Color c;
517 c = strtoul (color->value().c_str(), 0, 16);
518 colors.insert (make_pair (name->value(), c));
524 UIConfiguration::load_modifiers (XMLNode const & node)
526 PBD::LocaleGuard lg ("C");
527 XMLNodeList const nlist = node.children();
528 XMLNodeConstIterator niter;
529 XMLProperty const *name;
530 XMLProperty const *mod;
534 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
535 if ((*niter)->name() != X_("Modifier")) {
539 name = (*niter)->property (X_("name"));
540 mod = (*niter)->property (X_("modifier"));
543 SVAModifier svam (mod->value());
544 modifiers.insert (make_pair (name->value(), svam));
550 UIConfiguration::set_variables (const XMLNode& node)
552 #undef UI_CONFIG_VARIABLE
553 #define UI_CONFIG_VARIABLE(Type,var,name,val) if (var.set_from_node (node)) { ParameterChanged (name); }
554 #define CANVAS_FONT_VARIABLE(var,name) if (var.set_from_node (node)) { ParameterChanged (name); }
555 #include "ui_config_vars.h"
556 #include "canvas_vars.h"
557 #undef UI_CONFIG_VARIABLE
558 #undef CANVAS_FONT_VARIABLE
561 ArdourCanvas::SVAModifier
562 UIConfiguration::modifier (string const & name) const
564 Modifiers::const_iterator m = modifiers.find (name);
565 if (m != modifiers.end()) {
568 return SVAModifier ();
572 UIConfiguration::color_mod (std::string const & colorname, std::string const & modifiername) const
574 return HSV (color (colorname)).mod (modifier (modifiername)).color ();
578 UIConfiguration::color_mod (const ArdourCanvas::Color& color, std::string const & modifiername) const
580 return HSV (color).mod (modifier (modifiername)).color ();
584 UIConfiguration::color (const std::string& name, bool* failed) const
586 ColorAliases::const_iterator e = color_aliases.find (name);
592 if (e != color_aliases.end ()) {
593 Colors::const_iterator rc = colors.find (e->second);
594 if (rc != colors.end()) {
598 /* not an alias, try directly */
599 Colors::const_iterator rc = colors.find (name);
600 if (rc != colors.end()) {
606 /* only show this message if the caller wasn't interested in
609 cerr << string_compose (_("Color %1 not found"), name) << endl;
616 return rgba_to_color ((g_random_int()%256)/255.0,
617 (g_random_int()%256)/255.0,
618 (g_random_int()%256)/255.0,
623 UIConfiguration::quantized (Color c) const
626 hsv.h = hue_width * (round (hsv.h/hue_width));
631 UIConfiguration::set_color (string const& name, ArdourCanvas::Color color)
633 Colors::iterator i = colors.find (name);
634 if (i == colors.end()) {
638 colors_modified = true;
640 ARDOUR_UI_UTILS::ColorsChanged (); /* EMIT SIGNAL */
644 UIConfiguration::set_alias (string const & name, string const & alias)
646 ColorAliases::iterator i = color_aliases.find (name);
647 if (i == color_aliases.end()) {
652 aliases_modified = true;
654 ARDOUR_UI_UTILS::ColorsChanged (); /* EMIT SIGNAL */
658 UIConfiguration::set_modifier (string const & name, SVAModifier svam)
660 Modifiers::iterator m = modifiers.find (name);
662 if (m == modifiers.end()) {
667 modifiers_modified = true;
669 ARDOUR_UI_UTILS::ColorsChanged (); /* EMIT SIGNAL */
673 UIConfiguration::load_rc_file (bool themechange, bool allow_own)
675 string basename = ui_rc_file.get();
676 std::string rc_file_path;
678 if (!find_file (ardour_config_search_path(), basename, rc_file_path)) {
679 warning << string_compose (_("Unable to find UI style file %1 in search path %2. %3 will look strange"),
680 basename, ardour_config_search_path().to_string(), PROGRAM_NAME)
685 info << "Loading ui configuration file " << rc_file_path << endmsg;
687 Gtkmm2ext::UI::instance()->load_rcfile (rc_file_path, themechange);