TextEditor.cpp 64 KB

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