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