Merge branch 'cairocanvas'
[ardour.git] / gtk2_ardour / ui_config.cc
1 /*
2     Copyright (C) 1999-2006 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 #include <unistd.h>
21 #include <cstdlib>
22 #include <cstdio> /* for snprintf, grrr */
23
24 #include <glibmm/miscutils.h>
25
26 #include "pbd/failed_constructor.h"
27 #include "pbd/xml++.h"
28 #include "pbd/file_utils.h"
29 #include "pbd/error.h"
30
31 #include "gtkmm2ext/rgb_macros.h"
32
33 #include "ardour/filesystem_paths.h"
34
35 #include "ui_config.h"
36
37 #include "i18n.h"
38
39 using namespace std;
40 using namespace PBD;
41 using namespace ARDOUR;
42
43 static const char* ui_config_file_name = "ui_config";
44 static const char* default_ui_config_file_name = "default_ui_config";
45
46 UIConfiguration::UIConfiguration ()
47         :
48 #undef  UI_CONFIG_VARIABLE
49 #undef  CANVAS_VARIABLE
50 #define UI_CONFIG_VARIABLE(Type,var,name,val) var (name,val),
51 #define CANVAS_VARIABLE(var,name) var (name),
52 #define CANVAS_STRING_VARIABLE(var,name) var (name),
53 #define CANVAS_FONT_VARIABLE(var,name) var (name),
54 #include "ui_config_vars.h"
55 #include "canvas_vars.h"
56 #undef  UI_CONFIG_VARIABLE
57 #undef  CANVAS_VARIABLE
58 #undef  CANVAS_STRING_VARIABLE
59 #undef  CANVAS_FONT_VARIABLE
60         _dirty (false)
61 {
62         load_state();
63 }
64
65 UIConfiguration::~UIConfiguration ()
66 {
67 }
68
69 void
70 UIConfiguration::map_parameters (boost::function<void (std::string)>& functor)
71 {
72 #undef  UI_CONFIG_VARIABLE
73 #define UI_CONFIG_VARIABLE(Type,var,Name,value) functor (Name);
74 #include "ui_config_vars.h"
75 #undef  UI_CONFIG_VARIABLE
76 }
77
78 int
79 UIConfiguration::load_defaults ()
80 {
81         int found = 0;
82         std::string rcfile;
83
84         if (find_file (ardour_config_search_path(), default_ui_config_file_name, rcfile) ) {
85                 XMLTree tree;
86                 found = 1;
87
88                 info << string_compose (_("Loading default ui configuration file %1"), rcfile) << endl;
89
90                 if (!tree.read (rcfile.c_str())) {
91                         error << string_compose(_("cannot read default ui configuration file \"%1\""), rcfile) << endmsg;
92                         return -1;
93                 }
94
95                 if (set_state (*tree.root(), Stateful::loading_state_version)) {
96                         error << string_compose(_("default ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
97                         return -1;
98                 }
99
100                 _dirty = false;
101         }
102
103         return found;
104 }
105
106 int
107 UIConfiguration::load_state ()
108 {
109         bool found = false;
110
111         std::string rcfile;
112
113         if ( find_file (ardour_config_search_path(), default_ui_config_file_name, rcfile)) {
114                 XMLTree tree;
115                 found = true;
116
117                 info << string_compose (_("Loading default ui configuration file %1"), rcfile) << endl;
118
119                 if (!tree.read (rcfile.c_str())) {
120                         error << string_compose(_("cannot read default ui configuration file \"%1\""), rcfile) << endmsg;
121                         return -1;
122                 }
123
124                 if (set_state (*tree.root(), Stateful::loading_state_version)) {
125                         error << string_compose(_("default ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
126                         return -1;
127                 }
128         }
129
130         if (find_file (ardour_config_search_path(), ui_config_file_name, rcfile)) {
131                 XMLTree tree;
132                 found = true;
133
134                 info << string_compose (_("Loading user ui configuration file %1"), rcfile) << endmsg;
135
136                 if (!tree.read (rcfile)) {
137                         error << string_compose(_("cannot read ui configuration file \"%1\""), rcfile) << endmsg;
138                         return -1;
139                 }
140
141                 if (set_state (*tree.root(), Stateful::loading_state_version)) {
142                         error << string_compose(_("user ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
143                         return -1;
144                 }
145
146                 _dirty = false;
147         }
148
149         if (!found)
150                 error << _("could not find any ui configuration file, canvas will look broken.") << endmsg;
151
152         pack_canvasvars();
153
154         return 0;
155 }
156
157 int
158 UIConfiguration::save_state()
159 {
160         XMLTree tree;
161
162         std::string rcfile(user_config_directory());
163         rcfile = Glib::build_filename (rcfile, ui_config_file_name);
164
165         // this test seems bogus?
166         if (rcfile.length()) {
167                 tree.set_root (&get_state());
168                 if (!tree.write (rcfile.c_str())){
169                         error << string_compose (_("Config file %1 not saved"), rcfile) << endmsg;
170                         return -1;
171                 }
172         }
173
174         _dirty = false;
175
176         return 0;
177 }
178
179 XMLNode&
180 UIConfiguration::get_state ()
181 {
182         XMLNode* root;
183         LocaleGuard lg (X_("POSIX"));
184
185         root = new XMLNode("Ardour");
186
187         root->add_child_nocopy (get_variables ("UI"));
188         root->add_child_nocopy (get_variables ("Canvas"));
189
190         if (_extra_xml) {
191                 root->add_child_copy (*_extra_xml);
192         }
193
194         return *root;
195 }
196
197 XMLNode&
198 UIConfiguration::get_variables (std::string which_node)
199 {
200         XMLNode* node;
201         LocaleGuard lg (X_("POSIX"));
202
203         node = new XMLNode(which_node);
204
205 #undef  UI_CONFIG_VARIABLE
206 #undef  CANVAS_VARIABLE
207 #define UI_CONFIG_VARIABLE(Type,var,Name,value) if (node->name() == "UI") { var.add_to_node (*node); }
208 #define CANVAS_VARIABLE(var,Name) if (node->name() == "Canvas") { var.add_to_node (*node); }
209 #define CANVAS_STRING_VARIABLE(var,Name) if (node->name() == "Canvas") { var.add_to_node (*node); }
210 #define CANVAS_FONT_VARIABLE(var,Name) if (node->name() == "Canvas") { var.add_to_node (*node); }
211 #include "ui_config_vars.h"
212 #include "canvas_vars.h"
213 #undef  UI_CONFIG_VARIABLE
214 #undef  CANVAS_VARIABLE
215 #undef  CANVAS_STRING_VARIABLE
216 #undef  CANVAS_FONT_VARIABLE
217
218         return *node;
219 }
220
221 int
222 UIConfiguration::set_state (const XMLNode& root, int /*version*/)
223 {
224         if (root.name() != "Ardour") {
225                 return -1;
226         }
227
228         Stateful::save_extra_xml (root);
229
230         XMLNodeList nlist = root.children();
231         XMLNodeConstIterator niter;
232         XMLNode *node;
233
234         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
235
236                 node = *niter;
237
238                 if (node->name() == "Canvas" ||  node->name() == "UI") {
239                         set_variables (*node);
240
241                 }
242         }
243
244         return 0;
245 }
246
247 void
248 UIConfiguration::set_variables (const XMLNode& node)
249 {
250 #undef  UI_CONFIG_VARIABLE
251 #undef  CANVAS_VARIABLE
252 #define UI_CONFIG_VARIABLE(Type,var,name,val) \
253          if (var.set_from_node (node)) { \
254                  ParameterChanged (name); \
255                  }
256 #define CANVAS_VARIABLE(var,name) \
257          if (var.set_from_node (node)) { \
258                  ParameterChanged (name); \
259                  }
260 #define CANVAS_STRING_VARIABLE(var,name)        \
261          if (var.set_from_node (node)) { \
262                  ParameterChanged (name); \
263                  }
264 #define CANVAS_FONT_VARIABLE(var,name)  \
265          if (var.set_from_node (node)) { \
266                  ParameterChanged (name); \
267                  }
268 #include "ui_config_vars.h"
269 #include "canvas_vars.h"
270 #undef  UI_CONFIG_VARIABLE
271 #undef  CANVAS_VARIABLE
272 #undef  CANVAS_STRING_VARIABLE
273 #undef  CANVAS_FONT_VARIABLE
274 }
275
276 void
277 UIConfiguration::pack_canvasvars ()
278 {
279 #undef  CANVAS_VARIABLE
280 #define CANVAS_VARIABLE(var,name) canvas_colors.insert (std::pair<std::string,ColorVariable<uint32_t>* >(name,&var));
281 #define CANVAS_STRING_VARIABLE(var,name) 
282 #define CANVAS_FONT_VARIABLE(var,name) 
283 #include "canvas_vars.h"
284 #undef  CANVAS_VARIABLE
285 #undef  CANVAS_STRING_VARIABLE
286 #undef  CANVAS_FONT_VARIABLE
287 }
288
289 uint32_t
290 UIConfiguration::color_by_name (const std::string& name)
291 {
292         map<std::string,ColorVariable<uint32_t>* >::iterator i = canvas_colors.find (name);
293
294         if (i != canvas_colors.end()) {
295                 return i->second->get();
296         }
297 #if 0 // yet unsed experimental style postfix
298         /* Idea: use identical colors but different font/sizes
299          * for variants of the same 'widget'.
300          *
301          * example:
302          *  set_name("mute button");  // in route_ui.cc
303          *  set_name("mute button small"); // in mixer_strip.cc
304          *
305          * ardour3_widget_list.rc:
306          *  widget "*mute button" style:highest "small_button"
307          *  widget "*mute button small" style:highest "very_small_text"
308          *
309          * both use color-schema of defined in
310          *   BUTTON_VARS(MuteButton, "mute button")
311          *
312          * (in this particular example the widgets should be packed
313          * vertically shinking the mixer strip ones are currently not)
314          */
315         const size_t name_len = name.size();
316         const size_t name_sep = name.find(':');
317         for (i = canvas_colors.begin(); i != canvas_colors.end(), name_sep != string::npos; ++i) {
318                 const size_t cmp_len = i->first.size();
319                 const size_t cmp_sep = i->first.find(':');
320                 if (cmp_len >= name_len || cmp_sep == string::npos) continue;
321                 if (name.substr(name_sep) != i->first.substr(cmp_sep)) continue;
322                 if (name.substr(0, cmp_sep) != i->first.substr(0, cmp_sep)) continue;
323                 return i->second->get();
324         }
325 #endif
326
327         // cerr << string_compose (_("Color %1 not found"), name) << endl;
328         return RGBA_TO_UINT (g_random_int()%256,g_random_int()%256,g_random_int()%256,0xff);
329 }
330
331 void
332 UIConfiguration::set_dirty ()
333 {
334         _dirty = true;
335 }
336
337 bool
338 UIConfiguration::dirty () const
339 {
340         return _dirty;
341 }