Fix non visibility of previously-visible MIDI automation
[ardour.git] / gtk2_ardour / gui_object.cc
1 /*
2     Copyright (C) 2011 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 #include <iostream>
21 #include <iomanip>
22 #include <sstream>
23
24 #include <boost/variant/static_visitor.hpp>
25
26 #include "gui_object.h"
27 #include "i18n.h"
28
29 using std::string;
30
31 const string GUIObjectState::xml_node_name (X_("GUIObjectState"));
32
33 GUIObjectState::~GUIObjectState ()
34 {
35         clear_maps ();
36 }
37
38 void
39 GUIObjectState::clear_maps ()
40 {
41         _property_maps.clear ();
42 }
43
44 class gos_string_vistor : public boost::static_visitor<> {
45   public:
46     gos_string_vistor (std::ostream& o) 
47             : stream (o) {}
48             
49     void operator() (const int64_t& i) {
50             stream << i;
51     }
52
53     void operator() (const std::string& s) {
54             stream << s;
55     }
56
57   private:
58     std::ostream& stream;
59 };
60
61 std::string 
62 GUIObjectState::get_string (const std::string& id, const std::string& prop_name, bool* empty)
63 {
64         StringPropertyMap::iterator i = _property_maps.find (id);
65         
66         if (i == _property_maps.end()) {
67                 if (empty) {
68                         *empty = true;
69                 }
70                 return string();
71         }
72         
73         const PropertyMap& pmap (i->second);
74         PropertyMap::const_iterator p = pmap.find (prop_name);
75         
76         if (p == pmap.end()) {
77                 if (empty) {
78                         *empty = true;
79                 }
80                 return string();
81         }
82         
83         std::stringstream ss;
84         gos_string_vistor gsv (ss);
85
86         boost::apply_visitor (gsv, p->second);
87
88         if (empty) {
89                 *empty = false;
90         }
91         
92         return ss.str ();
93 }
94
95 XMLNode&
96 GUIObjectState::get_state () const
97 {
98         XMLNode* root = new XMLNode (xml_node_name);
99         
100         for (StringPropertyMap::const_iterator i = _property_maps.begin(); i != _property_maps.end(); ++i) {
101
102                 const PropertyMap& pmap (i->second);
103                 XMLNode* id_node = new XMLNode (X_("Object"));
104                 
105                 id_node->add_property ("id", i->first);
106                 
107                 for (PropertyMap::const_iterator p = pmap.begin(); p != pmap.end(); ++p) {
108                         std::stringstream ss;
109                         gos_string_vistor gsv (ss);
110                         boost::apply_visitor (gsv, p->second);
111                         id_node->add_property (p->first.c_str(), ss.str());
112                 }
113                 
114                 root->add_child_nocopy (*id_node);
115         }
116
117
118         return *root;
119 }
120
121 int
122 GUIObjectState::set_state (const XMLNode& node)
123 {
124         if (node.name() != xml_node_name) {
125                 return -1;
126         }
127         
128         clear_maps ();
129         
130         for (XMLNodeList::const_iterator i = node.children().begin(); i != node.children().end(); ++i) {
131                 if ((*i)->name() == X_("Object")) {
132
133                         XMLNode* child = (*i);
134                         const XMLProperty* idp = child->property (X_("id"));
135
136                         if (!idp) {
137                                 continue;
138                         }
139
140                         string id (idp->value());
141                         
142                         for (XMLPropertyList::const_iterator p = child->properties().begin(); p != child->properties().end(); ++p) {
143                                 /* note that this always sets the property with
144                                    a string value, and so is not equivalent to
145                                    a call made by the program that passed a
146                                    scalar.
147                                 */
148                                 if ((*p)->name() != X_("id")) {
149                                         set (id, (*p)->name(), (*p)->value());
150                                 }
151                         }
152                 }
153         }
154
155         return 0;
156 }
157
158
159 void
160 GUIObjectState::load (const XMLNode& node)
161 {
162         (void) set_state (node);
163 }
164
165 GUIObjectState&
166 GUIObjectState::operator= (const GUIObjectState& other)
167 {
168         _property_maps = other._property_maps;
169
170         return *this;
171 }
172
173 /** @return begin iterator into our StringPropertyMap */
174 GUIObjectState::StringPropertyMap::const_iterator
175 GUIObjectState::begin () const
176 {
177         return _property_maps.begin ();
178 }
179
180 /** @return end iterator into our StringPropertyMap */
181 GUIObjectState::StringPropertyMap::const_iterator
182 GUIObjectState::end () const
183 {
184         return _property_maps.end ();
185 }
186