aboutsummaryrefslogtreecommitdiff
path: root/gui/EditTextWidget.cpp
blob: 10caf64cd76f890655d23a42c58308a1a1bc0e4d (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
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2002 The ScummVM project
 *
 * 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Header$
 */

#include "stdafx.h"
#include "EditTextWidget.h"
#include "dialog.h"
#include "newgui.h"

EditTextWidget::EditTextWidget(Dialog *boss, int x, int y, int w, int h, const String &text)
	: StaticTextWidget(boss, x, y-1, w, h+2, text, kTextAlignLeft), _backupString(text)
{
	_flags = WIDGET_ENABLED | WIDGET_CLEARBG | WIDGET_RETAIN_FOCUS | WIDGET_WANT_TICKLE;
	_type = kEditTextWidget;

	_caretVisible = false;
	_caretTime = 0;
}

void EditTextWidget::handleTickle()
{
	uint32 time = _boss->getGui()->get_time();
	if (_caretTime < time) {
		_caretTime = time + kCaretBlinkTime;
		if (_caretVisible) {
			drawCaret(true);
		} else {
			drawCaret(false);
		}
	}
}

void EditTextWidget::handleMouseDown(int x, int y, int button, int clickCount)
{
	// TODO - once we support "real editing" (i.e. caret can be at any spot),
	// a mouse click should place the caret.
}

bool EditTextWidget::handleKeyDown(uint16 ascii, int keycode, int modifiers)
{
	bool handled = true;
	bool dirty = false;

	// First remove caret
	if (_caretVisible)
		drawCaret(true);

	switch (keycode) {
		case '\n':	// enter/return
		case '\r':
			_boss->releaseFocus();
			dirty = true;
			break;
		case 27:	// escape
			_label = _backupString;
			_boss->releaseFocus();
			dirty = true;
			break;
		case 8:		// backspace
			_label.deleteLastChar();
			dirty = true;
			break;
		case 256+20:	// left arrow
			break;
		case 256+19:	// right arrow
			break;
		case 256+22:	// home
			break;
		case 256+23:	// end
			break;
		default:
			if (isprint((char)ascii)) {
				_label += (char)ascii;
				dirty = true;
			} else {
				handled = false;
			}
	}

	if (dirty)
		draw();
	
	return handled;
}

void EditTextWidget::drawWidget(bool hilite)
{
	NewGui			*gui = _boss->getGui();

	// Draw a thin frame around us.
	gui->hline(_x, _y, _x+_w-1, gui->_color);
	gui->hline(_x, _y+_h-1, _x+_w-1, gui->_shadowcolor);
	gui->vline(_x, _y, _y+_h-1, gui->_color);
	gui->vline(_x+_w-1, _y, _y+_h-1, gui->_shadowcolor);

	// Draw the text
	_align = (gui->getStringWidth(_label) > _w-6) ? kTextAlignRight : kTextAlignLeft;
	gui->drawString(_label, _x+2, _y+3, _w-6, gui->_textcolor, _align);
}

void EditTextWidget::drawCaret(bool erase)
{
	// Only draw if item is visible
	if (!isVisible() || !_boss->isVisible())
		return;

	NewGui *gui = _boss->getGui();
	
	int16 color = erase ? gui->_bgcolor : gui->_textcolorhi;
	int x = _x + _boss->getX() + 3;
	int y = _y + _boss->getY() + 1;

	// TODO - once we support "real editing" (i.e. caret can be at any spot),
	// x should be calculated based on the current caret position.
	int width = gui->getStringWidth(_label);
	if (width > _w-6)
		width = _w-6;
	x += width;

	gui->vline(x, y, y+kLineHeight, color);
	gui->addDirtyRect(x, y, 2, kLineHeight);
	
	_caretVisible = !erase;
}