add session-scope selection ops for Stripables
[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/xml++.h"
30
31 #include "ardour/presentation_info.h"
32
33 #include "i18n.h"
34
35 using namespace ARDOUR;
36 using namespace PBD;
37 using std::string;
38
39 string PresentationInfo::state_node_name = X_("PresentationInfo");
40 PBD::Signal0<void> PresentationInfo::Change;
41 PBD::Signal0<void> PresentationInfo::SelectionChange;
42
43 namespace ARDOUR {
44         namespace Properties {
45                 PBD::PropertyDescriptor<bool>     selected;
46                 PBD::PropertyDescriptor<uint32_t> order;
47                 PBD::PropertyDescriptor<uint32_t> color;
48         }
49 }
50
51 const PresentationInfo::order_t PresentationInfo::max_order = UINT32_MAX;
52 const PresentationInfo::Flag PresentationInfo::Bus = PresentationInfo::Flag (PresentationInfo::AudioBus|PresentationInfo::MidiBus);
53 const PresentationInfo::Flag PresentationInfo::Track = PresentationInfo::Flag (PresentationInfo::AudioTrack|PresentationInfo::MidiTrack);
54 const PresentationInfo::Flag PresentationInfo::Route = PresentationInfo::Flag (PresentationInfo::Bus|PresentationInfo::Track);
55 const PresentationInfo::Flag PresentationInfo::AllRoutes = PresentationInfo::Flag (PresentationInfo::Route|PresentationInfo::MasterOut|PresentationInfo::MonitorOut);
56 const PresentationInfo::Flag PresentationInfo::AllStripables = PresentationInfo::Flag (PresentationInfo::AllRoutes|PresentationInfo::VCA);
57
58 void
59 PresentationInfo::make_property_quarks ()
60 {
61         Properties::selected.property_id = g_quark_from_static_string (X_("selected"));
62         DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for selected = %1\n",    Properties::selected.property_id));
63         Properties::color.property_id = g_quark_from_static_string (X_("color"));
64         DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for color = %1\n",       Properties::color.property_id));
65         Properties::order.property_id = g_quark_from_static_string (X_("order"));
66         DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for order = %1\n",       Properties::order.property_id));
67 }
68
69 PresentationInfo::PresentationInfo (Flag f)
70         : _order (0)
71         , _flags (Flag (f & ~OrderSet))
72         , _color (0)
73 {
74         /* OrderSet is not set */
75 }
76
77 PresentationInfo::PresentationInfo (order_t o, Flag f)
78         : _order (o)
79         , _flags (Flag (f | OrderSet))
80         , _color (0)
81 {
82         /* OrderSet is set */
83 }
84 PresentationInfo::PresentationInfo (PresentationInfo const& other)
85         : _order (other.order())
86         , _flags (other.flags())
87         , _color (other.color())
88 {
89 }
90
91 XMLNode&
92 PresentationInfo::get_state ()
93 {
94         XMLNode* node = new XMLNode (state_node_name);
95         node->add_property ("order", PBD::to_string (_order, std::dec));
96         node->add_property ("flags", enum_2_string (_flags));
97         node->add_property ("color", PBD::to_string (_color, std::dec));
98
99         return *node;
100 }
101
102 int
103 PresentationInfo::set_state (XMLNode const& node, int /* version */)
104 {
105         if (node.name() != state_node_name) {
106                 return -1;
107         }
108
109         XMLProperty const* prop;
110         PropertyChange pc;
111
112         if ((prop = node.property (X_("order"))) != 0) {
113                 order_t o = atoi (prop->value());
114                 if (o != _order) {
115                         pc.add (Properties::order);
116                         _order = o;
117                 }
118                 _order = atoi (prop->value());
119         }
120
121         if ((prop = node.property (X_("flags"))) != 0) {
122                 Flag f = Flag (string_2_enum (prop->value(), f));
123                 if ((f&Hidden) != (_flags&Hidden)) {
124                         pc.add (Properties::hidden);
125                 }
126                 _flags = f;
127         }
128
129         if ((prop = node.property (X_("color"))) != 0) {
130                 color_t c = atoi (prop->value());
131                 if (c != _color) {
132                         pc.add (Properties::color);
133                         _color = c;
134                 }
135         }
136
137         send_change (PropertyChange (pc));
138
139         return 0;
140
141 }
142
143 PresentationInfo::Flag
144 PresentationInfo::get_flags (XMLNode const& node)
145 {
146         XMLNodeList nlist = node.children ();
147
148         for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter){
149                 XMLNode* child = *niter;
150
151                 if (child->name() == PresentationInfo::state_node_name) {
152                         XMLProperty const* prop = child->property (X_("flags"));
153                         if (prop) {
154                                 Flag f = (Flag) string_2_enum (prop->value(), f);
155                                 return f;
156                         }
157                 }
158         }
159         return Flag (0);
160 }
161
162 void
163 PresentationInfo::set_color (PresentationInfo::color_t c)
164 {
165         if (c != _color) {
166                 _color = c;
167                 send_change (PropertyChange (Properties::color));
168         }
169 }
170
171 bool
172 PresentationInfo::color_set () const
173 {
174         /* all RGBA values zero? not set.
175          *
176          * this is heuristic, but it is fairly realistic. who will ever set
177          * a color to completely transparent black? only the constructor ..
178          */
179         return _color != 0;
180 }
181
182 void
183 PresentationInfo::set_selected (bool yn)
184 {
185         if (yn != selected()) {
186                 if (yn) {
187                         _flags = Flag (_flags | Selected);
188                 } else {
189                         _flags = Flag (_flags & ~Selected);
190                 }
191                 send_change (PropertyChange (Properties::selected));
192         }
193 }
194
195 void
196 PresentationInfo::set_hidden (bool yn)
197 {
198         if (yn != hidden()) {
199
200                 if (yn) {
201                         _flags = Flag (_flags | Hidden);
202                 } else {
203                         _flags = Flag (_flags & ~Hidden);
204                 }
205
206                 send_change (PropertyChange (Properties::hidden));
207         }
208 }
209
210 void
211 PresentationInfo::set_order (order_t order)
212 {
213         _flags = Flag (_flags|OrderSet);
214
215         if (order != _order) {
216                 _order = order;
217                 send_change (PropertyChange (Properties::order));
218         }
219 }
220
221 PresentationInfo&
222 PresentationInfo::operator= (PresentationInfo const& other)
223 {
224         if (this != &other) {
225                 _order = other.order();
226                 _flags = other.flags();
227                 _color = other.color();
228         }
229
230         return *this;
231 }
232
233 std::ostream&
234 operator<<(std::ostream& o, ARDOUR::PresentationInfo const& pi)
235 {
236         return o << pi.order() << '/' << enum_2_string (pi.flags()) << '/' << pi.color();
237 }