LineEdit.h
1 /* Copyright (C) 2015 Marc Boris Duerner
2  Copyright (C) 2015 Laurentiu-Gheorghe Crisan
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Lesser General Public
6  License as published by the Free Software Foundation; either
7  version 2.1 of the License, or (at your option) any later version.
8 
9  As a special exception, you may use this file as part of a free
10  software library without restriction. Specifically, if other files
11  instantiate templates or use macros or inline functions from this
12  file, or you compile this file and link it with other files to
13  produce an executable, this file does not by itself cause the
14  resulting executable to be covered by the GNU General Public
15  License. This exception does not however invalidate any other
16  reasons why the executable file might be covered by the GNU Library
17  General Public License.
18 
19  This library is distributed in the hope that it will be useful,
20  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  Lesser General Public License for more details.
23 
24  You should have received a copy of the GNU Lesser General Public
25  License along with this library; if not, write to the Free Software
26  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
27  MA 02110-1301 USA
28 */
29 
30 #ifndef PT_FORMS_LINEEDIT_H
31 #define PT_FORMS_LINEEDIT_H
32 
33 #include <Pt/Forms/Control.h>
34 #include <Pt/Forms/LineEditStyler.h>
35 #include <Pt/Forms/LineEditor.h>
36 #include <Pt/Forms/Adjustment.h>
37 #include <Pt/SmartPtr.h>
38 #include <Pt/String.h>
39 
40 namespace Pt {
41 
42 namespace Forms {
43 
80 class PT_FORMS_API LineEdit : public Control
81 {
82  public:
83  typedef Control Base;
84 
87  enum EchoMode
88  {
91  Normal = 0,
92 
95  Hidden = 1,
96 
99  Masked = 2
100  };
101 
102  public:
106 
110 
113  bool isEditable() const;
114 
119  void setEditable(bool e);
120 
123  const Pt::String& text() const;
124 
127  void setText(const Pt::String& str);
128 
131  bool isEmpty() const;
132 
138  const Pt::String& displayText() const;
139 
142  const Pt::String& placeholderText() const;
143 
147 
151 
154  void setEchoMode(EchoMode mode);
155 
159 
163 
166  std::size_t cursorPosition() const;
167 
170  void setCursorPosition(std::size_t n);
171 
174  bool isAccepted() const;
175 
178  void setAccepted(bool a);
179 
182  bool isHighlighted() const;
183 
187 
191 
195 
196  public:
199  const Gfx::Brush& background() const;
200 
203  void setBackground(const Gfx::Brush& b);
204 
207  const Gfx::Pen& contour() const;
208 
211  void setContour(const Gfx::Pen& p);
212 
215  const Gfx::Color& textColor() const;
216 
219  void setTextColor(const Gfx::Color& color);
220 
223  Gfx::Font font() const;
224 
227  void setFont(const Gfx::Font& font);
228 
231  void setFontSize(std::size_t size);
232 
236 
240 
245  void setRenderer(LineEditRenderer* renderer);
246 
250 
251  protected:
254  virtual Gfx::SizeF onMeasure(const SizePolicy& policy);
255 
258  virtual void onLayout(const Gfx::RectF& rect);
259 
262  virtual void onInvalidate();
263 
266  virtual void onPaint(PaintContext& context, const Gfx::RectF& rect);
267 
270  virtual void onPaintChrome(PaintContext& context,
271  const Gfx::RectF& rect,
272  const Gfx::RectF& textRect,
273  const String& text,
274  const Gfx::PointF& textPos,
275  const Gfx::RectF& cursor,
276  const Gfx::RectF& selection,
277  const LineEditState& state);
278 
279  protected:
282  virtual bool onEnterEvent(const EnterEvent& ev);
283 
286  virtual bool onLeaveEvent(const LeaveEvent& ev);
287 
290  virtual bool onKeyEvent(const KeyEvent& ev);
291 
294  virtual bool onMouseEvent(const MouseEvent& ev);
295 
298  virtual bool onTouchEvent(const TouchEvent& ev);
299 
302  virtual void onResizeEvent(const ResizeEvent& ev);
303 
306  virtual void onFocusEvent(const FocusEvent& ev);
307 
308  private:
309  Pt::Signal<const Pt::String&> _textEdited;
310  Pt::Signal<const Pt::String&> _returnPressed;
311  Pt::Signal<const Pt::String&> _editingFinished;
312 
313  LineEditor _editor;
314  TextLine _line;
315  Pt::String _placeholderText;
316  bool _isEditable;
317  bool _isAccepted;
318  bool _isTextChanged;
319  bool _isHighlighted;
320  double _pendingCursorX;
321  EchoMode _echoMode;
322  double _spacing;
323 
324  LineEditStyler _styler;
325 
326  Gfx::RectF _textRect;
327  Gfx::RectF _cursorRect;
328 };
329 
330 } // namespace
331 
332 } // namespace
333 
334 #endif
Core module.
Definition: Allocator.h:33
Key press or release.
Definition: KeyEvent.h:50
Adjustment textAdjustment() const
Returns the horizontal adjustment of the text.
EchoMode echoMode() const
Returns how entered text is shown.
Touch press, move, or release.
Definition: TouchEvent.h:51
Single-line text, caret, and scroll helper.
Definition: LineEditor.h:57
EchoMode
How entered text is shown.
Definition: LineEdit.h:88
Pointer entered a widget.
Definition: EnterEvent.h:49
void setFontSlant(Gfx::Font::Slant slant)
Sets the widget-local font slant to slant.
Attributes for the drawing of outlines.
Definition: Pen.h:54
Gfx::Font font() const
Returns the effective font.
virtual bool onTouchEvent(const TouchEvent &ev)
Places the caret from a touch press when the field is editable.
void setFontSize(std::size_t size)
Sets the widget-local font size to size.
const Pt::String & placeholderText() const
Returns the placeholder shown while the field is empty.
Pt::Signal< const Pt::String & > & returnPressed()
Returns the signal emitted when Return completes an edit.
Reports that a widget was resized.
Definition: ResizeEvent.h:50
void setFont(const Gfx::Font &font)
Sets the widget-local font to font.
virtual bool onMouseEvent(const MouseEvent &ev)
Places the caret from a pointer press when the field is editable.
Renders the look of a line edit.
Definition: LineEditStyler.h:117
LineEditState lineEditState() const
Returns the transient visual state for the current paint pass.
bool isEditable() const
Returns true if the user can change the text.
void setTextAdjustment(Adjustment a)
Sets the horizontal adjustment of the text to a.
const Pt::String & displayText() const
Returns the text shown in the entry.
Binds a line edit to the current style renderer.
Definition: LineEditStyler.h:265
Slant
Describes the requested font slant.
Definition: Font.h:76
void setPlaceholderText(const Pt::String &s)
Sets the placeholder shown while the field is empty to s.
const Gfx::Pen & contour() const
Returns the effective contour pen.
virtual void onFocusEvent(const FocusEvent &ev)
Begins text input on focus, or emits editingFinished() on loss.
Transient visual state of a line edit.
Definition: LineEditStyler.h:48
const Gfx::Color & textColor() const
Returns the effective text color.
Active painting session on a Forms paint surface.
Definition: PaintContext.h:51
virtual bool onKeyEvent(const KeyEvent &ev)
Inserts, deletes, or moves the caret, or completes an edit.
const Gfx::Brush & background() const
Returns the effective background brush.
virtual void onLayout(const Gfx::RectF &rect)
Places the entry, caret, and text in rect.
Size with floating-point width and height.
Definition: Size.h:47
Point with floating-point X and Y coordinates.
Definition: Point.h:47
virtual void onResizeEvent(const ResizeEvent &ev)
Forwards the resize to the base control.
std::size_t cursorPosition() const
Returns the caret index in the entered text.
Reusable view used as application content.
Definition: Control.h:107
void setRenderer(LineEditRenderer *renderer)
Assigns renderer as the family renderer.
Weight
Describes the requested font weight.
Definition: Font.h:61
bool isEmpty() const
Returns true if the entered text is empty.
Pt::Signal< const Pt::String & > & editingFinished()
Returns the signal emitted when focus loss completes an edit.
void setEchoMode(EchoMode mode)
Sets how entered text is shown to mode.
void setBackground(const Gfx::Brush &b)
Sets the widget-local background brush to b.
LineEdit()
Creates an empty line edit.
virtual void onPaintChrome(PaintContext &context, const Gfx::RectF &rect, const Gfx::RectF &textRect, const String &text, const Gfx::PointF &textPos, const Gfx::RectF &cursor, const Gfx::RectF &selection, const LineEditState &state)
Paints the line-edit chrome for state.
void setTextColor(const Gfx::Color &color)
Sets the widget-local text color to color.
void setCursorPosition(std::size_t n)
Sets the caret index in the entered text to n.
Pt::Signal< const Pt::String & > & textEdited()
Returns the signal emitted when the entered text changes.
Aligns content along one axis.
Definition: Adjustment.h:60
Widget that hosts controls on a paint surface.
Definition: View.h:86
virtual Gfx::SizeF onMeasure(const SizePolicy &policy)
Measures the entry and frame.
Font request used for text drawing and measurement.
Definition: Font.h:56
virtual bool onLeaveEvent(const LeaveEvent &ev)
Clears the highlight when the pointer leaves.
Pointer left a widget.
Definition: LeaveEvent.h:49
Constraint used when measuring a control.
Definition: SizePolicy.h:48
Standard color type.
Definition: Color.h:47
bool isHighlighted() const
Returns true if the pointer is over the field.
void setFontWeight(Gfx::Font::Weight weight)
Sets the widget-local font weight to weight.
Reports that a widget gained or lost focus.
Definition: FocusEvent.h:48
~LineEdit()
Destroys the line edit.
void setContour(const Gfx::Pen &p)
Sets the widget-local contour pen to p.
void setAccepted(bool a)
Sets whether Return and focus loss complete an edit.
Unicode capable basic_string.
Definition: Api-String.h:44
void setEditable(bool e)
Sets whether the user can change the text.
Fill description for shapes and text.
Definition: Brush.h:145
virtual bool onEnterEvent(const EnterEvent &ev)
Highlights the field when the pointer enters.
Rect with floating-point coordinates.
Definition: Rect.h:47
void setText(const Pt::String &str)
Sets the entered text to str and emits textEdited().
virtual void onPaint(PaintContext &context, const Gfx::RectF &rect)
Paints the entry, text, selection, and caret.
virtual void onInvalidate()
Binds the styler.
Pointer movement or button change.
Definition: MouseEvent.h:134
Single-line text field.
Definition: LineEdit.h:81
bool isAccepted() const
Returns true if Return and focus loss complete an edit.
const Pt::String & text() const
Returns the entered text.
Represents one laid-out line in a TextBlock.
Definition: TextBlock.h:58