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.
20 #if !defined USE_CAIRO_IMAGE_SURFACE && !defined NDEBUG
21 #define OPTIONAL_CAIRO_IMAGE_SURFACE
28 #include <cstdio> /* for snprintf, grrr */
30 #include <cairo/cairo.h>
32 #include <glibmm/miscutils.h>
33 #include <glib/gstdio.h>
35 #include "pbd/convert.h"
36 #include "pbd/failed_constructor.h"
37 #include "pbd/xml++.h"
38 #include "pbd/file_utils.h"
39 #include "pbd/error.h"
40 #include "pbd/stacktrace.h"
42 #include "gtkmm2ext/rgb_macros.h"
43 #include "gtkmm2ext/gtk_ui.h"
45 #include "ardour/filesystem_paths.h"
47 #include "ardour_ui.h"
48 #include "global_signals.h"
49 #include "ui_config.h"
55 using namespace ARDOUR;
56 using namespace ArdourCanvas;
58 static const char* ui_config_file_name = "ui_config";
59 static const char* default_ui_config_file_name = "default_ui_config";
60 UIConfiguration* UIConfiguration::_instance = 0;
62 static const double hue_width = 18.0;
64 UIConfiguration::UIConfiguration ()
66 #undef UI_CONFIG_VARIABLE
67 #define UI_CONFIG_VARIABLE(Type,var,name,val) var (name,val),
68 #define CANVAS_FONT_VARIABLE(var,name) var (name),
69 #include "ui_config_vars.h"
70 #include "canvas_vars.h"
71 #undef UI_CONFIG_VARIABLE
72 #undef CANVAS_FONT_VARIABLE
75 aliases_modified (false),
76 colors_modified (false),
77 modifiers_modified (false),
84 ARDOUR_UI_UTILS::ColorsChanged.connect (boost::bind (&UIConfiguration::colors_changed, this));
86 ParameterChanged.connect (sigc::mem_fun (*this, &UIConfiguration::parameter_changed));
89 UIConfiguration::~UIConfiguration ()
94 UIConfiguration::colors_changed ()
98 /* In theory, one of these ought to work:
100 gtk_rc_reparse_all_for_settings (gtk_settings_get_default(), true);
101 gtk_rc_reset_styles (gtk_settings_get_default());
103 but in practice, neither of them do. So just reload the current
104 GTK RC file, which causes a reset of all styles and a redraw
107 parameter_changed ("ui-rc-file");
111 UIConfiguration::parameter_changed (string param)
115 if (param == "ui-rc-file") {
117 } else if (param == "color-file") {
125 UIConfiguration::reset_gtk_theme ()
129 ss << "gtk_color_scheme = \"" << hex;
131 for (ColorAliases::iterator g = color_aliases.begin(); g != color_aliases.end(); ++g) {
133 if (g->first.find ("gtk_") == 0) {
134 ColorAliases::const_iterator a = color_aliases.find (g->first);
135 const string gtk_name = g->first.substr (4);
136 ss << gtk_name << ":#" << std::setw (6) << setfill ('0') << (color (g->second) >> 8) << ';';
140 ss << '"' << dec << endl;
142 /* reset GTK color scheme */
144 Gtk::Settings::get_default()->property_gtk_color_scheme() = ss.str();
148 UIConfiguration::map_parameters (boost::function<void (std::string)>& functor)
150 #undef UI_CONFIG_VARIABLE
151 #define UI_CONFIG_VARIABLE(Type,var,Name,value) functor (Name);
152 #include "ui_config_vars.h"
153 #undef UI_CONFIG_VARIABLE
157 UIConfiguration::pre_gui_init ()
159 #ifdef CAIRO_SUPPORTS_FORCE_BUGGY_GRADIENTS_ENVIRONMENT_VARIABLE
160 if (get_buggy_gradients()) {
161 g_setenv ("FORCE_BUGGY_GRADIENTS", "1", 1);
164 #ifdef OPTIONAL_CAIRO_IMAGE_SURFACE
165 if (get_cairo_image_surface()) {
166 g_setenv ("ARDOUR_IMAGE_SURFACE", "1", 1);
173 UIConfiguration::post_gui_init ()
180 UIConfiguration::load_defaults ()
185 if (find_file (ardour_config_search_path(), default_ui_config_file_name, rcfile) ) {
188 info << string_compose (_("Loading default ui configuration file %1"), rcfile) << endmsg;
190 if (!tree.read (rcfile.c_str())) {
191 error << string_compose(_("cannot read default ui configuration file \"%1\""), rcfile) << endmsg;
193 if (set_state (*tree.root(), Stateful::loading_state_version)) {
194 error << string_compose(_("default ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
202 warning << string_compose (_("Could not find default UI configuration file %1"), default_ui_config_file_name) << endmsg;
207 /* reload color theme */
208 load_color_theme (false);
209 ARDOUR_UI_UTILS::ColorsChanged (); /* EMIT SIGNAL */
216 UIConfiguration::load_color_theme (bool allow_own)
224 basename += color_file.get();
225 basename += ".colors";
227 if (find_file (ardour_config_search_path(), basename, cfile)) {
233 basename = color_file.get();
234 basename += ".colors";
236 if (find_file (ardour_config_search_path(), basename, cfile)) {
245 info << string_compose (_("Loading color file %1"), cfile) << endmsg;
247 if (!tree.read (cfile.c_str())) {
248 error << string_compose(_("cannot read color file \"%1\""), cfile) << endmsg;
252 if (set_state (*tree.root(), Stateful::loading_state_version)) {
253 error << string_compose(_("color file \"%1\" not loaded successfully."), cfile) << endmsg;
257 ARDOUR_UI_UTILS::ColorsChanged ();
259 warning << string_compose (_("Color file %1 not found"), basename) << endmsg;
266 UIConfiguration::store_color_theme ()
269 LocaleGuard lg (X_("C"));
271 root = new XMLNode("Ardour");
273 XMLNode* parent = new XMLNode (X_("Colors"));
274 for (Colors::const_iterator i = colors.begin(); i != colors.end(); ++i) {
275 XMLNode* node = new XMLNode (X_("Color"));
276 node->add_property (X_("name"), i->first);
278 ss << "0x" << setw (8) << setfill ('0') << hex << i->second;
279 node->add_property (X_("value"), ss.str());
280 parent->add_child_nocopy (*node);
282 root->add_child_nocopy (*parent);
284 parent = new XMLNode (X_("ColorAliases"));
285 for (ColorAliases::const_iterator i = color_aliases.begin(); i != color_aliases.end(); ++i) {
286 XMLNode* node = new XMLNode (X_("ColorAlias"));
287 node->add_property (X_("name"), i->first);
288 node->add_property (X_("alias"), i->second);
289 parent->add_child_nocopy (*node);
291 root->add_child_nocopy (*parent);
293 parent = new XMLNode (X_("Modifiers"));
294 for (Modifiers::const_iterator i = modifiers.begin(); i != modifiers.end(); ++i) {
295 XMLNode* node = new XMLNode (X_("Modifier"));
296 node->add_property (X_("name"), i->first);
297 node->add_property (X_("modifier"), i->second.to_string());
298 parent->add_child_nocopy (*node);
300 root->add_child_nocopy (*parent);
303 std::string colorfile = Glib::build_filename (user_config_directory(), (string ("my-") + color_file.get() + ".colors"));
305 tree.set_root (root);
307 if (!tree.write (colorfile.c_str())){
308 error << string_compose (_("Color file %1 not saved"), colorfile) << endmsg;
316 UIConfiguration::load_state ()
322 if (find_file (ardour_config_search_path(), default_ui_config_file_name, rcfile)) {
326 info << string_compose (_("Loading default ui configuration file %1"), rcfile) << endmsg;
328 if (!tree.read (rcfile.c_str())) {
329 error << string_compose(_("cannot read default ui configuration file \"%1\""), rcfile) << endmsg;
333 if (set_state (*tree.root(), Stateful::loading_state_version)) {
334 error << string_compose(_("default ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
339 if (find_file (ardour_config_search_path(), ui_config_file_name, rcfile)) {
343 info << string_compose (_("Loading user ui configuration file %1"), rcfile) << endmsg;
345 if (!tree.read (rcfile)) {
346 error << string_compose(_("cannot read ui configuration file \"%1\""), rcfile) << endmsg;
350 if (set_state (*tree.root(), Stateful::loading_state_version)) {
351 error << string_compose(_("user ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
359 error << _("could not find any ui configuration file, canvas will look broken.") << endmsg;
366 UIConfiguration::save_state()
370 std::string rcfile = Glib::build_filename (user_config_directory(), ui_config_file_name);
374 tree.set_root (&get_state());
376 if (!tree.write (rcfile.c_str())){
377 error << string_compose (_("Config file %1 not saved"), rcfile) << endmsg;
384 if (aliases_modified || colors_modified || modifiers_modified) {
386 if (store_color_theme ()) {
387 error << string_compose (_("Color file %1 not saved"), color_file.get()) << endmsg;
391 aliases_modified = false;
392 colors_modified = false;
393 modifiers_modified = false;
401 UIConfiguration::get_state ()
404 LocaleGuard lg (X_("C"));
406 root = new XMLNode("Ardour");
408 root->add_child_nocopy (get_variables ("UI"));
409 root->add_child_nocopy (get_variables ("Canvas"));
412 root->add_child_copy (*_extra_xml);
419 UIConfiguration::get_variables (std::string which_node)
422 LocaleGuard lg (X_("C"));
424 node = new XMLNode (which_node);
426 #undef UI_CONFIG_VARIABLE
427 #undef CANVAS_FONT_VARIABLE
428 #define UI_CONFIG_VARIABLE(Type,var,Name,value) if (node->name() == "UI") { var.add_to_node (*node); }
429 #define CANVAS_FONT_VARIABLE(var,Name) if (node->name() == "Canvas") { var.add_to_node (*node); }
430 #include "ui_config_vars.h"
431 #include "canvas_vars.h"
432 #undef UI_CONFIG_VARIABLE
433 #undef CANVAS_FONT_VARIABLE
439 UIConfiguration::set_state (const XMLNode& root, int /*version*/)
441 /* this can load a generic UI configuration file or a colors file */
443 if (root.name() != "Ardour") {
447 Stateful::save_extra_xml (root);
449 XMLNodeList nlist = root.children();
450 XMLNodeConstIterator niter;
453 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
457 if (node->name() == "Canvas" || node->name() == "UI") {
458 set_variables (*node);
463 XMLNode* colors = find_named_node (root, X_("Colors"));
466 load_colors (*colors);
469 XMLNode* aliases = find_named_node (root, X_("ColorAliases"));
472 load_color_aliases (*aliases);
475 XMLNode* modifiers = find_named_node (root, X_("Modifiers"));
478 load_modifiers (*modifiers);
485 UIConfiguration::load_color_aliases (XMLNode const & node)
487 XMLNodeList const nlist = node.children();
488 XMLNodeConstIterator niter;
489 XMLProperty const *name;
490 XMLProperty const *alias;
492 color_aliases.clear ();
494 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
495 if ((*niter)->name() != X_("ColorAlias")) {
498 name = (*niter)->property (X_("name"));
499 alias = (*niter)->property (X_("alias"));
502 color_aliases.insert (make_pair (name->value(), alias->value()));
508 UIConfiguration::load_colors (XMLNode const & node)
510 XMLNodeList const nlist = node.children();
511 XMLNodeConstIterator niter;
512 XMLProperty const *name;
513 XMLProperty const *color;
517 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
518 if ((*niter)->name() != X_("Color")) {
521 name = (*niter)->property (X_("name"));
522 color = (*niter)->property (X_("value"));
525 ArdourCanvas::Color c;
526 c = strtoul (color->value().c_str(), 0, 16);
527 colors.insert (make_pair (name->value(), c));
533 UIConfiguration::load_modifiers (XMLNode const & node)
535 PBD::LocaleGuard lg ("C");
536 XMLNodeList const nlist = node.children();
537 XMLNodeConstIterator niter;
538 XMLProperty const *name;
539 XMLProperty const *mod;
543 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
544 if ((*niter)->name() != X_("Modifier")) {
548 name = (*niter)->property (X_("name"));
549 mod = (*niter)->property (X_("modifier"));
552 SVAModifier svam (mod->value());
553 modifiers.insert (make_pair (name->value(), svam));
559 UIConfiguration::set_variables (const XMLNode& node)
561 #undef UI_CONFIG_VARIABLE
562 #define UI_CONFIG_VARIABLE(Type,var,name,val) if (var.set_from_node (node)) { ParameterChanged (name); }
563 #define CANVAS_FONT_VARIABLE(var,name) if (var.set_from_node (node)) { ParameterChanged (name); }
564 #include "ui_config_vars.h"
565 #include "canvas_vars.h"
566 #undef UI_CONFIG_VARIABLE
567 #undef CANVAS_FONT_VARIABLE
570 ArdourCanvas::SVAModifier
571 UIConfiguration::modifier (string const & name) const
573 Modifiers::const_iterator m = modifiers.find (name);
574 if (m != modifiers.end()) {
577 return SVAModifier ();
581 UIConfiguration::color_mod (std::string const & colorname, std::string const & modifiername) const
583 return HSV (color (colorname)).mod (modifier (modifiername)).color ();
587 UIConfiguration::color_mod (const ArdourCanvas::Color& color, std::string const & modifiername) const
589 return HSV (color).mod (modifier (modifiername)).color ();
593 UIConfiguration::color (const std::string& name, bool* failed) const
595 ColorAliases::const_iterator e = color_aliases.find (name);
601 if (e != color_aliases.end ()) {
602 Colors::const_iterator rc = colors.find (e->second);
603 if (rc != colors.end()) {
607 /* not an alias, try directly */
608 Colors::const_iterator rc = colors.find (name);
609 if (rc != colors.end()) {
615 /* only show this message if the caller wasn't interested in
618 cerr << string_compose (_("Color %1 not found"), name) << endl;
625 return rgba_to_color ((g_random_int()%256)/255.0,
626 (g_random_int()%256)/255.0,
627 (g_random_int()%256)/255.0,
632 UIConfiguration::quantized (Color c) const
635 hsv.h = hue_width * (round (hsv.h/hue_width));
640 UIConfiguration::set_color (string const& name, ArdourCanvas::Color color)
642 Colors::iterator i = colors.find (name);
643 if (i == colors.end()) {
647 colors_modified = true;
649 ARDOUR_UI_UTILS::ColorsChanged (); /* EMIT SIGNAL */
653 UIConfiguration::set_alias (string const & name, string const & alias)
655 ColorAliases::iterator i = color_aliases.find (name);
656 if (i == color_aliases.end()) {
661 aliases_modified = true;
663 ARDOUR_UI_UTILS::ColorsChanged (); /* EMIT SIGNAL */
667 UIConfiguration::set_modifier (string const & name, SVAModifier svam)
669 Modifiers::iterator m = modifiers.find (name);
671 if (m == modifiers.end()) {
676 modifiers_modified = true;
678 ARDOUR_UI_UTILS::ColorsChanged (); /* EMIT SIGNAL */
682 UIConfiguration::load_rc_file (bool themechange, bool allow_own)
684 string basename = ui_rc_file.get();
685 std::string rc_file_path;
687 if (!find_file (ardour_config_search_path(), basename, rc_file_path)) {
688 warning << string_compose (_("Unable to find UI style file %1 in search path %2. %3 will look strange"),
689 basename, ardour_config_search_path().to_string(), PROGRAM_NAME)
694 info << "Loading ui configuration file " << rc_file_path << endmsg;
696 Gtkmm2ext::UI::instance()->load_rcfile (rc_file_path, themechange);