aboutsummaryrefslogtreecommitdiff
path: root/gui/theme-config.cpp
blob: 4249fd882c4652a5c8274a7a1ec8d85c53a10211 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2006 The ScummVM project
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * $URL$
 * $Id$
 */

#include "gui/theme.h"
#include "gui/eval.h"

namespace GUI {

const char *Theme::_defaultConfigINI = "\n\
# Define our classic greenish theme here\n\
[320xY]\n\
def_widgetSize=kNormalWidgetSize\n\
def_buttonWidth=kButtonWidth\n\
def_buttonHeight=kButtonHeight\n\
def_sliderWidth=kSliderWidth\n\
def_sliderHeight=kSliderHeight\n\
def_kLineHeight=12\n\
def_kFontHeight=10\n\
def_globOptionsW=(w - 2 * 10)\n\
def_globOptionsH=(h - 1 * 40)\n\
def_tabPopupsLabelW=100\n\
def_midiControlsSpacing=1\n\
use=XxY\n\
\n\
[XxY]\n\
def_widgetSize=kBigWidgetSize\n\
def_buttonWidth=kBigButtonWidth\n\
def_buttonHeight=kBigButtonHeight\n\
def_sliderWidth=kBigSliderWidth\n\
def_sliderHeight=kBigSliderHeight\n\
def_kLineHeight=16\n\
def_kFontHeight=14\n\
def_globOptionsW=(w - 2 * 10)\n\
def_globOptionsH=(h - 2 * 40)\n\
def_tabPopupsLabelW=150\n\
def_midiControlsSpacing=2\n\
chooser_headline=10 6 (w - 2 * 16) (kLineHeight)\n\
chooser_list=10 (6 + kLineHeight + 2) (w - 2 * 16) (h - self.y - buttonHeight - 12)\n\
\n\
## launcher\n\
hBorder=10\n\
launcher_version=hBorder 8 (w - 2 * hBorder) kLineHeight\n\
top=(h - 8 - buttonHeight)\n\
numButtons=4\n\
space=8\n\
butWidth=((w - 2 * hBorder - space * (numButtons - 1)) / numButtons)\n\
launcher_quit_button=hBorder top butWidth buttonHeight\n\
launcher_about_button=(prev.x2 + space) top butWidth buttonHeight\n\
launcher_options_button=(prev.x2 + space) top butWidth buttonHeight\n\
launcher_start_button=(prev.x2 + space) top butWidth buttonHeight\n\
top=(top - buttonHeight * 2)\n\
numButtons=3\n\
space=10\n\
butWidth=((w - 2 * hBorder - space * (numButtons - 1)) / numButtons)\n\
launcher_addGame_button=hBorder top butWidth buttonHeight\n\
launcher_editGame_button=(prev.x2 + space) top butWidth buttonHeight\n\
launcher_removeGame_button=(prev.x2 + space) top butWidth buttonHeight\n\
launcher_list=hBorder (kLineHeight + 16) (w - 2 * hBorder) (top - kLineHeight - 20)\n\
\n\
# global options\n\
globaloptions=10 40 globOptionsW globOptionsH\n\
set_parent=globaloptions\n\
vBorder=5\n\
globaloptions_tabwidget=0, vBorder parent.w (parent.h - buttonHeight -8 - 2 * vBorder)\n\
\n\
# graphics tab\n\
opYoffset=vBorder\n\
opWidth=globOptionsW\n\
useWithPrefix=graphicsControls globaloptions_\n\
\n\
# audio tab\n\
opYoffset=vBorder\n\
opWidth=globOptionsW\n\
useWithPrefix=audioControls globaloptions_\n\
useWithPrefix=volumeControls globaloptions_\n\
\n\
# MIDI tab\n\
opYoffset=vBorder\n\
opWidth=globOptionsW\n\
useWithPrefix=midiControls globaloptions_\n\
\n\
# paths tab\n\
yoffset=vBorder\n\
globaloptions_savebutton=5 yoffset (buttonWidth + 5) buttonHeight\n\
globaloptions_savepath=(prev.x2 + 20) (vBorder + 3) (parent.w - (5 + buttonWidth + 20) - 10) kLineHeight\n\
yoffset=(yoffset + buttonHeight + 4)\n\
globaloptions_extrabutton=5 yoffset (buttonWidth + 5) buttonHeight\n\
globaloptions_extrapath=(prev.x2 + 20) (vBorder + 3) (parent.w - (5 + buttonWidth + 20) - 10) kLineHeight\n\
yoffset=(yoffset + buttonHeight + 4)\n\
globaloptions_keysbutton=5 yoffset buttonWidth buttonHeight\n\
\n\
globaloptions_ok=(parent.w - (buttonWidth + 10)) (parent.h - buttonHeight - 8) buttonWidth buttonHeight\n\
globaloptions_cancel=(parent.w - 2 * (buttonWidth + 10)) (parent.h - buttonHeight - 8) buttonWidth buttonHeight\n\
\n\
# game options\n\
opYoffset=(vBorder + buttonHeight)\n\
opWidth=globOptionsW\n\
useWithPrefix=graphicsControls gameoptions_\n\
\n\
opYoffset=(vBorder + buttonHeight)\n\
useWithPrefix=audioControls gameoptions_\n\
\n\
opYoffset=(vBorder + buttonHeight)\n\
useWithPrefix=volumeControls gameoptions_\n\
\n\
opYoffset=(vBorder + buttonHeight)\n\
useWithPrefix=midiControls gameoptions_\n\
\n\
### SCUMM game options\n\
opYoffset=8\n\
useWithPrefix=volumeControls scummoptions_\n\
\n\
[graphicsControls]\n\
gcx=10\n\
gcw=(opWidth - 2 * 10)\n\
grModePopup=(gcx - 5) opYoffset (gcw + 5) kLineHeight\n\
opYoffset=(opYoffset + kLineHeight + 4)\n\
grRenderPopup=(gcx - 5) opYoffset (gcw + 5) kLineHeight\n\
opYoffset=(opYoffset + kLineHeight + 4)\n\
grFullscreenCheckbox=gcx opYoffset (kFontHeight + 10 + 96) buttonHeight\n\
opYoffset=(opYoffset + buttonHeight)\n\
grAspectCheckbox=gcx opYoffset (kFontHeight + 10 + 136) buttonHeight\n\
opYoffset=(opYoffset + buttonHeight)\n\
\n\
[audioControls]\n\
aux=10\n\
auw=(opWidth - 2 * 10)\n\
auMidiPopup=(aux - 5) opYoffset (auw + 5) kLineHeight\n\
opYoffset=(opYoffset + kLineHeight + 4)\n\
auSubtitlesCheckbox=aux opYoffset (kFontHeight + 10 + 102) buttonHeight\n\
opYoffset=(opYoffset + buttonHeight + 18)\n\
\n\
[volumeControls]\n\
vctextw=95\n\
vcxoff=(vctextw + 15)\n\
vcMusicText=10 (opYoffset + 2) vctextw kLineHeight\n\
vcMusicSlider=vcxoff opYoffset sliderWidth sliderHeight\n\
vcMusicLabel=(vcxoff + prev.w + 10) (opYoffset + 2) 24 kLineHeight\n\
opYoffset=(opYoffset + sliderHeight + 4)\n\
vcSfxText=10 (opYoffset + 2) vctextw kLineHeight\n\
vcSfxSlider=vcxoff opYoffset sliderWidth sliderHeight\n\
vcSfxLabel=(vcxoff + prev.w + 10) (opYoffset + 2) 24 kLineHeight\n\
opYoffset=(opYoffset + sliderHeight + 4)\n\
vcSpeechText=10 (opYoffset + 2) vctextw kLineHeight\n\
vcSpeechSlider=vcxoff opYoffset sliderWidth sliderHeight\n\
vcSpeechLabel=(vcxoff + prev.w + 10) (opYoffset + 2) 24 kLineHeight\n\
opYoffset=(opYoffset + sliderHeight + 4)\n\
\n\
[midiControls]\n\
mcx=10\n\
mcFontButton=mcx opYoffset buttonWidth buttonHeight\n\
mcFontPath=(prev.x2 + 20) (opYoffset + 3) (opWidth - (buttonWidth + 20) - 10) kLineHeight\n\
opYoffset=(opYoffset + buttonHeight + 2 * midiControlsSpacing)\n\
mcMixedCheckbox=mcx opYoffset (kFontHeight + 10 + 135) buttonHeight\n\
opYoffset=(opYoffset + buttonHeight + midiControlsSpacing)\n\
mcMt32Checkbox=mcx opYoffset (kFontHeight + 10 + 256) buttonHeight\n\
opYoffset=(opYoffset + buttonHeight + midiControlsSpacing)\n\
mcGSCheckbox=mcx opYoffset (kFontHeight + 10 + 142) buttonHeight\n\
opYoffset=(opYoffset + buttonHeight + midiControlsSpacing)\n\
";

using Common::String;

void Theme::processSingleLine(const String &section, const String prefix, const String name, const String str) {
	int level = 0;
	int start = 0;
	uint i;
	int value;
	const char *postfixes[] = {"x", "y", "w", "h"};
	int npostfix = 0;

	// Make self.BLAH work
	for (i = 0; i < ARRAYSIZE(postfixes); i++) {
		String from, to;

		from = String("self.") + postfixes[i];
		to = prefix + name + "." + postfixes[i];

		_evaluator->setAlias(from, to);
		_evaluator->setVariable(to, EVAL_UNDEF_VAR);
	}

	for (i = 0; i < str.size(); i++) {
		if (isspace(str[i]) && level == 0) {
			value = _evaluator->eval(String(&(str.c_str()[start]), i - start), section, name, start);
			_evaluator->setVariable(prefix + name + "." + postfixes[npostfix++], value);
			start = i + 1;
		}
		if (str[i] == '(')
			level++;

		if (str[i] == ')') {
			if (level == 0) {
				error("Extra ')' in section: [%s] expression: \"%s\" start is at: %d",
					  section.c_str(), name.c_str(), start);
			}
			level--;
		}
	}

	if (level > 0)
		error("Missing ')' in section: [%s] expression: \"%s\" start is at: %d",
			  section.c_str(), name.c_str(), start);

	value = _evaluator->eval(String(&(str.c_str()[start]), i - start), section, name, start);

	// process VAR=VALUE construct
	if (npostfix == 0)
		_evaluator->setVariable(name, value);
	else
		_evaluator->setVariable(prefix + name + "." + postfixes[npostfix], value);

	// If we have all 4 parameters, set .x2 and .y2
	if (npostfix == 3) {
		_evaluator->setVariable(prefix + name + ".x2", 
			_evaluator->getVar(prefix + name + ".x") + _evaluator->getVar(prefix + name + ".w"));
		_evaluator->setVariable(prefix + name + ".y2", 
			_evaluator->getVar(prefix +name + ".y") + _evaluator->getVar(prefix + name + ".h"));
	}

	if (npostfix != 0)
		setSpecialAlias("prev", prefix + name);
}


void Theme::processResSection(Common::ConfigFile &config, String name, bool skipDefs, const String prefix) {
	debug(3, "Reading section: [%s]", name.c_str());

	const Common::ConfigFile::SectionKeyList &keys = config.getKeys(name);

	Common::ConfigFile::SectionKeyList::const_iterator iterk;
	for (iterk = keys.begin(); iterk != keys.end(); ++iterk) {
		if (iterk->key == "set_parent") {
			setSpecialAlias("parent", prefix + iterk->value);
			continue;
		}
		if (iterk->key.hasPrefix("set_")) {
			_evaluator->setAlias(name, iterk->key, prefix + iterk->value);
			continue;
		}
		if (iterk->key.hasPrefix("def_")) {
			if (!skipDefs)
				_evaluator->setVariable(name, prefix + iterk->key, iterk->value);
			continue;
		}
		if (iterk->key == "use") {
			if (iterk->value == name)
				error("Theme section [%s]: cannot use itself", name.c_str());
			if (!config.hasSection(name))
				error("Undefined use of section [%s]", name.c_str());
			processResSection(config, iterk->value, true);
			continue;
		}
		if (iterk->key == "useWithPrefix") {
			const char *temp = iterk->value.c_str();
            const char *pos = strrchr(temp, ' ');
			String n, pref;

			if (pos == NULL)
				error("2 arguments required for useWithPrefix keyword");

			n = String(temp, strchr(temp, ' ') - temp);
			pref = String(pos + 1);

			if (n == name)
				error("Theme section [%s]: cannot use itself", n.c_str());
			if (!config.hasSection(n))
				error("Undefined use of section [%s]", n.c_str());
			processResSection(config, n, true, pref);
			continue;
		}
		processSingleLine(name, prefix, iterk->key, iterk->value);
	}
}

void Theme::setSpecialAlias(const String alias, const String &name) {
	const char *postfixes[] = {"x", "y", "w", "h", "x2", "y2"};
	int i;

	for (i = 0; i < ARRAYSIZE(postfixes); i++) {
		String from, to;

		from = alias + "." + postfixes[i];
		to = name + "." + postfixes[i];

		_evaluator->setAlias(from, to);
	}
}

bool Theme::isThemeLoadingRequired() {
	int x = g_system->getOverlayWidth(), y = g_system->getOverlayHeight();

	if (_loadedThemeX == x && _loadedThemeY == y)
		return false;

	_loadedThemeX = x;
	_loadedThemeY = y;

	return true;
}

void Theme::loadTheme(Common::ConfigFile &config, bool reset) {
	char name[80];
	int x = g_system->getOverlayWidth(), y = g_system->getOverlayHeight();

	if (reset)
		_evaluator->reset();

	strcpy(name, "XxY");
	if (config.hasSection(name))
		processResSection(config, name);

	sprintf(name, "%dxY", x);
	if (config.hasSection(name))
		processResSection(config, name);

	sprintf(name, "Xx%d", y);
	if (config.hasSection(name))
		processResSection(config, name);

	sprintf(name, "%dx%d", x, y);
	if (config.hasSection(name))
		processResSection(config, name);
}

} // End of namespace GUI