GTextEditor.cpp 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419
  1. #include <AK/QuickSort.h>
  2. #include <AK/StringBuilder.h>
  3. #include <Kernel/KeyCode.h>
  4. #include <LibGUI/GAction.h>
  5. #include <LibGUI/GClipboard.h>
  6. #include <LibGUI/GFontDatabase.h>
  7. #include <LibGUI/GMenu.h>
  8. #include <LibGUI/GPainter.h>
  9. #include <LibGUI/GScrollBar.h>
  10. #include <LibGUI/GTextEditor.h>
  11. #include <LibGUI/GWindow.h>
  12. #include <ctype.h>
  13. #include <fcntl.h>
  14. #include <stdio.h>
  15. #include <unistd.h>
  16. //#define DEBUG_GTEXTEDITOR
  17. GTextEditor::GTextEditor(GWidget* parent)
  18. : GTextEditor(Type::MultiLine, parent)
  19. {
  20. }
  21. GTextEditor::GTextEditor(Type type, GWidget* parent)
  22. : GScrollableWidget(parent)
  23. , m_type(type)
  24. {
  25. set_document(GTextDocument::create());
  26. set_frame_shape(FrameShape::Container);
  27. set_frame_shadow(FrameShadow::Sunken);
  28. set_frame_thickness(2);
  29. set_scrollbars_enabled(is_multi_line());
  30. set_font(GFontDatabase::the().get_by_name("Csilla Thin"));
  31. // FIXME: Recompute vertical scrollbar step size on font change.
  32. vertical_scrollbar().set_step(line_height());
  33. m_cursor = { 0, 0 };
  34. create_actions();
  35. }
  36. GTextEditor::~GTextEditor()
  37. {
  38. if (m_document)
  39. m_document->unregister_client(*this);
  40. }
  41. void GTextEditor::create_actions()
  42. {
  43. m_undo_action = GCommonActions::make_undo_action([&](auto&) { undo(); }, this);
  44. m_redo_action = GCommonActions::make_redo_action([&](auto&) { redo(); }, this);
  45. m_undo_action->set_enabled(false);
  46. m_redo_action->set_enabled(false);
  47. m_cut_action = GCommonActions::make_cut_action([&](auto&) { cut(); }, this);
  48. m_copy_action = GCommonActions::make_copy_action([&](auto&) { copy(); }, this);
  49. m_paste_action = GCommonActions::make_paste_action([&](auto&) { paste(); }, this);
  50. m_delete_action = GCommonActions::make_delete_action([&](auto&) { do_delete(); }, this);
  51. }
  52. void GTextEditor::set_text(const StringView& text)
  53. {
  54. if (is_single_line() && text.length() == line(0).length() && !memcmp(text.characters_without_null_termination(), line(0).characters(), text.length()))
  55. return;
  56. m_selection.clear();
  57. document().set_text(text);
  58. update_content_size();
  59. recompute_all_visual_lines();
  60. if (is_single_line())
  61. set_cursor(0, line(0).length());
  62. else
  63. set_cursor(0, 0);
  64. did_update_selection();
  65. update();
  66. }
  67. void GTextEditor::update_content_size()
  68. {
  69. int content_width = 0;
  70. int content_height = 0;
  71. for (auto& line : m_line_visual_data) {
  72. content_width = max(line.visual_rect.width(), content_width);
  73. content_height += line.visual_rect.height();
  74. }
  75. content_width += m_horizontal_content_padding * 2;
  76. if (is_right_text_alignment(m_text_alignment))
  77. content_width = max(frame_inner_rect().width(), content_width);
  78. set_content_size({ content_width, content_height });
  79. set_size_occupied_by_fixed_elements({ ruler_width(), 0 });
  80. }
  81. GTextPosition GTextEditor::text_position_at(const Point& a_position) const
  82. {
  83. auto position = a_position;
  84. position.move_by(horizontal_scrollbar().value(), vertical_scrollbar().value());
  85. position.move_by(-(m_horizontal_content_padding + ruler_width()), 0);
  86. position.move_by(-frame_thickness(), -frame_thickness());
  87. int line_index = -1;
  88. if (is_line_wrapping_enabled()) {
  89. for (int i = 0; i < lines().size(); ++i) {
  90. auto& rect = m_line_visual_data[i].visual_rect;
  91. if (position.y() >= rect.top() && position.y() <= rect.bottom()) {
  92. line_index = i;
  93. break;
  94. }
  95. if (position.y() > rect.bottom())
  96. line_index = lines().size() - 1;
  97. }
  98. } else {
  99. line_index = position.y() / line_height();
  100. }
  101. line_index = max(0, min(line_index, line_count() - 1));
  102. int column_index;
  103. switch (m_text_alignment) {
  104. case TextAlignment::CenterLeft:
  105. column_index = (position.x() + glyph_width() / 2) / glyph_width();
  106. if (is_line_wrapping_enabled()) {
  107. for_each_visual_line(line_index, [&](const Rect& rect, const StringView&, int start_of_line) {
  108. if (rect.contains_vertically(position.y())) {
  109. column_index += start_of_line;
  110. return IterationDecision::Break;
  111. }
  112. return IterationDecision::Continue;
  113. });
  114. }
  115. break;
  116. case TextAlignment::CenterRight:
  117. // FIXME: Support right-aligned line wrapping, I guess.
  118. ASSERT(!is_line_wrapping_enabled());
  119. column_index = (position.x() - content_x_for_position({ line_index, 0 }) + glyph_width() / 2) / glyph_width();
  120. break;
  121. default:
  122. ASSERT_NOT_REACHED();
  123. }
  124. column_index = max(0, min(column_index, lines()[line_index].length()));
  125. return { line_index, column_index };
  126. }
  127. void GTextEditor::doubleclick_event(GMouseEvent& event)
  128. {
  129. if (event.button() != GMouseButton::Left)
  130. return;
  131. // NOTE: This ensures that spans are updated before we look at them.
  132. flush_pending_change_notification_if_needed();
  133. m_triple_click_timer.start();
  134. m_in_drag_select = false;
  135. auto start = text_position_at(event.position());
  136. auto end = start;
  137. auto& line = lines()[start.line()];
  138. if (!document().has_spans()) {
  139. while (start.column() > 0) {
  140. if (isspace(line.characters()[start.column() - 1]))
  141. break;
  142. start.set_column(start.column() - 1);
  143. }
  144. while (end.column() < line.length()) {
  145. if (isspace(line.characters()[end.column()]))
  146. break;
  147. end.set_column(end.column() + 1);
  148. }
  149. } else {
  150. for (auto& span : document().spans()) {
  151. if (!span.range.contains(start))
  152. continue;
  153. start = span.range.start();
  154. end = span.range.end();
  155. end.set_column(end.column() + 1);
  156. break;
  157. }
  158. }
  159. m_selection.set(start, end);
  160. set_cursor(end);
  161. update();
  162. did_update_selection();
  163. }
  164. void GTextEditor::mousedown_event(GMouseEvent& event)
  165. {
  166. if (event.button() != GMouseButton::Left) {
  167. return;
  168. }
  169. if (m_triple_click_timer.is_valid() && m_triple_click_timer.elapsed() < 250) {
  170. m_triple_click_timer = CElapsedTimer();
  171. GTextPosition start;
  172. GTextPosition end;
  173. if (is_multi_line()) {
  174. // select *current* line
  175. start = GTextPosition(m_cursor.line(), 0);
  176. end = GTextPosition(m_cursor.line(), lines()[m_cursor.line()].length());
  177. } else {
  178. // select *whole* line
  179. start = GTextPosition(0, 0);
  180. end = GTextPosition(line_count() - 1, lines()[line_count() - 1].length());
  181. }
  182. m_selection.set(start, end);
  183. set_cursor(end);
  184. return;
  185. }
  186. if (event.modifiers() & Mod_Shift) {
  187. if (!has_selection())
  188. m_selection.set(m_cursor, {});
  189. } else {
  190. m_selection.clear();
  191. }
  192. m_in_drag_select = true;
  193. set_cursor(text_position_at(event.position()));
  194. if (!(event.modifiers() & Mod_Shift)) {
  195. if (!has_selection())
  196. m_selection.set(m_cursor, {});
  197. }
  198. if (m_selection.start().is_valid() && m_selection.start() != m_cursor)
  199. m_selection.set_end(m_cursor);
  200. // FIXME: Only update the relevant rects.
  201. update();
  202. did_update_selection();
  203. }
  204. void GTextEditor::mouseup_event(GMouseEvent& event)
  205. {
  206. if (event.button() == GMouseButton::Left) {
  207. if (m_in_drag_select) {
  208. m_in_drag_select = false;
  209. }
  210. return;
  211. }
  212. }
  213. void GTextEditor::mousemove_event(GMouseEvent& event)
  214. {
  215. if (m_in_drag_select) {
  216. set_cursor(text_position_at(event.position()));
  217. m_selection.set_end(m_cursor);
  218. did_update_selection();
  219. update();
  220. return;
  221. }
  222. }
  223. int GTextEditor::ruler_width() const
  224. {
  225. if (!m_ruler_visible)
  226. return 0;
  227. // FIXME: Resize based on needed space.
  228. return 5 * font().glyph_width('x') + 4;
  229. }
  230. Rect GTextEditor::ruler_content_rect(int line_index) const
  231. {
  232. if (!m_ruler_visible)
  233. return {};
  234. return {
  235. 0 - ruler_width() + horizontal_scrollbar().value(),
  236. line_content_rect(line_index).y(),
  237. ruler_width(),
  238. line_content_rect(line_index).height()
  239. };
  240. }
  241. Rect GTextEditor::ruler_rect_in_inner_coordinates() const
  242. {
  243. return { 0, 0, ruler_width(), height() - height_occupied_by_horizontal_scrollbar() };
  244. }
  245. Rect GTextEditor::visible_text_rect_in_inner_coordinates() const
  246. {
  247. return {
  248. m_horizontal_content_padding + (m_ruler_visible ? (ruler_rect_in_inner_coordinates().right() + 1) : 0),
  249. 0,
  250. frame_inner_rect().width() - (m_horizontal_content_padding * 2) - width_occupied_by_vertical_scrollbar() - ruler_width(),
  251. frame_inner_rect().height() - height_occupied_by_horizontal_scrollbar()
  252. };
  253. }
  254. void GTextEditor::paint_event(GPaintEvent& event)
  255. {
  256. // NOTE: This ensures that spans are updated before we look at them.
  257. flush_pending_change_notification_if_needed();
  258. GFrame::paint_event(event);
  259. GPainter painter(*this);
  260. painter.add_clip_rect(widget_inner_rect());
  261. painter.add_clip_rect(event.rect());
  262. painter.fill_rect(event.rect(), Color::White);
  263. painter.translate(frame_thickness(), frame_thickness());
  264. auto ruler_rect = ruler_rect_in_inner_coordinates();
  265. if (m_ruler_visible) {
  266. painter.fill_rect(ruler_rect, Color::WarmGray);
  267. painter.draw_line(ruler_rect.top_right(), ruler_rect.bottom_right(), Color::DarkGray);
  268. }
  269. painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  270. if (m_ruler_visible)
  271. painter.translate(ruler_width(), 0);
  272. int first_visible_line = text_position_at(event.rect().top_left()).line();
  273. int last_visible_line = text_position_at(event.rect().bottom_right()).line();
  274. auto selection = normalized_selection();
  275. bool has_selection = selection.is_valid();
  276. if (m_ruler_visible) {
  277. for (int i = first_visible_line; i <= last_visible_line; ++i) {
  278. bool is_current_line = i == m_cursor.line();
  279. auto ruler_line_rect = ruler_content_rect(i);
  280. painter.draw_text(
  281. ruler_line_rect.shrunken(2, 0).translated(0, m_line_spacing / 2),
  282. String::number(i + 1),
  283. is_current_line ? Font::default_bold_font() : font(),
  284. TextAlignment::TopRight,
  285. is_current_line ? Color::DarkGray : Color::MidGray);
  286. }
  287. }
  288. Rect text_clip_rect {
  289. (m_ruler_visible ? (ruler_rect_in_inner_coordinates().right() + frame_thickness() + 1) : frame_thickness()),
  290. frame_thickness(),
  291. width() - width_occupied_by_vertical_scrollbar() - ruler_width(),
  292. height() - height_occupied_by_horizontal_scrollbar()
  293. };
  294. painter.add_clip_rect(text_clip_rect);
  295. for (int line_index = first_visible_line; line_index <= last_visible_line; ++line_index) {
  296. auto& line = lines()[line_index];
  297. bool physical_line_has_selection = has_selection && line_index >= selection.start().line() && line_index <= selection.end().line();
  298. int first_visual_line_with_selection = -1;
  299. int last_visual_line_with_selection = -1;
  300. if (physical_line_has_selection) {
  301. if (selection.start().line() < line_index)
  302. first_visual_line_with_selection = 0;
  303. else
  304. first_visual_line_with_selection = visual_line_containing(line_index, selection.start().column());
  305. if (selection.end().line() > line_index)
  306. last_visual_line_with_selection = m_line_visual_data[line_index].visual_line_breaks.size();
  307. else
  308. last_visual_line_with_selection = visual_line_containing(line_index, selection.end().column());
  309. }
  310. int selection_start_column_within_line = selection.start().line() == line_index ? selection.start().column() : 0;
  311. int selection_end_column_within_line = selection.end().line() == line_index ? selection.end().column() : line.length();
  312. int visual_line_index = 0;
  313. for_each_visual_line(line_index, [&](const Rect& visual_line_rect, const StringView& visual_line_text, int start_of_visual_line) {
  314. if (is_multi_line() && line_index == m_cursor.line())
  315. painter.fill_rect(visual_line_rect, Color(230, 230, 230));
  316. #ifdef DEBUG_GTEXTEDITOR
  317. painter.draw_rect(visual_line_rect, Color::Cyan);
  318. #endif
  319. if (!document().has_spans()) {
  320. // Fast-path for plain text
  321. painter.draw_text(visual_line_rect, visual_line_text, m_text_alignment, Color::Black);
  322. } else {
  323. int advance = font().glyph_width(' ') + font().glyph_spacing();
  324. Rect character_rect = { visual_line_rect.location(), { font().glyph_width(' '), line_height() } };
  325. for (int i = 0; i < visual_line_text.length(); ++i) {
  326. const Font* font = &this->font();
  327. Color color;
  328. Optional<Color> background_color;
  329. GTextPosition physical_position(line_index, start_of_visual_line + i);
  330. // FIXME: This is *horribly* inefficient.
  331. for (auto& span : document().spans()) {
  332. if (!span.range.contains(physical_position))
  333. continue;
  334. color = span.color;
  335. if (span.font)
  336. font = span.font;
  337. background_color = span.background_color;
  338. break;
  339. }
  340. if (background_color.has_value())
  341. painter.fill_rect(character_rect, background_color.value());
  342. painter.draw_text(character_rect, visual_line_text.substring_view(i, 1), *font, m_text_alignment, color);
  343. character_rect.move_by(advance, 0);
  344. }
  345. }
  346. bool physical_line_has_selection = has_selection && line_index >= selection.start().line() && line_index <= selection.end().line();
  347. if (physical_line_has_selection) {
  348. bool current_visual_line_has_selection = (line_index != selection.start().line() && line_index != selection.end().line())
  349. || (visual_line_index >= first_visual_line_with_selection && visual_line_index <= last_visual_line_with_selection);
  350. if (current_visual_line_has_selection) {
  351. bool selection_begins_on_current_visual_line = visual_line_index == first_visual_line_with_selection;
  352. bool selection_ends_on_current_visual_line = visual_line_index == last_visual_line_with_selection;
  353. int selection_left = selection_begins_on_current_visual_line
  354. ? content_x_for_position({ line_index, selection_start_column_within_line })
  355. : m_horizontal_content_padding;
  356. int selection_right = selection_ends_on_current_visual_line
  357. ? content_x_for_position({ line_index, selection_end_column_within_line })
  358. : visual_line_rect.right() + 1;
  359. Rect selection_rect {
  360. selection_left,
  361. visual_line_rect.y(),
  362. selection_right - selection_left,
  363. visual_line_rect.height()
  364. };
  365. painter.fill_rect(selection_rect, Color::from_rgb(0x955233));
  366. int start_of_selection_within_visual_line = max(0, selection_start_column_within_line - start_of_visual_line);
  367. int end_of_selection_within_visual_line = selection_end_column_within_line - start_of_visual_line;
  368. StringView visual_selected_text {
  369. visual_line_text.characters_without_null_termination() + start_of_selection_within_visual_line,
  370. end_of_selection_within_visual_line - start_of_selection_within_visual_line
  371. };
  372. painter.draw_text(selection_rect, visual_selected_text, TextAlignment::CenterLeft, Color::White);
  373. }
  374. }
  375. ++visual_line_index;
  376. return IterationDecision::Continue;
  377. });
  378. }
  379. if (is_focused() && m_cursor_state)
  380. painter.fill_rect(cursor_content_rect(), Color::Red);
  381. }
  382. void GTextEditor::toggle_selection_if_needed_for_event(const GKeyEvent& event)
  383. {
  384. if (event.shift() && !m_selection.is_valid()) {
  385. m_selection.set(m_cursor, {});
  386. did_update_selection();
  387. update();
  388. return;
  389. }
  390. if (!event.shift() && m_selection.is_valid()) {
  391. m_selection.clear();
  392. did_update_selection();
  393. update();
  394. return;
  395. }
  396. }
  397. void GTextEditor::select_all()
  398. {
  399. GTextPosition start_of_document { 0, 0 };
  400. GTextPosition end_of_document { line_count() - 1, lines()[line_count() - 1].length() };
  401. m_selection.set(start_of_document, end_of_document);
  402. did_update_selection();
  403. set_cursor(end_of_document);
  404. update();
  405. }
  406. void GTextEditor::get_selection_line_boundaries(int& first_line, int& last_line)
  407. {
  408. auto selection = normalized_selection();
  409. if (!selection.is_valid()) {
  410. first_line = m_cursor.line();
  411. last_line = m_cursor.line();
  412. return;
  413. }
  414. first_line = selection.start().line();
  415. last_line = selection.end().line();
  416. if (first_line != last_line && selection.end().column() == 0)
  417. last_line -= 1;
  418. }
  419. void GTextEditor::move_selected_lines_up()
  420. {
  421. int first_line;
  422. int last_line;
  423. get_selection_line_boundaries(first_line, last_line);
  424. if (first_line == 0)
  425. return;
  426. auto& lines = document().lines();
  427. lines.insert(last_line, lines.take(first_line - 1));
  428. m_cursor = { first_line - 1, 0 };
  429. if (has_selection()) {
  430. m_selection.set_start({ first_line - 1, 0 });
  431. m_selection.set_end({ last_line - 1, line(last_line - 1).length() });
  432. }
  433. did_change();
  434. update();
  435. }
  436. void GTextEditor::move_selected_lines_down()
  437. {
  438. int first_line;
  439. int last_line;
  440. get_selection_line_boundaries(first_line, last_line);
  441. auto& lines = document().lines();
  442. if (last_line >= (lines.size() - 1))
  443. return;
  444. lines.insert(first_line, lines.take(last_line + 1));
  445. m_cursor = { first_line + 1, 0 };
  446. if (has_selection()) {
  447. m_selection.set_start({ first_line + 1, 0 });
  448. m_selection.set_end({ last_line + 1, line(last_line + 1).length() });
  449. }
  450. did_change();
  451. update();
  452. }
  453. void GTextEditor::sort_selected_lines()
  454. {
  455. if (is_readonly())
  456. return;
  457. if (!has_selection())
  458. return;
  459. int first_line;
  460. int last_line;
  461. get_selection_line_boundaries(first_line, last_line);
  462. auto& lines = document().lines();
  463. auto start = lines.begin() + first_line;
  464. auto end = lines.begin() + last_line + 1;
  465. quick_sort(start, end, [](auto& a, auto& b) {
  466. return strcmp(a.characters(), b.characters()) < 0;
  467. });
  468. did_change();
  469. update();
  470. }
  471. void GTextEditor::keydown_event(GKeyEvent& event)
  472. {
  473. if (is_single_line() && event.key() == KeyCode::Key_Tab)
  474. return GWidget::keydown_event(event);
  475. if (is_single_line() && event.key() == KeyCode::Key_Return) {
  476. if (on_return_pressed)
  477. on_return_pressed();
  478. return;
  479. }
  480. if (event.key() == KeyCode::Key_Escape) {
  481. if (on_escape_pressed)
  482. on_escape_pressed();
  483. return;
  484. }
  485. if (event.key() == KeyCode::Key_Up) {
  486. if (m_cursor.line() > 0) {
  487. if (event.ctrl() && event.shift()) {
  488. move_selected_lines_up();
  489. return;
  490. }
  491. int new_line = m_cursor.line() - 1;
  492. int new_column = min(m_cursor.column(), lines()[new_line].length());
  493. toggle_selection_if_needed_for_event(event);
  494. set_cursor(new_line, new_column);
  495. if (event.shift() && m_selection.start().is_valid()) {
  496. m_selection.set_end(m_cursor);
  497. did_update_selection();
  498. }
  499. }
  500. return;
  501. }
  502. if (event.key() == KeyCode::Key_Down) {
  503. if (m_cursor.line() < (lines().size() - 1)) {
  504. if (event.ctrl() && event.shift()) {
  505. move_selected_lines_down();
  506. return;
  507. }
  508. int new_line = m_cursor.line() + 1;
  509. int new_column = min(m_cursor.column(), lines()[new_line].length());
  510. toggle_selection_if_needed_for_event(event);
  511. set_cursor(new_line, new_column);
  512. if (event.shift() && m_selection.start().is_valid()) {
  513. m_selection.set_end(m_cursor);
  514. did_update_selection();
  515. }
  516. }
  517. return;
  518. }
  519. if (event.key() == KeyCode::Key_PageUp) {
  520. if (m_cursor.line() > 0) {
  521. int new_line = max(0, m_cursor.line() - visible_content_rect().height() / line_height());
  522. int new_column = min(m_cursor.column(), lines()[new_line].length());
  523. toggle_selection_if_needed_for_event(event);
  524. set_cursor(new_line, new_column);
  525. if (event.shift() && m_selection.start().is_valid()) {
  526. m_selection.set_end(m_cursor);
  527. did_update_selection();
  528. }
  529. }
  530. return;
  531. }
  532. if (event.key() == KeyCode::Key_PageDown) {
  533. if (m_cursor.line() < (lines().size() - 1)) {
  534. int new_line = min(line_count() - 1, m_cursor.line() + visible_content_rect().height() / line_height());
  535. int new_column = min(m_cursor.column(), lines()[new_line].length());
  536. toggle_selection_if_needed_for_event(event);
  537. set_cursor(new_line, new_column);
  538. if (event.shift() && m_selection.start().is_valid()) {
  539. m_selection.set_end(m_cursor);
  540. did_update_selection();
  541. }
  542. }
  543. return;
  544. }
  545. if (event.key() == KeyCode::Key_Left) {
  546. if (event.ctrl() && document().has_spans()) {
  547. // FIXME: Do something nice when the document has no spans.
  548. auto span = document().first_non_skippable_span_before(m_cursor);
  549. GTextPosition new_cursor = !span.has_value()
  550. ? GTextPosition(0, 0)
  551. : span.value().range.start();
  552. toggle_selection_if_needed_for_event(event);
  553. set_cursor(new_cursor);
  554. if (event.shift() && m_selection.start().is_valid()) {
  555. m_selection.set_end(m_cursor);
  556. did_update_selection();
  557. }
  558. return;
  559. }
  560. if (m_cursor.column() > 0) {
  561. int new_column = m_cursor.column() - 1;
  562. toggle_selection_if_needed_for_event(event);
  563. set_cursor(m_cursor.line(), new_column);
  564. if (event.shift() && m_selection.start().is_valid()) {
  565. m_selection.set_end(m_cursor);
  566. did_update_selection();
  567. }
  568. } else if (m_cursor.line() > 0) {
  569. int new_line = m_cursor.line() - 1;
  570. int new_column = lines()[new_line].length();
  571. toggle_selection_if_needed_for_event(event);
  572. set_cursor(new_line, new_column);
  573. if (event.shift() && m_selection.start().is_valid()) {
  574. m_selection.set_end(m_cursor);
  575. did_update_selection();
  576. }
  577. }
  578. return;
  579. }
  580. if (event.key() == KeyCode::Key_Right) {
  581. if (event.ctrl() && document().has_spans()) {
  582. // FIXME: Do something nice when the document has no spans.
  583. auto span = document().first_non_skippable_span_after(m_cursor);
  584. GTextPosition new_cursor = !span.has_value()
  585. ? document().spans().last().range.end()
  586. : span.value().range.start();
  587. toggle_selection_if_needed_for_event(event);
  588. set_cursor(new_cursor);
  589. if (event.shift() && m_selection.start().is_valid()) {
  590. m_selection.set_end(m_cursor);
  591. did_update_selection();
  592. }
  593. return;
  594. }
  595. int new_line = m_cursor.line();
  596. int new_column = m_cursor.column();
  597. if (m_cursor.column() < current_line().length()) {
  598. new_line = m_cursor.line();
  599. new_column = m_cursor.column() + 1;
  600. } else if (m_cursor.line() != line_count() - 1) {
  601. new_line = m_cursor.line() + 1;
  602. new_column = 0;
  603. }
  604. toggle_selection_if_needed_for_event(event);
  605. set_cursor(new_line, new_column);
  606. if (event.shift() && m_selection.start().is_valid()) {
  607. m_selection.set_end(m_cursor);
  608. did_update_selection();
  609. }
  610. return;
  611. }
  612. if (!event.ctrl() && event.key() == KeyCode::Key_Home) {
  613. int first_nonspace_column = current_line().first_non_whitespace_column();
  614. toggle_selection_if_needed_for_event(event);
  615. if (m_cursor.column() == first_nonspace_column)
  616. set_cursor(m_cursor.line(), 0);
  617. else
  618. set_cursor(m_cursor.line(), first_nonspace_column);
  619. if (event.shift() && m_selection.start().is_valid()) {
  620. m_selection.set_end(m_cursor);
  621. did_update_selection();
  622. }
  623. return;
  624. }
  625. if (!event.ctrl() && event.key() == KeyCode::Key_End) {
  626. toggle_selection_if_needed_for_event(event);
  627. set_cursor(m_cursor.line(), current_line().length());
  628. if (event.shift() && m_selection.start().is_valid()) {
  629. m_selection.set_end(m_cursor);
  630. did_update_selection();
  631. }
  632. return;
  633. }
  634. if (event.ctrl() && event.key() == KeyCode::Key_Home) {
  635. toggle_selection_if_needed_for_event(event);
  636. set_cursor(0, 0);
  637. if (event.shift() && m_selection.start().is_valid()) {
  638. m_selection.set_end(m_cursor);
  639. did_update_selection();
  640. }
  641. return;
  642. }
  643. if (event.ctrl() && event.key() == KeyCode::Key_End) {
  644. toggle_selection_if_needed_for_event(event);
  645. set_cursor(line_count() - 1, lines()[line_count() - 1].length());
  646. if (event.shift() && m_selection.start().is_valid()) {
  647. m_selection.set_end(m_cursor);
  648. did_update_selection();
  649. }
  650. return;
  651. }
  652. if (event.modifiers() == Mod_Ctrl && event.key() == KeyCode::Key_A) {
  653. select_all();
  654. return;
  655. }
  656. if (event.alt() && event.shift() && event.key() == KeyCode::Key_S) {
  657. sort_selected_lines();
  658. return;
  659. }
  660. if (event.key() == KeyCode::Key_Backspace) {
  661. if (is_readonly())
  662. return;
  663. if (has_selection()) {
  664. delete_selection();
  665. did_update_selection();
  666. return;
  667. }
  668. if (m_cursor.column() > 0) {
  669. int erase_count = 1;
  670. if (current_line().first_non_whitespace_column() >= m_cursor.column()) {
  671. int new_column;
  672. if (m_cursor.column() % m_soft_tab_width == 0)
  673. new_column = m_cursor.column() - m_soft_tab_width;
  674. else
  675. new_column = (m_cursor.column() / m_soft_tab_width) * m_soft_tab_width;
  676. erase_count = m_cursor.column() - new_column;
  677. }
  678. // Backspace within line
  679. GTextRange erased_range({ m_cursor.line(), m_cursor.column() - erase_count }, m_cursor);
  680. auto erased_text = document().text_in_range(erased_range);
  681. execute<RemoveTextCommand>(erased_text, erased_range);
  682. return;
  683. }
  684. if (m_cursor.column() == 0 && m_cursor.line() != 0) {
  685. // Backspace at column 0; merge with previous line
  686. int previous_length = line(m_cursor.line() - 1).length();
  687. GTextRange erased_range({ m_cursor.line() - 1, previous_length }, m_cursor);
  688. execute<RemoveTextCommand>("\n", erased_range);
  689. return;
  690. }
  691. return;
  692. }
  693. if (event.modifiers() == Mod_Shift && event.key() == KeyCode::Key_Delete) {
  694. if (is_readonly())
  695. return;
  696. delete_current_line();
  697. return;
  698. }
  699. if (event.key() == KeyCode::Key_Delete) {
  700. if (is_readonly())
  701. return;
  702. do_delete();
  703. return;
  704. }
  705. if (!is_readonly() && !event.ctrl() && !event.alt() && !event.text().is_empty())
  706. insert_at_cursor_or_replace_selection(event.text());
  707. }
  708. void GTextEditor::delete_current_line()
  709. {
  710. if (has_selection())
  711. return delete_selection();
  712. document().remove_line(m_cursor.line());
  713. if (lines().is_empty())
  714. document().append_line(make<GTextDocumentLine>(document()));
  715. m_cursor.set_column(0);
  716. update_content_size();
  717. update();
  718. }
  719. void GTextEditor::do_delete()
  720. {
  721. if (is_readonly())
  722. return;
  723. if (has_selection())
  724. return delete_selection();
  725. if (m_cursor.column() < current_line().length()) {
  726. // Delete within line
  727. current_line().remove(document(), m_cursor.column());
  728. did_change();
  729. update_cursor();
  730. return;
  731. }
  732. if (m_cursor.column() == current_line().length() && m_cursor.line() != line_count() - 1) {
  733. // Delete at end of line; merge with next line
  734. auto& next_line = lines()[m_cursor.line() + 1];
  735. int previous_length = current_line().length();
  736. current_line().append(document(), next_line.characters(), next_line.length());
  737. document().remove_line(m_cursor.line() + 1);
  738. update();
  739. did_change();
  740. set_cursor(m_cursor.line(), previous_length);
  741. return;
  742. }
  743. }
  744. int GTextEditor::content_x_for_position(const GTextPosition& position) const
  745. {
  746. auto& line = lines()[position.line()];
  747. int x_offset = -1;
  748. switch (m_text_alignment) {
  749. case TextAlignment::CenterLeft:
  750. for_each_visual_line(position.line(), [&](const Rect&, const StringView& view, int start_of_visual_line) {
  751. if (position.column() >= start_of_visual_line && ((position.column() - start_of_visual_line) <= view.length())) {
  752. x_offset = (position.column() - start_of_visual_line) * glyph_width();
  753. return IterationDecision::Break;
  754. }
  755. return IterationDecision::Continue;
  756. });
  757. return m_horizontal_content_padding + x_offset;
  758. case TextAlignment::CenterRight:
  759. // FIXME
  760. ASSERT(!is_line_wrapping_enabled());
  761. return content_width() - m_horizontal_content_padding - (line.length() * glyph_width()) + (position.column() * glyph_width());
  762. default:
  763. ASSERT_NOT_REACHED();
  764. }
  765. }
  766. Rect GTextEditor::content_rect_for_position(const GTextPosition& position) const
  767. {
  768. if (!position.is_valid())
  769. return {};
  770. ASSERT(!lines().is_empty());
  771. ASSERT(position.column() <= (current_line().length() + 1));
  772. int x = content_x_for_position(position);
  773. if (is_single_line()) {
  774. Rect rect { x, 0, 1, font().glyph_height() + 2 };
  775. rect.center_vertically_within({ {}, frame_inner_rect().size() });
  776. return rect;
  777. }
  778. Rect rect;
  779. for_each_visual_line(position.line(), [&](const Rect& visual_line_rect, const StringView& view, int start_of_visual_line) {
  780. if (position.column() >= start_of_visual_line && ((position.column() - start_of_visual_line) <= view.length())) {
  781. // NOTE: We have to subtract the horizontal padding here since it's part of the visual line rect
  782. // *and* included in what we get from content_x_for_position().
  783. rect = {
  784. visual_line_rect.x() + x - (m_horizontal_content_padding),
  785. visual_line_rect.y(),
  786. 1,
  787. line_height()
  788. };
  789. return IterationDecision::Break;
  790. }
  791. return IterationDecision::Continue;
  792. });
  793. return rect;
  794. }
  795. Rect GTextEditor::cursor_content_rect() const
  796. {
  797. return content_rect_for_position(m_cursor);
  798. }
  799. Rect GTextEditor::line_widget_rect(int line_index) const
  800. {
  801. auto rect = line_content_rect(line_index);
  802. rect.set_x(frame_thickness());
  803. rect.set_width(frame_inner_rect().width());
  804. rect.move_by(0, -(vertical_scrollbar().value()));
  805. rect.move_by(0, frame_thickness());
  806. rect.intersect(frame_inner_rect());
  807. return rect;
  808. }
  809. void GTextEditor::scroll_position_into_view(const GTextPosition& position)
  810. {
  811. auto rect = content_rect_for_position(position);
  812. if (position.column() == 0)
  813. rect.set_x(content_x_for_position({ position.line(), 0 }) - 2);
  814. else if (position.column() == lines()[position.line()].length())
  815. rect.set_x(content_x_for_position({ position.line(), lines()[position.line()].length() }) + 2);
  816. scroll_into_view(rect, true, true);
  817. }
  818. void GTextEditor::scroll_cursor_into_view()
  819. {
  820. scroll_position_into_view(m_cursor);
  821. }
  822. Rect GTextEditor::line_content_rect(int line_index) const
  823. {
  824. auto& line = lines()[line_index];
  825. if (is_single_line()) {
  826. Rect line_rect = { content_x_for_position({ line_index, 0 }), 0, line.length() * glyph_width(), font().glyph_height() + 2 };
  827. line_rect.center_vertically_within({ {}, frame_inner_rect().size() });
  828. return line_rect;
  829. }
  830. if (is_line_wrapping_enabled())
  831. return m_line_visual_data[line_index].visual_rect;
  832. return {
  833. content_x_for_position({ line_index, 0 }),
  834. line_index * line_height(),
  835. line.length() * glyph_width(),
  836. line_height()
  837. };
  838. }
  839. void GTextEditor::update_cursor()
  840. {
  841. update(line_widget_rect(m_cursor.line()));
  842. }
  843. void GTextEditor::set_cursor(int line, int column)
  844. {
  845. set_cursor({ line, column });
  846. }
  847. void GTextEditor::set_cursor(const GTextPosition& a_position)
  848. {
  849. ASSERT(!lines().is_empty());
  850. GTextPosition position = a_position;
  851. if (position.line() >= lines().size())
  852. position.set_line(lines().size() - 1);
  853. if (position.column() > lines()[position.line()].length())
  854. position.set_column(lines()[position.line()].length());
  855. if (m_cursor != position) {
  856. // NOTE: If the old cursor is no longer valid, repaint everything just in case.
  857. auto old_cursor_line_rect = m_cursor.line() < lines().size()
  858. ? line_widget_rect(m_cursor.line())
  859. : rect();
  860. m_cursor = position;
  861. m_cursor_state = true;
  862. scroll_cursor_into_view();
  863. update(old_cursor_line_rect);
  864. update_cursor();
  865. }
  866. cursor_did_change();
  867. if (on_cursor_change)
  868. on_cursor_change();
  869. }
  870. void GTextEditor::focusin_event(CEvent&)
  871. {
  872. update_cursor();
  873. start_timer(500);
  874. }
  875. void GTextEditor::focusout_event(CEvent&)
  876. {
  877. stop_timer();
  878. }
  879. void GTextEditor::timer_event(CTimerEvent&)
  880. {
  881. m_cursor_state = !m_cursor_state;
  882. if (is_focused())
  883. update_cursor();
  884. }
  885. bool GTextEditor::write_to_file(const StringView& path)
  886. {
  887. int fd = open_with_path_length(path.characters_without_null_termination(), path.length(), O_WRONLY | O_CREAT | O_TRUNC, 0666);
  888. if (fd < 0) {
  889. perror("open");
  890. return false;
  891. }
  892. // Compute the final file size and ftruncate() to make writing fast.
  893. // FIXME: Remove this once the kernel is smart enough to do this instead.
  894. off_t file_size = 0;
  895. for (int i = 0; i < lines().size(); ++i)
  896. file_size += lines()[i].length();
  897. file_size += lines().size() - 1;
  898. int rc = ftruncate(fd, file_size);
  899. if (rc < 0) {
  900. perror("ftruncate");
  901. return false;
  902. }
  903. for (int i = 0; i < lines().size(); ++i) {
  904. auto& line = lines()[i];
  905. if (line.length()) {
  906. ssize_t nwritten = write(fd, line.characters(), line.length());
  907. if (nwritten < 0) {
  908. perror("write");
  909. close(fd);
  910. return false;
  911. }
  912. }
  913. if (i != lines().size() - 1) {
  914. char ch = '\n';
  915. ssize_t nwritten = write(fd, &ch, 1);
  916. if (nwritten != 1) {
  917. perror("write");
  918. close(fd);
  919. return false;
  920. }
  921. }
  922. }
  923. close(fd);
  924. return true;
  925. }
  926. String GTextEditor::text() const
  927. {
  928. StringBuilder builder;
  929. for (int i = 0; i < line_count(); ++i) {
  930. auto& line = lines()[i];
  931. builder.append(line.characters(), line.length());
  932. if (i != line_count() - 1)
  933. builder.append('\n');
  934. }
  935. return builder.to_string();
  936. }
  937. void GTextEditor::clear()
  938. {
  939. document().remove_all_lines();
  940. document().append_line(make<GTextDocumentLine>(document()));
  941. m_selection.clear();
  942. did_update_selection();
  943. set_cursor(0, 0);
  944. update();
  945. }
  946. String GTextEditor::selected_text() const
  947. {
  948. if (!has_selection())
  949. return {};
  950. return document().text_in_range(m_selection);
  951. }
  952. void GTextEditor::delete_selection()
  953. {
  954. auto selection = normalized_selection();
  955. execute<RemoveTextCommand>(selected_text(), selection);
  956. m_selection.clear();
  957. did_update_selection();
  958. did_change();
  959. set_cursor(selection.start());
  960. update();
  961. }
  962. void GTextEditor::insert_at_cursor_or_replace_selection(const StringView& text)
  963. {
  964. ASSERT(!is_readonly());
  965. if (has_selection())
  966. delete_selection();
  967. execute<InsertTextCommand>(text, m_cursor);
  968. }
  969. void GTextEditor::cut()
  970. {
  971. if (is_readonly())
  972. return;
  973. auto selected_text = this->selected_text();
  974. printf("Cut: \"%s\"\n", selected_text.characters());
  975. GClipboard::the().set_data(selected_text);
  976. delete_selection();
  977. }
  978. void GTextEditor::copy()
  979. {
  980. auto selected_text = this->selected_text();
  981. printf("Copy: \"%s\"\n", selected_text.characters());
  982. GClipboard::the().set_data(selected_text);
  983. }
  984. void GTextEditor::paste()
  985. {
  986. if (is_readonly())
  987. return;
  988. auto paste_text = GClipboard::the().data();
  989. printf("Paste: \"%s\"\n", paste_text.characters());
  990. insert_at_cursor_or_replace_selection(paste_text);
  991. }
  992. void GTextEditor::enter_event(CEvent&)
  993. {
  994. ASSERT(window());
  995. window()->set_override_cursor(GStandardCursor::IBeam);
  996. }
  997. void GTextEditor::leave_event(CEvent&)
  998. {
  999. ASSERT(window());
  1000. window()->set_override_cursor(GStandardCursor::None);
  1001. }
  1002. void GTextEditor::did_change()
  1003. {
  1004. ASSERT(!is_readonly());
  1005. update_content_size();
  1006. recompute_all_visual_lines();
  1007. m_undo_action->set_enabled(can_undo());
  1008. m_redo_action->set_enabled(can_redo());
  1009. if (!m_has_pending_change_notification) {
  1010. m_has_pending_change_notification = true;
  1011. deferred_invoke([this](auto&) {
  1012. if (!m_has_pending_change_notification)
  1013. return;
  1014. if (on_change)
  1015. on_change();
  1016. m_has_pending_change_notification = false;
  1017. });
  1018. }
  1019. }
  1020. void GTextEditor::set_readonly(bool readonly)
  1021. {
  1022. if (m_readonly == readonly)
  1023. return;
  1024. m_readonly = readonly;
  1025. m_cut_action->set_enabled(!is_readonly() && has_selection());
  1026. m_delete_action->set_enabled(!is_readonly());
  1027. m_paste_action->set_enabled(!is_readonly());
  1028. }
  1029. void GTextEditor::did_update_selection()
  1030. {
  1031. m_cut_action->set_enabled(!is_readonly() && has_selection());
  1032. m_copy_action->set_enabled(has_selection());
  1033. if (on_selection_change)
  1034. on_selection_change();
  1035. if (is_line_wrapping_enabled()) {
  1036. // FIXME: Try to repaint less.
  1037. update();
  1038. }
  1039. }
  1040. void GTextEditor::context_menu_event(GContextMenuEvent& event)
  1041. {
  1042. if (!m_context_menu) {
  1043. m_context_menu = make<GMenu>();
  1044. m_context_menu->add_action(undo_action());
  1045. m_context_menu->add_action(redo_action());
  1046. m_context_menu->add_separator();
  1047. m_context_menu->add_action(cut_action());
  1048. m_context_menu->add_action(copy_action());
  1049. m_context_menu->add_action(paste_action());
  1050. m_context_menu->add_action(delete_action());
  1051. if (!m_custom_context_menu_actions.is_empty()) {
  1052. m_context_menu->add_separator();
  1053. for (auto& action : m_custom_context_menu_actions) {
  1054. m_context_menu->add_action(action);
  1055. }
  1056. }
  1057. }
  1058. m_context_menu->popup(event.screen_position());
  1059. }
  1060. void GTextEditor::set_text_alignment(TextAlignment alignment)
  1061. {
  1062. if (m_text_alignment == alignment)
  1063. return;
  1064. m_text_alignment = alignment;
  1065. update();
  1066. }
  1067. void GTextEditor::resize_event(GResizeEvent& event)
  1068. {
  1069. GScrollableWidget::resize_event(event);
  1070. update_content_size();
  1071. recompute_all_visual_lines();
  1072. }
  1073. void GTextEditor::set_selection(const GTextRange& selection)
  1074. {
  1075. if (m_selection == selection)
  1076. return;
  1077. m_selection = selection;
  1078. set_cursor(m_selection.end());
  1079. scroll_position_into_view(normalized_selection().start());
  1080. update();
  1081. }
  1082. void GTextEditor::recompute_all_visual_lines()
  1083. {
  1084. int y_offset = 0;
  1085. for (int line_index = 0; line_index < line_count(); ++line_index) {
  1086. recompute_visual_lines(line_index);
  1087. m_line_visual_data[line_index].visual_rect.set_y(y_offset);
  1088. y_offset += m_line_visual_data[line_index].visual_rect.height();
  1089. }
  1090. update_content_size();
  1091. }
  1092. void GTextEditor::ensure_cursor_is_valid()
  1093. {
  1094. auto new_cursor = m_cursor;
  1095. if (new_cursor.line() >= lines().size())
  1096. new_cursor.set_line(lines().size() - 1);
  1097. if (new_cursor.column() > lines()[new_cursor.line()].length())
  1098. new_cursor.set_column(lines()[new_cursor.line()].length());
  1099. if (m_cursor != new_cursor)
  1100. set_cursor(new_cursor);
  1101. }
  1102. int GTextEditor::visual_line_containing(int line_index, int column) const
  1103. {
  1104. int visual_line_index = 0;
  1105. for_each_visual_line(line_index, [&](const Rect&, const StringView& view, int start_of_visual_line) {
  1106. if (column >= start_of_visual_line && ((column - start_of_visual_line) < view.length()))
  1107. return IterationDecision::Break;
  1108. ++visual_line_index;
  1109. return IterationDecision::Continue;
  1110. });
  1111. return visual_line_index;
  1112. }
  1113. void GTextEditor::recompute_visual_lines(int line_index)
  1114. {
  1115. auto& line = document().line(line_index);
  1116. auto& visual_data = m_line_visual_data[line_index];
  1117. visual_data.visual_line_breaks.clear_with_capacity();
  1118. int available_width = visible_text_rect_in_inner_coordinates().width();
  1119. if (is_line_wrapping_enabled()) {
  1120. int line_width_so_far = 0;
  1121. for (int i = 0; i < line.length(); ++i) {
  1122. auto ch = line.characters()[i];
  1123. auto glyph_width = font().glyph_width(ch);
  1124. if ((line_width_so_far + glyph_width) > available_width) {
  1125. visual_data.visual_line_breaks.append(i);
  1126. line_width_so_far = glyph_width;
  1127. continue;
  1128. }
  1129. line_width_so_far += glyph_width;
  1130. }
  1131. }
  1132. visual_data.visual_line_breaks.append(line.length());
  1133. if (is_line_wrapping_enabled())
  1134. visual_data.visual_rect = { m_horizontal_content_padding, 0, available_width, visual_data.visual_line_breaks.size() * line_height() };
  1135. else
  1136. visual_data.visual_rect = { m_horizontal_content_padding, 0, font().width(line.view()), line_height() };
  1137. }
  1138. template<typename Callback>
  1139. void GTextEditor::for_each_visual_line(int line_index, Callback callback) const
  1140. {
  1141. auto editor_visible_text_rect = visible_text_rect_in_inner_coordinates();
  1142. int start_of_line = 0;
  1143. int visual_line_index = 0;
  1144. auto& line = document().line(line_index);
  1145. auto& visual_data = m_line_visual_data[line_index];
  1146. for (auto visual_line_break : visual_data.visual_line_breaks) {
  1147. auto visual_line_view = StringView(line.characters() + start_of_line, visual_line_break - start_of_line);
  1148. Rect visual_line_rect {
  1149. visual_data.visual_rect.x(),
  1150. visual_data.visual_rect.y() + (visual_line_index * line_height()),
  1151. font().width(visual_line_view),
  1152. line_height()
  1153. };
  1154. if (is_right_text_alignment(text_alignment()))
  1155. visual_line_rect.set_right_without_resize(editor_visible_text_rect.right());
  1156. if (!is_multi_line())
  1157. visual_line_rect.center_vertically_within(editor_visible_text_rect);
  1158. if (callback(visual_line_rect, visual_line_view, start_of_line) == IterationDecision::Break)
  1159. break;
  1160. start_of_line = visual_line_break;
  1161. ++visual_line_index;
  1162. }
  1163. }
  1164. void GTextEditor::set_line_wrapping_enabled(bool enabled)
  1165. {
  1166. if (m_line_wrapping_enabled == enabled)
  1167. return;
  1168. m_line_wrapping_enabled = enabled;
  1169. horizontal_scrollbar().set_visible(!m_line_wrapping_enabled);
  1170. update_content_size();
  1171. recompute_all_visual_lines();
  1172. update();
  1173. }
  1174. void GTextEditor::add_custom_context_menu_action(GAction& action)
  1175. {
  1176. m_custom_context_menu_actions.append(action);
  1177. }
  1178. void GTextEditor::did_change_font()
  1179. {
  1180. vertical_scrollbar().set_step(line_height());
  1181. GWidget::did_change_font();
  1182. }
  1183. void GTextEditor::document_did_append_line()
  1184. {
  1185. m_line_visual_data.append(make<LineVisualData>());
  1186. recompute_all_visual_lines();
  1187. update();
  1188. }
  1189. void GTextEditor::document_did_remove_line(int line_index)
  1190. {
  1191. m_line_visual_data.remove(line_index);
  1192. recompute_all_visual_lines();
  1193. update();
  1194. }
  1195. void GTextEditor::document_did_remove_all_lines()
  1196. {
  1197. m_line_visual_data.clear();
  1198. recompute_all_visual_lines();
  1199. update();
  1200. }
  1201. void GTextEditor::document_did_insert_line(int line_index)
  1202. {
  1203. m_line_visual_data.insert(line_index, make<LineVisualData>());
  1204. recompute_all_visual_lines();
  1205. update();
  1206. }
  1207. void GTextEditor::document_did_change()
  1208. {
  1209. ensure_cursor_is_valid();
  1210. recompute_all_visual_lines();
  1211. update();
  1212. undo_action().set_enabled(can_undo());
  1213. redo_action().set_enabled(can_redo());
  1214. }
  1215. void GTextEditor::document_did_set_text()
  1216. {
  1217. m_line_visual_data.clear();
  1218. for (int i = 0; i < m_document->line_count(); ++i)
  1219. m_line_visual_data.append(make<LineVisualData>());
  1220. document_did_change();
  1221. }
  1222. void GTextEditor::document_did_set_cursor(const GTextPosition& position)
  1223. {
  1224. set_cursor(position);
  1225. }
  1226. void GTextEditor::set_document(GTextDocument& document)
  1227. {
  1228. if (m_document.ptr() == &document)
  1229. return;
  1230. if (m_document)
  1231. m_document->unregister_client(*this);
  1232. m_document = document;
  1233. m_line_visual_data.clear();
  1234. for (int i = 0; i < m_document->line_count(); ++i) {
  1235. m_line_visual_data.append(make<LineVisualData>());
  1236. }
  1237. m_cursor = { 0, 0 };
  1238. recompute_all_visual_lines();
  1239. update();
  1240. m_document->register_client(*this);
  1241. }
  1242. void GTextEditor::flush_pending_change_notification_if_needed()
  1243. {
  1244. if (!m_has_pending_change_notification)
  1245. return;
  1246. if (on_change)
  1247. on_change();
  1248. m_has_pending_change_notification = false;
  1249. }