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));
83 UIConfiguration::~UIConfiguration ()
88 UIConfiguration::colors_changed ()
92 /* In theory, one of these ought to work:
94 gtk_rc_reparse_all_for_settings (gtk_settings_get_default(), true);
95 gtk_rc_reset_styles (gtk_settings_get_default());
97 but in practice, neither of them do. So just reload the current
98 GTK RC file, which causes a reset of all styles and a redraw
101 parameter_changed ("ui-rc-file");
105 UIConfiguration::parameter_changed (string param)
109 if (param == "ui-rc-file") {
111 } else if (param == "color-file") {
119 UIConfiguration::reset_gtk_theme ()
123 ss << "gtk_color_scheme = \"" << hex;
125 for (ColorAliases::iterator g = color_aliases.begin(); g != color_aliases.end(); ++g) {
127 if (g->first.find ("gtk_") == 0) {
128 ColorAliases::const_iterator a = color_aliases.find (g->first);
129 const string gtk_name = g->first.substr (4);
130 ss << gtk_name << ":#" << std::setw (6) << setfill ('0') << (color (g->second) >> 8) << ';';
134 ss << '"' << dec << endl;
136 /* reset GTK color scheme */
138 Gtk::Settings::get_default()->property_gtk_color_scheme() = ss.str();
142 UIConfiguration::map_parameters (boost::function<void (std::string)>& functor)
144 #undef UI_CONFIG_VARIABLE
145 #define UI_CONFIG_VARIABLE(Type,var,Name,value) functor (Name);
146 #include "ui_config_vars.h"
147 #undef UI_CONFIG_VARIABLE
151 UIConfiguration::pre_gui_init ()
153 if (get_buggy_gradients()) {
154 g_setenv ("FORCE_BUGGY_GRADIENTS", "1", 1);
161 UIConfiguration::post_gui_init ()
168 UIConfiguration::load_defaults ()
173 if (find_file (ardour_config_search_path(), default_ui_config_file_name, rcfile) ) {
176 info << string_compose (_("Loading default ui configuration file %1"), rcfile) << endmsg;
178 if (!tree.read (rcfile.c_str())) {
179 error << string_compose(_("cannot read default ui configuration file \"%1\""), rcfile) << endmsg;
181 if (set_state (*tree.root(), Stateful::loading_state_version)) {
182 error << string_compose(_("default ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
190 warning << string_compose (_("Could not find default UI configuration file %1"), default_ui_config_file_name) << endmsg;
195 /* reload color theme */
196 load_color_theme (false);
197 ARDOUR_UI_UTILS::ColorsChanged (); /* EMIT SIGNAL */
204 UIConfiguration::load_color_theme (bool allow_own)
212 basename += color_file.get();
213 basename += ".colors";
215 if (find_file (ardour_config_search_path(), basename, cfile)) {
221 basename = color_file.get();
222 basename += ".colors";
224 if (find_file (ardour_config_search_path(), basename, cfile)) {
233 info << string_compose (_("Loading color file %1"), cfile) << endmsg;
235 if (!tree.read (cfile.c_str())) {
236 error << string_compose(_("cannot read color file \"%1\""), cfile) << endmsg;
240 if (set_state (*tree.root(), Stateful::loading_state_version)) {
241 error << string_compose(_("color file \"%1\" not loaded successfully."), cfile) << endmsg;
245 ARDOUR_UI_UTILS::ColorsChanged ();
247 warning << string_compose (_("Color file %1 not found"), basename) << endmsg;
254 UIConfiguration::store_color_theme ()
257 LocaleGuard lg (X_("C"));
259 root = new XMLNode("Ardour");
261 XMLNode* parent = new XMLNode (X_("Colors"));
262 for (Colors::const_iterator i = colors.begin(); i != colors.end(); ++i) {
263 XMLNode* node = new XMLNode (X_("Color"));
264 node->add_property (X_("name"), i->first);
266 ss << "0x" << setw (8) << setfill ('0') << hex << i->second;
267 node->add_property (X_("value"), ss.str());
268 parent->add_child_nocopy (*node);
270 root->add_child_nocopy (*parent);
272 parent = new XMLNode (X_("ColorAliases"));
273 for (ColorAliases::const_iterator i = color_aliases.begin(); i != color_aliases.end(); ++i) {
274 XMLNode* node = new XMLNode (X_("ColorAlias"));
275 node->add_property (X_("name"), i->first);
276 node->add_property (X_("alias"), i->second);
277 parent->add_child_nocopy (*node);
279 root->add_child_nocopy (*parent);
281 parent = new XMLNode (X_("Modifiers"));
282 for (Modifiers::const_iterator i = modifiers.begin(); i != modifiers.end(); ++i) {
283 XMLNode* node = new XMLNode (X_("Modifier"));
284 node->add_property (X_("name"), i->first);
285 node->add_property (X_("modifier"), i->second.to_string());
286 parent->add_child_nocopy (*node);
288 root->add_child_nocopy (*parent);
291 std::string colorfile = Glib::build_filename (user_config_directory(), (string ("my-") + color_file.get() + ".colors"));
293 tree.set_root (root);
295 if (!tree.write (colorfile.c_str())){
296 error << string_compose (_("Color file %1 not saved"), colorfile) << endmsg;
304 UIConfiguration::load_state ()
310 if (find_file (ardour_config_search_path(), default_ui_config_file_name, rcfile)) {
314 info << string_compose (_("Loading default ui configuration file %1"), rcfile) << endmsg;
316 if (!tree.read (rcfile.c_str())) {
317 error << string_compose(_("cannot read default ui configuration file \"%1\""), rcfile) << endmsg;
321 if (set_state (*tree.root(), Stateful::loading_state_version)) {
322 error << string_compose(_("default ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
327 if (find_file (ardour_config_search_path(), ui_config_file_name, rcfile)) {
331 info << string_compose (_("Loading user ui configuration file %1"), rcfile) << endmsg;
333 if (!tree.read (rcfile)) {
334 error << string_compose(_("cannot read ui configuration file \"%1\""), rcfile) << endmsg;
338 if (set_state (*tree.root(), Stateful::loading_state_version)) {
339 error << string_compose(_("user ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
347 error << _("could not find any ui configuration file, canvas will look broken.") << endmsg;
354 UIConfiguration::save_state()
358 std::string rcfile = Glib::build_filename (user_config_directory(), ui_config_file_name);
362 tree.set_root (&get_state());
364 if (!tree.write (rcfile.c_str())){
365 error << string_compose (_("Config file %1 not saved"), rcfile) << endmsg;
372 if (aliases_modified || colors_modified || modifiers_modified) {
374 if (store_color_theme ()) {
375 error << string_compose (_("Color file %1 not saved"), color_file.get()) << endmsg;
379 aliases_modified = false;
380 colors_modified = false;
381 modifiers_modified = false;
389 UIConfiguration::get_state ()
392 LocaleGuard lg (X_("C"));
394 root = new XMLNode("Ardour");
396 root->add_child_nocopy (get_variables ("UI"));
397 root->add_child_nocopy (get_variables ("Canvas"));
400 root->add_child_copy (*_extra_xml);
407 UIConfiguration::get_variables (std::string which_node)
410 LocaleGuard lg (X_("C"));
412 node = new XMLNode (which_node);
414 #undef UI_CONFIG_VARIABLE
415 #undef CANVAS_FONT_VARIABLE
416 #define UI_CONFIG_VARIABLE(Type,var,Name,value) if (node->name() == "UI") { var.add_to_node (*node); }
417 #define CANVAS_FONT_VARIABLE(var,Name) if (node->name() == "Canvas") { var.add_to_node (*node); }
418 #include "ui_config_vars.h"
419 #include "canvas_vars.h"
420 #undef UI_CONFIG_VARIABLE
421 #undef CANVAS_FONT_VARIABLE
427 UIConfiguration::set_state (const XMLNode& root, int /*version*/)
429 /* this can load a generic UI configuration file or a colors file */
431 if (root.name() != "Ardour") {
435 Stateful::save_extra_xml (root);
437 XMLNodeList nlist = root.children();
438 XMLNodeConstIterator niter;
441 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
445 if (node->name() == "Canvas" || node->name() == "UI") {
446 set_variables (*node);
451 XMLNode* colors = find_named_node (root, X_("Colors"));
454 load_colors (*colors);
457 XMLNode* aliases = find_named_node (root, X_("ColorAliases"));
460 load_color_aliases (*aliases);
463 XMLNode* modifiers = find_named_node (root, X_("Modifiers"));
466 load_modifiers (*modifiers);
473 UIConfiguration::load_color_aliases (XMLNode const & node)
475 XMLNodeList const nlist = node.children();
476 XMLNodeConstIterator niter;
477 XMLProperty const *name;
478 XMLProperty const *alias;
480 color_aliases.clear ();
482 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
483 if ((*niter)->name() != X_("ColorAlias")) {
486 name = (*niter)->property (X_("name"));
487 alias = (*niter)->property (X_("alias"));
490 color_aliases.insert (make_pair (name->value(), alias->value()));
496 UIConfiguration::load_colors (XMLNode const & node)
498 XMLNodeList const nlist = node.children();
499 XMLNodeConstIterator niter;
500 XMLProperty const *name;
501 XMLProperty const *color;
505 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
506 if ((*niter)->name() != X_("Color")) {
509 name = (*niter)->property (X_("name"));
510 color = (*niter)->property (X_("value"));
513 ArdourCanvas::Color c;
514 c = strtoul (color->value().c_str(), 0, 16);
515 colors.insert (make_pair (name->value(), c));
521 UIConfiguration::load_modifiers (XMLNode const & node)
523 PBD::LocaleGuard lg ("C");
524 XMLNodeList const nlist = node.children();
525 XMLNodeConstIterator niter;
526 XMLProperty const *name;
527 XMLProperty const *mod;
531 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
532 if ((*niter)->name() != X_("Modifier")) {
536 name = (*niter)->property (X_("name"));
537 mod = (*niter)->property (X_("modifier"));
540 SVAModifier svam (mod->value());
541 modifiers.insert (make_pair (name->value(), svam));
547 UIConfiguration::set_variables (const XMLNode& node)
549 #undef UI_CONFIG_VARIABLE
550 #define UI_CONFIG_VARIABLE(Type,var,name,val) if (var.set_from_node (node)) { ParameterChanged (name); }
551 #define CANVAS_FONT_VARIABLE(var,name) if (var.set_from_node (node)) { ParameterChanged (name); }
552 #include "ui_config_vars.h"
553 #include "canvas_vars.h"
554 #undef UI_CONFIG_VARIABLE
555 #undef CANVAS_FONT_VARIABLE
558 ArdourCanvas::SVAModifier
559 UIConfiguration::modifier (string const & name) const
561 Modifiers::const_iterator m = modifiers.find (name);
562 if (m != modifiers.end()) {
565 return SVAModifier ();
569 UIConfiguration::color_mod (std::string const & colorname, std::string const & modifiername) const
571 return HSV (color (colorname)).mod (modifier (modifiername)).color ();
575 UIConfiguration::color_mod (const ArdourCanvas::Color& color, std::string const & modifiername) const
577 return HSV (color).mod (modifier (modifiername)).color ();
581 UIConfiguration::color (const std::string& name, bool* failed) const
583 ColorAliases::const_iterator e = color_aliases.find (name);
589 if (e != color_aliases.end ()) {
590 Colors::const_iterator rc = colors.find (e->second);
591 if (rc != colors.end()) {
595 /* not an alias, try directly */
596 Colors::const_iterator rc = colors.find (name);
597 if (rc != colors.end()) {
603 /* only show this message if the caller wasn't interested in
606 cerr << string_compose (_("Color %1 not found"), name) << endl;
613 return rgba_to_color ((g_random_int()%256)/255.0,
614 (g_random_int()%256)/255.0,
615 (g_random_int()%256)/255.0,
620 UIConfiguration::quantized (Color c) const
623 hsv.h = hue_width * (round (hsv.h/hue_width));
628 UIConfiguration::set_color (string const& name, ArdourCanvas::Color color)
630 Colors::iterator i = colors.find (name);
631 if (i == colors.end()) {
635 colors_modified = true;
637 ARDOUR_UI_UTILS::ColorsChanged (); /* EMIT SIGNAL */
641 UIConfiguration::set_alias (string const & name, string const & alias)
643 ColorAliases::iterator i = color_aliases.find (name);
644 if (i == color_aliases.end()) {
649 aliases_modified = true;
651 ARDOUR_UI_UTILS::ColorsChanged (); /* EMIT SIGNAL */
655 UIConfiguration::set_modifier (string const & name, SVAModifier svam)
657 Modifiers::iterator m = modifiers.find (name);
659 if (m == modifiers.end()) {
664 modifiers_modified = true;
666 ARDOUR_UI_UTILS::ColorsChanged (); /* EMIT SIGNAL */
670 UIConfiguration::load_rc_file (bool themechange, bool allow_own)
672 string basename = ui_rc_file.get();
673 std::string rc_file_path;
675 if (!find_file (ardour_config_search_path(), basename, rc_file_path)) {
676 warning << string_compose (_("Unable to find UI style file %1 in search path %2. %3 will look strange"),
677 basename, ardour_config_search_path().to_string(), PROGRAM_NAME)
682 info << "Loading ui configuration file " << rc_file_path << endmsg;
684 Gtkmm2ext::UI::instance()->load_rcfile (rc_file_path, themechange);