Debian Codegraph @

fontforgeexe/gtk/language_list.cpp

raw symbols SCIP
1 /* Copyright (C) 2025 by Maxim Iorsh <iorsh@[email protected]>
2 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are met:
5
6 * Redistributions of source code must retain the above copyright notice, this
7 * list of conditions and the following disclaimer.
8
9 * Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12
13 * The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include "language_list.hpp"
29
30 #include "utils.hpp"
31 #include "intl.h"
32
33 namespace ff::dlg {
34
35 LanguageListDlg::LanguageListDlg(GWindow parent,
36 const ff::dlg::LanguageRecords& lang_recs,
37 const std::vector<int>& initial_selection)
38 : DialogBase(parent), list_(1, false, Gtk::SELECTION_MULTIPLE) {
39 set_title(_("Language List"));
40 set_help_context("ui/dialogs/lookups.html", "#lookups-scripts-dlg");
41
42 auto scrolled_window = Gtk::make_managed<Gtk::ScrolledWindow>();
43 scrolled_window->set_policy(Gtk::POLICY_NEVER, Gtk::POLICY_AUTOMATIC);
44
45 auto search_entry = Gtk::make_managed<Gtk::SearchEntry>();
46 // The default behavior resets the current selection and selects the
47 // matching row. Since we have a multiple-selection list, we want to just
48 // scroll to the matching row.
49 search_entry->signal_search_changed().connect([search_entry, this]() {
50 Glib::ustring search_text = search_entry->get_text();
51 // Look for matching row, assuming the rows are sorted
52 // alphabetically.
53 int row_idx = 0;
54 for (; row_idx < list_.size(); ++row_idx) {
55 if (search_text.lowercase() < list_.get_text(row_idx).lowercase())
56 break;
57 }
58 list_.scroll_to_row(Gtk::TreePath(std::to_string(row_idx)), 0.0);
59 });
60
61 for (const auto& [name, tag] : lang_recs) {
62 list_.append(name);
63 }
64 list_.set_headers_visible(false);
65 list_.set_enable_search(false);
66 list_.set_tooltip_text(
67 _("Select as many languages as needed\n"
68 "Hold down the control key when clicking\n"
69 "to make disjoint selections."));
70
71 // Set initial selection
72 for (int idx : initial_selection) {
73 list_.get_selection()->select(Gtk::TreePath(std::to_string(idx)));
74 }
75
76 // Show initially roughly 8 rows.
77 property_default_height() = 25 * ui_utils::ui_font_eX_size();
78
79 scrolled_window->add(list_);
80
81 get_content_area()->pack_start(*search_entry, Gtk::PACK_SHRINK);
82 get_content_area()->pack_start(*scrolled_window, Gtk::PACK_EXPAND_WIDGET);
83
84 // Connect this signal before the normal Ok/Cancel processing slot, to
85 // prevent dialog closing if we wish.
86 signal_response().connect(
87 [this](int response_id) {
88 if (response_id == Gtk::RESPONSE_OK) {
89 if (list_.get_selected().empty()) {
90 ui_utils::post_error(
91 _("Language Missing"),
92 _("You must select at least one language.\nUse the "
93 "\"Default\" language if nothing else fits."));
94 signal_response().emission_stop();
95 }
96 }
97 },
98 false);
99
100 signal_realize().connect([this]() { list_.grab_focus(); });
101
102 show_all();
103 }
104
105 std::vector<int> LanguageListDlg::show(
106 GWindow parent, const ff::dlg::LanguageRecords& lang_recs,
107 const std::vector<int>& initial_selection) {
108 LanguageListDlg dialog(parent, lang_recs, initial_selection);
109
110 Gtk::ResponseType result = dialog.run();
111 std::vector<int> selection;
112
113 if (result == Gtk::RESPONSE_OK) {
114 selection = dialog.get_selection();
115 }
116 return selection;
117 }
118
119 } // namespace ff::dlg