1 | /* Evolution internal utilities - Glade dialog widget utilities |
---|
2 | * |
---|
3 | * Copyright (C) 2000 Ximian, Inc. |
---|
4 | * Copyright (C) 2000 Ximian, Inc. |
---|
5 | * |
---|
6 | * Author: Federico Mena-Quintero <federico@ximian.com> |
---|
7 | * |
---|
8 | * This program is free software; you can redistribute it and/or |
---|
9 | * modify it under the terms of version 2 of the GNU General Public |
---|
10 | * License as published by the Free Software Foundation. |
---|
11 | * |
---|
12 | * This program is distributed in the hope that it will be useful, |
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
15 | * General Public License for more details. |
---|
16 | * |
---|
17 | * You should have received a copy of the GNU General Public |
---|
18 | * License along with this program; if not, write to the |
---|
19 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
---|
20 | * Boston, MA 02111-1307, USA. |
---|
21 | */ |
---|
22 | |
---|
23 | #include <config.h> |
---|
24 | |
---|
25 | #include <math.h> |
---|
26 | #include <string.h> |
---|
27 | #include <time.h> |
---|
28 | #include <gtk/gtkmenu.h> |
---|
29 | #include <gtk/gtkmenuitem.h> |
---|
30 | #include <gtk/gtkoptionmenu.h> |
---|
31 | #include <gtk/gtkradiobutton.h> |
---|
32 | #include <gtk/gtksignal.h> |
---|
33 | #include <gtk/gtkspinbutton.h> |
---|
34 | #include <libgnomeui/gnome-dateedit.h> |
---|
35 | #include <libgnomeui/gnome-propertybox.h> |
---|
36 | #include <gal/widgets/e-unicode.h> |
---|
37 | |
---|
38 | #include "e-dialog-widgets.h" |
---|
39 | |
---|
40 | |
---|
41 | |
---|
42 | /* A widget, a pointer to the variable it will modify, and extra information */ |
---|
43 | typedef struct { |
---|
44 | GtkWidget *widget; |
---|
45 | gpointer value_var; |
---|
46 | gpointer info; |
---|
47 | } WidgetHook; |
---|
48 | |
---|
49 | /* Hook information for a complete dialog */ |
---|
50 | typedef struct { |
---|
51 | GSList *whooks; |
---|
52 | } DialogHooks; |
---|
53 | |
---|
54 | |
---|
55 | |
---|
56 | /* Destroy handler for the dialog; frees the dialog hooks */ |
---|
57 | static void |
---|
58 | dialog_destroy_cb (GtkObject *dialog, gpointer data) |
---|
59 | { |
---|
60 | DialogHooks *hooks; |
---|
61 | |
---|
62 | hooks = data; |
---|
63 | |
---|
64 | g_slist_free (hooks->whooks); |
---|
65 | hooks->whooks = NULL; |
---|
66 | |
---|
67 | g_free (hooks); |
---|
68 | gtk_object_set_data (dialog, "dialog-hooks", NULL); |
---|
69 | } |
---|
70 | |
---|
71 | /* Ensures that the dialog has the necessary attached data to store the widget |
---|
72 | * hook information. |
---|
73 | */ |
---|
74 | static DialogHooks * |
---|
75 | get_dialog_hooks (GtkWidget *dialog) |
---|
76 | { |
---|
77 | DialogHooks *hooks; |
---|
78 | |
---|
79 | hooks = gtk_object_get_data (GTK_OBJECT (dialog), "dialog-hooks"); |
---|
80 | if (!hooks) { |
---|
81 | hooks = g_new0 (DialogHooks, 1); |
---|
82 | gtk_object_set_data (GTK_OBJECT (dialog), "dialog-hooks", hooks); |
---|
83 | gtk_signal_connect (GTK_OBJECT (dialog), "destroy", |
---|
84 | GTK_SIGNAL_FUNC (dialog_destroy_cb), hooks); |
---|
85 | } |
---|
86 | |
---|
87 | return hooks; |
---|
88 | } |
---|
89 | |
---|
90 | /* Converts an mapped value to the appropriate index in an item group. The |
---|
91 | * values for the items are provided as a -1-terminated array. |
---|
92 | */ |
---|
93 | static int |
---|
94 | value_to_index (const int *value_map, int value) |
---|
95 | { |
---|
96 | int i; |
---|
97 | |
---|
98 | for (i = 0; value_map[i] != -1; i++) |
---|
99 | if (value_map[i] == value) |
---|
100 | return i; |
---|
101 | |
---|
102 | return -1; |
---|
103 | } |
---|
104 | |
---|
105 | /* Converts an index in an item group to the appropriate mapped value. See the |
---|
106 | * function above. |
---|
107 | */ |
---|
108 | static int |
---|
109 | index_to_value (const int *value_map, int index) |
---|
110 | { |
---|
111 | int i; |
---|
112 | |
---|
113 | /* We do this the hard way, i.e. not as a simple array reference, to |
---|
114 | * check for correctness. |
---|
115 | */ |
---|
116 | |
---|
117 | for (i = 0; value_map[i] != -1; i++) |
---|
118 | if (i == index) |
---|
119 | return value_map[i]; |
---|
120 | |
---|
121 | return -1; |
---|
122 | } |
---|
123 | |
---|
124 | /* Callback for the "toggled" signal of toggle buttons */ |
---|
125 | static void |
---|
126 | toggled_cb (GtkToggleButton *toggle, gpointer data) |
---|
127 | { |
---|
128 | GnomePropertyBox *pbox; |
---|
129 | |
---|
130 | pbox = GNOME_PROPERTY_BOX (data); |
---|
131 | |
---|
132 | /* For radio buttons, we only notify the property box if the button is |
---|
133 | * active, because we'll get one call for each of the changed buttons in |
---|
134 | * the radio group. |
---|
135 | */ |
---|
136 | if (!GTK_IS_RADIO_BUTTON (toggle) || toggle->active) |
---|
137 | gnome_property_box_changed (pbox); |
---|
138 | } |
---|
139 | |
---|
140 | /* Hooks a radio button group */ |
---|
141 | static void |
---|
142 | hook_radio (GtkWidget *dialog, GtkRadioButton *radio, gpointer value_var, gpointer info) |
---|
143 | { |
---|
144 | GSList *group; |
---|
145 | GSList *l; |
---|
146 | int *value; |
---|
147 | const int *value_map; |
---|
148 | |
---|
149 | group = gtk_radio_button_group (radio); |
---|
150 | |
---|
151 | /* Set the value */ |
---|
152 | |
---|
153 | value = (int *) value_var; |
---|
154 | value_map = (const int *) info; |
---|
155 | |
---|
156 | e_dialog_radio_set (GTK_WIDGET (radio), *value, value_map); |
---|
157 | |
---|
158 | /* Hook to changed */ |
---|
159 | |
---|
160 | if (GNOME_IS_PROPERTY_BOX (dialog)) |
---|
161 | for (l = group; l; l = l->next) |
---|
162 | gtk_signal_connect (GTK_OBJECT (l->data), "toggled", |
---|
163 | GTK_SIGNAL_FUNC (toggled_cb), dialog); |
---|
164 | } |
---|
165 | |
---|
166 | /* Gets the value of a radio button group */ |
---|
167 | static void |
---|
168 | get_radio_value (GtkRadioButton *radio, gpointer value_var, gpointer info) |
---|
169 | { |
---|
170 | int *value; |
---|
171 | const int *value_map; |
---|
172 | |
---|
173 | value = (int *) value_var; |
---|
174 | value_map = (const int *) info; |
---|
175 | |
---|
176 | *value = e_dialog_radio_get (GTK_WIDGET (radio), value_map); |
---|
177 | } |
---|
178 | |
---|
179 | /* Callback for the "activate" signal of menu items */ |
---|
180 | static void |
---|
181 | activate_cb (GtkMenuItem *item, gpointer data) |
---|
182 | { |
---|
183 | GnomePropertyBox *pbox; |
---|
184 | |
---|
185 | pbox = GNOME_PROPERTY_BOX (data); |
---|
186 | gnome_property_box_changed (pbox); |
---|
187 | } |
---|
188 | |
---|
189 | /* Hooks an option menu */ |
---|
190 | static void |
---|
191 | hook_option_menu (GtkWidget *dialog, GtkOptionMenu *omenu, gpointer value_var, gpointer info) |
---|
192 | { |
---|
193 | int *value; |
---|
194 | const int *value_map; |
---|
195 | |
---|
196 | /* Set the value */ |
---|
197 | |
---|
198 | value = (int *) value_var; |
---|
199 | value_map = (const int *) info; |
---|
200 | |
---|
201 | e_dialog_option_menu_set (GTK_WIDGET (omenu), *value, value_map); |
---|
202 | |
---|
203 | /* Hook to changed */ |
---|
204 | |
---|
205 | if (GNOME_IS_PROPERTY_BOX (dialog)) { |
---|
206 | GtkMenu *menu; |
---|
207 | GList *l; |
---|
208 | |
---|
209 | menu = GTK_MENU (gtk_option_menu_get_menu (omenu)); |
---|
210 | |
---|
211 | for (l = GTK_MENU_SHELL (menu)->children; l; l = l->next) |
---|
212 | gtk_signal_connect (GTK_OBJECT (l->data), "activate", |
---|
213 | GTK_SIGNAL_FUNC (activate_cb), dialog); |
---|
214 | } |
---|
215 | } |
---|
216 | |
---|
217 | /* Gets the value of an option menu */ |
---|
218 | static void |
---|
219 | get_option_menu_value (GtkOptionMenu *omenu, gpointer value_var, gpointer info) |
---|
220 | { |
---|
221 | int *value; |
---|
222 | const int *value_map; |
---|
223 | |
---|
224 | value = (int *) value_var; |
---|
225 | value_map = (const int *) info; |
---|
226 | |
---|
227 | *value = e_dialog_option_menu_get (GTK_WIDGET (omenu), value_map); |
---|
228 | } |
---|
229 | |
---|
230 | /* Hooks a toggle button */ |
---|
231 | static void |
---|
232 | hook_toggle (GtkWidget *dialog, GtkToggleButton *toggle, gpointer value_var, gpointer info) |
---|
233 | { |
---|
234 | gboolean *value; |
---|
235 | |
---|
236 | /* Set the value */ |
---|
237 | |
---|
238 | value = (gboolean *) value_var; |
---|
239 | e_dialog_toggle_set (GTK_WIDGET (toggle), *value); |
---|
240 | |
---|
241 | /* Hook to changed */ |
---|
242 | |
---|
243 | if (GNOME_IS_PROPERTY_BOX (dialog)) |
---|
244 | gtk_signal_connect (GTK_OBJECT (toggle), "toggled", |
---|
245 | GTK_SIGNAL_FUNC (toggled_cb), dialog); |
---|
246 | } |
---|
247 | |
---|
248 | /* Gets the value of a toggle button */ |
---|
249 | static void |
---|
250 | get_toggle_value (GtkToggleButton *toggle, gpointer value_var, gpointer info) |
---|
251 | { |
---|
252 | gboolean *value; |
---|
253 | |
---|
254 | value = (gboolean *) value_var; |
---|
255 | *value = e_dialog_toggle_get (GTK_WIDGET (toggle)); |
---|
256 | } |
---|
257 | |
---|
258 | /* Callback for the "value_changed" signal of the adjustment of a spin button */ |
---|
259 | static void |
---|
260 | value_changed_cb (GtkAdjustment *adj, gpointer data) |
---|
261 | { |
---|
262 | GnomePropertyBox *pbox; |
---|
263 | |
---|
264 | pbox = GNOME_PROPERTY_BOX (data); |
---|
265 | gnome_property_box_changed (pbox); |
---|
266 | } |
---|
267 | |
---|
268 | /* Hooks a spin button */ |
---|
269 | static void |
---|
270 | hook_spin_button (GtkWidget *dialog, GtkSpinButton *spin, gpointer value_var, gpointer info) |
---|
271 | { |
---|
272 | double *value; |
---|
273 | GtkAdjustment *adj; |
---|
274 | |
---|
275 | /* Set the value */ |
---|
276 | |
---|
277 | value = (double *) value_var; |
---|
278 | e_dialog_spin_set (GTK_WIDGET (spin), *value); |
---|
279 | |
---|
280 | /* Hook to changed */ |
---|
281 | |
---|
282 | adj = gtk_spin_button_get_adjustment (spin); |
---|
283 | |
---|
284 | if (GNOME_IS_PROPERTY_BOX (dialog)) |
---|
285 | gtk_signal_connect (GTK_OBJECT (adj), "value_changed", |
---|
286 | GTK_SIGNAL_FUNC (value_changed_cb), dialog); |
---|
287 | } |
---|
288 | |
---|
289 | /* Gets the value of a spin button */ |
---|
290 | static void |
---|
291 | get_spin_button_value (GtkSpinButton *spin, gpointer value_var, gpointer info) |
---|
292 | { |
---|
293 | double *value; |
---|
294 | |
---|
295 | value = (double *) value_var; |
---|
296 | *value = e_dialog_spin_get_double (GTK_WIDGET (spin)); |
---|
297 | } |
---|
298 | |
---|
299 | /* Callback for the "changed" signal of a GtkEditable widget */ |
---|
300 | static void |
---|
301 | changed_cb (GtkEditable *editable, gpointer data) |
---|
302 | { |
---|
303 | GnomePropertyBox *pbox; |
---|
304 | |
---|
305 | pbox = GNOME_PROPERTY_BOX (data); |
---|
306 | gnome_property_box_changed (pbox); |
---|
307 | } |
---|
308 | |
---|
309 | /* Hooks a GtkEditable widget */ |
---|
310 | static void |
---|
311 | hook_editable (GtkWidget *dialog, GtkEditable *editable, gpointer value_var, gpointer info) |
---|
312 | { |
---|
313 | char **value; |
---|
314 | |
---|
315 | /* Set the value */ |
---|
316 | |
---|
317 | value = (char **) value_var; |
---|
318 | |
---|
319 | e_dialog_editable_set (GTK_WIDGET (editable), *value); |
---|
320 | |
---|
321 | /* Hook to changed */ |
---|
322 | |
---|
323 | if (GNOME_IS_PROPERTY_BOX (dialog)) |
---|
324 | gtk_signal_connect (GTK_OBJECT (editable), "changed", |
---|
325 | GTK_SIGNAL_FUNC (changed_cb), dialog); |
---|
326 | } |
---|
327 | |
---|
328 | /* Gets the value of a GtkEditable widget */ |
---|
329 | static void |
---|
330 | get_editable_value (GtkEditable *editable, gpointer value_var, gpointer data) |
---|
331 | { |
---|
332 | char **value; |
---|
333 | |
---|
334 | value = (char **) value_var; |
---|
335 | if (*value) |
---|
336 | g_free (*value); |
---|
337 | |
---|
338 | *value = e_dialog_editable_get (GTK_WIDGET (editable)); |
---|
339 | } |
---|
340 | |
---|
341 | /** |
---|
342 | * e_dialog_editable_set: |
---|
343 | * @widget: A #GtkEditable widget. |
---|
344 | * @value: String value. |
---|
345 | * |
---|
346 | * Sets the string value inside a #GtkEditable-derived widget. |
---|
347 | **/ |
---|
348 | void |
---|
349 | e_dialog_editable_set (GtkWidget *widget, const char *value) |
---|
350 | { |
---|
351 | g_return_if_fail (widget != NULL); |
---|
352 | g_return_if_fail (GTK_IS_EDITABLE (widget)); |
---|
353 | |
---|
354 | gtk_editable_delete_text (GTK_EDITABLE (widget), 0, -1); |
---|
355 | |
---|
356 | if (value) { |
---|
357 | gint pos; |
---|
358 | |
---|
359 | pos = 0; |
---|
360 | e_utf8_gtk_editable_insert_text (GTK_EDITABLE (widget), value, strlen (value), &pos); |
---|
361 | } |
---|
362 | } |
---|
363 | |
---|
364 | /** |
---|
365 | * e_dialog_editable_get: |
---|
366 | * @widget: A #GtkEditable widget. |
---|
367 | * |
---|
368 | * Queries the string value inside a #GtkEditable-derived widget. |
---|
369 | * |
---|
370 | * Return value: String value. You should free it when you are done with it. |
---|
371 | * This function can return NULL if the string could not be converted from |
---|
372 | * GTK+'s encoding into UTF8. |
---|
373 | **/ |
---|
374 | char * |
---|
375 | e_dialog_editable_get (GtkWidget *widget) |
---|
376 | { |
---|
377 | g_return_val_if_fail (widget != NULL, NULL); |
---|
378 | g_return_val_if_fail (GTK_IS_EDITABLE (widget), NULL); |
---|
379 | |
---|
380 | return e_utf8_gtk_editable_get_chars (GTK_EDITABLE (widget), 0, -1); |
---|
381 | } |
---|
382 | |
---|
383 | /** |
---|
384 | * e_dialog_radio_set: |
---|
385 | * @widget: A #GtkRadioButton in a radio button group. |
---|
386 | * @value: Enumerated value. |
---|
387 | * @value_map: Map from enumeration values to array indices. |
---|
388 | * |
---|
389 | * Sets the selected item in a radio group. The specified @widget can be any of |
---|
390 | * the #GtkRadioButtons in the group. Each radio button should correspond to an |
---|
391 | * enumeration value; the specified @value will be mapped to an integer from |
---|
392 | * zero to the number of items in the group minus 1 by using a mapping table |
---|
393 | * specified in @value_map. The last element in this table should be -1. Thus |
---|
394 | * a table to map three possible interpolation values to integers could be |
---|
395 | * specified as { NEAREST_NEIGHBOR, BILINEAR, HYPERBOLIC, -1 }. |
---|
396 | **/ |
---|
397 | void |
---|
398 | e_dialog_radio_set (GtkWidget *widget, int value, const int *value_map) |
---|
399 | { |
---|
400 | GSList *group; |
---|
401 | int i; |
---|
402 | GSList *l; |
---|
403 | |
---|
404 | g_return_if_fail (widget != NULL); |
---|
405 | g_return_if_fail (GTK_IS_RADIO_BUTTON (widget)); |
---|
406 | g_return_if_fail (value_map != NULL); |
---|
407 | |
---|
408 | group = gtk_radio_button_group (GTK_RADIO_BUTTON (widget)); |
---|
409 | |
---|
410 | i = value_to_index (value_map, value); |
---|
411 | if (i != -1) { |
---|
412 | /* Groups are built by prepending items, so the list ends up in reverse |
---|
413 | * order; we need to flip the index around. |
---|
414 | */ |
---|
415 | i = g_slist_length (group) - i - 1; |
---|
416 | |
---|
417 | l = g_slist_nth (group, i); |
---|
418 | if (!l) |
---|
419 | g_message ("e_dialog_radio_set(): could not find index %d in radio group!", |
---|
420 | i); |
---|
421 | |
---|
422 | gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (l->data), TRUE); |
---|
423 | } else |
---|
424 | g_message ("e_dialog_radio_set(): could not find value %d in value map!", |
---|
425 | value); |
---|
426 | } |
---|
427 | |
---|
428 | /** |
---|
429 | * e_dialog_radio_get: |
---|
430 | * @widget: A #GtkRadioButton in a radio button group. |
---|
431 | * @value_map: Map from enumeration values to array indices. |
---|
432 | * |
---|
433 | * Queries the selected item in a #GtkRadioButton group. Please read the |
---|
434 | * description of e_dialog_radio_set() to see how @value_map maps enumeration |
---|
435 | * values to button indices. |
---|
436 | * |
---|
437 | * Return value: Enumeration value which corresponds to the selected item in the |
---|
438 | * radio group. |
---|
439 | **/ |
---|
440 | int |
---|
441 | e_dialog_radio_get (GtkWidget *widget, const int *value_map) |
---|
442 | { |
---|
443 | GSList *group; |
---|
444 | GSList *l; |
---|
445 | int i; |
---|
446 | int v; |
---|
447 | |
---|
448 | g_return_val_if_fail (widget != NULL, -1); |
---|
449 | g_return_val_if_fail (GTK_IS_RADIO_BUTTON (widget), -1); |
---|
450 | g_return_val_if_fail (value_map != NULL, -1); |
---|
451 | |
---|
452 | group = gtk_radio_button_group (GTK_RADIO_BUTTON (widget)); |
---|
453 | |
---|
454 | for (i = 0, l = group; l; l = l->next, i++) { |
---|
455 | widget = GTK_WIDGET (l->data); |
---|
456 | |
---|
457 | if (GTK_TOGGLE_BUTTON (widget)->active) |
---|
458 | break; |
---|
459 | } |
---|
460 | |
---|
461 | if (!l) |
---|
462 | g_assert_not_reached (); |
---|
463 | |
---|
464 | /* Groups are built by prepending items, so the list ends up in reverse |
---|
465 | * order; we need to flip the index around. |
---|
466 | */ |
---|
467 | i = g_slist_length (group) - i - 1; |
---|
468 | |
---|
469 | v = index_to_value (value_map, i); |
---|
470 | if (v == -1) { |
---|
471 | g_message ("e_dialog_radio_get(): could not find index %d in value map!", i); |
---|
472 | return -1; |
---|
473 | } |
---|
474 | |
---|
475 | return v; |
---|
476 | } |
---|
477 | |
---|
478 | /** |
---|
479 | * e_dialog_toggle_set: |
---|
480 | * @widget: A #GtkToggleButton. |
---|
481 | * @value: Toggle value. |
---|
482 | * |
---|
483 | * Sets the value of a #GtkToggleButton-derived widget. This should not be used |
---|
484 | * for radio buttons; it is more convenient to use use e_dialog_radio_set() |
---|
485 | * instead. |
---|
486 | **/ |
---|
487 | void |
---|
488 | e_dialog_toggle_set (GtkWidget *widget, gboolean value) |
---|
489 | { |
---|
490 | g_return_if_fail (widget != NULL); |
---|
491 | g_return_if_fail (GTK_IS_TOGGLE_BUTTON (widget)); |
---|
492 | |
---|
493 | gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), value); |
---|
494 | } |
---|
495 | |
---|
496 | /** |
---|
497 | * e_dialog_toggle_get: |
---|
498 | * @widget: A #GtkToggleButton. |
---|
499 | * |
---|
500 | * Queries the value of a #GtkToggleButton-derived widget. This should not be |
---|
501 | * used for radio buttons; it is more convenient to use e_dialog_radio_get() |
---|
502 | * instead. |
---|
503 | * |
---|
504 | * Return value: Toggle value. |
---|
505 | **/ |
---|
506 | gboolean |
---|
507 | e_dialog_toggle_get (GtkWidget *widget) |
---|
508 | { |
---|
509 | g_return_val_if_fail (widget != NULL, FALSE); |
---|
510 | g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (widget), FALSE); |
---|
511 | |
---|
512 | return GTK_TOGGLE_BUTTON (widget)->active; |
---|
513 | } |
---|
514 | |
---|
515 | /** |
---|
516 | * e_dialog_spin_set: |
---|
517 | * @widget: A #GtkSpinButton. |
---|
518 | * @value: Numeric value. |
---|
519 | * |
---|
520 | * Sets the value of a #GtkSpinButton widget. |
---|
521 | **/ |
---|
522 | void |
---|
523 | e_dialog_spin_set (GtkWidget *widget, double value) |
---|
524 | { |
---|
525 | GtkAdjustment *adj; |
---|
526 | |
---|
527 | g_return_if_fail (widget != NULL); |
---|
528 | g_return_if_fail (GTK_IS_SPIN_BUTTON (widget)); |
---|
529 | |
---|
530 | adj = gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (widget)); |
---|
531 | |
---|
532 | adj->value = value; |
---|
533 | gtk_signal_emit_by_name (GTK_OBJECT (adj), "value_changed"); |
---|
534 | } |
---|
535 | |
---|
536 | /** |
---|
537 | * e_dialog_spin_get_double: |
---|
538 | * @widget: A #GtkSpinButton. |
---|
539 | * |
---|
540 | * Queries the floating-point value of a #GtkSpinButton widget. |
---|
541 | * |
---|
542 | * Return value: Numeric value. |
---|
543 | **/ |
---|
544 | double |
---|
545 | e_dialog_spin_get_double (GtkWidget *widget) |
---|
546 | { |
---|
547 | GtkAdjustment *adj; |
---|
548 | |
---|
549 | g_return_val_if_fail (widget != NULL, 0.0); |
---|
550 | g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), 0.0); |
---|
551 | |
---|
552 | adj = gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (widget)); |
---|
553 | return adj->value; |
---|
554 | } |
---|
555 | |
---|
556 | /** |
---|
557 | * e_dialog_spin_get_int: |
---|
558 | * @widget: A #GtkSpinButton. |
---|
559 | * |
---|
560 | * Queries the integer value of a #GtkSpinButton widget. |
---|
561 | * |
---|
562 | * Return value: Numeric value. |
---|
563 | **/ |
---|
564 | int |
---|
565 | e_dialog_spin_get_int (GtkWidget *widget) |
---|
566 | { |
---|
567 | double value; |
---|
568 | |
---|
569 | g_return_val_if_fail (widget != NULL, -1); |
---|
570 | g_return_val_if_fail (GTK_IS_SPIN_BUTTON (widget), -1); |
---|
571 | |
---|
572 | value = e_dialog_spin_get_double (widget); |
---|
573 | return (int) floor (value); |
---|
574 | } |
---|
575 | |
---|
576 | /** |
---|
577 | * e_dialog_option_menu_set: |
---|
578 | * @widget: A #GtkOptionMenu. |
---|
579 | * @value: Enumerated value. |
---|
580 | * @value_map: Map from enumeration values to array indices. |
---|
581 | * |
---|
582 | * Sets the selected item in a #GtkOptionMenu. Please read the description of |
---|
583 | * e_dialog_radio_set() to see how @value_map maps enumeration values to item |
---|
584 | * indices. |
---|
585 | **/ |
---|
586 | void |
---|
587 | e_dialog_option_menu_set (GtkWidget *widget, int value, const int *value_map) |
---|
588 | { |
---|
589 | int i; |
---|
590 | |
---|
591 | g_return_if_fail (widget != NULL); |
---|
592 | g_return_if_fail (GTK_IS_OPTION_MENU (widget)); |
---|
593 | g_return_if_fail (value_map != NULL); |
---|
594 | |
---|
595 | i = value_to_index (value_map, value); |
---|
596 | |
---|
597 | if (i != -1) |
---|
598 | gtk_option_menu_set_history (GTK_OPTION_MENU (widget), i); |
---|
599 | else |
---|
600 | g_message ("e_dialog_option_menu_set(): could not find value %d in value map!", |
---|
601 | value); |
---|
602 | } |
---|
603 | |
---|
604 | /** |
---|
605 | * e_dialog_option_menu_get: |
---|
606 | * @widget: A #GtkOptionMenu. |
---|
607 | * @value_map: Map from enumeration values to array indices. |
---|
608 | * |
---|
609 | * Queries the selected item in a #GtkOptionMenu. Please read the description |
---|
610 | * of e_dialog_radio_set() to see how @value_map maps enumeration values to item |
---|
611 | * indices. |
---|
612 | * |
---|
613 | * Return value: Enumeration value which corresponds to the selected item in the |
---|
614 | * option menu. |
---|
615 | **/ |
---|
616 | int |
---|
617 | e_dialog_option_menu_get (GtkWidget *widget, const int *value_map) |
---|
618 | { |
---|
619 | GtkMenu *menu; |
---|
620 | GtkWidget *active; |
---|
621 | GList *children; |
---|
622 | GList *l; |
---|
623 | int i; |
---|
624 | int v; |
---|
625 | |
---|
626 | g_return_val_if_fail (widget != NULL, -1); |
---|
627 | g_return_val_if_fail (GTK_IS_OPTION_MENU (widget), -1); |
---|
628 | g_return_val_if_fail (value_map != NULL, -1); |
---|
629 | |
---|
630 | menu = GTK_MENU (gtk_option_menu_get_menu (GTK_OPTION_MENU (widget))); |
---|
631 | |
---|
632 | active = gtk_menu_get_active (menu); |
---|
633 | g_assert (active != NULL); |
---|
634 | |
---|
635 | children = GTK_MENU_SHELL (menu)->children; |
---|
636 | |
---|
637 | for (i = 0, l = children; l; l = l->next, i++) { |
---|
638 | if (GTK_WIDGET (l->data) == active) |
---|
639 | break; |
---|
640 | } |
---|
641 | |
---|
642 | if (!l) |
---|
643 | g_assert_not_reached (); |
---|
644 | |
---|
645 | v = index_to_value (value_map, i); |
---|
646 | if (v == -1) { |
---|
647 | g_message ("e_dialog_option_menu_get(): could not find index %d in value map!", i); |
---|
648 | return -1; |
---|
649 | } |
---|
650 | |
---|
651 | return v; |
---|
652 | } |
---|
653 | |
---|
654 | /** |
---|
655 | * e_dialog_dateedit_set: |
---|
656 | * @widget: A #GnomeDateEdit widget. |
---|
657 | * @t: Date/time value. |
---|
658 | * |
---|
659 | * Sets the value of a #GnomeDateEdit widget. |
---|
660 | **/ |
---|
661 | void |
---|
662 | e_dialog_dateedit_set (GtkWidget *widget, time_t t) |
---|
663 | { |
---|
664 | g_return_if_fail (widget != NULL); |
---|
665 | g_return_if_fail (GNOME_IS_DATE_EDIT (widget)); |
---|
666 | |
---|
667 | gnome_date_edit_set_time (GNOME_DATE_EDIT (widget), t); |
---|
668 | } |
---|
669 | |
---|
670 | /** |
---|
671 | * e_dialog_dateedit_get: |
---|
672 | * @widget: A #GnomeDateEdit widget. |
---|
673 | * |
---|
674 | * Queries the value of a #GnomeDateEdit widget. |
---|
675 | * |
---|
676 | * Return value: Date/time value. |
---|
677 | **/ |
---|
678 | time_t |
---|
679 | e_dialog_dateedit_get (GtkWidget *widget) |
---|
680 | { |
---|
681 | g_return_val_if_fail (widget != NULL, -1); |
---|
682 | g_return_val_if_fail (GNOME_IS_DATE_EDIT (widget), -1); |
---|
683 | |
---|
684 | return gnome_date_edit_get_date (GNOME_DATE_EDIT (widget)); |
---|
685 | } |
---|
686 | |
---|
687 | /** |
---|
688 | * e_dialog_widget_hook_value: |
---|
689 | * @dialog: Dialog box in which the @widget lives in. |
---|
690 | * @widget: A widget that will control a variable. |
---|
691 | * @value_var: Pointer to the variable that the @widget will control. |
---|
692 | * @info: NULL for most widgets, or an integer value map array (see |
---|
693 | * e_dialog_radio_set() for details). |
---|
694 | * |
---|
695 | * Hooks a widget from a dialog box to the variable it will modify. Supported |
---|
696 | * widgets are: #GtkEditable (char *), #GtkRadioButton (int/value_map pair; see |
---|
697 | * e_dialog_radio_set() for more information), #GtkTogglebutton (gboolean), |
---|
698 | * #GtkSpinButton (double), #GtkOptionMenu (int/value_map pair), and |
---|
699 | * #GnomeDateEdit (time_t). |
---|
700 | * |
---|
701 | * A pointer to the appropriate variable to modify should be passed in @value_var. |
---|
702 | * For values that take a value_map array as well, it should be passed in @info. |
---|
703 | * |
---|
704 | * The widgets within a dialog that are hooked with this function will set their |
---|
705 | * respective variables only when e_dialog_get_values() is called. The typical |
---|
706 | * use is to call that function in the handler for the "OK" button of a dialog |
---|
707 | * box. |
---|
708 | * |
---|
709 | * In addition, if the specified @dialog is a #GnomePropertyBox, the widgets wil |
---|
710 | * automatically turn on the "Apply" button of the property box when they are |
---|
711 | * modified by the user. |
---|
712 | * |
---|
713 | * Return value: TRUE if the type of the specified @widget is supported, FALSE |
---|
714 | * otherwise. |
---|
715 | **/ |
---|
716 | gboolean |
---|
717 | e_dialog_widget_hook_value (GtkWidget *dialog, GtkWidget *widget, |
---|
718 | gpointer value_var, gpointer info) |
---|
719 | { |
---|
720 | DialogHooks *hooks; |
---|
721 | WidgetHook *wh; |
---|
722 | |
---|
723 | g_return_val_if_fail (dialog != NULL, FALSE); |
---|
724 | g_return_val_if_fail (widget != NULL, FALSE); |
---|
725 | g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE); |
---|
726 | g_return_val_if_fail (value_var != NULL, FALSE); |
---|
727 | |
---|
728 | hooks = get_dialog_hooks (dialog); |
---|
729 | |
---|
730 | /* First check if it is a "group" widget, like a radio button or an |
---|
731 | * option menu. Then we check for normal ungrouped widgets. |
---|
732 | */ |
---|
733 | |
---|
734 | if (GTK_IS_RADIO_BUTTON (widget)) |
---|
735 | hook_radio (dialog, GTK_RADIO_BUTTON (widget), value_var, info); |
---|
736 | else if (GTK_IS_OPTION_MENU (widget)) |
---|
737 | hook_option_menu (dialog, GTK_OPTION_MENU (widget), value_var, info); |
---|
738 | else if (GTK_IS_TOGGLE_BUTTON (widget)) |
---|
739 | hook_toggle (dialog, GTK_TOGGLE_BUTTON (widget), value_var, info); |
---|
740 | else if (GTK_IS_SPIN_BUTTON (widget)) |
---|
741 | hook_spin_button (dialog, GTK_SPIN_BUTTON (widget), value_var, info); |
---|
742 | else if (GTK_IS_EDITABLE (widget)) |
---|
743 | hook_editable (dialog, GTK_EDITABLE (widget), value_var, info); |
---|
744 | else |
---|
745 | return FALSE; |
---|
746 | |
---|
747 | wh = g_new (WidgetHook, 1); |
---|
748 | wh->widget = widget; |
---|
749 | wh->value_var = value_var; |
---|
750 | wh->info = info; |
---|
751 | |
---|
752 | hooks->whooks = g_slist_prepend (hooks->whooks, wh); |
---|
753 | |
---|
754 | return TRUE; |
---|
755 | } |
---|
756 | |
---|
757 | /** |
---|
758 | * e_dialog_get_values: |
---|
759 | * @dialog: A dialog box whose widgets have been hooked to the appropriate |
---|
760 | * variables with e_dialog_widget_hook_value(). |
---|
761 | * |
---|
762 | * Makes every widget in a @dialog that was hooked with |
---|
763 | * e_dialog_widget_hook_value() apply its value to its corresponding variable. |
---|
764 | * The typical usage is to call this function in the handler for the "OK" button |
---|
765 | * of a dialog box. |
---|
766 | **/ |
---|
767 | void |
---|
768 | e_dialog_get_values (GtkWidget *dialog) |
---|
769 | { |
---|
770 | DialogHooks *hooks; |
---|
771 | GSList *l; |
---|
772 | |
---|
773 | g_return_if_fail (dialog != NULL); |
---|
774 | |
---|
775 | hooks = get_dialog_hooks (dialog); |
---|
776 | |
---|
777 | for (l = hooks->whooks; l; l = l->next) { |
---|
778 | WidgetHook *wh; |
---|
779 | |
---|
780 | wh = l->data; |
---|
781 | |
---|
782 | if (GTK_IS_RADIO_BUTTON (wh->widget)) |
---|
783 | get_radio_value (GTK_RADIO_BUTTON (wh->widget), wh->value_var, wh->info); |
---|
784 | else if (GTK_IS_OPTION_MENU (wh->widget)) |
---|
785 | get_option_menu_value (GTK_OPTION_MENU (wh->widget), wh->value_var, wh->info); |
---|
786 | else if (GTK_IS_TOGGLE_BUTTON (wh->widget)) |
---|
787 | get_toggle_value (GTK_TOGGLE_BUTTON (wh->widget), wh->value_var, wh->info); |
---|
788 | else if (GTK_IS_SPIN_BUTTON (wh->widget)) |
---|
789 | get_spin_button_value (GTK_SPIN_BUTTON (wh->widget), wh->value_var, wh->info); |
---|
790 | else if (GTK_IS_EDITABLE (wh->widget)) |
---|
791 | get_editable_value (GTK_EDITABLE (wh->widget), wh->value_var, wh->info); |
---|
792 | else |
---|
793 | g_assert_not_reached (); |
---|
794 | } |
---|
795 | } |
---|
796 | |
---|
797 | /** |
---|
798 | * e_dialog_xml_widget_hook_value: |
---|
799 | * @xml: Glade XML description of a dialog box. |
---|
800 | * @dialog: Dialog box in which the widget lives in. |
---|
801 | * @widget_name: Name of the widget in the Glade XML data. |
---|
802 | * @value_var: Pointer to the variable that the widget will control. |
---|
803 | * @info: NULL for most widgets, or an integer value map array (see |
---|
804 | * e_dialog_radio_set() for details). |
---|
805 | * |
---|
806 | * Similar to e_dialog_widget_hook_value(), but uses the widget from a #GladeXML |
---|
807 | * data structure. |
---|
808 | * |
---|
809 | * Return value: TRUE if the type of the specified widget is supported, FALSE |
---|
810 | * otherwise. |
---|
811 | **/ |
---|
812 | gboolean |
---|
813 | e_dialog_xml_widget_hook_value (GladeXML *xml, GtkWidget *dialog, const char *widget_name, |
---|
814 | gpointer value_var, gpointer info) |
---|
815 | { |
---|
816 | GtkWidget *widget; |
---|
817 | |
---|
818 | g_return_val_if_fail (xml != NULL, FALSE); |
---|
819 | g_return_val_if_fail (GLADE_IS_XML (xml), FALSE); |
---|
820 | g_return_val_if_fail (dialog != NULL, FALSE); |
---|
821 | g_return_val_if_fail (widget_name != NULL, FALSE); |
---|
822 | g_return_val_if_fail (value_var != NULL, FALSE); |
---|
823 | |
---|
824 | widget = glade_xml_get_widget (xml, widget_name); |
---|
825 | if (!widget) { |
---|
826 | g_message ("e_dialog_xml_widget_hook_value(): could not find widget `%s' in " |
---|
827 | "Glade data!", widget_name); |
---|
828 | return FALSE; |
---|
829 | } |
---|
830 | |
---|
831 | return e_dialog_widget_hook_value (dialog, widget, value_var, info); |
---|
832 | } |
---|