LibGUI+LibGfx: Move check box painting to Gfx::StylePainter

This will allow us to render check boxes from other places. :^)
This commit is contained in:
Andreas Kling 2020-09-11 17:21:53 +02:00
parent 072e94caa2
commit 1a2b626746
Notes: sideshowbarker 2024-07-19 02:46:04 +09:00
5 changed files with 44 additions and 25 deletions

View file

@ -33,21 +33,6 @@
namespace GUI {
static const char* s_checked_bitmap_data = {
" "
" # "
" ## "
" ### "
" ## ### "
" ##### "
" ### "
" # "
" "
};
static Gfx::CharacterBitmap* s_checked_bitmap;
static const int s_checked_bitmap_width = 9;
static const int s_checked_bitmap_height = 9;
static const int s_box_width = 13;
static const int s_box_height = 13;
@ -81,17 +66,8 @@ void CheckBox::paint_event(PaintEvent& event)
0, height() / 2 - s_box_height / 2 - 1,
s_box_width, s_box_height
};
painter.fill_rect(box_rect, is_enabled() ? palette().base() : palette().window());
Gfx::StylePainter::paint_frame(painter, box_rect, palette(), Gfx::FrameShape::Container, Gfx::FrameShadow::Sunken, 2);
if (is_being_pressed())
painter.draw_rect(box_rect.shrunken(4, 4), Color::MidGray);
if (is_checked()) {
if (!s_checked_bitmap)
s_checked_bitmap = &Gfx::CharacterBitmap::create_from_ascii(s_checked_bitmap_data, s_checked_bitmap_width, s_checked_bitmap_height).leak_ref();
painter.draw_bitmap(box_rect.shrunken(4, 4).location(), *s_checked_bitmap, palette().base_text());
}
Gfx::StylePainter::paint_check_box(painter, box_rect, palette(), is_enabled(), is_checked(), is_being_pressed());
paint_text(painter, text_rect, font(), Gfx::TextAlignment::TopLeft);
}

View file

@ -27,6 +27,7 @@
#include <AK/StringView.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/CharacterBitmap.h>
#include <LibGfx/ClassicStylePainter.h>
#include <LibGfx/Painter.h>
#include <LibGfx/Palette.h>
@ -345,4 +346,38 @@ void ClassicStylePainter::paint_radio_button(Painter& painter, const IntRect& re
auto& bitmap = circle_bitmap(is_checked, is_being_pressed);
painter.blit(rect.location(), bitmap, bitmap.rect());
}
static const char* s_checked_bitmap_data = {
" "
" # "
" ## "
" ### "
" ## ### "
" ##### "
" ### "
" # "
" "
};
static Gfx::CharacterBitmap* s_checked_bitmap;
static const int s_checked_bitmap_width = 9;
static const int s_checked_bitmap_height = 9;
void ClassicStylePainter::paint_check_box(Painter& painter, const IntRect& rect, const Palette& palette, bool is_enabled, bool is_checked, bool is_being_pressed)
{
painter.fill_rect(rect, is_enabled ? palette.base() : palette.window());
paint_frame(painter, rect, palette, Gfx::FrameShape::Container, Gfx::FrameShadow::Sunken, 2);
if (is_being_pressed) {
// FIXME: This color should not be hard-coded.
painter.draw_rect(rect.shrunken(4, 4), Color::MidGray);
}
if (is_checked) {
if (!s_checked_bitmap)
s_checked_bitmap = &Gfx::CharacterBitmap::create_from_ascii(s_checked_bitmap_data, s_checked_bitmap_width, s_checked_bitmap_height).leak_ref();
painter.draw_bitmap(rect.shrunken(4, 4).location(), *s_checked_bitmap, palette.base_text());
}
}
}

View file

@ -42,6 +42,7 @@ public:
void paint_window_frame(Painter&, const IntRect&, const Palette&) override;
void paint_progress_bar(Painter&, const IntRect&, const Palette&, int min, int max, int value, const StringView& text) override;
void paint_radio_button(Painter&, const IntRect&, const Palette&, bool is_checked, bool is_being_pressed) override;
void paint_check_box(Painter&, const IntRect&, const Palette&, bool is_enabled, bool is_checked, bool is_being_pressed) override;
};
}

View file

@ -73,4 +73,9 @@ void StylePainter::paint_radio_button(Painter& painter, const IntRect& rect, con
current().paint_radio_button(painter, rect, palette, is_checked, is_being_pressed);
}
void StylePainter::paint_check_box(Painter& painter, const IntRect& rect, const Palette& palette, bool is_enabled, bool is_checked, bool is_being_pressed)
{
current().paint_check_box(painter, rect, palette, is_enabled, is_checked, is_being_pressed);
}
}

View file

@ -61,6 +61,7 @@ public:
virtual void paint_window_frame(Painter&, const IntRect&, const Palette&) = 0;
virtual void paint_progress_bar(Painter&, const IntRect&, const Palette&, int min, int max, int value, const StringView& text) = 0;
virtual void paint_radio_button(Painter&, const IntRect&, const Palette&, bool is_checked, bool is_being_pressed) = 0;
virtual void paint_check_box(Painter&, const IntRect&, const Palette&, bool is_enabled, bool is_checked, bool is_being_pressed) = 0;
protected:
BaseStylePainter() { }
@ -78,6 +79,7 @@ public:
static void paint_window_frame(Painter&, const IntRect&, const Palette&);
static void paint_progress_bar(Painter&, const IntRect&, const Palette&, int min, int max, int value, const StringView& text);
static void paint_radio_button(Painter&, const IntRect&, const Palette&, bool is_checked, bool is_being_pressed);
static void paint_check_box(Painter&, const IntRect&, const Palette&, bool is_enabled, bool is_checked, bool is_being_pressed);
};
}