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);
267 std::string colorfile = Glib::build_filename (user_config_directory(), (string ("my-") + color_file.get() + ".colors"));
269 tree.set_root (root);
271 if (!tree.write (colorfile.c_str())){
272 error << string_compose (_("Color file %1 not saved"), colorfile) << endmsg;
280 UIConfiguration::load_state ()
286 if (find_file (ardour_config_search_path(), default_ui_config_file_name, rcfile)) {
290 info << string_compose (_("Loading default ui configuration file %1"), rcfile) << endmsg;
292 if (!tree.read (rcfile.c_str())) {
293 error << string_compose(_("cannot read default ui configuration file \"%1\""), rcfile) << endmsg;
297 if (set_state (*tree.root(), Stateful::loading_state_version)) {
298 error << string_compose(_("default ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
303 if (find_file (ardour_config_search_path(), ui_config_file_name, rcfile)) {
307 info << string_compose (_("Loading user ui configuration file %1"), rcfile) << endmsg;
309 if (!tree.read (rcfile)) {
310 error << string_compose(_("cannot read ui configuration file \"%1\""), rcfile) << endmsg;
314 if (set_state (*tree.root(), Stateful::loading_state_version)) {
315 error << string_compose(_("user ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
323 error << _("could not find any ui configuration file, canvas will look broken.") << endmsg;
330 UIConfiguration::save_state()
334 std::string rcfile = Glib::build_filename (user_config_directory(), ui_config_file_name);
338 tree.set_root (&get_state());
340 if (!tree.write (rcfile.c_str())){
341 error << string_compose (_("Config file %1 not saved"), rcfile) << endmsg;
348 if (aliases_modified || colors_modified) {
350 if (store_color_theme ()) {
351 error << string_compose (_("Color file %1 not saved"), color_file.get()) << endmsg;
355 aliases_modified = false;
356 colors_modified = false;
364 UIConfiguration::get_state ()
367 LocaleGuard lg (X_("POSIX"));
369 root = new XMLNode("Ardour");
371 root->add_child_nocopy (get_variables ("UI"));
372 root->add_child_nocopy (get_variables ("Canvas"));
375 root->add_child_copy (*_extra_xml);
382 UIConfiguration::get_variables (std::string which_node)
385 LocaleGuard lg (X_("POSIX"));
387 node = new XMLNode (which_node);
389 #undef UI_CONFIG_VARIABLE
390 #undef CANVAS_FONT_VARIABLE
391 #define UI_CONFIG_VARIABLE(Type,var,Name,value) if (node->name() == "UI") { var.add_to_node (*node); }
392 #define CANVAS_FONT_VARIABLE(var,Name) if (node->name() == "Canvas") { var.add_to_node (*node); }
393 #include "ui_config_vars.h"
394 #include "canvas_vars.h"
395 #undef UI_CONFIG_VARIABLE
396 #undef CANVAS_FONT_VARIABLE
402 UIConfiguration::set_state (const XMLNode& root, int /*version*/)
404 /* this can load a generic UI configuration file or a colors file */
406 if (root.name() != "Ardour") {
410 Stateful::save_extra_xml (root);
412 XMLNodeList nlist = root.children();
413 XMLNodeConstIterator niter;
416 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
420 if (node->name() == "Canvas" || node->name() == "UI") {
421 set_variables (*node);
426 XMLNode* colors = find_named_node (root, X_("Colors"));
429 load_colors (*colors);
432 XMLNode* aliases = find_named_node (root, X_("ColorAliases"));
435 load_color_aliases (*aliases);
442 UIConfiguration::load_color_aliases (XMLNode const & node)
444 XMLNodeList const nlist = node.children();
445 XMLNodeConstIterator niter;
446 XMLProperty const *name;
447 XMLProperty const *alias;
449 color_aliases.clear ();
451 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
452 if ((*niter)->name() != X_("ColorAlias")) {
455 name = (*niter)->property (X_("name"));
456 alias = (*niter)->property (X_("alias"));
459 color_aliases.insert (make_pair (name->value(), alias->value()));
465 UIConfiguration::load_colors (XMLNode const & node)
467 XMLNodeList const nlist = node.children();
468 XMLNodeConstIterator niter;
469 XMLProperty const *name;
470 XMLProperty const *color;
474 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
475 if ((*niter)->name() != X_("Color")) {
478 name = (*niter)->property (X_("name"));
479 color = (*niter)->property (X_("value"));
482 ArdourCanvas::Color c;
483 c = strtol (color->value().c_str(), 0, 16);
484 colors.insert (make_pair (name->value(), c));
490 UIConfiguration::set_variables (const XMLNode& node)
492 #undef UI_CONFIG_VARIABLE
493 #define UI_CONFIG_VARIABLE(Type,var,name,val) if (var.set_from_node (node)) { ParameterChanged (name); }
494 #define CANVAS_FONT_VARIABLE(var,name) if (var.set_from_node (node)) { ParameterChanged (name); }
495 #include "ui_config_vars.h"
496 #include "canvas_vars.h"
497 #undef UI_CONFIG_VARIABLE
498 #undef CANVAS_FONT_VARIABLE
502 UIConfiguration::color (const std::string& name, bool* failed) const
504 ColorAliases::const_iterator e = color_aliases.find (name);
510 if (e != color_aliases.end ()) {
511 Colors::const_iterator rc = colors.find (e->second);
512 if (rc != colors.end()) {
516 /* not an alias, try directly */
517 Colors::const_iterator rc = colors.find (name);
518 if (rc != colors.end()) {
524 /* only show this message if the caller wasn't interested in
527 cerr << string_compose (_("Color %1 not found"), name) << endl;
534 return rgba_to_color ((g_random_int()%256)/255.0,
535 (g_random_int()%256)/255.0,
536 (g_random_int()%256)/255.0,
541 UIConfiguration::quantized (Color c) const
544 hsv.h = hue_width * (round (hsv.h/hue_width));
549 UIConfiguration::set_color (string const& name, ArdourCanvas::Color color)
551 Colors::iterator i = colors.find (name);
552 if (i == colors.end()) {
556 colors_modified = true;
558 ARDOUR_UI_UTILS::ColorsChanged (); /* EMIT SIGNAL */
562 UIConfiguration::set_alias (string const & name, string const & alias)
564 ColorAliases::iterator i = color_aliases.find (name);
565 if (i == color_aliases.end()) {
570 aliases_modified = true;
572 ARDOUR_UI_UTILS::ColorsChanged (); /* EMIT SIGNAL */
576 UIConfiguration::load_rc_file (bool themechange, bool allow_own)
578 string basename = ui_rc_file.get();
579 std::string rc_file_path;
581 if (!find_file (ardour_config_search_path(), basename, rc_file_path)) {
582 warning << string_compose (_("Unable to find UI style file %1 in search path %2. %3 will look strange"),
583 basename, ardour_config_search_path().to_string(), PROGRAM_NAME)
588 info << "Loading ui configuration file " << rc_file_path << endmsg;
590 Gtkmm2ext::UI::instance()->load_rcfile (rc_file_path, themechange);