Merge branch 'master' into windows
[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 int
63 UIConfiguration::load_defaults ()
64 {
65         int found = 0;
66
67         std::string default_ui_rc_file;
68         std::string rcfile;
69
70         if (getenv ("ARDOUR_SAE")) {
71                 rcfile = "ardour3_ui_sae.conf";
72         } else {
73                 rcfile = "ardour3_ui_default.conf";
74         }
75
76         if (find_file_in_search_path (ardour_config_search_path(), rcfile, default_ui_rc_file) ) {
77                 XMLTree tree;
78                 found = 1;
79
80                 string rcfile = default_ui_rc_file;
81
82                 info << string_compose (_("Loading default ui configuration file %1"), rcfile) << endl;
83
84                 if (!tree.read (rcfile.c_str())) {
85                         error << string_compose(_("cannot read default ui configuration file \"%1\""), rcfile) << endmsg;
86                         return -1;
87                 }
88
89                 if (set_state (*tree.root(), Stateful::loading_state_version)) {
90                         error << string_compose(_("default ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
91                         return -1;
92                 }
93
94                 _dirty = false;
95         }
96
97         return found;
98 }
99
100 int
101 UIConfiguration::load_state ()
102 {
103         bool found = false;
104
105         std::string default_ui_rc_file;
106
107         if ( find_file_in_search_path (ardour_config_search_path(), "ardour3_ui_default.conf", default_ui_rc_file)) {
108                 XMLTree tree;
109                 found = true;
110
111                 string rcfile = default_ui_rc_file;
112
113                 info << string_compose (_("Loading default ui configuration file %1"), rcfile) << endl;
114
115                 if (!tree.read (rcfile.c_str())) {
116                         error << string_compose(_("cannot read default ui configuration file \"%1\""), rcfile) << endmsg;
117                         return -1;
118                 }
119
120                 if (set_state (*tree.root(), Stateful::loading_state_version)) {
121                         error << string_compose(_("default ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
122                         return -1;
123                 }
124         }
125
126         std::string user_ui_rc_file;
127
128         if (find_file_in_search_path (ardour_config_search_path(), "ardour3_ui.conf", user_ui_rc_file)) {
129                 XMLTree tree;
130                 found = true;
131
132                 string rcfile = user_ui_rc_file;
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, "ardour3_ui.conf");
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 #include "ui_config_vars.h"
210 #include "canvas_vars.h"
211 #undef  UI_CONFIG_VARIABLE
212 #undef  CANVAS_VARIABLE
213
214         return *node;
215 }
216
217 int
218 UIConfiguration::set_state (const XMLNode& root, int /*version*/)
219 {
220         if (root.name() != "Ardour") {
221                 return -1;
222         }
223
224         Stateful::save_extra_xml (root);
225
226         XMLNodeList nlist = root.children();
227         XMLNodeConstIterator niter;
228         XMLNode *node;
229
230         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
231
232                 node = *niter;
233
234                 if (node->name() == "Canvas" ||  node->name() == "UI") {
235                         set_variables (*node);
236
237                 }
238         }
239
240         return 0;
241 }
242
243 void
244 UIConfiguration::set_variables (const XMLNode& node)
245 {
246 #undef  UI_CONFIG_VARIABLE
247 #undef  CANVAS_VARIABLE
248 #define UI_CONFIG_VARIABLE(Type,var,name,val) \
249          if (var.set_from_node (node)) { \
250                  ParameterChanged (name); \
251                  }
252 #define CANVAS_VARIABLE(var,name) \
253          if (var.set_from_node (node)) { \
254                  ParameterChanged (name); \
255                  }
256 #include "ui_config_vars.h"
257 #include "canvas_vars.h"
258 #undef  UI_CONFIG_VARIABLE
259 #undef  CANVAS_VARIABLE
260 }
261
262 void
263 UIConfiguration::pack_canvasvars ()
264 {
265 #undef  CANVAS_VARIABLE
266 #define CANVAS_VARIABLE(var,name) canvas_colors.insert (std::pair<std::string,UIConfigVariable<uint32_t>* >(name,&var));
267 #include "canvas_vars.h"
268 #undef  CANVAS_VARIABLE
269 }
270
271 uint32_t
272 UIConfiguration::color_by_name (const std::string& name)
273 {
274         map<std::string,UIConfigVariable<uint32_t>* >::iterator i = canvas_colors.find (name);
275
276         if (i != canvas_colors.end()) {
277                 return i->second->get();
278         }
279
280         // cerr << string_compose (_("Color %1 not found"), name) << endl;
281         return RGBA_TO_UINT (g_random_int()%256,g_random_int()%256,g_random_int()%256,0xff);
282 }
283
284 void
285 UIConfiguration::set_dirty ()
286 {
287         _dirty = true;
288 }
289
290 bool
291 UIConfiguration::dirty () const
292 {
293         return _dirty;
294 }