GTextEditor.cpp 46 KB

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