put man pages here at top level
[ardour2.git] / gtk2_ardour / ui_config.cc
blobce9fbce172c2a52e67630a59d25a0b6c19ff1346
1 /*
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.
20 #include <unistd.h>
21 #include <cstdio> /* for snprintf, grrr */
23 #include <glibmm/miscutils.h>
25 #include <pbd/failed_constructor.h>
26 #include <pbd/xml++.h>
27 #include <pbd/error.h>
29 #include <ardour/ardour.h>
31 #include "ui_config.h"
33 #include "i18n.h"
35 using namespace std;
36 using namespace PBD;
37 using namespace ARDOUR;
39 UIConfiguration::UIConfiguration ()
41 #undef UI_CONFIG_VARIABLE
42 #undef CANVAS_VARIABLE
43 #define UI_CONFIG_VARIABLE(Type,var,name,val) var (name,val),
44 #define CANVAS_VARIABLE(var,name) var (name),
45 #include "ui_config_vars.h"
46 #include "canvas_vars.h"
47 #undef UI_CONFIG_VARIABLE
48 #undef CANVAS_VARIABLE
49 hack(true)
51 load_state();
54 UIConfiguration::~UIConfiguration ()
58 int
59 UIConfiguration::load_defaults ()
61 int found = 0;
62 std::string rcfile;
63 const char* ui_conf;
65 ui_conf = getenv ("ARDOUR_UI_CONF");
67 if (ui_conf && ui_conf[0] != '\0') {
68 rcfile = find_config_file (ui_conf);
69 } else {
70 if (getenv ("ARDOUR_SAE")) {
71 rcfile = find_config_file ("ardour2_ui_sae.conf");
72 } else {
73 rcfile = find_config_file ("ardour2_ui_default.conf");
77 if (rcfile.length()) {
79 XMLTree tree;
80 found = 1;
82 cerr << string_compose (_("loading default ui configuration file %1"), rcfile) << endl;
84 if (!tree.read (rcfile.c_str())) {
85 error << string_compose(_("Ardour: cannot read default ui configuration file \"%1\""), rcfile) << endmsg;
86 return -1;
89 if (set_state (*tree.root())) {
90 error << string_compose(_("Ardour: default ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
91 return -1;
94 return found;
97 int
98 UIConfiguration::load_state ()
100 int found = load_defaults ();
101 std::string rcfile = find_config_file ("ardour2_ui.conf");
103 if (rcfile.length())
105 XMLTree tree;
106 found = true;
108 cerr << string_compose (_("loading user ui configuration file %1"), rcfile) << endl;
110 if (!tree.read (rcfile)) {
111 error << string_compose(_("Ardour: cannot read ui configuration file \"%1\""), rcfile) << endmsg;
112 return -1;
115 if (set_state (*tree.root())) {
116 error << string_compose(_("Ardour: user ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
117 return -1;
121 if (!found)
122 error << "Ardour: could not find any ui configuration file, canvas will look broken." << endmsg;
124 pack_canvasvars();
125 return 0;
129 UIConfiguration::save_state()
131 XMLTree tree;
132 string rcfile;
134 rcfile = Glib::build_filename (get_user_ardour_path (), "ardour2_ui.conf");
136 if (rcfile.length()) {
137 tree.set_root (&get_state());
138 if (!tree.write (rcfile.c_str())){
139 error << string_compose (_("UI config file %1 not saved"), rcfile) << endmsg;
140 return -1;
144 return 0;
147 XMLNode&
148 UIConfiguration::get_state ()
150 XMLNode* root;
151 LocaleGuard lg (X_("POSIX"));
153 root = new XMLNode("Ardour");
155 root->add_child_nocopy (get_variables ("UI"));
156 root->add_child_nocopy (get_variables ("Canvas"));
158 if (_extra_xml) {
159 root->add_child_copy (*_extra_xml);
162 return *root;
165 XMLNode&
166 UIConfiguration::get_variables (std::string which_node)
168 XMLNode* node;
169 LocaleGuard lg (X_("POSIX"));
171 node = new XMLNode(which_node);
173 #undef UI_CONFIG_VARIABLE
174 #undef CANVAS_VARIABLE
175 #define UI_CONFIG_VARIABLE(Type,var,Name,value) if (node->name() == "UI") { var.add_to_node (*node); }
176 #define CANVAS_VARIABLE(var,Name) if (node->name() == "Canvas") { var.add_to_node (*node); }
177 #include "ui_config_vars.h"
178 #include "canvas_vars.h"
179 #undef UI_CONFIG_VARIABLE
180 #undef CANVAS_VARIABLE
182 return *node;
186 UIConfiguration::set_state (const XMLNode& root)
188 if (root.name() != "Ardour") {
189 return -1;
192 XMLNodeList nlist = root.children();
193 XMLNodeConstIterator niter;
194 XMLNode *node;
196 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
198 node = *niter;
199 if (node->name() == "Canvas" || node->name() == "UI") {
200 set_variables (*node);
202 } else if (node->name() == "extra") {
203 _extra_xml = new XMLNode (*node);
207 return 0;
210 void
211 UIConfiguration::set_variables (const XMLNode& node)
213 #undef UI_CONFIG_VARIABLE
214 #undef CANVAS_VARIABLE
215 #define UI_CONFIG_VARIABLE(Type,var,name,val) \
216 if (var.set_from_node (node)) { \
217 ParameterChanged (name); \
219 #define CANVAS_VARIABLE(var,name) \
220 if (var.set_from_node (node)) { \
221 ParameterChanged (name); \
223 #include "ui_config_vars.h"
224 #include "canvas_vars.h"
225 #undef UI_CONFIG_VARIABLE
226 #undef CANVAS_VARIABLE
229 void
230 UIConfiguration::pack_canvasvars ()
232 #undef CANVAS_VARIABLE
233 #define CANVAS_VARIABLE(var,name) canvas_colors.push_back(&var);
234 #include "canvas_vars.h"
235 #undef CANVAS_VARIABLE