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