xtd - Reference Guide  0.1.0
Modern c++17/20 framework to create console, GUI and unit test applications on Windows, macOS, Linux, iOS and android.
switch_button.h
1 #include "button_base.h"
2 #include "control_paint.h"
3 
4 namespace xtd {
5  namespace forms {
6  class switch_button : public button_base {
7  public:
8  switch_button() = default;
9 
10  virtual bool auto_check() const {return auto_check_;}
11  virtual switch_button& auto_check(bool auto_check) {
12  if (auto_check_ != auto_check)
13  auto_check_ = auto_check;
14  return *this;
15  }
16 
17  virtual bool rounded() const {return rounded_;}
18  virtual switch_button& rounded(bool value) {
19  if (rounded_ != value) {
20  rounded_ = value;
21  invalidate();
22  }
23  return *this;
24  }
25 
26  virtual bool checked() const {return checked_;}
27  virtual switch_button& checked(bool checked) {
28  if (checked_ != checked) {
29  checked_ = checked;
30  invalidate();
31  on_checked_changed(event_args::empty);
32  }
33  return *this;
34  }
35 
37 
38  protected:
39  virtual void on_checked_changed(const event_args& e) {checked_changed(*this, e);}
40 
41  void on_mouse_down(const mouse_event_args& e) override {
42  button_base::on_mouse_down(e);
43  if (auto_check_) checked(!checked());
44  }
45 
46  void on_handle_created(const event_args& e) override {
47  button_base::on_handle_created(e);
48  invalidate();
49  }
50 
51  void on_resize(const event_args& e) override {
52  button_base::on_resize(e);
53  invalidate();
54  }
55 
56  void on_paint(paint_event_args& e) override {
57  button_base::on_paint(e);
58 
59  drawing::color button_back_color = checked_ ? xtd::forms::theme_colors::current_theme().accent() : xtd::forms::theme_colors::current_theme().gray_text();
60  drawing::color text_color = checked_ ? fore_color() : drawing::color::average(button_back_color, fore_color(), .33);
61  drawing::color slider_color = back_color();
62 
63  if (!enabled()) {
64  button_back_color = drawing::color::average(back_color(), button_back_color, .33);
65  text_color = drawing::color::average(button_back_color, text_color, .33);
66  slider_color = drawing::color::average(button_back_color, slider_color, .33);
67  }
68 
69  if (rounded_) {
70  float height = static_cast<float>(e.clip_rectangle().height() - e.clip_rectangle().height() % 2);
71  float offset = (e.clip_rectangle().width() - height) / 2.0f - 0.5f;
72  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));
73 
74  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);
75  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);
76  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));
77  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));
78  } else {
79  drawing::size_f slider_size(static_cast<float>((e.clip_rectangle().width() - 6) / 2), static_cast<float>(e.clip_rectangle().height() - 6));
80  drawing::point_f slider_location(checked_ ? drawing::point_f(width() / 2.0f, 3.0f) : drawing::point_f(3.0f, 3.0f));
81 
82  std::string text = checked_ ? "ON" : "OFF";
83  drawing::size_f string_size = e.graphics().measure_string(text, font());
84  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));
85 
86  e.graphics().clear(button_back_color);
87  e.graphics().draw_string(text, font(), drawing::solid_brush(text_color), string_location);
88  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());
89  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());
90  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());
91  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());
92 
93  e.graphics().fill_rectangle(drawing::solid_brush(slider_color), {slider_location, slider_size});
94  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());
95  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);
96  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);
97  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);
98  }
99  }
100 
101  private:
102  bool rounded_ = true;
103  bool auto_check_ = true;
104  bool checked_ = false;
105  };
106  }
107 }
virtual int32_t width() const
Gets the width of the control.
Definition: control.h:563
Represents the base class for classes that contain event data, and provides a value to use for events...
Definition: event_args.h:9
Definition: switch_button.h:6
Implements the basic functionality common to button controls.
Definition: button_base.h:17
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:68
The xtd namespace contains all fundamental classes to access Hardware, Os, System, and more.
Definition: system_report.h:15
virtual drawing::font font() const
Gets the font of the text displayed by the control.
Definition: point_f.h:16
Stores an ordered pair of integers, which specify a height and width.
Definition: size_f.h:15
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:51
virtual int32_t height() const
Gets the height of the control.
Definition: control.h:415
Represents an ARGB (alpha, red, green, blue) color.
Definition: color.h:29
Defines an object used to draw lines and curves. This class cannot be inherited.
Definition: solid_brush.h:11
Definition: event.h:6
Definition: paint_event_args.h:16
virtual drawing::color fore_color() const
Gets the foreground color of the control.
static const event_args empty
Provides a value to use with events that do not have event data.
Definition: event_args.h:23
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:582
virtual const std::string & text() const
Gets the text associated with this control.
Definition: control.h:534
float height() const
Gets he vertical component of this Size Class.
Definition: size_f.h:46
float width() const
Gets the horizontal component of this Size class.
Definition: size_f.h:56
Definition: mouse_event_args.h:20
virtual drawing::color back_color() const
Gets the background color for the control.
virtual bool enabled() const
Gets a value indicating whether the control can respond to user interaction.
Definition: control.h:380