File: mactext.h

package info (click to toggle)
scummvm 2.7.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 363,784 kB
  • sloc: cpp: 3,622,060; asm: 27,410; python: 10,528; sh: 10,241; xml: 6,752; java: 5,579; perl: 2,570; yacc: 1,635; javascript: 1,016; lex: 539; makefile: 398; ansic: 378; awk: 275; objc: 82; sed: 11; php: 1
file content (383 lines) | stat: -rw-r--r-- 11,791 bytes parent folder | download
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
/* 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_MACTEXT_H
#define GRAPHICS_MACGUI_MACTEXT_H

#include "common/timer.h"
#include "common/system.h"

#include "graphics/macgui/macwindowmanager.h"
#include "graphics/macgui/macfontmanager.h"
#include "graphics/macgui/macmenu.h"
#include "graphics/macgui/macwidget.h"
#include "graphics/macgui/macwindow.h"

namespace Graphics {

class MacMenu;
class MacText;
class MacWidget;
class MacWindow;
class MacWindowManager;

struct MacFontRun {
	Common::U32String text;

	uint16 fontId;
	byte textSlant;
	uint16 fontSize;
	uint16 palinfo1;
	uint16 palinfo2;
	uint16 palinfo3;
	uint32 fgcolor;
	// to determine whether the next word is part of this one
	bool wordContinuation;
	const Font *font;
	MacWindowManager *wm;

	MacFontRun() {
		wm = nullptr;
		fontId = textSlant = fontSize = 0;
		palinfo1 = palinfo2 = palinfo3 = 0;
		fgcolor = 0;
		font = nullptr;
		wordContinuation = false;
	}

	MacFontRun(MacWindowManager *wm_) {
		wm = wm_;
		fontId = textSlant = fontSize = 0;
		palinfo1 = palinfo2 = palinfo3 = 0;
		fgcolor = 0;
		font = nullptr;
		wordContinuation = false;
	}

	MacFontRun(MacWindowManager *wm_, uint16 fontId_, byte textSlant_, uint16 fontSize_,
			uint16 palinfo1_, uint16 palinfo2_, uint16 palinfo3_) {
		setValues(wm_, fontId_, textSlant_, fontSize_, palinfo1_, palinfo2_, palinfo3_);
		wordContinuation = false;
	}

	MacFontRun(MacWindowManager *wm_, const Font *font_, byte textSlant_, uint16 fontSize_,
			uint16 palinfo1_, uint16 palinfo2_, uint16 palinfo3_) {
		setValues(wm_, 0, textSlant_, fontSize_, palinfo1_, palinfo2_, palinfo3_);
		font = font_;
		wordContinuation = false;
	}

	void setValues(MacWindowManager *wm_, uint16 fontId_, byte textSlant_, uint16 fontSize_,
			uint16 palinfo1_, uint16 palinfo2_, uint16 palinfo3_) {
		wm        = wm_;
		fontId    = fontId_;
		textSlant = textSlant_;
		fontSize  = fontSize_;
		palinfo1  = palinfo1_;
		palinfo2  = palinfo2_;
		palinfo3  = palinfo3_;
		fgcolor   = wm_->findBestColor(palinfo1_ & 0xff, palinfo2_ & 0xff, palinfo3_ & 0xff);
		font      = nullptr;
	}

	const Font *getFont();

	const Common::String toString();
	bool equals(MacFontRun &to);

	Common::CodePage getEncoding();
	bool plainByteMode();
	Common::String getEncodedText();
};

struct MacTextLine {
	int width;
	int height;
	int y;
	int charwidth;
	bool paragraphEnd;

	Common::Array<MacFontRun> chunks;

	MacTextLine() {
		width = height = charwidth = -1;
		y = 0;
		paragraphEnd = false;
	}

	MacFontRun &firstChunk() { return chunks[0]; }
	MacFontRun &lastChunk() { return chunks[chunks.size() - 1]; }

	/**
	 * Search for a chunk at given char column.
	 *
	 * @param col Requested column, gets modified with in-chunk column
	 * @returns Chunk number
	 *
	 * @note If requested column is too big, returns last character in the line
	 */
	uint getChunkNum(int *col);
};

struct SelectedText {
	int startX, startY;
	int endX, endY;
	int startRow, startCol;
	int endRow, endCol;

