aboutsummaryrefslogtreecommitdiff
path: root/lib/gui/einputstring.cpp
blob: 4562e6332a56dcd56577607487d6d2c92b3fc7e6 (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
#include <lib/gui/einputstring.h>

DEFINE_REF(eInputContentString);

eInputContentString::eInputContentString()
{
	m_string = "bla";
	m_cursor = 0;
	m_input = 0;
	m_len = m_string.size();
}

void eInputContentString::getDisplay(std::string &res, int &cursor)
{
	res = m_string;
	cursor = m_cursor;
}

void eInputContentString::moveCursor(int dir)
{
	int old_cursor = m_cursor;
	
	switch (dir)
	{
	case dirLeft:
		--m_cursor;
		break;
	case dirRight:
		++m_cursor;
		break;
	case dirHome:
		m_cursor = 0;
		break;
	case dirEnd:
		m_cursor = m_len;
		break;
	}
	
	if (m_cursor < 0)
		m_cursor = 0;
	if (m_cursor > m_len)
		m_cursor = m_len;
	
	if (m_cursor != old_cursor)
		if (m_input)
			m_input->invalidate();
}

int eInputContentString::haveKey(int code, int overwrite)
{
	int have_char = -1;

	if (code >= 0x8020)
		have_char = code &~ 0x8000;
	
	if (have_char != -1)
	{
		if (overwrite && m_cursor < m_len)
			m_string[m_cursor] = have_char;
		else
		{
			m_string.insert(m_cursor, 1, have_char);
			++m_len;
		}
		
		m_cursor++;
		
		ASSERT(m_cursor <= m_len);
 
		if (m_input)
			m_input->invalidate();
		return 1;
	}
	return 0;
}

void eInputContentString::deleteChar(int dir)
{
	if (dir == deleteForward)
	{
		eDebug("forward");
		if (m_cursor != m_len)
			++m_cursor;
		else
			return;
	}
		/* backward delete at begin */
	if (!m_cursor)
		return;
	
	if (!m_len)
		return;
	
	m_string.erase(m_cursor - 1, m_cursor);
	
	m_len--;
	m_cursor--;

	if (m_input)
		m_input->invalidate();
}

int eInputContentString::isValid()
{
	return 1;
}

void eInputContentString::validate()
{
}

void eInputContentString::setText(const std::string &str)
{
	m_string = str;
	m_len = m_string.size();
	if (m_cursor > m_len)
		m_cursor = m_len;
	
	if (m_input)
		m_input->invalidate();
}

std::string eInputContentString::getText()
{
	return m_string;
}