ladybird/LibGUI/GButton.cpp
Andreas Kling 9b71307d49 WindowServer: Support windows with alpha channels. And per-WSWindow opacity.
This patch also adds a Format concept to GraphicsBitmap. For now there are
only two formats: RGB32 and RGBA32. Windows with alpha channel have their
backing stores created in the RGBA32 format.

Use this to make Terminal windows semi-transparent for that comfy rice look.
There is one problem here, in that window compositing overdraw incurs
multiple passes of blending of the same pixels. This leads to a mismatch in
opacity which is obviously not good. I will work on this in a later patch.

The alpha blending is currently straight C++. It should be relatively easy
to optimize this using SSE instructions.

For now I'm just happy with the cute effect. :^)
2019-02-19 01:42:53 +01:00

91 lines
2.4 KiB
C++

#include "GButton.h"
#include <SharedGraphics/Painter.h>
#include <LibGUI/GStyle.h>
//#define GBUTTON_DEBUG
GButton::GButton(GWidget* parent)
: GWidget(parent)
{
set_fill_with_background_color(false);
}
GButton::~GButton()
{
}
void GButton::set_caption(String&& caption)
{
if (caption == m_caption)
return;
m_caption = move(caption);
update();
}
void GButton::paint_event(GPaintEvent&)
{
Painter painter(*this);
GStyle::the().paint_button(painter, rect(), m_being_pressed);
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 (m_tracking_cursor) {
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;
m_tracking_cursor = true;
set_global_cursor_tracking(true);
update();
}
GWidget::mousedown_event(event);
}
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;
m_tracking_cursor = false;
set_global_cursor_tracking(false);
update();
if (was_being_pressed) {
if (on_click)
on_click(*this);
}
}
GWidget::mouseup_event(event);
}