/[svn]/gigedit/trunk/src/gigedit/Settings.cpp
ViewVC logotype

Contents of /gigedit/trunk/src/gigedit/Settings.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3711 - (show annotations) (download)
Fri Jan 10 14:22:25 2020 UTC (20 months ago) by schoenebeck
File size: 13696 byte(s)
* Added boolean setting to main menu -> "View" -> "Instrument Properties
  by Double Click", which allows to prevent double clicking an
  instrument on the instruments list view from opening its properties
  dialog.

* Bumped version (1.1.1.svn11).

1 /*
2 Copyright (c) 2014-2020 Christian Schoenebeck
3
4 This file is part of "gigedit" and released under the terms of the
5 GNU General Public License version 2.
6 */
7
8 #include "global.h"
9 #include "Settings.h"
10 #ifdef GLIB_HEADER_FILE
11 # include GLIB_HEADER_FILE(glib.h)
12 #else
13 # include <glib.h>
14 #endif
15 #include <glibmm/keyfile.h>
16 #include <iostream>
17 #include <stdio.h>
18 #include <fstream>
19 #include <string.h>
20
21 #if (GTKMM_MAJOR_VERSION == 2 && GTKMM_MINOR_VERSION < 40) || GTKMM_MAJOR_VERSION < 2
22 # define HAS_GLIB_KEYFILE_SAVE_TO_FILE 0
23 #else
24 # define HAS_GLIB_KEYFILE_SAVE_TO_FILE 1
25 #endif
26
27 static std::string configDir() {
28 //printf("configDir '%s'\n", g_get_user_config_dir());
29 return g_get_user_config_dir();
30 }
31
32 static std::string dirSep() {
33 //printf("sep '%s'\n", G_DIR_SEPARATOR_S);
34 return G_DIR_SEPARATOR_S;
35 }
36
37 static std::string configFile() {
38 return configDir() + dirSep() + "gigedit.conf";
39 }
40
41 static std::string groupName(Settings::Group_t group) {
42 switch (group) {
43 case Settings::GLOBAL: return "Global";
44 case Settings::MAIN_WINDOW: return "MainWindow";
45 case Settings::SCRIPT_EDITOR: return "ScriptEditor";
46 case Settings::DIMENSION_MANAGER: return "DimensionManager";
47 case Settings::SCRIPT_SLOTS: return "ScriptSlots";
48 case Settings::COMBINE_INSTRUMENTS: return "CombineInstruments";
49 case Settings::MIDI_RULES: return "MidiRules";
50 case Settings::FILE_PROPS: return "FileProps";
51 case Settings::INSTR_PROPS: return "InstrProps";
52 case Settings::SAMPLE_PROPS: return "SampleProps";
53 case Settings::SAMPLE_REFS: return "SampleRefs";
54 case Settings::MACRO_EDITOR: return "MacroEditor";
55 case Settings::MACROS_SETUP: return "MacrosSetup";
56 case Settings::MACROS: return "Macros";
57 }
58 return "Global";
59 }
60
61 #if !HAS_GLIB_KEYFILE_SAVE_TO_FILE
62
63 static bool saveToFile(Glib::KeyFile* keyfile, std::string filename) {
64 Glib::ustring s = keyfile->to_data();
65 std::ofstream out;
66 out.open(filename.c_str(), std::ios_base::out | std::ios_base::trunc);
67 out << s;
68 out.close();
69 return true;
70 }
71
72 #endif // ! HAS_GLIB_KEYFILE_SAVE_TO_FILE
73
74 static Settings* _instance = NULL;
75
76 Settings* Settings::singleton() {
77 if (!_instance) {
78 _instance = new Settings;
79 _instance->load();
80 }
81 return _instance;
82 }
83
84 Settings::Settings() : Glib::ObjectBase(typeid(Settings)),
85 warnUserOnExtensions(*this, GLOBAL, "warnUserOnExtensions", true),
86 syncSamplerInstrumentSelection(*this, GLOBAL, "syncSamplerInstrumentSelection", true),
87 moveRootNoteWithRegionMoved(*this, GLOBAL, "moveRootNoteWithRegionMoved", true),
88 autoRestoreWindowDimension(*this, GLOBAL, "autoRestoreWindowDimension", false),
89 saveWithTemporaryFile(*this, GLOBAL, "saveWithTemporaryFile", false),
90 showTooltips(*this, GLOBAL, "showNewbieTooltips", true),
91 instrumentDoubleClickOpensProps(*this, GLOBAL, "openInstrPropsByDoubleClick", true),
92 mainWindowX(*this, MAIN_WINDOW, "x", -1),
93 mainWindowY(*this, MAIN_WINDOW, "y", -1),
94 mainWindowW(*this, MAIN_WINDOW, "w", -1),
95 mainWindowH(*this, MAIN_WINDOW, "h", -1),
96 scriptEditorWindowX(*this, SCRIPT_EDITOR, "x", -1),
97 scriptEditorWindowY(*this, SCRIPT_EDITOR, "y", -1),
98 scriptEditorWindowW(*this, SCRIPT_EDITOR, "w", -1),
99 scriptEditorWindowH(*this, SCRIPT_EDITOR, "h", -1),
100 scriptEditorFontSize(*this, SCRIPT_EDITOR, "fontSize", -1),
101 dimensionManagerWindowX(*this, DIMENSION_MANAGER, "x", -1),
102 dimensionManagerWindowY(*this, DIMENSION_MANAGER, "y", -1),
103 dimensionManagerWindowW(*this, DIMENSION_MANAGER, "w", -1),
104 dimensionManagerWindowH(*this, DIMENSION_MANAGER, "h", -1),
105 scriptSlotsWindowX(*this, SCRIPT_SLOTS, "x", -1),
106 scriptSlotsWindowY(*this, SCRIPT_SLOTS, "y", -1),
107 scriptSlotsWindowW(*this, SCRIPT_SLOTS, "w", -1),
108 scriptSlotsWindowH(*this, SCRIPT_SLOTS, "h", -1),
109 combineInstrumentsWindowX(*this, COMBINE_INSTRUMENTS, "x", -1),
110 combineInstrumentsWindowY(*this, COMBINE_INSTRUMENTS, "y", -1),
111 combineInstrumentsWindowW(*this, COMBINE_INSTRUMENTS, "w", -1),
112 combineInstrumentsWindowH(*this, COMBINE_INSTRUMENTS, "h", -1),
113 midiRulesWindowX(*this, MIDI_RULES, "x", -1),
114 midiRulesWindowY(*this, MIDI_RULES, "y", -1),
115 midiRulesWindowW(*this, MIDI_RULES, "w", -1),
116 midiRulesWindowH(*this, MIDI_RULES, "h", -1),
117 filePropsWindowX(*this, FILE_PROPS, "x", -1),
118 filePropsWindowY(*this, FILE_PROPS, "y", -1),
119 filePropsWindowW(*this, FILE_PROPS, "w", -1),
120 filePropsWindowH(*this, FILE_PROPS, "h", -1),
121 instrPropsWindowX(*this, INSTR_PROPS, "x", -1),
122 instrPropsWindowY(*this, INSTR_PROPS, "y", -1),
123 instrPropsWindowW(*this, INSTR_PROPS, "w", -1),
124 instrPropsWindowH(*this, INSTR_PROPS, "h", -1),
125 samplePropsWindowX(*this, SAMPLE_PROPS, "x", -1),
126 samplePropsWindowY(*this, SAMPLE_PROPS, "y", -1),
127 samplePropsWindowW(*this, SAMPLE_PROPS, "w", -1),
128 samplePropsWindowH(*this, SAMPLE_PROPS, "h", -1),
129 sampleRefsWindowX(*this, SAMPLE_REFS, "x", -1),
130 sampleRefsWindowY(*this, SAMPLE_REFS, "y", -1),
131 sampleRefsWindowW(*this, SAMPLE_REFS, "w", -1),
132 sampleRefsWindowH(*this, SAMPLE_REFS, "h", -1),
133 macroEditorWindowX(*this, MACRO_EDITOR, "x", -1),
134 macroEditorWindowY(*this, MACRO_EDITOR, "y", -1),
135 macroEditorWindowW(*this, MACRO_EDITOR, "w", -1),
136 macroEditorWindowH(*this, MACRO_EDITOR, "h", -1),
137 macrosSetupWindowX(*this, MACROS_SETUP, "x", -1),
138 macrosSetupWindowY(*this, MACROS_SETUP, "y", -1),
139 macrosSetupWindowW(*this, MACROS_SETUP, "w", -1),
140 macrosSetupWindowH(*this, MACROS_SETUP, "h", -1),
141 m_ignoreNotifies(false)
142 {
143 m_boolProps.push_back(&warnUserOnExtensions);
144 m_boolProps.push_back(&syncSamplerInstrumentSelection);
145 m_boolProps.push_back(&moveRootNoteWithRegionMoved);
146 m_boolProps.push_back(&autoRestoreWindowDimension);
147 m_boolProps.push_back(&saveWithTemporaryFile);
148 m_boolProps.push_back(&showTooltips);
149 m_boolProps.push_back(&instrumentDoubleClickOpensProps);
150 m_intProps.push_back(&mainWindowX);
151 m_intProps.push_back(&mainWindowY);
152 m_intProps.push_back(&mainWindowW);
153 m_intProps.push_back(&mainWindowH);
154 m_intProps.push_back(&scriptEditorWindowX);
155 m_intProps.push_back(&scriptEditorWindowY);
156 m_intProps.push_back(&scriptEditorWindowW);
157 m_intProps.push_back(&scriptEditorWindowH);
158 m_intProps.push_back(&scriptEditorFontSize);
159 m_intProps.push_back(&dimensionManagerWindowX);
160 m_intProps.push_back(&dimensionManagerWindowY);
161 m_intProps.push_back(&dimensionManagerWindowW);
162 m_intProps.push_back(&dimensionManagerWindowH);
163 m_intProps.push_back(&scriptSlotsWindowX);
164 m_intProps.push_back(&scriptSlotsWindowY);
165 m_intProps.push_back(&scriptSlotsWindowW);
166 m_intProps.push_back(&scriptSlotsWindowH);
167 m_intProps.push_back(&combineInstrumentsWindowX);
168 m_intProps.push_back(&combineInstrumentsWindowY);
169 m_intProps.push_back(&combineInstrumentsWindowW);
170 m_intProps.push_back(&combineInstrumentsWindowH);
171 m_intProps.push_back(&midiRulesWindowX);
172 m_intProps.push_back(&midiRulesWindowY);
173 m_intProps.push_back(&midiRulesWindowW);
174 m_intProps.push_back(&midiRulesWindowH);
175 m_intProps.push_back(&filePropsWindowX);
176 m_intProps.push_back(&filePropsWindowY);
177 m_intProps.push_back(&filePropsWindowW);
178 m_intProps.push_back(&filePropsWindowH);
179 m_intProps.push_back(&instrPropsWindowX);
180 m_intProps.push_back(&instrPropsWindowY);
181 m_intProps.push_back(&instrPropsWindowW);
182 m_intProps.push_back(&instrPropsWindowH);
183 m_intProps.push_back(&samplePropsWindowX);
184 m_intProps.push_back(&samplePropsWindowY);
185 m_intProps.push_back(&samplePropsWindowW);
186 m_intProps.push_back(&samplePropsWindowH);
187 m_intProps.push_back(&sampleRefsWindowX);
188 m_intProps.push_back(&sampleRefsWindowY);
189 m_intProps.push_back(&sampleRefsWindowW);
190 m_intProps.push_back(&sampleRefsWindowH);
191 m_intProps.push_back(&macroEditorWindowX);
192 m_intProps.push_back(&macroEditorWindowY);
193 m_intProps.push_back(&macroEditorWindowW);
194 m_intProps.push_back(&macroEditorWindowH);
195 m_intProps.push_back(&macrosSetupWindowX);
196 m_intProps.push_back(&macrosSetupWindowY);
197 m_intProps.push_back(&macrosSetupWindowW);
198 m_intProps.push_back(&macrosSetupWindowH);
199 }
200
201 void Settings::onPropertyChanged(Glib::PropertyBase* pProperty, RawValueType_t type, Group_t group) {
202 if (m_ignoreNotifies) return;
203
204 //printf("Settings::onPropertyChanged(%s)\n", pProperty->get_name().c_str());
205
206 Glib::KeyFile file;
207 try {
208 bool ok = file.load_from_file(configFile());
209 if (!ok) {
210 std::cerr << "Could not load '" << configFile() << "'\n" << std::flush;
211 }
212 } catch (...) {
213 std::cerr << "Could not load '" << configFile() << "'\n" << std::flush;
214 }
215
216 switch (type) {
217 case BOOLEAN: {
218 Property<bool>* prop = static_cast<Property<bool>*>(pProperty);
219 //std::cout << "Saving bool setting '" << prop->get_name() << "'\n" << std::flush;
220 file.set_boolean(groupName(prop->group()), prop->get_name(), prop->get_value());
221 break;
222 }
223 case INTEGER: {
224 Property<int>* prop = static_cast<Property<int>*>(pProperty);
225 //std::cout << "Saving int setting '" << prop->get_name() << "'\n" << std::flush;
226 file.set_integer(groupName(prop->group()), prop->get_name(), prop->get_value());
227 break;
228 }
229 case UNKNOWN:
230 std::cerr << "BUG: Unknown setting raw type of property '" << pProperty->get_name() << "'\n" << std::flush;
231 return;
232 }
233
234 try {
235 #if HAS_GLIB_KEYFILE_SAVE_TO_FILE
236 bool ok = file.save_to_file(configFile());
237 #else
238 bool ok = saveToFile(&file, configFile());
239 #endif
240 if (!ok) {
241 std::cerr << "Failed saving gigedit config to '" << configFile() << "'\n" << std::flush;
242 } else {
243 //std::cout <<"gigedit CONFIG SAVED\n";
244 }
245 } catch (...) {
246 std::cerr << "Failed saving gigedit config to '" << configFile() << "'\n" << std::flush;
247 }
248 }
249
250 void Settings::load() {
251 Glib::KeyFile file;
252 try {
253 bool ok = file.load_from_file(configFile());
254 if (!ok) return;
255 } catch (...) {
256 std::cerr << "Could not load gigedit config file '" << configFile() << "'\n" << std::flush;
257 return;
258 }
259
260 // ignore onPropertyChanged() calls during updating the property values below
261 m_ignoreNotifies = true;
262
263 for (int i = 0; i < m_boolProps.size(); ++i) {
264 Property<bool>* prop = static_cast<Property<bool>*>(m_boolProps[i]);
265 try {
266 const std::string group = groupName(prop->group());
267 if (!file.has_group(group)) continue;
268 if (!file.has_key(group, prop->get_name())) continue;
269 const bool value = file.get_boolean(group, prop->get_name());
270 prop->set_value(value);
271 } catch (...) {
272 continue;
273 }
274 }
275
276 for (int i = 0; i < m_intProps.size(); ++i) {
277 Property<int>* prop = static_cast<Property<int>*>(m_intProps[i]);
278 try {
279 const std::string group = groupName(prop->group());
280 if (!file.has_group(group)) continue;
281 if (!file.has_key(group, prop->get_name())) continue;
282 const int value = file.get_integer(group, prop->get_name());
283 prop->set_value(value);
284 } catch (...) {
285 continue;
286 }
287 }
288
289 m_ignoreNotifies = false;
290 }
291
292 #define MACRO_LIST_NAME "srlzl"
293
294 void Settings::loadMacros(std::vector<Serialization::Archive>& macros) {
295 const std::string group = groupName(MACROS);
296 macros.clear();
297 Glib::KeyFile file;
298 try {
299 bool ok = file.load_from_file(configFile());
300 if (!ok) return;
301 } catch (...) {
302 std::cerr << "Could not load gigedit config file '" << configFile() << "'\n" << std::flush;
303 return;
304 }
305 if (!file.has_group(group)) return;
306 if (!file.has_key(group, MACRO_LIST_NAME))
307 return;
308 std::vector<Glib::ustring> v = file.get_string_list(group, MACRO_LIST_NAME);
309 for (int i = 0; i < v.size(); ++i) {
310 Serialization::Archive macro;
311 macro.decode((const uint8_t*)v[i].c_str(), v[i].length());
312 macros.push_back(macro);
313 }
314 }
315
316 void Settings::saveMacros(const std::vector<Serialization::Archive>& macros) {
317 const std::string group = groupName(MACROS);
318 Glib::KeyFile file;
319 try {
320 bool ok = file.load_from_file(configFile());
321 if (!ok) {
322 std::cerr << "Could not load '" << configFile() << "'\n" << std::flush;
323 }
324 } catch (...) {
325 std::cerr << "Could not load '" << configFile() << "'\n" << std::flush;
326 return;
327 }
328
329 std::vector<Glib::ustring> v;
330 for (int i = 0; i < macros.size(); ++i) {
331 const Serialization::RawData& rawData = const_cast<Serialization::Archive&>(macros[i]).rawData();
332 std::string s((const char*)&rawData[0], rawData.size());
333 v.push_back(s);
334 }
335
336 file.set_string_list(group, MACRO_LIST_NAME, v);
337
338 try {
339 #if HAS_GLIB_KEYFILE_SAVE_TO_FILE
340 bool ok = file.save_to_file(configFile());
341 #else
342 bool ok = saveToFile(&file, configFile());
343 #endif
344 if (!ok) {
345 std::cerr << "Failed saving gigedit config to '" << configFile() << "'\n" << std::flush;
346 } else {
347 //std::cout <<"gigedit CONFIG SAVED\n";
348 }
349 } catch (...) {
350 std::cerr << "Failed saving gigedit config to '" << configFile() << "'\n" << std::flush;
351 }
352
353 }

  ViewVC Help
Powered by ViewVC