GTextEditor.cpp 46 KB

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