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