GTextEditor.cpp 25 KB

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