add new Controllable::GroupDisposition value, ForGroup, to identify changes being...
[ardour.git] / libs / pbd / pbd / controllable.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 __pbd_controllable_h__
21 #define __pbd_controllable_h__
22
23 #include <string>
24 #include <set>
25 #include <map>
26
27 #include "pbd/libpbd_visibility.h"
28 #include "pbd/signals.h"
29 #include <glibmm/threads.h>
30
31 #include "pbd/statefuldestructible.h"
32
33 using std::min;
34 using std::max;
35
36 class XMLNode;
37
38 namespace PBD {
39
40 /* This is a pure virtual class to represent a scalar control.
41  *
42  * Note that it contains no storage/state for the controllable thing that it
43  * represents. Derived classes must provide set_value()/get_value() methods,
44  * which will involve (somehow) an actual location to store the value.
45  *
46  * In essence, this is an interface, not a class.
47  *
48  * Without overriding upper() and lower(), a derived class will function
49  * as a control whose value can range between 0 and 1.0.
50  *
51  */
52
53 class LIBPBD_API Controllable : public PBD::StatefulDestructible {
54   public:
55         enum Flag {
56                 Toggle = 0x1,
57                 GainLike = 0x2,
58         };
59
60         Controllable (const std::string& name, Flag f = Flag (0));
61         virtual ~Controllable() { Destroyed (this); }
62
63         /* We express Controllable values in one of three ways:
64          * 1. `user' --- as presented to the user (e.g. dB, Hz, etc.)
65          * 2. `interface' --- as used in some cases for the UI representation
66          * (in order to make controls behave logarithmically).
67          * 3. `internal' --- as passed to a processor, track, plugin, or whatever.
68          *
69          * Note that in some cases user and processor may be the same
70          * (and interface different) e.g. frequency, which is presented
71          * to the user and passed to the processor in linear terms, but
72          * which needs log scaling in the interface.
73          *
74          * In other cases, user and interface may be the same (and processor different)
75          * e.g. gain, which is presented to the user in log terms (dB)
76          * but passed to the processor as a linear quantity.
77          */
78
79         /* Within an application, various Controllables might be considered to
80          * be "grouped" in a way that implies that setting 1 of them also
81          * modifies others in the group.
82          */
83
84         enum GroupControlDisposition {
85                 InverseGroup,  /* set all controls in the same "group" as this one */
86                 NoGroup,     /* set only this control */
87                 UseGroup,     /* use group settings to decide which group controls are altered */
88                 ForGroup     /* this setting is being done *for* the group
89                                 (i.e. UseGroup was set in the callchain
90                                 somewhere).
91                              */
92         };
93
94         /** Get and Set `internal' value
95          *
96          * All derived classes must implement this.
97          *
98          * Basic derived classes will ignore @param group_override,
99          * but more sophisticated children, notably those that
100          * proxy the value setting logic via an object that is aware of group
101          * relationships between this control and others, will find it useful.
102          */
103         virtual void set_value (double, GroupControlDisposition group_override) = 0;
104         virtual double get_value (void) const = 0;
105
106         /** Conversions between `internal', 'interface', and 'user' values */
107         virtual double internal_to_interface (double i) const {return  (i-lower())/(upper() - lower());}  //by default, the interface range is just a linear interpolation between lower and upper values
108         virtual double interface_to_internal (double i) const {return lower() + i*(upper() - lower());}
109         virtual double internal_to_user (double i) const {return i;}  //by default the internal value is the same as the user value
110         virtual double user_to_internal (double i) const {return i;}  //by default the internal value is the same as the user value
111
112         /** Get and Set `interface' value  (typically, fraction of knob travel) */
113         virtual float get_interface() const { return (internal_to_interface(get_value())); }
114         virtual void set_interface (float fraction) { fraction = min( max(0.0f, fraction), 1.0f);  set_value(interface_to_internal(fraction), NoGroup); }
115
116         /** Get and Set `user' value  ( dB or milliseconds, etc.  This MIGHT be the same as the internal value, but in a few cases it is not ) */
117         virtual float get_user() const { return (internal_to_user(get_value())); }
118         virtual void set_user (float user_v) { set_value(user_to_internal(user_v), NoGroup); }
119         virtual std::string get_user_string() const { return std::string(); }
120
121         PBD::Signal0<void> LearningFinished;
122         static PBD::Signal3<void,PBD::Controllable*,int,int> CreateBinding;
123         static PBD::Signal1<void,PBD::Controllable*> DeleteBinding;
124
125         static PBD::Signal1<bool,PBD::Controllable*> StartLearning;
126         static PBD::Signal1<void,PBD::Controllable*> StopLearning;
127
128         static PBD::Signal1<void,Controllable*> Destroyed;
129
130         PBD::Signal0<void> Changed;
131
132         int set_state (const XMLNode&, int version);
133         XMLNode& get_state ();
134
135         std::string name()      const { return _name; }
136
137         bool touching () const { return _touching; }
138         void set_touching (bool yn) { _touching = yn; }
139
140         bool is_toggle() const { return _flags & Toggle; }
141         bool is_gain_like() const { return _flags & GainLike; }
142
143         virtual double lower() const { return 0.0; }
144         virtual double upper() const { return 1.0; }
145         virtual double normal() const { return 0.0; }  //the default value
146
147         Flag flags() const { return _flags; }
148         void set_flags (Flag f);
149
150         static Controllable* by_id (const PBD::ID&);
151         static Controllable* by_name (const std::string&);
152         static const std::string xml_node_name;
153
154   private:
155         std::string _name;
156         std::string _units;
157         Flag        _flags;
158         bool        _touching;
159
160         static void add (Controllable&);
161         static void remove (Controllable*);
162
163         typedef std::set<PBD::Controllable*> Controllables;
164         static Glib::Threads::RWLock registry_lock;
165         static Controllables registry;
166 };
167
168 /* a utility class for the occasions when you need but do not have
169    a Controllable
170 */
171
172 class LIBPBD_API IgnorableControllable : public Controllable
173 {
174   public:
175         IgnorableControllable () : PBD::Controllable ("ignoreMe") {}
176         ~IgnorableControllable () {}
177
178         void set_value (double /*v*/, PBD::Controllable::GroupControlDisposition /* group_override */) {}
179         double get_value () const { return 0.0; }
180 };
181
182 }
183
184 #endif /* __pbd_controllable_h__ */