Updates references from using underscore filenames to hyphen filenames for files...
[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 #include <config.h>
23 #include "intl.h"
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include <gtk/gtk.h>
30 #include <gdk/gdkkeysyms.h>
31
32 #include "main.h"
33 #include "ui-misc.h"
34
35 #include "history-list.h"
36
37 #include <langinfo.h>
38
39 /*
40  *-----------------------------------------------------------------------------
41  * widget and layout utilities
42  *-----------------------------------------------------------------------------
43  */
44
45 GtkWidget *pref_box_new(GtkWidget *parent_box, gboolean fill,
46                         GtkOrientation orientation, gboolean padding)
47 {
48         GtkWidget *box;
49
50         if (orientation == GTK_ORIENTATION_HORIZONTAL)
51                 {
52                 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, padding);
53                 }
54         else
55                 {
56                 box = gtk_box_new(GTK_ORIENTATION_VERTICAL, padding);
57                 }
58
59         gtk_box_pack_start(GTK_BOX(parent_box), box, fill, fill, 0);
60         gtk_widget_show(box);
61
62         return box;
63 }
64
65 GtkWidget *pref_group_new(GtkWidget *parent_box, gboolean fill,
66                           const gchar *text, GtkOrientation orientation)
67 {
68         GtkWidget *box;
69         GtkWidget *vbox;
70         GtkWidget *hbox;
71         GtkWidget *label;
72
73         vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
74
75         /* add additional spacing if necessary */
76         if (GTK_IS_VBOX(parent_box))
77                 {
78                 GList *list = gtk_container_get_children(GTK_CONTAINER(parent_box));
79                 if (list)
80                         {
81                         pref_spacer(vbox, PREF_PAD_GROUP - PREF_PAD_GAP);
82                         }
83                 g_list_free(list);
84                 }
85
86         gtk_box_pack_start(GTK_BOX(parent_box), vbox, fill, fill, 0);
87         gtk_widget_show(vbox);
88
89         label = gtk_label_new(text);
90         gtk_label_set_xalign(GTK_LABEL(label), 0.0);
91         gtk_label_set_yalign(GTK_LABEL(label), 0.5);
92         pref_label_bold(label, TRUE, FALSE);
93
94         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
95         gtk_widget_show(label);
96
97         hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_INDENT);
98         gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 0);
99         gtk_widget_show(hbox);
100
101         /* indent using empty box */
102         pref_spacer(hbox, 0);
103
104         if (orientation == GTK_ORIENTATION_HORIZONTAL)
105                 {
106                 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_SPACE);
107                 }
108         else
109                 {
110                 box = gtk_box_new(GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
111                 }
112         gtk_box_pack_start(GTK_BOX(hbox), box, TRUE, TRUE, 0);
113         gtk_widget_show(box);
114
115         g_object_set_data(G_OBJECT(box), "pref_group", vbox);
116
117         return box;
118 }
119
120 GtkWidget *pref_group_parent(GtkWidget *child)
121 {
122         GtkWidget *parent;
123
124         parent = child;
125         while (parent)
126                 {
127                 GtkWidget *group;
128
129                 group = g_object_get_data(G_OBJECT(parent), "pref_group");
130                 if (group && GTK_IS_WIDGET(group)) return group;
131
132                 parent = gtk_widget_get_parent(parent);
133                 }
134
135         return child;
136 }
137
138 GtkWidget *pref_frame_new(GtkWidget *parent_box, gboolean fill,
139                           const gchar *text,
140                           GtkOrientation orientation, gboolean padding)
141 {
142         GtkWidget *box;
143         GtkWidget *frame = NULL;
144
145         frame = gtk_frame_new(text);
146         gtk_box_pack_start(GTK_BOX(parent_box), frame, fill, fill, 0);
147         gtk_widget_show(frame);
148
149         if (orientation == GTK_ORIENTATION_HORIZONTAL)
150                 {
151                 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, padding);
152                 }
153         else
154                 {
155                 box = gtk_box_new(GTK_ORIENTATION_VERTICAL, padding);
156                 }
157         gtk_container_add(GTK_CONTAINER(frame), box);
158         gtk_container_set_border_width(GTK_CONTAINER(box), PREF_PAD_BORDER);
159         gtk_widget_show(box);
160
161         return box;
162 }
163
164 GtkWidget *pref_spacer(GtkWidget *parent_box, gboolean padding)
165 {
166         GtkWidget *spacer;
167
168         spacer = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
169         gtk_box_pack_start(GTK_BOX(parent_box), spacer, FALSE, FALSE, padding / 2);
170         gtk_widget_show(spacer);
171
172         return spacer;
173 }
174
175 GtkWidget *pref_line(GtkWidget *parent_box, gboolean padding)
176 {
177         GtkWidget *spacer;
178
179         if (GTK_IS_HBOX(parent_box))
180                 {
181                 spacer = gtk_vseparator_new();
182                 }
183         else
184                 {
185                 spacer = gtk_hseparator_new();
186                 }
187
188         gtk_box_pack_start(GTK_BOX(parent_box), spacer, FALSE, FALSE, padding / 2);
189         gtk_widget_show(spacer);
190
191         return spacer;
192 }
193
194 GtkWidget *pref_label_new(GtkWidget *parent_box, const gchar *text)
195 {
196         GtkWidget *label;
197
198         label = gtk_label_new(text);
199         gtk_box_pack_start(GTK_BOX(parent_box), label, FALSE, FALSE, 0);
200         gtk_widget_show(label);
201
202         return label;
203 }
204
205 GtkWidget *pref_label_new_mnemonic(GtkWidget *parent_box, const gchar *text, GtkWidget *widget)
206 {
207         GtkWidget *label;
208
209         label = gtk_label_new_with_mnemonic(text);
210         gtk_label_set_mnemonic_widget(GTK_LABEL(label), widget);
211         gtk_box_pack_start(GTK_BOX(parent_box), label, FALSE, FALSE, 0);
212         gtk_widget_show(label);
213
214         return label;
215 }
216
217 void pref_label_bold(GtkWidget *label, gboolean bold, gboolean increase_size)
218 {
219         PangoAttrList *pal;
220         PangoAttribute *pa;
221
222         if (!bold && !increase_size) return;
223
224         pal = pango_attr_list_new();
225
226         if (bold)
227                 {
228                 pa = pango_attr_weight_new(PANGO_WEIGHT_BOLD);
229                 pa->start_index = 0;
230                 pa->end_index = G_MAXINT;
231                 pango_attr_list_insert(pal, pa);
232                 }
233
234         if (increase_size)
235                 {
236                 pa = pango_attr_scale_new(PANGO_SCALE_LARGE);
237                 pa->start_index = 0;
238                 pa->end_index = G_MAXINT;
239                 pango_attr_list_insert(pal, pa);
240                 }
241
242         gtk_label_set_attributes(GTK_LABEL(label), pal);
243         pango_attr_list_unref(pal);
244 }
245
246 GtkWidget *pref_button_new(GtkWidget *parent_box, const gchar *stock_id,
247                            const gchar *text, gboolean hide_stock_text,
248                            GCallback func, gpointer data)
249 {
250         GtkWidget *button;
251
252         if (stock_id && !text && !hide_stock_text)
253                 {
254                 button = gtk_button_new_from_stock(stock_id);
255                 }
256         else
257                 {
258                 GtkWidget *image = NULL;
259                 GtkWidget *label = NULL;
260
261                 button = gtk_button_new();
262
263                 if (stock_id) image = gtk_image_new_from_icon_name(stock_id, GTK_ICON_SIZE_BUTTON);
264                 if (text)
265                         {
266                         label = gtk_label_new_with_mnemonic(text);
267                         gtk_label_set_xalign(GTK_LABEL(label), 0.5);
268                         gtk_label_set_yalign(GTK_LABEL(label), 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_box_new(GTK_ORIENTATION_HORIZONTAL, 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 UNUSED(func), gpointer UNUSED(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 UNUSED(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_box_new(GTK_ORIENTATION_VERTICAL, 0);
608                 box = pref_group_new(shell, TRUE, text, orientation);
609                 }
610         else
611                 {
612                 if (orientation == GTK_ORIENTATION_HORIZONTAL)
613                         {
614                         box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_SPACE);
615                         }
616                 else
617                         {
618                         box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 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_box_new(GTK_ORIENTATION_HORIZONTAL, 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 *UNUSED(widget), GdkEventButton *UNUSED(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 *UNUSED(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 *UNUSED(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 *UNUSED(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 *UNUSED(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 *UNUSED(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 *UNUSED(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_box_new(GTK_ORIENTATION_HORIZONTAL, 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                                         log_printf("Warning: 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 *UNUSED(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 *UNUSED(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 *UNUSED(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 *UNUSED(widget), GdkEventCrossing *UNUSED(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 *UNUSED(widget), GdkEventCrossing *UNUSED(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 *UNUSED(event), gpointer UNUSED(data))
1336 {
1337         GtkAllocation allocation;
1338
1339         gtk_widget_get_allocation(widget, &allocation);
1340
1341         cairo_t *cr = gdk_cairo_create(gtk_widget_get_window(widget));
1342
1343         gtk_render_handle (gtk_widget_get_style_context (widget),
1344                            cr, allocation.x, allocation.y, allocation.width, allocation.height);
1345         cairo_destroy(cr);
1346
1347         return TRUE;
1348 }
1349
1350 static void sizer_realize_cb(GtkWidget *widget, gpointer data)
1351 {
1352         SizerData *sd = data;
1353         GdkCursorType n;
1354
1355         n = 0;
1356         if (sd->position & SIZER_POS_TOP || sd->position & SIZER_POS_BOTTOM)
1357                 {
1358                 n = GDK_SB_V_DOUBLE_ARROW;
1359                 }
1360         if (sd->position & SIZER_POS_LEFT || sd->position & SIZER_POS_RIGHT)
1361                 {
1362                 n = (n != 0) ? GDK_FLEUR : GDK_SB_H_DOUBLE_ARROW;
1363                 }
1364
1365         if (n != 0 && gtk_widget_get_window(widget))
1366                 {
1367                 GdkCursor *cursor;
1368                 cursor = gdk_cursor_new(n);
1369                 gdk_window_set_cursor(gtk_widget_get_window(widget), cursor);
1370                 g_object_unref(G_OBJECT(cursor));
1371                 }
1372 }
1373
1374 static void sizer_destroy_cb(GtkWidget *UNUSED(widget), gpointer data)
1375 {
1376         SizerData *sd = data;
1377
1378         g_free(sd);
1379 }
1380
1381 GtkWidget *sizer_new(GtkWidget *parent, GtkWidget *bounding_widget,
1382                      SizerPositionType position)
1383 {
1384         SizerData *sd;
1385         gint handle_size;
1386
1387         sd = g_new0(SizerData, 1);
1388
1389         sd->sizer = gtk_event_box_new();
1390         sd->parent = parent;
1391         sd->bounding_widget = bounding_widget;
1392         sd->position = position;
1393         sd->hsize_min = -1;
1394         sd->hsize_max = -1;
1395         sd->vsize_min = -1;
1396         sd->vsize_max = -1;
1397
1398         sd->in_drag = FALSE;
1399         sd->handle_prelit = FALSE;
1400
1401         g_signal_connect(G_OBJECT(sd->sizer), "destroy",
1402                          G_CALLBACK(sizer_destroy_cb), sd);
1403
1404         g_signal_connect(G_OBJECT(sd->sizer), "motion_notify_event",
1405                          G_CALLBACK(sizer_motion_cb), sd);
1406         g_signal_connect(G_OBJECT(sd->sizer), "button_press_event",
1407                          G_CALLBACK(sizer_press_cb), sd);
1408         g_signal_connect(G_OBJECT(sd->sizer), "button_release_event",
1409                          G_CALLBACK(sizer_release_cb), sd);
1410
1411         g_signal_connect(G_OBJECT(sd->sizer), "enter_notify_event",
1412                          G_CALLBACK(sizer_enter_cb), sd);
1413         g_signal_connect(G_OBJECT(sd->sizer), "leave_notify_event",
1414                          G_CALLBACK(sizer_leave_cb), sd);
1415
1416         gtk_widget_set_events(sd->sizer, GDK_POINTER_MOTION_MASK |
1417                                          GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_PRESS_MASK |
1418                                          GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK);
1419
1420         g_signal_connect(sd->sizer, "realize",
1421                          G_CALLBACK(sizer_realize_cb), sd);
1422         g_signal_connect(sd->sizer, "expose_event",
1423                          G_CALLBACK(sizer_expose_cb), sd);
1424
1425         handle_size = sizer_default_handle_size();
1426
1427         gtk_widget_set_size_request(sd->sizer, handle_size, handle_size);
1428 #if 0
1429         /* use this if you add a shadow border to the handle */
1430         gtk_widget_set_size_request(sd->sizer, handle_size + sd->sizer->style->xthickness * 2,
1431                                                handle_size + sd->sizer->style->ythickness * 2);
1432 #endif
1433
1434         g_object_set_data(G_OBJECT(sd->sizer), SIZER_DATA_KEY,sd);
1435
1436         return sd->sizer;
1437 }
1438
1439 void sizer_set_limits(GtkWidget *sizer,
1440                       gint hsize_min, gint hsize_max,
1441                       gint vsize_min, gint vsize_max)
1442 {
1443         SizerData *sd;
1444
1445         sd = g_object_get_data(G_OBJECT(sizer), SIZER_DATA_KEY);
1446         if (!sd) return;
1447
1448         sd->hsize_min = hsize_min;
1449         sd->hsize_max = hsize_max;
1450         sd->vsize_min = vsize_min;
1451         sd->vsize_max = vsize_max;
1452 }
1453
1454
1455 /*
1456  *-----------------------------------------------------------------------------
1457  * storing data in a history list with key,data pairs
1458  *-----------------------------------------------------------------------------
1459  */
1460
1461 #define PREF_LIST_MARKER_INT "[INT]:"
1462 #define PREF_LIST_MARKER_DOUBLE "[DOUBLE]:"
1463 #define PREF_LIST_MARKER_STRING "[STRING]:"
1464
1465 static GList *pref_list_find(const gchar *group, const gchar *token)
1466 {
1467         GList *work;
1468         gint l;
1469
1470         l = strlen(token);
1471
1472         work = history_list_get_by_key(group);
1473         while (work)
1474                 {
1475                 const gchar *text = work->data;
1476
1477                 if (strncmp(text, token, l) == 0) return work;
1478
1479                 work = work->next;
1480                 }
1481
1482         return NULL;
1483 }
1484
1485 static gboolean pref_list_get(const gchar *group, const gchar *key, const gchar *marker, const gchar **result)
1486 {
1487         gchar *token;
1488         GList *work;
1489         gboolean ret;
1490
1491         if (!group || !key || !marker)
1492                 {
1493                 *result = NULL;
1494                 return FALSE;
1495                 }
1496
1497         token = g_strconcat(key, marker, NULL);
1498
1499         work = pref_list_find(group, token);
1500         if (work)
1501                 {
1502                 *result = (const gchar *)work->data + strlen(token);
1503                 if (strlen(*result) == 0) *result = NULL;
1504                 ret = TRUE;
1505                 }
1506         else
1507                 {
1508                 *result = NULL;
1509                 ret = FALSE;
1510                 }
1511
1512         g_free(token);
1513
1514         return ret;
1515 }
1516
1517 static void pref_list_set(const gchar *group, const gchar *key, const gchar *marker, const gchar *text)
1518 {
1519         gchar *token;
1520         gchar *path;
1521         GList *work;
1522
1523         if (!group || !key || !marker) return;
1524
1525         token = g_strconcat(key, marker, NULL);
1526         path = g_strconcat(token, text, NULL);
1527
1528         work = pref_list_find(group, token);
1529         if (work)
1530                 {
1531                 gchar *old_path = work->data;
1532
1533                 if (text)
1534                         {
1535                         work->data = path;
1536                         path = NULL;
1537
1538                         g_free(old_path);
1539                         }
1540                 else
1541                         {
1542                         history_list_item_remove(group, old_path);
1543                         }
1544                 }
1545         else if (text)
1546                 {
1547                 history_list_add_to_key(group, path, 0);
1548                 }
1549
1550         g_free(path);
1551         g_free(token);
1552 }
1553
1554 void pref_list_int_set(const gchar *group, const gchar *key, gint value)
1555 {
1556         gchar *text;
1557
1558         text = g_strdup_printf("%d", value);
1559         pref_list_set(group, key, PREF_LIST_MARKER_INT, text);
1560         g_free(text);
1561 }
1562
1563 gboolean pref_list_int_get(const gchar *group, const gchar *key, gint *result)
1564 {
1565         const gchar *text;
1566
1567         if (!group || !key)
1568                 {
1569                 *result = 0;
1570                 return FALSE;
1571                 }
1572
1573         if (pref_list_get(group, key, PREF_LIST_MARKER_INT, &text) && text)
1574                 {
1575                 *result = (gint)strtol(text, NULL, 10);
1576                 return TRUE;
1577                 }
1578
1579         *result = 0;
1580         return FALSE;
1581 }
1582
1583 void pref_list_double_set(const gchar *group, const gchar *key, gdouble value)
1584 {
1585         gchar text[G_ASCII_DTOSTR_BUF_SIZE];
1586
1587         g_ascii_dtostr(text, sizeof(text), value);
1588         pref_list_set(group, key, PREF_LIST_MARKER_DOUBLE, text);
1589 }
1590
1591 gboolean pref_list_double_get(const gchar *group, const gchar *key, gdouble *result)
1592 {
1593         const gchar *text;
1594
1595         if (!group || !key)
1596                 {
1597                 *result = 0;
1598                 return FALSE;
1599                 }
1600
1601         if (pref_list_get(group, key, PREF_LIST_MARKER_DOUBLE, &text) && text)
1602                 {
1603                 *result = g_ascii_strtod(text, NULL);
1604                 return TRUE;
1605                 }
1606
1607         *result = 0;
1608         return FALSE;
1609 }
1610
1611 void pref_list_string_set(const gchar *group, const gchar *key, const gchar *value)
1612 {
1613         pref_list_set(group, key, PREF_LIST_MARKER_STRING, value);
1614 }
1615
1616 gboolean pref_list_string_get(const gchar *group, const gchar *key, const gchar **result)
1617 {
1618         return pref_list_get(group, key, PREF_LIST_MARKER_STRING, result);
1619 }
1620
1621
1622 void pref_color_button_set_cb(GtkWidget *widget, gpointer data)
1623 {
1624         GdkColor *color = data;
1625
1626         gtk_color_button_get_color(GTK_COLOR_BUTTON(widget), color);
1627 }
1628
1629 GtkWidget *pref_color_button_new(GtkWidget *parent_box,
1630                                  const gchar *title, const GdkColor *color,
1631                                  GCallback func, gpointer data)
1632 {
1633         GtkWidget *button;
1634
1635         if (color)
1636                 {
1637                 button = gtk_color_button_new_with_color(color);
1638                 }
1639         else
1640                 {
1641                 button = gtk_color_button_new();
1642                 }
1643
1644         if (func) g_signal_connect(G_OBJECT(button), "color-set", func, data);
1645
1646         if (title)
1647                 {
1648                 GtkWidget *label;
1649                 GtkWidget *hbox;
1650
1651                 gtk_color_button_set_title(GTK_COLOR_BUTTON(button), title);
1652                 label = gtk_label_new(title);
1653
1654                 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
1655                 gtk_box_pack_start(GTK_BOX(parent_box), hbox, TRUE, TRUE, 0);
1656
1657                 gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 0);
1658                 gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
1659
1660                 gtk_widget_show_all(hbox);
1661                 }
1662         else
1663                 {
1664                 gtk_widget_show(button);
1665                 }
1666
1667         return button;
1668 }
1669
1670 /*
1671  *-----------------------------------------------------------------------------
1672  * text widget
1673  *-----------------------------------------------------------------------------
1674  */
1675
1676 gchar *text_widget_text_pull(GtkWidget *text_widget)
1677 {
1678         if (GTK_IS_TEXT_VIEW(text_widget))
1679                 {
1680                 GtkTextBuffer *buffer;
1681                 GtkTextIter start, end;
1682
1683                 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_widget));
1684                 gtk_text_buffer_get_bounds(buffer, &start, &end);
1685
1686                 return gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
1687                 }
1688         else if (GTK_IS_ENTRY(text_widget))
1689                 {
1690                 return g_strdup(gtk_entry_get_text(GTK_ENTRY(text_widget)));
1691                 }
1692         else
1693                 {
1694                 return NULL;
1695                 }
1696
1697 }
1698
1699 gchar *text_widget_text_pull_selected(GtkWidget *text_widget)
1700 {
1701         if (GTK_IS_TEXT_VIEW(text_widget))
1702                 {
1703                 GtkTextBuffer *buffer;
1704                 GtkTextIter start, end;
1705
1706                 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_widget));
1707                 gtk_text_buffer_get_bounds(buffer, &start, &end);
1708
1709                 if (gtk_text_buffer_get_selection_bounds(buffer, &start, &end))
1710                         {
1711                         gtk_text_iter_set_line_offset(&start, 0);
1712                         gtk_text_iter_forward_to_line_end(&end);
1713                         }
1714
1715                 return gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
1716                 }
1717         else if (GTK_IS_ENTRY(text_widget))
1718                 {
1719                 return g_strdup(gtk_entry_get_text(GTK_ENTRY(text_widget)));
1720                 }
1721         else
1722                 {
1723                 return NULL;
1724                 }
1725 }
1726
1727 gboolean defined_mouse_buttons(GtkWidget *UNUSED(widget), GdkEventButton *event, gpointer data)
1728 {
1729         LayoutWindow *lw = data;
1730         GtkAction *action;
1731         gboolean ret = FALSE;
1732
1733         switch (event->button)
1734                 {
1735                 case MOUSE_BUTTON_8:
1736                         if (options->mouse_button_8)
1737                                 {
1738                                 action = gtk_action_group_get_action(lw->action_group, options->mouse_button_8);
1739                                 if (action)
1740                                         {
1741                                         gtk_action_activate(action);
1742                                         }
1743                                 ret = TRUE;
1744                                 }
1745                                 break;
1746                 case MOUSE_BUTTON_9:
1747                         if (options->mouse_button_9)
1748                                 {
1749                                 action = gtk_action_group_get_action(lw->action_group, options->mouse_button_9);
1750                                 if (action)
1751                                         {
1752                                         gtk_action_activate(action);
1753                                         }
1754                                 ret = TRUE;
1755                                 }
1756                         break;
1757                 default:
1758                         break;
1759                 }
1760
1761         return ret;
1762 }
1763
1764 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */