provide a heuristic to decide if the color has been set for a PresentationInfo object
[ardour.git] / libs / ardour / ardour / presentation_info.h
1 /*
2     Copyright (C) 2016 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 __libardour_presentation_info_h__
21 #define __libardour_presentation_info_h__
22
23 #include <iostream>
24 #include <string>
25
26 #include <stdint.h>
27
28 #include "pbd/signals.h"
29 #include "pbd/stateful.h"
30 #include "pbd/properties.h"
31
32 #include "ardour/libardour_visibility.h"
33
34 class XMLNode;
35
36 namespace ARDOUR {
37
38 namespace Properties {
39         LIBARDOUR_API extern PBD::PropertyDescriptor<uint32_t> order;
40         LIBARDOUR_API extern PBD::PropertyDescriptor<uint32_t> color;
41         LIBARDOUR_API extern PBD::PropertyDescriptor<bool> selected;
42         /* we use this; declared in region.cc */
43         LIBARDOUR_API extern PBD::PropertyDescriptor<bool> hidden;
44 }
45
46 class LIBARDOUR_API PresentationInfo : public PBD::Stateful
47 {
48   public:
49
50         /* a PresentationInfo object exists to share information between
51          * different user interfaces (e.g. GUI and a Mackie Control surface)
52          * about:
53          *
54          *     - ordering
55          *     - selection status
56          *     - visibility
57          *     - object identity
58          *
59          * ORDERING
60          *
61          * One UI takes control of ordering by setting the "order" value for
62          * the PresentationInfo component of every Stripable object. In Ardour,
63          * this is done by the GUI (mostly because it is very hard for the user
64          * to re-order things on a control surface).
65          *
66          * Ordering is a complex beast, however. Different user interfaces may
67          * display things in different ways. For example, the GUI of Ardour
68          * allows the user to mix busses in between tracks. A control surface
69          * may do the same, but may also allow the user to press a button that
70          * makes it show only busses, or only MIDI tracks. At that point, the
71          * ordering on the surface differs from the ordering in the GUI.
72          *
73          * There are several pathways for the order being set:
74          *
75          *   - object created during session loading from XML
76          *           - numeric order will be set during ::set_state(), based on
77          *           - type will be set during ctor call
78          *
79          *   - object created in response to user request
80          *              - numeric order will be set by Session, before adding
81          *                   to container.
82          *              - type set during ctor call
83          *
84          *
85          * OBJECT IDENTITY
86          *
87          * Control surfaces/protocols often need to be able to get a handle on
88          * an object identified only abstractly, such as the "5th audio track"
89          * or "the master out". A PresentationInfo object uniquely identifies
90          * all objects in this way through the combination of its _order member
91          * and part of its _flags member. The _flags member identifies the type
92          * of object, as well as selection/hidden status. The type may never
93          * change after construction (not strictly the constructor itself, but
94          * a more generalized notion of construction, as in "ready to use").
95          *
96          * SELECTION
97          *
98          * When an object is selected, its _flags member will have the Selected
99          * bit set.
100          *
101          * VISIBILITY
102          *
103          * When an object is hidden, its _flags member will have the Hidden
104          * bit set.
105          *
106          *
107          */
108
109         enum Flag {
110                 /* Type information */
111                 AudioTrack = 0x1,
112                 MidiTrack = 0x2,
113                 AudioBus = 0x4,
114                 MidiBus = 0x8,
115                 VCA = 0x10,
116                 MasterOut = 0x20,
117                 MonitorOut = 0x40,
118                 Auditioner = 0x80,
119                 /* These are for sharing Stripable states between the GUI and other
120                  * user interfaces/control surfaces
121                  */
122                 Selected = 0x100,
123                 Hidden = 0x200,
124                 /* single bit indicates that the group order is set */
125                 OrderSet = 0x400,
126
127                 /* special mask to delect out "state" bits */
128                 StatusMask = (Selected|Hidden)
129         };
130
131         static const Flag AllRoutes; /* mask to use for any route include master+monitor, but not auditioner */
132         static const Flag Route;     /* mask for any route (bus or track */
133         static const Flag Track;     /* mask to use for any track */
134         static const Flag Bus;       /* mask to use for any bus */
135
136         typedef uint32_t order_t;
137         typedef uint32_t color_t;
138
139         PresentationInfo (Flag f);
140         PresentationInfo (order_t o, Flag f);
141         PresentationInfo (PresentationInfo const &);
142
143         static const order_t max_order;
144
145         order_t  order() const { return _order; }
146         color_t  color() const { return _color; }
147
148         bool color_set() const;
149
150         void set_color (color_t);
151         void set_selected (bool yn);
152         void set_hidden (bool yn);
153
154         PresentationInfo::Flag flags() const { return _flags; }
155
156         bool order_set() const { return _flags & OrderSet; }
157
158         bool hidden() const { return _flags & Hidden; }
159         bool selected() const { return _flags & Selected; }
160         bool special() const { return _flags & (MasterOut|MonitorOut|Auditioner); }
161
162         void set_flag (PresentationInfo::Flag f) {
163                 _flags = PresentationInfo::Flag (_flags | f);
164         }
165
166         void unset_flag (PresentationInfo::Flag f) {
167                 _flags = PresentationInfo::Flag (_flags & ~f);
168         }
169
170         void set_flags (Flag f) {
171                 _flags = f;
172         }
173
174         bool flag_match (Flag f) const {
175                 /* no flags, match all */
176
177                 if (f == Flag (0)) {
178                         return true;
179                 }
180
181                 if (f & StatusMask) {
182                         /* status bits set, must match them */
183                         if ((_flags & StatusMask) != (f & StatusMask)) {
184                                 return false;
185                         }
186                 }
187
188                 /* Generic flags in f, match the right stuff */
189
190                 if (f == Bus && (_flags & Bus)) {
191                         /* some kind of bus */
192                         return true;
193                 }
194                 if (f == Track && (_flags & Track)) {
195                         /* some kind of track */
196                         return true;
197                 }
198                 if (f == Route && (_flags & Route)) {
199                         /* any kind of route, but not master, monitor in
200                            or auditioner.
201                          */
202                         return true;
203                 }
204
205                 if (f == AllRoutes && (_flags & AllRoutes)) {
206                         /* any kind of route, but not auditioner. Ask for that
207                            specifically.
208                         */
209                         return true;
210                 }
211
212                 /* compare without status mask - we already checked that above 
213                  */
214
215                 return (f &~ (StatusMask|OrderSet)) == (_flags &~ (StatusMask|OrderSet));
216         }
217
218         int set_state (XMLNode const&, int);
219         XMLNode& get_state ();
220
221         bool operator< (PresentationInfo const& other) const {
222                 return order() < other.order();
223         }
224
225         bool match (PresentationInfo const& other) const {
226                 return (_order == other.order()) && flag_match (other.flags());
227         }
228
229         bool operator==(PresentationInfo const& other) {
230                 return (_order == other.order()) && (_flags == other.flags());
231         }
232
233         bool operator!=(PresentationInfo const& other) {
234                 return (_order != other.order()) || (_flags != other.flags());
235         }
236
237         PresentationInfo& operator= (PresentationInfo const& other);
238
239         static Flag get_flags (XMLNode const& node);
240         static std::string state_node_name;
241
242         /* for things concerned about *any* PresentationInfo. This is emitted
243          * only at the request of another object that has finished making some
244          * changes (e.g. reordering things)
245          */
246
247         static PBD::Signal0<void> Change;
248
249         static void make_property_quarks ();
250
251   protected:
252         friend class Stripable;
253         void set_order (order_t order);
254
255   private:
256         order_t _order;
257         Flag    _flags;
258         color_t _color;
259 };
260
261 }
262
263 std::ostream& operator<<(std::ostream& o, ARDOUR::PresentationInfo const& rid);
264
265 #endif /* __libardour_presentation_info_h__ */