OrcaSlicer latest
OrcaSlicer
Loading...
Searching...
No Matches
SwitchButton Class Reference

#include </home/runner/work/OrcaSlicer/OrcaSlicer/src/slic3r/GUI/Widgets/SwitchButton.hpp>

Inheritance diagram for SwitchButton:
Collaboration diagram for SwitchButton:

Public Member Functions

 SwitchButton (wxWindow *parent=NULL, wxWindowID id=wxID_ANY)
void SetLabels (wxString const &lbl_on, wxString const &lbl_off)
void SetTextColor (StateColor const &color)
void SetTextColor2 (StateColor const &color)
void SetTrackColor (StateColor const &color)
void SetThumbColor (StateColor const &color)
void SetValue (bool value) override
void Rescale ()
bool SetBackgroundColour (const wxColour &colour) override

Private Member Functions

void update ()

Private Attributes

ScalableBitmap m_on
ScalableBitmap m_off
wxString labels [2]
StateColor text_color
StateColor text_color2
StateColor track_color
StateColor thumb_color

Detailed Description

Constructor & Destructor Documentation

◆ SwitchButton()

SwitchButton::SwitchButton ( wxWindow * parent = NULL,
wxWindowID id = wxID_ANY )
48 : wxBitmapToggleButton(parent, id, wxNullBitmap, wxDefaultPosition, wxDefaultSize, wxBORDER_NONE | wxBU_EXACTFIT)
49 , m_on(this, "toggle_on", 16)
50 , m_off(this, "toggle_off", 16)
51 , text_color(std::pair{0xfffffe, (int) StateColor::Checked}, std::pair{0x6B6B6B, (int) StateColor::Normal})
52 , track_color(0xD9D9D9)
53 , thumb_color(std::pair{0x009688, (int) StateColor::Checked}, std::pair{0xD9D9D9, (int) StateColor::Normal})
54{
56 Bind(wxEVT_TOGGLEBUTTON, [this](auto& e) { update(); e.Skip(); });
57 SetFont(Label::Body_12);
58
59#ifdef __WXGTK__
60 Slic3r::GUI::RemoveButtonBorder(this);
61#endif
62
63 Rescale();
64}
static wxFont Body_12
Definition Label.hpp:55
@ Normal
Definition StateColor.hpp:16
@ Checked
Definition StateColor.hpp:18
static wxColor GetParentBackgroundColor(wxWindow *parent)
Definition StaticBox.cpp:122
StateColor thumb_color
Definition SwitchButton.hpp:58
void update()
Definition SwitchButton.cpp:233
bool SetBackgroundColour(const wxColour &colour) override
Definition SwitchButton.cpp:105
StateColor text_color
Definition SwitchButton.hpp:55
ScalableBitmap m_off
Definition SwitchButton.hpp:52
StateColor track_color
Definition SwitchButton.hpp:57
ScalableBitmap m_on
Definition SwitchButton.hpp:51
void Rescale()
Definition SwitchButton.cpp:115

References Label::Body_12, StaticBox::GetParentBackgroundColor(), m_off, m_on, Rescale(), SetBackgroundColour(), text_color, thumb_color, track_color, and update().

Here is the call graph for this function:

Member Function Documentation

◆ Rescale()

