2019-01-20 04:49:48 +01:00
|
|
|
#include "GListBox.h"
|
2019-01-19 23:49:56 +01:00
|
|
|
#include <SharedGraphics/Font.h>
|
|
|
|
|
#include <SharedGraphics/Painter.h>
|
2018-10-13 00:20:44 +02:00
|
|
|
|
2019-01-20 04:49:48 +01:00
|
|
|
GListBox::GListBox(GWidget* parent)
|
|
|
|
|
: GWidget(parent)
|
2018-10-13 00:20:44 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-20 04:49:48 +01:00
|
|
|
GListBox::~GListBox()
|
2018-10-13 00:20:44 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-20 04:49:48 +01:00
|
|
|
Rect GListBox::item_rect(int index) const
|
2018-10-13 00:20:44 +02:00
|
|
|
{
|
2019-01-16 17:54:06 +01:00
|
|
|
int item_height = font().glyph_height() + 2;
|
2019-01-12 17:29:29 +01:00
|
|
|
return Rect { 2, 2 + (index * item_height), width() - 4, item_height };
|
2018-10-13 00:20:44 +02:00
|
|
|
}
|
|
|
|
|
|
2019-02-28 19:34:55 +01:00
|
|
|
void GListBox::paint_event(GPaintEvent& event)
|
2018-10-13 00:20:44 +02:00
|
|
|
{
|
|
|
|
|
Painter painter(*this);
|
2019-02-28 19:34:55 +01:00
|
|
|
painter.set_clip_rect(event.rect());
|
2018-10-13 20:59:25 +02:00
|
|
|
|
2019-01-28 20:38:13 +01:00
|
|
|
painter.fill_rect({ rect().x() + 1, rect().y() + 1, rect().width() - 2, rect().height() - 2 }, background_color());
|
|
|
|
|
painter.draw_rect(rect(), foreground_color());
|
2018-10-13 00:20:44 +02:00
|
|
|
|
2019-01-21 00:46:08 +01:00
|
|
|
if (is_focused())
|
2019-01-12 17:02:54 +01:00
|
|
|
painter.draw_focus_rect(rect());
|
2018-10-13 20:59:25 +02:00
|
|
|
|
2019-01-21 00:46:08 +01:00
|
|
|
for (int i = m_scroll_offset; i < static_cast<int>(m_items.size()); ++i) {
|
|
|
|
|
auto item_rect = this->item_rect(i);
|
|
|
|
|
Rect text_rect(item_rect.x() + 1, item_rect.y() + 1, item_rect.width() - 2, item_rect.height() - 2);
|
2018-10-13 00:20:44 +02:00
|
|
|
|
2019-01-21 00:46:08 +01:00
|
|
|
Color item_text_color = foreground_color();
|
|
|
|
|
if (m_selected_index == i) {
|
|
|
|
|
if (is_focused())
|
|
|
|
|
painter.fill_rect(item_rect, Color(0, 32, 128));
|
2018-10-13 20:59:25 +02:00
|
|
|
else
|
2019-01-21 00:46:08 +01:00
|
|
|
painter.fill_rect(item_rect, Color(96, 96, 96));
|
|
|
|
|
item_text_color = Color::White;
|
2018-10-13 00:20:44 +02:00
|
|
|
}
|
2019-02-11 10:08:37 +01:00
|
|
|
painter.draw_text(item_rect, m_items[i], TextAlignment::TopLeft, item_text_color);
|
2018-10-13 00:20:44 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-21 00:46:08 +01:00
|
|
|
void GListBox::mousedown_event(GMouseEvent& event)
|
2018-10-13 00:20:44 +02:00
|
|
|
{
|
2019-01-20 04:49:48 +01:00
|
|
|
dbgprintf("GListBox::mouseDownEvent %d,%d\n", event.x(), event.y());
|
2019-01-21 00:46:08 +01:00
|
|
|
for (int i = m_scroll_offset; i < static_cast<int>(m_items.size()); ++i) {
|
|
|
|
|
auto item_rect = this->item_rect(i);
|
|
|
|
|
if (item_rect.contains(event.position())) {
|
|
|
|
|
m_selected_index = i;
|
2019-01-20 04:49:48 +01:00
|
|
|
dbgprintf("GListBox: selected item %u (\"%s\")\n", i, m_items[i].characters());
|
2018-10-13 00:20:44 +02:00
|
|
|
update();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-21 00:46:08 +01:00
|
|
|
void GListBox::add_item(String&& item)
|
2018-10-13 00:20:44 +02:00
|
|
|
{
|
2019-01-11 04:10:07 +01:00
|
|
|
m_items.append(move(item));
|
2019-01-21 00:46:08 +01:00
|
|
|
if (m_selected_index == -1)
|
|
|
|
|
m_selected_index = 0;
|
2018-10-13 00:20:44 +02:00
|
|
|
}
|
|
|
|
|
|