GTextEditor.cpp 47 KB

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