Simple approach to putting plugin controls into the
[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 <sstream>
22
23 #include "gui_object.h"
24 #include "i18n.h"
25
26 using std::string;
27
28 const string GUIObjectState::xml_node_name (X_("GUIObjectState"));
29
30 GUIObjectState::GUIObjectState ()
31         : _state (X_("GUIObjectState"))
32 {
33
34 }
35
36 XMLNode *
37 GUIObjectState::get_node (const XMLNode* parent, const string& id)
38 {
39         XMLNodeList const & children = parent->children ();
40         for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
41                 if ((*i)->name() != X_("Object")) {
42                         continue;
43                 }
44
45                 XMLProperty* p = (*i)->property (X_("id"));
46                 if (p && p->value() == id) {
47                         return *i;
48                 }
49         }
50
51         return 0;
52 }
53
54 XMLNode *
55 GUIObjectState::get_or_add_node (XMLNode* parent, const string& id)
56 {
57         XMLNode* child = get_node (parent, id);
58         if (!child) {
59                 child = new XMLNode (X_("Object"));
60                 child->add_property (X_("id"), id);
61                 parent->add_child_nocopy (*child);
62         }
63
64         return child;
65 }
66
67 XMLNode *
68 GUIObjectState::get_or_add_node (const string& id)
69 {
70         return get_or_add_node (&_state, id);
71 }
72
73 /** Get a string from our state.
74  *  @param id property of Object node to look for.
75  *  @param prop_name name of the Object property to return.
76  *  @param empty if non-0, filled in with true if the property is currently non-existant, otherwise false.
77  *  @return value of property `prop_name', or empty.
78  */
79
80 string 
81 GUIObjectState::get_string (const string& id, const string& prop_name, bool* empty)
82 {
83         XMLNode* child = get_node (&_state, id);
84         if (!child) {
85                 if (empty) {
86                         *empty = true;
87                 }
88                 return string ();
89         }
90
91         XMLProperty* p = child->property (prop_name);
92         if (!p) {
93                 if (empty) {
94                         *empty = true;
95                 }
96                 return string ();
97         }
98
99         if (empty) {
100                 *empty = false;
101         }
102
103         return p->value ();
104 }
105
106 XMLNode&
107 GUIObjectState::get_state () const
108 {
109         return *new XMLNode (_state);
110 }
111
112 int
113 GUIObjectState::set_state (const XMLNode& node)
114 {
115         if (node.name() != xml_node_name) {
116                 return -1;
117         }
118
119         _state = node;
120         return 0;
121 }
122
123 void
124 GUIObjectState::load (const XMLNode& node)
125 {
126         (void) set_state (node);
127 }
128
129 GUIObjectState&
130 GUIObjectState::operator= (const GUIObjectState& other)
131 {
132         _state = other._state;
133         return *this;
134 }
135
136 std::list<string>
137 GUIObjectState::all_ids () const
138 {
139         std::list<string> ids;
140         XMLNodeList const & children = _state.children ();
141         for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
142                 if ((*i)->name() != X_("Object")) {
143                         continue;
144                 }
145
146                 XMLProperty* p = (*i)->property (X_("id"));
147                 if (p) {
148                         ids.push_back (p->value ());
149                 }
150         }
151
152         return ids;
153 }
154
155