	SelectedText() {
		startX = startY = -1;
		endX = endY = -1;
		startRow = startCol = -1;
		endRow = endCol = -1;
	}

	bool needsRender() {
		return startX != endX || startY != endY;
	}
};

class MacText : public MacWidget {
public:
	MacText(MacWidget *parent, int x, int y, int w, int h, MacWindowManager *wm, const Common::U32String &s, const MacFont *font, uint32 fgcolor, uint32 bgcolor, int maxWidth, TextAlign textAlignment = kTextAlignLeft, int interlinear = 0, uint16 border = 0, uint16 gutter = 0, uint16 boxShadow = 0, uint16 textShadow = 0, bool fixedDims = true);
	// 0 pixels between the lines by default

	MacText(const Common::U32String &s, MacWindowManager *wm, const MacFont *font, uint32 fgcolor, uint32 bgcolor, int maxWidth, TextAlign textAlignment, int interlinear = 0, bool fixedDims = true);

	MacText(const Common::U32String &s, MacWindowManager *wm, const Font *font, uint32 fgcolor, uint32 bgcolor, int maxWidth, TextAlign textAlignment, int interlinear = 0, bool fixedDims = true);

	virtual ~MacText();

	virtual void resize(int w, int h);
	bool processEvent(Common::Event &event) override;

	bool needsRedraw() override { return _contentIsDirty || _cursorDirty; }

	void render();
	void undrawCursor();
	void draw(ManagedSurface *g, int x, int y, int w, int h, int xoff, int yoff);
	bool draw(ManagedSurface *g, bool forceRedraw = false) override;
	bool draw(bool forceRedraw = false) override;
	void drawToPoint(ManagedSurface *g, Common::Rect srcRect, Common::Point dstPoint);
	void drawToPoint(ManagedSurface *g, Common::Point dstPoint);

	Graphics::ManagedSurface *getSurface() { return _surface; }
	int getInterLinear() { return _interLinear; }
	void setInterLinear(int interLinear);
	void setMaxWidth(int maxWidth);
	void setDefaultFormatting(uint16 fontId, byte textSlant, uint16 fontSize,
														uint16 palinfo1, uint16 palinfo2, uint16 palinfo3);
	const MacFontRun &getDefaultFormatting() { return _defaultFormatting; }

	void setAlignOffset(TextAlign align);
	TextAlign getAlign() { return _textAlignment; }
	virtual Common::Point calculateOffset();
	void setActive(bool active) override;
	void setEditable(bool editable);

	void setColors(uint32 fg, uint32 bg) override;
	// set fgcolor for line x
	void setTextColor(uint32 color, uint32 line);
	void setTextColor(uint32 color, uint32 start, uint32 end);

	void appendText(const Common::U32String &str, int fontId = kMacFontChicago, int fontSize = 12, int fontSlant = kMacFontRegular, bool skipAdd = false);
	void appendText(const Common::U32String &str, int fontId = kMacFontChicago, int fontSize = 12, int fontSlant = kMacFontRegular, uint16 r = 0, uint16 g = 0, uint16 b = 0, bool skipAdd = false);
	void appendText(const Common::U32String &str, const Font *font, uint16 r = 0, uint16 g = 0, uint16 b = 0, bool skipAdd = false);

	int getTextFont() { return _defaultFormatting.fontId; }
	void enforceTextFont(uint16 fontId);

	// because currently, we are counting linespacing as font height
	int getTextSize() { return _defaultFormatting.fontSize; }
	void setTextSize(int textSize);

	int getTextSize(int start, int end);
	void setTextSize(int textSize, int start, int end);

	uint32 getTextColor() { return _defaultFormatting.fgcolor; }
	uint32 getTextColor(int start, int end);

	int getTextFont(int start, int end);
	void setTextFont(int fontId, int start, int end);

	int getTextSlant(int start, int end);
	void setTextSlant(int textSlant, int start, int end);
	void enforceTextSlant(int textSlant);

	// director text related-functions
	int getMouseChar(int x, int y);
	int getMouseWord(int x, int y);
	int getMouseItem(int x, int y);
	int getMouseLine(int x, int y);

private:
	MacFontRun getTextChunks(int start, int end);
	void setTextChunks(int start, int end, int param, void (*callback)(MacFontRun &, int));

