2 * Copyright (C) 2004 John Ellis
3 * Copyright (C) 2008 - 2016 The Geeqie Team
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.
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.
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.
27 #include "history-list.h"
36 *-----------------------------------------------------------------------------
37 * widget and layout utilities
38 *-----------------------------------------------------------------------------
41 GtkWidget *pref_box_new(GtkWidget *parent_box, gboolean fill,
42 GtkOrientation orientation, gboolean padding)
46 if (orientation == GTK_ORIENTATION_HORIZONTAL)
48 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, padding);
52 box = gtk_box_new(GTK_ORIENTATION_VERTICAL, padding);
55 gq_gtk_box_pack_start(GTK_BOX(parent_box), box, fill, fill, 0);
61 GtkWidget *pref_group_new(GtkWidget *parent_box, gboolean fill,
62 const gchar *text, GtkOrientation orientation)
69 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
71 /* add additional spacing if necessary */
72 if (GTK_IS_VBOX(parent_box))
74 GList *list = gtk_container_get_children(GTK_CONTAINER(parent_box));
77 pref_spacer(vbox, PREF_PAD_GROUP - PREF_PAD_GAP);
82 gq_gtk_box_pack_start(GTK_BOX(parent_box), vbox, fill, fill, 0);
83 gtk_widget_show(vbox);
85 label = gtk_label_new(text);
86 gtk_label_set_xalign(GTK_LABEL(label), 0.0);
87 gtk_label_set_yalign(GTK_LABEL(label), 0.5);
88 pref_label_bold(label, TRUE, FALSE);
90 gq_gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
91 gtk_widget_show(label);
93 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_INDENT);
94 gq_gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 0);
95 gtk_widget_show(hbox);
97 /* indent using empty box */
100 if (orientation == GTK_ORIENTATION_HORIZONTAL)
102 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_SPACE);
106 box = gtk_box_new(GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
108 gq_gtk_box_pack_start(GTK_BOX(hbox), box, TRUE, TRUE, 0);
109 gtk_widget_show(box);
111 g_object_set_data(G_OBJECT(box), "pref_group", vbox);
116 GtkWidget *pref_group_parent(GtkWidget *child)
125 group = static_cast<GtkWidget *>(g_object_get_data(G_OBJECT(parent), "pref_group"));
126 if (group && GTK_IS_WIDGET(group)) return group;
128 parent = gtk_widget_get_parent(parent);
134 GtkWidget *pref_frame_new(GtkWidget *parent_box, gboolean fill,
136 GtkOrientation orientation, gboolean padding)
139 GtkWidget *frame = nullptr;
141 frame = gtk_frame_new(text);
142 gq_gtk_box_pack_start(GTK_BOX(parent_box), frame, fill, fill, 0);
143 gtk_widget_show(frame);
145 if (orientation == GTK_ORIENTATION_HORIZONTAL)
147 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, padding);
151 box = gtk_box_new(GTK_ORIENTATION_VERTICAL, padding);
153 gq_gtk_container_add(GTK_WIDGET(frame), box);
154 gtk_container_set_border_width(GTK_CONTAINER(box), PREF_PAD_BORDER);
155 gtk_widget_show(box);
160 GtkWidget *pref_spacer(GtkWidget *parent_box, gboolean padding)
164 spacer = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
165 gq_gtk_box_pack_start(GTK_BOX(parent_box), spacer, FALSE, FALSE, padding / 2);
166 gtk_widget_show(spacer);
171 GtkWidget *pref_line(GtkWidget *parent_box, gboolean padding)
175 spacer = gtk_separator_new(GTK_IS_HBOX(parent_box) ? GTK_ORIENTATION_VERTICAL : GTK_ORIENTATION_HORIZONTAL);
176 gq_gtk_box_pack_start(GTK_BOX(parent_box), spacer, FALSE, FALSE, padding / 2);
177 gtk_widget_show(spacer);
182 GtkWidget *pref_label_new(GtkWidget *parent_box, const gchar *text)
186 label = gtk_label_new(text);
187 gq_gtk_box_pack_start(GTK_BOX(parent_box), label, FALSE, FALSE, 0);
188 gtk_widget_show(label);
193 GtkWidget *pref_label_new_mnemonic(GtkWidget *parent_box, const gchar *text, GtkWidget *widget)
197 label = gtk_label_new_with_mnemonic(text);
198 gtk_label_set_mnemonic_widget(GTK_LABEL(label), widget);
199 gq_gtk_box_pack_start(GTK_BOX(parent_box), label, FALSE, FALSE, 0);
200 gtk_widget_show(label);
205 void pref_label_bold(GtkWidget *label, gboolean bold, gboolean increase_size)
210 if (!bold && !increase_size) return;
212 pal = pango_attr_list_new();
216 pa = pango_attr_weight_new(PANGO_WEIGHT_BOLD);
218 pa->end_index = G_MAXINT;
219 pango_attr_list_insert(pal, pa);
224 pa = pango_attr_scale_new(PANGO_SCALE_LARGE);
226 pa->end_index = G_MAXINT;
227 pango_attr_list_insert(pal, pa);
230 gtk_label_set_attributes(GTK_LABEL(label), pal);
231 pango_attr_list_unref(pal);
234 GtkWidget *pref_button_new(GtkWidget *parent_box, const gchar *icon_name,
235 const gchar *text, GCallback func, gpointer data)
241 button = gtk_button_new_from_icon_name(icon_name, GTK_ICON_SIZE_BUTTON);
245 button = gtk_button_new();
250 gtk_button_set_use_underline(GTK_BUTTON(button), TRUE);
251 gtk_button_set_label(GTK_BUTTON(button), text);
254 if (func) g_signal_connect(G_OBJECT(button), "clicked", func, data);
258 gq_gtk_box_pack_start(GTK_BOX(parent_box), button, FALSE, FALSE, 0);
259 gtk_widget_show(button);
265 static GtkWidget *real_pref_checkbox_new(GtkWidget *parent_box, const gchar *text, gboolean mnemonic_text,
266 gboolean active, GCallback func, gpointer data)
272 button = gtk_check_button_new_with_mnemonic(text);
276 button = gtk_check_button_new_with_label(text);
278 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), active);
279 if (func) g_signal_connect(G_OBJECT(button), "clicked", func, data);
281 gq_gtk_box_pack_start(GTK_BOX(parent_box), button, FALSE, FALSE, 0);
282 gtk_widget_show(button);
287 GtkWidget *pref_checkbox_new(GtkWidget *parent_box, const gchar *text, gboolean active,
288 GCallback func, gpointer data)
290 return real_pref_checkbox_new(parent_box, text, FALSE, active, func, data);
293 #pragma GCC diagnostic push
294 #pragma GCC diagnostic ignored "-Wunused-function"
295 GtkWidget *pref_checkbox_new_mnemonic_unused(GtkWidget *parent_box, const gchar *text, gboolean active,
296 GCallback func, gpointer data)
298 return real_pref_checkbox_new(parent_box, text, TRUE, active, func, data);
300 #pragma GCC diagnostic pop
302 static void pref_checkbox_int_cb(GtkWidget *widget, gpointer data)
304 auto result = static_cast<gboolean *>(data);
306 *result = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
309 GtkWidget *pref_checkbox_new_int(GtkWidget *parent_box, const gchar *text, gboolean active,
314 button = pref_checkbox_new(parent_box, text, active,
315 G_CALLBACK(pref_checkbox_int_cb), result);
321 static void pref_checkbox_link_sensitivity_cb(GtkWidget *button, gpointer data)
323 auto widget = static_cast<GtkWidget *>(data);
325 gtk_widget_set_sensitive(widget, gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button)));
328 void pref_checkbox_link_sensitivity(GtkWidget *button, GtkWidget *widget)
330 g_signal_connect(G_OBJECT(button), "toggled",
331 G_CALLBACK(pref_checkbox_link_sensitivity_cb), widget);
333 pref_checkbox_link_sensitivity_cb(button, widget);
336 #pragma GCC diagnostic push
337 #pragma GCC diagnostic ignored "-Wunused-function"
338 static void pref_checkbox_link_sensitivity_swap_cb_unused(GtkWidget *button, gpointer data)
340 auto *widget = static_cast<GtkWidget *>(data);
342 gtk_widget_set_sensitive(widget, !gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button)));
345 void pref_checkbox_link_sensitivity_swap_unused(GtkWidget *button, GtkWidget *widget)
347 g_signal_connect(G_OBJECT(button), "toggled",
348 G_CALLBACK(pref_checkbox_link_sensitivity_swap_cb_unused), widget);
350 pref_checkbox_link_sensitivity_swap_cb_unused(button, widget);
352 #pragma GCC diagnostic pop
354 static GtkWidget *real_pref_radiobutton_new(GtkWidget *parent_box, GtkWidget *sibling,
355 const gchar *text, gboolean mnemonic_text, gboolean active,
356 GCallback func, gpointer data)
360 GtkToggleButton *group;;
370 group = gtk_radio_button_get_group(GTK_RADIO_BUTTON(sibling));
381 button = gtk_toggle_button_new_with_mnemonic(text);
382 gtk_toggle_button_set_group(button, group);
384 button = gtk_radio_button_new_with_mnemonic(group, text);
390 button = gtk_toggle_button_new_with_label(text);
391 gtk_toggle_button_set_group(button, group);
393 button = gtk_radio_button_new_with_label(group, text);
397 if (active) gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), active);
398 if (func) g_signal_connect(G_OBJECT(button), "clicked", func, data);
400 gq_gtk_box_pack_start(GTK_BOX(parent_box), button, FALSE, FALSE, 0);
401 gtk_widget_show(button);
406 GtkWidget *pref_radiobutton_new(GtkWidget *parent_box, GtkWidget *sibling,
407 const gchar *text, gboolean active,
408 GCallback func, gpointer data)
410 return real_pref_radiobutton_new(parent_box, sibling, text, FALSE, active, func, data);
413 #pragma GCC diagnostic push
414 #pragma GCC diagnostic ignored "-Wunused-function"
415 GtkWidget *pref_radiobutton_new_mnemonic_unused(GtkWidget *parent_box, GtkWidget *sibling,
416 const gchar *text, gboolean active,
417 GCallback func, gpointer data)
419 return real_pref_radiobutton_new(parent_box, sibling, text, TRUE, active, func, data);
422 #define PREF_RADIO_VALUE_KEY "pref_radio_value"
424 static void pref_radiobutton_int_cb_unused(GtkWidget *widget, gpointer data)
426 auto *result = static_cast<gboolean *>(data);
428 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
430 *result = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), PREF_RADIO_VALUE_KEY));
434 GtkWidget *pref_radiobutton_new_int_unused(GtkWidget *parent_box, GtkWidget *sibling,
435 const gchar *text, gboolean active,
436 gboolean *result, gboolean value,
441 button = pref_radiobutton_new(parent_box, sibling, text, active,
442 G_CALLBACK(pref_radiobutton_int_cb_unused), result);
443 g_object_set_data(G_OBJECT(button), PREF_RADIO_VALUE_KEY, GINT_TO_POINTER(value));
444 if (active) *result = value;
448 #pragma GCC diagnostic pop
450 static GtkWidget *real_pref_spin_new(GtkWidget *parent_box, const gchar *text, const gchar *suffix,
451 gboolean mnemonic_text,
452 gdouble min, gdouble max, gdouble step, gint digits,
454 GCallback func, gpointer data)
460 box = pref_box_new(parent_box, FALSE, GTK_ORIENTATION_HORIZONTAL, PREF_PAD_SPACE);
462 spin = gtk_spin_button_new_with_range(min, max, step);
463 gtk_spin_button_set_digits(GTK_SPIN_BUTTON(spin), digits);
464 gtk_spin_button_set_value(GTK_SPIN_BUTTON(spin), value);
468 g_signal_connect(G_OBJECT(spin), "value_changed", G_CALLBACK(func), data);
475 label = pref_label_new_mnemonic(box, text, spin);
479 label = pref_label_new(box, text);
481 pref_link_sensitivity(label, spin);
484 gq_gtk_box_pack_start(GTK_BOX(box), spin, FALSE, FALSE, 0);
485 gtk_widget_show(spin);
487 /* perhaps this should only be PREF_PAD_GAP distance from spinbutton ? */
490 label = pref_label_new(box, suffix);
491 pref_link_sensitivity(label, spin);
497 GtkWidget *pref_spin_new(GtkWidget *parent_box, const gchar *text, const gchar *suffix,
498 gdouble min, gdouble max, gdouble step, gint digits,
500 GCallback func, gpointer data)
502 return real_pref_spin_new(parent_box, text, suffix, FALSE,
503 min, max, step, digits, value, func, data);
506 #pragma GCC diagnostic push
507 #pragma GCC diagnostic ignored "-Wunused-function"
508 GtkWidget *pref_spin_new_mnemonic_unused(GtkWidget *parent_box, const gchar *text, const gchar *suffix,
509 gdouble min, gdouble max, gdouble step, gint digits,
511 GCallback func, gpointer data)
513 return real_pref_spin_new(parent_box, text, suffix, TRUE,
514 min, max, step, digits, value, func, data);
516 #pragma GCC diagnostic pop
518 static void pref_spin_int_cb(GtkWidget *widget, gpointer data)
520 auto var = static_cast<gint *>(data);
521 *var = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(widget));
524 GtkWidget *pref_spin_new_int(GtkWidget *parent_box, const gchar *text, const gchar *suffix,
525 gint min, gint max, gint step,
526 gint value, gint *value_var)
529 return pref_spin_new(parent_box, text, suffix,
530 static_cast<gdouble>(min), static_cast<gdouble>(max), static_cast<gdouble>(step), 0,
532 G_CALLBACK(pref_spin_int_cb), value_var);
535 static void pref_link_sensitivity_cb(GtkWidget *watch, GtkStateType, gpointer data)
537 auto widget = static_cast<GtkWidget *>(data);
539 gtk_widget_set_sensitive(widget, gtk_widget_is_sensitive(watch));
542 void pref_link_sensitivity(GtkWidget *widget, GtkWidget *watch)
544 g_signal_connect(G_OBJECT(watch), "state_changed",
545 G_CALLBACK(pref_link_sensitivity_cb), widget);
548 void pref_signal_block_data(GtkWidget *widget, gpointer data)
550 g_signal_handlers_block_matched(widget, G_SIGNAL_MATCH_DATA,
551 0, 0, nullptr, nullptr, data);
554 void pref_signal_unblock_data(GtkWidget *widget, gpointer data)
556 g_signal_handlers_unblock_matched(widget, G_SIGNAL_MATCH_DATA,
557 0, 0, nullptr, nullptr, data);
560 GtkWidget *pref_table_new(GtkWidget *parent_box, gint, gint, gboolean, gboolean fill)
564 table = gtk_grid_new();
565 gtk_grid_set_row_spacing(GTK_GRID(table), PREF_PAD_GAP);
566 gtk_grid_set_column_spacing(GTK_GRID(table), PREF_PAD_SPACE);
570 gq_gtk_box_pack_start(GTK_BOX(parent_box), table, fill, fill, 0);
571 gtk_widget_show(table);
577 GtkWidget *pref_table_box(GtkWidget *table, gint column, gint row,
578 GtkOrientation orientation, const gchar *text)
585 shell = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
586 box = pref_group_new(shell, TRUE, text, orientation);
590 if (orientation == GTK_ORIENTATION_HORIZONTAL)
592 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_SPACE);
596 box = gtk_box_new(GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
601 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);
603 gtk_widget_show(shell);
608 GtkWidget *pref_table_label(GtkWidget *table, gint column, gint row,
609 const gchar *text, GtkAlign alignment)
613 label = gtk_label_new(text);
614 gtk_widget_set_halign(label, alignment);
615 gtk_widget_set_valign(label, GTK_ALIGN_CENTER);
616 gq_gtk_grid_attach(GTK_GRID(table), label, column, column + 1, row, row + 1, GTK_FILL, static_cast<GtkAttachOptions>(0), 0, 0);
617 gtk_widget_show(label);
622 GtkWidget *pref_table_button(GtkWidget *table, gint column, gint row,
623 const gchar *stock_id, const gchar *text,
624 GCallback func, gpointer data)
628 button = pref_button_new(nullptr, stock_id, text, func, data);
629 gq_gtk_grid_attach(GTK_GRID(table), button, column, column + 1, row, row + 1, GTK_FILL, static_cast<GtkAttachOptions>(0), 0, 0);
630 gtk_widget_show(button);
635 GtkWidget *pref_table_spin(GtkWidget *table, gint column, gint row,
636 const gchar *text, const gchar *suffix,
637 gdouble min, gdouble max, gdouble step, gint digits,
639 GCallback func, gpointer data)
645 spin = gtk_spin_button_new_with_range(min, max, step);
646 gtk_spin_button_set_digits(GTK_SPIN_BUTTON(spin), digits);
647 gtk_spin_button_set_value(GTK_SPIN_BUTTON(spin), value);
650 g_signal_connect(G_OBJECT(spin), "value_changed", G_CALLBACK(func), data);
655 label = pref_table_label(table, column, row, text, GTK_ALIGN_END);
656 pref_link_sensitivity(label, spin);
662 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_SPACE);
663 gq_gtk_box_pack_start(GTK_BOX(box), spin, FALSE, FALSE, 0);
664 gtk_widget_show(spin);
666 label = pref_label_new(box, suffix);
667 pref_link_sensitivity(label, spin);
674 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);
675 gtk_widget_show(box);
680 GtkWidget *pref_table_spin_new_int(GtkWidget *table, gint column, gint row,
681 const gchar *text, const gchar *suffix,
682 gint min, gint max, gint step,
683 gint value, gint *value_var)
686 return pref_table_spin(table, column, row,
688 static_cast<gdouble>(min), static_cast<gdouble>(max), static_cast<gdouble>(step), 0,
690 G_CALLBACK(pref_spin_int_cb), value_var);
694 GtkWidget *pref_toolbar_new(GtkWidget *parent_box)
698 tbar = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
702 gq_gtk_box_pack_start(GTK_BOX(parent_box), tbar, FALSE, FALSE, 0);
703 gtk_widget_show(tbar);
708 GtkWidget *pref_toolbar_button(GtkWidget *toolbar,
709 const gchar *icon_name, const gchar *label, gboolean toggle,
710 const gchar *description,
711 GCallback func, gpointer data)
715 if (toggle) // TODO: TG seems no function uses toggle now
717 item = GTK_WIDGET(gtk_toggle_tool_button_new());
718 if (icon_name) gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(item), icon_name);
719 if (label) gtk_tool_button_set_label(GTK_TOOL_BUTTON(item), label);
723 GtkWidget *icon = nullptr;
726 icon = gtk_image_new_from_icon_name(icon_name, GTK_ICON_SIZE_LARGE_TOOLBAR); // TODO: TG which size?
727 gtk_widget_show(icon);
729 item = GTK_WIDGET(gtk_tool_button_new(icon, label));
731 gtk_tool_button_set_use_underline(GTK_TOOL_BUTTON(item), TRUE);
733 if (func) g_signal_connect(item, "clicked", func, data);
734 gq_gtk_container_add(GTK_WIDGET(toolbar), item);
735 gtk_widget_show(item);
739 gtk_widget_set_tooltip_text(item, description);
745 #pragma GCC diagnostic push
746 #pragma GCC diagnostic ignored "-Wunused-function"
747 void pref_toolbar_button_set_icon_unused(GtkWidget *button, GtkWidget *widget, const gchar *stock_id)
751 gtk_tool_button_set_icon_widget(GTK_TOOL_BUTTON(button), widget);
755 gtk_tool_button_set_stock_id(GTK_TOOL_BUTTON(button), stock_id);
759 GtkWidget *pref_toolbar_spacer_unused(GtkWidget *toolbar)
763 item = GTK_WIDGET(gtk_separator_tool_item_new());
764 gq_gtk_container_add(GTK_WIDGET(toolbar), item);
765 gtk_widget_show(item);
769 #pragma GCC diagnostic pop
773 *-----------------------------------------------------------------------------
774 * date selection entry
775 *-----------------------------------------------------------------------------
778 #define DATE_SELECION_KEY "date_selection_data"
796 static void date_selection_popup_hide(DateSelection *ds)
798 if (!ds->window) return;
800 if (gtk_widget_has_grab(ds->window))
802 gtk_grab_remove(ds->window);
803 gdk_keyboard_ungrab(GDK_CURRENT_TIME);
804 gdk_pointer_ungrab(GDK_CURRENT_TIME);
807 gtk_widget_hide(ds->window);
809 g_object_unref(ds->window);
810 ds->window = nullptr;
811 ds->calendar = nullptr;
813 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ds->button), FALSE);
816 static gboolean date_selection_popup_release_cb(GtkWidget *, GdkEventButton *, gpointer data)
818 auto ds = static_cast<DateSelection *>(data);
820 date_selection_popup_hide(ds);
824 static gboolean date_selection_popup_press_cb(GtkWidget *, GdkEventButton *event, gpointer data)
826 auto ds = static_cast<DateSelection *>(data);
832 xr = static_cast<gint>(event->x_root);
833 yr = static_cast<gint>(event->y_root);
835 window = gtk_widget_get_window(ds->window);
836 gdk_window_get_origin(window, &x, &y);
837 w = gdk_window_get_width(window);
838 h = gdk_window_get_height(window);
840 if (xr < x || yr < y || xr > x + w || yr > y + h)
842 g_signal_connect(G_OBJECT(ds->window), "button_release_event",
843 G_CALLBACK(date_selection_popup_release_cb), ds);
850 static void date_selection_popup_sync(DateSelection *ds)
857 GDateTime *date_selected;
859 date_selected = gtk_calendar_get_date(GTK_CALENDAR(ds->calendar));
860 g_date_time_get_ymd(date_selected, static_cast<guint>(&year), static_cast<guint>(&month), static_cast<guint>(&day));
862 g_date_time_unref(date_selected);
864 gtk_calendar_get_date(GTK_CALENDAR(ds->calendar), &year, &month, &day);
865 /* month is range 0 to 11 */
868 date_selection_set(ds->box, day, month, year);
871 static gboolean date_selection_popup_keypress_cb(GtkWidget *, GdkEventKey *event, gpointer data)
873 auto ds = static_cast<DateSelection *>(data);
875 switch (event->keyval)
878 case GDK_KEY_KP_Enter:
880 case GDK_KEY_ISO_Left_Tab:
881 date_selection_popup_sync(ds);
882 date_selection_popup_hide(ds);
885 date_selection_popup_hide(ds);
894 static void date_selection_day_cb(GtkWidget *, gpointer data)
896 auto ds = static_cast<DateSelection *>(data);
898 date_selection_popup_sync(ds);
901 static void date_selection_doubleclick_cb(GtkWidget *, gpointer data)
903 auto ds = static_cast<DateSelection *>(data);
905 date_selection_popup_hide(ds);
908 static void date_selection_popup(DateSelection *ds)
913 GtkAllocation button_allocation;
914 GtkAllocation window_allocation;
916 if (ds->window) return;
918 ds->window = gtk_window_new(GTK_WINDOW_POPUP);
919 gtk_window_set_resizable(GTK_WINDOW(ds->window), FALSE);
920 g_signal_connect(G_OBJECT(ds->window), "button_press_event",
921 G_CALLBACK(date_selection_popup_press_cb), ds);
922 g_signal_connect(G_OBJECT(ds->window), "key_press_event",
923 G_CALLBACK(date_selection_popup_keypress_cb), ds);
925 ds->calendar = gtk_calendar_new();
926 gq_gtk_container_add(GTK_WIDGET(ds->window), ds->calendar);
927 gtk_widget_show(ds->calendar);
929 date = date_selection_get(ds->box);
931 gtk_calendar_select_day(GTK_CALENDAR(ds->calendar), date);
933 gtk_calendar_select_month(GTK_CALENDAR(ds->calendar), g_date_time_get_month(date), g_date_time_get_year(date));
934 gtk_calendar_select_day(GTK_CALENDAR(ds->calendar), g_date_time_get_day_of_month(date));
936 g_date_time_unref(date);
938 g_signal_connect(G_OBJECT(ds->calendar), "day_selected",
939 G_CALLBACK(date_selection_day_cb), ds);
940 g_signal_connect(G_OBJECT(ds->calendar), "day_selected_double_click",
941 G_CALLBACK(date_selection_doubleclick_cb), ds);
943 gtk_widget_realize(ds->window);
945 gdk_window_get_origin(gtk_widget_get_window(ds->button), &wx, &wy);
947 gtk_widget_get_allocation(ds->button, &button_allocation);
948 gtk_widget_get_allocation(ds->window, &window_allocation);
950 x = wx + button_allocation.x + button_allocation.width - window_allocation.width;
951 y = wy + button_allocation.y + button_allocation.height;
953 if (y + window_allocation.height > gdk_screen_height())
955 y = wy + button_allocation.y - window_allocation.height;
960 gq_gtk_window_move(GTK_WINDOW(ds->window), x, y);
961 gtk_widget_show(ds->window);
963 gtk_widget_grab_focus(ds->calendar);
964 gdk_pointer_grab(gtk_widget_get_window(ds->window), TRUE,
965 static_cast<GdkEventMask>(GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_MOTION_MASK),
966 nullptr, nullptr, GDK_CURRENT_TIME);
967 gdk_keyboard_grab(gtk_widget_get_window(ds->window), TRUE, GDK_CURRENT_TIME);
968 gtk_grab_add(ds->window);
970 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ds->button), TRUE);
973 static void date_selection_button_cb(GtkWidget *, gpointer data)
975 auto ds = static_cast<DateSelection *>(data);
977 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(ds->button)) == (!ds->window))
979 date_selection_popup(ds);
983 static void button_size_allocate_cb(GtkWidget *button, GtkAllocation *allocation, gpointer data)
985 auto spin = static_cast<GtkWidget *>(data);
986 GtkRequisition spin_requisition;
987 gtk_widget_get_requisition(spin, &spin_requisition);
989 if (allocation->height > spin_requisition.height)
991 GtkAllocation button_allocation;
992 GtkAllocation spin_allocation;
994 gtk_widget_get_allocation(button, &button_allocation);
995 gtk_widget_get_allocation(spin, &spin_allocation);
996 button_allocation.height = spin_requisition.height;
997 button_allocation.y = spin_allocation.y +
998 (spin_allocation.height - spin_requisition.height) / 2;
999 gtk_widget_size_allocate(button, &button_allocation);
1003 static void spin_increase(GtkWidget *spin, gint value)
1007 gtk_widget_size_request(spin, &req);
1008 gtk_widget_set_size_request(spin, req.width + value, -1);
1011 static void date_selection_destroy_cb(GtkWidget *, gpointer data)
1013 auto ds = static_cast<DateSelection *>(data);
1015 date_selection_popup_hide(ds);
1020 GtkWidget *date_selection_new()
1025 ds = g_new0(DateSelection, 1);
1029 ds->box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
1030 g_signal_connect(G_OBJECT(ds->box), "destroy",
1031 G_CALLBACK(date_selection_destroy_cb), ds);
1033 date_format = nl_langinfo(D_FMT);
1035 if (strlen(date_format) == 8)
1037 for (i=1; i<8; i=i+3)
1039 switch (date_format[i])
1042 ds->spin_d = pref_spin_new(ds->box, nullptr, nullptr, 1, 31, 1, 0, 1, nullptr, nullptr);
1045 ds->spin_m = pref_spin_new(ds->box, nullptr, nullptr, 1, 12, 1, 0, 1, nullptr, nullptr);
1048 ds->spin_y = pref_spin_new(ds->box, nullptr, nullptr, 1900, 9999, 1, 0, 1900, nullptr, nullptr);
1051 log_printf("Warning: Date locale %s is unknown", date_format);
1058 ds->spin_m = pref_spin_new(ds->box, nullptr, nullptr, 1, 12, 1, 0, 1, nullptr, nullptr);
1059 ds->spin_d = pref_spin_new(ds->box, nullptr, nullptr, 1, 31, 1, 0, 1, nullptr, nullptr);
1060 ds->spin_y = pref_spin_new(ds->box, nullptr, nullptr, 1900, 9999, 1, 0, 1900, nullptr, nullptr);
1063 spin_increase(ds->spin_y, 5);
1065 ds->button = gtk_toggle_button_new();
1066 g_signal_connect(G_OBJECT(ds->button), "size_allocate",
1067 G_CALLBACK(button_size_allocate_cb), ds->spin_y);
1069 icon = gtk_image_new_from_icon_name(GQ_ICON_PAN_DOWN, GTK_ICON_SIZE_BUTTON);
1070 gq_gtk_container_add(GTK_WIDGET(ds->button), icon);
1071 gtk_widget_show(icon);
1073 gq_gtk_box_pack_start(GTK_BOX(ds->box), ds->button, FALSE, FALSE, 0);
1074 g_signal_connect(G_OBJECT(ds->button), "clicked",
1075 G_CALLBACK(date_selection_button_cb), ds);
1076 gtk_widget_show(ds->button);
1078 g_object_set_data(G_OBJECT(ds->box), DATE_SELECION_KEY, ds);
1083 void date_selection_set(GtkWidget *widget, gint day, gint month, gint year)
1087 ds = static_cast<DateSelection *>(g_object_get_data(G_OBJECT(widget), DATE_SELECION_KEY));
1090 gtk_spin_button_set_value(GTK_SPIN_BUTTON(ds->spin_d), static_cast<gdouble>(day));
1091 gtk_spin_button_set_value(GTK_SPIN_BUTTON(ds->spin_m), static_cast<gdouble>(month));
1092 gtk_spin_button_set_value(GTK_SPIN_BUTTON(ds->spin_y), static_cast<gdouble>(year));
1096 * @brief Returns date structure set to value of spin buttons
1097 * @param widget #DateSelection
1100 * Free returned structure with g_date_time_unref();
1102 GDateTime *date_selection_get(GtkWidget *widget)
1110 ds = static_cast<DateSelection *>(g_object_get_data(G_OBJECT(widget), DATE_SELECION_KEY));
1116 day = gtk_spin_button_get_value(GTK_SPIN_BUTTON(ds->spin_d));
1117 month = gtk_spin_button_get_value(GTK_SPIN_BUTTON(ds->spin_m));
1118 year = gtk_spin_button_get_value(GTK_SPIN_BUTTON(ds->spin_y));
1120 date = g_date_time_new_local(year, month, day, 0, 0, 0);
1125 void date_selection_time_set(GtkWidget *widget, time_t t)
1132 date_selection_set(widget, lt->tm_mday, lt->tm_mon + 1, lt->tm_year + 1900);
1135 #pragma GCC diagnostic push
1136 #pragma GCC diagnostic ignored "-Wunused-function"
1137 time_t date_selection_time_get_unused(GtkWidget *widget)
1144 date_selection_get(widget);
1150 lt.tm_mon = month - 1;
1151 lt.tm_year = year - 1900;
1156 #pragma GCC diagnostic pop
1159 *-----------------------------------------------------------------------------
1160 * storing data in a history list with key,data pairs
1161 *-----------------------------------------------------------------------------
1164 #define PREF_LIST_MARKER_INT "[INT]:"
1165 #define PREF_LIST_MARKER_DOUBLE "[DOUBLE]:"
1166 #define PREF_LIST_MARKER_STRING "[STRING]:"
1168 static GList *pref_list_find(const gchar *group, const gchar *token)
1175 work = history_list_get_by_key(group);
1178 auto text = static_cast<const gchar *>(work->data);
1180 if (strncmp(text, token, l) == 0) return work;
1188 static gboolean pref_list_get(const gchar *group, const gchar *key, const gchar *marker, const gchar **result)
1194 if (!group || !key || !marker)
1200 token = g_strconcat(key, marker, NULL);
1202 work = pref_list_find(group, token);
1205 *result = static_cast<const gchar *>(work->data) + strlen(token);
1206 if (strlen(*result) == 0) *result = nullptr;
1220 static void pref_list_set(const gchar *group, const gchar *key, const gchar *marker, const gchar *text)
1226 if (!group || !key || !marker) return;
1228 token = g_strconcat(key, marker, NULL);
1229 path = g_strconcat(token, text, NULL);
1231 work = pref_list_find(group, token);
1234 auto old_path = static_cast<gchar *>(work->data);
1245 history_list_item_remove(group, old_path);
1250 history_list_add_to_key(group, path, 0);
1257 void pref_list_int_set(const gchar *group, const gchar *key, gint value)
1261 text = g_strdup_printf("%d", value);
1262 pref_list_set(group, key, PREF_LIST_MARKER_INT, text);
1266 gboolean pref_list_int_get(const gchar *group, const gchar *key, gint *result)
1276 if (pref_list_get(group, key, PREF_LIST_MARKER_INT, &text) && text)
1278 *result = static_cast<gint>(strtol(text, nullptr, 10));
1286 #pragma GCC diagnostic push
1287 #pragma GCC diagnostic ignored "-Wunused-function"
1288 void pref_list_double_set_unused(const gchar *group, const gchar *key, gdouble value)
1290 gchar text[G_ASCII_DTOSTR_BUF_SIZE];
1292 g_ascii_dtostr(text, sizeof(text), value);
1293 pref_list_set(group, key, PREF_LIST_MARKER_DOUBLE, text);
1296 gboolean pref_list_double_get_unused(const gchar *group, const gchar *key, gdouble *result)
1306 if (pref_list_get(group, key, PREF_LIST_MARKER_DOUBLE, &text) && text)
1308 *result = g_ascii_strtod(text, nullptr);
1316 void pref_list_string_set_unused(const gchar *group, const gchar *key, const gchar *value)
1318 pref_list_set(group, key, PREF_LIST_MARKER_STRING, value);
1321 gboolean pref_list_string_get_unused(const gchar *group, const gchar *key, const gchar **result)
1323 return pref_list_get(group, key, PREF_LIST_MARKER_STRING, result);
1325 #pragma GCC diagnostic pop
1327 void pref_color_button_set_cb(GtkWidget *widget, gpointer data)
1329 auto color = static_cast<GdkRGBA *>(data);
1331 gtk_color_chooser_get_rgba(GTK_COLOR_CHOOSER(widget), color);
1334 GtkWidget *pref_color_button_new(GtkWidget *parent_box, const gchar *title, GdkRGBA *color, GCallback func, gpointer data)
1340 button = gtk_color_button_new_with_rgba(color);
1344 button = gtk_color_button_new();
1347 if (func) g_signal_connect(G_OBJECT(button), "color-set", func, data);
1354 gtk_color_button_set_title(GTK_COLOR_BUTTON(button), title);
1355 label = gtk_label_new(title);
1357 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
1358 gq_gtk_box_pack_start(GTK_BOX(parent_box), hbox, TRUE, TRUE, 0);
1360 gq_gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 0);
1361 gq_gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
1363 gq_gtk_widget_show_all(hbox);
1367 gtk_widget_show(button);
1374 *-----------------------------------------------------------------------------
1376 *-----------------------------------------------------------------------------
1379 gchar *text_widget_text_pull(GtkWidget *text_widget)
1381 if (GTK_IS_TEXT_VIEW(text_widget))
1383 GtkTextBuffer *buffer;
1384 GtkTextIter start, end;
1386 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_widget));
1387 gtk_text_buffer_get_bounds(buffer, &start, &end);
1389 return gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
1392 if (GTK_IS_ENTRY(text_widget))
1394 return g_strdup(gq_gtk_entry_get_text(GTK_ENTRY(text_widget)));
1402 gchar *text_widget_text_pull_selected(GtkWidget *text_widget)
1404 if (GTK_IS_TEXT_VIEW(text_widget))
1406 GtkTextBuffer *buffer;
1407 GtkTextIter start, end;
1409 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_widget));
1410 gtk_text_buffer_get_bounds(buffer, &start, &end);
1412 if (gtk_text_buffer_get_selection_bounds(buffer, &start, &end))
1414 gtk_text_iter_set_line_offset(&start, 0);
1415 gtk_text_iter_forward_to_line_end(&end);
1418 return gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
1421 if (GTK_IS_ENTRY(text_widget))
1423 return g_strdup(gq_gtk_entry_get_text(GTK_ENTRY(text_widget)));
1430 static gint simple_sort_cb(gconstpointer a, gconstpointer b)
1432 const ActionItem *a_action;
1433 const ActionItem *b_action;
1435 a_action = static_cast<const ActionItem *>(a);
1436 b_action = static_cast<const ActionItem *>(b);
1438 return g_strcmp0(a_action->name, b_action->name);
1441 void free_action_items_cb(gpointer data)
1443 ActionItem *action_item;
1445 action_item = static_cast<ActionItem *>(data);
1446 g_free((gchar *)action_item->icon_name);
1447 g_free((gchar *)action_item->name);
1448 g_free((gchar *)action_item->label);
1449 g_free(action_item);
1452 void action_items_free(GList *list)
1454 g_list_free_full(list, free_action_items_cb);
1458 * @brief Get a list of menu actions
1460 * @returns GList ActionItem
1462 * Free returned list with action_items_free(list)
1464 * The list generated is used in the --remote --action-list command and
1465 * programmable mouse buttons 8 and 9.
1467 GList* get_action_items()
1469 ActionItem *action_item;
1470 const gchar *accel_path;
1477 GList *list_duplicates = nullptr;
1478 GList *list_unique = nullptr;
1482 LayoutWindow *lw = nullptr;
1484 if (!layout_valid(&lw))
1489 groups = gtk_ui_manager_get_action_groups(lw->ui_manager);
1492 actions = gtk_action_group_list_actions(GTK_ACTION_GROUP(groups->data));
1495 action = GTK_ACTION(actions->data);
1496 accel_path = gtk_action_get_accel_path(action);
1498 if (accel_path && gtk_accel_map_lookup_entry(accel_path, nullptr))
1500 g_object_get(action, "tooltip", &tooltip, "label", &label, NULL);
1502 action_name = g_path_get_basename(accel_path);
1504 /* Menu actions are irrelevant */
1505 if (g_strstr_len(action_name, -1, "Menu") == nullptr)
1507 action_item = g_new0(ActionItem, 1);
1509 /* .desktop items need the program name, Geeqie menu items need the tooltip */
1510 if (g_strstr_len(action_name, -1, ".desktop") == nullptr)
1513 /* Tooltips with newlines affect output format */
1514 if (tooltip && (g_strstr_len(tooltip, -1, "\n") == nullptr) )
1516 action_item->label = g_strdup(tooltip);
1520 action_item->label = g_strdup(label);
1525 action_item->label = g_strdup(label);
1528 action_item->name = action_name;
1529 action_item->icon_name = g_strdup(gtk_action_get_stock_id(action));
1531 list_duplicates = g_list_prepend(list_duplicates, action_item);
1534 actions = actions->next;
1537 groups = groups->next;
1540 /* Use the shortest name i.e. ignore -Alt versions. Sort makes the shortest first in the list */
1541 list_duplicates = g_list_sort(list_duplicates, simple_sort_cb);
1543 /* Ignore duplicate entries */
1544 work1 = list_duplicates;
1548 work2 = list_unique;
1549 /* The first entry must be unique, list_unique is null so control bypasses the while */
1552 if (g_strcmp0(static_cast<ActionItem *>(work2->data)->label, static_cast<ActionItem *>(work1->data)->label) == 0)
1557 work2 = work2->next;
1562 action_item = g_new0(ActionItem, 1);
1563 action_item->name = g_strdup(static_cast<ActionItem *>(work1->data)->name);
1564 action_item->label = g_strdup(static_cast<ActionItem *>(work1->data)->label);
1565 action_item->icon_name = g_strdup(static_cast<ActionItem *>(work1->data)->icon_name);
1566 list_unique = g_list_append(list_unique, action_item);
1568 work1 = work1->next;
1571 g_list_free_full(list_duplicates, free_action_items_cb);
1576 gboolean defined_mouse_buttons(GtkWidget *, GdkEventButton *event, gpointer data)
1578 auto lw = static_cast<LayoutWindow *>(data);
1580 gboolean ret = FALSE;
1582 switch (event->button)
1584 case MOUSE_BUTTON_8:
1585 if (options->mouse_button_8)
1587 if (g_strstr_len(options->mouse_button_8, -1, ".desktop") != nullptr)
1589 file_util_start_editor_from_filelist(options->mouse_button_8, layout_selection_list(lw), layout_get_path(lw), lw->window);
1594 action = gtk_action_group_get_action(lw->action_group, options->mouse_button_8);
1597 gtk_action_activate(action);
1603 case MOUSE_BUTTON_9:
1604 if (options->mouse_button_9)
1606 if (g_strstr_len(options->mouse_button_9, -1, ".desktop") != nullptr)
1608 file_util_start_editor_from_filelist(options->mouse_button_9, layout_selection_list(lw), layout_get_path(lw), lw->window);
1612 action = gtk_action_group_get_action(lw->action_group, options->mouse_button_9);
1616 gtk_action_activate(action);
1629 GdkPixbuf *gq_gtk_icon_theme_load_icon_copy(GtkIconTheme *icon_theme, const gchar *icon_name, gint size, GtkIconLookupFlags flags)
1631 GError *error = nullptr;
1632 GdkPixbuf *icon = gtk_icon_theme_load_icon(icon_theme, icon_name, size, flags, &error);
1633 if (error) return nullptr;
1635 GdkPixbuf *pixbuf = gdk_pixbuf_copy(icon);
1636 g_object_unref(icon);
1640 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */