GlyphMapWidget.cpp 13 KB

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