GTextEditor.cpp 46 KB

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