annotate mde/gui/WidgetLoader.d @ 175:1cbde9807293

Compile/link-time fixes for ldc & non-debug builds. Moved WidgetManager to widget/ Reverted IChildWidget to an interface, not an abstract class. Introduced a work-around for a compiler problem. May not cover all cases.
author Diggory Hardy <diggory.hardy@gmail.com>
date Fri, 11 Sep 2009 20:56:53 +0200
parents 3d58adc17d20
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
159
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
1 /* LICENSE BLOCK
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
2 Part of mde: a Modular D game-oriented Engine
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
3 Copyright © 2007-2008 Diggory Hardy
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
4
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
5 This program is free software: you can redistribute it and/or modify it under the terms
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
6 of the GNU General Public License as published by the Free Software Foundation, either
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
7 version 2 of the License, or (at your option) any later version.
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
8
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
10 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
11 See the GNU General Public License for more details.
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
12
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
13 You should have received a copy of the GNU General Public License
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
14 along with this program. If not, see <http://www.gnu.org/licenses/>. */
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
15
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
16 /******************************************************************************
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
17 * A gui manager extension to load widgets from files.
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
18 *
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
19 * Public methods in this class should be thread safe (by locking on a mutex).
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
20 *****************************************************************************/
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
21 module mde.gui.WidgetLoader;
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
22
174
3d58adc17d20 Temporary commit to allow backup
Diggory Hardy <diggory.hardy@gmail.com>
parents: 171
diff changeset
23 import mde.gui.widget.WidgetManager;
159
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
24 import mde.gui.WidgetDataSet;
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
25 import mde.gui.exception;
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
26
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
27 import mt = mde.file.mergetag.DataSet;
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
28 import mde.file.mergetag.Reader;
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
29 import mde.file.mergetag.Writer;
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
30 import mde.file.paths;
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
31
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
32 import tango.util.log.Log : Log, Logger;
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
33
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
34 private Logger logger;
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
35 static this () {
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
36 logger = Log.getLogger ("mde.gui.WidgetLoader");
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
37 }
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
38
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
39 /******************************************************************************
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
40 * Contains the code for loading and saving an entire gui (more than one may
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
41 * exist), but not the code for drawing it or handling user input.
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
42 *
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
43 * This abstract class exists solely for separating out some of the functionality.
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
44 *****************************************************************************/
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
45 abstract scope class AWidgetLoader : AWidgetManager
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
46 {
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
47 /** Construct a new widget loader.
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
48 *
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
49 * params:
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
50 * fileName = Name of file specifying the gui, excluding path and extension.
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
51 */
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
52 protected this (char[] file) {
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
53 mutex = new Mutex; // Used on functions intended to be called from outside the gui package.
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
54 fileName = file;
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
55 super (file);
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
56 }
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
57
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
58 /* Load the widgets' data from the file specified to the CTOR.
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
59 *
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
60 * params:
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
61 * allDesigns = Load all sections
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
62 */
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
63 private void loadData (bool allDesigns = false) {
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
64 if (allLoaded || (defaultDesign !is null && allDesigns == false))
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
65 return; // test if already loaded
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
66
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
67 // Set up a reader
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
68 scope IReader reader;
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
69 try {
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
70 reader = confDir.makeMTReader (fileName, PRIORITY.HIGH_LOW, null, true);
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
71
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
72 // Read from the HEADER:
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
73 // Get the renderer
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
74 char[]* p = "Renderer" in reader.dataset.header._charA;
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
75 if (p is null || *p is null) {
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
76 logger.warn ("No renderer specified: using \"Simple\"");
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
77 rendName = "Simple";
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
78 }
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
79 else
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
80 rendName = *p;
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
81
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
82 // Get which section to use
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
83 p = "Design" in reader.dataset.header._charA;
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
84 if (p is null || *p is null) {
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
85 logger.warn ("No gui design specified: trying \"Default\"");
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
86 defaultDesign = "Default";
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
87 }
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
88 else
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
89 defaultDesign = *p;
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
90
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
91 // Read the body:
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
92 // Load the chosen design
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
93 reader.dataSecCreator = delegate mt.IDataSection(mt.ID id) {
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
94 WidgetDataSet* p = id in data;
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
95 if (p is null) {
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
96 data[id] = new WidgetDataSet;
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
97 return *(id in data);
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
98 }
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
99 return *p;
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
100 };
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
101
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
102 if (allDesigns) {
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
103 reader.read;
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
104 allLoaded = true;
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
105 } else
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
106 reader.read([defaultDesign]);
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
107 } catch (NoFileException) {
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
108 logger.error ("Unable to load GUI: no config file: "~fileName);
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
109 // just return: not a fatal error (so long as the game can run without a GUI!)
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
110 } catch (Exception e) {
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
111 logger.error ("Unable to load GUI: errors parsing config file ("~confDir.getFileName(fileName,PRIORITY.HIGH_LOW)~"):");
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
112 logger.error (e.msg);
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
113 throw new GuiException ("Failure parsing config file");
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
114 }
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
115 }
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
116
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
117 /** Load the gui from some design.
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
118 *
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
119 * If a design was previously loaded, its changes are saved first.
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
120 *
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
121 * Params:
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
122 * name = Design to load. If null, the default will be loaded.
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
123 */
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
124 void loadDesign (char[] name = null) {
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
125 if (changes !is null) // A design was previously loaded
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
126 save; // own lock
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
127
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
128 mutex.lock;
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
129 scope(exit) mutex.unlock;
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
130
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
131 // Load data (loadData tests if it's already loaded first):
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
132 if (name is null) {
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
133 loadData (false);
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
134 name = defaultDesign;
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
135 } else
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
136 loadData (true);
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
137
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
138
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
139 // Get data:
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
140 auto p = name in data;
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
141 while (p is null) {
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
142 if (name == defaultDesign)
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
143 throw new GuiException ("Unable to load [specified or] default design");
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
144 name = defaultDesign; // try again with the default
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
145 p = name in data;
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
146 }
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
147 curData = *p;
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
148
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
149 // Get/create a changes section:
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
150 if (changesDS is null)
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
151 changesDS = new mt.DataSet;
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
152
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
153 mt.IDataSection* q = name in changesDS.sec;
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
154 if (!q || ((changes = cast(WidgetDataChanges) *q) is null)) {
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
155 changes = new WidgetDataChanges (curData);
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
156 changesDS.sec[name] = changes;
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
157 }
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
158
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
159 // Create the widgets:
171
7f7b2011b759 Partially complete commit: code runs but context menus don't work.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 159
diff changeset
160 createWidgets;
159
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
161 }
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
162
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
163 /** Save changes, if any exist.
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
164 *
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
165 * Is run when the manager is destroyed, but could be run at other times too. */
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
166 void save () {
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
167 preSave;
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
168
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
169 mutex.lock;
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
170 scope(exit) mutex.unlock;
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
171
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
172 // Make all widgets save any changed data:
171
7f7b2011b759 Partially complete commit: code runs but context menus don't work.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 159
diff changeset
173 saveChanges;
159
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
174
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
175 if (changes.noChanges)
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
176 return;
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
177
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
178 if (loadUserFile) { // merge entries from user file into current changes
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
179 try {
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
180 scope IReader reader = confDir.makeMTReader (
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
181 fileName, PRIORITY.HIGH_ONLY, changesDS, true);
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
182
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
183 // Create if necessary, only corresponding to existing designs read:
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
184 reader.dataSecCreator = delegate mt.IDataSection(mt.ID id) {
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
185 WidgetDataSet* p = id in data;
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
186 if (p is null)
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
187 throw new Exception ("File has changed since it was loaded!");
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
188 return new WidgetDataChanges (*p);
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
189 };
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
190
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
191 reader.read;
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
192 } catch (NoFileException) {
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
193 // No user file exists; not an error.
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
194 } catch (Exception e) {
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
195 logger.error ("Error reading "~confDir.getFileName(fileName,PRIORITY.HIGH_ONLY)~" prior to saving:");
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
196 logger.error (e.msg);
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
197 logger.error ("Overwriting the file.");
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
198 // Continue...
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
199 }
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
200 loadUserFile = false; // don't need to do it again
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
201 }
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
202
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
203 try { // Save
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
204 IWriter writer;
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
205 writer = confDir.makeMTWriter (fileName, changesDS);
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
206 writer.write;
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
207 } catch (Exception e) {
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
208 logger.error ("Saving to "~confDir.getFileName(fileName,PRIORITY.HIGH_ONLY)~" failed:");
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
209 logger.error (e.msg);
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
210 // No point in throwing since it doesn't affect anything else.
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
211 }
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
212 }
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
213
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
214 /** Get the names of all designs available. */
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
215 char[][] designs() {
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
216 synchronized(mutex) {
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
217 loadData (true);
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
218 return data.keys;
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
219 }
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
220 }
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
221
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
222 protected:
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
223 // Called by derived classes, not thread safe for the same instance
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
224 //BEGIN WidgetManagement methods
171
7f7b2011b759 Partially complete commit: code runs but context menus don't work.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 159
diff changeset
225 /** Called before saving to make sure no data is being edited (and thus
7f7b2011b759 Partially complete commit: code runs but context menus don't work.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 159
diff changeset
226 * would not be saved). */
175
1cbde9807293 Compile/link-time fixes for ldc & non-debug builds.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 174
diff changeset
227 abstract void preSave ();
159
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
228 //END WidgetManagement methods
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
229
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
230
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
231 // Dataset/design data:
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
232 final char[] fileName;
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
233 char[] defaultDesign; // The design specified in the file header.
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
234
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
235 // Loaded data, indexed by design name. May not be loaded for all gui designs:
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
236 scope WidgetDataSet[char[]] data;
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
237 private bool allLoaded = false; // applies to data
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
238 scope mt.DataSet changesDS; // changes and sections from user file (used for saving)
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
239 bool loadUserFile = true; // still need to load user file for saving?
b06b04c75e86 Finished last commit, rearranged code for the WidgetManager class.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
240 }