Phoenard
PHNTextBox.h
Go to the documentation of this file.
1 /*
2 The MIT License (MIT)
3 
4 This file is part of the Phoenard Arduino library
5 Copyright (c) 2014 Phoenard
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13 
14 The above copyright notice and this permission notice shall be included in
15 all copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 THE SOFTWARE.
24 */
25 
31 #include "PHNWidget.h"
32 #include "PHNScrollbar.h"
33 #include "PHNButton.h"
34 
35 #ifndef _PHN_WIDGET_TEXT_H_
36 #define _PHN_WIDGET_TEXT_H_
37 
38 #define PHN_WIDGET_TEXT_SCROLLWIDTH 16
39 #define PHN_WIDGET_TEXT_BLINKINTERVAL 600
40 #define PHN_WIDGET_TEXT_DRAGSELDELAY 1000
41 
55 class PHN_TextBox : public PHN_Widget, public PHN_TextContainer {
56  public:
58  PHN_TextBox(void);
60  void setDimension(int columns, int rows);
62  void setTextSize(int size);
64  void setMaxLength(int length);
66  const int maxLength() { return (textBuff.dataSize - 1); }
68  int textSize(void) { return this->_textSize; }
70  void showScrollbar(bool visible);
72  void showBackspace(bool visible);
74  void showCursor(bool visible);
76  PHN_Scrollbar &scrollbar(void) { return scroll; }
78  bool ensureVisible(int charPosition);
80  int selectionStart(void) { return selStart; }
82  int selectionLength(void) { return selLength; }
84  void setSelectionRange(int position, int length);
86  void setSelection(const char* text);
88  void setSelection(char character);
90  void backspace(void);
91 
93 
95  void invalidate(int startPosition);
97  void invalidate(int startPosition, int endPosition);
98 
99  virtual void setTextRaw(const char* text, int textLen);
100  virtual int textLength(void) { return this->length; }
101  virtual const char* text(void) { return (char*) textBuff.data; }
102  virtual void update(void);
103  virtual void draw(void);
104  private:
105  void drawTextFromTo(int charStart, int charEnd, bool drawBackground);
106  void drawCursor(bool visible);
107  void updateScrollLimit(void);
108  void updateScrollLimit(int col, int row);
109  PHN_Scrollbar scroll;
110  PHN_Button backspaceBtn;
111  int scrollOffset;
112  int rows, cols;
113  int chr_w, chr_h;
114  int _textSize;
115  int textAreaWidth;
116  int length;
117  int selLength, selStart, selEnd;
118  int dragStart;
119  long dragLastClick;
120  int invalidateStart, invalidateEnd;
121  bool invalidateAppended;
122  int cursor_x, cursor_y;
123  bool cursor_visible;
124  bool cursor_blinkVisible;
125  unsigned long cursor_blinkLast;
126  DataBuffer textBuff;
127 };
128 
129 #endif
Shows an area of text that allows selections and dynamic editing.
Definition: PHNTextBox.h:55
void setMaxLength(int length)
Sets the maximum length of the text that can be displayed.
Definition: PHNTextBox.cpp:52
void backspace(void)
Performs a backspace, deleting the selection or character before the cursor.
Definition: PHNTextBox.cpp:212
bool ensureVisible(int charPosition)
Ensures the character specified is displayed.
Definition: PHNTextBox.cpp:90
Contains the PHN_Button widget; a pressable three-state button.
Abstraction for a widget that can store and update text.
Definition: PHNTextContainer.h:41
Maintains data stored on the heap and frees it when the class is destructed.
Definition: DataBuffer.h:44
void showBackspace(bool visible)
Sets whether a button to perform a backspace is displayed.
Definition: PHNTextBox.cpp:72
Contains the PHN_Scrollbar widget; a widget for a bar that can be dragged.
virtual void draw(void)
Draw function routine, where widgets must perform drawing.
Definition: PHNTextBox.cpp:399
void invalidate(void)
Invalidated the widget, causing it to be re-drawn at a later time.
Definition: PHNWidget.cpp:119
void setSelection(const char *text)
Replaces the selected text with the text specified.
Definition: PHNTextBox.cpp:229
void setDimension(int columns, int rows)
Updates the width and height of the textbox to fit the columns and rows specified.
Definition: PHNTextBox.cpp:30
virtual const char * text(void)
Gets the text displayed.
Definition: PHNTextBox.h:101
PHN_Scrollbar & scrollbar(void)
Gets the scrollbar widget used to scroll text.
Definition: PHNTextBox.h:76
PHN_TextBox(void)
Initializes some defaults.
Definition: PHNTextBox.cpp:3
void setTextSize(int size)
Sets the font size of the text.
Definition: PHNTextBox.cpp:60
int selectionLength(void)
Gets the length of the user selection.
Definition: PHNTextBox.h:82
virtual int textLength(void)
Gets the length of the text displayed.
Definition: PHNTextBox.h:100
A single widget which covers a rectangular area of the screen.
Definition: PHNWidget.h:145
void setSelectionRange(int position, int length)
Sets up a new selection range for the user.
Definition: PHNTextBox.cpp:161
const int maxLength()
Gets the maximum length of the text that can be displayed.
Definition: PHNTextBox.h:66
int selectionStart(void)
Gets the start of the user selection.
Definition: PHNTextBox.h:80
The button widget can be clicked by the user, showing three states.
Definition: PHNButton.h:45
void showCursor(bool visible)
Sets whether the cursor is displayed.
Definition: PHNTextBox.cpp:86
virtual void update(void)
Update function routine, where widget logic must be performed.
Definition: PHNTextBox.cpp:282
virtual void setTextRaw(const char *text, int textLen)
Sets the new text to display using a buffer and length.
Definition: PHNTextBox.cpp:77
Holds the PHN_Widget and PHN_WidgetContainer classes used for managing widgets.
int textSize(void)
Gets the font size of the text.
Definition: PHNTextBox.h:68
Shows a scroll bar the user can drag.
Definition: PHNScrollbar.h:47
void showScrollbar(bool visible)
Sets whether the scrollbar is displayed.
Definition: PHNTextBox.cpp:67
unsigned char visible
Draw state flags of the widget (0=invisible, 0x1=visible, 0x2=drawn, 0x4=draw_disabled) ...
Definition: PHNWidget.h:224