Choose some non-overlapping defaults for User Interaction modifier keys.
[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
37 #include <gtkmm/settings.h>
38
39 #include "pbd/convert.h"
40 #include "pbd/error.h"
41 #include "pbd/failed_constructor.h"
42 #include "pbd/file_utils.h"
43 #include "pbd/gstdio_compat.h"
44 #include "pbd/locale_guard.h"
45 #include "pbd/unwind.h"
46 #include "pbd/xml++.h"
47
48 #include "ardour/filesystem_paths.h"
49 #include "ardour/search_paths.h"
50 #include "ardour/revision.h"
51 #include "ardour/utils.h"
52
53 #include "gtkmm2ext/rgb_macros.h"
54 #include "gtkmm2ext/gtk_ui.h"
55
56 #include "ui_config.h"
57
58 #include "pbd/i18n.h"
59
60 using namespace std;
61 using namespace PBD;
62 using namespace ARDOUR;
63 using namespace ArdourCanvas;
64
65 static const char* ui_config_file_name = "ui_config";
66 static const char* default_ui_config_file_name = "default_ui_config";
67
68 static const double hue_width = 18.0;
69 std::string UIConfiguration::color_file_suffix = X_(".colors");
70
71 UIConfiguration&
72 UIConfiguration::instance ()
73 {
74         static UIConfiguration s_instance;
75         return s_instance;
76 }
77
78 UIConfiguration::UIConfiguration ()
79         :
80 #undef  UI_CONFIG_VARIABLE
81 #define UI_CONFIG_VARIABLE(Type,var,name,val) var (name,val),
82 #define CANVAS_FONT_VARIABLE(var,name) var (name),
83 #include "ui_config_vars.h"
84 #include "canvas_vars.h"
85 #undef  UI_CONFIG_VARIABLE
86 #undef  CANVAS_FONT_VARIABLE
87
88         _dirty (false),
89         aliases_modified (false),
90         colors_modified (false),
91         modifiers_modified (false),
92         block_save (0)
93 {
94         load_state();
95
96         ColorsChanged.connect (boost::bind (&UIConfiguration::colors_changed, this));
97
98         ParameterChanged.connect (sigc::mem_fun (*this, &UIConfiguration::parameter_changed));
99 }
100
101 UIConfiguration::~UIConfiguration ()
102 {
103 }
104
105 void
106 UIConfiguration::colors_changed ()
107 {
108         reset_gtk_theme ();
109
110         /* In theory, one of these ought to work:
111
112            gtk_rc_reparse_all_for_settings (gtk_settings_get_default(), true);
113            gtk_rc_reset_styles (gtk_settings_get_default());
114
115            but in practice, neither of them do. So just reload the current
116            GTK RC file, which causes a reset of all styles and a redraw
117         */
118
119         parameter_changed ("ui-rc-file");
120 }
121
122 void
123 UIConfiguration::parameter_changed (string param)
124 {
125         _dirty = true;
126
127         if (param == "ui-rc-file") {
128                 load_rc_file (true);
129         } else if (param == "color-file") {
130                 load_color_theme (true);
131         }
132
133         save_state ();
134 }
135
136 void
137 UIConfiguration::reset_gtk_theme ()
138 {
139         LocaleGuard lg;
140         stringstream ss;
141
142         ss << "gtk_color_scheme = \"" << hex;
143
144         for (ColorAliases::iterator g = color_aliases.begin(); g != color_aliases.end(); ++g) {
145
146                 if (g->first.find ("gtk_") == 0) {
147                         const string gtk_name = g->first.substr (4);
148                         ss << gtk_name << ":#" << std::setw (6) << setfill ('0') << (color (g->second) >> 8) << ';';
149                 }
150         }
151
152         ss << '"' << dec << endl;
153
154         /* reset GTK color scheme */
155
156         Gtk::Settings::get_default()->property_gtk_color_scheme() = ss.str();
157 }
158
159 void
160 UIConfiguration::reset_dpi ()
161 {
162         long val = get_font_scale();
163         set_pango_fontsize ();
164         /* Xft rendering */
165
166         gtk_settings_set_long_property (gtk_settings_get_default(),
167                                         "gtk-xft-dpi", val, "ardour");
168         DPIReset(); //Emit Signal
169 }
170
171 void
172 UIConfiguration::set_pango_fontsize ()
173 {
174         long val = get_font_scale();
175
176         /* FT2 rendering - used by GnomeCanvas, sigh */
177
178 #ifndef PLATFORM_WINDOWS
179         pango_ft2_font_map_set_resolution ((PangoFT2FontMap*) pango_ft2_font_map_new(), val/1024, val/1024);
180 #endif
181
182         /* Cairo rendering, in case there is any */
183
184         pango_cairo_font_map_set_resolution ((PangoCairoFontMap*) pango_cairo_font_map_get_default(), val/1024);
185 }
186
187 float
188 UIConfiguration::get_ui_scale ()
189 {
190         return get_font_scale () / 102400.;
191 }
192
193 void
194 UIConfiguration::map_parameters (boost::function<void (std::string)>& functor)
195 {
196 #undef  UI_CONFIG_VARIABLE
197 #define UI_CONFIG_VARIABLE(Type,var,Name,value) functor (Name);
198 #include "ui_config_vars.h"
199 #undef  UI_CONFIG_VARIABLE
200 }
201
202 int
203 UIConfiguration::pre_gui_init ()
204 {
205 #ifdef CAIRO_SUPPORTS_FORCE_BUGGY_GRADIENTS_ENVIRONMENT_VARIABLE
206         if (get_buggy_gradients()) {
207                 g_setenv ("FORCE_BUGGY_GRADIENTS", "1", 1);
208         }
209 #endif
210 #ifdef OPTIONAL_CAIRO_IMAGE_SURFACE
211         if (get_cairo_image_surface()) {
212                 g_setenv ("ARDOUR_IMAGE_SURFACE", "1", 1);
213         }
214 #endif
215         return 0;
216 }
217
218 UIConfiguration*
219 UIConfiguration::post_gui_init ()
220 {
221         load_color_theme (true);
222         return this;
223 }
224
225 int
226 UIConfiguration::load_defaults ()
227 {
228         std::string rcfile;
229         int ret = -1;
230
231         if (find_file (ardour_config_search_path(), default_ui_config_file_name, rcfile) ) {
232                 XMLTree tree;
233
234                 info << string_compose (_("Loading default ui configuration file %1"), rcfile) << endmsg;
235
236                 if (!tree.read (rcfile.c_str())) {
237                         error << string_compose(_("cannot read default ui configuration file \"%1\""), rcfile) << endmsg;
238                 } else {
239                         if (set_state (*tree.root(), Stateful::loading_state_version)) {
240                                 error << string_compose(_("default ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
241                         } else {
242                                 _dirty = false;
243                                 ret = 0;
244                         }
245                 }
246
247         } else {
248                 warning << string_compose (_("Could not find default UI configuration file %1"), default_ui_config_file_name) << endmsg;
249         }
250
251
252         if (ret == 0) {
253                 /* reload color theme */
254                 load_color_theme (false);
255         }
256
257         return ret;
258 }
259
260 std::string
261 UIConfiguration::color_file_name (bool use_my, bool with_version) const
262 {
263         string basename;
264
265         if (use_my) {
266                 basename += "my-";
267         }
268
269         //this is the overall theme file, e.g. "dark" plus "-downcase(PROGRAM_NAME)"
270         basename += color_file.get();  
271
272         std::string rev (revision);
273         std::size_t pos = rev.find_first_of("-");
274
275         if (with_version && pos != string::npos && pos > 0) {
276                 basename += "-";
277                 basename += rev.substr (0, pos); // COLORFILE_VERSION - program major.minor
278         }
279
280         basename += color_file_suffix;
281         return basename;
282 }
283
284 int
285 UIConfiguration::load_color_file (string const & path)
286 {
287         XMLTree tree;
288
289         info << string_compose (_("Loading color file %1"), path) << endmsg;
290
291         if (!tree.read (path.c_str())) {
292                 error << string_compose(_("cannot read color file \"%1\""), path) << endmsg;
293                 return -1;
294         }
295
296         if (set_state (*tree.root(), Stateful::loading_state_version)) {
297                 error << string_compose(_("color file \"%1\" not loaded successfully."), path) << endmsg;
298                 return -1;
299         }
300
301         return 0;
302 }
303
304 int
305 UIConfiguration::load_color_theme (bool allow_own)
306 {
307         std::string cfile;
308         bool found = false;
309         /* ColorsChanged() will trigger a  parameter_changed () which
310          * in turn calls save_state()
311          */
312         PBD::Unwinder<uint32_t> uw (block_save, block_save + 1);
313
314         if (find_file (theme_search_path(), color_file_name (false, true), cfile)) {
315                 found = true;
316         }
317
318         if (!found) {
319                 if (find_file (theme_search_path(), color_file_name (false, false), cfile)) {
320                         found = true;
321                 }
322         }
323
324         if (!found) {
325                 warning << string_compose (_("Color file for %1 not found along %2"), color_file.get(), theme_search_path().to_string()) << endmsg;
326                 return -1;
327         }
328
329         (void) load_color_file (cfile);
330
331         if (allow_own) {
332
333                 found = false;
334
335                 PBD::Searchpath sp (user_config_directory());
336
337                 /* user's own color files never have the program name in them */
338
339                 if (find_file (sp, color_file_name (true, true), cfile)) {
340                         found = true;
341                 }
342
343                 if (!found) {
344                         if (find_file (sp, color_file_name (true, false), cfile)) {
345                                 found = true;
346                         }
347                 }
348
349                 if (found) {
350                         (void) load_color_file (cfile);
351                 }
352
353         }
354
355         ColorsChanged ();
356
357         return 0;
358 }
359
360 int
361 UIConfiguration::store_color_theme ()
362 {
363         XMLNode* root;
364         LocaleGuard lg;
365
366         root = new XMLNode("Ardour");
367
368         XMLNode* parent = new XMLNode (X_("Colors"));
369         for (Colors::const_iterator i = colors.begin(); i != colors.end(); ++i) {
370                 XMLNode* node = new XMLNode (X_("Color"));
371                 node->add_property (X_("name"), i->first);
372                 stringstream ss;
373                 ss << "0x" << setw (8) << setfill ('0') << hex << i->second;
374                 node->add_property (X_("value"), ss.str());
375                 parent->add_child_nocopy (*node);
376         }
377         root->add_child_nocopy (*parent);
378
379         parent = new XMLNode (X_("ColorAliases"));
380         for (ColorAliases::const_iterator i = color_aliases.begin(); i != color_aliases.end(); ++i) {
381                 XMLNode* node = new XMLNode (X_("ColorAlias"));
382                 node->add_property (X_("name"), i->first);
383                 node->add_property (X_("alias"), i->second);
384                 parent->add_child_nocopy (*node);
385         }
386         root->add_child_nocopy (*parent);
387
388         parent = new XMLNode (X_("Modifiers"));
389         for (Modifiers::const_iterator i = modifiers.begin(); i != modifiers.end(); ++i) {
390                 XMLNode* node = new XMLNode (X_("Modifier"));
391                 node->add_property (X_("name"), i->first);
392                 node->add_property (X_("modifier"), i->second.to_string());
393                 parent->add_child_nocopy (*node);
394         }
395         root->add_child_nocopy (*parent);
396
397         XMLTree tree;
398         std::string colorfile = Glib::build_filename (user_config_directory(), color_file_name (true, true));;
399
400         tree.set_root (root);
401
402         if (!tree.write (colorfile.c_str())){
403                 error << string_compose (_("Color file %1 not saved"), colorfile) << endmsg;
404                 return -1;
405         }
406
407         return 0;
408 }
409
410 int
411 UIConfiguration::load_state ()
412 {
413         LocaleGuard lg; // a single guard for all 3 configs
414         bool found = false;
415
416         std::string rcfile;
417
418         if (find_file (ardour_config_search_path(), default_ui_config_file_name, rcfile)) {
419                 XMLTree tree;
420                 found = true;
421
422                 info << string_compose (_("Loading default ui configuration file %1"), rcfile) << endmsg;
423
424                 if (!tree.read (rcfile.c_str())) {
425                         error << string_compose(_("cannot read default ui configuration file \"%1\""), rcfile) << endmsg;
426                         return -1;
427                 }
428
429                 if (set_state (*tree.root(), Stateful::loading_state_version)) {
430                         error << string_compose(_("default ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
431                         return -1;
432                 }
433         }
434
435         if (find_file (ardour_config_search_path(), ui_config_file_name, rcfile)) {
436                 XMLTree tree;
437                 found = true;
438
439                 info << string_compose (_("Loading user ui configuration file %1"), rcfile) << endmsg;
440
441                 if (!tree.read (rcfile)) {
442                         error << string_compose(_("cannot read ui configuration file \"%1\""), rcfile) << endmsg;
443                         return -1;
444                 }
445
446                 if (set_state (*tree.root(), Stateful::loading_state_version)) {
447                         error << string_compose(_("user ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
448                         return -1;
449                 }
450
451                 _dirty = false;
452         }
453
454         if (!found) {
455                 error << _("could not find any ui configuration file, canvas will look broken.") << endmsg;
456         }
457
458         return 0;
459 }
460
461 int
462 UIConfiguration::save_state()
463 {
464         if (block_save != 0) {
465                 return -1;
466         }
467
468         if (_dirty) {
469                 std::string rcfile = Glib::build_filename (user_config_directory(), ui_config_file_name);
470
471                 XMLTree tree;
472
473                 tree.set_root (&get_state());
474
475                 if (!tree.write (rcfile.c_str())){
476                         error << string_compose (_("Config file %1 not saved"), rcfile) << endmsg;
477                         return -1;
478                 }
479
480                 _dirty = false;
481         }
482
483         if (aliases_modified || colors_modified || modifiers_modified) {
484
485                 if (store_color_theme ()) {
486                         error << string_compose (_("Color file %1 not saved"), color_file.get()) << endmsg;
487                         return -1;
488                 }
489
490                 aliases_modified = false;
491                 colors_modified = false;
492                 modifiers_modified = false;
493         }
494
495
496         return 0;
497 }
498
499 XMLNode&
500 UIConfiguration::get_state ()
501 {
502         XMLNode* root;
503         LocaleGuard lg;
504
505         root = new XMLNode("Ardour");
506
507         root->add_child_nocopy (get_variables ("UI"));
508         root->add_child_nocopy (get_variables ("Canvas"));
509
510         if (_extra_xml) {
511                 root->add_child_copy (*_extra_xml);
512         }
513
514         return *root;
515 }
516
517 XMLNode&
518 UIConfiguration::get_variables (std::string which_node)
519 {
520         XMLNode* node;
521         LocaleGuard lg;
522
523         node = new XMLNode (which_node);
524
525 #undef  UI_CONFIG_VARIABLE
526 #undef  CANVAS_FONT_VARIABLE
527 #define UI_CONFIG_VARIABLE(Type,var,Name,value) if (node->name() == "UI") { var.add_to_node (*node); }
528 #define CANVAS_FONT_VARIABLE(var,Name) if (node->name() == "Canvas") { var.add_to_node (*node); }
529 #include "ui_config_vars.h"
530 #include "canvas_vars.h"
531 #undef  UI_CONFIG_VARIABLE
532 #undef  CANVAS_FONT_VARIABLE
533
534         return *node;
535 }
536
537 int
538 UIConfiguration::set_state (const XMLNode& root, int /*version*/)
539 {
540         LocaleGuard lg;
541         /* this can load a generic UI configuration file or a colors file */
542
543         if (root.name() != "Ardour") {
544                 return -1;
545         }
546
547         Stateful::save_extra_xml (root);
548
549         XMLNodeList nlist = root.children();
550         XMLNodeConstIterator niter;
551         XMLNode *node;
552
553         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
554
555                 node = *niter;
556
557                 if (node->name() == "Canvas" ||  node->name() == "UI") {
558                         set_variables (*node);
559
560                 }
561         }
562
563         XMLNode* colors = find_named_node (root, X_("Colors"));
564
565         if (colors) {
566                 load_colors (*colors);
567         }
568
569         XMLNode* aliases = find_named_node (root, X_("ColorAliases"));
570
571         if (aliases) {
572                 load_color_aliases (*aliases);
573         }
574
575         XMLNode* modifiers = find_named_node (root, X_("Modifiers"));
576
577         if (modifiers) {
578                 load_modifiers (*modifiers);
579         }
580
581         return 0;
582 }
583
584 void
585 UIConfiguration::load_color_aliases (XMLNode const & node)
586 {
587         XMLNodeList const nlist = node.children();
588         XMLNodeConstIterator niter;
589         XMLProperty const *name;
590         XMLProperty const *alias;
591
592         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
593                 XMLNode const * child = *niter;
594                 if (child->name() != X_("ColorAlias")) {
595                         continue;
596                 }
597                 name = child->property (X_("name"));
598                 alias = child->property (X_("alias"));
599
600                 if (name && alias) {
601                         color_aliases[name->value()] = alias->value();
602                 }
603         }
604 }
605
606 void
607 UIConfiguration::load_colors (XMLNode const & node)
608 {
609         XMLNodeList const nlist = node.children();
610         XMLNodeConstIterator niter;
611         XMLProperty const *name;
612         XMLProperty const *color;
613
614         /* don't clear colors, so that we can load > 1 color file and have
615            the subsequent ones overwrite the later ones.
616         */
617
618         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
619                 XMLNode const * child = *niter;
620                 if (child->name() != X_("Color")) {
621                         continue;
622                 }
623                 name = child->property (X_("name"));
624                 color = child->property (X_("value"));
625
626                 if (name && color) {
627                         ArdourCanvas::Color c;
628                         c = strtoul (color->value().c_str(), 0, 16);
629                         /* insert or replace color name definition */
630                         colors[name->value()] =  c;
631                 }
632         }
633 }
634
635 void
636 UIConfiguration::load_modifiers (XMLNode const & node)
637 {
638         PBD::LocaleGuard lg;
639         XMLNodeList const nlist = node.children();
640         XMLNodeConstIterator niter;
641         XMLProperty const *name;
642         XMLProperty const *mod;
643
644         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
645                 XMLNode const * child = *niter;
646                 if (child->name() != X_("Modifier")) {
647                         continue;
648                 }
649
650                 name = child->property (X_("name"));
651                 mod = child->property (X_("modifier"));
652
653                 if (name && mod) {
654                         SVAModifier svam (mod->value());
655                         modifiers[name->value()] = svam;
656                 }
657         }
658 }
659
660 void
661 UIConfiguration::set_variables (const XMLNode& node)
662 {
663 #undef  UI_CONFIG_VARIABLE
664 #define UI_CONFIG_VARIABLE(Type,var,name,val) if (var.set_from_node (node)) { ParameterChanged (name); }
665 #define CANVAS_FONT_VARIABLE(var,name)        if (var.set_from_node (node)) { ParameterChanged (name); }
666 #include "ui_config_vars.h"
667 #include "canvas_vars.h"
668 #undef  UI_CONFIG_VARIABLE
669 #undef  CANVAS_FONT_VARIABLE
670 }
671
672 ArdourCanvas::SVAModifier
673 UIConfiguration::modifier (string const & name) const
674 {
675         Modifiers::const_iterator m = modifiers.find (name);
676         if (m != modifiers.end()) {
677                 return m->second;
678         }
679         return SVAModifier ();
680 }
681
682 ArdourCanvas::Color
683 UIConfiguration::color_mod (std::string const & colorname, std::string const & modifiername) const
684 {
685         return HSV (color (colorname)).mod (modifier (modifiername)).color ();
686 }
687
688 ArdourCanvas::Color
689 UIConfiguration::color_mod (const ArdourCanvas::Color& color, std::string const & modifiername) const
690 {
691         return HSV (color).mod (modifier (modifiername)).color ();
692 }
693
694 ArdourCanvas::Color
695 UIConfiguration::color (const std::string& name, bool* failed) const
696 {
697         ColorAliases::const_iterator e = color_aliases.find (name);
698
699         if (failed) {
700                 *failed = false;
701         }
702
703         if (e != color_aliases.end ()) {
704                 Colors::const_iterator rc = colors.find (e->second);
705                 if (rc != colors.end()) {
706                         return rc->second;
707                 }
708         } else {
709                 /* not an alias, try directly */
710                 Colors::const_iterator rc = colors.find (name);
711                 if (rc != colors.end()) {
712                         return rc->second;
713                 }
714         }
715
716         if (!failed) {
717                 /* only show this message if the caller wasn't interested in
718                    the fail status.
719                 */
720                 cerr << string_compose (_("Color %1 not found"), name) << endl;
721         }
722
723         if (failed) {
724                 *failed = true;
725         }
726
727         return rgba_to_color ((g_random_int()%256)/255.0,
728                               (g_random_int()%256)/255.0,
729                               (g_random_int()%256)/255.0,
730                               0xff);
731 }
732
733 Color
734 UIConfiguration::quantized (Color c) const
735 {
736         HSV hsv (c);
737         hsv.h = hue_width * (round (hsv.h/hue_width));
738         return hsv.color ();
739 }
740
741 void
742 UIConfiguration::set_color (string const& name, ArdourCanvas::Color color)
743 {
744         Colors::iterator i = colors.find (name);
745         if (i == colors.end()) {
746                 return;
747         }
748         i->second = color;
749         colors_modified = true;
750
751         ColorsChanged (); /* EMIT SIGNAL */
752 }
753
754 void
755 UIConfiguration::set_alias (string const & name, string const & alias)
756 {
757         ColorAliases::iterator i = color_aliases.find (name);
758         if (i == color_aliases.end()) {
759                 return;
760         }
761
762         i->second = alias;
763         aliases_modified = true;
764
765         ColorsChanged (); /* EMIT SIGNAL */
766 }
767
768 void
769 UIConfiguration::set_modifier (string const & name, SVAModifier svam)
770 {
771         Modifiers::iterator m = modifiers.find (name);
772
773         if (m == modifiers.end()) {
774                 return;
775         }
776
777         m->second = svam;
778         modifiers_modified = true;
779
780         ColorsChanged (); /* EMIT SIGNAL */
781 }
782
783 void
784 UIConfiguration::load_rc_file (bool themechange, bool allow_own)
785 {
786         string basename = ui_rc_file.get();
787         std::string rc_file_path;
788
789         if (!find_file (ardour_config_search_path(), basename, rc_file_path)) {
790                 warning << string_compose (_("Unable to find UI style file %1 in search path %2. %3 will look strange"),
791                                            basename, ardour_config_search_path().to_string(), PROGRAM_NAME)
792                                 << endmsg;
793                 return;
794         }
795
796         info << "Loading ui configuration file " << rc_file_path << endmsg;
797
798         Gtkmm2ext::UI::instance()->load_rcfile (rc_file_path, themechange);
799 }