TextWidget.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #include "TextWidget.h"
  2. #include <AK/String.h>
  3. #include <AK/StringBuilder.h>
  4. #include <AK/Vector.h>
  5. #include <LibDraw/Palette.h>
  6. #include <LibGUI/GPainter.h>
  7. TextWidget::TextWidget(GWidget* parent)
  8. : GFrame(parent)
  9. {
  10. }
  11. TextWidget::TextWidget(const StringView& text, GWidget* parent)
  12. : GFrame(parent)
  13. , m_text(text)
  14. {
  15. }
  16. TextWidget::~TextWidget()
  17. {
  18. }
  19. void TextWidget::set_text(const StringView& text)
  20. {
  21. if (text == m_text)
  22. return;
  23. m_text = text;
  24. wrap_and_set_height();
  25. update();
  26. }
  27. void TextWidget::paint_event(GPaintEvent& event)
  28. {
  29. GFrame::paint_event(event);
  30. GPainter painter(*this);
  31. painter.add_clip_rect(event.rect());
  32. int indent = 0;
  33. if (frame_thickness() > 0)
  34. indent = font().glyph_width('x') / 2;
  35. for (int i = 0; i < m_lines.size(); i++) {
  36. auto& line = m_lines[i];
  37. auto text_rect = frame_inner_rect();
  38. text_rect.move_by(indent, i * m_line_height);
  39. if (!line.is_empty())
  40. text_rect.set_width(text_rect.width() - indent * 2);
  41. if (is_enabled()) {
  42. painter.draw_text(text_rect, line, m_text_alignment, palette().color(foreground_role()), TextElision::None);
  43. } else {
  44. painter.draw_text(text_rect.translated(1, 1), line, font(), text_alignment(), Color::White, TextElision::Right);
  45. painter.draw_text(text_rect, line, font(), text_alignment(), Color::from_rgb(0x808080), TextElision::Right);
  46. }
  47. }
  48. }
  49. void TextWidget::resize_event(GResizeEvent& event)
  50. {
  51. wrap_and_set_height();
  52. GWidget::resize_event(event);
  53. }
  54. void TextWidget::wrap_and_set_height()
  55. {
  56. Vector<String> words;
  57. Optional<size_t> start;
  58. for (size_t i = 0; i < m_text.length(); i++) {
  59. auto ch = m_text[i];
  60. if (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n') {
  61. if (start.has_value())
  62. words.append(m_text.substring(start.value(), i - start.value()));
  63. start = -1;
  64. } else if (!start.has_value()) {
  65. start = i;
  66. }
  67. }
  68. if (start.has_value())
  69. words.append(m_text.substring(start, m_text.length() - start.value()));
  70. auto rect = frame_inner_rect();
  71. if (frame_thickness() > 0)
  72. rect.set_width(rect.width() - font().glyph_width('x'));
  73. StringBuilder builder;
  74. Vector<String> lines;
  75. int line_width = 0;
  76. for (auto& word : words) {
  77. int word_width = font().width(word);
  78. if (line_width != 0)
  79. word_width += font().glyph_width('x');
  80. if (line_width + word_width > rect.width()) {
  81. lines.append(builder.to_string());
  82. line_width = 0;
  83. }
  84. if (line_width != 0)
  85. builder.append(' ');
  86. builder.append(word);
  87. line_width += word_width;
  88. }
  89. auto last_line = builder.to_string();
  90. if (!last_line.is_empty()) {
  91. lines.append(last_line);
  92. }
  93. m_lines = lines;
  94. set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  95. set_preferred_size(0, m_lines.size() * m_line_height + frame_thickness() * 2);
  96. }