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