2 Copyright (C) 1999-2006 Paul Davis
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.
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.
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.
22 #include <cstdio> /* for snprintf, grrr */
24 #include <glibmm/miscutils.h>
26 #include "pbd/failed_constructor.h"
27 #include "pbd/xml++.h"
28 #include "pbd/file_utils.h"
29 #include "pbd/error.h"
31 #include "gtkmm2ext/rgb_macros.h"
33 #include "ardour/filesystem_paths.h"
35 #include "ui_config.h"
41 using namespace ARDOUR;
43 static const char* ui_config_file_name = "ui_config";
44 static const char* default_ui_config_file_name = "default_ui_config";
46 UIConfiguration::UIConfiguration ()
48 #undef UI_CONFIG_VARIABLE
49 #undef CANVAS_VARIABLE
50 #define UI_CONFIG_VARIABLE(Type,var,name,val) var (name,val),
51 #define CANVAS_VARIABLE(var,name) var (name),
52 #define CANVAS_STRING_VARIABLE(var,name) var (name),
53 #define CANVAS_FONT_VARIABLE(var,name) var (name),
54 #include "ui_config_vars.h"
55 #include "canvas_vars.h"
56 #undef UI_CONFIG_VARIABLE
57 #undef CANVAS_VARIABLE
58 #undef CANVAS_STRING_VARIABLE
59 #undef CANVAS_FONT_VARIABLE
65 UIConfiguration::~UIConfiguration ()
70 UIConfiguration::map_parameters (boost::function<void (std::string)>& functor)
72 #undef UI_CONFIG_VARIABLE
73 #define UI_CONFIG_VARIABLE(Type,var,Name,value) functor (Name);
74 #include "ui_config_vars.h"
75 #undef UI_CONFIG_VARIABLE
79 UIConfiguration::load_defaults ()
84 if (find_file (ardour_config_search_path(), default_ui_config_file_name, rcfile) ) {
88 info << string_compose (_("Loading default ui configuration file %1"), rcfile) << endl;
90 if (!tree.read (rcfile.c_str())) {
91 error << string_compose(_("cannot read default ui configuration file \"%1\""), rcfile) << endmsg;
95 if (set_state (*tree.root(), Stateful::loading_state_version)) {
96 error << string_compose(_("default ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
107 UIConfiguration::load_state ()
113 if ( find_file (ardour_config_search_path(), default_ui_config_file_name, rcfile)) {
117 info << string_compose (_("Loading default ui configuration file %1"), rcfile) << endl;
119 if (!tree.read (rcfile.c_str())) {
120 error << string_compose(_("cannot read default ui configuration file \"%1\""), rcfile) << endmsg;
124 if (set_state (*tree.root(), Stateful::loading_state_version)) {
125 error << string_compose(_("default ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
130 if (find_file (ardour_config_search_path(), ui_config_file_name, rcfile)) {
134 info << string_compose (_("Loading user ui configuration file %1"), rcfile) << endmsg;
136 if (!tree.read (rcfile)) {
137 error << string_compose(_("cannot read ui configuration file \"%1\""), rcfile) << endmsg;
141 if (set_state (*tree.root(), Stateful::loading_state_version)) {
142 error << string_compose(_("user ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
150 error << _("could not find any ui configuration file, canvas will look broken.") << endmsg;
158 UIConfiguration::save_state()
162 std::string rcfile(user_config_directory());
163 rcfile = Glib::build_filename (rcfile, ui_config_file_name);
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;
180 UIConfiguration::get_state ()
183 LocaleGuard lg (X_("POSIX"));
185 root = new XMLNode("Ardour");
187 root->add_child_nocopy (get_variables ("UI"));
188 root->add_child_nocopy (get_variables ("Canvas"));
191 root->add_child_copy (*_extra_xml);
198 UIConfiguration::get_variables (std::string which_node)
201 LocaleGuard lg (X_("POSIX"));
203 node = new XMLNode(which_node);
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 #define CANVAS_STRING_VARIABLE(var,Name) if (node->name() == "Canvas") { var.add_to_node (*node); }
210 #define CANVAS_FONT_VARIABLE(var,Name) if (node->name() == "Canvas") { var.add_to_node (*node); }
211 #include "ui_config_vars.h"
212 #include "canvas_vars.h"
213 #undef UI_CONFIG_VARIABLE
214 #undef CANVAS_VARIABLE
215 #undef CANVAS_STRING_VARIABLE
216 #undef CANVAS_FONT_VARIABLE
222 UIConfiguration::set_state (const XMLNode& root, int /*version*/)
224 if (root.name() != "Ardour") {
228 Stateful::save_extra_xml (root);
230 XMLNodeList nlist = root.children();
231 XMLNodeConstIterator niter;
234 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
238 if (node->name() == "Canvas" || node->name() == "UI") {
239 set_variables (*node);
248 UIConfiguration::set_variables (const XMLNode& node)
250 #undef UI_CONFIG_VARIABLE
251 #undef CANVAS_VARIABLE
252 #define UI_CONFIG_VARIABLE(Type,var,name,val) \
253 if (var.set_from_node (node)) { \
254 ParameterChanged (name); \
256 #define CANVAS_VARIABLE(var,name) \
257 if (var.set_from_node (node)) { \
258 ParameterChanged (name); \
260 #define CANVAS_STRING_VARIABLE(var,name) \
261 if (var.set_from_node (node)) { \
262 ParameterChanged (name); \
264 #define CANVAS_FONT_VARIABLE(var,name) \
265 if (var.set_from_node (node)) { \
266 ParameterChanged (name); \
268 #include "ui_config_vars.h"
269 #include "canvas_vars.h"
270 #undef UI_CONFIG_VARIABLE
271 #undef CANVAS_VARIABLE
272 #undef CANVAS_STRING_VARIABLE
273 #undef CANVAS_FONT_VARIABLE
277 UIConfiguration::pack_canvasvars ()
279 #undef CANVAS_VARIABLE
280 #define CANVAS_VARIABLE(var,name) canvas_colors.insert (std::pair<std::string,ColorVariable<uint32_t>* >(name,&var));
281 #define CANVAS_STRING_VARIABLE(var,name)
282 #define CANVAS_FONT_VARIABLE(var,name)
283 #include "canvas_vars.h"
284 #undef CANVAS_VARIABLE
285 #undef CANVAS_STRING_VARIABLE
286 #undef CANVAS_FONT_VARIABLE
290 UIConfiguration::color_by_name (const std::string& name)
292 map<std::string,ColorVariable<uint32_t>* >::iterator i = canvas_colors.find (name);
294 if (i != canvas_colors.end()) {
295 return i->second->get();
298 // cerr << string_compose (_("Color %1 not found"), name) << endl;
299 return RGBA_TO_UINT (g_random_int()%256,g_random_int()%256,g_random_int()%256,0xff);
303 UIConfiguration::set_dirty ()
309 UIConfiguration::dirty () const