GTextEditor.cpp 26 KB

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