TextEditor.cpp 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/CharacterTypes.h>
  7. #include <AK/Debug.h>
  8. #include <AK/ScopeGuard.h>
  9. #include <AK/StringBuilder.h>
  10. #include <AK/TemporaryChange.h>
  11. #include <LibCore/Timer.h>
  12. #include <LibGUI/Action.h>
  13. #include <LibGUI/AutocompleteProvider.h>
  14. #include <LibGUI/Clipboard.h>
  15. #include <LibGUI/EditingEngine.h>
  16. #include <LibGUI/InputBox.h>
  17. #include <LibGUI/Menu.h>
  18. #include <LibGUI/Painter.h>
  19. #include <LibGUI/RegularEditingEngine.h>
  20. #include <LibGUI/Scrollbar.h>
  21. #include <LibGUI/TextEditor.h>
  22. #include <LibGUI/Window.h>
  23. #include <LibGfx/Bitmap.h>
  24. #include <LibGfx/Font.h>
  25. #include <LibGfx/FontDatabase.h>
  26. #include <LibGfx/Palette.h>
  27. #include <LibSyntax/Highlighter.h>
  28. #include <fcntl.h>
  29. #include <math.h>
  30. #include <stdio.h>
  31. #include <unistd.h>
  32. REGISTER_WIDGET(GUI, TextEditor)
  33. namespace GUI {
  34. TextEditor::TextEditor(Type type)
  35. : m_type(type)
  36. {
  37. REGISTER_STRING_PROPERTY("text", text, set_text);
  38. REGISTER_STRING_PROPERTY("placeholder", placeholder, set_placeholder);
  39. REGISTER_ENUM_PROPERTY("mode", mode, set_mode, Mode,
  40. { Editable, "Editable" },
  41. { ReadOnly, "ReadOnly" },
  42. { DisplayOnly, "DisplayOnly" });
  43. set_focus_policy(GUI::FocusPolicy::StrongFocus);
  44. set_accepts_emoji_input(true);
  45. set_override_cursor(Gfx::StandardCursor::IBeam);
  46. set_background_role(ColorRole::Base);
  47. set_foreground_role(ColorRole::BaseText);
  48. set_document(TextDocument::create());
  49. if (is_single_line())
  50. set_visualize_trailing_whitespace(false);
  51. set_scrollbars_enabled(is_multi_line());
  52. if (is_multi_line())
  53. set_font(Gfx::FontDatabase::default_fixed_width_font());
  54. vertical_scrollbar().set_step(line_height());
  55. m_cursor = { 0, 0 };
  56. m_automatic_selection_scroll_timer = add<Core::Timer>(100, [this] {
  57. automatic_selection_scroll_timer_fired();
  58. });
  59. m_automatic_selection_scroll_timer->stop();
  60. create_actions();
  61. set_editing_engine(make<RegularEditingEngine>());
  62. }
  63. TextEditor::~TextEditor()
  64. {
  65. if (m_document)
  66. m_document->unregister_client(*this);
  67. }
  68. void TextEditor::create_actions()
  69. {
  70. m_undo_action = CommonActions::make_undo_action([&](auto&) { undo(); }, this);
  71. m_redo_action = CommonActions::make_redo_action([&](auto&) { redo(); }, this);
  72. m_undo_action->set_enabled(false);
  73. m_redo_action->set_enabled(false);
  74. m_cut_action = CommonActions::make_cut_action([&](auto&) { cut(); }, this);
  75. m_copy_action = CommonActions::make_copy_action([&](auto&) { copy(); }, this);
  76. m_paste_action = CommonActions::make_paste_action([&](auto&) { paste(); }, this);
  77. m_delete_action = CommonActions::make_delete_action([&](auto&) { do_delete(); }, this);
  78. if (is_multi_line()) {
  79. m_go_to_line_action = Action::create(
  80. "Go to line...", { Mod_Ctrl, Key_L }, Gfx::Bitmap::load_from_file("/res/icons/16x16/go-forward.png"), [this](auto&) {
  81. String value;
  82. if (InputBox::show(window(), value, "Line:", "Go to line") == InputBox::ExecOK) {
  83. auto line_target = value.to_uint();
  84. if (line_target.has_value()) {
  85. set_cursor_and_focus_line(line_target.value() - 1, 0);
  86. }
  87. }
  88. },
  89. this);
  90. }
  91. m_select_all_action = CommonActions::make_select_all_action([this](auto&) { select_all(); }, this);
  92. }
  93. void TextEditor::set_text(const StringView& text)
  94. {
  95. m_selection.clear();
  96. document().set_text(text);
  97. update_content_size();
  98. recompute_all_visual_lines();
  99. if (is_single_line())
  100. set_cursor(0, line(0).length());
  101. else
  102. set_cursor(0, 0);
  103. did_update_selection();
  104. update();
  105. }
  106. void TextEditor::update_content_size()
  107. {
  108. int content_width = 0;
  109. int content_height = 0;
  110. for (auto& line : m_line_visual_data) {
  111. content_width = max(line.visual_rect.width(), content_width);
  112. content_height += line.visual_rect.height();
  113. }
  114. content_width += m_horizontal_content_padding * 2;
  115. if (is_right_text_alignment(m_text_alignment))
  116. content_width = max(frame_inner_rect().width(), content_width);
  117. set_content_size({ content_width, content_height });
  118. set_size_occupied_by_fixed_elements({ ruler_width() + gutter_width(), 0 });
  119. }
  120. TextPosition TextEditor::text_position_at_content_position(const Gfx::IntPoint& content_position) const
  121. {
  122. auto position = content_position;
  123. if (is_single_line() && icon())
  124. position.translate_by(-(icon_size() + icon_padding()), 0);
  125. size_t line_index = 0;
  126. if (position.y() >= 0) {
  127. if (is_wrapping_enabled()) {
  128. for (size_t i = 0; i < line_count(); ++i) {
  129. auto& rect = m_line_visual_data[i].visual_rect;
  130. if (position.y() >= rect.top() && position.y() <= rect.bottom()) {
  131. line_index = i;
  132. break;
  133. }
  134. if (position.y() > rect.bottom())
  135. line_index = line_count() - 1;
  136. }
  137. } else {
  138. line_index = (size_t)(position.y() / line_height());
  139. }
  140. line_index = max((size_t)0, min(line_index, line_count() - 1));
  141. }
  142. size_t column_index = 0;
  143. switch (m_text_alignment) {
  144. case Gfx::TextAlignment::CenterLeft:
  145. for_each_visual_line(line_index, [&](const Gfx::IntRect& rect, auto& view, size_t start_of_line, [[maybe_unused]] bool is_last_visual_line) {
  146. if (is_multi_line() && !rect.contains_vertically(position.y()) && !is_last_visual_line)
  147. return IterationDecision::Continue;
  148. column_index = start_of_line;
  149. if (position.x() <= 0) {
  150. // We're outside the text on the left side, put cursor at column 0 on this visual line.
  151. } else {
  152. int glyph_x = 0;
  153. size_t i = 0;
  154. for (; i < view.length(); ++i) {
  155. int advance = font().glyph_width(view.code_points()[i]) + font().glyph_spacing();
  156. if ((glyph_x + (advance / 2)) >= position.x())
  157. break;
  158. glyph_x += advance;
  159. }
  160. column_index += i;
  161. }
  162. return IterationDecision::Break;
  163. });
  164. break;
  165. case Gfx::TextAlignment::CenterRight:
  166. // FIXME: Support right-aligned line wrapping, I guess.
  167. VERIFY(!is_wrapping_enabled());
  168. column_index = (position.x() - content_x_for_position({ line_index, 0 }) + fixed_glyph_width() / 2) / fixed_glyph_width();
  169. break;
  170. default:
  171. VERIFY_NOT_REACHED();
  172. }
  173. column_index = max((size_t)0, min(column_index, line(line_index).length()));
  174. return { line_index, column_index };
  175. }
  176. TextPosition TextEditor::text_position_at(const Gfx::IntPoint& widget_position) const
  177. {
  178. auto content_position = widget_position;
  179. content_position.translate_by(horizontal_scrollbar().value(), vertical_scrollbar().value());
  180. content_position.translate_by(-(m_horizontal_content_padding + ruler_width() + gutter_width()), 0);
  181. content_position.translate_by(-frame_thickness(), -frame_thickness());
  182. return text_position_at_content_position(content_position);
  183. }
  184. void TextEditor::doubleclick_event(MouseEvent& event)
  185. {
  186. if (event.button() != MouseButton::Left)
  187. return;
  188. if (is_displayonly())
  189. return;
  190. if (!current_line().can_select())
  191. return;
  192. // NOTE: This ensures that spans are updated before we look at them.
  193. flush_pending_change_notification_if_needed();
  194. m_triple_click_timer.start();
  195. m_in_drag_select = false;
  196. auto start = text_position_at(event.position());
  197. auto end = start;
  198. if (!document().has_spans()) {
  199. start = document().first_word_break_before(start, false);
  200. end = document().first_word_break_after(end);
  201. } else {
  202. for (auto& span : document().spans()) {
  203. if (!span.range.contains(start))
  204. continue;
  205. start = span.range.start();
  206. end = span.range.end();
  207. end.set_column(end.column() + 1);
  208. break;
  209. }
  210. }
  211. m_selection.set(start, end);
  212. set_cursor(end);
  213. update();
  214. did_update_selection();
  215. }
  216. void TextEditor::mousedown_event(MouseEvent& event)
  217. {
  218. if (event.button() != MouseButton::Left) {
  219. return;
  220. }
  221. if (on_mousedown)
  222. on_mousedown();
  223. if (is_displayonly())
  224. return;
  225. if (m_triple_click_timer.is_valid() && m_triple_click_timer.elapsed() < 250) {
  226. m_triple_click_timer = Core::ElapsedTimer();
  227. TextPosition start;
  228. TextPosition end;
  229. if (is_multi_line()) {
  230. // select *current* line
  231. start = TextPosition(m_cursor.line(), 0);
  232. end = TextPosition(m_cursor.line(), line(m_cursor.line()).length());
  233. } else {
  234. // select *whole* line
  235. start = TextPosition(0, 0);
  236. end = TextPosition(line_count() - 1, line(line_count() - 1).length());
  237. }
  238. m_selection.set(start, end);
  239. set_cursor(end);
  240. update();
  241. did_update_selection();
  242. return;
  243. }
  244. if (event.modifiers() & Mod_Shift) {
  245. if (!has_selection())
  246. m_selection.set(m_cursor, {});
  247. } else {
  248. m_selection.clear();
  249. }
  250. m_in_drag_select = true;
  251. m_automatic_selection_scroll_timer->start();
  252. set_cursor(text_position_at(event.position()));
  253. if (!(event.modifiers() & Mod_Shift)) {
  254. if (!has_selection())
  255. m_selection.set(m_cursor, {});
  256. }
  257. if (m_selection.start().is_valid() && m_selection.start() != m_cursor)
  258. m_selection.set_end(m_cursor);
  259. // FIXME: Only update the relevant rects.
  260. update();
  261. did_update_selection();
  262. }
  263. void TextEditor::mouseup_event(MouseEvent& event)
  264. {
  265. if (event.button() == MouseButton::Left) {
  266. if (m_in_drag_select) {
  267. m_in_drag_select = false;
  268. }
  269. return;
  270. }
  271. }
  272. void TextEditor::mousemove_event(MouseEvent& event)
  273. {
  274. m_last_mousemove_position = event.position();
  275. if (m_in_drag_select && (rect().contains(event.position()) || !m_automatic_selection_scroll_timer->is_active())) {
  276. set_cursor(text_position_at(event.position()));
  277. m_selection.set_end(m_cursor);
  278. did_update_selection();
  279. update();
  280. return;
  281. }
  282. }
  283. void TextEditor::automatic_selection_scroll_timer_fired()
  284. {
  285. if (!m_in_drag_select) {
  286. m_automatic_selection_scroll_timer->stop();
  287. return;
  288. }
  289. set_cursor(text_position_at(m_last_mousemove_position));
  290. m_selection.set_end(m_cursor);
  291. did_update_selection();
  292. update();
  293. }
  294. int TextEditor::ruler_width() const
  295. {
  296. if (!m_ruler_visible)
  297. return 0;
  298. int line_count_digits = static_cast<int>(log10(line_count())) + 1;
  299. constexpr size_t padding = 5;
  300. return line_count() < 10 ? (line_count_digits + 1) * font().glyph_width('x') + padding : line_count_digits * font().glyph_width('x') + padding;
  301. }
  302. int TextEditor::gutter_width() const
  303. {
  304. if (!m_gutter_visible)
  305. return 0;
  306. return line_height(); // square gutter
  307. }
  308. Gfx::IntRect TextEditor::ruler_content_rect(size_t line_index) const
  309. {
  310. if (!m_ruler_visible)
  311. return {};
  312. return {
  313. 0 - ruler_width() + horizontal_scrollbar().value(),
  314. line_content_rect(line_index).y(),
  315. ruler_width(),
  316. line_content_rect(line_index).height()
  317. };
  318. }
  319. Gfx::IntRect TextEditor::gutter_content_rect(size_t line_index) const
  320. {
  321. if (!m_gutter_visible)
  322. return {};
  323. return {
  324. 0 - ruler_width() - gutter_width() + horizontal_scrollbar().value(),
  325. line_content_rect(line_index).y(),
  326. gutter_width(),
  327. line_content_rect(line_index).height()
  328. };
  329. }
  330. Gfx::IntRect TextEditor::ruler_rect_in_inner_coordinates() const
  331. {
  332. return { gutter_width(), 0, ruler_width(), height() - height_occupied_by_horizontal_scrollbar() };
  333. }
  334. Gfx::IntRect TextEditor::gutter_rect_in_inner_coordinates() const
  335. {
  336. return { 0, 0, gutter_width(), height() - height_occupied_by_horizontal_scrollbar() };
  337. }
  338. Gfx::IntRect TextEditor::visible_text_rect_in_inner_coordinates() const
  339. {
  340. return {
  341. m_horizontal_content_padding + (m_ruler_visible ? (ruler_rect_in_inner_coordinates().right() + 1) : 0),
  342. 0,
  343. frame_inner_rect().width() - (m_horizontal_content_padding * 2) - width_occupied_by_vertical_scrollbar() - ruler_width(),
  344. frame_inner_rect().height() - height_occupied_by_horizontal_scrollbar()
  345. };
  346. }
  347. void TextEditor::paint_event(PaintEvent& event)
  348. {
  349. Color widget_background_color = palette().color(is_enabled() ? background_role() : Gfx::ColorRole::Window);
  350. // NOTE: This ensures that spans are updated before we look at them.
  351. flush_pending_change_notification_if_needed();
  352. Frame::paint_event(event);
  353. Painter painter(*this);
  354. painter.add_clip_rect(widget_inner_rect());
  355. painter.add_clip_rect(event.rect());
  356. painter.fill_rect(event.rect(), widget_background_color);
  357. if (is_displayonly() && is_focused()) {
  358. widget_background_color = palette().selection();
  359. Gfx::IntRect display_rect {
  360. widget_inner_rect().x() + 1,
  361. widget_inner_rect().y() + 1,
  362. widget_inner_rect().width() - 2,
  363. widget_inner_rect().height() - 2
  364. };
  365. painter.add_clip_rect(display_rect);
  366. painter.add_clip_rect(event.rect());
  367. painter.fill_rect(event.rect(), widget_background_color);
  368. }
  369. painter.translate(frame_thickness(), frame_thickness());
  370. if (m_gutter_visible) {
  371. auto gutter_rect = gutter_rect_in_inner_coordinates();
  372. painter.fill_rect(gutter_rect, palette().gutter());
  373. if (!m_ruler_visible)
  374. painter.draw_line(gutter_rect.top_right(), gutter_rect.bottom_right(), palette().gutter_border());
  375. }
  376. if (m_ruler_visible) {
  377. auto ruler_rect = ruler_rect_in_inner_coordinates();
  378. painter.fill_rect(ruler_rect, palette().ruler());
  379. painter.draw_line(ruler_rect.top_right(), ruler_rect.bottom_right(), palette().ruler_border());
  380. }
  381. painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  382. painter.translate(gutter_width(), 0);
  383. painter.translate(ruler_width(), 0);
  384. size_t first_visible_line = text_position_at(event.rect().top_left()).line();
  385. size_t last_visible_line = text_position_at(event.rect().bottom_right()).line();
  386. auto selection = normalized_selection();
  387. bool has_selection = selection.is_valid();
  388. if (m_ruler_visible) {
  389. for (size_t i = first_visible_line; i <= last_visible_line; ++i) {
  390. bool is_current_line = i == m_cursor.line();
  391. auto ruler_line_rect = ruler_content_rect(i);
  392. painter.draw_text(
  393. ruler_line_rect.shrunken(2, 0).translated(0, m_line_spacing / 2),
  394. String::number(i + 1),
  395. is_current_line ? font().bold_variant() : font(),
  396. Gfx::TextAlignment::TopRight,
  397. is_current_line ? palette().ruler_active_text() : palette().ruler_inactive_text());
  398. }
  399. }
  400. auto text_left = 0;
  401. if (m_ruler_visible)
  402. text_left = ruler_rect_in_inner_coordinates().right() + 1;
  403. else if (m_gutter_visible)
  404. text_left = gutter_rect_in_inner_coordinates().right() + 1;
  405. text_left += frame_thickness();
  406. Gfx::IntRect text_clip_rect {
  407. 0,
  408. frame_thickness(),
  409. width() - width_occupied_by_vertical_scrollbar() - text_left,
  410. height() - height_occupied_by_horizontal_scrollbar()
  411. };
  412. text_clip_rect.translate_by(horizontal_scrollbar().value(), vertical_scrollbar().value());
  413. painter.add_clip_rect(text_clip_rect);
  414. size_t span_index = 0;
  415. if (document().has_spans()) {
  416. for (;;) {
  417. if (span_index >= document().spans().size() || document().spans()[span_index].range.end().line() >= first_visible_line) {
  418. break;
  419. }
  420. ++span_index;
  421. }
  422. }
  423. for (size_t line_index = first_visible_line; line_index <= last_visible_line; ++line_index) {
  424. auto& line = this->line(line_index);
  425. bool physical_line_has_selection = has_selection && line_index >= selection.start().line() && line_index <= selection.end().line();
  426. size_t first_visual_line_with_selection = 0;
  427. size_t last_visual_line_with_selection = 0;
  428. if (physical_line_has_selection) {
  429. if (selection.start().line() < line_index)
  430. first_visual_line_with_selection = 0;
  431. else
  432. first_visual_line_with_selection = visual_line_containing(line_index, selection.start().column());
  433. if (selection.end().line() > line_index)
  434. last_visual_line_with_selection = m_line_visual_data[line_index].visual_line_breaks.size();
  435. else
  436. last_visual_line_with_selection = visual_line_containing(line_index, selection.end().column());
  437. }
  438. size_t selection_start_column_within_line = selection.start().line() == line_index ? selection.start().column() : 0;
  439. size_t selection_end_column_within_line = selection.end().line() == line_index ? selection.end().column() : line.length();
  440. size_t visual_line_index = 0;
  441. for_each_visual_line(line_index, [&](const Gfx::IntRect& visual_line_rect, auto& visual_line_text, size_t start_of_visual_line, [[maybe_unused]] bool is_last_visual_line) {
  442. if (is_multi_line() && line_index == m_cursor.line())
  443. painter.fill_rect(visual_line_rect, widget_background_color.darkened(0.9f));
  444. if constexpr (TEXTEDITOR_DEBUG)
  445. painter.draw_rect(visual_line_rect, Color::Cyan);
  446. if (!placeholder().is_empty() && document().is_empty() && !is_focused() && line_index == 0) {
  447. auto line_rect = visual_line_rect;
  448. line_rect.set_width(font().width(placeholder()));
  449. painter.draw_text(line_rect, placeholder(), m_text_alignment, palette().color(Gfx::ColorRole::PlaceholderText));
  450. } else if (!document().has_spans()) {
  451. // Fast-path for plain text
  452. auto color = palette().color(is_enabled() ? foreground_role() : Gfx::ColorRole::DisabledText);
  453. if (is_displayonly() && is_focused())
  454. color = palette().color(is_enabled() ? Gfx::ColorRole::SelectionText : Gfx::ColorRole::DisabledText);
  455. painter.draw_text(visual_line_rect, visual_line_text, m_text_alignment, color);
  456. } else {
  457. auto unspanned_color = palette().color(is_enabled() ? foreground_role() : Gfx::ColorRole::DisabledText);
  458. if (is_displayonly() && is_focused())
  459. unspanned_color = palette().color(is_enabled() ? Gfx::ColorRole::SelectionText : Gfx::ColorRole::DisabledText);
  460. RefPtr<Gfx::Font> unspanned_font = this->font();
  461. size_t next_column = 0;
  462. Gfx::IntRect span_rect = { visual_line_rect.location(), { 0, line_height() } };
  463. auto draw_text_helper = [&](size_t start, size_t end, RefPtr<Gfx::Font>& font, Color& color, Optional<Color> background_color = {}, bool underline = false) {
  464. size_t length = end - start;
  465. if (length == 0)
  466. return;
  467. auto text = visual_line_text.substring_view(start, length);
  468. span_rect.set_width(font->width(text));
  469. if (background_color.has_value()) {
  470. painter.fill_rect(span_rect, background_color.value());
  471. }
  472. painter.draw_text(span_rect, text, *font, m_text_alignment, color);
  473. if (underline) {
  474. painter.draw_line(span_rect.bottom_left().translated(0, 1), span_rect.bottom_right().translated(0, 1), color);
  475. }
  476. span_rect.translate_by(span_rect.width(), 0);
  477. };
  478. for (;;) {
  479. if (span_index >= document().spans().size()) {
  480. break;
  481. }
  482. auto& span = document().spans()[span_index];
  483. if (!span.range.is_valid()) {
  484. ++span_index;
  485. continue;
  486. }
  487. if (span.range.end().line() < line_index) {
  488. dbgln_if(TEXTEDITOR_DEBUG, "spans not sorted (span end {}:{} is before current line {}) => ignoring", span.range.end().line(), span.range.end().column(), line_index);
  489. ++span_index;
  490. continue;
  491. }
  492. if (span.range.start().line() > line_index
  493. || (span.range.start().line() == line_index && span.range.start().column() >= start_of_visual_line + visual_line_text.length())) {
  494. // no more spans in this line, moving on
  495. break;
  496. }
  497. if (span.range.start().line() == span.range.end().line() && span.range.end().column() < span.range.start().column()) {
  498. dbgln_if(TEXTEDITOR_DEBUG, "span form {}:{} to {}:{} has negative length => ignoring", span.range.start().line(), span.range.start().column(), span.range.end().line(), span.range.end().column());
  499. ++span_index;
  500. continue;
  501. }
  502. if (span.range.end().line() == line_index && span.range.end().column() < start_of_visual_line + next_column) {
  503. dbgln_if(TEXTEDITOR_DEBUG, "spans not sorted (span end {}:{} is before current position {}:{}) => ignoring",
  504. span.range.end().line(), span.range.end().column(), line_index, start_of_visual_line + next_column);
  505. ++span_index;
  506. continue;
  507. }
  508. size_t span_start;
  509. if (span.range.start().line() < line_index || span.range.start().column() < start_of_visual_line) {
  510. span_start = 0;
  511. } else {
  512. span_start = span.range.start().column() - start_of_visual_line;
  513. }
  514. if (span_start < next_column) {
  515. dbgln_if(TEXTEDITOR_DEBUG, "span started before the current position, maybe two spans overlap? (span start {} is before current position {}) => ignoring", span_start, next_column);
  516. ++span_index;
  517. continue;
  518. }
  519. size_t span_end;
  520. bool span_consumned;
  521. if (span.range.end().line() > line_index || span.range.end().column() > start_of_visual_line + visual_line_text.length()) {
  522. span_end = visual_line_text.length();
  523. span_consumned = false;
  524. } else {
  525. span_end = span.range.end().column() - start_of_visual_line;
  526. span_consumned = true;
  527. }
  528. if (span_start != next_column) {
  529. // draw unspanned text between spans
  530. draw_text_helper(next_column, span_start, unspanned_font, unspanned_color);
  531. }
  532. auto font = unspanned_font;
  533. if (span.attributes.bold) {
  534. if (auto bold_font = Gfx::FontDatabase::the().get(font->family(), font->presentation_size(), 700))
  535. font = bold_font;
  536. }
  537. draw_text_helper(span_start, span_end, font, span.attributes.color, span.attributes.background_color, span.attributes.underline);
  538. next_column = span_end;
  539. if (!span_consumned) {
  540. // continue with same span on next line
  541. break;
  542. } else {
  543. ++span_index;
  544. }
  545. }
  546. // draw unspanned text after last span
  547. if (next_column < visual_line_text.length()) {
  548. draw_text_helper(next_column, visual_line_text.length(), unspanned_font, unspanned_color);
  549. }
  550. // consume all spans that should end this line
  551. // this is necessary since the spans can include the new line character
  552. while (is_last_visual_line && span_index < document().spans().size()) {
  553. auto& span = document().spans()[span_index];
  554. if (span.range.end().line() == line_index) {
  555. ++span_index;
  556. } else {
  557. break;
  558. }
  559. }
  560. }
  561. if (m_visualize_trailing_whitespace && line.ends_in_whitespace()) {
  562. size_t physical_column;
  563. auto last_non_whitespace_column = line.last_non_whitespace_column();
  564. if (last_non_whitespace_column.has_value())
  565. physical_column = last_non_whitespace_column.value() + 1;
  566. else
  567. physical_column = 0;
  568. size_t end_of_visual_line = (start_of_visual_line + visual_line_text.length());
  569. if (physical_column < end_of_visual_line) {
  570. size_t visual_column = physical_column > start_of_visual_line ? (physical_column - start_of_visual_line) : 0;
  571. Gfx::IntRect whitespace_rect {
  572. content_x_for_position({ line_index, visual_column }),
  573. visual_line_rect.y(),
  574. font().width(visual_line_text.substring_view(visual_column, visual_line_text.length() - visual_column)),
  575. visual_line_rect.height()
  576. };
  577. painter.fill_rect_with_dither_pattern(whitespace_rect, Color(), Color(255, 192, 192));
  578. }
  579. }
  580. if (m_visualize_leading_whitespace && line.leading_spaces() > 0) {
  581. size_t physical_column = line.leading_spaces();
  582. size_t end_of_leading_whitespace = (start_of_visual_line + physical_column);
  583. size_t end_of_visual_line = (start_of_visual_line + visual_line_text.length());
  584. if (end_of_leading_whitespace < end_of_visual_line) {
  585. Gfx::IntRect whitespace_rect {
  586. content_x_for_position({ line_index, start_of_visual_line }),
  587. visual_line_rect.y(),
  588. font().width(visual_line_text.substring_view(0, end_of_leading_whitespace)),
  589. visual_line_rect.height()
  590. };
  591. painter.fill_rect_with_dither_pattern(whitespace_rect, Color(), Color(192, 255, 192));
  592. }
  593. }
  594. if (physical_line_has_selection) {
  595. size_t start_of_selection_within_visual_line = (size_t)max(0, (int)selection_start_column_within_line - (int)start_of_visual_line);
  596. size_t end_of_selection_within_visual_line = selection_end_column_within_line - start_of_visual_line;
  597. bool current_visual_line_has_selection = start_of_selection_within_visual_line != end_of_selection_within_visual_line
  598. && ((line_index != selection.start().line() && line_index != selection.end().line())
  599. || (visual_line_index >= first_visual_line_with_selection && visual_line_index <= last_visual_line_with_selection));
  600. if (current_visual_line_has_selection) {
  601. bool selection_begins_on_current_visual_line = visual_line_index == first_visual_line_with_selection;
  602. bool selection_ends_on_current_visual_line = visual_line_index == last_visual_line_with_selection;
  603. int selection_left = selection_begins_on_current_visual_line
  604. ? content_x_for_position({ line_index, (size_t)selection_start_column_within_line })
  605. : m_horizontal_content_padding;
  606. int selection_right = selection_ends_on_current_visual_line
  607. ? content_x_for_position({ line_index, (size_t)selection_end_column_within_line })
  608. : visual_line_rect.right() + 1;
  609. Gfx::IntRect selection_rect {
  610. selection_left,
  611. visual_line_rect.y(),
  612. selection_right - selection_left,
  613. visual_line_rect.height()
  614. };
  615. Color background_color = is_focused() ? palette().selection() : palette().inactive_selection();
  616. Color text_color = is_focused() ? palette().selection_text() : palette().inactive_selection_text();
  617. painter.fill_rect(selection_rect, background_color);
  618. if (visual_line_text.code_points()) {
  619. Utf32View visual_selected_text {
  620. visual_line_text.code_points() + start_of_selection_within_visual_line,
  621. end_of_selection_within_visual_line - start_of_selection_within_visual_line
  622. };
  623. painter.draw_text(selection_rect, visual_selected_text, Gfx::TextAlignment::CenterLeft, text_color);
  624. }
  625. }
  626. }
  627. ++visual_line_index;
  628. return IterationDecision::Continue;
  629. });
  630. }
  631. if (!is_multi_line() && m_icon) {
  632. Gfx::IntRect icon_rect { icon_padding(), 1, icon_size(), icon_size() };
  633. painter.draw_scaled_bitmap(icon_rect, *m_icon, m_icon->rect());
  634. }
  635. if (is_focused() && m_cursor_state && !is_displayonly())
  636. painter.fill_rect(cursor_content_rect(), palette().text_cursor());
  637. }
  638. void TextEditor::select_all()
  639. {
  640. TextPosition start_of_document { 0, 0 };
  641. TextPosition end_of_document { line_count() - 1, line(line_count() - 1).length() };
  642. m_selection.set(end_of_document, start_of_document);
  643. did_update_selection();
  644. set_cursor(start_of_document);
  645. update();
  646. }
  647. void TextEditor::keydown_event(KeyEvent& event)
  648. {
  649. TemporaryChange change { m_should_keep_autocomplete_box, true };
  650. if (m_autocomplete_box && m_autocomplete_box->is_visible() && (event.key() == KeyCode::Key_Return || event.key() == KeyCode::Key_Tab)) {
  651. m_autocomplete_box->apply_suggestion();
  652. m_autocomplete_box->close();
  653. return;
  654. }
  655. if (m_autocomplete_box && m_autocomplete_box->is_visible() && event.key() == KeyCode::Key_Escape) {
  656. m_autocomplete_box->close();
  657. return;
  658. }
  659. if (m_autocomplete_box && m_autocomplete_box->is_visible() && event.key() == KeyCode::Key_Up) {
  660. m_autocomplete_box->previous_suggestion();
  661. return;
  662. }
  663. if (m_autocomplete_box && m_autocomplete_box->is_visible() && event.key() == KeyCode::Key_Down) {
  664. m_autocomplete_box->next_suggestion();
  665. return;
  666. }
  667. if (is_single_line()) {
  668. if (event.key() == KeyCode::Key_Tab)
  669. return AbstractScrollableWidget::keydown_event(event);
  670. if (event.modifiers() == KeyModifier::Mod_Shift && event.key() == KeyCode::Key_Return) {
  671. if (on_shift_return_pressed)
  672. on_shift_return_pressed();
  673. return;
  674. }
  675. if (event.key() == KeyCode::Key_Return) {
  676. if (on_return_pressed)
  677. on_return_pressed();
  678. return;
  679. }
  680. if (event.key() == KeyCode::Key_Up) {
  681. if (on_up_pressed)
  682. on_up_pressed();
  683. return;
  684. }
  685. if (event.key() == KeyCode::Key_Down) {
  686. if (on_down_pressed)
  687. on_down_pressed();
  688. return;
  689. }
  690. if (event.key() == KeyCode::Key_PageUp) {
  691. if (on_pageup_pressed)
  692. on_pageup_pressed();
  693. return;
  694. }
  695. if (event.key() == KeyCode::Key_PageDown) {
  696. if (on_pagedown_pressed)
  697. on_pagedown_pressed();
  698. return;
  699. }
  700. } else if (!is_multi_line()) {
  701. VERIFY_NOT_REACHED();
  702. }
  703. ArmedScopeGuard update_autocomplete { [&] {
  704. if (m_autocomplete_box && m_autocomplete_box->is_visible()) {
  705. m_autocomplete_provider->provide_completions([&](auto completions) {
  706. m_autocomplete_box->update_suggestions(move(completions));
  707. });
  708. }
  709. } };
  710. if (is_multi_line() && !event.shift() && !event.alt() && event.ctrl() && event.key() == KeyCode::Key_Space) {
  711. if (m_autocomplete_provider) {
  712. try_show_autocomplete();
  713. update_autocomplete.disarm();
  714. return;
  715. }
  716. }
  717. if (m_editing_engine->on_key(event))
  718. return;
  719. if (event.key() == KeyCode::Key_Escape) {
  720. if (on_escape_pressed)
  721. on_escape_pressed();
  722. return;
  723. }
  724. if (event.modifiers() == Mod_Shift && event.key() == KeyCode::Key_Delete) {
  725. if (m_autocomplete_box)
  726. m_autocomplete_box->close();
  727. return;
  728. }
  729. if (event.key() == KeyCode::Key_Delete) {
  730. if (m_autocomplete_box)
  731. m_autocomplete_box->close();
  732. return;
  733. }
  734. if (event.key() == KeyCode::Key_Backspace) {
  735. if (!is_editable())
  736. return;
  737. if (m_autocomplete_box)
  738. m_autocomplete_box->close();
  739. if (has_selection()) {
  740. delete_selection();
  741. did_update_selection();
  742. return;
  743. }
  744. if (m_cursor.column() > 0) {
  745. int erase_count = 1;
  746. if (event.modifiers() == Mod_Ctrl) {
  747. auto word_break_pos = document().first_word_break_before(m_cursor, true);
  748. erase_count = m_cursor.column() - word_break_pos.column();
  749. } else if (current_line().first_non_whitespace_column() >= m_cursor.column()) {
  750. int new_column;
  751. if (m_cursor.column() % m_soft_tab_width == 0)
  752. new_column = m_cursor.column() - m_soft_tab_width;
  753. else
  754. new_column = (m_cursor.column() / m_soft_tab_width) * m_soft_tab_width;
  755. erase_count = m_cursor.column() - new_column;
  756. }
  757. // Backspace within line
  758. TextRange erased_range({ m_cursor.line(), m_cursor.column() - erase_count }, m_cursor);
  759. auto erased_text = document().text_in_range(erased_range);
  760. execute<RemoveTextCommand>(erased_text, erased_range);
  761. return;
  762. }
  763. if (m_cursor.column() == 0 && m_cursor.line() != 0) {
  764. // Backspace at column 0; merge with previous line
  765. size_t previous_length = line(m_cursor.line() - 1).length();
  766. TextRange erased_range({ m_cursor.line() - 1, previous_length }, m_cursor);
  767. execute<RemoveTextCommand>("\n", erased_range);
  768. return;
  769. }
  770. return;
  771. }
  772. if (!event.ctrl() && !event.alt() && event.code_point() != 0) {
  773. add_code_point(event.code_point());
  774. return;
  775. }
  776. event.ignore();
  777. }
  778. void TextEditor::delete_current_line()
  779. {
  780. if (has_selection())
  781. return delete_selection();
  782. TextPosition start;
  783. TextPosition end;
  784. if (m_cursor.line() == 0 && line_count() == 1) {
  785. start = { 0, 0 };
  786. end = { 0, line(0).length() };
  787. } else if (m_cursor.line() == line_count() - 1) {
  788. start = { m_cursor.line() - 1, line(m_cursor.line() - 1).length() };
  789. end = { m_cursor.line(), line(m_cursor.line()).length() };
  790. } else {
  791. start = { m_cursor.line(), 0 };
  792. end = { m_cursor.line() + 1, 0 };
  793. }
  794. TextRange erased_range(start, end);
  795. execute<RemoveTextCommand>(document().text_in_range(erased_range), erased_range);
  796. }
  797. void TextEditor::do_delete()
  798. {
  799. if (!is_editable())
  800. return;
  801. if (has_selection())
  802. return delete_selection();
  803. if (m_cursor.column() < current_line().length()) {
  804. // Delete within line
  805. TextRange erased_range(m_cursor, { m_cursor.line(), m_cursor.column() + 1 });
  806. execute<RemoveTextCommand>(document().text_in_range(erased_range), erased_range);
  807. return;
  808. }
  809. if (m_cursor.column() == current_line().length() && m_cursor.line() != line_count() - 1) {
  810. // Delete at end of line; merge with next line
  811. TextRange erased_range(m_cursor, { m_cursor.line() + 1, 0 });
  812. execute<RemoveTextCommand>(document().text_in_range(erased_range), erased_range);
  813. return;
  814. }
  815. }
  816. void TextEditor::add_code_point(u32 code_point)
  817. {
  818. if (!is_editable())
  819. return;
  820. StringBuilder sb;
  821. sb.append_code_point(code_point);
  822. if (should_autocomplete_automatically()) {
  823. if (sb.string_view().is_whitespace())
  824. m_autocomplete_timer->stop();
  825. else
  826. m_autocomplete_timer->start();
  827. }
  828. insert_at_cursor_or_replace_selection(sb.to_string());
  829. };
  830. void TextEditor::reset_cursor_blink()
  831. {
  832. m_cursor_state = true;
  833. update_cursor();
  834. stop_timer();
  835. start_timer(500);
  836. }
  837. void TextEditor::update_selection(bool is_selecting)
  838. {
  839. if (is_selecting && !selection()->is_valid()) {
  840. selection()->set(cursor(), {});
  841. did_update_selection();
  842. update();
  843. return;
  844. }
  845. if (!is_selecting && selection()->is_valid()) {
  846. selection()->clear();
  847. did_update_selection();
  848. update();
  849. return;
  850. }
  851. if (is_selecting && selection()->start().is_valid()) {
  852. selection()->set_end(cursor());
  853. did_update_selection();
  854. update();
  855. return;
  856. }
  857. }
  858. int TextEditor::content_x_for_position(const TextPosition& position) const
  859. {
  860. auto& line = this->line(position.line());
  861. int x_offset = 0;
  862. switch (m_text_alignment) {
  863. case Gfx::TextAlignment::CenterLeft:
  864. for_each_visual_line(position.line(), [&](const Gfx::IntRect&, auto& visual_line_view, size_t start_of_visual_line, bool is_last_visual_line) {
  865. size_t offset_in_visual_line = position.column() - start_of_visual_line;
  866. auto before_line_end = is_last_visual_line ? (offset_in_visual_line <= visual_line_view.length()) : (offset_in_visual_line < visual_line_view.length());
  867. if (position.column() >= start_of_visual_line && before_line_end) {
  868. if (offset_in_visual_line == 0) {
  869. x_offset = 0;
  870. } else {
  871. x_offset = font().width(visual_line_view.substring_view(0, offset_in_visual_line));
  872. x_offset += font().glyph_spacing();
  873. }
  874. return IterationDecision::Break;
  875. }
  876. return IterationDecision::Continue;
  877. });
  878. return m_horizontal_content_padding + ((is_single_line() && icon()) ? (icon_size() + icon_padding()) : 0) + x_offset;
  879. case Gfx::TextAlignment::CenterRight:
  880. // FIXME
  881. VERIFY(!is_wrapping_enabled());
  882. return content_width() - m_horizontal_content_padding - (line.length() * fixed_glyph_width()) + (position.column() * fixed_glyph_width());
  883. default:
  884. VERIFY_NOT_REACHED();
  885. }
  886. }
  887. Gfx::IntRect TextEditor::content_rect_for_position(const TextPosition& position) const
  888. {
  889. if (!position.is_valid())
  890. return {};
  891. VERIFY(!lines().is_empty());
  892. VERIFY(position.column() <= (current_line().length() + 1));
  893. int x = content_x_for_position(position);
  894. if (is_single_line()) {
  895. Gfx::IntRect rect { x, 0, 1, line_height() };
  896. rect.center_vertically_within({ {}, frame_inner_rect().size() });
  897. return rect;
  898. }
  899. Gfx::IntRect rect;
  900. for_each_visual_line(position.line(), [&](const Gfx::IntRect& visual_line_rect, auto& view, size_t start_of_visual_line, bool is_last_visual_line) {
  901. auto before_line_end = is_last_visual_line ? ((position.column() - start_of_visual_line) <= view.length()) : ((position.column() - start_of_visual_line) < view.length());
  902. if (position.column() >= start_of_visual_line && before_line_end) {
  903. // NOTE: We have to subtract the horizontal padding here since it's part of the visual line rect
  904. // *and* included in what we get from content_x_for_position().
  905. rect = {
  906. visual_line_rect.x() + x - (m_horizontal_content_padding),
  907. visual_line_rect.y(),
  908. m_editing_engine->cursor_width() == CursorWidth::WIDE ? 7 : 1,
  909. line_height()
  910. };
  911. return IterationDecision::Break;
  912. }
  913. return IterationDecision::Continue;
  914. });
  915. return rect;
  916. }
  917. Gfx::IntRect TextEditor::cursor_content_rect() const
  918. {
  919. return content_rect_for_position(m_cursor);
  920. }
  921. Gfx::IntRect TextEditor::line_widget_rect(size_t line_index) const
  922. {
  923. auto rect = line_content_rect(line_index);
  924. rect.set_x(frame_thickness());
  925. rect.set_width(frame_inner_rect().width());
  926. rect.translate_by(0, -(vertical_scrollbar().value()));
  927. rect.translate_by(0, frame_thickness());
  928. rect.intersect(frame_inner_rect());
  929. return rect;
  930. }
  931. void TextEditor::scroll_position_into_view(const TextPosition& position)
  932. {
  933. auto rect = content_rect_for_position(position);
  934. if (position.column() == 0)
  935. rect.set_x(content_x_for_position({ position.line(), 0 }) - 2);
  936. else if (position.column() == line(position.line()).length())
  937. rect.set_x(content_x_for_position({ position.line(), line(position.line()).length() }) + 2);
  938. scroll_into_view(rect, true, true);
  939. }
  940. void TextEditor::scroll_cursor_into_view()
  941. {
  942. if (!m_reflow_deferred)
  943. scroll_position_into_view(m_cursor);
  944. }
  945. Gfx::IntRect TextEditor::line_content_rect(size_t line_index) const
  946. {
  947. auto& line = this->line(line_index);
  948. if (is_single_line()) {
  949. Gfx::IntRect line_rect = { content_x_for_position({ line_index, 0 }), 0, font().width(line.view()), font().glyph_height() + 4 };
  950. line_rect.center_vertically_within({ {}, frame_inner_rect().size() });
  951. return line_rect;
  952. }
  953. if (is_wrapping_enabled())
  954. return m_line_visual_data[line_index].visual_rect;
  955. return {
  956. content_x_for_position({ line_index, 0 }),
  957. (int)line_index * line_height(),
  958. font().width(line.view()),
  959. line_height()
  960. };
  961. }
  962. void TextEditor::set_cursor_and_focus_line(size_t line, size_t column)
  963. {
  964. u_int index_max = line_count() - 1;
  965. set_cursor(line, column);
  966. if (line > 1 && line < index_max) {
  967. int headroom = frame_inner_rect().height() / 3;
  968. do {
  969. auto line_data = m_line_visual_data[line];
  970. headroom -= line_data.visual_rect.height();
  971. line--;
  972. } while (line > 0 && headroom > 0);
  973. Gfx::IntRect rect = { 0, line_content_rect(line).y(),
  974. 1, frame_inner_rect().height() };
  975. scroll_into_view(rect, false, true);
  976. }
  977. }
  978. void TextEditor::update_cursor()
  979. {
  980. update(line_widget_rect(m_cursor.line()));
  981. }
  982. void TextEditor::set_cursor(size_t line, size_t column)
  983. {
  984. set_cursor({ line, column });
  985. }
  986. void TextEditor::set_cursor(const TextPosition& a_position)
  987. {
  988. VERIFY(!lines().is_empty());
  989. TextPosition position = a_position;
  990. if (position.line() >= line_count())
  991. position.set_line(line_count() - 1);
  992. if (position.column() > lines()[position.line()].length())
  993. position.set_column(lines()[position.line()].length());
  994. if (m_cursor != position && is_visual_data_up_to_date()) {
  995. // NOTE: If the old cursor is no longer valid, repaint everything just in case.
  996. auto old_cursor_line_rect = m_cursor.line() < line_count()
  997. ? line_widget_rect(m_cursor.line())
  998. : rect();
  999. m_cursor = position;
  1000. m_cursor_state = true;
  1001. scroll_cursor_into_view();
  1002. update(old_cursor_line_rect);
  1003. update_cursor();
  1004. } else if (m_cursor != position) {
  1005. m_cursor = position;
  1006. m_cursor_state = true;
  1007. }
  1008. cursor_did_change();
  1009. if (on_cursor_change)
  1010. on_cursor_change();
  1011. if (m_highlighter)
  1012. m_highlighter->cursor_did_change();
  1013. }
  1014. void TextEditor::focusin_event(FocusEvent& event)
  1015. {
  1016. if (event.source() == FocusSource::Keyboard)
  1017. select_all();
  1018. m_cursor_state = true;
  1019. update_cursor();
  1020. stop_timer();
  1021. start_timer(500);
  1022. if (on_focusin)
  1023. on_focusin();
  1024. }
  1025. void TextEditor::focusout_event(FocusEvent&)
  1026. {
  1027. if (is_displayonly() && has_selection())
  1028. m_selection.clear();
  1029. stop_timer();
  1030. if (on_focusout)
  1031. on_focusout();
  1032. }
  1033. void TextEditor::timer_event(Core::TimerEvent&)
  1034. {
  1035. m_cursor_state = !m_cursor_state;
  1036. if (is_focused())
  1037. update_cursor();
  1038. }
  1039. bool TextEditor::write_to_file(const String& path)
  1040. {
  1041. int fd = open(path.characters(), O_WRONLY | O_CREAT | O_TRUNC, 0666);
  1042. if (fd < 0) {
  1043. perror("open");
  1044. return false;
  1045. }
  1046. ScopeGuard fd_guard = [fd] { close(fd); };
  1047. off_t file_size = 0;
  1048. if (line_count() == 1 && line(0).is_empty()) {
  1049. // Truncate to zero.
  1050. } else {
  1051. // Compute the final file size and ftruncate() to make writing fast.
  1052. // FIXME: Remove this once the kernel is smart enough to do this instead.
  1053. for (size_t i = 0; i < line_count(); ++i)
  1054. file_size += line(i).length();
  1055. file_size += line_count();
  1056. }
  1057. if (ftruncate(fd, file_size) < 0) {
  1058. perror("ftruncate");
  1059. return false;
  1060. }
  1061. if (file_size == 0) {
  1062. // A size 0 file doesn't need a data copy.
  1063. } else {
  1064. for (size_t i = 0; i < line_count(); ++i) {
  1065. auto& line = this->line(i);
  1066. if (line.length()) {
  1067. auto line_as_utf8 = line.to_utf8();
  1068. ssize_t nwritten = write(fd, line_as_utf8.characters(), line_as_utf8.length());
  1069. if (nwritten < 0) {
  1070. perror("write");
  1071. return false;
  1072. }
  1073. }
  1074. char ch = '\n';
  1075. ssize_t nwritten = write(fd, &ch, 1);
  1076. if (nwritten != 1) {
  1077. perror("write");
  1078. return false;
  1079. }
  1080. }
  1081. }
  1082. document().set_unmodified();
  1083. return true;
  1084. }
  1085. String TextEditor::text() const
  1086. {
  1087. return document().text();
  1088. }
  1089. void TextEditor::clear()
  1090. {
  1091. document().remove_all_lines();
  1092. document().append_line(make<TextDocumentLine>(document()));
  1093. m_selection.clear();
  1094. did_update_selection();
  1095. set_cursor(0, 0);
  1096. update();
  1097. }
  1098. String TextEditor::selected_text() const
  1099. {
  1100. if (!has_selection())
  1101. return {};
  1102. return document().text_in_range(m_selection);
  1103. }
  1104. size_t TextEditor::number_of_selected_words() const
  1105. {
  1106. if (!has_selection())
  1107. return 0;
  1108. size_t word_count = 0;
  1109. bool in_word = false;
  1110. auto selected_text = this->selected_text();
  1111. for (char c : selected_text) {
  1112. if (in_word && is_ascii_space(c)) {
  1113. in_word = false;
  1114. word_count++;
  1115. continue;
  1116. }
  1117. if (!in_word && !is_ascii_space(c))
  1118. in_word = true;
  1119. }
  1120. if (in_word)
  1121. word_count++;
  1122. return word_count;
  1123. }
  1124. void TextEditor::delete_selection()
  1125. {
  1126. auto selection = normalized_selection();
  1127. auto selected = selected_text();
  1128. m_selection.clear();
  1129. execute<RemoveTextCommand>(selected, selection);
  1130. did_update_selection();
  1131. did_change();
  1132. set_cursor(selection.start());
  1133. update();
  1134. }
  1135. void TextEditor::delete_text_range(TextRange range)
  1136. {
  1137. auto normalized_range = range.normalized();
  1138. execute<RemoveTextCommand>(document().text_in_range(normalized_range), normalized_range);
  1139. did_change();
  1140. set_cursor(normalized_range.start());
  1141. update();
  1142. }
  1143. void TextEditor::insert_at_cursor_or_replace_selection(const StringView& text)
  1144. {
  1145. ReflowDeferrer defer(*this);
  1146. VERIFY(is_editable());
  1147. if (has_selection())
  1148. delete_selection();
  1149. // Check if adding a newline leaves the previous line as just whitespace.
  1150. auto const clear_length = m_cursor.column();
  1151. auto const should_clear_last_line = text == "\n"
  1152. && clear_length > 0
  1153. && current_line().leading_spaces() == clear_length;
  1154. execute<InsertTextCommand>(text, m_cursor);
  1155. if (should_clear_last_line) { // If it does leave just whitespace, clear it.
  1156. auto const original_cursor_position = cursor();
  1157. TextPosition start(original_cursor_position.line() - 1, 0);
  1158. TextPosition end(original_cursor_position.line() - 1, clear_length);
  1159. TextRange erased_range(start, end);
  1160. execute<RemoveTextCommand>(document().text_in_range(erased_range), erased_range);
  1161. set_cursor(original_cursor_position);
  1162. }
  1163. }
  1164. void TextEditor::cut()
  1165. {
  1166. if (!is_editable())
  1167. return;
  1168. auto selected_text = this->selected_text();
  1169. dbgln_if(TEXTEDITOR_DEBUG, "Cut: \"{}\"", selected_text);
  1170. Clipboard::the().set_plain_text(selected_text);
  1171. delete_selection();
  1172. }
  1173. void TextEditor::copy()
  1174. {
  1175. auto selected_text = this->selected_text();
  1176. dbgln_if(TEXTEDITOR_DEBUG, "Copy: \"{}\"\n", selected_text);
  1177. Clipboard::the().set_plain_text(selected_text);
  1178. }
  1179. void TextEditor::paste()
  1180. {
  1181. if (!is_editable())
  1182. return;
  1183. auto paste_text = Clipboard::the().data();
  1184. if (paste_text.is_empty())
  1185. return;
  1186. dbgln_if(TEXTEDITOR_DEBUG, "Paste: \"{}\"", String::copy(paste_text));
  1187. TemporaryChange change(m_automatic_indentation_enabled, false);
  1188. insert_at_cursor_or_replace_selection(paste_text);
  1189. }
  1190. void TextEditor::defer_reflow()
  1191. {
  1192. ++m_reflow_deferred;
  1193. }
  1194. void TextEditor::undefer_reflow()
  1195. {
  1196. VERIFY(m_reflow_deferred);
  1197. if (!--m_reflow_deferred) {
  1198. if (m_reflow_requested) {
  1199. recompute_all_visual_lines();
  1200. scroll_cursor_into_view();
  1201. }
  1202. }
  1203. }
  1204. void TextEditor::try_show_autocomplete()
  1205. {
  1206. if (m_autocomplete_provider) {
  1207. m_autocomplete_provider->provide_completions([&](auto completions) {
  1208. auto has_completions = !completions.is_empty();
  1209. m_autocomplete_box->update_suggestions(move(completions));
  1210. auto position = content_rect_for_position(cursor()).translated(0, -visible_content_rect().y()).bottom_right().translated(screen_relative_rect().top_left().translated(ruler_width(), 0).translated(10, 5));
  1211. if (has_completions)
  1212. m_autocomplete_box->show(position);
  1213. });
  1214. }
  1215. }
  1216. void TextEditor::enter_event(Core::Event&)
  1217. {
  1218. m_automatic_selection_scroll_timer->stop();
  1219. }
  1220. void TextEditor::leave_event(Core::Event&)
  1221. {
  1222. if (m_in_drag_select)
  1223. m_automatic_selection_scroll_timer->start();
  1224. }
  1225. void TextEditor::did_change()
  1226. {
  1227. update_content_size();
  1228. recompute_all_visual_lines();
  1229. if (m_autocomplete_box && !m_should_keep_autocomplete_box) {
  1230. m_autocomplete_box->close();
  1231. if (m_autocomplete_timer)
  1232. m_autocomplete_timer->stop();
  1233. }
  1234. if (!m_has_pending_change_notification) {
  1235. m_has_pending_change_notification = true;
  1236. deferred_invoke([this](auto&) {
  1237. if (!m_has_pending_change_notification)
  1238. return;
  1239. if (on_change)
  1240. on_change();
  1241. if (m_highlighter)
  1242. m_highlighter->rehighlight(palette());
  1243. m_has_pending_change_notification = false;
  1244. });
  1245. }
  1246. }
  1247. void TextEditor::set_mode(const Mode mode)
  1248. {
  1249. if (m_mode == mode)
  1250. return;
  1251. m_mode = mode;
  1252. switch (mode) {
  1253. case Editable:
  1254. m_cut_action->set_enabled(true && has_selection());
  1255. m_delete_action->set_enabled(true);
  1256. m_paste_action->set_enabled(true);
  1257. set_accepts_emoji_input(true);
  1258. break;
  1259. case DisplayOnly:
  1260. case ReadOnly:
  1261. m_cut_action->set_enabled(false && has_selection());
  1262. m_delete_action->set_enabled(false);
  1263. m_paste_action->set_enabled(false);
  1264. set_accepts_emoji_input(false);
  1265. break;
  1266. default:
  1267. VERIFY_NOT_REACHED();
  1268. }
  1269. if (!is_displayonly())
  1270. set_override_cursor(Gfx::StandardCursor::IBeam);
  1271. else
  1272. set_override_cursor(Gfx::StandardCursor::None);
  1273. }
  1274. void TextEditor::did_update_selection()
  1275. {
  1276. m_cut_action->set_enabled(is_editable() && has_selection());
  1277. m_copy_action->set_enabled(has_selection());
  1278. if (on_selection_change)
  1279. on_selection_change();
  1280. if (is_wrapping_enabled()) {
  1281. // FIXME: Try to repaint less.
  1282. update();
  1283. }
  1284. }
  1285. void TextEditor::context_menu_event(ContextMenuEvent& event)
  1286. {
  1287. if (is_displayonly())
  1288. return;
  1289. if (!m_context_menu) {
  1290. m_context_menu = Menu::construct();
  1291. m_context_menu->add_action(undo_action());
  1292. m_context_menu->add_action(redo_action());
  1293. m_context_menu->add_separator();
  1294. m_context_menu->add_action(cut_action());
  1295. m_context_menu->add_action(copy_action());
  1296. m_context_menu->add_action(paste_action());
  1297. m_context_menu->add_action(delete_action());
  1298. m_context_menu->add_separator();
  1299. m_context_menu->add_action(select_all_action());
  1300. if (is_multi_line()) {
  1301. m_context_menu->add_separator();
  1302. m_context_menu->add_action(go_to_line_action());
  1303. }
  1304. if (!m_custom_context_menu_actions.is_empty()) {
  1305. m_context_menu->add_separator();
  1306. for (auto& action : m_custom_context_menu_actions) {
  1307. m_context_menu->add_action(action);
  1308. }
  1309. }
  1310. }
  1311. m_context_menu->popup(event.screen_position());
  1312. }
  1313. void TextEditor::set_text_alignment(Gfx::TextAlignment alignment)
  1314. {
  1315. if (m_text_alignment == alignment)
  1316. return;
  1317. m_text_alignment = alignment;
  1318. update();
  1319. }
  1320. void TextEditor::resize_event(ResizeEvent& event)
  1321. {
  1322. AbstractScrollableWidget::resize_event(event);
  1323. update_content_size();
  1324. recompute_all_visual_lines();
  1325. }
  1326. void TextEditor::theme_change_event(ThemeChangeEvent& event)
  1327. {
  1328. AbstractScrollableWidget::theme_change_event(event);
  1329. if (m_highlighter)
  1330. m_highlighter->rehighlight(palette());
  1331. }
  1332. void TextEditor::set_selection(const TextRange& selection)
  1333. {
  1334. if (m_selection == selection)
  1335. return;
  1336. m_selection = selection;
  1337. set_cursor(m_selection.end());
  1338. scroll_position_into_view(normalized_selection().start());
  1339. update();
  1340. }
  1341. void TextEditor::clear_selection()
  1342. {
  1343. if (!has_selection())
  1344. return;
  1345. m_selection.clear();
  1346. update();
  1347. }
  1348. void TextEditor::recompute_all_visual_lines()
  1349. {
  1350. if (m_reflow_deferred) {
  1351. m_reflow_requested = true;
  1352. return;
  1353. }
  1354. m_reflow_requested = false;
  1355. int y_offset = 0;
  1356. for (size_t line_index = 0; line_index < line_count(); ++line_index) {
  1357. recompute_visual_lines(line_index);
  1358. m_line_visual_data[line_index].visual_rect.set_y(y_offset);
  1359. y_offset += m_line_visual_data[line_index].visual_rect.height();
  1360. }
  1361. update_content_size();
  1362. }
  1363. void TextEditor::ensure_cursor_is_valid()
  1364. {
  1365. auto new_cursor = m_cursor;
  1366. if (new_cursor.line() >= line_count())
  1367. new_cursor.set_line(line_count() - 1);
  1368. if (new_cursor.column() > line(new_cursor.line()).length())
  1369. new_cursor.set_column(line(new_cursor.line()).length());
  1370. if (m_cursor != new_cursor)
  1371. set_cursor(new_cursor);
  1372. }
  1373. size_t TextEditor::visual_line_containing(size_t line_index, size_t column) const
  1374. {
  1375. size_t visual_line_index = 0;
  1376. for_each_visual_line(line_index, [&](const Gfx::IntRect&, auto& view, size_t start_of_visual_line, [[maybe_unused]] bool is_last_visual_line) {
  1377. if (column >= start_of_visual_line && ((column - start_of_visual_line) < view.length()))
  1378. return IterationDecision::Break;
  1379. ++visual_line_index;
  1380. return IterationDecision::Continue;
  1381. });
  1382. return visual_line_index;
  1383. }
  1384. void TextEditor::recompute_visual_lines(size_t line_index)
  1385. {
  1386. auto& line = document().line(line_index);
  1387. auto& visual_data = m_line_visual_data[line_index];
  1388. visual_data.visual_line_breaks.clear_with_capacity();
  1389. int available_width = visible_text_rect_in_inner_coordinates().width();
  1390. if (is_wrapping_enabled()) {
  1391. int line_width_so_far = 0;
  1392. size_t last_whitespace_index = 0;
  1393. size_t line_width_since_last_whitespace = 0;
  1394. auto glyph_spacing = font().glyph_spacing();
  1395. for (size_t i = 0; i < line.length(); ++i) {
  1396. auto code_point = line.code_points()[i];
  1397. if (is_ascii_space(code_point)) {
  1398. last_whitespace_index = i;
  1399. line_width_since_last_whitespace = 0;
  1400. }
  1401. auto glyph_width = font().glyph_or_emoji_width(code_point);
  1402. line_width_since_last_whitespace += glyph_width + glyph_spacing;
  1403. if ((line_width_so_far + glyph_width + glyph_spacing) > available_width) {
  1404. if (m_wrapping_mode == WrappingMode::WrapAtWords && last_whitespace_index != 0) {
  1405. // Plus 1 to get the first letter of the word.
  1406. visual_data.visual_line_breaks.append(last_whitespace_index + 1);
  1407. line_width_so_far = line_width_since_last_whitespace;
  1408. last_whitespace_index = 0;
  1409. line_width_since_last_whitespace = 0;
  1410. } else {
  1411. visual_data.visual_line_breaks.append(i);
  1412. line_width_so_far = glyph_width + glyph_spacing;
  1413. }
  1414. continue;
  1415. }
  1416. line_width_so_far += glyph_width + glyph_spacing;
  1417. }
  1418. }
  1419. visual_data.visual_line_breaks.append(line.length());
  1420. if (is_wrapping_enabled())
  1421. visual_data.visual_rect = { m_horizontal_content_padding, 0, available_width, static_cast<int>(visual_data.visual_line_breaks.size()) * line_height() };
  1422. else
  1423. visual_data.visual_rect = { m_horizontal_content_padding, 0, font().width(line.view()), line_height() };
  1424. }
  1425. template<typename Callback>
  1426. void TextEditor::for_each_visual_line(size_t line_index, Callback callback) const
  1427. {
  1428. auto editor_visible_text_rect = visible_text_rect_in_inner_coordinates();
  1429. size_t start_of_line = 0;
  1430. size_t visual_line_index = 0;
  1431. auto& line = document().line(line_index);
  1432. auto& visual_data = m_line_visual_data[line_index];
  1433. for (auto visual_line_break : visual_data.visual_line_breaks) {
  1434. auto visual_line_view = Utf32View(line.code_points() + start_of_line, visual_line_break - start_of_line);
  1435. Gfx::IntRect visual_line_rect {
  1436. visual_data.visual_rect.x(),
  1437. visual_data.visual_rect.y() + ((int)visual_line_index * line_height()),
  1438. font().width(visual_line_view) + font().glyph_spacing(),
  1439. line_height()
  1440. };
  1441. if (is_right_text_alignment(text_alignment()))
  1442. visual_line_rect.set_right_without_resize(editor_visible_text_rect.right());
  1443. if (is_single_line()) {
  1444. visual_line_rect.center_vertically_within(editor_visible_text_rect);
  1445. if (m_icon)
  1446. visual_line_rect.translate_by(icon_size() + icon_padding(), 0);
  1447. }
  1448. if (callback(visual_line_rect, visual_line_view, start_of_line, visual_line_index == visual_data.visual_line_breaks.size() - 1) == IterationDecision::Break)
  1449. break;
  1450. start_of_line = visual_line_break;
  1451. ++visual_line_index;
  1452. }
  1453. }
  1454. void TextEditor::set_wrapping_mode(WrappingMode mode)
  1455. {
  1456. if (m_wrapping_mode == mode)
  1457. return;
  1458. m_wrapping_mode = mode;
  1459. horizontal_scrollbar().set_visible(m_wrapping_mode == WrappingMode::NoWrap);
  1460. update_content_size();
  1461. recompute_all_visual_lines();
  1462. update();
  1463. }
  1464. void TextEditor::add_custom_context_menu_action(Action& action)
  1465. {
  1466. m_custom_context_menu_actions.append(action);
  1467. }
  1468. void TextEditor::did_change_font()
  1469. {
  1470. vertical_scrollbar().set_step(line_height());
  1471. recompute_all_visual_lines();
  1472. update();
  1473. AbstractScrollableWidget::did_change_font();
  1474. }
  1475. void TextEditor::document_did_append_line()
  1476. {
  1477. m_line_visual_data.append(make<LineVisualData>());
  1478. recompute_all_visual_lines();
  1479. update();
  1480. }
  1481. void TextEditor::document_did_remove_line(size_t line_index)
  1482. {
  1483. m_line_visual_data.remove(line_index);
  1484. recompute_all_visual_lines();
  1485. update();
  1486. }
  1487. void TextEditor::document_did_remove_all_lines()
  1488. {
  1489. m_line_visual_data.clear();
  1490. recompute_all_visual_lines();
  1491. update();
  1492. }
  1493. void TextEditor::document_did_insert_line(size_t line_index)
  1494. {
  1495. m_line_visual_data.insert(line_index, make<LineVisualData>());
  1496. recompute_all_visual_lines();
  1497. update();
  1498. }
  1499. void TextEditor::document_did_change()
  1500. {
  1501. did_change();
  1502. update();
  1503. }
  1504. void TextEditor::document_did_update_undo_stack()
  1505. {
  1506. auto make_action_text = [](auto prefix, auto suffix) {
  1507. StringBuilder builder;
  1508. builder.append(prefix);
  1509. if (suffix.has_value()) {
  1510. builder.append(' ');
  1511. builder.append(suffix.value());
  1512. }
  1513. return builder.to_string();
  1514. };
  1515. m_undo_action->set_enabled(can_undo());
  1516. m_redo_action->set_enabled(can_redo());
  1517. m_undo_action->set_text(make_action_text("&Undo", document().undo_stack().undo_action_text()));
  1518. m_redo_action->set_text(make_action_text("&Redo", document().undo_stack().redo_action_text()));
  1519. // FIXME: This is currently firing more often than it should.
  1520. // Ideally we'd only send this out when the undo stack modified state actually changes.
  1521. if (on_modified_change)
  1522. on_modified_change(document().is_modified());
  1523. }
  1524. void TextEditor::document_did_set_text()
  1525. {
  1526. m_line_visual_data.clear();
  1527. for (size_t i = 0; i < m_document->line_count(); ++i)
  1528. m_line_visual_data.append(make<LineVisualData>());
  1529. document_did_change();
  1530. }
  1531. void TextEditor::document_did_set_cursor(const TextPosition& position)
  1532. {
  1533. set_cursor(position);
  1534. }
  1535. void TextEditor::set_document(TextDocument& document)
  1536. {
  1537. if (m_document.ptr() == &document)
  1538. return;
  1539. if (m_document)
  1540. m_document->unregister_client(*this);
  1541. m_document = document;
  1542. m_line_visual_data.clear();
  1543. for (size_t i = 0; i < m_document->line_count(); ++i) {
  1544. m_line_visual_data.append(make<LineVisualData>());
  1545. }
  1546. set_cursor(0, 0);
  1547. if (has_selection())
  1548. m_selection.clear();
  1549. recompute_all_visual_lines();
  1550. update();
  1551. m_document->register_client(*this);
  1552. }
  1553. void TextEditor::flush_pending_change_notification_if_needed()
  1554. {
  1555. if (!m_has_pending_change_notification)
  1556. return;
  1557. if (on_change)
  1558. on_change();
  1559. if (m_highlighter)
  1560. m_highlighter->rehighlight(palette());
  1561. m_has_pending_change_notification = false;
  1562. }
  1563. const Syntax::Highlighter* TextEditor::syntax_highlighter() const
  1564. {
  1565. return m_highlighter.ptr();
  1566. }
  1567. void TextEditor::set_syntax_highlighter(OwnPtr<Syntax::Highlighter> highlighter)
  1568. {
  1569. if (m_highlighter)
  1570. m_highlighter->detach();
  1571. m_highlighter = move(highlighter);
  1572. if (m_highlighter) {
  1573. m_highlighter->attach(*this);
  1574. m_highlighter->rehighlight(palette());
  1575. } else
  1576. document().set_spans({});
  1577. }
  1578. const AutocompleteProvider* TextEditor::autocomplete_provider() const
  1579. {
  1580. return m_autocomplete_provider.ptr();
  1581. }
  1582. void TextEditor::set_autocomplete_provider(OwnPtr<AutocompleteProvider>&& provider)
  1583. {
  1584. if (m_autocomplete_provider)
  1585. m_autocomplete_provider->detach();
  1586. m_autocomplete_provider = move(provider);
  1587. if (m_autocomplete_provider) {
  1588. m_autocomplete_provider->attach(*this);
  1589. if (!m_autocomplete_box)
  1590. m_autocomplete_box = make<AutocompleteBox>(*this);
  1591. }
  1592. if (m_autocomplete_box)
  1593. m_autocomplete_box->close();
  1594. }
  1595. const EditingEngine* TextEditor::editing_engine() const
  1596. {
  1597. return m_editing_engine.ptr();
  1598. }
  1599. void TextEditor::set_editing_engine(OwnPtr<EditingEngine> editing_engine)
  1600. {
  1601. if (m_editing_engine)
  1602. m_editing_engine->detach();
  1603. m_editing_engine = move(editing_engine);
  1604. VERIFY(m_editing_engine);
  1605. m_editing_engine->attach(*this);
  1606. m_cursor_state = true;
  1607. update_cursor();
  1608. stop_timer();
  1609. start_timer(500);
  1610. }
  1611. int TextEditor::line_height() const
  1612. {
  1613. return font().glyph_height() + m_line_spacing;
  1614. }
  1615. int TextEditor::fixed_glyph_width() const
  1616. {
  1617. VERIFY(font().is_fixed_width());
  1618. return font().glyph_width(' ');
  1619. }
  1620. void TextEditor::set_icon(const Gfx::Bitmap* icon)
  1621. {
  1622. if (m_icon == icon)
  1623. return;
  1624. m_icon = icon;
  1625. update();
  1626. }
  1627. void TextEditor::set_visualize_trailing_whitespace(bool enabled)
  1628. {
  1629. if (m_visualize_trailing_whitespace == enabled)
  1630. return;
  1631. m_visualize_trailing_whitespace = enabled;
  1632. update();
  1633. }
  1634. void TextEditor::set_visualize_leading_whitespace(bool enabled)
  1635. {
  1636. if (m_visualize_leading_whitespace == enabled)
  1637. return;
  1638. m_visualize_leading_whitespace = enabled;
  1639. update();
  1640. }
  1641. void TextEditor::set_should_autocomplete_automatically(bool value)
  1642. {
  1643. if (value == should_autocomplete_automatically())
  1644. return;
  1645. if (value) {
  1646. VERIFY(m_autocomplete_provider);
  1647. m_autocomplete_timer = Core::Timer::create_single_shot(m_automatic_autocomplete_delay_ms, [this] { try_show_autocomplete(); });
  1648. return;
  1649. }
  1650. remove_child(*m_autocomplete_timer);
  1651. m_autocomplete_timer = nullptr;
  1652. }
  1653. int TextEditor::number_of_visible_lines() const
  1654. {
  1655. return visible_content_rect().height() / line_height();
  1656. }
  1657. void TextEditor::set_ruler_visible(bool visible)
  1658. {
  1659. if (m_ruler_visible == visible)
  1660. return;
  1661. m_ruler_visible = visible;
  1662. recompute_all_visual_lines();
  1663. update();
  1664. }
  1665. void TextEditor::set_gutter_visible(bool visible)
  1666. {
  1667. if (m_gutter_visible == visible)
  1668. return;
  1669. m_gutter_visible = visible;
  1670. recompute_all_visual_lines();
  1671. update();
  1672. }
  1673. void TextEditor::undo()
  1674. {
  1675. clear_selection();
  1676. document().undo();
  1677. }
  1678. void TextEditor::redo()
  1679. {
  1680. clear_selection();
  1681. document().redo();
  1682. }
  1683. }