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/utils.h"
55 #include "ui_config.h"
61 using namespace ARDOUR;
62 using namespace ArdourCanvas;
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;
70 UIConfiguration::instance ()
72 static UIConfiguration s_instance;
76 UIConfiguration::UIConfiguration ()
78 #undef UI_CONFIG_VARIABLE
79 #define UI_CONFIG_VARIABLE(Type,var,name,val) var (name,val),
80 #define CANVAS_FONT_VARIABLE(var,name) var (name),
81 #include "ui_config_vars.h"
82 #include "canvas_vars.h"
83 #undef UI_CONFIG_VARIABLE
84 #undef CANVAS_FONT_VARIABLE
87 aliases_modified (false),
88 colors_modified (false),
89 modifiers_modified (false),
94 ColorsChanged.connect (boost::bind (&UIConfiguration::colors_changed, this));
96 ParameterChanged.connect (sigc::mem_fun (*this, &UIConfiguration::parameter_changed));
99 UIConfiguration::~UIConfiguration ()
104 UIConfiguration::colors_changed ()
108 /* In theory, one of these ought to work:
110 gtk_rc_reparse_all_for_settings (gtk_settings_get_default(), true);
111 gtk_rc_reset_styles (gtk_settings_get_default());
113 but in practice, neither of them do. So just reload the current
114 GTK RC file, which causes a reset of all styles and a redraw
117 parameter_changed ("ui-rc-file");
121 UIConfiguration::parameter_changed (string param)
125 if (param == "ui-rc-file") {
127 } else if (param == "color-file") {
135 UIConfiguration::reset_gtk_theme ()
139 ss << "gtk_color_scheme = \"" << hex;
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 ss << gtk_name << ":#" << std::setw (6) << setfill ('0') << (color (g->second) >> 8) << ';';
149 ss << '"' << dec << endl;
151 /* reset GTK color scheme */
153 Gtk::Settings::get_default()->property_gtk_color_scheme() = ss.str();
157 UIConfiguration::reset_dpi ()
159 long val = get_font_scale();
160 set_pango_fontsize ();
163 gtk_settings_set_long_property (gtk_settings_get_default(),
164 "gtk-xft-dpi", val, "ardour");
165 DPIReset(); //Emit Signal
169 UIConfiguration::set_pango_fontsize ()
171 long val = get_font_scale();
173 /* FT2 rendering - used by GnomeCanvas, sigh */
175 #ifndef PLATFORM_WINDOWS
176 pango_ft2_font_map_set_resolution ((PangoFT2FontMap*) pango_ft2_font_map_new(), val/1024, val/1024);
179 /* Cairo rendering, in case there is any */
181 pango_cairo_font_map_set_resolution ((PangoCairoFontMap*) pango_cairo_font_map_get_default(), val/1024);
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 ()
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;
250 /* reload color theme */
251 load_color_theme (false);
258 UIConfiguration::load_color_theme (bool allow_own)
263 /* ColorsChanged() will trigger a parameter_changed () which
264 * in turn calls save_state()
266 PBD::Unwinder<uint32_t> uw (block_save, block_save + 1);
270 basename += color_file.get();
271 basename += ".colors";
273 if (find_file (ardour_config_search_path(), basename, cfile)) {
279 basename = color_file.get();
280 basename += ".colors";
282 if (find_file (ardour_config_search_path(), basename, cfile)) {
291 info << string_compose (_("Loading color file %1"), cfile) << endmsg;
293 if (!tree.read (cfile.c_str())) {
294 error << string_compose(_("cannot read color file \"%1\""), cfile) << endmsg;
298 if (set_state (*tree.root(), Stateful::loading_state_version)) {
299 error << string_compose(_("color file \"%1\" not loaded successfully."), cfile) << endmsg;
305 warning << string_compose (_("Color file %1 not found"), basename) << endmsg;
312 UIConfiguration::store_color_theme ()
317 root = new XMLNode("Ardour");
319 XMLNode* parent = new XMLNode (X_("Colors"));
320 for (Colors::const_iterator i = colors.begin(); i != colors.end(); ++i) {
321 XMLNode* node = new XMLNode (X_("Color"));
322 node->add_property (X_("name"), i->first);
324 ss << "0x" << setw (8) << setfill ('0') << hex << i->second;
325 node->add_property (X_("value"), ss.str());
326 parent->add_child_nocopy (*node);
328 root->add_child_nocopy (*parent);
330 parent = new XMLNode (X_("ColorAliases"));
331 for (ColorAliases::const_iterator i = color_aliases.begin(); i != color_aliases.end(); ++i) {
332 XMLNode* node = new XMLNode (X_("ColorAlias"));
333 node->add_property (X_("name"), i->first);
334 node->add_property (X_("alias"), i->second);
335 parent->add_child_nocopy (*node);
337 root->add_child_nocopy (*parent);
339 parent = new XMLNode (X_("Modifiers"));
340 for (Modifiers::const_iterator i = modifiers.begin(); i != modifiers.end(); ++i) {
341 XMLNode* node = new XMLNode (X_("Modifier"));
342 node->add_property (X_("name"), i->first);
343 node->add_property (X_("modifier"), i->second.to_string());
344 parent->add_child_nocopy (*node);
346 root->add_child_nocopy (*parent);
349 std::string colorfile = Glib::build_filename (user_config_directory(), (string ("my-") + color_file.get() + ".colors"));
351 tree.set_root (root);
353 if (!tree.write (colorfile.c_str())){
354 error << string_compose (_("Color file %1 not saved"), colorfile) << endmsg;
362 UIConfiguration::load_state ()
364 LocaleGuard lg; // a single guard for all 3 configs
369 if (find_file (ardour_config_search_path(), default_ui_config_file_name, rcfile)) {
373 info << string_compose (_("Loading default ui configuration file %1"), rcfile) << endmsg;
375 if (!tree.read (rcfile.c_str())) {
376 error << string_compose(_("cannot read default ui configuration file \"%1\""), rcfile) << endmsg;
380 if (set_state (*tree.root(), Stateful::loading_state_version)) {
381 error << string_compose(_("default ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
386 if (find_file (ardour_config_search_path(), ui_config_file_name, rcfile)) {
390 info << string_compose (_("Loading user ui configuration file %1"), rcfile) << endmsg;
392 if (!tree.read (rcfile)) {
393 error << string_compose(_("cannot read ui configuration file \"%1\""), rcfile) << endmsg;
397 if (set_state (*tree.root(), Stateful::loading_state_version)) {
398 error << string_compose(_("user ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
406 error << _("could not find any ui configuration file, canvas will look broken.") << endmsg;
413 UIConfiguration::save_state()
415 if (block_save != 0) {
420 std::string rcfile = Glib::build_filename (user_config_directory(), ui_config_file_name);
424 tree.set_root (&get_state());
426 if (!tree.write (rcfile.c_str())){
427 error << string_compose (_("Config file %1 not saved"), rcfile) << endmsg;
434 if (aliases_modified || colors_modified || modifiers_modified) {
436 if (store_color_theme ()) {
437 error << string_compose (_("Color file %1 not saved"), color_file.get()) << endmsg;
441 aliases_modified = false;
442 colors_modified = false;
443 modifiers_modified = false;
451 UIConfiguration::get_state ()
456 root = new XMLNode("Ardour");
458 root->add_child_nocopy (get_variables ("UI"));
459 root->add_child_nocopy (get_variables ("Canvas"));
462 root->add_child_copy (*_extra_xml);
469 UIConfiguration::get_variables (std::string which_node)
474 node = new XMLNode (which_node);
476 #undef UI_CONFIG_VARIABLE
477 #undef CANVAS_FONT_VARIABLE
478 #define UI_CONFIG_VARIABLE(Type,var,Name,value) if (node->name() == "UI") { var.add_to_node (*node); }
479 #define CANVAS_FONT_VARIABLE(var,Name) if (node->name() == "Canvas") { var.add_to_node (*node); }
480 #include "ui_config_vars.h"
481 #include "canvas_vars.h"
482 #undef UI_CONFIG_VARIABLE
483 #undef CANVAS_FONT_VARIABLE
489 UIConfiguration::set_state (const XMLNode& root, int /*version*/)
492 /* this can load a generic UI configuration file or a colors file */
494 if (root.name() != "Ardour") {
498 Stateful::save_extra_xml (root);
500 XMLNodeList nlist = root.children();
501 XMLNodeConstIterator niter;
504 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
508 if (node->name() == "Canvas" || node->name() == "UI") {
509 set_variables (*node);
514 XMLNode* colors = find_named_node (root, X_("Colors"));
517 load_colors (*colors);
520 XMLNode* aliases = find_named_node (root, X_("ColorAliases"));
523 load_color_aliases (*aliases);
526 XMLNode* modifiers = find_named_node (root, X_("Modifiers"));
529 load_modifiers (*modifiers);
536 UIConfiguration::load_color_aliases (XMLNode const & node)
538 XMLNodeList const nlist = node.children();
539 XMLNodeConstIterator niter;
540 XMLProperty const *name;
541 XMLProperty const *alias;
543 color_aliases.clear ();
545 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
546 XMLNode const * child = *niter;
547 if (child->name() != X_("ColorAlias")) {
550 name = child->property (X_("name"));
551 alias = child->property (X_("alias"));
554 color_aliases.insert (make_pair (name->value(), alias->value()));
560 UIConfiguration::load_colors (XMLNode const & node)
562 XMLNodeList const nlist = node.children();
563 XMLNodeConstIterator niter;
564 XMLProperty const *name;
565 XMLProperty const *color;
569 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
570 XMLNode const * child = *niter;
571 if (child->name() != X_("Color")) {
574 name = child->property (X_("name"));
575 color = child->property (X_("value"));
578 ArdourCanvas::Color c;
579 c = strtoul (color->value().c_str(), 0, 16);
580 colors.insert (make_pair (name->value(), c));
586 UIConfiguration::load_modifiers (XMLNode const & node)
589 XMLNodeList const nlist = node.children();
590 XMLNodeConstIterator niter;
591 XMLProperty const *name;
592 XMLProperty const *mod;
596 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
597 XMLNode const * child = *niter;
598 if (child->name() != X_("Modifier")) {
602 name = child->property (X_("name"));
603 mod = child->property (X_("modifier"));
606 SVAModifier svam (mod->value());
607 modifiers.insert (make_pair (name->value(), svam));
613 UIConfiguration::set_variables (const XMLNode& node)
615 #undef UI_CONFIG_VARIABLE
616 #define UI_CONFIG_VARIABLE(Type,var,name,val) if (var.set_from_node (node)) { ParameterChanged (name); }
617 #define CANVAS_FONT_VARIABLE(var,name) if (var.set_from_node (node)) { ParameterChanged (name); }
618 #include "ui_config_vars.h"
619 #include "canvas_vars.h"
620 #undef UI_CONFIG_VARIABLE
621 #undef CANVAS_FONT_VARIABLE
624 ArdourCanvas::SVAModifier
625 UIConfiguration::modifier (string const & name) const
627 Modifiers::const_iterator m = modifiers.find (name);
628 if (m != modifiers.end()) {
631 return SVAModifier ();
635 UIConfiguration::color_mod (std::string const & colorname, std::string const & modifiername) const
637 return HSV (color (colorname)).mod (modifier (modifiername)).color ();
641 UIConfiguration::color_mod (const ArdourCanvas::Color& color, std::string const & modifiername) const
643 return HSV (color).mod (modifier (modifiername)).color ();
647 UIConfiguration::color (const std::string& name, bool* failed) const
649 ColorAliases::const_iterator e = color_aliases.find (name);
655 if (e != color_aliases.end ()) {
656 Colors::const_iterator rc = colors.find (e->second);
657 if (rc != colors.end()) {
661 /* not an alias, try directly */
662 Colors::const_iterator rc = colors.find (name);
663 if (rc != colors.end()) {
669 /* only show this message if the caller wasn't interested in
672 cerr << string_compose (_("Color %1 not found"), name) << endl;
679 return rgba_to_color ((g_random_int()%256)/255.0,
680 (g_random_int()%256)/255.0,
681 (g_random_int()%256)/255.0,
686 UIConfiguration::quantized (Color c) const
689 hsv.h = hue_width * (round (hsv.h/hue_width));
694 UIConfiguration::set_color (string const& name, ArdourCanvas::Color color)
696 Colors::iterator i = colors.find (name);
697 if (i == colors.end()) {
701 colors_modified = true;
703 ColorsChanged (); /* EMIT SIGNAL */
707 UIConfiguration::set_alias (string const & name, string const & alias)
709 ColorAliases::iterator i = color_aliases.find (name);
710 if (i == color_aliases.end()) {
715 aliases_modified = true;
717 ColorsChanged (); /* EMIT SIGNAL */
721 UIConfiguration::set_modifier (string const & name, SVAModifier svam)
723 Modifiers::iterator m = modifiers.find (name);
725 if (m == modifiers.end()) {
730 modifiers_modified = true;
732 ColorsChanged (); /* EMIT SIGNAL */
736 UIConfiguration::load_rc_file (bool themechange, bool allow_own)
738 string basename = ui_rc_file.get();
739 std::string rc_file_path;
741 if (!find_file (ardour_config_search_path(), basename, rc_file_path)) {
742 warning << string_compose (_("Unable to find UI style file %1 in search path %2. %3 will look strange"),
743 basename, ardour_config_search_path().to_string(), PROGRAM_NAME)
748 info << "Loading ui configuration file " << rc_file_path << endmsg;
750 Gtkmm2ext::UI::instance()->load_rcfile (rc_file_path, themechange);