GlyphMapWidget.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Mustafa Quraish <mustafa@serenityos.org>
  4. * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
  5. * Copyright (c) 2022, the SerenityOS developers.
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #include "GlyphMapWidget.h"
  10. #include <LibGUI/Painter.h>
  11. #include <LibGfx/Font/BitmapFont.h>
  12. #include <LibGfx/Font/Emoji.h>
  13. #include <LibGfx/Palette.h>
  14. REGISTER_WIDGET(GUI, GlyphMapWidget);
  15. namespace GUI {
  16. GlyphMapWidget::Selection GlyphMapWidget::Selection::normalized() const
  17. {
  18. if (m_size > 0)
  19. return *this;
  20. return { m_start + m_size, -m_size + 1 };
  21. }
  22. void GlyphMapWidget::Selection::resize_by(int i)
  23. {
  24. m_size += i;
  25. if (m_size == 0) {
  26. if (i < 0)
  27. m_size--;
  28. else
  29. m_size++;
  30. }
  31. }
  32. bool GlyphMapWidget::Selection::contains(int i) const
  33. {
  34. auto this_normalized = normalized();
  35. return i >= this_normalized.m_start && i < this_normalized.m_start + this_normalized.m_size;
  36. }
  37. void GlyphMapWidget::Selection::extend_to(int glyph)
  38. {
  39. m_size = glyph - m_start;
  40. if (m_size >= 0)
  41. m_size++;
  42. }
  43. GlyphMapWidget::GlyphMapWidget()
  44. {
  45. set_focus_policy(FocusPolicy::StrongFocus);
  46. horizontal_scrollbar().set_visible(false);
  47. did_change_font();
  48. set_active_glyph('A');
  49. m_automatic_selection_scroll_timer = add<Core::Timer>(20, [this] {
  50. if (!m_in_drag_select) {
  51. m_automatic_selection_scroll_timer->stop();
  52. return;
  53. }
  54. auto glyph = glyph_at_position_clamped(m_last_mousemove_position);
  55. m_selection.extend_to(glyph);
  56. set_active_glyph(glyph, ShouldResetSelection::No);
  57. scroll_to_glyph(glyph);
  58. update();
  59. });
  60. m_automatic_selection_scroll_timer->stop();
  61. }
  62. void GlyphMapWidget::resize_event(ResizeEvent& event)
  63. {
  64. recalculate_content_size();
  65. AbstractScrollableWidget::resize_event(event);
  66. }
  67. void GlyphMapWidget::set_active_glyph(int glyph, ShouldResetSelection should_reset_selection)
  68. {
  69. if (m_active_glyph == glyph && should_reset_selection == ShouldResetSelection::No)
  70. return;
  71. m_active_glyph = glyph;
  72. if (should_reset_selection == ShouldResetSelection::Yes) {
  73. m_selection.set_start(glyph);
  74. m_selection.set_size(1);
  75. }
  76. if (on_active_glyph_changed)
  77. on_active_glyph_changed(glyph);
  78. update();
  79. }
  80. void GlyphMapWidget::set_selection(int start, int size, Optional<u32> active_glyph)
  81. {
  82. m_selection.set_start(start);
  83. m_selection.set_size(size);
  84. if (active_glyph.has_value())
  85. set_active_glyph(active_glyph.value(), ShouldResetSelection::No);
  86. }
  87. Gfx::IntRect GlyphMapWidget::get_outer_rect(int glyph) const
  88. {
  89. glyph -= m_active_range.first;
  90. int row = glyph / columns();
  91. int column = glyph % columns();
  92. return Gfx::IntRect {
  93. column * (font().max_glyph_width() + m_horizontal_spacing),
  94. row * (font().glyph_height() + m_vertical_spacing),
  95. font().max_glyph_width() + m_horizontal_spacing,
  96. font().glyph_height() + m_vertical_spacing
  97. }
  98. .translated(frame_thickness() - horizontal_scrollbar().value(), frame_thickness() - vertical_scrollbar().value());
  99. }
  100. void GlyphMapWidget::update_glyph(int glyph)
  101. {
  102. set_glyph_modified(glyph, true);
  103. update(get_outer_rect(glyph));
  104. }
  105. void GlyphMapWidget::paint_event(PaintEvent& event)
  106. {
  107. Frame::paint_event(event);
  108. Painter painter(*this);
  109. painter.add_clip_rect(widget_inner_rect());
  110. painter.add_clip_rect(event.rect());
  111. painter.set_font(font());
  112. painter.fill_rect(widget_inner_rect(), palette().window().darkened(0.8f));
  113. auto first_row = vertical_scrollbar().value() / vertical_scrollbar().step();
  114. auto first_glyph = first_row * columns() + m_active_range.first;
  115. auto last_glyph = m_active_range.last;
  116. for (u32 glyph = first_glyph; glyph <= first_glyph + m_visible_glyphs && glyph <= last_glyph; ++glyph) {
  117. Gfx::IntRect outer_rect = get_outer_rect(glyph);
  118. Gfx::IntRect inner_rect(
  119. outer_rect.x() + m_horizontal_spacing / 2,
  120. outer_rect.y() + m_vertical_spacing / 2,
  121. font().max_glyph_width(),
  122. font().glyph_height());
  123. if (m_selection.contains(glyph)) {
  124. painter.fill_rect(outer_rect, is_focused() ? palette().selection() : palette().inactive_selection());
  125. if (font().contains_glyph(glyph))
  126. painter.draw_glyph(inner_rect.location(), glyph, is_focused() ? palette().selection_text() : palette().inactive_selection_text());
  127. else if (auto* emoji = Gfx::Emoji::emoji_for_code_point(glyph); emoji && m_show_system_emoji)
  128. painter.draw_emoji(inner_rect.location(), *emoji, font());
  129. } else if (font().contains_glyph(glyph)) {
  130. if (m_highlight_modifications && m_modified_glyphs.contains(glyph)) {
  131. if (m_original_font->contains_glyph(glyph)) {
  132. // Modified
  133. if (palette().is_dark())
  134. painter.fill_rect(outer_rect, Gfx::Color { 0, 65, 159 });
  135. else
  136. painter.fill_rect(outer_rect, Gfx::Color { 138, 185, 252 });
  137. } else {
  138. // Newly created
  139. if (palette().is_dark())
  140. painter.fill_rect(outer_rect, Gfx::Color { 8, 127, 0 });
  141. else
  142. painter.fill_rect(outer_rect, Gfx::Color { 133, 251, 116 });
  143. }
  144. } else {
  145. painter.fill_rect(outer_rect, palette().base());
  146. }
  147. painter.draw_glyph(inner_rect.location(), glyph, palette().base_text());
  148. } else if (auto* emoji = Gfx::Emoji::emoji_for_code_point(glyph); emoji && m_show_system_emoji) {
  149. painter.draw_emoji(inner_rect.location(), *emoji, font());
  150. } else {
  151. if (m_highlight_modifications && m_original_font->contains_glyph(glyph)) {
  152. // Deleted
  153. if (palette().is_dark())
  154. painter.fill_rect(outer_rect, Gfx::Color { 127, 0, 0 });
  155. else
  156. painter.fill_rect(outer_rect, Gfx::Color { 255, 150, 150 });
  157. } else {
  158. painter.fill_rect(outer_rect, palette().window());
  159. }
  160. }
  161. }
  162. painter.draw_focus_rect(get_outer_rect(m_active_glyph), palette().focus_outline());
  163. }
  164. Optional<int> GlyphMapWidget::glyph_at_position(Gfx::IntPoint position) const
  165. {
  166. Gfx::IntPoint map_offset { frame_thickness() - horizontal_scrollbar().value(), frame_thickness() - vertical_scrollbar().value() };
  167. auto map_position = position - map_offset;
  168. auto col = (map_position.x() - 1) / ((font().max_glyph_width() + m_horizontal_spacing));
  169. auto row = (map_position.y() - 1) / ((font().glyph_height() + m_vertical_spacing));
  170. auto glyph = row * columns() + col + m_active_range.first;
  171. if (row >= 0 && row < rows() && col >= 0 && col < columns() && glyph < m_glyph_count + m_active_range.first)
  172. return glyph;
  173. return {};
  174. }
  175. int GlyphMapWidget::glyph_at_position_clamped(Gfx::IntPoint position) const
  176. {
  177. Gfx::IntPoint map_offset { frame_thickness() - horizontal_scrollbar().value(), frame_thickness() - vertical_scrollbar().value() };
  178. auto map_position = position - map_offset;
  179. auto col = clamp((map_position.x() - 1) / ((font().max_glyph_width() + m_horizontal_spacing)), 0, columns() - 1);
  180. auto row = clamp((map_position.y() - 1) / ((font().glyph_height() + m_vertical_spacing)), 0, rows() - 1);
  181. auto glyph = row * columns() + col + m_active_range.first;
  182. if (row == rows() - 1)
  183. glyph = min(glyph, m_glyph_count + m_active_range.first - 1);
  184. return glyph;
  185. }
  186. void GlyphMapWidget::context_menu_event(GUI::ContextMenuEvent& event)
  187. {
  188. if (on_context_menu_request)
  189. on_context_menu_request(event);
  190. }
  191. void GlyphMapWidget::mousedown_event(MouseEvent& event)
  192. {
  193. if (event.button() == MouseButton::Secondary)
  194. return;
  195. if (auto maybe_glyph = glyph_at_position(event.position()); maybe_glyph.has_value()) {
  196. auto glyph = maybe_glyph.value();
  197. if (event.shift())
  198. m_selection.extend_to(glyph);
  199. m_in_drag_select = true;
  200. m_automatic_selection_scroll_timer->start();
  201. set_active_glyph(glyph, event.shift() ? ShouldResetSelection::No : ShouldResetSelection::Yes);
  202. }
  203. }
  204. void GlyphMapWidget::mouseup_event(GUI::MouseEvent& event)
  205. {
  206. if (event.button() == MouseButton::Secondary)
  207. return;
  208. if (!m_in_drag_select)
  209. return;
  210. auto constrained = event.position().constrained(widget_inner_rect());
  211. if (auto maybe_glyph = glyph_at_position(constrained); maybe_glyph.has_value()) {
  212. auto glyph = maybe_glyph.value();
  213. m_selection.extend_to(glyph);
  214. set_active_glyph(glyph, ShouldResetSelection::No);
  215. }
  216. m_in_drag_select = false;
  217. }
  218. void GlyphMapWidget::mousemove_event(GUI::MouseEvent& event)
  219. {
  220. m_last_mousemove_position = event.position();
  221. }
  222. void GlyphMapWidget::doubleclick_event(MouseEvent& event)
  223. {
  224. if (on_glyph_double_clicked) {
  225. if (auto maybe_glyph = glyph_at_position(event.position()); maybe_glyph.has_value())
  226. on_glyph_double_clicked(maybe_glyph.value());
  227. }
  228. }
  229. void GlyphMapWidget::keydown_event(KeyEvent& event)
  230. {
  231. if (event.key() == KeyCode::Key_Tab) {
  232. AbstractScrollableWidget::keydown_event(event);
  233. return;
  234. }
  235. if (event.key() == KeyCode::Key_Escape) {
  236. m_selection.set_size(1);
  237. m_selection.set_start(m_active_glyph);
  238. if (on_escape_pressed)
  239. on_escape_pressed();
  240. return;
  241. }
  242. if (!event.modifiers() && event.is_arrow_key()) {
  243. m_selection.set_size(1);
  244. m_selection.set_start(m_active_glyph);
  245. }
  246. if (event.shift() && event.is_arrow_key()) {
  247. auto resizing_end = m_selection.start() + m_selection.size() - (m_selection.size() > 0 ? 1 : 0);
  248. set_active_glyph(resizing_end, ShouldResetSelection::No);
  249. scroll_to_glyph(resizing_end);
  250. }
  251. int first_glyph = m_active_range.first;
  252. int last_glyph = m_active_range.last;
  253. auto selection = m_selection.normalized();
  254. if (event.key() == KeyCode::Key_Up) {
  255. if (m_active_glyph - m_columns < first_glyph)
  256. return;
  257. if (event.ctrl() && selection.start() - m_columns < first_glyph)
  258. return;
  259. if (event.shift())
  260. m_selection.extend_to(m_active_glyph - m_columns);
  261. else
  262. m_selection.set_start(m_selection.start() - m_columns);
  263. set_active_glyph(m_active_glyph - m_columns, ShouldResetSelection::No);
  264. scroll_to_glyph(m_active_glyph);
  265. return;
  266. }
  267. if (event.key() == KeyCode::Key_Down) {
  268. if (m_active_glyph + m_columns > last_glyph)
  269. return;
  270. if (event.ctrl() && selection.start() + selection.size() - 1 + m_columns > last_glyph)
  271. return;
  272. if (event.shift())
  273. m_selection.extend_to(m_active_glyph + m_columns);
  274. else
  275. m_selection.set_start(m_selection.start() + m_columns);
  276. set_active_glyph(m_active_glyph + m_columns, ShouldResetSelection::No);
  277. scroll_to_glyph(m_active_glyph);
  278. return;
  279. }
  280. if (event.key() == KeyCode::Key_Left) {
  281. if (m_active_glyph - 1 < first_glyph)
  282. return;
  283. if (event.ctrl() && selection.start() - 1 < first_glyph)
  284. return;
  285. if (event.shift())
  286. m_selection.resize_by(-1);
  287. else
  288. m_selection.set_start(m_selection.start() - 1);
  289. set_active_glyph(m_active_glyph - 1, ShouldResetSelection::No);
  290. scroll_to_glyph(m_active_glyph);
  291. return;
  292. }
  293. if (event.key() == KeyCode::Key_Right) {
  294. if (m_active_glyph + 1 > last_glyph)
  295. return;
  296. if (event.ctrl() && selection.start() + selection.size() > last_glyph)
  297. return;
  298. if (event.shift())
  299. m_selection.resize_by(1);
  300. else
  301. m_selection.set_start(m_selection.start() + 1);
  302. set_active_glyph(m_active_glyph + 1, ShouldResetSelection::No);
  303. scroll_to_glyph(m_active_glyph);
  304. return;
  305. }
  306. if (event.key() == KeyCode::Key_Home) {
  307. if (event.alt()) {
  308. set_active_glyph(first_glyph);
  309. scroll_to_glyph(m_active_glyph);
  310. return;
  311. }
  312. if (event.ctrl() && event.shift()) {
  313. m_selection.extend_to(first_glyph);
  314. set_active_glyph(first_glyph, ShouldResetSelection::No);
  315. scroll_to_glyph(m_active_glyph);
  316. return;
  317. }
  318. auto start_of_row = (m_active_glyph - first_glyph) / m_columns * m_columns;
  319. if (event.shift())
  320. m_selection.extend_to(start_of_row + first_glyph);
  321. set_active_glyph(start_of_row + first_glyph, event.shift() ? ShouldResetSelection::No : ShouldResetSelection::Yes);
  322. return;
  323. }
  324. if (event.key() == KeyCode::Key_End) {
  325. if (event.alt()) {
  326. set_active_glyph(last_glyph);
  327. scroll_to_glyph(m_active_glyph);
  328. return;
  329. }
  330. if (event.ctrl() && event.shift()) {
  331. m_selection.extend_to(last_glyph);
  332. set_active_glyph(last_glyph, ShouldResetSelection::No);
  333. scroll_to_glyph(m_active_glyph);
  334. return;
  335. }
  336. auto end_of_row = (m_active_glyph - first_glyph) / m_columns * m_columns + (m_columns - 1);
  337. end_of_row = clamp(end_of_row + first_glyph, first_glyph, last_glyph);
  338. if (event.shift())
  339. m_selection.extend_to(end_of_row);
  340. set_active_glyph(end_of_row, event.shift() ? ShouldResetSelection::No : ShouldResetSelection::Yes);
  341. return;
  342. }
  343. {
  344. auto first_visible_row = vertical_scrollbar().value() / vertical_scrollbar().step();
  345. auto last_visible_row = first_visible_row + m_visible_rows;
  346. auto current_row = (m_active_glyph - first_glyph) / columns();
  347. auto page = m_active_glyph;
  348. if (event.key() == KeyCode::Key_PageDown) {
  349. auto current_page = m_active_glyph + m_columns * (last_visible_row - current_row);
  350. auto next_page = m_active_glyph + m_columns * m_visible_rows;
  351. auto remainder = m_active_glyph + m_columns * ((last_glyph - first_glyph) / columns() - current_row);
  352. if (current_row < last_visible_row && current_page <= last_glyph)
  353. page = current_page;
  354. else if (next_page <= last_glyph)
  355. page = next_page;
  356. else if (remainder <= last_glyph)
  357. page = remainder;
  358. else
  359. page = remainder - m_columns; // Bottom rows do not always extend across all columns
  360. if (event.shift())
  361. m_selection.extend_to(page);
  362. set_active_glyph(page, event.shift() ? ShouldResetSelection::No : ShouldResetSelection::Yes);
  363. scroll_to_glyph(m_active_glyph);
  364. return;
  365. }
  366. if (event.key() == KeyCode::Key_PageUp) {
  367. auto current_page = m_active_glyph - m_columns * (current_row - first_visible_row);
  368. auto previous_page = m_active_glyph - m_columns * m_visible_rows;
  369. auto remainder = m_active_glyph - m_columns * current_row;
  370. if (current_row > first_visible_row && current_page >= first_glyph)
  371. page = current_page;
  372. else if (previous_page >= first_glyph)
  373. page = previous_page;
  374. else
  375. page = remainder;
  376. if (event.shift())
  377. m_selection.extend_to(page);
  378. set_active_glyph(page, event.shift() ? ShouldResetSelection::No : ShouldResetSelection::Yes);
  379. scroll_to_glyph(m_active_glyph);
  380. return;
  381. }
  382. }
  383. event.ignore();
  384. }
  385. void GlyphMapWidget::did_change_font()
  386. {
  387. recalculate_content_size();
  388. vertical_scrollbar().set_step(font().glyph_height() + m_vertical_spacing);
  389. }
  390. void GlyphMapWidget::scroll_to_glyph(int glyph)
  391. {
  392. glyph -= m_active_range.first;
  393. int row = glyph / columns();
  394. int column = glyph % columns();
  395. auto scroll_rect = Gfx::IntRect {
  396. column * (font().max_glyph_width() + m_horizontal_spacing),
  397. row * (font().glyph_height() + m_vertical_spacing),
  398. font().max_glyph_width() + m_horizontal_spacing,
  399. font().glyph_height() + m_vertical_spacing
  400. };
  401. scroll_into_view(scroll_rect, true, true);
  402. }
  403. void GlyphMapWidget::select_previous_existing_glyph()
  404. {
  405. bool search_wrapped = false;
  406. int first_glyph = m_active_range.first;
  407. int last_glyph = m_active_range.last;
  408. for (int i = active_glyph() - 1;; --i) {
  409. if (i < first_glyph && !search_wrapped) {
  410. i = last_glyph;
  411. search_wrapped = true;
  412. } else if (i < first_glyph && search_wrapped) {
  413. break;
  414. }
  415. if (font().contains_glyph(i)) {
  416. set_focus(true);
  417. set_active_glyph(i);
  418. scroll_to_glyph(i);
  419. break;
  420. }
  421. }
  422. }
  423. void GlyphMapWidget::select_next_existing_glyph()
  424. {
  425. bool search_wrapped = false;
  426. int first_glyph = m_active_range.first;
  427. int last_glyph = m_active_range.last;
  428. for (int i = active_glyph() + 1;; ++i) {
  429. if (i > last_glyph && !search_wrapped) {
  430. i = first_glyph;
  431. search_wrapped = true;
  432. } else if (i > last_glyph && search_wrapped) {
  433. break;
  434. }
  435. if (font().contains_glyph(i)) {
  436. set_focus(true);
  437. set_active_glyph(i);
  438. scroll_to_glyph(i);
  439. break;
  440. }
  441. }
  442. }
  443. void GlyphMapWidget::recalculate_content_size()
  444. {
  445. auto event_width = widget_inner_rect().width();
  446. auto event_height = widget_inner_rect().height();
  447. m_columns = max(event_width / (font().max_glyph_width() + m_horizontal_spacing), 1);
  448. m_rows = ceil_div(m_glyph_count, m_columns);
  449. constexpr auto overdraw_margins = 2;
  450. auto max_visible_rows = event_height / (font().glyph_height() + m_vertical_spacing);
  451. m_visible_rows = min(max_visible_rows, m_rows);
  452. m_visible_glyphs = (m_visible_rows + overdraw_margins) * m_columns;
  453. int content_width = columns() * (font().max_glyph_width() + m_horizontal_spacing);
  454. int content_height = rows() * (font().glyph_height() + m_vertical_spacing);
  455. set_content_size({ content_width, content_height });
  456. scroll_to_glyph(m_active_glyph);
  457. }
  458. void GlyphMapWidget::set_active_range(Unicode::CodePointRange range)
  459. {
  460. if (m_active_range.first == range.first && m_active_range.last == range.last)
  461. return;
  462. m_active_range = range;
  463. m_glyph_count = range.last - range.first + 1;
  464. set_active_glyph(range.first);
  465. recalculate_content_size();
  466. update();
  467. }
  468. void GlyphMapWidget::set_highlight_modifications(bool highlight_modifications)
  469. {
  470. if (m_highlight_modifications == highlight_modifications)
  471. return;
  472. m_highlight_modifications = highlight_modifications;
  473. update();
  474. }
  475. void GlyphMapWidget::set_show_system_emoji(bool show)
  476. {
  477. if (m_show_system_emoji == show)
  478. return;
  479. m_show_system_emoji = show;
  480. update();
  481. }
  482. void GlyphMapWidget::set_glyph_modified(u32 glyph, bool modified)
  483. {
  484. if (modified)
  485. m_modified_glyphs.set(glyph);
  486. else
  487. m_modified_glyphs.remove(glyph);
  488. }
  489. bool GlyphMapWidget::glyph_is_modified(u32 glyph)
  490. {
  491. return m_modified_glyphs.contains(glyph);
  492. }
  493. ErrorOr<void> GlyphMapWidget::set_font(Gfx::Font const& font)
  494. {
  495. m_original_font = TRY(font.try_clone());
  496. m_modified_glyphs.clear();
  497. AbstractScrollableWidget::set_font(font);
  498. return {};
  499. }
  500. Optional<UISize> GlyphMapWidget::calculated_min_size() const
  501. {
  502. auto scrollbar = vertical_scrollbar().effective_min_size().height().as_int();
  503. auto min_height = max(font().glyph_height() + m_vertical_spacing, scrollbar);
  504. auto min_width = font().max_glyph_width() + m_horizontal_spacing + width_occupied_by_vertical_scrollbar();
  505. return { { min_width + frame_thickness() * 2, min_height + frame_thickness() * 2 } };
  506. }
  507. }