GTextEditor.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. #include <LibGUI/GTextEditor.h>
  2. #include <LibGUI/GScrollBar.h>
  3. #include <LibGUI/GFontDatabase.h>
  4. #include <SharedGraphics/Painter.h>
  5. #include <Kernel/KeyCode.h>
  6. GTextEditor::GTextEditor(GWidget* parent)
  7. : GWidget(parent)
  8. {
  9. set_font(GFontDatabase::the().get_by_name("Csilla Thin"));
  10. set_fill_with_background_color(false);
  11. m_vertical_scrollbar = new GScrollBar(Orientation::Vertical, this);
  12. m_vertical_scrollbar->set_step(4);
  13. m_vertical_scrollbar->on_change = [this] (int) {
  14. update();
  15. };
  16. m_horizontal_scrollbar = new GScrollBar(Orientation::Horizontal, this);
  17. m_horizontal_scrollbar->set_step(4);
  18. m_horizontal_scrollbar->set_big_step(30);
  19. m_horizontal_scrollbar->on_change = [this] (int) {
  20. update();
  21. };
  22. }
  23. GTextEditor::~GTextEditor()
  24. {
  25. }
  26. void GTextEditor::set_text(const String& text)
  27. {
  28. m_lines.clear();
  29. int start_of_current_line = 0;
  30. auto add_line = [&] (int current_position) {
  31. int line_length = current_position - start_of_current_line;
  32. Line line;
  33. if (line_length)
  34. line.set_text(text.substring(start_of_current_line, current_position - start_of_current_line));
  35. m_lines.append(move(line));
  36. start_of_current_line = current_position + 1;
  37. };
  38. int i = 0;
  39. for (i = 0; i < text.length(); ++i) {
  40. if (text[i] == '\n')
  41. add_line(i);
  42. }
  43. add_line(i);
  44. set_cursor(0, 0);
  45. update();
  46. }
  47. void GTextEditor::resize_event(GResizeEvent& event)
  48. {
  49. update_scrollbar_ranges();
  50. m_vertical_scrollbar->set_relative_rect(event.size().width() - m_vertical_scrollbar->preferred_size().width(), 0, m_vertical_scrollbar->preferred_size().width(), event.size().height() - m_horizontal_scrollbar->preferred_size().height());
  51. m_horizontal_scrollbar->set_relative_rect(0, event.size().height() - m_horizontal_scrollbar->preferred_size().height(), event.size().width() - m_vertical_scrollbar->preferred_size().width(), m_horizontal_scrollbar->preferred_size().height());
  52. }
  53. void GTextEditor::update_scrollbar_ranges()
  54. {
  55. int available_height = height() - m_horizontal_scrollbar->height();
  56. int excess_height = max(0, (line_count() * line_height()) - available_height);
  57. m_vertical_scrollbar->set_range(0, excess_height);
  58. int available_width = width() - m_vertical_scrollbar->width();
  59. int excess_width = max(0, content_width() - available_width);
  60. m_horizontal_scrollbar->set_range(0, excess_width);
  61. m_vertical_scrollbar->set_big_step(visible_content_rect().height());
  62. }
  63. int GTextEditor::content_width() const
  64. {
  65. // FIXME: Cache this somewhere.
  66. int max_width = 0;
  67. for (auto& line : m_lines)
  68. max_width = max(line.width(font()), max_width);
  69. return max_width;
  70. }
  71. void GTextEditor::mousedown_event(GMouseEvent& event)
  72. {
  73. (void)event;
  74. }
  75. void GTextEditor::paint_event(GPaintEvent& event)
  76. {
  77. Painter painter(*this);
  78. painter.set_clip_rect(event.rect());
  79. painter.fill_rect(event.rect(), Color::White);
  80. painter.translate(-m_horizontal_scrollbar->value(), -m_vertical_scrollbar->value());
  81. painter.translate(padding(), padding());
  82. int exposed_width = max(content_width(), width());
  83. for (int i = 0; i < line_count(); ++i) {
  84. auto& line = m_lines[i];
  85. auto line_rect = line_content_rect(i);
  86. line_rect.set_width(exposed_width);
  87. if (i == m_cursor.line() && is_focused())
  88. painter.fill_rect(line_rect, Color(230, 230, 230));
  89. painter.draw_text(line_rect, line.text(), TextAlignment::CenterLeft, Color::Black);
  90. }
  91. if (is_focused() && m_cursor_state)
  92. painter.fill_rect(cursor_content_rect(), Color::Red);
  93. painter.translate(-padding(), -padding());
  94. painter.translate(m_horizontal_scrollbar->value(), m_vertical_scrollbar->value());
  95. painter.fill_rect({ m_horizontal_scrollbar->relative_rect().top_right().translated(1, 0), { m_vertical_scrollbar->preferred_size().width(), m_horizontal_scrollbar->preferred_size().height() } }, Color::LightGray);
  96. if (is_focused()) {
  97. Rect item_area_rect { 0, 0, width() - m_vertical_scrollbar->width(), height() - m_horizontal_scrollbar->height() };
  98. painter.draw_rect(item_area_rect, Color::from_rgb(0x84351a));
  99. };
  100. }
  101. void GTextEditor::keydown_event(GKeyEvent& event)
  102. {
  103. if (!event.modifiers() && event.key() == KeyCode::Key_Up) {
  104. if (m_cursor.line() > 0) {
  105. int new_line = m_cursor.line() - 1;
  106. int new_column = min(m_cursor.column(), m_lines[new_line].length());
  107. set_cursor(new_line, new_column);
  108. }
  109. return;
  110. }
  111. if (!event.modifiers() && event.key() == KeyCode::Key_Down) {
  112. if (m_cursor.line() < (m_lines.size() - 1)) {
  113. int new_line = m_cursor.line() + 1;
  114. int new_column = min(m_cursor.column(), m_lines[new_line].length());
  115. set_cursor(new_line, new_column);
  116. }
  117. return;
  118. }
  119. if (!event.modifiers() && event.key() == KeyCode::Key_Left) {
  120. if (m_cursor.column() > 0) {
  121. int new_column = m_cursor.column() - 1;
  122. set_cursor(m_cursor.line(), new_column);
  123. }
  124. return;
  125. }
  126. if (!event.modifiers() && event.key() == KeyCode::Key_Right) {
  127. if (m_cursor.column() < current_line().length()) {
  128. int new_column = m_cursor.column() + 1;
  129. set_cursor(m_cursor.line(), new_column);
  130. }
  131. return;
  132. }
  133. if (!event.modifiers() && event.key() == KeyCode::Key_Home) {
  134. set_cursor(m_cursor.line(), 0);
  135. return;
  136. }
  137. if (!event.modifiers() && event.key() == KeyCode::Key_End) {
  138. set_cursor(m_cursor.line(), current_line().length());
  139. return;
  140. }
  141. if (event.ctrl() && event.key() == KeyCode::Key_Home) {
  142. set_cursor(0, 0);
  143. return;
  144. }
  145. if (event.ctrl() && event.key() == KeyCode::Key_End) {
  146. set_cursor(line_count() - 1, m_lines[line_count() - 1].length());
  147. return;
  148. }
  149. return GWidget::keydown_event(event);
  150. }
  151. Rect GTextEditor::visible_content_rect() const
  152. {
  153. return {
  154. m_horizontal_scrollbar->value(),
  155. m_vertical_scrollbar->value(),
  156. width() - m_vertical_scrollbar->width() - padding() * 2,
  157. height() - m_horizontal_scrollbar->height() - padding() * 2
  158. };
  159. }
  160. Rect GTextEditor::cursor_content_rect() const
  161. {
  162. if (!m_cursor.is_valid())
  163. return { };
  164. ASSERT(!m_lines.is_empty());
  165. ASSERT(m_cursor.column() <= (current_line().text().length() + 1));
  166. int x = 0;
  167. for (int i = 0; i < m_cursor.column(); ++i)
  168. x += font().glyph_width(current_line().text()[i]);
  169. return { x, m_cursor.line() * line_height(), 1, line_height() };
  170. }
  171. Rect GTextEditor::line_widget_rect(int line_index) const
  172. {
  173. ASSERT(m_horizontal_scrollbar);
  174. ASSERT(m_vertical_scrollbar);
  175. auto rect = line_content_rect(line_index);
  176. rect.move_by(-(m_horizontal_scrollbar->value() - padding()), -(m_vertical_scrollbar->value() - padding()));
  177. rect.intersect(this->rect());
  178. return rect;
  179. }
  180. void GTextEditor::scroll_cursor_into_view()
  181. {
  182. auto visible_content_rect = this->visible_content_rect();
  183. auto rect = cursor_content_rect();
  184. if (visible_content_rect.is_empty())
  185. return;
  186. if (visible_content_rect.contains(rect))
  187. return;
  188. if (rect.top() < visible_content_rect.top())
  189. m_vertical_scrollbar->set_value(rect.top());
  190. else if (rect.bottom() > visible_content_rect.bottom())
  191. m_vertical_scrollbar->set_value(rect.bottom() - visible_content_rect.height());
  192. if (rect.left() < visible_content_rect.left())
  193. m_horizontal_scrollbar->set_value(rect.left());
  194. else if (rect.right() > visible_content_rect.right())
  195. m_horizontal_scrollbar->set_value(rect.right() - visible_content_rect.width());
  196. update();
  197. }
  198. Rect GTextEditor::line_content_rect(int line_index) const
  199. {
  200. return {
  201. 0,
  202. line_index * line_height(),
  203. content_width(),
  204. line_height()
  205. };
  206. }
  207. void GTextEditor::update_cursor()
  208. {
  209. update(line_widget_rect(m_cursor.line()));
  210. }
  211. void GTextEditor::set_cursor(int line, int column)
  212. {
  213. if (m_cursor.line() == line && m_cursor.column() == column)
  214. return;
  215. update_cursor();
  216. m_cursor = GTextPosition(line, column);
  217. m_cursor_state = true;
  218. scroll_cursor_into_view();
  219. update_cursor();
  220. if (on_cursor_change)
  221. on_cursor_change(*this);
  222. }
  223. void GTextEditor::focusin_event(GEvent&)
  224. {
  225. update_cursor();
  226. start_timer(500);
  227. }
  228. void GTextEditor::focusout_event(GEvent&)
  229. {
  230. stop_timer();
  231. }
  232. void GTextEditor::timer_event(GTimerEvent&)
  233. {
  234. m_cursor_state = !m_cursor_state;
  235. if (is_focused())
  236. update_cursor();
  237. }
  238. void GTextEditor::Line::set_text(const String& text)
  239. {
  240. if (text == m_text)
  241. return;
  242. m_text = text;
  243. m_cached_width = -1;
  244. }
  245. int GTextEditor::Line::width(const Font& font) const
  246. {
  247. if (m_cached_width < 0)
  248. m_cached_width = font.width(m_text);
  249. return m_cached_width;
  250. }