Moved canvas colors and theme file selection into new ui specific config system,...
[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 <cstdio> /* for snprintf, grrr */
22
23 #include <pbd/failed_constructor.h>
24 #include <pbd/xml++.h>
25 #include <pbd/filesystem.h>
26 #include <pbd/file_utils.h>
27 #include <pbd/error.h>
28
29 #include <ardour/ardour.h>
30 #include <ardour/filesystem_paths.h>
31
32 #include "ui_config.h"
33
34 #include "i18n.h"
35
36 using namespace std;
37 using namespace PBD;
38 using namespace ARDOUR;
39
40 UIConfiguration::UIConfiguration ()
41         :
42 #undef  UI_CONFIG_VARIABLE
43 #undef  CANVAS_VARIABLE
44 #define UI_CONFIG_VARIABLE(Type,var,name,val) var (name,val),
45 #define CANVAS_VARIABLE(var,name) var (name),
46 #include "ui_config_vars.h"
47 #include "canvas_vars.h"
48 #undef  UI_CONFIG_VARIABLE
49 #undef  CANVAS_VARIABLE
50         hack(true)
51 {
52         load_state();
53 }
54
55 UIConfiguration::~UIConfiguration ()
56 {
57 }
58
59 int
60 UIConfiguration::load_state ()
61 {
62         bool found = false;
63
64         sys::path default_ui_rc_file;
65         
66         if ( find_file_in_search_path (ardour_search_path() + system_config_search_path(),
67                         "ardour2_ui_default.conf", default_ui_rc_file) )
68         {
69                 XMLTree tree;
70                 found = true;
71
72                 string rcfile = default_ui_rc_file.to_string();
73
74                 cerr << string_compose (_("loading default ui configuration file %1"), rcfile) << endl;
75                 
76                 if (!tree.read (rcfile.c_str())) {
77                         error << string_compose(_("Ardour: cannot read default ui configuration file \"%1\""), rcfile) << endmsg;
78                         return -1;
79                 }
80
81                 if (set_state (*tree.root())) {
82                         error << string_compose(_("Ardour: default ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
83                         return -1;
84                 }
85         }
86
87         sys::path user_ui_rc_file;
88
89         if (find_file_in_search_path (ardour_search_path() + user_config_directory(),
90                         "ardour2_ui.conf", user_ui_rc_file))
91         {
92                 XMLTree tree;
93                 found = true;
94         
95                 string rcfile = user_ui_rc_file.to_string();
96
97                 cerr << string_compose (_("loading user ui configuration file %1"), rcfile) << endl;
98
99                 if (!tree.read (rcfile)) {
100                         error << string_compose(_("Ardour: cannot read ui configuration file \"%1\""), rcfile) << endmsg;
101                         return -1;
102                 }
103
104                 if (set_state (*tree.root())) {
105                         error << string_compose(_("Ardour: user ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
106                         return -1;
107                 }
108         }
109
110         if (!found)
111                 error << "Ardour: could not find any ui configuration file, canvas will look broken." << endmsg;
112
113         pack_canvasvars();
114         return 0;
115 }
116
117 int
118 UIConfiguration::save_state()
119 {
120         XMLTree tree;
121
122         try
123         {
124                 sys::create_directories (user_config_directory ());
125         }
126         catch (const sys::filesystem_error& ex)
127         {
128                 error << "Could not create user configuration directory" << endmsg;
129                 return -1;
130         }
131         
132         sys::path rcfile_path(user_config_directory());
133
134         rcfile_path /= "ardour2_ui.conf";
135         const string rcfile = rcfile_path.to_string();
136
137         // this test seems bogus?
138         if (rcfile.length()) {
139                 tree.set_root (&get_state());
140                 if (!tree.write (rcfile.c_str())){
141                         error << string_compose (_("Config file %1 not saved"), rcfile) << endmsg;
142                         return -1;
143                 }
144         }
145
146         return 0;
147 }
148
149 XMLNode&
150 UIConfiguration::get_state ()
151 {
152         XMLNode* root;
153         LocaleGuard lg (X_("POSIX"));
154
155         root = new XMLNode("Ardour");
156         
157         root->add_child_nocopy (get_variables ("UI"));
158         root->add_child_nocopy (get_variables ("Canvas"));
159         
160         if (_extra_xml) {
161                 root->add_child_copy (*_extra_xml);
162         }
163         
164         return *root;
165 }
166
167 XMLNode&
168 UIConfiguration::get_variables (std::string which_node)
169 {
170         XMLNode* node;
171         LocaleGuard lg (X_("POSIX"));
172
173         node = new XMLNode(which_node);
174
175 #undef  UI_CONFIG_VARIABLE
176 #undef  CANVAS_VARIABLE
177 #define UI_CONFIG_VARIABLE(Type,var,Name,value) if (node->name() == "UI") { var.add_to_node (*node); }
178 #define CANVAS_VARIABLE(var,Name) if (node->name() == "Canvas") { var.add_to_node (*node); }
179 #include "ui_config_vars.h"
180 #include "canvas_vars.h"
181 #undef  UI_CONFIG_VARIABLE
182 #undef  CANVAS_VARIABLE
183
184         return *node;
185 }
186
187 int
188 UIConfiguration::set_state (const XMLNode& root)
189 {
190         if (root.name() != "Ardour") {
191                 return -1;
192         }
193
194         XMLNodeList nlist = root.children();
195         XMLNodeConstIterator niter;
196         XMLNode *node;
197
198         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
199
200                 node = *niter;
201                 if (node->name() == "Canvas" ||  node->name() == "UI") {
202                         set_variables (*node);
203
204                 } else if (node->name() == "extra") {
205                         _extra_xml = new XMLNode (*node);
206
207                 }
208         }
209         return 0;
210 }
211
212 void
213 UIConfiguration::set_variables (const XMLNode& node)
214 {
215 #undef  UI_CONFIG_VARIABLE
216 #undef  CANVAS_VARIABLE
217 #define UI_CONFIG_VARIABLE(Type,var,name,val) \
218          if (var.set_from_node (node)) { \
219                  ParameterChanged (name); \
220                  }
221 #define CANVAS_VARIABLE(var,name) \
222          if (var.set_from_node (node)) { \
223                  ParameterChanged (name); \
224                  }
225 #include "ui_config_vars.h"
226 #include "canvas_vars.h"
227 #undef  UI_CONFIG_VARIABLE
228 #undef  CANVAS_VARIABLE
229 }
230
231 void
232 UIConfiguration::pack_canvasvars ()
233 {
234 #undef  CANVAS_VARIABLE
235 #define CANVAS_VARIABLE(var,name) canvas_colors.push_back(&var); 
236 #include "canvas_vars.h"
237 #undef  CANVAS_VARIABLE
238         cerr << "Configuration::pack_canvasvars () called, canvas_colors.size() = " << canvas_colors.size() << endl;
239         
240 }
241
242