mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-10 13:00:29 +03:00
b4da451c9a
When a mouse button is pressed inside a window, put that window into an automatic mouse tracking state where all mouse events are sent to that window until all mouse buttons are released. This might feel even better if it only cared about the mouse buttons you actually pressed while *inside* the windows to get released, I don't know. I'll have to use it for a while and see how it's like.
103 lines
2.5 KiB
C++
103 lines
2.5 KiB
C++
#include "GButton.h"
|
|
#include <SharedGraphics/Painter.h>
|
|
#include <LibGUI/GStyle.h>
|
|
|
|
//#define GBUTTON_DEBUG
|
|
|
|
GButton::GButton(GWidget* parent)
|
|
: GWidget(parent)
|
|
{
|
|
}
|
|
|
|
GButton::~GButton()
|
|
{
|
|
}
|
|
|
|
void GButton::set_caption(const String& caption)
|
|
{
|
|
if (caption == m_caption)
|
|
return;
|
|
m_caption = caption;
|
|
update();
|
|
}
|
|
|
|
void GButton::paint_event(GPaintEvent& event)
|
|
{
|
|
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());
|
|
painter.draw_text(content_rect, caption(), TextAlignment::Center, Color::Black);
|
|
} else {
|
|
painter.draw_text(content_rect, caption(), TextAlignment::Center, Color::Black);
|
|
}
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
void GButton::mousedown_event(GMouseEvent& event)
|
|
{
|
|
#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();
|
|
}
|
|
GWidget::mousedown_event(event);
|
|
}
|
|
|
|
void GButton::click()
|
|
{
|
|
if (on_click)
|
|
on_click(*this);
|
|
}
|
|
|
|
void GButton::mouseup_event(GMouseEvent& event)
|
|
{
|
|
#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);
|
|
}
|
|
|
|
void GButton::enter_event(GEvent&)
|
|
{
|
|
m_hovered = true;
|
|
update();
|
|
}
|
|
|
|
void GButton::leave_event(GEvent&)
|
|
{
|
|
m_hovered = false;
|
|
update();
|
|
}
|