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.
27 #include <cstdio> /* for snprintf, grrr */
29 #include <cairo/cairo.h>
31 #include <pango/pangoft2.h> // for fontmap resolution control for GnomeCanvas
32 #include <pango/pangocairo.h> // for fontmap resolution control for GnomeCanvas
34 #include <glibmm/miscutils.h>
36 #include <gtkmm/settings.h>
38 #include "pbd/convert.h"
39 #include "pbd/error.h"
40 #include "pbd/failed_constructor.h"
41 #include "pbd/file_utils.h"
42 #include "pbd/gstdio_compat.h"
43 #include "pbd/unwind.h"
44 #include "pbd/xml++.h"
46 #include "ardour/filesystem_paths.h"
47 #include "ardour/search_paths.h"
48 #include "ardour/revision.h"
49 #include "ardour/utils.h"
50 #include "ardour/types_convert.h"
52 #include "gtkmm2ext/rgb_macros.h"
53 #include "gtkmm2ext/gtk_ui.h"
55 #include "ui_config.h"
61 using namespace ARDOUR;
62 using namespace Gtkmm2ext;
64 static const char* ui_config_file_name = "ui_config";
65 static const char* default_ui_config_file_name = "default_ui_config";
67 static const double hue_width = 18.0;
68 std::string UIConfiguration::color_file_suffix = X_(".colors");
71 UIConfiguration::instance ()
73 static UIConfiguration s_instance;
74 _instance = &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") {
130 load_color_theme (true);
137 UIConfiguration::reset_gtk_theme ()
139 std::string color_scheme_string("gtk_color_scheme = \"");
141 for (ColorAliases::iterator g = color_aliases.begin(); g != color_aliases.end(); ++g) {
143 if (g->first.find ("gtk_") == 0) {
144 const string gtk_name = g->first.substr (4);
145 Gtkmm2ext::Color a_color = color (g->second);
147 color_scheme_string += gtk_name + ":#" + color_to_hex_string_no_alpha (a_color) + ';';
151 color_scheme_string += '"';
153 /* reset GTK color scheme */
155 Gtk::Settings::get_default()->property_gtk_color_scheme() = color_scheme_string;
159 UIConfiguration::reset_dpi ()
161 long val = get_font_scale();
163 /* FT2 rendering - used by GnomeCanvas, sigh */
165 #ifndef PLATFORM_WINDOWS
166 pango_ft2_font_map_set_resolution ((PangoFT2FontMap*) pango_ft2_font_map_new(), val/1024, val/1024); // XXX pango_ft2_font_map_new leaks
169 /* Cairo rendering, in case there is any */
171 pango_cairo_font_map_set_resolution ((PangoCairoFontMap*) pango_cairo_font_map_get_default(), val/1024);
175 gtk_settings_set_long_property (gtk_settings_get_default(),
176 "gtk-xft-dpi", val, "ardour");
177 DPIReset(); //Emit Signal
181 UIConfiguration::get_ui_scale ()
183 return get_font_scale () / 102400.;
187 UIConfiguration::map_parameters (boost::function<void (std::string)>& functor)
189 #undef UI_CONFIG_VARIABLE
190 #define UI_CONFIG_VARIABLE(Type,var,Name,value) functor (Name);
191 #include "ui_config_vars.h"
192 #undef UI_CONFIG_VARIABLE
196 UIConfiguration::pre_gui_init ()
198 #ifdef CAIRO_SUPPORTS_FORCE_BUGGY_GRADIENTS_ENVIRONMENT_VARIABLE
199 if (get_buggy_gradients()) {
200 g_setenv ("FORCE_BUGGY_GRADIENTS", "1", 1);
203 #ifndef USE_CAIRO_IMAGE_SURFACE
204 if (get_cairo_image_surface()) {
205 g_setenv ("ARDOUR_IMAGE_SURFACE", "1", 1);
212 UIConfiguration::post_gui_init ()
214 load_color_theme (true);
219 UIConfiguration::load_defaults ()
224 if (find_file (ardour_config_search_path(), default_ui_config_file_name, rcfile) ) {
227 info << string_compose (_("Loading default ui configuration file %1"), rcfile) << endmsg;
229 if (!tree.read (rcfile.c_str())) {
230 error << string_compose(_("cannot read default ui configuration file \"%1\""), rcfile) << endmsg;
232 if (set_state (*tree.root(), Stateful::loading_state_version)) {
233 error << string_compose(_("default ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
241 warning << string_compose (_("Could not find default UI configuration file %1"), default_ui_config_file_name) << endmsg;
245 /* reload color theme */
246 load_color_theme (false);
253 UIConfiguration::color_file_name (bool use_my, bool with_version) const
261 std::string color_name = color_file.get();
262 size_t sep = color_name.find_first_of("-");
263 if (sep != string::npos) {
264 color_name = color_name.substr (0, sep);
267 basename += color_name;
269 basename += downcase(std::string(PROGRAM_NAME));
271 std::string rev (revision);
272 std::size_t pos = rev.find_first_of("-");
274 if (with_version && pos != string::npos && pos > 0) {
276 basename += rev.substr (0, pos); // COLORFILE_VERSION - program major.minor
279 basename += color_file_suffix;
284 UIConfiguration::load_color_file (string const & path)
288 info << string_compose (_("Loading color file %1"), path) << endmsg;
290 if (!tree.read (path.c_str())) {
291 error << string_compose(_("cannot read color file \"%1\""), path) << endmsg;
295 if (set_state (*tree.root(), Stateful::loading_state_version)) {
296 error << string_compose(_("color file \"%1\" not loaded successfully."), path) << endmsg;
304 UIConfiguration::load_color_theme (bool allow_own)
308 /* ColorsChanged() will trigger a parameter_changed () which
309 * in turn calls save_state()
311 PBD::Unwinder<uint32_t> uw (block_save, block_save + 1);
313 if (find_file (theme_search_path(), color_file_name (false, true), cfile)) {
318 if (find_file (theme_search_path(), color_file_name (false, false), cfile)) {
324 warning << string_compose (_("Color file for %1 not found along %2"), color_file.get(), theme_search_path().to_string()) << endmsg;
328 (void) load_color_file (cfile);
334 PBD::Searchpath sp (user_config_directory());
336 /* user's own color files never have the program name in them */
338 if (find_file (sp, color_file_name (true, true), cfile)) {
343 if (find_file (sp, color_file_name (true, false), cfile)) {
349 (void) load_color_file (cfile);
360 UIConfiguration::store_color_theme ()
364 root = new XMLNode("Ardour");
366 XMLNode* parent = new XMLNode (X_("Colors"));
367 for (Colors::const_iterator i = colors.begin(); i != colors.end(); ++i) {
368 XMLNode* node = new XMLNode (X_("Color"));
369 node->set_property (X_("name"), i->first);
370 node->set_property (X_("value"), color_to_hex_string (i->second));
371 parent->add_child_nocopy (*node);
373 root->add_child_nocopy (*parent);
375 parent = new XMLNode (X_("ColorAliases"));
376 for (ColorAliases::const_iterator i = color_aliases.begin(); i != color_aliases.end(); ++i) {
377 XMLNode* node = new XMLNode (X_("ColorAlias"));
378 node->set_property (X_("name"), i->first);
379 node->set_property (X_("alias"), i->second);
380 parent->add_child_nocopy (*node);
382 root->add_child_nocopy (*parent);
384 parent = new XMLNode (X_("Modifiers"));
385 for (Modifiers::const_iterator i = modifiers.begin(); i != modifiers.end(); ++i) {
386 XMLNode* node = new XMLNode (X_("Modifier"));
387 node->set_property (X_("name"), i->first);
388 node->set_property (X_("modifier"), i->second.to_string());
389 parent->add_child_nocopy (*node);
391 root->add_child_nocopy (*parent);
394 std::string colorfile = Glib::build_filename (user_config_directory(), color_file_name (true, true));;
396 tree.set_root (root);
398 if (!tree.write (colorfile.c_str())){
399 error << string_compose (_("Color file %1 not saved"), colorfile) << endmsg;
407 UIConfiguration::load_state ()
413 if (find_file (ardour_config_search_path(), default_ui_config_file_name, rcfile)) {
417 info << string_compose (_("Loading default ui configuration file %1"), rcfile) << endmsg;
419 if (!tree.read (rcfile.c_str())) {
420 error << string_compose(_("cannot read default ui configuration file \"%1\""), rcfile) << endmsg;
424 if (set_state (*tree.root(), Stateful::loading_state_version)) {
425 error << string_compose(_("default ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
430 if (find_file (ardour_config_search_path(), ui_config_file_name, rcfile)) {
434 info << string_compose (_("Loading user ui configuration file %1"), rcfile) << endmsg;
436 if (!tree.read (rcfile)) {
437 error << string_compose(_("cannot read ui configuration file \"%1\""), rcfile) << endmsg;
441 if (set_state (*tree.root(), Stateful::loading_state_version)) {
442 error << string_compose(_("user ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
450 error << _("could not find any ui configuration file, canvas will look broken.") << endmsg;
457 UIConfiguration::save_state()
459 if (block_save != 0) {
464 std::string rcfile = Glib::build_filename (user_config_directory(), ui_config_file_name);
468 tree.set_root (&get_state());
470 if (!tree.write (rcfile.c_str())){
471 error << string_compose (_("Config file %1 not saved"), rcfile) << endmsg;
478 if (aliases_modified || colors_modified || modifiers_modified) {
480 if (store_color_theme ()) {
481 error << string_compose (_("Color file %1 not saved"), color_file.get()) << endmsg;
485 aliases_modified = false;
486 colors_modified = false;
487 modifiers_modified = false;
495 UIConfiguration::get_state ()
499 root = new XMLNode("Ardour");
501 root->add_child_nocopy (get_variables ("UI"));
502 root->add_child_nocopy (get_variables ("Canvas"));
505 root->add_child_copy (*_extra_xml);
512 UIConfiguration::get_variables (std::string which_node)
516 node = new XMLNode (which_node);
518 #undef UI_CONFIG_VARIABLE
519 #undef CANVAS_FONT_VARIABLE
520 #define UI_CONFIG_VARIABLE(Type,var,Name,value) if (node->name() == "UI") { var.add_to_node (*node); }
521 #define CANVAS_FONT_VARIABLE(var,Name) if (node->name() == "Canvas") { var.add_to_node (*node); }
522 #include "ui_config_vars.h"
523 #include "canvas_vars.h"
524 #undef UI_CONFIG_VARIABLE
525 #undef CANVAS_FONT_VARIABLE
531 UIConfiguration::set_state (const XMLNode& root, int /*version*/)
533 /* this can load a generic UI configuration file or a colors file */
535 if (root.name() != "Ardour") {
539 Stateful::save_extra_xml (root);
541 XMLNodeList nlist = root.children();
542 XMLNodeConstIterator niter;
545 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
549 if (node->name() == "Canvas" || node->name() == "UI") {
550 set_variables (*node);
555 XMLNode* colors = find_named_node (root, X_("Colors"));
558 load_colors (*colors);
561 XMLNode* aliases = find_named_node (root, X_("ColorAliases"));
564 load_color_aliases (*aliases);
567 XMLNode* modifiers = find_named_node (root, X_("Modifiers"));
570 load_modifiers (*modifiers);
577 UIConfiguration::load_color_aliases (XMLNode const & node)
579 XMLNodeList const nlist = node.children();
580 XMLNodeConstIterator niter;
581 XMLProperty const *name;
582 XMLProperty const *alias;
584 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
585 XMLNode const * child = *niter;
586 if (child->name() != X_("ColorAlias")) {
589 name = child->property (X_("name"));
590 alias = child->property (X_("alias"));
593 color_aliases[name->value()] = alias->value();
599 UIConfiguration::load_colors (XMLNode const & node)
601 XMLNodeList const nlist = node.children();
602 XMLNodeConstIterator niter;
603 XMLProperty const *name;
604 XMLProperty const *color;
606 /* don't clear colors, so that we can load > 1 color file and have
607 the subsequent ones overwrite the later ones.
610 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
611 XMLNode const * child = *niter;
612 if (child->name() != X_("Color")) {
615 name = child->property (X_("name"));
616 color = child->property (X_("value"));
620 c = strtoul (color->value().c_str(), 0, 16);
621 /* insert or replace color name definition */
622 colors[name->value()] = c;
628 UIConfiguration::load_modifiers (XMLNode const & node)
630 XMLNodeList const nlist = node.children();
631 XMLNodeConstIterator niter;
632 XMLProperty const *name;
633 XMLProperty const *mod;
635 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
636 XMLNode const * child = *niter;
637 if (child->name() != X_("Modifier")) {
641 name = child->property (X_("name"));
642 mod = child->property (X_("modifier"));
645 SVAModifier svam (mod->value());
646 modifiers[name->value()] = svam;
652 UIConfiguration::set_variables (const XMLNode& node)
654 #undef UI_CONFIG_VARIABLE
655 #define UI_CONFIG_VARIABLE(Type,var,name,val) if (var.set_from_node (node)) { ParameterChanged (name); }
656 #define CANVAS_FONT_VARIABLE(var,name) if (var.set_from_node (node)) { ParameterChanged (name); }
657 #include "ui_config_vars.h"
658 #include "canvas_vars.h"
659 #undef UI_CONFIG_VARIABLE
660 #undef CANVAS_FONT_VARIABLE
663 Gtkmm2ext::SVAModifier
664 UIConfiguration::modifier (string const & name) const
666 Modifiers::const_iterator m = modifiers.find (name);
667 if (m != modifiers.end()) {
670 return SVAModifier ();
674 UIConfiguration::color_mod (std::string const & colorname, std::string const & modifiername) const
676 return HSV (color (colorname)).mod (modifier (modifiername)).color ();
680 UIConfiguration::color_mod (const Gtkmm2ext::Color& color, std::string const & modifiername) const
682 return HSV (color).mod (modifier (modifiername)).color ();
686 UIConfiguration::color (const std::string& name, bool* failed) const
688 ColorAliases::const_iterator e = color_aliases.find (name);
694 if (e != color_aliases.end ()) {
695 Colors::const_iterator rc = colors.find (e->second);
696 if (rc != colors.end()) {
700 /* not an alias, try directly */
701 Colors::const_iterator rc = colors.find (name);
702 if (rc != colors.end()) {
708 /* only show this message if the caller wasn't interested in
711 cerr << string_compose (_("Color %1 not found"), name) << endl;
718 return rgba_to_color ((g_random_int()%256)/255.0,
719 (g_random_int()%256)/255.0,
720 (g_random_int()%256)/255.0,
725 UIConfiguration::quantized (Color c) const
728 hsv.h = hue_width * (round (hsv.h/hue_width));
733 UIConfiguration::set_color (string const& name, Gtkmm2ext::Color color)
735 Colors::iterator i = colors.find (name);
736 if (i == colors.end()) {
740 colors_modified = true;
742 ColorsChanged (); /* EMIT SIGNAL */
746 UIConfiguration::set_alias (string const & name, string const & alias)
748 ColorAliases::iterator i = color_aliases.find (name);
749 if (i == color_aliases.end()) {
754 aliases_modified = true;
756 ColorsChanged (); /* EMIT SIGNAL */
760 UIConfiguration::set_modifier (string const & name, SVAModifier svam)
762 Modifiers::iterator m = modifiers.find (name);
764 if (m == modifiers.end()) {
769 modifiers_modified = true;
771 ColorsChanged (); /* EMIT SIGNAL */
775 UIConfiguration::load_rc_file (bool themechange, bool allow_own)
777 string basename = ui_rc_file.get();
778 std::string rc_file_path;
780 if (!find_file (ardour_config_search_path(), basename, rc_file_path)) {
781 warning << string_compose (_("Unable to find UI style file %1 in search path %2. %3 will look strange"),
782 basename, ardour_config_search_path().to_string(), PROGRAM_NAME)
787 info << string_compose (_("Loading ui configuration file %1"), rc_file_path) << endmsg;
789 Gtkmm2ext::UI::instance()->load_rcfile (rc_file_path, themechange);
793 UIConfiguration::color_to_hex_string (Gtkmm2ext::Color c)
796 int retval = g_snprintf (buf, sizeof(buf), "%08x", c);
798 if (retval < 0 || retval >= (int)sizeof(buf)) {
805 UIConfiguration::color_to_hex_string_no_alpha (Gtkmm2ext::Color c)
807 c >>= 8; // shift/remove alpha
809 int retval = g_snprintf (buf, sizeof(buf), "%06x", c);
811 if (retval < 0 || retval >= (int)sizeof(buf)) {