-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathmactextwindow.cpp
More file actions
618 lines (494 loc) · 17.8 KB
/
mactextwindow.cpp
File metadata and controls
618 lines (494 loc) · 17.8 KB
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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
/* 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/>.
*
*/
#include "common/timer.h"
#include "common/system.h"
#include "graphics/macgui/macwindowmanager.h"
#include "graphics/macgui/macfontmanager.h"
#include "graphics/macgui/mactextwindow.h"
#include "graphics/macgui/macmenu.h"
#define SCROLLBAR_DELAY 300
namespace Graphics {
enum {
kConWOverlap = 20,
kConHOverlap = 20,
kConWPadding = 3,
kConHPadding = 4,
kConOverscan = 3,
kConScrollStep = 12,
};
MacTextWindow::MacTextWindow(MacWindowManager *wm, const MacFont *font, int fgcolor, int bgcolor, int maxWidth, TextAlign textAlignment, MacMenu *menu, int padding) :
MacWindow(wm->getLastId(), true, true, true, wm), _bgcolor(bgcolor), _maxWidth(maxWidth), _menu(menu) {
_font = font;
_mactext = new MacText(this, 0, 0, 0, 0, _wm, Common::U32String(""), font, fgcolor, bgcolor, maxWidth, textAlignment, 0, padding);
_fontRef = wm->_fontMan->getFont(*font);
init();
}
MacTextWindow::MacTextWindow(MacWindowManager *wm, const Font *font, int fgcolor, int bgcolor, int maxWidth, TextAlign textAlignment, MacMenu *menu, int padding) :
MacWindow(wm->getLastId(), true, true, true, wm), _bgcolor(bgcolor), _maxWidth(maxWidth), _menu(menu) {
_font = nullptr;
_mactext = new MacText(Common::U32String(""), _wm, font, fgcolor, bgcolor, maxWidth, textAlignment, 0, true);
_fontRef = font;
init();
}
void MacTextWindow::init() {
_inputTextHeight = 0;
_inputIsDirty = true;
_editable = true;
_selectable = true;
_textColorRGB = 0;
_scrollDirection = kBorderNone;
_clickedScrollPart = kBorderNone;
_nextScrollTime = 0;
_nextWheelEventTime = 0;
_scrollDelay = 50;
_isDragging = false;
_dragStartY = 0;
_dragStartScrollPos = 0;
// Disable autoselect on activation
_mactext->setAutoSelect(false);
if (_wm->_mode & kWMModeWin95) {
// in win95 mode, we set scrollbar as default
_hasScrollBar = true;
setBorderType(kWinBorderWin95Scrollbar);
loadInternalBorder(kWindowBorderScrollbar | kWindowBorderActive);
}
}
void MacTextWindow::resize(int w, int h) {
if (_composeSurface->w == w && _composeSurface->h == h)
return;
undrawInput();
MacWindow::resize(w, h);
_maxWidth = getInnerDimensions().width();
_mactext->resize(_maxWidth, getInnerDimensions().height());
}
void MacTextWindow::setDimensions(const Common::Rect &r) {
resize(r.width(), r.height());
_dims.moveTo(r.left, r.top);
updateInnerDims();
_contentIsDirty = true;
_wm->setFullRefresh(true);
_mactext->setDimensions(Common::Rect(_innerDims.width(), _innerDims.height()));
}
void MacTextWindow::appendText(const Common::U32String &str, const MacFont *macFont, bool skipAdd) {
// the reason we put undrawInput here before appendText, is we don't want the appended text affect our input
// thus, we first delete all of out input, and we append new text, and we redraw the input
undrawInput();
// we calc the rgb though fgcolor
uint16 red = (_textColorRGB >> 16) & 0xFF;
uint16 green = (_textColorRGB >> 8) & 0xFF;
uint16 blue = (_textColorRGB) & 0xFF;
// Adding empty line at the bottom of the input text area if needed
_mactext->setInputPadding(true);
if (macFont)
_mactext->appendText(str, macFont->getId(), macFont->getSize(), macFont->getSlant(), red, green, blue, skipAdd);
else {
_mactext->appendText(str, _fontRef, red, green, blue, skipAdd);
}
_contentIsDirty = true;
_inputIsDirty = true; //force it to redraw input
if (_editable) {
// Add one line of bottom padding to ensure the last line is not covered
int padding = _mactext->getLineHeight(_mactext->getLineCount() - 1);
padding = MIN<int>(padding, getInnerDimensions().height());
int oldScroll = _mactext->_scrollPos;
_mactext->_scrollPos = MAX<int>(0, _mactext->getTextHeight() - getInnerDimensions().height() + padding);
if (_mactext->_scrollPos != oldScroll) {
_mactext->undrawCursor();
_mactext->_cursorY -= (_mactext->_scrollPos - oldScroll);
_mactext->_cursorDirty = true;
}
}
if (_wm->_mode & kWMModeWin95)
calcScrollBar();
// if we enable the dynamic scrollbar, and the text height is smaller than window height, then we disable the border
// if the window is editable, then we don't disable the border, because in editable window, the area you scroll is always bigger
// than the window
if (!_editable && (_mode & kWindowModeDynamicScrollbar) && _mactext->getTextHeight() < getInnerDimensions().height()) {
int w = getDimensions().width();
int h = getDimensions().height();
enableScrollbar(false);
disableBorder();
resize(w, h);
_mactext->_fullRefresh = true;
}
// if the text height is bigger than the window, then we enable the scrollbar again
if (!_editable && (_mode & kWindowModeDynamicScrollbar) && _mactext->getTextHeight() > getInnerDimensions().height()) {
enableScrollbar(true);
int w = getDimensions().width();
int h = getDimensions().height();
resize(w, h);
_mactext->_fullRefresh = true;
}
}
void MacTextWindow::appendText(const Common::String &str, const MacFont *macFont, bool skipAdd) {
appendText(Common::U32String(str), macFont, skipAdd);
}
void MacTextWindow::setMarkdownText(const Common::U32String &str) {
_mactext->setMarkdownText(str);
_contentIsDirty = true;
}
void MacTextWindow::clearText() {
_mactext->clearText();
_contentIsDirty = true;
_borderIsDirty = true;
}
MacTextWindow::~MacTextWindow() {
delete _mactext;
}
void MacTextWindow::setTextWindowFont(const MacFont *font) {
_font = font;
_fontRef = _wm->_fontMan->getFont(*font);
_mactext->setDefaultFormatting(font->getId(), font->getSlant(), font->getSize(), 0, 0, 0);
}
const MacFont *MacTextWindow::getTextWindowFont() {
return _font;
}
bool MacTextWindow::draw(bool forceRedraw) {
uint32 now = g_system->getMillis();
// check if we need to hide the scroll bar
if (!(_wm->_mode & kWMModeWin95)) {
if (_nextWheelEventTime != 0 && now >= _nextWheelEventTime) {
if (_scrollDirection == kBorderNone && _clickedScrollPart == kBorderNone) {
setScroll(0, 0); // hide the scrollbar
_nextWheelEventTime = 0; // reset timer
}
}
}
// handle mouse button scrolling
if (_scrollDirection != kBorderNone) {
Common::Point mousePos = g_system->getEventManager()->getMousePos();
if (isInBorder(mousePos.x, mousePos.y) != _clickedScrollPart) {
_scrollDirection = kBorderNone;
setHighlight(kBorderNone);
calcScrollBar();
if (!(_wm->_mode & kWMModeWin95)) {
setScroll(0, 0);
}
}
if (_scrollDirection != kBorderNone && now >= _nextScrollTime) {
if (_scrollDirection == kBorderScrollUp) {
_mactext->scroll(-1);
} else if (_scrollDirection == kBorderScrollDown) {
_mactext->scroll(1);
}
calcScrollBar();
_nextScrollTime = now + _scrollDelay;
}
}
bool needsContentRedraw = _contentIsDirty || _inputIsDirty || _mactext->needsRedraw() || forceRedraw;
if (!_borderIsDirty && !needsContentRedraw)
return false;
if (_borderIsDirty || forceRedraw)
drawBorder();
if (_inputIsDirty || forceRedraw) {
drawInput();
_inputIsDirty = false;
needsContentRedraw = true; // input update needs a redraw
}
if (needsContentRedraw) {
// only clear the surface if we are actually going to redraw the text
if (_wm->_mode & kWMModeWin95) {
_composeSurface->clear(_bgcolor);
} else {
_composeSurface->clear(_wm->_colorWhite);
}
_contentIsDirty = false;
_mactext->draw(_composeSurface, forceRedraw);
}
return true;
}
bool MacTextWindow::draw(ManagedSurface *g, bool forceRedraw) {
if (!draw(forceRedraw))
return false;
g->blitFrom(*_composeSurface, Common::Rect(0, 0, _composeSurface->w, _composeSurface->h), Common::Point(_innerDims.left, _innerDims.top));
uint32 transcolor = (_wm->_pixelformat.bytesPerPixel == 1) ? _wm->_colorGreen : 0;
g->transBlitFrom(_borderSurface, Common::Rect(0, 0, _borderSurface.w, _borderSurface.h), Common::Point(_dims.left, _dims.top), transcolor);
return true;
}
void MacTextWindow::blit(ManagedSurface *g, Common::Rect &dest) {
g->transBlitFrom(*_composeSurface, _composeSurface->getBounds(), dest, _wm->_colorGreen2);
}
Common::U32String MacTextWindow::getSelection(bool formatted, bool newlines) {
return _mactext->getSelection(formatted, newlines);
}
int MacTextWindow::getMouseLine(int x, int y) {
// TODO: Improve the algorithm here, since after long scrolling there is
// sometimes error of +2 rows
x -= getInnerDimensions().left;
y -= getInnerDimensions().top + kConScrollStep;
return _mactext->getMouseLine(x, y);
}
void MacTextWindow::calcScrollBar() {
// since this function only able for the window which has scroll bar
// thus, if it doesn't has scrollbar, then we don't have to calc it
if (!_hasScrollBar)
return;
int maxText = 0, maxScrollbar = 0, displayHeight = 0;
displayHeight = getInnerDimensions().height();
maxScrollbar = getDimensions().height() - getBorderOffsets().upperScrollHeight - getBorderOffsets().lowerScrollHeight;
maxText = _mactext->getTextHeight();
// if we enable the win95 mode but the text height is smaller than window height, then we don't draw the scrollbar
if (_wm->_mode & kWMModeWin95 && displayHeight > maxText && !_editable)
return;
int maxScroll = 0;
// identical to MacText scroll() logic
if (_editable)
maxScroll = maxText - kConScrollStep;
else
maxScroll = maxText - displayHeight;
float contentHeight = (float)(maxText + displayHeight);
float scrollSize = (float)(maxScrollbar * displayHeight) / contentHeight;
int range = maxScrollbar - (int)scrollSize - 1;
float ratio = CLIP<float>((float)_mactext->_scrollPos / (float)maxScroll, 0.0f, 1.0f);
float scrollPos = ratio * (float)range;
setScroll(scrollPos, scrollSize);
}
void MacTextWindow::calcWin95Scroll(int &scrollAreaTop, int &scrollAreaHeight, int &barY, int &barHeight) {
const BorderOffsets &offsets = getBorderOffsets();
scrollAreaTop = offsets.upperScrollHeight;
scrollAreaHeight = _dims.height() - offsets.upperScrollHeight - offsets.lowerScrollHeight;
int contentHeight = _mactext->getTextHeight();
int winHeight = getInnerDimensions().height();
// if content fits in the current window
if (contentHeight <= winHeight || contentHeight == 0) {
barHeight = scrollAreaHeight;
barY = scrollAreaTop;
return;
}
float winRatio = (float)winHeight / (float)contentHeight;
barHeight = MAX<int>(8, (int)(scrollAreaHeight * winRatio)); // 8 the min height of scrollBar
int maxTextScroll = contentHeight - winHeight;
int maxBarScroll = scrollAreaHeight - barHeight;
float scrollAmount = (float)_mactext->_scrollPos / (float)maxTextScroll;
barY = scrollAreaTop + (int)(maxBarScroll * scrollAmount);
}
bool MacTextWindow::processEvent(Common::Event &event) {
WindowClick click = isInBorder(event.mouse.x, event.mouse.y);
if (!(g_system->getEventManager()->getButtonState() & Common::EventManager::LBUTTON)) {
if (_isDragging) {
_isDragging = false;
}
if (_clickedScrollPart != kBorderNone || _scrollDirection != kBorderNone) {
_scrollDirection = kBorderNone;
_clickedScrollPart = kBorderNone;
setHighlight(kBorderNone);
if (!(_wm->_mode & kWMModeWin95)) {
setScroll(0, 0);
}
return true;
}
}
if (event.type == Common::EVENT_MOUSEMOVE) {
if (!(_wm->_mode & kWMModeWin95)) {
if (_clickedScrollPart == kBorderScrollUp || _clickedScrollPart == kBorderScrollDown) {
if (click == kBorderScrollUp) {
if (_scrollDirection != kBorderScrollUp) {
_scrollDirection = kBorderScrollUp;
_clickedScrollPart = kBorderScrollUp;
setHighlight(kBorderScrollUp);
calcScrollBar();
}
} else if (click == kBorderScrollDown) {
if (_scrollDirection != kBorderScrollDown) {
_scrollDirection = kBorderScrollDown;
_clickedScrollPart = kBorderScrollDown;
setHighlight(kBorderScrollDown);
calcScrollBar();
}
} else {
if (_scrollDirection != kBorderNone) {
_scrollDirection = kBorderNone;
setHighlight(kBorderNone);
calcScrollBar();
setScroll(0, 0);
}
}
return true;
}
} else if ((_wm->_mode & kWMModeWin95) && _isDragging) {
int scrollAreaTop, scrollAreaHeight, barY, barHeight;
calcWin95Scroll(scrollAreaTop, scrollAreaHeight, barY, barHeight);
int relMouseY = event.mouse.y - _dims.top;
int diffY = relMouseY - _dragStartY;
if (diffY != 0) {
int maxTextScroll = _mactext->getTextHeight() - getInnerDimensions().height();
int maxBarScroll = scrollAreaHeight - barHeight;
if (maxBarScroll > 0) {
float pixelsPerUnit = (float)maxTextScroll / (float)maxBarScroll;
int newScrollPos = _dragStartScrollPos + (int)(diffY * pixelsPerUnit);
newScrollPos = CLIP<int>(newScrollPos, 0, maxTextScroll);
if (newScrollPos != _mactext->_scrollPos) {
_mactext->_scrollPos = newScrollPos;
_mactext->setDirty(true);
_contentIsDirty = true;
_borderIsDirty = true;
calcScrollBar();
}
}
}
return true;
}
}
if (event.type == Common::EVENT_LBUTTONDOWN) {
if ((_wm->_mode & kWMModeWin95) && (click == kBorderScrollUp || click == kBorderScrollDown)) {
const BorderOffsets &offsets = getBorderOffsets();
int scrollBarWidth = offsets.right;
if (event.mouse.x >= _dims.right - scrollBarWidth && event.mouse.x < _dims.right) {
int scrollAreaTop, scrollAreaHeight, barY, barHeight;
calcWin95Scroll(scrollAreaTop, scrollAreaHeight, barY, barHeight);
int relMouseY = event.mouse.y - _dims.top;
if (relMouseY >= barY && relMouseY < barY + barHeight) {
_isDragging = true;
_dragStartY = relMouseY; // store window relative Y
_dragStartScrollPos = _mactext->_scrollPos;
return true;
}
}
}
}
if (event.type == Common::EVENT_LBUTTONUP) {
if (_isDragging) {
_isDragging = false;
return true;
}
}
if (event.type == Common::EVENT_KEYDOWN) {
if (!_editable)
return false;
_wm->setActiveWindow(getId());
if (event.kbd.flags & (Common::KBD_ALT | Common::KBD_CTRL | Common::KBD_META)) {
if (_mactext->processEvent(event))
return true;
}
switch (event.kbd.keycode) {
case Common::KEYCODE_BACKSPACE:
if (!_inputText.empty()) {
if (_mactext->hasSelection()) {
_mactext->cutSelection();
} else {
_inputText.deleteLastChar();
}
_inputIsDirty = true;
}
return true;
case Common::KEYCODE_RETURN:
undrawInput();
_inputIsDirty = true; // we force it to redraw input
return false; // Pass it to the higher level for processing
default:
if (event.kbd.ascii == '~')
return false;
if (event.kbd.ascii >= 0x20 && event.kbd.ascii <= 0x7f) {
if (_mactext->hasSelection())
_mactext->cutSelection();
_inputText += (char)event.kbd.ascii;
_inputIsDirty = true;
return true;
}
break;
}
}
if (hasAllFocus()) // We are being dragged or resized
return MacWindow::processEvent(event); // Pass it to upstream
if (event.type == Common::EVENT_WHEELUP) {
//setHighlight(kBorderScrollUp);
_mactext->scroll(-2);
_contentIsDirty = true;
calcScrollBar();
_nextWheelEventTime = g_system->getMillis() + SCROLLBAR_DELAY; // hide the bar after 300ms from now
return true;
}
if (event.type == Common::EVENT_WHEELDOWN) {
//setHighlight(kBorderScrollDown);
_mactext->scroll(2);
_contentIsDirty = true;
calcScrollBar();
_nextWheelEventTime = g_system->getMillis() + SCROLLBAR_DELAY; // hide the bar after 300ms from now
return true;
}
if (click == kBorderScrollUp || click == kBorderScrollDown) {
if (event.type == Common::EVENT_LBUTTONDOWN) {
setHighlight(click);
_scrollDirection = click;
_clickedScrollPart = click;
calcScrollBar();
return true;
} else if (event.type == Common::EVENT_LBUTTONUP) {
// reset scrolling state
_scrollDirection = kBorderNone;
_clickedScrollPart = kBorderNone;
setHighlight(kBorderNone);
// hide the scroll bar
if (!(_wm->_mode & kWMModeWin95)) {
setScroll(0, 0);
}
return true;
}
return false;
}
if (event.type == Common::EVENT_LBUTTONUP && _clickedScrollPart != kBorderNone) {
_scrollDirection = kBorderNone;
_clickedScrollPart = kBorderNone;
setHighlight(kBorderNone);
if (!(_wm->_mode & kWMModeWin95)) {
setScroll(0, 0);
}
return true;
}
if (click == kBorderInner) {
// Call callback for processing any events
if (_callback)
(*_callback)(click, event, _dataPtr);
if (!_selectable)
return false;
event.mouse.x -= _innerDims.left;
event.mouse.y -= _innerDims.top;
return _mactext->processEvent(event);
}
return MacWindow::processEvent(event);
}
void MacTextWindow::undrawInput() {
for (uint i = 0; i < _inputTextHeight; i++)
_mactext->removeLastLine();
_inputTextHeight = 0;
}
void MacTextWindow::drawInput() {
if (!_editable)
return;
int oldLen = _mactext->getLineCount() - _inputTextHeight;
// add new input line to the text
appendText(_inputText, _font, true);
// Now recalc new text height
int newLen = _mactext->getLineCount();
_inputTextHeight = newLen - oldLen;
_contentIsDirty = true;
}
void MacTextWindow::clearInput() {
_inputText.clear();
}
void MacTextWindow::appendInput(const Common::U32String &str) {
_inputText += str;
drawInput();
}
void MacTextWindow::appendInput(const Common::String &str) {
appendInput(Common::U32String(str));
}
} // End of namespace Graphics