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