Fixed overflow issue. Code originally meant to truncate the 64 bit integer did not...
[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 <glibmm/miscutils.h>
24
25 #include <pbd/failed_constructor.h>
26 #include <pbd/xml++.h>
27 #include <pbd/error.h>
28
29 #include <ardour/ardour.h>
30
31 #include "ui_config.h"
32
33 #include "i18n.h"
34
35 using namespace std;
36 using namespace PBD;
37 using namespace ARDOUR;
38
39 UIConfiguration::UIConfiguration ()
40         :
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)
50 {
51         load_state();
52 }
53
54 UIConfiguration::~UIConfiguration ()
55 {
56 }
57
58 int
59 UIConfiguration::load_defaults ()
60 {
61         int found = 0;
62         std::string rcfile;
63         const char* ui_conf;
64
65         ui_conf = getenv ("ARDOUR_UI_CONF");
66
67         if (ui_conf && ui_conf[0] != '\0') {
68                 rcfile = find_config_file (ui_conf);
69         } else {
70                 rcfile = find_config_file ("ardour2_ui_default.conf");
71         }
72
73         if (rcfile.length()) {
74
75                 XMLTree tree;
76                 found = 1;
77
78                 cerr << string_compose (_("loading default ui configuration file %1"), rcfile) << endl;
79                 
80                 if (!tree.read (rcfile.c_str())) {
81                         error << string_compose(_("Ardour: cannot read default ui configuration file \"%1\""), rcfile) << endmsg;
82                         return -1;
83                 }
84
85                 if (set_state (*tree.root())) {
86                         error << string_compose(_("Ardour: default ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
87                         return -1;
88                 }
89         }
90         return found;
91 }
92         
93 int
94 UIConfiguration::load_state ()
95 {
96         int found = load_defaults ();
97         std::string rcfile = find_config_file ("ardour2_ui.conf");
98
99         if (rcfile.length())
100         {
101                 XMLTree tree;
102                 found = true;
103
104                 cerr << string_compose (_("loading user ui configuration file %1"), rcfile) << endl;
105
106                 if (!tree.read (rcfile)) {
107                         error << string_compose(_("Ardour: cannot read ui configuration file \"%1\""), rcfile) << endmsg;
108                         return -1;
109                 }
110
111                 if (set_state (*tree.root())) {
112                         error << string_compose(_("Ardour: user ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
113                         return -1;
114                 }
115         }
116
117         if (!found)
118                 error << "Ardour: could not find any ui configuration file, canvas will look broken." << endmsg;
119
120         pack_canvasvars();
121         return 0;
122 }
123
124 int
125 UIConfiguration::save_state()
126 {
127         XMLTree tree;
128         string rcfile;
129
130         rcfile = Glib::build_filename (get_user_ardour_path (), "ardour2_ui.conf");
131
132         if (rcfile.length()) {
133                 tree.set_root (&get_state());
134                 if (!tree.write (rcfile.c_str())){
135                         error << string_compose (_("UI config file %1 not saved"), rcfile) << endmsg;
136                         return -1;
137                 }
138         }
139
140         return 0;
141 }
142
143 XMLNode&
144 UIConfiguration::get_state ()
145 {
146         XMLNode* root;
147         LocaleGuard lg (X_("POSIX"));
148
149         root = new XMLNode("Ardour");
150         
151         root->add_child_nocopy (get_variables ("UI"));
152         root->add_child_nocopy (get_variables ("Canvas"));
153         
154         if (_extra_xml) {
155                 root->add_child_copy (*_extra_xml);
156         }
157         
158         return *root;
159 }
160
161 XMLNode&
162 UIConfiguration::get_variables (std::string which_node)
163 {
164         XMLNode* node;
165         LocaleGuard lg (X_("POSIX"));
166
167         node = new XMLNode(which_node);
168
169 #undef  UI_CONFIG_VARIABLE
170 #undef  CANVAS_VARIABLE
171 #define UI_CONFIG_VARIABLE(Type,var,Name,value) if (node->name() == "UI") { var.add_to_node (*node); }
172 #define CANVAS_VARIABLE(var,Name) if (node->name() == "Canvas") { var.add_to_node (*node); }
173 #include "ui_config_vars.h"
174 #include "canvas_vars.h"
175 #undef  UI_CONFIG_VARIABLE
176 #undef  CANVAS_VARIABLE
177
178         return *node;
179 }
180
181 int
182 UIConfiguration::set_state (const XMLNode& root)
183 {
184         if (root.name() != "Ardour") {
185                 return -1;
186         }
187
188         XMLNodeList nlist = root.children();
189         XMLNodeConstIterator niter;
190         XMLNode *node;
191
192         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
193
194                 node = *niter;
195                 if (node->name() == "Canvas" ||  node->name() == "UI") {
196                         set_variables (*node);
197
198                 } else if (node->name() == "extra") {
199                         _extra_xml = new XMLNode (*node);
200
201                 }
202         }
203         return 0;
204 }
205
206 void
207 UIConfiguration::set_variables (const XMLNode& node)
208 {
209 #undef  UI_CONFIG_VARIABLE
210 #undef  CANVAS_VARIABLE
211 #define UI_CONFIG_VARIABLE(Type,var,name,val) \
212          if (var.set_from_node (node)) { \
213                  ParameterChanged (name); \
214                  }
215 #define CANVAS_VARIABLE(var,name) \
216          if (var.set_from_node (node)) { \
217                  ParameterChanged (name); \
218                  }
219 #include "ui_config_vars.h"
220 #include "canvas_vars.h"
221 #undef  UI_CONFIG_VARIABLE
222 #undef  CANVAS_VARIABLE
223 }
224
225 void
226 UIConfiguration::pack_canvasvars ()
227 {
228 #undef  CANVAS_VARIABLE
229 #define CANVAS_VARIABLE(var,name) canvas_colors.push_back(&var); 
230 #include "canvas_vars.h"
231 #undef  CANVAS_VARIABLE
232 }
233
234