1
0
Fork 0

Codechange: Use a helper method to test if text file window is wrapped.

pull/14293/head
Peter Nelson 2025-05-14 18:06:51 +01:00 committed by Peter Nelson
parent 940071a5f2
commit 46b745a06a
2 changed files with 14 additions and 8 deletions

View File

@ -111,7 +111,7 @@ void TextfileWindow::ConstructWindow()
uint TextfileWindow::ReflowContent() uint TextfileWindow::ReflowContent()
{ {
uint height = 0; uint height = 0;
if (!IsWidgetLowered(WID_TF_WRAPTEXT)) { if (!this->IsTextWrapped()) {
for (auto &line : this->lines) { for (auto &line : this->lines) {
line.top = height; line.top = height;
height++; height++;
@ -150,7 +150,7 @@ uint TextfileWindow::GetContentHeight()
/** Set scrollbars to the right lengths. */ /** Set scrollbars to the right lengths. */
void TextfileWindow::SetupScrollbars(bool force_reflow) void TextfileWindow::SetupScrollbars(bool force_reflow)
{ {
if (IsWidgetLowered(WID_TF_WRAPTEXT)) { if (this->IsTextWrapped()) {
/* Reflow is mandatory if text wrapping is on */ /* Reflow is mandatory if text wrapping is on */
uint height = this->ReflowContent(); uint height = this->ReflowContent();
this->vscroll->SetCount(height); this->vscroll->SetCount(height);
@ -161,7 +161,7 @@ void TextfileWindow::SetupScrollbars(bool force_reflow)
this->hscroll->SetCount(this->max_length); this->hscroll->SetCount(this->max_length);
} }
this->SetWidgetDisabledState(WID_TF_HSCROLLBAR, IsWidgetLowered(WID_TF_WRAPTEXT)); this->SetWidgetDisabledState(WID_TF_HSCROLLBAR, this->IsTextWrapped());
} }
@ -310,7 +310,7 @@ const TextfileWindow::Hyperlink *TextfileWindow::GetHyperlink(Point pt) const
const int clicked_row = this->GetRowFromWidget(pt.y, WID_TF_BACKGROUND, WidgetDimensions::scaled.frametext.top, GetCharacterHeight(FS_MONO)) + this->GetScrollbar(WID_TF_VSCROLLBAR)->GetPosition(); const int clicked_row = this->GetRowFromWidget(pt.y, WID_TF_BACKGROUND, WidgetDimensions::scaled.frametext.top, GetCharacterHeight(FS_MONO)) + this->GetScrollbar(WID_TF_VSCROLLBAR)->GetPosition();
size_t line_index; size_t line_index;
size_t subline; size_t subline;
if (IsWidgetLowered(WID_TF_WRAPTEXT)) { if (this->IsTextWrapped()) {
auto it = std::ranges::find_if(this->lines, [clicked_row](const Line &l) { return l.top <= clicked_row && l.bottom > clicked_row; }); auto it = std::ranges::find_if(this->lines, [clicked_row](const Line &l) { return l.top <= clicked_row && l.bottom > clicked_row; });
if (it == this->lines.cend()) return nullptr; if (it == this->lines.cend()) return nullptr;
line_index = it - this->lines.cbegin(); line_index = it - this->lines.cbegin();
@ -329,7 +329,7 @@ const TextfileWindow::Hyperlink *TextfileWindow::GetHyperlink(Point pt) const
if (found_links.empty()) return nullptr; if (found_links.empty()) return nullptr;
/* Build line layout to figure out character position that was clicked. */ /* Build line layout to figure out character position that was clicked. */
uint window_width = IsWidgetLowered(WID_TF_WRAPTEXT) ? this->GetWidget<NWidgetCore>(WID_TF_BACKGROUND)->current_x - WidgetDimensions::scaled.frametext.Horizontal() : INT_MAX; uint window_width = this->IsTextWrapped() ? this->GetWidget<NWidgetCore>(WID_TF_BACKGROUND)->current_x - WidgetDimensions::scaled.frametext.Horizontal() : INT_MAX;
Layouter layout(this->lines[line_index].text, window_width, FS_MONO); Layouter layout(this->lines[line_index].text, window_width, FS_MONO);
assert(subline < layout.size()); assert(subline < layout.size());
ptrdiff_t char_index = layout.GetCharAtPosition(pt.x - WidgetDimensions::scaled.frametext.left, subline); ptrdiff_t char_index = layout.GetCharAtPosition(pt.x - WidgetDimensions::scaled.frametext.left, subline);
@ -582,7 +582,7 @@ void TextfileWindow::AfterLoadMarkdown()
fr = fr.Translate(-fr.left, -fr.top); fr = fr.Translate(-fr.left, -fr.top);
int line_height = GetCharacterHeight(FS_MONO); int line_height = GetCharacterHeight(FS_MONO);
if (!IsWidgetLowered(WID_TF_WRAPTEXT)) fr = ScrollRect(fr, *this->hscroll, 1); if (!this->IsTextWrapped()) fr = ScrollRect(fr, *this->hscroll, 1);
int pos = this->vscroll->GetPosition(); int pos = this->vscroll->GetPosition();
int cap = this->vscroll->GetCapacity(); int cap = this->vscroll->GetCapacity();
@ -592,7 +592,7 @@ void TextfileWindow::AfterLoadMarkdown()
if (line.top > pos + cap) break; if (line.top > pos + cap) break;
int y_offset = (line.top - pos) * line_height; int y_offset = (line.top - pos) * line_height;
if (IsWidgetLowered(WID_TF_WRAPTEXT)) { if (this->IsTextWrapped()) {
DrawStringMultiLineWithClipping(fr.left, fr.right, y_offset, y_offset + (line.bottom - line.top) * line_height, line.text, line.colour, SA_TOP | SA_LEFT, false, FS_MONO); DrawStringMultiLineWithClipping(fr.left, fr.right, y_offset, y_offset + (line.bottom - line.top) * line_height, line.text, line.colour, SA_TOP | SA_LEFT, false, FS_MONO);
} else { } else {
DrawString(fr.left, fr.right, y_offset, line.text, line.colour, SA_TOP | SA_LEFT, false, FS_MONO); DrawString(fr.left, fr.right, y_offset, line.text, line.colour, SA_TOP | SA_LEFT, false, FS_MONO);
@ -626,7 +626,7 @@ void TextfileWindow::ScrollToLine(size_t line)
{ {
Scrollbar *sb = this->GetScrollbar(WID_TF_VSCROLLBAR); Scrollbar *sb = this->GetScrollbar(WID_TF_VSCROLLBAR);
int newpos; int newpos;
if (this->IsWidgetLowered(WID_TF_WRAPTEXT)) { if (this->IsTextWrapped()) {
newpos = this->lines[line].top; newpos = this->lines[line].top;
} else { } else {
newpos = static_cast<int>(line); newpos = static_cast<int>(line);
@ -635,6 +635,11 @@ void TextfileWindow::ScrollToLine(size_t line)
this->SetDirty(); this->SetDirty();
} }
bool TextfileWindow::IsTextWrapped() const
{
return this->IsWidgetLowered(WID_TF_WRAPTEXT);
}
/* virtual */ void TextfileWindow::Reset() /* virtual */ void TextfileWindow::Reset()
{ {
this->search_iterator = 0; this->search_iterator = 0;

View File

@ -37,6 +37,7 @@ struct TextfileWindow : public Window, MissingGlyphSearcher {
bool Monospace() override; bool Monospace() override;
void SetFontNames(FontCacheSettings *settings, std::string_view font_name, const void *os_data) override; void SetFontNames(FontCacheSettings *settings, std::string_view font_name, const void *os_data) override;
void ScrollToLine(size_t line); void ScrollToLine(size_t line);
bool IsTextWrapped() const;
virtual void LoadTextfile(const std::string &textfile, Subdirectory dir); virtual void LoadTextfile(const std::string &textfile, Subdirectory dir);