Merge branch 'noppc' of https://github.com/mojofunk/ardour into cairocanvas
[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 class XMLNode;
34
35 namespace PBD {
36
37 class LIBPBD_API Controllable : public PBD::StatefulDestructible {
38   public:
39         enum Flag {
40                 Toggle = 0x1,
41                 GainLike = 0x2,
42         };
43
44         Controllable (const std::string& name, Flag f = Flag (0));
45         virtual ~Controllable() { Destroyed (this); }
46
47         /* We express Controllable values in one of three ways:
48          * 1. `user' --- as presented to the user (e.g. dB, Hz, etc.)
49          * 2. `interface' --- as used in some cases for the UI representation
50          * (in order to make controls behave logarithmically).
51          * 3. `internal' --- as passed to a processor, track, plugin, or whatever.
52          *
53          * Note that in some cases user and processor may be the same
54          * (and interface different) e.g. frequency, which is presented
55          * to the user and passed to the processor in linear terms, but
56          * which needs log scaling in the interface.
57          *
58          * In other cases, user and interface may be the same (and processor different)
59          * e.g. gain, which is presented to the user in log terms (dB)
60          * but passed to the processor as a linear quantity.
61          */
62
63         /** Set `internal' value */
64         virtual void set_value (double) = 0;
65         /** @return `internal' value */
66         virtual double get_value (void) const = 0;
67
68         PBD::Signal0<void> LearningFinished;
69         static PBD::Signal3<void,PBD::Controllable*,int,int> CreateBinding;
70         static PBD::Signal1<void,PBD::Controllable*> DeleteBinding;
71
72         static PBD::Signal1<bool,PBD::Controllable*> StartLearning;
73         static PBD::Signal1<void,PBD::Controllable*> StopLearning;
74
75         static PBD::Signal1<void,Controllable*> Destroyed;
76         
77         PBD::Signal0<void> Changed;
78
79         int set_state (const XMLNode&, int version);
80         XMLNode& get_state ();
81
82         std::string name()      const { return _name; }
83
84         bool touching () const { return _touching; }
85         void set_touching (bool yn) { _touching = yn; }
86
87         bool is_toggle() const { return _flags & Toggle; }
88         bool is_gain_like() const { return _flags & GainLike; }
89
90         virtual double lower() const { return 0.0; }
91         virtual double upper() const { return 1.0; }
92
93         Flag flags() const { return _flags; }
94         void set_flags (Flag f);
95
96         static Controllable* by_id (const PBD::ID&);
97         static Controllable* by_name (const std::string&);
98         static const std::string xml_node_name;
99   private:
100         std::string _name;
101
102         Flag        _flags;
103         bool        _touching;
104
105         static void add (Controllable&);
106         static void remove (Controllable*);
107
108         typedef std::set<PBD::Controllable*> Controllables;
109         static Glib::Threads::RWLock registry_lock;
110         static Controllables registry;
111 };
112
113 /* a utility class for the occasions when you need but do not have
114    a Controllable
115 */
116
117 class LIBPBD_API IgnorableControllable : public Controllable 
118 {
119   public: 
120         IgnorableControllable () : PBD::Controllable ("ignoreMe") {}
121         ~IgnorableControllable () {}
122     
123         void set_value (double /*v*/) {}
124         double get_value () const { return 0.0; }
125 };
126
127 }
128
129 #endif /* __pbd_controllable_h__ */