AWind
TextBoxEditString.h
1 #pragma once
2 #include "TextBox.h"
3 
4 template <class T> class TextBoxEditTString : public TextBox
5 {
6  const static int _bufSize = 70;
7  const static int _editSize = 18;
8  int _editPos, _caretPos, _bufLen;
9  char _buf[_bufSize+1];
10  char _tmpBuf[20];
11 public:
12  TextBoxEditTString(int left,int top,int width,int height,T *text):TextBox(left,top,width,height)
13  {
14  _editPos = -1;
15  _bufLen = 0;
16  _caretPos = -1;
17  }
18  virtual void OnDraw(DC *dc)
19  {
20  TextBox::OnDraw(dc);
21  dc->DrawText(_tmpBuf,_offset_x,_offset_y);
22  dc->DrawCaret(_caretPos, _offset_x, _offset_y);
23  }
24  void GetEditText() {
25  int s, e;
26  s = _bufLen - _editSize;
27  if (s<0)
28  s = 0;
29  e = (_bufLen > _editSize)? _editSize : _bufLen;
30  strncpy(_tmpBuf, _buf+s, e);
31  _tmpBuf[e] = 0;
32  }
33  void SetText(T *text)
34  {
35  if (_caretPos == -1)
36  {
37  int len;
38  len = strlen(text);
39  _bufLen = (len > _bufSize)? _bufSize : len;
40  strncpy(_buf, text, _bufLen);
41  _buf[_bufLen] = 0;
42  _editPos = _bufLen;
43  _caretPos = (_bufLen > _editSize)? _editSize : _bufLen;
44  GetEditText();
45  }
46  if(_changedEvent!=NULL)
47  _changedEvent->NotifyContentChanged(this);
48  Invalidate();
49  }
50  T * GetText()
51  {
52  char *s;
53  s = (char*) malloc(sizeof(char) * (strlen(_buf)+1) );
54  strcpy(s, _buf);
55  return s;
56  }
57  void Closed() {
58  _editPos = -1;
59  _bufLen = 0;
60  _caretPos = -1;
61  //_buf[0] = 0;
62  _tmpBuf[0] = 0;
63  }
64  void UpdateEdit(byte b)
65  {
66  int i;
67  switch(b) {
68  case 8: //backspace
69  if (_editPos>0) {
70  for (i=_editPos-1; i<_bufLen; i++)
71  _buf[i] = _buf[i+1];
72  _editPos--;
73  _bufLen--;
74  if (_bufLen < _editSize)
75  _caretPos--;
76  }
77  break;
78  case 13: //clear
79  _editPos = 0;
80  _bufLen = 0;
81  _caretPos = 0;
82  _buf[0] = 0;
83  break;
84  default: //add/insert char
85  if ((_editPos < _bufSize) && (_bufLen < _bufSize)) {
86  if (_editPos != _bufLen) {
87  for (i=_bufLen; i>=_editPos; i--)
88  _buf[i+1] = _buf[i];
89  }
90  _buf[_editPos++] = (char)b;
91  _bufLen++;
92  if (_caretPos<_editSize)
93  _caretPos++;
94  if (_editPos == _bufLen)
95  _buf[_editPos] = 0;
96  }
97  break;
98  }
99  if (_bufLen>0)
100  GetEditText();
101  else
102  _tmpBuf[0] = 0;
103  SetText(_tmpBuf);
104  }
105 };
void DrawText(const __FlashStringHelper *text, int x, int y)
Draws PROGMEM string. Input coordinates have to be defined in the window coordinate system...
Definition: DC.h:177
Base class for window with text content.
Definition: TextBox.h:30
Definition: TextBoxEditString.h:4
Device context. Abstraction layer to the device specific drawing code. Coordinates in drawing functio...
Definition: DC.h:29
void DrawCaret(int pos, int x, int y)
Draw caret. Input coordinates have to be defined in the window coordinate system. ...
Definition: DC.h:228
void Invalidate()
If function is called than the window manager updates the window.
Definition: Window.h:157
void OnDraw(DC *dc)
Implements drawing code.
Definition: TextBox.h:69
virtual void OnDraw(DC *dc)
Implements drawing code.
Definition: TextBoxEditString.h:18
TextBox(int left, int top, int width, int height)
Constructor.
Definition: TextBox.h:45
virtual void NotifyContentChanged(Window *window)=0
Has to be implemented in target class.