aboutsummaryrefslogtreecommitdiff
path: root/graphics/macgui/macwindow.h
blob: f1e0cac7f39dd395911bbbb1597c7d888be05d0b (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
/* 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 GRAPHICS_MACGUI_MACWINDOW_H
#define GRAPHICS_MACGUI_MACWINDOW_H

#include "common/stream.h"

#include "graphics/managed_surface.h"
#include "graphics/transparent_surface.h"
#include "graphics/nine_patch.h"
#include "graphics/palette.h"

#include "graphics/macgui/macwindowborder.h"

namespace Graphics {

class MacWindowManager;
class MacWindowBorder;

namespace MacWindowConstants {
enum WindowType {
	kWindowUnknown,
	kWindowWindow,
	kWindowMenu
};

enum {
	kBorderWidth = 17
};

enum WindowClick {
	kBorderNone = 0,
	kBorderScrollUp,
	kBorderScrollDown,
	kBorderScrollLeft,
	kBorderScrollRight,
	kBorderCloseButton,
	kBorderInner,
	kBorderBorder,
	kBorderResizeButton
};
}
using namespace MacWindowConstants;

/**
 * Abstract class that defines common functionality for all window classes.
 * It supports event callbacks and drawing.
 */
class BaseMacWindow {
public:
	/**
	 * Base constructor.
	 * @param id ID of the window.
	 * @param editable True if the window is editable.
	 * @param wm Pointer to the MacWindowManager that owns the window.
	 */
	BaseMacWindow(int id, bool editable, MacWindowManager *wm);
	virtual ~BaseMacWindow() {}

	/**
	 * Accessor method for the complete dimensions of the window.
	 * @return Dimensions of the window (including border) relative to the WM's screen.
	 */
	const Common::Rect &getDimensions() { return _dims; }

	/**
	 * Accessor method to the id of the window.
	 * @return The id set in the constructor.
	 */
	int getId() { return _id; }

	/**
	 * Accessor method to the type of window.
	 * Each subclass must indicate it's type.
	 * @return The type of the window.
	 */
	WindowType getType() { return _type; }

	/**
	 * Accessor method to check whether the window is editable (e.g. for resizing).
	 * @return True if the window is editable as indicated in the constructor.
	 */
	bool isEditable() { return _editable; }

	/**
	 * Method to access the entire surface of the window (e.g. to draw an image).
	 * @return A pointer to the entire surface of the window.
	 */
	ManagedSurface *getSurface() { return &_surface; }

	/**
	 * Abstract method for indicating whether the window is active or inactive.
	 * Used by the WM to handle focus on windows, etc.
	 * @param active Desired state of the window.
	 */
	virtual void setActive(bool active) = 0;

	/**
	 * Method for marking the window for redraw.
	 * @param dirty True if the window needs to be redrawn.
	 */
	void setDirty(bool dirty) { _contentIsDirty = dirty; }

	/**
	 * Method called to draw the window into the target surface.
	 * This method is most often called by the WM, and relies on
	 * the window being marked as dirty unless otherwise specified.
	 * @param g Surface on which to draw the window.
	 * @param forceRedraw It's behavior depends on the subclass.
	 */
	virtual bool draw(ManagedSurface *g, bool forceRedraw = false) = 0;

	/**
	 * Method called by the WM when there is an event concerning the window.
	 * Note that depending on the subclass of the window, it might not be called
	 * if the window is not active.
	 * @param event Event to be processed.
	 * @return true If the event was successfully consumed and processed.
	 */
	virtual bool processEvent(Common::Event &event) = 0;

	virtual bool hasAllFocus() = 0;

	/**
	 * Set the callback that will be used when an event needs to be processed.
	 * @param callback A function pointer to a function that accepts:
	 *					- A WindowClick, the pert of the window that was clicked.
	 *					- The event to be processed.
	 *					- Any additional required data (e.g. the engine's GUI).
	 */
	void setCallback(bool (*callback)(WindowClick, Common::Event &, void *), void *data) { _callback = callback; _dataPtr = data; }

protected:
	int _id;
	WindowType _type;

	bool _editable;

	ManagedSurface _surface;
	bool _contentIsDirty;

	Common::Rect _dims;

	bool (*_callback)(WindowClick, Common::Event &, void *);
	void *_dataPtr;

	MacWindowManager *_wm;
};

/**
 * An implementation of an ordinary window in the Mac interface.
 * It supports custom resizing, scrolling, borders, etc.
 */
class MacWindow : public BaseMacWindow {
public:
	/**
	 * Construct a simple window, with the default settings.
	 * Note that the scroll must be implemented in the event handling,
	 * even if the scrollable flag is set to true.
	 * @param id See BaseMacWindow.
	 * @param scrollable True if the window can be scrolled.
	 * @param resizable True if the window can be resized.
	 * @param editable See BaseMacWindow.
	 * @param wm See BaseMacWindow.
	 */
	MacWindow(int id, bool scrollable, bool resizable, bool editable, MacWindowManager *wm);
	virtual ~MacWindow();

	/**
	 * Change the window's location to fixed coordinates (not delta).
	 * @param x New left position of the window relative to the WM's screen.
	 * @param y New top position of the window relative to the WM's screen.
	 */
	void move(int x, int y);

	/*
	 * Change the width and the height of the window.
	 * @param w New width of the window.
	 * @param h New height of the window.
	 */
	virtual void resize(int w, int h);

	/**
	 * Change the dimensions of the window ([0, 0, 0, 0] by default).
	 * Note that this can be used to update both the position and the size
	 * of the window, although move() and resize() might be more comfortable.
	 * @param r The desired dimensions of the window.
	 */
	void setDimensions(const Common::Rect &r);

	/**
	 * Accessor to retrieve the dimensions of the inner surface of the window
	 * (i.e. without taking borders into account).
	 * Note that the returned dimensions' position is relative to the WM's
	 * screen, just like in getDimensions().
	 * @return The inner dimensions of the window.
	 */
	const Common::Rect &getInnerDimensions() { return _innerDims; }

	/**
	 * Set a background pattern for the window.
	 * @param pattern
	 */
	void setBackgroundPattern(int pattern);

	/**
	 * Similar to that described in BaseMacWindow.
	 * @param g See BaseMacWindow.
	 * @param forceRedraw If true, the borders are guarranteed to redraw.
	 */
	virtual bool draw(ManagedSurface *g, bool forceRedraw = false);

	/**
	 * Mutator to change the active state of the window.
	 * Most often called from the WM.
	 * @param active Target state.
	 */
	void setActive(bool active);
	/**
	 * Accessor to determine whether a window is active.
	 * @return True if the window is active.
	 */
	bool isActive();

	/**
	 * Mutator to change the title of the window.
	 * @param title Target title of the window.
	 */
	void setTitle(Common::String &title) { _title = title; }
	/**
	 * Highlight the target part of the window.
	 * Used for the default borders.
	 * @param highlightedPart Part to be highlighted.
	 */
	void setHighlight(WindowClick highlightedPart);
	/**
	 * Set the scroll poisition.
	 * @param scrollPos Target scroll position.
	 * @param scrollSize Size of the scrolling bar.
	 */
	void setScroll(float scrollPos, float scrollSize);
	/**
	 * See BaseMacWindow.
	 */
	virtual bool processEvent(Common::Event &event);
	bool hasAllFocus() { return _beingDragged || _beingResized; }

	/**
	 * Set arbitrary border from a BMP data stream, with custom border offsets.
	 * Note that the BMP has to be 9patch compliant. For examples, go to:
	 * https://github.com/blorente/MacVenture-Extract-Guide/tree/master/borders
	 * @param file The BMP data stream with the desired border.
	 * @param active Whether the border corresponds with the active state of the window.
	 * @param lo Width of the left side of the border, in pixels.
	 * @param ro Width of the right side of the border, in pixels.
	 * @param to Width of the top side of the border, in pixels.
	 * @param bo Width of the bottom side of the border, in pixels.
	 */
	void loadBorder(Common::SeekableReadStream &file, bool active, int lo, int ro, int to, int bo);
	//void setBorder(TransparentSurface &border, bool active);

	/**
	 * Indicate whether the window can be closed (false by default).
	 * @param closeable True if the window can be closed.
	 */
	void setCloseable(bool closeable);

private:
	void prepareBorderSurface(ManagedSurface *g);
	void drawSimpleBorder(ManagedSurface *g);
	void drawBorderFromSurface(ManagedSurface *g);
	void drawPattern();
	void drawBox(ManagedSurface *g, int x, int y, int w, int h);
	void fillRect(ManagedSurface *g, int x, int y, int w, int h, int color);
	const Font *getTitleFont();
	void updateInnerDims();

	bool isInCloseButton(int x, int y);
	bool isInResizeButton(int x, int y);
	WindowClick isInScroll(int x, int y);

protected:
	void drawBorder();
	WindowClick isInBorder(int x, int y);

protected:
	ManagedSurface _borderSurface;
	ManagedSurface _composeSurface;

	bool _borderIsDirty;

private:
	MacWindowBorder _macBorder;

	int _pattern;
	bool _hasPattern;

	bool _scrollable;
	bool _resizable;
	bool _active;

	bool _closeable;

	int _borderWidth;
	Common::Rect _innerDims;

	bool _beingDragged, _beingResized;
	int _draggedX, _draggedY;

	WindowClick _highlightedPart;
	float _scrollPos, _scrollSize;

	Common::String _title;
};



} // End of namespace Graphics

#endif