GTextEditor.cpp 47 KB

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