1 /* ScummVM - Graphic Adventure Engine
2 *
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 *
21 */
22
23 #include "common/timer.h"
24 #include "common/system.h"
25
26 #include "graphics/macgui/macwindowmanager.h"
27 #include "graphics/macgui/macfontmanager.h"
28 #include "graphics/macgui/mactextwindow.h"
29 #include "graphics/macgui/macmenu.h"
30
31 namespace Graphics {
32
33 enum {
34 kConWOverlap = 20,
35 kConHOverlap = 20,
36 kConWPadding = 3,
37 kConHPadding = 4,
38 kConOverscan = 3,
39 kConScrollStep = 12,
40
41 kCursorHeight = 12
42 };
43
44 static void cursorTimerHandler(void *refCon);
45
MacTextWindow(MacWindowManager * wm,const MacFont * font,int fgcolor,int bgcolor,int maxWidth,TextAlign textAlignment,MacMenu * menu,bool cursorHandler)46 MacTextWindow::MacTextWindow(MacWindowManager *wm, const MacFont *font, int fgcolor, int bgcolor, int maxWidth, TextAlign textAlignment, MacMenu *menu, bool cursorHandler) :
47 MacWindow(wm->getLastId(), true, true, true, wm) {
48
49 _font = font;
50 _menu = menu;
51 _mactext = new MacText("", _wm, font, fgcolor, bgcolor, maxWidth, textAlignment);
52
53 _fontRef = wm->_fontMan->getFont(*font);
54
55 _inputTextHeight = 0;
56 _maxWidth = maxWidth;
57
58 _inputIsDirty = true;
59 _inTextSelection = false;
60
61 _scrollPos = 0;
62
63 _cursorX = 0;
64 _cursorY = 0;
65 _cursorState = false;
66 _cursorOff = false;
67
68 _cursorDirty = true;
69
70 _cursorRect = new Common::Rect(0, 0, 1, kCursorHeight);
71
72 _cursorSurface = new ManagedSurface(1, kCursorHeight);
73 _cursorSurface->fillRect(*_cursorRect, _wm->_colorBlack);
74
75 if (cursorHandler)
76 g_system->getTimerManager()->installTimerProc(&cursorTimerHandler, 200000, this, "textWindowCursor");
77 }
78
resize(int w,int h)79 void MacTextWindow::resize(int w, int h) {
80 if (_surface.w == w && _surface.h == h)
81 return;
82
83 undrawInput();
84
85 MacWindow::resize(w, h);
86
87 _maxWidth = getInnerDimensions().width();
88 _mactext->setMaxWidth(_maxWidth);
89 }
90
appendText(Common::String str,const MacFont * macFont,bool skipAdd)91 void MacTextWindow::appendText(Common::String str, const MacFont *macFont, bool skipAdd) {
92 _mactext->appendText(str, macFont->getId(), macFont->getSize(), macFont->getSlant(), skipAdd);
93
94 _contentIsDirty = true;
95
96 _scrollPos = MAX(0, _mactext->getTextHeight() - getInnerDimensions().height());
97
98 updateCursorPos();
99 }
100
clearText()101 void MacTextWindow::clearText() {
102 _mactext->clearText();
103
104 _contentIsDirty = true;
105 _borderIsDirty = true;
106
107 updateCursorPos();
108 }
109
~MacTextWindow()110 MacTextWindow::~MacTextWindow() {
111 delete _cursorRect;
112 delete _cursorSurface;
113 delete _mactext;
114
115 g_system->getTimerManager()->removeTimerProc(&cursorTimerHandler);
116 }
117
setTextWindowFont(const MacFont * font)118 void MacTextWindow::setTextWindowFont(const MacFont *font) {
119 _font = font;
120
121 _fontRef = _wm->_fontMan->getFont(*font);
122
123 _mactext->setDefaultFormatting(font->getId(), font->getSlant(), font->getSize(), 0, 0, 0);
124 }
125
getTextWindowFont()126 const MacFont *MacTextWindow::getTextWindowFont() {
127 return _font;
128 }
129
draw(ManagedSurface * g,bool forceRedraw)130 bool MacTextWindow::draw(ManagedSurface *g, bool forceRedraw) {
131 if (!_borderIsDirty && !_contentIsDirty && !_cursorDirty && !_inputIsDirty && !forceRedraw)
132 return false;
133
134 if (_borderIsDirty || forceRedraw) {
135 drawBorder();
136
137 _composeSurface.clear(_wm->_colorWhite);
138 }
139
140 if (_inputIsDirty || forceRedraw) {
141 drawInput();
142 _inputIsDirty = false;
143 }
144
145 _contentIsDirty = false;
146
147 // Compose
148 _mactext->draw(&_composeSurface, 0, _scrollPos, _surface.w - 2, _scrollPos + _surface.h - 2, kConWOverlap - 2, kConWOverlap - 2);
149
150 if (_cursorState)
151 _composeSurface.blitFrom(*_cursorSurface, *_cursorRect, Common::Point(_cursorX + kConWOverlap - 2, _cursorY + kConHOverlap - 2));
152
153 if (_selectedText.endY != -1)
154 drawSelection();
155
156 _composeSurface.transBlitFrom(_borderSurface, kColorGreen);
157
158 g->transBlitFrom(_composeSurface, _composeSurface.getBounds(), Common::Point(_dims.left - 2, _dims.top - 2), kColorGreen2);
159
160 return true;
161 }
162
drawSelection()163 void MacTextWindow::drawSelection() {
164 if (_selectedText.endY == -1)
165 return;
166
167 SelectedText s = _selectedText;
168
169 if (s.startY > s.endY || (s.startY == s.endY && s.startX > s.endX)) {
170 SWAP(s.startX, s.endX);
171 SWAP(s.startY, s.endY);
172 SWAP(s.startRow, s.endRow);
173 SWAP(s.startCol, s.endCol);
174 }
175
176 int lastLineStart = s.endY;
177 s.endY += _mactext->getLineHeight(s.endRow);
178
179 int start = s.startY - _scrollPos;
180 start = MAX(0, start);
181
182 if (start > getInnerDimensions().height())
183 return;
184
185 int end = s.endY - _scrollPos;
186
187 if (end < 0)
188 return;
189
190 end = MIN((int)getInnerDimensions().height(), end);
191
192 int numLines = 0;
193 int x1 = 0, x2 = 0;
194
195 for (int y = start; y < end; y++) {
196 if (!numLines) {
197 x1 = 0;
198 x2 = getInnerDimensions().width() - 1;
199
200 if (y + _scrollPos == s.startY && s.startX > 0) {
201 numLines = _mactext->getLineHeight(s.startRow);
202 x1 = s.startX;
203 }
204 if (y + _scrollPos >= lastLineStart) {
205 numLines = _mactext->getLineHeight(s.endRow);
206 x2 = s.endX;
207 }
208 } else {
209 numLines--;
210 }
211
212 byte *ptr = (byte *)_composeSurface.getBasePtr(x1 + kConWOverlap - 2, y + kConWOverlap - 2);
213
214 for (int x = x1; x < x2; x++, ptr++)
215 if (*ptr == _wm->_colorBlack)
216 *ptr = _wm->_colorWhite;
217 else
218 *ptr = _wm->_colorBlack;
219 }
220 }
221
getSelection(bool formatted,bool newlines)222 Common::String MacTextWindow::getSelection(bool formatted, bool newlines) {
223 if (_selectedText.endY == -1)
224 return Common::String("");
225
226 SelectedText s = _selectedText;
227
228 if (s.startY > s.endY || (s.startY == s.endY && s.startX > s.endX)) {
229 SWAP(s.startRow, s.endRow);
230 SWAP(s.startCol, s.endCol);
231 }
232
233 return _mactext->getTextChunk(s.startRow, s.startCol, s.endRow, s.endCol, formatted, newlines);
234 }
235
clearSelection()236 void MacTextWindow::clearSelection() {
237 _selectedText.endY = _selectedText.startY = -1;
238 }
239
isCutAllowed()240 bool MacTextWindow::isCutAllowed() {
241 if (_selectedText.startRow >= (int)(_mactext->getLineCount() - _inputTextHeight) &&
242 _selectedText.endRow >= (int)(_mactext->getLineCount() - _inputTextHeight))
243 return true;
244
245 return false;
246 }
247
cutSelection()248 Common::String MacTextWindow::cutSelection() {
249 if (!isCutAllowed())
250 return Common::String("");
251
252 SelectedText s = _selectedText;
253
254 if (s.startY > s.endY || (s.startY == s.endY && s.startX > s.endX)) {
255 SWAP(s.startRow, s.endRow);
256 SWAP(s.startCol, s.endCol);
257 }
258
259 Common::String selection = _mactext->getTextChunk(s.startRow, s.startCol, s.endRow, s.endCol, false, false);
260
261 const char *selStart = strstr(_inputText.c_str(), selection.c_str());
262
263 if (!selStart) {
264 warning("Cannot find substring '%s' in '%s'", selection.c_str(), _inputText.c_str());
265
266 return Common::String("");
267 }
268
269 int selPos = selStart - _inputText.c_str();
270 Common::String newInput = Common::String(_inputText.c_str(), selPos) + Common::String(_inputText.c_str() + selPos + selection.size());
271
272 clearSelection();
273 clearInput();
274 appendInput(newInput);
275
276 return selection;
277 }
278
processEvent(Common::Event & event)279 bool MacTextWindow::processEvent(Common::Event &event) {
280 WindowClick click = isInBorder(event.mouse.x, event.mouse.y);
281
282 if (event.type == Common::EVENT_KEYDOWN) {
283 _wm->setActive(getId());
284
285 if (event.kbd.flags & (Common::KBD_ALT | Common::KBD_CTRL | Common::KBD_META)) {
286 return false;
287 }
288
289 switch (event.kbd.keycode) {
290 case Common::KEYCODE_BACKSPACE:
291 if (!_inputText.empty()) {
292 _inputText.deleteLastChar();
293 _inputIsDirty = true;
294 }
295 return true;
296
297 case Common::KEYCODE_RETURN:
298 undrawInput();
299 return false; // Pass it to the higher level for processing
300
301 default:
302 if (event.kbd.ascii == '~')
303 return false;
304
305 if (event.kbd.ascii >= 0x20 && event.kbd.ascii <= 0x7f) {
306 _inputText += (char)event.kbd.ascii;
307 _inputIsDirty = true;
308
309 return true;
310 }
311
312 break;
313 }
314 }
315
316 if (hasAllFocus())
317 return MacWindow::processEvent(event); // Pass it to upstream
318
319 if (event.type == Common::EVENT_WHEELUP) {
320 scroll(-2);
321 return true;
322 }
323
324 if (event.type == Common::EVENT_WHEELDOWN) {
325 scroll(2);
326 return true;
327 }
328
329 if (click == kBorderScrollUp || click == kBorderScrollDown) {
330 if (event.type == Common::EVENT_LBUTTONDOWN) {
331 int consoleHeight = getInnerDimensions().height();
332 int textFullSize = _mactext->getTextHeight();
333 float scrollPos = (float)_scrollPos / textFullSize;
334 float scrollSize = (float)consoleHeight / textFullSize;
335
336 setScroll(scrollPos, scrollSize);
337
338 return true;
339 } else if (event.type == Common::EVENT_LBUTTONUP) {
340 switch (click) {
341 case kBorderScrollUp:
342 scroll(-1);
343 break;
344 case kBorderScrollDown:
345 scroll(1);
346 break;
347 default:
348 return false;
349 }
350
351 return true;
352 }
353
354 return false;
355 }
356
357 if (click == kBorderInner) {
358 if (event.type == Common::EVENT_LBUTTONDOWN) {
359 startMarking(event.mouse.x, event.mouse.y);
360
361 return true;
362 } else if (event.type == Common::EVENT_LBUTTONUP && _menu) {
363 if (_inTextSelection) {
364 _inTextSelection = false;
365
366 if (_selectedText.endY == -1 ||
367 (_selectedText.endX == _selectedText.startX && _selectedText.endY == _selectedText.startY)) {
368 _selectedText.startY = _selectedText.endY = -1;
369 _contentIsDirty = true;
370 _menu->enableCommand("Edit", "Copy", false);
371 } else {
372 _menu->enableCommand("Edit", "Copy", true);
373
374 bool cutAllowed = isCutAllowed();
375
376 _menu->enableCommand("Edit", "Cut", cutAllowed);
377 _menu->enableCommand("Edit", "Clear", cutAllowed);
378 }
379 }
380
381 return true;
382 } else if (event.type == Common::EVENT_MOUSEMOVE) {
383 if (_inTextSelection) {
384 updateTextSelection(event.mouse.x, event.mouse.y);
385 return true;
386 }
387 }
388
389 return false;
390 }
391
392 return MacWindow::processEvent(event);
393 }
394
scroll(int delta)395 void MacTextWindow::scroll(int delta) {
396 int oldScrollPos = _scrollPos;
397
398 _scrollPos += delta * kConScrollStep;
399 _scrollPos = CLIP<int>(_scrollPos, 0, _mactext->getTextHeight() - kConScrollStep);
400 undrawCursor();
401 _cursorY -= (_scrollPos - oldScrollPos);
402 _contentIsDirty = true;
403 _borderIsDirty = true;
404 }
405
startMarking(int x,int y)406 void MacTextWindow::startMarking(int x, int y) {
407 x -= getInnerDimensions().left - 2;
408 y -= getInnerDimensions().top;
409
410 y += _scrollPos;
411
412 _mactext->getRowCol(x, y, &_selectedText.startX, &_selectedText.startY, &_selectedText.startRow, &_selectedText.startCol);
413
414 _selectedText.endY = -1;
415
416 _inTextSelection = true;
417 }
418
updateTextSelection(int x,int y)419 void MacTextWindow::updateTextSelection(int x, int y) {
420 x -= getInnerDimensions().left - 2;
421 y -= getInnerDimensions().top;
422
423 y += _scrollPos;
424
425 _mactext->getRowCol(x, y, &_selectedText.endX, &_selectedText.endY, &_selectedText.endRow, &_selectedText.endCol);
426
427 debug(3, "s: %d,%d (%d, %d) e: %d,%d (%d, %d)", _selectedText.startX, _selectedText.startY,
428 _selectedText.startRow, _selectedText.startCol, _selectedText.endX,
429 _selectedText.endY, _selectedText.endRow, _selectedText.endCol);
430
431 _contentIsDirty = true;
432 }
433
undrawInput()434 void MacTextWindow::undrawInput() {
435 for (uint i = 0; i < _inputTextHeight; i++)
436 _mactext->removeLastLine();
437
438 if (_inputTextHeight)
439 appendText("\n", _font, true);
440
441 _inputTextHeight = 0;
442 }
443
drawInput()444 void MacTextWindow::drawInput() {
445 undrawInput();
446
447 Common::Array<Common::String> text;
448
449 // Now recalc new text height
450 _fontRef->wordWrapText(_inputText, _maxWidth, text);
451 _inputTextHeight = MAX((uint)1, text.size()); // We always have line to clean
452
453 // And add new input line to the text
454 appendText(_inputText, _font, true);
455
456 _cursorX = _inputText.empty() ? 0 : _fontRef->getStringWidth(text[_inputTextHeight - 1]);
457
458 updateCursorPos();
459
460 _contentIsDirty = true;
461 }
462
clearInput()463 void MacTextWindow::clearInput() {
464 undrawCursor();
465
466 _cursorX = 0;
467 _inputText.clear();
468 }
469
appendInput(Common::String str)470 void MacTextWindow::appendInput(Common::String str) {
471 _inputText += str;
472
473 drawInput();
474 }
475
476 //////////////////
477 // Cursor stuff
cursorTimerHandler(void * refCon)478 static void cursorTimerHandler(void *refCon) {
479 MacTextWindow *w = (MacTextWindow *)refCon;
480
481 if (!w->_cursorOff)
482 w->_cursorState = !w->_cursorState;
483
484 w->_cursorDirty = true;
485 }
486
updateCursorPos()487 void MacTextWindow::updateCursorPos() {
488 _cursorY = _mactext->getTextHeight() - _scrollPos - kCursorHeight;
489
490 _cursorDirty = true;
491 }
492
undrawCursor()493 void MacTextWindow::undrawCursor() {
494 _cursorState = false;
495 _cursorDirty = true;
496 }
497
498
499 } // End of namespace Graphics
500