TextWidget.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "TextWidget.h"
  27. #include <AK/Optional.h>
  28. #include <AK/String.h>
  29. #include <AK/StringBuilder.h>
  30. #include <AK/Vector.h>
  31. #include <LibGUI/Painter.h>
  32. #include <LibGfx/Font.h>
  33. #include <LibGfx/Palette.h>
  34. TextWidget::TextWidget(const StringView& text)
  35. : m_text(text)
  36. {
  37. }
  38. TextWidget::~TextWidget()
  39. {
  40. }
  41. void TextWidget::set_text(const StringView& text)
  42. {
  43. if (text == m_text)
  44. return;
  45. m_text = text;
  46. wrap_and_set_height();
  47. update();
  48. }
  49. void TextWidget::paint_event(GUI::PaintEvent& event)
  50. {
  51. GUI::Frame::paint_event(event);
  52. GUI::Painter painter(*this);
  53. painter.add_clip_rect(event.rect());
  54. int indent = 0;
  55. if (frame_thickness() > 0)
  56. indent = font().glyph_width('x') / 2;
  57. for (size_t i = 0; i < m_lines.size(); i++) {
  58. auto& line = m_lines[i];
  59. auto text_rect = frame_inner_rect();
  60. text_rect.move_by(indent, i * m_line_height);
  61. if (!line.is_empty())
  62. text_rect.set_width(text_rect.width() - indent * 2);
  63. if (is_enabled()) {
  64. painter.draw_text(text_rect, line, m_text_alignment, palette().color(foreground_role()), Gfx::TextElision::None);
  65. } else {
  66. painter.draw_text(text_rect.translated(1, 1), line, font(), text_alignment(), Color::White, Gfx::TextElision::Right);
  67. painter.draw_text(text_rect, line, font(), text_alignment(), Color::from_rgb(0x808080), Gfx::TextElision::Right);
  68. }
  69. }
  70. }
  71. void TextWidget::resize_event(GUI::ResizeEvent& event)
  72. {
  73. wrap_and_set_height();
  74. GUI::Widget::resize_event(event);
  75. }
  76. void TextWidget::wrap_and_set_height()
  77. {
  78. Vector<String> words;
  79. Optional<size_t> start;
  80. for (size_t i = 0; i < m_text.length(); i++) {
  81. auto ch = m_text[i];
  82. if (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n') {
  83. if (start.has_value())
  84. words.append(m_text.substring(start.value(), i - start.value()));
  85. start.clear();
  86. } else if (!start.has_value()) {
  87. start = i;
  88. }
  89. }
  90. if (start.has_value())
  91. words.append(m_text.substring(start.value(), m_text.length() - start.value()));
  92. auto rect = frame_inner_rect();
  93. if (frame_thickness() > 0)
  94. rect.set_width(rect.width() - font().glyph_width('x'));
  95. StringBuilder builder;
  96. Vector<String> lines;
  97. int line_width = 0;
  98. for (auto& word : words) {
  99. int word_width = font().width(word);
  100. if (line_width != 0)
  101. word_width += font().glyph_width('x');
  102. if (line_width + word_width > rect.width()) {
  103. lines.append(builder.to_string());
  104. builder.clear();
  105. line_width = 0;
  106. }
  107. if (line_width != 0)
  108. builder.append(' ');
  109. builder.append(word);
  110. line_width += word_width;
  111. }
  112. auto last_line = builder.to_string();
  113. if (!last_line.is_empty()) {
  114. lines.append(last_line);
  115. }
  116. m_lines = lines;
  117. set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  118. set_preferred_size(0, m_lines.size() * m_line_height + frame_thickness() * 2);
  119. }