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