GTextEditor.cpp 47 KB

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