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