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