GTextEditor.cpp 23 KB

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