864a68b1a8f4c2ef2eef446f1d901c771c2576ec
[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 v    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/signals.h"
28 #include <glibmm/thread.h>
29
30 #include "pbd/statefuldestructible.h"
31
32 class XMLNode;
33
34 namespace PBD {
35
36 class Controllable : public PBD::StatefulDestructible {
37   public:
38         enum Flag {
39                 Toggle = 0x1,
40                 Discrete = 0x2,
41                 GainLike = 0x4,
42                 IntegerOnly = 0x8
43         };
44
45         Controllable (const std::string& name, Flag f = Flag (0));
46         virtual ~Controllable() { Destroyed (this); }
47
48         /* We express Controllable values in one of three ways:
49          * 1. `user' --- as presented to the user (e.g. dB, Hz etc.)
50          * 2. `UI' --- as used in some cases for the internal representation
51          *    of the UI.  This may be the same as `user', or may be something
52          *    like the natural log of frequency in order that sliders operate
53          *    in a logarithmic fashion.
54          * 3. `plugin' --- as passed to a plugin.
55          */
56
57         /** Set `user' value */
58         virtual void set_value (double) = 0;
59         /** @return `user' value */
60         virtual double get_value (void) const = 0;
61
62         PBD::Signal0<void> LearningFinished;
63         static PBD::Signal3<void,PBD::Controllable*,int,int> CreateBinding;
64         static PBD::Signal1<void,PBD::Controllable*> DeleteBinding;
65
66         static PBD::Signal1<bool,PBD::Controllable*> StartLearning;
67         static PBD::Signal1<void,PBD::Controllable*> StopLearning;
68
69         static PBD::Signal1<void,Controllable*> Destroyed;
70         
71         PBD::Signal0<void> Changed;
72
73         int set_state (const XMLNode&, int version);
74         XMLNode& get_state ();
75
76         std::string name()      const { return _name; }
77
78         bool touching () const { return _touching; }
79         void set_touching (bool yn) { _touching = yn; }
80
81         bool is_toggle() const { return _flags & Toggle; }
82         bool is_discrete() const { return _flags & Discrete; }
83         bool is_gain_like() const { return _flags & GainLike; }
84         bool is_integral_only() const { return _flags & IntegerOnly; }
85
86         virtual double lower() const { return 0.0; }
87         virtual double upper() const { return 1.0; }
88
89         Flag flags() const { return _flags; }
90         void set_flags (Flag f);
91
92         virtual uint32_t get_discrete_values (std::list<float>&) { return 0; /* no values returned */ }
93
94         static Controllable* by_id (const PBD::ID&);
95         static Controllable* by_name (const std::string&);
96         static const std::string xml_node_name;
97   private:
98         std::string _name;
99
100         Flag        _flags;
101         bool        _touching;
102
103         static void add (Controllable&);
104         static void remove (Controllable*);
105
106         typedef std::set<PBD::Controllable*> Controllables;
107         static Glib::StaticRWLock registry_lock;
108         static Controllables registry;
109 };
110
111 /* a utility class for the occasions when you need but do not have
112    a Controllable
113 */
114
115 class IgnorableControllable : public Controllable 
116 {
117   public: 
118         IgnorableControllable () : PBD::Controllable ("ignoreMe") {}
119         ~IgnorableControllable () {}
120     
121         void set_value (double /*v*/) {}
122         double get_value () const { return 0.0; }
123 };
124
125 }
126
127 #endif /* __pbd_controllable_h__ */