Label.cpp 6.2 KB

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