aboutsummaryrefslogtreecommitdiff
path: root/engines/sci/gui/gui_windowmgr.cpp
blob: 3a474a1a38fd1f6c607621beccd6c3cf9739e6bb (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
/* 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 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 "common/util.h"

#include "sci/sci.h"
#include "sci/engine/state.h"
#include "sci/tools.h"
#include "sci/gui/gui_gfx.h"
#include "sci/gui/gui_windowmgr.h"
#include "sci/gui/gui_memmgr.h"

namespace Sci {

Common::Rect _picRect(0,10,320, 200);

// window styles
enum {
	TRANSPARENT = 1,
	NOFRAME		= 2,
	TITLE		= 4,
	TOPMOST		= 8,
	USER		= 0x80
};

SciGUIwindowMgr::SciGUIwindowMgr(EngineState *state, SciGUIgfx *gfx)
	: _s(state), _gfx(gfx) {

	// FIXME: remove memmgr
	InitMem(0x320);

	HEAPHANDLE wmgrPortH = heapNewPtr(sizeof(sciPort), kDataPort, "wmgrPort");
	heapClearPtr(wmgrPortH);
	_wmgrPort = (sciPort *)heap2Ptr(wmgrPortH);

	int16 offTop = 20;

	_gfx->OpenPort(_wmgrPort);
	_gfx->SetPort(_wmgrPort);
	_gfx->SetOrigin(0, offTop);
	_wmgrPort->rect.bottom = 200 - offTop;
	_wmgrPort->rect.right = 320;
	_wmgrPort->rect.moveTo(0, 0);
	_wmgrPort->curTop = 0;
	_wmgrPort->curLeft = 0;

	windowList.AddToFront(wmgrPortH);

	_picWind = NewWindow(&_picRect, 0, 0, TRANSPARENT | NOFRAME, 0, 1);
}

SciGUIwindowMgr::~SciGUIwindowMgr() {
}

int16 SciGUIwindowMgr::isFrontWindow(sciWnd *pWnd) {
	if (heap2Ptr(windowList.getLast()) == (byte *)pWnd)
		return 1;
	return 0;
}

void SciGUIwindowMgr::SelectWindow(HEAPHANDLE hh) {
	sciPort *port = (sciPort *)heap2Ptr(hh);
	_gfx->SetPort(port);
	if (hh != windowList.getLast()) { // selecting not topmost window
		sciWnd *prevwnd = (sciWnd *)heap2Ptr(port->node.prev);
		BeginUpdate(prevwnd);
		windowList.MoveToEnd(hh);
		EndUpdate(prevwnd);
	}
	_gfx->SetPort(port);
}

void SciGUIwindowMgr::BeginUpdate(sciWnd *wnd) {
	sciPort *oldPort = _gfx->SetPort(_wmgrPort);
	sciWnd *node = (sciWnd *)heap2Ptr(windowList.getLast());
	while (node != wnd) {
		UpdateWindow(node);
		node = (sciWnd *)heap2Ptr(node->node.prev);
	}
	_gfx->SetPort(oldPort);
}

void SciGUIwindowMgr::EndUpdate(sciWnd *wnd) {
	sciPort *oldPort = _gfx->SetPort(_wmgrPort);
	sciWnd *last = (sciWnd *)heap2Ptr(windowList.getLast());
	while (wnd != last) {
		wnd = (sciWnd *)heap2Ptr(wnd->node.next);
		UpdateWindow(wnd);
	}
	_gfx->SetPort(oldPort);
}

SCILanguage SciGUIwindowMgr::getSCILanguage() {
	return kLangEnglish;
}

char *SciGUIwindowMgr::StrSplit(char *buff, const char *msg, const char *fmt) {
	SCILanguage gameLang = getSCILanguage();
	SCILanguage subtitleLang = kLangNone;
	char *retval;
//	if (_theGame.getHandle())
		//subtitleLang = (SCILanguage)_theGame.getProperty(0x58); // subtitleLang property

	if (buff == msg) {
		char str[2000];
		getIntlString(str, msg, fmt, gameLang, subtitleLang);
		retval = strcpy(buff, str);
	} else
		retval = getIntlString(buff, msg, fmt, gameLang, subtitleLang);
	return retval;
}
//--------------------------------
// In multilanguage game the msg has format ___english_text__#I___italian_text___
// The function should place in buff a translated part of msg or the 1st one if a translation
// does not exist
char *SciGUIwindowMgr::getIntlString(char *buff, const char *msg, const char *fmt,
		SCILanguage gameLang, SCILanguage subtitleLang) {

	// prefer subtitleLang if set
	SCILanguage lang = subtitleLang != kLangNone ? subtitleLang : gameLang;
	const char *ptr = msg, *szFrom;
	char ch;
	int nLen = 0;
	// searching for language code in msg
	while (*ptr) {
		ch = *(ptr + 1);
		if(*ptr == '#' && (ch == 'I' || ch == 'F' || ch == 'G' || ch == 'S')) {
			ptr +=2;
			break;
		}
	ptr++;
	}
	// if a language code was found...
	if (*ptr) {
		if ((lang == kLangItalian && ch == 'I') || (lang == kLangFrench && ch == 'F') ||
				(lang == kLangGerman && ch == 'G') || (lang == kLangSpanish && ch == 'S')) {
			nLen = (int)strlen(ptr);
			szFrom = ptr;
		} else {
			nLen = ptr - msg - 2;
			szFrom = msg;
		}
	} else {
		nLen = ptr - msg;
		szFrom = msg;
	}
	if (fmt && subtitleLang != kLangNone) {
		strcpy(buff, fmt);
		strncat(buff, szFrom, nLen);
		buff[nLen + strlen(fmt)] = 0;
	} else {
		strncpy(buff, szFrom, nLen);
		buff[nLen] = 0;
	}
	return buff;
}

sciWnd *SciGUIwindowMgr::NewWindow(Common::Rect *rect, Common::Rect *rect2, const char *title, uint16 style, uint16 arg8, uint16 argA) {
	HEAPHANDLE hWnd = heapNewPtr(sizeof(sciWnd), kDataWindow, title);
	Common::Rect r;

	if (!hWnd) {
		warning("Can't open window!");
		return 0;
	}
	heapClearPtr(hWnd);
	if (style & TOPMOST)
		windowList.AddToFront(hWnd);
	else
		windowList.AddToEnd(hWnd);
	sciWnd *pwnd = (sciWnd *)heap2Ptr(hWnd);
	_gfx->OpenPort((sciPort *)pwnd);
	r = *rect;
	pwnd->rect = *rect;
	if (rect2)
		pwnd->rect1 = *rect2;
	
	pwnd->wndStyle = style;
	pwnd->hSaved1 = pwnd->hSaved2 = NULL_REG;
	pwnd->bDrawed = false;
	if ((style & TRANSPARENT) == 0)
		pwnd->uSaveFlag = (arg8 == 0xFFFF ? 1 : 3);
	
	if (title && (style & TITLE)) {
		HEAPHANDLE hTitle = heapNewPtr((uint16)strlen(title) + 1, kDataString, title);
		if (!hTitle) {
			warning("Can't open window!");
			return 0;
		}
		pwnd->hTitle = hTitle;
		StrSplit((char *)heap2Ptr(hTitle), title, NULL);
	}
	
	r = *rect;
	if (style == USER || (style & NOFRAME) == 0) {
		r.grow(1);
		if (style & TITLE) {
			r.top -= 10;
			r.bottom++;
		}
	}
	
	pwnd->rect0 = r;
	const Common::Rect *wmprect = &_wmgrPort->rect;
	int16 oldtop = pwnd->rect0.top;
	int16 oldleft = pwnd->rect0.left;
	if (wmprect->top > pwnd->rect0.top)
		pwnd->rect0.moveTo(pwnd->rect0.left, wmprect->top);
	
	if (wmprect->bottom < pwnd->rect0.bottom)
		pwnd->rect0.moveTo(pwnd->rect0.left, wmprect->bottom
				- pwnd->rect0.bottom + pwnd->rect0.top);
	
	if (wmprect->right < pwnd->rect0.right)
		pwnd->rect0.moveTo(wmprect->right + pwnd->rect0.left
				- pwnd->rect0.right, pwnd->rect0.top);
	
	if (wmprect->left > pwnd->rect0.left)
		pwnd->rect0.moveTo(wmprect->left, pwnd->rect0.top);
	
	pwnd->rect.moveTo(pwnd->rect.left + pwnd->rect0.left - oldleft,
			pwnd->rect.top + pwnd->rect0.top - oldtop);
	if (rect2 == 0)
		pwnd->rect1 = pwnd->rect0;
	
	if (argA)
		DrawWindow(pwnd);
	_gfx->SetPort((sciPort *)pwnd);
	_gfx->SetOrigin(pwnd->rect.left, pwnd->rect.top + _wmgrPort->top);
	pwnd->rect.moveTo(0, 0);
	return pwnd;
}

void SciGUIwindowMgr::DrawWindow(sciWnd *pWnd) {
	if (pWnd->bDrawed)
		return;
	Common::Rect r;
	int16 wndStyle = pWnd->wndStyle;

	pWnd->bDrawed = true;
	sciPort *oldport = _gfx->SetPort(_wmgrPort);
	_gfx->PenColor(0);
	if ((wndStyle & TRANSPARENT) == 0) {
		pWnd->hSaved1 = _gfx->SaveBits(pWnd->rect1, 1);
		if (pWnd->uSaveFlag & 2) {
			pWnd->hSaved2 = _gfx->SaveBits(pWnd->rect1, 2);
			if ((wndStyle & USER) == 0)
				_gfx->FillRect(pWnd->rect1, 2, 0, 0xF);
		}
	}
	
	// drawing frame,shadow and title
	if ((wndStyle & USER) == 0) {
		r = pWnd->rect0;
		if ((wndStyle & NOFRAME) == 0) {
			r.translate(1, 1);
			_gfx->FrameRect(r);// shadow
			r.translate(-1, -1);
			_gfx->FrameRect(r);// window frame
			if (wndStyle & TITLE) {
				_gfx->FrameRect(r);
				r.grow(-1);
				_gfx->FillRect(r, 1, 0);
				if (pWnd->hTitle) {
					int16 oldcolor = _gfx->GetPort()->penClr;
					_gfx->PenColor(255);
					_gfx->TextBox((char *)heap2Ptr(pWnd->hTitle), 1, r, 1, 0);
					_gfx->PenColor(oldcolor);
				}
				
				r = pWnd->rect0;
				r.top += 9;
			}
			
			r.grow(-1);
		}
		
		if ((wndStyle & TRANSPARENT) == 0)
			_gfx->FillRect(r, 1, pWnd->backClr);
		_gfx->ShowBits(pWnd->rect0, 1);
	}
	_gfx->SetPort(oldport);
}

void SciGUIwindowMgr::DisposeWindow(sciWnd *pWnd, int16 arg2) {
	_gfx->SetPort(_wmgrPort);
	_gfx->RestoreBits(pWnd->hSaved1);
	_gfx->RestoreBits(pWnd->hSaved2);
	if (arg2)
		_gfx->ShowBits(pWnd->rect0, 1);
//	else
//		g_sci->ReAnimate(&pwnd->rect0);
	HEAPHANDLE hh = ptr2heap((byte *)pWnd);
	windowList.DeleteNode(hh);
	SelectWindow(windowList.getLast());
	if (pWnd->hTitle)
		heapDisposePtr(pWnd->hTitle);
	heapDisposePtr(hh);
}

void SciGUIwindowMgr::UpdateWindow(sciWnd *wnd) {
}

} // end of namespace Sci