GTextEditor.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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, (content_height() + padding() * 2) - 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() + padding() * 2) - 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_height() const
  64. {
  65. return line_count() * line_height();
  66. }
  67. int GTextEditor::content_width() const
  68. {
  69. // FIXME: Cache this somewhere.
  70. int max_width = 0;
  71. for (auto& line : m_lines)
  72. max_width = max(line.width(font()), max_width);
  73. return max_width;
  74. }
  75. void GTextEditor::mousedown_event(GMouseEvent& event)
  76. {
  77. auto position = event.position();
  78. position.move_by(m_horizontal_scrollbar->value(), m_vertical_scrollbar->value());
  79. position.move_by(-padding(), -padding());
  80. int line_index = position.y() / line_height();
  81. int column_index = position.x() / glyph_width();
  82. column_index = min(column_index, m_lines[line_index].length());
  83. set_cursor(line_index, column_index);
  84. }
  85. void GTextEditor::paint_event(GPaintEvent& event)
  86. {
  87. Painter painter(*this);
  88. painter.set_clip_rect(event.rect());
  89. painter.fill_rect(event.rect(), Color::White);
  90. painter.translate(-m_horizontal_scrollbar->value(), -m_vertical_scrollbar->value());
  91. painter.translate(padding(), padding());
  92. int exposed_width = max(content_width(), width());
  93. for (int i = 0; i < line_count(); ++i) {
  94. auto& line = m_lines[i];
  95. auto line_rect = line_content_rect(i);
  96. line_rect.set_width(exposed_width);
  97. if (i == m_cursor.line() && is_focused())
  98. painter.fill_rect(line_rect, Color(230, 230, 230));
  99. painter.draw_text(line_rect, line.characters(), line.length(), TextAlignment::CenterLeft, Color::Black);
  100. }
  101. if (is_focused() && m_cursor_state)
  102. painter.fill_rect(cursor_content_rect(), Color::Red);
  103. painter.translate(-padding(), -padding());
  104. painter.translate(m_horizontal_scrollbar->value(), m_vertical_scrollbar->value());
  105. 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);
  106. if (is_focused()) {
  107. Rect item_area_rect { 0, 0, width() - m_vertical_scrollbar->width(), height() - m_horizontal_scrollbar->height() };
  108. painter.draw_rect(item_area_rect, Color::from_rgb(0x84351a));
  109. };
  110. }
  111. void GTextEditor::keydown_event(GKeyEvent& event)
  112. {
  113. if (!event.modifiers() && event.key() == KeyCode::Key_Up) {
  114. if (m_cursor.line() > 0) {
  115. int new_line = m_cursor.line() - 1;
  116. int new_column = min(m_cursor.column(), m_lines[new_line].length());
  117. set_cursor(new_line, new_column);
  118. }
  119. return;
  120. }
  121. if (!event.modifiers() && event.key() == KeyCode::Key_Down) {
  122. if (m_cursor.line() < (m_lines.size() - 1)) {
  123. int new_line = m_cursor.line() + 1;
  124. int new_column = min(m_cursor.column(), m_lines[new_line].length());
  125. set_cursor(new_line, new_column);
  126. }
  127. return;
  128. }
  129. if (!event.modifiers() && event.key() == KeyCode::Key_Left) {
  130. if (m_cursor.column() > 0) {
  131. int new_column = m_cursor.column() - 1;
  132. set_cursor(m_cursor.line(), new_column);
  133. }
  134. return;
  135. }
  136. if (!event.modifiers() && event.key() == KeyCode::Key_Right) {
  137. if (m_cursor.column() < current_line().length()) {
  138. int new_column = m_cursor.column() + 1;
  139. set_cursor(m_cursor.line(), new_column);
  140. }
  141. return;
  142. }
  143. if (!event.modifiers() && event.key() == KeyCode::Key_Home) {
  144. set_cursor(m_cursor.line(), 0);
  145. return;
  146. }
  147. if (!event.modifiers() && event.key() == KeyCode::Key_End) {
  148. set_cursor(m_cursor.line(), current_line().length());
  149. return;
  150. }
  151. if (event.ctrl() && event.key() == KeyCode::Key_Home) {
  152. set_cursor(0, 0);
  153. return;
  154. }
  155. if (event.ctrl() && event.key() == KeyCode::Key_End) {
  156. set_cursor(line_count() - 1, m_lines[line_count() - 1].length());
  157. return;
  158. }
  159. if (!event.text().is_empty())
  160. insert_at_cursor(event.text()[0]);
  161. return GWidget::keydown_event(event);
  162. }
  163. void GTextEditor::insert_at_cursor(char ch)
  164. {
  165. }
  166. Rect GTextEditor::visible_content_rect() const
  167. {
  168. return {
  169. m_horizontal_scrollbar->value(),
  170. m_vertical_scrollbar->value(),
  171. width() - m_vertical_scrollbar->width() - padding() * 2,
  172. height() - m_horizontal_scrollbar->height() - padding() * 2
  173. };
  174. }
  175. Rect GTextEditor::cursor_content_rect() const
  176. {
  177. if (!m_cursor.is_valid())
  178. return { };
  179. ASSERT(!m_lines.is_empty());
  180. ASSERT(m_cursor.column() <= (current_line().length() + 1));
  181. return { m_cursor.column() * glyph_width(), m_cursor.line() * line_height(), 1, line_height() };
  182. }
  183. Rect GTextEditor::line_widget_rect(int line_index) const
  184. {
  185. ASSERT(m_horizontal_scrollbar);
  186. ASSERT(m_vertical_scrollbar);
  187. auto rect = line_content_rect(line_index);
  188. rect.move_by(-(m_horizontal_scrollbar->value() - padding()), -(m_vertical_scrollbar->value() - padding()));
  189. rect.set_width(rect.width() + 1); // Add 1 pixel for when the cursor is on the end.
  190. rect.intersect(this->rect());
  191. return rect;
  192. }
  193. void GTextEditor::scroll_cursor_into_view()
  194. {
  195. auto visible_content_rect = this->visible_content_rect();
  196. auto rect = cursor_content_rect();
  197. if (visible_content_rect.is_empty())
  198. return;
  199. if (visible_content_rect.contains(rect))
  200. return;
  201. if (rect.top() < visible_content_rect.top())
  202. m_vertical_scrollbar->set_value(rect.top());
  203. else if (rect.bottom() > visible_content_rect.bottom())
  204. m_vertical_scrollbar->set_value(rect.bottom() - visible_content_rect.height());
  205. if (rect.left() < visible_content_rect.left())
  206. m_horizontal_scrollbar->set_value(rect.left());
  207. else if (rect.right() > visible_content_rect.right())
  208. m_horizontal_scrollbar->set_value(rect.right() - visible_content_rect.width());
  209. update();
  210. }
  211. Rect GTextEditor::line_content_rect(int line_index) const
  212. {
  213. return {
  214. 0,
  215. line_index * line_height(),
  216. content_width(),
  217. line_height()
  218. };
  219. }
  220. void GTextEditor::update_cursor()
  221. {
  222. update(line_widget_rect(m_cursor.line()));
  223. }
  224. void GTextEditor::set_cursor(int line, int column)
  225. {
  226. if (m_cursor.line() == line && m_cursor.column() == column)
  227. return;
  228. auto old_cursor_line_rect = line_widget_rect(m_cursor.line());
  229. m_cursor = GTextPosition(line, column);
  230. m_cursor_state = true;
  231. scroll_cursor_into_view();
  232. update(old_cursor_line_rect);
  233. update_cursor();
  234. if (on_cursor_change)
  235. on_cursor_change(*this);
  236. }
  237. void GTextEditor::focusin_event(GEvent&)
  238. {
  239. update_cursor();
  240. start_timer(500);
  241. }
  242. void GTextEditor::focusout_event(GEvent&)
  243. {
  244. stop_timer();
  245. }
  246. void GTextEditor::timer_event(GTimerEvent&)
  247. {
  248. m_cursor_state = !m_cursor_state;
  249. if (is_focused())
  250. update_cursor();
  251. }
  252. GTextEditor::Line::Line()
  253. {
  254. m_text.append(0);
  255. }
  256. void GTextEditor::Line::set_text(const String& text)
  257. {
  258. if (text.length() == length() && !memcmp(text.characters(), characters(), length()))
  259. return;
  260. m_text.resize(text.length() + 1);
  261. memcpy(m_text.data(), text.characters(), text.length() + 1);
  262. }
  263. int GTextEditor::Line::width(const Font& font) const
  264. {
  265. return font.glyph_width('x') * length();
  266. }