GTextEditor.cpp 28 KB

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