GlyphMapWidget.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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)
  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) + 1,
  94. row * (font().glyph_height() + m_vertical_spacing) + 1,
  95. font().max_glyph_width() + m_horizontal_spacing,
  96. font().glyph_height() + m_horizontal_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. int scroll_steps = vertical_scrollbar().value() / vertical_scrollbar().step();
  114. int first_visible_glyph = scroll_steps * columns();
  115. int range_offset = m_active_range.first;
  116. int last_glyph = m_active_range.last + 1;
  117. for (int glyph = first_visible_glyph + range_offset; glyph <= first_visible_glyph + m_visible_glyphs + range_offset && glyph < last_glyph; ++glyph) {
  118. Gfx::IntRect outer_rect = get_outer_rect(glyph);
  119. Gfx::IntRect inner_rect(
  120. outer_rect.x() + m_horizontal_spacing / 2,
  121. outer_rect.y() + m_vertical_spacing / 2,
  122. font().max_glyph_width(),
  123. font().glyph_height());
  124. if (m_selection.contains(glyph)) {
  125. painter.fill_rect(outer_rect, is_focused() ? palette().selection() : palette().inactive_selection());
  126. if (font().contains_glyph(glyph))
  127. painter.draw_glyph(inner_rect.location(), glyph, is_focused() ? palette().selection_text() : palette().inactive_selection_text());
  128. else if (auto* emoji = Gfx::Emoji::emoji_for_code_point(glyph))
  129. painter.draw_emoji(inner_rect.location(), *emoji, font());
  130. } else if (font().contains_glyph(glyph)) {
  131. if (m_highlight_modifications && m_modified_glyphs.contains(glyph)) {
  132. if (m_original_font->contains_glyph(glyph)) {
  133. // Modified
  134. if (palette().is_dark())
  135. painter.fill_rect(outer_rect, Gfx::Color { 0, 65, 159 });
  136. else
  137. painter.fill_rect(outer_rect, Gfx::Color { 138, 185, 252 });
  138. } else {
  139. // Newly created
  140. if (palette().is_dark())
  141. painter.fill_rect(outer_rect, Gfx::Color { 8, 127, 0 });
  142. else
  143. painter.fill_rect(outer_rect, Gfx::Color { 133, 251, 116 });
  144. }
  145. } else {
  146. painter.fill_rect(outer_rect, palette().base());
  147. }
  148. painter.draw_glyph(inner_rect.location(), glyph, palette().base_text());
  149. } else if (auto* emoji = Gfx::Emoji::emoji_for_code_point(glyph)) {
  150. painter.fill_rect(outer_rect, Gfx::Color { 255, 150, 150 });
  151. painter.draw_emoji(inner_rect.location(), *emoji, font());
  152. } else {
  153. if (m_highlight_modifications && m_original_font->contains_glyph(glyph)) {
  154. // Deleted
  155. if (palette().is_dark())
  156. painter.fill_rect(outer_rect, Gfx::Color { 127, 0, 0 });
  157. else
  158. painter.fill_rect(outer_rect, Gfx::Color { 255, 150, 150 });
  159. } else {
  160. painter.fill_rect(outer_rect, palette().window());
  161. }
  162. }
  163. }
  164. painter.draw_focus_rect(get_outer_rect(m_active_glyph), palette().focus_outline());
  165. }
  166. Optional<int> GlyphMapWidget::glyph_at_position(Gfx::IntPoint position) const
  167. {
  168. Gfx::IntPoint map_offset { frame_thickness() - horizontal_scrollbar().value(), frame_thickness() - vertical_scrollbar().value() };
  169. auto map_position = position - map_offset;
  170. auto col = (map_position.x() - 1) / ((font().max_glyph_width() + m_horizontal_spacing));
  171. auto row = (map_position.y() - 1) / ((font().glyph_height() + m_vertical_spacing));
  172. auto glyph = row * columns() + col + m_active_range.first;
  173. if (row >= 0 && row < rows() && col >= 0 && col < columns() && glyph < m_glyph_count + m_active_range.first)
  174. return glyph;
  175. return {};
  176. }
  177. int GlyphMapWidget::glyph_at_position_clamped(Gfx::IntPoint position) const
  178. {
  179. Gfx::IntPoint map_offset { frame_thickness() - horizontal_scrollbar().value(), frame_thickness() - vertical_scrollbar().value() };
  180. auto map_position = position - map_offset;
  181. auto col = clamp((map_position.x() - 1) / ((font().max_glyph_width() + m_horizontal_spacing)), 0, columns() - 1);
  182. auto row = clamp((map_position.y() - 1) / ((font().glyph_height() + m_vertical_spacing)), 0, rows() - 1);
  183. auto glyph = row * columns() + col + m_active_range.first;
  184. if (row == rows() - 1)
  185. glyph = min(glyph, m_glyph_count + m_active_range.first - 1);
  186. return glyph;
  187. }
  188. void GlyphMapWidget::context_menu_event(GUI::ContextMenuEvent& event)
  189. {
  190. if (on_context_menu_request)
  191. on_context_menu_request(event);
  192. }
  193. void GlyphMapWidget::mousedown_event(MouseEvent& event)
  194. {
  195. if (event.button() == MouseButton::Secondary)
  196. return;
  197. if (auto maybe_glyph = glyph_at_position(event.position()); maybe_glyph.has_value()) {
  198. auto glyph = maybe_glyph.value();
  199. if (event.shift())
  200. m_selection.extend_to(glyph);
  201. m_in_drag_select = true;
  202. m_automatic_selection_scroll_timer->start();
  203. set_active_glyph(glyph, event.shift() ? ShouldResetSelection::No : ShouldResetSelection::Yes);
  204. }
  205. }
  206. void GlyphMapWidget::mouseup_event(GUI::MouseEvent& event)
  207. {
  208. if (event.button() == MouseButton::Secondary)
  209. return;
  210. if (!m_in_drag_select)
  211. return;
  212. auto constrained = event.position().constrained(widget_inner_rect());
  213. if (auto maybe_glyph = glyph_at_position(constrained); maybe_glyph.has_value()) {
  214. auto glyph = maybe_glyph.value();
  215. m_selection.extend_to(glyph);
  216. set_active_glyph(glyph, ShouldResetSelection::No);
  217. }
  218. m_in_drag_select = false;
  219. }
  220. void GlyphMapWidget::mousemove_event(GUI::MouseEvent& event)
  221. {
  222. m_last_mousemove_position = event.position();
  223. }
  224. void GlyphMapWidget::doubleclick_event(MouseEvent& event)
  225. {
  226. if (on_glyph_double_clicked) {
  227. if (auto maybe_glyph = glyph_at_position(event.position()); maybe_glyph.has_value())
  228. on_glyph_double_clicked(maybe_glyph.value());
  229. }
  230. }
  231. void GlyphMapWidget::keydown_event(KeyEvent& event)
  232. {
  233. Widget::keydown_event(event);
  234. int range_offset = m_active_range.first;
  235. if (!event.ctrl() && !event.shift()) {
  236. m_selection.set_size(1);
  237. m_selection.set_start(m_active_glyph);
  238. }
  239. if (event.key() == KeyCode::Key_Up) {
  240. if (m_selection.start() - range_offset >= m_columns) {
  241. if (event.shift())
  242. m_selection.resize_by(-m_columns);
  243. else
  244. m_selection.set_start(m_selection.start() - m_columns);
  245. set_active_glyph(m_active_glyph - m_columns, ShouldResetSelection::No);
  246. scroll_to_glyph(m_active_glyph);
  247. return;
  248. }
  249. }
  250. if (event.key() == KeyCode::Key_Down) {
  251. if (m_selection.start() < m_glyph_count + range_offset - m_columns) {
  252. if (event.shift())
  253. m_selection.resize_by(m_columns);
  254. else
  255. m_selection.set_start(m_selection.start() + m_columns);
  256. set_active_glyph(m_active_glyph + m_columns, ShouldResetSelection::No);
  257. scroll_to_glyph(m_active_glyph);
  258. return;
  259. }
  260. }
  261. if (event.key() == KeyCode::Key_Left) {
  262. if (m_selection.start() > range_offset) {
  263. if (event.shift())
  264. m_selection.resize_by(-1);
  265. else
  266. m_selection.set_start(m_selection.start() - 1);
  267. set_active_glyph(m_active_glyph - 1, ShouldResetSelection::No);
  268. scroll_to_glyph(m_active_glyph);
  269. return;
  270. }
  271. }
  272. if (event.key() == KeyCode::Key_Right) {
  273. if (m_selection.start() < m_glyph_count + range_offset - 1) {
  274. if (event.shift())
  275. m_selection.resize_by(1);
  276. else
  277. m_selection.set_start(m_selection.start() + 1);
  278. set_active_glyph(m_active_glyph + 1, ShouldResetSelection::No);
  279. scroll_to_glyph(m_active_glyph);
  280. return;
  281. }
  282. }
  283. // FIXME: Support selection for these.
  284. if (event.ctrl() && event.key() == KeyCode::Key_Home) {
  285. set_active_glyph(m_active_range.first);
  286. scroll_to_glyph(m_active_glyph);
  287. return;
  288. }
  289. if (event.ctrl() && event.key() == KeyCode::Key_End) {
  290. set_active_glyph(m_active_range.last);
  291. scroll_to_glyph(m_active_glyph);
  292. return;
  293. }
  294. if (!event.ctrl() && event.key() == KeyCode::Key_Home) {
  295. auto start_of_row = (m_active_glyph - range_offset) / m_columns * m_columns;
  296. set_active_glyph(start_of_row + range_offset);
  297. return;
  298. }
  299. if (!event.ctrl() && event.key() == KeyCode::Key_End) {
  300. auto end_of_row = (m_active_glyph - range_offset) / m_columns * m_columns + (m_columns - 1);
  301. end_of_row = clamp(end_of_row + range_offset, m_active_range.first, m_active_range.last);
  302. set_active_glyph(end_of_row);
  303. return;
  304. }
  305. }
  306. void GlyphMapWidget::did_change_font()
  307. {
  308. recalculate_content_size();
  309. vertical_scrollbar().set_step(font().glyph_height() + m_vertical_spacing);
  310. }
  311. void GlyphMapWidget::scroll_to_glyph(int glyph)
  312. {
  313. glyph -= m_active_range.first;
  314. int row = glyph / columns();
  315. int column = glyph % columns();
  316. auto scroll_rect = Gfx::IntRect {
  317. column * (font().max_glyph_width() + m_horizontal_spacing) + 1,
  318. row * (font().glyph_height() + m_vertical_spacing) + 1,
  319. font().max_glyph_width() + m_horizontal_spacing,
  320. font().glyph_height() + m_horizontal_spacing
  321. };
  322. scroll_into_view(scroll_rect, true, true);
  323. }
  324. void GlyphMapWidget::select_previous_existing_glyph()
  325. {
  326. bool search_wrapped = false;
  327. int first_glyph = m_active_range.first;
  328. int last_glyph = m_active_range.last;
  329. for (int i = active_glyph() - 1;; --i) {
  330. if (i < first_glyph && !search_wrapped) {
  331. i = last_glyph;
  332. search_wrapped = true;
  333. } else if (i < first_glyph && search_wrapped) {
  334. break;
  335. }
  336. if (font().contains_glyph(i)) {
  337. set_focus(true);
  338. set_active_glyph(i);
  339. scroll_to_glyph(i);
  340. break;
  341. }
  342. }
  343. }
  344. void GlyphMapWidget::select_next_existing_glyph()
  345. {
  346. bool search_wrapped = false;
  347. int first_glyph = m_active_range.first;
  348. int last_glyph = m_active_range.last;
  349. for (int i = active_glyph() + 1;; ++i) {
  350. if (i > last_glyph && !search_wrapped) {
  351. i = first_glyph;
  352. search_wrapped = true;
  353. } else if (i > last_glyph && search_wrapped) {
  354. break;
  355. }
  356. if (font().contains_glyph(i)) {
  357. set_focus(true);
  358. set_active_glyph(i);
  359. scroll_to_glyph(i);
  360. break;
  361. }
  362. }
  363. }
  364. void GlyphMapWidget::recalculate_content_size()
  365. {
  366. auto inner_rect = frame_inner_rect();
  367. int event_width = inner_rect.width() - vertical_scrollbar().width() - m_horizontal_spacing;
  368. int event_height = inner_rect.height();
  369. m_visible_glyphs = (event_width * event_height) / (font().max_glyph_width() * font().glyph_height());
  370. m_columns = max(event_width / (font().max_glyph_width() + m_horizontal_spacing), 1);
  371. m_rows = ceil_div(m_glyph_count, m_columns);
  372. int content_width = columns() * (font().max_glyph_width() + m_horizontal_spacing);
  373. int content_height = rows() * (font().glyph_height() + m_vertical_spacing) + frame_thickness();
  374. set_content_size({ content_width, content_height });
  375. scroll_to_glyph(m_active_glyph);
  376. }
  377. void GlyphMapWidget::set_active_range(Unicode::CodePointRange range)
  378. {
  379. if (m_active_range.first == range.first && m_active_range.last == range.last)
  380. return;
  381. m_active_range = range;
  382. m_glyph_count = range.last - range.first + 1;
  383. set_active_glyph(range.first);
  384. recalculate_content_size();
  385. update();
  386. }
  387. void GlyphMapWidget::set_highlight_modifications(bool highlight_modifications)
  388. {
  389. if (m_highlight_modifications == highlight_modifications)
  390. return;
  391. m_highlight_modifications = highlight_modifications;
  392. update();
  393. }
  394. void GlyphMapWidget::set_glyph_modified(u32 glyph, bool modified)
  395. {
  396. if (modified)
  397. m_modified_glyphs.set(glyph);
  398. else
  399. m_modified_glyphs.remove(glyph);
  400. }
  401. bool GlyphMapWidget::glyph_is_modified(u32 glyph)
  402. {
  403. return m_modified_glyphs.contains(glyph);
  404. }
  405. void GlyphMapWidget::set_font(Gfx::Font const& font)
  406. {
  407. AbstractScrollableWidget::set_font(font);
  408. m_original_font = font.clone();
  409. m_modified_glyphs.clear();
  410. }
  411. }