diff options
Diffstat (limited to 'Widgets')
-rw-r--r-- | Widgets/ListBox.cpp | 58 | ||||
-rw-r--r-- | Widgets/ListBox.h | 24 | ||||
-rw-r--r-- | Widgets/Makefile | 1 | ||||
-rw-r--r-- | Widgets/Window.cpp | 2 | ||||
-rw-r--r-- | Widgets/test.cpp | 10 |
5 files changed, 93 insertions, 2 deletions
diff --git a/Widgets/ListBox.cpp b/Widgets/ListBox.cpp new file mode 100644 index 0000000000..4f067e75f4 --- /dev/null +++ b/Widgets/ListBox.cpp @@ -0,0 +1,58 @@ +#include "ListBox.h" +#include "Painter.h" +#include "Font.h" + +ListBox::ListBox(Widget* parent) + : Widget(parent) +{ +} + +ListBox::~ListBox() +{ +} + +unsigned ListBox::itemHeight() const +{ + return Font::defaultFont().glyphHeight() + 2; +} + +void ListBox::onPaint(PaintEvent&) +{ + Painter painter(*this); + painter.fillRect(rect(), Color::White); + painter.drawRect(rect(), Color::Black); + + for (unsigned i = m_scrollOffset; i < m_items.size(); ++i) { + Rect itemRect(1, 1 + (i * itemHeight()), width() - 2, itemHeight()); + Rect textRect(itemRect.x() + 1, itemRect.y() + 1, itemRect.width() - 2, itemRect.height() - 2); + + Color itemTextColor = foregroundColor(); + if (m_selectedIndex == i) { + painter.fillRect(itemRect, Color(0, 32, 128)); + itemTextColor = Color::White; + } + painter.drawText(textRect, m_items[i], Painter::TextAlignment::TopLeft, itemTextColor); + } +} + +void ListBox::onMouseDown(MouseEvent& event) +{ + printf("ListBox::onMouseDown %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()); + if (itemRect.contains(event.position())) { + m_selectedIndex = i; + printf("ListBox: selected item %u (\"%s\")\n", i, m_items[i].characters()); + update(); + return; + } + } +} + +void ListBox::addItem(String&& item) +{ + m_items.append(std::move(item)); + if (m_selectedIndex == -1) + m_selectedIndex = 0; +} + diff --git a/Widgets/ListBox.h b/Widgets/ListBox.h new file mode 100644 index 0000000000..fe90f68ed2 --- /dev/null +++ b/Widgets/ListBox.h @@ -0,0 +1,24 @@ +#pragma once + +#include "Widget.h" + +class ListBox final : public Widget { +public: + explicit ListBox(Widget* parent); + virtual ~ListBox() override; + + void addItem(String&&); + int selectedIndex() const { return m_selectedIndex; } + +private: + virtual void onPaint(PaintEvent&) override; + virtual void onMouseDown(MouseEvent&) override; + + unsigned itemHeight() const; + + unsigned m_scrollOffset { 0 }; + int m_selectedIndex { -1 }; + + Vector<String> m_items; +}; + diff --git a/Widgets/Makefile b/Widgets/Makefile index 33778a71e8..31759e668c 100644 --- a/Widgets/Makefile +++ b/Widgets/Makefile @@ -25,6 +25,7 @@ VFS_OBJS = \ ClockWidget.o \ CBitmap.o \ CheckBox.o \ + ListBox.o \ test.o OBJS = $(AK_OBJS) $(VFS_OBJS) diff --git a/Widgets/Window.cpp b/Widgets/Window.cpp index 4848a89c9b..9cd670481e 100644 --- a/Widgets/Window.cpp +++ b/Widgets/Window.cpp @@ -50,7 +50,7 @@ void Window::event(Event& event) { if (event.isMouseEvent()) { auto& me = static_cast<MouseEvent&>(event); - printf("Window{%p}: %s %d,%d\n", this, me.name(), me.x(), me.y()); + //printf("Window{%p}: %s %d,%d\n", this, me.name(), me.x(), me.y()); if (m_mainWidget) { auto result = m_mainWidget->hitTest(me.x(), me.y()); //printf("hit test for %d,%d found: %s{%p} %d,%d\n", me.x(), me.y(), result.widget->className(), result.widget, result.localX, result.localY); diff --git a/Widgets/test.cpp b/Widgets/test.cpp index f27ced63e4..02e7f9c2fb 100644 --- a/Widgets/test.cpp +++ b/Widgets/test.cpp @@ -8,6 +8,7 @@ #include "Window.h" #include "ClockWidget.h" #include "CheckBox.h" +#include "ListBox.h" #include <cstdio> int main(int argc, char** argv) @@ -47,7 +48,7 @@ int main(int argc, char** argv) { auto* widgetTestWindow = new Window; widgetTestWindow->setTitle("Widget test"); - widgetTestWindow->setRect({ 20, 40, 100, 60 }); + widgetTestWindow->setRect({ 20, 40, 100, 160 }); auto* widgetTestWindowWidget = new Widget; widgetTestWindowWidget->setWindowRelativeRect({ 0, 0, 100, 100 }); @@ -64,6 +65,13 @@ int main(int argc, char** argv) auto* c = new CheckBox(widgetTestWindowWidget); c->setWindowRelativeRect({ 0, 40, 100, 20 }); c->setCaption("CheckBox"); + + auto *lb = new ListBox(widgetTestWindowWidget); + lb->setWindowRelativeRect({0, 60, 100, 100 }); + lb->addItem("This"); + lb->addItem("is"); + lb->addItem("a"); + lb->addItem("ListBox"); } auto* win = new Window; |