Debian Codegraph @

fontforgeexe/gtk/widgets/num_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 "num_entry.hpp"
29
30 #include <cstring>
31
32 namespace ff::widgets {
33
34 NumericalEntry::NumericalEntry() {
35 signal_insert_text().connect(
36 sigc::mem_fun(*this, &NumericalEntry::validate_text_cb), false);
37 }
38
39 void NumericalEntry::validate_text_cb(const Glib::ustring& text,
40 int* position) {
41 // Predict what would be the Gtk::Entry contents after the
42 // insertion.
43 Glib::ustring current_text = get_text();
44 Glib::ustring new_text = current_text;
45 new_text.replace(*position, 0, text);
46
47 if (!validate_text(new_text)) {
48 signal_insert_text().emission_stop();
49 }
50 }
51
52 void IntegerEntry::set_value(int val) { set_text(std::to_string(val)); }
53
54 int IntegerEntry::get_value() const {
55 Glib::ustring text_val = get_text();
56 int val = 0;
57
58 try {
59 val = std::stoi(text_val);
60 } catch (...) {
61 // The user can steer the logic here by typing "-" and then leaving
62 // the widget
63 val = 0;
64 }
65
66 return val;
67 }
68
69 // See also source code of gtk_spin_button_insert_text() in GTK repository.
70 bool IntegerEntry::validate_text(const Glib::ustring& text) const {
71 // Special cases which arise when the user started typing a not yet
72 // recognizable number
73 if (text == "-") {
74 return true;
75 }
76
77 // Attempt numeric conversion
78 char* end{};
79 errno = 0;
80 std::strtol(text.c_str(), &end, 10);
81
82 // If the new Gtk::Entry contents can't be converted to a number,
83 // the change should be discarded.
84 if (end != (text.c_str() + std::strlen(text.c_str())) || errno != 0) {
85 errno = 0;
86 return false;
87 }
88 return true;
89 }
90
91 DoubleEntry::DoubleEntry() {
92 decimal_point_ = std::string(1, *std::localeconv()->decimal_point);
93 }
94
95 void DoubleEntry::set_value(double val) {
96 std::string text_val = std::to_string(val);
97
98 // to_string() formats the number with excessive trailing zeros. We need to
99 // strip them.
100 size_t last_non_zero = text_val.find_last_not_of('0');
101
102 if (last_non_zero == std::string::npos) {
103 // All zeros, which is probably not really possible.
104 text_val = "0.0";
105 } else {
106 // If the non-zero character is period, keep one zero after it
107 if (text_val[last_non_zero] == decimal_point_[0]) {
108 last_non_zero = std::min(last_non_zero + 1, text_val.size() - 1);
109 }
110 text_val.erase(last_non_zero + 1);
111 }
112
113 set_text(text_val);
114 }
115
116 double DoubleEntry::get_value() const {
117 Glib::ustring text_val = get_text();
118 double val = 0.0;
119
120 try {
121 val = std::stod(text_val);
122 } catch (...) {
123 // The user can steer the logic here by typing "-" and then leaving
124 // the widget
125 val = 0.0;
126 }
127
128 return val;
129 }
130
131 // See also source code of gtk_spin_button_insert_text() in GTK repository.
132 bool DoubleEntry::validate_text(const Glib::ustring& text) const {
133 // Special cases which arise when the user started typing a not yet
134 // recognizable number
135 if (text == "-" || text == decimal_point_ || text == "-" + decimal_point_) {
136 return true;
137 }
138
139 // Attempt numeric conversion
140 char* end{};
141 errno = 0;
142 std::strtod(text.c_str(), &end);
143
144 // If the new Gtk::Entry contents can't be converted to a number,
145 // the change should be discarded.
146 if (end != (text.c_str() + std::strlen(text.c_str())) || errno != 0) {
147 errno = 0;
148 return false;
149 }
150 return true;
151 }
152
153 } // namespace ff::widgets