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.
23 #include "gui_object.h"
29 GUIObjectState::get_node (const XMLNode* parent, const string& id)
31 XMLNodeList const & children = parent->children ();
32 for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
33 if ((*i)->name() != X_("Object")) {
36 if ((*i)->has_property_with_value(X_("id"), id)) {
44 GUIObjectState::get_or_add_node (XMLNode* parent, const string& id)
46 XMLNode* child = get_node (parent, id);
48 child = new XMLNode (X_("Object"));
49 child->set_property (X_("id"), id);
50 parent->add_child_nocopy (*child);
56 const string GUIObjectState::xml_node_name (X_("GUIObjectState"));
58 GUIObjectState::GUIObjectState ()
59 : _state (X_("GUIObjectState"))
64 GUIObjectState::get_or_add_node (const string& id)
66 std::map <std::string, XMLNode*>::iterator i = object_map.find (id);
67 if (i != object_map.end()) {
70 //assert (get_node (&_state, id) == 0); // XXX performance penalty due to get_node()
71 XMLNode* child = new XMLNode (X_("Object"));
72 child->set_property (X_("id"), id);
73 _state.add_child_nocopy (*child);
74 object_map[id] = child;
79 GUIObjectState::remove_node (const std::string& id)
81 object_map.erase (id);
82 _state.remove_nodes_and_delete(X_("id"), id );
86 GUIObjectState::get_string (const string& id, const string& prop_name, bool* empty)
88 std::map <std::string, XMLNode*>::const_iterator i = object_map.find (id);
89 if (i == object_map.end()) {
90 //assert (get_node (&_state, id) == 0); // XXX performance penalty due to get_node()
96 //assert (get_node (&_state, id) == i->second); // XXX performance penalty due to get_node()
98 XMLProperty const * p (i->second->property (prop_name));
114 GUIObjectState::get_state () const
116 return *new XMLNode (_state);
120 GUIObjectState::set_state (const XMLNode& node)
122 if (node.name() != xml_node_name) {
129 XMLNodeList const & children (_state.children ());
130 for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
131 if ((*i)->name() != X_("Object")) {
134 XMLProperty const * prop = (*i)->property (X_("id"));
138 object_map[prop->value ()] = *i;
144 GUIObjectState::load (const XMLNode& node)
146 (void) set_state (node);
150 GUIObjectState::all_ids () const
152 std::list<string> ids;
153 for (std::map <std::string, XMLNode*>::const_iterator i = object_map.begin ();
154 i != object_map.end (); ++i) {
155 ids.push_back (i->first);