GTextEditor.cpp 25 KB

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