TextEditor.cpp 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851
  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.color;
  436. if (span.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.background_color;
  441. underline = span.is_underlined;
  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. if (m_autocomplete_box && m_autocomplete_box->is_visible() && (event.key() == KeyCode::Key_Return || event.key() == KeyCode::Key_Tab)) {
  618. m_autocomplete_box->apply_suggestion();
  619. m_autocomplete_box->close();
  620. return;
  621. }
  622. if (m_autocomplete_box && m_autocomplete_box->is_visible() && event.key() == KeyCode::Key_Escape) {
  623. m_autocomplete_box->close();
  624. return;
  625. }
  626. if (m_autocomplete_box && m_autocomplete_box->is_visible() && event.key() == KeyCode::Key_Up) {
  627. m_autocomplete_box->previous_suggestion();
  628. return;
  629. }
  630. if (m_autocomplete_box && m_autocomplete_box->is_visible() && event.key() == KeyCode::Key_Down) {
  631. m_autocomplete_box->next_suggestion();
  632. return;
  633. }
  634. if (is_single_line() && event.key() == KeyCode::Key_Tab)
  635. return ScrollableWidget::keydown_event(event);
  636. if (is_single_line() && event.key() == KeyCode::Key_Return) {
  637. if (on_return_pressed)
  638. on_return_pressed();
  639. return;
  640. }
  641. ArmedScopeGuard update_autocomplete { [&] {
  642. if (m_autocomplete_box && m_autocomplete_box->is_visible()) {
  643. m_autocomplete_provider->provide_completions([&](auto completions) {
  644. m_autocomplete_box->update_suggestions(move(completions));
  645. });
  646. }
  647. } };
  648. if (event.key() == KeyCode::Key_Escape) {
  649. if (on_escape_pressed)
  650. on_escape_pressed();
  651. return;
  652. }
  653. if (is_multi_line() && event.key() == KeyCode::Key_Up) {
  654. if (m_cursor.line() > 0) {
  655. if (event.ctrl() && event.shift()) {
  656. move_selected_lines_up();
  657. return;
  658. }
  659. size_t new_line = m_cursor.line() - 1;
  660. size_t new_column = min(m_cursor.column(), line(new_line).length());
  661. toggle_selection_if_needed_for_event(event);
  662. set_cursor(new_line, new_column);
  663. if (event.shift() && m_selection.start().is_valid()) {
  664. m_selection.set_end(m_cursor);
  665. did_update_selection();
  666. }
  667. }
  668. return;
  669. } else if (event.key() == KeyCode::Key_Up) {
  670. if (on_up_pressed)
  671. on_up_pressed();
  672. return;
  673. }
  674. if (is_multi_line() && event.key() == KeyCode::Key_Down) {
  675. if (m_cursor.line() < (line_count() - 1)) {
  676. if (event.ctrl() && event.shift()) {
  677. move_selected_lines_down();
  678. return;
  679. }
  680. size_t new_line = m_cursor.line() + 1;
  681. size_t new_column = min(m_cursor.column(), line(new_line).length());
  682. toggle_selection_if_needed_for_event(event);
  683. set_cursor(new_line, new_column);
  684. if (event.shift() && m_selection.start().is_valid()) {
  685. m_selection.set_end(m_cursor);
  686. did_update_selection();
  687. }
  688. }
  689. return;
  690. } else if (event.key() == KeyCode::Key_Down) {
  691. if (on_down_pressed)
  692. on_down_pressed();
  693. return;
  694. }
  695. if (is_multi_line() && event.key() == KeyCode::Key_PageUp) {
  696. if (m_cursor.line() > 0) {
  697. size_t page_step = (size_t)visible_content_rect().height() / (size_t)line_height();
  698. size_t new_line = m_cursor.line() < page_step ? 0 : m_cursor.line() - page_step;
  699. size_t new_column = min(m_cursor.column(), line(new_line).length());
  700. toggle_selection_if_needed_for_event(event);
  701. set_cursor(new_line, new_column);
  702. if (event.shift() && m_selection.start().is_valid()) {
  703. m_selection.set_end(m_cursor);
  704. did_update_selection();
  705. }
  706. }
  707. return;
  708. } else if (event.key() == KeyCode::Key_PageUp) {
  709. if (on_pageup_pressed)
  710. on_pageup_pressed();
  711. return;
  712. }
  713. if (is_multi_line() && event.key() == KeyCode::Key_PageDown) {
  714. if (m_cursor.line() < (line_count() - 1)) {
  715. int new_line = min(line_count() - 1, m_cursor.line() + visible_content_rect().height() / line_height());
  716. int new_column = min(m_cursor.column(), lines()[new_line].length());
  717. toggle_selection_if_needed_for_event(event);
  718. set_cursor(new_line, new_column);
  719. if (event.shift() && m_selection.start().is_valid()) {
  720. m_selection.set_end(m_cursor);
  721. did_update_selection();
  722. }
  723. }
  724. return;
  725. } else if (event.key() == KeyCode::Key_PageDown) {
  726. if (on_pagedown_pressed)
  727. on_pagedown_pressed();
  728. return;
  729. }
  730. if (event.key() == KeyCode::Key_Left) {
  731. if (!event.shift() && m_selection.is_valid()) {
  732. set_cursor(m_selection.normalized().start());
  733. m_selection.clear();
  734. did_update_selection();
  735. if (!event.ctrl()) {
  736. update();
  737. return;
  738. }
  739. }
  740. if (event.ctrl()) {
  741. TextPosition new_cursor;
  742. if (document().has_spans()) {
  743. auto span = document().first_non_skippable_span_before(m_cursor);
  744. if (span.has_value()) {
  745. new_cursor = span.value().range.start();
  746. } else {
  747. // No remaining spans, just use word break calculation
  748. new_cursor = document().first_word_break_before(m_cursor, true);
  749. }
  750. } else {
  751. new_cursor = document().first_word_break_before(m_cursor, true);
  752. }
  753. toggle_selection_if_needed_for_event(event);
  754. set_cursor(new_cursor);
  755. if (event.shift() && m_selection.start().is_valid()) {
  756. m_selection.set_end(m_cursor);
  757. did_update_selection();
  758. }
  759. return;
  760. }
  761. if (m_cursor.column() > 0) {
  762. int new_column = m_cursor.column() - 1;
  763. toggle_selection_if_needed_for_event(event);
  764. set_cursor(m_cursor.line(), new_column);
  765. if (event.shift() && m_selection.start().is_valid()) {
  766. m_selection.set_end(m_cursor);
  767. did_update_selection();
  768. }
  769. } else if (m_cursor.line() > 0) {
  770. int new_line = m_cursor.line() - 1;
  771. int new_column = lines()[new_line].length();
  772. toggle_selection_if_needed_for_event(event);
  773. set_cursor(new_line, new_column);
  774. if (event.shift() && m_selection.start().is_valid()) {
  775. m_selection.set_end(m_cursor);
  776. did_update_selection();
  777. }
  778. }
  779. return;
  780. }
  781. if (event.key() == KeyCode::Key_Right) {
  782. if (!event.shift() && m_selection.is_valid()) {
  783. set_cursor(m_selection.normalized().end());
  784. m_selection.clear();
  785. did_update_selection();
  786. if (!event.ctrl()) {
  787. update();
  788. return;
  789. }
  790. }
  791. if (event.ctrl()) {
  792. TextPosition new_cursor;
  793. if (document().has_spans()) {
  794. auto span = document().first_non_skippable_span_after(m_cursor);
  795. if (span.has_value()) {
  796. new_cursor = span.value().range.start();
  797. } else {
  798. // No remaining spans, just use word break calculation
  799. new_cursor = document().first_word_break_after(m_cursor);
  800. }
  801. } else {
  802. new_cursor = document().first_word_break_after(m_cursor);
  803. }
  804. toggle_selection_if_needed_for_event(event);
  805. set_cursor(new_cursor);
  806. if (event.shift() && m_selection.start().is_valid()) {
  807. m_selection.set_end(m_cursor);
  808. did_update_selection();
  809. }
  810. return;
  811. }
  812. int new_line = m_cursor.line();
  813. int new_column = m_cursor.column();
  814. if (m_cursor.column() < current_line().length()) {
  815. new_line = m_cursor.line();
  816. new_column = m_cursor.column() + 1;
  817. } else if (m_cursor.line() != line_count() - 1) {
  818. new_line = m_cursor.line() + 1;
  819. new_column = 0;
  820. }
  821. toggle_selection_if_needed_for_event(event);
  822. set_cursor(new_line, new_column);
  823. if (event.shift() && m_selection.start().is_valid()) {
  824. m_selection.set_end(m_cursor);
  825. did_update_selection();
  826. }
  827. return;
  828. }
  829. if (!event.ctrl() && event.key() == KeyCode::Key_Home) {
  830. size_t first_nonspace_column = current_line().first_non_whitespace_column();
  831. toggle_selection_if_needed_for_event(event);
  832. if (m_cursor.column() == first_nonspace_column)
  833. set_cursor(m_cursor.line(), 0);
  834. else
  835. set_cursor(m_cursor.line(), first_nonspace_column);
  836. if (event.shift() && m_selection.start().is_valid()) {
  837. m_selection.set_end(m_cursor);
  838. did_update_selection();
  839. }
  840. return;
  841. }
  842. if (!event.ctrl() && event.key() == KeyCode::Key_End) {
  843. toggle_selection_if_needed_for_event(event);
  844. set_cursor(m_cursor.line(), current_line().length());
  845. if (event.shift() && m_selection.start().is_valid()) {
  846. m_selection.set_end(m_cursor);
  847. did_update_selection();
  848. }
  849. return;
  850. }
  851. if (event.ctrl() && event.key() == KeyCode::Key_Home) {
  852. toggle_selection_if_needed_for_event(event);
  853. set_cursor(0, 0);
  854. if (event.shift() && m_selection.start().is_valid()) {
  855. m_selection.set_end(m_cursor);
  856. did_update_selection();
  857. }
  858. return;
  859. }
  860. if (event.ctrl() && event.key() == KeyCode::Key_End) {
  861. toggle_selection_if_needed_for_event(event);
  862. set_cursor(line_count() - 1, lines()[line_count() - 1].length());
  863. if (event.shift() && m_selection.start().is_valid()) {
  864. m_selection.set_end(m_cursor);
  865. did_update_selection();
  866. }
  867. return;
  868. }
  869. if (event.alt() && event.shift() && event.key() == KeyCode::Key_S) {
  870. sort_selected_lines();
  871. return;
  872. }
  873. if (event.key() == KeyCode::Key_Backspace) {
  874. if (!is_editable())
  875. return;
  876. if (has_selection()) {
  877. delete_selection();
  878. did_update_selection();
  879. return;
  880. }
  881. if (m_cursor.column() > 0) {
  882. int erase_count = 1;
  883. if (event.modifiers() == Mod_Ctrl) {
  884. auto word_break_pos = document().first_word_break_before(m_cursor, true);
  885. erase_count = m_cursor.column() - word_break_pos.column();
  886. } else if (current_line().first_non_whitespace_column() >= m_cursor.column()) {
  887. int new_column;
  888. if (m_cursor.column() % m_soft_tab_width == 0)
  889. new_column = m_cursor.column() - m_soft_tab_width;
  890. else
  891. new_column = (m_cursor.column() / m_soft_tab_width) * m_soft_tab_width;
  892. erase_count = m_cursor.column() - new_column;
  893. }
  894. // Backspace within line
  895. TextRange erased_range({ m_cursor.line(), m_cursor.column() - erase_count }, m_cursor);
  896. auto erased_text = document().text_in_range(erased_range);
  897. execute<RemoveTextCommand>(erased_text, erased_range);
  898. return;
  899. }
  900. if (m_cursor.column() == 0 && m_cursor.line() != 0) {
  901. // Backspace at column 0; merge with previous line
  902. size_t previous_length = line(m_cursor.line() - 1).length();
  903. TextRange erased_range({ m_cursor.line() - 1, previous_length }, m_cursor);
  904. execute<RemoveTextCommand>("\n", erased_range);
  905. return;
  906. }
  907. return;
  908. }
  909. if (event.modifiers() == Mod_Shift && event.key() == KeyCode::Key_Delete) {
  910. if (!is_editable())
  911. return;
  912. delete_current_line();
  913. return;
  914. }
  915. if (event.key() == KeyCode::Key_Delete) {
  916. if (!is_editable())
  917. return;
  918. do_delete();
  919. return;
  920. }
  921. if (!event.shift() && !event.alt() && event.ctrl() && event.key() == KeyCode::Key_Space) {
  922. if (m_autocomplete_provider) {
  923. m_autocomplete_provider->provide_completions([&](auto completions) {
  924. m_autocomplete_box->update_suggestions(move(completions));
  925. auto position = content_rect_for_position(cursor()).bottom_right().translated(screen_relative_rect().top_left().translated(ruler_width(), 0).translated(10, 5));
  926. m_autocomplete_box->show(position);
  927. });
  928. update_autocomplete.disarm();
  929. return;
  930. }
  931. }
  932. if (is_editable() && !event.ctrl() && !event.alt() && event.code_point() != 0) {
  933. StringBuilder sb;
  934. sb.append_code_point(event.code_point());
  935. insert_at_cursor_or_replace_selection(sb.to_string());
  936. return;
  937. }
  938. event.ignore();
  939. }
  940. void TextEditor::delete_current_line()
  941. {
  942. if (has_selection())
  943. return delete_selection();
  944. TextPosition start;
  945. TextPosition end;
  946. if (m_cursor.line() == 0 && line_count() == 1) {
  947. start = { 0, 0 };
  948. end = { 0, line(0).length() };
  949. } else if (m_cursor.line() == line_count() - 1) {
  950. start = { m_cursor.line() - 1, line(m_cursor.line()).length() };
  951. end = { m_cursor.line(), line(m_cursor.line()).length() };
  952. } else {
  953. start = { m_cursor.line(), 0 };
  954. end = { m_cursor.line() + 1, 0 };
  955. }
  956. TextRange erased_range(start, end);
  957. execute<RemoveTextCommand>(document().text_in_range(erased_range), erased_range);
  958. }
  959. void TextEditor::do_delete()
  960. {
  961. if (!is_editable())
  962. return;
  963. if (has_selection())
  964. return delete_selection();
  965. if (m_cursor.column() < current_line().length()) {
  966. // Delete within line
  967. TextRange erased_range(m_cursor, { m_cursor.line(), m_cursor.column() + 1 });
  968. execute<RemoveTextCommand>(document().text_in_range(erased_range), erased_range);
  969. return;
  970. }
  971. if (m_cursor.column() == current_line().length() && m_cursor.line() != line_count() - 1) {
  972. // Delete at end of line; merge with next line
  973. TextRange erased_range(m_cursor, { m_cursor.line() + 1, 0 });
  974. execute<RemoveTextCommand>(document().text_in_range(erased_range), erased_range);
  975. return;
  976. }
  977. }
  978. int TextEditor::content_x_for_position(const TextPosition& position) const
  979. {
  980. auto& line = this->line(position.line());
  981. int x_offset = 0;
  982. switch (m_text_alignment) {
  983. case Gfx::TextAlignment::CenterLeft:
  984. 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) {
  985. size_t offset_in_visual_line = position.column() - start_of_visual_line;
  986. if (position.column() >= start_of_visual_line && (offset_in_visual_line <= visual_line_view.length())) {
  987. if (offset_in_visual_line == 0) {
  988. x_offset = 0;
  989. } else {
  990. x_offset = font().width(visual_line_view.substring_view(0, offset_in_visual_line));
  991. x_offset += font().glyph_spacing();
  992. }
  993. return IterationDecision::Break;
  994. }
  995. return IterationDecision::Continue;
  996. });
  997. return m_horizontal_content_padding + ((is_single_line() && icon()) ? (icon_size() + icon_padding()) : 0) + x_offset;
  998. case Gfx::TextAlignment::CenterRight:
  999. // FIXME
  1000. ASSERT(!is_line_wrapping_enabled());
  1001. return content_width() - m_horizontal_content_padding - (line.length() * fixed_glyph_width()) + (position.column() * fixed_glyph_width());
  1002. default:
  1003. ASSERT_NOT_REACHED();
  1004. }
  1005. }
  1006. Gfx::IntRect TextEditor::content_rect_for_position(const TextPosition& position) const
  1007. {
  1008. if (!position.is_valid())
  1009. return {};
  1010. ASSERT(!lines().is_empty());
  1011. ASSERT(position.column() <= (current_line().length() + 1));
  1012. int x = content_x_for_position(position);
  1013. if (is_single_line()) {
  1014. Gfx::IntRect rect { x, 0, 1, line_height() };
  1015. rect.center_vertically_within({ {}, frame_inner_rect().size() });
  1016. return rect;
  1017. }
  1018. Gfx::IntRect rect;
  1019. 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) {
  1020. if (position.column() >= start_of_visual_line && ((position.column() - start_of_visual_line) <= view.length())) {
  1021. // NOTE: We have to subtract the horizontal padding here since it's part of the visual line rect
  1022. // *and* included in what we get from content_x_for_position().
  1023. rect = {
  1024. visual_line_rect.x() + x - (m_horizontal_content_padding),
  1025. visual_line_rect.y(),
  1026. 1,
  1027. line_height()
  1028. };
  1029. return IterationDecision::Break;
  1030. }
  1031. return IterationDecision::Continue;
  1032. });
  1033. return rect;
  1034. }
  1035. Gfx::IntRect TextEditor::cursor_content_rect() const
  1036. {
  1037. return content_rect_for_position(m_cursor);
  1038. }
  1039. Gfx::IntRect TextEditor::line_widget_rect(size_t line_index) const
  1040. {
  1041. auto rect = line_content_rect(line_index);
  1042. rect.set_x(frame_thickness());
  1043. rect.set_width(frame_inner_rect().width());
  1044. rect.move_by(0, -(vertical_scrollbar().value()));
  1045. rect.move_by(0, frame_thickness());
  1046. rect.intersect(frame_inner_rect());
  1047. return rect;
  1048. }
  1049. void TextEditor::scroll_position_into_view(const TextPosition& position)
  1050. {
  1051. auto rect = content_rect_for_position(position);
  1052. if (position.column() == 0)
  1053. rect.set_x(content_x_for_position({ position.line(), 0 }) - 2);
  1054. else if (position.column() == line(position.line()).length())
  1055. rect.set_x(content_x_for_position({ position.line(), line(position.line()).length() }) + 2);
  1056. scroll_into_view(rect, true, true);
  1057. }
  1058. void TextEditor::scroll_cursor_into_view()
  1059. {
  1060. if (!m_reflow_deferred)
  1061. scroll_position_into_view(m_cursor);
  1062. }
  1063. Gfx::IntRect TextEditor::line_content_rect(size_t line_index) const
  1064. {
  1065. auto& line = this->line(line_index);
  1066. if (is_single_line()) {
  1067. Gfx::IntRect line_rect = { content_x_for_position({ line_index, 0 }), 0, font().width(line.view()), font().glyph_height() + 4 };
  1068. line_rect.center_vertically_within({ {}, frame_inner_rect().size() });
  1069. return line_rect;
  1070. }
  1071. if (is_line_wrapping_enabled())
  1072. return m_line_visual_data[line_index].visual_rect;
  1073. return {
  1074. content_x_for_position({ line_index, 0 }),
  1075. (int)line_index * line_height(),
  1076. font().width(line.view()),
  1077. line_height()
  1078. };
  1079. }
  1080. void TextEditor::set_cursor_and_focus_line(size_t line, size_t column)
  1081. {
  1082. u_int index_max = line_count() - 1;
  1083. set_cursor(line, column);
  1084. if (line > 1 && line < index_max) {
  1085. int headroom = frame_inner_rect().height() / 3;
  1086. do {
  1087. auto line_data = m_line_visual_data[line];
  1088. headroom -= line_data.visual_rect.height();
  1089. line--;
  1090. } while (line > 0 && headroom > 0);
  1091. Gfx::IntRect rect = { 0, line_content_rect(line).y(),
  1092. 1, frame_inner_rect().height() };
  1093. scroll_into_view(rect, false, true);
  1094. }
  1095. }
  1096. void TextEditor::update_cursor()
  1097. {
  1098. update(line_widget_rect(m_cursor.line()));
  1099. }
  1100. void TextEditor::set_cursor(size_t line, size_t column)
  1101. {
  1102. set_cursor({ line, column });
  1103. }
  1104. void TextEditor::set_cursor(const TextPosition& a_position)
  1105. {
  1106. ASSERT(!lines().is_empty());
  1107. TextPosition position = a_position;
  1108. if (position.line() >= line_count())
  1109. position.set_line(line_count() - 1);
  1110. if (position.column() > lines()[position.line()].length())
  1111. position.set_column(lines()[position.line()].length());
  1112. if (m_cursor != position && is_visual_data_up_to_date()) {
  1113. // NOTE: If the old cursor is no longer valid, repaint everything just in case.
  1114. auto old_cursor_line_rect = m_cursor.line() < line_count()
  1115. ? line_widget_rect(m_cursor.line())
  1116. : rect();
  1117. m_cursor = position;
  1118. m_cursor_state = true;
  1119. scroll_cursor_into_view();
  1120. update(old_cursor_line_rect);
  1121. update_cursor();
  1122. } else if (m_cursor != position) {
  1123. m_cursor = position;
  1124. m_cursor_state = true;
  1125. }
  1126. cursor_did_change();
  1127. if (on_cursor_change)
  1128. on_cursor_change();
  1129. if (m_highlighter)
  1130. m_highlighter->cursor_did_change();
  1131. }
  1132. void TextEditor::focusin_event(FocusEvent& event)
  1133. {
  1134. if (event.source() == FocusSource::Keyboard)
  1135. select_all();
  1136. m_cursor_state = true;
  1137. update_cursor();
  1138. start_timer(500);
  1139. if (on_focusin)
  1140. on_focusin();
  1141. }
  1142. void TextEditor::focusout_event(FocusEvent&)
  1143. {
  1144. stop_timer();
  1145. if (on_focusout)
  1146. on_focusout();
  1147. }
  1148. void TextEditor::timer_event(Core::TimerEvent&)
  1149. {
  1150. m_cursor_state = !m_cursor_state;
  1151. if (is_focused())
  1152. update_cursor();
  1153. }
  1154. bool TextEditor::write_to_file(const StringView& path)
  1155. {
  1156. int fd = open_with_path_length(path.characters_without_null_termination(), path.length(), O_WRONLY | O_CREAT | O_TRUNC, 0666);
  1157. if (fd < 0) {
  1158. perror("open");
  1159. return false;
  1160. }
  1161. // Compute the final file size and ftruncate() to make writing fast.
  1162. // FIXME: Remove this once the kernel is smart enough to do this instead.
  1163. off_t file_size = 0;
  1164. for (size_t i = 0; i < line_count(); ++i)
  1165. file_size += line(i).length();
  1166. file_size += line_count() - 1;
  1167. int rc = ftruncate(fd, file_size);
  1168. if (rc < 0) {
  1169. perror("ftruncate");
  1170. return false;
  1171. }
  1172. for (size_t i = 0; i < line_count(); ++i) {
  1173. auto& line = this->line(i);
  1174. if (line.length()) {
  1175. auto line_as_utf8 = line.to_utf8();
  1176. ssize_t nwritten = write(fd, line_as_utf8.characters(), line_as_utf8.length());
  1177. if (nwritten < 0) {
  1178. perror("write");
  1179. close(fd);
  1180. return false;
  1181. }
  1182. }
  1183. if (i != line_count() - 1) {
  1184. char ch = '\n';
  1185. ssize_t nwritten = write(fd, &ch, 1);
  1186. if (nwritten != 1) {
  1187. perror("write");
  1188. close(fd);
  1189. return false;
  1190. }
  1191. }
  1192. }
  1193. close(fd);
  1194. return true;
  1195. }
  1196. String TextEditor::text() const
  1197. {
  1198. return document().text();
  1199. }
  1200. void TextEditor::clear()
  1201. {
  1202. document().remove_all_lines();
  1203. document().append_line(make<TextDocumentLine>(document()));
  1204. m_selection.clear();
  1205. did_update_selection();
  1206. set_cursor(0, 0);
  1207. update();
  1208. }
  1209. String TextEditor::selected_text() const
  1210. {
  1211. if (!has_selection())
  1212. return {};
  1213. return document().text_in_range(m_selection);
  1214. }
  1215. void TextEditor::delete_selection()
  1216. {
  1217. auto selection = normalized_selection();
  1218. execute<RemoveTextCommand>(selected_text(), selection);
  1219. m_selection.clear();
  1220. did_update_selection();
  1221. did_change();
  1222. set_cursor(selection.start());
  1223. update();
  1224. }
  1225. void TextEditor::insert_at_cursor_or_replace_selection(const StringView& text)
  1226. {
  1227. ReflowDeferrer defer(*this);
  1228. ASSERT(is_editable());
  1229. if (has_selection())
  1230. delete_selection();
  1231. execute<InsertTextCommand>(text, m_cursor);
  1232. }
  1233. void TextEditor::cut()
  1234. {
  1235. if (!is_editable())
  1236. return;
  1237. auto selected_text = this->selected_text();
  1238. printf("Cut: \"%s\"\n", selected_text.characters());
  1239. Clipboard::the().set_plain_text(selected_text);
  1240. delete_selection();
  1241. }
  1242. void TextEditor::copy()
  1243. {
  1244. auto selected_text = this->selected_text();
  1245. printf("Copy: \"%s\"\n", selected_text.characters());
  1246. Clipboard::the().set_plain_text(selected_text);
  1247. }
  1248. void TextEditor::paste()
  1249. {
  1250. if (!is_editable())
  1251. return;
  1252. auto paste_text = Clipboard::the().data();
  1253. printf("Paste: \"%s\"\n", String::copy(paste_text).characters());
  1254. TemporaryChange change(m_automatic_indentation_enabled, false);
  1255. insert_at_cursor_or_replace_selection(paste_text);
  1256. }
  1257. void TextEditor::defer_reflow()
  1258. {
  1259. ++m_reflow_deferred;
  1260. }
  1261. void TextEditor::undefer_reflow()
  1262. {
  1263. ASSERT(m_reflow_deferred);
  1264. if (!--m_reflow_deferred) {
  1265. if (m_reflow_requested) {
  1266. recompute_all_visual_lines();
  1267. scroll_cursor_into_view();
  1268. }
  1269. }
  1270. }
  1271. void TextEditor::enter_event(Core::Event&)
  1272. {
  1273. m_automatic_selection_scroll_timer->stop();
  1274. }
  1275. void TextEditor::leave_event(Core::Event&)
  1276. {
  1277. if (m_in_drag_select)
  1278. m_automatic_selection_scroll_timer->start();
  1279. }
  1280. void TextEditor::did_change()
  1281. {
  1282. update_content_size();
  1283. recompute_all_visual_lines();
  1284. m_undo_action->set_enabled(can_undo());
  1285. m_redo_action->set_enabled(can_redo());
  1286. if (!m_has_pending_change_notification) {
  1287. m_has_pending_change_notification = true;
  1288. deferred_invoke([this](auto&) {
  1289. if (!m_has_pending_change_notification)
  1290. return;
  1291. if (on_change)
  1292. on_change();
  1293. if (m_highlighter)
  1294. m_highlighter->rehighlight(palette());
  1295. m_has_pending_change_notification = false;
  1296. });
  1297. }
  1298. }
  1299. void TextEditor::set_mode(const Mode mode)
  1300. {
  1301. if (m_mode == mode)
  1302. return;
  1303. m_mode = mode;
  1304. switch (mode) {
  1305. case Editable:
  1306. m_cut_action->set_enabled(true && has_selection());
  1307. m_delete_action->set_enabled(true);
  1308. m_paste_action->set_enabled(true);
  1309. set_accepts_emoji_input(true);
  1310. break;
  1311. case DisplayOnly:
  1312. case ReadOnly:
  1313. m_cut_action->set_enabled(false && has_selection());
  1314. m_delete_action->set_enabled(false);
  1315. m_paste_action->set_enabled(false);
  1316. set_accepts_emoji_input(false);
  1317. break;
  1318. default:
  1319. ASSERT_NOT_REACHED();
  1320. }
  1321. if (!is_displayonly())
  1322. set_override_cursor(Gfx::StandardCursor::IBeam);
  1323. else
  1324. set_override_cursor(Gfx::StandardCursor::None);
  1325. }
  1326. void TextEditor::set_has_visible_list(bool visible)
  1327. {
  1328. if (m_has_visible_list == visible)
  1329. return;
  1330. m_has_visible_list = visible;
  1331. }
  1332. void TextEditor::did_update_selection()
  1333. {
  1334. m_cut_action->set_enabled(is_editable() && has_selection());
  1335. m_copy_action->set_enabled(has_selection());
  1336. if (on_selection_change)
  1337. on_selection_change();
  1338. if (is_line_wrapping_enabled()) {
  1339. // FIXME: Try to repaint less.
  1340. update();
  1341. }
  1342. }
  1343. void TextEditor::context_menu_event(ContextMenuEvent& event)
  1344. {
  1345. if (is_displayonly())
  1346. return;
  1347. if (!m_context_menu) {
  1348. m_context_menu = Menu::construct();
  1349. m_context_menu->add_action(undo_action());
  1350. m_context_menu->add_action(redo_action());
  1351. m_context_menu->add_separator();
  1352. m_context_menu->add_action(cut_action());
  1353. m_context_menu->add_action(copy_action());
  1354. m_context_menu->add_action(paste_action());
  1355. m_context_menu->add_action(delete_action());
  1356. m_context_menu->add_separator();
  1357. m_context_menu->add_action(select_all_action());
  1358. if (is_multi_line()) {
  1359. m_context_menu->add_separator();
  1360. m_context_menu->add_action(go_to_line_action());
  1361. }
  1362. if (!m_custom_context_menu_actions.is_empty()) {
  1363. m_context_menu->add_separator();
  1364. for (auto& action : m_custom_context_menu_actions) {
  1365. m_context_menu->add_action(action);
  1366. }
  1367. }
  1368. }
  1369. m_context_menu->popup(event.screen_position());
  1370. }
  1371. void TextEditor::set_text_alignment(Gfx::TextAlignment alignment)
  1372. {
  1373. if (m_text_alignment == alignment)
  1374. return;
  1375. m_text_alignment = alignment;
  1376. update();
  1377. }
  1378. void TextEditor::resize_event(ResizeEvent& event)
  1379. {
  1380. ScrollableWidget::resize_event(event);
  1381. update_content_size();
  1382. recompute_all_visual_lines();
  1383. }
  1384. void TextEditor::theme_change_event(ThemeChangeEvent& event)
  1385. {
  1386. ScrollableWidget::theme_change_event(event);
  1387. if (m_highlighter)
  1388. m_highlighter->rehighlight(palette());
  1389. }
  1390. void TextEditor::set_selection(const TextRange& selection)
  1391. {
  1392. if (m_selection == selection)
  1393. return;
  1394. m_selection = selection;
  1395. set_cursor(m_selection.end());
  1396. scroll_position_into_view(normalized_selection().start());
  1397. update();
  1398. }
  1399. void TextEditor::clear_selection()
  1400. {
  1401. if (!has_selection())
  1402. return;
  1403. m_selection.clear();
  1404. update();
  1405. }
  1406. void TextEditor::recompute_all_visual_lines()
  1407. {
  1408. if (m_reflow_deferred) {
  1409. m_reflow_requested = true;
  1410. return;
  1411. }
  1412. m_reflow_requested = false;
  1413. int y_offset = 0;
  1414. for (size_t line_index = 0; line_index < line_count(); ++line_index) {
  1415. recompute_visual_lines(line_index);
  1416. m_line_visual_data[line_index].visual_rect.set_y(y_offset);
  1417. y_offset += m_line_visual_data[line_index].visual_rect.height();
  1418. }
  1419. update_content_size();
  1420. }
  1421. void TextEditor::ensure_cursor_is_valid()
  1422. {
  1423. auto new_cursor = m_cursor;
  1424. if (new_cursor.line() >= line_count())
  1425. new_cursor.set_line(line_count() - 1);
  1426. if (new_cursor.column() > line(new_cursor.line()).length())
  1427. new_cursor.set_column(line(new_cursor.line()).length());
  1428. if (m_cursor != new_cursor)
  1429. set_cursor(new_cursor);
  1430. }
  1431. size_t TextEditor::visual_line_containing(size_t line_index, size_t column) const
  1432. {
  1433. size_t visual_line_index = 0;
  1434. for_each_visual_line(line_index, [&](const Gfx::IntRect&, auto& view, size_t start_of_visual_line, [[maybe_unused]] bool is_last_visual_line) {
  1435. if (column >= start_of_visual_line && ((column - start_of_visual_line) < view.length()))
  1436. return IterationDecision::Break;
  1437. ++visual_line_index;
  1438. return IterationDecision::Continue;
  1439. });
  1440. return visual_line_index;
  1441. }
  1442. void TextEditor::recompute_visual_lines(size_t line_index)
  1443. {
  1444. auto& line = document().line(line_index);
  1445. auto& visual_data = m_line_visual_data[line_index];
  1446. visual_data.visual_line_breaks.clear_with_capacity();
  1447. int available_width = visible_text_rect_in_inner_coordinates().width();
  1448. if (is_line_wrapping_enabled()) {
  1449. int line_width_so_far = 0;
  1450. auto glyph_spacing = font().glyph_spacing();
  1451. for (size_t i = 0; i < line.length(); ++i) {
  1452. auto code_point = line.code_points()[i];
  1453. auto glyph_width = font().glyph_or_emoji_width(code_point);
  1454. if ((line_width_so_far + glyph_width + glyph_spacing) > available_width) {
  1455. visual_data.visual_line_breaks.append(i);
  1456. line_width_so_far = glyph_width + glyph_spacing;
  1457. continue;
  1458. }
  1459. line_width_so_far += glyph_width + glyph_spacing;
  1460. }
  1461. }
  1462. visual_data.visual_line_breaks.append(line.length());
  1463. if (is_line_wrapping_enabled())
  1464. visual_data.visual_rect = { m_horizontal_content_padding, 0, available_width, static_cast<int>(visual_data.visual_line_breaks.size()) * line_height() };
  1465. else
  1466. visual_data.visual_rect = { m_horizontal_content_padding, 0, font().width(line.view()), line_height() };
  1467. }
  1468. template<typename Callback>
  1469. void TextEditor::for_each_visual_line(size_t line_index, Callback callback) const
  1470. {
  1471. auto editor_visible_text_rect = visible_text_rect_in_inner_coordinates();
  1472. size_t start_of_line = 0;
  1473. size_t visual_line_index = 0;
  1474. auto& line = document().line(line_index);
  1475. auto& visual_data = m_line_visual_data[line_index];
  1476. for (auto visual_line_break : visual_data.visual_line_breaks) {
  1477. auto visual_line_view = Utf32View(line.code_points() + start_of_line, visual_line_break - start_of_line);
  1478. Gfx::IntRect visual_line_rect {
  1479. visual_data.visual_rect.x(),
  1480. visual_data.visual_rect.y() + ((int)visual_line_index * line_height()),
  1481. font().width(visual_line_view) + font().glyph_spacing(),
  1482. line_height()
  1483. };
  1484. if (is_right_text_alignment(text_alignment()))
  1485. visual_line_rect.set_right_without_resize(editor_visible_text_rect.right());
  1486. if (is_single_line()) {
  1487. visual_line_rect.center_vertically_within(editor_visible_text_rect);
  1488. if (m_icon)
  1489. visual_line_rect.move_by(icon_size() + icon_padding(), 0);
  1490. }
  1491. if (callback(visual_line_rect, visual_line_view, start_of_line, visual_line_index == visual_data.visual_line_breaks.size() - 1) == IterationDecision::Break)
  1492. break;
  1493. start_of_line = visual_line_break;
  1494. ++visual_line_index;
  1495. }
  1496. }
  1497. void TextEditor::set_line_wrapping_enabled(bool enabled)
  1498. {
  1499. if (m_line_wrapping_enabled == enabled)
  1500. return;
  1501. m_line_wrapping_enabled = enabled;
  1502. horizontal_scrollbar().set_visible(!m_line_wrapping_enabled);
  1503. update_content_size();
  1504. recompute_all_visual_lines();
  1505. update();
  1506. }
  1507. void TextEditor::add_custom_context_menu_action(Action& action)
  1508. {
  1509. m_custom_context_menu_actions.append(action);
  1510. }
  1511. void TextEditor::did_change_font()
  1512. {
  1513. vertical_scrollbar().set_step(line_height());
  1514. recompute_all_visual_lines();
  1515. update();
  1516. ScrollableWidget::did_change_font();
  1517. }
  1518. void TextEditor::document_did_append_line()
  1519. {
  1520. m_line_visual_data.append(make<LineVisualData>());
  1521. recompute_all_visual_lines();
  1522. update();
  1523. }
  1524. void TextEditor::document_did_remove_line(size_t line_index)
  1525. {
  1526. m_line_visual_data.remove(line_index);
  1527. recompute_all_visual_lines();
  1528. update();
  1529. }
  1530. void TextEditor::document_did_remove_all_lines()
  1531. {
  1532. m_line_visual_data.clear();
  1533. recompute_all_visual_lines();
  1534. update();
  1535. }
  1536. void TextEditor::document_did_insert_line(size_t line_index)
  1537. {
  1538. m_line_visual_data.insert(line_index, make<LineVisualData>());
  1539. recompute_all_visual_lines();
  1540. update();
  1541. }
  1542. void TextEditor::document_did_change()
  1543. {
  1544. did_change();
  1545. update();
  1546. }
  1547. void TextEditor::document_did_set_text()
  1548. {
  1549. m_line_visual_data.clear();
  1550. for (size_t i = 0; i < m_document->line_count(); ++i)
  1551. m_line_visual_data.append(make<LineVisualData>());
  1552. document_did_change();
  1553. }
  1554. void TextEditor::document_did_set_cursor(const TextPosition& position)
  1555. {
  1556. set_cursor(position);
  1557. }
  1558. void TextEditor::set_document(TextDocument& document)
  1559. {
  1560. if (m_document.ptr() == &document)
  1561. return;
  1562. if (m_document)
  1563. m_document->unregister_client(*this);
  1564. m_document = document;
  1565. m_line_visual_data.clear();
  1566. for (size_t i = 0; i < m_document->line_count(); ++i) {
  1567. m_line_visual_data.append(make<LineVisualData>());
  1568. }
  1569. set_cursor(0, 0);
  1570. if (has_selection())
  1571. m_selection.clear();
  1572. recompute_all_visual_lines();
  1573. update();
  1574. m_document->register_client(*this);
  1575. }
  1576. void TextEditor::flush_pending_change_notification_if_needed()
  1577. {
  1578. if (!m_has_pending_change_notification)
  1579. return;
  1580. if (on_change)
  1581. on_change();
  1582. if (m_highlighter)
  1583. m_highlighter->rehighlight(palette());
  1584. m_has_pending_change_notification = false;
  1585. }
  1586. const SyntaxHighlighter* TextEditor::syntax_highlighter() const
  1587. {
  1588. return m_highlighter.ptr();
  1589. }
  1590. void TextEditor::set_syntax_highlighter(OwnPtr<SyntaxHighlighter> highlighter)
  1591. {
  1592. if (m_highlighter)
  1593. m_highlighter->detach();
  1594. m_highlighter = move(highlighter);
  1595. if (m_highlighter) {
  1596. m_highlighter->attach(*this);
  1597. m_highlighter->rehighlight(palette());
  1598. } else
  1599. document().set_spans({});
  1600. }
  1601. const AutocompleteProvider* TextEditor::autocomplete_provider() const
  1602. {
  1603. return m_autocomplete_provider.ptr();
  1604. }
  1605. void TextEditor::set_autocomplete_provider(OwnPtr<AutocompleteProvider>&& provider)
  1606. {
  1607. if (m_autocomplete_provider)
  1608. m_autocomplete_provider->detach();
  1609. m_autocomplete_provider = move(provider);
  1610. if (m_autocomplete_provider) {
  1611. m_autocomplete_provider->attach(*this);
  1612. if (!m_autocomplete_box)
  1613. m_autocomplete_box = make<AutocompleteBox>(*this);
  1614. }
  1615. if (m_autocomplete_box)
  1616. m_autocomplete_box->close();
  1617. }
  1618. int TextEditor::line_height() const
  1619. {
  1620. return font().glyph_height() + m_line_spacing;
  1621. }
  1622. int TextEditor::fixed_glyph_width() const
  1623. {
  1624. ASSERT(font().is_fixed_width());
  1625. return font().glyph_width(' ');
  1626. }
  1627. void TextEditor::set_icon(const Gfx::Bitmap* icon)
  1628. {
  1629. if (m_icon == icon)
  1630. return;
  1631. m_icon = icon;
  1632. update();
  1633. }
  1634. void TextEditor::set_visualize_trailing_whitespace(bool enabled)
  1635. {
  1636. if (m_visualize_trailing_whitespace == enabled)
  1637. return;
  1638. m_visualize_trailing_whitespace = enabled;
  1639. update();
  1640. }
  1641. }