1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 16:17:45 +00:00

Let widget have a font() instead of using Font::defaultFont() everywhere.

This commit is contained in:
Andreas Kling 2018-10-14 13:06:05 +02:00
parent e5acbca0e8
commit fc1facf5c0
8 changed files with 32 additions and 25 deletions

View file

@ -1,7 +1,6 @@
#include "CheckBox.h" #include "CheckBox.h"
#include "Painter.h" #include "Painter.h"
#include "CBitmap.h" #include "CBitmap.h"
#include "Font.h"
#include <cstdio> #include <cstdio>
CheckBox::CheckBox(Widget* parent) CheckBox::CheckBox(Widget* parent)
@ -81,7 +80,7 @@ void CheckBox::paintEvent(PaintEvent&)
auto textRect = rect(); auto textRect = rect();
textRect.setLeft(bitmap->width() + 4); textRect.setLeft(bitmap->width() + 4);
textRect.setTop(height() / 2 - Font::defaultFont().glyphHeight() / 2); textRect.setTop(height() / 2 - font().glyphHeight() / 2);
Point bitmapPosition; Point bitmapPosition;
bitmapPosition.setX(2); bitmapPosition.setX(2);

View file

@ -14,7 +14,7 @@ ListBox::~ListBox()
unsigned ListBox::itemHeight() const unsigned ListBox::itemHeight() const
{ {
return Font::defaultFont().glyphHeight() + 2; return font().glyphHeight() + 2;
} }
void ListBox::paintEvent(PaintEvent&) void ListBox::paintEvent(PaintEvent&)

View file

