1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2026-01-19 14:11:01 +00:00
serenity/Widgets/ListBox.cpp
2019-01-12 17:02:54 +01:00

67 lines
1.7 KiB
C++

#include "ListBox.h"
#include "Painter.h"
#include "Font.h"
#include "Window.h"
ListBox::ListBox(Widget* parent)
: Widget(parent)
{
}
ListBox::~ListBox()
{
}
unsigned ListBox::itemHeight() const
{
return font().glyphHeight() + 2;
}
void ListBox::paintEvent(PaintEvent&)
{
Painter painter(*this);
// FIXME: Reduce overdraw.
painter.fill_rect(rect(), Color::White);
painter.draw_rect(rect(), Color::Black);
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());
Rect textRect(itemRect.x() + 1, itemRect.y() + 1, itemRect.width() - 2, itemRect.height() - 2);
Color itemTextColor = foregroundColor();
if (m_selectedIndex == i) {
if (isFocused())
painter.fill_rect(itemRect, Color(0, 32, 128));
else
painter.fill_rect(itemRect, Color(96, 96, 96));
itemTextColor = Color::White;
}
painter.draw_text(textRect, m_items[i], Painter::TextAlignment::TopLeft, itemTextColor);
}
}
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());
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(move(item));
if (m_selectedIndex == -1)
m_selectedIndex = 0;
}