xtd - Reference Guide  0.2.0
Modern c++17/20 framework to create console, GUI and unit test applications on Windows, macOS, Linux, iOS and android.
switch_button.h
Go to the documentation of this file.
1 #include <optional>
5 #include "button_base.h"
6 #include "control_paint.h"
7 
8 namespace xtd {
9  namespace forms {
19  class switch_button : public button_base {
20  public:
22  switch_button() = default;
23 
27  virtual bool auto_check() const {return auto_check_;}
33  if (auto_check_ != auto_check)
34  auto_check_ = auto_check;
35  return *this;
36  }
37 
40  virtual xtd::drawing::color checked_back_color() const {return checked_back_color_.value_or(xtd::forms::theme_colors::current_theme().accent());}
44  if (!checked_back_color_.has_value() || checked_back_color_.value() != color) {
45  checked_back_color_ = color;
46  invalidate();
47  }
48  return *this;
49  }
51  virtual switch_button& checked_back_color(nullptr_t) {
52  if (checked_back_color_.has_value()) {
53  checked_back_color_.reset();
54  invalidate();
55  }
56  return *this;
57  }
59 
62  virtual xtd::drawing::color slider_color() const {return slider_color_.value_or(back_color());}
66  if (!slider_color_.has_value() || slider_color_.value() != color) {
67  slider_color_ = color;
68  invalidate();
69  }
70  return *this;
71  }
73  virtual switch_button& slider_color(nullptr_t) {
74  if (slider_color_.has_value()) {
75  slider_color_.reset();
76  invalidate();
77  }
78  return *this;
79  }
81 
84  virtual bool rounded() const {return rounded_;}
88  virtual switch_button& rounded(bool value) {
89  if (rounded_ != value) {
90  rounded_ = value;
91  invalidate();
92  }
93  return *this;
94  }
95 
98  virtual bool checked() const {return checked_;}
102  virtual switch_button& checked(bool checked) {
103  if (checked_ != checked) {
104  checked_ = checked;
105  invalidate();
107  }
108  return *this;
109  }
110 
113  virtual bool show_text() const {return show_text_;}
117  virtual switch_button& show_text(bool value) {
118  if (show_text_ != value) {
119  show_text_ = value;
120  invalidate();
121  }
122  return *this;
123  }
124 
128 
129  protected:
134  virtual void on_checked_changed(const event_args& e) {checked_changed(*this, e);}
135 
136  void on_mouse_down(const mouse_event_args& e) override {
138  if (auto_check_) checked(!checked());
139  }
140 
141  void on_handle_created(const event_args& e) override {
143  invalidate();
144  }
145 
146  void on_resize(const event_args& e) override {
148  invalidate();
149  }
150 
151  void on_paint(paint_event_args& e) override {
152  drawing::color button_back_color = checked_ ? checked_back_color() : xtd::forms::theme_colors::current_theme().gray_text();
153  drawing::color text_color = checked_ ? fore_color() : drawing::color::average(button_back_color, fore_color(), .33);
155  xtd::ustring text = checked_ ? "ON" : "OFF";
156 
157  if (!enabled()) {
158  button_back_color = drawing::color::average(back_color(), button_back_color, .33);
159  text_color = drawing::color::average(button_back_color, text_color, .33);
160  slider_color = drawing::color::average(button_back_color, slider_color, .33);
161  }
162 
163  if (rounded_) {
164  float height = static_cast<float>(e.clip_rectangle().height() - e.clip_rectangle().height() % 2);
165  float offset = (e.clip_rectangle().width() - height) / 2.0f - 0.5f;
166  drawing::point_f slider_location(checked_ ? drawing::point_f(e.clip_rectangle().width() - height - offset / 2.0f + 2.0f, 2.0f) : drawing::point_f(offset / 2.0f + 2.0f, 2.0f));
167 
168  drawing::size_f string_size = e.graphics().measure_string(text, font());
169  drawing::point_f string_location(checked_ ? drawing::point_f(slider_location.x() - string_size.width() - 2, (height- string_size.height()) / 2) : drawing::point_f(slider_location.x() + static_cast<float>(height - 4) + 2, (height - string_size.height()) / 2));
170 
171  e.graphics().fill_pie(drawing::solid_brush(button_back_color), offset / 2.0f, .0f, static_cast<float>(height), static_cast<float>(height), 90.0f, 180.0f);
172  e.graphics().fill_pie(drawing::solid_brush(button_back_color), e.clip_rectangle().width() - height - offset / 2.0f - 0.5f, .0f, static_cast<float>(height), static_cast<float>(height), 270.0f, 180.0f);
173  e.graphics().fill_rectangle(drawing::solid_brush(button_back_color), height / 2.0f + offset / 2.0f, .0f, static_cast<float>(e.clip_rectangle().width() - height) - offset, static_cast<float>(height));
174  e.graphics().fill_ellipse(drawing::solid_brush(slider_color), slider_location.x(), slider_location.y(), static_cast<float>(height - 4), static_cast<float>(height - 4));
175  if (show_text_) e.graphics().draw_string(text, font(), drawing::solid_brush(text_color), string_location);
176  } else {
177  drawing::size_f slider_size(static_cast<float>((e.clip_rectangle().width() - 6) / 2), static_cast<float>(e.clip_rectangle().height() - 6));
178  drawing::point_f slider_location(checked_ ? drawing::point_f(width() / 2.0f, 3.0f) : drawing::point_f(3.0f, 3.0f));
179 
180  drawing::size_f string_size = e.graphics().measure_string(text, font());
181  drawing::point_f string_location(checked_ ? drawing::point_f((width() / 2 - string_size.width()) / 2, (height() - string_size.height()) / 2) : drawing::point_f(width() / 2 + (width() / 2 - string_size.width()) / 2, (height() - string_size.height()) / 2));
182 
183  e.graphics().clear(button_back_color);
184  if (show_text_) e.graphics().draw_string(text, font(), drawing::solid_brush(text_color), string_location);
185  e.graphics().draw_line(drawing::pen(control_paint::dark(button_back_color), 2), e.clip_rectangle().left(), e.clip_rectangle().top(), e.clip_rectangle().right(), e.clip_rectangle().top());
186  e.graphics().draw_line(drawing::pen(control_paint::dark(button_back_color), 2), e.clip_rectangle().left(), e.clip_rectangle().top(), e.clip_rectangle().left(), e.clip_rectangle().bottom());
187  e.graphics().draw_line(drawing::pen(control_paint::light(button_back_color), 2), e.clip_rectangle().left(), e.clip_rectangle().bottom(), e.clip_rectangle().right(), e.clip_rectangle().bottom());
188  e.graphics().draw_line(drawing::pen(control_paint::light(button_back_color), 2), e.clip_rectangle().right(), e.clip_rectangle().top() + 2, e.clip_rectangle().right(), e.clip_rectangle().bottom());
189 
190  e.graphics().fill_rectangle(drawing::solid_brush(slider_color), {slider_location, slider_size});
191  e.graphics().draw_line(drawing::pen(control_paint::light(slider_color)), slider_location.x(), slider_location.y(), slider_location.x() + slider_size.width(), slider_location.y());
192  e.graphics().draw_line(drawing::pen(control_paint::light(slider_color)), slider_location.x(), slider_location.y(), slider_location.x(), slider_location.y() + slider_size.height() - 2);
193  e.graphics().draw_line(drawing::pen(control_paint::dark(slider_color)), slider_location.x(), slider_location.y() + slider_size.height() - 1, slider_location.x() + slider_size.width(), slider_location.y() + slider_size.height() - 1);
194  e.graphics().draw_line(drawing::pen(control_paint::dark(slider_color)), slider_location.x() + slider_size.width(), slider_location.y(), slider_location.x() + slider_size.width(), slider_location.y() + slider_size.height() - 1);
195  }
197  }
198 
199  private:
200  std::optional<xtd::drawing::color> checked_back_color_;
201  std::optional<xtd::drawing::color> slider_color_;
202  bool rounded_ = true;
203  bool auto_check_ = true;
204  bool show_text_ = false;
205  bool checked_ = false;
206  };
207  }
208 }
virtual void on_checked_changed(const event_args &e)
Raises the switch_button::checked_changed event.
Definition: switch_button.h:134
void on_paint(paint_event_args &e) override
Raises the control::paint event.
Definition: switch_button.h:151
virtual bool enabled() const
Gets a value indicating whether the control can respond to user interaction.
virtual drawing::color back_color() const
Gets the background color for the control.
Contains xtd::forms::button_base class.
virtual void on_handle_created(const event_args &e)
Raises the control::handle_created event.
static color average(const color &color1, const color &color2, double weight, bool average_alpha)
Returns the weighted average color between the two given colors.
Definition: color.h:592
void on_resize(const xtd::event_args &e) override
Raises the control::resize event.
Definition: button_base.h:191
Represents the base class for classes that contain event data, and provides a value to use for events...
Definition: event_args.h:18
static const event_args empty
Provides a value to use with events that do not have event data.
Definition: event_args.h:31
Represents a standard Windows switch button.
Definition: switch_button.h:19
virtual void on_paint(paint_event_args &e)
Raises the control::paint event.
Implements the basic functionality common to button controls.
Definition: button_base.h:26
Contains xtd::forms::control_paint class.
The xtd namespace contains all fundamental classes to access Hardware, Os, System, and more.
Definition: system_report.h:17
virtual int32_t height() const
Gets the height of the control.
The system-defined color of the accent color (macos specific. On other platform is same as menu_highl...
virtual bool checked() const
Gets a value indicating whether the switch_button is in the checked state.
Definition: switch_button.h:98
const drawing::rectangle & clip_rectangle() const
Gets the rectangle in which to paint.
Definition: paint_event_args.h:43
Represents text as a sequence of UTF-8 code units.
Definition: ustring.h:48
switch_button()=default
Initialize new instance of switch_button class.
drawing::graphics & graphics()
Gets the graphics used to paint.
Represents an ordered pair of floating-point x- and y-coordinates that defines a point in a two-dimen...
Definition: point_f.h:26
Stores an ordered pair of floating-point, which specify a height and width.
Definition: size_f.h:24
event< switch_button, event_handler > checked_changed
Occurs when the value of the checked property changes.
Definition: switch_button.h:127
Represents an ARGB (alpha, red, green, blue) color.
Definition: color.h:39
Defines a xtd::drawing::brush of a single color. Brushes are used to fill graphics shapes...
Definition: solid_brush.h:22
virtual switch_button & checked_back_color(const xtd::drawing::color &color)
Sets the checked background color for the control.
Definition: switch_button.h:43
Represents an event.
Definition: event.h:21
Provides data for the paint event.
Definition: paint_event_args.h:27
Defines an object used to draw lines and curves. This class cannot be inherited.
Definition: pen.h:29
void on_mouse_down(const mouse_event_args &e) override
Raises the control::mouse_down event.
Definition: switch_button.h:136
virtual void invalidate() const
Invalidates the entire surface of the control and causes the control to be redrawn.
virtual int32_t width() const
Gets the width of the control.
virtual switch_button & checked(bool checked)
Sets a value indicating whether the switch_button is in the checked state.
Definition: switch_button.h:102
virtual drawing::color fore_color() const
Gets the foreground color of the control.
void clear(const color &color)
Clears the entire drawing surface and fills it with the specified background color.
virtual xtd::drawing::color slider_color() const
Gets the slider color for the control.
Definition: switch_button.h:62
static xtd::drawing::color light(const xtd::drawing::color &base_color)
Creates a new light color object for the control from the specified color.
Definition: control_paint.h:128
virtual switch_button & slider_color(const xtd::drawing::color &color)
Sets the slider color for the control.
Definition: switch_button.h:65
void on_resize(const event_args &e) override
Raises the control::resize event.
Definition: switch_button.h:146
virtual switch_button & auto_check(bool auto_check)
Sets a value indicating whether the checked and the switch_button&#39;s appearance are automatically chan...
Definition: switch_button.h:32
static xtd::drawing::color dark(const xtd::drawing::color &base_color)
Creates a new dark color object for the control from the specified color.
Definition: control_paint.h:61
float width() const
Gets the horizontal component of this Size class.
Definition: size_f.h:65
void on_handle_created(const event_args &e) override
Raises the control::handle_created event.
Definition: switch_button.h:141
virtual drawing::font font() const
Gets the font of the text displayed by the control.
Provides data for the xtd::forms::control::mouse_up, xtd::forms::control::mouse_down, and xtd::forms::control::mouse_move events.
Definition: mouse_event_args.h:29
virtual switch_button & rounded(bool value)
Sets a value indicating whether the switch_button is rounded appearance.
Definition: switch_button.h:88
The xtd::forms namespace contains classes for creating Windows-based applications that take full adva...
Definition: about_box.h:13
virtual bool show_text() const
Gets a value indicating whether the switch_button text is shown.
Definition: switch_button.h:113
float height() const
Gets he vertical component of this Size Class.
Definition: size_f.h:55
virtual const xtd::ustring & text() const
Gets the text associated with this control.
virtual switch_button & show_text(bool value)
Sets a value indicating whether the switch_button text is shown.
Definition: switch_button.h:117
virtual bool auto_check() const
Gets a value indicating whether the checked and the switch_button&#39;s appearance are automatically chan...
Definition: switch_button.h:27
virtual xtd::drawing::color checked_back_color() const
Gets the checked background color for the control.
Definition: switch_button.h:40
virtual bool rounded() const
Gets a value indicating whether the switch_button is rounded appearance.
Definition: switch_button.h:84
void on_mouse_down(const mouse_event_args &e) override
Raises the control::mouse_down event.
Definition: button_base.h:161