2 * Copyright (C) 2008-2014 David Robillard <d@drobilla.net>
3 * Copyright (C) 2008-2016 Paul Davis <paul@linuxaudiosystems.com>
4 * Copyright (C) 2009-2010 Carl Hetherington <carl@carlh.net>
5 * Copyright (C) 2012-2016 Tim Mayberry <mojofunk@gmail.com>
6 * Copyright (C) 2014-2017 Robin Gareus <robin@gareus.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #if !defined USE_CAIRO_IMAGE_SURFACE && !defined NDEBUG
24 #define OPTIONAL_CAIRO_IMAGE_SURFACE
31 #include <cstdio> /* for snprintf, grrr */
33 #include <cairo/cairo.h>
35 #include <pango/pangoft2.h> // for fontmap resolution control for GnomeCanvas
36 #include <pango/pangocairo.h> // for fontmap resolution control for GnomeCanvas
38 #include <glibmm/miscutils.h>
40 #include <gtkmm/settings.h>
42 #include "pbd/convert.h"
43 #include "pbd/error.h"
44 #include "pbd/failed_constructor.h"
45 #include "pbd/file_utils.h"
46 #include "pbd/gstdio_compat.h"
47 #include "pbd/unwind.h"
48 #include "pbd/xml++.h"
50 #include "ardour/filesystem_paths.h"
51 #include "ardour/search_paths.h"
52 #include "ardour/revision.h"
53 #include "ardour/utils.h"
54 #include "ardour/types_convert.h"
56 #include "gtkmm2ext/rgb_macros.h"
57 #include "gtkmm2ext/gtk_ui.h"
59 #include "ui_config.h"
65 using namespace ARDOUR;
66 using namespace Gtkmm2ext;
68 static const char* ui_config_file_name = "ui_config";
69 static const char* default_ui_config_file_name = "default_ui_config";
71 static const double hue_width = 18.0;
72 std::string UIConfiguration::color_file_suffix = X_(".colors");
75 UIConfiguration::instance ()
77 static UIConfiguration s_instance;
78 _instance = &s_instance;
82 UIConfiguration::UIConfiguration ()
84 #undef UI_CONFIG_VARIABLE
85 #define UI_CONFIG_VARIABLE(Type,var,name,val) var (name,val),
86 #define CANVAS_FONT_VARIABLE(var,name) var (name),
87 #include "ui_config_vars.h"
88 #include "canvas_vars.h"
89 #undef UI_CONFIG_VARIABLE
90 #undef CANVAS_FONT_VARIABLE
93 aliases_modified (false),
94 colors_modified (false),
95 modifiers_modified (false),
100 ColorsChanged.connect (boost::bind (&UIConfiguration::colors_changed, this));
102 ParameterChanged.connect (sigc::mem_fun (*this, &UIConfiguration::parameter_changed));
105 UIConfiguration::~UIConfiguration ()
110 UIConfiguration::colors_changed ()
114 /* In theory, one of these ought to work:
116 gtk_rc_reparse_all_for_settings (gtk_settings_get_default(), true);
117 gtk_rc_reset_styles (gtk_settings_get_default());
119 but in practice, neither of them do. So just reload the current
120 GTK RC file, which causes a reset of all styles and a redraw
123 parameter_changed ("ui-rc-file");
127 UIConfiguration::parameter_changed (string param)
131 if (param == "ui-rc-file") {
133 } else if (param == "color-file") {
134 load_color_theme (true);
141 UIConfiguration::reset_gtk_theme ()
143 std::string color_scheme_string("gtk_color_scheme = \"");
145 for (ColorAliases::iterator g = color_aliases.begin(); g != color_aliases.end(); ++g) {
147 if (g->first.find ("gtk_") == 0) {
148 const string gtk_name = g->first.substr (4);
149 Gtkmm2ext::Color a_color = color (g->second);
151 color_scheme_string += gtk_name + ":#" + color_to_hex_string_no_alpha (a_color) + ';';
155 color_scheme_string += '"';
157 /* reset GTK color scheme */
159 Gtk::Settings::get_default()->property_gtk_color_scheme() = color_scheme_string;
163 UIConfiguration::reset_dpi ()
165 long val = get_font_scale();
167 /* FT2 rendering - used by GnomeCanvas, sigh */
169 #ifndef PLATFORM_WINDOWS
170 pango_ft2_font_map_set_resolution ((PangoFT2FontMap*) pango_ft2_font_map_new(), val/1024, val/1024); // XXX pango_ft2_font_map_new leaks
173 /* Cairo rendering, in case there is any */
175 pango_cairo_font_map_set_resolution ((PangoCairoFontMap*) pango_cairo_font_map_get_default(), val/1024);
179 gtk_settings_set_long_property (gtk_settings_get_default(),
180 "gtk-xft-dpi", val, "ardour");
181 DPIReset(); //Emit Signal
185 UIConfiguration::get_ui_scale ()
187 return get_font_scale () / 102400.;
191 UIConfiguration::map_parameters (boost::function<void (std::string)>& functor)
193 #undef UI_CONFIG_VARIABLE
194 #define UI_CONFIG_VARIABLE(Type,var,Name,value) functor (Name);
195 #include "ui_config_vars.h"
196 #undef UI_CONFIG_VARIABLE
200 UIConfiguration::pre_gui_init ()
202 #ifdef CAIRO_SUPPORTS_FORCE_BUGGY_GRADIENTS_ENVIRONMENT_VARIABLE
203 if (get_buggy_gradients()) {
204 g_setenv ("FORCE_BUGGY_GRADIENTS", "1", 1);
207 #ifdef OPTIONAL_CAIRO_IMAGE_SURFACE
208 if (get_cairo_image_surface()) {
209 g_setenv ("ARDOUR_IMAGE_SURFACE", "1", 1);
216 UIConfiguration::post_gui_init ()
218 load_color_theme (true);
223 UIConfiguration::load_defaults ()
228 if (find_file (ardour_config_search_path(), default_ui_config_file_name, rcfile) ) {
231 info << string_compose (_("Loading default ui configuration file %1"), rcfile) << endmsg;
233 if (!tree.read (rcfile.c_str())) {
234 error << string_compose(_("cannot read default ui configuration file \"%1\""), rcfile) << endmsg;
236 if (set_state (*tree.root(), Stateful::loading_state_version)) {
237 error << string_compose(_("default ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
245 warning << string_compose (_("Could not find default UI configuration file %1"), default_ui_config_file_name) << endmsg;
249 /* reload color theme */
250 load_color_theme (false);
257 UIConfiguration::color_file_name (bool use_my, bool with_version) const
265 std::string color_name = color_file.get();
266 size_t sep = color_name.find_first_of("-");
267 if (sep != string::npos) {
268 color_name = color_name.substr (0, sep);
271 basename += color_name;
273 basename += downcase(std::string(PROGRAM_NAME));
275 std::string rev (revision);
276 std::size_t pos = rev.find_first_of("-");
278 if (with_version && pos != string::npos && pos > 0) {
280 basename += rev.substr (0, pos); // COLORFILE_VERSION - program major.minor
283 basename += color_file_suffix;
288 UIConfiguration::load_color_file (string const & path)
292 info << string_compose (_("Loading color file %1"), path) << endmsg;
294 if (!tree.read (path.c_str())) {
295 error << string_compose(_("cannot read color file \"%1\""), path) << endmsg;
299 if (set_state (*tree.root(), Stateful::loading_state_version)) {
300 error << string_compose(_("color file \"%1\" not loaded successfully."), path) << endmsg;
308 UIConfiguration::load_color_theme (bool allow_own)
312 /* ColorsChanged() will trigger a parameter_changed () which
313 * in turn calls save_state()
315 PBD::Unwinder<uint32_t> uw (block_save, block_save + 1);
317 if (find_file (theme_search_path(), color_file_name (false, true), cfile)) {
322 if (find_file (theme_search_path(), color_file_name (false, false), cfile)) {
328 warning << string_compose (_("Color file for %1 not found along %2"), color_file.get(), theme_search_path().to_string()) << endmsg;
332 (void) load_color_file (cfile);
338 PBD::Searchpath sp (user_config_directory());
340 /* user's own color files never have the program name in them */
342 if (find_file (sp, color_file_name (true, true), cfile)) {
347 if (find_file (sp, color_file_name (true, false), cfile)) {
353 (void) load_color_file (cfile);
364 UIConfiguration::store_color_theme ()
368 root = new XMLNode("Ardour");
370 XMLNode* parent = new XMLNode (X_("Colors"));
371 for (Colors::const_iterator i = colors.begin(); i != colors.end(); ++i) {
372 XMLNode* node = new XMLNode (X_("Color"));
373 node->set_property (X_("name"), i->first);
374 node->set_property (X_("value"), color_to_hex_string (i->second));
375 parent->add_child_nocopy (*node);
377 root->add_child_nocopy (*parent);
379 parent = new XMLNode (X_("ColorAliases"));
380 for (ColorAliases::const_iterator i = color_aliases.begin(); i != color_aliases.end(); ++i) {
381 XMLNode* node = new XMLNode (X_("ColorAlias"));
382 node->set_property (X_("name"), i->first);
383 node->set_property (X_("alias"), i->second);
384 parent->add_child_nocopy (*node);
386 root->add_child_nocopy (*parent);
388 parent = new XMLNode (X_("Modifiers"));
389 for (Modifiers::const_iterator i = modifiers.begin(); i != modifiers.end(); ++i) {
390 XMLNode* node = new XMLNode (X_("Modifier"));
391 node->set_property (X_("name"), i->first);
392 node->set_property (X_("modifier"), i->second.to_string());
393 parent->add_child_nocopy (*node);
395 root->add_child_nocopy (*parent);
398 std::string colorfile = Glib::build_filename (user_config_directory(), color_file_name (true, true));;
400 tree.set_root (root);
402 if (!tree.write (colorfile.c_str())){
403 error << string_compose (_("Color file %1 not saved"), colorfile) << endmsg;
411 UIConfiguration::load_state ()
417 if (find_file (ardour_config_search_path(), default_ui_config_file_name, rcfile)) {
421 info << string_compose (_("Loading default ui configuration file %1"), rcfile) << endmsg;
423 if (!tree.read (rcfile.c_str())) {
424 error << string_compose(_("cannot read default ui configuration file \"%1\""), rcfile) << endmsg;
428 if (set_state (*tree.root(), Stateful::loading_state_version)) {
429 error << string_compose(_("default ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
434 if (find_file (ardour_config_search_path(), ui_config_file_name, rcfile)) {
438 info << string_compose (_("Loading user ui configuration file %1"), rcfile) << endmsg;
440 if (!tree.read (rcfile)) {
441 error << string_compose(_("cannot read ui configuration file \"%1\""), rcfile) << endmsg;
445 if (set_state (*tree.root(), Stateful::loading_state_version)) {
446 error << string_compose(_("user ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
454 error << _("could not find any ui configuration file, canvas will look broken.") << endmsg;
461 UIConfiguration::save_state()
463 if (block_save != 0) {
468 std::string rcfile = Glib::build_filename (user_config_directory(), ui_config_file_name);
472 tree.set_root (&get_state());
474 if (!tree.write (rcfile.c_str())){
475 error << string_compose (_("Config file %1 not saved"), rcfile) << endmsg;
482 if (aliases_modified || colors_modified || modifiers_modified) {
484 if (store_color_theme ()) {
485 error << string_compose (_("Color file %1 not saved"), color_file.get()) << endmsg;
489 aliases_modified = false;
490 colors_modified = false;
491 modifiers_modified = false;
499 UIConfiguration::get_state ()
503 root = new XMLNode("Ardour");
505 root->add_child_nocopy (get_variables ("UI"));
506 root->add_child_nocopy (get_variables ("Canvas"));
509 root->add_child_copy (*_extra_xml);
516 UIConfiguration::get_variables (std::string which_node)
520 node = new XMLNode (which_node);
522 #undef UI_CONFIG_VARIABLE
523 #undef CANVAS_FONT_VARIABLE
524 #define UI_CONFIG_VARIABLE(Type,var,Name,value) if (node->name() == "UI") { var.add_to_node (*node); }
525 #define CANVAS_FONT_VARIABLE(var,Name) if (node->name() == "Canvas") { var.add_to_node (*node); }
526 #include "ui_config_vars.h"
527 #include "canvas_vars.h"
528 #undef UI_CONFIG_VARIABLE
529 #undef CANVAS_FONT_VARIABLE
535 UIConfiguration::set_state (const XMLNode& root, int /*version*/)
537 /* this can load a generic UI configuration file or a colors file */
539 if (root.name() != "Ardour") {
543 Stateful::save_extra_xml (root);
545 XMLNodeList nlist = root.children();
546 XMLNodeConstIterator niter;
549 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
553 if (node->name() == "Canvas" || node->name() == "UI") {
554 set_variables (*node);
559 XMLNode* colors = find_named_node (root, X_("Colors"));
562 load_colors (*colors);
565 XMLNode* aliases = find_named_node (root, X_("ColorAliases"));
568 load_color_aliases (*aliases);
571 XMLNode* modifiers = find_named_node (root, X_("Modifiers"));
574 load_modifiers (*modifiers);
581 UIConfiguration::load_color_aliases (XMLNode const & node)
583 XMLNodeList const nlist = node.children();
584 XMLNodeConstIterator niter;
585 XMLProperty const *name;
586 XMLProperty const *alias;
588 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
589 XMLNode const * child = *niter;
590 if (child->name() != X_("ColorAlias")) {
593 name = child->property (X_("name"));
594 alias = child->property (X_("alias"));
597 color_aliases[name->value()] = alias->value();
603 UIConfiguration::load_colors (XMLNode const & node)
605 XMLNodeList const nlist = node.children();
606 XMLNodeConstIterator niter;
607 XMLProperty const *name;
608 XMLProperty const *color;
610 /* don't clear colors, so that we can load > 1 color file and have
611 the subsequent ones overwrite the later ones.
614 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
615 XMLNode const * child = *niter;
616 if (child->name() != X_("Color")) {
619 name = child->property (X_("name"));
620 color = child->property (X_("value"));
624 c = strtoul (color->value().c_str(), 0, 16);
625 /* insert or replace color name definition */
626 colors[name->value()] = c;
632 UIConfiguration::load_modifiers (XMLNode const & node)
634 XMLNodeList const nlist = node.children();
635 XMLNodeConstIterator niter;
636 XMLProperty const *name;
637 XMLProperty const *mod;
639 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
640 XMLNode const * child = *niter;
641 if (child->name() != X_("Modifier")) {
645 name = child->property (X_("name"));
646 mod = child->property (X_("modifier"));
649 SVAModifier svam (mod->value());
650 modifiers[name->value()] = svam;
656 UIConfiguration::set_variables (const XMLNode& node)
658 #undef UI_CONFIG_VARIABLE
659 #define UI_CONFIG_VARIABLE(Type,var,name,val) if (var.set_from_node (node)) { ParameterChanged (name); }
660 #define CANVAS_FONT_VARIABLE(var,name) if (var.set_from_node (node)) { ParameterChanged (name); }
661 #include "ui_config_vars.h"
662 #include "canvas_vars.h"
663 #undef UI_CONFIG_VARIABLE
664 #undef CANVAS_FONT_VARIABLE
667 Gtkmm2ext::SVAModifier
668 UIConfiguration::modifier (string const & name) const
670 Modifiers::const_iterator m = modifiers.find (name);
671 if (m != modifiers.end()) {
674 return SVAModifier ();
678 UIConfiguration::color_mod (std::string const & colorname, std::string const & modifiername) const
680 return HSV (color (colorname)).mod (modifier (modifiername)).color ();
684 UIConfiguration::color_mod (const Gtkmm2ext::Color& color, std::string const & modifiername) const
686 return HSV (color).mod (modifier (modifiername)).color ();
690 UIConfiguration::color (const std::string& name, bool* failed) const
692 ColorAliases::const_iterator e = color_aliases.find (name);
698 if (e != color_aliases.end ()) {
699 Colors::const_iterator rc = colors.find (e->second);
700 if (rc != colors.end()) {
704 /* not an alias, try directly */
705 Colors::const_iterator rc = colors.find (name);
706 if (rc != colors.end()) {
712 /* only show this message if the caller wasn't interested in
715 cerr << string_compose (_("Color %1 not found"), name) << endl;
722 return rgba_to_color ((g_random_int()%256)/255.0,
723 (g_random_int()%256)/255.0,
724 (g_random_int()%256)/255.0,
729 UIConfiguration::quantized (Color c) const
732 hsv.h = hue_width * (round (hsv.h/hue_width));
737 UIConfiguration::set_color (string const& name, Gtkmm2ext::Color color)
739 Colors::iterator i = colors.find (name);
740 if (i == colors.end()) {
744 colors_modified = true;
746 ColorsChanged (); /* EMIT SIGNAL */
750 UIConfiguration::set_alias (string const & name, string const & alias)
752 ColorAliases::iterator i = color_aliases.find (name);
753 if (i == color_aliases.end()) {
758 aliases_modified = true;
760 ColorsChanged (); /* EMIT SIGNAL */
764 UIConfiguration::set_modifier (string const & name, SVAModifier svam)
766 Modifiers::iterator m = modifiers.find (name);
768 if (m == modifiers.end()) {
773 modifiers_modified = true;
775 ColorsChanged (); /* EMIT SIGNAL */
779 UIConfiguration::load_rc_file (bool themechange, bool allow_own)
781 string basename = ui_rc_file.get();
782 std::string rc_file_path;
784 if (!find_file (ardour_config_search_path(), basename, rc_file_path)) {
785 warning << string_compose (_("Unable to find UI style file %1 in search path %2. %3 will look strange"),
786 basename, ardour_config_search_path().to_string(), PROGRAM_NAME)
791 info << string_compose (_("Loading ui configuration file %1"), rc_file_path) << endmsg;
793 Gtkmm2ext::UI::instance()->load_rcfile (rc_file_path, themechange);
797 UIConfiguration::color_to_hex_string (Gtkmm2ext::Color c)
800 int retval = g_snprintf (buf, sizeof(buf), "%08x", c);
802 if (retval < 0 || retval >= (int)sizeof(buf)) {
809 UIConfiguration::color_to_hex_string_no_alpha (Gtkmm2ext::Color c)
811 c >>= 8; // shift/remove alpha
813 int retval = g_snprintf (buf, sizeof(buf), "%06x", c);
815 if (retval < 0 || retval >= (int)sizeof(buf)) {