GTextEditor.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. 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. }
  173. Rect GTextEditor::visible_content_rect() const
  174. {
  175. return {
  176. m_horizontal_scrollbar->value(),
  177. m_vertical_scrollbar->value(),
  178. width() - m_vertical_scrollbar->width() - padding() * 2,
  179. height() - m_horizontal_scrollbar->height() - padding() * 2
  180. };
  181. }
  182. Rect GTextEditor::cursor_content_rect() const
  183. {
  184. if (!m_cursor.is_valid())
  185. return { };
  186. ASSERT(!m_lines.is_empty());
  187. ASSERT(m_cursor.column() <= (current_line().length() + 1));
  188. return { m_cursor.column() * glyph_width(), m_cursor.line() * line_height(), 1, line_height() };
  189. }
  190. Rect GTextEditor::line_widget_rect(int line_index) const
  191. {
  192. ASSERT(m_horizontal_scrollbar);
  193. ASSERT(m_vertical_scrollbar);
  194. auto rect = line_content_rect(line_index);
  195. rect.move_by(-(m_horizontal_scrollbar->value() - padding()), -(m_vertical_scrollbar->value() - padding()));
  196. rect.set_width(rect.width() + 1); // Add 1 pixel for when the cursor is on the end.
  197. rect.intersect(this->rect());
  198. return rect;
  199. }
  200. void GTextEditor::scroll_cursor_into_view()
  201. {
  202. auto visible_content_rect = this->visible_content_rect();
  203. auto rect = cursor_content_rect();
  204. if (visible_content_rect.is_empty())
  205. return;
  206. if (visible_content_rect.contains(rect))
  207. return;
  208. if (rect.top() < visible_content_rect.top())
  209. m_vertical_scrollbar->set_value(rect.top());
  210. else if (rect.bottom() > visible_content_rect.bottom())
  211. m_vertical_scrollbar->set_value(rect.bottom() - visible_content_rect.height());
  212. if (rect.left() < visible_content_rect.left())
  213. m_horizontal_scrollbar->set_value(rect.left());
  214. else if (rect.right() > visible_content_rect.right())
  215. m_horizontal_scrollbar->set_value(rect.right() - visible_content_rect.width());
  216. update();
  217. }
  218. Rect GTextEditor::line_content_rect(int line_index) const
  219. {
  220. return {
  221. 0,
  222. line_index * line_height(),
  223. content_width(),
  224. line_height()
  225. };
  226. }
  227. void GTextEditor::update_cursor()
  228. {
  229. update(line_widget_rect(m_cursor.line()));
  230. }
  231. void GTextEditor::set_cursor(int line, int column)
  232. {
  233. set_cursor({ line, column });
  234. }
  235. void GTextEditor::set_cursor(const GTextPosition& position)
  236. {
  237. if (m_cursor == position)
  238. return;
  239. auto old_cursor_line_rect = line_widget_rect(m_cursor.line());
  240. m_cursor = position;
  241. m_cursor_state = true;
  242. scroll_cursor_into_view();
  243. update(old_cursor_line_rect);
  244. update_cursor();
  245. if (on_cursor_change)
  246. on_cursor_change(*this);
  247. }
  248. void GTextEditor::focusin_event(GEvent&)
  249. {
  250. update_cursor();
  251. start_timer(500);
  252. }
  253. void GTextEditor::focusout_event(GEvent&)
  254. {
  255. stop_timer();
  256. }
  257. void GTextEditor::timer_event(GTimerEvent&)
  258. {
  259. m_cursor_state = !m_cursor_state;
  260. if (is_focused())
  261. update_cursor();
  262. }
  263. GTextEditor::Line::Line()
  264. {
  265. m_text.append(0);
  266. }
  267. void GTextEditor::Line::set_text(const String& text)
  268. {
  269. if (text.length() == length() && !memcmp(text.characters(), characters(), length()))
  270. return;
  271. m_text.resize(text.length() + 1);
  272. memcpy(m_text.data(), text.characters(), text.length() + 1);
  273. }
  274. int GTextEditor::Line::width(const Font& font) const
  275. {
  276. return font.glyph_width('x') * length();
  277. }