GTextEditor.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  1. #include <LibGUI/GTextEditor.h>
  2. #include <LibGUI/GScrollBar.h>
  3. #include <LibGUI/GFontDatabase.h>
  4. #include <LibGUI/GClipboard.h>
  5. #include <LibGUI/GPainter.h>
  6. #include <LibGUI/GWindow.h>
  7. #include <Kernel/KeyCode.h>
  8. #include <AK/StringBuilder.h>
  9. #include <unistd.h>
  10. #include <fcntl.h>
  11. #include <stdio.h>
  12. GTextEditor::GTextEditor(Type type, GWidget* parent)
  13. : GScrollableWidget(parent)
  14. , m_type(type)
  15. {
  16. set_frame_shape(GFrame::Shape::Container);
  17. set_frame_shadow(GFrame::Shadow::Sunken);
  18. set_frame_thickness(2);
  19. set_scrollbars_enabled(is_multi_line());
  20. m_ruler_visible = is_multi_line();
  21. set_font(GFontDatabase::the().get_by_name("Csilla Thin"));
  22. m_lines.append(make<Line>());
  23. m_cursor = { 0, 0 };
  24. }
  25. GTextEditor::~GTextEditor()
  26. {
  27. }
  28. void GTextEditor::set_text(const String& text)
  29. {
  30. if (is_single_line() && text.length() == m_lines[0]->length() && !memcmp(text.characters(), m_lines[0]->characters(), text.length()))
  31. return;
  32. m_selection.clear();
  33. m_lines.clear();
  34. int start_of_current_line = 0;
  35. auto add_line = [&] (int current_position) {
  36. int line_length = current_position - start_of_current_line;
  37. auto line = make<Line>();
  38. if (line_length)
  39. line->set_text(text.substring(start_of_current_line, current_position - start_of_current_line));
  40. m_lines.append(move(line));
  41. start_of_current_line = current_position + 1;
  42. };
  43. int i = 0;
  44. for (i = 0; i < text.length(); ++i) {
  45. if (text[i] == '\n')
  46. add_line(i);
  47. }
  48. add_line(i);
  49. update_content_size();
  50. if (is_single_line())
  51. set_cursor(0, m_lines[0]->length());
  52. else
  53. set_cursor(0, 0);
  54. update();
  55. }
  56. void GTextEditor::update_content_size()
  57. {
  58. int content_width = 0;
  59. for (auto& line : m_lines)
  60. content_width = max(line->width(font()), content_width);
  61. content_width += m_horizontal_content_padding * 2;
  62. int content_height = line_count() * line_height();
  63. set_content_size({ content_width, content_height });
  64. set_size_occupied_by_fixed_elements({ ruler_width(), 0 });
  65. }
  66. GTextPosition GTextEditor::text_position_at(const Point& a_position) const
  67. {
  68. auto position = a_position;
  69. position.move_by(horizontal_scrollbar().value(), vertical_scrollbar().value());
  70. position.move_by(-(m_horizontal_content_padding + ruler_width()), 0);
  71. int line_index = position.y() / line_height();
  72. int column_index = position.x() / glyph_width();
  73. line_index = max(0, min(line_index, line_count() - 1));
  74. column_index = max(0, min(column_index, m_lines[line_index]->length()));
  75. return { line_index, column_index };
  76. }
  77. void GTextEditor::mousedown_event(GMouseEvent& event)
  78. {
  79. if (event.button() == GMouseButton::Left) {
  80. if (event.modifiers() & Mod_Shift) {
  81. if (!has_selection())
  82. m_selection.set(m_cursor, { });
  83. } else {
  84. m_selection.clear();
  85. }
  86. m_in_drag_select = true;
  87. set_cursor(text_position_at(event.position()));
  88. if (!(event.modifiers() & Mod_Shift)) {
  89. if (!has_selection())
  90. m_selection.set(m_cursor, { });
  91. }
  92. if (m_selection.start().is_valid())
  93. m_selection.set_end(m_cursor);
  94. // FIXME: Only update the relevant rects.
  95. update();
  96. return;
  97. }
  98. }
  99. void GTextEditor::mouseup_event(GMouseEvent& event)
  100. {
  101. if (event.button() == GMouseButton::Left) {
  102. if (m_in_drag_select) {
  103. m_in_drag_select = false;
  104. }
  105. return;
  106. }
  107. }
  108. void GTextEditor::mousemove_event(GMouseEvent& event)
  109. {
  110. if (m_in_drag_select) {
  111. set_cursor(text_position_at(event.position()));
  112. m_selection.set_end(m_cursor);
  113. update();
  114. return;
  115. }
  116. }
  117. int GTextEditor::ruler_width() const
  118. {
  119. if (!m_ruler_visible)
  120. return 0;
  121. // FIXME: Resize based on needed space.
  122. return 5 * font().glyph_width('x') + 4;
  123. }
  124. Rect GTextEditor::ruler_content_rect(int line_index) const
  125. {
  126. if (!m_ruler_visible)
  127. return { };
  128. return {
  129. 0 - ruler_width() + horizontal_scrollbar().value(),
  130. line_index * line_height(),
  131. ruler_width(),
  132. line_height()
  133. };
  134. }
  135. void GTextEditor::paint_event(GPaintEvent& event)
  136. {
  137. GFrame::paint_event(event);
  138. GPainter painter(*this);
  139. painter.add_clip_rect(widget_inner_rect());
  140. painter.add_clip_rect(event.rect());
  141. painter.fill_rect(event.rect(), Color::White);
  142. Rect ruler_rect { 0, 0, ruler_width(), height() - height_occupied_by_horizontal_scrollbar()};
  143. if (m_ruler_visible) {
  144. painter.fill_rect(ruler_rect, Color::LightGray);
  145. painter.draw_line(ruler_rect.top_right(), ruler_rect.bottom_right(), Color::DarkGray);
  146. }
  147. painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  148. if (m_ruler_visible)
  149. painter.translate(ruler_width(), 0);
  150. int exposed_width = max(content_width(), width());
  151. int first_visible_line = text_position_at(event.rect().top_left()).line();
  152. int last_visible_line = text_position_at(event.rect().bottom_right()).line();
  153. auto selection = normalized_selection();
  154. bool has_selection = selection.is_valid();
  155. if (m_ruler_visible) {
  156. for (int i = first_visible_line; i <= last_visible_line; ++i) {
  157. bool is_current_line = i == m_cursor.line();
  158. auto ruler_line_rect = ruler_content_rect(i);
  159. painter.draw_text(
  160. ruler_line_rect.shrunken(2, 0),
  161. String::format("%u", i),
  162. is_current_line ? Font::default_bold_font() : font(),
  163. TextAlignment::CenterRight,
  164. is_current_line ? Color::DarkGray : Color::MidGray
  165. );
  166. }
  167. }
  168. painter.add_clip_rect({ m_ruler_visible ? (ruler_rect.right() + 1) : 0, 0, width() - width_occupied_by_vertical_scrollbar() - ruler_width(), height() - height_occupied_by_horizontal_scrollbar() });
  169. for (int i = first_visible_line; i <= last_visible_line; ++i) {
  170. auto& line = *m_lines[i];
  171. auto line_rect = line_content_rect(i);
  172. line_rect.set_width(exposed_width);
  173. if (is_multi_line() && i == m_cursor.line())
  174. painter.fill_rect(line_rect, Color(230, 230, 230));
  175. painter.draw_text(line_rect, line.characters(), line.length(), TextAlignment::CenterLeft, Color::Black);
  176. bool line_has_selection = has_selection && i >= selection.start().line() && i <= selection.end().line();
  177. if (line_has_selection) {
  178. int selection_start_column_on_line = selection.start().line() == i ? selection.start().column() : 0;
  179. int selection_end_column_on_line = selection.end().line() == i ? selection.end().column() : line.length();
  180. int selection_left = m_horizontal_content_padding + selection_start_column_on_line * font().glyph_width('x');
  181. int selection_right = line_rect.left() + selection_end_column_on_line * font().glyph_width('x');
  182. Rect selection_rect { selection_left, line_rect.y(), selection_right - selection_left, line_rect.height() };
  183. painter.fill_rect(selection_rect, Color::from_rgb(0x955233));
  184. painter.draw_text(selection_rect, line.characters() + selection_start_column_on_line, line.length() - selection_start_column_on_line - (line.length() - selection_end_column_on_line), TextAlignment::CenterLeft, Color::White);
  185. }
  186. }
  187. if (is_focused() && m_cursor_state)
  188. painter.fill_rect(cursor_content_rect(), Color::Red);
  189. }
  190. void GTextEditor::toggle_selection_if_needed_for_event(const GKeyEvent& event)
  191. {
  192. if (event.shift() && !m_selection.is_valid()) {
  193. m_selection.set(m_cursor, { });
  194. update();
  195. return;
  196. }
  197. if (!event.shift() && m_selection.is_valid()) {
  198. m_selection.clear();
  199. update();
  200. return;
  201. }
  202. }
  203. void GTextEditor::keydown_event(GKeyEvent& event)
  204. {
  205. if (event.key() == KeyCode::Key_Escape) {
  206. if (on_escape_pressed)
  207. on_escape_pressed(*this);
  208. return;
  209. }
  210. if (event.key() == KeyCode::Key_Up) {
  211. if (m_cursor.line() > 0) {
  212. int new_line = m_cursor.line() - 1;
  213. int new_column = min(m_cursor.column(), m_lines[new_line]->length());
  214. toggle_selection_if_needed_for_event(event);
  215. set_cursor(new_line, new_column);
  216. if (m_selection.start().is_valid())
  217. m_selection.set_end(m_cursor);
  218. }
  219. return;
  220. }
  221. if (event.key() == KeyCode::Key_Down) {
  222. if (m_cursor.line() < (m_lines.size() - 1)) {
  223. int new_line = m_cursor.line() + 1;
  224. int new_column = min(m_cursor.column(), m_lines[new_line]->length());
  225. toggle_selection_if_needed_for_event(event);
  226. set_cursor(new_line, new_column);
  227. if (m_selection.start().is_valid())
  228. m_selection.set_end(m_cursor);
  229. }
  230. return;
  231. }
  232. if (event.key() == KeyCode::Key_PageUp) {
  233. if (m_cursor.line() > 0) {
  234. int new_line = max(0, m_cursor.line() - visible_content_rect().height() / line_height());
  235. int new_column = min(m_cursor.column(), m_lines[new_line]->length());
  236. toggle_selection_if_needed_for_event(event);
  237. set_cursor(new_line, new_column);
  238. if (m_selection.start().is_valid())
  239. m_selection.set_end(m_cursor);
  240. }
  241. return;
  242. }
  243. if (event.key() == KeyCode::Key_PageDown) {
  244. if (m_cursor.line() < (m_lines.size() - 1)) {
  245. int new_line = min(line_count() - 1, m_cursor.line() + visible_content_rect().height() / line_height());
  246. int new_column = min(m_cursor.column(), m_lines[new_line]->length());
  247. toggle_selection_if_needed_for_event(event);
  248. set_cursor(new_line, new_column);
  249. if (m_selection.start().is_valid())
  250. m_selection.set_end(m_cursor);
  251. }
  252. return;
  253. }
  254. if (event.key() == KeyCode::Key_Left) {
  255. if (m_cursor.column() > 0) {
  256. int new_column = m_cursor.column() - 1;
  257. toggle_selection_if_needed_for_event(event);
  258. set_cursor(m_cursor.line(), new_column);
  259. if (m_selection.start().is_valid())
  260. m_selection.set_end(m_cursor);
  261. } else if (m_cursor.line() > 0) {
  262. int new_line = m_cursor.line() - 1;
  263. int new_column = m_lines[new_line]->length();
  264. toggle_selection_if_needed_for_event(event);
  265. set_cursor(new_line, new_column);
  266. if (m_selection.start().is_valid())
  267. m_selection.set_end(m_cursor);
  268. }
  269. return;
  270. }
  271. if (event.key() == KeyCode::Key_Right) {
  272. if (m_cursor.column() < current_line().length()) {
  273. int new_column = m_cursor.column() + 1;
  274. toggle_selection_if_needed_for_event(event);
  275. set_cursor(m_cursor.line(), new_column);
  276. if (m_selection.start().is_valid())
  277. m_selection.set_end(m_cursor);
  278. } else if (m_cursor.line() != line_count() - 1) {
  279. int new_line = m_cursor.line() + 1;
  280. int new_column = 0;
  281. toggle_selection_if_needed_for_event(event);
  282. set_cursor(new_line, new_column);
  283. if (m_selection.start().is_valid())
  284. m_selection.set_end(m_cursor);
  285. }
  286. return;
  287. }
  288. if (!event.ctrl() && event.key() == KeyCode::Key_Home) {
  289. toggle_selection_if_needed_for_event(event);
  290. set_cursor(m_cursor.line(), 0);
  291. if (m_selection.start().is_valid())
  292. m_selection.set_end(m_cursor);
  293. return;
  294. }
  295. if (!event.ctrl() && event.key() == KeyCode::Key_End) {
  296. toggle_selection_if_needed_for_event(event);
  297. set_cursor(m_cursor.line(), current_line().length());
  298. if (m_selection.start().is_valid())
  299. m_selection.set_end(m_cursor);
  300. return;
  301. }
  302. if (event.ctrl() && event.key() == KeyCode::Key_Home) {
  303. toggle_selection_if_needed_for_event(event);
  304. set_cursor(0, 0);
  305. if (m_selection.start().is_valid())
  306. m_selection.set_end(m_cursor);
  307. return;
  308. }
  309. if (event.ctrl() && event.key() == KeyCode::Key_End) {
  310. toggle_selection_if_needed_for_event(event);
  311. set_cursor(line_count() - 1, m_lines[line_count() - 1]->length());
  312. if (m_selection.start().is_valid())
  313. m_selection.set_end(m_cursor);
  314. return;
  315. }
  316. if (event.modifiers() == Mod_Ctrl && event.key() == KeyCode::Key_A) {
  317. GTextPosition start_of_document { 0, 0 };
  318. GTextPosition end_of_document { line_count() - 1, m_lines[line_count() - 1]->length() };
  319. m_selection.set(start_of_document, end_of_document);
  320. set_cursor(end_of_document);
  321. update();
  322. return;
  323. }
  324. if (event.key() == KeyCode::Key_Backspace) {
  325. if (has_selection()) {
  326. delete_selection();
  327. return;
  328. }
  329. if (m_cursor.column() > 0) {
  330. // Backspace within line
  331. current_line().remove(m_cursor.column() - 1);
  332. update_content_size();
  333. set_cursor(m_cursor.line(), m_cursor.column() - 1);
  334. return;
  335. }
  336. if (m_cursor.column() == 0 && m_cursor.line() != 0) {
  337. // Backspace at column 0; merge with previous line
  338. auto& previous_line = *m_lines[m_cursor.line() - 1];
  339. int previous_length = previous_line.length();
  340. previous_line.append(current_line().characters(), current_line().length());
  341. m_lines.remove(m_cursor.line());
  342. update_content_size();
  343. update();
  344. set_cursor(m_cursor.line() - 1, previous_length);
  345. return;
  346. }
  347. return;
  348. }
  349. if (event.modifiers() == Mod_Shift && event.key() == KeyCode::Key_Delete) {
  350. delete_current_line();
  351. return;
  352. }
  353. if (event.key() == KeyCode::Key_Delete) {
  354. do_delete();
  355. return;
  356. }
  357. if (!event.ctrl() && !event.alt() && !event.text().is_empty())
  358. insert_at_cursor_or_replace_selection(event.text());
  359. return GWidget::keydown_event(event);
  360. }
  361. void GTextEditor::delete_current_line()
  362. {
  363. if (has_selection())
  364. return delete_selection();
  365. m_lines.remove(m_cursor.line());
  366. if (m_lines.is_empty())
  367. m_lines.append(make<Line>());
  368. update_content_size();
  369. update();
  370. }
  371. void GTextEditor::do_delete()
  372. {
  373. if (has_selection())
  374. return delete_selection();
  375. if (m_cursor.column() < current_line().length()) {
  376. // Delete within line
  377. current_line().remove(m_cursor.column());
  378. update_cursor();
  379. did_change();
  380. return;
  381. }
  382. if (m_cursor.column() == current_line().length() && m_cursor.line() != line_count() - 1) {
  383. // Delete at end of line; merge with next line
  384. auto& next_line = *m_lines[m_cursor.line() + 1];
  385. int previous_length = current_line().length();
  386. current_line().append(next_line.characters(), next_line.length());
  387. m_lines.remove(m_cursor.line() + 1);
  388. update();
  389. set_cursor(m_cursor.line(), previous_length);
  390. did_change();
  391. return;
  392. }
  393. }
  394. void GTextEditor::insert_at_cursor(const String& text)
  395. {
  396. // FIXME: This should obviously not be implemented this way.
  397. for (int i = 0; i < text.length(); ++i) {
  398. insert_at_cursor(text[i]);
  399. }
  400. }
  401. void GTextEditor::insert_at_cursor(char ch)
  402. {
  403. bool at_head = m_cursor.column() == 0;
  404. bool at_tail = m_cursor.column() == current_line().length();
  405. if (ch == '\n') {
  406. if (is_single_line()) {
  407. if (on_return_pressed)
  408. on_return_pressed(*this);
  409. return;
  410. }
  411. if (at_tail || at_head) {
  412. m_lines.insert(m_cursor.line() + (at_tail ? 1 : 0), make<Line>());
  413. update();
  414. set_cursor(m_cursor.line() + 1, 0);
  415. did_change();
  416. return;
  417. }
  418. auto new_line = make<Line>();
  419. new_line->append(current_line().characters() + m_cursor.column(), current_line().length() - m_cursor.column());
  420. current_line().truncate(m_cursor.column());
  421. m_lines.insert(m_cursor.line() + 1, move(new_line));
  422. update();
  423. set_cursor(m_cursor.line() + 1, 0);
  424. did_change();
  425. return;
  426. }
  427. if (ch == '\t') {
  428. int next_soft_tab_stop = ((m_cursor.column() + m_soft_tab_width) / m_soft_tab_width) * m_soft_tab_width;
  429. int spaces_to_insert = next_soft_tab_stop - m_cursor.column();
  430. for (int i = 0; i < spaces_to_insert; ++i) {
  431. current_line().insert(m_cursor.column(), ' ');
  432. }
  433. set_cursor(m_cursor.line(), next_soft_tab_stop);
  434. update_cursor();
  435. did_change();
  436. return;
  437. }
  438. current_line().insert(m_cursor.column(), ch);
  439. set_cursor(m_cursor.line(), m_cursor.column() + 1);
  440. update_cursor();
  441. did_change();
  442. }
  443. Rect GTextEditor::cursor_content_rect() const
  444. {
  445. if (!m_cursor.is_valid())
  446. return { };
  447. ASSERT(!m_lines.is_empty());
  448. ASSERT(m_cursor.column() <= (current_line().length() + 1));
  449. if (is_single_line()) {
  450. Rect cursor_rect = { m_horizontal_content_padding + m_cursor.column() * glyph_width(), 0, 1, font().glyph_height() + 2 };
  451. cursor_rect.center_vertically_within(rect());
  452. // FIXME: This would not be necessary if we knew more about the font and could center it based on baseline and x-height.
  453. cursor_rect.move_by(0, 1);
  454. return cursor_rect;
  455. }
  456. return { m_horizontal_content_padding + m_cursor.column() * glyph_width(), m_cursor.line() * line_height(), 1, line_height() };
  457. }
  458. Rect GTextEditor::line_widget_rect(int line_index) const
  459. {
  460. auto rect = line_content_rect(line_index);
  461. rect.move_by(-(horizontal_scrollbar().value() + ruler_width() + m_horizontal_content_padding), -(vertical_scrollbar().value()));
  462. rect.set_width(rect.width() + 1); // Add 1 pixel for when the cursor is on the end.
  463. rect.intersect(this->rect());
  464. // This feels rather hackish, but extend the rect to the edge of the content view:
  465. rect.set_right(frame_inner_rect().right());
  466. return rect;
  467. }
  468. void GTextEditor::scroll_cursor_into_view()
  469. {
  470. auto rect = cursor_content_rect();
  471. if (m_cursor.column() == 0)
  472. rect.set_x(0);
  473. else if (m_cursor.column() >= m_lines[m_cursor.line()]->length())
  474. rect.set_x(m_lines[m_cursor.line()]->width(font()) + m_horizontal_content_padding * 2);
  475. scroll_into_view(rect, true, true);
  476. }
  477. Rect GTextEditor::line_content_rect(int line_index) const
  478. {
  479. if (is_single_line()) {
  480. Rect line_rect = { m_horizontal_content_padding, 0, content_width(), font().glyph_height() + 2 };
  481. line_rect.center_vertically_within(rect());
  482. // FIXME: This would not be necessary if we knew more about the font and could center it based on baseline and x-height.
  483. line_rect.move_by(0, 1);
  484. return line_rect;
  485. }
  486. return {
  487. m_horizontal_content_padding,
  488. line_index * line_height(),
  489. content_width(),
  490. line_height()
  491. };
  492. }
  493. void GTextEditor::update_cursor()
  494. {
  495. update(line_widget_rect(m_cursor.line()));
  496. }
  497. void GTextEditor::set_cursor(int line, int column)
  498. {
  499. set_cursor({ line, column });
  500. }
  501. void GTextEditor::set_cursor(const GTextPosition& position)
  502. {
  503. ASSERT(!m_lines.is_empty());
  504. ASSERT(position.line() < m_lines.size());
  505. ASSERT(position.column() <= m_lines[position.line()]->length());
  506. if (m_cursor != position) {
  507. auto old_cursor_line_rect = line_widget_rect(m_cursor.line());
  508. m_cursor = position;
  509. m_cursor_state = true;
  510. scroll_cursor_into_view();
  511. update(old_cursor_line_rect);
  512. update_cursor();
  513. }
  514. if (on_cursor_change)
  515. on_cursor_change(*this);
  516. }
  517. void GTextEditor::focusin_event(GEvent&)
  518. {
  519. update_cursor();
  520. start_timer(500);
  521. }
  522. void GTextEditor::focusout_event(GEvent&)
  523. {
  524. stop_timer();
  525. }
  526. void GTextEditor::timer_event(GTimerEvent&)
  527. {
  528. m_cursor_state = !m_cursor_state;
  529. if (is_focused())
  530. update_cursor();
  531. }
  532. GTextEditor::Line::Line()
  533. {
  534. clear();
  535. }
  536. void GTextEditor::Line::clear()
  537. {
  538. m_text.clear();
  539. m_text.append(0);
  540. }
  541. void GTextEditor::Line::set_text(const String& text)
  542. {
  543. if (text.length() == length() && !memcmp(text.characters(), characters(), length()))
  544. return;
  545. if (text.is_empty()) {
  546. clear();
  547. return;
  548. }
  549. m_text.resize(text.length() + 1);
  550. memcpy(m_text.data(), text.characters(), text.length() + 1);
  551. }
  552. int GTextEditor::Line::width(const Font& font) const
  553. {
  554. return font.glyph_width('x') * length();
  555. }
  556. void GTextEditor::Line::append(const char* characters, int length)
  557. {
  558. int old_length = m_text.size() - 1;
  559. m_text.resize(m_text.size() + length);
  560. memcpy(m_text.data() + old_length, characters, length);
  561. m_text.last() = 0;
  562. }
  563. void GTextEditor::Line::append(char ch)
  564. {
  565. insert(length(), ch);
  566. }
  567. void GTextEditor::Line::prepend(char ch)
  568. {
  569. insert(0, ch);
  570. }
  571. void GTextEditor::Line::insert(int index, char ch)
  572. {
  573. if (index == length()) {
  574. m_text.last() = ch;
  575. m_text.append(0);
  576. } else {
  577. m_text.insert(index, move(ch));
  578. }
  579. }
  580. void GTextEditor::Line::remove(int index)
  581. {
  582. if (index == length()) {
  583. m_text.take_last();
  584. m_text.last() = 0;
  585. } else {
  586. m_text.remove(index);
  587. }
  588. }
  589. void GTextEditor::Line::truncate(int length)
  590. {
  591. m_text.resize(length + 1);
  592. m_text.last() = 0;
  593. }
  594. bool GTextEditor::write_to_file(const String& path)
  595. {
  596. int fd = open(path.characters(), O_WRONLY | O_CREAT | O_TRUNC, 0666);
  597. if (fd < 0) {
  598. perror("open");
  599. return false;
  600. }
  601. for (int i = 0; i < m_lines.size(); ++i) {
  602. auto& line = *m_lines[i];
  603. if (line.length()) {
  604. ssize_t nwritten = write(fd, line.characters(), line.length());
  605. if (nwritten < 0) {
  606. perror("write");
  607. close(fd);
  608. return false;
  609. }
  610. }
  611. if (i != m_lines.size() - 1) {
  612. char ch = '\n';
  613. ssize_t nwritten = write(fd, &ch, 1);
  614. if (nwritten != 1) {
  615. perror("write");
  616. close(fd);
  617. return false;
  618. }
  619. }
  620. }
  621. close(fd);
  622. return true;
  623. }
  624. String GTextEditor::text() const
  625. {
  626. StringBuilder builder;
  627. for (int i = 0; i < line_count(); ++i) {
  628. auto& line = *m_lines[i];
  629. builder.append(line.characters(), line.length());
  630. if (i != line_count() - 1)
  631. builder.append('\n');
  632. }
  633. return builder.to_string();
  634. }
  635. void GTextEditor::clear()
  636. {
  637. m_lines.clear();
  638. m_lines.append(make<Line>());
  639. m_selection.clear();
  640. set_cursor(0, 0);
  641. update();
  642. }
  643. String GTextEditor::selected_text() const
  644. {
  645. if (!has_selection())
  646. return { };
  647. auto selection = normalized_selection();
  648. StringBuilder builder;
  649. for (int i = selection.start().line(); i <= selection.end().line(); ++i) {
  650. auto& line = *m_lines[i];
  651. int selection_start_column_on_line = selection.start().line() == i ? selection.start().column() : 0;
  652. int selection_end_column_on_line = selection.end().line() == i ? selection.end().column() : line.length();
  653. builder.append(line.characters() + selection_start_column_on_line, selection_end_column_on_line - selection_start_column_on_line);
  654. if (i != selection.end().line())
  655. builder.append('\n');
  656. }
  657. return builder.to_string();
  658. }
  659. void GTextEditor::delete_selection()
  660. {
  661. if (!has_selection())
  662. return;
  663. auto selection = normalized_selection();
  664. // First delete all the lines in between the first and last one.
  665. for (int i = selection.start().line() + 1; i < selection.end().line();) {
  666. m_lines.remove(i);
  667. selection.end().set_line(selection.end().line() - 1);
  668. }
  669. if (selection.start().line() == selection.end().line()) {
  670. // Delete within same line.
  671. auto& line = *m_lines[selection.start().line()];
  672. bool whole_line_is_selected = selection.start().column() == 0 && selection.end().column() == line.length();
  673. if (whole_line_is_selected) {
  674. line.clear();
  675. } else {
  676. auto before_selection = String(line.characters(), line.length()).substring(0, selection.start().column());
  677. auto after_selection = String(line.characters(), line.length()).substring(selection.end().column(), line.length() - selection.end().column());
  678. StringBuilder builder(before_selection.length() + after_selection.length());
  679. builder.append(before_selection);
  680. builder.append(after_selection);
  681. line.set_text(builder.to_string());
  682. }
  683. } else {
  684. // Delete across a newline, merging lines.
  685. ASSERT(selection.start().line() == selection.end().line() - 1);
  686. auto& first_line = *m_lines[selection.start().line()];
  687. auto& second_line = *m_lines[selection.end().line()];
  688. auto before_selection = String(first_line.characters(), first_line.length()).substring(0, selection.start().column());
  689. auto after_selection = String(second_line.characters(), second_line.length()).substring(selection.end().column(), second_line.length() - selection.end().column());
  690. StringBuilder builder(before_selection.length() + after_selection.length());
  691. builder.append(before_selection);
  692. builder.append(after_selection);
  693. first_line.set_text(builder.to_string());
  694. m_lines.remove(selection.end().line());
  695. }
  696. if (m_lines.is_empty())
  697. m_lines.append(make<Line>());
  698. m_selection.clear();
  699. set_cursor(selection.start());
  700. update();
  701. did_change();
  702. }
  703. void GTextEditor::insert_at_cursor_or_replace_selection(const String& text)
  704. {
  705. if (has_selection())
  706. delete_selection();
  707. insert_at_cursor(text);
  708. }
  709. void GTextEditor::cut()
  710. {
  711. auto selected_text = this->selected_text();
  712. printf("Cut: \"%s\"\n", selected_text.characters());
  713. GClipboard::the().set_data(selected_text);
  714. delete_selection();
  715. }
  716. void GTextEditor::copy()
  717. {
  718. auto selected_text = this->selected_text();
  719. printf("Copy: \"%s\"\n", selected_text.characters());
  720. GClipboard::the().set_data(selected_text);
  721. }
  722. void GTextEditor::paste()
  723. {
  724. auto paste_text = GClipboard::the().data();
  725. printf("Paste: \"%s\"\n", paste_text.characters());
  726. insert_at_cursor_or_replace_selection(paste_text);
  727. }
  728. void GTextEditor::enter_event(GEvent&)
  729. {
  730. ASSERT(window());
  731. window()->set_override_cursor(GStandardCursor::IBeam);
  732. }
  733. void GTextEditor::leave_event(GEvent&)
  734. {
  735. ASSERT(window());
  736. window()->set_override_cursor(GStandardCursor::None);
  737. }
  738. void GTextEditor::did_change()
  739. {
  740. update_content_size();
  741. if (!m_have_pending_change_notification) {
  742. m_have_pending_change_notification = true;
  743. deferred_invoke([this] (auto&) {
  744. if (on_change)
  745. on_change();
  746. m_have_pending_change_notification = false;
  747. });
  748. }
  749. }