LibGUI: Share code for widget rendering styles in a GStyle class.

Since GScrollBar wants its internal buttons to look like GButtons,
let's share the painting code between them.
This commit is contained in:
Andreas Kling 2019-02-10 07:11:01 +01:00
parent 9ea2131adf
commit e354c08c98
Notes: sideshowbarker 2024-07-19 15:48:39 +09:00
9 changed files with 88 additions and 40 deletions

View File

@ -1,5 +1,6 @@
#include "GButton.h"
#include <SharedGraphics/Painter.h>
#include <LibGUI/GStyle.h>
//#define GBUTTON_DEBUG
@ -23,37 +24,9 @@ void GButton::set_caption(String&& caption)
void GButton::paint_event(GPaintEvent&)
{
Color button_color = Color::LightGray;
Color highlight_color = Color::White;
Color shadow_color = Color(96, 96, 96);
Painter painter(*this);
painter.draw_rect(rect(), Color::Black, true);
if (m_being_pressed) {
// Base
painter.fill_rect(rect().shrunken(2, 2), button_color);
// Sunken shadow
painter.draw_line({ 1, 1 }, { width() - 2, 1 }, shadow_color);
painter.draw_line({ 1, 2 }, {1, height() - 2 }, shadow_color);
} else {
// Base
painter.fill_rect({ 3, 3, width() - 5, height() - 5 }, button_color);
painter.fill_rect_with_gradient({ 3, 3, width() - 5, height() - 5 }, button_color, Color::White);
// White highlight
painter.draw_line({ 1, 1 }, { width() - 2, 1 }, highlight_color);
painter.draw_line({ 1, 2 }, { width() - 3, 2 }, highlight_color);
painter.draw_line({ 1, 3 }, { 1, height() - 2 }, highlight_color);
painter.draw_line({ 2, 3 }, { 2, height() - 3 }, highlight_color);
// Gray shadow
painter.draw_line({ width() - 2, 1 }, { width() - 2, height() - 4 }, shadow_color);
painter.draw_line({ width() - 3, 2 }, { width() - 3, height() - 4 }, shadow_color);
painter.draw_line({ 1, height() - 2 }, { width() - 2, height() - 2 }, shadow_color);
painter.draw_line({ 2, height() - 3 }, { width() - 2, height() - 3 }, shadow_color);
}
GStyle::the().paint_button(painter, rect(), m_being_pressed);
if (!caption().is_empty() || m_icon) {
auto content_rect = rect();

View File

@ -1,4 +1,5 @@
#include <LibGUI/GScrollBar.h>
#include <LibGUI/GStyle.h>
#include <SharedGraphics/GraphicsBitmap.h>
#include <SharedGraphics/Painter.h>
@ -71,6 +72,11 @@ int GScrollBar::scrubbable_range_in_pixels() const
return height() - button_size() * 3;
}
bool GScrollBar::has_scrubber() const
{
return m_max != m_min;
}
Rect GScrollBar::scrubber_rect() const
{
int range_size = m_max - m_min;
@ -122,16 +128,14 @@ void GScrollBar::paint_event(GPaintEvent&)
painter.fill_rect(rect(), Color(164, 164, 164));
painter.draw_rect(up_button_rect(), Color::DarkGray, true);
painter.fill_rect_with_gradient(up_button_rect().shrunken(2, 2), Color::LightGray, Color::White);
GStyle::the().paint_button(painter, up_button_rect(), false);
painter.draw_bitmap(up_button_rect().location().translated(3, 3), *s_up_arrow_bitmap, Color::Black);
painter.draw_rect(down_button_rect(), Color::DarkGray, true);
painter.fill_rect_with_gradient(down_button_rect().shrunken(2, 2), Color::LightGray, Color::White);
GStyle::the().paint_button(painter, down_button_rect(), false);
painter.draw_bitmap(down_button_rect().location().translated(3, 3), *s_down_arrow_bitmap, Color::Black);
painter.draw_rect(scrubber_rect(), Color::White, true);
painter.fill_rect_with_gradient(scrubber_rect().shrunken(2, 2), Color::LightGray, Color::White);
if (has_scrubber())
GStyle::the().paint_button(painter, scrubber_rect(), m_scrubbing);
}
void GScrollBar::mousedown_event(GMouseEvent& event)
@ -154,11 +158,12 @@ void GScrollBar::mousedown_event(GMouseEvent& event)
set_value(value() + m_big_step);
return;
}
if (scrubber_rect().contains(event.position())) {
if (has_scrubber() && scrubber_rect().contains(event.position())) {
m_scrubbing = true;
m_scrub_start_value = value();
m_scrub_origin = event.position();
set_global_cursor_tracking(true);
update();
return;
}
}
@ -171,6 +176,7 @@ void GScrollBar::mouseup_event(GMouseEvent& event)
return;
m_scrubbing = false;
set_global_cursor_tracking(false);
update();
}
void GScrollBar::mousemove_event(GMouseEvent& event)

