ladybird/LibGUI/GButton.cpp

103 lines
2.5 KiB
C++
Raw Normal View History

#include "GButton.h"
2019-01-19 23:49:56 +01:00
#include <SharedGraphics/Painter.h>
#include <LibGUI/GStyle.h>
2018-10-11 01:48:09 +02:00
//#define GBUTTON_DEBUG
GButton::GButton(GWidget* parent)
: GWidget(parent)
2018-10-11 01:48:09 +02:00
{
}
GButton::~GButton()
2018-10-11 01:48:09 +02:00
{
}
void GButton::set_caption(const String& caption)
2018-10-11 01:48:09 +02:00
{
if (caption == m_caption)
return;
m_caption = caption;
2018-10-11 01:48:09 +02:00
update();
}
void GButton::paint_event(GPaintEvent& event)
2018-10-11 01:48:09 +02:00
{
Painter painter(*this);
painter.set_clip_rect(event.rect());
GStyle::the().paint_button(painter, rect(), m_button_style, m_being_pressed, m_hovered);
if (!caption().is_empty() || m_icon) {
auto content_rect = rect();
auto icon_location = m_icon ? content_rect.center().translated(-(m_icon->width() / 2), -(m_icon->height() / 2)) : Point();
if (m_being_pressed) {
content_rect.move_by(1, 1);
icon_location.move_by(1, 1);
}
if (m_icon) {
painter.blit(icon_location, *m_icon, m_icon->rect());
2019-02-11 10:08:37 +01:00
painter.draw_text(content_rect, caption(), TextAlignment::Center, Color::Black);
} else {
2019-02-11 10:08:37 +01:00
painter.draw_text(content_rect, caption(), TextAlignment::Center, Color::Black);
}
2018-10-11 01:48:09 +02:00
}
}
void GButton::mousemove_event(GMouseEvent& event)
{
if (event.buttons() == GMouseButton::Left) {
bool being_pressed = rect().contains(event.position());
if (being_pressed != m_being_pressed) {
m_being_pressed = being_pressed;
update();
}
}
GWidget::mousemove_event(event);
}
2019-01-21 00:46:08 +01:00
void GButton::mousedown_event(GMouseEvent& event)
2018-10-11 01:48:09 +02:00
{
#ifdef GBUTTON_DEBUG
dbgprintf("GButton::mouse_down_event: x=%d, y=%d, button=%u\n", event.x(), event.y(), (unsigned)event.button());
#endif
if (event.button() == GMouseButton::Left) {
m_being_pressed = true;
update();
}
2019-01-21 00:46:08 +01:00
GWidget::mousedown_event(event);
2018-10-11 01:48:09 +02:00
}
void GButton::click()
{
if (on_click)
on_click(*this);
}
2019-01-21 00:46:08 +01:00
void GButton::mouseup_event(GMouseEvent& event)
2018-10-12 15:52:41 +02:00
{
#ifdef GBUTTON_DEBUG
dbgprintf("GButton::mouse_up_event: x=%d, y=%d, button=%u\n", event.x(), event.y(), (unsigned)event.button());
#endif
if (event.button() == GMouseButton::Left) {
bool was_being_pressed = m_being_pressed;
m_being_pressed = false;
update();
if (was_being_pressed)
click();
}
GWidget::mouseup_event(event);
2018-10-12 15:52:41 +02:00
}
void GButton::enter_event(GEvent&)
{
m_hovered = true;
update();
}
void GButton::leave_event(GEvent&)
{
m_hovered = false;
update();
}