GTextEditor.cpp 26 KB

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