Hasty Badger
Small UI library (a branch of Turbo Badger)
 All Classes Namespaces Functions Variables Enumerations Enumerator Friends Groups Pages
tb_editfield.h
1 // ================================================================================
2 // == This file is a part of Turbo Badger. (C) 2011-2014, Emil SegerÃ¥s ==
3 // == See tb_core.h for more information. ==
4 // ================================================================================
5 
6 #ifndef TB_EDITFIELD_H
7 #define TB_EDITFIELD_H
8 
9 #include "tb_widgets_common.h"
10 #include "tb_msg.h"
11 #include "tb_style_edit.h"
12 #include <stdlib.h>
13 
14 namespace tb {
15 
18 enum EDIT_TYPE {
19  EDIT_TYPE_TEXT,
20  EDIT_TYPE_SEARCH,
21  EDIT_TYPE_PASSWORD,
22  EDIT_TYPE_EMAIL,
23  EDIT_TYPE_PHONE,
24  EDIT_TYPE_URL,
25  EDIT_TYPE_NUMBER
26 };
27 
45 {
46 public:
47  class TBEditField *editfield;
48  virtual int GetContent(const char *text);
49  virtual TBTextFragmentContent *CreateFragmentContent(const char *text, int text_len);
50 };
51 
56 {
57 private: // May only be used by TBEditField.
58  friend class TBEditField;
60 public:
61  virtual void OnPaintChildren(const PaintProps &paint_props);
62  virtual void GetChildTranslation(int &x, int &y) const;
63  virtual WIDGET_HIT_STATUS GetHitStatus(int x, int y);
64 };
65 
75 {
76 public:
77  // For safe typecasting
78  TBOBJECT_SUBCLASS(TBEditField, TBWidget);
79 
80  TBEditField();
81  ~TBEditField();
82 
85 
88  void SetMultiline(bool multiline);
89  bool GetMultiline() const { return m_style_edit.packed.multiline_on; }
90 
92  void SetStyling(bool styling);
93  bool GetStyling() const { return m_style_edit.packed.styling_on; }
94 
98  void SetReadOnly(bool readonly);
99  bool GetReadOnly() const { return m_style_edit.packed.read_only; }
100 
102  void SetWrapping(bool wrapping);
103  bool GetWrapping() const { return m_style_edit.packed.wrapping; }
104 
109  void SetAdaptToContentSize(bool adapt);
110  bool GetAdaptToContentSize() const { return m_adapt_to_content_size; }
111 
118  void SetVirtualWidth(int virtual_width);
119  int GetVirtualWidth() const { return m_virtual_width; }
120 
122  TBStyleEdit *GetStyleEdit() { return &m_style_edit; }
123 
126  void SetEditType(EDIT_TYPE type);
127  EDIT_TYPE GetEditType() { return m_edit_type; }
128 
133  virtual bool GetCustomSkinCondition(const TBSkinCondition::CONDITION_INFO &info);
134 
139  void SetTextAlign(TB_TEXT_ALIGN align) { m_style_edit.SetAlign(align); }
140  TB_TEXT_ALIGN GetTextAlign() const { return m_style_edit.align; }
141 
144  virtual void SetValueDouble(double value);
145 
147  virtual double GetValueDouble() const { return GetText().atof(); }
148 
149  virtual bool SetText(const TBStr & text) { return m_style_edit.SetText(text, TB_CARET_POS_BEGINNING); }
150  virtual bool GetText(TBStr &text) const { return m_style_edit.GetText(text); }
151  using TBWidget::GetText;
152 
153  using TBWidget::Invalidate;
154 
157  bool SetText(const TBStr & text, TB_CARET_POS pos)
158  { return m_style_edit.SetText(text, pos); }
159 
161  virtual bool SetPlaceholderText(const TBStr & text) { return m_placeholder.SetText(text); }
162  virtual bool GetPlaceholderText(TBStr &text) const { return m_placeholder.GetText(text); }
163 
165  void SetFormat(TBStr format);
166  const TBStr & GetFormat() const { return m_format; }
167 
168  virtual void ScrollTo(int x, int y);
169  virtual TBWidget::ScrollInfo GetScrollInfo();
170  virtual TBWidget *GetScrollRoot() { return &m_root; }
171 
172  virtual bool OnEvent(const TBWidgetEvent &ev);
173  virtual void OnPaint(const PaintProps &paint_props);
174  virtual void OnPaintChildren(const PaintProps &paint_props);
175  virtual void OnInflate(const INFLATE_INFO &info);
176  virtual void OnDeflate(const INFLATE_INFO &info);
177  virtual void OnAdded();
178  virtual void OnFontChanged();
179  virtual void OnFocusChanged(bool focused);
180  virtual void OnResized(int old_w, int old_h);
181  virtual TBWidget *GetContentRoot() { return &m_root; }
182 
184 
185  virtual void OnMessageReceived(TBMessage *msg);
186 private:
187  TBScrollBar m_scrollbar_x;
188  TBScrollBar m_scrollbar_y;
189  TBWidgetString m_placeholder;
190  EDIT_TYPE m_edit_type;
191  TBStr m_format;
192  TBEditFieldScrollRoot m_root;
193  TBEditFieldContentFactory m_content_factory;
194  TBStyleEdit m_style_edit;
195  bool m_adapt_to_content_size;
196  int m_virtual_width;
197  void UpdateScrollbarVisibility(bool multiline);
198 
199  // == TBStyleEditListener =======================
200  virtual void OnChange();
201  virtual bool OnEnter();
202  virtual void Invalidate(const TBRect &rect);
203  virtual void DrawString(int32_t x, int32_t y, TBFontFace *font, const TBColor &color, const char *str, int32_t len);
204  virtual void DrawRect(const TBRect &rect, const TBColor &color);
205  virtual void DrawRectFill(const TBRect &rect, const TBColor &color);
206  virtual void DrawTextSelectionBg(const TBRect &rect);
207  virtual void DrawContentSelectionFg(const TBRect &rect);
208  virtual void DrawCaret(const TBRect &rect);
209  virtual void Scroll(int32_t dx, int32_t dy);
210  virtual void UpdateScrollbars();
211  virtual void CaretBlinkStart();
212  virtual void CaretBlinkStop();
213 };
214 
215 } // namespace tb
216 
217 #endif // TB_EDITFIELD_H
void SetAlign(TB_TEXT_ALIGN align)
Set the default text alignment and all currently selected blocks, or the block of the current caret p...
Definition: tb_style_edit.cpp:1964
virtual TBWidget::ScrollInfo GetScrollInfo()
If this is a widget that scroll children (see GetChildTranslation), it should return the current scro...
Definition: tb_editfield.cpp:203
void SetWrapping(bool wrapping)
Set to true if the text should wrap if multi line is enabled (See SetMultiline).
Definition: tb_editfield.cpp:135
void SetTextAlign(TB_TEXT_ALIGN align)
Set which alignment the text should have if the space given when painting is larger than the text...
Definition: tb_editfield.h:139
Listener for TBStyleEdit.
Definition: tb_style_edit.h:24
The base TBWidget class.
Definition: tb_widgets.h:418
bool SetText(const TBStr &text, TB_CARET_POS pos)
&lt; Make Invalidate in base class available.
Definition: tb_editfield.h:157
virtual void OnPaint(const PaintProps &paint_props)
Callback for painting this widget.
Definition: tb_editfield.cpp:308
Content for a non-text TBTextFragment.
Definition: tb_style_edit_content.h:17
void SetStyling(bool styling)
Set if styling should be enabled or not.
Definition: tb_editfield.cpp:121
TBFontFace represents a loaded font that can measure and render strings.
Definition: tb_font_renderer.h:142
virtual TBWidget * GetScrollRoot()
If this widget is implementing ScrollTo and GetScrollInfo but the corresponding GetChildTranslation i...
Definition: tb_editfield.h:170
Definition: tb_widgets.h:112
TBEditField is a one line or multi line textfield that is editable or read-only.
Definition: tb_editfield.h:74
TB_TEXT_ALIGN
TB_TEXT_ALIGN specifies horizontal text alignment.
Definition: tb_widgets_common.h:22
virtual void ScrollTo(int x, int y)
If this is a widget that scroll children (see GetChildTranslation), it should scroll to the coordinat...
Definition: tb_editfield.cpp:193
void SetVirtualWidth(int virtual_width)
The virtual width is only used if the size is adapting to content size (See SetAdaptToContentSize) an...
Definition: tb_editfield.cpp:100
PaintProps contains properties needed for painting a widget.
Definition: tb_widgets.h:731
TBScrollBar is a scroll bar in the given axis.
Definition: tb_widgets_common.h:320
The default content factory for embedded content in TBEditField with styling enabled.
Definition: tb_editfield.h:44
double atof() const
Call atof() on the string.
Definition: tb_str.h:96
virtual PreferredSize OnCalculatePreferredContentSize(const SizeConstraints &constraints)
Calculate the preferred content size for this widget.
Definition: tb_editfield.cpp:379
Specifies size constraints used during size calculations.
Definition: tb_widgets.h:321
TBEditFieldScrollRoot - Internal for TBEditField.
Definition: tb_editfield.h:55
virtual WIDGET_HIT_STATUS GetHitStatus(int x, int y)
Get hit status tests if this widget should be hit at the given coordinate.
Definition: tb_editfield.cpp:568
virtual bool GetCustomSkinCondition(const TBSkinCondition::CONDITION_INFO &info)
Support custom skin condition properties.
Definition: tb_editfield.cpp:157
virtual bool GetText(TBStr &text) const
Get the text of this widget.
Definition: tb_editfield.h:150
TBWidgetString holds a string that can be painted as one line with the set alignment.
Definition: tb_widgets_common.h:33
virtual void OnAdded()
Called when this widget has been added to a parent (after calling OnChildAdded on parent)...
Definition: tb_editfield.cpp:353
virtual TBTextFragmentContent * CreateFragmentContent(const char *text, int text_len)
Create content for a string previosly consumed by calling GetContent.
Definition: tb_editfield.cpp:632
EDIT_TYPE
EDIT_TYPE - These types does not restrict input (may change in the future).
Definition: tb_editfield.h:18
Edit and formats TBTextFragment&#39;s.
Definition: tb_style_edit.h:319
void SetMultiline(bool multiline)
Set if multiple lines should be allowed or not.
Definition: tb_editfield.cpp:110
WIDGET_HIT_STATUS
Hit status return value for TBWidget::GetHitStatus.
Definition: tb_widgets.h:398
virtual void OnFontChanged()
Called when the font has changed.
Definition: tb_editfield.cpp:358
virtual void OnFocusChanged(bool focused)
Called when the focus has changed.
Definition: tb_editfield.cpp:363
virtual TBWidget * GetContentRoot()
Get this widget or a child widget that should be root for other children.
Definition: tb_editfield.h:181
INFLATE_INFO contains info passed to TBWidget::OnInflate during resource loading. ...
Definition: tb_widgets_reader.h:21
TBMessageHandler handles a list of pending messages posted to itself.
Definition: tb_msg.h:72
TBStr is a simple string class.
Definition: tb_str.h:62
virtual void OnMessageReceived(TBMessage *msg)
Called when a message is delivered.
Definition: tb_editfield.cpp:433
virtual void SetValueDouble(double value)
Set the value in double precision.
Definition: tb_editfield.cpp:179
Simple rectangle class.
Definition: tb_geometry.h:25
Creates TBTextFragmentContent if the sequence of text matches known content.
Definition: tb_style_edit.h:46
virtual void OnInflate(const INFLATE_INFO &info)
Called when this widget is inflated from resources, before any children have been inflated...
Definition: tb_widgets_reader.cpp:397
TBMessage is a message created and owned by TBMessageHandler.
Definition: tb_msg.h:47
TBColor contains a 32bit color.
Definition: tb_color.h:21
TBRect GetVisibleRect()
Get the visible rect (the GetPaddingRect() minus any scrollbars)
Definition: tb_editfield.cpp:75
void Invalidate()
Invalidate should be called if the widget need to be repainted, to make sure the renderer repaints it...
Definition: tb_widgets.cpp:134
virtual void GetChildTranslation(int &x, int &y) const
Return translation the children should have.
Definition: tb_editfield.cpp:561
void SetFormat(TBStr format)
The printf formatting string, used for formatting Set*Value().
Definition: tb_editfield.cpp:186
TBStyleEdit * GetStyleEdit()
Get the TBStyleEdit object that contains more functions and settings.
Definition: tb_editfield.h:122
virtual void OnPaintChildren(const PaintProps &paint_props)
Callback for painting child widgets.
Definition: tb_editfield.cpp:339
virtual double GetValueDouble() const
Return the value in double precision.
Definition: tb_editfield.h:147
virtual bool SetText(const TBStr &text)
Set the text of this widget.
Definition: tb_editfield.h:149
virtual int GetContent(const char *text)
Should return then length of the text that represents content that can be created by this factory...
Definition: tb_editfield.cpp:627
virtual bool OnEvent(const TBWidgetEvent &ev)
Callback for handling events.
Definition: tb_editfield.cpp:215
void SetReadOnly(bool readonly)
Set if read only mode should be enabled.
Definition: tb_editfield.cpp:126
PreferredSize contains size preferences for a TBWidget.
Definition: tb_widgets.h:277
void SetAdaptToContentSize(bool adapt)
Set to true if the preferred size of this editfield should adapt to the size of the content (disabled...
Definition: tb_editfield.cpp:92
virtual void OnDeflate(const INFLATE_INFO &info)
Called when this widget is deflated to a node, before any children have been deflated.
Definition: tb_widgets_reader.cpp:431
virtual void OnResized(int old_w, int old_h)
Called when this widget has been resized.
Definition: tb_editfield.cpp:368
virtual void OnPaintChildren(const PaintProps &paint_props)
Callback for painting child widgets.
Definition: tb_editfield.cpp:550
void SetEditType(EDIT_TYPE type)
Set the edit type that is a hint for virtual keyboards about what the content should be...
Definition: tb_editfield.cpp:147
virtual bool SetPlaceholderText(const TBStr &text)
Set the placeholder text.
Definition: tb_editfield.h:161
TBStr GetText() const
Get the text of this widget.
Definition: tb_widgets.h:942