enough with umpteen "i18n.h" files. Consolidate on pbd/i18n.h
[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 ();
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 ();
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_program_name, bool with_version) const
262 {
263         string basename;
264
265         if (use_my) {
266                 basename += "my-";
267         }
268
269         basename += color_file.get();  //this is the overall theme file, e.g. "dark"
270
271         if (with_program_name) {
272                 basename += '-';
273                 basename += downcase (PROGRAM_NAME);
274         }
275
276         std::string rev (revision);
277         std::size_t pos = rev.find_first_of("-");
278
279         if (with_version && pos != string::npos && pos > 0) {
280                 basename += "-";
281                 basename += rev.substr (0, pos); // COLORFILE_VERSION - program major.minor
282         }
283
284         basename += color_file_suffix;
285
286         return basename;
287 }
288
289 int
290 UIConfiguration::load_color_theme (bool allow_own)
291 {
292         std::string cfile;
293         bool found = false;
294         /* ColorsChanged() will trigger a  parameter_changed () which
295          * in turn calls save_state()
296          */
297         PBD::Unwinder<uint32_t> uw (block_save, block_save + 1);
298         const bool running_from_source = ARDOUR_UI_UTILS::running_from_source_tree ();
299
300         if (allow_own) {
301
302                 PBD::Searchpath sp (user_config_directory());
303
304                 /* user's own color files never have the program name in them */
305
306                 if (find_file (sp, color_file_name (true, false, true), cfile)) {
307                         found = true;
308                 }
309
310                 if (!found) {
311                         if (find_file (sp, color_file_name (true, false, false), cfile)) {
312                                 found = true;
313                         }
314                 }
315
316         }
317
318         if (!found) {
319                 if (find_file (theme_search_path(), color_file_name (false, running_from_source, true), cfile)) {
320                         found = true;
321                 }
322
323                 if (!found) {
324                         if (find_file (theme_search_path(), color_file_name (false, running_from_source, false), cfile)) {
325                                 found = true;
326                         }
327                 }
328         }
329
330         if (!found) {
331                 warning << string_compose (_("Color file for %1 not found along %2"), color_file.get(), theme_search_path().to_string()) << endmsg;
332                 return -1;
333         }
334
335
336         XMLTree tree;
337
338         info << string_compose (_("Loading color file %1"), cfile) << endmsg;
339
340         if (!tree.read (cfile.c_str())) {
341                 error << string_compose(_("cannot read color file \"%1\""), cfile) << endmsg;
342                 return -1;
343         }
344
345         if (set_state (*tree.root(), Stateful::loading_state_version)) {
346                 error << string_compose(_("color file \"%1\" not loaded successfully."), cfile) << endmsg;
347                 return -1;
348                 }
349
350         ColorsChanged ();
351
352         return 0;
353 }
354
355 int
356 UIConfiguration::store_color_theme ()
357 {
358         XMLNode* root;
359         LocaleGuard lg;
360
361         root = new XMLNode("Ardour");
362
363         XMLNode* parent = new XMLNode (X_("Colors"));
364         for (Colors::const_iterator i = colors.begin(); i != colors.end(); ++i) {
365                 XMLNode* node = new XMLNode (X_("Color"));
366                 node->add_property (X_("name"), i->first);
367                 stringstream ss;
368                 ss << "0x" << setw (8) << setfill ('0') << hex << i->second;
369                 node->add_property (X_("value"), ss.str());
370                 parent->add_child_nocopy (*node);
371         }
372         root->add_child_nocopy (*parent);
373
374         parent = new XMLNode (X_("ColorAliases"));
375         for (ColorAliases::const_iterator i = color_aliases.begin(); i != color_aliases.end(); ++i) {
376                 XMLNode* node = new XMLNode (X_("ColorAlias"));
377                 node->add_property (X_("name"), i->first);
378                 node->add_property (X_("alias"), i->second);
379                 parent->add_child_nocopy (*node);
380         }
381         root->add_child_nocopy (*parent);
382
383         parent = new XMLNode (X_("Modifiers"));
384         for (Modifiers::const_iterator i = modifiers.begin(); i != modifiers.end(); ++i) {
385                 XMLNode* node = new XMLNode (X_("Modifier"));
386                 node->add_property (X_("name"), i->first);
387                 node->add_property (X_("modifier"), i->second.to_string());
388                 parent->add_child_nocopy (*node);
389         }
390         root->add_child_nocopy (*parent);
391
392         XMLTree tree;
393         std::string colorfile = Glib::build_filename (user_config_directory(), color_file_name (true, false, true));;
394
395         tree.set_root (root);
396
397         if (!tree.write (colorfile.c_str())){
398                 error << string_compose (_("Color file %1 not saved"), colorfile) << endmsg;
399                 return -1;
400         }
401
402         return 0;
403 }
404
405 int
406 UIConfiguration::load_state ()
407 {
408         LocaleGuard lg; // a single guard for all 3 configs
409         bool found = false;
410
411         std::string rcfile;
412
413         if (find_file (ardour_config_search_path(), default_ui_config_file_name, rcfile)) {
414                 XMLTree tree;
415                 found = true;
416
417                 info << string_compose (_("Loading default ui configuration file %1"), rcfile) << endmsg;
418
419                 if (!tree.read (rcfile.c_str())) {
420                         error << string_compose(_("cannot read default ui configuration file \"%1\""), rcfile) << endmsg;
421                         return -1;
422                 }
423
424                 if (set_state (*tree.root(), Stateful::loading_state_version)) {
425                         error << string_compose(_("default ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
426                         return -1;
427                 }
428         }
429
430         if (find_file (ardour_config_search_path(), ui_config_file_name, rcfile)) {
431                 XMLTree tree;
432                 found = true;
433
434                 info << string_compose (_("Loading user ui configuration file %1"), rcfile) << endmsg;
435
436                 if (!tree.read (rcfile)) {
437                         error << string_compose(_("cannot read ui configuration file \"%1\""), rcfile) << endmsg;
438                         return -1;
439                 }
440
441                 if (set_state (*tree.root(), Stateful::loading_state_version)) {
442                         error << string_compose(_("user ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
443                         return -1;
444                 }
445
446                 _dirty = false;
447         }
448
449         if (!found) {
450                 error << _("could not find any ui configuration file, canvas will look broken.") << endmsg;
451         }
452
453         return 0;
454 }
455
456 int
457 UIConfiguration::save_state()
458 {
459         if (block_save != 0) {
460                 return -1;
461         }
462
463         if (_dirty) {
464                 std::string rcfile = Glib::build_filename (user_config_directory(), ui_config_file_name);
465
466                 XMLTree tree;
467
468                 tree.set_root (&get_state());
469
470                 if (!tree.write (rcfile.c_str())){
471                         error << string_compose (_("Config file %1 not saved"), rcfile) << endmsg;
472                         return -1;
473                 }
474
475                 _dirty = false;
476         }
477
478         if (aliases_modified || colors_modified || modifiers_modified) {
479
480                 if (store_color_theme ()) {
481                         error << string_compose (_("Color file %1 not saved"), color_file.get()) << endmsg;
482                         return -1;
483                 }
484
485                 aliases_modified = false;
486                 colors_modified = false;
487                 modifiers_modified = false;
488         }
489
490
491         return 0;
492 }
493
494 XMLNode&
495 UIConfiguration::get_state ()
496 {
497         XMLNode* root;
498         LocaleGuard lg;
499
500         root = new XMLNode("Ardour");
501
502         root->add_child_nocopy (get_variables ("UI"));
503         root->add_child_nocopy (get_variables ("Canvas"));
504
505         if (_extra_xml) {
506                 root->add_child_copy (*_extra_xml);
507         }
508
509         return *root;
510 }
511
512 XMLNode&
513 UIConfiguration::get_variables (std::string which_node)
514 {
515         XMLNode* node;
516         LocaleGuard lg;
517
518         node = new XMLNode (which_node);
519
520 #undef  UI_CONFIG_VARIABLE
521 #undef  CANVAS_FONT_VARIABLE
522 #define UI_CONFIG_VARIABLE(Type,var,Name,value) if (node->name() == "UI") { var.add_to_node (*node); }
523 #define CANVAS_FONT_VARIABLE(var,Name) if (node->name() == "Canvas") { var.add_to_node (*node); }
524 #include "ui_config_vars.h"
525 #include "canvas_vars.h"
526 #undef  UI_CONFIG_VARIABLE
527 #undef  CANVAS_FONT_VARIABLE
528
529         return *node;
530 }
531
532 int
533 UIConfiguration::set_state (const XMLNode& root, int /*version*/)
534 {
535         LocaleGuard lg;
536         /* this can load a generic UI configuration file or a colors file */
537
538         if (root.name() != "Ardour") {
539                 return -1;
540         }
541
542         Stateful::save_extra_xml (root);
543
544         XMLNodeList nlist = root.children();
545         XMLNodeConstIterator niter;
546         XMLNode *node;
547
548         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
549
550                 node = *niter;
551
552                 if (node->name() == "Canvas" ||  node->name() == "UI") {
553                         set_variables (*node);
554
555                 }
556         }
557
558         XMLNode* colors = find_named_node (root, X_("Colors"));
559
560         if (colors) {
561                 load_colors (*colors);
562         }
563
564         XMLNode* aliases = find_named_node (root, X_("ColorAliases"));
565
566         if (aliases) {
567                 load_color_aliases (*aliases);
568         }
569
570         XMLNode* modifiers = find_named_node (root, X_("Modifiers"));
571
572         if (modifiers) {
573                 load_modifiers (*modifiers);
574         }
575
576         return 0;
577 }
578
579 void
580 UIConfiguration::load_color_aliases (XMLNode const & node)
581 {
582         XMLNodeList const nlist = node.children();
583         XMLNodeConstIterator niter;
584         XMLProperty const *name;
585         XMLProperty const *alias;
586
587         color_aliases.clear ();
588
589         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
590                 XMLNode const * child = *niter;
591                 if (child->name() != X_("ColorAlias")) {
592                         continue;
593                 }
594                 name = child->property (X_("name"));
595                 alias = child->property (X_("alias"));
596
597                 if (name && alias) {
598                         color_aliases.insert (make_pair (name->value(), alias->value()));
599                 }
600         }
601 }
602
603 void
604 UIConfiguration::load_colors (XMLNode const & node)
605 {
606         XMLNodeList const nlist = node.children();
607         XMLNodeConstIterator niter;
608         XMLProperty const *name;
609         XMLProperty const *color;
610
611         colors.clear ();
612
613         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
614                 XMLNode const * child = *niter;
615                 if (child->name() != X_("Color")) {
616                         continue;
617                 }
618                 name = child->property (X_("name"));
619                 color = child->property (X_("value"));
620
621                 if (name && color) {
622                         ArdourCanvas::Color c;
623                         c = strtoul (color->value().c_str(), 0, 16);
624                         colors.insert (make_pair (name->value(), c));
625                 }
626         }
627 }
628
629 void
630 UIConfiguration::load_modifiers (XMLNode const & node)
631 {
632         PBD::LocaleGuard lg;
633         XMLNodeList const nlist = node.children();
634         XMLNodeConstIterator niter;
635         XMLProperty const *name;
636         XMLProperty const *mod;
637
638         modifiers.clear ();
639
640         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
641                 XMLNode const * child = *niter;
642                 if (child->name() != X_("Modifier")) {
643                         continue;
644                 }
645
646                 name = child->property (X_("name"));
647                 mod = child->property (X_("modifier"));
648
649                 if (name && mod) {
650                         SVAModifier svam (mod->value());
651                         modifiers.insert (make_pair (name->value(), svam));
652                 }
653         }
654 }
655
656 void
657 UIConfiguration::set_variables (const XMLNode& node)
658 {
659 #undef  UI_CONFIG_VARIABLE
660 #define UI_CONFIG_VARIABLE(Type,var,name,val) if (var.set_from_node (node)) { ParameterChanged (name); }
661 #define CANVAS_FONT_VARIABLE(var,name)        if (var.set_from_node (node)) { ParameterChanged (name); }
662 #include "ui_config_vars.h"
663 #include "canvas_vars.h"
664 #undef  UI_CONFIG_VARIABLE
665 #undef  CANVAS_FONT_VARIABLE
666 }
667
668 ArdourCanvas::SVAModifier
669 UIConfiguration::modifier (string const & name) const
670 {
671         Modifiers::const_iterator m = modifiers.find (name);
672         if (m != modifiers.end()) {
673                 return m->second;
674         }
675         return SVAModifier ();
676 }
677
678 ArdourCanvas::Color
679 UIConfiguration::color_mod (std::string const & colorname, std::string const & modifiername) const
680 {
681         return HSV (color (colorname)).mod (modifier (modifiername)).color ();
682 }
683
684 ArdourCanvas::Color
685 UIConfiguration::color_mod (const ArdourCanvas::Color& color, std::string const & modifiername) const
686 {
687         return HSV (color).mod (modifier (modifiername)).color ();
688 }
689
690 ArdourCanvas::Color
691 UIConfiguration::color (const std::string& name, bool* failed) const
692 {
693         ColorAliases::const_iterator e = color_aliases.find (name);
694
695         if (failed) {
696                 *failed = false;
697         }
698
699         if (e != color_aliases.end ()) {
700                 Colors::const_iterator rc = colors.find (e->second);
701                 if (rc != colors.end()) {
702                         return rc->second;
703                 }
704         } else {
705                 /* not an alias, try directly */
706                 Colors::const_iterator rc = colors.find (name);
707                 if (rc != colors.end()) {
708                         return rc->second;
709                 }
710         }
711
712         if (!failed) {
713                 /* only show this message if the caller wasn't interested in
714                    the fail status.
715                 */
716                 cerr << string_compose (_("Color %1 not found"), name) << endl;
717         }
718
719         if (failed) {
720                 *failed = true;
721         }
722
723         return rgba_to_color ((g_random_int()%256)/255.0,
724                               (g_random_int()%256)/255.0,
725                               (g_random_int()%256)/255.0,
726                               0xff);
727 }
728
729 Color
730 UIConfiguration::quantized (Color c) const
731 {
732         HSV hsv (c);
733         hsv.h = hue_width * (round (hsv.h/hue_width));
734         return hsv.color ();
735 }
736
737 void
738 UIConfiguration::set_color (string const& name, ArdourCanvas::Color color)
739 {
740         Colors::iterator i = colors.find (name);
741         if (i == colors.end()) {
742                 return;
743         }
744         i->second = color;
745         colors_modified = true;
746
747         ColorsChanged (); /* EMIT SIGNAL */
748 }
749
750 void
751 UIConfiguration::set_alias (string const & name, string const & alias)
752 {
753         ColorAliases::iterator i = color_aliases.find (name);
754         if (i == color_aliases.end()) {
755                 return;
756         }
757
758         i->second = alias;
759         aliases_modified = true;
760
761         ColorsChanged (); /* EMIT SIGNAL */
762 }
763
764 void
765 UIConfiguration::set_modifier (string const & name, SVAModifier svam)
766 {
767         Modifiers::iterator m = modifiers.find (name);
768
769         if (m == modifiers.end()) {
770                 return;
771         }
772
773         m->second = svam;
774         modifiers_modified = true;
775
776         ColorsChanged (); /* EMIT SIGNAL */
777 }
778
779 void
780 UIConfiguration::load_rc_file (bool themechange, bool allow_own)
781 {
782         string basename = ui_rc_file.get();
783         std::string rc_file_path;
784
785         if (!find_file (ardour_config_search_path(), basename, rc_file_path)) {
786                 warning << string_compose (_("Unable to find UI style file %1 in search path %2. %3 will look strange"),
787                                            basename, ardour_config_search_path().to_string(), PROGRAM_NAME)
788                                 << endmsg;
789                 return;
790         }
791
792         info << "Loading ui configuration file " << rc_file_path << endmsg;
793
794         Gtkmm2ext::UI::instance()->load_rcfile (rc_file_path, themechange);
795 }