GTextEditor.cpp 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426
  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. size_t line_index = 0;
  88. if (is_line_wrapping_enabled()) {
  89. for (size_t i = 0; i < line_count(); ++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 = line_count() - 1;
  97. }
  98. } else {
  99. line_index = (size_t)(position.y() / line_height());
  100. }
  101. line_index = max((size_t)0, min(line_index, line_count() - 1));
  102. size_t 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&, size_t 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((size_t)0, min(column_index, line(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 = this->line(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(), line(m_cursor.line()).length());
  177. } else {
  178. // select *whole* line
  179. start = GTextPosition(0, 0);
  180. end = GTextPosition(line_count() - 1, line(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(size_t 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. size_t first_visible_line = text_position_at(event.rect().top_left()).line();
  273. size_t 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 (size_t 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 (size_t line_index = first_visible_line; line_index <= last_visible_line; ++line_index) {
  296. auto& line = this->line(line_index);
  297. bool physical_line_has_selection = has_selection && line_index >= selection.start().line() && line_index <= selection.end().line();
  298. size_t first_visual_line_with_selection = 0;
  299. size_t last_visual_line_with_selection = 0;
  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. size_t selection_start_column_within_line = selection.start().line() == line_index ? selection.start().column() : 0;
  311. size_t selection_end_column_within_line = selection.end().line() == line_index ? selection.end().column() : line.length();
  312. size_t visual_line_index = 0;
  313. for_each_visual_line(line_index, [&](const Rect& visual_line_rect, const StringView& visual_line_text, size_t 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 (size_t 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. size_t start_of_selection_within_visual_line = max((size_t)0, selection_start_column_within_line - start_of_visual_line);
  367. size_t 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, line(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(size_t& first_line, size_t& 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. size_t first_line;
  422. size_t 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((int)last_line, lines.take((int)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. size_t first_line;
  439. size_t last_line;
  440. get_selection_line_boundaries(first_line, last_line);
  441. auto& lines = document().lines();
  442. if (last_line >= (size_t)(lines.size() - 1))
  443. return;
  444. lines.insert((int)first_line, lines.take((int)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. size_t first_line;
  460. size_t last_line;
  461. get_selection_line_boundaries(first_line, last_line);
  462. auto& lines = document().lines();
  463. auto start = lines.begin() + (int)first_line;
  464. auto end = lines.begin() + (int)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. size_t new_line = m_cursor.line() - 1;
  492. size_t new_column = min(m_cursor.column(), line(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() < (line_count() - 1)) {
  504. if (event.ctrl() && event.shift()) {
  505. move_selected_lines_down();
  506. return;
  507. }
  508. size_t new_line = m_cursor.line() + 1;
  509. size_t new_column = min(m_cursor.column(), line(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. size_t new_line = max((size_t)0, m_cursor.line() - (size_t)visible_content_rect().height() / (size_t)line_height());
  522. size_t new_column = min(m_cursor.column(), line(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() < (line_count() - 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. size_t 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. size_t 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. GTextPosition start;
  713. GTextPosition end;
  714. if (m_cursor.line() == 0 && line_count() == 1) {
  715. start = { 0, 0 };
  716. end = { 0, line(0).length() };
  717. } else if (m_cursor.line() == line_count() - 1) {
  718. start = { m_cursor.line() - 1, line(m_cursor.line()).length() };
  719. end = { m_cursor.line(), line(m_cursor.line()).length() };
  720. } else {
  721. start = { m_cursor.line(), 0 };
  722. end = { m_cursor.line() + 1, 0 };
  723. }
  724. GTextRange erased_range(start, end);
  725. execute<RemoveTextCommand>(document().text_in_range(erased_range), erased_range);
  726. }
  727. void GTextEditor::do_delete()
  728. {
  729. if (is_readonly())
  730. return;
  731. if (has_selection())
  732. return delete_selection();
  733. if (m_cursor.column() < current_line().length()) {
  734. // Delete within line
  735. GTextRange erased_range(m_cursor, { m_cursor.line(), m_cursor.column() + 1 });
  736. execute<RemoveTextCommand>(document().text_in_range(erased_range), erased_range);
  737. return;
  738. }
  739. if (m_cursor.column() == current_line().length() && m_cursor.line() != line_count() - 1) {
  740. // Delete at end of line; merge with next line
  741. GTextRange erased_range(m_cursor, { m_cursor.line() + 1, 0 });
  742. execute<RemoveTextCommand>(document().text_in_range(erased_range), erased_range);
  743. return;
  744. }
  745. }
  746. int GTextEditor::content_x_for_position(const GTextPosition& position) const
  747. {
  748. auto& line = this->line(position.line());
  749. int x_offset = -1;
  750. switch (m_text_alignment) {
  751. case TextAlignment::CenterLeft:
  752. for_each_visual_line(position.line(), [&](const Rect&, const StringView& view, size_t start_of_visual_line) {
  753. if (position.column() >= start_of_visual_line && ((position.column() - start_of_visual_line) <= view.length())) {
  754. x_offset = (position.column() - start_of_visual_line) * glyph_width();
  755. return IterationDecision::Break;
  756. }
  757. return IterationDecision::Continue;
  758. });
  759. return m_horizontal_content_padding + x_offset;
  760. case TextAlignment::CenterRight:
  761. // FIXME
  762. ASSERT(!is_line_wrapping_enabled());
  763. return content_width() - m_horizontal_content_padding - (line.length() * glyph_width()) + (position.column() * glyph_width());
  764. default:
  765. ASSERT_NOT_REACHED();
  766. }
  767. }
  768. Rect GTextEditor::content_rect_for_position(const GTextPosition& position) const
  769. {
  770. if (!position.is_valid())
  771. return {};
  772. ASSERT(!lines().is_empty());
  773. ASSERT(position.column() <= (current_line().length() + 1));
  774. int x = content_x_for_position(position);
  775. if (is_single_line()) {
  776. Rect rect { x, 0, 1, font().glyph_height() + 2 };
  777. rect.center_vertically_within({ {}, frame_inner_rect().size() });
  778. return rect;
  779. }
  780. Rect rect;
  781. for_each_visual_line(position.line(), [&](const Rect& visual_line_rect, const StringView& view, size_t start_of_visual_line) {
  782. if (position.column() >= start_of_visual_line && ((position.column() - start_of_visual_line) <= view.length())) {
  783. // NOTE: We have to subtract the horizontal padding here since it's part of the visual line rect
  784. // *and* included in what we get from content_x_for_position().
  785. rect = {
  786. visual_line_rect.x() + x - (m_horizontal_content_padding),
  787. visual_line_rect.y(),
  788. 1,
  789. line_height()
  790. };
  791. return IterationDecision::Break;
  792. }
  793. return IterationDecision::Continue;
  794. });
  795. return rect;
  796. }
  797. Rect GTextEditor::cursor_content_rect() const
  798. {
  799. return content_rect_for_position(m_cursor);
  800. }
  801. Rect GTextEditor::line_widget_rect(size_t line_index) const
  802. {
  803. auto rect = line_content_rect(line_index);
  804. rect.set_x(frame_thickness());
  805. rect.set_width(frame_inner_rect().width());
  806. rect.move_by(0, -(vertical_scrollbar().value()));
  807. rect.move_by(0, frame_thickness());
  808. rect.intersect(frame_inner_rect());
  809. return rect;
  810. }
  811. void GTextEditor::scroll_position_into_view(const GTextPosition& position)
  812. {
  813. auto rect = content_rect_for_position(position);
  814. if (position.column() == 0)
  815. rect.set_x(content_x_for_position({ position.line(), 0 }) - 2);
  816. else if (position.column() == line(position.line()).length())
  817. rect.set_x(content_x_for_position({ position.line(), line(position.line()).length() }) + 2);
  818. scroll_into_view(rect, true, true);
  819. }
  820. void GTextEditor::scroll_cursor_into_view()
  821. {
  822. scroll_position_into_view(m_cursor);
  823. }
  824. Rect GTextEditor::line_content_rect(size_t line_index) const
  825. {
  826. auto& line = this->line(line_index);
  827. if (is_single_line()) {
  828. Rect line_rect = { content_x_for_position({ line_index, 0 }), 0, (int)line.length() * glyph_width(), font().glyph_height() + 2 };
  829. line_rect.center_vertically_within({ {}, frame_inner_rect().size() });
  830. return line_rect;
  831. }
  832. if (is_line_wrapping_enabled())
  833. return m_line_visual_data[line_index].visual_rect;
  834. return {
  835. content_x_for_position({ line_index, 0 }),
  836. (int)line_index * line_height(),
  837. (int)line.length() * glyph_width(),
  838. line_height()
  839. };
  840. }
  841. void GTextEditor::update_cursor()
  842. {
  843. update(line_widget_rect(m_cursor.line()));
  844. }
  845. void GTextEditor::set_cursor(size_t line, size_t column)
  846. {
  847. set_cursor({ line, column });
  848. }
  849. void GTextEditor::set_cursor(const GTextPosition& a_position)
  850. {
  851. ASSERT(!lines().is_empty());
  852. GTextPosition position = a_position;
  853. if (position.line() >= line_count())
  854. position.set_line(line_count() - 1);
  855. if (position.column() > lines()[position.line()].length())
  856. position.set_column(lines()[position.line()].length());
  857. if (m_cursor != position) {
  858. // NOTE: If the old cursor is no longer valid, repaint everything just in case.
  859. auto old_cursor_line_rect = m_cursor.line() < line_count()
  860. ? line_widget_rect(m_cursor.line())
  861. : rect();
  862. m_cursor = position;
  863. m_cursor_state = true;
  864. scroll_cursor_into_view();
  865. update(old_cursor_line_rect);
  866. update_cursor();
  867. }
  868. cursor_did_change();
  869. if (on_cursor_change)
  870. on_cursor_change();
  871. }
  872. void GTextEditor::focusin_event(CEvent&)
  873. {
  874. update_cursor();
  875. start_timer(500);
  876. }
  877. void GTextEditor::focusout_event(CEvent&)
  878. {
  879. stop_timer();
  880. }
  881. void GTextEditor::timer_event(CTimerEvent&)
  882. {
  883. m_cursor_state = !m_cursor_state;
  884. if (is_focused())
  885. update_cursor();
  886. }
  887. bool GTextEditor::write_to_file(const StringView& path)
  888. {
  889. int fd = open_with_path_length(path.characters_without_null_termination(), path.length(), O_WRONLY | O_CREAT | O_TRUNC, 0666);
  890. if (fd < 0) {
  891. perror("open");
  892. return false;
  893. }
  894. // Compute the final file size and ftruncate() to make writing fast.
  895. // FIXME: Remove this once the kernel is smart enough to do this instead.
  896. off_t file_size = 0;
  897. for (size_t i = 0; i < line_count(); ++i)
  898. file_size += line(i).length();
  899. file_size += line_count() - 1;
  900. int rc = ftruncate(fd, file_size);
  901. if (rc < 0) {
  902. perror("ftruncate");
  903. return false;
  904. }
  905. for (size_t i = 0; i < line_count(); ++i) {
  906. auto& line = this->line(i);
  907. if (line.length()) {
  908. ssize_t nwritten = write(fd, line.characters(), line.length());
  909. if (nwritten < 0) {
  910. perror("write");
  911. close(fd);
  912. return false;
  913. }
  914. }
  915. if (i != line_count() - 1) {
  916. char ch = '\n';
  917. ssize_t nwritten = write(fd, &ch, 1);
  918. if (nwritten != 1) {
  919. perror("write");
  920. close(fd);
  921. return false;
  922. }
  923. }
  924. }
  925. close(fd);
  926. return true;
  927. }
  928. String GTextEditor::text() const
  929. {
  930. StringBuilder builder;
  931. for (size_t i = 0; i < line_count(); ++i) {
  932. auto& line = this->line(i);
  933. builder.append(line.characters(), line.length());
  934. if (i != line_count() - 1)
  935. builder.append('\n');
  936. }
  937. return builder.to_string();
  938. }
  939. void GTextEditor::clear()
  940. {
  941. document().remove_all_lines();
  942. document().append_line(make<GTextDocumentLine>(document()));
  943. m_selection.clear();
  944. did_update_selection();
  945. set_cursor(0, 0);
  946. update();
  947. }
  948. String GTextEditor::selected_text() const
  949. {
  950. if (!has_selection())
  951. return {};
  952. return document().text_in_range(m_selection);
  953. }
  954. void GTextEditor::delete_selection()
  955. {
  956. auto selection = normalized_selection();
  957. execute<RemoveTextCommand>(selected_text(), selection);
  958. m_selection.clear();
  959. did_update_selection();
  960. did_change();
  961. set_cursor(selection.start());
  962. update();
  963. }
  964. void GTextEditor::insert_at_cursor_or_replace_selection(const StringView& text)
  965. {
  966. ASSERT(!is_readonly());
  967. if (has_selection())
  968. delete_selection();
  969. execute<InsertTextCommand>(text, m_cursor);
  970. }
  971. void GTextEditor::cut()
  972. {
  973. if (is_readonly())
  974. return;
  975. auto selected_text = this->selected_text();
  976. printf("Cut: \"%s\"\n", selected_text.characters());
  977. GClipboard::the().set_data(selected_text);
  978. delete_selection();
  979. }
  980. void GTextEditor::copy()
  981. {
  982. auto selected_text = this->selected_text();
  983. printf("Copy: \"%s\"\n", selected_text.characters());
  984. GClipboard::the().set_data(selected_text);
  985. }
  986. void GTextEditor::paste()
  987. {
  988. if (is_readonly())
  989. return;
  990. auto paste_text = GClipboard::the().data();
  991. printf("Paste: \"%s\"\n", paste_text.characters());
  992. insert_at_cursor_or_replace_selection(paste_text);
  993. }
  994. void GTextEditor::enter_event(CEvent&)
  995. {
  996. ASSERT(window());
  997. window()->set_override_cursor(GStandardCursor::IBeam);
  998. }
  999. void GTextEditor::leave_event(CEvent&)
  1000. {
  1001. ASSERT(window());
  1002. window()->set_override_cursor(GStandardCursor::None);
  1003. }
  1004. void GTextEditor::did_change()
  1005. {
  1006. update_content_size();
  1007. recompute_all_visual_lines();
  1008. m_undo_action->set_enabled(can_undo());
  1009. m_redo_action->set_enabled(can_redo());
  1010. if (!m_has_pending_change_notification) {
  1011. m_has_pending_change_notification = true;
  1012. deferred_invoke([this](auto&) {
  1013. if (!m_has_pending_change_notification)
  1014. return;
  1015. if (on_change)
  1016. on_change();
  1017. m_has_pending_change_notification = false;
  1018. });
  1019. }
  1020. }
  1021. void GTextEditor::set_readonly(bool readonly)
  1022. {
  1023. if (m_readonly == readonly)
  1024. return;
  1025. m_readonly = readonly;
  1026. m_cut_action->set_enabled(!is_readonly() && has_selection());
  1027. m_delete_action->set_enabled(!is_readonly());
  1028. m_paste_action->set_enabled(!is_readonly());
  1029. }
  1030. void GTextEditor::did_update_selection()
  1031. {
  1032. m_cut_action->set_enabled(!is_readonly() && has_selection());
  1033. m_copy_action->set_enabled(has_selection());
  1034. if (on_selection_change)
  1035. on_selection_change();
  1036. if (is_line_wrapping_enabled()) {
  1037. // FIXME: Try to repaint less.
  1038. update();
  1039. }
  1040. }
  1041. void GTextEditor::context_menu_event(GContextMenuEvent& event)
  1042. {
  1043. if (!m_context_menu) {
  1044. m_context_menu = GMenu::construct();
  1045. m_context_menu->add_action(undo_action());
  1046. m_context_menu->add_action(redo_action());
  1047. m_context_menu->add_separator();
  1048. m_context_menu->add_action(cut_action());
  1049. m_context_menu->add_action(copy_action());
  1050. m_context_menu->add_action(paste_action());
  1051. m_context_menu->add_action(delete_action());
  1052. if (!m_custom_context_menu_actions.is_empty()) {
  1053. m_context_menu->add_separator();
  1054. for (auto& action : m_custom_context_menu_actions) {
  1055. m_context_menu->add_action(action);
  1056. }
  1057. }
  1058. }
  1059. m_context_menu->popup(event.screen_position());
  1060. }
  1061. void GTextEditor::set_text_alignment(TextAlignment alignment)
  1062. {
  1063. if (m_text_alignment == alignment)
  1064. return;
  1065. m_text_alignment = alignment;
  1066. update();
  1067. }
  1068. void GTextEditor::resize_event(GResizeEvent& event)
  1069. {
  1070. GScrollableWidget::resize_event(event);
  1071. update_content_size();
  1072. recompute_all_visual_lines();
  1073. }
  1074. void GTextEditor::set_selection(const GTextRange& selection)
  1075. {
  1076. if (m_selection == selection)
  1077. return;
  1078. m_selection = selection;
  1079. set_cursor(m_selection.end());
  1080. scroll_position_into_view(normalized_selection().start());
  1081. update();
  1082. }
  1083. void GTextEditor::clear_selection()
  1084. {
  1085. if (!has_selection())
  1086. return;
  1087. m_selection.clear();
  1088. update();
  1089. }
  1090. void GTextEditor::recompute_all_visual_lines()
  1091. {
  1092. int y_offset = 0;
  1093. for (size_t line_index = 0; line_index < line_count(); ++line_index) {
  1094. recompute_visual_lines(line_index);
  1095. m_line_visual_data[line_index].visual_rect.set_y(y_offset);
  1096. y_offset += m_line_visual_data[line_index].visual_rect.height();
  1097. }
  1098. update_content_size();
  1099. }
  1100. void GTextEditor::ensure_cursor_is_valid()
  1101. {
  1102. auto new_cursor = m_cursor;
  1103. if (new_cursor.line() >= line_count())
  1104. new_cursor.set_line(line_count() - 1);
  1105. if (new_cursor.column() > line(new_cursor.line()).length())
  1106. new_cursor.set_column(line(new_cursor.line()).length());
  1107. if (m_cursor != new_cursor)
  1108. set_cursor(new_cursor);
  1109. }
  1110. size_t GTextEditor::visual_line_containing(size_t line_index, size_t column) const
  1111. {
  1112. size_t visual_line_index = 0;
  1113. for_each_visual_line(line_index, [&](const Rect&, const StringView& view, size_t start_of_visual_line) {
  1114. if (column >= start_of_visual_line && ((column - start_of_visual_line) < view.length()))
  1115. return IterationDecision::Break;
  1116. ++visual_line_index;
  1117. return IterationDecision::Continue;
  1118. });
  1119. return visual_line_index;
  1120. }
  1121. void GTextEditor::recompute_visual_lines(size_t line_index)
  1122. {
  1123. auto& line = document().line(line_index);
  1124. auto& visual_data = m_line_visual_data[line_index];
  1125. visual_data.visual_line_breaks.clear_with_capacity();
  1126. int available_width = visible_text_rect_in_inner_coordinates().width();
  1127. if (is_line_wrapping_enabled()) {
  1128. int line_width_so_far = 0;
  1129. for (size_t i = 0; i < line.length(); ++i) {
  1130. auto ch = line.characters()[i];
  1131. auto glyph_width = font().glyph_width(ch);
  1132. if ((line_width_so_far + glyph_width) > available_width) {
  1133. visual_data.visual_line_breaks.append(i);
  1134. line_width_so_far = glyph_width;
  1135. continue;
  1136. }
  1137. line_width_so_far += glyph_width;
  1138. }
  1139. }
  1140. visual_data.visual_line_breaks.append(line.length());
  1141. if (is_line_wrapping_enabled())
  1142. visual_data.visual_rect = { m_horizontal_content_padding, 0, available_width, visual_data.visual_line_breaks.size() * line_height() };
  1143. else
  1144. visual_data.visual_rect = { m_horizontal_content_padding, 0, font().width(line.view()), line_height() };
  1145. }
  1146. template<typename Callback>
  1147. void GTextEditor::for_each_visual_line(size_t line_index, Callback callback) const
  1148. {
  1149. auto editor_visible_text_rect = visible_text_rect_in_inner_coordinates();
  1150. size_t start_of_line = 0;
  1151. size_t visual_line_index = 0;
  1152. auto& line = document().line(line_index);
  1153. auto& visual_data = m_line_visual_data[line_index];
  1154. for (auto visual_line_break : visual_data.visual_line_breaks) {
  1155. auto visual_line_view = StringView(line.characters() + start_of_line, visual_line_break - start_of_line);
  1156. Rect visual_line_rect {
  1157. visual_data.visual_rect.x(),
  1158. visual_data.visual_rect.y() + ((int)visual_line_index * line_height()),
  1159. font().width(visual_line_view),
  1160. line_height()
  1161. };
  1162. if (is_right_text_alignment(text_alignment()))
  1163. visual_line_rect.set_right_without_resize(editor_visible_text_rect.right());
  1164. if (!is_multi_line())
  1165. visual_line_rect.center_vertically_within(editor_visible_text_rect);
  1166. if (callback(visual_line_rect, visual_line_view, start_of_line) == IterationDecision::Break)
  1167. break;
  1168. start_of_line = visual_line_break;
  1169. ++visual_line_index;
  1170. }
  1171. }
  1172. void GTextEditor::set_line_wrapping_enabled(bool enabled)
  1173. {
  1174. if (m_line_wrapping_enabled == enabled)
  1175. return;
  1176. m_line_wrapping_enabled = enabled;
  1177. horizontal_scrollbar().set_visible(!m_line_wrapping_enabled);
  1178. update_content_size();
  1179. recompute_all_visual_lines();
  1180. update();
  1181. }
  1182. void GTextEditor::add_custom_context_menu_action(GAction& action)
  1183. {
  1184. m_custom_context_menu_actions.append(action);
  1185. }
  1186. void GTextEditor::did_change_font()
  1187. {
  1188. vertical_scrollbar().set_step(line_height());
  1189. GWidget::did_change_font();
  1190. }
  1191. void GTextEditor::document_did_append_line()
  1192. {
  1193. m_line_visual_data.append(make<LineVisualData>());
  1194. recompute_all_visual_lines();
  1195. update();
  1196. }
  1197. void GTextEditor::document_did_remove_line(size_t line_index)
  1198. {
  1199. m_line_visual_data.remove(line_index);
  1200. recompute_all_visual_lines();
  1201. update();
  1202. }
  1203. void GTextEditor::document_did_remove_all_lines()
  1204. {
  1205. m_line_visual_data.clear();
  1206. recompute_all_visual_lines();
  1207. update();
  1208. }
  1209. void GTextEditor::document_did_insert_line(size_t line_index)
  1210. {
  1211. m_line_visual_data.insert(line_index, make<LineVisualData>());
  1212. recompute_all_visual_lines();
  1213. update();
  1214. }
  1215. void GTextEditor::document_did_change()
  1216. {
  1217. did_change();
  1218. update();
  1219. }
  1220. void GTextEditor::document_did_set_text()
  1221. {
  1222. m_line_visual_data.clear();
  1223. for (size_t i = 0; i < m_document->line_count(); ++i)
  1224. m_line_visual_data.append(make<LineVisualData>());
  1225. document_did_change();
  1226. }
  1227. void GTextEditor::document_did_set_cursor(const GTextPosition& position)
  1228. {
  1229. set_cursor(position);
  1230. }
  1231. void GTextEditor::set_document(GTextDocument& document)
  1232. {
  1233. if (m_document.ptr() == &document)
  1234. return;
  1235. if (m_document)
  1236. m_document->unregister_client(*this);
  1237. m_document = document;
  1238. m_line_visual_data.clear();
  1239. for (size_t i = 0; i < m_document->line_count(); ++i) {
  1240. m_line_visual_data.append(make<LineVisualData>());
  1241. }
  1242. m_cursor = { 0, 0 };
  1243. if (has_selection())
  1244. m_selection.clear();
  1245. recompute_all_visual_lines();
  1246. update();
  1247. m_document->register_client(*this);
  1248. }
  1249. void GTextEditor::flush_pending_change_notification_if_needed()
  1250. {
  1251. if (!m_has_pending_change_notification)
  1252. return;
  1253. if (on_change)
  1254. on_change();
  1255. m_has_pending_change_notification = false;
  1256. }