fix up various issues with UIConfiguration, saving state, RC file loading etc.
[ardour.git] / gtk2_ardour / ui_config.h
1 /*
2     Copyright (C) 2000-2007 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 #ifndef __ardour_ui_configuration_h__
21 #define __ardour_ui_configuration_h__
22
23 #include <sstream>
24 #include <ostream>
25 #include <iostream>
26
27 #include <boost/function.hpp>
28 #include <boost/bind.hpp>
29
30 #include "pbd/stateful.h"
31 #include "pbd/xml++.h"
32 #include "ardour/configuration_variable.h"
33
34 #include "canvas/colors.h"
35
36 #include "utils.h"
37
38 /* This is very similar to ARDOUR::ConfigVariable but expects numeric values to
39  * be in hexadecimal. This is because it is intended for use with color
40  * specifications which are easier to scan for issues in "rrggbbaa" format than
41  * as decimals.
42  */
43 template<class T>
44 class ColorVariable : public ARDOUR::ConfigVariableBase
45 {
46     public:
47         ColorVariable (std::string str) : ARDOUR::ConfigVariableBase (str) {}
48         ColorVariable (std::string str, T val) : ARDOUR::ConfigVariableBase (str), value (val) {}
49
50         bool set (T val) {
51                 if (val == value) {
52                         return false;
53                 }
54                 value = val;
55                 return true;
56         }
57
58         T get() const {
59                 return value;
60         }
61
62         std::string get_as_string () const {
63                 std::stringstream ss;
64                 ss << std::hex;
65                 ss.fill('0');
66                 ss.width(8);
67                 ss << value;
68                 return ss.str ();
69         }
70
71         void set_from_string (std::string const & s) {
72                 std::stringstream ss;
73                 ss << std::hex;
74                 ss << s;
75                 ss >> value;
76         }
77
78   protected:
79         T get_for_save() { return value; }
80         T value;
81 };
82
83 class UIConfiguration : public PBD::Stateful
84 {
85     public:
86         struct RelativeHSV {
87                 RelativeHSV (const std::string& b, const ArdourCanvas::HSV& mod) 
88                         : base_color (b)
89                         , modifier (mod)
90                         , quantized_hue (-1.0) {}
91                 std::string base_color;
92                 ArdourCanvas::HSV modifier;
93                 double quantized_hue;
94
95                 ArdourCanvas::HSV get() const;
96         };
97
98         UIConfiguration();
99         ~UIConfiguration();
100
101         static UIConfiguration* instance() { return _instance; }
102
103         std::map<std::string,ColorVariable<ArdourCanvas::Color> *> configurable_colors;
104
105         bool dirty () const;
106         void set_dirty ();
107
108         int load_state ();
109         int save_state ();
110         int load_defaults ();
111
112         static void load_rc_file (std::string const &, bool themechange);
113         
114         int set_state (const XMLNode&, int version);
115         XMLNode& get_state (void);
116         XMLNode& get_variables (std::string);
117         void set_variables (const XMLNode&);
118
119         typedef std::map<std::string,RelativeHSV> RelativeColors;
120         typedef std::map<std::string,std::string> ColorAliases;
121
122         RelativeColors relative_colors;
123         ColorAliases color_aliases;
124
125         void set_alias (std::string const & name, std::string const & alias);
126         
127         void reset_relative (const std::string& name, const RelativeHSV& new_value);
128         
129         RelativeHSV color_as_relative_hsv (ArdourCanvas::Color c);
130         ArdourCanvas::Color quantized (ArdourCanvas::Color) const;
131
132         ArdourCanvas::Color base_color_by_name (const std::string&) const;
133         ArdourCanvas::Color color (const std::string&) const;
134         ArdourCanvas::HSV  color_hsv (const std::string&) const;
135
136         sigc::signal<void,std::string> ParameterChanged;
137         void map_parameters (boost::function<void (std::string)>&);
138
139         void parameter_changed (std::string);
140         
141 #undef UI_CONFIG_VARIABLE
142 #define UI_CONFIG_VARIABLE(Type,var,name,value) \
143         Type get_##var () const { return var.get(); } \
144         bool set_##var (Type val) { bool ret = var.set (val); if (ret) { ParameterChanged (name); } return ret;  }
145 #include "ui_config_vars.h"
146 #undef  UI_CONFIG_VARIABLE
147 #define CANVAS_FONT_VARIABLE(var,name) \
148         Pango::FontDescription get_##var () const { return ARDOUR_UI_UTILS::sanitized_font (var.get()); } \
149         bool set_##var (const std::string& val) { bool ret = var.set (val); if (ret) { ParameterChanged (name); } return ret;  }
150 #include "canvas_vars.h"
151 #undef CANVAS_FONT_VARIABLE
152
153 #undef CANVAS_BASE_COLOR
154 #define CANVAS_BASE_COLOR(var,name,val) \
155         ArdourCanvas::Color get_##var() const { return var.get(); } \
156         bool set_##var (ArdourCanvas::Color v) { bool ret = var.set (v); if (ret) { ParameterChanged (name); } return ret;  } \
157         bool set_##var(const ArdourCanvas::HSV& v) const { return set_##var (v.color()); }
158 #include "base_colors.h"
159 #undef CANVAS_BASE_COLOR
160
161 #undef COLOR_ALIAS
162 #define COLOR_ALIAS(var,name,alias) ArdourCanvas::Color get_##var() const { return color (name); }
163 #include "color_aliases.h"
164 #undef COLOR_ALIAS
165
166   private:
167         /* declare variables */
168
169 #undef  UI_CONFIG_VARIABLE
170 #define UI_CONFIG_VARIABLE(Type,var,name,value) ARDOUR::ConfigVariable<Type> var;
171 #include "ui_config_vars.h"
172 #undef UI_CONFIG_VARIABLE
173
174 #define CANVAS_FONT_VARIABLE(var,name) ARDOUR::ConfigVariable<std::string> var;
175 #include "canvas_vars.h"
176 #undef CANVAS_FONT_VARIABLE
177
178         /* declare base color variables (these are modifiable by the user) */
179
180 #undef CANVAS_BASE_COLOR
181 #define CANVAS_BASE_COLOR(var,name,val) ColorVariable<ArdourCanvas::Color> var;
182 #include "base_colors.h"
183 #undef CANVAS_BASE_COLOR
184
185         XMLNode& state ();
186         bool _dirty;
187         bool aliases_modified;
188         bool derived_modified;
189         
190         static UIConfiguration* _instance;
191
192         void load_color_aliases (XMLNode const &);
193         void reset_gtk_theme ();
194         void colors_changed ();
195         
196         XMLNode _saved_state_node;
197         int     _saved_state_version;
198 };
199
200 #endif /* __ardour_ui_configuration_h__ */
201