Trim trailing white spaces on empty lines.
[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 static void pref_link_sensitivity_cb(GtkWidget *watch, GtkStateType prev_state, gpointer data)
547 {
548         GtkWidget *widget = data;
549
550         gtk_widget_set_sensitive(widget, gtk_widget_is_sensitive(watch));
551 }
552
553 void pref_link_sensitivity(GtkWidget *widget, GtkWidget *watch)
554 {
555         g_signal_connect(G_OBJECT(watch), "state_changed",
556                          G_CALLBACK(pref_link_sensitivity_cb), widget);
557 }
558
559 void pref_signal_block_data(GtkWidget *widget, gpointer data)
560 {
561         g_signal_handlers_block_matched(widget, G_SIGNAL_MATCH_DATA,
562                                         0, 0, NULL, NULL, data);
563 }
564
565 void pref_signal_unblock_data(GtkWidget *widget, gpointer data)
566 {
567         g_signal_handlers_unblock_matched(widget, G_SIGNAL_MATCH_DATA,
568                                           0, 0, NULL, NULL, data);
569 }
570
571 GtkWidget *pref_table_new(GtkWidget *parent_box, gint columns, gint rows,
572                           gboolean homogeneous, gboolean fill)
573 {
574         GtkWidget *table;
575
576         table = gtk_table_new(rows, columns, homogeneous);
577         gtk_table_set_row_spacings(GTK_TABLE(table), PREF_PAD_GAP);
578         gtk_table_set_col_spacings(GTK_TABLE(table), PREF_PAD_SPACE);
579
580         if (parent_box)
581                 {
582                 gtk_box_pack_start(GTK_BOX(parent_box), table, fill, fill, 0);
583                 gtk_widget_show(table);
584                 }
585
586         return table;
587 }
588
589 GtkWidget *pref_table_box(GtkWidget *table, gint column, gint row,
590                           GtkOrientation orientation, const gchar *text)
591 {
592         GtkWidget *box;
593         GtkWidget *shell;
594
595         if (text)
596                 {
597                 shell = gtk_vbox_new(FALSE, 0);
598                 box = pref_group_new(shell, TRUE, text, orientation);
599                 }
600         else
601                 {
602                 if (orientation == GTK_ORIENTATION_HORIZONTAL)
603                         {
604                         box = gtk_hbox_new(FALSE, PREF_PAD_SPACE);
605                         }
606                 else
607                         {
608                         box = gtk_vbox_new(FALSE, PREF_PAD_GAP);
609                         }
610                 shell = box;
611                 }
612
613         gtk_table_attach(GTK_TABLE(table), shell, column, column + 1, row, row + 1,
614                          GTK_EXPAND | GTK_FILL, 0, 0, 0);
615
616         gtk_widget_show(shell);
617
618         return box;
619 }
620
621 GtkWidget *pref_table_label(GtkWidget *table, gint column, gint row,
622                             const gchar *text, gfloat alignment)
623 {
624         GtkWidget *label;
625         GtkWidget *align;
626
627         align = gtk_alignment_new(alignment, 0.50, 0.0, 0.0);
628         gtk_table_attach(GTK_TABLE(table), align, column, column + 1, row, row + 1,
629                          GTK_FILL, 0, 0, 0);
630         gtk_widget_show(align);
631         label = gtk_label_new(text);
632         gtk_container_add(GTK_CONTAINER(align), label);
633         gtk_widget_show(label);
634
635         return label;
636 }
637
638 GtkWidget *pref_table_button(GtkWidget *table, gint column, gint row,
639                              const gchar *stock_id, const gchar *text, gboolean hide_stock_text,
640                              GCallback func, gpointer data)
641 {
642         GtkWidget *button;
643
644         button = pref_button_new(NULL, stock_id, text, hide_stock_text, func, data);
645         gtk_table_attach(GTK_TABLE(table), button, column, column + 1, row, row + 1,
646                          GTK_FILL, 0, 0, 0);
647         gtk_widget_show(button);
648
649         return button;
650 }
651
652 GtkWidget *pref_table_spin(GtkWidget *table, gint column, gint row,
653                            const gchar *text, const gchar *suffix,
654                            gdouble min, gdouble max, gdouble step, gint digits,
655                            gdouble value,
656                            GCallback func, gpointer data)
657 {
658         GtkWidget *spin;
659         GtkWidget *box;
660         GtkWidget *label;
661
662         spin = gtk_spin_button_new_with_range(min, max, step);
663         gtk_spin_button_set_digits(GTK_SPIN_BUTTON(spin), digits);
664         gtk_spin_button_set_value(GTK_SPIN_BUTTON(spin), value);
665         if (func)
666                 {
667                 g_signal_connect(G_OBJECT(spin), "value_changed", G_CALLBACK(func), data);
668                 }
669
670         if (text)
671                 {
672                 label = pref_table_label(table, column, row, text, 1.0);
673                 pref_link_sensitivity(label, spin);
674                 column++;
675                 }
676
677         if (suffix)
678                 {
679                 box = gtk_hbox_new(FALSE, PREF_PAD_SPACE);
680                 gtk_box_pack_start(GTK_BOX(box), spin, FALSE, FALSE, 0);
681                 gtk_widget_show(spin);
682
683                 label = pref_label_new(box, suffix);
684                 pref_link_sensitivity(label, spin);
685                 }
686         else
687                 {
688                 box = spin;
689                 }
690
691         gtk_table_attach(GTK_TABLE(table), box, column, column + 1, row, row + 1,
692                          GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
693         gtk_widget_show(box);
694
695         return spin;
696 }
697
698 GtkWidget *pref_table_spin_new_int(GtkWidget *table, gint column, gint row,
699                                    const gchar *text, const gchar *suffix,
700                                    gint min, gint max, gint step,
701                                    gint value, gint *value_var)
702 {
703         *value_var = value;
704         return pref_table_spin(table, column, row,
705                                text, suffix,
706                                (gdouble)min, (gdouble)max, (gdouble)step, 0,
707                                value,
708                                G_CALLBACK(pref_spin_int_cb), value_var);
709 }
710
711
712 GtkWidget *pref_toolbar_new(GtkWidget *parent_box, GtkToolbarStyle style)
713 {
714         GtkWidget *tbar;
715
716         tbar = gtk_toolbar_new();
717         gtk_toolbar_set_style(GTK_TOOLBAR(tbar), style);
718
719         if (parent_box)
720                 {
721                 gtk_box_pack_start(GTK_BOX(parent_box), tbar, FALSE, FALSE, 0);
722                 gtk_widget_show(tbar);
723                 }
724         return tbar;
725 }
726
727 GtkWidget *pref_toolbar_button(GtkWidget *toolbar,
728                                const gchar *stock_id, const gchar *label, gboolean toggle,
729                                const gchar *description,
730                                GCallback func, gpointer data)
731 {
732         GtkWidget *item;
733
734         if (toggle)
735                 {
736                 if (stock_id)
737                         {
738                         item = GTK_WIDGET(gtk_toggle_tool_button_new_from_stock(stock_id));
739                         }
740                 else
741                         {
742                         item = GTK_WIDGET(gtk_toggle_tool_button_new());
743                         }
744                 }
745         else
746                 {
747                 if (stock_id)
748                         {
749                         item = GTK_WIDGET(gtk_tool_button_new_from_stock(stock_id));
750                         }
751                 else
752                         {
753                         item = GTK_WIDGET(gtk_tool_button_new(NULL, NULL));
754                         }
755                 }
756         gtk_tool_button_set_use_underline(GTK_TOOL_BUTTON(item), TRUE);
757
758         if (label) gtk_tool_button_set_label(GTK_TOOL_BUTTON(item), label);
759
760         if (func) g_signal_connect(item, "clicked", func, data);
761         gtk_container_add(GTK_CONTAINER(toolbar), item);
762         gtk_widget_show(item);
763
764         if (description)
765                 {
766                 gtk_widget_set_tooltip_text(item, description);
767                 }
768
769         return item;
770 }
771
772 void pref_toolbar_button_set_icon(GtkWidget *button, GtkWidget *widget, const gchar *stock_id)
773 {
774         if (widget)
775                 {
776                 gtk_tool_button_set_icon_widget(GTK_TOOL_BUTTON(button), widget);
777                 }
778         else if (stock_id)
779                 {
780                 gtk_tool_button_set_stock_id(GTK_TOOL_BUTTON(button), stock_id);
781                 }
782 }
783
784 GtkWidget *pref_toolbar_spacer(GtkWidget *toolbar)
785 {
786         GtkWidget *item;
787
788         item = GTK_WIDGET(gtk_separator_tool_item_new());
789         gtk_container_add(GTK_CONTAINER(toolbar), item);
790         gtk_widget_show(item);
791
792         return item;
793 }
794
795
796 /*
797  *-----------------------------------------------------------------------------
798  * date selection entry
799  *-----------------------------------------------------------------------------
800  */
801
802 #define DATE_SELECION_KEY "date_selection_data"
803
804
805 typedef struct _DateSelection DateSelection;
806 struct _DateSelection
807 {
808         GtkWidget *box;
809
810         GtkWidget *spin_d;
811         GtkWidget *spin_m;
812         GtkWidget *spin_y;
813
814         GtkWidget *button;
815
816         GtkWidget *window;
817         GtkWidget *calendar;
818 };
819
820
821 static void date_selection_popup_hide(DateSelection *ds)
822 {
823         if (!ds->window) return;
824
825         if (gtk_widget_has_grab(ds->window))
826                 {
827                 gtk_grab_remove(ds->window);
828                 gdk_keyboard_ungrab(GDK_CURRENT_TIME);
829                 gdk_pointer_ungrab(GDK_CURRENT_TIME);
830                 }
831
832         gtk_widget_hide(ds->window);
833
834         gtk_widget_destroy(ds->window);
835         ds->window = NULL;
836         ds->calendar = NULL;
837
838         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ds->button), FALSE);
839 }
840
841 static gboolean date_selection_popup_release_cb(GtkWidget *widget, GdkEventButton *event, gpointer data)
842 {
843         DateSelection *ds = data;
844
845         date_selection_popup_hide(ds);
846         return TRUE;
847 }
848
849 static gboolean date_selection_popup_press_cb(GtkWidget *widget, GdkEventButton *event, gpointer data)
850 {
851         DateSelection *ds = data;
852         gint x, y;
853         gint w, h;
854         gint xr, yr;
855         GdkWindow *window;
856
857         xr = (gint)event->x_root;
858         yr = (gint)event->y_root;
859
860         window = gtk_widget_get_window(ds->window);
861         gdk_window_get_origin(window, &x, &y);
862         w = gdk_window_get_width(window);
863         h = gdk_window_get_height(window);
864
865         if (xr < x || yr < y || xr > x + w || yr > y + h)
866                 {
867                 g_signal_connect(G_OBJECT(ds->window), "button_release_event",
868                                  G_CALLBACK(date_selection_popup_release_cb), ds);
869                 return TRUE;
870                 }
871
872         return FALSE;
873 }
874
875 static void date_selection_popup_sync(DateSelection *ds)
876 {
877         guint day, month, year;
878
879         gtk_calendar_get_date(GTK_CALENDAR(ds->calendar), &year, &month, &day);
880         date_selection_set(ds->box, day, month + 1, year);
881 }
882
883 static gboolean date_selection_popup_keypress_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
884 {
885         DateSelection *ds = data;
886
887         switch (event->keyval)
888                 {
889                 case GDK_KEY_Return:
890                 case GDK_KEY_KP_Enter:
891                 case GDK_KEY_Tab:
892                 case GDK_KEY_ISO_Left_Tab:
893                         date_selection_popup_sync(ds);
894                         date_selection_popup_hide(ds);
895                         break;
896                 case GDK_KEY_Escape:
897                         date_selection_popup_hide(ds);
898                         break;
899                 default:
900                         break;
901                 }
902
903         return FALSE;
904 }
905
906 static void date_selection_day_cb(GtkWidget *widget, gpointer data)
907 {
908         DateSelection *ds = data;
909
910         date_selection_popup_sync(ds);
911 }
912
913 static void date_selection_doubleclick_cb(GtkWidget *widget, gpointer data)
914 {
915         DateSelection *ds = data;
916
917         date_selection_popup_hide(ds);
918 }
919
920 static void date_selection_popup(DateSelection *ds)
921 {
922         gint x, y;
923         gint wx, wy;
924         gint day, month, year;
925         GtkAllocation button_allocation;
926         GtkAllocation window_allocation;
927
928         if (ds->window) return;
929
930         ds->window = gtk_window_new(GTK_WINDOW_POPUP);
931         gtk_window_set_resizable(GTK_WINDOW(ds->window), FALSE);
932         g_signal_connect(G_OBJECT(ds->window), "button_press_event",
933                          G_CALLBACK(date_selection_popup_press_cb), ds);
934         g_signal_connect(G_OBJECT(ds->window), "key_press_event",
935                          G_CALLBACK(date_selection_popup_keypress_cb), ds);
936
937         ds->calendar = gtk_calendar_new();
938         gtk_container_add(GTK_CONTAINER(ds->window), ds->calendar);
939         gtk_widget_show(ds->calendar);
940
941         date_selection_get(ds->box, &day, &month, &year);
942         gtk_calendar_select_month(GTK_CALENDAR(ds->calendar), month - 1, year);
943         gtk_calendar_select_day(GTK_CALENDAR(ds->calendar), day);
944
945         g_signal_connect(G_OBJECT(ds->calendar), "day_selected",
946                          G_CALLBACK(date_selection_day_cb), ds);
947         g_signal_connect(G_OBJECT(ds->calendar), "day_selected_double_click",
948                         G_CALLBACK(date_selection_doubleclick_cb), ds);
949
950         gtk_widget_realize(ds->window);
951
952         gdk_window_get_origin(gtk_widget_get_window(ds->button), &wx, &wy);
953
954         gtk_widget_get_allocation(ds->button, &button_allocation);
955         gtk_widget_get_allocation(ds->window, &window_allocation);
956
957         x = wx + button_allocation.x + button_allocation.width - window_allocation.width;
958         y = wy + button_allocation.y + button_allocation.height;
959
960         if (y + window_allocation.height > gdk_screen_height())
961                 {
962                 y = wy + button_allocation.y - window_allocation.height;
963                 }
964         if (x < 0) x = 0;
965         if (y < 0) y = 0;
966
967         gtk_window_move(GTK_WINDOW(ds->window), x, y);
968         gtk_widget_show(ds->window);
969
970         gtk_widget_grab_focus(ds->calendar);
971         gdk_pointer_grab(gtk_widget_get_window(ds->window), TRUE,
972                          GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_MOTION_MASK,
973                          NULL, NULL, GDK_CURRENT_TIME);
974         gdk_keyboard_grab(gtk_widget_get_window(ds->window), TRUE, GDK_CURRENT_TIME);
975         gtk_grab_add(ds->window);
976
977         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ds->button), TRUE);
978 }
979
980 static void date_selection_button_cb(GtkWidget *widget, gpointer data)
981 {
982         DateSelection *ds = data;
983
984         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(ds->button)) == (!ds->window))
985                 {
986                 date_selection_popup(ds);
987                 }
988 }
989
990 static void button_size_allocate_cb(GtkWidget *button, GtkAllocation *allocation, gpointer data)
991 {
992         GtkWidget *spin = data;
993         GtkRequisition spin_requisition;
994         gtk_widget_get_requisition(spin, &spin_requisition);
995
996         if (allocation->height > spin_requisition.height)
997                 {
998                 GtkAllocation button_allocation;
999                 GtkAllocation spin_allocation;
1000
1001                 gtk_widget_get_allocation(button, &button_allocation);
1002                 gtk_widget_get_allocation(spin, &spin_allocation);
1003                 button_allocation.height = spin_requisition.height;
1004                 button_allocation.y = spin_allocation.y +
1005                         (spin_allocation.height - spin_requisition.height) / 2;
1006                 gtk_widget_size_allocate(button, &button_allocation);
1007                 }
1008 }
1009
1010 static void spin_increase(GtkWidget *spin, gint value)
1011 {
1012         GtkRequisition req;
1013
1014         gtk_widget_size_request(spin, &req);
1015         gtk_widget_set_size_request(spin, req.width + value, -1);
1016 }
1017
1018 static void date_selection_destroy_cb(GtkWidget *widget, gpointer data)
1019 {
1020         DateSelection *ds = data;
1021
1022         date_selection_popup_hide(ds);
1023
1024         g_free(ds);
1025 }
1026
1027 GtkWidget *date_selection_new(void)
1028 {
1029         DateSelection *ds;
1030         GtkWidget *arrow;
1031
1032         ds = g_new0(DateSelection, 1);
1033
1034         ds->box = gtk_hbox_new(FALSE, 2);
1035         g_signal_connect(G_OBJECT(ds->box), "destroy",
1036                          G_CALLBACK(date_selection_destroy_cb), ds);
1037
1038         /* FIXME: use option menu with text format of month instead of a spin button */
1039         ds->spin_m = pref_spin_new(ds->box, NULL, NULL, 1, 12, 1, 0, 1, NULL, NULL);
1040         ds->spin_d = pref_spin_new(ds->box, NULL, NULL, 1, 31, 1, 0, 1, NULL, NULL);
1041         ds->spin_y = pref_spin_new(ds->box, NULL, NULL, 1900, 9999, 1, 0, 1900, NULL, NULL);
1042         spin_increase(ds->spin_y, 5);
1043
1044         ds->button = gtk_toggle_button_new();
1045         g_signal_connect(G_OBJECT(ds->button), "size_allocate",
1046                          G_CALLBACK(button_size_allocate_cb), ds->spin_y);
1047
1048         arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_NONE);
1049         gtk_container_add(GTK_CONTAINER(ds->button), arrow);
1050         gtk_widget_show(arrow);
1051
1052         gtk_box_pack_start(GTK_BOX(ds->box), ds->button, FALSE, FALSE, 0);
1053         g_signal_connect(G_OBJECT(ds->button), "clicked",
1054                          G_CALLBACK(date_selection_button_cb), ds);
1055         gtk_widget_show(ds->button);
1056
1057         g_object_set_data(G_OBJECT(ds->box), DATE_SELECION_KEY, ds);
1058
1059         return ds->box;
1060 }
1061
1062 void date_selection_set(GtkWidget *widget, gint day, gint month, gint year)
1063 {
1064         DateSelection *ds;
1065
1066         ds = g_object_get_data(G_OBJECT(widget), DATE_SELECION_KEY);
1067         if (!ds) return;
1068
1069         gtk_spin_button_set_value(GTK_SPIN_BUTTON(ds->spin_d), (gdouble)day);
1070         gtk_spin_button_set_value(GTK_SPIN_BUTTON(ds->spin_m), (gdouble)month);
1071         gtk_spin_button_set_value(GTK_SPIN_BUTTON(ds->spin_y), (gdouble)year);
1072 }
1073
1074
1075 void date_selection_get(GtkWidget *widget, gint *day, gint *month, gint *year)
1076 {
1077         DateSelection *ds;
1078
1079         ds = g_object_get_data(G_OBJECT(widget), DATE_SELECION_KEY);
1080         if (!ds) return;
1081
1082         if (day) *day = gtk_spin_button_get_value(GTK_SPIN_BUTTON(ds->spin_d));
1083         if (month) *month = gtk_spin_button_get_value(GTK_SPIN_BUTTON(ds->spin_m));
1084         if (year) *year = gtk_spin_button_get_value(GTK_SPIN_BUTTON(ds->spin_y));
1085 }
1086
1087 void date_selection_time_set(GtkWidget *widget, time_t t)
1088 {
1089         struct tm *lt;
1090
1091         lt = localtime(&t);
1092         if (!lt) return;
1093
1094         date_selection_set(widget, lt->tm_mday, lt->tm_mon + 1, lt->tm_year + 1900);
1095 }
1096
1097 time_t date_selection_time_get(GtkWidget *widget)
1098 {
1099         struct tm lt;
1100         gint day = 0;
1101         gint month = 0;
1102         gint year = 0;
1103
1104         date_selection_get(widget, &day, &month ,&year);
1105
1106         lt.tm_sec = 0;
1107         lt.tm_min = 0;
1108         lt.tm_hour = 0;
1109         lt.tm_mday = day;
1110         lt.tm_mon = month - 1;
1111         lt.tm_year = year - 1900;
1112         lt.tm_isdst = 0;
1113
1114         return mktime(&lt);
1115 }
1116
1117
1118 /*
1119  *-----------------------------------------------------------------------------
1120  * Sizer, without using a GtkPaned
1121  *-----------------------------------------------------------------------------
1122  */
1123
1124 #define SIZER_DATA_KEY "sizer_data"
1125
1126 typedef struct _SizerData SizerData;
1127 struct _SizerData
1128 {
1129         GtkWidget *sizer;
1130         GtkWidget *parent;
1131         GtkWidget *bounding_widget;
1132         SizerPositionType position;
1133
1134         gint hsize_min;
1135         gint hsize_max;
1136         gint vsize_min;
1137         gint vsize_max;
1138
1139         gboolean in_drag;
1140         gint press_x;
1141         gint press_y;
1142         gint press_width;
1143         gint press_height;
1144
1145         gboolean handle_prelit;
1146 };
1147
1148
1149 static gint sizer_default_handle_size(void)
1150 {
1151         gint handle_size = 5;
1152         GtkWidget *paned;
1153         GtkStyle *style;
1154
1155         paned = gtk_hpaned_new();
1156
1157         style = gtk_rc_get_style(paned);
1158         gtk_widget_set_style(paned, style);
1159         gtk_widget_style_get(paned, "handle_size", &handle_size, NULL);
1160
1161         gtk_widget_destroy(paned);
1162
1163         return handle_size;
1164 }
1165
1166 static gboolean sizer_motion_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
1167 {
1168         SizerData *sd = data;
1169         gint x, y;
1170         gint w, h;
1171         GtkAllocation parent_allocation;
1172
1173         if (!sd->in_drag) return FALSE;
1174
1175         x = sd->press_x - bevent->x_root;
1176         y = sd->press_y - bevent->y_root;
1177
1178         w = sd->press_width;
1179         h = sd->press_height;
1180
1181         if (sd->position & SIZER_POS_LEFT)
1182                 {
1183                 w += x;
1184                 }
1185         else if (sd->position & SIZER_POS_RIGHT)
1186                 {
1187                 w -= x;
1188                 }
1189
1190         if (sd->position & SIZER_POS_TOP)
1191                 {
1192                 h += y;
1193                 }
1194         else if (sd->position & SIZER_POS_BOTTOM)
1195                 {
1196                 h -= y;
1197                 }
1198
1199         if (sd->hsize_min >= 0) w = MAX(w, sd->hsize_min);
1200         if (sd->vsize_min >= 0) h = MAX(h, sd->vsize_min);
1201
1202         if (sd->bounding_widget)
1203                 {
1204                 GtkAllocation sizer_allocation;
1205                 GtkAllocation bounding_allocation;
1206                 gtk_widget_get_allocation(sd->sizer, &sizer_allocation);
1207                 gtk_widget_get_allocation(sd->bounding_widget, &bounding_allocation);
1208                 w = CLAMP(w, sizer_allocation.width, bounding_allocation.width);
1209                 h = CLAMP(h, sizer_allocation.height, bounding_allocation.height);
1210                 }
1211         else
1212                 {
1213                 GtkAllocation sizer_allocation;
1214                 gtk_widget_get_allocation(sd->sizer, &sizer_allocation);
1215                 if (w < sizer_allocation.width) w = sizer_allocation.width;
1216                 if (h < sizer_allocation.height) h = sizer_allocation.height;
1217                 }
1218
1219         if (sd->hsize_max >= 0) w = MIN(w, sd->hsize_max);
1220         if (sd->vsize_max >= 0) h = MIN(h, sd->vsize_max);
1221
1222         gtk_widget_get_allocation(sd->parent, &parent_allocation);
1223         if (w == parent_allocation.width) w = -1;
1224         if (h == parent_allocation.height) h = -1;
1225
1226         if (w > 0 || h > 0) gtk_widget_set_size_request(sd->parent, w, h);
1227
1228         return TRUE;
1229 }
1230
1231 static gboolean sizer_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
1232 {
1233         SizerData *sd = data;
1234         GtkAllocation parent_allocation;
1235
1236         if (bevent->button != MOUSE_BUTTON_LEFT) return FALSE;
1237
1238         sd->in_drag = TRUE;
1239         sd->press_x = bevent->x_root;
1240         sd->press_y = bevent->y_root;
1241
1242         gtk_widget_get_allocation(sd->parent, &parent_allocation);
1243         sd->press_width = parent_allocation.width;
1244         sd->press_height = parent_allocation.height;
1245
1246         gdk_pointer_grab(gtk_widget_get_window(sd->sizer), FALSE,
1247                          GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
1248                          NULL, NULL, bevent->time);
1249         gtk_grab_add(sd->sizer);
1250
1251         return TRUE;
1252 }
1253
1254 static gboolean sizer_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
1255 {
1256         SizerData *sd = data;
1257
1258         if (bevent->button != MOUSE_BUTTON_LEFT) return FALSE;
1259
1260         if (gdk_pointer_is_grabbed() && gtk_widget_has_grab(sd->sizer))
1261                 {
1262                 gtk_grab_remove(sd->sizer);
1263                 gdk_pointer_ungrab(bevent->time);
1264                 }
1265
1266         sd->in_drag = FALSE;
1267
1268         return TRUE;
1269 }
1270
1271 static void sizer_set_prelight(SizerData *sd, gboolean prelit)
1272 {
1273         GtkAllocation sizer_allocation;
1274         gtk_widget_get_allocation(sd->sizer, &sizer_allocation);
1275
1276         sd->handle_prelit = prelit;
1277         gtk_widget_queue_draw_area(sd->sizer, 0, 0,
1278                                    sizer_allocation.width, sizer_allocation.height);
1279 }
1280
1281 static gboolean sizer_enter_cb(GtkWidget *widget, GdkEventCrossing *event, gpointer data)
1282 {
1283         SizerData *sd = data;
1284
1285         sizer_set_prelight(sd, TRUE);
1286         return TRUE;
1287 }
1288
1289 static gboolean sizer_leave_cb(GtkWidget *widget, GdkEventCrossing *event, gpointer data)
1290 {
1291         SizerData *sd = data;
1292
1293         sizer_set_prelight(sd, FALSE);
1294         return TRUE;
1295 }
1296
1297 static gboolean sizer_expose_cb(GtkWidget *widget, GdkEventExpose *event, gpointer data)
1298 {
1299 #if GTK_CHECK_VERSION(3,0,0)
1300         GtkAllocation allocation;
1301
1302         gtk_widget_get_allocation(widget, &allocation);
1303
1304         cairo_t *cr = gdk_cairo_create(gtk_widget_get_window(widget));
1305
1306         gtk_render_handle (gtk_widget_get_style_context (widget),
1307                            cr, allocation.x, allocation.y, allocation.width, allocation.height);
1308         cairo_destroy(cr);
1309 #else
1310         SizerData *sd = data;
1311         GdkRectangle clip;
1312         GtkOrientation orientation;
1313         GtkStateType state;
1314         GtkAllocation allocation;
1315
1316         gtk_widget_get_allocation(widget, &allocation);
1317
1318         if (sd->position & SIZER_POS_LEFT || sd->position & SIZER_POS_RIGHT)
1319                 {
1320                 orientation = GTK_ORIENTATION_VERTICAL;
1321                 }
1322         else
1323                 {
1324                 orientation = GTK_ORIENTATION_HORIZONTAL;
1325                 }
1326
1327         if (sd->handle_prelit)
1328                 {
1329                 state = GTK_STATE_PRELIGHT;
1330                 }
1331         else
1332                 {
1333                 state = gtk_widget_get_state(widget);
1334                 }
1335
1336         gdk_region_get_clipbox(event->region, &clip);
1337
1338         gtk_paint_handle(gtk_widget_get_style(widget), gtk_widget_get_window(widget), state,
1339                          GTK_SHADOW_NONE, &clip, widget, "paned",
1340                          0, 0,
1341                          allocation.width, allocation.height,
1342                          orientation);
1343 #endif
1344
1345         return TRUE;
1346 }
1347
1348 static void sizer_realize_cb(GtkWidget *widget, gpointer data)
1349 {
1350         SizerData *sd = data;
1351         GdkCursorType n;
1352
1353         n = 0;
1354         if (sd->position & SIZER_POS_TOP || sd->position & SIZER_POS_BOTTOM)
1355                 {
1356                 n = GDK_SB_V_DOUBLE_ARROW;
1357                 }
1358         if (sd->position & SIZER_POS_LEFT || sd->position & SIZER_POS_RIGHT)
1359                 {
1360                 n = (n != 0) ? GDK_FLEUR : GDK_SB_H_DOUBLE_ARROW;
1361                 }
1362
1363         if (n != 0 && gtk_widget_get_window(widget))
1364                 {
1365                 GdkCursor *cursor;
1366                 cursor = gdk_cursor_new(n);
1367                 gdk_window_set_cursor(gtk_widget_get_window(widget), cursor);
1368                 gdk_cursor_unref(cursor);
1369                 }
1370 }
1371
1372 static void sizer_destroy_cb(GtkWidget *widget, gpointer data)
1373 {
1374         SizerData *sd = data;
1375
1376         g_free(sd);
1377 }
1378
1379 GtkWidget *sizer_new(GtkWidget *parent, GtkWidget *bounding_widget,
1380                      SizerPositionType position)
1381 {
1382         SizerData *sd;
1383         gint handle_size;
1384
1385         sd = g_new0(SizerData, 1);
1386
1387         sd->sizer = gtk_event_box_new();
1388         sd->parent = parent;
1389         sd->bounding_widget = bounding_widget;
1390         sd->position = position;
1391         sd->hsize_min = -1;
1392         sd->hsize_max = -1;
1393         sd->vsize_min = -1;
1394         sd->vsize_max = -1;
1395
1396         sd->in_drag = FALSE;
1397         sd->handle_prelit = FALSE;
1398
1399         g_signal_connect(G_OBJECT(sd->sizer), "destroy",
1400                          G_CALLBACK(sizer_destroy_cb), sd);
1401
1402         g_signal_connect(G_OBJECT(sd->sizer), "motion_notify_event",
1403                          G_CALLBACK(sizer_motion_cb), sd);
1404         g_signal_connect(G_OBJECT(sd->sizer), "button_press_event",
1405                          G_CALLBACK(sizer_press_cb), sd);
1406         g_signal_connect(G_OBJECT(sd->sizer), "button_release_event",
1407                          G_CALLBACK(sizer_release_cb), sd);
1408
1409         g_signal_connect(G_OBJECT(sd->sizer), "enter_notify_event",
1410                          G_CALLBACK(sizer_enter_cb), sd);
1411         g_signal_connect(G_OBJECT(sd->sizer), "leave_notify_event",
1412                          G_CALLBACK(sizer_leave_cb), sd);
1413
1414         gtk_widget_set_events(sd->sizer, GDK_POINTER_MOTION_MASK |
1415                                          GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_PRESS_MASK |
1416                                          GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK);
1417
1418         g_signal_connect(sd->sizer, "realize",
1419                          G_CALLBACK(sizer_realize_cb), sd);
1420         g_signal_connect(sd->sizer, "expose_event",
1421                          G_CALLBACK(sizer_expose_cb), sd);
1422
1423         handle_size = sizer_default_handle_size();
1424
1425         gtk_widget_set_size_request(sd->sizer, handle_size, handle_size);
1426 #if 0
1427         /* use this if you add a shadow border to the handle */
1428         gtk_widget_set_size_request(sd->sizer, handle_size + sd->sizer->style->xthickness * 2,
1429                                                handle_size + sd->sizer->style->ythickness * 2);
1430 #endif
1431
1432         g_object_set_data(G_OBJECT(sd->sizer), SIZER_DATA_KEY,sd);
1433
1434         return sd->sizer;
1435 }
1436
1437 void sizer_set_limits(GtkWidget *sizer,
1438                       gint hsize_min, gint hsize_max,
1439                       gint vsize_min, gint vsize_max)
1440 {
1441         SizerData *sd;
1442
1443         sd = g_object_get_data(G_OBJECT(sizer), SIZER_DATA_KEY);
1444         if (!sd) return;
1445
1446         sd->hsize_min = hsize_min;
1447         sd->hsize_max = hsize_max;
1448         sd->vsize_min = vsize_min;
1449         sd->vsize_max = vsize_max;
1450 }
1451
1452
1453 /*
1454  *-----------------------------------------------------------------------------
1455  * storing data in a history list with key,data pairs
1456  *-----------------------------------------------------------------------------
1457  */
1458
1459 #define PREF_LIST_MARKER_INT "[INT]:"
1460 #define PREF_LIST_MARKER_DOUBLE "[DOUBLE]:"
1461 #define PREF_LIST_MARKER_STRING "[STRING]:"
1462
1463 static GList *pref_list_find(const gchar *group, const gchar *token)
1464 {
1465         GList *work;
1466         gint l;
1467
1468         l = strlen(token);
1469
1470         work = history_list_get_by_key(group);
1471         while (work)
1472                 {
1473                 const gchar *text = work->data;
1474
1475                 if (strncmp(text, token, l) == 0) return work;
1476
1477                 work = work->next;
1478                 }
1479
1480         return NULL;
1481 }
1482
1483 static gboolean pref_list_get(const gchar *group, const gchar *key, const gchar *marker, const gchar **result)
1484 {
1485         gchar *token;
1486         GList *work;
1487         gboolean ret;
1488
1489         if (!group || !key || !marker)
1490                 {
1491                 *result = NULL;
1492                 return FALSE;
1493                 }
1494
1495         token = g_strconcat(key, marker, NULL);
1496
1497         work = pref_list_find(group, token);
1498         if (work)
1499                 {
1500                 *result = (const gchar *)work->data + strlen(token);
1501                 if (strlen(*result) == 0) *result = NULL;
1502                 ret = TRUE;
1503                 }
1504         else
1505                 {
1506                 *result = NULL;
1507                 ret = FALSE;
1508                 }
1509
1510         g_free(token);
1511
1512         return ret;
1513 }
1514
1515 static void pref_list_set(const gchar *group, const gchar *key, const gchar *marker, const gchar *text)
1516 {
1517         gchar *token;
1518         gchar *path;
1519         GList *work;
1520
1521         if (!group || !key || !marker) return;
1522
1523         token = g_strconcat(key, marker, NULL);
1524         path = g_strconcat(token, text, NULL);
1525
1526         work = pref_list_find(group, token);
1527         if (work)
1528                 {
1529                 gchar *old_path = work->data;
1530
1531                 if (text)
1532                         {
1533                         work->data = path;
1534                         path = NULL;
1535
1536                         g_free(old_path);
1537                         }
1538                 else
1539                         {
1540                         history_list_item_remove(group, old_path);
1541                         }
1542                 }
1543         else if (text)
1544                 {
1545                 history_list_add_to_key(group, path, 0);
1546                 }
1547
1548         g_free(path);
1549         g_free(token);
1550 }
1551
1552 void pref_list_int_set(const gchar *group, const gchar *key, gint value)
1553 {
1554         gchar *text;
1555
1556         text = g_strdup_printf("%d", value);
1557         pref_list_set(group, key, PREF_LIST_MARKER_INT, text);
1558         g_free(text);
1559 }
1560
1561 gboolean pref_list_int_get(const gchar *group, const gchar *key, gint *result)
1562 {
1563         const gchar *text;
1564
1565         if (!group || !key)
1566                 {
1567                 *result = 0;
1568                 return FALSE;
1569                 }
1570
1571         if (pref_list_get(group, key, PREF_LIST_MARKER_INT, &text) && text)
1572                 {
1573                 *result = (gint)strtol(text, NULL, 10);
1574                 return TRUE;
1575                 }
1576
1577         *result = 0;
1578         return FALSE;
1579 }
1580
1581 void pref_list_double_set(const gchar *group, const gchar *key, gdouble value)
1582 {
1583         gchar text[G_ASCII_DTOSTR_BUF_SIZE];
1584
1585         g_ascii_dtostr(text, sizeof(text), value);
1586         pref_list_set(group, key, PREF_LIST_MARKER_DOUBLE, text);
1587 }
1588
1589 gboolean pref_list_double_get(const gchar *group, const gchar *key, gdouble *result)
1590 {
1591         const gchar *text;
1592
1593         if (!group || !key)
1594                 {
1595                 *result = 0;
1596                 return FALSE;
1597                 }
1598
1599         if (pref_list_get(group, key, PREF_LIST_MARKER_DOUBLE, &text) && text)
1600                 {
1601                 *result = g_ascii_strtod(text, NULL);
1602                 return TRUE;
1603                 }
1604
1605         *result = 0;
1606         return FALSE;
1607 }
1608
1609 void pref_list_string_set(const gchar *group, const gchar *key, const gchar *value)
1610 {
1611         pref_list_set(group, key, PREF_LIST_MARKER_STRING, value);
1612 }
1613
1614 gboolean pref_list_string_get(const gchar *group, const gchar *key, const gchar **result)
1615 {
1616         return pref_list_get(group, key, PREF_LIST_MARKER_STRING, result);
1617 }
1618
1619
1620 void pref_color_button_set_cb(GtkWidget *widget, gpointer data)
1621 {
1622         GdkColor *color = data;
1623
1624         gtk_color_button_get_color(GTK_COLOR_BUTTON(widget), color);
1625 }
1626
1627 GtkWidget *pref_color_button_new(GtkWidget *parent_box,
1628                                  const gchar *title, const GdkColor *color,
1629                                  GCallback func, gpointer data)
1630 {
1631         GtkWidget *button;
1632
1633         if (color)
1634                 {
1635                 button = gtk_color_button_new_with_color(color);
1636                 }
1637         else
1638                 {
1639                 button = gtk_color_button_new();
1640                 }
1641
1642         if (func) g_signal_connect(G_OBJECT(button), "color-set", func, data);
1643
1644         if (title)
1645                 {
1646                 GtkWidget *label;
1647                 GtkWidget *hbox;
1648
1649                 gtk_color_button_set_title(GTK_COLOR_BUTTON(button), title);
1650                 label = gtk_label_new(title);
1651
1652                 hbox = gtk_hbox_new(TRUE, 0);
1653                 gtk_box_pack_start(GTK_BOX(parent_box), hbox, TRUE, TRUE, 0);
1654
1655                 gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 0);
1656                 gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
1657
1658                 gtk_widget_show_all(hbox);
1659                 }
1660         else
1661                 {
1662                 gtk_widget_show(button);
1663                 }
1664
1665         return button;
1666 }
1667
1668 /*
1669  *-----------------------------------------------------------------------------
1670  * text widget
1671  *-----------------------------------------------------------------------------
1672  */
1673
1674 gchar *text_widget_text_pull(GtkWidget *text_widget)
1675 {
1676         if (GTK_IS_TEXT_VIEW(text_widget))
1677                 {
1678                 GtkTextBuffer *buffer;
1679                 GtkTextIter start, end;
1680
1681                 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_widget));
1682                 gtk_text_buffer_get_bounds(buffer, &start, &end);
1683
1684                 return gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
1685                 }
1686         else if (GTK_IS_ENTRY(text_widget))
1687                 {
1688                 return g_strdup(gtk_entry_get_text(GTK_ENTRY(text_widget)));
1689                 }
1690         else
1691                 {
1692                 return NULL;
1693                 }
1694
1695 }
1696
1697 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */