Big ol' automation refactor.
[ardour.git] / libs / ardour / ardour / configuration_variable.h
1 /*
2     Copyright (C) 2000-2007 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 #ifndef __ardour_configuration_variable_h__
21 #define __ardour_configuration_variable_h__
22
23 #include <sstream>
24 #include <ostream>
25 #include <iostream>
26
27 #include <pbd/xml++.h>
28
29 namespace ARDOUR {
30
31 class ConfigVariableBase {
32   public:
33         enum Owner {
34                 Default = 0x1,
35                 System = 0x2,
36                 Config = 0x4,
37                 Session = 0x8,
38                 Interface = 0x10
39         };
40
41         ConfigVariableBase (std::string str) : _name (str), _owner (Default) {}
42         virtual ~ConfigVariableBase() {}
43
44         std::string name() const { return _name; }
45         Owner owner() const { return _owner; }
46
47         virtual void add_to_node (XMLNode& node) = 0;
48         virtual bool set_from_node (const XMLNode& node, Owner owner) = 0;
49
50         void show_stored_value (const std::string&);
51         static void set_show_stored_values (bool yn);
52
53   protected:
54         std::string _name;
55         Owner _owner;
56         static bool show_stores;
57
58         void notify ();
59         void miss ();
60 };
61
62 template<class T>
63 class ConfigVariable : public ConfigVariableBase
64 {
65   public:
66         ConfigVariable (std::string str) : ConfigVariableBase (str) {}
67         ConfigVariable (std::string str, T val) : ConfigVariableBase (str), value (val) {}
68
69         virtual bool set (T val, Owner owner = ARDOUR::ConfigVariableBase::Config) {
70                 if (val == value) {
71                         miss ();
72                         return false;
73                 }
74                 value = val;
75                 _owner = (ConfigVariableBase::Owner)(_owner |owner);
76                 notify ();
77                 return true;
78         }
79
80         T get() const {
81                 return value;
82         }
83
84         void add_to_node (XMLNode& node) {
85                 std::stringstream ss;
86                 if (node.name() == "Canvas") {
87                         ss << std::hex;
88                         ss.fill('0');
89                         ss.width(8);
90                 }
91                 ss << value;
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);
97         }
98         
99         bool set_from_node (const XMLNode& node, Owner owner) {
100
101                 if (node.name() == "Config" || node.name() == "Canvas") {
102
103                         /* ardour.rc */
104
105                         const XMLProperty* prop;
106                         XMLNodeList nlist;
107                         XMLNodeConstIterator niter;
108                         XMLNode* child;
109                         
110                         nlist = node.children();
111                         
112                         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
113                                 
114                                 child = *niter;
115                                 
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")
122                                                                         ss << std::hex;
123                                                                 ss << prop->value();
124                                                                 ss >> value;
125                                                                 _owner = (ConfigVariableBase::Owner)(_owner |owner);
126                                                                 return true;
127                                                         }
128                                                 }
129                                         }
130                                 }
131                         }
132                         
133                 } else if (node.name() == "Options") {
134
135                         /* session file */
136
137                         XMLNodeList olist;
138                         XMLNodeConstIterator oiter;
139                         XMLNode* option;
140                         const XMLProperty* opt_prop;
141                         
142                         olist = node.children();
143                         
144                         for (oiter = olist.begin(); oiter != olist.end(); ++oiter) {
145                                 
146                                 option = *oiter;
147                                 
148                                 if (option->name() == _name) {
149                                         if ((opt_prop = option->property ("val")) != 0) {
150                                                 std::stringstream ss;
151                                                 ss << opt_prop->value();
152                                                 ss >> value;
153                                                 _owner = (ConfigVariableBase::Owner)(_owner |owner);
154                                                 return true;
155                                         }
156                                 }
157                         }
158                 }
159
160                 return false;
161         }
162
163   protected:
164         virtual T get_for_save() { return value; }
165         T value;
166 };
167
168 template<class T>
169 class ConfigVariableWithMutation : public ConfigVariable<T>
170 {
171   public:
172         ConfigVariableWithMutation (std::string name, T val, T (*m)(T)) 
173                 : ConfigVariable<T> (name, val), mutator (m) {}
174
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);
179                 } 
180                 return false;
181         }
182
183   protected:
184         virtual T get_for_save() { return unmutated_value; }
185         T unmutated_value;
186         T (*mutator)(T);
187 };
188
189 }
190
191 #endif /* __ardour_configuration_variable_h__ */