GTextEditor.cpp 26 KB

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