Properly remember window position.
[ardour.git] / libs / pbd / configuration_variable.cc
1 /*
2     Copyright (C) 1999-2009 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
22 #include "pbd/compose.h"
23 #include "pbd/configuration_variable.h"
24 #include "pbd/debug.h"
25
26 #if defined(_MSC_VER) && (_MSC_VER < 1800)
27 // MSVC only introduced std::strtof in VS2013
28 #define strtof(s, e) strtod(s, e)
29 #endif
30
31 using namespace std;
32 using namespace PBD;
33
34 void
35 ConfigVariableBase::add_to_node (XMLNode& node)
36 {
37         const std::string v = get_as_string ();
38         DEBUG_TRACE (DEBUG::Configuration, string_compose ("Config variable %1 stored as [%2]\n", _name, v));
39         XMLNode* child = new XMLNode ("Option");
40         child->add_property ("name", _name);
41         child->add_property ("value", v);
42         node.add_child_nocopy (*child);
43 }
44
45 bool
46 ConfigVariableBase::set_from_node (XMLNode const & node)
47 {
48         if (node.name() == "Config" || node.name() == "Canvas" || node.name() == "UI") {
49
50                 /* ardour.rc */
51
52                 const XMLProperty* prop;
53                 XMLNodeList nlist;
54                 XMLNodeConstIterator niter;
55                 XMLNode const * child;
56
57                 nlist = node.children();
58
59                 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
60
61                         child = *niter;
62
63                         if (child->name() == "Option") {
64                                 if ((prop = child->property ("name")) != 0) {
65                                         if (prop->value() == _name) {
66                                                 if ((prop = child->property ("value")) != 0) {
67                                                         set_from_string (prop->value());
68                                                         return true;
69                                                 }
70                                         }
71                                 }
72                         }
73                 }
74
75         } else if (node.name() == "Options") {
76
77                 /* session file */
78
79                 XMLNodeList olist;
80                 XMLNodeConstIterator oiter;
81                 XMLNode* option;
82                 const XMLProperty* opt_prop;
83
84                 olist = node.children();
85
86                 for (oiter = olist.begin(); oiter != olist.end(); ++oiter) {
87
88                         option = *oiter;
89
90                         if (option->name() == _name) {
91                                 if ((opt_prop = option->property ("val")) != 0) {
92                                         set_from_string (opt_prop->value());
93                                         return true;
94                                 }
95                         }
96                 }
97         }
98
99         return false;
100 }
101
102 void
103 ConfigVariableBase::notify ()
104 {
105         // placeholder for any debugging desired when a config variable is modified
106 }
107
108 void
109 ConfigVariableBase::miss ()
110 {
111         // placeholder for any debugging desired when a config variable
112         // is set but to the same value as it already has
113 }
114
115 /* Specialisation of ConfigVariable to deal with float (-inf etc)
116  * http://stackoverflow.com/questions/23374095/should-a-stringstream-parse-infinity-as-an-infinite-value
117  */
118 template<> void
119 ConfigVariable<float>::set_from_string (std::string const & s) {
120         value = std::strtof (s.c_str(), NULL);
121 }