2 Copyright (C) 2000-2007 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 #ifndef __ardour_configuration_variable_h__
21 #define __ardour_configuration_variable_h__
27 #include <pbd/xml++.h>
31 class ConfigVariableBase {
41 ConfigVariableBase (std::string str) : _name (str), _owner (Default) {}
42 virtual ~ConfigVariableBase() {}
44 std::string name() const { return _name; }
45 Owner owner() const { return _owner; }
47 virtual void add_to_node (XMLNode& node) = 0;
48 virtual bool set_from_node (const XMLNode& node, Owner owner) = 0;
50 void show_stored_value (const std::string&);
51 static void set_show_stored_values (bool yn);
56 static bool show_stores;
63 class ConfigVariable : public ConfigVariableBase
66 ConfigVariable (std::string str) : ConfigVariableBase (str) {}
67 ConfigVariable (std::string str, T val) : ConfigVariableBase (str), value (val) {}
69 virtual bool set (T val, Owner owner = ARDOUR::ConfigVariableBase::Config) {
75 _owner = (ConfigVariableBase::Owner)(_owner |owner);
84 void add_to_node (XMLNode& node) {
86 if (node.name() == "Canvas") {
92 show_stored_value (ss.str());
93 XMLNode* child = new XMLNode ("Option");
94 child->add_property ("name", _name);
95 child->add_property ("value", ss.str());
96 node.add_child_nocopy (*child);
99 bool set_from_node (const XMLNode& node, Owner owner) {
101 if (node.name() == "Config" || node.name() == "Canvas") {
105 const XMLProperty* prop;
107 XMLNodeConstIterator niter;
110 nlist = node.children();
112 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
116 if (child->name() == "Option") {
117 if ((prop = child->property ("name")) != 0) {
118 if (prop->value() == _name) {
119 if ((prop = child->property ("value")) != 0) {
120 std::stringstream ss;
121 if (node.name() == "Canvas")
125 _owner = (ConfigVariableBase::Owner)(_owner |owner);
133 } else if (node.name() == "Options") {
138 XMLNodeConstIterator oiter;
140 const XMLProperty* opt_prop;
142 olist = node.children();
144 for (oiter = olist.begin(); oiter != olist.end(); ++oiter) {
148 if (option->name() == _name) {
149 if ((opt_prop = option->property ("val")) != 0) {
150 std::stringstream ss;
151 ss << opt_prop->value();
153 _owner = (ConfigVariableBase::Owner)(_owner |owner);
164 virtual T get_for_save() { return value; }
169 class ConfigVariableWithMutation : public ConfigVariable<T>
172 ConfigVariableWithMutation (std::string name, T val, T (*m)(T))
173 : ConfigVariable<T> (name, val), mutator (m) {}
175 bool set (T val, ConfigVariableBase::Owner owner) {
176 if (unmutated_value != val) {
177 unmutated_value = val;
178 return ConfigVariable<T>::set (mutator (val), owner);
184 virtual T get_for_save() { return unmutated_value; }
191 #endif /* __ardour_configuration_variable_h__ */