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>
36 #include <glib/gstdio.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";
65 UIConfiguration* UIConfiguration::_instance = 0;
67 static const double hue_width = 18.0;
69 sigc::signal<void> UIConfiguration::ColorsChanged;
71 sigc::signal<void> UIConfiguration::DPIReset;
73 UIConfiguration::UIConfiguration ()
75 #undef UI_CONFIG_VARIABLE
76 #define UI_CONFIG_VARIABLE(Type,var,name,val) var (name,val),
77 #define CANVAS_FONT_VARIABLE(var,name) var (name),
78 #include "ui_config_vars.h"
79 #include "canvas_vars.h"
80 #undef UI_CONFIG_VARIABLE
81 #undef CANVAS_FONT_VARIABLE
84 aliases_modified (false),
85 colors_modified (false),
86 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::map_parameters (boost::function<void (std::string)>& functor)
186 #undef UI_CONFIG_VARIABLE
187 #define UI_CONFIG_VARIABLE(Type,var,Name,value) functor (Name);
188 #include "ui_config_vars.h"
189 #undef UI_CONFIG_VARIABLE
193 UIConfiguration::pre_gui_init ()
195 #ifdef CAIRO_SUPPORTS_FORCE_BUGGY_GRADIENTS_ENVIRONMENT_VARIABLE
196 if (get_buggy_gradients()) {
197 g_setenv ("FORCE_BUGGY_GRADIENTS", "1", 1);
200 #ifdef OPTIONAL_CAIRO_IMAGE_SURFACE
201 if (get_cairo_image_surface()) {
202 g_setenv ("ARDOUR_IMAGE_SURFACE", "1", 1);
209 UIConfiguration::post_gui_init ()
216 UIConfiguration::load_defaults ()
221 if (find_file (ardour_config_search_path(), default_ui_config_file_name, rcfile) ) {
224 info << string_compose (_("Loading default ui configuration file %1"), rcfile) << endmsg;
226 if (!tree.read (rcfile.c_str())) {
227 error << string_compose(_("cannot read default ui configuration file \"%1\""), rcfile) << endmsg;
229 if (set_state (*tree.root(), Stateful::loading_state_version)) {
230 error << string_compose(_("default ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
238 warning << string_compose (_("Could not find default UI configuration file %1"), default_ui_config_file_name) << endmsg;
243 /* reload color theme */
244 load_color_theme (false);
245 ColorsChanged (); /* EMIT SIGNAL */
252 UIConfiguration::load_color_theme (bool allow_own)
260 basename += color_file.get();
261 basename += ".colors";
263 if (find_file (ardour_config_search_path(), basename, cfile)) {
269 basename = color_file.get();
270 basename += ".colors";
272 if (find_file (ardour_config_search_path(), basename, cfile)) {
281 info << string_compose (_("Loading color file %1"), cfile) << endmsg;
283 if (!tree.read (cfile.c_str())) {
284 error << string_compose(_("cannot read color file \"%1\""), cfile) << endmsg;
288 if (set_state (*tree.root(), Stateful::loading_state_version)) {
289 error << string_compose(_("color file \"%1\" not loaded successfully."), cfile) << endmsg;
295 warning << string_compose (_("Color file %1 not found"), basename) << endmsg;
302 UIConfiguration::store_color_theme ()
305 LocaleGuard lg (X_("C"));
307 root = new XMLNode("Ardour");
309 XMLNode* parent = new XMLNode (X_("Colors"));
310 for (Colors::const_iterator i = colors.begin(); i != colors.end(); ++i) {
311 XMLNode* node = new XMLNode (X_("Color"));
312 node->add_property (X_("name"), i->first);
314 ss << "0x" << setw (8) << setfill ('0') << hex << i->second;
315 node->add_property (X_("value"), ss.str());
316 parent->add_child_nocopy (*node);
318 root->add_child_nocopy (*parent);
320 parent = new XMLNode (X_("ColorAliases"));
321 for (ColorAliases::const_iterator i = color_aliases.begin(); i != color_aliases.end(); ++i) {
322 XMLNode* node = new XMLNode (X_("ColorAlias"));
323 node->add_property (X_("name"), i->first);
324 node->add_property (X_("alias"), i->second);
325 parent->add_child_nocopy (*node);
327 root->add_child_nocopy (*parent);
329 parent = new XMLNode (X_("Modifiers"));
330 for (Modifiers::const_iterator i = modifiers.begin(); i != modifiers.end(); ++i) {
331 XMLNode* node = new XMLNode (X_("Modifier"));
332 node->add_property (X_("name"), i->first);
333 node->add_property (X_("modifier"), i->second.to_string());
334 parent->add_child_nocopy (*node);
336 root->add_child_nocopy (*parent);
339 std::string colorfile = Glib::build_filename (user_config_directory(), (string ("my-") + color_file.get() + ".colors"));
341 tree.set_root (root);
343 if (!tree.write (colorfile.c_str())){
344 error << string_compose (_("Color file %1 not saved"), colorfile) << endmsg;
352 UIConfiguration::load_state ()
358 if (find_file (ardour_config_search_path(), default_ui_config_file_name, rcfile)) {
362 info << string_compose (_("Loading default ui configuration file %1"), rcfile) << endmsg;
364 if (!tree.read (rcfile.c_str())) {
365 error << string_compose(_("cannot read default ui configuration file \"%1\""), rcfile) << endmsg;
369 if (set_state (*tree.root(), Stateful::loading_state_version)) {
370 error << string_compose(_("default ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
375 if (find_file (ardour_config_search_path(), ui_config_file_name, rcfile)) {
379 info << string_compose (_("Loading user ui configuration file %1"), rcfile) << endmsg;
381 if (!tree.read (rcfile)) {
382 error << string_compose(_("cannot read ui configuration file \"%1\""), rcfile) << endmsg;
386 if (set_state (*tree.root(), Stateful::loading_state_version)) {
387 error << string_compose(_("user ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
395 error << _("could not find any ui configuration file, canvas will look broken.") << endmsg;
402 UIConfiguration::save_state()
406 std::string rcfile = Glib::build_filename (user_config_directory(), ui_config_file_name);
410 tree.set_root (&get_state());
412 if (!tree.write (rcfile.c_str())){
413 error << string_compose (_("Config file %1 not saved"), rcfile) << endmsg;
420 if (aliases_modified || colors_modified || modifiers_modified) {
422 if (store_color_theme ()) {
423 error << string_compose (_("Color file %1 not saved"), color_file.get()) << endmsg;
427 aliases_modified = false;
428 colors_modified = false;
429 modifiers_modified = false;
437 UIConfiguration::get_state ()
440 LocaleGuard lg (X_("C"));
442 root = new XMLNode("Ardour");
444 root->add_child_nocopy (get_variables ("UI"));
445 root->add_child_nocopy (get_variables ("Canvas"));
448 root->add_child_copy (*_extra_xml);
455 UIConfiguration::get_variables (std::string which_node)
458 LocaleGuard lg (X_("C"));
460 node = new XMLNode (which_node);
462 #undef UI_CONFIG_VARIABLE
463 #undef CANVAS_FONT_VARIABLE
464 #define UI_CONFIG_VARIABLE(Type,var,Name,value) if (node->name() == "UI") { var.add_to_node (*node); }
465 #define CANVAS_FONT_VARIABLE(var,Name) if (node->name() == "Canvas") { var.add_to_node (*node); }
466 #include "ui_config_vars.h"
467 #include "canvas_vars.h"
468 #undef UI_CONFIG_VARIABLE
469 #undef CANVAS_FONT_VARIABLE
475 UIConfiguration::set_state (const XMLNode& root, int /*version*/)
477 /* this can load a generic UI configuration file or a colors file */
479 if (root.name() != "Ardour") {
483 Stateful::save_extra_xml (root);
485 XMLNodeList nlist = root.children();
486 XMLNodeConstIterator niter;
489 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
493 if (node->name() == "Canvas" || node->name() == "UI") {
494 set_variables (*node);
499 XMLNode* colors = find_named_node (root, X_("Colors"));
502 load_colors (*colors);
505 XMLNode* aliases = find_named_node (root, X_("ColorAliases"));
508 load_color_aliases (*aliases);
511 XMLNode* modifiers = find_named_node (root, X_("Modifiers"));
514 load_modifiers (*modifiers);
521 UIConfiguration::load_color_aliases (XMLNode const & node)
523 XMLNodeList const nlist = node.children();
524 XMLNodeConstIterator niter;
525 XMLProperty const *name;
526 XMLProperty const *alias;
528 color_aliases.clear ();
530 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
531 if ((*niter)->name() != X_("ColorAlias")) {
534 name = (*niter)->property (X_("name"));
535 alias = (*niter)->property (X_("alias"));
538 color_aliases.insert (make_pair (name->value(), alias->value()));
544 UIConfiguration::load_colors (XMLNode const & node)
546 XMLNodeList const nlist = node.children();
547 XMLNodeConstIterator niter;
548 XMLProperty const *name;
549 XMLProperty const *color;
553 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
554 if ((*niter)->name() != X_("Color")) {
557 name = (*niter)->property (X_("name"));
558 color = (*niter)->property (X_("value"));
561 ArdourCanvas::Color c;
562 c = strtoul (color->value().c_str(), 0, 16);
563 colors.insert (make_pair (name->value(), c));
569 UIConfiguration::load_modifiers (XMLNode const & node)
571 PBD::LocaleGuard lg ("C");
572 XMLNodeList const nlist = node.children();
573 XMLNodeConstIterator niter;
574 XMLProperty const *name;
575 XMLProperty const *mod;
579 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
580 if ((*niter)->name() != X_("Modifier")) {
584 name = (*niter)->property (X_("name"));
585 mod = (*niter)->property (X_("modifier"));
588 SVAModifier svam (mod->value());
589 modifiers.insert (make_pair (name->value(), svam));
595 UIConfiguration::set_variables (const XMLNode& node)
597 #undef UI_CONFIG_VARIABLE
598 #define UI_CONFIG_VARIABLE(Type,var,name,val) if (var.set_from_node (node)) { ParameterChanged (name); }
599 #define CANVAS_FONT_VARIABLE(var,name) if (var.set_from_node (node)) { ParameterChanged (name); }
600 #include "ui_config_vars.h"
601 #include "canvas_vars.h"
602 #undef UI_CONFIG_VARIABLE
603 #undef CANVAS_FONT_VARIABLE
606 ArdourCanvas::SVAModifier
607 UIConfiguration::modifier (string const & name) const
609 Modifiers::const_iterator m = modifiers.find (name);
610 if (m != modifiers.end()) {
613 return SVAModifier ();
617 UIConfiguration::color_mod (std::string const & colorname, std::string const & modifiername) const
619 return HSV (color (colorname)).mod (modifier (modifiername)).color ();
623 UIConfiguration::color_mod (const ArdourCanvas::Color& color, std::string const & modifiername) const
625 return HSV (color).mod (modifier (modifiername)).color ();
629 UIConfiguration::color (const std::string& name, bool* failed) const
631 ColorAliases::const_iterator e = color_aliases.find (name);
637 if (e != color_aliases.end ()) {
638 Colors::const_iterator rc = colors.find (e->second);
639 if (rc != colors.end()) {
643 /* not an alias, try directly */
644 Colors::const_iterator rc = colors.find (name);
645 if (rc != colors.end()) {
651 /* only show this message if the caller wasn't interested in
654 cerr << string_compose (_("Color %1 not found"), name) << endl;
661 return rgba_to_color ((g_random_int()%256)/255.0,
662 (g_random_int()%256)/255.0,
663 (g_random_int()%256)/255.0,
668 UIConfiguration::quantized (Color c) const
671 hsv.h = hue_width * (round (hsv.h/hue_width));
676 UIConfiguration::set_color (string const& name, ArdourCanvas::Color color)
678 Colors::iterator i = colors.find (name);
679 if (i == colors.end()) {
683 colors_modified = true;
685 ColorsChanged (); /* EMIT SIGNAL */
689 UIConfiguration::set_alias (string const & name, string const & alias)
691 ColorAliases::iterator i = color_aliases.find (name);
692 if (i == color_aliases.end()) {
697 aliases_modified = true;
699 ColorsChanged (); /* EMIT SIGNAL */
703 UIConfiguration::set_modifier (string const & name, SVAModifier svam)
705 Modifiers::iterator m = modifiers.find (name);
707 if (m == modifiers.end()) {
712 modifiers_modified = true;
714 ColorsChanged (); /* EMIT SIGNAL */
718 UIConfiguration::load_rc_file (bool themechange, bool allow_own)
720 string basename = ui_rc_file.get();
721 std::string rc_file_path;
723 if (!find_file (ardour_config_search_path(), basename, rc_file_path)) {
724 warning << string_compose (_("Unable to find UI style file %1 in search path %2. %3 will look strange"),
725 basename, ardour_config_search_path().to_string(), PROGRAM_NAME)
730 info << "Loading ui configuration file " << rc_file_path << endmsg;
732 Gtkmm2ext::UI::instance()->load_rcfile (rc_file_path, themechange);