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