View File

@ -18,6 +18,7 @@ public:
void set_value(int value);
void set_step(int step) { m_step = step; }
void set_big_step(int big_step) { m_big_step = big_step; }
bool has_scrubber() const;
Function<void(int)> on_change;

52
LibGUI/GStyle.cpp Normal file
View File

@ -0,0 +1,52 @@
#include <LibGUI/GStyle.h>
#include <SharedGraphics/Painter.h>
GStyle* s_the;
GStyle& GStyle::the()
{
if (!s_the)
s_the = new GStyle;
return *s_the;
}
GStyle::GStyle()
{
}
void GStyle::paint_button(Painter& painter, const Rect& rect, bool pressed)
{
Color button_color = Color::LightGray;
Color highlight_color = Color::White;
Color shadow_color = Color(96, 96, 96);
painter.draw_rect(rect, Color::Black, true);
painter.translate(rect.location());
if (pressed) {
// Base
painter.fill_rect({ 1, 1, rect.width() - 2, rect.height() - 2 }, button_color);
// Sunken shadow
painter.draw_line({ 1, 1 }, { rect.width() - 2, 1 }, shadow_color);
painter.draw_line({ 1, 2 }, {1, rect.height() - 2 }, shadow_color);
} else {
// Base
painter.fill_rect({ 3, 3, rect.width() - 5, rect.height() - 5 }, button_color);
painter.fill_rect_with_gradient({ 3, 3, rect.width() - 5, rect.height() - 5 }, button_color, Color::White);
// White highlight
painter.draw_line({ 1, 1 }, { rect.width() - 2, 1 }, highlight_color);
painter.draw_line({ 1, 2 }, { rect.width() - 3, 2 }, highlight_color);
painter.draw_line({ 1, 3 }, { 1, rect.height() - 2 }, highlight_color);
painter.draw_line({ 2, 3 }, { 2, rect.height() - 3 }, highlight_color);
// Gray shadow
painter.draw_line({ rect.width() - 2, 1 }, { rect.width() - 2, rect.height() - 4 }, shadow_color);
painter.draw_line({ rect.width() - 3, 2 }, { rect.width() - 3, rect.height() - 4 }, shadow_color);
painter.draw_line({ 1, rect.height() - 2 }, { rect.width() - 2, rect.height() - 2 }, shadow_color);
painter.draw_line({ 2, rect.height() - 3 }, { rect.width() - 2, rect.height() - 3 }, shadow_color);
}
painter.translate(-rect.location().x(), -rect.location().y());
}

14
LibGUI/GStyle.h Normal file
View File

@ -0,0 +1,14 @@
#pragma once
class Painter;
class Rect;
class GStyle {
public:
static GStyle& the();
void paint_button(Painter& painter, const Rect& rect, bool pressed);
private:
GStyle();
};

View File

@ -16,6 +16,7 @@ LIBGUI_OBJS = \
GTextBox.o \
GScrollBar.o \
GWidget.o \
GStyle.o \
GWindow.o
OBJS = $(SHAREDGRAPHICS_OBJS) $(LIBGUI_OBJS)

View File

@ -50,6 +50,7 @@ public:
Rect clip_rect() const { return m_clip_rect; }
void translate(int dx, int dy) { m_translation.move_by(dx, dy); }
void translate(const Point& delta) { m_translation.move_by(delta); }
private:
void set_pixel_with_draw_op(dword& pixel, const Color&);

View File

@ -28,7 +28,7 @@ public:
move_by(delta.x(), delta.y());
}
Point translated(int dx, int dy)
Point translated(int dx, int dy) const
{
Point point = *this;
point.move_by(dx, dy);

View File

@ -68,21 +68,21 @@ public:
set_height(height() - h);
}
Rect shrunken(int w, int h)
Rect shrunken(int w, int h) const
{
Rect rect = *this;
rect.shrink(w, h);
return rect;
}
Rect inflated(int w, int h)
Rect inflated(int w, int h) const
{
Rect rect = *this;
rect.inflate(w, h);
return rect;
}
Rect translated(int dx, int dy)
Rect translated(int dx, int dy) const
{
Rect rect = *this;
rect.move_by(dx, dy);