Bug fix: Copy icon not displayed
[geeqie.git] / src / ui-misc.cc
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 <stdlib.h>
23 #include <string.h>
24
25 #include "main.h"
26 #include "ui-misc.h"
27
28 #include "history-list.h"
29
30 #include <langinfo.h>
31
32 /*
33  *-----------------------------------------------------------------------------
34  * widget and layout utilities
35  *-----------------------------------------------------------------------------
36  */
37
38 GtkWidget *pref_box_new(GtkWidget *parent_box, gboolean fill,
39                         GtkOrientation orientation, gboolean padding)
40 {
41         GtkWidget *box;
42
43         if (orientation == GTK_ORIENTATION_HORIZONTAL)
44                 {
45                 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, padding);
46                 }
47         else
48                 {
49                 box = gtk_box_new(GTK_ORIENTATION_VERTICAL, padding);
50                 }
51
52         gtk_box_pack_start(GTK_BOX(parent_box), box, fill, fill, 0);
53         gtk_widget_show(box);
54
55         return box;
56 }
57
58 GtkWidget *pref_group_new(GtkWidget *parent_box, gboolean fill,
59                           const gchar *text, GtkOrientation orientation)
60 {
61         GtkWidget *box;
62         GtkWidget *vbox;
63         GtkWidget *hbox;
64         GtkWidget *label;
65
66         vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
67
68         /* add additional spacing if necessary */
69         if (GTK_IS_VBOX(parent_box))
70                 {
71                 GList *list = gtk_container_get_children(GTK_CONTAINER(parent_box));
72                 if (list)
73                         {
74                         pref_spacer(vbox, PREF_PAD_GROUP - PREF_PAD_GAP);
75                         }
76                 g_list_free(list);
77                 }
78
79         gtk_box_pack_start(GTK_BOX(parent_box), vbox, fill, fill, 0);
80         gtk_widget_show(vbox);
81
82         label = gtk_label_new(text);
83         gtk_label_set_xalign(GTK_LABEL(label), 0.0);
84         gtk_label_set_yalign(GTK_LABEL(label), 0.5);
85         pref_label_bold(label, TRUE, FALSE);
86
87         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
88         gtk_widget_show(label);
89
90         hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_INDENT);
91         gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 0);
92         gtk_widget_show(hbox);
93
94         /* indent using empty box */
95         pref_spacer(hbox, 0);
96
97         if (orientation == GTK_ORIENTATION_HORIZONTAL)
98                 {
99                 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_SPACE);
100                 }
101         else
102                 {
103                 box = gtk_box_new(GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
104                 }
105         gtk_box_pack_start(GTK_BOX(hbox), box, TRUE, TRUE, 0);
106         gtk_widget_show(box);
107
108         g_object_set_data(G_OBJECT(box), "pref_group", vbox);
109
110         return box;
111 }
112
113 GtkWidget *pref_group_parent(GtkWidget *child)
114 {
115         GtkWidget *parent;
116
117         parent = child;
118         while (parent)
119                 {
120                 GtkWidget *group;
121
122                 group = static_cast<GtkWidget *>(g_object_get_data(G_OBJECT(parent), "pref_group"));
123                 if (group && GTK_IS_WIDGET(group)) return group;
124
125                 parent = gtk_widget_get_parent(parent);
126                 }
127
128         return child;
129 }
130
131 GtkWidget *pref_frame_new(GtkWidget *parent_box, gboolean fill,
132                           const gchar *text,
133                           GtkOrientation orientation, gboolean padding)
134 {
135         GtkWidget *box;
136         GtkWidget *frame = NULL;
137
138         frame = gtk_frame_new(text);
139         gtk_box_pack_start(GTK_BOX(parent_box), frame, fill, fill, 0);
140         gtk_widget_show(frame);
141
142         if (orientation == GTK_ORIENTATION_HORIZONTAL)
143                 {
144                 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, padding);
145                 }
146         else
147                 {
148                 box = gtk_box_new(GTK_ORIENTATION_VERTICAL, padding);
149                 }
150         gtk_container_add(GTK_CONTAINER(frame), box);
151         gtk_container_set_border_width(GTK_CONTAINER(box), PREF_PAD_BORDER);
152         gtk_widget_show(box);
153
154         return box;
155 }
156
157 GtkWidget *pref_spacer(GtkWidget *parent_box, gboolean padding)
158 {
159         GtkWidget *spacer;
160
161         spacer = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
162         gtk_box_pack_start(GTK_BOX(parent_box), spacer, FALSE, FALSE, padding / 2);
163         gtk_widget_show(spacer);
164
165         return spacer;
166 }
167
168 GtkWidget *pref_line(GtkWidget *parent_box, gboolean padding)
169 {
170         GtkWidget *spacer;
171
172         if (GTK_IS_HBOX(parent_box))
173                 {
174                 spacer = gtk_vseparator_new();
175                 }
176         else
177                 {
178                 spacer = gtk_hseparator_new();
179                 }
180
181         gtk_box_pack_start(GTK_BOX(parent_box), spacer, FALSE, FALSE, padding / 2);
182         gtk_widget_show(spacer);
183
184         return spacer;
185 }
186
187 GtkWidget *pref_label_new(GtkWidget *parent_box, const gchar *text)
188 {
189         GtkWidget *label;
190
191         label = gtk_label_new(text);
192         gtk_box_pack_start(GTK_BOX(parent_box), label, FALSE, FALSE, 0);
193         gtk_widget_show(label);
194
195         return label;
196 }
197
198 GtkWidget *pref_label_new_mnemonic(GtkWidget *parent_box, const gchar *text, GtkWidget *widget)
199 {
200         GtkWidget *label;
201
202         label = gtk_label_new_with_mnemonic(text);
203         gtk_label_set_mnemonic_widget(GTK_LABEL(label), widget);
204         gtk_box_pack_start(GTK_BOX(parent_box), label, FALSE, FALSE, 0);
205         gtk_widget_show(label);
206
207         return label;
208 }
209
210 void pref_label_bold(GtkWidget *label, gboolean bold, gboolean increase_size)
211 {
212         PangoAttrList *pal;
213         PangoAttribute *pa;
214
215         if (!bold && !increase_size) return;
216
217         pal = pango_attr_list_new();
218
219         if (bold)
220                 {
221                 pa = pango_attr_weight_new(PANGO_WEIGHT_BOLD);
222                 pa->start_index = 0;
223                 pa->end_index = G_MAXINT;
224                 pango_attr_list_insert(pal, pa);
225                 }
226
227         if (increase_size)
228                 {
229                 pa = pango_attr_scale_new(PANGO_SCALE_LARGE);
230                 pa->start_index = 0;
231                 pa->end_index = G_MAXINT;
232                 pango_attr_list_insert(pal, pa);
233                 }
234
235         gtk_label_set_attributes(GTK_LABEL(label), pal);
236         pango_attr_list_unref(pal);
237 }
238
239 GtkWidget *pref_button_new(GtkWidget *parent_box, const gchar *stock_id,
240                            const gchar *text, gboolean hide_stock_text,
241                            GCallback func, gpointer data)
242 {
243         GtkWidget *button;
244
245         if (stock_id && !text && !hide_stock_text)
246                 {
247                 button = gtk_button_new_from_stock(stock_id);
248                 }
249         else
250                 {
251                 GtkWidget *image = NULL;
252                 GtkWidget *label = NULL;
253
254                 button = gtk_button_new();
255
256                 if (stock_id)
257                         {
258                         image = gtk_image_new_from_stock(stock_id, GTK_ICON_SIZE_BUTTON);
259                         }
260
261                 if (text)
262                         {
263                         label = gtk_label_new_with_mnemonic(text);
264                         gtk_label_set_xalign(GTK_LABEL(label), 0.5);
265                         gtk_label_set_yalign(GTK_LABEL(label), 0.5);
266                         gtk_label_set_mnemonic_widget(GTK_LABEL(label), button);
267                         }
268
269                 if (image && label)
270                         {
271                         GtkWidget *align;
272                         GtkWidget *hbox;
273
274                         hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_BUTTON_ICON_GAP);
275
276                         align = gtk_alignment_new(0.5, 0.5, 0.0, 0.0);
277                         gtk_container_add(GTK_CONTAINER(button), align);
278                         gtk_widget_show(align);
279
280                         gtk_container_add(GTK_CONTAINER(align), hbox);
281                         gtk_widget_show(hbox);
282
283                         gtk_box_pack_start(GTK_BOX(hbox), image, FALSE, FALSE, 0);
284                         gtk_box_pack_end(GTK_BOX(hbox), label, FALSE, FALSE, 0);
285                         }
286                 else
287                         {
288                         if (image)
289                                 {
290                                 gtk_container_add(GTK_CONTAINER(button), image);
291                                 }
292                         else if (label)
293                                 {
294                                 gtk_container_add(GTK_CONTAINER(button), label);
295                                 }
296                         }
297
298                 if (image) gtk_widget_show(image);
299                 if (label) gtk_widget_show(label);
300                 }
301
302         if (func) g_signal_connect(G_OBJECT(button), "clicked", func, data);
303
304         if (parent_box)
305                 {
306                 gtk_box_pack_start(GTK_BOX(parent_box), button, FALSE, FALSE, 0);
307                 gtk_widget_show(button);
308                 }
309
310         return button;
311 }
312
313 static GtkWidget *real_pref_checkbox_new(GtkWidget *parent_box, const gchar *text, gboolean mnemonic_text,
314                                          gboolean active, GCallback func, gpointer data)
315 {
316         GtkWidget *button;
317
318         if (mnemonic_text)
319                 {
320                 button = gtk_check_button_new_with_mnemonic(text);
321                 }
322         else
323                 {
324                 button = gtk_check_button_new_with_label(text);
325                 }
326         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), active);
327         if (func) g_signal_connect(G_OBJECT(button), "clicked", func, data);
328
329         gtk_box_pack_start(GTK_BOX(parent_box), button, FALSE, FALSE, 0);
330         gtk_widget_show(button);
331
332         return button;
333 }
334
335 GtkWidget *pref_checkbox_new(GtkWidget *parent_box, const gchar *text, gboolean active,
336                              GCallback func, gpointer data)
337 {
338         return real_pref_checkbox_new(parent_box, text, FALSE, active, func, data);
339 }
340
341 //GtkWidget *pref_checkbox_new_mnemonic(GtkWidget *parent_box, const gchar *text, gboolean active,
342                                       //GCallback func, gpointer data)
343 //{
344         //return real_pref_checkbox_new(parent_box, text, TRUE, active, func, data);
345 //}
346
347 static void pref_checkbox_int_cb(GtkWidget *widget, gpointer data)
348 {
349         gboolean *result = static_cast<gboolean *>(data);
350
351         *result = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
352 }
353
354 GtkWidget *pref_checkbox_new_int(GtkWidget *parent_box, const gchar *text, gboolean active,
355                                  gboolean *result)
356 {
357         GtkWidget *button;
358
359         button = pref_checkbox_new(parent_box, text, active,
360                                    G_CALLBACK(pref_checkbox_int_cb), result);
361         *result = active;
362
363         return button;
364 }
365
366 static void pref_checkbox_link_sensitivity_cb(GtkWidget *button, gpointer data)
367 {
368         GtkWidget *widget = static_cast<GtkWidget *>(data);
369
370         gtk_widget_set_sensitive(widget, gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button)));
371 }
372
373 void pref_checkbox_link_sensitivity(GtkWidget *button, GtkWidget *widget)
374 {
375         g_signal_connect(G_OBJECT(button), "toggled",
376                          G_CALLBACK(pref_checkbox_link_sensitivity_cb), widget);
377
378         pref_checkbox_link_sensitivity_cb(button, widget);
379 }
380
381 //static void pref_checkbox_link_sensitivity_swap_cb(GtkWidget *button, gpointer data)
382 //{
383         //GtkWidget *widget = static_cast<GtkWidget *>(data);
384
385         //gtk_widget_set_sensitive(widget, !gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button)));
386 //}
387
388 //void pref_checkbox_link_sensitivity_swap(GtkWidget *button, GtkWidget *widget)
389 //{
390         //g_signal_connect(G_OBJECT(button), "toggled",
391                          //G_CALLBACK(pref_checkbox_link_sensitivity_swap_cb), widget);
392
393         //pref_checkbox_link_sensitivity_swap_cb(button, widget);
394 //}
395
396 static GtkWidget *real_pref_radiobutton_new(GtkWidget *parent_box, GtkWidget *sibling,
397                                             const gchar *text, gboolean mnemonic_text, gboolean active,
398                                             GCallback func, gpointer data)
399 {
400         GtkWidget *button;
401         GSList *group;
402
403         if (sibling)
404                 {
405                 group = gtk_radio_button_get_group(GTK_RADIO_BUTTON(sibling));
406                 }
407         else
408                 {
409                 group = NULL;
410                 }
411
412         if (mnemonic_text)
413                 {
414                 button = gtk_radio_button_new_with_mnemonic(group, text);
415                 }
416         else
417                 {
418                 button = gtk_radio_button_new_with_label(group, text);
419                 }
420
421         if (active) gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), active);
422         if (func) g_signal_connect(G_OBJECT(button), "clicked", func, data);
423
424         gtk_box_pack_start(GTK_BOX(parent_box), button, FALSE, FALSE, 0);
425         gtk_widget_show(button);
426
427         return button;
428 }
429
430 GtkWidget *pref_radiobutton_new(GtkWidget *parent_box, GtkWidget *sibling,
431                                 const gchar *text, gboolean active,
432                                 GCallback func, gpointer data)
433 {
434         return real_pref_radiobutton_new(parent_box, sibling, text, FALSE, active, func, data);
435 }
436
437 //GtkWidget *pref_radiobutton_new_mnemonic(GtkWidget *parent_box, GtkWidget *sibling,
438                                          //const gchar *text, gboolean active,
439                                          //GCallback func, gpointer data)
440 //{
441         //return real_pref_radiobutton_new(parent_box, sibling, text, TRUE, active, func, data);
442 //}
443
444 #define PREF_RADIO_VALUE_KEY "pref_radio_value"
445
446 //static void pref_radiobutton_int_cb(GtkWidget *widget, gpointer data)
447 //{
448         //gboolean *result = static_cast<gboolean *>(data);
449
450         //if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
451                 //{
452                 //*result = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), PREF_RADIO_VALUE_KEY));
453                 //}
454 //}
455
456 //GtkWidget *pref_radiobutton_new_int(GtkWidget *parent_box, GtkWidget *sibling,
457                                     //const gchar *text, gboolean active,
458                                     //gboolean *result, gboolean value,
459                                     //GCallback UNUSED(func), gpointer UNUSED(data))
460 //{
461         //GtkWidget *button;
462
463         //button = pref_radiobutton_new(parent_box, sibling, text, active,
464                                       //G_CALLBACK(pref_radiobutton_int_cb), result);
465         //g_object_set_data(G_OBJECT(button), PREF_RADIO_VALUE_KEY, GINT_TO_POINTER(value));
466         //if (active) *result = value;
467
468         //return button;
469 //}
470
471 static GtkWidget *real_pref_spin_new(GtkWidget *parent_box, const gchar *text, const gchar *suffix,
472                                      gboolean mnemonic_text,
473                                      gdouble min, gdouble max, gdouble step, gint digits,
474                                      gdouble value,
475                                      GCallback func, gpointer data)
476 {
477         GtkWidget *spin;
478         GtkWidget *box;
479         GtkWidget *label;
480
481         box = pref_box_new(parent_box, FALSE, GTK_ORIENTATION_HORIZONTAL, PREF_PAD_SPACE);
482
483         spin = gtk_spin_button_new_with_range(min, max, step);
484         gtk_spin_button_set_digits(GTK_SPIN_BUTTON(spin), digits);
485         gtk_spin_button_set_value(GTK_SPIN_BUTTON(spin), value);
486
487         if (func)
488                 {
489                 g_signal_connect(G_OBJECT(spin), "value_changed", G_CALLBACK(func), data);
490                 }
491
492         if (text)
493                 {
494                 if (mnemonic_text)
495                         {
496                         label = pref_label_new_mnemonic(box, text, spin);
497                         }
498                 else
499                         {
500                         label = pref_label_new(box, text);
501                         }
502                 pref_link_sensitivity(label, spin);
503                 }
504
505         gtk_box_pack_start(GTK_BOX(box), spin, FALSE, FALSE, 0);
506         gtk_widget_show(spin);
507
508         /* perhaps this should only be PREF_PAD_GAP distance from spinbutton ? */
509         if (suffix)
510                 {
511                 label =  pref_label_new(box, suffix);
512                 pref_link_sensitivity(label, spin);
513                 }
514
515         return spin;
516 }
517
518 GtkWidget *pref_spin_new(GtkWidget *parent_box, const gchar *text, const gchar *suffix,
519                          gdouble min, gdouble max, gdouble step, gint digits,
520                          gdouble value,
521                          GCallback func, gpointer data)
522 {
523         return real_pref_spin_new(parent_box, text, suffix, FALSE,
524                                   min, max, step, digits, value, func, data);
525 }
526
527 //GtkWidget *pref_spin_new_mnemonic(GtkWidget *parent_box, const gchar *text, const gchar *suffix,
528                                   //gdouble min, gdouble max, gdouble step, gint digits,
529                                   //gdouble value,
530                                   //GCallback func, gpointer data)
531 //{
532         //return real_pref_spin_new(parent_box, text, suffix, TRUE,
533                                   //min, max, step, digits, value, func, data);
534 //}
535
536 static void pref_spin_int_cb(GtkWidget *widget, gpointer data)
537 {
538         gint *var = static_cast<gint *>(data);
539         *var = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(widget));
540 }
541
542 GtkWidget *pref_spin_new_int(GtkWidget *parent_box, const gchar *text, const gchar *suffix,
543                              gint min, gint max, gint step,
544                              gint value, gint *value_var)
545 {
546         *value_var = value;
547         return pref_spin_new(parent_box, text, suffix,
548                              (gdouble)min, (gdouble)max, (gdouble)step, 0,
549                              value,
550                              G_CALLBACK(pref_spin_int_cb), value_var);
551 }
552
553 static void pref_link_sensitivity_cb(GtkWidget *watch, GtkStateType UNUSED(prev_state), gpointer data)
554 {
555         GtkWidget *widget = static_cast<GtkWidget *>(data);
556
557         gtk_widget_set_sensitive(widget, gtk_widget_is_sensitive(watch));
558 }
559
560 void pref_link_sensitivity(GtkWidget *widget, GtkWidget *watch)
561 {
562         g_signal_connect(G_OBJECT(watch), "state_changed",
563                          G_CALLBACK(pref_link_sensitivity_cb), widget);
564 }
565
566 void pref_signal_block_data(GtkWidget *widget, gpointer data)
567 {
568         g_signal_handlers_block_matched(widget, G_SIGNAL_MATCH_DATA,
569                                         0, 0, NULL, NULL, data);
570 }
571
572 void pref_signal_unblock_data(GtkWidget *widget, gpointer data)
573 {
574         g_signal_handlers_unblock_matched(widget, G_SIGNAL_MATCH_DATA,
575                                           0, 0, NULL, NULL, data);
576 }
577
578 GtkWidget *pref_table_new(GtkWidget *parent_box, gint columns, gint rows,
579                           gboolean homogeneous, gboolean fill)
580 {
581         GtkWidget *table;
582
583         table = gtk_table_new(rows, columns, homogeneous);
584         gtk_table_set_row_spacings(GTK_TABLE(table), PREF_PAD_GAP);
585         gtk_table_set_col_spacings(GTK_TABLE(table), PREF_PAD_SPACE);
586
587         if (parent_box)
588                 {
589                 gtk_box_pack_start(GTK_BOX(parent_box), table, fill, fill, 0);
590                 gtk_widget_show(table);
591                 }
592
593         return table;
594 }
595
596 GtkWidget *pref_table_box(GtkWidget *table, gint column, gint row,
597                           GtkOrientation orientation, const gchar *text)
598 {
599         GtkWidget *box;
600         GtkWidget *shell;
601
602         if (text)
603                 {
604                 shell = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
605                 box = pref_group_new(shell, TRUE, text, orientation);
606                 }
607         else
608                 {
609                 if (orientation == GTK_ORIENTATION_HORIZONTAL)
610                         {
611                         box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_SPACE);
612                         }
613                 else
614                         {
615                         box = gtk_box_new(GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
616                         }
617                 shell = box;
618                 }
619
620         gtk_table_attach(GTK_TABLE(table), shell, column, column + 1, row, row + 1,
621                          static_cast<GtkAttachOptions>(GTK_EXPAND | GTK_FILL), static_cast<GtkAttachOptions>(0), 0, 0);
622
623         gtk_widget_show(shell);
624
625         return box;
626 }
627
628 GtkWidget *pref_table_label(GtkWidget *table, gint column, gint row,
629                             const gchar *text, gfloat alignment)
630 {
631         GtkWidget *label;
632         GtkWidget *align;
633
634         align = gtk_alignment_new(alignment, 0.50, 0.0, 0.0);
635         gtk_table_attach(GTK_TABLE(table), align, column, column + 1, row, row + 1,
636                          GTK_FILL, static_cast<GtkAttachOptions>(0), 0, 0);
637         gtk_widget_show(align);
638         label = gtk_label_new(text);
639         gtk_container_add(GTK_CONTAINER(align), label);
640         gtk_widget_show(label);
641
642         return label;
643 }
644
645 GtkWidget *pref_table_button(GtkWidget *table, gint column, gint row,
646                              const gchar *stock_id, const gchar *text, gboolean hide_stock_text,
647                              GCallback func, gpointer data)
648 {
649         GtkWidget *button;
650
651         button = pref_button_new(NULL, stock_id, text, hide_stock_text, func, data);
652         gtk_table_attach(GTK_TABLE(table), button, column, column + 1, row, row + 1,
653                          GTK_FILL, static_cast<GtkAttachOptions>(0), 0, 0);
654         gtk_widget_show(button);
655
656         return button;
657 }
658
659 GtkWidget *pref_table_spin(GtkWidget *table, gint column, gint row,
660                            const gchar *text, const gchar *suffix,
661                            gdouble min, gdouble max, gdouble step, gint digits,
662                            gdouble value,
663                            GCallback func, gpointer data)
664 {
665         GtkWidget *spin;
666         GtkWidget *box;
667         GtkWidget *label;
668
669         spin = gtk_spin_button_new_with_range(min, max, step);
670         gtk_spin_button_set_digits(GTK_SPIN_BUTTON(spin), digits);
671         gtk_spin_button_set_value(GTK_SPIN_BUTTON(spin), value);
672         if (func)
673                 {
674                 g_signal_connect(G_OBJECT(spin), "value_changed", G_CALLBACK(func), data);
675                 }
676
677         if (text)
678                 {
679                 label = pref_table_label(table, column, row, text, 1.0);
680                 pref_link_sensitivity(label, spin);
681                 column++;
682                 }
683
684         if (suffix)
685                 {
686                 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_SPACE);
687                 gtk_box_pack_start(GTK_BOX(box), spin, FALSE, FALSE, 0);
688                 gtk_widget_show(spin);
689
690                 label = pref_label_new(box, suffix);
691                 pref_link_sensitivity(label, spin);
692                 }
693         else
694                 {
695                 box = spin;
696                 }
697
698         gtk_table_attach(GTK_TABLE(table), box, column, column + 1, row, row + 1,
699                          static_cast<GtkAttachOptions>(GTK_EXPAND | GTK_FILL), static_cast<GtkAttachOptions>(GTK_EXPAND | GTK_FILL), 0, 0);
700         gtk_widget_show(box);
701
702         return spin;
703 }
704
705 GtkWidget *pref_table_spin_new_int(GtkWidget *table, gint column, gint row,
706                                    const gchar *text, const gchar *suffix,
707                                    gint min, gint max, gint step,
708                                    gint value, gint *value_var)
709 {
710         *value_var = value;
711         return pref_table_spin(table, column, row,
712                                text, suffix,
713                                (gdouble)min, (gdouble)max, (gdouble)step, 0,
714                                value,
715                                G_CALLBACK(pref_spin_int_cb), value_var);
716 }
717
718
719 GtkWidget *pref_toolbar_new(GtkWidget *parent_box, GtkToolbarStyle style)
720 {
721         GtkWidget *tbar;
722
723         tbar = gtk_toolbar_new();
724         gtk_toolbar_set_style(GTK_TOOLBAR(tbar), style);
725
726         if (parent_box)
727                 {
728                 gtk_box_pack_start(GTK_BOX(parent_box), tbar, FALSE, FALSE, 0);
729                 gtk_widget_show(tbar);
730                 }
731         return tbar;
732 }
733
734 GtkWidget *pref_toolbar_button(GtkWidget *toolbar,
735                                const gchar *stock_id, const gchar *label, gboolean toggle,
736                                const gchar *description,
737                                GCallback func, gpointer data)
738 {
739         GtkWidget *item;
740
741         if (toggle)
742                 {
743                 if (stock_id)
744                         {
745                         item = GTK_WIDGET(gtk_toggle_tool_button_new_from_stock(stock_id));
746                         }
747                 else
748                         {
749                         item = GTK_WIDGET(gtk_toggle_tool_button_new());
750                         }
751                 }
752         else
753                 {
754                 if (stock_id)
755                         {
756                         item = GTK_WIDGET(gtk_tool_button_new_from_stock(stock_id));
757                         }
758                 else
759                         {
760                         item = GTK_WIDGET(gtk_tool_button_new(NULL, NULL));
761                         }
762                 }
763         gtk_tool_button_set_use_underline(GTK_TOOL_BUTTON(item), TRUE);
764
765         if (label) gtk_tool_button_set_label(GTK_TOOL_BUTTON(item), label);
766
767         if (func) g_signal_connect(item, "clicked", func, data);
768         gtk_container_add(GTK_CONTAINER(toolbar), item);
769         gtk_widget_show(item);
770
771         if (description)
772                 {
773                 gtk_widget_set_tooltip_text(item, description);
774                 }
775
776         return item;
777 }
778
779 //void pref_toolbar_button_set_icon(GtkWidget *button, GtkWidget *widget, const gchar *stock_id)
780 //{
781         //if (widget)
782                 //{
783                 //gtk_tool_button_set_icon_widget(GTK_TOOL_BUTTON(button), widget);
784                 //}
785         //else if (stock_id)
786                 //{
787                 //gtk_tool_button_set_stock_id(GTK_TOOL_BUTTON(button), stock_id);
788                 //}
789 //}
790
791 //GtkWidget *pref_toolbar_spacer(GtkWidget *toolbar)
792 //{
793         //GtkWidget *item;
794
795         //item = GTK_WIDGET(gtk_separator_tool_item_new());
796         //gtk_container_add(GTK_CONTAINER(toolbar), item);
797         //gtk_widget_show(item);
798
799         //return item;
800 //}
801
802
803 /*
804  *-----------------------------------------------------------------------------
805  * date selection entry
806  *-----------------------------------------------------------------------------
807  */
808
809 #define DATE_SELECION_KEY "date_selection_data"
810
811
812 typedef struct _DateSelection DateSelection;
813 struct _DateSelection
814 {
815         GtkWidget *box;
816
817         GtkWidget *spin_d;
818         GtkWidget *spin_m;
819         GtkWidget *spin_y;
820
821         GtkWidget *button;
822
823         GtkWidget *window;
824         GtkWidget *calendar;
825 };
826
827
828 static void date_selection_popup_hide(DateSelection *ds)
829 {
830         if (!ds->window) return;
831
832         if (gtk_widget_has_grab(ds->window))
833                 {
834                 gtk_grab_remove(ds->window);
835                 gdk_keyboard_ungrab(GDK_CURRENT_TIME);
836                 gdk_pointer_ungrab(GDK_CURRENT_TIME);
837                 }
838
839         gtk_widget_hide(ds->window);
840
841         gtk_widget_destroy(ds->window);
842         ds->window = NULL;
843         ds->calendar = NULL;
844
845         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ds->button), FALSE);
846 }
847
848 static gboolean date_selection_popup_release_cb(GtkWidget *UNUSED(widget), GdkEventButton *UNUSED(event), gpointer data)
849 {
850         DateSelection *ds = static_cast<DateSelection *>(data);
851
852         date_selection_popup_hide(ds);
853         return TRUE;
854 }
855
856 static gboolean date_selection_popup_press_cb(GtkWidget *UNUSED(widget), GdkEventButton *event, gpointer data)
857 {
858         DateSelection *ds = static_cast<DateSelection *>(data);
859         gint x, y;
860         gint w, h;
861         gint xr, yr;
862         GdkWindow *window;
863
864         xr = (gint)event->x_root;
865         yr = (gint)event->y_root;
866
867         window = gtk_widget_get_window(ds->window);
868         gdk_window_get_origin(window, &x, &y);
869         w = gdk_window_get_width(window);
870         h = gdk_window_get_height(window);
871
872         if (xr < x || yr < y || xr > x + w || yr > y + h)
873                 {
874                 g_signal_connect(G_OBJECT(ds->window), "button_release_event",
875                                  G_CALLBACK(date_selection_popup_release_cb), ds);
876                 return TRUE;
877                 }
878
879         return FALSE;
880 }
881
882 static void date_selection_popup_sync(DateSelection *ds)
883 {
884         guint day, month, year;
885
886         gtk_calendar_get_date(GTK_CALENDAR(ds->calendar), &year, &month, &day);
887         date_selection_set(ds->box, day, month + 1, year);
888 }
889
890 static gboolean date_selection_popup_keypress_cb(GtkWidget *UNUSED(widget), GdkEventKey *event, gpointer data)
891 {
892         DateSelection *ds = static_cast<DateSelection *>(data);
893
894         switch (event->keyval)
895                 {
896                 case GDK_KEY_Return:
897                 case GDK_KEY_KP_Enter:
898                 case GDK_KEY_Tab:
899                 case GDK_KEY_ISO_Left_Tab:
900                         date_selection_popup_sync(ds);
901                         date_selection_popup_hide(ds);
902                         break;
903                 case GDK_KEY_Escape:
904                         date_selection_popup_hide(ds);
905                         break;
906                 default:
907                         break;
908                 }
909
910         return FALSE;
911 }
912
913 static void date_selection_day_cb(GtkWidget *UNUSED(widget), gpointer data)
914 {
915         DateSelection *ds = static_cast<DateSelection *>(data);
916
917         date_selection_popup_sync(ds);
918 }
919
920 static void date_selection_doubleclick_cb(GtkWidget *UNUSED(widget), gpointer data)
921 {
922         DateSelection *ds = static_cast<DateSelection *>(data);
923
924         date_selection_popup_hide(ds);
925 }
926
927 static void date_selection_popup(DateSelection *ds)
928 {
929         gint x, y;
930         gint wx, wy;
931         gint day, month, year;
932         GtkAllocation button_allocation;
933         GtkAllocation window_allocation;
934
935         if (ds->window) return;
936
937         ds->window = gtk_window_new(GTK_WINDOW_POPUP);
938         gtk_window_set_resizable(GTK_WINDOW(ds->window), FALSE);
939         g_signal_connect(G_OBJECT(ds->window), "button_press_event",
940                          G_CALLBACK(date_selection_popup_press_cb), ds);
941         g_signal_connect(G_OBJECT(ds->window), "key_press_event",
942                          G_CALLBACK(date_selection_popup_keypress_cb), ds);
943
944         ds->calendar = gtk_calendar_new();
945         gtk_container_add(GTK_CONTAINER(ds->window), ds->calendar);
946         gtk_widget_show(ds->calendar);
947
948         date_selection_get(ds->box, &day, &month, &year);
949         gtk_calendar_select_month(GTK_CALENDAR(ds->calendar), month - 1, year);
950         gtk_calendar_select_day(GTK_CALENDAR(ds->calendar), day);
951
952         g_signal_connect(G_OBJECT(ds->calendar), "day_selected",
953                          G_CALLBACK(date_selection_day_cb), ds);
954         g_signal_connect(G_OBJECT(ds->calendar), "day_selected_double_click",
955                         G_CALLBACK(date_selection_doubleclick_cb), ds);
956
957         gtk_widget_realize(ds->window);
958
959         gdk_window_get_origin(gtk_widget_get_window(ds->button), &wx, &wy);
960
961         gtk_widget_get_allocation(ds->button, &button_allocation);
962         gtk_widget_get_allocation(ds->window, &window_allocation);
963
964         x = wx + button_allocation.x + button_allocation.width - window_allocation.width;
965         y = wy + button_allocation.y + button_allocation.height;
966
967         if (y + window_allocation.height > gdk_screen_height())
968                 {
969                 y = wy + button_allocation.y - window_allocation.height;
970                 }
971         if (x < 0) x = 0;
972         if (y < 0) y = 0;
973
974         gtk_window_move(GTK_WINDOW(ds->window), x, y);
975         gtk_widget_show(ds->window);
976
977         gtk_widget_grab_focus(ds->calendar);
978         gdk_pointer_grab(gtk_widget_get_window(ds->window), TRUE,
979                          static_cast<GdkEventMask>(GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_MOTION_MASK),
980                          NULL, NULL, GDK_CURRENT_TIME);
981         gdk_keyboard_grab(gtk_widget_get_window(ds->window), TRUE, GDK_CURRENT_TIME);
982         gtk_grab_add(ds->window);
983
984         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ds->button), TRUE);
985 }
986
987 static void date_selection_button_cb(GtkWidget *UNUSED(widget), gpointer data)
988 {
989         DateSelection *ds = static_cast<DateSelection *>(data);
990
991         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(ds->button)) == (!ds->window))
992                 {
993                 date_selection_popup(ds);
994                 }
995 }
996
997 static void button_size_allocate_cb(GtkWidget *button, GtkAllocation *allocation, gpointer data)
998 {
999         GtkWidget *spin = static_cast<GtkWidget *>(data);
1000         GtkRequisition spin_requisition;
1001         gtk_widget_get_requisition(spin, &spin_requisition);
1002
1003         if (allocation->height > spin_requisition.height)
1004                 {
1005                 GtkAllocation button_allocation;
1006                 GtkAllocation spin_allocation;
1007
1008                 gtk_widget_get_allocation(button, &button_allocation);
1009                 gtk_widget_get_allocation(spin, &spin_allocation);
1010                 button_allocation.height = spin_requisition.height;
1011                 button_allocation.y = spin_allocation.y +
1012                         (spin_allocation.height - spin_requisition.height) / 2;
1013                 gtk_widget_size_allocate(button, &button_allocation);
1014                 }
1015 }
1016
1017 static void spin_increase(GtkWidget *spin, gint value)
1018 {
1019         GtkRequisition req;
1020
1021         gtk_widget_size_request(spin, &req);
1022         gtk_widget_set_size_request(spin, req.width + value, -1);
1023 }
1024
1025 static void date_selection_destroy_cb(GtkWidget *UNUSED(widget), gpointer data)
1026 {
1027         DateSelection *ds = static_cast<DateSelection *>(data);
1028
1029         date_selection_popup_hide(ds);
1030
1031         g_free(ds);
1032 }
1033
1034 GtkWidget *date_selection_new(void)
1035 {
1036         DateSelection *ds;
1037         GtkWidget *arrow;
1038
1039         ds = g_new0(DateSelection, 1);
1040         gchar *date_format;
1041         gint i;
1042
1043         ds->box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
1044         g_signal_connect(G_OBJECT(ds->box), "destroy",
1045                          G_CALLBACK(date_selection_destroy_cb), ds);
1046
1047         date_format = nl_langinfo(D_FMT);
1048
1049         if (strlen(date_format) == 8)
1050                 {
1051                 for (i=1; i<8; i=i+3)
1052                         {
1053                         switch (date_format[i])
1054                                 {
1055                                 case 'd':
1056                                         ds->spin_d = pref_spin_new(ds->box, NULL, NULL, 1, 31, 1, 0, 1, NULL, NULL);
1057                                         break;
1058                                 case 'm':
1059                                         ds->spin_m = pref_spin_new(ds->box, NULL, NULL, 1, 12, 1, 0, 1, NULL, NULL);
1060                                         break;
1061                                 case 'y': case 'Y':
1062                                         ds->spin_y = pref_spin_new(ds->box, NULL, NULL, 1900, 9999, 1, 0, 1900, NULL, NULL);
1063                                         break;
1064                                 default:
1065                                         log_printf("Warning: Date locale %s is unknown", date_format);
1066                                         break;
1067                                 }
1068                         }
1069                 }
1070         else
1071                 {
1072                 ds->spin_m = pref_spin_new(ds->box, NULL, NULL, 1, 12, 1, 0, 1, NULL, NULL);
1073                 ds->spin_d = pref_spin_new(ds->box, NULL, NULL, 1, 31, 1, 0, 1, NULL, NULL);
1074                 ds->spin_y = pref_spin_new(ds->box, NULL, NULL, 1900, 9999, 1, 0, 1900, NULL, NULL);
1075                 }
1076
1077         spin_increase(ds->spin_y, 5);
1078
1079         ds->button = gtk_toggle_button_new();
1080         g_signal_connect(G_OBJECT(ds->button), "size_allocate",
1081                          G_CALLBACK(button_size_allocate_cb), ds->spin_y);
1082
1083         arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_NONE);
1084         gtk_container_add(GTK_CONTAINER(ds->button), arrow);
1085         gtk_widget_show(arrow);
1086
1087         gtk_box_pack_start(GTK_BOX(ds->box), ds->button, FALSE, FALSE, 0);
1088         g_signal_connect(G_OBJECT(ds->button), "clicked",
1089                          G_CALLBACK(date_selection_button_cb), ds);
1090         gtk_widget_show(ds->button);
1091
1092         g_object_set_data(G_OBJECT(ds->box), DATE_SELECION_KEY, ds);
1093
1094         return ds->box;
1095 }
1096
1097 void date_selection_set(GtkWidget *widget, gint day, gint month, gint year)
1098 {
1099         DateSelection *ds;
1100
1101         ds = static_cast<DateSelection *>(g_object_get_data(G_OBJECT(widget), DATE_SELECION_KEY));
1102         if (!ds) return;
1103
1104         gtk_spin_button_set_value(GTK_SPIN_BUTTON(ds->spin_d), (gdouble)day);
1105         gtk_spin_button_set_value(GTK_SPIN_BUTTON(ds->spin_m), (gdouble)month);
1106         gtk_spin_button_set_value(GTK_SPIN_BUTTON(ds->spin_y), (gdouble)year);
1107 }
1108
1109
1110 void date_selection_get(GtkWidget *widget, gint *day, gint *month, gint *year)
1111 {
1112         DateSelection *ds;
1113
1114         ds = static_cast<DateSelection *>(g_object_get_data(G_OBJECT(widget), DATE_SELECION_KEY));
1115         if (!ds) return;
1116
1117         if (day) *day = gtk_spin_button_get_value(GTK_SPIN_BUTTON(ds->spin_d));
1118         if (month) *month = gtk_spin_button_get_value(GTK_SPIN_BUTTON(ds->spin_m));
1119         if (year) *year = gtk_spin_button_get_value(GTK_SPIN_BUTTON(ds->spin_y));
1120 }
1121
1122 void date_selection_time_set(GtkWidget *widget, time_t t)
1123 {
1124         struct tm *lt;
1125
1126         lt = localtime(&t);
1127         if (!lt) return;
1128
1129         date_selection_set(widget, lt->tm_mday, lt->tm_mon + 1, lt->tm_year + 1900);
1130 }
1131
1132 //time_t date_selection_time_get(GtkWidget *widget)
1133 //{
1134         //struct tm lt;
1135         //gint day = 0;
1136         //gint month = 0;
1137         //gint year = 0;
1138
1139         //date_selection_get(widget, &day, &month ,&year);
1140
1141         //lt.tm_sec = 0;
1142         //lt.tm_min = 0;
1143         //lt.tm_hour = 0;
1144         //lt.tm_mday = day;
1145         //lt.tm_mon = month - 1;
1146         //lt.tm_year = year - 1900;
1147         //lt.tm_isdst = 0;
1148
1149         //return mktime(&lt);
1150 //}
1151
1152 /*
1153  *-----------------------------------------------------------------------------
1154  * storing data in a history list with key,data pairs
1155  *-----------------------------------------------------------------------------
1156  */
1157
1158 #define PREF_LIST_MARKER_INT "[INT]:"
1159 #define PREF_LIST_MARKER_DOUBLE "[DOUBLE]:"
1160 #define PREF_LIST_MARKER_STRING "[STRING]:"
1161
1162 static GList *pref_list_find(const gchar *group, const gchar *token)
1163 {
1164         GList *work;
1165         gint l;
1166
1167         l = strlen(token);
1168
1169         work = history_list_get_by_key(group);
1170         while (work)
1171                 {
1172                 const gchar *text = static_cast<const gchar *>(work->data);
1173
1174                 if (strncmp(text, token, l) == 0) return work;
1175
1176                 work = work->next;
1177                 }
1178
1179         return NULL;
1180 }
1181
1182 static gboolean pref_list_get(const gchar *group, const gchar *key, const gchar *marker, const gchar **result)
1183 {
1184         gchar *token;
1185         GList *work;
1186         gboolean ret;
1187
1188         if (!group || !key || !marker)
1189                 {
1190                 *result = NULL;
1191                 return FALSE;
1192                 }
1193
1194         token = g_strconcat(key, marker, NULL);
1195
1196         work = pref_list_find(group, token);
1197         if (work)
1198                 {
1199                 *result = (const gchar *)work->data + strlen(token);
1200                 if (strlen(*result) == 0) *result = NULL;
1201                 ret = TRUE;
1202                 }
1203         else
1204                 {
1205                 *result = NULL;
1206                 ret = FALSE;
1207                 }
1208
1209         g_free(token);
1210
1211         return ret;
1212 }
1213
1214 static void pref_list_set(const gchar *group, const gchar *key, const gchar *marker, const gchar *text)
1215 {
1216         gchar *token;
1217         gchar *path;
1218         GList *work;
1219
1220         if (!group || !key || !marker) return;
1221
1222         token = g_strconcat(key, marker, NULL);
1223         path = g_strconcat(token, text, NULL);
1224
1225         work = pref_list_find(group, token);
1226         if (work)
1227                 {
1228                 gchar *old_path = static_cast<gchar *>(work->data);
1229
1230                 if (text)
1231                         {
1232                         work->data = path;
1233                         path = NULL;
1234
1235                         g_free(old_path);
1236                         }
1237                 else
1238                         {
1239                         history_list_item_remove(group, old_path);
1240                         }
1241                 }
1242         else if (text)
1243                 {
1244                 history_list_add_to_key(group, path, 0);
1245                 }
1246
1247         g_free(path);
1248         g_free(token);
1249 }
1250
1251 void pref_list_int_set(const gchar *group, const gchar *key, gint value)
1252 {
1253         gchar *text;
1254
1255         text = g_strdup_printf("%d", value);
1256         pref_list_set(group, key, PREF_LIST_MARKER_INT, text);
1257         g_free(text);
1258 }
1259
1260 gboolean pref_list_int_get(const gchar *group, const gchar *key, gint *result)
1261 {
1262         const gchar *text;
1263
1264         if (!group || !key)
1265                 {
1266                 *result = 0;
1267                 return FALSE;
1268                 }
1269
1270         if (pref_list_get(group, key, PREF_LIST_MARKER_INT, &text) && text)
1271                 {
1272                 *result = (gint)strtol(text, NULL, 10);
1273                 return TRUE;
1274                 }
1275
1276         *result = 0;
1277         return FALSE;
1278 }
1279
1280 //void pref_list_double_set(const gchar *group, const gchar *key, gdouble value)
1281 //{
1282         //gchar text[G_ASCII_DTOSTR_BUF_SIZE];
1283
1284         //g_ascii_dtostr(text, sizeof(text), value);
1285         //pref_list_set(group, key, PREF_LIST_MARKER_DOUBLE, text);
1286 //}
1287
1288 //gboolean pref_list_double_get(const gchar *group, const gchar *key, gdouble *result)
1289 //{
1290         //const gchar *text;
1291
1292         //if (!group || !key)
1293                 //{
1294                 //*result = 0;
1295                 //return FALSE;
1296                 //}
1297
1298         //if (pref_list_get(group, key, PREF_LIST_MARKER_DOUBLE, &text) && text)
1299                 //{
1300                 //*result = g_ascii_strtod(text, NULL);
1301                 //return TRUE;
1302                 //}
1303
1304         //*result = 0;
1305         //return FALSE;
1306 //}
1307
1308 //void pref_list_string_set(const gchar *group, const gchar *key, const gchar *value)
1309 //{
1310         //pref_list_set(group, key, PREF_LIST_MARKER_STRING, value);
1311 //}
1312
1313 //gboolean pref_list_string_get(const gchar *group, const gchar *key, const gchar **result)
1314 //{
1315         //return pref_list_get(group, key, PREF_LIST_MARKER_STRING, result);
1316 //}
1317
1318
1319 void pref_color_button_set_cb(GtkWidget *widget, gpointer data)
1320 {
1321         GdkColor *color = static_cast<GdkColor *>(data);
1322
1323         gtk_color_button_get_color(GTK_COLOR_BUTTON(widget), color);
1324 }
1325
1326 GtkWidget *pref_color_button_new(GtkWidget *parent_box,
1327                                  const gchar *title, const GdkColor *color,
1328                                  GCallback func, gpointer data)
1329 {
1330         GtkWidget *button;
1331
1332         if (color)
1333                 {
1334                 button = gtk_color_button_new_with_color(color);
1335                 }
1336         else
1337                 {
1338                 button = gtk_color_button_new();
1339                 }
1340
1341         if (func) g_signal_connect(G_OBJECT(button), "color-set", func, data);
1342
1343         if (title)
1344                 {
1345                 GtkWidget *label;
1346                 GtkWidget *hbox;
1347
1348                 gtk_color_button_set_title(GTK_COLOR_BUTTON(button), title);
1349                 label = gtk_label_new(title);
1350
1351                 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
1352                 gtk_box_pack_start(GTK_BOX(parent_box), hbox, TRUE, TRUE, 0);
1353
1354                 gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 0);
1355                 gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
1356
1357                 gtk_widget_show_all(hbox);
1358                 }
1359         else
1360                 {
1361                 gtk_widget_show(button);
1362                 }
1363
1364         return button;
1365 }
1366
1367 /*
1368  *-----------------------------------------------------------------------------
1369  * text widget
1370  *-----------------------------------------------------------------------------
1371  */
1372
1373 gchar *text_widget_text_pull(GtkWidget *text_widget)
1374 {
1375         if (GTK_IS_TEXT_VIEW(text_widget))
1376                 {
1377                 GtkTextBuffer *buffer;
1378                 GtkTextIter start, end;
1379
1380                 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_widget));
1381                 gtk_text_buffer_get_bounds(buffer, &start, &end);
1382
1383                 return gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
1384                 }
1385         else if (GTK_IS_ENTRY(text_widget))
1386                 {
1387                 return g_strdup(gtk_entry_get_text(GTK_ENTRY(text_widget)));
1388                 }
1389         else
1390                 {
1391                 return NULL;
1392                 }
1393
1394 }
1395
1396 gchar *text_widget_text_pull_selected(GtkWidget *text_widget)
1397 {
1398         if (GTK_IS_TEXT_VIEW(text_widget))
1399                 {
1400                 GtkTextBuffer *buffer;
1401                 GtkTextIter start, end;
1402
1403                 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_widget));
1404                 gtk_text_buffer_get_bounds(buffer, &start, &end);
1405
1406                 if (gtk_text_buffer_get_selection_bounds(buffer, &start, &end))
1407                         {
1408                         gtk_text_iter_set_line_offset(&start, 0);
1409                         gtk_text_iter_forward_to_line_end(&end);
1410                         }
1411
1412                 return gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
1413                 }
1414         else if (GTK_IS_ENTRY(text_widget))
1415                 {
1416                 return g_strdup(gtk_entry_get_text(GTK_ENTRY(text_widget)));
1417                 }
1418         else
1419                 {
1420                 return NULL;
1421                 }
1422 }
1423
1424 gboolean defined_mouse_buttons(GtkWidget *UNUSED(widget), GdkEventButton *event, gpointer data)
1425 {
1426         LayoutWindow *lw = static_cast<LayoutWindow *>(data);
1427         GtkAction *action;
1428         gboolean ret = FALSE;
1429
1430         switch (event->button)
1431                 {
1432                 case MOUSE_BUTTON_8:
1433                         if (options->mouse_button_8)
1434                                 {
1435                                 action = gtk_action_group_get_action(lw->action_group, options->mouse_button_8);
1436                                 if (action)
1437                                         {
1438                                         gtk_action_activate(action);
1439                                         }
1440                                 ret = TRUE;
1441                                 }
1442                                 break;
1443                 case MOUSE_BUTTON_9:
1444                         if (options->mouse_button_9)
1445                                 {
1446                                 action = gtk_action_group_get_action(lw->action_group, options->mouse_button_9);
1447                                 if (action)
1448                                         {
1449                                         gtk_action_activate(action);
1450                                         }
1451                                 ret = TRUE;
1452                                 }
1453                         break;
1454                 default:
1455                         break;
1456                 }
1457
1458         return ret;
1459 }
1460
1461 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */