GTextEditor.cpp 46 KB

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