aboutsummaryrefslogtreecommitdiff
path: root/engines/glk/windows.h
blob: bdd9674e43b9168bfeead464f664e0da7ef0008f (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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
/* 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.
 *
 */

#ifndef GLK_WINDOWS_H
#define GLK_WINDOWS_H

#include "common/array.h"
#include "common/list.h"
#include "common/rect.h"
#include "graphics/screen.h"
#include "glk/events.h"
#include "glk/fonts.h"
#include "glk/glk_types.h"
#include "glk/screen.h"
#include "glk/selection.h"
#include "glk/streams.h"

namespace Glk {

class Window;
class PairWindow;

#define HISTORYLEN 100
#define SCROLLBACK 512
#define TBLINELEN 300
#define GLI_SUBPIX 8


/**
 * Main windows manager
 */
class Windows {
	friend class Window;
public:
	class iterator {
	private:
		Windows *_windows;
		Window *_current;
	public:
		/**
		 * Constructor
		 */
		iterator(Windows *windows, Window *start) : _windows(windows), _current(start) {}

		/**
		 * Dereference
		 */
		Window *operator*() const {
			return _current;
		}

		/**
		 * Move to next
		 */
		iterator &operator++();

		/**
		 * Equality test
		 */
		bool operator==(const iterator &i) {
			return _current == i._current;
		}

		/**
		 * Inequality test
		 */
		bool operator!=(const iterator &i) {
			return _current != i._current;
		}
	};
	friend class iterator;
private:
	Graphics::Screen *_screen;
	Window *_windowList;       ///< List of all windows
	Window *_rootWin;          ///< The topmost window
	Window *_focusWin;         ///< The window selected by the player
	bool _drawSelect;
private:
	/**
	 * Create a new window
	 */
	Window *newWindow(uint type, uint rock);

	/**
	 * Create a new pair window
	 */
	PairWindow *newPairWindow(uint method, Window *key, uint size);

	/**
	 * Set the window focus
	 */
	void refocus(Window *win);

	/**
	 * Used to loop over windows in tree order
	 */
	Window *iterateTreeOrder(Window *win);

	/**
	 * Pick first window which has a more request
	 */
	void inputMoreFocus();

	/**
	 *
	 */
	void inputNextFocus();

	/**
	 * Pick first window which might want scrolling.
	 * This is called after pressing page keys.
	 */
	void inputScrollFocus();
public:
	static bool _overrideReverse;
	static bool _overrideFgSet;
	static bool _overrideBgSet;
	static bool _forceRedraw;
	static bool _claimSelect;
	static bool _moreFocus;
	static uint _overrideFgVal;
	static uint _overrideBgVal;
	static uint _zcolor_fg, _zcolor_bg;
	static uint _zcolor_LightGrey;
	static uint _zcolor_Foreground;
	static uint _zcolor_Background;
	static uint _zcolor_Bright;

	static uint rgbShift(uint color);
public:
	/**
	 * Constructor
	 */
	Windows(Graphics::Screen *screen);

	/**
	 * Destructor
	 */
	~Windows();

	/**
	 * Open a new window
	 */
	Window *windowOpen(Window *splitwin, uint method, uint size,
					   uint wintype, uint rock);

	/**
	 * Close an existing window
	 */
	void windowClose(Window *win, StreamResult *result = nullptr);

	/**
	 * Return the root window
	 */
	Window *getRoot() const {
		return _rootWin;
	}

	/**
	 * Gets the focused window
	 */
	Window *getFocusWindow() const {
		return _focusWin;
	}

	/**
	 * Setst the focused window
	 */
	void setFocus(Window *win) {
		_focusWin = win;
	}

	/**
	 * Pick first window which might want input. This is called after every keystroke.
	 */
	void inputGuessFocus();

	/**
	 * Handle input keypress
	 */
	void inputHandleKey(uint key);

	/**
	 * Handle mouse clicks
	 */
	void inputHandleClick(const Point &pos);

	void selectionChanged();

	void clearClaimSelect() {
		_claimSelect = false;
	}

	/**
	 * Rearrange windows
	 */
	void rearrange();

	void redraw();

	void redrawRect(const Rect &r);

	/**
	 * Repaint an area of the windows
	 */
	void repaint(const Rect &box);

	/**
	 * Get an iterator that will move over the tree
	 */
	iterator begin() {
		return iterator(this, _windowList);
	}

	/**
	 * Returns the end point of window iteration
	 */
	iterator end() {
		return iterator(this, nullptr);
	}
};

/**
 * Used for the static definition of default styles
 */
struct WindowStyleStatic {
	FACES font;
	byte bg[3];
	byte fg[3];
	bool reverse;
};

/**
 * Window styles
 */
struct WindowStyle {
	FACES font;
	uint bg;
	uint fg;
	bool reverse;

	/**
	 * Constructor
	 */
	WindowStyle() : font(MONOR), fg(0), bg(0), reverse(false) {}

	/**
	 * Constructor
	 */
	WindowStyle(const WindowStyleStatic &src);

	/**
	 * Equality comparison
	 */
	bool operator==(const WindowStyle &src) const {
		return !memcmp(this, &src, sizeof(WindowStyle));
	}

	/**
	 * Returns true if the font is proportinate
	 */
	bool isProp() const {
		return font == PROPR || font == PROPI || font == PROPB || font == PROPZ;
	}

	/**
	 * Returns true ifont the font is bold
	 */
	bool isBold() const {
		return font == PROPB || font == PROPZ || font == MONOB || font == MONOZ;
	}

	/**
	 * Returns true ifont the font is italic
	 */
	bool isItalic() const {
		return font == PROPI || font == PROPZ || font == MONOI || font == MONOZ;
	}

	/**
	 * Returns a font that has the following combination of proportinate, bold, and italic
	 */
	static FACES makeFont(bool p, bool b, bool i) {
		if (p && !b && !i) return PROPR;
		if (p && !b &&  i) return PROPI;
		if (p &&  b && !i) return PROPB;
		if (p &&  b &&  i) return PROPZ;
		if (!p && !b && !i) return MONOR;
		if (!p && !b &&  i) return MONOI;
		if (!p &&  b && !i) return MONOB;
		if (!p &&  b &&  i) return MONOZ;
		return PROPR;
	}
};

/**
 * Window attributes
 */
struct Attributes {
	unsigned fgset   : 1;
	unsigned bgset   : 1;
	unsigned reverse : 1;
	unsigned         : 1;
	unsigned style   : 4;
	uint fgcolor;
	uint bgcolor;
	uint hyper;

	/**
	 * Constructor
	 */
	Attributes() {
		clear();
	}

	/**
	 * Clear
	 */
	void clear();

	/**
	 * Set the style
	 */
	void set(uint s) {
		clear();
		style = s;
	}

	/**
	 * Equality comparison
	 */
	bool operator==(const Attributes &src) const {
		return fgset == src.fgset && bgset == src.bgset && reverse == src.reverse
			   && style == src.style && fgcolor == src.fgcolor && bgcolor == src.bgcolor
			   && hyper == src.hyper;
	}
	/**
	 * Inequality comparison
	 */
	bool operator!=(const Attributes &src) const {
		return fgset != src.fgset || bgset != src.bgset || reverse != src.reverse
			   || style != src.style || fgcolor != src.fgcolor || bgcolor != src.bgcolor
			   || hyper != src.hyper;
	}

	/**
	 * Return the background color for the current font style
	 */
	uint attrBg(const WindowStyle *styles);

	/**
	 * Return the foreground color for the current font style
	 */
	uint attrFg(const WindowStyle *styles);

	/**
	 * Get the font for the current font style
	 */
	FACES attrFont(const WindowStyle *styles) const {
		return styles[style].font;
	}
};

/**
 * Window definition
 */
class Window {
public:
	Windows *_windows;
	uint _rock;
	uint _type;

	Window *_parent;       ///< pair window which contains this one
	Window *_next, *_prev; ///< in the big linked list of windows
	Rect _bbox;
	int _yAdj;

	Stream *_stream;       ///< the window stream.
	Stream *_echoStream;   ///< the window's echo stream, if any.

	bool _lineRequest;
	bool _lineRequestUni;
	bool _charRequest;
	bool _charRequestUni;
	bool _mouseRequest;
	bool _hyperRequest;
	bool _moreRequest;
	bool _scrollRequest;
	bool _imageLoaded;

	uint _echoLineInputBase;
	uint *_lineTerminatorsBase;
	uint _termCt;

	Attributes _attr;
	uint _bgColor, _fgColor;

	gidispatch_rock_t _dispRock;
public:
	static bool checkBasicTerminators(uint32 ch);
public:
	/**
	 * Constructor
	 */
	Window(Windows *windows, uint rock);

	/**
	 * Destructor
	 */
	virtual ~Window();

	/**
	 * Close and delete the window
	 */
	void close(bool recurse = true);

	/**
	 * Get the font info structure associated with the window
	 */
	virtual FontInfo *getFontInfo();

	/**
	 * Rearranges the window
	 */
	virtual void rearrange(const Rect &box) {
		_bbox = box;
	}

	/**
	 * Set the size of a window
	 */
	virtual void setSize(const Point &newSize) {
		_bbox.setWidth(newSize.x);
		_bbox.setHeight(newSize.y);
		rearrange(_bbox);
	}

	/**
	 * Sets the position of a window
	 */
	virtual void setPosition(const Point &newPos) {
		_bbox.moveTo(newPos);
		rearrange(_bbox);
	}

	/**
	 * Get window split size within parent pair window
	 */
	virtual uint getSplit(uint size, bool vertical) const {
		return 0;
	}

	/**
	 * Write a character
	 */
	virtual void putCharUni(uint32 ch) {}

	/**
	 * Unput a unicode character
	 */
	virtual bool unputCharUni(uint32 ch) {
		return false;
	}

	/**
	 * Get the cursor position
	 */
	virtual Point getCursor() const { return Point(); }

	/**
	 * Move the cursor
	 */
	virtual void moveCursor(const Point &newPos);

	/**
	 * Clear the window
	 */
	virtual void clear() {}

	/**
	 * Click the window
	 */
	virtual void click(const Point &newPos) {}

	/**
	 * Prepare for inputing a line
	 */
	virtual void requestLineEvent(char *buf, uint maxlen, uint initlen);

	/**
	 * Prepare for inputing a line
	 */
	virtual void requestLineEventUni(uint32 *buf, uint maxlen, uint initlen);

	/**
	 * Cancel an input line event
	 */
	virtual void cancelLineEvent(Event *ev);

	/**
	 * Cancel a character event
	 */
	virtual void cancelCharEvent() {}

	/**
	 * Cancel a mouse event
	 */
	virtual void cancelMouseEvent() {}

	/**
	 * Cancel a hyperlink event
	 */
	virtual void cancelHyperlinkEvent() {}

	/**
	 * Redraw the window
	 */
	virtual void redraw();

	bool imageDraw(uint image, uint align, int val1, int val2);

	int acceptScroll(uint arg);

	bool checkTerminators(uint32 ch);

	void setTerminatorsLineEvent(const uint32 *keycodes, uint count);

	virtual void acceptReadLine(uint32 arg);

	virtual void acceptReadChar(uint arg);

	virtual void getArrangement(uint *method, uint *size, Window **keyWin);

	virtual void setArrangement(uint method, uint size, Window *keyWin);

	virtual void getSize(uint *width, uint *height) const;

	virtual void requestCharEvent();

	virtual void requestCharEventUni();

	virtual void setEchoLineEvent(uint val) {}

	virtual void requestMouseEvent() {}

	virtual void requestHyperlinkEvent() {}

	virtual void flowBreak();

	virtual void eraseRect(bool whole, const Rect &box);

	virtual void fillRect(uint color, const Rect &box);

	virtual void setBackgroundColor(uint color);

	/**
	 * Returns a pointer to the styles for the window
	 */
	virtual const WindowStyle *getStyles() const;
};
typedef Window *winid_t;

/**
 * Blank window
 */
class BlankWindow : public Window {
public:
	/**
	 * Constructor
	 */
	BlankWindow(Windows *windows, uint rock);
};

} // End of namespace Glk

#endif