summaryrefslogtreecommitdiff
path: root/Widgets
diff options
context:
space:
mode:
Diffstat (limited to 'Widgets')
-rw-r--r--Widgets/CheckBox.cpp3
-rw-r--r--Widgets/ListBox.cpp2
-rw-r--r--Widgets/Painter.cpp10
-rw-r--r--Widgets/Painter.h4
-rw-r--r--Widgets/TerminalWidget.cpp12
-rw-r--r--Widgets/TextBox.cpp12
-rw-r--r--Widgets/Widget.cpp9
-rw-r--r--Widgets/Widget.h5
8 files changed, 32 insertions, 25 deletions
diff --git a/Widgets/CheckBox.cpp b/Widgets/CheckBox.cpp
index 372f1d208e..0a2a34a24e 100644
--- a/Widgets/CheckBox.cpp
+++ b/Widgets/CheckBox.cpp
@@ -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);
diff --git a/Widgets/ListBox.cpp b/Widgets/ListBox.cpp
index 53d146fa88..4e3d0a6627 100644
--- a/Widgets/ListBox.cpp
+++ b/Widgets/ListBox.cpp
@@ -14,7 +14,7 @@ ListBox::~ListBox()
unsigned ListBox::itemHeight() const
{
- return Font::defaultFont().glyphHeight() + 2;
+ return font().glyphHeight() + 2;
}
void ListBox::paintEvent(PaintEvent&)
diff --git a/Widgets/Painter.cpp b/Widgets/Painter.cpp
index 8a5f4a1d00..e505e9d541 100644
--- a/Widgets/Painter.cpp
+++ b/Widgets/Painter.cpp
@@ -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);
}
diff --git a/Widgets/Painter.h b/Widgets/Painter.h
index 248887de7c..cbaeda2612 100644
--- a/Widgets/Painter.h
+++ b/Widgets/Painter.h
@@ -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;
diff --git a/Widgets/TerminalWidget.cpp b/Widgets/TerminalWidget.cpp
index 729cf16b16..56782eba37 100644
--- a/Widgets/TerminalWidget.cpp
+++ b/Widgets/TerminalWidget.cpp
@@ -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));
}
}
diff --git a/Widgets/TextBox.cpp b/Widgets/TextBox.cpp
index 8107385f28..291ff83175 100644
--- a/Widgets/TextBox.cpp
+++ b/Widgets/TextBox.cpp
@@ -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());
}
}
diff --git a/Widgets/Widget.cpp b/Widgets/Widget.cpp
index e002834b3f..97c860a8a8 100644
--- a/Widgets/Widget.cpp
+++ b/Widgets/Widget.cpp
@@ -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);
+}
diff --git a/Widgets/Widget.h b/Widgets/Widget.h
index c0e9b428f2..2f754f044b 100644
--- a/Widgets/Widget.h
+++ b/Widgets/Widget.h
@@ -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 };