GTextEditor.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. #include <LibGUI/GTextEditor.h>
  2. #include <LibGUI/GScrollBar.h>
  3. #include <LibGUI/GFontDatabase.h>
  4. #include <LibGUI/GClipboard.h>
  5. #include <SharedGraphics/Painter.h>
  6. #include <Kernel/KeyCode.h>
  7. #include <AK/StringBuilder.h>
  8. #include <unistd.h>
  9. #include <fcntl.h>
  10. #include <stdio.h>
  11. GTextEditor::GTextEditor(GWidget* parent)
  12. : GWidget(parent)
  13. {
  14. set_font(GFontDatabase::the().get_by_name("Csilla Thin"));
  15. set_fill_with_background_color(false);
  16. m_vertical_scrollbar = new GScrollBar(Orientation::Vertical, this);
  17. m_vertical_scrollbar->set_step(4);
  18. m_vertical_scrollbar->on_change = [this] (int) {
  19. update();
  20. };
  21. m_horizontal_scrollbar = new GScrollBar(Orientation::Horizontal, this);
  22. m_horizontal_scrollbar->set_step(4);
  23. m_horizontal_scrollbar->set_big_step(30);
  24. m_horizontal_scrollbar->on_change = [this] (int) {
  25. update();
  26. };
  27. m_corner_widget = new GWidget(this);
  28. m_lines.append(make<Line>());
  29. }
  30. GTextEditor::~GTextEditor()
  31. {
  32. }
  33. void GTextEditor::set_text(const String& text)
  34. {
  35. m_lines.clear();
  36. int start_of_current_line = 0;
  37. auto add_line = [&] (int current_position) {
  38. int line_length = current_position - start_of_current_line;
  39. auto line = make<Line>();
  40. if (line_length)
  41. line->set_text(text.substring(start_of_current_line, current_position - start_of_current_line));
  42. m_lines.append(move(line));
  43. start_of_current_line = current_position + 1;
  44. };
  45. int i = 0;
  46. for (i = 0; i < text.length(); ++i) {
  47. if (text[i] == '\n')
  48. add_line(i);
  49. }
  50. add_line(i);
  51. set_cursor(0, 0);
  52. update();
  53. }
  54. void GTextEditor::resize_event(GResizeEvent& event)
  55. {
  56. update_scrollbar_ranges();
  57. m_vertical_scrollbar->set_relative_rect(event.size().width() - m_vertical_scrollbar->preferred_size().width(), 0, m_vertical_scrollbar->preferred_size().width(), event.size().height() - m_horizontal_scrollbar->preferred_size().height());
  58. m_horizontal_scrollbar->set_relative_rect(0, event.size().height() - m_horizontal_scrollbar->preferred_size().height(), event.size().width() - m_vertical_scrollbar->preferred_size().width(), m_horizontal_scrollbar->preferred_size().height());
  59. m_corner_widget->set_relative_rect(m_horizontal_scrollbar->rect().right() + 1, m_vertical_scrollbar->rect().bottom() + 1, m_horizontal_scrollbar->height(), m_vertical_scrollbar->width());
  60. }
  61. void GTextEditor::update_scrollbar_ranges()
  62. {
  63. int available_height = height() - m_horizontal_scrollbar->height();
  64. int excess_height = max(0, (content_height() + padding() * 2) - available_height);
  65. m_vertical_scrollbar->set_range(0, excess_height);
  66. int available_width = width() - m_vertical_scrollbar->width() - ruler_width();
  67. int excess_width = max(0, (content_width() + padding() * 2) - available_width);
  68. m_horizontal_scrollbar->set_range(0, excess_width);
  69. m_vertical_scrollbar->set_big_step(visible_content_rect().height());
  70. }
  71. int GTextEditor::content_height() const
  72. {
  73. return line_count() * line_height();
  74. }
  75. int GTextEditor::content_width() const
  76. {
  77. // FIXME: Cache this somewhere.
  78. int max_width = 0;
  79. for (auto& line : m_lines)
  80. max_width = max(line->width(font()), max_width);
  81. return max_width;
  82. }
  83. GTextPosition GTextEditor::text_position_at(const Point& a_position) const
  84. {
  85. auto position = a_position;
  86. position.move_by(m_horizontal_scrollbar->value(), m_vertical_scrollbar->value());
  87. position.move_by(-(padding() + ruler_width()), -padding());
  88. int line_index = position.y() / line_height();
  89. int column_index = position.x() / glyph_width();
  90. line_index = max(0, min(line_index, line_count() - 1));
  91. column_index = max(0, min(column_index, m_lines[line_index]->length()));
  92. return { line_index, column_index };
  93. }
  94. void GTextEditor::mousedown_event(GMouseEvent& event)
  95. {
  96. if (event.button() == GMouseButton::Left) {
  97. if (event.modifiers() & Mod_Shift) {
  98. if (!has_selection())
  99. m_selection.set(m_cursor, { });
  100. } else {
  101. m_selection.clear();
  102. }
  103. m_in_drag_select = true;
  104. set_global_cursor_tracking(true);
  105. set_cursor(text_position_at(event.position()));
  106. if (!(event.modifiers() & Mod_Shift)) {
  107. if (!has_selection())
  108. m_selection.set(m_cursor, { });
  109. }
  110. if (m_selection.start().is_valid())
  111. m_selection.set_end(m_cursor);
  112. // FIXME: Only update the relevant rects.
  113. update();
  114. return;
  115. }
  116. }
  117. void GTextEditor::mouseup_event(GMouseEvent& event)
  118. {
  119. if (event.button() == GMouseButton::Left) {
  120. if (m_in_drag_select) {
  121. m_in_drag_select = false;
  122. set_global_cursor_tracking(false);
  123. }
  124. return;
  125. }
  126. }
  127. void GTextEditor::mousemove_event(GMouseEvent& event)
  128. {
  129. if (m_in_drag_select) {
  130. set_cursor(text_position_at(event.position()));
  131. m_selection.set_end(m_cursor);
  132. update();
  133. return;
  134. }
  135. }
  136. int GTextEditor::ruler_width() const
  137. {
  138. // FIXME: Resize based on needed space.
  139. return 5 * font().glyph_width('x') + 4;
  140. }
  141. Rect GTextEditor::ruler_content_rect(int line_index) const
  142. {
  143. return {
  144. 0 - ruler_width() - padding() + m_horizontal_scrollbar->value(),
  145. line_index * line_height(),
  146. ruler_width(),
  147. line_height()
  148. };
  149. }
  150. void GTextEditor::paint_event(GPaintEvent& event)
  151. {
  152. Painter painter(*this);
  153. Rect item_area_rect { 0, 0, width() - m_vertical_scrollbar->width(), height() - m_horizontal_scrollbar->height() };
  154. painter.set_clip_rect(item_area_rect);
  155. painter.set_clip_rect(event.rect());
  156. painter.fill_rect(event.rect(), Color::White);
  157. Rect ruler_rect { 0, 0, ruler_width(), height() - m_horizontal_scrollbar->height()};
  158. painter.fill_rect(ruler_rect, Color::LightGray);
  159. painter.draw_line(ruler_rect.top_right(), ruler_rect.bottom_right(), Color::DarkGray);
  160. painter.save();
  161. painter.translate(-m_horizontal_scrollbar->value(), -m_vertical_scrollbar->value());
  162. painter.translate(padding() + ruler_width(), padding());
  163. int exposed_width = max(content_width(), width());
  164. int first_visible_line = text_position_at(event.rect().top_left()).line();
  165. int last_visible_line = text_position_at(event.rect().bottom_right()).line();
  166. auto selection = normalized_selection();
  167. bool has_selection = selection.is_valid();
  168. for (int i = first_visible_line; i <= last_visible_line; ++i) {
  169. bool is_current_line = i == m_cursor.line();
  170. auto ruler_line_rect = ruler_content_rect(i);
  171. painter.draw_text(
  172. ruler_line_rect.shrunken(2, 0),
  173. String::format("%u", i),
  174. is_current_line ? Font::default_bold_font() : font(),
  175. TextAlignment::CenterRight,
  176. is_current_line ? Color::DarkGray : Color::MidGray
  177. );
  178. }
  179. painter.set_clip_rect({ ruler_rect.right() + 1, 0, width() - m_vertical_scrollbar->width() - ruler_width(), height() - m_horizontal_scrollbar->height() });
  180. for (int i = first_visible_line; i <= last_visible_line; ++i) {
  181. auto& line = *m_lines[i];
  182. auto line_rect = line_content_rect(i);
  183. line_rect.set_width(exposed_width);
  184. if (i == m_cursor.line())
  185. painter.fill_rect(line_rect, Color(230, 230, 230));
  186. painter.draw_text(line_rect, line.characters(), line.length(), TextAlignment::CenterLeft, Color::Black);
  187. bool line_has_selection = has_selection && i >= selection.start().line() && i <= selection.end().line();
  188. if (line_has_selection) {
  189. int selection_start_column_on_line = selection.start().line() == i ? selection.start().column() : 0;
  190. int selection_end_column_on_line = selection.end().line() == i ? selection.end().column() : line.length();
  191. int selection_left = selection_start_column_on_line * font().glyph_width('x');
  192. int selection_right = line_rect.left() + selection_end_column_on_line * font().glyph_width('x');
  193. Rect selection_rect { selection_left, line_rect.y(), selection_right - selection_left, line_rect.height() };
  194. painter.fill_rect(selection_rect, Color::from_rgb(0x955233));
  195. painter.draw_text(selection_rect, line.characters() + selection_start_column_on_line, line.length() - selection_start_column_on_line - (line.length() - selection_end_column_on_line), TextAlignment::CenterLeft, Color::White);
  196. }
  197. }
  198. if (is_focused() && m_cursor_state)
  199. painter.fill_rect(cursor_content_rect(), Color::Red);
  200. painter.restore();
  201. if (is_focused())
  202. painter.draw_rect(item_area_rect, Color::from_rgb(0x84351a));
  203. }
  204. void GTextEditor::toggle_selection_if_needed_for_event(const GKeyEvent& event)
  205. {
  206. if (event.shift() && !m_selection.is_valid()) {
  207. m_selection.set(m_cursor, { });
  208. update();
  209. return;
  210. }
  211. if (!event.shift() && m_selection.is_valid()) {
  212. m_selection.clear();
  213. update();
  214. return;
  215. }
  216. }
  217. void GTextEditor::keydown_event(GKeyEvent& event)
  218. {
  219. if (event.key() == KeyCode::Key_Up) {
  220. if (m_cursor.line() > 0) {
  221. int new_line = m_cursor.line() - 1;
  222. int new_column = min(m_cursor.column(), m_lines[new_line]->length());
  223. toggle_selection_if_needed_for_event(event);
  224. set_cursor(new_line, new_column);
  225. if (m_selection.start().is_valid())
  226. m_selection.set_end(m_cursor);
  227. }
  228. return;
  229. }
  230. if (event.key() == KeyCode::Key_Down) {
  231. if (m_cursor.line() < (m_lines.size() - 1)) {
  232. int new_line = m_cursor.line() + 1;
  233. int new_column = min(m_cursor.column(), m_lines[new_line]->length());
  234. toggle_selection_if_needed_for_event(event);
  235. set_cursor(new_line, new_column);
  236. if (m_selection.start().is_valid())
  237. m_selection.set_end(m_cursor);
  238. }
  239. return;
  240. }
  241. if (event.key() == KeyCode::Key_PageUp) {
  242. if (m_cursor.line() > 0) {
  243. int new_line = max(0, m_cursor.line() - visible_content_rect().height() / line_height());
  244. int new_column = min(m_cursor.column(), m_lines[new_line]->length());
  245. toggle_selection_if_needed_for_event(event);
  246. set_cursor(new_line, new_column);
  247. if (m_selection.start().is_valid())
  248. m_selection.set_end(m_cursor);
  249. }
  250. return;
  251. }
  252. if (event.key() == KeyCode::Key_PageDown) {
  253. if (m_cursor.line() < (m_lines.size() - 1)) {
  254. int new_line = min(line_count() - 1, m_cursor.line() + visible_content_rect().height() / line_height());
  255. int new_column = min(m_cursor.column(), m_lines[new_line]->length());
  256. toggle_selection_if_needed_for_event(event);
  257. set_cursor(new_line, new_column);
  258. if (m_selection.start().is_valid())
  259. m_selection.set_end(m_cursor);
  260. }
  261. return;
  262. }
  263. if (event.key() == KeyCode::Key_Left) {
  264. if (m_cursor.column() > 0) {
  265. int new_column = m_cursor.column() - 1;
  266. toggle_selection_if_needed_for_event(event);
  267. set_cursor(m_cursor.line(), new_column);
  268. if (m_selection.start().is_valid())
  269. m_selection.set_end(m_cursor);
  270. } else if (m_cursor.line() > 0) {
  271. int new_line = m_cursor.line() - 1;
  272. int new_column = m_lines[new_line]->length();
  273. toggle_selection_if_needed_for_event(event);
  274. set_cursor(new_line, new_column);
  275. if (m_selection.start().is_valid())
  276. m_selection.set_end(m_cursor);
  277. }
  278. return;
  279. }
  280. if (event.key() == KeyCode::Key_Right) {
  281. if (m_cursor.column() < current_line().length()) {
  282. int new_column = m_cursor.column() + 1;
  283. toggle_selection_if_needed_for_event(event);
  284. set_cursor(m_cursor.line(), new_column);
  285. if (m_selection.start().is_valid())
  286. m_selection.set_end(m_cursor);
  287. } else if (m_cursor.line() != line_count() - 1) {
  288. int new_line = m_cursor.line() + 1;
  289. int new_column = 0;
  290. toggle_selection_if_needed_for_event(event);
  291. set_cursor(new_line, new_column);
  292. if (m_selection.start().is_valid())
  293. m_selection.set_end(m_cursor);
  294. }
  295. return;
  296. }
  297. if (!event.ctrl() && event.key() == KeyCode::Key_Home) {
  298. toggle_selection_if_needed_for_event(event);
  299. set_cursor(m_cursor.line(), 0);
  300. if (m_selection.start().is_valid())
  301. m_selection.set_end(m_cursor);
  302. return;
  303. }
  304. if (!event.ctrl() && event.key() == KeyCode::Key_End) {
  305. toggle_selection_if_needed_for_event(event);
  306. set_cursor(m_cursor.line(), current_line().length());
  307. if (m_selection.start().is_valid())
  308. m_selection.set_end(m_cursor);
  309. return;
  310. }
  311. if (event.ctrl() && event.key() == KeyCode::Key_Home) {
  312. toggle_selection_if_needed_for_event(event);
  313. set_cursor(0, 0);
  314. if (m_selection.start().is_valid())
  315. m_selection.set_end(m_cursor);
  316. return;
  317. }
  318. if (event.ctrl() && event.key() == KeyCode::Key_End) {
  319. toggle_selection_if_needed_for_event(event);
  320. set_cursor(line_count() - 1, m_lines[line_count() - 1]->length());
  321. if (m_selection.start().is_valid())
  322. m_selection.set_end(m_cursor);
  323. return;
  324. }
  325. if (event.modifiers() == Mod_Ctrl && event.key() == KeyCode::Key_A) {
  326. GTextPosition start_of_document { 0, 0 };
  327. GTextPosition end_of_document { line_count() - 1, m_lines[line_count() - 1]->length() };
  328. m_selection.set(start_of_document, end_of_document);
  329. set_cursor(end_of_document);
  330. update();
  331. return;
  332. }
  333. if (event.key() == KeyCode::Key_Backspace) {
  334. if (has_selection()) {
  335. delete_selection();
  336. return;
  337. }
  338. if (m_cursor.column() > 0) {
  339. // Backspace within line
  340. current_line().remove(m_cursor.column() - 1);
  341. update_scrollbar_ranges();
  342. set_cursor(m_cursor.line(), m_cursor.column() - 1);
  343. return;
  344. }
  345. if (m_cursor.column() == 0 && m_cursor.line() != 0) {
  346. // Backspace at column 0; merge with previous line
  347. auto& previous_line = *m_lines[m_cursor.line() - 1];
  348. int previous_length = previous_line.length();
  349. previous_line.append(current_line().characters(), current_line().length());
  350. m_lines.remove(m_cursor.line());
  351. update_scrollbar_ranges();
  352. update();
  353. set_cursor(m_cursor.line() - 1, previous_length);
  354. return;
  355. }
  356. return;
  357. }
  358. if (event.key() == KeyCode::Key_Delete) {
  359. if (has_selection()) {
  360. delete_selection();
  361. return;
  362. }
  363. if (m_cursor.column() < current_line().length()) {
  364. // Delete within line
  365. current_line().remove(m_cursor.column());
  366. update_scrollbar_ranges();
  367. update_cursor();
  368. return;
  369. }
  370. if (m_cursor.column() == current_line().length() && m_cursor.line() != line_count() - 1) {
  371. // Delete at end of line; merge with next line
  372. auto& next_line = *m_lines[m_cursor.line() + 1];
  373. int previous_length = current_line().length();
  374. current_line().append(next_line.characters(), next_line.length());
  375. m_lines.remove(m_cursor.line() + 1);
  376. update_scrollbar_ranges();
  377. update();
  378. set_cursor(m_cursor.line(), previous_length);
  379. return;
  380. }
  381. return;
  382. }
  383. if (!event.ctrl() && !event.alt() && !event.text().is_empty())
  384. insert_at_cursor_or_replace_selection(event.text());
  385. return GWidget::keydown_event(event);
  386. }
  387. void GTextEditor::insert_at_cursor(const String& text)
  388. {
  389. // FIXME: This should obviously not be implemented this way.
  390. for (int i = 0; i < text.length(); ++i) {
  391. insert_at_cursor(text[i]);
  392. }
  393. }
  394. void GTextEditor::insert_at_cursor(char ch)
  395. {
  396. bool at_head = m_cursor.column() == 0;
  397. bool at_tail = m_cursor.column() == current_line().length();
  398. if (ch == '\n') {
  399. if (at_tail || at_head) {
  400. m_lines.insert(m_cursor.line() + (at_tail ? 1 : 0), make<Line>());
  401. update_scrollbar_ranges();
  402. update();
  403. set_cursor(m_cursor.line() + 1, 0);
  404. return;
  405. }
  406. auto new_line = make<Line>();
  407. new_line->append(current_line().characters() + m_cursor.column(), current_line().length() - m_cursor.column());
  408. current_line().truncate(m_cursor.column());
  409. m_lines.insert(m_cursor.line() + 1, move(new_line));
  410. update_scrollbar_ranges();
  411. update();
  412. set_cursor(m_cursor.line() + 1, 0);
  413. return;
  414. }
  415. if (ch == '\t') {
  416. int next_soft_tab_stop = ((m_cursor.column() + m_soft_tab_width) / m_soft_tab_width) * m_soft_tab_width;
  417. int spaces_to_insert = next_soft_tab_stop - m_cursor.column();
  418. for (int i = 0; i < spaces_to_insert; ++i) {
  419. current_line().insert(m_cursor.column(), ' ');
  420. }
  421. update_scrollbar_ranges();
  422. set_cursor(m_cursor.line(), next_soft_tab_stop);
  423. update_cursor();
  424. return;
  425. }
  426. current_line().insert(m_cursor.column(), ch);
  427. update_scrollbar_ranges();
  428. set_cursor(m_cursor.line(), m_cursor.column() + 1);
  429. update_cursor();
  430. }
  431. Rect GTextEditor::visible_content_rect() const
  432. {
  433. return {
  434. m_horizontal_scrollbar->value(),
  435. m_vertical_scrollbar->value(),
  436. width() - m_vertical_scrollbar->width() - padding() * 2 - ruler_width(),
  437. height() - m_horizontal_scrollbar->height() - padding() * 2
  438. };
  439. }
  440. Rect GTextEditor::cursor_content_rect() const
  441. {
  442. if (!m_cursor.is_valid())
  443. return { };
  444. ASSERT(!m_lines.is_empty());
  445. ASSERT(m_cursor.column() <= (current_line().length() + 1));
  446. return { m_cursor.column() * glyph_width(), m_cursor.line() * line_height(), 1, line_height() };
  447. }
  448. Rect GTextEditor::line_widget_rect(int line_index) const
  449. {
  450. ASSERT(m_horizontal_scrollbar);
  451. ASSERT(m_vertical_scrollbar);
  452. auto rect = line_content_rect(line_index);
  453. rect.move_by(-(m_horizontal_scrollbar->value() - padding()), -(m_vertical_scrollbar->value() - padding()));
  454. rect.set_width(rect.width() + 1); // Add 1 pixel for when the cursor is on the end.
  455. rect.intersect(this->rect());
  456. // This feels rather hackish, but extend the rect to the edge of the content view:
  457. rect.set_right(m_vertical_scrollbar->relative_rect().left() - 1);
  458. return rect;
  459. }
  460. void GTextEditor::scroll_cursor_into_view()
  461. {
  462. auto visible_content_rect = this->visible_content_rect();
  463. auto rect = cursor_content_rect();
  464. if (visible_content_rect.is_empty())
  465. return;
  466. if (visible_content_rect.contains(rect))
  467. return;
  468. if (rect.top() < visible_content_rect.top())
  469. m_vertical_scrollbar->set_value(rect.top());
  470. else if (rect.bottom() > visible_content_rect.bottom())
  471. m_vertical_scrollbar->set_value(rect.bottom() - visible_content_rect.height());
  472. if (rect.left() < visible_content_rect.left())
  473. m_horizontal_scrollbar->set_value(rect.left());
  474. else if (rect.right() > visible_content_rect.right())
  475. m_horizontal_scrollbar->set_value(rect.right() - visible_content_rect.width());
  476. update();
  477. }
  478. Rect GTextEditor::line_content_rect(int line_index) const
  479. {
  480. return {
  481. 0,
  482. line_index * line_height(),
  483. content_width(),
  484. line_height()
  485. };
  486. }
  487. void GTextEditor::update_cursor()
  488. {
  489. update(line_widget_rect(m_cursor.line()));
  490. }
  491. void GTextEditor::set_cursor(int line, int column)
  492. {
  493. set_cursor({ line, column });
  494. }
  495. void GTextEditor::set_cursor(const GTextPosition& position)
  496. {
  497. ASSERT(!m_lines.is_empty());
  498. ASSERT(position.line() < m_lines.size());
  499. ASSERT(position.column() <= m_lines[position.line()]->length());
  500. if (m_cursor != position) {
  501. auto old_cursor_line_rect = line_widget_rect(m_cursor.line());
  502. m_cursor = position;
  503. m_cursor_state = true;
  504. scroll_cursor_into_view();
  505. update(old_cursor_line_rect);
  506. update_cursor();
  507. }
  508. if (on_cursor_change)
  509. on_cursor_change(*this);
  510. }
  511. void GTextEditor::focusin_event(GEvent&)
  512. {
  513. update_cursor();
  514. start_timer(500);
  515. }
  516. void GTextEditor::focusout_event(GEvent&)
  517. {
  518. stop_timer();
  519. }
  520. void GTextEditor::timer_event(GTimerEvent&)
  521. {
  522. m_cursor_state = !m_cursor_state;
  523. if (is_focused())
  524. update_cursor();
  525. }
  526. GTextEditor::Line::Line()
  527. {
  528. clear();
  529. }
  530. void GTextEditor::Line::clear()
  531. {
  532. m_text.clear();
  533. m_text.append(0);
  534. }
  535. void GTextEditor::Line::set_text(const String& text)
  536. {
  537. if (text.length() == length() && !memcmp(text.characters(), characters(), length()))
  538. return;
  539. if (text.is_empty()) {
  540. clear();
  541. return;
  542. }
  543. m_text.resize(text.length() + 1);
  544. memcpy(m_text.data(), text.characters(), text.length() + 1);
  545. }
  546. int GTextEditor::Line::width(const Font& font) const
  547. {
  548. return font.glyph_width('x') * length();
  549. }
  550. void GTextEditor::Line::append(const char* characters, int length)
  551. {
  552. int old_length = m_text.size() - 1;
  553. m_text.resize(m_text.size() + length);
  554. memcpy(m_text.data() + old_length, characters, length);
  555. m_text.last() = 0;
  556. }
  557. void GTextEditor::Line::append(char ch)
  558. {
  559. insert(length(), ch);
  560. }
  561. void GTextEditor::Line::prepend(char ch)
  562. {
  563. insert(0, ch);
  564. }
  565. void GTextEditor::Line::insert(int index, char ch)
  566. {
  567. if (index == length()) {
  568. m_text.last() = ch;
  569. m_text.append(0);
  570. } else {
  571. m_text.insert(index, move(ch));
  572. }
  573. }
  574. void GTextEditor::Line::remove(int index)
  575. {
  576. if (index == length()) {
  577. m_text.take_last();
  578. m_text.last() = 0;
  579. } else {
  580. m_text.remove(index);
  581. }
  582. }
  583. void GTextEditor::Line::truncate(int length)
  584. {
  585. m_text.resize(length + 1);
  586. m_text.last() = 0;
  587. }
  588. bool GTextEditor::write_to_file(const String& path)
  589. {
  590. int fd = open(path.characters(), O_WRONLY | O_CREAT, 0666);
  591. if (fd < 0) {
  592. perror("open");
  593. return false;
  594. }
  595. for (int i = 0; i < m_lines.size(); ++i) {
  596. auto& line = *m_lines[i];
  597. if (line.length()) {
  598. ssize_t nwritten = write(fd, line.characters(), line.length());
  599. if (nwritten < 0) {
  600. perror("write");
  601. close(fd);
  602. return false;
  603. }
  604. }
  605. if (i != m_lines.size() - 1) {
  606. char ch = '\n';
  607. ssize_t nwritten = write(fd, &ch, 1);
  608. if (nwritten != 1) {
  609. perror("write");
  610. close(fd);
  611. return false;
  612. }
  613. }
  614. }
  615. close(fd);
  616. return true;
  617. }
  618. String GTextEditor::selected_text() const
  619. {
  620. if (!has_selection())
  621. return { };
  622. auto selection = normalized_selection();
  623. StringBuilder builder;
  624. for (int i = selection.start().line(); i <= selection.end().line(); ++i) {
  625. auto& line = *m_lines[i];
  626. int selection_start_column_on_line = selection.start().line() == i ? selection.start().column() : 0;
  627. int selection_end_column_on_line = selection.end().line() == i ? selection.end().column() : line.length();
  628. builder.append(line.characters() + selection_start_column_on_line, selection_end_column_on_line - selection_start_column_on_line);
  629. if (i != selection.end().line())
  630. builder.append('\n');
  631. }
  632. return builder.to_string();
  633. }
  634. void GTextEditor::delete_selection()
  635. {
  636. if (!has_selection())
  637. return;
  638. auto selection = normalized_selection();
  639. // First delete all the lines in between the first and last one.
  640. for (int i = selection.start().line() + 1; i < selection.end().line();) {
  641. m_lines.remove(i);
  642. selection.end().set_line(selection.end().line() - 1);
  643. }
  644. if (selection.start().line() == selection.end().line()) {
  645. // Delete within same line.
  646. auto& line = *m_lines[selection.start().line()];
  647. bool whole_line_is_selected = selection.start().column() == 0 && selection.end().column() == line.length();
  648. if (whole_line_is_selected) {
  649. line.clear();
  650. } else {
  651. auto before_selection = String(line.characters(), line.length()).substring(0, selection.start().column());
  652. auto after_selection = String(line.characters(), line.length()).substring(selection.end().column(), line.length() - selection.end().column());
  653. StringBuilder builder(before_selection.length() + after_selection.length());
  654. builder.append(before_selection);
  655. builder.append(after_selection);
  656. line.set_text(builder.to_string());
  657. }
  658. } else {
  659. // Delete across a newline, merging lines.
  660. ASSERT(selection.start().line() == selection.end().line() - 1);
  661. auto& first_line = *m_lines[selection.start().line()];
  662. auto& second_line = *m_lines[selection.end().line()];
  663. auto before_selection = String(first_line.characters(), first_line.length()).substring(0, selection.start().column());
  664. auto after_selection = String(second_line.characters(), second_line.length()).substring(selection.end().column(), second_line.length() - selection.end().column());
  665. StringBuilder builder(before_selection.length() + after_selection.length());
  666. builder.append(before_selection);
  667. builder.append(after_selection);
  668. first_line.set_text(builder.to_string());
  669. m_lines.remove(selection.end().line());
  670. }
  671. if (m_lines.is_empty())
  672. m_lines.append(make<Line>());
  673. m_selection.clear();
  674. set_cursor(selection.start());
  675. update();
  676. }
  677. void GTextEditor::insert_at_cursor_or_replace_selection(const String& text)
  678. {
  679. if (has_selection())
  680. delete_selection();
  681. insert_at_cursor(text);
  682. }
  683. void GTextEditor::cut()
  684. {
  685. auto selected_text = this->selected_text();
  686. printf("Cut: \"%s\"\n", selected_text.characters());
  687. GClipboard::the().set_data(selected_text);
  688. delete_selection();
  689. }
  690. void GTextEditor::copy()
  691. {
  692. auto selected_text = this->selected_text();
  693. printf("Copy: \"%s\"\n", selected_text.characters());
  694. GClipboard::the().set_data(selected_text);
  695. }
  696. void GTextEditor::paste()
  697. {
  698. auto paste_text = GClipboard::the().data();
  699. printf("Paste: \"%s\"\n", paste_text.characters());
  700. insert_at_cursor_or_replace_selection(paste_text);
  701. }