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