GTextEditor.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  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. else if (is_single_line())
  189. painter.draw_rect(item_area_rect, Color::DarkGray);
  190. }
  191. void GTextEditor::toggle_selection_if_needed_for_event(const GKeyEvent& event)
  192. {
  193. if (event.shift() && !m_selection.is_valid()) {
  194. m_selection.set(m_cursor, { });
  195. update();
  196. return;
  197. }
  198. if (!event.shift() && m_selection.is_valid()) {
  199. m_selection.clear();
  200. update();
  201. return;
  202. }
  203. }
  204. void GTextEditor::keydown_event(GKeyEvent& event)
  205. {
  206. if (event.key() == KeyCode::Key_Escape) {
  207. if (on_escape_pressed)
  208. on_escape_pressed(*this);
  209. return;
  210. }
  211. if (event.key() == KeyCode::Key_Up) {
  212. if (m_cursor.line() > 0) {
  213. int new_line = m_cursor.line() - 1;
  214. int new_column = min(m_cursor.column(), m_lines[new_line]->length());
  215. toggle_selection_if_needed_for_event(event);
  216. set_cursor(new_line, new_column);
  217. if (m_selection.start().is_valid())
  218. m_selection.set_end(m_cursor);
  219. }
  220. return;
  221. }
  222. if (event.key() == KeyCode::Key_Down) {
  223. if (m_cursor.line() < (m_lines.size() - 1)) {
  224. int new_line = m_cursor.line() + 1;
  225. int new_column = min(m_cursor.column(), m_lines[new_line]->length());
  226. toggle_selection_if_needed_for_event(event);
  227. set_cursor(new_line, new_column);
  228. if (m_selection.start().is_valid())
  229. m_selection.set_end(m_cursor);
  230. }
  231. return;
  232. }
  233. if (event.key() == KeyCode::Key_PageUp) {
  234. if (m_cursor.line() > 0) {
  235. int new_line = max(0, m_cursor.line() - visible_content_rect().height() / line_height());
  236. int new_column = min(m_cursor.column(), m_lines[new_line]->length());
  237. toggle_selection_if_needed_for_event(event);
  238. set_cursor(new_line, new_column);
  239. if (m_selection.start().is_valid())
  240. m_selection.set_end(m_cursor);
  241. }
  242. return;
  243. }
  244. if (event.key() == KeyCode::Key_PageDown) {
  245. if (m_cursor.line() < (m_lines.size() - 1)) {
  246. int new_line = min(line_count() - 1, m_cursor.line() + visible_content_rect().height() / line_height());
  247. int new_column = min(m_cursor.column(), m_lines[new_line]->length());
  248. toggle_selection_if_needed_for_event(event);
  249. set_cursor(new_line, new_column);
  250. if (m_selection.start().is_valid())
  251. m_selection.set_end(m_cursor);
  252. }
  253. return;
  254. }
  255. if (event.key() == KeyCode::Key_Left) {
  256. if (m_cursor.column() > 0) {
  257. int new_column = m_cursor.column() - 1;
  258. toggle_selection_if_needed_for_event(event);
  259. set_cursor(m_cursor.line(), new_column);
  260. if (m_selection.start().is_valid())
  261. m_selection.set_end(m_cursor);
  262. } else if (m_cursor.line() > 0) {
  263. int new_line = m_cursor.line() - 1;
  264. int new_column = m_lines[new_line]->length();
  265. toggle_selection_if_needed_for_event(event);
  266. set_cursor(new_line, new_column);
  267. if (m_selection.start().is_valid())
  268. m_selection.set_end(m_cursor);
  269. }
  270. return;
  271. }
  272. if (event.key() == KeyCode::Key_Right) {
  273. if (m_cursor.column() < current_line().length()) {
  274. int new_column = m_cursor.column() + 1;
  275. toggle_selection_if_needed_for_event(event);
  276. set_cursor(m_cursor.line(), new_column);
  277. if (m_selection.start().is_valid())
  278. m_selection.set_end(m_cursor);
  279. } else if (m_cursor.line() != line_count() - 1) {
  280. int new_line = m_cursor.line() + 1;
  281. int new_column = 0;
  282. toggle_selection_if_needed_for_event(event);
  283. set_cursor(new_line, new_column);
  284. if (m_selection.start().is_valid())
  285. m_selection.set_end(m_cursor);
  286. }
  287. return;
  288. }
  289. if (!event.ctrl() && event.key() == KeyCode::Key_Home) {
  290. toggle_selection_if_needed_for_event(event);
  291. set_cursor(m_cursor.line(), 0);
  292. if (m_selection.start().is_valid())
  293. m_selection.set_end(m_cursor);
  294. return;
  295. }
  296. if (!event.ctrl() && event.key() == KeyCode::Key_End) {
  297. toggle_selection_if_needed_for_event(event);
  298. set_cursor(m_cursor.line(), current_line().length());
  299. if (m_selection.start().is_valid())
  300. m_selection.set_end(m_cursor);
  301. return;
  302. }
  303. if (event.ctrl() && event.key() == KeyCode::Key_Home) {
  304. toggle_selection_if_needed_for_event(event);
  305. set_cursor(0, 0);
  306. if (m_selection.start().is_valid())
  307. m_selection.set_end(m_cursor);
  308. return;
  309. }
  310. if (event.ctrl() && event.key() == KeyCode::Key_End) {
  311. toggle_selection_if_needed_for_event(event);
  312. set_cursor(line_count() - 1, m_lines[line_count() - 1]->length());
  313. if (m_selection.start().is_valid())
  314. m_selection.set_end(m_cursor);
  315. return;
  316. }
  317. if (event.modifiers() == Mod_Ctrl && event.key() == KeyCode::Key_A) {
  318. GTextPosition start_of_document { 0, 0 };
  319. GTextPosition end_of_document { line_count() - 1, m_lines[line_count() - 1]->length() };
  320. m_selection.set(start_of_document, end_of_document);
  321. set_cursor(end_of_document);
  322. update();
  323. return;
  324. }
  325. if (event.key() == KeyCode::Key_Backspace) {
  326. if (has_selection()) {
  327. delete_selection();
  328. return;
  329. }
  330. if (m_cursor.column() > 0) {
  331. // Backspace within line
  332. current_line().remove(m_cursor.column() - 1);
  333. update_content_size();
  334. set_cursor(m_cursor.line(), m_cursor.column() - 1);
  335. return;
  336. }
  337. if (m_cursor.column() == 0 && m_cursor.line() != 0) {
  338. // Backspace at column 0; merge with previous line
  339. auto& previous_line = *m_lines[m_cursor.line() - 1];
  340. int previous_length = previous_line.length();
  341. previous_line.append(current_line().characters(), current_line().length());
  342. m_lines.remove(m_cursor.line());
  343. update_content_size();
  344. update();
  345. set_cursor(m_cursor.line() - 1, previous_length);
  346. return;
  347. }
  348. return;
  349. }
  350. if (event.modifiers() == Mod_Shift && event.key() == KeyCode::Key_Delete) {
  351. delete_current_line();
  352. return;
  353. }
  354. if (event.key() == KeyCode::Key_Delete) {
  355. do_delete();
  356. return;
  357. }
  358. if (!event.ctrl() && !event.alt() && !event.text().is_empty())
  359. insert_at_cursor_or_replace_selection(event.text());
  360. return GWidget::keydown_event(event);
  361. }
  362. void GTextEditor::delete_current_line()
  363. {
  364. if (has_selection())
  365. return delete_selection();
  366. m_lines.remove(m_cursor.line());
  367. if (m_lines.is_empty())
  368. m_lines.append(make<Line>());
  369. update_content_size();
  370. update();
  371. }
  372. void GTextEditor::do_delete()
  373. {
  374. if (has_selection())
  375. return delete_selection();
  376. if (m_cursor.column() < current_line().length()) {
  377. // Delete within line
  378. current_line().remove(m_cursor.column());
  379. update_content_size();
  380. update_cursor();
  381. return;
  382. }
  383. if (m_cursor.column() == current_line().length() && m_cursor.line() != line_count() - 1) {
  384. // Delete at end of line; merge with next line
  385. auto& next_line = *m_lines[m_cursor.line() + 1];
  386. int previous_length = current_line().length();
  387. current_line().append(next_line.characters(), next_line.length());
  388. m_lines.remove(m_cursor.line() + 1);
  389. update_content_size();
  390. update();
  391. set_cursor(m_cursor.line(), previous_length);
  392. return;
  393. }
  394. }
  395. void GTextEditor::insert_at_cursor(const String& text)
  396. {
  397. // FIXME: This should obviously not be implemented this way.
  398. for (int i = 0; i < text.length(); ++i) {
  399. insert_at_cursor(text[i]);
  400. }
  401. }
  402. void GTextEditor::insert_at_cursor(char ch)
  403. {
  404. bool at_head = m_cursor.column() == 0;
  405. bool at_tail = m_cursor.column() == current_line().length();
  406. if (ch == '\n') {
  407. if (is_single_line()) {
  408. if (on_return_pressed)
  409. on_return_pressed(*this);
  410. return;
  411. }
  412. if (at_tail || at_head) {
  413. m_lines.insert(m_cursor.line() + (at_tail ? 1 : 0), make<Line>());
  414. update_content_size();
  415. update();
  416. set_cursor(m_cursor.line() + 1, 0);
  417. return;
  418. }
  419. auto new_line = make<Line>();
  420. new_line->append(current_line().characters() + m_cursor.column(), current_line().length() - m_cursor.column());
  421. current_line().truncate(m_cursor.column());
  422. m_lines.insert(m_cursor.line() + 1, move(new_line));
  423. update_content_size();
  424. update();
  425. set_cursor(m_cursor.line() + 1, 0);
  426. return;
  427. }
  428. if (ch == '\t') {
  429. int next_soft_tab_stop = ((m_cursor.column() + m_soft_tab_width) / m_soft_tab_width) * m_soft_tab_width;
  430. int spaces_to_insert = next_soft_tab_stop - m_cursor.column();
  431. for (int i = 0; i < spaces_to_insert; ++i) {
  432. current_line().insert(m_cursor.column(), ' ');
  433. }
  434. update_content_size();
  435. set_cursor(m_cursor.line(), next_soft_tab_stop);
  436. update_cursor();
  437. return;
  438. }
  439. current_line().insert(m_cursor.column(), ch);
  440. update_content_size();
  441. set_cursor(m_cursor.line(), m_cursor.column() + 1);
  442. update_cursor();
  443. }
  444. Rect GTextEditor::cursor_content_rect() const
  445. {
  446. if (!m_cursor.is_valid())
  447. return { };
  448. ASSERT(!m_lines.is_empty());
  449. ASSERT(m_cursor.column() <= (current_line().length() + 1));
  450. return { m_horizontal_content_padding + m_cursor.column() * glyph_width(), m_cursor.line() * line_height(), 1, line_height() };
  451. }
  452. Rect GTextEditor::line_widget_rect(int line_index) const
  453. {
  454. auto rect = line_content_rect(line_index);
  455. rect.move_by(-(horizontal_scrollbar().value() + m_horizontal_content_padding), -(vertical_scrollbar().value()));
  456. rect.set_width(rect.width() + 1); // Add 1 pixel for when the cursor is on the end.
  457. rect.intersect(this->rect());
  458. // This feels rather hackish, but extend the rect to the edge of the content view:
  459. rect.set_right(vertical_scrollbar().relative_rect().left() - 1);
  460. return rect;
  461. }
  462. void GTextEditor::scroll_cursor_into_view()
  463. {
  464. auto rect = cursor_content_rect();
  465. if (m_cursor.column() == 0)
  466. rect.set_x(0);
  467. else if (m_cursor.column() >= m_lines[m_cursor.line()]->length())
  468. rect.set_x(m_lines[m_cursor.line()]->width(font()) + m_horizontal_content_padding * 2);
  469. scroll_into_view(rect, true, true);
  470. }
  471. Rect GTextEditor::line_content_rect(int line_index) const
  472. {
  473. return {
  474. m_horizontal_content_padding,
  475. line_index * line_height(),
  476. content_width(),
  477. line_height()
  478. };
  479. }
  480. void GTextEditor::update_cursor()
  481. {
  482. update(line_widget_rect(m_cursor.line()));
  483. }
  484. void GTextEditor::set_cursor(int line, int column)
  485. {
  486. set_cursor({ line, column });
  487. }
  488. void GTextEditor::set_cursor(const GTextPosition& position)
  489. {
  490. ASSERT(!m_lines.is_empty());
  491. ASSERT(position.line() < m_lines.size());
  492. ASSERT(position.column() <= m_lines[position.line()]->length());
  493. if (m_cursor != position) {
  494. auto old_cursor_line_rect = line_widget_rect(m_cursor.line());
  495. m_cursor = position;
  496. m_cursor_state = true;
  497. scroll_cursor_into_view();
  498. update(old_cursor_line_rect);
  499. update_cursor();
  500. }
  501. if (on_cursor_change)
  502. on_cursor_change(*this);
  503. }
  504. void GTextEditor::focusin_event(GEvent&)
  505. {
  506. update_cursor();
  507. start_timer(500);
  508. }
  509. void GTextEditor::focusout_event(GEvent&)
  510. {
  511. stop_timer();
  512. }
  513. void GTextEditor::timer_event(GTimerEvent&)
  514. {
  515. m_cursor_state = !m_cursor_state;
  516. if (is_focused())
  517. update_cursor();
  518. }
  519. GTextEditor::Line::Line()
  520. {
  521. clear();
  522. }
  523. void GTextEditor::Line::clear()
  524. {
  525. m_text.clear();
  526. m_text.append(0);
  527. }
  528. void GTextEditor::Line::set_text(const String& text)
  529. {
  530. if (text.length() == length() && !memcmp(text.characters(), characters(), length()))
  531. return;
  532. if (text.is_empty()) {
  533. clear();
  534. return;
  535. }
  536. m_text.resize(text.length() + 1);
  537. memcpy(m_text.data(), text.characters(), text.length() + 1);
  538. }
  539. int GTextEditor::Line::width(const Font& font) const
  540. {
  541. return font.glyph_width('x') * length();
  542. }
  543. void GTextEditor::Line::append(const char* characters, int length)
  544. {
  545. int old_length = m_text.size() - 1;
  546. m_text.resize(m_text.size() + length);
  547. memcpy(m_text.data() + old_length, characters, length);
  548. m_text.last() = 0;
  549. }
  550. void GTextEditor::Line::append(char ch)
  551. {
  552. insert(length(), ch);
  553. }
  554. void GTextEditor::Line::prepend(char ch)
  555. {
  556. insert(0, ch);
  557. }
  558. void GTextEditor::Line::insert(int index, char ch)
  559. {
  560. if (index == length()) {
  561. m_text.last() = ch;
  562. m_text.append(0);
  563. } else {
  564. m_text.insert(index, move(ch));
  565. }
  566. }
  567. void GTextEditor::Line::remove(int index)
  568. {
  569. if (index == length()) {
  570. m_text.take_last();
  571. m_text.last() = 0;
  572. } else {
  573. m_text.remove(index);
  574. }
  575. }
  576. void GTextEditor::Line::truncate(int length)
  577. {
  578. m_text.resize(length + 1);
  579. m_text.last() = 0;
  580. }
  581. bool GTextEditor::write_to_file(const String& path)
  582. {
  583. int fd = open(path.characters(), O_WRONLY | O_CREAT | O_TRUNC, 0666);
  584. if (fd < 0) {
  585. perror("open");
  586. return false;
  587. }
  588. for (int i = 0; i < m_lines.size(); ++i) {
  589. auto& line = *m_lines[i];
  590. if (line.length()) {
  591. ssize_t nwritten = write(fd, line.characters(), line.length());
  592. if (nwritten < 0) {
  593. perror("write");
  594. close(fd);
  595. return false;
  596. }
  597. }
  598. if (i != m_lines.size() - 1) {
  599. char ch = '\n';
  600. ssize_t nwritten = write(fd, &ch, 1);
  601. if (nwritten != 1) {
  602. perror("write");
  603. close(fd);
  604. return false;
  605. }
  606. }
  607. }
  608. close(fd);
  609. return true;
  610. }
  611. String GTextEditor::text() const
  612. {
  613. StringBuilder builder;
  614. for (int i = 0; i < line_count(); ++i) {
  615. auto& line = *m_lines[i];
  616. builder.append(line.characters(), line.length());
  617. if (i != line_count() - 1)
  618. builder.append('\n');
  619. }
  620. return builder.to_string();
  621. }
  622. void GTextEditor::clear()
  623. {
  624. m_lines.clear();
  625. m_lines.append(make<Line>());
  626. m_selection.clear();
  627. set_cursor(0, 0);
  628. update();
  629. }
  630. String GTextEditor::selected_text() const
  631. {
  632. if (!has_selection())
  633. return { };
  634. auto selection = normalized_selection();
  635. StringBuilder builder;
  636. for (int i = selection.start().line(); i <= selection.end().line(); ++i) {
  637. auto& line = *m_lines[i];
  638. int selection_start_column_on_line = selection.start().line() == i ? selection.start().column() : 0;
  639. int selection_end_column_on_line = selection.end().line() == i ? selection.end().column() : line.length();
  640. builder.append(line.characters() + selection_start_column_on_line, selection_end_column_on_line - selection_start_column_on_line);
  641. if (i != selection.end().line())
  642. builder.append('\n');
  643. }
  644. return builder.to_string();
  645. }
  646. void GTextEditor::delete_selection()
  647. {
  648. if (!has_selection())
  649. return;
  650. auto selection = normalized_selection();
  651. // First delete all the lines in between the first and last one.
  652. for (int i = selection.start().line() + 1; i < selection.end().line();) {
  653. m_lines.remove(i);
  654. selection.end().set_line(selection.end().line() - 1);
  655. }
  656. if (selection.start().line() == selection.end().line()) {
  657. // Delete within same line.
  658. auto& line = *m_lines[selection.start().line()];
  659. bool whole_line_is_selected = selection.start().column() == 0 && selection.end().column() == line.length();
  660. if (whole_line_is_selected) {
  661. line.clear();
  662. } else {
  663. auto before_selection = String(line.characters(), line.length()).substring(0, selection.start().column());
  664. auto after_selection = String(line.characters(), line.length()).substring(selection.end().column(), line.length() - selection.end().column());
  665. StringBuilder builder(before_selection.length() + after_selection.length());
  666. builder.append(before_selection);
  667. builder.append(after_selection);
  668. line.set_text(builder.to_string());
  669. }
  670. } else {
  671. // Delete across a newline, merging lines.
  672. ASSERT(selection.start().line() == selection.end().line() - 1);
  673. auto& first_line = *m_lines[selection.start().line()];
  674. auto& second_line = *m_lines[selection.end().line()];
  675. auto before_selection = String(first_line.characters(), first_line.length()).substring(0, selection.start().column());
  676. auto after_selection = String(second_line.characters(), second_line.length()).substring(selection.end().column(), second_line.length() - selection.end().column());
  677. StringBuilder builder(before_selection.length() + after_selection.length());
  678. builder.append(before_selection);
  679. builder.append(after_selection);
  680. first_line.set_text(builder.to_string());
  681. m_lines.remove(selection.end().line());
  682. }
  683. if (m_lines.is_empty())
  684. m_lines.append(make<Line>());
  685. m_selection.clear();
  686. set_cursor(selection.start());
  687. update();
  688. }
  689. void GTextEditor::insert_at_cursor_or_replace_selection(const String& text)
  690. {
  691. if (has_selection())
  692. delete_selection();
  693. insert_at_cursor(text);
  694. }
  695. void GTextEditor::cut()
  696. {
  697. auto selected_text = this->selected_text();
  698. printf("Cut: \"%s\"\n", selected_text.characters());
  699. GClipboard::the().set_data(selected_text);
  700. delete_selection();
  701. }
  702. void GTextEditor::copy()
  703. {
  704. auto selected_text = this->selected_text();
  705. printf("Copy: \"%s\"\n", selected_text.characters());
  706. GClipboard::the().set_data(selected_text);
  707. }
  708. void GTextEditor::paste()
  709. {
  710. auto paste_text = GClipboard::the().data();
  711. printf("Paste: \"%s\"\n", paste_text.characters());
  712. insert_at_cursor_or_replace_selection(paste_text);
  713. }