GTextEditor.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  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 <LibGUI/GWindow.h>
  7. #include <Kernel/KeyCode.h>
  8. #include <AK/StringBuilder.h>
  9. #include <unistd.h>
  10. #include <fcntl.h>
  11. #include <stdio.h>
  12. GTextEditor::GTextEditor(Type type, GWidget* parent)
  13. : GScrollableWidget(parent)
  14. , m_type(type)
  15. {
  16. set_frame_shape(GFrame::Shape::Container);
  17. set_frame_shadow(GFrame::Shadow::Sunken);
  18. set_frame_thickness(2);
  19. set_scrollbars_enabled(is_multi_line());
  20. m_ruler_visible = is_multi_line();
  21. set_font(GFontDatabase::the().get_by_name("Csilla Thin"));
  22. m_lines.append(make<Line>());
  23. m_cursor = { 0, 0 };
  24. }
  25. GTextEditor::~GTextEditor()
  26. {
  27. }
  28. void GTextEditor::set_text(const String& text)
  29. {
  30. if (is_single_line() && text.length() == m_lines[0]->length() && !memcmp(text.characters(), m_lines[0]->characters(), text.length()))
  31. return;
  32. m_lines.clear();
  33. int start_of_current_line = 0;
  34. auto add_line = [&] (int current_position) {
  35. int line_length = current_position - start_of_current_line;
  36. auto line = make<Line>();
  37. if (line_length)
  38. line->set_text(text.substring(start_of_current_line, current_position - start_of_current_line));
  39. m_lines.append(move(line));
  40. start_of_current_line = current_position + 1;
  41. };
  42. int i = 0;
  43. for (i = 0; i < text.length(); ++i) {
  44. if (text[i] == '\n')
  45. add_line(i);
  46. }
  47. add_line(i);
  48. update_content_size();
  49. if (is_single_line())
  50. set_cursor(0, m_lines[0]->length());
  51. else
  52. set_cursor(0, 0);
  53. update();
  54. }
  55. void GTextEditor::update_content_size()
  56. {
  57. int content_width = 0;
  58. for (auto& line : m_lines)
  59. content_width = max(line->width(font()), content_width);
  60. content_width += m_horizontal_content_padding * 2;
  61. int content_height = line_count() * line_height();
  62. set_content_size({ content_width, content_height });
  63. set_size_occupied_by_fixed_elements({ ruler_width(), 0 });
  64. }
  65. GTextPosition GTextEditor::text_position_at(const Point& a_position) const
  66. {
  67. auto position = a_position;
  68. position.move_by(horizontal_scrollbar().value(), vertical_scrollbar().value());
  69. position.move_by(-(m_horizontal_content_padding + ruler_width()), 0);
  70. int line_index = position.y() / line_height();
  71. int column_index = position.x() / glyph_width();
  72. line_index = max(0, min(line_index, line_count() - 1));
  73. column_index = max(0, min(column_index, m_lines[line_index]->length()));
  74. return { line_index, column_index };
  75. }
  76. void GTextEditor::mousedown_event(GMouseEvent& event)
  77. {
  78. if (event.button() == GMouseButton::Left) {
  79. if (event.modifiers() & Mod_Shift) {
  80. if (!has_selection())
  81. m_selection.set(m_cursor, { });
  82. } else {
  83. m_selection.clear();
  84. }
  85. m_in_drag_select = true;
  86. set_cursor(text_position_at(event.position()));
  87. if (!(event.modifiers() & Mod_Shift)) {
  88. if (!has_selection())
  89. m_selection.set(m_cursor, { });
  90. }
  91. if (m_selection.start().is_valid())
  92. m_selection.set_end(m_cursor);
  93. // FIXME: Only update the relevant rects.
  94. update();
  95. return;
  96. }
  97. }
  98. void GTextEditor::mouseup_event(GMouseEvent& event)
  99. {
  100. if (event.button() == GMouseButton::Left) {
  101. if (m_in_drag_select) {
  102. m_in_drag_select = false;
  103. }
  104. return;
  105. }
  106. }
  107. void GTextEditor::mousemove_event(GMouseEvent& event)
  108. {
  109. if (m_in_drag_select) {
  110. set_cursor(text_position_at(event.position()));
  111. m_selection.set_end(m_cursor);
  112. update();
  113. return;
  114. }
  115. }
  116. int GTextEditor::ruler_width() const
  117. {
  118. if (!m_ruler_visible)
  119. return 0;
  120. // FIXME: Resize based on needed space.
  121. return 5 * font().glyph_width('x') + 4;
  122. }
  123. Rect GTextEditor::ruler_content_rect(int line_index) const
  124. {
  125. if (!m_ruler_visible)
  126. return { };
  127. return {
  128. 0 - ruler_width() + horizontal_scrollbar().value(),
  129. line_index * line_height(),
  130. ruler_width(),
  131. line_height()
  132. };
  133. }
  134. void GTextEditor::paint_event(GPaintEvent& event)
  135. {
  136. GFrame::paint_event(event);
  137. GPainter painter(*this);
  138. painter.add_clip_rect(widget_inner_rect());
  139. painter.add_clip_rect(event.rect());
  140. painter.fill_rect(event.rect(), Color::White);
  141. Rect ruler_rect { 0, 0, ruler_width(), height() - height_occupied_by_horizontal_scrollbar()};
  142. if (m_ruler_visible) {
  143. painter.fill_rect(ruler_rect, Color::LightGray);
  144. painter.draw_line(ruler_rect.top_right(), ruler_rect.bottom_right(), Color::DarkGray);
  145. }
  146. painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  147. if (m_ruler_visible)
  148. painter.translate(ruler_width(), 0);
  149. int exposed_width = max(content_width(), width());
  150. int first_visible_line = text_position_at(event.rect().top_left()).line();
  151. int last_visible_line = text_position_at(event.rect().bottom_right()).line();
  152. auto selection = normalized_selection();
  153. bool has_selection = selection.is_valid();
  154. if (m_ruler_visible) {
  155. for (int i = first_visible_line; i <= last_visible_line; ++i) {
  156. bool is_current_line = i == m_cursor.line();
  157. auto ruler_line_rect = ruler_content_rect(i);
  158. painter.draw_text(
  159. ruler_line_rect.shrunken(2, 0),
  160. String::format("%u", i),
  161. is_current_line ? Font::default_bold_font() : font(),
  162. TextAlignment::CenterRight,
  163. is_current_line ? Color::DarkGray : Color::MidGray
  164. );
  165. }
  166. }
  167. 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() });
  168. for (int i = first_visible_line; i <= last_visible_line; ++i) {
  169. auto& line = *m_lines[i];
  170. auto line_rect = line_content_rect(i);
  171. line_rect.set_width(exposed_width);
  172. if (is_multi_line() && i == m_cursor.line())
  173. painter.fill_rect(line_rect, Color(230, 230, 230));
  174. painter.draw_text(line_rect, line.characters(), line.length(), TextAlignment::CenterLeft, Color::Black);
  175. bool line_has_selection = has_selection && i >= selection.start().line() && i <= selection.end().line();
  176. if (line_has_selection) {
  177. int selection_start_column_on_line = selection.start().line() == i ? selection.start().column() : 0;
  178. int selection_end_column_on_line = selection.end().line() == i ? selection.end().column() : line.length();
  179. int selection_left = m_horizontal_content_padding + selection_start_column_on_line * font().glyph_width('x');
  180. int selection_right = line_rect.left() + selection_end_column_on_line * font().glyph_width('x');
  181. Rect selection_rect { selection_left, line_rect.y(), selection_right - selection_left, line_rect.height() };
  182. painter.fill_rect(selection_rect, Color::from_rgb(0x955233));
  183. 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);
  184. }
  185. }
  186. if (is_focused() && m_cursor_state)
  187. painter.fill_rect(cursor_content_rect(), Color::Red);
  188. }
  189. void GTextEditor::toggle_selection_if_needed_for_event(const GKeyEvent& event)
  190. {
  191. if (event.shift() && !m_selection.is_valid()) {
  192. m_selection.set(m_cursor, { });
  193. update();
  194. return;
  195. }
  196. if (!event.shift() && m_selection.is_valid()) {
  197. m_selection.clear();
  198. update();
  199. return;
  200. }
  201. }
  202. void GTextEditor::keydown_event(GKeyEvent& event)
  203. {
  204. if (event.key() == KeyCode::Key_Escape) {
  205. if (on_escape_pressed)
  206. on_escape_pressed(*this);
  207. return;
  208. }
  209. if (event.key() == KeyCode::Key_Up) {
  210. if (m_cursor.line() > 0) {
  211. int new_line = m_cursor.line() - 1;
  212. int new_column = min(m_cursor.column(), m_lines[new_line]->length());
  213. toggle_selection_if_needed_for_event(event);
  214. set_cursor(new_line, new_column);
  215. if (m_selection.start().is_valid())
  216. m_selection.set_end(m_cursor);
  217. }
  218. return;
  219. }
  220. if (event.key() == KeyCode::Key_Down) {
  221. if (m_cursor.line() < (m_lines.size() - 1)) {
  222. int new_line = m_cursor.line() + 1;
  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. if (m_selection.start().is_valid())
  227. m_selection.set_end(m_cursor);
  228. }
  229. return;
  230. }
  231. if (event.key() == KeyCode::Key_PageUp) {
  232. if (m_cursor.line() > 0) {
  233. int new_line = max(0, m_cursor.line() - visible_content_rect().height() / line_height());
  234. int new_column = min(m_cursor.column(), m_lines[new_line]->length());
  235. toggle_selection_if_needed_for_event(event);
  236. set_cursor(new_line, new_column);
  237. if (m_selection.start().is_valid())
  238. m_selection.set_end(m_cursor);
  239. }
  240. return;
  241. }
  242. if (event.key() == KeyCode::Key_PageDown) {
  243. if (m_cursor.line() < (m_lines.size() - 1)) {
  244. int new_line = min(line_count() - 1, m_cursor.line() + visible_content_rect().height() / line_height());
  245. int new_column = min(m_cursor.column(), m_lines[new_line]->length());
  246. toggle_selection_if_needed_for_event(event);
  247. set_cursor(new_line, new_column);
  248. if (m_selection.start().is_valid())
  249. m_selection.set_end(m_cursor);
  250. }
  251. return;
  252. }
  253. if (event.key() == KeyCode::Key_Left) {
  254. if (m_cursor.column() > 0) {
  255. int new_column = m_cursor.column() - 1;
  256. toggle_selection_if_needed_for_event(event);
  257. set_cursor(m_cursor.line(), new_column);
  258. if (m_selection.start().is_valid())
  259. m_selection.set_end(m_cursor);
  260. } else if (m_cursor.line() > 0) {
  261. int new_line = m_cursor.line() - 1;
  262. int new_column = m_lines[new_line]->length();
  263. toggle_selection_if_needed_for_event(event);
  264. set_cursor(new_line, new_column);
  265. if (m_selection.start().is_valid())
  266. m_selection.set_end(m_cursor);
  267. }
  268. return;
  269. }
  270. if (event.key() == KeyCode::Key_Right) {
  271. if (m_cursor.column() < current_line().length()) {
  272. int new_column = m_cursor.column() + 1;
  273. toggle_selection_if_needed_for_event(event);
  274. set_cursor(m_cursor.line(), new_column);
  275. if (m_selection.start().is_valid())
  276. m_selection.set_end(m_cursor);
  277. } else if (m_cursor.line() != line_count() - 1) {
  278. int new_line = m_cursor.line() + 1;
  279. int new_column = 0;
  280. toggle_selection_if_needed_for_event(event);
  281. set_cursor(new_line, new_column);
  282. if (m_selection.start().is_valid())
  283. m_selection.set_end(m_cursor);
  284. }
  285. return;
  286. }
  287. if (!event.ctrl() && event.key() == KeyCode::Key_Home) {
  288. toggle_selection_if_needed_for_event(event);
  289. set_cursor(m_cursor.line(), 0);
  290. if (m_selection.start().is_valid())
  291. m_selection.set_end(m_cursor);
  292. return;
  293. }
  294. if (!event.ctrl() && event.key() == KeyCode::Key_End) {
  295. toggle_selection_if_needed_for_event(event);
  296. set_cursor(m_cursor.line(), current_line().length());
  297. if (m_selection.start().is_valid())
  298. m_selection.set_end(m_cursor);
  299. return;
  300. }
  301. if (event.ctrl() && event.key() == KeyCode::Key_Home) {
  302. toggle_selection_if_needed_for_event(event);
  303. set_cursor(0, 0);
  304. if (m_selection.start().is_valid())
  305. m_selection.set_end(m_cursor);
  306. return;
  307. }
  308. if (event.ctrl() && event.key() == KeyCode::Key_End) {
  309. toggle_selection_if_needed_for_event(event);
  310. set_cursor(line_count() - 1, m_lines[line_count() - 1]->length());
  311. if (m_selection.start().is_valid())
  312. m_selection.set_end(m_cursor);
  313. return;
  314. }
  315. if (event.modifiers() == Mod_Ctrl && event.key() == KeyCode::Key_A) {
  316. GTextPosition start_of_document { 0, 0 };
  317. GTextPosition end_of_document { line_count() - 1, m_lines[line_count() - 1]->length() };
  318. m_selection.set(start_of_document, end_of_document);
  319. set_cursor(end_of_document);
  320. update();
  321. return;
  322. }
  323. if (event.key() == KeyCode::Key_Backspace) {
  324. if (has_selection()) {
  325. delete_selection();
  326. return;
  327. }
  328. if (m_cursor.column() > 0) {
  329. // Backspace within line
  330. current_line().remove(m_cursor.column() - 1);
  331. update_content_size();
  332. set_cursor(m_cursor.line(), m_cursor.column() - 1);
  333. return;
  334. }
  335. if (m_cursor.column() == 0 && m_cursor.line() != 0) {
  336. // Backspace at column 0; merge with previous line
  337. auto& previous_line = *m_lines[m_cursor.line() - 1];
  338. int previous_length = previous_line.length();
  339. previous_line.append(current_line().characters(), current_line().length());
  340. m_lines.remove(m_cursor.line());
  341. update_content_size();
  342. update();
  343. set_cursor(m_cursor.line() - 1, previous_length);
  344. return;
  345. }
  346. return;
  347. }
  348. if (event.modifiers() == Mod_Shift && event.key() == KeyCode::Key_Delete) {
  349. delete_current_line();
  350. return;
  351. }
  352. if (event.key() == KeyCode::Key_Delete) {
  353. do_delete();
  354. return;
  355. }
  356. if (!event.ctrl() && !event.alt() && !event.text().is_empty())
  357. insert_at_cursor_or_replace_selection(event.text());
  358. return GWidget::keydown_event(event);
  359. }
  360. void GTextEditor::delete_current_line()
  361. {
  362. if (has_selection())
  363. return delete_selection();
  364. m_lines.remove(m_cursor.line());
  365. if (m_lines.is_empty())
  366. m_lines.append(make<Line>());
  367. update_content_size();
  368. update();
  369. }
  370. void GTextEditor::do_delete()
  371. {
  372. if (has_selection())
  373. return delete_selection();
  374. if (m_cursor.column() < current_line().length()) {
  375. // Delete within line
  376. current_line().remove(m_cursor.column());
  377. update_content_size();
  378. update_cursor();
  379. return;
  380. }
  381. if (m_cursor.column() == current_line().length() && m_cursor.line() != line_count() - 1) {
  382. // Delete at end of line; merge with next line
  383. auto& next_line = *m_lines[m_cursor.line() + 1];
  384. int previous_length = current_line().length();
  385. current_line().append(next_line.characters(), next_line.length());
  386. m_lines.remove(m_cursor.line() + 1);
  387. update_content_size();
  388. update();
  389. set_cursor(m_cursor.line(), previous_length);
  390. return;
  391. }
  392. }
  393. void GTextEditor::insert_at_cursor(const String& text)
  394. {
  395. // FIXME: This should obviously not be implemented this way.
  396. for (int i = 0; i < text.length(); ++i) {
  397. insert_at_cursor(text[i]);
  398. }
  399. }
  400. void GTextEditor::insert_at_cursor(char ch)
  401. {
  402. bool at_head = m_cursor.column() == 0;
  403. bool at_tail = m_cursor.column() == current_line().length();
  404. if (ch == '\n') {
  405. if (is_single_line()) {
  406. if (on_return_pressed)
  407. on_return_pressed(*this);
  408. return;
  409. }
  410. if (at_tail || at_head) {
  411. m_lines.insert(m_cursor.line() + (at_tail ? 1 : 0), make<Line>());
  412. update_content_size();
  413. update();
  414. set_cursor(m_cursor.line() + 1, 0);
  415. return;
  416. }
  417. auto new_line = make<Line>();
  418. new_line->append(current_line().characters() + m_cursor.column(), current_line().length() - m_cursor.column());
  419. current_line().truncate(m_cursor.column());
  420. m_lines.insert(m_cursor.line() + 1, move(new_line));
  421. update_content_size();
  422. update();
  423. set_cursor(m_cursor.line() + 1, 0);
  424. return;
  425. }
  426. if (ch == '\t') {
  427. int next_soft_tab_stop = ((m_cursor.column() + m_soft_tab_width) / m_soft_tab_width) * m_soft_tab_width;
  428. int spaces_to_insert = next_soft_tab_stop - m_cursor.column();
  429. for (int i = 0; i < spaces_to_insert; ++i) {
  430. current_line().insert(m_cursor.column(), ' ');
  431. }
  432. update_content_size();
  433. set_cursor(m_cursor.line(), next_soft_tab_stop);
  434. update_cursor();
  435. return;
  436. }
  437. current_line().insert(m_cursor.column(), ch);
  438. update_content_size();
  439. set_cursor(m_cursor.line(), m_cursor.column() + 1);
  440. update_cursor();
  441. }
  442. Rect GTextEditor::cursor_content_rect() const
  443. {
  444. if (!m_cursor.is_valid())
  445. return { };
  446. ASSERT(!m_lines.is_empty());
  447. ASSERT(m_cursor.column() <= (current_line().length() + 1));
  448. if (is_single_line()) {
  449. Rect cursor_rect = { m_horizontal_content_padding + m_cursor.column() * glyph_width(), 0, 1, font().glyph_height() + 2 };
  450. cursor_rect.center_vertically_within(rect());
  451. // FIXME: This would not be necessary if we knew more about the font and could center it based on baseline and x-height.
  452. cursor_rect.move_by(0, 1);
  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() + ruler_width() + 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(frame_inner_rect().right());
  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() + 2 };
  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. }
  726. void GTextEditor::enter_event(GEvent&)
  727. {
  728. ASSERT(window());
  729. window()->set_override_cursor(GStandardCursor::IBeam);
  730. }
  731. void GTextEditor::leave_event(GEvent&)
  732. {
  733. ASSERT(window());
  734. window()->set_override_cursor(GStandardCursor::None);
  735. }