GlyphMapWidget.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include "GlyphMapWidget.h"
  9. #include <LibGUI/Painter.h>
  10. #include <LibGfx/BitmapFont.h>
  11. #include <LibGfx/Emoji.h>
  12. #include <LibGfx/Palette.h>
  13. REGISTER_WIDGET(GUI, GlyphMapWidget);
  14. namespace GUI {
  15. GlyphMapWidget::Selection GlyphMapWidget::Selection::normalized() const
  16. {
  17. if (m_size > 0)
  18. return *this;
  19. return { m_start + m_size, -m_size + 1 };
  20. }
  21. void GlyphMapWidget::Selection::resize_by(int i)
  22. {
  23. m_size += i;
  24. if (m_size == 0) {
  25. if (i < 0)
  26. m_size--;
  27. else
  28. m_size++;
  29. }
  30. }
  31. bool GlyphMapWidget::Selection::contains(int i) const
  32. {
  33. auto this_normalized = normalized();
  34. return i >= this_normalized.m_start && i < this_normalized.m_start + this_normalized.m_size;
  35. }
  36. void GlyphMapWidget::Selection::extend_to(int glyph)
  37. {
  38. m_size = glyph - m_start;
  39. if (m_size > 0)
  40. m_size++;
  41. }
  42. GlyphMapWidget::GlyphMapWidget()
  43. {
  44. set_focus_policy(FocusPolicy::StrongFocus);
  45. horizontal_scrollbar().set_visible(false);
  46. did_change_font();
  47. set_active_glyph('A');
  48. }
  49. GlyphMapWidget::~GlyphMapWidget()
  50. {
  51. }
  52. void GlyphMapWidget::resize_event(ResizeEvent& event)
  53. {
  54. recalculate_content_size();
  55. AbstractScrollableWidget::resize_event(event);
  56. }
  57. void GlyphMapWidget::set_active_glyph(int glyph, ShouldResetSelection should_reset_selection)
  58. {
  59. if (m_active_glyph == glyph)
  60. return;
  61. m_active_glyph = glyph;
  62. if (should_reset_selection == ShouldResetSelection::Yes) {
  63. m_selection.set_start(glyph);
  64. m_selection.set_size(1);
  65. }
  66. if (on_active_glyph_changed)
  67. on_active_glyph_changed(glyph);
  68. update();
  69. }
  70. Gfx::IntRect GlyphMapWidget::get_outer_rect(int glyph) const
  71. {
  72. glyph -= m_active_range.first;
  73. int row = glyph / columns();
  74. int column = glyph % columns();
  75. return Gfx::IntRect {
  76. column * (font().max_glyph_width() + m_horizontal_spacing) + 1,
  77. row * (font().glyph_height() + m_vertical_spacing) + 1,
  78. font().max_glyph_width() + m_horizontal_spacing,
  79. font().glyph_height() + m_horizontal_spacing
  80. }
  81. .translated(frame_thickness() - horizontal_scrollbar().value(), frame_thickness() - vertical_scrollbar().value());
  82. }
  83. void GlyphMapWidget::update_glyph(int glyph)
  84. {
  85. update(get_outer_rect(glyph));
  86. }
  87. void GlyphMapWidget::paint_event(PaintEvent& event)
  88. {
  89. Frame::paint_event(event);
  90. Painter painter(*this);
  91. painter.add_clip_rect(widget_inner_rect());
  92. painter.add_clip_rect(event.rect());
  93. painter.set_font(font());
  94. painter.fill_rect(widget_inner_rect(), palette().inactive_window_title());
  95. int scroll_steps = vertical_scrollbar().value() / vertical_scrollbar().step();
  96. int first_visible_glyph = scroll_steps * columns();
  97. int range_offset = m_active_range.first;
  98. int last_glyph = m_active_range.last + 1;
  99. for (int glyph = first_visible_glyph + range_offset; glyph <= first_visible_glyph + m_visible_glyphs + range_offset && glyph < last_glyph; ++glyph) {
  100. Gfx::IntRect outer_rect = get_outer_rect(glyph);
  101. Gfx::IntRect inner_rect(
  102. outer_rect.x() + m_horizontal_spacing / 2,
  103. outer_rect.y() + m_vertical_spacing / 2,
  104. font().max_glyph_width(),
  105. font().glyph_height());
  106. if (m_selection.contains(glyph)) {
  107. painter.fill_rect(outer_rect, is_focused() ? palette().selection() : palette().inactive_selection());
  108. if (font().contains_glyph(glyph))
  109. painter.draw_glyph(inner_rect.location(), glyph, is_focused() ? palette().selection_text() : palette().inactive_selection_text());
  110. else if (auto* emoji = Gfx::Emoji::emoji_for_code_point(glyph))
  111. painter.draw_emoji(inner_rect.location(), *emoji, font());
  112. } else if (font().contains_glyph(glyph)) {
  113. painter.fill_rect(outer_rect, palette().base());
  114. painter.draw_glyph(inner_rect.location(), glyph, palette().base_text());
  115. } else if (auto* emoji = Gfx::Emoji::emoji_for_code_point(glyph)) {
  116. painter.fill_rect(outer_rect, Gfx::Color { 255, 150, 150 });
  117. painter.draw_emoji(inner_rect.location(), *emoji, font());
  118. }
  119. }
  120. painter.draw_focus_rect(get_outer_rect(m_active_glyph), Gfx::Color::Black);
  121. }
  122. Optional<int> GlyphMapWidget::glyph_at_position(Gfx::IntPoint position) const
  123. {
  124. Gfx::IntPoint map_offset { frame_thickness() - horizontal_scrollbar().value(), frame_thickness() - vertical_scrollbar().value() };
  125. auto map_position = position - map_offset;
  126. auto col = (map_position.x() - 1) / ((font().max_glyph_width() + m_horizontal_spacing));
  127. auto row = (map_position.y() - 1) / ((font().glyph_height() + m_vertical_spacing));
  128. auto glyph = row * columns() + col + m_active_range.first;
  129. if (row >= 0 && row < rows() && col >= 0 && col < columns() && glyph < m_glyph_count + m_active_range.first)
  130. return glyph;
  131. return {};
  132. }
  133. void GlyphMapWidget::mousedown_event(MouseEvent& event)
  134. {
  135. Frame::mousedown_event(event);
  136. if (auto maybe_glyph = glyph_at_position(event.position()); maybe_glyph.has_value()) {
  137. auto glyph = maybe_glyph.value();
  138. if (event.shift())
  139. m_selection.extend_to(glyph);
  140. else {
  141. m_selection.set_size(1);
  142. m_selection.set_start(glyph);
  143. }
  144. m_in_drag_select = true;
  145. set_active_glyph(glyph, ShouldResetSelection::No);
  146. }
  147. }
  148. void GlyphMapWidget::mouseup_event(GUI::MouseEvent& event)
  149. {
  150. Frame::mouseup_event(event);
  151. if (!m_in_drag_select)
  152. return;
  153. if (auto maybe_glyph = glyph_at_position(event.position()); maybe_glyph.has_value()) {
  154. auto glyph = maybe_glyph.value();
  155. m_selection.extend_to(glyph);
  156. m_in_drag_select = false;
  157. set_active_glyph(glyph, ShouldResetSelection::No);
  158. }
  159. }
  160. void GlyphMapWidget::mousemove_event(GUI::MouseEvent& event)
  161. {
  162. Frame::mousemove_event(event);
  163. if (!m_in_drag_select)
  164. return;
  165. if (auto maybe_glyph = glyph_at_position(event.position()); maybe_glyph.has_value()) {
  166. auto glyph = maybe_glyph.value();
  167. m_selection.extend_to(glyph);
  168. scroll_to_glyph(glyph);
  169. update();
  170. }
  171. }
  172. void GlyphMapWidget::doubleclick_event(MouseEvent& event)
  173. {
  174. Widget::doubleclick_event(event);
  175. if (on_glyph_double_clicked) {
  176. if (auto maybe_glyph = glyph_at_position(event.position()); maybe_glyph.has_value())
  177. on_glyph_double_clicked(maybe_glyph.value());
  178. }
  179. }
  180. void GlyphMapWidget::keydown_event(KeyEvent& event)
  181. {
  182. Frame::keydown_event(event);
  183. int range_offset = m_active_range.first;
  184. if (!event.ctrl() && !event.shift()) {
  185. m_selection.set_size(1);
  186. m_selection.set_start(m_active_glyph);
  187. }
  188. if (event.key() == KeyCode::Key_Up) {
  189. if (m_selection.start() - range_offset >= m_columns) {
  190. if (event.shift())
  191. m_selection.resize_by(-m_columns);
  192. else
  193. m_selection.set_start(m_selection.start() - m_columns);
  194. set_active_glyph(m_active_glyph - m_columns, ShouldResetSelection::No);
  195. scroll_to_glyph(m_active_glyph);
  196. return;
  197. }
  198. }
  199. if (event.key() == KeyCode::Key_Down) {
  200. if (m_selection.start() < m_glyph_count + range_offset - m_columns) {
  201. if (event.shift())
  202. m_selection.resize_by(m_columns);
  203. else
  204. m_selection.set_start(m_selection.start() + m_columns);
  205. set_active_glyph(m_active_glyph + m_columns, ShouldResetSelection::No);
  206. scroll_to_glyph(m_active_glyph);
  207. return;
  208. }
  209. }
  210. if (event.key() == KeyCode::Key_Left) {
  211. if (m_selection.start() > range_offset) {
  212. if (event.shift())
  213. m_selection.resize_by(-1);
  214. else
  215. m_selection.set_start(m_selection.start() - 1);
  216. set_active_glyph(m_active_glyph - 1, ShouldResetSelection::No);
  217. scroll_to_glyph(m_active_glyph);
  218. return;
  219. }
  220. }
  221. if (event.key() == KeyCode::Key_Right) {
  222. if (m_selection.start() < m_glyph_count + range_offset - 1) {
  223. if (event.shift())
  224. m_selection.resize_by(1);
  225. else
  226. m_selection.set_start(m_selection.start() + 1);
  227. set_active_glyph(m_active_glyph + 1, ShouldResetSelection::No);
  228. scroll_to_glyph(m_active_glyph);
  229. return;
  230. }
  231. }
  232. // FIXME: Support selection for these.
  233. if (event.ctrl() && event.key() == KeyCode::Key_Home) {
  234. set_active_glyph(m_active_range.first);
  235. scroll_to_glyph(m_active_glyph);
  236. return;
  237. }
  238. if (event.ctrl() && event.key() == KeyCode::Key_End) {
  239. set_active_glyph(m_active_range.last);
  240. scroll_to_glyph(m_active_glyph);
  241. return;
  242. }
  243. if (!event.ctrl() && event.key() == KeyCode::Key_Home) {
  244. auto start_of_row = (m_active_glyph - range_offset) / m_columns * m_columns;
  245. set_active_glyph(start_of_row + range_offset);
  246. return;
  247. }
  248. if (!event.ctrl() && event.key() == KeyCode::Key_End) {
  249. auto end_of_row = (m_active_glyph - range_offset) / m_columns * m_columns + (m_columns - 1);
  250. end_of_row = clamp(end_of_row + range_offset, m_active_range.first, m_active_range.last);
  251. set_active_glyph(end_of_row);
  252. return;
  253. }
  254. }
  255. void GlyphMapWidget::did_change_font()
  256. {
  257. recalculate_content_size();
  258. vertical_scrollbar().set_step(font().glyph_height() + m_vertical_spacing);
  259. }
  260. void GlyphMapWidget::scroll_to_glyph(int glyph)
  261. {
  262. glyph -= m_active_range.first;
  263. int row = glyph / columns();
  264. int column = glyph % columns();
  265. auto scroll_rect = Gfx::IntRect {
  266. column * (font().max_glyph_width() + m_horizontal_spacing) + 1,
  267. row * (font().glyph_height() + m_vertical_spacing) + 1,
  268. font().max_glyph_width() + m_horizontal_spacing,
  269. font().glyph_height() + m_horizontal_spacing
  270. };
  271. scroll_into_view(scroll_rect, true, true);
  272. }
  273. void GlyphMapWidget::select_previous_existing_glyph()
  274. {
  275. bool search_wrapped = false;
  276. int first_glyph = m_active_range.first;
  277. int last_glyph = m_active_range.last;
  278. for (int i = active_glyph() - 1;; --i) {
  279. if (i < first_glyph && !search_wrapped) {
  280. i = last_glyph;
  281. search_wrapped = true;
  282. } else if (i < first_glyph && search_wrapped) {
  283. break;
  284. }
  285. if (font().contains_glyph(i)) {
  286. set_focus(true);
  287. set_active_glyph(i);
  288. scroll_to_glyph(i);
  289. break;
  290. }
  291. }
  292. }
  293. void GlyphMapWidget::select_next_existing_glyph()
  294. {
  295. bool search_wrapped = false;
  296. int first_glyph = m_active_range.first;
  297. int last_glyph = m_active_range.last;
  298. for (int i = active_glyph() + 1;; ++i) {
  299. if (i > last_glyph && !search_wrapped) {
  300. i = first_glyph;
  301. search_wrapped = true;
  302. } else if (i > last_glyph && search_wrapped) {
  303. break;
  304. }
  305. if (font().contains_glyph(i)) {
  306. set_focus(true);
  307. set_active_glyph(i);
  308. scroll_to_glyph(i);
  309. break;
  310. }
  311. }
  312. }
  313. void GlyphMapWidget::recalculate_content_size()
  314. {
  315. auto inner_rect = frame_inner_rect();
  316. int event_width = inner_rect.width() - vertical_scrollbar().width() - m_horizontal_spacing;
  317. int event_height = inner_rect.height();
  318. m_visible_glyphs = (event_width * event_height) / (font().max_glyph_width() * font().glyph_height());
  319. m_columns = max(event_width / (font().max_glyph_width() + m_horizontal_spacing), 1);
  320. m_rows = ceil_div(m_glyph_count, m_columns);
  321. int content_width = columns() * (font().max_glyph_width() + m_horizontal_spacing);
  322. int content_height = rows() * (font().glyph_height() + m_vertical_spacing) + frame_thickness();
  323. set_content_size({ content_width, content_height });
  324. scroll_to_glyph(m_active_glyph);
  325. }
  326. void GlyphMapWidget::set_active_range(Unicode::CodePointRange range)
  327. {
  328. if (m_active_range.first == range.first && m_active_range.last == range.last)
  329. return;
  330. m_active_range = range;
  331. m_glyph_count = range.last - range.first + 1;
  332. set_active_glyph(range.first);
  333. recalculate_content_size();
  334. update();
  335. }
  336. }