1 /* EXTRAITS DE LA LICENCE
2 Copyright CEA, contributeurs : Luc BILLARD et Damien
3 CALISTE, laboratoire L_Sim, (2001-2005)
4
5 Adresse mèl :
6 BILLARD, non joignable par mèl ;
7 CALISTE, damien P caliste AT cea P fr.
8
9 Ce logiciel est un programme informatique servant à visualiser des
10 structures atomiques dans un rendu pseudo-3D.
11
12 Ce logiciel est régi par la licence CeCILL soumise au droit français et
13 respectant les principes de diffusion des logiciels libres. Vous pouvez
14 utiliser, modifier et/ou redistribuer ce programme sous les conditions
15 de la licence CeCILL telle que diffusée par le CEA, le CNRS et l'INRIA
16 sur le site "http://www.cecill.info".
17
18 Le fait que vous puissiez accéder à cet en-tête signifie que vous avez
19 pris connaissance de la licence CeCILL, et que vous en avez accepté les
20 termes (cf. le fichier Documentation/licence.fr.txt fourni avec ce logiciel).
21 */
22
23 /* LICENCE SUM UP
24 Copyright CEA, contributors : Luc BILLARD et Damien
25 CALISTE, laboratoire L_Sim, (2001-2005)
26
27 E-mail address:
28 BILLARD, not reachable any more ;
29 CALISTE, damien P caliste AT cea P fr.
30
31 This software is a computer program whose purpose is to visualize atomic
32 configurations in 3D.
33
34 This software is governed by the CeCILL license under French law and
35 abiding by the rules of distribution of free software. You can use,
36 modify and/ or redistribute the software under the terms of the CeCILL
37 license as circulated by CEA, CNRS and INRIA at the following URL
38 "http://www.cecill.info".
39
40 The fact that you are presently reading this means that you have had
41 knowledge of the CeCILL license and that you accept its terms. You can
42 find a copy of this licence shipped with this software at Documentation/licence.en.txt.
43 */
44 #include <gtk/gtk.h>
45
46 #include "gtk_numericalEntryWidget.h"
47
48 /**
49 * SECTION:gtk_numericalEntryWidget
50 * @short_description: Defines a widget to enter numerical values without
51 * any boundary or precision constrains.
52 *
53 * <para>This widget is based on the #GtkEntry widget but behaves more
54 * like a #GtkSpinButton is fact. It is designed to enter numerical
55 * values, but without any boundary or precision constrains. One can
56 * use either plain or scientific notations.</para>
57 */
58
59 enum {
60 VALUE_CHANGED_SIGNAL,
61 LAST_SIGNAL
62 };
63 static guint _signals[LAST_SIGNAL] = { 0 };
64 enum
65 {
66 PROP_0,
67 VALUE_PROP,
68 N_PROP
69 };
70 static GParamSpec *properties[N_PROP];
71
72 #define VISU_UI_NUMERICAL_ENTRY_FORMAT_DEFAULT "%g"
73
74 struct _VisuUiNumericalEntry
75 {
76 GtkEntry entry;
77
78 double value;
79 double printed_value;
80 gchar *format;
81 /* VisuUiNumericalEntryPrivate *private; */
82 };
83
84 struct _VisuUiNumericalEntryClass
85 {
86 GtkEntryClass parent_class;
87
88 void (*changed) (VisuUiNumericalEntry *numEntry, double oldValue);
89 };
90
91 static void visu_ui_numerical_entry_finalize(GObject *obj);
92 static void visu_ui_numerical_entry_get_property(GObject* obj, guint property_id,
93 GValue *value, GParamSpec *pspec);
94 static void visu_ui_numerical_entry_set_property(GObject* obj, guint property_id,
95 const GValue *value, GParamSpec *pspec);
96 static gboolean visu_ui_numerical_entry_focus_in(GtkWidget *wd, GdkEventFocus *event);
97 static gboolean visu_ui_numerical_entry_focus_out(GtkWidget *wd, GdkEventFocus *event);
98 static void visu_ui_numerical_entry_activate(GtkEntry *entry);
99
100 G_DEFINE_TYPE(VisuUiNumericalEntry, visu_ui_numerical_entry, GTK_TYPE_ENTRY)
101
102 /* Local methods. */
103 static void printStoredValue(VisuUiNumericalEntry* numericalEntry);
104 static gboolean parsePrintedValue(VisuUiNumericalEntry *numericalEntry, double *value);
105
visu_ui_numerical_entry_class_init(VisuUiNumericalEntryClass * klass)106 static void visu_ui_numerical_entry_class_init(VisuUiNumericalEntryClass *klass)
107 {
108 DBG_fprintf(stderr, "Gtk VisuUiNumericalEntry : creating the class of the widget.\n");
109
110 DBG_fprintf(stderr, " - adding new signals ;\n");
111 /**
112 * VisuUiNumericalEntry::value-changed:
113 * @entry: the #VisuUiNumericalEntry that emits the signal ;
114 * @oldValue: the previous value.
115 *
116 * This signal is emitted when a new valid numerical value is entered.
117 *
118 * Since: 3.1
119 */
120 _signals[VALUE_CHANGED_SIGNAL] =
121 g_signal_new ("value-changed",
122 G_TYPE_FROM_CLASS (klass),
123 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
124 G_STRUCT_OFFSET (VisuUiNumericalEntryClass, changed),
125 NULL,
126 NULL,
127 g_cclosure_marshal_VOID__DOUBLE,
128 G_TYPE_NONE, 1, G_TYPE_DOUBLE);
129
130 /* Connect freeing methods. */
131 G_OBJECT_CLASS(klass)->finalize = visu_ui_numerical_entry_finalize;
132 G_OBJECT_CLASS(klass)->set_property = visu_ui_numerical_entry_set_property;
133 G_OBJECT_CLASS(klass)->get_property = visu_ui_numerical_entry_get_property;
134 GTK_WIDGET_CLASS(klass)->focus_in_event = visu_ui_numerical_entry_focus_in;
135 GTK_WIDGET_CLASS(klass)->focus_out_event = visu_ui_numerical_entry_focus_out;
136 GTK_ENTRY_CLASS(klass)->activate = visu_ui_numerical_entry_activate;
137
138 /**
139 * VisuUiNumericalEntry::value:
140 *
141 * Store the value in the entry.
142 *
143 * Since: 3.8
144 */
145 properties[VALUE_PROP] = g_param_spec_double("value", "Value", "numerical value",
146 -G_MAXFLOAT, G_MAXFLOAT, 0.,
147 G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
148 g_object_class_install_properties(G_OBJECT_CLASS(klass), N_PROP, properties);
149 }
150
visu_ui_numerical_entry_finalize(GObject * obj)151 static void visu_ui_numerical_entry_finalize(GObject *obj)
152 {
153 VisuUiNumericalEntry *entry;
154
155 g_return_if_fail(obj);
156
157 DBG_fprintf(stderr, "Gtk VisuUiNumericalEntry: finalize object %p.\n", (gpointer)obj);
158
159 entry = VISU_UI_NUMERICAL_ENTRY(obj);
160 g_free(entry->format);
161
162 /* Chain up to the parent class */
163 G_OBJECT_CLASS(visu_ui_numerical_entry_parent_class)->finalize(obj);
164
165 DBG_fprintf(stderr, "Gtk VisuUiNumericalEntry: freeing ... OK.\n");
166 }
visu_ui_numerical_entry_get_property(GObject * obj,guint property_id,GValue * value,GParamSpec * pspec)167 static void visu_ui_numerical_entry_get_property(GObject* obj, guint property_id,
168 GValue *value, GParamSpec *pspec)
169 {
170 VisuUiNumericalEntry *self = VISU_UI_NUMERICAL_ENTRY(obj);
171
172 DBG_fprintf(stderr, "Gtk VisuUiNumericalEntry: get property '%s'.\n",
173 g_param_spec_get_name(pspec));
174 switch (property_id)
175 {
176 case VALUE_PROP:
177 g_value_set_double(value, self->value);
178 break;
179 default:
180 /* We don't have any other property... */
181 G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, property_id, pspec);
182 break;
183 }
184 }
visu_ui_numerical_entry_set_property(GObject * obj,guint property_id,const GValue * value,GParamSpec * pspec)185 static void visu_ui_numerical_entry_set_property(GObject* obj, guint property_id,
186 const GValue *value, GParamSpec *pspec)
187 {
188 VisuUiNumericalEntry *self = VISU_UI_NUMERICAL_ENTRY(obj);
189
190 DBG_fprintf(stderr, "Gtk VisuUiNumericalEntry: set property '%s'.\n",
191 g_param_spec_get_name(pspec));
192 switch (property_id)
193 {
194 case VALUE_PROP:
195 visu_ui_numerical_entry_setValue(self, g_value_get_double(value));
196 break;
197 default:
198 /* We don't have any other property... */
199 G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, property_id, pspec);
200 break;
201 }
202 }
203
visu_ui_numerical_entry_init(VisuUiNumericalEntry * numericalEntry)204 static void visu_ui_numerical_entry_init(VisuUiNumericalEntry *numericalEntry)
205 {
206 DBG_fprintf(stderr, "Gtk VisuUiNumericalEntry : initializing new object (%p).\n",
207 (gpointer)numericalEntry);
208
209 numericalEntry->format = g_strdup(VISU_UI_NUMERICAL_ENTRY_FORMAT_DEFAULT);
210 }
211
212 /**
213 * visu_ui_numerical_entry_new :
214 * @value: the initial value.
215 *
216 * A #VisuUiNumericalEntry widget is like a #GtkEntry widget, but it only accepts
217 * double precision values (written in plain format, e.g. 1.23456, or scientific
218 * notation, e.g. 1.2345e6). The widget can't be blank and there is always
219 * a value printed in it. If the user erase the current value or enter something
220 * that is not a recognised double precision value, the widget returns to its previous
221 * valid value.
222 *
223 * Returns: a newly created #VisuUiNumericalEntry widget.
224 */
visu_ui_numerical_entry_new(double value)225 GtkWidget* visu_ui_numerical_entry_new(double value)
226 {
227 return g_object_new(VISU_TYPE_UI_NUMERICAL_ENTRY, "value", value, NULL);
228 }
229
printStoredValue(VisuUiNumericalEntry * numericalEntry)230 static void printStoredValue(VisuUiNumericalEntry* numericalEntry)
231 {
232 gchar *str;
233
234 g_return_if_fail(VISU_IS_UI_NUMERICAL_ENTRY(numericalEntry));
235
236 str = g_strdup_printf(numericalEntry->format, numericalEntry->value);
237 gtk_entry_set_text(GTK_ENTRY(numericalEntry), str);
238 g_free(str);
239
240 if (!parsePrintedValue(numericalEntry, &numericalEntry->printed_value))
241 numericalEntry->printed_value = G_MAXFLOAT;
242 }
parsePrintedValue(VisuUiNumericalEntry * numericalEntry,double * value)243 static gboolean parsePrintedValue(VisuUiNumericalEntry *numericalEntry, double *value)
244 {
245 double valueDouble;
246 gchar *last;
247
248 g_return_val_if_fail(VISU_IS_UI_NUMERICAL_ENTRY(numericalEntry) && value, FALSE);
249
250 valueDouble = g_ascii_strtod(gtk_entry_get_text(GTK_ENTRY(numericalEntry)),
251 &last);
252 if (*last != '\0')
253 {
254 /* Wrong number. */
255 visu_ui_numerical_entry_warnValue(numericalEntry, numericalEntry->value);
256 return FALSE;
257 }
258
259 *value = valueDouble;
260 return TRUE;
261 }
262
263 /**
264 * visu_ui_numerical_entry_setValue:
265 * @numericalEntry: a #VisuUiNumericalEntry widget ;
266 * @value: a double precision value.
267 *
268 * Use this method to set the value for the given #numericalEntry widget.
269 */
visu_ui_numerical_entry_setValue(VisuUiNumericalEntry * numericalEntry,double value)270 void visu_ui_numerical_entry_setValue(VisuUiNumericalEntry* numericalEntry, double value)
271 {
272 double valueOld;
273
274 g_return_if_fail(VISU_IS_UI_NUMERICAL_ENTRY(numericalEntry));
275
276 if (value == numericalEntry->value)
277 return;
278
279 valueOld = numericalEntry->value;
280 numericalEntry->value = value;
281
282 printStoredValue(numericalEntry);
283
284 DBG_fprintf(stderr, "Gtk VisuUiNumericalEntry : emitting 'value-changed' signal.\n");
285 g_object_notify_by_pspec(G_OBJECT(numericalEntry), properties[VALUE_PROP]);
286 g_signal_emit(G_OBJECT(numericalEntry),
287 _signals[VALUE_CHANGED_SIGNAL], 0, valueOld, NULL);
288 }
289 /**
290 * visu_ui_numerical_entry_getValue:
291 * @numericalEntry: a #VisuUiNumericalEntry widget.
292 *
293 * You can get the value contained in the given @numericalEntry using this method.
294 *
295 * Returns: the double precision value printed in the #VisuUiNumericalEntry.
296 */
visu_ui_numerical_entry_getValue(VisuUiNumericalEntry * numericalEntry)297 double visu_ui_numerical_entry_getValue(VisuUiNumericalEntry *numericalEntry)
298 {
299 g_return_val_if_fail(VISU_IS_UI_NUMERICAL_ENTRY(numericalEntry), 0.);
300
301 return numericalEntry->value;
302 }
303
visu_ui_numerical_entry_activate(GtkEntry * entry)304 static void visu_ui_numerical_entry_activate(GtkEntry *entry)
305 {
306 VisuUiNumericalEntry *numericalEntry = VISU_UI_NUMERICAL_ENTRY(entry);
307 double valueDouble;
308
309 if (parsePrintedValue(numericalEntry, &valueDouble) &&
310 valueDouble != numericalEntry->printed_value)
311 visu_ui_numerical_entry_setValue(numericalEntry, valueDouble);
312 }
visu_ui_numerical_entry_focus_out(GtkWidget * wd,GdkEventFocus * event)313 static gboolean visu_ui_numerical_entry_focus_out(GtkWidget *wd, GdkEventFocus *event)
314 {
315 visu_ui_numerical_entry_activate(GTK_ENTRY(wd));
316 return GTK_WIDGET_CLASS(visu_ui_numerical_entry_parent_class)->focus_in_event(wd, event);
317 }
visu_ui_numerical_entry_focus_in(GtkWidget * wd,GdkEventFocus * event)318 static gboolean visu_ui_numerical_entry_focus_in(GtkWidget *wd, GdkEventFocus *event)
319 {
320 /* gtk_editable_select_region(GTK_EDITABLE(numericalEntry), 0, -1); */
321 return GTK_WIDGET_CLASS(visu_ui_numerical_entry_parent_class)->focus_out_event(wd, event);
322 }
_removeWarning(gpointer data)323 static gboolean _removeWarning(gpointer data)
324 {
325 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(data), GTK_ENTRY_ICON_SECONDARY, NULL);
326 return FALSE;
327 }
328 /**
329 * visu_ui_numerical_entry_warnValue:
330 * @numericalEntry: a #VisuUiNumericalEntry object.
331 * @fallback: a floating point value.
332 *
333 * Display a warning sign in the entry and fallback to the given value.
334 *
335 * Since: 3.8
336 **/
visu_ui_numerical_entry_warnValue(VisuUiNumericalEntry * numericalEntry,float fallback)337 void visu_ui_numerical_entry_warnValue(VisuUiNumericalEntry *numericalEntry, float fallback)
338 {
339 g_return_if_fail(VISU_IS_UI_NUMERICAL_ENTRY(numericalEntry));
340
341 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(numericalEntry),
342 GTK_ENTRY_ICON_SECONDARY, "dialog-warning");
343 g_timeout_add_seconds(2, _removeWarning, (gpointer)numericalEntry);
344
345 numericalEntry->value = G_MAXDOUBLE;
346 visu_ui_numerical_entry_setValue(numericalEntry, fallback);
347 }
348