File: gui_vmng_core.cpp

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (329 lines) | stat: -rw-r--r-- 10,634 bytes parent folder | download | duplicates (2)
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
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 *
 */

/**
 * THE GUI:
 * gui_vmng.cpp controls the windowing system. The gui is comprised of layered independent
 * windows.  The view manager controls which pieces of each window are visible, and which
 * window receives events.  The contents of of the windows, whether they be dialog boxes,
 * buffers, or some new addition has no bearing on the performance of the view manager.
 * Therefore, each window is created with a layer, an event handler, and a redraw function.
 * When the view manager determines that an area of a window needs to be redrawn, it
 * simply calls that window's redraw function.  It is up to the redraw function to ensure
 * that the rectangle is properly redrawn.  If an event occurs, the view manager will
 * determine which window should handle the event.
 *
 * To recap then, it manages the visual display of each window's current position
 * and relative layer, and when either a keyboard event, or mouse event is registered, which
 * window's evtHandler will be given the event to process. In addition to requesting a
 * window to redraw a portion of itself, or handle an event which has occurred, vmng.cpp
 * also displays the mouse in its current location.  Through the use of an off screen bitmap
 * which is an exact duplicate of what is visible on the monitor, the view manager creates
 * a flicker-free graphical display of the mouse and all visible windows.
 *
 * NOTE: FOR MANY OF THE FOLLOWING PROCEDURES, A "void *scrnContent" IS LISTED AMONG THE
 * PARAMETERS.  THIS PARAMETER REFERS TO THE STRUCTURE FOR WHICH THE WINDOW WAS CREATED, BE
 * IT A (Buffer*), (Dialog*), (TextScrn*), OR WHATEVER.  SINCE THE VIEW MANAGER ONLY
 * REQUESTS WINDOW REFRESHES AND PASSES EVENTS, THE CONTENTS OF THE WINDOW ARE UNKNOWN,
 * AND THEREFORE, ALL ARE STORED AS (void*). FROM NOW ON, THIS WILL BE KNOWN AS THE "WINDOW
 * IDENTIFIER".
 *
 * NOTE: THE TERM "WINDOW" AND THE TERM "SCREEN" ARE COMPLETELY INTERCHANGEABLE DURING
 * THE DOCUMENTATION OF ANY GUI SOURCE CODE.
 *
 * NOTE: ANY PROCEDURE IN THIS FILE WHICH, WHEN EXECUTED, RESULTS IN A VISUAL CHANGE TO 
 *          THE MONITOR (SUCH AS vmng_screen_show(), or MoveScreen())
 *			WILL ALSO RESTORE THE MONITOR'S IMAGE, TAKING CARE OF A VIDEO REFRESH REQUIREMENTS.
 */

#include "common/system.h"
#include "graphics/surface.h"
#include "m4/gui/gui_vmng.h"
#include "m4/gui/gui_dialog.h"
#include "m4/core/errors.h"
#include "m4/core/imath.h"
#include "m4/mem/memman.h"
#include "m4/mem/mem.h"
#include "m4/vars.h"

