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.
24 #include <cstdio> /* for snprintf, grrr */
26 #include <glibmm/miscutils.h>
27 #include <glib/gstdio.h>
29 #include "pbd/convert.h"
30 #include "pbd/failed_constructor.h"
31 #include "pbd/xml++.h"
32 #include "pbd/file_utils.h"
33 #include "pbd/error.h"
34 #include "pbd/stacktrace.h"
36 #include "gtkmm2ext/rgb_macros.h"
37 #include "gtkmm2ext/gtk_ui.h"
39 #include "ardour/filesystem_paths.h"
41 #include "ardour_ui.h"
42 #include "global_signals.h"
43 #include "ui_config.h"
49 using namespace ARDOUR;
50 using namespace ArdourCanvas;
52 static const char* ui_config_file_name = "ui_config";
53 static const char* default_ui_config_file_name = "default_ui_config";
54 UIConfiguration* UIConfiguration::_instance = 0;
56 static const double hue_width = 18.0;
58 UIConfiguration::UIConfiguration ()
60 #undef UI_CONFIG_VARIABLE
61 #define UI_CONFIG_VARIABLE(Type,var,name,val) var (name,val),
62 #define CANVAS_FONT_VARIABLE(var,name) var (name),
63 #include "ui_config_vars.h"
64 #include "canvas_vars.h"
65 #undef UI_CONFIG_VARIABLE
66 #undef CANVAS_FONT_VARIABLE
69 aliases_modified (false),
70 colors_modified (false),
77 ARDOUR_UI_UTILS::ColorsChanged.connect (boost::bind (&UIConfiguration::colors_changed, this));
79 ParameterChanged.connect (sigc::mem_fun (*this, &UIConfiguration::parameter_changed));
81 /* force GTK theme setting, so that loading an RC file will work */
86 UIConfiguration::~UIConfiguration ()
91 UIConfiguration::colors_changed ()
95 /* In theory, one of these ought to work:
97 gtk_rc_reparse_all_for_settings (gtk_settings_get_default(), true);
98 gtk_rc_reset_styles (gtk_settings_get_default());
100 but in practice, neither of them do. So just reload the current
101 GTK RC file, which causes a reset of all styles and a redraw
104 parameter_changed ("ui-rc-file");
108 UIConfiguration::parameter_changed (string param)
112 if (param == "ui-rc-file") {
114 } else if (param == "color-file") {
122 UIConfiguration::reset_gtk_theme ()
126 ss << "gtk_color_scheme = \"" << hex;
128 for (ColorAliases::iterator g = color_aliases.begin(); g != color_aliases.end(); ++g) {
130 if (g->first.find ("gtk_") == 0) {
131 ColorAliases::const_iterator a = color_aliases.find (g->first);
132 const string gtk_name = g->first.substr (4);
133 ss << gtk_name << ":#" << std::setw (6) << setfill ('0') << (color (g->second) >> 8) << ';';
137 ss << '"' << dec << endl;
139 /* reset GTK color scheme */
141 Gtk::Settings::get_default()->property_gtk_color_scheme() = ss.str();
145 UIConfiguration::map_parameters (boost::function<void (std::string)>& functor)
147 #undef UI_CONFIG_VARIABLE
148 #define UI_CONFIG_VARIABLE(Type,var,Name,value) functor (Name);
149 #include "ui_config_vars.h"
150 #undef UI_CONFIG_VARIABLE
154 UIConfiguration::load_defaults ()
159 if (find_file (ardour_config_search_path(), default_ui_config_file_name, rcfile) ) {
162 info << string_compose (_("Loading default ui configuration file %1"), rcfile) << endmsg;
164 if (!tree.read (rcfile.c_str())) {
165 error << string_compose(_("cannot read default ui configuration file \"%1\""), rcfile) << endmsg;
167 if (set_state (*tree.root(), Stateful::loading_state_version)) {
168 error << string_compose(_("default ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
176 warning << string_compose (_("Could not find default UI configuration file %1"), default_ui_config_file_name) << endmsg;
180 /* reload color theme */
181 load_color_theme (false);
182 ARDOUR_UI_UTILS::ColorsChanged (); /* EMIT SIGNAL */
189 UIConfiguration::load_color_theme (bool allow_own)
197 basename += color_file.get();
198 basename += ".colors";
200 if (find_file (ardour_config_search_path(), basename, cfile)) {
206 basename = color_file.get();
207 basename += ".colors";
209 if (find_file (ardour_config_search_path(), basename, cfile)) {
218 info << string_compose (_("Loading color file %1"), cfile) << endmsg;
220 if (!tree.read (cfile.c_str())) {
221 error << string_compose(_("cannot read color file \"%1\""), cfile) << endmsg;
225 if (set_state (*tree.root(), Stateful::loading_state_version)) {
226 error << string_compose(_("color file \"%1\" not loaded successfully."), cfile) << endmsg;
230 ARDOUR_UI_UTILS::ColorsChanged ();
232 warning << string_compose (_("Color file %1 not found"), basename) << endmsg;
239 UIConfiguration::store_color_theme ()
242 LocaleGuard lg (X_("POSIX"));
244 root = new XMLNode("Ardour");
246 XMLNode* parent = new XMLNode (X_("Colors"));
247 for (Colors::const_iterator i = colors.begin(); i != colors.end(); ++i) {
248 XMLNode* node = new XMLNode (X_("Color"));
249 node->add_property (X_("name"), i->first);
251 ss << "0x" << setw (8) << setfill ('0') << hex << i->second;
252 node->add_property (X_("value"), ss.str());
253 parent->add_child_nocopy (*node);
255 root->add_child_nocopy (*parent);
257 parent = new XMLNode (X_("ColorAliases"));
258 for (ColorAliases::const_iterator i = color_aliases.begin(); i != color_aliases.end(); ++i) {
259 XMLNode* node = new XMLNode (X_("ColorAlias"));
260 node->add_property (X_("name"), i->first);
261 node->add_property (X_("alias"), i->second);
262 parent->add_child_nocopy (*node);
264 root->add_child_nocopy (*parent);
266 parent = new XMLNode (X_("Modifiers"));
267 for (Modifiers::const_iterator i = modifiers.begin(); i != modifiers.end(); ++i) {
268 XMLNode* node = new XMLNode (X_("Modifier"));
269 node->add_property (X_("name"), i->first);
270 node->add_property (X_("modifier"), i->second.to_string());
271 parent->add_child_nocopy (*node);
273 root->add_child_nocopy (*parent);
276 std::string colorfile = Glib::build_filename (user_config_directory(), (string ("my-") + color_file.get() + ".colors"));
278 tree.set_root (root);
280 if (!tree.write (colorfile.c_str())){
281 error << string_compose (_("Color file %1 not saved"), colorfile) << endmsg;
289 UIConfiguration::load_state ()
295 if (find_file (ardour_config_search_path(), default_ui_config_file_name, rcfile)) {
299 info << string_compose (_("Loading default ui configuration file %1"), rcfile) << endmsg;
301 if (!tree.read (rcfile.c_str())) {
302 error << string_compose(_("cannot read default ui configuration file \"%1\""), rcfile) << endmsg;
306 if (set_state (*tree.root(), Stateful::loading_state_version)) {
307 error << string_compose(_("default ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
312 if (find_file (ardour_config_search_path(), ui_config_file_name, rcfile)) {
316 info << string_compose (_("Loading user ui configuration file %1"), rcfile) << endmsg;
318 if (!tree.read (rcfile)) {
319 error << string_compose(_("cannot read ui configuration file \"%1\""), rcfile) << endmsg;
323 if (set_state (*tree.root(), Stateful::loading_state_version)) {
324 error << string_compose(_("user ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
332 error << _("could not find any ui configuration file, canvas will look broken.") << endmsg;
339 UIConfiguration::save_state()
343 std::string rcfile = Glib::build_filename (user_config_directory(), ui_config_file_name);
347 tree.set_root (&get_state());
349 if (!tree.write (rcfile.c_str())){
350 error << string_compose (_("Config file %1 not saved"), rcfile) << endmsg;
357 if (aliases_modified || colors_modified) {
359 if (store_color_theme ()) {
360 error << string_compose (_("Color file %1 not saved"), color_file.get()) << endmsg;
364 aliases_modified = false;
365 colors_modified = false;
373 UIConfiguration::get_state ()
376 LocaleGuard lg (X_("POSIX"));
378 root = new XMLNode("Ardour");
380 root->add_child_nocopy (get_variables ("UI"));
381 root->add_child_nocopy (get_variables ("Canvas"));
384 root->add_child_copy (*_extra_xml);
391 UIConfiguration::get_variables (std::string which_node)
394 LocaleGuard lg (X_("POSIX"));
396 node = new XMLNode (which_node);
398 #undef UI_CONFIG_VARIABLE
399 #undef CANVAS_FONT_VARIABLE
400 #define UI_CONFIG_VARIABLE(Type,var,Name,value) if (node->name() == "UI") { var.add_to_node (*node); }
401 #define CANVAS_FONT_VARIABLE(var,Name) if (node->name() == "Canvas") { var.add_to_node (*node); }
402 #include "ui_config_vars.h"
403 #include "canvas_vars.h"
404 #undef UI_CONFIG_VARIABLE
405 #undef CANVAS_FONT_VARIABLE
411 UIConfiguration::set_state (const XMLNode& root, int /*version*/)
413 /* this can load a generic UI configuration file or a colors file */
415 if (root.name() != "Ardour") {
419 Stateful::save_extra_xml (root);
421 XMLNodeList nlist = root.children();
422 XMLNodeConstIterator niter;
425 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
429 if (node->name() == "Canvas" || node->name() == "UI") {
430 set_variables (*node);
435 XMLNode* colors = find_named_node (root, X_("Colors"));
438 load_colors (*colors);
441 XMLNode* aliases = find_named_node (root, X_("ColorAliases"));
444 load_color_aliases (*aliases);
447 XMLNode* modifiers = find_named_node (root, X_("Modifiers"));
450 load_modifiers (*modifiers);
457 UIConfiguration::load_color_aliases (XMLNode const & node)
459 XMLNodeList const nlist = node.children();
460 XMLNodeConstIterator niter;
461 XMLProperty const *name;
462 XMLProperty const *alias;
464 color_aliases.clear ();
466 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
467 if ((*niter)->name() != X_("ColorAlias")) {
470 name = (*niter)->property (X_("name"));
471 alias = (*niter)->property (X_("alias"));
474 color_aliases.insert (make_pair (name->value(), alias->value()));
480 UIConfiguration::load_colors (XMLNode const & node)
482 XMLNodeList const nlist = node.children();
483 XMLNodeConstIterator niter;
484 XMLProperty const *name;
485 XMLProperty const *color;
489 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
490 if ((*niter)->name() != X_("Color")) {
493 name = (*niter)->property (X_("name"));
494 color = (*niter)->property (X_("value"));
497 ArdourCanvas::Color c;
498 c = strtol (color->value().c_str(), 0, 16);
499 colors.insert (make_pair (name->value(), c));
505 UIConfiguration::load_modifiers (XMLNode const & node)
507 XMLNodeList const nlist = node.children();
508 XMLNodeConstIterator niter;
509 XMLProperty const *name;
510 XMLProperty const *mod;
514 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
515 if ((*niter)->name() != X_("Modifier")) {
518 name = (*niter)->property (X_("name"));
519 mod = (*niter)->property (X_("modifier"));
522 SVAModifier svam (mod->value());
523 modifiers.insert (make_pair (name->value(), svam));
529 UIConfiguration::set_variables (const XMLNode& node)
531 #undef UI_CONFIG_VARIABLE
532 #define UI_CONFIG_VARIABLE(Type,var,name,val) if (var.set_from_node (node)) { ParameterChanged (name); }
533 #define CANVAS_FONT_VARIABLE(var,name) if (var.set_from_node (node)) { ParameterChanged (name); }
534 #include "ui_config_vars.h"
535 #include "canvas_vars.h"
536 #undef UI_CONFIG_VARIABLE
537 #undef CANVAS_FONT_VARIABLE
540 ArdourCanvas::SVAModifier
541 UIConfiguration::modifier (string const & name) const
543 Modifiers::const_iterator m = modifiers.find (name);
544 if (m != modifiers.end()) {
547 return SVAModifier ();
551 UIConfiguration::color (const std::string& name, bool* failed) const
553 ColorAliases::const_iterator e = color_aliases.find (name);
559 if (e != color_aliases.end ()) {
560 Colors::const_iterator rc = colors.find (e->second);
561 if (rc != colors.end()) {
565 /* not an alias, try directly */
566 Colors::const_iterator rc = colors.find (name);
567 if (rc != colors.end()) {
573 /* only show this message if the caller wasn't interested in
576 cerr << string_compose (_("Color %1 not found"), name) << endl;
583 return rgba_to_color ((g_random_int()%256)/255.0,
584 (g_random_int()%256)/255.0,
585 (g_random_int()%256)/255.0,
590 UIConfiguration::quantized (Color c) const
593 hsv.h = hue_width * (round (hsv.h/hue_width));
598 UIConfiguration::set_color (string const& name, ArdourCanvas::Color color)
600 Colors::iterator i = colors.find (name);
601 if (i == colors.end()) {
605 colors_modified = true;
607 ARDOUR_UI_UTILS::ColorsChanged (); /* EMIT SIGNAL */
611 UIConfiguration::set_alias (string const & name, string const & alias)
613 ColorAliases::iterator i = color_aliases.find (name);
614 if (i == color_aliases.end()) {
619 aliases_modified = true;
621 ARDOUR_UI_UTILS::ColorsChanged (); /* EMIT SIGNAL */
625 UIConfiguration::load_rc_file (bool themechange, bool allow_own)
627 string basename = ui_rc_file.get();
628 std::string rc_file_path;
630 if (!find_file (ardour_config_search_path(), basename, rc_file_path)) {
631 warning << string_compose (_("Unable to find UI style file %1 in search path %2. %3 will look strange"),
632 basename, ardour_config_search_path().to_string(), PROGRAM_NAME)
637 info << "Loading ui configuration file " << rc_file_path << endmsg;
639 Gtkmm2ext::UI::instance()->load_rcfile (rc_file_path, themechange);