-
Notifications
You must be signed in to change notification settings - Fork 24
/
TextBoxEditString.h
107 lines (106 loc) · 2.24 KB
/
TextBoxEditString.h
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
#pragma once
#include "TextBox.h"
template <class T> class TextBoxEditTString : public TextBox
{
const static int _bufSize = 70;
const static int _editSize = 18;
int _editPos, _caretPos, _bufLen;
char _buf[_bufSize+1];
char _tmpBuf[20];
public:
TextBoxEditTString(int left,int top,int width,int height,T *text):TextBox(left,top,width,height)
{
_editPos = -1;
_bufLen = 0;
_caretPos = -1;
}
virtual void OnDraw(DC *dc)
{
TextBox::OnDraw(dc);
dc->DrawText(_tmpBuf,_offset_x,_offset_y);
dc->DrawCaret(_caretPos, _offset_x, _offset_y);
}
void GetEditText() {
int s, e;
s = _bufLen - _editSize;
if (s<0)
s = 0;
e = (_bufLen > _editSize)? _editSize : _bufLen;
strncpy(_tmpBuf, _buf+s, e);
_tmpBuf[e] = 0;
}
void SetText(T *text)
{
if (_caretPos == -1)
{
int len;
len = strlen(text);
_bufLen = (len > _bufSize)? _bufSize : len;
strncpy(_buf, text, _bufLen);
_buf[_bufLen] = 0;
_editPos = _bufLen;
_caretPos = (_bufLen > _editSize)? _editSize : _bufLen;
GetEditText();
}
if(_changedEvent!=NULL)
_changedEvent->NotifyContentChanged(this);
Invalidate();
}
T * GetText()
{
char *s;
s = (char*) malloc(sizeof(char) * (strlen(_buf)+1) );
strcpy(s, _buf);
return s;
}
void Closed() {
_editPos = -1;
_bufLen = 0;
_caretPos = -1;
//_buf[0] = 0;
_tmpBuf[0] = 0;
}
void UpdateEdit(byte b)
{
int i;
switch(b) {
case 8: //backspace
if (_editPos>0) {
for (i=_editPos-1; i<_bufLen; i++)
_buf[i] = _buf[i+1];
_editPos--;
_bufLen--;
if (_bufLen < _editSize)
_caretPos--;
}
break;
case 13: //clear
_editPos = 0;
_bufLen = 0;
_caretPos = 0;
_buf[0] = 0;
break;
default: //add/insert char
if ((_editPos < _bufSize) && (_bufLen < _bufSize)) {
if (_editPos != _bufLen) {
for (i=_bufLen; i>=_editPos; i--)
_buf[i+1] = _buf[i];
}
_buf[_editPos++] = (char)b;
_bufLen++;
if (_caretPos<_editSize)
_caretPos++;
if (_editPos == _bufLen)
_buf[_editPos] = 0;
}
break;
}
if (_bufLen>0)
GetEditText();
else
_tmpBuf[0] = 0;
SetText(_tmpBuf);
}
};
typedef TextBoxEditTString<const __FlashStringHelper> TextBoxEditFString;
typedef TextBoxEditTString<char> TextBoxEditString;