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