AWind
KeyboardWindow.h
1 #pragma once
2 /*
3  AWind.h - Arduino window library support for Color TFT LCD Boards
4  Copyright (C)2015 Andrei Degtiarev. All right reserved
5 
6 
7  You can always find the latest version of the library at
8  https://github.com/AndreiDegtiarev/AWind
9 
10 
11  This library is free software; you can redistribute it and/or
12  modify it under the terms of the CC BY-NC-SA 3.0 license.
13  Please see the included documents for further information.
14 
15  Commercial use of this library requires you to buy a license that
16  will allow commercial use. This includes using the library,
17  modified or not, as a tool to sell products.
18 
19  The license applies to all part of the library including the
20  examples and tools supplied with the library.
21 */
22 #include "TextBoxNumber.h"
23 #include "TextBoxString.h"
24 #include "IDialogClosedEventReceiver.h"
25 #include "DecoratorPrimitives.h"
26 #include "Dialog.h"
27 
28 
29 extern uint8_t BigFont[];
30 extern uint8_t SmallFont[];
32 class KeyboardWindow : public Dialog
33 {
34  //TextBoxNumber *_targetTextBox;
35  TextBoxString * _editField;
36  TextBoxNumber _digidWindows[10];
37  TextBoxFString * _pointSymbol;
38  TextBoxFString * _backspaceSymbol;
39 
40  char _editBuffer[15];
41  int _editPosition;
42  const static int _buttonSize=39;
43  const static int _buttonDistance=5;
44  const static int _textOffset=9;
45 public:
47 
51  KeyboardWindow(int left,int top):Dialog(left,top,7*(_buttonSize+_buttonDistance)+_buttonDistance,3*(_buttonSize+_buttonDistance)+_buttonDistance)
52  {
53 
54  int x=_buttonDistance;
55  int y=_buttonDistance;
56  _editField=new TextBoxString(x,y,Width()-2*_buttonDistance,_buttonSize,"");
57  _editField->SetDecorators(Environment::Get()->FindDecorators(F("EditTextBoxReadOnly")));
58  _editField->SetFont(BigFont);
59  y+=_buttonSize+_buttonDistance;
60  _backspaceSymbol=new Button(5*(_buttonSize+_buttonDistance)+_buttonDistance,y,_buttonSize,_buttonSize,F("<-"));
61  _btnOK=new Button(6*(_buttonSize+_buttonDistance)+_buttonDistance,y,_buttonSize,_buttonSize,F("E"));
62  DecoratorList * btnDecorators=Environment::Get()->FindDecorators(F("Button"));
63  for(int i=0;i<10;i++)
64  {
65  x=(i-(i<5?0:5))*(_buttonSize+_buttonDistance);
66  if(i==5)
67  y+=_buttonSize+_buttonDistance;
68  //_digidWindows[i]=new TextBoxNumber(x+_buttonDistance,y,_buttonSize,_buttonSize,0);
69  _digidWindows[i].Move(x+_buttonDistance,y,_buttonSize,_buttonSize);
70  _digidWindows[i].SetNumber(i);
71  _digidWindows[i].SetDecorators(btnDecorators);
72  initTextBox(&_digidWindows[i]);
73  }
74  _pointSymbol=new Button(5*(_buttonSize+_buttonDistance)+_buttonDistance,y,_buttonSize,_buttonSize,F("."));
75  _btnCancel=new Button(6*(_buttonSize+_buttonDistance)+_buttonDistance,y,_buttonSize,_buttonSize,F("C"));
76  initTextBox(_editField);
77  initTextBox(_backspaceSymbol);
78  _backspaceSymbol->SetMargins(_textOffset,_textOffset*1.5);
79  _backspaceSymbol->SetFont(SmallFont);
80  initTextBox(_pointSymbol);
81  initTextBox(_btnCancel);
82  initTextBox(_btnOK);
83  }
84 protected:
85  void initTextBox(TextBox *text)
86  {
87  text->SetMargins(_textOffset,_textOffset);
88  text->RegisterTouchEventReceiver(this);
89  AddChild(text);
90  }
91 public:
93  void Initialize(float value,int precision)
94  {
95  _editPosition=0;
96  dtostrf(value,0,precision,_editBuffer);
97  _editField->SetText(_editBuffer);
98  _editField->Invalidate();
99  _editPosition=strlen(_editBuffer);
100  }
102  float GetNumber()
103  {
104  return atof(_editBuffer);
105  }
107  void DoControlMessage(Window *window)
108  {
109  //out<<F("Keybord notify")<<endln;
110  //if(Process_OK_Cancel_Touch(window))
111  // return;
112  /*if(window == _enterSymbol || window == _cancelSymbol)
113  {
114  if(_dialogClosedEventReceiver!=NULL)
115  _dialogClosedEventReceiver->NotifyDialogClosed(this,window == _enterSymbol?IDialogClosedEventReceiver::OK:IDialogClosedEventReceiver::Cancel);
116  }
117  else*/
118  {
119  bool needUpdate=false;
120  if(window == _backspaceSymbol && _editPosition>0)
121  {
122  _editPosition--;
123  _editBuffer[_editPosition]=0;
124  needUpdate=true;
125  }
126  else if(window == _pointSymbol && _editPosition<14)
127  {
128  _editBuffer[_editPosition++]='.';
129  _editBuffer[_editPosition]=0;
130  needUpdate=true;
131  }
132  else
133  {
134  for(int i=0;i<10;i++)
135  if(window == &_digidWindows[i] && _editPosition<14)
136  {
137  _editBuffer[_editPosition++]='0'+i;
138  _editBuffer[_editPosition]=0;
139  needUpdate=true;
140  break;
141  }
142  }
143  if(needUpdate)
144  _editField->SetText(_editBuffer);
145  }
146  }
147 };
float GetNumber()
Returns number from edit window.
Definition: KeyboardWindow.h:102
void RegisterTouchEventReceiver(ITouchEventReceiver *touchEventReceiver)
Registers receiver for touch event.
Definition: Window.h:102
Base class for all window objects. Provides basic window functionality.
Definition: Window.h:34
void SetText(T *text)
Initialize window with text.
Definition: TextBoxString.h:64
void SetFont(uint8_t *font)
Sets font.
Definition: TextBox.h:64
Base class for window with text content.
Definition: TextBox.h:30
Button * _btnCancel
Button Cancel has to be initialized in derived class.
Definition: Dialog.h:32
Text box for string. It is templat class and can be used for differnt type of string (see bellow type...
Definition: TextBoxString.h:25
Implements Keyboard window, that helps to enter text/number information into text fields...
Definition: KeyboardWindow.h:32
static Environment * Get()
Returns singltone instance of environment.
Definition: Environment.h:44
void Initialize(float value, int precision)
Initializes edit field with float value of desired precission.
Definition: KeyboardWindow.h:93
Button * _btnOK
Button OK has to be initialized in derived class.
Definition: Dialog.h:30
Text box for numbers.
Definition: TextBoxNumber.h:24
KeyboardWindow(int left, int top)
Constructor.
Definition: KeyboardWindow.h:51
virtual void Move(int left, int top, int width, int height)
Moves and resizes window relativly to the parent window.
Definition: Window.h:149
Base class for dialog objects. See Dialogs example Provides basic window functionality.
Definition: Dialog.h:25
void DoControlMessage(Window *window)
Process touch message from child controls.
Definition: KeyboardWindow.h:107
void Invalidate()
If function is called than the window manager updates the window.
Definition: Window.h:157
void AddChild(Window *window)
Adds window child window.
Definition: Window.h:194
void SetNumber(float number)
Initialize window with number.
Definition: TextBoxNumber.h:74
virtual void SetDecorators(DecoratorList *decorators)
Sets window decorators list.
Definition: Window.h:76
Implement button control.
Definition: Button.h:24
DecoratorList * FindDecorators(const __FlashStringHelper *id)
Finds registered decorators by the name.
Definition: Environment.h:59
int Width()
Returns window width.
Definition: Window.h:184
void SetMargins(int offset_x, int offset_y)
Defines offset from left and top for text.
Definition: TextBox.h:58