fcc35ce97e5e62cd364357c2d6cf68a1ae224a60
[geeqie.git] / src / ui_misc.c
1 /*
2  * (SLIK) SimpLIstic sKin functions
3  * (C) 2004 John Ellis
4  * Copyright (C) 2008 - 2012 The Geeqie Team
5  *
6  * Author: John Ellis
7  *
8  * This software is released under the GNU General Public License (GNU GPL).
9  * Please read the included file COPYING for more information.
10  * This software comes with no warranty of any kind, use at your own risk!
11  */
12
13 #ifdef HAVE_CONFIG_H
14 #  include "config.h"
15 #endif
16 #include "intl.h"
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21
22 #include <gtk/gtk.h>
23 #include <gdk/gdkkeysyms.h>
24
25 #include "main.h"
26 #include "ui_misc.h"
27
28 #include "history_list.h"
29
30
31 /*
32  *-----------------------------------------------------------------------------
33  * widget and layout utilities
34  *-----------------------------------------------------------------------------
35  */
36
37 GtkWidget *pref_box_new(GtkWidget *parent_box, gboolean fill,
38                         GtkOrientation orientation, gboolean padding)
39 {
40         GtkWidget *box;
41
42         if (orientation == GTK_ORIENTATION_HORIZONTAL)
43                 {
44                 box = gtk_hbox_new(FALSE, padding);
45                 }
46         else
47                 {
48                 box = gtk_vbox_new(FALSE, padding);
49                 }
50
51         gtk_box_pack_start(GTK_BOX(parent_box), box, fill, fill, 0);
52         gtk_widget_show(box);
53
54         return box;
55 }
56
57 GtkWidget *pref_group_new(GtkWidget *parent_box, gboolean fill,
58                           const gchar *text, GtkOrientation orientation)
59 {
60         GtkWidget *box;
61         GtkWidget *vbox;
62         GtkWidget *hbox;
63         GtkWidget *label;
64
65         vbox = gtk_vbox_new(FALSE, PREF_PAD_GAP);
66
67         /* add additional spacing if necessary */
68         if (GTK_IS_VBOX(parent_box))
69                 {
70                 GList *list = gtk_container_get_children(GTK_CONTAINER(parent_box));
71                 if (list)
72                         {
73                         pref_spacer(vbox, PREF_PAD_GROUP - PREF_PAD_GAP);
74                         }
75                 g_list_free(list);
76                 }
77
78         gtk_box_pack_start(GTK_BOX(parent_box), vbox, fill, fill, 0);
79         gtk_widget_show(vbox);
80
81         label = gtk_label_new(text);
82         gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
83         pref_label_bold(label, TRUE, FALSE);
84
85         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
86         gtk_widget_show(label);
87
88         hbox = gtk_hbox_new(FALSE, PREF_PAD_INDENT);
89         gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 0);
90         gtk_widget_show(hbox);
91
92         /* indent using empty box */
93         pref_spacer(hbox, 0);
94
95         if (orientation == GTK_ORIENTATION_HORIZONTAL)
96                 {
97                 box = gtk_hbox_new(FALSE, PREF_PAD_SPACE);
98                 }
99         else
100                 {
101                 box = gtk_vbox_new(FALSE, PREF_PAD_GAP);
102                 }
103         gtk_box_pack_start(GTK_BOX(hbox), box, TRUE, TRUE, 0);
104         gtk_widget_show(box);
105
106         g_object_set_data(G_OBJECT(box), "pref_group", vbox);
107
108         return box;
109 }
110
111 GtkWidget *pref_group_parent(GtkWidget *child)
112 {
113         GtkWidget *parent;
114
115         parent = child;
116         while (parent)
117                 {
118                 GtkWidget *group;
119
120                 group = g_object_get_data(G_OBJECT(parent), "pref_group");
121                 if (group && GTK_IS_WIDGET(group)) return group;
122
123                 parent = gtk_widget_get_parent(parent);
124                 }
125
126         return child;
127 }
128
129 GtkWidget *pref_frame_new(GtkWidget *parent_box, gboolean fill,
130                           const gchar *text,
131                           GtkOrientation orientation, gboolean padding)
132 {
133         GtkWidget *box;
134         GtkWidget *frame = NULL;
135
136         frame = gtk_frame_new(text);
137         gtk_box_pack_start(GTK_BOX(parent_box), frame, fill, fill, 0);
138         gtk_widget_show(frame);
139
140         if (orientation == GTK_ORIENTATION_HORIZONTAL)
141                 {
142                 box = gtk_hbox_new(FALSE, padding);
143                 }
144         else
145                 {
146                 box = gtk_vbox_new(FALSE, padding);
147                 }
148         gtk_container_add(GTK_CONTAINER(frame), box);
149         gtk_container_set_border_width(GTK_CONTAINER(box), PREF_PAD_BORDER);
150         gtk_widget_show(box);
151
152         return box;
153 }
154
155 GtkWidget *pref_spacer(GtkWidget *parent_box, gboolean padding)
156 {
157         GtkWidget *spacer;
158
159         spacer = gtk_hbox_new(FALSE, 0);
160         gtk_box_pack_start(GTK_BOX(parent_box), spacer, FALSE, FALSE, padding / 2);
161         gtk_widget_show(spacer);
162
163         return spacer;
164 }
165
166 GtkWidget *pref_line(GtkWidget *parent_box, gboolean padding)
167 {
168         GtkWidget *spacer;
169
170         if (GTK_IS_HBOX(parent_box))
171                 {
172                 spacer = gtk_vseparator_new();
173                 }
174         else
175                 {
176                 spacer = gtk_hseparator_new();
177                 }
178
179         gtk_box_pack_start(GTK_BOX(parent_box), spacer, FALSE, FALSE, padding / 2);
180         gtk_widget_show(spacer);
181
182         return spacer;
183 }
184
185 GtkWidget *pref_label_new(GtkWidget *parent_box, const gchar *text)
186 {
187         GtkWidget *label;
188
189         label = gtk_label_new(text);
190         gtk_box_pack_start(GTK_BOX(parent_box), label, FALSE, FALSE, 0);
191         gtk_widget_show(label);
192
193         return label;
194 }
195
196 GtkWidget *pref_label_new_mnemonic(GtkWidget *parent_box, const gchar *text, GtkWidget *widget)
197 {
198         GtkWidget *label;
199
200         label = gtk_label_new_with_mnemonic(text);
201         gtk_label_set_mnemonic_widget(GTK_LABEL(label), widget);
202         gtk_box_pack_start(GTK_BOX(parent_box), label, FALSE, FALSE, 0);
203         gtk_widget_show(label);
204
205         return label;
206 }
207
208 void pref_label_bold(GtkWidget *label, gboolean bold, gboolean increase_size)
209 {
210         PangoAttrList *pal;
211         PangoAttribute *pa;
212
213         if (!bold && !increase_size) return;
214
215         pal = pango_attr_list_new();
216
217         if (bold)
218                 {
219                 pa = pango_attr_weight_new(PANGO_WEIGHT_BOLD);
220                 pa->start_index = 0;
221                 pa->end_index = G_MAXINT;
222                 pango_attr_list_insert(pal, pa);
223                 }
224
225         if (increase_size)
226                 {
227                 pa = pango_attr_scale_new(PANGO_SCALE_LARGE);
228                 pa->start_index = 0;
229                 pa->end_index = G_MAXINT;
230                 pango_attr_list_insert(pal, pa);
231                 }
232
233         gtk_label_set_attributes(GTK_LABEL(label), pal);
234         pango_attr_list_unref(pal);
235 }
236
237 GtkWidget *pref_button_new(GtkWidget *parent_box, const gchar *stock_id,
238                            const gchar *text, gboolean hide_stock_text,
239                            GCallback func, gpointer data)
240 {
241         GtkWidget *button;
242
243         if (stock_id && !text && !hide_stock_text)
244                 {
245                 button = gtk_button_new_from_stock(stock_id);
246                 }
247         else
248                 {
249                 GtkWidget *image = NULL;
250                 GtkWidget *label = NULL;
251
252                 button = gtk_button_new();
253
254                 if (stock_id) image = gtk_image_new_from_stock(stock_id, GTK_ICON_SIZE_BUTTON);
255                 if (text)
256                         {
257                         label = gtk_label_new_with_mnemonic(text);
258                         gtk_misc_set_alignment(GTK_MISC(label), 0.5, 0.5);
259                         gtk_label_set_mnemonic_widget(GTK_LABEL(label), button);
260                         }
261
262                 if (image && label)
263                         {
264                         GtkWidget *align;
265                         GtkWidget *hbox;
266
267                         hbox = gtk_hbox_new(FALSE, PREF_PAD_BUTTON_ICON_GAP);
268
269                         align = gtk_alignment_new(0.5, 0.5, 0.0, 0.0);
270                         gtk_container_add(GTK_CONTAINER(button), align);
271                         gtk_widget_show(align);
272
273                         gtk_container_add(GTK_CONTAINER(align), hbox);
274                         gtk_widget_show(hbox);
275
276                         gtk_box_pack_start(GTK_BOX(hbox), image, FALSE, FALSE, 0);
277                         gtk_box_pack_end(GTK_BOX(hbox), label, FALSE, FALSE, 0);
278                         }
279                 else
280                         {
281                         if (image)
282                                 {
283                                 gtk_container_add(GTK_CONTAINER(button), image);
284                                 }
285                         else if (label)
286                                 {
287                                 gtk_container_add(GTK_CONTAINER(button), label);
288                                 }
289                         }
290
291                 if (image) gtk_widget_show(image);
292                 if (label) gtk_widget_show(label);
293                 }
294
295         if (func) g_signal_connect(G_OBJECT(button), "clicked", func, data);
296
297         if (parent_box)
298                 {
299                 gtk_box_pack_start(GTK_BOX(parent_box), button, FALSE, FALSE, 0);
300                 gtk_widget_show(button);
301                 }
302
303         return button;
304 }
305
306 static GtkWidget *real_pref_checkbox_new(GtkWidget *parent_box, const gchar *text, gboolean mnemonic_text,
307                                          gboolean active, GCallback func, gpointer data)
308 {
309         GtkWidget *button;
310
311         if (mnemonic_text)
312                 {
313                 button = gtk_check_button_new_with_mnemonic(text);
314                 }
315         else
316                 {
317                 button = gtk_check_button_new_with_label(text);
318                 }
319         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), active);
320         if (func) g_signal_connect(G_OBJECT(button), "clicked", func, data);
321
322         gtk_box_pack_start(GTK_BOX(parent_box), button, FALSE, FALSE, 0);
323         gtk_widget_show(button);
324
325         return button;
326 }
327
328 GtkWidget *pref_checkbox_new(GtkWidget *parent_box, const gchar *text, gboolean active,
329                              GCallback func, gpointer data)
330 {
331         return real_pref_checkbox_new(parent_box, text, FALSE, active, func, data);
332 }
333
334 GtkWidget *pref_checkbox_new_mnemonic(GtkWidget *parent_box, const gchar *text, gboolean active,
335                                       GCallback func, gpointer data)
336 {
337         return real_pref_checkbox_new(parent_box, text, TRUE, active, func, data);
338 }
339
340 static void pref_checkbox_int_cb(GtkWidget *widget, gpointer data)
341 {
342         gboolean *result = data;
343
344         *result = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
345 }
346
347 GtkWidget *pref_checkbox_new_int(GtkWidget *parent_box, const gchar *text, gboolean active,
348                                  gboolean *result)
349 {
350         GtkWidget *button;
351
352         button = pref_checkbox_new(parent_box, text, active,
353                                    G_CALLBACK(pref_checkbox_int_cb), result);
354         *result = active;
355
356         return button;
357 }
358
359 static void pref_checkbox_link_sensitivity_cb(GtkWidget *button, gpointer data)
360 {
361         GtkWidget *widget = data;
362
363         gtk_widget_set_sensitive(widget, gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button)));
364 }
365
366 void pref_checkbox_link_sensitivity(GtkWidget *button, GtkWidget *widget)
367 {
368         g_signal_connect(G_OBJECT(button), "toggled",
369                          G_CALLBACK(pref_checkbox_link_sensitivity_cb), widget);
370
371         pref_checkbox_link_sensitivity_cb(button, widget);
372 }
373
374 static void pref_checkbox_link_sensitivity_swap_cb(GtkWidget *button, gpointer data)
375 {
376         GtkWidget *widget = data;
377
378         gtk_widget_set_sensitive(widget, !gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button)));
379 }
380
381 void pref_checkbox_link_sensitivity_swap(GtkWidget *button, GtkWidget *widget)
382 {
383         g_signal_connect(G_OBJECT(button), "toggled",
384                          G_CALLBACK(pref_checkbox_link_sensitivity_swap_cb), widget);
385
386         pref_checkbox_link_sensitivity_swap_cb(button, widget);
387 }
388
389 static GtkWidget *real_pref_radiobutton_new(GtkWidget *parent_box, GtkWidget *sibling,
390                                             const gchar *text, gboolean mnemonic_text, gboolean active,
391                                             GCallback func, gpointer data)
392 {
393         GtkWidget *button;
394         GSList *group;
395
396         if (sibling)
397                 {
398                 group = gtk_radio_button_get_group(GTK_RADIO_BUTTON(sibling));
399                 }
400         else
401                 {
402                 group = NULL;
403                 }
404
405         if (mnemonic_text)
406                 {
407                 button = gtk_radio_button_new_with_mnemonic(group, text);
408                 }
409         else
410                 {
411                 button = gtk_radio_button_new_with_label(group, text);
412                 }
413
414         if (active) gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), active);
415         if (func) g_signal_connect(G_OBJECT(button), "clicked", func, data);
416
417         gtk_box_pack_start(GTK_BOX(parent_box), button, FALSE, FALSE, 0);
418         gtk_widget_show(button);
419
420         return button;
421 }
422
423 GtkWidget *pref_radiobutton_new(GtkWidget *parent_box, GtkWidget *sibling,
424                                 const gchar *text, gboolean active,
425                                 GCallback func, gpointer data)
426 {
427         return real_pref_radiobutton_new(parent_box, sibling, text, FALSE, active, func, data);
428 }
429
430 GtkWidget *pref_radiobutton_new_mnemonic(GtkWidget *parent_box, GtkWidget *sibling,
431                                          const gchar *text, gboolean active,
432                                          GCallback func, gpointer data)
433 {
434         return real_pref_radiobutton_new(parent_box, sibling, text, TRUE, active, func, data);
435 }
436
437 #define PREF_RADIO_VALUE_KEY "pref_radio_value"
438
439 static void pref_radiobutton_int_cb(GtkWidget *widget, gpointer data)
440 {
441         gboolean *result = data;
442
443         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
444                 {
445                 *result = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), PREF_RADIO_VALUE_KEY));
446                 }
447 }
448
449 GtkWidget *pref_radiobutton_new_int(GtkWidget *parent_box, GtkWidget *sibling,
450                                     const gchar *text, gboolean active,
451                                     gboolean *result, gboolean value,
452                                     GCallback func, gpointer data)
453 {
454         GtkWidget *button;
455
456         button = pref_radiobutton_new(parent_box, sibling, text, active,
457                                       G_CALLBACK(pref_radiobutton_int_cb), result);
458         g_object_set_data(G_OBJECT(button), PREF_RADIO_VALUE_KEY, GINT_TO_POINTER(value));
459         if (active) *result = value;
460
461         return button;
462 }
463
464 static GtkWidget *real_pref_spin_new(GtkWidget *parent_box, const gchar *text, const gchar *suffix,
465                                      gboolean mnemonic_text,
466                                      gdouble min, gdouble max, gdouble step, gint digits,
467                                      gdouble value,
468                                      GCallback func, gpointer data)
469 {
470         GtkWidget *spin;
471         GtkWidget *box;
472         GtkWidget *label;
473
474         box = pref_box_new(parent_box, FALSE, GTK_ORIENTATION_HORIZONTAL, PREF_PAD_SPACE);
475
476         spin = gtk_spin_button_new_with_range(min, max, step);
477         gtk_spin_button_set_digits(GTK_SPIN_BUTTON(spin), digits);
478         gtk_spin_button_set_value(GTK_SPIN_BUTTON(spin), value);
479
480         if (func)
481                 {
482                 g_signal_connect(G_OBJECT(spin), "value_changed", G_CALLBACK(func), data);
483                 }
484
485         if (text)
486                 {
487                 if (mnemonic_text)
488                         {
489                         label = pref_label_new_mnemonic(box, text, spin);
490                         }
491                 else
492                         {
493                         label = pref_label_new(box, text);
494                         }
495                 pref_link_sensitivity(label, spin);
496                 }
497
498         gtk_box_pack_start(GTK_BOX(box), spin, FALSE, FALSE, 0);
499         gtk_widget_show(spin);
500
501         /* perhaps this should only be PREF_PAD_GAP distance from spinbutton ? */
502         if (suffix)
503                 {
504                 label =  pref_label_new(box, suffix);
505                 pref_link_sensitivity(label, spin);
506                 }
507
508         return spin;
509 }
510
511 GtkWidget *pref_spin_new(GtkWidget *parent_box, const gchar *text, const gchar *suffix,
512                          gdouble min, gdouble max, gdouble step, gint digits,
513                          gdouble value,
514                          GCallback func, gpointer data)
515 {
516         return real_pref_spin_new(parent_box, text, suffix, FALSE,
517                                   min, max, step, digits, value, func, data);
518 }
519
520 GtkWidget *pref_spin_new_mnemonic(GtkWidget *parent_box, const gchar *text, const gchar *suffix,
521                                   gdouble min, gdouble max, gdouble step, gint digits,
522                                   gdouble value,
523                                   GCallback func, gpointer data)
524 {
525         return real_pref_spin_new(parent_box, text, suffix, TRUE,
526                                   min, max, step, digits, value, func, data);
527 }
528
529 static void pref_spin_int_cb(GtkWidget *widget, gpointer data)
530 {
531         gint *var = data;
532         *var = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(widget));
533 }
534
535 GtkWidget *pref_spin_new_int(GtkWidget *parent_box, const gchar *text, const gchar *suffix,
536                              gint min, gint max, gint step,
537                              gint value, gint *value_var)
538 {
539         *value_var = value;
540         return pref_spin_new(parent_box, text, suffix,
541                              (gdouble)min, (gdouble)max, (gdouble)step, 0,
542                              value,
543                              G_CALLBACK(pref_spin_int_cb), value_var);
544 }
545
546 #if 0
547 void pref_spin_set_blocking(GtkWidget *spin, gdouble value, gpointer block_data)
548 {
549         g_signal_handlers_block_matched(G_OBJECT(spin), G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, block_data);
550         gtk_spin_button_set_value(GTK_SPIN_BUTTON(spin), value);
551         g_signal_handlers_unblock_matched(G_OBJECT(spin), G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, block_data);
552 }
553 #endif
554
555 static void pref_link_sensitivity_cb(GtkWidget *watch, GtkStateType prev_state, gpointer data)
556 {
557         GtkWidget *widget = data;
558
559 #if GTK_CHECK_VERSION(2,20,0)
560         gtk_widget_set_sensitive(widget, gtk_widget_is_sensitive(watch));
561 #else
562         gtk_widget_set_sensitive(widget, GTK_WIDGET_IS_SENSITIVE(watch));
563 #endif
564 }
565
566 void pref_link_sensitivity(GtkWidget *widget, GtkWidget *watch)
567 {
568         g_signal_connect(G_OBJECT(watch), "state_changed",
569                          G_CALLBACK(pref_link_sensitivity_cb), widget);
570 }
571
572 void pref_signal_block_data(GtkWidget *widget, gpointer data)
573 {
574         g_signal_handlers_block_matched(widget, G_SIGNAL_MATCH_DATA,
575                                         0, 0, NULL, NULL, data);
576 }
577
578 void pref_signal_unblock_data(GtkWidget *widget, gpointer data)
579 {
580         g_signal_handlers_unblock_matched(widget, G_SIGNAL_MATCH_DATA,
581                                           0, 0, NULL, NULL, data);
582 }
583
584 GtkWidget *pref_table_new(GtkWidget *parent_box, gint columns, gint rows,
585                           gboolean homogeneous, gboolean fill)
586 {
587         GtkWidget *table;
588
589         table = gtk_table_new(rows, columns, homogeneous);
590         gtk_table_set_row_spacings(GTK_TABLE(table), PREF_PAD_GAP);
591         gtk_table_set_col_spacings(GTK_TABLE(table), PREF_PAD_SPACE);
592
593         if (parent_box)
594                 {
595                 gtk_box_pack_start(GTK_BOX(parent_box), table, fill, fill, 0);
596                 gtk_widget_show(table);
597                 }
598
599         return table;
600 }
601
602 GtkWidget *pref_table_box(GtkWidget *table, gint column, gint row,
603                           GtkOrientation orientation, const gchar *text)
604 {
605         GtkWidget *box;
606         GtkWidget *shell;
607
608         if (text)
609                 {
610                 shell = gtk_vbox_new(FALSE, 0);
611                 box = pref_group_new(shell, TRUE, text, orientation);
612                 }
613         else
614                 {
615                 if (orientation == GTK_ORIENTATION_HORIZONTAL)
616                         {
617                         box = gtk_hbox_new(FALSE, PREF_PAD_SPACE);
618                         }
619                 else
620                         {
621                         box = gtk_vbox_new(FALSE, PREF_PAD_GAP);
622                         }
623                 shell = box;
624                 }
625
626         gtk_table_attach(GTK_TABLE(table), shell, column, column + 1, row, row + 1,
627                          GTK_EXPAND | GTK_FILL, 0, 0, 0);
628
629         gtk_widget_show(shell);
630
631         return box;
632 }
633
634 GtkWidget *pref_table_label(GtkWidget *table, gint column, gint row,
635                             const gchar *text, gfloat alignment)
636 {
637         GtkWidget *label;
638         GtkWidget *align;
639
640         align = gtk_alignment_new(alignment, 0.50, 0.0, 0.0);
641         gtk_table_attach(GTK_TABLE(table), align, column, column + 1, row, row + 1,
642                          GTK_FILL, 0, 0, 0);
643         gtk_widget_show(align);
644         label = gtk_label_new(text);
645         gtk_container_add(GTK_CONTAINER(align), label);
646         gtk_widget_show(label);
647
648         return label;
649 }
650
651 GtkWidget *pref_table_button(GtkWidget *table, gint column, gint row,
652                              const gchar *stock_id, const gchar *text, gboolean hide_stock_text,
653                              GCallback func, gpointer data)
654 {
655         GtkWidget *button;
656
657         button = pref_button_new(NULL, stock_id, text, hide_stock_text, func, data);
658         gtk_table_attach(GTK_TABLE(table), button, column, column + 1, row, row + 1,
659                          GTK_FILL, 0, 0, 0);
660         gtk_widget_show(button);
661
662         return button;
663 }
664
665 #if 0
666 static GtkWidget *pref_table_checkbox(GtkWidget *table, gint column, gint row,
667                                       const gchar *text, gint active,
668                                       GCallback func, gpointer data)
669 {
670         GtkWidget *button;
671
672         button = gtk_check_button_new_with_label(text);
673         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), active);
674         if (func) g_signal_connect(G_OBJECT(button), "clicked", func, data);
675
676         gtk_table_attach(GTK_TABLE(table), button, column, column + 1, row, row + 1,
677                          GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
678         gtk_widget_show(button);
679
680         return button;
681 }
682 #endif
683
684 GtkWidget *pref_table_spin(GtkWidget *table, gint column, gint row,
685                            const gchar *text, const gchar *suffix,
686                            gdouble min, gdouble max, gdouble step, gint digits,
687                            gdouble value,
688                            GCallback func, gpointer data)
689 {
690         GtkWidget *spin;
691         GtkWidget *box;
692         GtkWidget *label;
693
694         spin = gtk_spin_button_new_with_range(min, max, step);
695         gtk_spin_button_set_digits(GTK_SPIN_BUTTON(spin), digits);
696         gtk_spin_button_set_value(GTK_SPIN_BUTTON(spin), value);
697         if (func)
698                 {
699                 g_signal_connect(G_OBJECT(spin), "value_changed", G_CALLBACK(func), data);
700                 }
701
702         if (text)
703                 {
704                 label = pref_table_label(table, column, row, text, 1.0);
705                 pref_link_sensitivity(label, spin);
706                 column++;
707                 }
708
709         if (suffix)
710                 {
711                 box = gtk_hbox_new(FALSE, PREF_PAD_SPACE);
712                 gtk_box_pack_start(GTK_BOX(box), spin, FALSE, FALSE, 0);
713                 gtk_widget_show(spin);
714
715                 label = pref_label_new(box, suffix);
716                 pref_link_sensitivity(label, spin);
717                 }
718         else
719                 {
720                 box = spin;
721                 }
722
723         gtk_table_attach(GTK_TABLE(table), box, column, column + 1, row, row + 1,
724                          GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
725         gtk_widget_show(box);
726
727         return spin;
728 }
729
730 GtkWidget *pref_table_spin_new_int(GtkWidget *table, gint column, gint row,
731                                    const gchar *text, const gchar *suffix,
732                                    gint min, gint max, gint step,
733                                    gint value, gint *value_var)
734 {
735         *value_var = value;
736         return pref_table_spin(table, column, row,
737                                text, suffix,
738                                (gdouble)min, (gdouble)max, (gdouble)step, 0,
739                                value,
740                                G_CALLBACK(pref_spin_int_cb), value_var);
741 }
742
743
744 #if ! GTK_CHECK_VERSION(2,12,0)
745
746 static void pref_toolbar_destroy_cb(GtkWidget *widget, gpointer data)
747 {
748         GtkTooltips *tips = data;
749
750         g_object_unref(G_OBJECT(tips));
751 }
752
753 #endif
754
755 GtkWidget *pref_toolbar_new(GtkWidget *parent_box, GtkToolbarStyle style)
756 {
757         GtkWidget *tbar;
758 #if ! GTK_CHECK_VERSION(2,12,0)
759         GtkTooltips *tips;
760 #endif
761         
762         tbar = gtk_toolbar_new();
763         gtk_toolbar_set_style(GTK_TOOLBAR(tbar), style);
764
765         if (parent_box)
766                 {
767                 gtk_box_pack_start(GTK_BOX(parent_box), tbar, FALSE, FALSE, 0);
768                 gtk_widget_show(tbar);
769                 }
770
771 #if ! GTK_CHECK_VERSION(2,12,0)
772         tips = gtk_tooltips_new();
773
774         /* take ownership of tooltips */
775 #  ifdef GTK_OBJECT_FLOATING
776         /* GTK+ < 2.10 */
777         g_object_ref(G_OBJECT(tips));
778         gtk_object_sink(GTK_OBJECT(tips));
779 #  else
780         /* GTK+ >= 2.10 */
781         g_object_ref_sink(G_OBJECT(tips));
782 #  endif
783
784         g_object_set_data(G_OBJECT(tbar), "tooltips", tips);
785         g_signal_connect(G_OBJECT(tbar), "destroy",
786                          G_CALLBACK(pref_toolbar_destroy_cb), tips);
787
788         gtk_tooltips_enable(tips);
789 #endif
790
791         return tbar;
792 }
793
794 GtkWidget *pref_toolbar_button(GtkWidget *toolbar,
795                                const gchar *stock_id, const gchar *label, gboolean toggle,
796                                const gchar *description,
797                                GCallback func, gpointer data)
798 {
799         GtkWidget *item;
800
801         if (toggle)
802                 {
803                 if (stock_id)
804                         {
805                         item = GTK_WIDGET(gtk_toggle_tool_button_new_from_stock(stock_id));
806                         }
807                 else
808                         {
809                         item = GTK_WIDGET(gtk_toggle_tool_button_new());
810                         }
811                 }
812         else
813                 {
814                 if (stock_id)
815                         {
816                         item = GTK_WIDGET(gtk_tool_button_new_from_stock(stock_id));
817                         }
818                 else
819                         {
820                         item = GTK_WIDGET(gtk_tool_button_new(NULL, NULL));
821                         }
822                 }
823         gtk_tool_button_set_use_underline(GTK_TOOL_BUTTON(item), TRUE);
824
825         if (label) gtk_tool_button_set_label(GTK_TOOL_BUTTON(item), label);
826
827         if (func) g_signal_connect(item, "clicked", func, data);
828         gtk_container_add(GTK_CONTAINER(toolbar), item);
829         gtk_widget_show(item);
830
831         if (description)
832                 {
833
834 #if GTK_CHECK_VERSION(2,12,0)
835
836                 gtk_widget_set_tooltip_text(item, description);
837                         
838 #else
839                 GtkTooltips *tips;
840
841                 tips = g_object_get_data(G_OBJECT(toolbar), "tooltips");
842                 gtk_tool_item_set_tooltip(GTK_TOOL_ITEM(item), tips, description, NULL);
843 #endif
844         }
845
846         return item;
847 }
848
849 void pref_toolbar_button_set_icon(GtkWidget *button, GtkWidget *widget, const gchar *stock_id)
850 {
851         if (widget)
852                 {
853                 gtk_tool_button_set_icon_widget(GTK_TOOL_BUTTON(button), widget);
854                 }
855         else if (stock_id)
856                 {
857                 gtk_tool_button_set_stock_id(GTK_TOOL_BUTTON(button), stock_id);
858                 }
859 }
860
861 GtkWidget *pref_toolbar_spacer(GtkWidget *toolbar)
862 {
863         GtkWidget *item;
864
865         item = GTK_WIDGET(gtk_separator_tool_item_new());
866         gtk_container_add(GTK_CONTAINER(toolbar), item);
867         gtk_widget_show(item);
868
869         return item;
870 }
871
872
873 /*
874  *-----------------------------------------------------------------------------
875  * date selection entry
876  *-----------------------------------------------------------------------------
877  */
878
879 #define DATE_SELECION_KEY "date_selection_data"
880
881
882 typedef struct _DateSelection DateSelection;
883 struct _DateSelection
884 {
885         GtkWidget *box;
886
887         GtkWidget *spin_d;
888         GtkWidget *spin_m;
889         GtkWidget *spin_y;
890
891         GtkWidget *button;
892
893         GtkWidget *window;
894         GtkWidget *calendar;
895 };
896
897
898 static void date_selection_popup_hide(DateSelection *ds)
899 {
900         if (!ds->window) return;
901
902 #if GTK_CHECK_VERSION(2,20,0)
903         if (gtk_widget_has_grab(ds->window))
904 #else
905         if (GTK_WIDGET_HAS_GRAB(ds->window))
906 #endif
907                 {
908                 gtk_grab_remove(ds->window);
909                 gdk_keyboard_ungrab(GDK_CURRENT_TIME);
910                 gdk_pointer_ungrab(GDK_CURRENT_TIME);
911                 }
912
913         gtk_widget_hide(ds->window);
914
915         gtk_widget_destroy(ds->window);
916         ds->window = NULL;
917         ds->calendar = NULL;
918
919         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ds->button), FALSE);
920 }
921
922 static gboolean date_selection_popup_release_cb(GtkWidget *widget, GdkEventButton *event, gpointer data)
923 {
924         DateSelection *ds = data;
925
926         date_selection_popup_hide(ds);
927         return TRUE;
928 }
929
930 static gboolean date_selection_popup_press_cb(GtkWidget *widget, GdkEventButton *event, gpointer data)
931 {
932         DateSelection *ds = data;
933         gint x, y;
934         gint w, h;
935         gint xr, yr;
936         GdkWindow *window;
937
938         xr = (gint)event->x_root;
939         yr = (gint)event->y_root;
940
941         window = gtk_widget_get_window(ds->window);
942         gdk_window_get_origin(window, &x, &y);
943         w = gdk_window_get_width(window);
944         h = gdk_window_get_height(window);
945
946         if (xr < x || yr < y || xr > x + w || yr > y + h)
947                 {
948                 g_signal_connect(G_OBJECT(ds->window), "button_release_event",
949                                  G_CALLBACK(date_selection_popup_release_cb), ds);
950                 return TRUE;
951                 }
952
953         return FALSE;
954 }
955
956 static void date_selection_popup_sync(DateSelection *ds)
957 {
958         guint day, month, year;
959
960         gtk_calendar_get_date(GTK_CALENDAR(ds->calendar), &year, &month, &day);
961         date_selection_set(ds->box, day, month + 1, year);
962 }
963
964 static gboolean date_selection_popup_keypress_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
965 {
966         DateSelection *ds = data;
967
968         switch (event->keyval)
969                 {
970                 case GDK_Return:
971                 case GDK_KP_Enter:
972                 case GDK_Tab:
973                 case GDK_ISO_Left_Tab:
974                         date_selection_popup_sync(ds);
975                         date_selection_popup_hide(ds);
976                         break;
977                 case GDK_Escape:
978                         date_selection_popup_hide(ds);
979                         break;
980                 default:
981                         break;
982                 }
983
984         return FALSE;
985 }
986
987 static void date_selection_day_cb(GtkWidget *widget, gpointer data)
988 {
989         DateSelection *ds = data;
990
991         date_selection_popup_sync(ds);
992 }
993
994 static void date_selection_doubleclick_cb(GtkWidget *widget, gpointer data)
995 {
996         DateSelection *ds = data;
997
998         date_selection_popup_hide(ds);
999 }
1000
1001 static void date_selection_popup(DateSelection *ds)
1002 {
1003         gint x, y;
1004         gint wx, wy;
1005         gint day, month, year;
1006         GtkAllocation button_allocation;
1007         GtkAllocation window_allocation;
1008
1009         if (ds->window) return;
1010
1011         ds->window = gtk_window_new(GTK_WINDOW_POPUP);
1012         gtk_window_set_resizable(GTK_WINDOW(ds->window), FALSE);
1013         g_signal_connect(G_OBJECT(ds->window), "button_press_event",
1014                          G_CALLBACK(date_selection_popup_press_cb), ds);
1015         g_signal_connect(G_OBJECT(ds->window), "key_press_event",
1016                          G_CALLBACK(date_selection_popup_keypress_cb), ds);
1017
1018         ds->calendar = gtk_calendar_new();
1019         gtk_container_add(GTK_CONTAINER(ds->window), ds->calendar);
1020         gtk_widget_show(ds->calendar);
1021
1022         date_selection_get(ds->box, &day, &month, &year);
1023         gtk_calendar_select_month(GTK_CALENDAR(ds->calendar), month - 1, year);
1024         gtk_calendar_select_day(GTK_CALENDAR(ds->calendar), day);
1025
1026         g_signal_connect(G_OBJECT(ds->calendar), "day_selected",
1027                          G_CALLBACK(date_selection_day_cb), ds);
1028         g_signal_connect(G_OBJECT(ds->calendar), "day_selected_double_click",
1029                         G_CALLBACK(date_selection_doubleclick_cb), ds);
1030
1031         gtk_widget_realize(ds->window);
1032
1033         gdk_window_get_origin(gtk_widget_get_window(ds->button), &wx, &wy);
1034
1035         gtk_widget_get_allocation(ds->button, &button_allocation);
1036         gtk_widget_get_allocation(ds->window, &window_allocation);
1037         
1038         x = wx + button_allocation.x + button_allocation.width - window_allocation.width;
1039         y = wy + button_allocation.y + button_allocation.height;
1040
1041         if (y + window_allocation.height > gdk_screen_height())
1042                 {
1043                 y = wy + button_allocation.y - window_allocation.height;
1044                 }
1045         if (x < 0) x = 0;
1046         if (y < 0) y = 0;
1047
1048         gtk_window_move(GTK_WINDOW(ds->window), x, y);
1049         gtk_widget_show(ds->window);
1050
1051         gtk_widget_grab_focus(ds->calendar);
1052         gdk_pointer_grab(gtk_widget_get_window(ds->window), TRUE,
1053                          GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_MOTION_MASK,
1054                          NULL, NULL, GDK_CURRENT_TIME);
1055         gdk_keyboard_grab(gtk_widget_get_window(ds->window), TRUE, GDK_CURRENT_TIME);
1056         gtk_grab_add(ds->window);
1057
1058         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ds->button), TRUE);
1059 }
1060
1061 static void date_selection_button_cb(GtkWidget *widget, gpointer data)
1062 {
1063         DateSelection *ds = data;
1064
1065         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(ds->button)) == (!ds->window))
1066                 {
1067                 date_selection_popup(ds);
1068                 }
1069 }
1070
1071 static void button_size_allocate_cb(GtkWidget *button, GtkAllocation *allocation, gpointer data)
1072 {
1073         GtkWidget *spin = data;
1074         GtkRequisition spin_requisition;
1075         gtk_widget_get_requisition(spin, &spin_requisition);
1076
1077         if (allocation->height > spin_requisition.height)
1078                 {
1079                 GtkAllocation button_allocation;
1080                 GtkAllocation spin_allocation;
1081
1082                 gtk_widget_get_allocation(button, &button_allocation);
1083                 gtk_widget_get_allocation(spin, &spin_allocation);
1084                 button_allocation.height = spin_requisition.height;
1085                 button_allocation.y = spin_allocation.y +
1086                         (spin_allocation.height - spin_requisition.height) / 2;
1087                 gtk_widget_size_allocate(button, &button_allocation);
1088                 }
1089 }
1090
1091 static void spin_increase(GtkWidget *spin, gint value)
1092 {
1093         GtkRequisition req;
1094
1095         gtk_widget_size_request(spin, &req);
1096         gtk_widget_set_size_request(spin, req.width + value, -1);
1097 }
1098
1099 static void date_selection_destroy_cb(GtkWidget *widget, gpointer data)
1100 {
1101         DateSelection *ds = data;
1102
1103         date_selection_popup_hide(ds);
1104
1105         g_free(ds);
1106 }
1107
1108 GtkWidget *date_selection_new(void)
1109 {
1110         DateSelection *ds;
1111         GtkWidget *arrow;
1112
1113         ds = g_new0(DateSelection, 1);
1114
1115         ds->box = gtk_hbox_new(FALSE, 2);
1116         g_signal_connect(G_OBJECT(ds->box), "destroy",
1117                          G_CALLBACK(date_selection_destroy_cb), ds);
1118
1119         /* FIXME: use option menu with text format of month instead of a spin button */
1120         ds->spin_m = pref_spin_new(ds->box, NULL, NULL, 1, 12, 1, 0, 1, NULL, NULL);
1121         ds->spin_d = pref_spin_new(ds->box, NULL, NULL, 1, 31, 1, 0, 1, NULL, NULL);
1122         ds->spin_y = pref_spin_new(ds->box, NULL, NULL, 1900, 9999, 1, 0, 1900, NULL, NULL);
1123         spin_increase(ds->spin_y, 5);
1124
1125         ds->button = gtk_toggle_button_new();
1126         g_signal_connect(G_OBJECT(ds->button), "size_allocate",
1127                          G_CALLBACK(button_size_allocate_cb), ds->spin_y);
1128
1129         arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_NONE);
1130         gtk_container_add(GTK_CONTAINER(ds->button), arrow);
1131         gtk_widget_show(arrow);
1132
1133         gtk_box_pack_start(GTK_BOX(ds->box), ds->button, FALSE, FALSE, 0);
1134         g_signal_connect(G_OBJECT(ds->button), "clicked",
1135                          G_CALLBACK(date_selection_button_cb), ds);
1136         gtk_widget_show(ds->button);
1137
1138         g_object_set_data(G_OBJECT(ds->box), DATE_SELECION_KEY, ds);
1139
1140         return ds->box;
1141 }
1142
1143 void date_selection_set(GtkWidget *widget, gint day, gint month, gint year)
1144 {
1145         DateSelection *ds;
1146
1147         ds = g_object_get_data(G_OBJECT(widget), DATE_SELECION_KEY);
1148         if (!ds) return;
1149
1150         gtk_spin_button_set_value(GTK_SPIN_BUTTON(ds->spin_d), (gdouble)day);
1151         gtk_spin_button_set_value(GTK_SPIN_BUTTON(ds->spin_m), (gdouble)month);
1152         gtk_spin_button_set_value(GTK_SPIN_BUTTON(ds->spin_y), (gdouble)year);
1153 }
1154
1155
1156 void date_selection_get(GtkWidget *widget, gint *day, gint *month, gint *year)
1157 {
1158         DateSelection *ds;
1159
1160         ds = g_object_get_data(G_OBJECT(widget), DATE_SELECION_KEY);
1161         if (!ds) return;
1162
1163         if (day) *day = gtk_spin_button_get_value(GTK_SPIN_BUTTON(ds->spin_d));
1164         if (month) *month = gtk_spin_button_get_value(GTK_SPIN_BUTTON(ds->spin_m));
1165         if (year) *year = gtk_spin_button_get_value(GTK_SPIN_BUTTON(ds->spin_y));
1166 }
1167
1168 void date_selection_time_set(GtkWidget *widget, time_t t)
1169 {
1170         struct tm *lt;
1171
1172         lt = localtime(&t);
1173         if (!lt) return;
1174
1175         date_selection_set(widget, lt->tm_mday, lt->tm_mon + 1, lt->tm_year + 1900);
1176 }
1177
1178 time_t date_selection_time_get(GtkWidget *widget)
1179 {
1180         struct tm lt;
1181         gint day = 0;
1182         gint month = 0;
1183         gint year = 0;
1184
1185         date_selection_get(widget, &day, &month ,&year);
1186
1187         lt.tm_sec = 0;
1188         lt.tm_min = 0;
1189         lt.tm_hour = 0;
1190         lt.tm_mday = day;
1191         lt.tm_mon = month - 1;
1192         lt.tm_year = year - 1900;
1193         lt.tm_isdst = 0;
1194
1195         return mktime(&lt);
1196 }
1197
1198
1199 /*
1200  *-----------------------------------------------------------------------------
1201  * Sizer, without using a GtkPaned
1202  *-----------------------------------------------------------------------------
1203  */
1204
1205 #define SIZER_DATA_KEY "sizer_data"
1206
1207 typedef struct _SizerData SizerData;
1208 struct _SizerData
1209 {
1210         GtkWidget *sizer;
1211         GtkWidget *parent;
1212         GtkWidget *bounding_widget;
1213         SizerPositionType position;
1214
1215         gint hsize_min;
1216         gint hsize_max;
1217         gint vsize_min;
1218         gint vsize_max;
1219
1220         gboolean in_drag;
1221         gint press_x;
1222         gint press_y;
1223         gint press_width;
1224         gint press_height;
1225
1226         gboolean handle_prelit;
1227 };
1228
1229
1230 static gint sizer_default_handle_size(void)
1231 {
1232         gint handle_size = 5;
1233         GtkWidget *paned;
1234         GtkStyle *style;
1235
1236         paned = gtk_hpaned_new();
1237
1238         style = gtk_rc_get_style(paned);
1239         gtk_widget_set_style(paned, style);
1240         gtk_widget_style_get(paned, "handle_size", &handle_size, NULL);
1241
1242         gtk_widget_destroy(paned);
1243
1244         return handle_size;
1245 }
1246
1247 static gboolean sizer_motion_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
1248 {
1249         SizerData *sd = data;
1250         gint x, y;
1251         gint w, h;
1252         GtkAllocation parent_allocation;
1253
1254         if (!sd->in_drag) return FALSE;
1255
1256         x = sd->press_x - bevent->x_root;
1257         y = sd->press_y - bevent->y_root;
1258
1259         w = sd->press_width;
1260         h = sd->press_height;
1261
1262         if (sd->position & SIZER_POS_LEFT)
1263                 {
1264                 w += x;
1265                 }
1266         else if (sd->position & SIZER_POS_RIGHT)
1267                 {
1268                 w -= x;
1269                 }
1270
1271         if (sd->position & SIZER_POS_TOP)
1272                 {
1273                 h += y;
1274                 }
1275         else if (sd->position & SIZER_POS_BOTTOM)
1276                 {
1277                 h -= y;
1278                 }
1279
1280         if (sd->hsize_min >= 0) w = MAX(w, sd->hsize_min);
1281         if (sd->vsize_min >= 0) h = MAX(h, sd->vsize_min);
1282
1283         if (sd->bounding_widget)
1284                 {
1285                 GtkAllocation sizer_allocation;
1286                 GtkAllocation bounding_allocation;
1287                 gtk_widget_get_allocation(sd->sizer, &sizer_allocation);
1288                 gtk_widget_get_allocation(sd->bounding_widget, &bounding_allocation);
1289                 w = CLAMP(w, sizer_allocation.width, bounding_allocation.width);
1290                 h = CLAMP(h, sizer_allocation.height, bounding_allocation.height);
1291                 }
1292         else
1293                 {
1294                 GtkAllocation sizer_allocation;
1295                 gtk_widget_get_allocation(sd->sizer, &sizer_allocation);
1296                 if (w < sizer_allocation.width) w = sizer_allocation.width;
1297                 if (h < sizer_allocation.height) h = sizer_allocation.height;
1298                 }
1299
1300         if (sd->hsize_max >= 0) w = MIN(w, sd->hsize_max);
1301         if (sd->vsize_max >= 0) h = MIN(h, sd->vsize_max);
1302
1303         gtk_widget_get_allocation(sd->parent, &parent_allocation);
1304         if (w == parent_allocation.width) w = -1;
1305         if (h == parent_allocation.height) h = -1;
1306
1307         if (w > 0 || h > 0) gtk_widget_set_size_request(sd->parent, w, h);
1308
1309         return TRUE;
1310 }
1311
1312 static gboolean sizer_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
1313 {
1314         SizerData *sd = data;
1315         GtkAllocation parent_allocation;
1316
1317         if (bevent->button != MOUSE_BUTTON_LEFT) return FALSE;
1318
1319         sd->in_drag = TRUE;
1320         sd->press_x = bevent->x_root;
1321         sd->press_y = bevent->y_root;
1322
1323         gtk_widget_get_allocation(sd->parent, &parent_allocation);
1324         sd->press_width = parent_allocation.width;
1325         sd->press_height = parent_allocation.height;
1326
1327         gdk_pointer_grab(gtk_widget_get_window(sd->sizer), FALSE,
1328                          GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
1329                          NULL, NULL, bevent->time);
1330         gtk_grab_add(sd->sizer);
1331
1332         return TRUE;
1333 }
1334
1335 static gboolean sizer_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
1336 {
1337         SizerData *sd = data;
1338
1339         if (bevent->button != MOUSE_BUTTON_LEFT) return FALSE;
1340
1341 #if GTK_CHECK_VERSION(2,20,0)
1342         if (gdk_pointer_is_grabbed() && gtk_widget_has_grab(sd->sizer))
1343 #else
1344         if (gdk_pointer_is_grabbed() && GTK_WIDGET_HAS_GRAB(sd->sizer))
1345 #endif
1346                 {
1347                 gtk_grab_remove(sd->sizer);
1348                 gdk_pointer_ungrab(bevent->time);
1349                 }
1350
1351         sd->in_drag = FALSE;
1352
1353         return TRUE;
1354 }
1355
1356 static void sizer_set_prelight(SizerData *sd, gboolean prelit)
1357 {
1358         GtkAllocation sizer_allocation;
1359         gtk_widget_get_allocation(sd->sizer, &sizer_allocation);
1360
1361         sd->handle_prelit = prelit;
1362         gtk_widget_queue_draw_area(sd->sizer, 0, 0,
1363                                    sizer_allocation.width, sizer_allocation.height);
1364 }
1365
1366 static gboolean sizer_enter_cb(GtkWidget *widget, GdkEventCrossing *event, gpointer data)
1367 {
1368         SizerData *sd = data;
1369
1370         sizer_set_prelight(sd, TRUE);
1371         return TRUE;
1372 }
1373
1374 static gboolean sizer_leave_cb(GtkWidget *widget, GdkEventCrossing *event, gpointer data)
1375 {
1376         SizerData *sd = data;
1377
1378         sizer_set_prelight(sd, FALSE);
1379         return TRUE;
1380 }
1381
1382 static gboolean sizer_expose_cb(GtkWidget *widget, GdkEventExpose *event, gpointer data)
1383 {
1384         SizerData *sd = data;
1385         GdkRectangle clip;
1386         GtkOrientation orientation;
1387         GtkStateType state;
1388         GtkAllocation allocation;
1389
1390         gdk_region_get_clipbox(event->region, &clip);
1391
1392         if (sd->position & SIZER_POS_LEFT || sd->position & SIZER_POS_RIGHT)
1393                 {
1394                 orientation = GTK_ORIENTATION_VERTICAL;
1395                 }
1396         else
1397                 {
1398                 orientation = GTK_ORIENTATION_HORIZONTAL;
1399                 }
1400
1401         if (sd->handle_prelit)
1402                 {
1403                 state = GTK_STATE_PRELIGHT;
1404                 }
1405         else
1406                 {
1407                 state = gtk_widget_get_state(widget);
1408                 }
1409
1410         gtk_widget_get_allocation(widget, &allocation);
1411         gtk_paint_handle(gtk_widget_get_style(widget), gtk_widget_get_window(widget), state,
1412                          GTK_SHADOW_NONE, &clip, widget, "paned",
1413                          0, 0,
1414                          allocation.width, allocation.height,
1415                          orientation);
1416
1417         return TRUE;
1418 }
1419
1420 static void sizer_realize_cb(GtkWidget *widget, gpointer data)
1421 {
1422         SizerData *sd = data;
1423         GdkCursorType n;
1424
1425         n = 0;
1426         if (sd->position & SIZER_POS_TOP || sd->position & SIZER_POS_BOTTOM)
1427                 {
1428                 n = GDK_SB_V_DOUBLE_ARROW;
1429                 }
1430         if (sd->position & SIZER_POS_LEFT || sd->position & SIZER_POS_RIGHT)
1431                 {
1432                 n = (n != 0) ? GDK_FLEUR : GDK_SB_H_DOUBLE_ARROW;
1433                 }
1434
1435         if (n != 0 && gtk_widget_get_window(widget))
1436                 {
1437                 GdkCursor *cursor;
1438                 cursor = gdk_cursor_new(n);
1439                 gdk_window_set_cursor(gtk_widget_get_window(widget), cursor);
1440                 gdk_cursor_unref(cursor);
1441                 }
1442 }
1443
1444 static void sizer_destroy_cb(GtkWidget *widget, gpointer data)
1445 {
1446         SizerData *sd = data;
1447
1448         g_free(sd);
1449 }
1450
1451 GtkWidget *sizer_new(GtkWidget *parent, GtkWidget *bounding_widget,
1452                      SizerPositionType position)
1453 {
1454         SizerData *sd;
1455         gint handle_size;
1456
1457         sd = g_new0(SizerData, 1);
1458
1459         sd->sizer = gtk_event_box_new();
1460         sd->parent = parent;
1461         sd->bounding_widget = bounding_widget;
1462         sd->position = position;
1463         sd->hsize_min = -1;
1464         sd->hsize_max = -1;
1465         sd->vsize_min = -1;
1466         sd->vsize_max = -1;
1467
1468         sd->in_drag = FALSE;
1469         sd->handle_prelit = FALSE;
1470
1471         g_signal_connect(G_OBJECT(sd->sizer), "destroy",
1472                          G_CALLBACK(sizer_destroy_cb), sd);
1473
1474         g_signal_connect(G_OBJECT(sd->sizer), "motion_notify_event",
1475                          G_CALLBACK(sizer_motion_cb), sd);
1476         g_signal_connect(G_OBJECT(sd->sizer), "button_press_event",
1477                          G_CALLBACK(sizer_press_cb), sd);
1478         g_signal_connect(G_OBJECT(sd->sizer), "button_release_event",
1479                          G_CALLBACK(sizer_release_cb), sd);
1480
1481         g_signal_connect(G_OBJECT(sd->sizer), "enter_notify_event",
1482                          G_CALLBACK(sizer_enter_cb), sd);
1483         g_signal_connect(G_OBJECT(sd->sizer), "leave_notify_event",
1484                          G_CALLBACK(sizer_leave_cb), sd);
1485
1486         gtk_widget_set_events(sd->sizer, GDK_POINTER_MOTION_MASK |
1487                                          GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_PRESS_MASK |
1488                                          GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK);
1489
1490         g_signal_connect(sd->sizer, "realize",
1491                          G_CALLBACK(sizer_realize_cb), sd);
1492         g_signal_connect(sd->sizer, "expose_event",
1493                          G_CALLBACK(sizer_expose_cb), sd);
1494
1495         handle_size = sizer_default_handle_size();
1496
1497         gtk_widget_set_size_request(sd->sizer, handle_size, handle_size);
1498 #if 0
1499         /* use this if you add a shadow border to the handle */
1500         gtk_widget_set_size_request(sd->sizer, handle_size + sd->sizer->style->xthickness * 2,
1501                                                handle_size + sd->sizer->style->ythickness * 2);
1502 #endif
1503
1504         g_object_set_data(G_OBJECT(sd->sizer), SIZER_DATA_KEY,sd);
1505
1506         return sd->sizer;
1507 }
1508
1509 void sizer_set_limits(GtkWidget *sizer,
1510                       gint hsize_min, gint hsize_max,
1511                       gint vsize_min, gint vsize_max)
1512 {
1513         SizerData *sd;
1514
1515         sd = g_object_get_data(G_OBJECT(sizer), SIZER_DATA_KEY);
1516         if (!sd) return;
1517
1518         sd->hsize_min = hsize_min;
1519         sd->hsize_max = hsize_max;
1520         sd->vsize_min = vsize_min;
1521         sd->vsize_max = vsize_max;
1522 }
1523
1524
1525 /*
1526  *-----------------------------------------------------------------------------
1527  * storing data in a history list with key,data pairs
1528  *-----------------------------------------------------------------------------
1529  */
1530
1531 #define PREF_LIST_MARKER_INT "[INT]:"
1532 #define PREF_LIST_MARKER_DOUBLE "[DOUBLE]:"
1533 #define PREF_LIST_MARKER_STRING "[STRING]:"
1534
1535 static GList *pref_list_find(const gchar *group, const gchar *token)
1536 {
1537         GList *work;
1538         gint l;
1539
1540         l = strlen(token);
1541
1542         work = history_list_get_by_key(group);
1543         while (work)
1544                 {
1545                 const gchar *text = work->data;
1546
1547                 if (strncmp(text, token, l) == 0) return work;
1548
1549                 work = work->next;
1550                 }
1551
1552         return NULL;
1553 }
1554
1555 static gboolean pref_list_get(const gchar *group, const gchar *key, const gchar *marker, const gchar **result)
1556 {
1557         gchar *token;
1558         GList *work;
1559         gboolean ret;
1560
1561         if (!group || !key || !marker)
1562                 {
1563                 *result = NULL;
1564                 return FALSE;
1565                 }
1566
1567         token = g_strconcat(key, marker, NULL);
1568
1569         work = pref_list_find(group, token);
1570         if (work)
1571                 {
1572                 *result = (const gchar *)work->data + strlen(token);
1573                 if (strlen(*result) == 0) *result = NULL;
1574                 ret = TRUE;
1575                 }
1576         else
1577                 {
1578                 *result = NULL;
1579                 ret = FALSE;
1580                 }
1581
1582         g_free(token);
1583
1584         return ret;
1585 }
1586
1587 static void pref_list_set(const gchar *group, const gchar *key, const gchar *marker, const gchar *text)
1588 {
1589         gchar *token;
1590         gchar *path;
1591         GList *work;
1592
1593         if (!group || !key || !marker) return;
1594
1595         token = g_strconcat(key, marker, NULL);
1596         path = g_strconcat(token, text, NULL);
1597
1598         work = pref_list_find(group, token);
1599         if (work)
1600                 {
1601                 gchar *old_path = work->data;
1602
1603                 if (text)
1604                         {
1605                         work->data = path;
1606                         path = NULL;
1607
1608                         g_free(old_path);
1609                         }
1610                 else
1611                         {
1612                         history_list_item_remove(group, old_path);
1613                         }
1614                 }
1615         else if (text)
1616                 {
1617                 history_list_add_to_key(group, path, 0);
1618                 }
1619
1620         g_free(path);
1621         g_free(token);
1622 }
1623
1624 void pref_list_int_set(const gchar *group, const gchar *key, gint value)
1625 {
1626         gchar *text;
1627
1628         text = g_strdup_printf("%d", value);
1629         pref_list_set(group, key, PREF_LIST_MARKER_INT, text);
1630         g_free(text);
1631 }
1632
1633 gboolean pref_list_int_get(const gchar *group, const gchar *key, gint *result)
1634 {
1635         const gchar *text;
1636
1637         if (!group || !key)
1638                 {
1639                 *result = 0;
1640                 return FALSE;
1641                 }
1642
1643         if (pref_list_get(group, key, PREF_LIST_MARKER_INT, &text) && text)
1644                 {
1645                 *result = (gint)strtol(text, NULL, 10);
1646                 return TRUE;
1647                 }
1648
1649         *result = 0;
1650         return FALSE;
1651 }
1652
1653 void pref_list_double_set(const gchar *group, const gchar *key, gdouble value)
1654 {
1655         gchar text[G_ASCII_DTOSTR_BUF_SIZE];
1656
1657         g_ascii_dtostr(text, sizeof(text), value);
1658         pref_list_set(group, key, PREF_LIST_MARKER_DOUBLE, text);
1659 }
1660
1661 gboolean pref_list_double_get(const gchar *group, const gchar *key, gdouble *result)
1662 {
1663         const gchar *text;
1664
1665         if (!group || !key)
1666                 {
1667                 *result = 0;
1668                 return FALSE;
1669                 }
1670
1671         if (pref_list_get(group, key, PREF_LIST_MARKER_DOUBLE, &text) && text)
1672                 {
1673                 *result = g_ascii_strtod(text, NULL);
1674                 return TRUE;
1675                 }
1676
1677         *result = 0;
1678         return FALSE;
1679 }
1680
1681 void pref_list_string_set(const gchar *group, const gchar *key, const gchar *value)
1682 {
1683         pref_list_set(group, key, PREF_LIST_MARKER_STRING, value);
1684 }
1685
1686 gboolean pref_list_string_get(const gchar *group, const gchar *key, const gchar **result)
1687 {
1688         return pref_list_get(group, key, PREF_LIST_MARKER_STRING, result);
1689 }
1690
1691
1692 void pref_color_button_set_cb(GtkWidget *widget, gpointer data)
1693 {
1694         GdkColor *color = data;
1695
1696         gtk_color_button_get_color(GTK_COLOR_BUTTON(widget), color);
1697 }
1698
1699 GtkWidget *pref_color_button_new(GtkWidget *parent_box,
1700                                  const gchar *title, const GdkColor *color,
1701                                  GCallback func, gpointer data)
1702 {
1703         GtkWidget *button;
1704
1705         if (color)
1706                 {
1707                 button = gtk_color_button_new_with_color(color);
1708                 }
1709         else
1710                 {
1711                 button = gtk_color_button_new();
1712                 }
1713
1714         if (func) g_signal_connect(G_OBJECT(button), "color-set", func, data);
1715
1716         if (title)
1717                 {
1718                 GtkWidget *label;
1719                 GtkWidget *hbox;
1720
1721                 gtk_color_button_set_title(GTK_COLOR_BUTTON(button), title);
1722                 label = gtk_label_new(title);
1723
1724                 hbox = gtk_hbox_new(TRUE, 0);
1725                 gtk_box_pack_start(GTK_BOX(parent_box), hbox, TRUE, TRUE, 0);
1726
1727                 gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 0);
1728                 gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
1729
1730                 gtk_widget_show_all(hbox);
1731                 }
1732         else
1733                 {
1734                 gtk_widget_show(button);
1735                 }
1736
1737         return button;
1738 }
1739
1740 /*
1741  *-----------------------------------------------------------------------------
1742  * text widget
1743  *-----------------------------------------------------------------------------
1744  */
1745
1746 gchar *text_widget_text_pull(GtkWidget *text_widget)
1747 {
1748         if (GTK_IS_TEXT_VIEW(text_widget))
1749                 {
1750                 GtkTextBuffer *buffer;
1751                 GtkTextIter start, end;
1752
1753                 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_widget));
1754                 gtk_text_buffer_get_bounds(buffer, &start, &end);
1755
1756                 return gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
1757                 }
1758         else if (GTK_IS_ENTRY(text_widget))
1759                 {
1760                 return g_strdup(gtk_entry_get_text(GTK_ENTRY(text_widget)));
1761                 }
1762         else
1763                 {
1764                 return NULL;
1765                 }
1766
1767 }
1768
1769 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */