CharacterMapWidget.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 "CharacterSearchWidget.h"
  8. #include <AK/StringUtils.h>
  9. #include <Applications/CharacterMap/CharacterMapWindowGML.h>
  10. #include <LibConfig/Client.h>
  11. #include <LibDesktop/Launcher.h>
  12. #include <LibGUI/Action.h>
  13. #include <LibGUI/Application.h>
  14. #include <LibGUI/Clipboard.h>
  15. #include <LibGUI/FontPicker.h>
  16. #include <LibGUI/Icon.h>
  17. #include <LibGUI/InputBox.h>
  18. #include <LibGUI/Label.h>
  19. #include <LibGUI/Menu.h>
  20. #include <LibGUI/TextBox.h>
  21. #include <LibGUI/Toolbar.h>
  22. #include <LibUnicode/CharacterTypes.h>
  23. CharacterMapWidget::CharacterMapWidget()
  24. {
  25. load_from_gml(character_map_window_gml);
  26. m_toolbar = find_descendant_of_type_named<GUI::Toolbar>("toolbar");
  27. m_font_name_label = find_descendant_of_type_named<GUI::Label>("font_name");
  28. m_glyph_map = find_descendant_of_type_named<GUI::GlyphMapWidget>("glyph_map");
  29. m_output_box = find_descendant_of_type_named<GUI::TextBox>("output_box");
  30. m_copy_output_button = find_descendant_of_type_named<GUI::Button>("copy_output_button");
  31. m_statusbar = find_descendant_of_type_named<GUI::Statusbar>("statusbar");
  32. 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&) {
  33. auto font_picker = GUI::FontPicker::construct(window(), &font(), false);
  34. if (font_picker->exec() == GUI::Dialog::ExecOK) {
  35. auto& font = *font_picker->font();
  36. Config::write_string("CharacterMap", "History", "Font", font.qualified_name());
  37. set_font(font);
  38. }
  39. });
  40. m_copy_selection_action = GUI::CommonActions::make_copy_action([&](GUI::Action&) {
  41. auto selection = m_glyph_map->selection();
  42. StringBuilder builder;
  43. for (auto code_point = selection.start(); code_point < selection.start() + selection.size(); ++code_point) {
  44. if (!m_glyph_map->font().contains_glyph(code_point))
  45. continue;
  46. builder.append_code_point(code_point);
  47. }
  48. GUI::Clipboard::the().set_plain_text(builder.to_string());
  49. });
  50. m_copy_selection_action->set_status_tip("Copy the highlighted characters to the clipboard");
  51. 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&) {
  52. m_glyph_map->select_previous_existing_glyph();
  53. });
  54. m_previous_glyph_action->set_status_tip("Seek the previous visible glyph");
  55. 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&) {
  56. m_glyph_map->select_next_existing_glyph();
  57. });
  58. m_next_glyph_action->set_status_tip("Seek the next visible glyph");
  59. 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&) {
  60. String input;
  61. if (GUI::InputBox::show(window(), input, "Hexadecimal:", "Go to glyph") == GUI::InputBox::ExecOK && !input.is_empty()) {
  62. auto maybe_code_point = AK::StringUtils::convert_to_uint_from_hex(input);
  63. if (!maybe_code_point.has_value())
  64. return;
  65. auto code_point = clamp(maybe_code_point.value(), 0x0000, 0x10FFFF);
  66. m_glyph_map->set_focus(true);
  67. m_glyph_map->set_active_glyph(code_point);
  68. m_glyph_map->scroll_to_glyph(code_point);
  69. }
  70. });
  71. m_go_to_glyph_action->set_status_tip("Go to the specified code point");
  72. m_find_glyphs_action = GUI::Action::create("&Find glyphs...", { Mod_Ctrl, Key_F }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/find.png").release_value_but_fixme_should_propagate_errors(), [&](auto&) {
  73. if (m_find_window.is_null()) {
  74. m_find_window = GUI::Window::construct(window());
  75. auto& search_widget = m_find_window->set_main_widget<CharacterSearchWidget>();
  76. search_widget.on_character_selected = [&](auto code_point) {
  77. m_glyph_map->set_active_glyph(code_point);
  78. m_glyph_map->scroll_to_glyph(code_point);
  79. };
  80. m_find_window->set_icon(GUI::Icon::try_create_default_icon("find").value().bitmap_for_size(16));
  81. m_find_window->set_title("Find a character");
  82. m_find_window->resize(300, 400);
  83. }
  84. m_find_window->show();
  85. m_find_window->move_to_front();
  86. m_find_window->find_descendant_of_type_named<GUI::TextBox>("search_input")->set_focus(true);
  87. });
  88. m_toolbar->add_action(*m_choose_font_action);
  89. m_toolbar->add_separator();
  90. m_toolbar->add_action(*m_copy_selection_action);
  91. m_toolbar->add_separator();
  92. m_toolbar->add_action(*m_previous_glyph_action);
  93. m_toolbar->add_action(*m_next_glyph_action);
  94. m_toolbar->add_action(*m_go_to_glyph_action);
  95. m_toolbar->add_action(*m_find_glyphs_action);
  96. m_glyph_map->on_active_glyph_changed = [&](int) {
  97. update_statusbar();
  98. };
  99. m_glyph_map->on_glyph_double_clicked = [&](int code_point) {
  100. StringBuilder builder;
  101. builder.append(m_output_box->text());
  102. builder.append_code_point(code_point);
  103. m_output_box->set_text(builder.string_view());
  104. };
  105. m_copy_output_button->on_click = [&](auto) {
  106. GUI::Clipboard::the().set_plain_text(m_output_box->text());
  107. };
  108. did_change_font();
  109. update_statusbar();
  110. }
  111. CharacterMapWidget::~CharacterMapWidget()
  112. {
  113. }
  114. void CharacterMapWidget::initialize_menubar(GUI::Window& window)
  115. {
  116. auto& file_menu = window.add_menu("&File");
  117. file_menu.add_action(GUI::CommonActions::make_quit_action([](GUI::Action&) {
  118. GUI::Application::the()->quit();
  119. }));
  120. auto& help_menu = window.add_menu("&Help");
  121. help_menu.add_action(GUI::CommonActions::make_help_action([&](auto&) {
  122. Desktop::Launcher::open(URL::create_with_file_protocol("/usr/share/man/man1/CharacterMap.md"), "/bin/Help");
  123. }));
  124. help_menu.add_action(GUI::CommonActions::make_about_action("Character Map", GUI::Icon::default_icon("app-character-map"), &window));
  125. }
  126. void CharacterMapWidget::did_change_font()
  127. {
  128. m_glyph_map->set_font(font());
  129. m_font_name_label->set_text(font().human_readable_name());
  130. m_output_box->set_font(font());
  131. }
  132. void CharacterMapWidget::update_statusbar()
  133. {
  134. auto code_point = m_glyph_map->active_glyph();
  135. StringBuilder builder;
  136. builder.appendff("U+{:04X}", code_point);
  137. if (auto display_name = Unicode::code_point_display_name(code_point); display_name.has_value())
  138. builder.appendff(" - {}", display_name.value());
  139. m_statusbar->set_text(builder.to_string());
  140. }