void SwitchButton::Rescale ( )
116{
117 if (labels[0].IsEmpty()) {
118 m_on.msw_rescale();
119 m_off.msw_rescale();
120 }
121 else {
122 wxBitmapToggleButton::SetBackgroundColour(StaticBox::GetParentBackgroundColor(GetParent()));
123#ifdef __WXOSX__
125 int BS = (int) scale;
126#else
127 constexpr int BS = 1;
128#endif
129 wxSize thumbSize;
130 wxSize trackSize;
131 wxClientDC dc(this);
132#ifdef __WXOSX__
133 dc.SetFont(dc.GetFont().Scaled(scale));
134#endif
135 wxSize textSize[2];
136 {
137 textSize[0] = dc.GetTextExtent(labels[0]);
138 textSize[1] = dc.GetTextExtent(labels[1]);
139 }
140 float fontScale = 0;
141 {
142 thumbSize = textSize[0];
143 auto size = textSize[1];
144 if (size.x > thumbSize.x) thumbSize.x = size.x;
145 else size.x = thumbSize.x;
146 thumbSize.x += BS * 12;
147 thumbSize.y += BS * 6;
148 trackSize.x = thumbSize.x + size.x + BS * 10;
149 trackSize.y = thumbSize.y + BS * 2;
150 auto maxWidth = GetMaxWidth();
151#ifdef __WXOSX__
152 maxWidth *= scale;
153#endif
154 if (trackSize.x > maxWidth) {
155 fontScale = float(maxWidth) / trackSize.x;
156 thumbSize.x -= (trackSize.x - maxWidth) / 2;
157 trackSize.x = maxWidth;
158 }
159 }
160 for (int i = 0; i < 2; ++i) {
161 wxMemoryDC memdc(&dc);
162 wxImage image(trackSize);
163#ifndef __WXMSW__ // DrawText doesn't work properly on Windows with Alpha channel
164 image.InitAlpha();
165 memset(image.GetAlpha(), 0, trackSize.GetWidth() * trackSize.GetHeight());
166#endif
167 wxBitmap bmp(std::move(image));
168 memdc.SelectObject(bmp);
169#ifdef __WXMSW__
170 memdc.SetBackground(wxBrush(GetBackgroundColour()));
171 memdc.Clear();
172#endif
173 memdc.SetFont(dc.GetFont());
174#ifdef __WXMSW__
175 const double scale = GetDPIScaleFactor();
176 fontScale = scale;
177#endif
178 if (fontScale) {
179 memdc.SetFont(dc.GetFont().Scaled(fontScale));
180 textSize[0] = memdc.GetTextExtent(labels[0]);
181 textSize[1] = memdc.GetTextExtent(labels[1]);
182 }
184 {
185#ifdef __WXMSW__
186 wxGCDC dc2(memdc);
187#else
188 wxDC &dc2(memdc);
189#endif
190 dc2.SetBrush(wxBrush(track_color.colorForStates(state)));
191 dc2.SetPen(wxPen(track_color.colorForStates(state)));
192 dc2.DrawRoundedRectangle(wxRect({0, 0}, trackSize), trackSize.y / 2.0);
193 dc2.SetBrush(wxBrush(thumb_color.colorForStates(StateColor::Checked | StateColor::Enabled)));
194 dc2.SetPen(wxPen(thumb_color.colorForStates(StateColor::Checked | StateColor::Enabled)));
195 dc2.DrawRoundedRectangle(wxRect({ i == 0 ? BS : (trackSize.x - thumbSize.x - BS), BS}, thumbSize), thumbSize.y / 2.0);
196 }
197 memdc.SetTextForeground(text_color.colorForStates(state ^ StateColor::Checked));
198 auto text_y = BS + (thumbSize.y - textSize[0].y) / 2;
199#ifdef __APPLE__
201 text_y -= FromDIP(2);
202 }
203#endif
204 memdc.DrawText(labels[0], {BS + (thumbSize.x - textSize[0].x) / 2, text_y});
205 memdc.SetTextForeground(text_color2.count() == 0 ? text_color.colorForStates(state) : text_color2.colorForStates(state));
206 auto text_y_1 = BS + (thumbSize.y - textSize[1].y) / 2;
207#ifdef __APPLE__
209 text_y_1 -= FromDIP(2);
210 }
211#endif
212 memdc.DrawText(labels[1], {trackSize.x - thumbSize.x - BS + (thumbSize.x - textSize[1].x) / 2, text_y_1});
213 memdc.SelectObject(wxNullBitmap);
214#ifdef __WXOSX__
215 bmp = wxBitmap(bmp.ConvertToImage(), -1, scale);
216#elif defined(__WXMSW__)
217 bmp.SetScaleFactor(scale); // ORCA
218#endif
219 (i == 0 ? m_off : m_on).bmp() = bmp;
220 }
221 }
222 update();
223#ifdef __WXGTK__
224 wxSize bestSize = GetBestSize();
225 bestSize.IncTo(m_on.GetBmpSize());
226 SetSize(bestSize);
227 SetMinSize(bestSize);
228#else
229 SetSize(m_on.GetBmpSize());
230#endif
231}
int scale(const int val)
Definition WipeTowerDialog.cpp:58
@ Enabled
Definition StateColor.hpp:17
wxString labels[2]
Definition SwitchButton.hpp:54
StateColor text_color2
Definition SwitchButton.hpp:56
double mac_max_scaling_factor()
Definition MacDarkMode.mm:35
int is_mac_version_15()
Definition MacUtils.mm:15

