GTextEditor.cpp 10 KB

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