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 __pbd_controllable_h__
21 #define __pbd_controllable_h__
27 #include "pbd/libpbd_visibility.h"
28 #include "pbd/signals.h"
29 #include <glibmm/threads.h>
31 #include "pbd/statefuldestructible.h"
40 /** This is a pure virtual class to represent a scalar control.
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.
46 * In essence, this is an interface, not a class.
48 * Without overriding upper() and lower(), a derived class will function
49 * as a control whose value can range between 0 and 1.0.
52 class LIBPBD_API Controllable : public PBD::StatefulDestructible {
61 Controllable (const std::string& name, Flag f = Flag (0));
62 virtual ~Controllable() { Destroyed (this); }
64 /* We express Controllable values in one of three ways:
65 * 1. `user' --- as presented to the user (e.g. dB, Hz, etc.)
66 * 2. `interface' --- as used in some cases for the UI representation
67 * (in order to make controls behave logarithmically).
68 * 3. `internal' --- as passed to a processor, track, plugin, or whatever.
70 * Note that in some cases user and internal may be the same
71 * (and interface different) e.g. frequency, which is presented
72 * to the user and passed to the processor in linear terms, but
73 * which needs log scaling in the interface.
75 * In other cases, user and interface may be the same (and internal different)
76 * e.g. gain, which is presented to the user in log terms (dB)
77 * but passed to the processor as a linear quantity.
80 /* Within an application, various Controllables might be considered to
81 * be "grouped" in a way that implies that setting 1 of them also
82 * modifies others in the group.
85 enum GroupControlDisposition {
86 InverseGroup, /* set all controls in the same "group" as this one */
87 NoGroup, /* set only this control */
88 UseGroup, /* use group settings to decide which group controls are altered */
89 ForGroup /* this setting is being done *for* the group
90 (i.e. UseGroup was set in the callchain
95 /** Get and Set `internal' value
97 * All derived classes must implement this.
99 * Basic derived classes will ignore @param group_override,
100 * but more sophisticated children, notably those that
101 * proxy the value setting logic via an object that is aware of group
102 * relationships between this control and others, will find it useful.
104 virtual void set_value (double, GroupControlDisposition group_override) = 0;
105 virtual double get_value (void) const = 0;
107 /** This is used when saving state. By default it just calls
108 * get_value(), but a class with more complex semantics might override
109 * this to save some value that differs from what get_value() would
112 virtual double get_save_value () const { return get_value(); }
114 /** Conversions between `internal', 'interface', and 'user' values */
115 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
116 virtual double interface_to_internal (double i) const {return lower() + i*(upper() - lower());}
118 /** Get and Set `interface' value (typically, fraction of knob travel) */
119 virtual float get_interface() const { return (internal_to_interface(get_value())); }
120 virtual void set_interface (float fraction) { fraction = min( max(0.0f, fraction), 1.0f); set_value(interface_to_internal(fraction), NoGroup); }
122 virtual std::string get_user_string() const { return std::string(); }
124 PBD::Signal0<void> LearningFinished;
125 static PBD::Signal3<void,PBD::Controllable*,int,int> CreateBinding;
126 static PBD::Signal1<void,PBD::Controllable*> DeleteBinding;
128 static PBD::Signal1<bool,PBD::Controllable*> StartLearning;
129 static PBD::Signal1<void,PBD::Controllable*> StopLearning;
131 static PBD::Signal1<void,Controllable*> Destroyed;
133 static PBD::Signal1<void, boost::weak_ptr<PBD::Controllable> > GUIFocusChanged;
135 PBD::Signal2<void,bool,PBD::Controllable::GroupControlDisposition> Changed;
137 int set_state (const XMLNode&, int version);
138 virtual XMLNode& get_state ();
140 std::string name() const { return _name; }
142 bool touching () const { return _touching; }
144 bool is_toggle() const { return _flags & Toggle; }
145 bool is_gain_like() const { return _flags & GainLike; }
147 virtual double lower() const { return 0.0; }
148 virtual double upper() const { return 1.0; }
149 virtual double normal() const { return 0.0; } //the default value
151 Flag flags() const { return _flags; }
152 void set_flags (Flag f);
154 static Controllable* by_id (const PBD::ID&);
155 static Controllable* by_name (const std::string&);
156 static const std::string xml_node_name;
159 void set_touching (bool yn) { _touching = yn; }
168 static void add (Controllable&);
169 static void remove (Controllable*);
171 typedef std::set<PBD::Controllable*> Controllables;
172 static Glib::Threads::RWLock registry_lock;
173 static Controllables registry;
176 /* a utility class for the occasions when you need but do not have
180 class LIBPBD_API IgnorableControllable : public Controllable
183 IgnorableControllable () : PBD::Controllable ("ignoreMe") {}
184 ~IgnorableControllable () {}
186 void set_value (double /*v*/, PBD::Controllable::GroupControlDisposition /* group_override */) {}
187 double get_value () const { return 0.0; }
192 #endif /* __pbd_controllable_h__ */