Label.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGUI/Label.h>
  7. #include <LibGUI/Painter.h>
  8. #include <LibGfx/Bitmap.h>
  9. #include <LibGfx/Font.h>
  10. #include <LibGfx/Palette.h>
  11. REGISTER_WIDGET(GUI, Label)
  12. namespace GUI {
  13. Label::Label(String text)
  14. : m_text(move(text))
  15. {
  16. REGISTER_TEXT_ALIGNMENT_PROPERTY("text_alignment", text_alignment, set_text_alignment);
  17. set_frame_thickness(0);
  18. set_frame_shadow(Gfx::FrameShadow::Plain);
  19. set_frame_shape(Gfx::FrameShape::NoFrame);
  20. set_foreground_role(Gfx::ColorRole::WindowText);
  21. REGISTER_STRING_PROPERTY("text", text, set_text);
  22. REGISTER_BOOL_PROPERTY("autosize", is_autosize, set_autosize);
  23. REGISTER_BOOL_PROPERTY("word_wrap", is_word_wrap, set_word_wrap);
  24. }
  25. Label::~Label()
  26. {
  27. }
  28. void Label::set_autosize(bool autosize)
  29. {
  30. if (m_autosize == autosize)
  31. return;
  32. m_autosize = autosize;
  33. if (m_autosize)
  34. size_to_fit();
  35. }
  36. void Label::set_word_wrap(bool wrap)
  37. {
  38. if (m_word_wrap == wrap)
  39. return;
  40. m_word_wrap = wrap;
  41. if (is_word_wrap())
  42. wrap_text();
  43. }
  44. void Label::set_icon(const Gfx::Bitmap* icon)
  45. {
  46. if (m_icon == icon)
  47. return;
  48. m_icon = icon;
  49. update();
  50. }
  51. void Label::set_text(String text)
  52. {
  53. if (text == m_text)
  54. return;
  55. m_text = move(text);
  56. if (is_word_wrap())
  57. wrap_text();
  58. if (m_autosize)
  59. size_to_fit();
  60. update();
  61. did_change_text();
  62. }
  63. Gfx::IntRect Label::text_rect(size_t line) const
  64. {
  65. int indent = 0;
  66. if (frame_thickness() > 0)
  67. indent = font().glyph_width('x') / 2;
  68. auto rect = frame_inner_rect();
  69. rect.translate_by(indent, line * (font().glyph_height() + 1));
  70. rect.set_width(rect.width() - indent * 2);
  71. return rect;
  72. }
  73. void Label::paint_event(PaintEvent& event)
  74. {
  75. Frame::paint_event(event);
  76. Painter painter(*this);
  77. painter.add_clip_rect(event.rect());
  78. if (m_icon) {
  79. if (m_should_stretch_icon) {
  80. painter.draw_scaled_bitmap(frame_inner_rect(), *m_icon, m_icon->rect());
  81. } else {
  82. auto icon_location = frame_inner_rect().center().translated(-(m_icon->width() / 2), -(m_icon->height() / 2));
  83. painter.blit(icon_location, *m_icon, m_icon->rect());
  84. }
  85. }
  86. if (text().is_empty())
  87. return;
  88. if (is_word_wrap()) {
  89. wrap_text();
  90. for (size_t i = 0; i < m_lines.size(); i++) {
  91. auto& line = m_lines[i];
  92. auto text_rect = this->text_rect(i);
  93. if (is_enabled()) {
  94. painter.draw_text(text_rect, line, m_text_alignment, palette().color(foreground_role()), Gfx::TextElision::None);
  95. } else {
  96. painter.draw_text(text_rect.translated(1, 1), line, font(), text_alignment(), Color::White, Gfx::TextElision::Right);
  97. painter.draw_text(text_rect, line, font(), text_alignment(), Color::from_rgb(0x808080), Gfx::TextElision::Right);
  98. }
  99. }
  100. } else {
  101. auto text_rect = this->text_rect();
  102. if (is_enabled()) {
  103. painter.draw_text(text_rect, text(), m_text_alignment, palette().color(foreground_role()), Gfx::TextElision::Right);
  104. } else {
  105. painter.draw_text(text_rect.translated(1, 1), text(), font(), text_alignment(), Color::White, Gfx::TextElision::Right);
  106. painter.draw_text(text_rect, text(), font(), text_alignment(), Color::from_rgb(0x808080), Gfx::TextElision::Right);
  107. }
  108. }
  109. }
  110. void Label::size_to_fit()
  111. {
  112. set_fixed_width(font().width(m_text));
  113. }
  114. void Label::wrap_text()
  115. {
  116. Vector<String> words;
  117. Optional<size_t> start;
  118. for (size_t i = 0; i < m_text.length(); i++) {
  119. switch (m_text[i]) {
  120. case '\n':
  121. case '\r':
  122. case '\t':
  123. case ' ': {
  124. if (start.has_value())
  125. words.append(m_text.substring(start.value(), i - start.value()));
  126. start.clear();
  127. continue;
  128. }
  129. default: {
  130. if (!start.has_value())
  131. start = i;
  132. }
  133. }
  134. }
  135. if (start.has_value())
  136. words.append(m_text.substring(start.value(), m_text.length() - start.value()));
  137. auto rect = frame_inner_rect();
  138. if (frame_thickness() > 0)
  139. rect.set_width(rect.width() - font().glyph_width('x'));
  140. Vector<String> lines;
  141. StringBuilder builder;
  142. int line_width = 0;
  143. for (auto& word : words) {
  144. int word_width = font().width(word);
  145. if (line_width > 0)
  146. word_width += font().glyph_width('x');
  147. if (line_width + word_width > rect.width()) {
  148. lines.append(builder.to_string());
  149. builder.clear();
  150. line_width = 0;
  151. }
  152. if (line_width > 0)
  153. builder.append(' ');
  154. builder.append(word);
  155. line_width += word_width;
  156. }
  157. auto last_line = builder.to_string();
  158. if (!last_line.is_empty())
  159. lines.append(last_line);
  160. m_lines = lines;
  161. }
  162. }