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 <pango/pangoft2.h> // for fontmap resolution control for GnomeCanvas
33 #include <pango/pangocairo.h> // for fontmap resolution control for GnomeCanvas
35 #include <glibmm/miscutils.h>
36 #include <glib/gstdio.h>
38 #include "pbd/convert.h"
39 #include "pbd/failed_constructor.h"
40 #include "pbd/xml++.h"
41 #include "pbd/file_utils.h"
42 #include "pbd/error.h"
43 #include "pbd/stacktrace.h"
45 #include "gtkmm2ext/rgb_macros.h"
46 #include "gtkmm2ext/gtk_ui.h"
48 #include "ardour/filesystem_paths.h"
50 #include "ardour_ui.h"
51 #include "global_signals.h"
52 #include "ui_config.h"
58 using namespace ARDOUR;
59 using namespace ArdourCanvas;
61 static const char* ui_config_file_name = "ui_config";
62 static const char* default_ui_config_file_name = "default_ui_config";
63 UIConfiguration* UIConfiguration::_instance = 0;
65 static const double hue_width = 18.0;
67 UIConfiguration::UIConfiguration ()
69 #undef UI_CONFIG_VARIABLE
70 #define UI_CONFIG_VARIABLE(Type,var,name,val) var (name,val),
71 #define CANVAS_FONT_VARIABLE(var,name) var (name),
72 #include "ui_config_vars.h"
73 #include "canvas_vars.h"
74 #undef UI_CONFIG_VARIABLE
75 #undef CANVAS_FONT_VARIABLE
78 aliases_modified (false),
79 colors_modified (false),
80 modifiers_modified (false),
87 ARDOUR_UI_UTILS::ColorsChanged.connect (boost::bind (&UIConfiguration::colors_changed, this));
89 ParameterChanged.connect (sigc::mem_fun (*this, &UIConfiguration::parameter_changed));
92 UIConfiguration::~UIConfiguration ()
97 UIConfiguration::colors_changed ()
101 /* In theory, one of these ought to work:
103 gtk_rc_reparse_all_for_settings (gtk_settings_get_default(), true);
104 gtk_rc_reset_styles (gtk_settings_get_default());
106 but in practice, neither of them do. So just reload the current
107 GTK RC file, which causes a reset of all styles and a redraw
110 parameter_changed ("ui-rc-file");
114 UIConfiguration::parameter_changed (string param)
118 if (param == "ui-rc-file") {
120 } else if (param == "color-file") {
128 UIConfiguration::reset_gtk_theme ()
132 ss << "gtk_color_scheme = \"" << hex;
134 for (ColorAliases::iterator g = color_aliases.begin(); g != color_aliases.end(); ++g) {
136 if (g->first.find ("gtk_") == 0) {
137 const string gtk_name = g->first.substr (4);
138 ss << gtk_name << ":#" << std::setw (6) << setfill ('0') << (color (g->second) >> 8) << ';';
142 ss << '"' << dec << endl;
144 /* reset GTK color scheme */
146 Gtk::Settings::get_default()->property_gtk_color_scheme() = ss.str();
150 UIConfiguration::reset_dpi ()
152 long val = get_font_scale();
153 set_pango_fontsize ();
156 gtk_settings_set_long_property (gtk_settings_get_default(),
157 "gtk-xft-dpi", val, "ardour");
158 ARDOUR_UI_UTILS::DPIReset(); //Emit Signal
162 UIConfiguration::set_pango_fontsize ()
164 long val = get_font_scale();
166 /* FT2 rendering - used by GnomeCanvas, sigh */
168 #ifndef PLATFORM_WINDOWS
169 pango_ft2_font_map_set_resolution ((PangoFT2FontMap*) pango_ft2_font_map_new(), val/1024, val/1024);
172 /* Cairo rendering, in case there is any */
174 pango_cairo_font_map_set_resolution ((PangoCairoFontMap*) pango_cairo_font_map_get_default(), val/1024);
178 UIConfiguration::map_parameters (boost::function<void (std::string)>& functor)
180 #undef UI_CONFIG_VARIABLE
181 #define UI_CONFIG_VARIABLE(Type,var,Name,value) functor (Name);
182 #include "ui_config_vars.h"
183 #undef UI_CONFIG_VARIABLE
187 UIConfiguration::pre_gui_init ()
189 #ifdef CAIRO_SUPPORTS_FORCE_BUGGY_GRADIENTS_ENVIRONMENT_VARIABLE
190 if (get_buggy_gradients()) {
191 g_setenv ("FORCE_BUGGY_GRADIENTS", "1", 1);
194 #ifdef OPTIONAL_CAIRO_IMAGE_SURFACE
195 if (get_cairo_image_surface()) {
196 g_setenv ("ARDOUR_IMAGE_SURFACE", "1", 1);
203 UIConfiguration::post_gui_init ()
210 UIConfiguration::load_defaults ()
215 if (find_file (ardour_config_search_path(), default_ui_config_file_name, rcfile) ) {
218 info << string_compose (_("Loading default ui configuration file %1"), rcfile) << endmsg;
220 if (!tree.read (rcfile.c_str())) {
221 error << string_compose(_("cannot read default ui configuration file \"%1\""), rcfile) << endmsg;
223 if (set_state (*tree.root(), Stateful::loading_state_version)) {
224 error << string_compose(_("default ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
232 warning << string_compose (_("Could not find default UI configuration file %1"), default_ui_config_file_name) << endmsg;
237 /* reload color theme */
238 load_color_theme (false);
239 ARDOUR_UI_UTILS::ColorsChanged (); /* EMIT SIGNAL */
246 UIConfiguration::load_color_theme (bool allow_own)
254 basename += color_file.get();
255 basename += ".colors";
257 if (find_file (ardour_config_search_path(), basename, cfile)) {
263 basename = color_file.get();
264 basename += ".colors";
266 if (find_file (ardour_config_search_path(), basename, cfile)) {
275 info << string_compose (_("Loading color file %1"), cfile) << endmsg;
277 if (!tree.read (cfile.c_str())) {
278 error << string_compose(_("cannot read color file \"%1\""), cfile) << endmsg;
282 if (set_state (*tree.root(), Stateful::loading_state_version)) {
283 error << string_compose(_("color file \"%1\" not loaded successfully."), cfile) << endmsg;
287 ARDOUR_UI_UTILS::ColorsChanged ();
289 warning << string_compose (_("Color file %1 not found"), basename) << endmsg;
296 UIConfiguration::store_color_theme ()
299 LocaleGuard lg (X_("C"));
301 root = new XMLNode("Ardour");
303 XMLNode* parent = new XMLNode (X_("Colors"));
304 for (Colors::const_iterator i = colors.begin(); i != colors.end(); ++i) {
305 XMLNode* node = new XMLNode (X_("Color"));
306 node->add_property (X_("name"), i->first);
308 ss << "0x" << setw (8) << setfill ('0') << hex << i->second;
309 node->add_property (X_("value"), ss.str());
310 parent->add_child_nocopy (*node);
312 root->add_child_nocopy (*parent);
314 parent = new XMLNode (X_("ColorAliases"));
315 for (ColorAliases::const_iterator i = color_aliases.begin(); i != color_aliases.end(); ++i) {
316 XMLNode* node = new XMLNode (X_("ColorAlias"));
317 node->add_property (X_("name"), i->first);
318 node->add_property (X_("alias"), i->second);
319 parent->add_child_nocopy (*node);
321 root->add_child_nocopy (*parent);
323 parent = new XMLNode (X_("Modifiers"));
324 for (Modifiers::const_iterator i = modifiers.begin(); i != modifiers.end(); ++i) {
325 XMLNode* node = new XMLNode (X_("Modifier"));
326 node->add_property (X_("name"), i->first);
327 node->add_property (X_("modifier"), i->second.to_string());
328 parent->add_child_nocopy (*node);
330 root->add_child_nocopy (*parent);
333 std::string colorfile = Glib::build_filename (user_config_directory(), (string ("my-") + color_file.get() + ".colors"));
335 tree.set_root (root);
337 if (!tree.write (colorfile.c_str())){
338 error << string_compose (_("Color file %1 not saved"), colorfile) << endmsg;
346 UIConfiguration::load_state ()
352 if (find_file (ardour_config_search_path(), default_ui_config_file_name, rcfile)) {
356 info << string_compose (_("Loading default ui configuration file %1"), rcfile) << endmsg;
358 if (!tree.read (rcfile.c_str())) {
359 error << string_compose(_("cannot read default ui configuration file \"%1\""), rcfile) << endmsg;
363 if (set_state (*tree.root(), Stateful::loading_state_version)) {
364 error << string_compose(_("default ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
369 if (find_file (ardour_config_search_path(), ui_config_file_name, rcfile)) {
373 info << string_compose (_("Loading user ui configuration file %1"), rcfile) << endmsg;
375 if (!tree.read (rcfile)) {
376 error << string_compose(_("cannot read ui configuration file \"%1\""), rcfile) << endmsg;
380 if (set_state (*tree.root(), Stateful::loading_state_version)) {
381 error << string_compose(_("user ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
389 error << _("could not find any ui configuration file, canvas will look broken.") << endmsg;
396 UIConfiguration::save_state()
400 std::string rcfile = Glib::build_filename (user_config_directory(), ui_config_file_name);
404 tree.set_root (&get_state());
406 if (!tree.write (rcfile.c_str())){
407 error << string_compose (_("Config file %1 not saved"), rcfile) << endmsg;
414 if (aliases_modified || colors_modified || modifiers_modified) {
416 if (store_color_theme ()) {
417 error << string_compose (_("Color file %1 not saved"), color_file.get()) << endmsg;
421 aliases_modified = false;
422 colors_modified = false;
423 modifiers_modified = false;
431 UIConfiguration::get_state ()
434 LocaleGuard lg (X_("C"));
436 root = new XMLNode("Ardour");
438 root->add_child_nocopy (get_variables ("UI"));
439 root->add_child_nocopy (get_variables ("Canvas"));
442 root->add_child_copy (*_extra_xml);
449 UIConfiguration::get_variables (std::string which_node)
452 LocaleGuard lg (X_("C"));
454 node = new XMLNode (which_node);
456 #undef UI_CONFIG_VARIABLE
457 #undef CANVAS_FONT_VARIABLE
458 #define UI_CONFIG_VARIABLE(Type,var,Name,value) if (node->name() == "UI") { var.add_to_node (*node); }
459 #define CANVAS_FONT_VARIABLE(var,Name) if (node->name() == "Canvas") { var.add_to_node (*node); }
460 #include "ui_config_vars.h"
461 #include "canvas_vars.h"
462 #undef UI_CONFIG_VARIABLE
463 #undef CANVAS_FONT_VARIABLE
469 UIConfiguration::set_state (const XMLNode& root, int /*version*/)
471 /* this can load a generic UI configuration file or a colors file */
473 if (root.name() != "Ardour") {
477 Stateful::save_extra_xml (root);
479 XMLNodeList nlist = root.children();
480 XMLNodeConstIterator niter;
483 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
487 if (node->name() == "Canvas" || node->name() == "UI") {
488 set_variables (*node);
493 XMLNode* colors = find_named_node (root, X_("Colors"));
496 load_colors (*colors);
499 XMLNode* aliases = find_named_node (root, X_("ColorAliases"));
502 load_color_aliases (*aliases);
505 XMLNode* modifiers = find_named_node (root, X_("Modifiers"));
508 load_modifiers (*modifiers);
515 UIConfiguration::load_color_aliases (XMLNode const & node)
517 XMLNodeList const nlist = node.children();
518 XMLNodeConstIterator niter;
519 XMLProperty const *name;
520 XMLProperty const *alias;
522 color_aliases.clear ();
524 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
525 if ((*niter)->name() != X_("ColorAlias")) {
528 name = (*niter)->property (X_("name"));
529 alias = (*niter)->property (X_("alias"));
532 color_aliases.insert (make_pair (name->value(), alias->value()));
538 UIConfiguration::load_colors (XMLNode const & node)
540 XMLNodeList const nlist = node.children();
541 XMLNodeConstIterator niter;
542 XMLProperty const *name;
543 XMLProperty const *color;
547 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
548 if ((*niter)->name() != X_("Color")) {
551 name = (*niter)->property (X_("name"));
552 color = (*niter)->property (X_("value"));
555 ArdourCanvas::Color c;
556 c = strtoul (color->value().c_str(), 0, 16);
557 colors.insert (make_pair (name->value(), c));
563 UIConfiguration::load_modifiers (XMLNode const & node)
565 PBD::LocaleGuard lg ("C");
566 XMLNodeList const nlist = node.children();
567 XMLNodeConstIterator niter;
568 XMLProperty const *name;
569 XMLProperty const *mod;
573 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
574 if ((*niter)->name() != X_("Modifier")) {
578 name = (*niter)->property (X_("name"));
579 mod = (*niter)->property (X_("modifier"));
582 SVAModifier svam (mod->value());
583 modifiers.insert (make_pair (name->value(), svam));
589 UIConfiguration::set_variables (const XMLNode& node)
591 #undef UI_CONFIG_VARIABLE
592 #define UI_CONFIG_VARIABLE(Type,var,name,val) if (var.set_from_node (node)) { ParameterChanged (name); }
593 #define CANVAS_FONT_VARIABLE(var,name) if (var.set_from_node (node)) { ParameterChanged (name); }
594 #include "ui_config_vars.h"
595 #include "canvas_vars.h"
596 #undef UI_CONFIG_VARIABLE
597 #undef CANVAS_FONT_VARIABLE
600 ArdourCanvas::SVAModifier
601 UIConfiguration::modifier (string const & name) const
603 Modifiers::const_iterator m = modifiers.find (name);
604 if (m != modifiers.end()) {
607 return SVAModifier ();
611 UIConfiguration::color_mod (std::string const & colorname, std::string const & modifiername) const
613 return HSV (color (colorname)).mod (modifier (modifiername)).color ();
617 UIConfiguration::color_mod (const ArdourCanvas::Color& color, std::string const & modifiername) const
619 return HSV (color).mod (modifier (modifiername)).color ();
623 UIConfiguration::color (const std::string& name, bool* failed) const
625 ColorAliases::const_iterator e = color_aliases.find (name);
631 if (e != color_aliases.end ()) {
632 Colors::const_iterator rc = colors.find (e->second);
633 if (rc != colors.end()) {
637 /* not an alias, try directly */
638 Colors::const_iterator rc = colors.find (name);
639 if (rc != colors.end()) {
645 /* only show this message if the caller wasn't interested in
648 cerr << string_compose (_("Color %1 not found"), name) << endl;
655 return rgba_to_color ((g_random_int()%256)/255.0,
656 (g_random_int()%256)/255.0,
657 (g_random_int()%256)/255.0,
662 UIConfiguration::quantized (Color c) const
665 hsv.h = hue_width * (round (hsv.h/hue_width));
670 UIConfiguration::set_color (string const& name, ArdourCanvas::Color color)
672 Colors::iterator i = colors.find (name);
673 if (i == colors.end()) {
677 colors_modified = true;
679 ARDOUR_UI_UTILS::ColorsChanged (); /* EMIT SIGNAL */
683 UIConfiguration::set_alias (string const & name, string const & alias)
685 ColorAliases::iterator i = color_aliases.find (name);
686 if (i == color_aliases.end()) {
691 aliases_modified = true;
693 ARDOUR_UI_UTILS::ColorsChanged (); /* EMIT SIGNAL */
697 UIConfiguration::set_modifier (string const & name, SVAModifier svam)
699 Modifiers::iterator m = modifiers.find (name);
701 if (m == modifiers.end()) {
706 modifiers_modified = true;
708 ARDOUR_UI_UTILS::ColorsChanged (); /* EMIT SIGNAL */
712 UIConfiguration::load_rc_file (bool themechange, bool allow_own)
714 string basename = ui_rc_file.get();
715 std::string rc_file_path;
717 if (!find_file (ardour_config_search_path(), basename, rc_file_path)) {
718 warning << string_compose (_("Unable to find UI style file %1 in search path %2. %3 will look strange"),
719 basename, ardour_config_search_path().to_string(), PROGRAM_NAME)
724 info << "Loading ui configuration file " << rc_file_path << endmsg;
726 Gtkmm2ext::UI::instance()->load_rcfile (rc_file_path, themechange);