Merge branch 'cairocanvas'
[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 "utils.h"
32
33 /* This is very similar to ARDOUR::ConfigVariable but expects numeric values to
34  * be in hexadecimal. This is because it is intended for use with color
35  * specifications which are easier to scan for issues in "rrggbbaa" format than
36  * as decimals.
37  */
38 template<class T>
39 class ColorVariable : public ARDOUR::ConfigVariableBase
40 {
41   public:
42         ColorVariable (std::string str) : ARDOUR::ConfigVariableBase (str) {}
43         ColorVariable (std::string str, T val) : ARDOUR::ConfigVariableBase (str), value (val) {}
44
45         bool set (T val) {
46                 if (val == value) {
47                         return false;
48                 }
49                 value = val;
50                 return true;
51         }
52
53         T get() const {
54                 return value;
55         }
56
57         std::string get_as_string () const {
58                 std::stringstream ss;
59                 ss << std::hex;
60                 ss.fill('0');
61                 ss.width(8);
62                 ss << value;
63                 return ss.str ();
64         }
65
66         void set_from_string (std::string const & s) {
67                 std::stringstream ss;
68                 ss << std::hex;
69                 ss << s;
70                 ss >> value;
71         }
72
73   protected:
74         T get_for_save() { return value; }
75         T value;
76 };
77
78 class UIConfiguration : public PBD::Stateful
79 {
80   public:
81         UIConfiguration();
82         ~UIConfiguration();
83
84         std::map<std::string,ColorVariable<uint32_t> *> canvas_colors;
85
86         bool dirty () const;
87         void set_dirty ();
88
89         int load_state ();
90         int save_state ();
91         int load_defaults ();
92
93         int set_state (const XMLNode&, int version);
94         XMLNode& get_state (void);
95         XMLNode& get_variables (std::string);
96         void set_variables (const XMLNode&);
97         void pack_canvasvars ();
98
99         uint32_t color_by_name (const std::string&);
100
101         sigc::signal<void,std::string> ParameterChanged;
102         void map_parameters (boost::function<void (std::string)>&);
103
104 #undef UI_CONFIG_VARIABLE
105 #define UI_CONFIG_VARIABLE(Type,var,name,value) \
106         Type get_##var () const { return var.get(); } \
107         bool set_##var (Type val) { bool ret = var.set (val); if (ret) { ParameterChanged (name); } return ret;  }
108 #include "ui_config_vars.h"
109 #undef  UI_CONFIG_VARIABLE
110 #undef CANVAS_VARIABLE
111 #undef CANVAS_STRING_VARIABLE
112 #define CANVAS_VARIABLE(var,name) \
113         uint32_t get_##var () const { return var.get(); } \
114         bool set_##var (uint32_t val) { bool ret = var.set (val); if (ret) { ParameterChanged (name); } return ret;  }
115 #define CANVAS_STRING_VARIABLE(var,name) \
116         std::string get_##var () const { return var.get(); }                    \
117         bool set_##var (const std::string& val) { bool ret = var.set (val); if (ret) { ParameterChanged (name); } return ret;  }
118 #define CANVAS_FONT_VARIABLE(var,name) \
119         Pango::FontDescription get_##var () const { return ARDOUR_UI_UTILS::sanitized_font (var.get()); } \
120         bool set_##var (const std::string& val) { bool ret = var.set (val); if (ret) { ParameterChanged (name); } return ret;  }
121 #include "canvas_vars.h"
122 #undef  CANVAS_VARIABLE
123 #undef CANVAS_STRING_VARIABLE
124 #undef CANVAS_FONT_VARIABLE
125
126   private:
127
128         /* declare variables */
129
130 #undef  UI_CONFIG_VARIABLE
131 #define UI_CONFIG_VARIABLE(Type,var,name,value) ARDOUR::ConfigVariable<Type> var;
132 #include "ui_config_vars.h"
133 #undef UI_CONFIG_VARIABLE
134
135 #undef CANVAS_VARIABLE
136 #define CANVAS_VARIABLE(var,name) ColorVariable<uint32_t> var;
137 #define CANVAS_STRING_VARIABLE(var,name) ARDOUR::ConfigVariable<std::string> var;
138 #define CANVAS_FONT_VARIABLE(var,name) ARDOUR::ConfigVariable<std::string> var;
139 #include "canvas_vars.h"
140 #undef  CANVAS_VARIABLE
141 #undef CANVAS_STRING_VARIABLE
142 #undef CANVAS_FONT_VARIABLE
143
144         XMLNode& state ();
145         bool _dirty;
146 };
147
148 #endif /* __ardour_ui_configuration_h__ */
149