1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254 | /* Spin Button
*
* CtkSpinButton provides convenient ways to input data
* that can be seen as a value in a range. The examples
* here show that this does not necessarily mean numeric
* values, and it can include custom formatting.
*/
#include <glib/gi18n.h>
#include <ctk/ctk.h>
#include <math.h>
#include <stdlib.h>
static gint
hex_spin_input (CtkSpinButton *spin_button,
gdouble *new_val)
{
const gchar *buf;
gchar *err;
gdouble res;
buf = ctk_entry_get_text (CTK_ENTRY (spin_button));
res = strtol (buf, &err, 16);
*new_val = res;
if (*err)
return CTK_INPUT_ERROR;
else
return TRUE;
}
static gint
hex_spin_output (CtkSpinButton *spin_button)
{
CtkAdjustment *adjustment;
gchar *buf;
gint val;
adjustment = ctk_spin_button_get_adjustment (spin_button);
val = (gint) ctk_adjustment_get_value (adjustment);
if (fabs (val) < 1e-5)
buf = g_strdup ("0x00");
else
buf = g_strdup_printf ("0x%.2X", val);
if (strcmp (buf, ctk_entry_get_text (CTK_ENTRY (spin_button))))
ctk_entry_set_text (CTK_ENTRY (spin_button), buf);
g_free (buf);
return TRUE;
}
static gint
time_spin_input (CtkSpinButton *spin_button,
gdouble *new_val)
{
const gchar *text;
gchar **str;
gboolean found = FALSE;
gint hours;<--- The scope of the variable 'hours' can be reduced. [+]The scope of the variable 'hours' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
gint minutes;<--- The scope of the variable 'minutes' can be reduced. [+]The scope of the variable 'minutes' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
gchar *endh;
gchar *endm;
text = ctk_entry_get_text (CTK_ENTRY (spin_button));
str = g_strsplit (text, ":", 2);
if (g_strv_length (str) == 2)
{
hours = strtol (str[0], &endh, 10);
minutes = strtol (str[1], &endm, 10);
if (!*endh && !*endm &&
0 <= hours && hours < 24 &&
0 <= minutes && minutes < 60)
{
*new_val = hours * 60 + minutes;
found = TRUE;
}
}
g_strfreev (str);
if (!found)
{
*new_val = 0.0;
return CTK_INPUT_ERROR;
}
return TRUE;
}
static gint
time_spin_output (CtkSpinButton *spin_button)
{
CtkAdjustment *adjustment;
gchar *buf;
gdouble hours;
gdouble minutes;
adjustment = ctk_spin_button_get_adjustment (spin_button);
hours = ctk_adjustment_get_value (adjustment) / 60.0;
minutes = (hours - floor (hours)) * 60.0;
buf = g_strdup_printf ("%02.0f:%02.0f", floor (hours), floor (minutes + 0.5));
if (strcmp (buf, ctk_entry_get_text (CTK_ENTRY (spin_button))))
ctk_entry_set_text (CTK_ENTRY (spin_button), buf);
g_free (buf);
return TRUE;
}
static gchar *month[12] = {
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
};
static gint
month_spin_input (CtkSpinButton *spin_button,
gdouble *new_val)
{
gint i;
gboolean found = FALSE;
for (i = 1; i <= 12; i++)
{
gchar *tmp1, *tmp2;
tmp1 = g_ascii_strup (month[i - 1], -1);
tmp2 = g_ascii_strup (ctk_entry_get_text (CTK_ENTRY (spin_button)), -1);
if (strstr (tmp1, tmp2) == tmp1)
found = TRUE;
g_free (tmp1);
g_free (tmp2);
if (found)
break;
}
if (!found)
{
*new_val = 0.0;
return CTK_INPUT_ERROR;
}
*new_val = (gdouble) i;
return TRUE;
}
static gint
month_spin_output (CtkSpinButton *spin_button)
{
CtkAdjustment *adjustment;
gdouble value;
gint i;
adjustment = ctk_spin_button_get_adjustment (spin_button);
value = ctk_adjustment_get_value (adjustment);
for (i = 1; i <= 12; i++)
if (fabs (value - (double)i) < 1e-5)
{
if (strcmp (month[i-1], ctk_entry_get_text (CTK_ENTRY (spin_button))))
ctk_entry_set_text (CTK_ENTRY (spin_button), month[i-1]);
}
return TRUE;
}
static gboolean
value_to_label (GBinding *binding G_GNUC_UNUSED,
const GValue *from,
GValue *to,
gpointer user_data G_GNUC_UNUSED)
{
g_value_take_string (to, g_strdup_printf ("%g", g_value_get_double (from)));
return TRUE;
}
CtkWidget *
do_spinbutton (CtkWidget *do_widget)
{
static CtkWidget *window;
if (!window)
{
CtkBuilder *builder;
CtkAdjustment *adj;
CtkWidget *label;
builder = ctk_builder_new_from_resource ("/spinbutton/spinbutton.ui");
ctk_builder_add_callback_symbols (builder,
"hex_spin_input", G_CALLBACK (hex_spin_input),
"hex_spin_output", G_CALLBACK (hex_spin_output),
"time_spin_input", G_CALLBACK (time_spin_input),
"time_spin_output", G_CALLBACK (time_spin_output),
"month_spin_input", G_CALLBACK (month_spin_input),
"month_spin_output", G_CALLBACK (month_spin_output),
NULL);
ctk_builder_connect_signals (builder, NULL);
window = CTK_WIDGET (ctk_builder_get_object (builder, "window"));
ctk_window_set_screen (CTK_WINDOW (window),
ctk_widget_get_screen (do_widget));
ctk_window_set_title (CTK_WINDOW (window), "Spin Buttons");
ctk_window_set_resizable (CTK_WINDOW (window), FALSE);
g_signal_connect (window, "destroy",
G_CALLBACK (ctk_widget_destroyed), &window);
adj = CTK_ADJUSTMENT (ctk_builder_get_object (builder, "basic_adjustment"));
label = CTK_WIDGET (ctk_builder_get_object (builder, "basic_label"));
g_object_bind_property_full (adj, "value",
label, "label",
G_BINDING_SYNC_CREATE,
value_to_label,
NULL,
NULL, NULL);
adj = CTK_ADJUSTMENT (ctk_builder_get_object (builder, "hex_adjustment"));
label = CTK_WIDGET (ctk_builder_get_object (builder, "hex_label"));
g_object_bind_property_full (adj, "value",
label, "label",
G_BINDING_SYNC_CREATE,
value_to_label,
NULL,
NULL, NULL);
adj = CTK_ADJUSTMENT (ctk_builder_get_object (builder, "time_adjustment"));
label = CTK_WIDGET (ctk_builder_get_object (builder, "time_label"));
g_object_bind_property_full (adj, "value",
label, "label",
G_BINDING_SYNC_CREATE,
value_to_label,
NULL,
NULL, NULL);
adj = CTK_ADJUSTMENT (ctk_builder_get_object (builder, "month_adjustment"));
label = CTK_WIDGET (ctk_builder_get_object (builder, "month_label"));
g_object_bind_property_full (adj, "value",
label, "label",
G_BINDING_SYNC_CREATE,
value_to_label,
NULL,
NULL, NULL);
g_object_unref (builder);
}
if (!ctk_widget_get_visible (window))
ctk_widget_show_all (window);
else
ctk_widget_destroy (window);
return window;
}
|