LibGUI: Add a GPainter class that inherits from Painter.

This gets rid of the last little piece of LibGUI knowledge in Painter.
This commit is contained in:
Andreas Kling 2019-03-28 17:19:56 +01:00
parent 326c6fd607
commit 9fa21fa585
Notes: sideshowbarker 2024-07-19 14:54:59 +09:00
27 changed files with 75 additions and 78 deletions

View file

@ -7,7 +7,7 @@
#include <AK/FileSystemPath.h>
#include <AK/StringBuilder.h>
#include <SharedGraphics/GraphicsBitmap.h>
#include <SharedGraphics/Painter.h>
#include <LibGUI/GPainter.h>
#include <LibGUI/GLock.h>
static GLockable<HashMap<String, RetainPtr<GraphicsBitmap>>>& thumbnail_cache()

View file

@ -1,5 +1,5 @@
#include "FontEditor.h"
#include <SharedGraphics/Painter.h>
#include <LibGUI/GPainter.h>
#include <LibGUI/GButton.h>
#include <LibGUI/GLabel.h>
#include <LibGUI/GTextBox.h>
@ -158,7 +158,7 @@ Rect GlyphMapWidget::get_outer_rect(byte glyph) const
void GlyphMapWidget::paint_event(GPaintEvent&)
{
Painter painter(*this);
GPainter painter(*this);
painter.set_font(font());
painter.fill_rect(rect(), Color::White);
painter.draw_rect(rect(), Color::Black);
@ -219,7 +219,7 @@ void GlyphEditorWidget::set_glyph(byte glyph)
void GlyphEditorWidget::paint_event(GPaintEvent&)
{
Painter painter(*this);
GPainter painter(*this);
painter.fill_rect(rect(), Color::White);
painter.draw_rect(rect(), Color::Black);

View file

@ -1,5 +1,5 @@
#include "MemoryStatsWidget.h"
#include <SharedGraphics/Painter.h>
#include <LibGUI/GPainter.h>
#include <LibGUI/GBoxLayout.h>
#include <LibGUI/GLabel.h>
#include <LibGUI/GStyle.h>
@ -104,7 +104,7 @@ void MemoryStatsWidget::timer_event(GTimerEvent&)
void MemoryStatsWidget::paint_event(GPaintEvent& event)
{
Painter painter(*this);
GPainter painter(*this);
painter.set_clip_rect(event.rect());
GStyle::the().paint_surface(painter, rect());
}

View file

@ -8,7 +8,7 @@
#include <AK/AKString.h>
#include <AK/StringBuilder.h>
#include <SharedGraphics/Font.h>
#include <SharedGraphics/Painter.h>
#include <LibGUI/GPainter.h>
#include <AK/StdLibExtras.h>
#include <LibGUI/GApplication.h>
#include <LibGUI/GWindow.h>
@ -761,7 +761,7 @@ void Terminal::keydown_event(GKeyEvent& event)
void Terminal::paint_event(GPaintEvent&)
{
Painter painter(*this);
GPainter painter(*this);
if (m_needs_background_fill) {
m_needs_background_fill = false;

View file

@ -1,7 +1,7 @@
#include <LibGUI/GAbstractView.h>
#include <LibGUI/GModel.h>
#include <LibGUI/GScrollBar.h>
#include <SharedGraphics/Painter.h>
#include <LibGUI/GPainter.h>
#include <Kernel/KeyCode.h>
GAbstractView::GAbstractView(GWidget* parent)

View file

@ -1,5 +1,5 @@
#include "GButton.h"
#include <SharedGraphics/Painter.h>
#include <LibGUI/GPainter.h>
#include <LibGUI/GStyle.h>
//#define GBUTTON_DEBUG
@ -23,7 +23,7 @@ void GButton::set_caption(const String& caption)
void GButton::paint_event(GPaintEvent& event)
{
Painter painter(*this);
GPainter painter(*this);
painter.set_clip_rect(event.rect());
GStyle::the().paint_button(painter, rect(), m_button_style, m_being_pressed, m_hovered);

View file

@ -1,5 +1,5 @@
#include "GCheckBox.h"
#include <SharedGraphics/Painter.h>
#include <LibGUI/GPainter.h>
#include <SharedGraphics/CharacterBitmap.h>
#include <Kernel/KeyCode.h>
@ -54,7 +54,7 @@ void GCheckBox::set_checked(bool b)
void GCheckBox::paint_event(GPaintEvent& event)
{
Painter painter(*this);
GPainter painter(*this);
painter.set_clip_rect(event.rect());
auto text_rect = rect();

View file

@ -1,6 +1,6 @@
#include <LibGUI/GFrame.h>
#include <LibGUI/GStyle.h>
#include <SharedGraphics/Painter.h>
#include <LibGUI/GPainter.h>
GFrame::GFrame(GWidget* parent)
: GWidget(parent)
@ -16,7 +16,7 @@ void GFrame::paint_event(GPaintEvent& event)
if (m_shape == Shape::NoFrame)
return;
Painter painter(*this);
GPainter painter(*this);
painter.set_clip_rect(event.rect());
auto rect = this->rect();

View file

@ -1,7 +1,7 @@
#include <LibGUI/GItemView.h>
#include <LibGUI/GModel.h>
#include <LibGUI/GScrollBar.h>
#include <SharedGraphics/Painter.h>
#include <LibGUI/GPainter.h>
#include <Kernel/KeyCode.h>
GItemView::GItemView(GWidget* parent)
@ -98,7 +98,7 @@ void GItemView::paint_event(GPaintEvent& event)
{
GFrame::paint_event(event);
Painter painter(*this);
GPainter painter(*this);
painter.set_clip_rect(widget_inner_rect());
painter.set_clip_rect(event.rect());
painter.fill_rect(event.rect(), Color::White);

View file

@ -1,5 +1,5 @@
#include "GLabel.h"
#include <SharedGraphics/Painter.h>
#include <LibGUI/GPainter.h>
#include <SharedGraphics/GraphicsBitmap.h>
GLabel::GLabel(GWidget* parent)
@ -34,7 +34,7 @@ void GLabel::paint_event(GPaintEvent& event)
{
GFrame::paint_event(event);
Painter painter(*this);
GPainter painter(*this);
painter.set_clip_rect(event.rect());
if (m_icon) {

View file

@ -1,6 +1,6 @@
#include "GListBox.h"
#include <SharedGraphics/Font.h>
#include <SharedGraphics/Painter.h>
#include <LibGUI/GPainter.h>
GListBox::GListBox(GWidget* parent)
: GWidget(parent)
@ -19,7 +19,7 @@ Rect GListBox::item_rect(int index) const
void GListBox::paint_event(GPaintEvent& event)
{
Painter painter(*this);
GPainter painter(*this);
painter.set_clip_rect(event.rect());
painter.fill_rect({ rect().x() + 1, rect().y() + 1, rect().width() - 2, rect().height() - 2 }, background_color());

19
LibGUI/GPainter.cpp Normal file
View file

@ -0,0 +1,19 @@
#include <LibGUI/GPainter.h>
#include <LibGUI/GWidget.h>
#include <LibGUI/GWindow.h>
GPainter::GPainter(GraphicsBitmap& bitmap)
: Painter(bitmap)
{
}
GPainter::GPainter(GWidget& widget)
: Painter(*widget.window()->back_bitmap())
{
state().font = &widget.font();
auto origin_rect = widget.window_relative_rect();
state().translation = origin_rect.location();
state().clip_rect = origin_rect;
m_clip_origin = origin_rect;
state().clip_rect.intersect(m_target->rect());
}

12
LibGUI/GPainter.h Normal file
View file

@ -0,0 +1,12 @@
#pragma once
#include <SharedGraphics/Painter.h>
class GWidget;
class GraphicsBitmap;
class GPainter : public Painter {
public:
explicit GPainter(GWidget&);
explicit GPainter(GraphicsBitmap&);
};

View file

@ -1,5 +1,5 @@
#include <LibGUI/GProgressBar.h>
#include <SharedGraphics/Painter.h>
#include <LibGUI/GPainter.h>
#include <AK/StringBuilder.h>
GProgressBar::GProgressBar(GWidget* parent)
@ -32,7 +32,7 @@ void GProgressBar::set_range(int min, int max)
void GProgressBar::paint_event(GPaintEvent& event)
{
Painter painter(*this);
GPainter painter(*this);
painter.set_clip_rect(event.rect());
// First we fill the entire widget with the gradient. This incurs a bit of

View file

@ -2,7 +2,7 @@
#include <LibGUI/GStyle.h>
#include <SharedGraphics/CharacterBitmap.h>
#include <SharedGraphics/GraphicsBitmap.h>
#include <SharedGraphics/Painter.h>
#include <LibGUI/GPainter.h>
//#define GUTTER_DOES_PAGEUP_PAGEDOWN
@ -185,7 +185,7 @@ Rect GScrollBar::scrubber_rect() const
void GScrollBar::paint_event(GPaintEvent& event)
{
Painter painter(*this);
GPainter painter(*this);
painter.set_clip_rect(event.rect());
painter.fill_rect(rect(), Color(164, 164, 164));

View file

@ -2,7 +2,7 @@
#include <LibGUI/GLabel.h>
#include <LibGUI/GBoxLayout.h>
#include <LibGUI/GStyle.h>
#include <SharedGraphics/Painter.h>
#include <LibGUI/GPainter.h>
GStatusBar::GStatusBar(GWidget* parent)
: GWidget(parent)
@ -34,7 +34,7 @@ String GStatusBar::text() const
void GStatusBar::paint_event(GPaintEvent& event)
{
Painter painter(*this);
GPainter painter(*this);
painter.set_clip_rect(event.rect());
GStyle::the().paint_surface(painter, rect());
}

View file

@ -1,5 +1,5 @@
#include <LibGUI/GStyle.h>
#include <SharedGraphics/Painter.h>
#include <LibGUI/GPainter.h>
static GStyle* s_the;

View file

@ -1,7 +1,7 @@
#include <LibGUI/GTableView.h>
#include <LibGUI/GModel.h>
#include <LibGUI/GScrollBar.h>
#include <SharedGraphics/Painter.h>
#include <LibGUI/GPainter.h>
#include <Kernel/KeyCode.h>
GTableView::GTableView(GWidget* parent)
@ -100,7 +100,7 @@ void GTableView::paint_event(GPaintEvent& event)
{
GFrame::paint_event(event);
Painter painter(*this);
GPainter painter(*this);
painter.set_clip_rect(frame_inner_rect());
painter.set_clip_rect(event.rect());
painter.save();

View file

@ -2,7 +2,7 @@
#include <AK/StdLibExtras.h>
#include <SharedGraphics/CharacterBitmap.h>
#include <SharedGraphics/Font.h>
#include <SharedGraphics/Painter.h>
#include <LibGUI/GPainter.h>
#include <Kernel/KeyCode.h>
GTextBox::GTextBox(GWidget* parent)
@ -73,7 +73,7 @@ Point GTextBox::cursor_content_position() const
void GTextBox::paint_event(GPaintEvent& event)
{
Painter painter(*this);
GPainter painter(*this);
painter.set_clip_rect(event.rect());
painter.fill_rect(rect().shrunken(2, 2), background_color());

View file

@ -2,7 +2,7 @@
#include <LibGUI/GScrollBar.h>
#include <LibGUI/GFontDatabase.h>
#include <LibGUI/GClipboard.h>
#include <SharedGraphics/Painter.h>
#include <LibGUI/GPainter.h>
#include <Kernel/KeyCode.h>
#include <AK/StringBuilder.h>
#include <unistd.h>
@ -152,7 +152,7 @@ void GTextEditor::paint_event(GPaintEvent& event)
{
GFrame::paint_event(event);
Painter painter(*this);
GPainter painter(*this);
painter.set_clip_rect(widget_inner_rect());
painter.set_clip_rect(event.rect());
painter.fill_rect(event.rect(), Color::White);

View file

@ -2,7 +2,7 @@
#include <LibGUI/GBoxLayout.h>
#include <LibGUI/GButton.h>
#include <LibGUI/GAction.h>
#include <SharedGraphics/Painter.h>
#include <LibGUI/GPainter.h>
GToolBar::GToolBar(GWidget* parent)
: GWidget(parent)
@ -56,7 +56,7 @@ public:
virtual void paint_event(GPaintEvent& event) override
{
Painter painter(*this);
GPainter painter(*this);
painter.set_clip_rect(event.rect());
painter.translate(rect().center().x() - 1, 0);
painter.draw_line({ 0, 0 }, { 0, rect().bottom() }, Color::MidGray);
@ -77,7 +77,7 @@ void GToolBar::add_separator()
void GToolBar::paint_event(GPaintEvent& event)
{
Painter painter(*this);
GPainter painter(*this);
painter.set_clip_rect(event.rect());
GStyle::the().paint_surface(painter, rect());
}

View file

@ -5,7 +5,7 @@
#include <LibGUI/GLayout.h>
#include <AK/Assertions.h>
#include <SharedGraphics/GraphicsBitmap.h>
#include <SharedGraphics/Painter.h>
#include <LibGUI/GPainter.h>
#include <unistd.h>
@ -85,13 +85,13 @@ void GWidget::handle_paint_event(GPaintEvent& event)
{
ASSERT(is_visible());
if (fill_with_background_color()) {
Painter painter(*this);
GPainter painter(*this);
painter.fill_rect(event.rect(), background_color());
} else {
#ifdef DEBUG_WIDGET_UNDERDRAW
// FIXME: This is a bit broken.
// If the widget is not opaque, let's not mess it up with debugging color.
Painter painter(*this);
GPainter painter(*this);
painter.fill_rect(rect(), Color::Red);
#endif
}

View file

@ -3,7 +3,7 @@
#include "GEventLoop.h"
#include "GWidget.h"
#include <SharedGraphics/GraphicsBitmap.h>
#include <SharedGraphics/Painter.h>
#include <LibGUI/GPainter.h>
#include <LibC/stdio.h>
#include <LibC/stdlib.h>
#include <LibC/unistd.h>

View file

@ -8,6 +8,7 @@ SHAREDGRAPHICS_OBJS = \
../SharedGraphics/PNGLoader.o
LIBGUI_OBJS = \
GPainter.o \
GIODevice.o \
GFile.o \
GButton.o \

View file

@ -4,16 +4,6 @@
#include <SharedGraphics/CharacterBitmap.h>
#include <AK/Assertions.h>
#include <AK/StdLibExtras.h>
#ifdef LIBGUI
#include <LibGUI/GWidget.h>
#include <LibGUI/GWindow.h>
#include <LibGUI/GEventLoop.h>
#include <LibC/stdio.h>
#include <LibC/errno.h>
#include <LibC/string.h>
#endif
#include <unistd.h>
Painter::Painter(GraphicsBitmap& bitmap)
@ -25,22 +15,6 @@ Painter::Painter(GraphicsBitmap& bitmap)
m_clip_origin = state().clip_rect;
}
#ifdef LIBGUI
Painter::Painter(GWidget& widget)
: m_window(widget.window())
, m_target(*m_window->back_bitmap())
{
m_state_stack.append(State());
state().font = &widget.font();
auto origin_rect = widget.window_relative_rect();
state().translation = origin_rect.location();
state().clip_rect = origin_rect;
m_clip_origin = origin_rect;
state().clip_rect.intersect(m_target->rect());
}
#endif
Painter::~Painter()
{
}

View file

@ -12,16 +12,8 @@ class GlyphBitmap;
class GraphicsBitmap;
class Font;
#ifdef USERLAND
class GWidget;
class GWindow;
#endif
class Painter {
public:
#ifdef USERLAND
explicit Painter(GWidget&);
#endif
explicit Painter(GraphicsBitmap&);
~Painter();
void fill_rect(const Rect&, Color);
@ -64,7 +56,7 @@ public:
void save() { m_state_stack.append(m_state_stack.last()); }
void restore() { ASSERT(m_state_stack.size() > 1); m_state_stack.take_last(); }
private:
protected:
void set_pixel_with_draw_op(dword& pixel, const Color&);
void fill_rect_with_draw_op(const Rect&, Color);
void blit_with_alpha(const Point&, const GraphicsBitmap&, const Rect& src_rect);
@ -80,7 +72,6 @@ private:
const State& state() const { return m_state_stack.last(); }
Rect m_clip_origin;
GWindow* m_window { nullptr };
Retained<GraphicsBitmap> m_target;
Vector<State> m_state_stack;
};

View file

@ -8,7 +8,7 @@
#include <time.h>
#include <Kernel/Syscall.h>
#include <SharedGraphics/GraphicsBitmap.h>
#include <SharedGraphics/Painter.h>
#include <LibGUI/GPainter.h>
#include <LibGUI/GWindow.h>
#include <LibGUI/GWidget.h>
#include <LibGUI/GLabel.h>