References StateColor::Checked, StateColor::Enabled, StaticBox::GetParentBackgroundColor(), Slic3r::is_mac_version_15(), labels, m_off, m_on, Slic3r::GUI::mac_max_scaling_factor(), scale(), text_color, text_color2, thumb_color, track_color, and update().

Referenced by SwitchButton(), SetBackgroundColour(), SetLabels(), SetTextColor(), SetTextColor2(), SetThumbColor(), and SetTrackColor().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SetBackgroundColour()

bool SwitchButton::SetBackgroundColour ( const wxColour & colour)
override
106{
107 if (wxBitmapToggleButton::SetBackgroundColour(colour)) {
108 Rescale();
109 return true;
110 }
111
112 return false;
113}

References Rescale().

Referenced by SwitchButton().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SetLabels()

void SwitchButton::SetLabels ( wxString const & lbl_on,
wxString const & lbl_off )
67{
68 labels[0] = lbl_on;
69 labels[1] = lbl_off;
70 Rescale();
71}

References labels, and Rescale().

Here is the call graph for this function:

◆ SetTextColor()

void SwitchButton::SetTextColor ( StateColor const & color)
74{
75 text_color = color;
76 Rescale();
77}

References Rescale(), and text_color.

Here is the call graph for this function:

◆ SetTextColor2()

void SwitchButton::SetTextColor2 ( StateColor const & color)
80{
81 text_color2 = color;
82 Rescale();
83}

References Rescale(), and text_color2.

Here is the call graph for this function:

◆ SetThumbColor()

void SwitchButton::SetThumbColor ( StateColor const & color)
92{
93 thumb_color = color;
94 Rescale();
95}

References Rescale(), and thumb_color.

Here is the call graph for this function:

◆ SetTrackColor()

void SwitchButton::SetTrackColor ( StateColor const & color)
86{
87 track_color = color;
88 Rescale();
89}

References Rescale(), and track_color.

Here is the call graph for this function:

◆ SetValue()

void SwitchButton::SetValue ( bool value)
override
98{
99 if (value != GetValue()) {
100 wxBitmapToggleButton::SetValue(value);
101 update();
102 }
103}

References update().

Referenced by CustomWidgetsPlugin::addSwitchButtonProps().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ update()

void SwitchButton::update ( )
private
234{
235 SetBitmap((GetValue() ? m_on : m_off).bmp());
236}

References m_off, and m_on.

Referenced by SwitchButton(), Rescale(), and SetValue().

Here is the caller graph for this function:

Member Data Documentation

◆ labels

wxString SwitchButton::labels[2]
private

Referenced by Rescale(), and SetLabels().

◆ m_off

ScalableBitmap SwitchButton::m_off
private

Referenced by SwitchButton(), Rescale(), and update().

◆ m_on

ScalableBitmap SwitchButton::m_on
private

Referenced by SwitchButton(), Rescale(), and update().

◆ text_color

StateColor SwitchButton::text_color
private

Referenced by SwitchButton(), Rescale(), and SetTextColor().

◆ text_color2

StateColor SwitchButton::text_color2
private

Referenced by Rescale(), and SetTextColor2().

◆ thumb_color

StateColor SwitchButton::thumb_color
private

◆ track_color

StateColor SwitchButton::track_color
private

The documentation for this class was generated from the following files:
  • /home/runner/work/OrcaSlicer/OrcaSlicer/src/slic3r/GUI/Widgets/SwitchButton.hpp
  • /home/runner/work/OrcaSlicer/OrcaSlicer/src/slic3r/GUI/Widgets/SwitchButton.cpp