selected Stripables now have a counter to indicate the order they were selected in
[ardour.git] / libs / ardour / presentation_info.cc
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 #include <sstream>
20 #include <typeinfo>
21
22 #include <cassert>
23
24 #include "pbd/convert.h"
25 #include "pbd/debug.h"
26 #include "pbd/enumwriter.h"
27 #include "pbd/error.h"
28 #include "pbd/failed_constructor.h"
29 #include "pbd/stacktrace.h"
30 #include "pbd/xml++.h"
31
32 #include "ardour/presentation_info.h"
33
34 #include "pbd/i18n.h"
35
36 using namespace ARDOUR;
37 using namespace PBD;
38 using std::string;
39
40 string PresentationInfo::state_node_name = X_("PresentationInfo");
41
42 PBD::Signal1<void,PropertyChange const &> PresentationInfo::Change;
43 Glib::Threads::Mutex PresentationInfo::static_signal_lock;
44 int PresentationInfo::_change_signal_suspended = 0;
45 PBD::PropertyChange PresentationInfo::_pending_static_changes;
46 int PresentationInfo::selection_counter = 0;
47
48 namespace ARDOUR {
49         namespace Properties {
50                 PBD::PropertyDescriptor<bool>     selected;
51                 PBD::PropertyDescriptor<uint32_t> order;
52                 PBD::PropertyDescriptor<uint32_t> color;
53         }
54 }
55
56 void
57 PresentationInfo::suspend_change_signal ()
58 {
59         g_atomic_int_add (&_change_signal_suspended, 1);
60 }
61
62 void
63 PresentationInfo::unsuspend_change_signal ()
64 {
65         Glib::Threads::Mutex::Lock lm (static_signal_lock);
66
67         if (g_atomic_int_get (const_cast<gint*> (&_change_signal_suspended)) == 1) {
68
69                 /* atomically grab currently pending flags */
70
71                 PropertyChange pc = _pending_static_changes;
72                 _pending_static_changes.clear ();
73
74                 if (!pc.empty()) {
75
76                         /* emit the signal with further emissions still blocked
77                          * by _change_signal_suspended, but not by the lock.
78                          *
79                          * This means that if the handlers modify other PI
80                          * states, the signal for that won't be sent while they
81                          * are handling the current signal.
82                          */
83                         lm.release ();
84                         Change (pc); /* EMIT SIGNAL */
85                         lm.acquire ();
86                 }
87         }
88
89         g_atomic_int_add (const_cast<gint*>(&_change_signal_suspended), -1);
90 }
91
92 void
93 PresentationInfo::send_static_change (const PropertyChange& what_changed)
94 {
95         if (what_changed.empty()) {
96                 return;
97         }
98
99
100         if (g_atomic_int_get (&_change_signal_suspended)) {
101                 Glib::Threads::Mutex::Lock lm (static_signal_lock);
102                 _pending_static_changes.add (what_changed);
103                 return;
104         }
105
106         Change (what_changed);
107 }
108
109 const PresentationInfo::order_t PresentationInfo::max_order = UINT32_MAX;
110 const PresentationInfo::Flag PresentationInfo::Bus = PresentationInfo::Flag (PresentationInfo::AudioBus|PresentationInfo::MidiBus);
111 const PresentationInfo::Flag PresentationInfo::Track = PresentationInfo::Flag (PresentationInfo::AudioTrack|PresentationInfo::MidiTrack);
112 const PresentationInfo::Flag PresentationInfo::Route = PresentationInfo::Flag (PresentationInfo::Bus|PresentationInfo::Track);
113 const PresentationInfo::Flag PresentationInfo::AllRoutes = PresentationInfo::Flag (PresentationInfo::Route|PresentationInfo::MasterOut|PresentationInfo::MonitorOut);
114 const PresentationInfo::Flag PresentationInfo::AllStripables = PresentationInfo::Flag (PresentationInfo::AllRoutes|PresentationInfo::VCA);
115
116 void
117 PresentationInfo::make_property_quarks ()
118 {
119         Properties::selected.property_id = g_quark_from_static_string (X_("selected"));
120         DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for selected = %1\n",    Properties::selected.property_id));
121         Properties::color.property_id = g_quark_from_static_string (X_("color"));
122         DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for color = %1\n",       Properties::color.property_id));
123         Properties::order.property_id = g_quark_from_static_string (X_("order"));
124         DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for order = %1\n",       Properties::order.property_id));
125 }
126
127 PresentationInfo::PresentationInfo (Flag f)
128         : _order (0)
129         , _flags (Flag (f & ~OrderSet))
130         , _color (0)
131 {
132         /* OrderSet is not set */
133 }
134
135 PresentationInfo::PresentationInfo (order_t o, Flag f)
136         : _order (o)
137         , _flags (Flag (f | OrderSet))
138         , _color (0)
139 {
140         /* OrderSet is set */
141 }
142 PresentationInfo::PresentationInfo (PresentationInfo const& other)
143         : _order (other.order())
144         , _flags (other.flags())
145         , _color (other.color())
146 {
147 }
148
149 XMLNode&
150 PresentationInfo::get_state ()
151 {
152         XMLNode* node = new XMLNode (state_node_name);
153         node->add_property ("order", PBD::to_string (_order, std::dec));
154         node->add_property ("flags", enum_2_string (_flags));
155         node->add_property ("color", PBD::to_string (_color, std::dec));
156
157         return *node;
158 }
159
160 int
161 PresentationInfo::set_state (XMLNode const& node, int /* version */)
162 {
163         if (node.name() != state_node_name) {
164                 return -1;
165         }
166
167         XMLProperty const* prop;
168         PropertyChange pc;
169
170         if ((prop = node.property (X_("order"))) != 0) {
171                 order_t o = atoi (prop->value());
172                 if (o != _order) {
173                         pc.add (Properties::order);
174                         _order = o;
175                 }
176                 _order = atoi (prop->value());
177         }
178
179         if ((prop = node.property (X_("flags"))) != 0) {
180                 Flag f = Flag (string_2_enum (prop->value(), f));
181                 if ((f&Hidden) != (_flags&Hidden)) {
182                         pc.add (Properties::hidden);
183                 }
184                 _flags = f;
185         }
186
187         if ((prop = node.property (X_("color"))) != 0) {
188                 color_t c = atoi (prop->value());
189                 if (c != _color) {
190                         pc.add (Properties::color);
191                         _color = c;
192                 }
193         }
194
195         send_change (PropertyChange (pc));
196
197         return 0;
198
199 }
200
201 PresentationInfo::Flag
202 PresentationInfo::get_flags (XMLNode const& node)
203 {
204         XMLNodeList nlist = node.children ();
205
206         for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter){
207                 XMLNode* child = *niter;
208
209                 if (child->name() == PresentationInfo::state_node_name) {
210                         XMLProperty const* prop = child->property (X_("flags"));
211                         if (prop) {
212                                 Flag f = (Flag) string_2_enum (prop->value(), f);
213                                 return f;
214                         }
215                 }
216         }
217         return Flag (0);
218 }
219
220 void
221 PresentationInfo::set_color (PresentationInfo::color_t c)
222 {
223         if (c != _color) {
224                 _color = c;
225                 send_change (PropertyChange (Properties::color));
226                 send_static_change (PropertyChange (Properties::color));
227         }
228 }
229
230 bool
231 PresentationInfo::color_set () const
232 {
233         /* all RGBA values zero? not set.
234          *
235          * this is heuristic, but it is fairly realistic. who will ever set
236          * a color to completely transparent black? only the constructor ..
237          */
238         return _color != 0;
239 }
240
241 void
242 PresentationInfo::set_selected (bool yn)
243 {
244         if (yn != selected()) {
245                 if (yn) {
246                         _flags = Flag (_flags | Selected);
247                         _selection_cnt = g_atomic_int_add (&selection_counter, 1);
248                 } else {
249                         _flags = Flag (_flags & ~Selected);
250                         _selection_cnt = 0;
251                 }
252                 send_change (PropertyChange (Properties::selected));
253                 send_static_change (PropertyChange (Properties::selected));
254         }
255 }
256
257 void
258 PresentationInfo::set_hidden (bool yn)
259 {
260         if (yn != hidden()) {
261
262                 if (yn) {
263                         _flags = Flag (_flags | Hidden);
264                 } else {
265                         _flags = Flag (_flags & ~Hidden);
266                 }
267
268                 send_change (PropertyChange (Properties::hidden));
269                 send_static_change (PropertyChange (Properties::hidden));
270         }
271 }
272
273 void
274 PresentationInfo::set_order (order_t order)
275 {
276         _flags = Flag (_flags|OrderSet);
277
278         if (order != _order) {
279                 _order = order;
280                 send_change (PropertyChange (Properties::order));
281                 send_static_change (PropertyChange (Properties::order));
282         }
283 }
284
285 PresentationInfo&
286 PresentationInfo::operator= (PresentationInfo const& other)
287 {
288         if (this != &other) {
289                 _order = other.order();
290                 _flags = other.flags();
291                 _color = other.color();
292         }
293
294         return *this;
295 }
296
297 std::ostream&
298 operator<<(std::ostream& o, ARDOUR::PresentationInfo const& pi)
299 {
300         return o << pi.order() << '/' << enum_2_string (pi.flags()) << '/' << pi.color();
301 }