From c0c1c88f9b29e988e0c2960a4d3efad18abefaf8 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 12 Jan 2019 17:29:29 +0100 Subject: [PATCH] ListBox: Fix item rect inconsistency between paints and clicks. --- Widgets/ListBox.cpp | 13 +++++++------ Widgets/ListBox.h | 4 ++-- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/Widgets/ListBox.cpp b/Widgets/ListBox.cpp index b346892c93..d74a69cd0e 100644 --- a/Widgets/ListBox.cpp +++ b/Widgets/ListBox.cpp @@ -12,9 +12,10 @@ ListBox::~ListBox() { } -unsigned ListBox::itemHeight() const +Rect ListBox::item_rect(int index) const { - return font().glyphHeight() + 2; + int item_height = font().glyphHeight() + 2; + return Rect { 2, 2 + (index * item_height), width() - 4, item_height }; } void ListBox::paintEvent(PaintEvent&) @@ -28,8 +29,8 @@ void ListBox::paintEvent(PaintEvent&) if (isFocused()) painter.draw_focus_rect(rect()); - for (unsigned i = m_scrollOffset; i < m_items.size(); ++i) { - Rect itemRect(2, 2 + (i * itemHeight()), width() - 4, itemHeight()); + for (int i = m_scrollOffset; i < static_cast(m_items.size()); ++i) { + auto itemRect = item_rect(i); Rect textRect(itemRect.x() + 1, itemRect.y() + 1, itemRect.width() - 2, itemRect.height() - 2); Color itemTextColor = foregroundColor(); @@ -47,8 +48,8 @@ void ListBox::paintEvent(PaintEvent&) void ListBox::mouseDownEvent(MouseEvent& event) { printf("ListBox::mouseDownEvent %d,%d\n", event.x(), event.y()); - for (unsigned i = m_scrollOffset; i < m_items.size(); ++i) { - Rect itemRect(1, 1 + (i * itemHeight()), width() - 2, itemHeight()); + for (int i = m_scrollOffset; i < static_cast(m_items.size()); ++i) { + auto itemRect = item_rect(i); if (itemRect.contains(event.position())) { m_selectedIndex = i; printf("ListBox: selected item %u (\"%s\")\n", i, m_items[i].characters()); diff --git a/Widgets/ListBox.h b/Widgets/ListBox.h index 1e4125f4d4..ca227f2b54 100644 --- a/Widgets/ListBox.h +++ b/Widgets/ListBox.h @@ -15,9 +15,9 @@ private: virtual void mouseDownEvent(MouseEvent&) override; virtual const char* class_name() const override { return "ListBox"; } - unsigned itemHeight() const; + Rect item_rect(int index) const; - unsigned m_scrollOffset { 0 }; + int m_scrollOffset { 0 }; int m_selectedIndex { -1 }; Vector m_items;