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