ladybird/Widgets/Button.cpp

95 lines
2.8 KiB
C++
Raw Normal View History

2018-10-11 01:48:09 +02:00
#include "Button.h"
#include "Painter.h"
#include <cstdio>
Button::Button(Widget* parent)
: Widget(parent)
{
}
Button::~Button()
{
}
void Button::setCaption(String&& caption)
{
if (caption == m_caption)
return;
m_caption = std::move(caption);
update();
}
void Button::paintEvent(PaintEvent&)
2018-10-11 01:48:09 +02:00
{
2018-10-12 23:02:23 +02:00
Color buttonColor = Color::LightGray;
Color highlightColor = Color::White;
Color shadowColor = Color(96, 96, 96);
2018-10-11 01:48:09 +02:00
Painter painter(*this);
painter.drawPixel({ 0, 0 }, backgroundColor());
painter.drawPixel({ width() - 1, 0 }, backgroundColor());
painter.drawPixel({ 0, height() - 1 }, backgroundColor());
painter.drawPixel({ width() - 1, height() - 1 }, backgroundColor());
2018-10-12 23:02:23 +02:00
painter.drawLine({ 1, 0 }, { width() - 2, 0 }, Color::Black);
painter.drawLine({ 1, height() - 1 }, { width() - 2, height() - 1}, Color::Black);
painter.drawLine({ 0, 1 }, { 0, height() - 2 }, Color::Black);
painter.drawLine({ width() - 1, 1 }, { width() - 1, height() - 2 }, Color::Black);
2018-10-12 15:52:41 +02:00
if (m_beingPressed) {
// Base
painter.fillRect({ 1, 1, width() - 2, height() - 2 }, buttonColor);
2018-10-12 15:52:41 +02:00
// Sunken shadow
painter.drawLine({ 1, 1 }, { width() - 2, 1 }, shadowColor);
painter.drawLine({ 1, 2 }, {1, height() - 2 }, shadowColor);
} else {
// Base
painter.fillRect({ 3, 3, width() - 6, height() - 6 }, buttonColor);
2018-10-12 15:52:41 +02:00
// White highlight
painter.drawLine({ 1, 1 }, { width() - 2, 1 }, highlightColor);
painter.drawLine({ 1, 2 }, { width() - 3, 2 }, highlightColor);
painter.drawLine({ 1, 3 }, { 1, height() - 2 }, highlightColor);
painter.drawLine({ 2, 3 }, { 2, height() - 3 }, highlightColor);
// Gray shadow
painter.drawLine({ width() - 2, 1 }, { width() - 2, height() - 4 }, shadowColor);
painter.drawLine({ width() - 3, 2 }, { width() - 3, height() - 4 }, shadowColor);
painter.drawLine({ 1, height() - 2 }, { width() - 2, height() - 2 }, shadowColor);
painter.drawLine({ 2, height() - 3 }, { width() - 2, height() - 3 }, shadowColor);
}
2018-10-11 01:48:09 +02:00
if (!caption().isEmpty()) {
2018-10-12 15:52:41 +02:00
auto textRect = rect();
if (m_beingPressed)
textRect.moveBy(1, 1);
2018-10-12 23:02:23 +02:00
painter.drawText(textRect, caption(), Painter::TextAlignment::Center, Color::Black);
2018-10-11 01:48:09 +02:00
}
}
void Button::mouseDownEvent(MouseEvent& event)
2018-10-11 01:48:09 +02:00
{
printf("Button::mouseDownEvent: x=%d, y=%d, button=%u\n", event.x(), event.y(), (unsigned)event.button());
2018-10-11 01:48:09 +02:00
2018-10-12 15:52:41 +02:00
m_beingPressed = true;
2018-10-11 01:48:09 +02:00
update();
Widget::mouseDownEvent(event);
2018-10-11 01:48:09 +02:00
}
void Button::mouseUpEvent(MouseEvent& event)
2018-10-12 15:52:41 +02:00
{
printf("Button::mouseUpEvent: x=%d, y=%d, button=%u\n", event.x(), event.y(), (unsigned)event.button());
2018-10-12 15:52:41 +02:00
m_beingPressed = false;
update();
Widget::mouseUpEvent(event);
if (onClick)
onClick(*this);
2018-10-12 15:52:41 +02:00
}