	void appendText_(const Common::U32String &strWithFont, uint oldLen);
	void deletePreviousCharInternal(int *row, int *col);
	void insertTextFromClipboard();
	// getStringWidth for mactext version, because we may have the plain bytes mode
	int getStringWidth(MacFontRun &format, const Common::U32String &str);
	int getAlignOffset(int row);
	MacFontRun getFgColor();

public:
	void appendTextDefault(const Common::U32String &str, bool skipAdd = false);
	void appendTextDefault(const Common::String &str, bool skipAdd = false);
	void clearText();
	void removeLastLine();
	int getLineCount() { return _textLines.size(); }
	int getLineCharWidth(int line, bool enforce = false);
	int getLastLineWidth();
	int getTextHeight() { return _textMaxHeight; }
	int getLineHeight(int line);
	int getTextMaxWidth() { return _textMaxWidth; }

	void setText(const Common::U32String &str);

	void setFixDims(bool fixed) { _fixedDims = fixed; }
	bool getFixDims() { return _fixedDims; }

	void deleteSelection();
	void deletePreviousChar(int *row, int *col);
	void addNewLine(int *row, int *col);
	void insertChar(byte c, int *row, int *col);

	void getChunkPosFromIndex(int index, uint &lineNum, uint &chunkNum, uint &offset);
	void getRowCol(int x, int y, int *sx, int *sy, int *row, int *col);
	Common::U32String getTextChunk(int startRow, int startCol, int endRow, int endCol, bool formatted = false, bool newlines = true);

	Common::U32String getSelection(bool formatted = false, bool newlines = true);
	uint getSelectionIndex(bool start);
	void clearSelection();
	Common::U32String cutSelection();
	const SelectedText *getSelectedText() { return &_selectedText; }

	int getLineSpacing() { return _interLinear; }

	/**
	 * set the selection of mactext
	 * @param pos pos of selection, 0 represent first, -1 represent the end of text
	 * @param start selection start or selection end
	 */
	void setSelection(int pos, bool start);

	Common::U32String getEditedString();
	Common::U32String getText() { return _str; }
	Common::U32String getPlainText();

	void setSelRange(int selStart, int selEnd);

	void scroll(int delta);

private:
	void init();
	bool isCutAllowed();

	/**
	 * Returns line width in pixels. This takes into account chunks.
	 * The result is cached for faster subsequent calls.
	 *
	 * @param line Line number
	 * @param enforce Flag for indicating skipping the cache and computing the width,
	 *                must be called when text gets changed
	 * @param col Compute line width up to specified column, including this column
	 * @return line width in pixels, or 0 for non-existent lines
	 */
	int getLineWidth(int line, bool enforce = false, int col = -1);

	/**
	 * Rewraps paragraph containing given text row.
	 * When text is modified, we redo whole thing again without touching
	 * other paragraphs. Also, cursor position is returned in the arguments
	 */
	void reshuffleParagraph(int *row, int *col);

	void chopChunk(const Common::U32String &str, int *curLine);
	void splitString(const Common::U32String &str, int curLine = -1);
	void render(int from, int to, int shadow);
	void render(int from, int to);
	void recalcDims();
	void reallocSurface();

	void drawSelection(int xoff, int yoff);
	void updateCursorPos();

	void startMarking(int x, int y);
	void updateTextSelection(int x, int y);

public:
	int _cursorX, _cursorY;
	bool _cursorState;
	int _cursorRow, _cursorCol;

	bool _cursorDirty;
	Common::Rect *_cursorRect;
	bool _cursorOff;
	bool _selectable;

	int _scrollPos;

	bool _fullRefresh;

protected:
	Common::U32String _str;
	const MacFont *_macFont;

	int _maxWidth;
	int _interLinear;
	int _textShadow;

	bool _fixedDims;

	int _selEnd;
	int _selStart;

	int _textMaxWidth;
	int _textMaxHeight;

	ManagedSurface *_surface;
	ManagedSurface *_shadowSurface;

	TextAlign _textAlignment;

	Common::Array<MacTextLine> _textLines;
	MacFontRun _defaultFormatting;
	MacFontRun _currentFormatting;

	bool _macFontMode;

private:
	ManagedSurface *_cursorSurface;
	ManagedSurface *_cursorSurface2;

	int _editableRow;

	bool _inTextSelection;
	SelectedText _selectedText;

	MacMenu *_menu;
};

} // End of namespace Graphics

#endif