change the way font specifications are used in UI config files
[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 UIConfiguration::UIConfiguration ()
44         :
45 #undef  UI_CONFIG_VARIABLE
46 #undef  CANVAS_VARIABLE
47 #define UI_CONFIG_VARIABLE(Type,var,name,val) var (name,val),
48 #define CANVAS_VARIABLE(var,name) var (name),
49 #define CANVAS_STRING_VARIABLE(var,name) var (name),
50 #define CANVAS_FONT_VARIABLE(var,name) var (name),
51 #include "ui_config_vars.h"
52 #include "canvas_vars.h"
53 #undef  UI_CONFIG_VARIABLE
54 #undef  CANVAS_VARIABLE
55 #undef  CANVAS_STRING_VARIABLE
56 #undef  CANVAS_FONT_VARIABLE
57         _dirty (false)
58 {
59         load_state();
60 }
61
62 UIConfiguration::~UIConfiguration ()
63 {
64 }
65
66 void
67 UIConfiguration::map_parameters (boost::function<void (std::string)>& functor)
68 {
69 #undef  UI_CONFIG_VARIABLE
70 #define UI_CONFIG_VARIABLE(Type,var,Name,value) functor (Name);
71 #include "ui_config_vars.h"
72 #undef  UI_CONFIG_VARIABLE
73 }
74
75 int
76 UIConfiguration::load_defaults ()
77 {
78         int found = 0;
79
80         std::string default_ui_rc_file;
81         std::string rcfile;
82
83         if (getenv ("ARDOUR_SAE")) {
84                 rcfile = "ardour3_ui_sae.conf";
85         } else {
86                 rcfile = "ardour3_ui_default.conf";
87         }
88
89         if (find_file_in_search_path (ardour_config_search_path(), rcfile, default_ui_rc_file) ) {
90                 XMLTree tree;
91                 found = 1;
92
93                 string rcfile = default_ui_rc_file;
94
95                 info << string_compose (_("Loading default ui configuration file %1"), rcfile) << endl;
96
97                 if (!tree.read (rcfile.c_str())) {
98                         error << string_compose(_("cannot read default ui configuration file \"%1\""), rcfile) << endmsg;
99                         return -1;
100                 }
101
102                 if (set_state (*tree.root(), Stateful::loading_state_version)) {
103                         error << string_compose(_("default ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
104                         return -1;
105                 }
106
107                 _dirty = false;
108         }
109
110         return found;
111 }
112
113 int
114 UIConfiguration::load_state ()
115 {
116         bool found = false;
117
118         std::string default_ui_rc_file;
119
120         if ( find_file_in_search_path (ardour_config_search_path(), "ardour3_ui_default.conf", default_ui_rc_file)) {
121                 XMLTree tree;
122                 found = true;
123
124                 string rcfile = default_ui_rc_file;
125
126                 info << string_compose (_("Loading default ui configuration file %1"), rcfile) << endl;
127
128                 if (!tree.read (rcfile.c_str())) {
129                         error << string_compose(_("cannot read default ui configuration file \"%1\""), rcfile) << endmsg;
130                         return -1;
131                 }
132
133                 if (set_state (*tree.root(), Stateful::loading_state_version)) {
134                         error << string_compose(_("default ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
135                         return -1;
136                 }
137         }
138
139         std::string user_ui_rc_file;
140
141         if (find_file_in_search_path (ardour_config_search_path(), "ardour3_ui.conf", user_ui_rc_file)) {
142                 XMLTree tree;
143                 found = true;
144
145                 string rcfile = user_ui_rc_file;
146
147                 info << string_compose (_("Loading user ui configuration file %1"), rcfile) << endmsg;
148
149                 if (!tree.read (rcfile)) {
150                         error << string_compose(_("cannot read ui configuration file \"%1\""), rcfile) << endmsg;
151                         return -1;
152                 }
153
154                 if (set_state (*tree.root(), Stateful::loading_state_version)) {
155                         error << string_compose(_("user ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
156                         return -1;
157                 }
158
159                 _dirty = false;
160         }
161
162         if (!found)
163                 error << _("could not find any ui configuration file, canvas will look broken.") << endmsg;
164
165         pack_canvasvars();
166
167         return 0;
168 }
169
170 int
171 UIConfiguration::save_state()
172 {
173         XMLTree tree;
174
175         std::string rcfile(user_config_directory());
176         rcfile = Glib::build_filename (rcfile, "ardour3_ui.conf");
177
178         // this test seems bogus?
179         if (rcfile.length()) {
180                 tree.set_root (&get_state());
181                 if (!tree.write (rcfile.c_str())){
182                         error << string_compose (_("Config file %1 not saved"), rcfile) << endmsg;
183                         return -1;
184                 }
185         }
186
187         _dirty = false;
188
189         return 0;
190 }
191
192 XMLNode&
193 UIConfiguration::get_state ()
194 {
195         XMLNode* root;
196         LocaleGuard lg (X_("POSIX"));
197
198         root = new XMLNode("Ardour");
199
200         root->add_child_nocopy (get_variables ("UI"));
201         root->add_child_nocopy (get_variables ("Canvas"));
202
203         if (_extra_xml) {
204                 root->add_child_copy (*_extra_xml);
205         }
206
207         return *root;
208 }
209
210 XMLNode&
211 UIConfiguration::get_variables (std::string which_node)
212 {
213         XMLNode* node;
214         LocaleGuard lg (X_("POSIX"));
215
216         node = new XMLNode(which_node);
217
218 #undef  UI_CONFIG_VARIABLE
219 #undef  CANVAS_VARIABLE
220 #define UI_CONFIG_VARIABLE(Type,var,Name,value) if (node->name() == "UI") { var.add_to_node (*node); }
221 #define CANVAS_VARIABLE(var,Name) if (node->name() == "Canvas") { var.add_to_node (*node); }
222 #define CANVAS_STRING_VARIABLE(var,Name) if (node->name() == "Canvas") { var.add_to_node (*node); }
223 #define CANVAS_FONT_VARIABLE(var,Name) if (node->name() == "Canvas") { var.add_to_node (*node); }
224 #include "ui_config_vars.h"
225 #include "canvas_vars.h"
226 #undef  UI_CONFIG_VARIABLE
227 #undef  CANVAS_VARIABLE
228 #undef  CANVAS_STRING_VARIABLE
229 #undef  CANVAS_FONT_VARIABLE
230
231         return *node;
232 }
233
234 int
235 UIConfiguration::set_state (const XMLNode& root, int /*version*/)
236 {
237         if (root.name() != "Ardour") {
238                 return -1;
239         }
240
241         Stateful::save_extra_xml (root);
242
243         XMLNodeList nlist = root.children();
244         XMLNodeConstIterator niter;
245         XMLNode *node;
246
247         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
248
249                 node = *niter;
250
251                 if (node->name() == "Canvas" ||  node->name() == "UI") {
252                         set_variables (*node);
253
254                 }
255         }
256
257         return 0;
258 }
259
260 void
261 UIConfiguration::set_variables (const XMLNode& node)
262 {
263 #undef  UI_CONFIG_VARIABLE
264 #undef  CANVAS_VARIABLE
265 #define UI_CONFIG_VARIABLE(Type,var,name,val) \
266          if (var.set_from_node (node)) { \
267                  ParameterChanged (name); \
268                  }
269 #define CANVAS_VARIABLE(var,name) \
270          if (var.set_from_node (node)) { \
271                  ParameterChanged (name); \
272                  }
273 #define CANVAS_STRING_VARIABLE(var,name)        \
274          if (var.set_from_node (node)) { \
275                  ParameterChanged (name); \
276                  }
277 #define CANVAS_FONT_VARIABLE(var,name)  \
278          if (var.set_from_node (node)) { \
279                  ParameterChanged (name); \
280                  }
281 #include "ui_config_vars.h"
282 #include "canvas_vars.h"
283 #undef  UI_CONFIG_VARIABLE
284 #undef  CANVAS_VARIABLE
285 #undef  CANVAS_STRING_VARIABLE
286 #undef  CANVAS_FONT_VARIABLE
287 }
288
289 void
290 UIConfiguration::pack_canvasvars ()
291 {
292 #undef  CANVAS_VARIABLE
293 #define CANVAS_VARIABLE(var,name) canvas_colors.insert (std::pair<std::string,ColorVariable<uint32_t>* >(name,&var));
294 #define CANVAS_STRING_VARIABLE(var,name) 
295 #define CANVAS_FONT_VARIABLE(var,name) 
296 #include "canvas_vars.h"
297 #undef  CANVAS_VARIABLE
298 #undef  CANVAS_STRING_VARIABLE
299 #undef  CANVAS_FONT_VARIABLE
300 }
301
302 uint32_t
303 UIConfiguration::color_by_name (const std::string& name)
304 {
305         map<std::string,ColorVariable<uint32_t>* >::iterator i = canvas_colors.find (name);
306
307         if (i != canvas_colors.end()) {
308                 return i->second->get();
309         }
310
311         // cerr << string_compose (_("Color %1 not found"), name) << endl;
312         return RGBA_TO_UINT (g_random_int()%256,g_random_int()%256,g_random_int()%256,0xff);
313 }
314
315 void
316 UIConfiguration::set_dirty ()
317 {
318         _dirty = true;
319 }
320
321 bool
322 UIConfiguration::dirty () const
323 {
324         return _dirty;
325 }