2020-01-18 05:38:21 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2020-02-06 08:04:00 -03:00
|
|
|
#include <LibGfx/CharacterBitmap.h>
|
|
|
|
|
#include <LibGfx/Painter.h>
|
|
|
|
|
#include <LibGfx/StylePainter.h>
|
2020-02-06 16:03:37 -03:00
|
|
|
#include <WindowServer/Button.h>
|
|
|
|
|
#include <WindowServer/Event.h>
|
2021-06-18 22:21:30 -03:00
|
|
|
#include <WindowServer/Screen.h>
|
2020-02-06 16:03:37 -03:00
|
|
|
#include <WindowServer/WindowManager.h>
|
2019-04-05 13:40:36 -03:00
|
|
|
|
2020-02-06 16:03:37 -03:00
|
|
|
namespace WindowServer {
|
|
|
|
|
|
2020-07-29 17:22:05 -03:00
|
|
|
Button::Button(WindowFrame& frame, Function<void(Button&)>&& on_click_handler)
|
2019-04-05 13:40:36 -03:00
|
|
|
: on_click(move(on_click_handler))
|
2019-04-05 16:53:45 -03:00
|
|
|
, m_frame(frame)
|
2019-04-05 13:40:36 -03:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-06 16:03:37 -03:00
|
|
|
Button::~Button()
|
2019-04-05 13:40:36 -03:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-18 22:21:30 -03:00
|
|
|
void Button::paint(Screen& screen, Gfx::Painter& painter)
|
2019-04-05 13:40:36 -03:00
|
|
|
{
|
2020-02-06 16:03:37 -03:00
|
|
|
auto palette = WindowManager::the().palette();
|
2020-02-06 07:56:38 -03:00
|
|
|
Gfx::PainterStateSaver saver(painter);
|
2019-04-05 16:33:34 -03:00
|
|
|
painter.translate(relative_rect().location());
|
2020-02-06 07:56:38 -03:00
|
|
|
Gfx::StylePainter::paint_button(painter, rect(), palette, Gfx::ButtonStyle::Normal, m_pressed, m_hovered);
|
2020-07-29 17:22:05 -03:00
|
|
|
|
|
|
|
|
if (m_icon) {
|
2021-06-18 22:21:30 -03:00
|
|
|
auto& bitmap = m_icon->bitmap(screen.scale_factor());
|
|
|
|
|
auto icon_location = rect().center().translated(-(bitmap.width() / 2), -(bitmap.height() / 2));
|
2020-07-29 17:22:05 -03:00
|
|
|
if (m_pressed)
|
|
|
|
|
painter.translate(1, 1);
|
2021-06-18 22:21:30 -03:00
|
|
|
painter.blit(icon_location, bitmap, bitmap.rect());
|
2020-07-29 17:22:05 -03:00
|
|
|
}
|
2019-04-05 13:40:36 -03:00
|
|
|
}
|
|
|
|
|
|
2020-02-06 16:03:37 -03:00
|
|
|
void Button::on_mouse_event(const MouseEvent& event)
|
2019-04-05 13:40:36 -03:00
|
|
|
{
|
2021-05-03 19:02:29 -03:00
|
|
|
auto interesting_button = false;
|
|
|
|
|
|
|
|
|
|
switch (event.button()) {
|
|
|
|
|
case MouseButton::Left:
|
|
|
|
|
interesting_button = !!on_click;
|
|
|
|
|
break;
|
|
|
|
|
case MouseButton::Middle:
|
|
|
|
|
interesting_button = !!on_middle_click;
|
|
|
|
|
break;
|
|
|
|
|
case MouseButton::Right:
|
|
|
|
|
interesting_button = !!on_right_click;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-09 05:34:40 -03:00
|
|
|
if (event.type() != Event::Type::MouseMove && !interesting_button)
|
2021-05-03 19:02:29 -03:00
|
|
|
return;
|
|
|
|
|
|
2020-02-06 16:03:37 -03:00
|
|
|
auto& wm = WindowManager::the();
|
2019-04-05 16:53:45 -03:00
|
|
|
|
2021-01-26 17:50:02 -03:00
|
|
|
if (event.type() == Event::MouseDown && (event.button() == MouseButton::Left || event.button() == MouseButton::Right || event.button() == MouseButton::Middle)) {
|
2019-04-05 16:53:45 -03:00
|
|
|
m_pressed = true;
|
|
|
|
|
wm.set_cursor_tracking_button(this);
|
2020-08-18 01:45:10 -03:00
|
|
|
m_frame.invalidate(m_relative_rect);
|
2019-04-05 16:53:45 -03:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-26 17:50:02 -03:00
|
|
|
if (event.type() == Event::MouseUp && (event.button() == MouseButton::Left || event.button() == MouseButton::Right || event.button() == MouseButton::Middle)) {
|
2019-04-22 20:17:20 -03:00
|
|
|
if (wm.cursor_tracking_button() != this)
|
|
|
|
|
return;
|
|
|
|
|
wm.set_cursor_tracking_button(nullptr);
|
2019-04-05 16:53:45 -03:00
|
|
|
bool old_pressed = m_pressed;
|
|
|
|
|
m_pressed = false;
|
|
|
|
|
if (rect().contains(event.position())) {
|
2021-01-26 17:50:02 -03:00
|
|
|
switch (event.button()) {
|
|
|
|
|
case MouseButton::Left:
|
|
|
|
|
if (on_click)
|
|
|
|
|
on_click(*this);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case MouseButton::Right:
|
|
|
|
|
if (on_right_click)
|
|
|
|
|
on_right_click(*this);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
if (on_middle_click)
|
|
|
|
|
on_middle_click(*this);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-04-05 16:53:45 -03:00
|
|
|
}
|
2020-05-04 00:00:34 -03:00
|
|
|
if (old_pressed != m_pressed) {
|
|
|
|
|
// Would like to compute:
|
|
|
|
|
// m_hovered = rect_after_action().contains(event.position());
|
|
|
|
|
// However, we don't know that rect yet. We can make an educated
|
|
|
|
|
// guess which also looks okay even when wrong:
|
|
|
|
|
m_hovered = false;
|
2020-08-18 01:45:10 -03:00
|
|
|
m_frame.invalidate(m_relative_rect);
|
2020-05-04 00:00:34 -03:00
|
|
|
}
|
2019-04-05 23:08:09 -03:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-06 16:03:37 -03:00
|
|
|
if (event.type() == Event::MouseMove) {
|
2019-04-05 23:08:09 -03:00
|
|
|
bool old_hovered = m_hovered;
|
|
|
|
|
m_hovered = rect().contains(event.position());
|
|
|
|
|
wm.set_hovered_button(m_hovered ? this : nullptr);
|
|
|
|
|
if (old_hovered != m_hovered)
|
2020-08-18 01:45:10 -03:00
|
|
|
m_frame.invalidate(m_relative_rect);
|
2019-04-05 23:08:09 -03:00
|
|
|
}
|
|
|
|
|
|
2020-02-06 16:03:37 -03:00
|
|
|
if (event.type() == Event::MouseMove && event.buttons() & (unsigned)MouseButton::Left) {
|
2019-04-22 20:17:20 -03:00
|
|
|
if (wm.cursor_tracking_button() != this)
|
|
|
|
|
return;
|
2019-04-05 23:08:09 -03:00
|
|
|
bool old_pressed = m_pressed;
|
|
|
|
|
m_pressed = m_hovered;
|
|
|
|
|
if (old_pressed != m_pressed)
|
2020-08-18 01:45:10 -03:00
|
|
|
m_frame.invalidate(m_relative_rect);
|
2019-04-05 13:40:36 -03:00
|
|
|
}
|
|
|
|
|
}
|
2019-04-05 16:53:45 -03:00
|
|
|
|
2020-06-10 05:57:59 -03:00
|
|
|
Gfx::IntRect Button::screen_rect() const
|
2019-04-05 16:53:45 -03:00
|
|
|
{
|
|
|
|
|
return m_relative_rect.translated(m_frame.rect().location());
|
|
|
|
|
}
|
2020-02-06 16:03:37 -03:00
|
|
|
|
|
|
|
|
}
|