CharacterMapWidget.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "CharacterMapWidget.h"
  7. #include <AK/StringUtils.h>
  8. #include <Applications/CharacterMap/CharacterMapWindowGML.h>
  9. #include <LibConfig/Client.h>
  10. #include <LibGUI/Action.h>
  11. #include <LibGUI/Application.h>
  12. #include <LibGUI/Clipboard.h>
  13. #include <LibGUI/FontPicker.h>
  14. #include <LibGUI/Icon.h>
  15. #include <LibGUI/InputBox.h>
  16. #include <LibGUI/Label.h>
  17. #include <LibGUI/Menu.h>
  18. #include <LibGUI/TextBox.h>
  19. #include <LibGUI/Toolbar.h>
  20. #include <LibUnicode/CharacterTypes.h>
  21. CharacterMapWidget::CharacterMapWidget()
  22. {
  23. load_from_gml(character_map_window_gml);
  24. m_toolbar = find_descendant_of_type_named<GUI::Toolbar>("toolbar");
  25. m_font_name_label = find_descendant_of_type_named<GUI::Label>("font_name");
  26. m_glyph_map = find_descendant_of_type_named<GUI::GlyphMapWidget>("glyph_map");
  27. m_output_box = find_descendant_of_type_named<GUI::TextBox>("output_box");
  28. m_copy_output_button = find_descendant_of_type_named<GUI::Button>("copy_output_button");
  29. m_statusbar = find_descendant_of_type_named<GUI::Statusbar>("statusbar");
  30. m_choose_font_action = GUI::Action::create("Choose Font...", Gfx::Bitmap::try_load_from_file("/res/icons/16x16/app-font-editor.png").release_value_but_fixme_should_propagate_errors(), [&](auto&) {
  31. auto font_picker = GUI::FontPicker::construct(window(), &font(), false);
  32. if (font_picker->exec() == GUI::Dialog::ExecOK) {
  33. auto& font = *font_picker->font();
  34. Config::write_string("CharacterMap", "History", "Font", font.qualified_name());
  35. set_font(font);
  36. }
  37. });
  38. m_copy_selection_action = GUI::CommonActions::make_copy_action([&](GUI::Action&) {
  39. auto selection = m_glyph_map->selection();
  40. StringBuilder builder;
  41. for (auto code_point = selection.start(); code_point < selection.start() + selection.size(); ++code_point) {
  42. if (!m_glyph_map->font().contains_glyph(code_point))
  43. continue;
  44. builder.append_code_point(code_point);
  45. }
  46. GUI::Clipboard::the().set_plain_text(builder.to_string());
  47. });
  48. m_copy_selection_action->set_status_tip("Copy the highlighted characters to the clipboard");
  49. m_previous_glyph_action = GUI::Action::create("Previous character", { Mod_Alt, Key_Left }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/go-back.png").release_value_but_fixme_should_propagate_errors(), [&](auto&) {
  50. m_glyph_map->select_previous_existing_glyph();
  51. });
  52. m_previous_glyph_action->set_status_tip("Seek the previous visible glyph");
  53. m_next_glyph_action = GUI::Action::create("&Next Glyph", { Mod_Alt, Key_Right }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/go-forward.png").release_value_but_fixme_should_propagate_errors(), [&](auto&) {
  54. m_glyph_map->select_next_existing_glyph();
  55. });
  56. m_next_glyph_action->set_status_tip("Seek the next visible glyph");
  57. m_go_to_glyph_action = GUI::Action::create("Go to glyph...", { Mod_Ctrl, Key_G }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/go-to.png").release_value_but_fixme_should_propagate_errors(), [&](auto&) {
  58. String input;
  59. if (GUI::InputBox::show(window(), input, "Hexadecimal:", "Go to glyph") == GUI::InputBox::ExecOK && !input.is_empty()) {
  60. auto maybe_code_point = AK::StringUtils::convert_to_uint_from_hex(input);
  61. if (!maybe_code_point.has_value())
  62. return;
  63. auto code_point = clamp(maybe_code_point.value(), 0x0000, 0x10FFFF);
  64. m_glyph_map->set_focus(true);
  65. m_glyph_map->set_active_glyph(code_point);
  66. m_glyph_map->scroll_to_glyph(code_point);
  67. }
  68. });
  69. m_go_to_glyph_action->set_status_tip("Go to the specified code point");
  70. m_toolbar->add_action(*m_choose_font_action);
  71. m_toolbar->add_separator();
  72. m_toolbar->add_action(*m_copy_selection_action);
  73. m_toolbar->add_separator();
  74. m_toolbar->add_action(*m_previous_glyph_action);
  75. m_toolbar->add_action(*m_next_glyph_action);
  76. m_toolbar->add_action(*m_go_to_glyph_action);
  77. m_glyph_map->on_active_glyph_changed = [&](int) {
  78. update_statusbar();
  79. };
  80. m_glyph_map->on_glyph_double_clicked = [&](int code_point) {
  81. StringBuilder builder;
  82. builder.append(m_output_box->text());
  83. builder.append_code_point(code_point);
  84. m_output_box->set_text(builder.string_view());
  85. };
  86. m_copy_output_button->on_click = [&](auto) {
  87. GUI::Clipboard::the().set_plain_text(m_output_box->text());
  88. };
  89. did_change_font();
  90. update_statusbar();
  91. }
  92. CharacterMapWidget::~CharacterMapWidget()
  93. {
  94. }
  95. void CharacterMapWidget::initialize_menubar(GUI::Window& window)
  96. {
  97. auto& file_menu = window.add_menu("&File");
  98. file_menu.add_action(GUI::CommonActions::make_quit_action([](GUI::Action&) {
  99. GUI::Application::the()->quit();
  100. }));
  101. auto& help_menu = window.add_menu("&Help");
  102. help_menu.add_action(GUI::CommonActions::make_about_action("Character Map", GUI::Icon::default_icon("app-keyboard-settings"), &window));
  103. }
  104. void CharacterMapWidget::did_change_font()
  105. {
  106. m_glyph_map->set_font(font());
  107. m_font_name_label->set_text(font().qualified_name());
  108. m_output_box->set_font(font());
  109. }
  110. void CharacterMapWidget::update_statusbar()
  111. {
  112. auto code_point = m_glyph_map->active_glyph();
  113. StringBuilder builder;
  114. builder.appendff("U+{:04X}", code_point);
  115. if (auto display_name = Unicode::code_point_display_name(code_point); display_name.has_value())
  116. builder.appendff(" - {}", display_name.value());
  117. m_statusbar->set_text(builder.to_string());
  118. }