summaryrefslogtreecommitdiff
path: root/src/gui/widgets/browserbox.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/gui/widgets/browserbox.cpp')
-rw-r--r--src/gui/widgets/browserbox.cpp158
1 files changed, 81 insertions, 77 deletions
diff --git a/src/gui/widgets/browserbox.cpp b/src/gui/widgets/browserbox.cpp
index 91366720..bf337a0f 100644
--- a/src/gui/widgets/browserbox.cpp
+++ b/src/gui/widgets/browserbox.cpp
@@ -23,7 +23,6 @@
#include "gui/widgets/browserbox.h"
#include "keyboardconfig.h"
-#include "textrenderer.h"
#include "gui/gui.h"
#include "gui/truetypefont.h"
@@ -73,9 +72,12 @@ static void replaceKeys(std::string &text)
}
}
+
struct LayoutContext
{
- LayoutContext(gcn::Font *font);
+ LayoutContext(gcn::Font *font, const Palette &palette);
+
+ LinePart linePart(int x, std::string text);
int y = 0;
gcn::Font *font;
@@ -83,23 +85,39 @@ struct LayoutContext
const int minusWidth;
const int tildeWidth;
int lineHeight;
- gcn::Color selColor;
const gcn::Color textColor;
+ const std::optional<gcn::Color> textOutlineColor;
+ gcn::Color color;
+ std::optional<gcn::Color> outlineColor;
};
-LayoutContext::LayoutContext(gcn::Font *font)
+inline LayoutContext::LayoutContext(gcn::Font *font, const Palette &palette)
: font(font)
, fontHeight(font->getHeight())
, minusWidth(font->getWidth("-"))
, tildeWidth(font->getWidth("~"))
, lineHeight(fontHeight)
- , selColor(Theme::getThemeColor(Theme::TEXT))
- , textColor(Theme::getThemeColor(Theme::TEXT))
+ , textColor(palette.getColor(Theme::TEXT))
+ , textOutlineColor(palette.getOutlineColor(Theme::TEXT))
+ , color(textColor)
+ , outlineColor(textOutlineColor)
{
if (auto *trueTypeFont = dynamic_cast<const TrueTypeFont*>(font))
lineHeight = trueTypeFont->getLineHeight();
}
+inline LinePart LayoutContext::linePart(int x, std::string text)
+{
+ return {
+ x,
+ y,
+ color,
+ outlineColor,
+ std::move(text),
+ font
+ };
+}
+
BrowserBox::BrowserBox(Mode mode):
mMode(mode)
@@ -175,7 +193,7 @@ void BrowserBox::addRow(std::string_view row)
replaceKeys(newRow.text);
// Layout the newly added row
- LayoutContext context(getFont());
+ LayoutContext context(getFont(), gui->getTheme()->getPalette(mPalette));
context.y = getHeight();
layoutTextRow(newRow, context);
@@ -209,7 +227,7 @@ void BrowserBox::addRow(std::string_view row)
void BrowserBox::clearRows()
{
mTextRows.clear();
- setSize(0, 0);
+ setSize(mMode == AUTO_SIZE ? 0 : getWidth(), 0);
mHoveredLink.reset();
maybeRelayoutText();
}
@@ -252,19 +270,20 @@ void BrowserBox::draw(gcn::Graphics *graphics)
if (mHoveredLink)
{
+ auto &palette = gui->getTheme()->getPalette(mPalette);
auto &link = *mHoveredLink;
const gcn::Rectangle &rect = link.rect;
if (mHighlightMode & BACKGROUND)
{
- graphics->setColor(Theme::getThemeColor(Theme::HIGHLIGHT));
+ graphics->setColor(palette.getColor(Theme::HIGHLIGHT));
graphics->fillRectangle(rect);
}
if (mHighlightMode & UNDERLINE)
{
- graphics->setColor(Theme::getThemeColor(Theme::HYPERLINK));
+ graphics->setColor(palette.getColor(Theme::HYPERLINK));
graphics->drawLine(rect.x,
rect.y + rect.height,
rect.x + rect.width,
@@ -272,6 +291,8 @@ void BrowserBox::draw(gcn::Graphics *graphics)
}
}
+ auto g = static_cast<Graphics*>(graphics);
+
for (const auto &row : mTextRows)
{
for (const auto &part : row.parts)
@@ -281,15 +302,15 @@ void BrowserBox::draw(gcn::Graphics *graphics)
if (part.y > yEnd)
return;
- TextRenderer::renderText(graphics,
- part.text,
- part.x,
- part.y,
- Graphics::LEFT,
- part.color,
- part.font,
- mOutline,
- mShadows);
+ g->drawText(part.text,
+ part.x,
+ part.y,
+ Graphics::LEFT,
+ part.color,
+ part.font,
+ part.outlineColor.has_value() || mOutline,
+ mShadows,
+ part.outlineColor);
}
}
}
@@ -299,7 +320,7 @@ void BrowserBox::draw(gcn::Graphics *graphics)
*/
void BrowserBox::relayoutText()
{
- LayoutContext context(getFont());
+ LayoutContext context(getFont(), gui->getTheme()->getPalette(mPalette));
for (auto &row : mTextRows)
layoutTextRow(row, context);
@@ -317,7 +338,8 @@ void BrowserBox::layoutTextRow(TextRow &row, LayoutContext &context)
{
// each line starts with normal font in default color
context.font = getFont();
- context.selColor = context.textColor;
+ context.color = context.textColor;
+ context.outlineColor = context.textOutlineColor;
const int startY = context.y;
row.parts.clear();
@@ -330,15 +352,7 @@ void BrowserBox::layoutTextRow(TextRow &row, LayoutContext &context)
if (startsWith(row.text, "---"))
{
for (x = 0; x < getWidth(); x += context.minusWidth - 1)
- {
- row.parts.push_back(LinePart {
- x,
- context.y,
- context.selColor,
- "-",
- context.font
- });
- }
+ row.parts.push_back(context.linePart(x, "-"));
context.y += row.height;
@@ -347,7 +361,9 @@ void BrowserBox::layoutTextRow(TextRow &row, LayoutContext &context)
return;
}
- gcn::Color prevColor = context.selColor;
+ auto &palette = gui->getTheme()->getPalette(mPalette);
+ auto prevColor = context.color;
+ auto prevOutlineColor = context.outlineColor;
// TODO: Check if we must take texture size limits into account here
// TODO: Check if some of the O(n) calls can be removed
@@ -371,44 +387,38 @@ void BrowserBox::layoutTextRow(TextRow &row, LayoutContext &context)
const char c = row.text.at(start + 2);
start += 3;
- bool valid;
- const gcn::Color &col = Theme::getThemeColor(c, valid);
-
- if (c == '>')
- {
- context.selColor = prevColor;
- }
- else if (c == '<')
- {
- prevColor = context.selColor;
- context.selColor = col;
- }
- else if (c == 'B')
- {
- context.font = boldFont;
- }
- else if (c == 'b')
+ switch (c)
{
- context.font = getFont();
- }
- else if (valid)
- {
- context.selColor = col;
- }
- else switch (c)
- {
- case '1': context.selColor = RED; break;
- case '2': context.selColor = GREEN; break;
- case '3': context.selColor = BLUE; break;
- case '4': context.selColor = ORANGE; break;
- case '5': context.selColor = YELLOW; break;
- case '6': context.selColor = PINK; break;
- case '7': context.selColor = PURPLE; break;
- case '8': context.selColor = GRAY; break;
- case '9': context.selColor = BROWN; break;
- case '0':
- default:
- context.selColor = context.textColor;
+ case '>':
+ context.color = prevColor;
+ context.outlineColor = prevOutlineColor;
+ break;
+ case '<':
+ prevColor = context.color;
+ prevOutlineColor = context.outlineColor;
+ context.color = palette.getColor(Theme::HYPERLINK);
+ context.outlineColor = palette.getOutlineColor(Theme::HYPERLINK);
+ break;
+ case 'B':
+ context.font = boldFont;
+ break;
+ case 'b':
+ context.font = getFont();
+ break;
+ default: {
+ const auto colorId = Theme::getColorIdForChar(c);
+ if (colorId)
+ {
+ context.color = palette.getColor(*colorId);
+ context.outlineColor = palette.getOutlineColor(*colorId);
+ }
+ else
+ {
+ context.color = context.textColor;
+ context.outlineColor = context.textOutlineColor;
+ }
+ break;
+ }
}
// Update the position of the links
@@ -481,7 +491,8 @@ void BrowserBox::layoutTextRow(TextRow &row, LayoutContext &context)
row.parts.push_back(LinePart {
getWidth() - context.tildeWidth,
context.y,
- context.selColor,
+ context.color,
+ context.outlineColor,
"~",
getFont()
});
@@ -495,14 +506,7 @@ void BrowserBox::layoutTextRow(TextRow &row, LayoutContext &context)
wrapped = true;
}
- row.parts.push_back(LinePart {
- x,
- context.y,
- context.selColor,
- std::move(part),
- context.font
- });
-
+ row.parts.push_back(context.linePart(x, std::move(part)));
row.width = std::max(row.width, x + partWidth);
if (mMode == AUTO_WRAP && partWidth == 0)