introduce GUIObjectState; massive, pervasive changes in visibility and height managem...
[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 #if 0
53     void operator() (const double& d) {
54             stream << std::setprecision (12) << d;
55     }
56 #endif
57
58     void operator() (const std::string& s) {
59             stream << s;
60     }
61
62   private:
63     std::ostream& stream;
64 };
65
66 XMLNode&
67 GUIObjectState::get_state () const
68 {
69         XMLNode* root = new XMLNode (xml_node_name);
70         
71         for (StringPropertyMap::const_iterator i = _property_maps.begin(); i != _property_maps.end(); ++i) {
72
73                 const PropertyMap& pmap (i->second);
74                 XMLNode* id_node = new XMLNode (X_("Object"));
75                 
76                 id_node->add_property ("id", i->first);
77                 
78                 for (PropertyMap::const_iterator p = pmap.begin(); p != pmap.end(); ++p) {
79                         std::stringstream ss;
80                         gos_string_vistor gsv (ss);
81                         boost::apply_visitor (gsv, p->second);
82                         id_node->add_property (p->first.c_str(), ss.str());
83                 }
84                 
85                 root->add_child_nocopy (*id_node);
86         }
87
88
89         return *root;
90 }
91
92 int
93 GUIObjectState::set_state (const XMLNode& node)
94 {
95         if (node.name() != xml_node_name) {
96                 return -1;
97         }
98         
99         clear_maps ();
100         
101         for (XMLNodeList::const_iterator i = node.children().begin(); i != node.children().end(); ++i) {
102                 if ((*i)->name() == X_("Object")) {
103
104                         XMLNode* child = (*i);
105                         const XMLProperty* idp = child->property (X_("id"));
106
107                         if (!idp) {
108                                 continue;
109                         }
110
111                         string id (idp->value());
112                         
113                         for (XMLPropertyList::const_iterator p = child->properties().begin(); p != child->properties().end(); ++p) {
114                                 /* note that this always sets the property with
115                                    a string value, and so is not equivalent to
116                                    a call made by the program that passed a
117                                    scalar.
118                                 */
119                                 if ((*p)->name() != X_("id")) {
120                                         set (id, (*p)->name(), (*p)->value());
121                                 }
122                         }
123                 }
124         }
125
126         return 0;
127 }
128
129
130 void
131 GUIObjectState::load (const XMLNode& node)
132 {
133         (void) set_state (node);
134 }
135
136 GUIObjectState&
137 GUIObjectState::operator= (const GUIObjectState& other)
138 {
139         _property_maps = other._property_maps;
140
141         return *this;
142 }