diff options
Diffstat (limited to 'src/gui')
-rw-r--r-- | src/gui/widgets/listbox.cpp | 60 |
1 files changed, 45 insertions, 15 deletions
diff --git a/src/gui/widgets/listbox.cpp b/src/gui/widgets/listbox.cpp index 112de232..09f0036b 100644 --- a/src/gui/widgets/listbox.cpp +++ b/src/gui/widgets/listbox.cpp @@ -43,27 +43,57 @@ void ListBox::draw(gcn::Graphics *graphics) graphics->setFont(getFont()); - const int height = getRowHeight(); + const int boxWidth = getWidth() - 4; + gcn::Font *font = getFont(); + graphics->setColor(Theme::getThemeColor(Theme::TEXT)); - // Draw filled rectangle around the selected list element - if (mSelected >= 0) - { - auto highlightColor = Theme::getThemeColor(Theme::HIGHLIGHT); - highlightColor.a = gui->getTheme()->getGuiAlpha(); - graphics->setColor(highlightColor); - graphics->fillRectangle(gcn::Rectangle(0, height * mSelected, - getWidth(), height)); - } + int y = 0; - // Draw the list elements - graphics->setColor(Theme::getThemeColor(Theme::TEXT)); - for (int i = 0, y = 0; i < mListModel->getNumberOfElements(); - ++i, y += height) + for (int i = 0; i < mListModel->getNumberOfElements(); ++i) { - graphics->drawText(mListModel->getElementAt(i), 1, y); + std::string text = mListModel->getElementAt(i); + + // Word wrap + std::vector<std::string> lines; + std::istringstream stream(text); + std::string word, currentLine; + + while (stream >> word) + { + std::string testLine = currentLine.empty() ? word : currentLine + " " + word; + if (font->getWidth(testLine) < boxWidth) + { + currentLine = testLine; + } + else + { + lines.push_back(currentLine); + currentLine = word; + } + } + if (!currentLine.empty()) + lines.push_back(currentLine); + + // Highlight if selected + if (mSelected == i) + { + auto highlightColor = Theme::getThemeColor(Theme::HIGHLIGHT); + highlightColor.a = gui->getTheme()->getGuiAlpha(); + graphics->setColor(highlightColor); + graphics->fillRectangle(gcn::Rectangle(0, y, getWidth(), lines.size() * getRowHeight())); + graphics->setColor(Theme::getThemeColor(Theme::TEXT)); // Reset color + } + + // Draw each line + for (const std::string &line : lines) + { + graphics->drawText(line, 1, y); + y += getRowHeight(); + } } } + void ListBox::keyPressed(gcn::KeyEvent& keyEvent) { const gcn::Key key = keyEvent.getKey(); |