namespace M4 {

bool vmng_init() {
	if (_G(vmng_Initted))
		return false;
	_G(vmng_Initted) = true;

	_G(frontScreen) = nullptr;
	_G(backScreen) = nullptr;
	_G(inactiveScreens) = nullptr;

	if (!mem_register_stash_type(&_G(memtypeSCRN), sizeof(ScreenContext), 32, "+SCRN")) {
		return false;
	}
	if (!mem_register_stash_type(&_G(memtypeMATTE), sizeof(matte), 32, "+guiMATTE")) {
		return false;
	}
	if (!mem_register_stash_type(&_G(memtypeRECT), sizeof(RectList), 256, "+guiRecList")) {
		return false;
	}

	return true;
}

void vmng_shutdown() {
	ScreenContext *myScreen;
	Hotkey *myHotkeys, *tempHotkey;

	if (!_G(vmng_Initted))
		return;
	_G(vmng_Initted) = false;

	// First, destroy all active windows
	myScreen = _G(frontScreen);
	while (myScreen) {
		_G(frontScreen) = _G(frontScreen)->behind;
		if (myScreen->scrnType == SCRN_DLG) {
			vmng_Dialog_Destroy((Dialog *)myScreen->scrnContent);
		} else if (myScreen->scrnType == SCRN_TEXT) {
			vmng_TextScrn_Destroy((TextScrn *)myScreen->scrnContent);
		}

		myHotkeys = myScreen->scrnHotkeys;
		tempHotkey = myHotkeys;
		while (tempHotkey) {
			myHotkeys = myHotkeys->next;
			mem_free(tempHotkey);
			tempHotkey = myHotkeys;
		}

		mem_free_to_stash((void *)myScreen, _G(memtypeSCRN));
		myScreen = _G(frontScreen);
	}

	// Duplicate the above loop exactly for the list of inactive windows
	myScreen = _G(inactiveScreens);
	while (myScreen) {
		_G(inactiveScreens) = _G(inactiveScreens)->behind;
		if (myScreen->scrnType == SCRN_DLG) {
			vmng_Dialog_Destroy((Dialog *)myScreen->scrnContent);
		} else if (myScreen->scrnType == SCRN_TEXT) {
			vmng_TextScrn_Destroy((TextScrn *)myScreen->scrnContent);
		}
		myHotkeys = myScreen->scrnHotkeys;
		tempHotkey = myHotkeys;
		while (tempHotkey) {
			myHotkeys = myHotkeys->next;
			mem_free(tempHotkey);
			//mem_free_to_stash((void*)tempHotkey, memtypeHOTKEY);
			tempHotkey = myHotkeys;
		}

		mem_free_to_stash((void *)myScreen, _G(memtypeSCRN));
		myScreen = _G(inactiveScreens);
	}
}

ScreenContext *vmng_screen_create(int32 x1, int32 y1, int32 x2, int32 y2, int32 scrnType, uint32 scrnFlags,
	void *scrnContent, RefreshFunc redraw, EventHandler evtHandler) {
	ScreenContext *myScreen;

	if (!_G(vmng_Initted))
		return nullptr;

	if ((myScreen = (ScreenContext *)mem_get_from_stash(_G(memtypeSCRN), "+SCRN")) == nullptr)
		return nullptr;

	myScreen->x1 = x1;
	myScreen->y1 = y1;
	myScreen->x2 = x2;
	myScreen->y2 = y2;
	myScreen->scrnType = scrnType;
	myScreen->scrnFlags = scrnFlags;
	myScreen->scrnContent = scrnContent;
	myScreen->redraw = redraw;
	myScreen->evtHandler = evtHandler;
	myScreen->scrnHotkeys = nullptr;

	if (_G(inactiveScreens))
		_G(inactiveScreens)->infront = myScreen;

	myScreen->behind = _G(inactiveScreens);
	myScreen->infront = nullptr;
	_G(inactiveScreens) = myScreen;
	return myScreen;
}

void vmng_screen_dispose(void *scrnContent) {
	ScreenContext *myScreen;
	Hotkey *myHotkeys, *tempHotkey;
	if ((myScreen = ExtractScreen(scrnContent, SCRN_ANY)) == nullptr) return;
	RestoreScreens(myScreen->x1, myScreen->y1, myScreen->x2, myScreen->y2);
	myHotkeys = myScreen->scrnHotkeys;
	tempHotkey = myHotkeys;
	while (tempHotkey) {
		myHotkeys = myHotkeys->next;
		mem_free(tempHotkey);
		tempHotkey = myHotkeys;
	}
	mem_free_to_stash((void *)myScreen, _G(memtypeSCRN));
}

void vmng_screen_hide(void *scrnContent) {
	ScreenContext *myScreen;
	if ((myScreen = ExtractScreen(scrnContent, SCRN_ACTIVE)) == nullptr) return;
	RestoreScreens(myScreen->x1, myScreen->y1, myScreen->x2, myScreen->y2);
	myScreen->behind = _G(inactiveScreens);
	myScreen->infront = nullptr;
	if (_G(inactiveScreens))
		_G(inactiveScreens)->infront = myScreen;
	_G(inactiveScreens) = myScreen;
}

void vmng_screen_show(void *scrnContent) {
	ScreenContext *myScreen, *tempScreen;
	if ((myScreen = ExtractScreen(scrnContent, SCRN_ANY)) == nullptr)
		return;

	if (!_G(frontScreen)) {
		myScreen->infront = nullptr;
		myScreen->behind = nullptr;
		_G(frontScreen) = myScreen;
		_G(backScreen) = myScreen;

	} else {
		tempScreen = _G(frontScreen);
		while (tempScreen &&
			((tempScreen->scrnFlags & SF_LAYER) > (myScreen->scrnFlags & SF_LAYER))) {
			tempScreen = tempScreen->behind;
		}
		if (!tempScreen) {
			myScreen->behind = nullptr;
			myScreen->infront = _G(backScreen);
			_G(backScreen)->behind = myScreen;
			_G(backScreen) = myScreen;
		} else if (tempScreen == _G(frontScreen)) {
			myScreen->behind = _G(frontScreen);
			myScreen->infront = nullptr;
			_G(frontScreen)->infront = myScreen;
			_G(frontScreen) = myScreen;
		} else {
			myScreen->behind = tempScreen;
			myScreen->infront = tempScreen->infront;
			tempScreen->infront = myScreen;
			myScreen->infront->behind = myScreen;
		}
	}

	RestoreScreens(myScreen->x1, myScreen->y1, myScreen->x2, myScreen->y2);
}

ScreenContext *vmng_screen_find(void *scrnContent, int32 *status) {
	ScreenContext *myScreen;
	int32 myStatus = SCRN_ACTIVE;

	if (!_G(vmng_Initted))
		return nullptr;

	myScreen = _G(frontScreen);

	while (myScreen && (myScreen->scrnContent != scrnContent))
		myScreen = myScreen->behind;

	if (!myScreen) {
		myStatus = SCRN_INACTIVE;
		myScreen = _G(inactiveScreens);
		while (myScreen && (myScreen->scrnContent != scrnContent))
			myScreen = myScreen->behind;
	}

	if (status) {
		if (myScreen)
			*status = myStatus;
		else
			*status = SCRN_UNDEFN;
	}

	return myScreen;
}

void vmng_refresh_video(int32 scrnX, int32 scrnY, int32 x1, int32 y1, int32 x2, int32 y2, Buffer *srcBuffer) {
	assert(x2 <= srcBuffer->w && y2 <= srcBuffer->h);
	const byte *srcP = srcBuffer->data + (y1 * srcBuffer->stride) + x1;
	g_system->copyRectToScreen(srcP, srcBuffer->stride, scrnX, scrnY,
		x2 - x1 + 1, y2 - y1 + 1);
}

ScreenContext *ExtractScreen(void *scrnContent, int32 status) {
	ScreenContext *myScreen = nullptr, *tempScreen;
	if (!_G(vmng_Initted))
		return nullptr;

	if ((status == SCRN_ANY) || (status == SCRN_ACTIVE)) {
		// Search the active list, and remove the window if it is found
		myScreen = _G(frontScreen);

		while (myScreen && (myScreen->scrnContent != scrnContent))
			myScreen = myScreen->behind;

		if (myScreen) {
			if (myScreen == _G(frontScreen)) {
				if (myScreen == _G(backScreen)) {
					_G(frontScreen) = nullptr;
					_G(backScreen) = nullptr;
				} else {
					_G(frontScreen) = _G(frontScreen)->behind;
					_G(frontScreen)->infront = nullptr;
				}
			} else {
				tempScreen = myScreen->infront;
				tempScreen->behind = myScreen->behind;
				if (tempScreen->behind)
					tempScreen->behind->infront = tempScreen;
				else
					_G(backScreen) = tempScreen;
			}
		}
	}

	if (((status == SCRN_ANY) && (!myScreen)) || (status == SCRN_INACTIVE)) {
		// Search the inactive list and remove the window if it is found
		myScreen = _G(inactiveScreens);
		while (myScreen && (myScreen->scrnContent != scrnContent))
			myScreen = myScreen->behind;

		if (myScreen) {
			if (myScreen == _G(inactiveScreens)) {
				_G(inactiveScreens) = _G(inactiveScreens)->behind;
				if (_G(inactiveScreens))
					_G(inactiveScreens)->infront = nullptr;

			} else {
				tempScreen = myScreen->infront;
				tempScreen->behind = myScreen->behind;
				if (tempScreen->behind) tempScreen->behind->infront = tempScreen;
			}
		}
	}

	return myScreen;
}

} // End of namespace M4