merge with master
[ardour.git] / gtk2_ardour / ui_config.h
1 /*
2     Copyright (C) 2000-2007 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 #ifndef __ardour_ui_configuration_h__
21 #define __ardour_ui_configuration_h__
22
23 #include <sstream>
24 #include <ostream>
25 #include <iostream>
26
27 #include "pbd/stateful.h"
28 #include "pbd/xml++.h"
29 #include "ardour/configuration_variable.h"
30
31 template<class T>
32 class UIConfigVariable : public ARDOUR::ConfigVariableBase
33 {
34   public:
35         UIConfigVariable (std::string str) : ARDOUR::ConfigVariableBase (str) {}
36         UIConfigVariable (std::string str, T val) : ARDOUR::ConfigVariableBase (str), value (val) {}
37
38         bool set (T val) {
39                 if (val == value) {
40                         return false;
41                 }
42                 value = val;
43                 return true;
44         }
45
46         T get() const {
47                 return value;
48         }
49
50         std::string get_as_string () const {
51                 std::stringstream ss;
52                 ss << std::hex;
53                 ss.fill('0');
54                 ss.width(8);
55                 ss << value;
56                 return ss.str ();
57         }
58
59         void set_from_string (std::string const & s) {
60                 std::stringstream ss;
61                 ss << std::hex;
62                 ss << s;
63                 ss >> value;
64         }
65
66   protected:
67         T get_for_save() { return value; }
68         T value;
69 };
70
71 class UIConfiguration : public PBD::Stateful
72 {
73   public:
74         UIConfiguration();
75         ~UIConfiguration();
76
77         std::map<std::string,UIConfigVariable<uint32_t> *> canvas_colors;
78
79         bool dirty () const;
80         void set_dirty ();
81
82         int load_state ();
83         int save_state ();
84         int load_defaults ();
85
86         int set_state (const XMLNode&, int version);
87         XMLNode& get_state (void);
88         XMLNode& get_variables (std::string);
89         void set_variables (const XMLNode&);
90         void pack_canvasvars ();
91
92         uint32_t color_by_name (const std::string&);
93
94         sigc::signal<void,const char*> ParameterChanged;
95
96 #undef UI_CONFIG_VARIABLE
97 #define UI_CONFIG_VARIABLE(Type,var,name,value) \
98         Type get_##var () const { return var.get(); } \
99         bool set_##var (Type val) { bool ret = var.set (val); if (ret) { ParameterChanged (name); } return ret;  }
100 #include "ui_config_vars.h"
101 #undef  UI_CONFIG_VARIABLE
102 #undef CANVAS_VARIABLE
103 #define CANVAS_VARIABLE(var,name) \
104         uint32_t get_##var () const { return var.get(); } \
105         bool set_##var (uint32_t val) { bool ret = var.set (val); if (ret) { ParameterChanged (name); } return ret;  }
106 #include "canvas_vars.h"
107 #undef  CANVAS_VARIABLE
108
109   private:
110
111         /* declare variables */
112
113 #undef  UI_CONFIG_VARIABLE
114 #define UI_CONFIG_VARIABLE(Type,var,name,value) UIConfigVariable<Type> var;
115 #include "ui_config_vars.h"
116 #undef UI_CONFIG_VARIABLE
117
118 #undef CANVAS_VARIABLE
119 #define CANVAS_VARIABLE(var,name) UIConfigVariable<uint32_t> var;
120 #include "canvas_vars.h"
121 #undef  CANVAS_VARIABLE
122
123         XMLNode& state ();
124         bool _dirty;
125 };
126
127 #endif /* __ardour_ui_configuration_h__ */
128