SpinBox.h
1 /* Copyright (C) 2017 Marc Boris Duerner
2 
3  This library is free software; you can redistribute it and/or
4  modify it under the terms of the GNU Lesser General Public
5  License as published by the Free Software Foundation; either
6  version 2.1 of the License, or (at your option) any later version.
7 
8  As a special exception, you may use this file as part of a free
9  software library without restriction. Specifically, if other files
10  instantiate templates or use macros or inline functions from this
11  file, or you compile this file and link it with other files to
12  produce an executable, this file does not by itself cause the
13  resulting executable to be covered by the GNU General Public
14  License. This exception does not however invalidate any other
15  reasons why the executable file might be covered by the GNU Library
16  General Public License.
17 
18  This library is distributed in the hope that it will be useful,
19  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21  Lesser General Public License for more details.
22 
23  You should have received a copy of the GNU Lesser General Public
24  License along with this library; if not, write to the Free Software
25  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
26  MA 02110-1301 USA
27 */
28 
29 #ifndef PT_FORMS_SPINBOX_H
30 #define PT_FORMS_SPINBOX_H
31 
32 #include <Pt/Forms/Control.h>
33 #include <Pt/Forms/Button.h>
34 #include <Pt/Forms/PushButton.h>
35 #include <Pt/Forms/LineEditor.h>
36 #include <Pt/Forms/Adjustment.h>
37 #include <Pt/Forms/SpinBoxStyler.h>
38 #include <Pt/SmartPtr.h>
39 #include <Pt/String.h>
40 
41 namespace Pt {
42 
43 namespace Forms {
44 
52 class PT_FORMS_API SpinBoxButton : public Button
53 {
54  public:
55  typedef Button Base;
56 
59  enum Type
60  {
63  Up,
64 
67  Down
68  };
69 
70  public:
74 
78 
81  Type type() const;
82 
85  bool isPressed() const;
86 
87  protected:
90  virtual void onPressed();
91 
94  virtual void onReleased();
95 
98  virtual void onCanceled();
99 
100  protected:
103  virtual void onInvalidate();
104 
107  virtual void onPaint(PaintContext& context, const Gfx::RectF& rect);
108 
109  private:
110  Type _type;
111  bool _isPressed;
112 };
113 
114 
153 class PT_FORMS_API SpinBox : public Control
154 {
155  public:
156  typedef Control Base;
157 
158  public:
162 
166 
169  bool isEditable() const;
170 
175  void setEditable(bool e);
176 
179  int minimum() const;
180 
183  int maximum() const;
184 
187  void setRange(int min, int max);
188 
191  int value() const;
192 
195  void setValue(int n);
196 
199  const Pt::String& text() const;
200 
203  bool isEmpty() const;
204 
208 
212 
215  std::size_t cursorPosition() const;
216 
219  void setCursorPosition(std::size_t n);
220 
223  bool isAccepted() const;
224 
227  void setAccepted(bool a);
228 
231  bool isHighlighted() const;
232 
236 
240 
244 
245  public:
248  const Gfx::Brush& background() const;
249 
252  void setBackground(const Gfx::Brush& b);
253 
256  const Gfx::Brush& foreground() const;
257 
260  void setForeground(const Gfx::Brush& b);
261 
264  const Gfx::Pen& contour() const;
265 
268  void setContour(const Gfx::Pen& p);
269 
272  const Gfx::Color& textColor() const;
273 
276  void setTextColor(const Gfx::Color& color);
277 
280  Gfx::Font font() const;
281 
284  void setFont(const Gfx::Font& font);
285 
288  void setFontSize(std::size_t size);
289 
293 
297 
302  void setRenderer(SpinBoxRenderer* renderer);
303 
304  protected:
307  virtual Pt::String toText(int n) const;
308 
311  virtual bool toValue(const Pt::String& str, int& n) const;
312 
318  virtual bool onInput(const Pt::String& str) const;
319 
322  virtual void onStep(int n);
323 
324  private:
325  bool setInput(const Pt::String& str);
326 
327  void onUp();
328 
329  void onDown();
330 
331  protected:
334  virtual Gfx::SizeF onMeasure(const SizePolicy& policy);
335 
338  virtual void onLayout(const Gfx::RectF& rect);
339 
342  virtual void onInvalidate();
343 
346  virtual void onPaint(PaintContext& context, const Gfx::RectF& rect);
347 
350  virtual void onPaintChrome(PaintContext& context,
351  const Gfx::RectF& rect,
352  const Gfx::RectF& entryRect,
353  const Gfx::RectF& upButtonRect,
354  const Gfx::RectF& downButtonRect,
355  const SpinBoxState& state);
356 
359  virtual void onPaintText(PaintContext& context,
360  const Gfx::RectF& textRect,
361  const String& text,
362  const Gfx::PointF& textPos,
363  const Gfx::RectF& cursor,
364  const SpinBoxState& state);
365 
366  protected:
369  virtual bool onKeyEvent(const KeyEvent& ev);
370 
373  virtual bool onMouseEvent(const MouseEvent& ev);
374 
377  virtual bool onTouchEvent(const TouchEvent& ev);
378 
381  virtual bool onEnterEvent(const EnterEvent& ev);
382 
385  virtual bool onLeaveEvent(const LeaveEvent& ev);
386 
389  virtual void onFocusEvent(const FocusEvent& ev);
390 
391  private:
392  SpinBoxState spinBoxState() const;
393 
394  private:
395  Pt::Signal<int> _valueEdited;
396  Pt::Signal<const Pt::String&> _returnPressed;
397  Pt::Signal<const Pt::String&> _editingFinished;
398 
399  LineEditor _editor;
400  TextLine _line;
401  bool _isEditable;
402  bool _isAccepted;
403  bool _isTextChanged;
404  bool _isHighlighted;
405  double _pendingCursorX;
406  int _value;
407  int _minimum;
408  int _maximum;
409 
410  SpinBoxButton _downButton;
411  SpinBoxButton _upButton;
412  Gfx::RectF _entryRect;
413  Gfx::RectF _upButtonRect;
414  Gfx::RectF _downButtonRect;
415  Gfx::RectF _textRect;
416 
417  SpinBoxStyler _styler;
418 };
419 
420 } // namespace
421 
422 } // namespace
423 
424 #endif
virtual void onReleased()
Marks the control released.
virtual void onCanceled()
Restores the pressed state after an abandoned click.
Core module.
Definition: Allocator.h:33
Binds a spin box to the current style renderer.
Definition: SpinBoxStyler.h:308
Key press or release.
Definition: KeyEvent.h:50
virtual bool onKeyEvent(const KeyEvent &ev)
Inserts, deletes, or moves the caret, or completes an edit.
SpinBoxButton(Type type)
Creates an up or down control of type.
Touch press, move, or release.
Definition: TouchEvent.h:51
Single-line text, caret, and scroll helper.
Definition: LineEditor.h:57
Pointer entered a widget.
Definition: EnterEvent.h:49
Attributes for the drawing of outlines.
Definition: Pen.h:54
virtual bool onTouchEvent(const TouchEvent &ev)
Places the caret from a touch press when the box is editable.
virtual void onInvalidate()
Binds the styler.
int minimum() const
Returns the lower bound of the range.
std::size_t cursorPosition() const
Returns the caret index in the entry text.
Pt::Signal< const Pt::String & > & editingFinished()
Returns the signal emitted when focus loss completes an edit.
Type
Direction of the step control.
Definition: SpinBox.h:60
void setFontSlant(Gfx::Font::Slant slant)
Sets the widget-local font slant to slant.
~SpinBoxButton()
Destroys the step control.
const Gfx::Brush & background() const
Returns the effective background brush.
virtual void onPaintText(PaintContext &context, const Gfx::RectF &textRect, const String &text, const Gfx::PointF &textPos, const Gfx::RectF &cursor, const SpinBoxState &state)
Paints the entry text and caret for state.
const Pt::String & text() const
Returns the text shown in the entry.
void setCursorPosition(std::size_t n)
Sets the caret index in the entry text to n.
void setRange(int min, int max)
Sets the range to min through max and clamps the value.
virtual void onPressed()
Marks the control pressed.
Slant
Describes the requested font slant.
Definition: Font.h:76
void setBackground(const Gfx::Brush &b)
Sets the widget-local background brush to b.
Gfx::Font font() const
Returns the effective font.
int maximum() const
Returns the upper bound of the range.
void setValue(int n)
Sets the value to n, clamped to the range, and emits valueEdited().
SpinBox()
Creates a spin box.
Active painting session on a Forms paint surface.
Definition: PaintContext.h:51
void setEditable(bool e)
Sets whether the user can type the number.
void setAccepted(bool a)
Sets whether Return and focus loss complete an edit.
Adjustment textAdjustment() const
Returns the horizontal adjustment of the text.
Size with floating-point width and height.
Definition: Size.h:47
void setContour(const Gfx::Pen &p)
Sets the widget-local contour pen to p.
bool isPressed() const
Returns true if the control is currently pressed.
virtual bool toValue(const Pt::String &str, int &n) const
Parses str into n and returns true when the whole string is an integer.
const Gfx::Brush & foreground() const
Returns the effective foreground brush.
Point with floating-point X and Y coordinates.
Definition: Point.h:47
Reusable view used as application content.
Definition: Control.h:107
const Gfx::Color & textColor() const
Returns the effective text color.
virtual void onStep(int n)
Adds n to the value, clamped to the range.
void setForeground(const Gfx::Brush &b)
Sets the widget-local foreground brush to b.
virtual void onInvalidate()
Forwards invalidate to the base button.
Weight
Describes the requested font weight.
Definition: Font.h:61
@ Up
Increases the value.
Definition: SpinBox.h:63
bool isAccepted() const
Returns true if Return and focus loss complete an edit.
void setTextColor(const Gfx::Color &color)
Sets the widget-local text color to color.
virtual Gfx::SizeF onMeasure(const SizePolicy &policy)
Measures the entry, step controls, and frame.
void setFontWeight(Gfx::Font::Weight weight)
Sets the widget-local font weight to weight.
bool isEmpty() const
Returns true if the entry text is empty.
Type type() const
Returns whether the control increases or decreases the value.
virtual void onFocusEvent(const FocusEvent &ev)
Begins text input on focus, or emits editingFinished() on loss.
Integer editor with a range and step controls.
Definition: SpinBox.h:154
bool isEditable() const
Returns true if the user can type the number.
Aligns content along one axis.
Definition: Adjustment.h:60
Widget that hosts controls on a paint surface.
Definition: View.h:86
virtual bool onMouseEvent(const MouseEvent &ev)
Places the caret from a pointer press when the box is editable.
virtual void onPaint(PaintContext &context, const Gfx::RectF &rect)
Paints the entry, step controls, text, and caret.
Font request used for text drawing and measurement.
Definition: Font.h:56
Pt::Signal< int > & valueEdited()
Returns the signal emitted when the value changes.
Interaction control for press, release, and click.
Definition: Button.h:68
bool isHighlighted() const
Returns true if the pointer is over the box.
virtual Pt::String toText(int n) const
Returns the text shown for n.
void setTextAdjustment(Adjustment a)
Sets the horizontal adjustment of the text to a.
Transient visual state of a spin box.
Definition: SpinBoxStyler.h:48
virtual void onLayout(const Gfx::RectF &rect)
Places the entry and step controls in rect.
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
virtual bool onInput(const Pt::String &str) const
Returns true if str is accepted as typed input.
Reports that a widget gained or lost focus.
Definition: FocusEvent.h:48
void setFont(const Gfx::Font &font)
Sets the widget-local font to font.
virtual bool onLeaveEvent(const LeaveEvent &ev)
Clears the highlight when the pointer leaves.
Unicode capable basic_string.
Definition: Api-String.h:44
Renders the look of a spin box.
Definition: SpinBoxStyler.h:148
Fill description for shapes and text.
Definition: Brush.h:145
Rect with floating-point coordinates.
Definition: Rect.h:47
void setFontSize(std::size_t size)
Sets the widget-local font size to size.
~SpinBox()
Destroys the spin box.
void setRenderer(SpinBoxRenderer *renderer)
Assigns renderer as the family renderer.
virtual bool onEnterEvent(const EnterEvent &ev)
Highlights the box when the pointer enters.
Pointer movement or button change.
Definition: MouseEvent.h:134
Pt::Signal< const Pt::String & > & returnPressed()
Returns the signal emitted when Return completes an edit.
const Gfx::Pen & contour() const
Returns the effective contour pen.
int value() const
Returns the current value.
virtual void onPaintChrome(PaintContext &context, const Gfx::RectF &rect, const Gfx::RectF &entryRect, const Gfx::RectF &upButtonRect, const Gfx::RectF &downButtonRect, const SpinBoxState &state)
Paints the spin-box chrome for state.
virtual void onPaint(PaintContext &context, const Gfx::RectF &rect)
Paints the step control.
Up or down control used by a spin box.
Definition: SpinBox.h:53
Represents one laid-out line in a TextBlock.
Definition: TextBlock.h:58