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 "pbd/gstdio_compat.h"
36 #include "pbd/unwind.h"
37 #include <glibmm/miscutils.h>
39 #include <gtkmm/settings.h>
41 #include "pbd/convert.h"
42 #include "pbd/failed_constructor.h"
43 #include "pbd/xml++.h"
44 #include "pbd/file_utils.h"
45 #include "pbd/locale_guard.h"
46 #include "pbd/error.h"
47 #include "pbd/stacktrace.h"
49 #include "gtkmm2ext/rgb_macros.h"
50 #include "gtkmm2ext/gtk_ui.h"
52 #include "ardour/filesystem_paths.h"
53 #include "ardour/search_paths.h"
54 #include "ardour/utils.h"
56 #include "ui_config.h"
62 using namespace ARDOUR;
63 using namespace ArdourCanvas;
65 static const char* ui_config_file_name = "ui_config";
66 static const char* default_ui_config_file_name = "default_ui_config";
68 static const double hue_width = 18.0;
69 std::string UIConfiguration::color_file_suffix = X_(".colors");
72 UIConfiguration::instance ()
74 static UIConfiguration s_instance;
78 UIConfiguration::UIConfiguration ()
80 #undef UI_CONFIG_VARIABLE
81 #define UI_CONFIG_VARIABLE(Type,var,name,val) var (name,val),
82 #define CANVAS_FONT_VARIABLE(var,name) var (name),
83 #include "ui_config_vars.h"
84 #include "canvas_vars.h"
85 #undef UI_CONFIG_VARIABLE
86 #undef CANVAS_FONT_VARIABLE
89 aliases_modified (false),
90 colors_modified (false),
91 modifiers_modified (false),
96 ColorsChanged.connect (boost::bind (&UIConfiguration::colors_changed, this));
98 ParameterChanged.connect (sigc::mem_fun (*this, &UIConfiguration::parameter_changed));
101 UIConfiguration::~UIConfiguration ()
106 UIConfiguration::colors_changed ()
110 /* In theory, one of these ought to work:
112 gtk_rc_reparse_all_for_settings (gtk_settings_get_default(), true);
113 gtk_rc_reset_styles (gtk_settings_get_default());
115 but in practice, neither of them do. So just reload the current
116 GTK RC file, which causes a reset of all styles and a redraw
119 parameter_changed ("ui-rc-file");
123 UIConfiguration::parameter_changed (string param)
127 if (param == "ui-rc-file") {
129 } else if (param == "color-file") {
137 UIConfiguration::reset_gtk_theme ()
141 ss << "gtk_color_scheme = \"" << hex;
143 for (ColorAliases::iterator g = color_aliases.begin(); g != color_aliases.end(); ++g) {
145 if (g->first.find ("gtk_") == 0) {
146 const string gtk_name = g->first.substr (4);
147 ss << gtk_name << ":#" << std::setw (6) << setfill ('0') << (color (g->second) >> 8) << ';';
151 ss << '"' << dec << endl;
153 /* reset GTK color scheme */
155 Gtk::Settings::get_default()->property_gtk_color_scheme() = ss.str();
159 UIConfiguration::reset_dpi ()
161 long val = get_font_scale();
162 set_pango_fontsize ();
165 gtk_settings_set_long_property (gtk_settings_get_default(),
166 "gtk-xft-dpi", val, "ardour");
167 DPIReset(); //Emit Signal
171 UIConfiguration::set_pango_fontsize ()
173 long val = get_font_scale();
175 /* FT2 rendering - used by GnomeCanvas, sigh */
177 #ifndef PLATFORM_WINDOWS
178 pango_ft2_font_map_set_resolution ((PangoFT2FontMap*) pango_ft2_font_map_new(), val/1024, val/1024);
181 /* Cairo rendering, in case there is any */
183 pango_cairo_font_map_set_resolution ((PangoCairoFontMap*) pango_cairo_font_map_get_default(), val/1024);
187 UIConfiguration::get_ui_scale ()
189 return get_font_scale () / 102400.;
193 UIConfiguration::map_parameters (boost::function<void (std::string)>& functor)
195 #undef UI_CONFIG_VARIABLE
196 #define UI_CONFIG_VARIABLE(Type,var,Name,value) functor (Name);
197 #include "ui_config_vars.h"
198 #undef UI_CONFIG_VARIABLE
202 UIConfiguration::pre_gui_init ()
204 #ifdef CAIRO_SUPPORTS_FORCE_BUGGY_GRADIENTS_ENVIRONMENT_VARIABLE
205 if (get_buggy_gradients()) {
206 g_setenv ("FORCE_BUGGY_GRADIENTS", "1", 1);
209 #ifdef OPTIONAL_CAIRO_IMAGE_SURFACE
210 if (get_cairo_image_surface()) {
211 g_setenv ("ARDOUR_IMAGE_SURFACE", "1", 1);
218 UIConfiguration::post_gui_init ()
225 UIConfiguration::load_defaults ()
230 if (find_file (ardour_config_search_path(), default_ui_config_file_name, rcfile) ) {
233 info << string_compose (_("Loading default ui configuration file %1"), rcfile) << endmsg;
235 if (!tree.read (rcfile.c_str())) {
236 error << string_compose(_("cannot read default ui configuration file \"%1\""), rcfile) << endmsg;
238 if (set_state (*tree.root(), Stateful::loading_state_version)) {
239 error << string_compose(_("default ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
247 warning << string_compose (_("Could not find default UI configuration file %1"), default_ui_config_file_name) << endmsg;
252 /* reload color theme */
253 load_color_theme (false);
260 UIConfiguration::load_color_theme (bool allow_own)
265 /* ColorsChanged() will trigger a parameter_changed () which
266 * in turn calls save_state()
268 PBD::Unwinder<uint32_t> uw (block_save, block_save + 1);
272 basename += color_file.get();
273 basename += color_file_suffix;
275 if (find_file (theme_search_path(), basename, cfile)) {
281 basename = color_file.get();
282 basename += color_file_suffix;
284 if (find_file (theme_search_path(), basename, cfile)) {
293 info << string_compose (_("Loading color file %1"), cfile) << endmsg;
295 if (!tree.read (cfile.c_str())) {
296 error << string_compose(_("cannot read color file \"%1\""), cfile) << endmsg;
300 if (set_state (*tree.root(), Stateful::loading_state_version)) {
301 error << string_compose(_("color file \"%1\" not loaded successfully."), cfile) << endmsg;
307 warning << string_compose (_("Color file %1 not found"), basename) << endmsg;
314 UIConfiguration::store_color_theme ()
319 root = new XMLNode("Ardour");
321 XMLNode* parent = new XMLNode (X_("Colors"));
322 for (Colors::const_iterator i = colors.begin(); i != colors.end(); ++i) {
323 XMLNode* node = new XMLNode (X_("Color"));
324 node->add_property (X_("name"), i->first);
326 ss << "0x" << setw (8) << setfill ('0') << hex << i->second;
327 node->add_property (X_("value"), ss.str());
328 parent->add_child_nocopy (*node);
330 root->add_child_nocopy (*parent);
332 parent = new XMLNode (X_("ColorAliases"));
333 for (ColorAliases::const_iterator i = color_aliases.begin(); i != color_aliases.end(); ++i) {
334 XMLNode* node = new XMLNode (X_("ColorAlias"));
335 node->add_property (X_("name"), i->first);
336 node->add_property (X_("alias"), i->second);
337 parent->add_child_nocopy (*node);
339 root->add_child_nocopy (*parent);
341 parent = new XMLNode (X_("Modifiers"));
342 for (Modifiers::const_iterator i = modifiers.begin(); i != modifiers.end(); ++i) {
343 XMLNode* node = new XMLNode (X_("Modifier"));
344 node->add_property (X_("name"), i->first);
345 node->add_property (X_("modifier"), i->second.to_string());
346 parent->add_child_nocopy (*node);
348 root->add_child_nocopy (*parent);
351 std::string colorfile = Glib::build_filename (user_config_directory(), (string ("my-") + color_file.get() + color_file_suffix));
353 tree.set_root (root);
355 if (!tree.write (colorfile.c_str())){
356 error << string_compose (_("Color file %1 not saved"), colorfile) << endmsg;
364 UIConfiguration::load_state ()
366 LocaleGuard lg; // a single guard for all 3 configs
371 if (find_file (ardour_config_search_path(), default_ui_config_file_name, rcfile)) {
375 info << string_compose (_("Loading default ui configuration file %1"), rcfile) << endmsg;
377 if (!tree.read (rcfile.c_str())) {
378 error << string_compose(_("cannot read default ui configuration file \"%1\""), rcfile) << endmsg;
382 if (set_state (*tree.root(), Stateful::loading_state_version)) {
383 error << string_compose(_("default ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
388 if (find_file (ardour_config_search_path(), ui_config_file_name, rcfile)) {
392 info << string_compose (_("Loading user ui configuration file %1"), rcfile) << endmsg;
394 if (!tree.read (rcfile)) {
395 error << string_compose(_("cannot read ui configuration file \"%1\""), rcfile) << endmsg;
399 if (set_state (*tree.root(), Stateful::loading_state_version)) {
400 error << string_compose(_("user ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
408 error << _("could not find any ui configuration file, canvas will look broken.") << endmsg;
415 UIConfiguration::save_state()
417 if (block_save != 0) {
422 std::string rcfile = Glib::build_filename (user_config_directory(), ui_config_file_name);
426 tree.set_root (&get_state());
428 if (!tree.write (rcfile.c_str())){
429 error << string_compose (_("Config file %1 not saved"), rcfile) << endmsg;
436 if (aliases_modified || colors_modified || modifiers_modified) {
438 if (store_color_theme ()) {
439 error << string_compose (_("Color file %1 not saved"), color_file.get()) << endmsg;
443 aliases_modified = false;
444 colors_modified = false;
445 modifiers_modified = false;
453 UIConfiguration::get_state ()
458 root = new XMLNode("Ardour");
460 root->add_child_nocopy (get_variables ("UI"));
461 root->add_child_nocopy (get_variables ("Canvas"));
464 root->add_child_copy (*_extra_xml);
471 UIConfiguration::get_variables (std::string which_node)
476 node = new XMLNode (which_node);
478 #undef UI_CONFIG_VARIABLE
479 #undef CANVAS_FONT_VARIABLE
480 #define UI_CONFIG_VARIABLE(Type,var,Name,value) if (node->name() == "UI") { var.add_to_node (*node); }
481 #define CANVAS_FONT_VARIABLE(var,Name) if (node->name() == "Canvas") { var.add_to_node (*node); }
482 #include "ui_config_vars.h"
483 #include "canvas_vars.h"
484 #undef UI_CONFIG_VARIABLE
485 #undef CANVAS_FONT_VARIABLE
491 UIConfiguration::set_state (const XMLNode& root, int /*version*/)
494 /* this can load a generic UI configuration file or a colors file */
496 if (root.name() != "Ardour") {
500 Stateful::save_extra_xml (root);
502 XMLNodeList nlist = root.children();
503 XMLNodeConstIterator niter;
506 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
510 if (node->name() == "Canvas" || node->name() == "UI") {
511 set_variables (*node);
516 XMLNode* colors = find_named_node (root, X_("Colors"));
519 load_colors (*colors);
522 XMLNode* aliases = find_named_node (root, X_("ColorAliases"));
525 load_color_aliases (*aliases);
528 XMLNode* modifiers = find_named_node (root, X_("Modifiers"));
531 load_modifiers (*modifiers);
538 UIConfiguration::load_color_aliases (XMLNode const & node)
540 XMLNodeList const nlist = node.children();
541 XMLNodeConstIterator niter;
542 XMLProperty const *name;
543 XMLProperty const *alias;
545 color_aliases.clear ();
547 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
548 XMLNode const * child = *niter;
549 if (child->name() != X_("ColorAlias")) {
552 name = child->property (X_("name"));
553 alias = child->property (X_("alias"));
556 color_aliases.insert (make_pair (name->value(), alias->value()));
562 UIConfiguration::load_colors (XMLNode const & node)
564 XMLNodeList const nlist = node.children();
565 XMLNodeConstIterator niter;
566 XMLProperty const *name;
567 XMLProperty const *color;
571 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
572 XMLNode const * child = *niter;
573 if (child->name() != X_("Color")) {
576 name = child->property (X_("name"));
577 color = child->property (X_("value"));
580 ArdourCanvas::Color c;
581 c = strtoul (color->value().c_str(), 0, 16);
582 colors.insert (make_pair (name->value(), c));
588 UIConfiguration::load_modifiers (XMLNode const & node)
591 XMLNodeList const nlist = node.children();
592 XMLNodeConstIterator niter;
593 XMLProperty const *name;
594 XMLProperty const *mod;
598 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
599 XMLNode const * child = *niter;
600 if (child->name() != X_("Modifier")) {
604 name = child->property (X_("name"));
605 mod = child->property (X_("modifier"));
608 SVAModifier svam (mod->value());
609 modifiers.insert (make_pair (name->value(), svam));
615 UIConfiguration::set_variables (const XMLNode& node)
617 #undef UI_CONFIG_VARIABLE
618 #define UI_CONFIG_VARIABLE(Type,var,name,val) if (var.set_from_node (node)) { ParameterChanged (name); }
619 #define CANVAS_FONT_VARIABLE(var,name) if (var.set_from_node (node)) { ParameterChanged (name); }
620 #include "ui_config_vars.h"
621 #include "canvas_vars.h"
622 #undef UI_CONFIG_VARIABLE
623 #undef CANVAS_FONT_VARIABLE
626 ArdourCanvas::SVAModifier
627 UIConfiguration::modifier (string const & name) const
629 Modifiers::const_iterator m = modifiers.find (name);
630 if (m != modifiers.end()) {
633 return SVAModifier ();
637 UIConfiguration::color_mod (std::string const & colorname, std::string const & modifiername) const
639 return HSV (color (colorname)).mod (modifier (modifiername)).color ();
643 UIConfiguration::color_mod (const ArdourCanvas::Color& color, std::string const & modifiername) const
645 return HSV (color).mod (modifier (modifiername)).color ();
649 UIConfiguration::color (const std::string& name, bool* failed) const
651 ColorAliases::const_iterator e = color_aliases.find (name);
657 if (e != color_aliases.end ()) {
658 Colors::const_iterator rc = colors.find (e->second);
659 if (rc != colors.end()) {
663 /* not an alias, try directly */
664 Colors::const_iterator rc = colors.find (name);
665 if (rc != colors.end()) {
671 /* only show this message if the caller wasn't interested in
674 cerr << string_compose (_("Color %1 not found"), name) << endl;
681 return rgba_to_color ((g_random_int()%256)/255.0,
682 (g_random_int()%256)/255.0,
683 (g_random_int()%256)/255.0,
688 UIConfiguration::quantized (Color c) const
691 hsv.h = hue_width * (round (hsv.h/hue_width));
696 UIConfiguration::set_color (string const& name, ArdourCanvas::Color color)
698 Colors::iterator i = colors.find (name);
699 if (i == colors.end()) {
703 colors_modified = true;
705 ColorsChanged (); /* EMIT SIGNAL */
709 UIConfiguration::set_alias (string const & name, string const & alias)
711 ColorAliases::iterator i = color_aliases.find (name);
712 if (i == color_aliases.end()) {
717 aliases_modified = true;
719 ColorsChanged (); /* EMIT SIGNAL */
723 UIConfiguration::set_modifier (string const & name, SVAModifier svam)
725 Modifiers::iterator m = modifiers.find (name);
727 if (m == modifiers.end()) {
732 modifiers_modified = true;
734 ColorsChanged (); /* EMIT SIGNAL */
738 UIConfiguration::load_rc_file (bool themechange, bool allow_own)
740 string basename = ui_rc_file.get();
741 std::string rc_file_path;
743 if (!find_file (ardour_config_search_path(), basename, rc_file_path)) {
744 warning << string_compose (_("Unable to find UI style file %1 in search path %2. %3 will look strange"),
745 basename, ardour_config_search_path().to_string(), PROGRAM_NAME)
750 info << "Loading ui configuration file " << rc_file_path << endmsg;
752 Gtkmm2ext::UI::instance()->load_rcfile (rc_file_path, themechange);