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
41namespace Pt {
42
43namespace Forms {
44
52class PT_FORMS_API SpinBoxButton : public Button
53{
54 public:
55 typedef Button Base;
56
59 enum Type
60 {
64
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
153class 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
281
284 void setFont(const Gfx::Font& font);
285
288 void setFontSize(std::size_t size);
289
293
297
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
Aligns content along one axis.
Definition Adjustment.h:60
Button()
Creates a button that accepts focus.
Control()
Creates an unparented control.
Pointer entered a widget.
Definition EnterEvent.h:49
Reports that a widget gained or lost focus.
Definition FocusEvent.h:48
Key press or release.
Definition KeyEvent.h:50
Pointer left a widget.
Definition LeaveEvent.h:49
Single-line text, caret, and scroll helper.
Definition LineEditor.h:57
Pointer movement or button change.
Definition MouseEvent.h:134
Active painting session on a Forms paint surface.
Definition PaintContext.h:51
Constraint used when measuring a control.
Definition SizePolicy.h:48
Up or down control used by a spin box.
Definition SpinBox.h:53
Type
Direction of the step control.
Definition SpinBox.h:60
@ Up
Increases the value.
Definition SpinBox.h:63
@ Down
Decreases the value.
Definition SpinBox.h:67
virtual void onPaint(PaintContext &context, const Gfx::RectF &rect)
Paints the step control.
virtual void onCanceled()
Restores the pressed state after an abandoned click.
bool isPressed() const
Returns true if the control is currently pressed.
virtual void onPressed()
Marks the control pressed.
SpinBoxButton(Type type)
Creates an up or down control of type.
~SpinBoxButton()
Destroys the step control.
Type type() const
Returns whether the control increases or decreases the value.
virtual void onInvalidate()
Forwards invalidate to the base button.
virtual void onReleased()
Marks the control released.
Renders the look of a spin box.
Definition SpinBoxStyler.h:148
Transient visual state of a spin box.
Definition SpinBoxStyler.h:48
Binds a spin box to the current style renderer.
Definition SpinBoxStyler.h:308
virtual bool onMouseEvent(const MouseEvent &ev)
Places the caret from a pointer press when the box is editable.
bool isEditable() const
Returns true if the user can type the number.
Pt::Signal< const Pt::String & > & returnPressed()
Returns the signal emitted when Return completes an edit.
Adjustment textAdjustment() const
Returns the horizontal adjustment of the text.
virtual void onFocusEvent(const FocusEvent &ev)
Begins text input on focus, or emits editingFinished() on loss.
const Gfx::Color & textColor() const
Returns the effective text color.
virtual bool onTouchEvent(const TouchEvent &ev)
Places the caret from a touch press when the box is editable.
void setCursorPosition(std::size_t n)
Sets the caret index in the entry text to n.
Pt::Signal< const Pt::String & > & editingFinished()
Returns the signal emitted when focus loss completes an edit.
virtual void onPaint(PaintContext &context, const Gfx::RectF &rect)
Paints the entry, step controls, text, and caret.
void setForeground(const Gfx::Brush &b)
Sets the widget-local foreground brush to b.
virtual bool onEnterEvent(const EnterEvent &ev)
Highlights the box when the pointer enters.
const Pt::String & text() const
Returns the text shown in the entry.
const Gfx::Brush & background() const
Returns the effective background brush.
const Gfx::Brush & foreground() const
Returns the effective foreground brush.
virtual bool onInput(const Pt::String &str) const
Returns true if str is accepted as typed input.
Gfx::Font font() const
Returns the effective font.
Pt::Signal< int > & valueEdited()
Returns the signal emitted when the value changes.
virtual void onLayout(const Gfx::RectF &rect)
Places the entry and step controls in rect.
void setRange(int min, int max)
Sets the range to min through max and clamps the value.
bool isHighlighted() const
Returns true if the pointer is over the box.
void setValue(int n)
Sets the value to n, clamped to the range, and emits valueEdited().
virtual bool toValue(const Pt::String &str, int &n) const
Parses str into n and returns true when the whole string is an integer.
void setEditable(bool e)
Sets whether the user can type the number.
SpinBox()
Creates a spin box.
void setBackground(const Gfx::Brush &b)
Sets the widget-local background brush to b.
int value() const
Returns the current value.
int maximum() const
Returns the upper bound of the range.
void setContour(const Gfx::Pen &p)
Sets the widget-local contour pen to p.
virtual Gfx::SizeF onMeasure(const SizePolicy &policy)
Measures the entry, step controls, and frame.
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.
virtual bool onLeaveEvent(const LeaveEvent &ev)
Clears the highlight when the pointer leaves.
void setAccepted(bool a)
Sets whether Return and focus loss complete an edit.
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.
void setRenderer(SpinBoxRenderer *renderer)
Assigns renderer as the family renderer.
virtual bool onKeyEvent(const KeyEvent &ev)
Inserts, deletes, or moves the caret, or completes an edit.
void setTextColor(const Gfx::Color &color)
Sets the widget-local text color to color.
~SpinBox()
Destroys the spin box.
int minimum() const
Returns the lower bound of the range.
void setFont(const Gfx::Font &font)
Sets the widget-local font to font.
const Gfx::Pen & contour() const
Returns the effective contour pen.
bool isAccepted() const
Returns true if Return and focus loss complete an edit.
bool isEmpty() const
Returns true if the entry text is empty.
virtual Pt::String toText(int n) const
Returns the text shown for n.
void setFontWeight(Gfx::Font::Weight weight)
Sets the widget-local font weight to weight.
void setTextAdjustment(Adjustment a)
Sets the horizontal adjustment of the text to a.
std::size_t cursorPosition() const
Returns the caret index in the entry text.
virtual void onInvalidate()
Binds the styler.
virtual void onStep(int n)
Adds n to the value, clamped to the range.
void setFontSize(std::size_t size)
Sets the widget-local font size to size.
void setFontSlant(Gfx::Font::Slant slant)
Sets the widget-local font slant to slant.
Represents one laid-out line in a TextBlock.
Definition TextBlock.h:58
Touch press, move, or release.
Definition TouchEvent.h:51
const Gfx::SizeF & size() const
Returns the size in local logical coordinates.
const Cursor * cursor() const
Returns this widget's cursor or the default cursor.
Fill color, gradient or texture.
Definition Brush.h:151
8-bit ARGB color.
Definition Color.h:65
Font family, size and style.
Definition Font.h:63
Slant
Describes the requested font slant.
Definition Font.h:83
Weight
Describes the requested font weight.
Definition Font.h:68
Outline color, width and style.
Definition Pen.h:59
Multicast Signal to call multiple slots.
Definition Signal.h:190
Unicode capable basic_string.
Definition Api-String.h:67
Graphical user interfaces.
Definition ActivateEvent.h:40
Core module.
Definition Allocator.h:33