Move DPIReset and ColorsChanged signals into UIConfiguration
[ardour.git] / gtk2_ardour / ui_config.cc
1 /*
2     Copyright (C) 1999-2014 Paul Davis
3
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.
8
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.
13
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.
17
18 */
19
20 #if !defined USE_CAIRO_IMAGE_SURFACE && !defined NDEBUG
21 #define OPTIONAL_CAIRO_IMAGE_SURFACE
22 #endif
23
24 #include <iostream>
25 #include <sstream>
26 #include <unistd.h>
27 #include <cstdlib>
28 #include <cstdio> /* for snprintf, grrr */
29
30 #include <cairo/cairo.h>
31
32 #include <pango/pangoft2.h> // for fontmap resolution control for GnomeCanvas
33 #include <pango/pangocairo.h> // for fontmap resolution control for GnomeCanvas
34
35 #include <glibmm/miscutils.h>
36 #include <glib/gstdio.h>
37
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"
44
45 #include "gtkmm2ext/rgb_macros.h"
46 #include "gtkmm2ext/gtk_ui.h"
47
48 #include "ardour/filesystem_paths.h"
49
50 #include "ardour_ui.h"
51 #include "ui_config.h"
52
53 #include "i18n.h"
54
55 using namespace std;
56 using namespace PBD;
57 using namespace ARDOUR;
58 using namespace ArdourCanvas;
59
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;
63
64 static const double hue_width = 18.0;
65
66 sigc::signal<void>  UIConfiguration::ColorsChanged;
67
68 sigc::signal<void>  UIConfiguration::DPIReset;
69
70 UIConfiguration::UIConfiguration ()
71         :
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
79
80         _dirty (false),
81         aliases_modified (false),
82         colors_modified (false),
83         modifiers_modified (false),
84         block_save (0)
85 {
86         _instance = this;
87
88         load_state();
89
90         ColorsChanged.connect (boost::bind (&UIConfiguration::colors_changed, this));
91
92         ParameterChanged.connect (sigc::mem_fun (*this, &UIConfiguration::parameter_changed));
93 }
94
95 UIConfiguration::~UIConfiguration ()
96 {
97 }
98
99 void
100 UIConfiguration::colors_changed ()
101 {
102         reset_gtk_theme ();
103
104         /* In theory, one of these ought to work:
105
106            gtk_rc_reparse_all_for_settings (gtk_settings_get_default(), true);
107            gtk_rc_reset_styles (gtk_settings_get_default());
108
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
111         */
112
113         parameter_changed ("ui-rc-file");
114 }
115
116 void
117 UIConfiguration::parameter_changed (string param)
118 {
119         _dirty = true;
120         
121         if (param == "ui-rc-file") {
122                 load_rc_file (true);
123         } else if (param == "color-file") {
124                 load_color_theme ();
125         }
126
127         save_state ();
128 }
129
130 void
131 UIConfiguration::reset_gtk_theme ()
132 {
133         stringstream ss;
134
135         ss << "gtk_color_scheme = \"" << hex;
136         
137         for (ColorAliases::iterator g = color_aliases.begin(); g != color_aliases.end(); ++g) {
138                 
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) << ';';
142                 }
143         }
144
145         ss << '"' << dec << endl;
146
147         /* reset GTK color scheme */
148
149         Gtk::Settings::get_default()->property_gtk_color_scheme() = ss.str();
150 }
151
152 void
153 UIConfiguration::reset_dpi ()
154 {
155         long val = get_font_scale();
156         set_pango_fontsize ();
157         /* Xft rendering */
158
159         gtk_settings_set_long_property (gtk_settings_get_default(),
160                                         "gtk-xft-dpi", val, "ardour");
161         DPIReset(); //Emit Signal
162 }
163
164 void
165 UIConfiguration::set_pango_fontsize ()
166 {
167         long val = get_font_scale();
168
169         /* FT2 rendering - used by GnomeCanvas, sigh */
170
171 #ifndef PLATFORM_WINDOWS
172         pango_ft2_font_map_set_resolution ((PangoFT2FontMap*) pango_ft2_font_map_new(), val/1024, val/1024);
173 #endif
174
175         /* Cairo rendering, in case there is any */
176
177         pango_cairo_font_map_set_resolution ((PangoCairoFontMap*) pango_cairo_font_map_get_default(), val/1024);
178 }
179
180 void
181 UIConfiguration::map_parameters (boost::function<void (std::string)>& functor)
182 {
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
187 }
188
189 int
190 UIConfiguration::pre_gui_init ()
191 {
192 #ifdef CAIRO_SUPPORTS_FORCE_BUGGY_GRADIENTS_ENVIRONMENT_VARIABLE
193         if (get_buggy_gradients()) {
194                 g_setenv ("FORCE_BUGGY_GRADIENTS", "1", 1);
195         }
196 #endif
197 #ifdef OPTIONAL_CAIRO_IMAGE_SURFACE
198         if (get_cairo_image_surface()) {
199                 g_setenv ("ARDOUR_IMAGE_SURFACE", "1", 1);
200         }
201 #endif
202         return 0;
203 }
204
205 UIConfiguration*
206 UIConfiguration::post_gui_init ()
207 {
208         load_color_theme ();
209         return this;
210 }
211
212 int
213 UIConfiguration::load_defaults ()
214 {
215         std::string rcfile;
216         int ret = -1;
217         
218         if (find_file (ardour_config_search_path(), default_ui_config_file_name, rcfile) ) {
219                 XMLTree tree;
220
221                 info << string_compose (_("Loading default ui configuration file %1"), rcfile) << endmsg;
222
223                 if (!tree.read (rcfile.c_str())) {
224                         error << string_compose(_("cannot read default ui configuration file \"%1\""), rcfile) << endmsg;
225                 } else {
226                         if (set_state (*tree.root(), Stateful::loading_state_version)) {
227                                 error << string_compose(_("default ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
228                         } else {
229                                 _dirty = false;
230                                 ret = 0;
231                         }
232                 }
233
234         } else {
235                 warning << string_compose (_("Could not find default UI configuration file %1"), default_ui_config_file_name) << endmsg;
236         }
237
238
239         if (ret == 0) {
240                 /* reload color theme */
241                 load_color_theme (false);
242                 ColorsChanged (); /* EMIT SIGNAL */
243         }
244
245         return ret;
246 }
247
248 int
249 UIConfiguration::load_color_theme (bool allow_own)
250 {
251         std::string cfile;
252         string basename;
253         bool found = false;
254
255         if (allow_own) {
256                 basename = "my-";
257                 basename += color_file.get();
258                 basename += ".colors";
259
260                 if (find_file (ardour_config_search_path(), basename, cfile)) {
261                         found = true;
262                 }
263         }
264
265         if (!found) {
266                 basename = color_file.get();
267                 basename += ".colors";
268         
269                 if (find_file (ardour_config_search_path(), basename, cfile)) {
270                         found = true;
271                 }
272         }
273
274         if (found) {
275
276                 XMLTree tree;
277                 
278                 info << string_compose (_("Loading color file %1"), cfile) << endmsg;
279
280                 if (!tree.read (cfile.c_str())) {
281                         error << string_compose(_("cannot read color file \"%1\""), cfile) << endmsg;
282                         return -1;
283                 }
284
285                 if (set_state (*tree.root(), Stateful::loading_state_version)) {
286                         error << string_compose(_("color file \"%1\" not loaded successfully."), cfile) << endmsg;
287                         return -1;
288                 }
289
290                 ColorsChanged ();
291         } else {
292                 warning << string_compose (_("Color file %1 not found"), basename) << endmsg;
293         }
294
295         return 0;
296 }
297
298 int
299 UIConfiguration::store_color_theme ()
300 {
301         XMLNode* root;
302         LocaleGuard lg (X_("C"));
303
304         root = new XMLNode("Ardour");
305
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);
310                 stringstream ss;
311                 ss << "0x" << setw (8) << setfill ('0') << hex << i->second;
312                 node->add_property (X_("value"), ss.str());
313                 parent->add_child_nocopy (*node);
314         }
315         root->add_child_nocopy (*parent);
316         
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);
323         }
324         root->add_child_nocopy (*parent);
325
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);
332         }
333         root->add_child_nocopy (*parent);
334
335         XMLTree tree;
336         std::string colorfile = Glib::build_filename (user_config_directory(), (string ("my-") + color_file.get() + ".colors"));
337         
338         tree.set_root (root);
339
340         if (!tree.write (colorfile.c_str())){
341                 error << string_compose (_("Color file %1 not saved"), colorfile) << endmsg;
342                 return -1;
343         }
344
345         return 0;
346 }
347
348 int
349 UIConfiguration::load_state ()
350 {
351         bool found = false;
352
353         std::string rcfile;
354
355         if (find_file (ardour_config_search_path(), default_ui_config_file_name, rcfile)) {
356                 XMLTree tree;
357                 found = true;
358
359                 info << string_compose (_("Loading default ui configuration file %1"), rcfile) << endmsg;
360
361                 if (!tree.read (rcfile.c_str())) {
362                         error << string_compose(_("cannot read default ui configuration file \"%1\""), rcfile) << endmsg;
363                         return -1;
364                 }
365
366                 if (set_state (*tree.root(), Stateful::loading_state_version)) {
367                         error << string_compose(_("default ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
368                         return -1;
369                 }
370         }
371
372         if (find_file (ardour_config_search_path(), ui_config_file_name, rcfile)) {
373                 XMLTree tree;
374                 found = true;
375
376                 info << string_compose (_("Loading user ui configuration file %1"), rcfile) << endmsg;
377
378                 if (!tree.read (rcfile)) {
379                         error << string_compose(_("cannot read ui configuration file \"%1\""), rcfile) << endmsg;
380                         return -1;
381                 }
382
383                 if (set_state (*tree.root(), Stateful::loading_state_version)) {
384                         error << string_compose(_("user ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
385                         return -1;
386                 }
387
388                 _dirty = false;
389         }
390
391         if (!found) {
392                 error << _("could not find any ui configuration file, canvas will look broken.") << endmsg;
393         }
394
395         return 0;
396 }
397
398 int
399 UIConfiguration::save_state()
400 {
401
402         if (_dirty) {
403                 std::string rcfile = Glib::build_filename (user_config_directory(), ui_config_file_name);
404                 
405                 XMLTree tree;
406
407                 tree.set_root (&get_state());
408
409                 if (!tree.write (rcfile.c_str())){
410                         error << string_compose (_("Config file %1 not saved"), rcfile) << endmsg;
411                         return -1;
412                 }
413
414                 _dirty = false;
415         }
416
417         if (aliases_modified || colors_modified || modifiers_modified) {
418
419                 if (store_color_theme ()) {
420                         error << string_compose (_("Color file %1 not saved"), color_file.get()) << endmsg;
421                         return -1;
422                 }
423
424                 aliases_modified = false;
425                 colors_modified = false;
426                 modifiers_modified = false;
427         }
428         
429
430         return 0;
431 }
432
433 XMLNode&
434 UIConfiguration::get_state ()
435 {
436         XMLNode* root;
437         LocaleGuard lg (X_("C"));
438
439         root = new XMLNode("Ardour");
440
441         root->add_child_nocopy (get_variables ("UI"));
442         root->add_child_nocopy (get_variables ("Canvas"));
443
444         if (_extra_xml) {
445                 root->add_child_copy (*_extra_xml);
446         }
447
448         return *root;
449 }
450
451 XMLNode&
452 UIConfiguration::get_variables (std::string which_node)
453 {
454         XMLNode* node;
455         LocaleGuard lg (X_("C"));
456
457         node = new XMLNode (which_node);
458
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
467
468         return *node;
469 }
470
471 int
472 UIConfiguration::set_state (const XMLNode& root, int /*version*/)
473 {
474         /* this can load a generic UI configuration file or a colors file */
475
476         if (root.name() != "Ardour") {
477                 return -1;
478         }
479
480         Stateful::save_extra_xml (root);
481
482         XMLNodeList nlist = root.children();
483         XMLNodeConstIterator niter;
484         XMLNode *node;
485
486         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
487
488                 node = *niter;
489
490                 if (node->name() == "Canvas" ||  node->name() == "UI") {
491                         set_variables (*node);
492
493                 }
494         }
495
496         XMLNode* colors = find_named_node (root, X_("Colors"));
497
498         if (colors) {
499                 load_colors (*colors);
500         }
501
502         XMLNode* aliases = find_named_node (root, X_("ColorAliases"));
503
504         if (aliases) {
505                 load_color_aliases (*aliases);
506         }
507
508         XMLNode* modifiers = find_named_node (root, X_("Modifiers"));
509
510         if (modifiers) {
511                 load_modifiers (*modifiers);
512         }
513
514         return 0;
515 }
516
517 void
518 UIConfiguration::load_color_aliases (XMLNode const & node)
519 {
520         XMLNodeList const nlist = node.children();
521         XMLNodeConstIterator niter;
522         XMLProperty const *name;
523         XMLProperty const *alias;
524         
525         color_aliases.clear ();
526
527         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
528                 if ((*niter)->name() != X_("ColorAlias")) {
529                         continue;
530                 }
531                 name = (*niter)->property (X_("name"));
532                 alias = (*niter)->property (X_("alias"));
533
534                 if (name && alias) {
535                         color_aliases.insert (make_pair (name->value(), alias->value()));
536                 }
537         }
538 }
539
540 void
541 UIConfiguration::load_colors (XMLNode const & node)
542 {
543         XMLNodeList const nlist = node.children();
544         XMLNodeConstIterator niter;
545         XMLProperty const *name;
546         XMLProperty const *color;
547         
548         colors.clear ();
549
550         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
551                 if ((*niter)->name() != X_("Color")) {
552                         continue;
553                 }
554                 name = (*niter)->property (X_("name"));
555                 color = (*niter)->property (X_("value"));
556
557                 if (name && color) {
558                         ArdourCanvas::Color c;
559                         c = strtoul (color->value().c_str(), 0, 16);
560                         colors.insert (make_pair (name->value(), c));
561                 }
562         }
563 }
564
565 void
566 UIConfiguration::load_modifiers (XMLNode const & node)
567 {
568         PBD::LocaleGuard lg ("C");
569         XMLNodeList const nlist = node.children();
570         XMLNodeConstIterator niter;
571         XMLProperty const *name;
572         XMLProperty const *mod;
573         
574         modifiers.clear ();
575         
576         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
577                 if ((*niter)->name() != X_("Modifier")) {
578                         continue;
579                 }
580
581                 name = (*niter)->property (X_("name"));
582                 mod = (*niter)->property (X_("modifier"));
583
584                 if (name && mod) {
585                         SVAModifier svam (mod->value());
586                         modifiers.insert (make_pair (name->value(), svam));
587                 }
588         }
589 }
590
591 void
592 UIConfiguration::set_variables (const XMLNode& node)
593 {
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
601 }
602
603 ArdourCanvas::SVAModifier
604 UIConfiguration::modifier (string const & name) const
605 {
606         Modifiers::const_iterator m = modifiers.find (name);
607         if (m != modifiers.end()) {
608                 return m->second;
609         }
610         return SVAModifier ();
611 }
612
613 ArdourCanvas::Color
614 UIConfiguration::color_mod (std::string const & colorname, std::string const & modifiername) const
615 {
616         return HSV (color (colorname)).mod (modifier (modifiername)).color ();
617 }
618
619 ArdourCanvas::Color
620 UIConfiguration::color_mod (const ArdourCanvas::Color& color, std::string const & modifiername) const
621 {
622         return HSV (color).mod (modifier (modifiername)).color ();
623 }
624
625 ArdourCanvas::Color
626 UIConfiguration::color (const std::string& name, bool* failed) const
627 {
628         ColorAliases::const_iterator e = color_aliases.find (name);
629
630         if (failed) {
631                 *failed = false;
632         }
633         
634         if (e != color_aliases.end ()) {
635                 Colors::const_iterator rc = colors.find (e->second);
636                 if (rc != colors.end()) {
637                         return rc->second;
638                 }
639         } else {
640                 /* not an alias, try directly */
641                 Colors::const_iterator rc = colors.find (name);
642                 if (rc != colors.end()) {
643                         return rc->second;
644                 }
645         }
646         
647         if (!failed) {
648                 /* only show this message if the caller wasn't interested in
649                    the fail status.
650                 */
651                 cerr << string_compose (_("Color %1 not found"), name) << endl;
652         }
653
654         if (failed) {
655                 *failed = true;
656         }
657         
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,
661                               0xff);
662 }
663
664 Color
665 UIConfiguration::quantized (Color c) const
666 {
667         HSV hsv (c);
668         hsv.h = hue_width * (round (hsv.h/hue_width));
669         return hsv.color ();
670 }
671
672 void
673 UIConfiguration::set_color (string const& name, ArdourCanvas::Color color)
674 {
675         Colors::iterator i = colors.find (name);
676         if (i == colors.end()) {
677                 return;
678         }
679         i->second = color;
680         colors_modified = true;
681
682         ColorsChanged (); /* EMIT SIGNAL */
683 }
684
685 void
686 UIConfiguration::set_alias (string const & name, string const & alias)
687 {
688         ColorAliases::iterator i = color_aliases.find (name);
689         if (i == color_aliases.end()) {
690                 return;
691         }
692
693         i->second = alias;
694         aliases_modified = true;
695
696         ColorsChanged (); /* EMIT SIGNAL */
697 }
698
699 void
700 UIConfiguration::set_modifier (string const & name, SVAModifier svam)
701 {
702         Modifiers::iterator m = modifiers.find (name);
703
704         if (m == modifiers.end()) {
705                 return;
706         }
707
708         m->second = svam;
709         modifiers_modified = true;
710
711         ColorsChanged (); /* EMIT SIGNAL */
712 }
713
714 void
715 UIConfiguration::load_rc_file (bool themechange, bool allow_own)
716 {
717         string basename = ui_rc_file.get();
718         std::string rc_file_path;
719
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)
723                                 << endmsg;
724                 return;
725         }
726
727         info << "Loading ui configuration file " << rc_file_path << endmsg;
728
729         Gtkmm2ext::UI::instance()->load_rcfile (rc_file_path, themechange);
730 }
731
732