File: macwindow.h

package info (click to toggle)
scummvm 2.9.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 450,268 kB
  • sloc: cpp: 4,297,604; asm: 28,322; python: 12,901; sh: 11,219; java: 8,477; xml: 7,843; perl: 2,633; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (437 lines) | stat: -rw-r--r-- 12,723 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
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
/* 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/>.
 *
 */

#ifndef GRAPHICS_MACGUI_MACWINDOW_H
#define GRAPHICS_MACGUI_MACWINDOW_H

#include "common/stream.h"

#include "graphics/managed_surface.h"
#include "graphics/nine_patch.h"
#include "graphics/font.h"

#include "graphics/macgui/macwidget.h"
#include "graphics/macgui/macwindowborder.h"

namespace Graphics {

class MacWindowManager;
class MacWindowBorder;
class MacWidget;

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

enum {
	kBorderWidth = 17
};

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

enum {
	kWindowModeDynamicScrollbar = 1 << 0
};
}
using namespace MacWindowConstants;

/**
 * Abstract class that defines common functionality for all window classes.
 * It supports event callbacks and drawing.
 */
class BaseMacWindow : public MacWidget {
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 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; }

	/**
	 * Mutator to change the visible state of the window.
	 * @param visible Target state.
	 */
	virtual void setVisible(bool visible, bool silent = false);
	/**
	 * Accessor to determine whether a window is active.
	 * @return True if the window is active.
	 */
	bool isVisible();

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

	/**
	 * Method to access the border surface of the window.
	 * @return A pointer to the border surface of the window.
	 */
	virtual ManagedSurface *getBorderSurface() = 0;

	/**
	 * 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.
	 */
	virtual const Common::Rect &getInnerDimensions() = 0;

	/**
	 * Method called to internally draw the window. This relies on the window
	 * being marked as dirty unless otherwise specified.
	 * @param forceRedraw Its behavior depends on the subclass.
	 */
	virtual bool draw(bool forceRedraw = false) = 0;

	/**
	 * 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;

	/**
	 * Method that checks if the window is needs redrawing.
	 */
	virtual bool isDirty() = 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;

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

	bool _visible;

	bool _draggable;
};

/**
 * 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 (outer dimensions).
	 * @param w New width of the window.
	 * @param h New height of the window.
	 */
	virtual void resize(int w, int h);

	/*
	 * Change the width and the height of the inner window.
	 * @param w New width of the window.
	 * @param h New height of the window.
	 */
	virtual void resizeInner(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) override;

	/**
	 * Change the inner dimension of the window.
	 * Note that this changes the window inner dimension and calculates
	 * outer dimension (ie with border, etc)
	 * @param r The desired dimensions of the window.
	 */
	void setInnerDimensions(const Common::Rect &r);

	/**
	 * 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.
	 */
	bool draw(ManagedSurface *g, bool forceRedraw = false) override;

	bool draw(bool forceRedraw = false) override;
	void blit(ManagedSurface *g, Common::Rect &dest) override;

	const Common::Rect &getInnerDimensions() override { return _innerDims; }
	ManagedSurface *getBorderSurface() override { return &_borderSurface; }

	/**
	 * Centers the window using the dimensions of the parent window manager, or undoes this; does
	 * nothing if WM is null.
	 */
	void center(bool toCenter = true);

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

	/**
	 * Mutator to change the title of the window.
	 * @param title Target title.
	 */
	void setTitle(const Common::String &title);

	/**
	 * Set visibility of window title.
	 * @param visible visibility of window.
	 */
	virtual void setTitleVisible(bool visible);

	/**
	 * Get visibility of window title.
	 */
	bool isTitleVisible();

	/**
	 * Accessor to get the title of the window.
	 * @return Title.
	 */
	const Common::String &getTitle() const { return _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.
	 */
	bool processEvent(Common::Event &event) override;
	bool hasAllFocus() override { 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, uint32 flags, int lo = -1, int ro = -1, int to = -1, int bo = -1);
	void loadBorder(Common::SeekableReadStream &file, uint32 flags, BorderOffsets offsets);
	void setBorder(Graphics::ManagedSurface *surface, uint32 flags, BorderOffsets offsets);
	void disableBorder();
	void loadInternalBorder(uint32 flags);
	/**
	 * we better set this before we load the border
	 * @param scrollbar state
	 */
	void enableScrollbar(bool active) { _hasScrollBar = active; }

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

	/**
	 * Mutator to change the border type.
	 * @param borderType Border type.
	 */
	void setBorderType(int borderType);
	/**
	 * Accessor to get the border type.
	 * @return Border type.
	 */
	int getBorderType() const { return _borderType; };

	/**
	 * We should call this method whenever we need border flags
	 * don't calc border flags yourself
	 * @return Border flags
	 */
	uint32 getBorderFlags() const;

	void addDirtyRect(const Common::Rect &r);
	void markAllDirty();
	void mergeDirtyRects();

	bool isDirty() override { return _borderIsDirty || _contentIsDirty; }

	void setBorderDirty(bool dirty) { _borderIsDirty = true; }
	void resizeBorderSurface();

	void setMode(uint32 mode) { _mode = mode; }
	void setBorderOffsets(BorderOffsets &offsets) { _macBorder.setOffsets(offsets); }

	void updateInnerDims();

private:
	void rebuildSurface(); // Propagate dimensions change and recreate patter/borders, etc.
	void drawBorderFromSurface(ManagedSurface *g, uint32 flags);
	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 updateOuterDims();

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

protected:
	void drawBorder();
	WindowClick isInBorder(int x, int y) const;
	BorderOffsets getBorderOffsets() const { return _macBorder.getOffset(); }

protected:
	ManagedSurface _borderSurface;

	bool _borderIsDirty;
	Common::Rect _innerDims;

	Common::List<Common::Rect> _dirtyRects;
	bool _hasScrollBar;

	uint32 _mode;
private:
	MacWindowBorder _macBorder;

	int _pattern;
	bool _hasPattern;

	bool _scrollable;
	bool _resizable;

	bool _closeable;
	bool _isTitleVisible;

	int _borderWidth;

	bool _beingDragged, _beingResized;
	int _draggedX, _draggedY;

	WindowClick _highlightedPart;

	Common::String _title;
	Common::String _shadowedTitle;

	int _borderType;
};

} // End of namespace Graphics

#endif