2019-01-20 04:49:48 +01:00
|
|
|
#include "GButton.h"
|
2019-03-28 17:19:56 +01:00
|
|
|
#include <LibGUI/GPainter.h>
|
2019-03-28 17:32:38 +01:00
|
|
|
#include <SharedGraphics/StylePainter.h>
|
2019-04-04 15:20:02 +02:00
|
|
|
#include <AK/StringBuilder.h>
|
2018-10-11 01:48:09 +02:00
|
|
|
|
2019-01-27 20:26:45 +01:00
|
|
|
//#define GBUTTON_DEBUG
|
|
|
|
|
2019-01-20 04:49:48 +01:00
|
|
|
GButton::GButton(GWidget* parent)
|
|
|
|
: GWidget(parent)
|
2018-10-11 01:48:09 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-01-20 04:49:48 +01:00
|
|
|
GButton::~GButton()
|
2018-10-11 01:48:09 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-03-19 00:01:02 +01:00
|
|
|
void GButton::set_caption(const String& caption)
|
2018-10-11 01:48:09 +02:00
|
|
|
{
|
|
|
|
if (caption == m_caption)
|
|
|
|
return;
|
2019-03-19 00:01:02 +01:00
|
|
|
m_caption = caption;
|
2018-10-11 01:48:09 +02:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2019-04-04 13:18:27 +02:00
|
|
|
void GButton::set_checked(bool checked)
|
|
|
|
{
|
|
|
|
if (m_checked == checked)
|
|
|
|
return;
|
|
|
|
m_checked = checked;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2019-02-28 19:34:55 +01:00
|
|
|
void GButton::paint_event(GPaintEvent& event)
|
2018-10-11 01:48:09 +02:00
|
|
|
{
|
2019-03-28 17:19:56 +01:00
|
|
|
GPainter painter(*this);
|
2019-03-29 15:01:54 +01:00
|
|
|
painter.add_clip_rect(event.rect());
|
2018-10-12 14:58:16 +02:00
|
|
|
|
2019-04-04 13:18:27 +02:00
|
|
|
StylePainter::paint_button(painter, rect(), m_button_style, m_being_pressed, m_hovered, m_checkable && m_checked);
|
2018-10-12 14:58:16 +02:00
|
|
|
|
2019-04-04 15:20:02 +02:00
|
|
|
if (m_caption.is_empty() && !m_icon)
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto content_rect = rect().shrunken(10, 2);
|
|
|
|
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);
|
2018-10-11 01:48:09 +02:00
|
|
|
}
|
2019-04-04 15:20:02 +02:00
|
|
|
if (m_icon)
|
|
|
|
painter.blit(icon_location, *m_icon, m_icon->rect());
|
|
|
|
auto& font = (m_checkable && m_checked) ? Font::default_bold_font() : this->font();
|
2019-04-06 00:57:16 +02:00
|
|
|
painter.draw_text(content_rect, m_caption, font, text_alignment(), foreground_color(), TextElision::Right);
|
2018-10-11 01:48:09 +02:00
|
|
|
}
|
|
|
|
|
2019-01-27 08:48:34 +01:00
|
|
|
void GButton::mousemove_event(GMouseEvent& event)
|
|
|
|
{
|
2019-03-24 15:01:56 +01:00
|
|
|
if (event.buttons() == GMouseButton::Left) {
|
2019-01-27 08:48:34 +01:00
|
|
|
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
|
|
|
{
|
2019-01-27 20:26:45 +01: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
|
|
|
}
|
|
|
|
|
2019-03-19 01:41:00 +01: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
|
|
|
{
|
2019-01-27 20:26:45 +01: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();
|
2019-03-19 01:41:00 +01:00
|
|
|
if (was_being_pressed)
|
|
|
|
click();
|
2019-01-27 08:48:34 +01:00
|
|
|
}
|
2019-01-27 20:26:45 +01:00
|
|
|
GWidget::mouseup_event(event);
|
2018-10-12 15:52:41 +02:00
|
|
|
}
|
|
|
|
|
2019-04-10 16:56:55 +02:00
|
|
|
void GButton::enter_event(CEvent&)
|
2019-02-20 10:12:19 +01:00
|
|
|
{
|
|
|
|
m_hovered = true;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2019-04-10 16:56:55 +02:00
|
|
|
void GButton::leave_event(CEvent&)
|
2019-02-20 10:12:19 +01:00
|
|
|
{
|
|
|
|
m_hovered = false;
|
|
|
|
update();
|
|
|
|
}
|