| 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 <numeric> |
| 29 | #include <string> |
| 30 | #include <gtkmm.h> |
| 31 | |
| 32 | #include "simple_dialogs.hpp" |
| 33 | |
| 34 | #include "intl.h" |
| 35 | #include "application.hpp" |
| 36 | #include "bitmaps_dlg.hpp" |
| 37 | #include "dialog_base.hpp" |
| 38 | #include "language_list.hpp" |
| 39 | #include "utils.hpp" |
| 40 | |
| 41 | namespace ff::dlg { |
| 42 | |
| 43 | // A simple dialog to query the user for a number of new encoding slots to add. |
| 44 | class NumericalInputDialog final : public DialogBase { |
| 45 | private: |
| 46 | Gtk::SpinButton* input; |
| 47 | |
| 48 | NumericalInputDialog(GWindow parent, const std::string& title, |
| 49 | const std::string& label) |
| 50 | : DialogBase(parent) { |
| 51 | set_title(title); |
| 52 | set_resizable(false); |
| 53 | |
| 54 | auto label_widget = Gtk::make_managed<Gtk::Label>(label); |
| 55 | get_content_area()->pack_start(*label_widget); |
| 56 | |
| 57 | input = Gtk::make_managed<Gtk::SpinButton>(); |
| 58 | input->set_numeric(true); |
| 59 | input->set_adjustment(Gtk::Adjustment::create(1, 1, 65535, 1, 5, 0)); |
| 60 | input->set_activates_default(); |
| 61 | get_content_area()->pack_start(*input); |
| 62 | |
| 63 | show_all(); |
| 64 | } |
| 65 | |
| 66 | int get_value() const { return input->get_value_as_int(); } |
| 67 | |
| 68 | public: |
| 69 | // Show the dialog and return either the entered integer, 0 if the dialog |
| 70 | // was cancelled/closed, or -1 if something strange happened |
| 71 | static int show(GWindow parent, const std::string& title, |
| 72 | const std::string& label) { |
| 73 | NumericalInputDialog dialog(parent, title, label); |
| 74 | int i = -1; |
| 75 | dialog.signal_response().connect([&](int response_id) { |
| 76 | // Ok/Add returns the entered integer |
| 77 | if (response_id == Gtk::RESPONSE_OK) { |
| 78 | i = dialog.get_value(); |
| 79 | } |
| 80 | // Cancel/close returns 0, which is semantically equivalent to the |
| 81 | // user actually entering 0 |
| 82 | else if (response_id == Gtk::RESPONSE_CANCEL || |
| 83 | response_id == Gtk::RESPONSE_DELETE_EVENT || |
| 84 | response_id == Gtk::RESPONSE_CLOSE) { |
| 85 | i = 0; |
| 86 | } |
| 87 | // Any other response, which shouldn't really be possible, returns |
| 88 | // -1 to indicate an error |
| 89 | }); |
| 90 | dialog.run(); |
| 91 | return i; |
| 92 | } |
| 93 | }; |
| 94 | |
| 95 | } // namespace ff::dlg |
| 96 | |
| 97 | // Shim for the C code to call the dialog |
| 98 | int add_encoding_slots_dialog(GWindow parent, bool cid) { |
| 99 | // To avoid instability, the GTK application is lazily initialized only when |
| 100 | // a GTK window is invoked. |
| 101 | ff::app::GtkApp(); |
| 102 | |
| 103 | return ff::dlg::NumericalInputDialog::show( |
| 104 | parent, _("Add Encoding Slots..."), |
| 105 | cid ? _("How many CID slots do you wish to add?") |
| 106 | : _("How many unencoded glyph slots do you wish to add?")); |
| 107 | } |
| 108 | |
| 109 | char* language_list_dialog(GWindow parent, const LanguageRec* languages, |
| 110 | const char* initial_tags) { |
| 111 | // To avoid instability, the GTK application is lazily initialized only when |
| 112 | // a GTK window is invoked. |
| 113 | ff::app::GtkApp(); |
| 114 | |
| 115 | ff::dlg::LanguageRecords language_vec; |
| 116 | for (const LanguageRec* lang = languages; lang->name != nullptr; ++lang) { |
| 117 | language_vec.emplace_back(lang->name, lang->tag); |
| 118 | } |
| 119 | |
| 120 | // Compute initial selection |
| 121 | std::stringstream ss(initial_tags); |
| 122 | std::vector<int> tag_list; |
| 123 | std::vector<std::string> unrecognized_tags; |
| 124 | while (ss.good()) { |
| 125 | std::string substr; |
| 126 | std::getline(ss, substr, ','); |
| 127 | auto it = std::find_if( |
| 128 | language_vec.begin(), language_vec.end(), |
| 129 | [&substr](const auto& rec) { return (rec.second == substr); }); |
| 130 | if (it != language_vec.end()) { |
| 131 | tag_list.push_back(it - language_vec.begin()); |
| 132 | } else { |
| 133 | unrecognized_tags.push_back(substr); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | if (unrecognized_tags.size() == 1) |
| 138 | ff::ui_utils::post_error( |
| 139 | _("Unknown Language"), |
| 140 | _("The language, '%s', is not in the list of known " |
| 141 | "languages and will be omitted"), |
| 142 | unrecognized_tags[0].c_str()); |
| 143 | else if (unrecognized_tags.size() > 1) |
| 144 | ff::ui_utils::post_error( |
| 145 | _("Unknown Language"), |
| 146 | _("Several language tags, including '%s', are not in " |
| 147 | "the list of known languages and will be omitted"), |
| 148 | unrecognized_tags[0].c_str()); |
| 149 | |
| 150 | std::vector<int> selection = |
| 151 | ff::dlg::LanguageListDlg::show(parent, language_vec, tag_list); |
| 152 | |
| 153 | if (selection.empty()) { |
| 154 | return nullptr; |
| 155 | } else { |
| 156 | auto append_tag = [&language_vec](std::string a, int b) { |
| 157 | return std::move(a) + ',' + (const char*)language_vec[b].second; |
| 158 | }; |
| 159 | |
| 160 | // Comma-seperated tags |
| 161 | std::string s((const char*)language_vec[selection[0]].second); |
| 162 | s = std::accumulate(std::next(selection.begin()), selection.end(), |
| 163 | s, // start with first element |
| 164 | append_tag); |
| 165 | return strdup(s.c_str()); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | namespace ff::dlg { |
| 170 | |
| 171 | // A simple dialog to display properties as name-value pairs. |
| 172 | class ShowPropertiesDialog final : public DialogBase { |
| 173 | public: |
| 174 | Gtk::SpinButton* input; |
| 175 | |
| 176 | ShowPropertiesDialog(GWindow parent, const L10nText& title, |
| 177 | const PropertyVec& properties) |
| 178 | : DialogBase(parent) { |
| 179 | set_title(title); |
| 180 | set_resizable(false); |
| 181 | |
| 182 | auto main_grid = Gtk::make_managed<Gtk::Grid>(); |
| 183 | main_grid->set_column_spacing(ff::ui_utils::ui_font_em_size()); |
| 184 | |
| 185 | for (size_t i = 0; i < properties.size(); ++i) { |
| 186 | const auto& [label, value] = properties[i]; |
| 187 | |
| 188 | auto label_widget = Gtk::make_managed<Gtk::Label>(label); |
| 189 | label_widget->set_halign(Gtk::ALIGN_START); |
| 190 | main_grid->attach(*label_widget, 0, i); |
| 191 | |
| 192 | Glib::ustring value_str = value; |
| 193 | auto value_widget = Gtk::make_managed<Gtk::Label>(value_str); |
| 194 | value_widget->set_halign(Gtk::ALIGN_START); |
| 195 | main_grid->attach(*value_widget, 1, i); |
| 196 | |
| 197 | // Support clickable URLs in the value field |
| 198 | if (value_str.find("http://") == 0 || |
| 199 | value_str.find("https://") == 0) { |
| 200 | value_widget->set_markup( |
| 201 | "<a href=\"" + Glib::Markup::escape_text(value_str) + |
| 202 | "\">" + Glib::Markup::escape_text(value_str) + "</a>"); |
| 203 | value_widget->set_selectable(true); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | get_content_area()->pack_start(*main_grid); |
| 208 | show_all(); |
| 209 | remove_cancel_button(); |
| 210 | } |
| 211 | }; |
| 212 | |
| 213 | } // namespace ff::dlg |
| 214 | |
| 215 | void show_properties_dialog(GWindow parent, const L10nText& title, |
| 216 | const PropertyVec& properties) { |
| 217 | // To avoid instability, the GTK application is lazily initialized only when |
| 218 | // a GTK window is invoked. |
| 219 | ff::app::GtkApp(); |
| 220 | |
| 221 | ff::dlg::ShowPropertiesDialog dlg(parent, title, properties); |
| 222 | dlg.run(); |
| 223 | } |
| 224 | |
| 225 | void update_appearance() { |
| 226 | ff::app::GtkApp(); |
| 227 | ff::app::load_legacy_style(); |
| 228 | } |