Let widget have a font() instead of using Font::defaultFont() everywhere.
This commit is contained in:
parent
e5acbca0e8
commit
fc1facf5c0
Notes:
sideshowbarker
2024-07-19 18:48:22 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/fc1facf5c09
8 changed files with 32 additions and 25 deletions
|
@ -1,7 +1,6 @@
|
|||
#include "CheckBox.h"
|
||||
#include "Painter.h"
|
||||
#include "CBitmap.h"
|
||||
#include "Font.h"
|
||||
#include <cstdio>
|
||||
|
||||
CheckBox::CheckBox(Widget* parent)
|
||||
|
@ -81,7 +80,7 @@ void CheckBox::paintEvent(PaintEvent&)
|
|||
|
||||
auto textRect = rect();
|
||||
textRect.setLeft(bitmap->width() + 4);
|
||||
textRect.setTop(height() / 2 - Font::defaultFont().glyphHeight() / 2);
|
||||
textRect.setTop(height() / 2 - font().glyphHeight() / 2);
|
||||
|
||||
Point bitmapPosition;
|
||||
bitmapPosition.setX(2);
|
||||
|
|
|
@ -14,7 +14,7 @@ ListBox::~ListBox()
|
|||
|
||||
unsigned ListBox::itemHeight() const
|
||||
{
|
||||
return Font::defaultFont().glyphHeight() + 2;
|
||||
return font().glyphHeight() + 2;
|
||||
}
|
||||
|
||||
void ListBox::paintEvent(PaintEvent&)
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
|
||||
Painter::Painter(Widget& widget)
|
||||
: m_widget(widget)
|
||||
, m_font(Font::defaultFont())
|
||||
, m_font(&widget.font())
|
||||
{
|
||||
if (auto* window = widget.window()) {
|
||||
m_translation = window->position();
|
||||
|
@ -115,9 +115,9 @@ void Painter::drawText(const Rect& rect, const String& text, TextAlignment align
|
|||
if (alignment == TextAlignment::TopLeft) {
|
||||
point = rect.location();
|
||||
} else if (alignment == TextAlignment::Center) {
|
||||
int textWidth = text.length() * m_font.glyphWidth();
|
||||
int textWidth = text.length() * font().glyphWidth();
|
||||
point = rect.center();
|
||||
point.moveBy(-(textWidth / 2), -(m_font.glyphHeight() / 2));
|
||||
point.moveBy(-(textWidth / 2), -(font().glyphHeight() / 2));
|
||||
} else {
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
|
@ -126,12 +126,12 @@ void Painter::drawText(const Rect& rect, const String& text, TextAlignment align
|
|||
byte ch = text[i];
|
||||
if (ch == ' ')
|
||||
continue;
|
||||
auto* bitmap = m_font.glyphBitmap(ch);
|
||||
auto* bitmap = font().glyphBitmap(ch);
|
||||
if (!bitmap) {
|
||||
printf("Font doesn't have 0x%02x ('%c')\n", ch, ch);
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
int x = point.x() + i * m_font.glyphWidth();
|
||||
int x = point.x() + i * font().glyphWidth();
|
||||
int y = point.y();
|
||||
drawBitmap({ x, y }, *bitmap, color);
|
||||
}
|
||||
|
|
|
@ -26,11 +26,11 @@ public:
|
|||
|
||||
void xorRect(const Rect&, Color);
|
||||
|
||||
const Font& font() const;
|
||||
const Font& font() const { return *m_font; }
|
||||
|
||||
private:
|
||||
Widget& m_widget;
|
||||
Font& m_font;
|
||||
const Font* m_font;
|
||||
Point m_translation;
|
||||
|
||||
Rect m_clipRect;
|
||||
|
|
|
@ -14,9 +14,7 @@ TerminalWidget::TerminalWidget(Widget* parent)
|
|||
{
|
||||
g_tw = this;
|
||||
|
||||
auto& font = Font::defaultFont();
|
||||
|
||||
setWindowRelativeRect({ 0, 0, (columns() * font.glyphWidth()) + 4, (rows() * font.glyphHeight()) + 4 });
|
||||
setWindowRelativeRect({ 0, 0, (columns() * font().glyphWidth()) + 4, (rows() * font().glyphHeight()) + 4 });
|
||||
|
||||
printf("rekt: %d x %d\n", width(), height());
|
||||
m_screen = new CharacterWithAttributes[rows() * columns()];
|
||||
|
@ -65,15 +63,13 @@ void TerminalWidget::paintEvent(PaintEvent&)
|
|||
Painter painter(*this);
|
||||
painter.fillRect(rect(), Color::Black);
|
||||
|
||||
auto& font = Font::defaultFont();
|
||||
|
||||
char buf[2] = { 0, 0 };
|
||||
for (unsigned row = 0; row < m_rows; ++row) {
|
||||
int y = row * font.glyphHeight();
|
||||
int y = row * font().glyphHeight();
|
||||
for (unsigned column = 0; column < m_columns; ++column) {
|
||||
int x = column * font.glyphWidth();
|
||||
int x = column * font().glyphWidth();
|
||||
buf[0] = at(row, column).character;
|
||||
painter.drawText({ x + 2, y + 2, width(), font.glyphHeight() }, buf, Painter::TextAlignment::TopLeft, Color(0xa0, 0xa0, 0xa0));
|
||||
painter.drawText({ x + 2, y + 2, width(), font().glyphHeight() }, buf, Painter::TextAlignment::TopLeft, Color(0xa0, 0xa0, 0xa0));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -35,20 +35,18 @@ void TextBox::paintEvent(PaintEvent&)
|
|||
Rect innerRect = rect();
|
||||
innerRect.shrink(6, 6);
|
||||
|
||||
auto& font = Font::defaultFont();
|
||||
|
||||
unsigned maxCharsToPaint = innerRect.width() / font.glyphWidth();
|
||||
unsigned maxCharsToPaint = innerRect.width() / font().glyphWidth();
|
||||
|
||||
int firstVisibleChar = max((int)m_cursorPosition - (int)maxCharsToPaint, 0);
|
||||
unsigned charsToPaint = min(m_text.length() - firstVisibleChar, maxCharsToPaint);
|
||||
|
||||
int y = innerRect.center().y() - font.glyphHeight() / 2;
|
||||
int y = innerRect.center().y() - font().glyphHeight() / 2;
|
||||
for (unsigned i = 0; i < charsToPaint; ++i) {
|
||||
char ch = m_text[firstVisibleChar + i];
|
||||
if (ch == ' ')
|
||||
continue;
|
||||
int x = innerRect.x() + (i * font.glyphWidth());
|
||||
auto* bitmap = font.glyphBitmap(ch);
|
||||
int x = innerRect.x() + (i * font().glyphWidth());
|
||||
auto* bitmap = font().glyphBitmap(ch);
|
||||
if (!bitmap) {
|
||||
printf("TextBox: glyph missing: %02x\n", ch);
|
||||
ASSERT_NOT_REACHED();
|
||||
|
@ -58,7 +56,7 @@ void TextBox::paintEvent(PaintEvent&)
|
|||
|
||||
if (isFocused() && m_cursorBlinkState) {
|
||||
unsigned visibleCursorPosition = m_cursorPosition - firstVisibleChar;
|
||||
Rect cursorRect(innerRect.x() + visibleCursorPosition * font.glyphWidth(), innerRect.y(), 1, innerRect.height());
|
||||
Rect cursorRect(innerRect.x() + visibleCursorPosition * font().glyphWidth(), innerRect.y(), 1, innerRect.height());
|
||||
painter.fillRect(cursorRect, foregroundColor());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
Widget::Widget(Widget* parent)
|
||||
: Object(parent)
|
||||
{
|
||||
setFont(nullptr);
|
||||
m_backgroundColor = Color::White;
|
||||
m_foregroundColor = Color::Black;
|
||||
}
|
||||
|
@ -146,3 +147,11 @@ void Widget::setFocus(bool focus)
|
|||
if (auto* win = window())
|
||||
win->setFocusedWidget(this);
|
||||
}
|
||||
|
||||
void Widget::setFont(RetainPtr<Font>&& font)
|
||||
{
|
||||
if (!font)
|
||||
m_font = Font::defaultFont();
|
||||
else
|
||||
m_font = std::move(font);
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
#include "Object.h"
|
||||
#include "Rect.h"
|
||||
#include "Color.h"
|
||||
#include "Font.h"
|
||||
#include <AK/String.h>
|
||||
#include <functional>
|
||||
|
||||
|
@ -79,12 +80,16 @@ public:
|
|||
void setFillWithBackgroundColor(bool b) { m_fillWithBackgroundColor = b; }
|
||||
bool fillWithBackgroundColor() const { return m_fillWithBackgroundColor; }
|
||||
|
||||
const Font& font() const { return *m_font; }
|
||||
void setFont(RetainPtr<Font>&&);
|
||||
|
||||
private:
|
||||
Window* m_window { nullptr };
|
||||
|
||||
Rect m_relativeRect;
|
||||
Color m_backgroundColor;
|
||||
Color m_foregroundColor;
|
||||
RetainPtr<Font> m_font;
|
||||
|
||||
bool m_hasPendingPaintEvent { false };
|
||||
bool m_fillWithBackgroundColor { false };
|
||||
|
|
Loading…
Add table
Reference in a new issue