@ -9,7 +9,7 @@
Painter::Painter(Widget& widget) Painter::Painter(Widget& widget)
: m_widget(widget) : m_widget(widget)
, m_font(Font::defaultFont()) , m_font(&widget.font())
{ {
if (auto* window = widget.window()) { if (auto* window = widget.window()) {
m_translation = window->position(); m_translation = window->position();
@ -115,9 +115,9 @@ void Painter::drawText(const Rect& rect, const String& text, TextAlignment align
if (alignment == TextAlignment::TopLeft) { if (alignment == TextAlignment::TopLeft) {
point = rect.location(); point = rect.location();
} else if (alignment == TextAlignment::Center) { } else if (alignment == TextAlignment::Center) {
int textWidth = text.length() * m_font.glyphWidth(); int textWidth = text.length() * font().glyphWidth();
point = rect.center(); point = rect.center();
point.moveBy(-(textWidth / 2), -(m_font.glyphHeight() / 2)); point.moveBy(-(textWidth / 2), -(font().glyphHeight() / 2));
} else { } else {
ASSERT_NOT_REACHED(); ASSERT_NOT_REACHED();
} }
@ -126,12 +126,12 @@ void Painter::drawText(const Rect& rect, const String& text, TextAlignment align
byte ch = text[i]; byte ch = text[i];
if (ch == ' ') if (ch == ' ')
continue; continue;
auto* bitmap = m_font.glyphBitmap(ch); auto* bitmap = font().glyphBitmap(ch);
if (!bitmap) { if (!bitmap) {
printf("Font doesn't have 0x%02x ('%c')\n", ch, ch); printf("Font doesn't have 0x%02x ('%c')\n", ch, ch);
ASSERT_NOT_REACHED(); ASSERT_NOT_REACHED();
} }
int x = point.x() + i * m_font.glyphWidth(); int x = point.x() + i * font().glyphWidth();
int y = point.y(); int y = point.y();
drawBitmap({ x, y }, *bitmap, color); drawBitmap({ x, y }, *bitmap, color);
} }

View file

@ -26,11 +26,11 @@ public:
void xorRect(const Rect&, Color); void xorRect(const Rect&, Color);
const Font& font() const; const Font& font() const { return *m_font; }
private: private:
Widget& m_widget; Widget& m_widget;
Font& m_font; const Font* m_font;
Point m_translation; Point m_translation;
Rect m_clipRect; Rect m_clipRect;

View file

@ -14,9 +14,7 @@ TerminalWidget::TerminalWidget(Widget* parent)
{ {
g_tw = this; 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()); printf("rekt: %d x %d\n", width(), height());
m_screen = new CharacterWithAttributes[rows() * columns()]; m_screen = new CharacterWithAttributes[rows() * columns()];
@ -65,15 +63,13 @@ void TerminalWidget::paintEvent(PaintEvent&)
Painter painter(*this); Painter painter(*this);
painter.fillRect(rect(), Color::Black); painter.fillRect(rect(), Color::Black);
auto& font = Font::defaultFont();
char buf[2] = { 0, 0 }; char buf[2] = { 0, 0 };
for (unsigned row = 0; row < m_rows; ++row) { 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) { for (unsigned column = 0; column < m_columns; ++column) {
int x = column * font.glyphWidth(); int x = column * font().glyphWidth();
buf[0] = at(row, column).character; 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));
} }
} }

View file

@ -35,20 +35,18 @@ void TextBox::paintEvent(PaintEvent&)
Rect innerRect = rect(); Rect innerRect = rect();
innerRect.shrink(6, 6); 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); int firstVisibleChar = max((int)m_cursorPosition - (int)maxCharsToPaint, 0);
unsigned charsToPaint = min(m_text.length() - firstVisibleChar, maxCharsToPaint); 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) { for (unsigned i = 0; i < charsToPaint; ++i) {
char ch = m_text[firstVisibleChar + i]; char ch = m_text[firstVisibleChar + i];
if (ch == ' ') if (ch == ' ')
continue; continue;
int x = innerRect.x() + (i * font.glyphWidth()); int x = innerRect.x() + (i * font().glyphWidth());
auto* bitmap = font.glyphBitmap(ch); auto* bitmap = font().glyphBitmap(ch);
if (!bitmap) { if (!bitmap) {
printf("TextBox: glyph missing: %02x\n", ch); printf("TextBox: glyph missing: %02x\n", ch);
ASSERT_NOT_REACHED(); ASSERT_NOT_REACHED();
@ -58,7 +56,7 @@ void TextBox::paintEvent(PaintEvent&)
if (isFocused() && m_cursorBlinkState) { if (isFocused() && m_cursorBlinkState) {
unsigned visibleCursorPosition = m_cursorPosition - firstVisibleChar; 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()); painter.fillRect(cursorRect, foregroundColor());
} }
} }

View file

@ -9,6 +9,7 @@
Widget::Widget(Widget* parent) Widget::Widget(Widget* parent)
: Object(parent) : Object(parent)
{ {
setFont(nullptr);
m_backgroundColor = Color::White; m_backgroundColor = Color::White;
m_foregroundColor = Color::Black; m_foregroundColor = Color::Black;
} }
@ -146,3 +147,11 @@ void Widget::setFocus(bool focus)
if (auto* win = window()) if (auto* win = window())
win->setFocusedWidget(this); win->setFocusedWidget(this);
} }
void Widget::setFont(RetainPtr<Font>&& font)
{
if (!font)
m_font = Font::defaultFont();
else
m_font = std::move(font);
}

View file

@ -4,6 +4,7 @@
#include "Object.h" #include "Object.h"
#include "Rect.h" #include "Rect.h"
#include "Color.h" #include "Color.h"
#include "Font.h"
#include <AK/String.h> #include <AK/String.h>
#include <functional> #include <functional>
@ -79,12 +80,16 @@ public:
void setFillWithBackgroundColor(bool b) { m_fillWithBackgroundColor = b; } void setFillWithBackgroundColor(bool b) { m_fillWithBackgroundColor = b; }
bool fillWithBackgroundColor() const { return m_fillWithBackgroundColor; } bool fillWithBackgroundColor() const { return m_fillWithBackgroundColor; }
const Font& font() const { return *m_font; }
void setFont(RetainPtr<Font>&&);
private: private:
Window* m_window { nullptr }; Window* m_window { nullptr };
Rect m_relativeRect; Rect m_relativeRect;
Color m_backgroundColor; Color m_backgroundColor;
Color m_foregroundColor; Color m_foregroundColor;
RetainPtr<Font> m_font;
bool m_hasPendingPaintEvent { false }; bool m_hasPendingPaintEvent { false };
bool m_fillWithBackgroundColor { false }; bool m_fillWithBackgroundColor { false };