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 "pbd/convert.h"
39 #include "pbd/failed_constructor.h"
40 #include "pbd/xml++.h"
41 #include "pbd/file_utils.h"
42 #include "pbd/error.h"
43 #include "pbd/stacktrace.h"
45 #include "gtkmm2ext/rgb_macros.h"
46 #include "gtkmm2ext/gtk_ui.h"
48 #include "ardour/filesystem_paths.h"
50 #include "ardour_ui.h"
51 #include "ui_config.h"
57 using namespace ARDOUR;
58 using namespace ArdourCanvas;
60 static const char* ui_config_file_name = "ui_config";
61 static const char* default_ui_config_file_name = "default_ui_config";
62 UIConfiguration* UIConfiguration::_instance = 0;
64 static const double hue_width = 18.0;
66 sigc::signal<void> UIConfiguration::ColorsChanged;
68 sigc::signal<void> UIConfiguration::DPIReset;
70 UIConfiguration::UIConfiguration ()
72 #undef UI_CONFIG_VARIABLE
73 #define UI_CONFIG_VARIABLE(Type,var,name,val) var (name,val),
74 #define CANVAS_FONT_VARIABLE(var,name) var (name),
75 #include "ui_config_vars.h"
76 #include "canvas_vars.h"
77 #undef UI_CONFIG_VARIABLE
78 #undef CANVAS_FONT_VARIABLE
81 aliases_modified (false),
82 colors_modified (false),
83 modifiers_modified (false),
90 ColorsChanged.connect (boost::bind (&UIConfiguration::colors_changed, this));
92 ParameterChanged.connect (sigc::mem_fun (*this, &UIConfiguration::parameter_changed));
95 UIConfiguration::~UIConfiguration ()
100 UIConfiguration::colors_changed ()
104 /* In theory, one of these ought to work:
106 gtk_rc_reparse_all_for_settings (gtk_settings_get_default(), true);
107 gtk_rc_reset_styles (gtk_settings_get_default());
109 but in practice, neither of them do. So just reload the current
110 GTK RC file, which causes a reset of all styles and a redraw
113 parameter_changed ("ui-rc-file");
117 UIConfiguration::parameter_changed (string param)
121 if (param == "ui-rc-file") {
123 } else if (param == "color-file") {
131 UIConfiguration::reset_gtk_theme ()
135 ss << "gtk_color_scheme = \"" << hex;
137 for (ColorAliases::iterator g = color_aliases.begin(); g != color_aliases.end(); ++g) {
139 if (g->first.find ("gtk_") == 0) {
140 const string gtk_name = g->first.substr (4);
141 ss << gtk_name << ":#" << std::setw (6) << setfill ('0') << (color (g->second) >> 8) << ';';
145 ss << '"' << dec << endl;
147 /* reset GTK color scheme */
149 Gtk::Settings::get_default()->property_gtk_color_scheme() = ss.str();
153 UIConfiguration::reset_dpi ()
155 long val = get_font_scale();
156 set_pango_fontsize ();
159 gtk_settings_set_long_property (gtk_settings_get_default(),
160 "gtk-xft-dpi", val, "ardour");
161 DPIReset(); //Emit Signal
165 UIConfiguration::set_pango_fontsize ()
167 long val = get_font_scale();
169 /* FT2 rendering - used by GnomeCanvas, sigh */
171 #ifndef PLATFORM_WINDOWS
172 pango_ft2_font_map_set_resolution ((PangoFT2FontMap*) pango_ft2_font_map_new(), val/1024, val/1024);
175 /* Cairo rendering, in case there is any */
177 pango_cairo_font_map_set_resolution ((PangoCairoFontMap*) pango_cairo_font_map_get_default(), val/1024);
181 UIConfiguration::map_parameters (boost::function<void (std::string)>& functor)
183 #undef UI_CONFIG_VARIABLE
184 #define UI_CONFIG_VARIABLE(Type,var,Name,value) functor (Name);
185 #include "ui_config_vars.h"
186 #undef UI_CONFIG_VARIABLE
190 UIConfiguration::pre_gui_init ()
192 #ifdef CAIRO_SUPPORTS_FORCE_BUGGY_GRADIENTS_ENVIRONMENT_VARIABLE
193 if (get_buggy_gradients()) {
194 g_setenv ("FORCE_BUGGY_GRADIENTS", "1", 1);
197 #ifdef OPTIONAL_CAIRO_IMAGE_SURFACE
198 if (get_cairo_image_surface()) {
199 g_setenv ("ARDOUR_IMAGE_SURFACE", "1", 1);
206 UIConfiguration::post_gui_init ()
213 UIConfiguration::load_defaults ()
218 if (find_file (ardour_config_search_path(), default_ui_config_file_name, rcfile) ) {
221 info << string_compose (_("Loading default ui configuration file %1"), rcfile) << endmsg;
223 if (!tree.read (rcfile.c_str())) {
224 error << string_compose(_("cannot read default ui configuration file \"%1\""), rcfile) << endmsg;
226 if (set_state (*tree.root(), Stateful::loading_state_version)) {
227 error << string_compose(_("default ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
235 warning << string_compose (_("Could not find default UI configuration file %1"), default_ui_config_file_name) << endmsg;
240 /* reload color theme */
241 load_color_theme (false);
242 ColorsChanged (); /* EMIT SIGNAL */
249 UIConfiguration::load_color_theme (bool allow_own)
257 basename += color_file.get();
258 basename += ".colors";
260 if (find_file (ardour_config_search_path(), basename, cfile)) {
266 basename = color_file.get();
267 basename += ".colors";
269 if (find_file (ardour_config_search_path(), basename, cfile)) {
278 info << string_compose (_("Loading color file %1"), cfile) << endmsg;
280 if (!tree.read (cfile.c_str())) {
281 error << string_compose(_("cannot read color file \"%1\""), cfile) << endmsg;
285 if (set_state (*tree.root(), Stateful::loading_state_version)) {
286 error << string_compose(_("color file \"%1\" not loaded successfully."), cfile) << endmsg;
292 warning << string_compose (_("Color file %1 not found"), basename) << endmsg;
299 UIConfiguration::store_color_theme ()
302 LocaleGuard lg (X_("C"));
304 root = new XMLNode("Ardour");
306 XMLNode* parent = new XMLNode (X_("Colors"));
307 for (Colors::const_iterator i = colors.begin(); i != colors.end(); ++i) {
308 XMLNode* node = new XMLNode (X_("Color"));
309 node->add_property (X_("name"), i->first);
311 ss << "0x" << setw (8) << setfill ('0') << hex << i->second;
312 node->add_property (X_("value"), ss.str());
313 parent->add_child_nocopy (*node);
315 root->add_child_nocopy (*parent);
317 parent = new XMLNode (X_("ColorAliases"));
318 for (ColorAliases::const_iterator i = color_aliases.begin(); i != color_aliases.end(); ++i) {
319 XMLNode* node = new XMLNode (X_("ColorAlias"));
320 node->add_property (X_("name"), i->first);
321 node->add_property (X_("alias"), i->second);
322 parent->add_child_nocopy (*node);
324 root->add_child_nocopy (*parent);
326 parent = new XMLNode (X_("Modifiers"));
327 for (Modifiers::const_iterator i = modifiers.begin(); i != modifiers.end(); ++i) {
328 XMLNode* node = new XMLNode (X_("Modifier"));
329 node->add_property (X_("name"), i->first);
330 node->add_property (X_("modifier"), i->second.to_string());
331 parent->add_child_nocopy (*node);
333 root->add_child_nocopy (*parent);
336 std::string colorfile = Glib::build_filename (user_config_directory(), (string ("my-") + color_file.get() + ".colors"));
338 tree.set_root (root);
340 if (!tree.write (colorfile.c_str())){
341 error << string_compose (_("Color file %1 not saved"), colorfile) << endmsg;
349 UIConfiguration::load_state ()
355 if (find_file (ardour_config_search_path(), default_ui_config_file_name, rcfile)) {
359 info << string_compose (_("Loading default ui configuration file %1"), rcfile) << endmsg;
361 if (!tree.read (rcfile.c_str())) {
362 error << string_compose(_("cannot read default ui configuration file \"%1\""), rcfile) << endmsg;
366 if (set_state (*tree.root(), Stateful::loading_state_version)) {
367 error << string_compose(_("default ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
372 if (find_file (ardour_config_search_path(), ui_config_file_name, rcfile)) {
376 info << string_compose (_("Loading user ui configuration file %1"), rcfile) << endmsg;
378 if (!tree.read (rcfile)) {
379 error << string_compose(_("cannot read ui configuration file \"%1\""), rcfile) << endmsg;
383 if (set_state (*tree.root(), Stateful::loading_state_version)) {
384 error << string_compose(_("user ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
392 error << _("could not find any ui configuration file, canvas will look broken.") << endmsg;
399 UIConfiguration::save_state()
403 std::string rcfile = Glib::build_filename (user_config_directory(), ui_config_file_name);
407 tree.set_root (&get_state());
409 if (!tree.write (rcfile.c_str())){
410 error << string_compose (_("Config file %1 not saved"), rcfile) << endmsg;
417 if (aliases_modified || colors_modified || modifiers_modified) {
419 if (store_color_theme ()) {
420 error << string_compose (_("Color file %1 not saved"), color_file.get()) << endmsg;
424 aliases_modified = false;
425 colors_modified = false;
426 modifiers_modified = false;
434 UIConfiguration::get_state ()
437 LocaleGuard lg (X_("C"));
439 root = new XMLNode("Ardour");
441 root->add_child_nocopy (get_variables ("UI"));
442 root->add_child_nocopy (get_variables ("Canvas"));
445 root->add_child_copy (*_extra_xml);
452 UIConfiguration::get_variables (std::string which_node)
455 LocaleGuard lg (X_("C"));
457 node = new XMLNode (which_node);
459 #undef UI_CONFIG_VARIABLE
460 #undef CANVAS_FONT_VARIABLE
461 #define UI_CONFIG_VARIABLE(Type,var,Name,value) if (node->name() == "UI") { var.add_to_node (*node); }
462 #define CANVAS_FONT_VARIABLE(var,Name) if (node->name() == "Canvas") { var.add_to_node (*node); }
463 #include "ui_config_vars.h"
464 #include "canvas_vars.h"
465 #undef UI_CONFIG_VARIABLE
466 #undef CANVAS_FONT_VARIABLE
472 UIConfiguration::set_state (const XMLNode& root, int /*version*/)
474 /* this can load a generic UI configuration file or a colors file */
476 if (root.name() != "Ardour") {
480 Stateful::save_extra_xml (root);
482 XMLNodeList nlist = root.children();
483 XMLNodeConstIterator niter;
486 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
490 if (node->name() == "Canvas" || node->name() == "UI") {
491 set_variables (*node);
496 XMLNode* colors = find_named_node (root, X_("Colors"));
499 load_colors (*colors);
502 XMLNode* aliases = find_named_node (root, X_("ColorAliases"));
505 load_color_aliases (*aliases);
508 XMLNode* modifiers = find_named_node (root, X_("Modifiers"));
511 load_modifiers (*modifiers);
518 UIConfiguration::load_color_aliases (XMLNode const & node)
520 XMLNodeList const nlist = node.children();
521 XMLNodeConstIterator niter;
522 XMLProperty const *name;
523 XMLProperty const *alias;
525 color_aliases.clear ();
527 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
528 if ((*niter)->name() != X_("ColorAlias")) {
531 name = (*niter)->property (X_("name"));
532 alias = (*niter)->property (X_("alias"));
535 color_aliases.insert (make_pair (name->value(), alias->value()));
541 UIConfiguration::load_colors (XMLNode const & node)
543 XMLNodeList const nlist = node.children();
544 XMLNodeConstIterator niter;
545 XMLProperty const *name;
546 XMLProperty const *color;
550 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
551 if ((*niter)->name() != X_("Color")) {
554 name = (*niter)->property (X_("name"));
555 color = (*niter)->property (X_("value"));
558 ArdourCanvas::Color c;
559 c = strtoul (color->value().c_str(), 0, 16);
560 colors.insert (make_pair (name->value(), c));
566 UIConfiguration::load_modifiers (XMLNode const & node)
568 PBD::LocaleGuard lg ("C");
569 XMLNodeList const nlist = node.children();
570 XMLNodeConstIterator niter;
571 XMLProperty const *name;
572 XMLProperty const *mod;
576 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
577 if ((*niter)->name() != X_("Modifier")) {
581 name = (*niter)->property (X_("name"));
582 mod = (*niter)->property (X_("modifier"));
585 SVAModifier svam (mod->value());
586 modifiers.insert (make_pair (name->value(), svam));
592 UIConfiguration::set_variables (const XMLNode& node)
594 #undef UI_CONFIG_VARIABLE
595 #define UI_CONFIG_VARIABLE(Type,var,name,val) if (var.set_from_node (node)) { ParameterChanged (name); }
596 #define CANVAS_FONT_VARIABLE(var,name) if (var.set_from_node (node)) { ParameterChanged (name); }
597 #include "ui_config_vars.h"
598 #include "canvas_vars.h"
599 #undef UI_CONFIG_VARIABLE
600 #undef CANVAS_FONT_VARIABLE
603 ArdourCanvas::SVAModifier
604 UIConfiguration::modifier (string const & name) const
606 Modifiers::const_iterator m = modifiers.find (name);
607 if (m != modifiers.end()) {
610 return SVAModifier ();
614 UIConfiguration::color_mod (std::string const & colorname, std::string const & modifiername) const
616 return HSV (color (colorname)).mod (modifier (modifiername)).color ();
620 UIConfiguration::color_mod (const ArdourCanvas::Color& color, std::string const & modifiername) const
622 return HSV (color).mod (modifier (modifiername)).color ();
626 UIConfiguration::color (const std::string& name, bool* failed) const
628 ColorAliases::const_iterator e = color_aliases.find (name);
634 if (e != color_aliases.end ()) {
635 Colors::const_iterator rc = colors.find (e->second);
636 if (rc != colors.end()) {
640 /* not an alias, try directly */
641 Colors::const_iterator rc = colors.find (name);
642 if (rc != colors.end()) {
648 /* only show this message if the caller wasn't interested in
651 cerr << string_compose (_("Color %1 not found"), name) << endl;
658 return rgba_to_color ((g_random_int()%256)/255.0,
659 (g_random_int()%256)/255.0,
660 (g_random_int()%256)/255.0,
665 UIConfiguration::quantized (Color c) const
668 hsv.h = hue_width * (round (hsv.h/hue_width));
673 UIConfiguration::set_color (string const& name, ArdourCanvas::Color color)
675 Colors::iterator i = colors.find (name);
676 if (i == colors.end()) {
680 colors_modified = true;
682 ColorsChanged (); /* EMIT SIGNAL */
686 UIConfiguration::set_alias (string const & name, string const & alias)
688 ColorAliases::iterator i = color_aliases.find (name);
689 if (i == color_aliases.end()) {
694 aliases_modified = true;
696 ColorsChanged (); /* EMIT SIGNAL */
700 UIConfiguration::set_modifier (string const & name, SVAModifier svam)
702 Modifiers::iterator m = modifiers.find (name);
704 if (m == modifiers.end()) {
709 modifiers_modified = true;
711 ColorsChanged (); /* EMIT SIGNAL */
715 UIConfiguration::load_rc_file (bool themechange, bool allow_own)
717 string basename = ui_rc_file.get();
718 std::string rc_file_path;
720 if (!find_file (ardour_config_search_path(), basename, rc_file_path)) {
721 warning << string_compose (_("Unable to find UI style file %1 in search path %2. %3 will look strange"),
722 basename, ardour_config_search_path().to_string(), PROGRAM_NAME)
727 info << "Loading ui configuration file " << rc_file_path << endmsg;
729 Gtkmm2ext::UI::instance()->load_rcfile (rc_file_path, themechange);