GTextEditor.cpp 48 KB

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