various fixes to MidiRegionView selection handling, key handling, drawing of ghost...
[ardour2.git] / gtk2_ardour / gui_object.cc
blobfa708b25ad4ffb4319e81b44713450eb1f327de7
1 /*
2 Copyright (C) 2011 Paul Davis
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.
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.
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.
20 #include <iostream>
21 #include <iomanip>
22 #include <sstream>
24 #include <boost/variant/static_visitor.hpp>
26 #include "gui_object.h"
27 #include "i18n.h"
29 using std::string;
31 const string GUIObjectState::xml_node_name (X_("GUIObjectState"));
33 GUIObjectState::~GUIObjectState ()
35 clear_maps ();
38 void
39 GUIObjectState::clear_maps ()
41 _property_maps.clear ();
44 class gos_string_vistor : public boost::static_visitor<> {
45 public:
46 gos_string_vistor (std::ostream& o)
47 : stream (o) {}
49 void operator() (const int64_t& i) {
50 stream << i;
53 void operator() (const std::string& s) {
54 stream << s;
57 private:
58 std::ostream& stream;
61 std::string
62 GUIObjectState::get_string (const std::string& id, const std::string& prop_name, bool* empty)
64 StringPropertyMap::iterator i = _property_maps.find (id);
66 if (i == _property_maps.end()) {
67 if (empty) {
68 *empty = true;
70 return string();
73 const PropertyMap& pmap (i->second);
74 PropertyMap::const_iterator p = pmap.find (prop_name);
76 if (p == pmap.end()) {
77 if (empty) {
78 *empty = true;
80 return string();
83 std::stringstream ss;
84 gos_string_vistor gsv (ss);
86 boost::apply_visitor (gsv, p->second);
88 if (empty) {
89 *empty = false;
92 return ss.str ();
95 XMLNode&
96 GUIObjectState::get_state () const
98 XMLNode* root = new XMLNode (xml_node_name);
100 for (StringPropertyMap::const_iterator i = _property_maps.begin(); i != _property_maps.end(); ++i) {
102 const PropertyMap& pmap (i->second);
103 XMLNode* id_node = new XMLNode (X_("Object"));
105 id_node->add_property ("id", i->first);
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());
114 root->add_child_nocopy (*id_node);
118 return *root;
122 GUIObjectState::set_state (const XMLNode& node)
124 if (node.name() != xml_node_name) {
125 return -1;
128 clear_maps ();
130 for (XMLNodeList::const_iterator i = node.children().begin(); i != node.children().end(); ++i) {
131 if ((*i)->name() == X_("Object")) {
133 XMLNode* child = (*i);
134 const XMLProperty* idp = child->property (X_("id"));
136 if (!idp) {
137 continue;
140 string id (idp->value());
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.
148 if ((*p)->name() != X_("id")) {
149 set (id, (*p)->name(), (*p)->value());
155 return 0;
159 void
160 GUIObjectState::load (const XMLNode& node)
162 (void) set_state (node);
165 GUIObjectState&
166 GUIObjectState::operator= (const GUIObjectState& other)
168 _property_maps = other._property_maps;
170 return *this;
173 /** @return begin iterator into our StringPropertyMap */
174 GUIObjectState::StringPropertyMap::const_iterator
175 GUIObjectState::begin () const
177 return _property_maps.begin ();
180 /** @return end iterator into our StringPropertyMap */
181 GUIObjectState::StringPropertyMap::const_iterator
182 GUIObjectState::end () const
184 return _property_maps.end ();