mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 18:18:12 +00:00
LibGUI: Make it possible for GButton to be checkable.
This commit is contained in:
parent
5ec121e194
commit
19eb814850
4 changed files with 28 additions and 11 deletions
|
@ -21,12 +21,20 @@ void GButton::set_caption(const String& caption)
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void GButton::set_checked(bool checked)
|
||||||
|
{
|
||||||
|
if (m_checked == checked)
|
||||||
|
return;
|
||||||
|
m_checked = checked;
|
||||||
|
update();
|
||||||
|
}
|
||||||
|
|
||||||
void GButton::paint_event(GPaintEvent& event)
|
void GButton::paint_event(GPaintEvent& event)
|
||||||
{
|
{
|
||||||
GPainter painter(*this);
|
GPainter painter(*this);
|
||||||
painter.add_clip_rect(event.rect());
|
painter.add_clip_rect(event.rect());
|
||||||
|
|
||||||
StylePainter::paint_button(painter, rect(), m_button_style, m_being_pressed, m_hovered);
|
StylePainter::paint_button(painter, rect(), m_button_style, m_being_pressed, m_hovered, m_checkable && m_checked);
|
||||||
|
|
||||||
if (!caption().is_empty() || m_icon) {
|
if (!caption().is_empty() || m_icon) {
|
||||||
auto content_rect = rect();
|
auto content_rect = rect();
|
||||||
|
@ -35,12 +43,10 @@ void GButton::paint_event(GPaintEvent& event)
|
||||||
content_rect.move_by(1, 1);
|
content_rect.move_by(1, 1);
|
||||||
icon_location.move_by(1, 1);
|
icon_location.move_by(1, 1);
|
||||||
}
|
}
|
||||||
if (m_icon) {
|
if (m_icon)
|
||||||
painter.blit(icon_location, *m_icon, m_icon->rect());
|
painter.blit(icon_location, *m_icon, m_icon->rect());
|
||||||
painter.draw_text(content_rect, caption(), TextAlignment::Center, Color::Black);
|
auto& font = (m_checkable && m_checked) ? Font::default_bold_font() : this->font();
|
||||||
} else {
|
painter.draw_text(content_rect, caption(), font, TextAlignment::Center, Color::Black);
|
||||||
painter.draw_text(content_rect, caption(), TextAlignment::Center, Color::Black);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,6 +18,12 @@ public:
|
||||||
const GraphicsBitmap* icon() const { return m_icon.ptr(); }
|
const GraphicsBitmap* icon() const { return m_icon.ptr(); }
|
||||||
GraphicsBitmap* icon() { return m_icon.ptr(); }
|
GraphicsBitmap* icon() { return m_icon.ptr(); }
|
||||||
|
|
||||||
|
bool is_checkable() const { return m_checkable; }
|
||||||
|
void set_checkable(bool checkable) { m_checkable = checkable; }
|
||||||
|
|
||||||
|
bool is_checked() const { return m_checked; }
|
||||||
|
void set_checked(bool);
|
||||||
|
|
||||||
Function<void(GButton&)> on_click;
|
Function<void(GButton&)> on_click;
|
||||||
|
|
||||||
void set_button_style(ButtonStyle style) { m_button_style = style; }
|
void set_button_style(ButtonStyle style) { m_button_style = style; }
|
||||||
|
@ -40,5 +46,7 @@ private:
|
||||||
ButtonStyle m_button_style { ButtonStyle::Normal };
|
ButtonStyle m_button_style { ButtonStyle::Normal };
|
||||||
bool m_being_pressed { false };
|
bool m_being_pressed { false };
|
||||||
bool m_hovered { false };
|
bool m_hovered { false };
|
||||||
|
bool m_checkable { false };
|
||||||
|
bool m_checked { false };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -1,17 +1,20 @@
|
||||||
#include <SharedGraphics/StylePainter.h>
|
#include <SharedGraphics/StylePainter.h>
|
||||||
#include <LibGUI/GPainter.h>
|
#include <LibGUI/GPainter.h>
|
||||||
|
|
||||||
static void paint_button_new(Painter& painter, const Rect& rect, bool pressed)
|
static void paint_button_new(Painter& painter, const Rect& rect, bool pressed, bool checked)
|
||||||
{
|
{
|
||||||
Color button_color = Color::from_rgb(0xc0c0c0);
|
Color button_color = Color::from_rgb(0xc0c0c0);
|
||||||
Color highlight_color2 = Color::from_rgb(0xdfdfdf);
|
Color highlight_color2 = Color::from_rgb(0xdfdfdf);
|
||||||
Color shadow_color1 = Color::from_rgb(0x808080);
|
Color shadow_color1 = Color::from_rgb(0x808080);
|
||||||
Color shadow_color2 = Color::from_rgb(0x404040);
|
Color shadow_color2 = Color::from_rgb(0x404040);
|
||||||
|
|
||||||
|
if (checked)
|
||||||
|
button_color = Color::from_rgb(0xd6d2ce);
|
||||||
|
|
||||||
PainterStateSaver saver(painter);
|
PainterStateSaver saver(painter);
|
||||||
painter.translate(rect.location());
|
painter.translate(rect.location());
|
||||||
|
|
||||||
if (pressed) {
|
if (pressed || checked) {
|
||||||
// Base
|
// Base
|
||||||
painter.fill_rect({ 1, 1, rect.width() - 2, rect.height() - 2 }, button_color);
|
painter.fill_rect({ 1, 1, rect.width() - 2, rect.height() - 2 }, button_color);
|
||||||
|
|
||||||
|
@ -45,10 +48,10 @@ static void paint_button_new(Painter& painter, const Rect& rect, bool pressed)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void StylePainter::paint_button(Painter& painter, const Rect& rect, ButtonStyle button_style, bool pressed, bool hovered)
|
void StylePainter::paint_button(Painter& painter, const Rect& rect, ButtonStyle button_style, bool pressed, bool hovered, bool checked)
|
||||||
{
|
{
|
||||||
if (button_style == ButtonStyle::Normal)
|
if (button_style == ButtonStyle::Normal)
|
||||||
return paint_button_new(painter, rect, pressed);
|
return paint_button_new(painter, rect, pressed, checked);
|
||||||
|
|
||||||
Color button_color = Color::LightGray;
|
Color button_color = Color::LightGray;
|
||||||
Color highlight_color = Color::White;
|
Color highlight_color = Color::White;
|
||||||
|
|
|
@ -7,6 +7,6 @@ enum class ButtonStyle { Normal, CoolBar, OldNormal };
|
||||||
|
|
||||||
class StylePainter {
|
class StylePainter {
|
||||||
public:
|
public:
|
||||||
static void paint_button(Painter&, const Rect&, ButtonStyle, bool pressed, bool hovered = false);
|
static void paint_button(Painter&, const Rect&, ButtonStyle, bool pressed, bool hovered = false, bool checked = false);
|
||||||
static void paint_surface(Painter&, const Rect&, bool paint_vertical_lines = true);
|
static void paint_surface(Painter&, const Rect&, bool paint_vertical_lines = true);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue