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 <glibmm/miscutils.h>
38 #include <gtkmm/settings.h>
40 #include "pbd/convert.h"
41 #include "pbd/failed_constructor.h"
42 #include "pbd/xml++.h"
43 #include "pbd/file_utils.h"
44 #include "pbd/locale_guard.h"
45 #include "pbd/error.h"
46 #include "pbd/stacktrace.h"
48 #include "gtkmm2ext/rgb_macros.h"
49 #include "gtkmm2ext/gtk_ui.h"
51 #include "ardour/filesystem_paths.h"
52 #include "ardour/utils.h"
54 #include "ui_config.h"
60 using namespace ARDOUR;
61 using namespace ArdourCanvas;
63 static const char* ui_config_file_name = "ui_config";
64 static const char* default_ui_config_file_name = "default_ui_config";
66 static const double hue_width = 18.0;
69 UIConfiguration::instance ()
71 static UIConfiguration s_instance;
75 UIConfiguration::UIConfiguration ()
77 #undef UI_CONFIG_VARIABLE
78 #define UI_CONFIG_VARIABLE(Type,var,name,val) var (name,val),
79 #define CANVAS_FONT_VARIABLE(var,name) var (name),
80 #include "ui_config_vars.h"
81 #include "canvas_vars.h"
82 #undef UI_CONFIG_VARIABLE
83 #undef CANVAS_FONT_VARIABLE
86 aliases_modified (false),
87 colors_modified (false),
88 modifiers_modified (false),
93 ColorsChanged.connect (boost::bind (&UIConfiguration::colors_changed, this));
95 ParameterChanged.connect (sigc::mem_fun (*this, &UIConfiguration::parameter_changed));
98 UIConfiguration::~UIConfiguration ()
103 UIConfiguration::colors_changed ()
107 /* In theory, one of these ought to work:
109 gtk_rc_reparse_all_for_settings (gtk_settings_get_default(), true);
110 gtk_rc_reset_styles (gtk_settings_get_default());
112 but in practice, neither of them do. So just reload the current
113 GTK RC file, which causes a reset of all styles and a redraw
116 parameter_changed ("ui-rc-file");
120 UIConfiguration::parameter_changed (string param)
124 if (param == "ui-rc-file") {
126 } else if (param == "color-file") {
134 UIConfiguration::reset_gtk_theme ()
138 ss << "gtk_color_scheme = \"" << hex;
140 for (ColorAliases::iterator g = color_aliases.begin(); g != color_aliases.end(); ++g) {
142 if (g->first.find ("gtk_") == 0) {
143 const string gtk_name = g->first.substr (4);
144 ss << gtk_name << ":#" << std::setw (6) << setfill ('0') << (color (g->second) >> 8) << ';';
148 ss << '"' << dec << endl;
150 /* reset GTK color scheme */
152 Gtk::Settings::get_default()->property_gtk_color_scheme() = ss.str();
156 UIConfiguration::reset_dpi ()
158 long val = get_font_scale();
159 set_pango_fontsize ();
162 gtk_settings_set_long_property (gtk_settings_get_default(),
163 "gtk-xft-dpi", val, "ardour");
164 DPIReset(); //Emit Signal
168 UIConfiguration::set_pango_fontsize ()
170 long val = get_font_scale();
172 /* FT2 rendering - used by GnomeCanvas, sigh */
174 #ifndef PLATFORM_WINDOWS
175 pango_ft2_font_map_set_resolution ((PangoFT2FontMap*) pango_ft2_font_map_new(), val/1024, val/1024);
178 /* Cairo rendering, in case there is any */
180 pango_cairo_font_map_set_resolution ((PangoCairoFontMap*) pango_cairo_font_map_get_default(), val/1024);
184 UIConfiguration::get_ui_scale ()
186 return get_font_scale () / 102400.;
190 UIConfiguration::map_parameters (boost::function<void (std::string)>& functor)
192 #undef UI_CONFIG_VARIABLE
193 #define UI_CONFIG_VARIABLE(Type,var,Name,value) functor (Name);
194 #include "ui_config_vars.h"
195 #undef UI_CONFIG_VARIABLE
199 UIConfiguration::pre_gui_init ()
201 #ifdef CAIRO_SUPPORTS_FORCE_BUGGY_GRADIENTS_ENVIRONMENT_VARIABLE
202 if (get_buggy_gradients()) {
203 g_setenv ("FORCE_BUGGY_GRADIENTS", "1", 1);
206 #ifdef OPTIONAL_CAIRO_IMAGE_SURFACE
207 if (get_cairo_image_surface()) {
208 g_setenv ("ARDOUR_IMAGE_SURFACE", "1", 1);
215 UIConfiguration::post_gui_init ()
222 UIConfiguration::load_defaults ()
227 if (find_file (ardour_config_search_path(), default_ui_config_file_name, rcfile) ) {
230 info << string_compose (_("Loading default ui configuration file %1"), rcfile) << endmsg;
232 if (!tree.read (rcfile.c_str())) {
233 error << string_compose(_("cannot read default ui configuration file \"%1\""), rcfile) << endmsg;
235 if (set_state (*tree.root(), Stateful::loading_state_version)) {
236 error << string_compose(_("default ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
244 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);
251 ColorsChanged (); /* EMIT SIGNAL */
258 UIConfiguration::load_color_theme (bool allow_own)
266 basename += color_file.get();
267 basename += ".colors";
269 if (find_file (ardour_config_search_path(), basename, cfile)) {
275 basename = color_file.get();
276 basename += ".colors";
278 if (find_file (ardour_config_search_path(), basename, cfile)) {
287 info << string_compose (_("Loading color file %1"), cfile) << endmsg;
289 if (!tree.read (cfile.c_str())) {
290 error << string_compose(_("cannot read color file \"%1\""), cfile) << endmsg;
294 if (set_state (*tree.root(), Stateful::loading_state_version)) {
295 error << string_compose(_("color file \"%1\" not loaded successfully."), cfile) << endmsg;
301 warning << string_compose (_("Color file %1 not found"), basename) << endmsg;
308 UIConfiguration::store_color_theme ()
311 LocaleGuard lg (X_("C"));
313 root = new XMLNode("Ardour");
315 XMLNode* parent = new XMLNode (X_("Colors"));
316 for (Colors::const_iterator i = colors.begin(); i != colors.end(); ++i) {
317 XMLNode* node = new XMLNode (X_("Color"));
318 node->add_property (X_("name"), i->first);
320 ss << "0x" << setw (8) << setfill ('0') << hex << i->second;
321 node->add_property (X_("value"), ss.str());
322 parent->add_child_nocopy (*node);
324 root->add_child_nocopy (*parent);
326 parent = new XMLNode (X_("ColorAliases"));
327 for (ColorAliases::const_iterator i = color_aliases.begin(); i != color_aliases.end(); ++i) {
328 XMLNode* node = new XMLNode (X_("ColorAlias"));
329 node->add_property (X_("name"), i->first);
330 node->add_property (X_("alias"), i->second);
331 parent->add_child_nocopy (*node);
333 root->add_child_nocopy (*parent);
335 parent = new XMLNode (X_("Modifiers"));
336 for (Modifiers::const_iterator i = modifiers.begin(); i != modifiers.end(); ++i) {
337 XMLNode* node = new XMLNode (X_("Modifier"));
338 node->add_property (X_("name"), i->first);
339 node->add_property (X_("modifier"), i->second.to_string());
340 parent->add_child_nocopy (*node);
342 root->add_child_nocopy (*parent);
345 std::string colorfile = Glib::build_filename (user_config_directory(), (string ("my-") + color_file.get() + ".colors"));
347 tree.set_root (root);
349 if (!tree.write (colorfile.c_str())){
350 error << string_compose (_("Color file %1 not saved"), colorfile) << endmsg;
358 UIConfiguration::load_state ()
364 if (find_file (ardour_config_search_path(), default_ui_config_file_name, rcfile)) {
368 info << string_compose (_("Loading default ui configuration file %1"), rcfile) << endmsg;
370 if (!tree.read (rcfile.c_str())) {
371 error << string_compose(_("cannot read default ui configuration file \"%1\""), rcfile) << endmsg;
375 if (set_state (*tree.root(), Stateful::loading_state_version)) {
376 error << string_compose(_("default ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
381 if (find_file (ardour_config_search_path(), ui_config_file_name, rcfile)) {
385 info << string_compose (_("Loading user ui configuration file %1"), rcfile) << endmsg;
387 if (!tree.read (rcfile)) {
388 error << string_compose(_("cannot read ui configuration file \"%1\""), rcfile) << endmsg;
392 if (set_state (*tree.root(), Stateful::loading_state_version)) {
393 error << string_compose(_("user ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
401 error << _("could not find any ui configuration file, canvas will look broken.") << endmsg;
408 UIConfiguration::save_state()
412 std::string rcfile = Glib::build_filename (user_config_directory(), ui_config_file_name);
416 tree.set_root (&get_state());
418 if (!tree.write (rcfile.c_str())){
419 error << string_compose (_("Config file %1 not saved"), rcfile) << endmsg;
426 if (aliases_modified || colors_modified || modifiers_modified) {
428 if (store_color_theme ()) {
429 error << string_compose (_("Color file %1 not saved"), color_file.get()) << endmsg;
433 aliases_modified = false;
434 colors_modified = false;
435 modifiers_modified = false;
443 UIConfiguration::get_state ()
446 LocaleGuard lg (X_("C"));
448 root = new XMLNode("Ardour");
450 root->add_child_nocopy (get_variables ("UI"));
451 root->add_child_nocopy (get_variables ("Canvas"));
454 root->add_child_copy (*_extra_xml);
461 UIConfiguration::get_variables (std::string which_node)
464 LocaleGuard lg (X_("C"));
466 node = new XMLNode (which_node);
468 #undef UI_CONFIG_VARIABLE
469 #undef CANVAS_FONT_VARIABLE
470 #define UI_CONFIG_VARIABLE(Type,var,Name,value) if (node->name() == "UI") { var.add_to_node (*node); }
471 #define CANVAS_FONT_VARIABLE(var,Name) if (node->name() == "Canvas") { var.add_to_node (*node); }
472 #include "ui_config_vars.h"
473 #include "canvas_vars.h"
474 #undef UI_CONFIG_VARIABLE
475 #undef CANVAS_FONT_VARIABLE
481 UIConfiguration::set_state (const XMLNode& root, int /*version*/)
483 /* this can load a generic UI configuration file or a colors file */
485 if (root.name() != "Ardour") {
489 Stateful::save_extra_xml (root);
491 XMLNodeList nlist = root.children();
492 XMLNodeConstIterator niter;
495 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
499 if (node->name() == "Canvas" || node->name() == "UI") {
500 set_variables (*node);
505 XMLNode* colors = find_named_node (root, X_("Colors"));
508 load_colors (*colors);
511 XMLNode* aliases = find_named_node (root, X_("ColorAliases"));
514 load_color_aliases (*aliases);
517 XMLNode* modifiers = find_named_node (root, X_("Modifiers"));
520 load_modifiers (*modifiers);
527 UIConfiguration::load_color_aliases (XMLNode const & node)
529 XMLNodeList const nlist = node.children();
530 XMLNodeConstIterator niter;
531 XMLProperty const *name;
532 XMLProperty const *alias;
534 color_aliases.clear ();
536 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
537 if ((*niter)->name() != X_("ColorAlias")) {
540 name = (*niter)->property (X_("name"));
541 alias = (*niter)->property (X_("alias"));
544 color_aliases.insert (make_pair (name->value(), alias->value()));
550 UIConfiguration::load_colors (XMLNode const & node)
552 XMLNodeList const nlist = node.children();
553 XMLNodeConstIterator niter;
554 XMLProperty const *name;
555 XMLProperty const *color;
559 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
560 if ((*niter)->name() != X_("Color")) {
563 name = (*niter)->property (X_("name"));
564 color = (*niter)->property (X_("value"));
567 ArdourCanvas::Color c;
568 c = strtoul (color->value().c_str(), 0, 16);
569 colors.insert (make_pair (name->value(), c));
575 UIConfiguration::load_modifiers (XMLNode const & node)
577 PBD::LocaleGuard lg ("C");
578 XMLNodeList const nlist = node.children();
579 XMLNodeConstIterator niter;
580 XMLProperty const *name;
581 XMLProperty const *mod;
585 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
586 if ((*niter)->name() != X_("Modifier")) {
590 name = (*niter)->property (X_("name"));
591 mod = (*niter)->property (X_("modifier"));
594 SVAModifier svam (mod->value());
595 modifiers.insert (make_pair (name->value(), svam));
601 UIConfiguration::set_variables (const XMLNode& node)
603 #undef UI_CONFIG_VARIABLE
604 #define UI_CONFIG_VARIABLE(Type,var,name,val) if (var.set_from_node (node)) { ParameterChanged (name); }
605 #define CANVAS_FONT_VARIABLE(var,name) if (var.set_from_node (node)) { ParameterChanged (name); }
606 #include "ui_config_vars.h"
607 #include "canvas_vars.h"
608 #undef UI_CONFIG_VARIABLE
609 #undef CANVAS_FONT_VARIABLE
612 ArdourCanvas::SVAModifier
613 UIConfiguration::modifier (string const & name) const
615 Modifiers::const_iterator m = modifiers.find (name);
616 if (m != modifiers.end()) {
619 return SVAModifier ();
623 UIConfiguration::color_mod (std::string const & colorname, std::string const & modifiername) const
625 return HSV (color (colorname)).mod (modifier (modifiername)).color ();
629 UIConfiguration::color_mod (const ArdourCanvas::Color& color, std::string const & modifiername) const
631 return HSV (color).mod (modifier (modifiername)).color ();
635 UIConfiguration::color (const std::string& name, bool* failed) const
637 ColorAliases::const_iterator e = color_aliases.find (name);
643 if (e != color_aliases.end ()) {
644 Colors::const_iterator rc = colors.find (e->second);
645 if (rc != colors.end()) {
649 /* not an alias, try directly */
650 Colors::const_iterator rc = colors.find (name);
651 if (rc != colors.end()) {
657 /* only show this message if the caller wasn't interested in
660 cerr << string_compose (_("Color %1 not found"), name) << endl;
667 return rgba_to_color ((g_random_int()%256)/255.0,
668 (g_random_int()%256)/255.0,
669 (g_random_int()%256)/255.0,
674 UIConfiguration::quantized (Color c) const
677 hsv.h = hue_width * (round (hsv.h/hue_width));
682 UIConfiguration::set_color (string const& name, ArdourCanvas::Color color)
684 Colors::iterator i = colors.find (name);
685 if (i == colors.end()) {
689 colors_modified = true;
691 ColorsChanged (); /* EMIT SIGNAL */
695 UIConfiguration::set_alias (string const & name, string const & alias)
697 ColorAliases::iterator i = color_aliases.find (name);
698 if (i == color_aliases.end()) {
703 aliases_modified = true;
705 ColorsChanged (); /* EMIT SIGNAL */
709 UIConfiguration::set_modifier (string const & name, SVAModifier svam)
711 Modifiers::iterator m = modifiers.find (name);
713 if (m == modifiers.end()) {
718 modifiers_modified = true;
720 ColorsChanged (); /* EMIT SIGNAL */
724 UIConfiguration::load_rc_file (bool themechange, bool allow_own)
726 string basename = ui_rc_file.get();
727 std::string rc_file_path;
729 if (!find_file (ardour_config_search_path(), basename, rc_file_path)) {
730 warning << string_compose (_("Unable to find UI style file %1 in search path %2. %3 will look strange"),
731 basename, ardour_config_search_path().to_string(), PROGRAM_NAME)
736 info << "Loading ui configuration file " << rc_file_path << endmsg;
738 Gtkmm2ext::UI::instance()->load_rcfile (rc_file_path, themechange);