if using a top-menubar (e.g. OS X), don't quit when editor window is closed via WM...
[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                 if (getenv ("ARDOUR_SAE")) {
71                         rcfile = find_config_file ("ardour2_ui_sae.conf");
72                 } else {
73                         rcfile = find_config_file ("ardour2_ui_default.conf");
74                 }
75         }
76
77         if (rcfile.length()) {
78
79                 XMLTree tree;
80                 found = 1;
81
82                 cerr << string_compose (_("loading default ui configuration file %1"), rcfile) << endl;
83                 
84                 if (!tree.read (rcfile.c_str())) {
85                         error << string_compose(_("Ardour: cannot read default ui configuration file \"%1\""), rcfile) << endmsg;
86                         return -1;
87                 }
88
89                 if (set_state (*tree.root())) {
90                         error << string_compose(_("Ardour: default ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
91                         return -1;
92                 }
93         }
94         return found;
95 }
96         
97 int
98 UIConfiguration::load_state ()
99 {
100         int found = load_defaults ();
101         std::string rcfile = find_config_file ("ardour2_ui.conf");
102
103         if (rcfile.length())
104         {
105                 XMLTree tree;
106                 found = true;
107
108                 cerr << string_compose (_("loading user ui configuration file %1"), rcfile) << endl;
109
110                 if (!tree.read (rcfile)) {
111                         error << string_compose(_("Ardour: cannot read ui configuration file \"%1\""), rcfile) << endmsg;
112                         return -1;
113                 }
114
115                 if (set_state (*tree.root())) {
116                         error << string_compose(_("Ardour: user ui configuration file \"%1\" not loaded successfully."), rcfile) << endmsg;
117                         return -1;
118                 }
119         }
120
121         if (!found)
122                 error << "Ardour: could not find any ui configuration file, canvas will look broken." << endmsg;
123
124         pack_canvasvars();
125         return 0;
126 }
127
128 int
129 UIConfiguration::save_state()
130 {
131         XMLTree tree;
132         string rcfile;
133
134         rcfile = Glib::build_filename (get_user_ardour_path (), "ardour2_ui.conf");
135
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;
141                 }
142         }
143
144         return 0;
145 }
146
147 XMLNode&
148 UIConfiguration::get_state ()
149 {
150         XMLNode* root;
151         LocaleGuard lg (X_("POSIX"));
152
153         root = new XMLNode("Ardour");
154         
155         root->add_child_nocopy (get_variables ("UI"));
156         root->add_child_nocopy (get_variables ("Canvas"));
157         
158         if (_extra_xml) {
159                 root->add_child_copy (*_extra_xml);
160         }
161         
162         return *root;
163 }
164
165 XMLNode&
166 UIConfiguration::get_variables (std::string which_node)
167 {
168         XMLNode* node;
169         LocaleGuard lg (X_("POSIX"));
170
171         node = new XMLNode(which_node);
172
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
181
182         return *node;
183 }
184
185 int
186 UIConfiguration::set_state (const XMLNode& root)
187 {
188         if (root.name() != "Ardour") {
189                 return -1;
190         }
191
192         XMLNodeList nlist = root.children();
193         XMLNodeConstIterator niter;
194         XMLNode *node;
195
196         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
197
198                 node = *niter;
199                 if (node->name() == "Canvas" ||  node->name() == "UI") {
200                         set_variables (*node);
201
202                 } else if (node->name() == "extra") {
203                         _extra_xml = new XMLNode (*node);
204
205                 }
206         }
207         return 0;
208 }
209
210 void
211 UIConfiguration::set_variables (const XMLNode& node)
212 {
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); \
218                  }
219 #define CANVAS_VARIABLE(var,name) \
220          if (var.set_from_node (node)) { \
221                  ParameterChanged (name); \
222                  }
223 #include "ui_config_vars.h"
224 #include "canvas_vars.h"
225 #undef  UI_CONFIG_VARIABLE
226 #undef  CANVAS_VARIABLE
227 }
228
229 void
230 UIConfiguration::pack_canvasvars ()
231 {
232 #undef  CANVAS_VARIABLE
233 #define CANVAS_VARIABLE(var,name) canvas_colors.push_back(&var); 
234 #include "canvas_vars.h"
235 #undef  CANVAS_VARIABLE
236 }
237
238