GTextEditor.cpp 46 KB

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