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>
37 #include <gtkmm/settings.h>
39 #include "pbd/convert.h"
40 #include "pbd/error.h"
41 #include "pbd/failed_constructor.h"
42 #include "pbd/file_utils.h"
43 #include "pbd/gstdio_compat.h"
44 #include "pbd/locale_guard.h"
45 #include "pbd/unwind.h"
46 #include "pbd/xml++.h"
48 #include "ardour/filesystem_paths.h"
49 #include "ardour/search_paths.h"
50 #include "ardour/revision.h"
51 #include "ardour/utils.h"
53 #include "gtkmm2ext/rgb_macros.h"
54 #include "gtkmm2ext/gtk_ui.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 ()
142 ss << "gtk_color_scheme = \"" << hex;
144 for (ColorAliases::iterator g = color_aliases.begin(); g != color_aliases.end(); ++g) {
146 if (g->first.find ("gtk_") == 0) {
147 const string gtk_name = g->first.substr (4);
148 ss << gtk_name << ":#" << std::setw (6) << setfill ('0') << (color (g->second) >> 8) << ';';
152 ss << '"' << dec << endl;
154 /* reset GTK color scheme */
156 Gtk::Settings::get_default()->property_gtk_color_scheme() = ss.str();
160 UIConfiguration::reset_dpi ()
162 long val = get_font_scale();
163 set_pango_fontsize ();
166 gtk_settings_set_long_property (gtk_settings_get_default(),
167 "gtk-xft-dpi", val, "ardour");
168 DPIReset(); //Emit Signal
172 UIConfiguration::set_pango_fontsize ()
174 long val = get_font_scale();
176 /* FT2 rendering - used by GnomeCanvas, sigh */
178 #ifndef PLATFORM_WINDOWS
179 pango_ft2_font_map_set_resolution ((PangoFT2FontMap*) pango_ft2_font_map_new(), val/1024, val/1024);
182 /* Cairo rendering, in case there is any */
184 pango_cairo_font_map_set_resolution ((PangoCairoFontMap*) pango_cairo_font_map_get_default(), val/1024);
188 UIConfiguration::get_ui_scale ()
190 return get_font_scale () / 102400.;
194 UIConfiguration::map_parameters (boost::function<void (std::string)>& functor)
196 #undef UI_CONFIG_VARIABLE
197 #define UI_CONFIG_VARIABLE(Type,var,Name,value) functor (Name);
198 #include "ui_config_vars.h"
199 #undef UI_CONFIG_VARIABLE
203 UIConfiguration::pre_gui_init ()
205 #ifdef CAIRO_SUPPORTS_FORCE_BUGGY_GRADIENTS_ENVIRONMENT_VARIABLE
206 if (get_buggy_gradients()) {
207 g_setenv ("FORCE_BUGGY_GRADIENTS", "1", 1);
210 #ifdef OPTIONAL_CAIRO_IMAGE_SURFACE
211 if (get_cairo_image_surface()) {
212 g_setenv ("ARDOUR_IMAGE_SURFACE", "1", 1);
219 UIConfiguration::post_gui_init ()
226 UIConfiguration::load_defaults ()
231 if (find_file (ardour_config_search_path(), default_ui_config_file_name, rcfile) ) {
234 info << string_compose (_("Loading default ui configuration file %1"), rcfile) << endmsg;
236 if (!tree.read (rcfile.c_str())) {
237 error << string_compose(_("cannot read default ui configuration file \"%1\""), rcfile) << endmsg;
239 if (set_state (*tree.root(), Stateful::loading_state_version)) {
240 error << string_compose(_("default ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
248 warning << string_compose (_("Could not find default UI configuration file %1"), default_ui_config_file_name) << endmsg;
253 /* reload color theme */
254 load_color_theme (false);
261 UIConfiguration::color_file_name (bool use_my, bool with_program_name, bool with_version) const
269 basename += color_file.get(); //this is the overall theme file, e.g. "dark"
271 if (with_program_name) {
273 basename += downcase (PROGRAM_NAME);
276 std::string rev (revision);
277 std::size_t pos = rev.find_first_of("-");
279 if (with_version && pos != string::npos && pos > 0) {
281 basename += rev.substr (0, pos); // COLORFILE_VERSION - program major.minor
284 basename += color_file_suffix;
290 UIConfiguration::load_color_theme (bool allow_own)
294 /* ColorsChanged() will trigger a parameter_changed () which
295 * in turn calls save_state()
297 PBD::Unwinder<uint32_t> uw (block_save, block_save + 1);
298 const bool running_from_source = ARDOUR_UI_UTILS::running_from_source_tree ();
302 PBD::Searchpath sp (user_config_directory());
304 /* user's own color files never have the program name in them */
306 if (find_file (sp, color_file_name (true, false, true), cfile)) {
311 if (find_file (sp, color_file_name (true, false, false), cfile)) {
319 if (find_file (theme_search_path(), color_file_name (false, running_from_source, true), cfile)) {
324 if (find_file (theme_search_path(), color_file_name (false, running_from_source, false), cfile)) {
331 warning << string_compose (_("Color file for %1 not found along %2"), color_file.get(), theme_search_path().to_string()) << endmsg;
338 info << string_compose (_("Loading color file %1"), cfile) << endmsg;
340 if (!tree.read (cfile.c_str())) {
341 error << string_compose(_("cannot read color file \"%1\""), cfile) << endmsg;
345 if (set_state (*tree.root(), Stateful::loading_state_version)) {
346 error << string_compose(_("color file \"%1\" not loaded successfully."), cfile) << endmsg;
356 UIConfiguration::store_color_theme ()
361 root = new XMLNode("Ardour");
363 XMLNode* parent = new XMLNode (X_("Colors"));
364 for (Colors::const_iterator i = colors.begin(); i != colors.end(); ++i) {
365 XMLNode* node = new XMLNode (X_("Color"));
366 node->add_property (X_("name"), i->first);
368 ss << "0x" << setw (8) << setfill ('0') << hex << i->second;
369 node->add_property (X_("value"), ss.str());
370 parent->add_child_nocopy (*node);
372 root->add_child_nocopy (*parent);
374 parent = new XMLNode (X_("ColorAliases"));
375 for (ColorAliases::const_iterator i = color_aliases.begin(); i != color_aliases.end(); ++i) {
376 XMLNode* node = new XMLNode (X_("ColorAlias"));
377 node->add_property (X_("name"), i->first);
378 node->add_property (X_("alias"), i->second);
379 parent->add_child_nocopy (*node);
381 root->add_child_nocopy (*parent);
383 parent = new XMLNode (X_("Modifiers"));
384 for (Modifiers::const_iterator i = modifiers.begin(); i != modifiers.end(); ++i) {
385 XMLNode* node = new XMLNode (X_("Modifier"));
386 node->add_property (X_("name"), i->first);
387 node->add_property (X_("modifier"), i->second.to_string());
388 parent->add_child_nocopy (*node);
390 root->add_child_nocopy (*parent);
393 std::string colorfile = Glib::build_filename (user_config_directory(), color_file_name (true, false, true));;
395 tree.set_root (root);
397 if (!tree.write (colorfile.c_str())){
398 error << string_compose (_("Color file %1 not saved"), colorfile) << endmsg;
406 UIConfiguration::load_state ()
408 LocaleGuard lg; // a single guard for all 3 configs
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 ()
500 root = new XMLNode("Ardour");
502 root->add_child_nocopy (get_variables ("UI"));
503 root->add_child_nocopy (get_variables ("Canvas"));
506 root->add_child_copy (*_extra_xml);
513 UIConfiguration::get_variables (std::string which_node)
518 node = new XMLNode (which_node);
520 #undef UI_CONFIG_VARIABLE
521 #undef CANVAS_FONT_VARIABLE
522 #define UI_CONFIG_VARIABLE(Type,var,Name,value) if (node->name() == "UI") { var.add_to_node (*node); }
523 #define CANVAS_FONT_VARIABLE(var,Name) if (node->name() == "Canvas") { var.add_to_node (*node); }
524 #include "ui_config_vars.h"
525 #include "canvas_vars.h"
526 #undef UI_CONFIG_VARIABLE
527 #undef CANVAS_FONT_VARIABLE
533 UIConfiguration::set_state (const XMLNode& root, int /*version*/)
536 /* this can load a generic UI configuration file or a colors file */
538 if (root.name() != "Ardour") {
542 Stateful::save_extra_xml (root);
544 XMLNodeList nlist = root.children();
545 XMLNodeConstIterator niter;
548 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
552 if (node->name() == "Canvas" || node->name() == "UI") {
553 set_variables (*node);
558 XMLNode* colors = find_named_node (root, X_("Colors"));
561 load_colors (*colors);
564 XMLNode* aliases = find_named_node (root, X_("ColorAliases"));
567 load_color_aliases (*aliases);
570 XMLNode* modifiers = find_named_node (root, X_("Modifiers"));
573 load_modifiers (*modifiers);
580 UIConfiguration::load_color_aliases (XMLNode const & node)
582 XMLNodeList const nlist = node.children();
583 XMLNodeConstIterator niter;
584 XMLProperty const *name;
585 XMLProperty const *alias;
587 color_aliases.clear ();
589 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
590 XMLNode const * child = *niter;
591 if (child->name() != X_("ColorAlias")) {
594 name = child->property (X_("name"));
595 alias = child->property (X_("alias"));
598 color_aliases.insert (make_pair (name->value(), alias->value()));
604 UIConfiguration::load_colors (XMLNode const & node)
606 XMLNodeList const nlist = node.children();
607 XMLNodeConstIterator niter;
608 XMLProperty const *name;
609 XMLProperty const *color;
613 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
614 XMLNode const * child = *niter;
615 if (child->name() != X_("Color")) {
618 name = child->property (X_("name"));
619 color = child->property (X_("value"));
622 ArdourCanvas::Color c;
623 c = strtoul (color->value().c_str(), 0, 16);
624 colors.insert (make_pair (name->value(), c));
630 UIConfiguration::load_modifiers (XMLNode const & node)
633 XMLNodeList const nlist = node.children();
634 XMLNodeConstIterator niter;
635 XMLProperty const *name;
636 XMLProperty const *mod;
640 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
641 XMLNode const * child = *niter;
642 if (child->name() != X_("Modifier")) {
646 name = child->property (X_("name"));
647 mod = child->property (X_("modifier"));
650 SVAModifier svam (mod->value());
651 modifiers.insert (make_pair (name->value(), svam));
657 UIConfiguration::set_variables (const XMLNode& node)
659 #undef UI_CONFIG_VARIABLE
660 #define UI_CONFIG_VARIABLE(Type,var,name,val) if (var.set_from_node (node)) { ParameterChanged (name); }
661 #define CANVAS_FONT_VARIABLE(var,name) if (var.set_from_node (node)) { ParameterChanged (name); }
662 #include "ui_config_vars.h"
663 #include "canvas_vars.h"
664 #undef UI_CONFIG_VARIABLE
665 #undef CANVAS_FONT_VARIABLE
668 ArdourCanvas::SVAModifier
669 UIConfiguration::modifier (string const & name) const
671 Modifiers::const_iterator m = modifiers.find (name);
672 if (m != modifiers.end()) {
675 return SVAModifier ();
679 UIConfiguration::color_mod (std::string const & colorname, std::string const & modifiername) const
681 return HSV (color (colorname)).mod (modifier (modifiername)).color ();
685 UIConfiguration::color_mod (const ArdourCanvas::Color& color, std::string const & modifiername) const
687 return HSV (color).mod (modifier (modifiername)).color ();
691 UIConfiguration::color (const std::string& name, bool* failed) const
693 ColorAliases::const_iterator e = color_aliases.find (name);
699 if (e != color_aliases.end ()) {
700 Colors::const_iterator rc = colors.find (e->second);
701 if (rc != colors.end()) {
705 /* not an alias, try directly */
706 Colors::const_iterator rc = colors.find (name);
707 if (rc != colors.end()) {
713 /* only show this message if the caller wasn't interested in
716 cerr << string_compose (_("Color %1 not found"), name) << endl;
723 return rgba_to_color ((g_random_int()%256)/255.0,
724 (g_random_int()%256)/255.0,
725 (g_random_int()%256)/255.0,
730 UIConfiguration::quantized (Color c) const
733 hsv.h = hue_width * (round (hsv.h/hue_width));
738 UIConfiguration::set_color (string const& name, ArdourCanvas::Color color)
740 Colors::iterator i = colors.find (name);
741 if (i == colors.end()) {
745 colors_modified = true;
747 ColorsChanged (); /* EMIT SIGNAL */
751 UIConfiguration::set_alias (string const & name, string const & alias)
753 ColorAliases::iterator i = color_aliases.find (name);
754 if (i == color_aliases.end()) {
759 aliases_modified = true;
761 ColorsChanged (); /* EMIT SIGNAL */
765 UIConfiguration::set_modifier (string const & name, SVAModifier svam)
767 Modifiers::iterator m = modifiers.find (name);
769 if (m == modifiers.end()) {
774 modifiers_modified = true;
776 ColorsChanged (); /* EMIT SIGNAL */
780 UIConfiguration::load_rc_file (bool themechange, bool allow_own)
782 string basename = ui_rc_file.get();
783 std::string rc_file_path;
785 if (!find_file (ardour_config_search_path(), basename, rc_file_path)) {
786 warning << string_compose (_("Unable to find UI style file %1 in search path %2. %3 will look strange"),
787 basename, ardour_config_search_path().to_string(), PROGRAM_NAME)
792 info << "Loading ui configuration file " << rc_file_path << endmsg;
794 Gtkmm2ext::UI::instance()->load_rcfile (rc_file_path, themechange);