GTextEditor.cpp 25 KB

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