libyuni
checkbox.h
1 /*
2 ** This file is part of libyuni, a cross-platform C++ framework (http://libyuni.org).
3 **
4 ** This Source Code Form is subject to the terms of the Mozilla Public License
5 ** v.2.0. If a copy of the MPL was not distributed with this file, You can
6 ** obtain one at http://mozilla.org/MPL/2.0/.
7 **
8 ** gitlab: https://gitlab.com/libyuni/libyuni/
9 ** github: https://github.com/libyuni/libyuni/ {mirror}
10 */
11 #ifndef __YUNI_UI_CONTROL_CHECKBOX_H__
12 # define __YUNI_UI_CONTROL_CHECKBOX_H__
13 
14 # include "../../yuni.h"
15 # include "control.h"
16 # include "../font.h"
17 
18 namespace Yuni
19 {
20 namespace UI
21 {
22 namespace Control
23 {
24 
25 
27  class CheckBox: public IControl
28  {
29  public:
32 
35 
36  public:
37  CheckBox(float x, float y, float width, float height):
38  IControl(x, y, width, height),
39  pChecked(false),
40  pBeingClicked(false)
41  {}
42 
44  IControl(position, size),
45  pChecked(false),
46  pBeingClicked(false)
47  {}
48 
50  virtual ~CheckBox() {}
51 
52  const String& text() const { return pText; }
53  void text(const AnyString& text) { pText = text; invalidate(); }
54 
56  void checked(bool newValue)
57  {
58  if (pChecked == newValue)
59  return;
60  pChecked = newValue;
61  invalidate();
62  // Callback
63  onCheckChanged(this, newValue);
64  }
66  bool checked() const { return pChecked; }
67 
69  virtual void draw(DrawingSurface::Ptr& surface, float xOffset, float yOffset) const override;
70 
71  private:
73  virtual EventPropagation mouseDown(Input::IMouse::Button btn, float, float) override
74  {
75  if (Input::IMouse::ButtonLeft == btn)
76  pBeingClicked = true;
77  return epStop;
78  }
80  virtual EventPropagation mouseUp(Input::IMouse::Button btn, float, float) override
81  {
82  if (Input::IMouse::ButtonLeft == btn && pBeingClicked)
83  {
84  pChecked = !pChecked;
85  invalidate();
86  onCheckChanged(this, pChecked);
87  }
88  pBeingClicked = false;
89  return epStop;
90  }
91 
92  private:
93  bool pChecked;
94 
95  bool pBeingClicked;
96 
97  String pText;
98 
99  String pHoverText;
100 
101  }; // class CheckBox
102 
103 
104 
105 
106 
107 } // namespace Control
108 } // namespace UI
109 } // namespace Yuni
110 
111 #endif // __YUNI_UI_CONTROL_CHECKBOX_H__
Ancestor::SmartPtrType< CheckBox >::Ptr Ptr
Smart pointer.
Definition: checkbox.h:31
float x() const
Get X position.
Definition: control.h:117
void checked(bool newValue)
Modify whether the checkbox is checked.
Definition: checkbox.h:56
A UI control is a part of 2D overlay that reacts to certain events.
Definition: control.h:41
Definition: program.cpp:19
float width() const
Get width.
Definition: control.h:142
A check box is a clickable control that has a boolean state: either checked or unchecked.
Definition: checkbox.h:27
float y() const
Get Y position.
Definition: control.h:119
Button
Button types.
Definition: mouse.h:44
float height() const
Get height.
Definition: control.h:144
SmartPtrType< IntrusiveSmartPtrType >::Ptr Ptr
Most convenient smartptr for this class.
Definition: intrusive.h:73
const Point2D< float > & size() const
Get size.
Definition: control.h:146
IControl()
Empty constructor.
Definition: control.h:81
Yuni::Bind< void(IControl *sender, bool newValue)> onCheckChanged
Checkbox checked callback.
Definition: checkbox.h:34
const Point2D< float > position() const
Get position.
Definition: control.h:121
Character stringThe class manipulates and stores sequences of characters.
Definition: fwd.h:32
virtual ~CheckBox()
Virtual destructor.
Definition: checkbox.h:50
void invalidate()
Invalidate the control (force redraw)
Definition: control.h:216
bool checked() const
Get whether the checkbox is checked.
Definition: checkbox.h:66
Definition: traits.h:29
virtual void draw(DrawingSurface::Ptr &surface, float xOffset, float yOffset) const override
Draw the checkbox on the surface.
Definition: checkbox.cpp:22