GTextEditor.cpp 25 KB

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