| 1 | // Aseprite UI Library |
|---|---|
| 2 | // Copyright (C) 2019-2022 Igara Studio S.A. |
| 3 | // Copyright (C) 2001-2017 David Capello |
| 4 | // |
| 5 | // This file is released under the terms of the MIT license. |
| 6 | // Read LICENSE.txt for more information. |
| 7 | |
| 8 | #ifdef HAVE_CONFIG_H |
| 9 | #include "config.h" |
| 10 | #endif |
| 11 | |
| 12 | #include "ui/listitem.h" |
| 13 | |
| 14 | #include "ui/message.h" |
| 15 | #include "ui/size_hint_event.h" |
| 16 | #include "ui/resize_event.h" |
| 17 | #include "ui/theme.h" |
| 18 | #include "ui/view.h" |
| 19 | |
| 20 | #include <algorithm> |
| 21 | |
| 22 | namespace ui { |
| 23 | |
| 24 | using namespace gfx; |
| 25 | |
| 26 | ListItem::ListItem(const std::string& text) |
| 27 | : Widget(kListItemWidget) |
| 28 | { |
| 29 | setDoubleBuffered(true); |
| 30 | setAlign(LEFT | MIDDLE); |
| 31 | setText(text); |
| 32 | initTheme(); |
| 33 | } |
| 34 | |
| 35 | bool ListItem::onProcessMessage(Message* msg) |
| 36 | { |
| 37 | switch (msg->type()) { |
| 38 | case kDoubleClickMessage: |
| 39 | // Propagate the message to the parent. |
| 40 | if (parent()) |
| 41 | return parent()->sendMessage(msg); |
| 42 | else |
| 43 | break; |
| 44 | break; |
| 45 | } |
| 46 | return Widget::onProcessMessage(msg); |
| 47 | } |
| 48 | |
| 49 | void ListItem::onResize(ResizeEvent& ev) |
| 50 | { |
| 51 | setBoundsQuietly(ev.bounds()); |
| 52 | |
| 53 | Rect crect = childrenBounds(); |
| 54 | for (auto child : children()) |
| 55 | child->setBounds(crect); |
| 56 | } |
| 57 | |
| 58 | void ListItem::onSizeHint(SizeHintEvent& ev) |
| 59 | { |
| 60 | int w = 0, h = 0; |
| 61 | Size maxSize; |
| 62 | |
| 63 | if (hasText()) { |
| 64 | if (m_textLength >= 0) { |
| 65 | maxSize.w = m_textLength; |
| 66 | maxSize.h = textHeight(); |
| 67 | } |
| 68 | else { |
| 69 | maxSize = textSize(); |
| 70 | m_textLength = maxSize.w; |
| 71 | } |
| 72 | } |
| 73 | else |
| 74 | maxSize.w = maxSize.h = 0; |
| 75 | |
| 76 | for (auto child : children()) { |
| 77 | Size reqSize = child->sizeHint(); |
| 78 | |
| 79 | maxSize.w = std::max(maxSize.w, reqSize.w); |
| 80 | maxSize.h = std::max(maxSize.h, reqSize.h); |
| 81 | } |
| 82 | |
| 83 | w = maxSize.w + border().width(); |
| 84 | h = maxSize.h + border().height(); |
| 85 | |
| 86 | ev.setSizeHint(Size(w, h)); |
| 87 | } |
| 88 | |
| 89 | void ListItem::onInitTheme(InitThemeEvent& ev) |
| 90 | { |
| 91 | Widget::onInitTheme(ev); |
| 92 | m_textLength = -1; |
| 93 | } |
| 94 | |
| 95 | void ListItem::onSetText() |
| 96 | { |
| 97 | Widget::onSetText(); |
| 98 | m_textLength = -1; |
| 99 | } |
| 100 | |
| 101 | } // namespace ui |
| 102 |