Debian Codegraph @

fontforgeexe/gtk/widgets/verified_entry.cpp

raw symbols SCIP
1 /* Copyright 2025 Maxim 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 "verified_entry.hpp"
29
30 namespace ff::widgets {
31
32 VerifiedEntry::VerifiedEntry() {
33 error_css_provider_ = Gtk::CssProvider::create();
34 error_css_provider_->load_from_data(
35 "entry { color: @error_color; border-color: @error_color}");
36
37 // Remove the error highlight when the user focuses on the widget, to
38 // prevent visual annoyance during the edit.
39 signal_focus_in_event().connect(
40 sigc::mem_fun(*this, &VerifiedEntry::focus_in_event_slot));
41
42 // Also remove the error highlight when contents change.
43 signal_changed().connect(
44 sigc::mem_fun(*this, &VerifiedEntry::changed_slot));
45 }
46
47 bool VerifiedEntry::verify() {
48 int start_pos = 0, end_pos = 0;
49 Glib::ustring text = get_text();
50 bool contents_valid = verifier_(text, start_pos, end_pos);
51 if (!contents_valid) {
52 set_error_state();
53 select_region(start_pos, end_pos);
54
55 // This widget is designed to clear highlight when the user focuses on
56 // it to fix the value. The focus is not always lost (e.g. when pressing
57 // Enter to close the dialog), so we force it to later trigger the
58 // focus-in.
59 Gtk::Window* top = dynamic_cast<Gtk::Window*>(get_toplevel());
60 if (top) top->unset_focus();
61 } else {
62 clear_error_state();
63 }
64 return contents_valid;
65 }
66
67 bool VerifiedEntry::focus_in_event_slot(GdkEventFocus*) {
68 clear_error_state();
69 return false;
70 }
71
72 void VerifiedEntry::changed_slot() { clear_error_state(); }
73
74 void VerifiedEntry::set_error_state() {
75 // Special CSS has higher priority than the legacy GDraw compatibility
76 // CSS, but lower than the GTK inspector CSS.
77 get_style_context()->add_provider(error_css_provider_,
78 GTK_STYLE_PROVIDER_PRIORITY_USER - 1);
79 set_icon_from_icon_name("dialog-warning-symbolic",
80 Gtk::ENTRY_ICON_SECONDARY);
81 }
82
83 void VerifiedEntry::clear_error_state() {
84 get_style_context()->remove_provider(error_css_provider_);
85 unset_icon(Gtk::ENTRY_ICON_SECONDARY);
86 }
87
88 } // namespace ff::widgets