enough with umpteen "i18n.h" files. Consolidate on pbd/i18n.h
[ardour.git] / libs / pbd / controllable.cc
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 #include "pbd/controllable.h"
21 #include "pbd/enumwriter.h"
22 #include "pbd/xml++.h"
23 #include "pbd/error.h"
24 #include "pbd/locale_guard.h"
25
26 #include "pbd/i18n.h"
27
28 using namespace PBD;
29 using namespace std;
30
31 PBD::Signal1<void,Controllable*> Controllable::Destroyed;
32 PBD::Signal1<bool,Controllable*> Controllable::StartLearning;
33 PBD::Signal1<void,Controllable*> Controllable::StopLearning;
34 PBD::Signal3<void,Controllable*,int,int> Controllable::CreateBinding;
35 PBD::Signal1<void,Controllable*> Controllable::DeleteBinding;
36
37 Glib::Threads::RWLock Controllable::registry_lock;
38 Controllable::Controllables Controllable::registry;
39 PBD::ScopedConnectionList* registry_connections = 0;
40 const std::string Controllable::xml_node_name = X_("Controllable");
41
42 Controllable::Controllable (const string& name, Flag f)
43         : _name (name)
44         , _flags (f)
45         , _touching (false)
46 {
47         add (*this);
48 }
49
50 void
51 Controllable::add (Controllable& ctl)
52 {
53         using namespace boost;
54
55         Glib::Threads::RWLock::WriterLock lm (registry_lock);
56         registry.insert (&ctl);
57
58         if (!registry_connections) {
59                 registry_connections = new ScopedConnectionList;
60         }
61
62         /* Controllable::remove() is static - no need to manage this connection */
63
64         ctl.DropReferences.connect_same_thread (*registry_connections, boost::bind (&Controllable::remove, &ctl));
65 }
66
67 void
68 Controllable::remove (Controllable* ctl)
69 {
70         Glib::Threads::RWLock::WriterLock lm (registry_lock);
71
72         for (Controllables::iterator i = registry.begin(); i != registry.end(); ++i) {
73                 if ((*i) == ctl) {
74                         registry.erase (i);
75                         break;
76                 }
77         }
78 }
79
80 Controllable*
81 Controllable::by_id (const ID& id)
82 {
83         Glib::Threads::RWLock::ReaderLock lm (registry_lock);
84
85         for (Controllables::iterator i = registry.begin(); i != registry.end(); ++i) {
86                 if ((*i)->id() == id) {
87                         return (*i);
88                 }
89         }
90         return 0;
91 }
92
93 Controllable*
94 Controllable::by_name (const string& str)
95 {
96         Glib::Threads::RWLock::ReaderLock lm (registry_lock);
97
98         for (Controllables::iterator i = registry.begin(); i != registry.end(); ++i) {
99                 if ((*i)->_name == str) {
100                         return (*i);
101                 }
102         }
103         return 0;
104 }
105
106 XMLNode&
107 Controllable::get_state ()
108 {
109         XMLNode* node = new XMLNode (xml_node_name);
110         LocaleGuard lg;
111         char buf[64];
112
113         /* Waves' "Pressure3" has a parameter called "µ-iness"
114          * which causes a  parser error : Input is not proper UTF-8, indicate encoding !
115          *  Bytes: 0xB5 0x2D 0x69 0x6E
116          *          <Controllable name="�-iness" id="2391" flags="" value="0.000000000000" p
117          */
118
119         // this is not reloaded from XML, but it must be present because it is
120         // used to find and identify XML nodes by various Controllable-derived objects
121
122         node->add_property (X_("name"), _name);
123
124         id().print (buf, sizeof (buf));
125         node->add_property (X_("id"), buf);
126         node->add_property (X_("flags"), enum_2_string (_flags));
127         snprintf (buf, sizeof (buf), "%2.12f", get_value());
128         node->add_property (X_("value"), buf);
129
130         if (_extra_xml) {
131                 node->add_child_copy (*_extra_xml);
132         }
133
134         return *node;
135 }
136
137
138 int
139 Controllable::set_state (const XMLNode& node, int /*version*/)
140 {
141         LocaleGuard lg;
142         const XMLProperty* prop;
143
144         Stateful::save_extra_xml (node);
145
146         set_id (node);
147
148         if ((prop = node.property (X_("flags"))) != 0) {
149                 _flags = (Flag) string_2_enum (prop->value(), _flags);
150         }
151
152         if ((prop = node.property (X_("value"))) != 0) {
153                 float val;
154
155                 if (sscanf (prop->value().c_str(), "%f", &val) == 1) {
156                         set_value (val, NoGroup);
157                 }
158         }
159
160         return 0;
161 }
162
163 void
164 Controllable::set_flags (Flag f)
165 {
166         _flags = f;
167 }