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