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.
26 #include "history-list.h"
34 *-----------------------------------------------------------------------------
35 * widget and layout utilities
36 *-----------------------------------------------------------------------------
39 GtkWidget *pref_box_new(GtkWidget *parent_box, gboolean fill,
40 GtkOrientation orientation, gboolean padding)
44 if (orientation == GTK_ORIENTATION_HORIZONTAL)
46 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, padding);
50 box = gtk_box_new(GTK_ORIENTATION_VERTICAL, padding);
53 gtk_box_pack_start(GTK_BOX(parent_box), box, fill, fill, 0);
59 GtkWidget *pref_group_new(GtkWidget *parent_box, gboolean fill,
60 const gchar *text, GtkOrientation orientation)
67 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
69 /* add additional spacing if necessary */
70 if (GTK_IS_VBOX(parent_box))
72 GList *list = gtk_container_get_children(GTK_CONTAINER(parent_box));
75 pref_spacer(vbox, PREF_PAD_GROUP - PREF_PAD_GAP);
80 gtk_box_pack_start(GTK_BOX(parent_box), vbox, fill, fill, 0);
81 gtk_widget_show(vbox);
83 label = gtk_label_new(text);
84 gtk_label_set_xalign(GTK_LABEL(label), 0.0);
85 gtk_label_set_yalign(GTK_LABEL(label), 0.5);
86 pref_label_bold(label, TRUE, FALSE);
88 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
89 gtk_widget_show(label);
91 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_INDENT);
92 gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 0);
93 gtk_widget_show(hbox);
95 /* indent using empty box */
98 if (orientation == GTK_ORIENTATION_HORIZONTAL)
100 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_SPACE);
104 box = gtk_box_new(GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
106 gtk_box_pack_start(GTK_BOX(hbox), box, TRUE, TRUE, 0);
107 gtk_widget_show(box);
109 g_object_set_data(G_OBJECT(box), "pref_group", vbox);
114 GtkWidget *pref_group_parent(GtkWidget *child)
123 group = static_cast<GtkWidget *>(g_object_get_data(G_OBJECT(parent), "pref_group"));
124 if (group && GTK_IS_WIDGET(group)) return group;
126 parent = gtk_widget_get_parent(parent);
132 GtkWidget *pref_frame_new(GtkWidget *parent_box, gboolean fill,
134 GtkOrientation orientation, gboolean padding)
137 GtkWidget *frame = nullptr;
139 frame = gtk_frame_new(text);
140 gtk_box_pack_start(GTK_BOX(parent_box), frame, fill, fill, 0);
141 gtk_widget_show(frame);
143 if (orientation == GTK_ORIENTATION_HORIZONTAL)
145 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, padding);
149 box = gtk_box_new(GTK_ORIENTATION_VERTICAL, padding);
151 gtk_container_add(GTK_CONTAINER(frame), box);
152 gtk_container_set_border_width(GTK_CONTAINER(box), PREF_PAD_BORDER);
153 gtk_widget_show(box);
158 GtkWidget *pref_spacer(GtkWidget *parent_box, gboolean padding)
162 spacer = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
163 gtk_box_pack_start(GTK_BOX(parent_box), spacer, FALSE, FALSE, padding / 2);
164 gtk_widget_show(spacer);
169 GtkWidget *pref_line(GtkWidget *parent_box, gboolean padding)
173 spacer = gtk_separator_new(GTK_IS_HBOX(parent_box) ? GTK_ORIENTATION_VERTICAL : GTK_ORIENTATION_HORIZONTAL);
174 gtk_box_pack_start(GTK_BOX(parent_box), spacer, FALSE, FALSE, padding / 2);
175 gtk_widget_show(spacer);
180 GtkWidget *pref_label_new(GtkWidget *parent_box, const gchar *text)
184 label = gtk_label_new(text);
185 gtk_box_pack_start(GTK_BOX(parent_box), label, FALSE, FALSE, 0);
186 gtk_widget_show(label);
191 GtkWidget *pref_label_new_mnemonic(GtkWidget *parent_box, const gchar *text, GtkWidget *widget)
195 label = gtk_label_new_with_mnemonic(text);
196 gtk_label_set_mnemonic_widget(GTK_LABEL(label), widget);
197 gtk_box_pack_start(GTK_BOX(parent_box), label, FALSE, FALSE, 0);
198 gtk_widget_show(label);
203 void pref_label_bold(GtkWidget *label, gboolean bold, gboolean increase_size)
208 if (!bold && !increase_size) return;
210 pal = pango_attr_list_new();
214 pa = pango_attr_weight_new(PANGO_WEIGHT_BOLD);
216 pa->end_index = G_MAXINT;
217 pango_attr_list_insert(pal, pa);
222 pa = pango_attr_scale_new(PANGO_SCALE_LARGE);
224 pa->end_index = G_MAXINT;
225 pango_attr_list_insert(pal, pa);
228 gtk_label_set_attributes(GTK_LABEL(label), pal);
229 pango_attr_list_unref(pal);
232 GtkWidget *pref_button_new(GtkWidget *parent_box, const gchar *icon_name,
233 const gchar *text, GCallback func, gpointer data)
239 button = gtk_button_new_from_icon_name(icon_name, GTK_ICON_SIZE_BUTTON);
243 button = gtk_button_new();
248 gtk_button_set_use_underline(GTK_BUTTON(button), TRUE);
249 gtk_button_set_label(GTK_BUTTON(button), text);
252 if (func) g_signal_connect(G_OBJECT(button), "clicked", func, data);
256 gtk_box_pack_start(GTK_BOX(parent_box), button, FALSE, FALSE, 0);
257 gtk_widget_show(button);
263 static GtkWidget *real_pref_checkbox_new(GtkWidget *parent_box, const gchar *text, gboolean mnemonic_text,
264 gboolean active, GCallback func, gpointer data)
270 button = gtk_check_button_new_with_mnemonic(text);
274 button = gtk_check_button_new_with_label(text);
276 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), active);
277 if (func) g_signal_connect(G_OBJECT(button), "clicked", func, data);
279 gtk_box_pack_start(GTK_BOX(parent_box), button, FALSE, FALSE, 0);
280 gtk_widget_show(button);
285 GtkWidget *pref_checkbox_new(GtkWidget *parent_box, const gchar *text, gboolean active,
286 GCallback func, gpointer data)
288 return real_pref_checkbox_new(parent_box, text, FALSE, active, func, data);
291 #pragma GCC diagnostic push
292 #pragma GCC diagnostic ignored "-Wunused-function"
293 GtkWidget *pref_checkbox_new_mnemonic_unused(GtkWidget *parent_box, const gchar *text, gboolean active,
294 GCallback func, gpointer data)
296 return real_pref_checkbox_new(parent_box, text, TRUE, active, func, data);
298 #pragma GCC diagnostic pop
300 static void pref_checkbox_int_cb(GtkWidget *widget, gpointer data)
302 auto result = static_cast<gboolean *>(data);
304 *result = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
307 GtkWidget *pref_checkbox_new_int(GtkWidget *parent_box, const gchar *text, gboolean active,
312 button = pref_checkbox_new(parent_box, text, active,
313 G_CALLBACK(pref_checkbox_int_cb), result);
319 static void pref_checkbox_link_sensitivity_cb(GtkWidget *button, gpointer data)
321 auto widget = static_cast<GtkWidget *>(data);
323 gtk_widget_set_sensitive(widget, gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button)));
326 void pref_checkbox_link_sensitivity(GtkWidget *button, GtkWidget *widget)
328 g_signal_connect(G_OBJECT(button), "toggled",
329 G_CALLBACK(pref_checkbox_link_sensitivity_cb), widget);
331 pref_checkbox_link_sensitivity_cb(button, widget);
334 #pragma GCC diagnostic push
335 #pragma GCC diagnostic ignored "-Wunused-function"
336 static void pref_checkbox_link_sensitivity_swap_cb_unused(GtkWidget *button, gpointer data)
338 GtkWidget *widget = static_cast<GtkWidget *>(data);
340 gtk_widget_set_sensitive(widget, !gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button)));
343 void pref_checkbox_link_sensitivity_swap_unused(GtkWidget *button, GtkWidget *widget)
345 g_signal_connect(G_OBJECT(button), "toggled",
346 G_CALLBACK(pref_checkbox_link_sensitivity_swap_cb_unused), widget);
348 pref_checkbox_link_sensitivity_swap_cb_unused(button, widget);
350 #pragma GCC diagnostic pop
352 static GtkWidget *real_pref_radiobutton_new(GtkWidget *parent_box, GtkWidget *sibling,
353 const gchar *text, gboolean mnemonic_text, gboolean active,
354 GCallback func, gpointer data)
361 group = gtk_radio_button_get_group(GTK_RADIO_BUTTON(sibling));
370 button = gtk_radio_button_new_with_mnemonic(group, text);
374 button = gtk_radio_button_new_with_label(group, text);
377 if (active) gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), active);
378 if (func) g_signal_connect(G_OBJECT(button), "clicked", func, data);
380 gtk_box_pack_start(GTK_BOX(parent_box), button, FALSE, FALSE, 0);
381 gtk_widget_show(button);
386 GtkWidget *pref_radiobutton_new(GtkWidget *parent_box, GtkWidget *sibling,
387 const gchar *text, gboolean active,
388 GCallback func, gpointer data)
390 return real_pref_radiobutton_new(parent_box, sibling, text, FALSE, active, func, data);
393 #pragma GCC diagnostic push
394 #pragma GCC diagnostic ignored "-Wunused-function"
395 GtkWidget *pref_radiobutton_new_mnemonic_unused(GtkWidget *parent_box, GtkWidget *sibling,
396 const gchar *text, gboolean active,
397 GCallback func, gpointer data)
399 return real_pref_radiobutton_new(parent_box, sibling, text, TRUE, active, func, data);
402 #define PREF_RADIO_VALUE_KEY "pref_radio_value"
404 static void pref_radiobutton_int_cb_unused(GtkWidget *widget, gpointer data)
406 gboolean *result = static_cast<gboolean *>(data);
408 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
410 *result = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), PREF_RADIO_VALUE_KEY));
414 GtkWidget *pref_radiobutton_new_int_unused(GtkWidget *parent_box, GtkWidget *sibling,
415 const gchar *text, gboolean active,
416 gboolean *result, gboolean value,
421 button = pref_radiobutton_new(parent_box, sibling, text, active,
422 G_CALLBACK(pref_radiobutton_int_cb_unused), result);
423 g_object_set_data(G_OBJECT(button), PREF_RADIO_VALUE_KEY, GINT_TO_POINTER(value));
424 if (active) *result = value;
428 #pragma GCC diagnostic pop
430 static GtkWidget *real_pref_spin_new(GtkWidget *parent_box, const gchar *text, const gchar *suffix,
431 gboolean mnemonic_text,
432 gdouble min, gdouble max, gdouble step, gint digits,
434 GCallback func, gpointer data)
440 box = pref_box_new(parent_box, FALSE, GTK_ORIENTATION_HORIZONTAL, PREF_PAD_SPACE);
442 spin = gtk_spin_button_new_with_range(min, max, step);
443 gtk_spin_button_set_digits(GTK_SPIN_BUTTON(spin), digits);
444 gtk_spin_button_set_value(GTK_SPIN_BUTTON(spin), value);
448 g_signal_connect(G_OBJECT(spin), "value_changed", G_CALLBACK(func), data);
455 label = pref_label_new_mnemonic(box, text, spin);
459 label = pref_label_new(box, text);
461 pref_link_sensitivity(label, spin);
464 gtk_box_pack_start(GTK_BOX(box), spin, FALSE, FALSE, 0);
465 gtk_widget_show(spin);
467 /* perhaps this should only be PREF_PAD_GAP distance from spinbutton ? */
470 label = pref_label_new(box, suffix);
471 pref_link_sensitivity(label, spin);
477 GtkWidget *pref_spin_new(GtkWidget *parent_box, const gchar *text, const gchar *suffix,
478 gdouble min, gdouble max, gdouble step, gint digits,
480 GCallback func, gpointer data)
482 return real_pref_spin_new(parent_box, text, suffix, FALSE,
483 min, max, step, digits, value, func, data);
486 #pragma GCC diagnostic push
487 #pragma GCC diagnostic ignored "-Wunused-function"
488 GtkWidget *pref_spin_new_mnemonic_unused(GtkWidget *parent_box, const gchar *text, const gchar *suffix,
489 gdouble min, gdouble max, gdouble step, gint digits,
491 GCallback func, gpointer data)
493 return real_pref_spin_new(parent_box, text, suffix, TRUE,
494 min, max, step, digits, value, func, data);
496 #pragma GCC diagnostic pop
498 static void pref_spin_int_cb(GtkWidget *widget, gpointer data)
500 auto var = static_cast<gint *>(data);
501 *var = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(widget));
504 GtkWidget *pref_spin_new_int(GtkWidget *parent_box, const gchar *text, const gchar *suffix,
505 gint min, gint max, gint step,
506 gint value, gint *value_var)
509 return pref_spin_new(parent_box, text, suffix,
510 static_cast<gdouble>(min), static_cast<gdouble>(max), static_cast<gdouble>(step), 0,
512 G_CALLBACK(pref_spin_int_cb), value_var);
515 static void pref_link_sensitivity_cb(GtkWidget *watch, GtkStateType, gpointer data)
517 auto widget = static_cast<GtkWidget *>(data);
519 gtk_widget_set_sensitive(widget, gtk_widget_is_sensitive(watch));
522 void pref_link_sensitivity(GtkWidget *widget, GtkWidget *watch)
524 g_signal_connect(G_OBJECT(watch), "state_changed",
525 G_CALLBACK(pref_link_sensitivity_cb), widget);
528 void pref_signal_block_data(GtkWidget *widget, gpointer data)
530 g_signal_handlers_block_matched(widget, G_SIGNAL_MATCH_DATA,
531 0, 0, nullptr, nullptr, data);
534 void pref_signal_unblock_data(GtkWidget *widget, gpointer data)
536 g_signal_handlers_unblock_matched(widget, G_SIGNAL_MATCH_DATA,
537 0, 0, nullptr, nullptr, data);
540 GtkWidget *pref_table_new(GtkWidget *parent_box, gint columns, gint rows,
541 gboolean homogeneous, gboolean fill)
545 table = gtk_table_new(rows, columns, homogeneous);
546 gtk_table_set_row_spacings(GTK_TABLE(table), PREF_PAD_GAP);
547 gtk_table_set_col_spacings(GTK_TABLE(table), PREF_PAD_SPACE);
551 gtk_box_pack_start(GTK_BOX(parent_box), table, fill, fill, 0);
552 gtk_widget_show(table);
558 GtkWidget *pref_table_box(GtkWidget *table, gint column, gint row,
559 GtkOrientation orientation, const gchar *text)
566 shell = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
567 box = pref_group_new(shell, TRUE, text, orientation);
571 if (orientation == GTK_ORIENTATION_HORIZONTAL)
573 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_SPACE);
577 box = gtk_box_new(GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
582 gtk_table_attach(GTK_TABLE(table), shell, column, column + 1, row, row + 1,
583 static_cast<GtkAttachOptions>(GTK_EXPAND | GTK_FILL), static_cast<GtkAttachOptions>(0), 0, 0);
585 gtk_widget_show(shell);
590 GtkWidget *pref_table_label(GtkWidget *table, gint column, gint row,
591 const gchar *text, GtkAlign alignment)
595 label = gtk_label_new(text);
596 gtk_widget_set_halign(label, alignment);
597 gtk_widget_set_valign(label, GTK_ALIGN_CENTER);
598 gtk_table_attach(GTK_TABLE(table), label, column, column + 1, row, row + 1,
599 GTK_FILL, static_cast<GtkAttachOptions>(0), 0, 0);
600 gtk_widget_show(label);
605 GtkWidget *pref_table_button(GtkWidget *table, gint column, gint row,
606 const gchar *stock_id, const gchar *text,
607 GCallback func, gpointer data)
611 button = pref_button_new(nullptr, stock_id, text, func, data);
612 gtk_table_attach(GTK_TABLE(table), button, column, column + 1, row, row + 1,
613 GTK_FILL, static_cast<GtkAttachOptions>(0), 0, 0);
614 gtk_widget_show(button);
619 GtkWidget *pref_table_spin(GtkWidget *table, gint column, gint row,
620 const gchar *text, const gchar *suffix,
621 gdouble min, gdouble max, gdouble step, gint digits,
623 GCallback func, gpointer data)
629 spin = gtk_spin_button_new_with_range(min, max, step);
630 gtk_spin_button_set_digits(GTK_SPIN_BUTTON(spin), digits);
631 gtk_spin_button_set_value(GTK_SPIN_BUTTON(spin), value);
634 g_signal_connect(G_OBJECT(spin), "value_changed", G_CALLBACK(func), data);
639 label = pref_table_label(table, column, row, text, GTK_ALIGN_END);
640 pref_link_sensitivity(label, spin);
646 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_SPACE);
647 gtk_box_pack_start(GTK_BOX(box), spin, FALSE, FALSE, 0);
648 gtk_widget_show(spin);
650 label = pref_label_new(box, suffix);
651 pref_link_sensitivity(label, spin);
658 gtk_table_attach(GTK_TABLE(table), box, column, column + 1, row, row + 1,
659 static_cast<GtkAttachOptions>(GTK_EXPAND | GTK_FILL), static_cast<GtkAttachOptions>(GTK_EXPAND | GTK_FILL), 0, 0);
660 gtk_widget_show(box);
665 GtkWidget *pref_table_spin_new_int(GtkWidget *table, gint column, gint row,
666 const gchar *text, const gchar *suffix,
667 gint min, gint max, gint step,
668 gint value, gint *value_var)
671 return pref_table_spin(table, column, row,
673 static_cast<gdouble>(min), static_cast<gdouble>(max), static_cast<gdouble>(step), 0,
675 G_CALLBACK(pref_spin_int_cb), value_var);
679 GtkWidget *pref_toolbar_new(GtkWidget *parent_box, GtkToolbarStyle style)
683 tbar = gtk_toolbar_new();
684 gtk_toolbar_set_style(GTK_TOOLBAR(tbar), style);
688 gtk_box_pack_start(GTK_BOX(parent_box), tbar, FALSE, FALSE, 0);
689 gtk_widget_show(tbar);
694 GtkWidget *pref_toolbar_button(GtkWidget *toolbar,
695 const gchar *icon_name, const gchar *label, gboolean toggle,
696 const gchar *description,
697 GCallback func, gpointer data)
701 if (toggle) // TODO: TG seems no function uses toggle now
703 item = GTK_WIDGET(gtk_toggle_tool_button_new());
704 if (icon_name) gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(item), icon_name);
705 if (label) gtk_tool_button_set_label(GTK_TOOL_BUTTON(item), label);
709 GtkWidget *icon = nullptr;
712 icon = gtk_image_new_from_icon_name(icon_name, GTK_ICON_SIZE_LARGE_TOOLBAR); // TODO: TG which size?
713 gtk_widget_show(icon);
715 item = GTK_WIDGET(gtk_tool_button_new(icon, label));
717 gtk_tool_button_set_use_underline(GTK_TOOL_BUTTON(item), TRUE);
719 if (func) g_signal_connect(item, "clicked", func, data);
720 gtk_container_add(GTK_CONTAINER(toolbar), item);
721 gtk_widget_show(item);
725 gtk_widget_set_tooltip_text(item, description);
731 #pragma GCC diagnostic push
732 #pragma GCC diagnostic ignored "-Wunused-function"
733 void pref_toolbar_button_set_icon_unused(GtkWidget *button, GtkWidget *widget, const gchar *stock_id)
737 gtk_tool_button_set_icon_widget(GTK_TOOL_BUTTON(button), widget);
741 gtk_tool_button_set_stock_id(GTK_TOOL_BUTTON(button), stock_id);
745 GtkWidget *pref_toolbar_spacer_unused(GtkWidget *toolbar)
749 item = GTK_WIDGET(gtk_separator_tool_item_new());
750 gtk_container_add(GTK_CONTAINER(toolbar), item);
751 gtk_widget_show(item);
755 #pragma GCC diagnostic pop
759 *-----------------------------------------------------------------------------
760 * date selection entry
761 *-----------------------------------------------------------------------------
764 #define DATE_SELECION_KEY "date_selection_data"
782 static void date_selection_popup_hide(DateSelection *ds)
784 if (!ds->window) return;
786 if (gtk_widget_has_grab(ds->window))
788 gtk_grab_remove(ds->window);
789 gdk_keyboard_ungrab(GDK_CURRENT_TIME);
790 gdk_pointer_ungrab(GDK_CURRENT_TIME);
793 gtk_widget_hide(ds->window);
795 gtk_widget_destroy(ds->window);
796 ds->window = nullptr;
797 ds->calendar = nullptr;
799 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ds->button), FALSE);
802 static gboolean date_selection_popup_release_cb(GtkWidget *, GdkEventButton *, gpointer data)
804 auto ds = static_cast<DateSelection *>(data);
806 date_selection_popup_hide(ds);
810 static gboolean date_selection_popup_press_cb(GtkWidget *, GdkEventButton *event, gpointer data)
812 auto ds = static_cast<DateSelection *>(data);
818 xr = static_cast<gint>(event->x_root);
819 yr = static_cast<gint>(event->y_root);
821 window = gtk_widget_get_window(ds->window);
822 gdk_window_get_origin(window, &x, &y);
823 w = gdk_window_get_width(window);
824 h = gdk_window_get_height(window);
826 if (xr < x || yr < y || xr > x + w || yr > y + h)
828 g_signal_connect(G_OBJECT(ds->window), "button_release_event",
829 G_CALLBACK(date_selection_popup_release_cb), ds);
836 static void date_selection_popup_sync(DateSelection *ds)
838 guint day, month, year;
840 gtk_calendar_get_date(GTK_CALENDAR(ds->calendar), &year, &month, &day);
841 date_selection_set(ds->box, day, month + 1, year);
844 static gboolean date_selection_popup_keypress_cb(GtkWidget *, GdkEventKey *event, gpointer data)
846 auto ds = static_cast<DateSelection *>(data);
848 switch (event->keyval)
851 case GDK_KEY_KP_Enter:
853 case GDK_KEY_ISO_Left_Tab:
854 date_selection_popup_sync(ds);
855 date_selection_popup_hide(ds);
858 date_selection_popup_hide(ds);
867 static void date_selection_day_cb(GtkWidget *, gpointer data)
869 auto ds = static_cast<DateSelection *>(data);
871 date_selection_popup_sync(ds);
874 static void date_selection_doubleclick_cb(GtkWidget *, gpointer data)
876 auto ds = static_cast<DateSelection *>(data);
878 date_selection_popup_hide(ds);
881 static void date_selection_popup(DateSelection *ds)
885 gint day, month, year;
886 GtkAllocation button_allocation;
887 GtkAllocation window_allocation;
889 if (ds->window) return;
891 ds->window = gtk_window_new(GTK_WINDOW_POPUP);
892 gtk_window_set_resizable(GTK_WINDOW(ds->window), FALSE);
893 g_signal_connect(G_OBJECT(ds->window), "button_press_event",
894 G_CALLBACK(date_selection_popup_press_cb), ds);
895 g_signal_connect(G_OBJECT(ds->window), "key_press_event",
896 G_CALLBACK(date_selection_popup_keypress_cb), ds);
898 ds->calendar = gtk_calendar_new();
899 gtk_container_add(GTK_CONTAINER(ds->window), ds->calendar);
900 gtk_widget_show(ds->calendar);
902 date_selection_get(ds->box, &day, &month, &year);
903 gtk_calendar_select_month(GTK_CALENDAR(ds->calendar), month - 1, year);
904 gtk_calendar_select_day(GTK_CALENDAR(ds->calendar), day);
906 g_signal_connect(G_OBJECT(ds->calendar), "day_selected",
907 G_CALLBACK(date_selection_day_cb), ds);
908 g_signal_connect(G_OBJECT(ds->calendar), "day_selected_double_click",
909 G_CALLBACK(date_selection_doubleclick_cb), ds);
911 gtk_widget_realize(ds->window);
913 gdk_window_get_origin(gtk_widget_get_window(ds->button), &wx, &wy);
915 gtk_widget_get_allocation(ds->button, &button_allocation);
916 gtk_widget_get_allocation(ds->window, &window_allocation);
918 x = wx + button_allocation.x + button_allocation.width - window_allocation.width;
919 y = wy + button_allocation.y + button_allocation.height;
921 if (y + window_allocation.height > gdk_screen_height())
923 y = wy + button_allocation.y - window_allocation.height;
928 gtk_window_move(GTK_WINDOW(ds->window), x, y);
929 gtk_widget_show(ds->window);
931 gtk_widget_grab_focus(ds->calendar);
932 gdk_pointer_grab(gtk_widget_get_window(ds->window), TRUE,
933 static_cast<GdkEventMask>(GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_MOTION_MASK),
934 nullptr, nullptr, GDK_CURRENT_TIME);
935 gdk_keyboard_grab(gtk_widget_get_window(ds->window), TRUE, GDK_CURRENT_TIME);
936 gtk_grab_add(ds->window);
938 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ds->button), TRUE);
941 static void date_selection_button_cb(GtkWidget *, gpointer data)
943 auto ds = static_cast<DateSelection *>(data);
945 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(ds->button)) == (!ds->window))
947 date_selection_popup(ds);
951 static void button_size_allocate_cb(GtkWidget *button, GtkAllocation *allocation, gpointer data)
953 auto spin = static_cast<GtkWidget *>(data);
954 GtkRequisition spin_requisition;
955 gtk_widget_get_requisition(spin, &spin_requisition);
957 if (allocation->height > spin_requisition.height)
959 GtkAllocation button_allocation;
960 GtkAllocation spin_allocation;
962 gtk_widget_get_allocation(button, &button_allocation);
963 gtk_widget_get_allocation(spin, &spin_allocation);
964 button_allocation.height = spin_requisition.height;
965 button_allocation.y = spin_allocation.y +
966 (spin_allocation.height - spin_requisition.height) / 2;
967 gtk_widget_size_allocate(button, &button_allocation);
971 static void spin_increase(GtkWidget *spin, gint value)
975 gtk_widget_size_request(spin, &req);
976 gtk_widget_set_size_request(spin, req.width + value, -1);
979 static void date_selection_destroy_cb(GtkWidget *, gpointer data)
981 auto ds = static_cast<DateSelection *>(data);
983 date_selection_popup_hide(ds);
988 GtkWidget *date_selection_new()
993 ds = g_new0(DateSelection, 1);
997 ds->box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
998 g_signal_connect(G_OBJECT(ds->box), "destroy",
999 G_CALLBACK(date_selection_destroy_cb), ds);
1001 date_format = nl_langinfo(D_FMT);
1003 if (strlen(date_format) == 8)
1005 for (i=1; i<8; i=i+3)
1007 switch (date_format[i])
1010 ds->spin_d = pref_spin_new(ds->box, nullptr, nullptr, 1, 31, 1, 0, 1, nullptr, nullptr);
1013 ds->spin_m = pref_spin_new(ds->box, nullptr, nullptr, 1, 12, 1, 0, 1, nullptr, nullptr);
1016 ds->spin_y = pref_spin_new(ds->box, nullptr, nullptr, 1900, 9999, 1, 0, 1900, nullptr, nullptr);
1019 log_printf("Warning: Date locale %s is unknown", date_format);
1026 ds->spin_m = pref_spin_new(ds->box, nullptr, nullptr, 1, 12, 1, 0, 1, nullptr, nullptr);
1027 ds->spin_d = pref_spin_new(ds->box, nullptr, nullptr, 1, 31, 1, 0, 1, nullptr, nullptr);
1028 ds->spin_y = pref_spin_new(ds->box, nullptr, nullptr, 1900, 9999, 1, 0, 1900, nullptr, nullptr);
1031 spin_increase(ds->spin_y, 5);
1033 ds->button = gtk_toggle_button_new();
1034 g_signal_connect(G_OBJECT(ds->button), "size_allocate",
1035 G_CALLBACK(button_size_allocate_cb), ds->spin_y);
1037 icon = gtk_image_new_from_icon_name(GQ_ICON_CALENDAR, GTK_ICON_SIZE_BUTTON);
1038 gtk_container_add(GTK_CONTAINER(ds->button), icon);
1039 gtk_widget_show(icon);
1041 gtk_box_pack_start(GTK_BOX(ds->box), ds->button, FALSE, FALSE, 0);
1042 g_signal_connect(G_OBJECT(ds->button), "clicked",
1043 G_CALLBACK(date_selection_button_cb), ds);
1044 gtk_widget_show(ds->button);
1046 g_object_set_data(G_OBJECT(ds->box), DATE_SELECION_KEY, ds);
1051 void date_selection_set(GtkWidget *widget, gint day, gint month, gint year)
1055 ds = static_cast<DateSelection *>(g_object_get_data(G_OBJECT(widget), DATE_SELECION_KEY));
1058 gtk_spin_button_set_value(GTK_SPIN_BUTTON(ds->spin_d), static_cast<gdouble>(day));
1059 gtk_spin_button_set_value(GTK_SPIN_BUTTON(ds->spin_m), static_cast<gdouble>(month));
1060 gtk_spin_button_set_value(GTK_SPIN_BUTTON(ds->spin_y), static_cast<gdouble>(year));
1064 void date_selection_get(GtkWidget *widget, gint *day, gint *month, gint *year)
1068 ds = static_cast<DateSelection *>(g_object_get_data(G_OBJECT(widget), DATE_SELECION_KEY));
1071 if (day) *day = gtk_spin_button_get_value(GTK_SPIN_BUTTON(ds->spin_d));
1072 if (month) *month = gtk_spin_button_get_value(GTK_SPIN_BUTTON(ds->spin_m));
1073 if (year) *year = gtk_spin_button_get_value(GTK_SPIN_BUTTON(ds->spin_y));
1076 void date_selection_time_set(GtkWidget *widget, time_t t)
1083 date_selection_set(widget, lt->tm_mday, lt->tm_mon + 1, lt->tm_year + 1900);
1086 #pragma GCC diagnostic push
1087 #pragma GCC diagnostic ignored "-Wunused-function"
1088 time_t date_selection_time_get_unused(GtkWidget *widget)
1095 date_selection_get(widget, &day, &month ,&year);
1101 lt.tm_mon = month - 1;
1102 lt.tm_year = year - 1900;
1107 #pragma GCC diagnostic pop
1110 *-----------------------------------------------------------------------------
1111 * storing data in a history list with key,data pairs
1112 *-----------------------------------------------------------------------------
1115 #define PREF_LIST_MARKER_INT "[INT]:"
1116 #define PREF_LIST_MARKER_DOUBLE "[DOUBLE]:"
1117 #define PREF_LIST_MARKER_STRING "[STRING]:"
1119 static GList *pref_list_find(const gchar *group, const gchar *token)
1126 work = history_list_get_by_key(group);
1129 auto text = static_cast<const gchar *>(work->data);
1131 if (strncmp(text, token, l) == 0) return work;
1139 static gboolean pref_list_get(const gchar *group, const gchar *key, const gchar *marker, const gchar **result)
1145 if (!group || !key || !marker)
1151 token = g_strconcat(key, marker, NULL);
1153 work = pref_list_find(group, token);
1156 *result = static_cast<const gchar *>(work->data) + strlen(token);
1157 if (strlen(*result) == 0) *result = nullptr;
1171 static void pref_list_set(const gchar *group, const gchar *key, const gchar *marker, const gchar *text)
1177 if (!group || !key || !marker) return;
1179 token = g_strconcat(key, marker, NULL);
1180 path = g_strconcat(token, text, NULL);
1182 work = pref_list_find(group, token);
1185 auto old_path = static_cast<gchar *>(work->data);
1196 history_list_item_remove(group, old_path);
1201 history_list_add_to_key(group, path, 0);
1208 void pref_list_int_set(const gchar *group, const gchar *key, gint value)
1212 text = g_strdup_printf("%d", value);
1213 pref_list_set(group, key, PREF_LIST_MARKER_INT, text);
1217 gboolean pref_list_int_get(const gchar *group, const gchar *key, gint *result)
1227 if (pref_list_get(group, key, PREF_LIST_MARKER_INT, &text) && text)
1229 *result = static_cast<gint>(strtol(text, nullptr, 10));
1237 #pragma GCC diagnostic push
1238 #pragma GCC diagnostic ignored "-Wunused-function"
1239 void pref_list_double_set_unused(const gchar *group, const gchar *key, gdouble value)
1241 gchar text[G_ASCII_DTOSTR_BUF_SIZE];
1243 g_ascii_dtostr(text, sizeof(text), value);
1244 pref_list_set(group, key, PREF_LIST_MARKER_DOUBLE, text);
1247 gboolean pref_list_double_get_unused(const gchar *group, const gchar *key, gdouble *result)
1257 if (pref_list_get(group, key, PREF_LIST_MARKER_DOUBLE, &text) && text)
1259 *result = g_ascii_strtod(text, NULL);
1267 void pref_list_string_set_unused(const gchar *group, const gchar *key, const gchar *value)
1269 pref_list_set(group, key, PREF_LIST_MARKER_STRING, value);
1272 gboolean pref_list_string_get_unused(const gchar *group, const gchar *key, const gchar **result)
1274 return pref_list_get(group, key, PREF_LIST_MARKER_STRING, result);
1276 #pragma GCC diagnostic pop
1278 void pref_color_button_set_cb(GtkWidget *widget, gpointer data)
1280 auto color = static_cast<GdkColor *>(data);
1282 gtk_color_button_get_color(GTK_COLOR_BUTTON(widget), color);
1285 GtkWidget *pref_color_button_new(GtkWidget *parent_box,
1286 const gchar *title, const GdkColor *color,
1287 GCallback func, gpointer data)
1293 button = gtk_color_button_new_with_color(color);
1297 button = gtk_color_button_new();
1300 if (func) g_signal_connect(G_OBJECT(button), "color-set", func, data);
1307 gtk_color_button_set_title(GTK_COLOR_BUTTON(button), title);
1308 label = gtk_label_new(title);
1310 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
1311 gtk_box_pack_start(GTK_BOX(parent_box), hbox, TRUE, TRUE, 0);
1313 gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 0);
1314 gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
1316 gtk_widget_show_all(hbox);
1320 gtk_widget_show(button);
1327 *-----------------------------------------------------------------------------
1329 *-----------------------------------------------------------------------------
1332 gchar *text_widget_text_pull(GtkWidget *text_widget)
1334 if (GTK_IS_TEXT_VIEW(text_widget))
1336 GtkTextBuffer *buffer;
1337 GtkTextIter start, end;
1339 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_widget));
1340 gtk_text_buffer_get_bounds(buffer, &start, &end);
1342 return gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
1344 else if (GTK_IS_ENTRY(text_widget))
1346 return g_strdup(gtk_entry_get_text(GTK_ENTRY(text_widget)));
1355 gchar *text_widget_text_pull_selected(GtkWidget *text_widget)
1357 if (GTK_IS_TEXT_VIEW(text_widget))
1359 GtkTextBuffer *buffer;
1360 GtkTextIter start, end;
1362 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_widget));
1363 gtk_text_buffer_get_bounds(buffer, &start, &end);
1365 if (gtk_text_buffer_get_selection_bounds(buffer, &start, &end))
1367 gtk_text_iter_set_line_offset(&start, 0);
1368 gtk_text_iter_forward_to_line_end(&end);
1371 return gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
1373 else if (GTK_IS_ENTRY(text_widget))
1375 return g_strdup(gtk_entry_get_text(GTK_ENTRY(text_widget)));
1383 static gint simple_sort_cb(gconstpointer a, gconstpointer b)
1385 const ActionItem *a_action;
1386 const ActionItem *b_action;
1388 a_action = static_cast<const ActionItem *>(a);
1389 b_action = static_cast<const ActionItem *>(b);
1391 return g_strcmp0(a_action->name, b_action->name);
1394 void free_action_items_cb(gpointer data)
1396 ActionItem *action_item;
1398 action_item = static_cast<ActionItem *>(data);
1399 g_free((gchar *)action_item->icon_name);
1400 g_free((gchar *)action_item->name);
1401 g_free((gchar *)action_item->label);
1402 g_free(action_item);
1405 void action_items_free(GList *list)
1407 g_list_free_full(list, free_action_items_cb);
1411 * @brief Get a list of menu actions
1413 * @returns GList ActionItem
1415 * Free returned list with action_items_free(list)
1417 * The list generated is used in the --remote --action-list command and
1418 * programmable mouse buttons 8 and 9.
1420 GList* get_action_items()
1422 ActionItem *action_item;
1423 const gchar *accel_path;
1430 GList *list = nullptr;
1433 LayoutWindow *lw = nullptr;
1435 if (!layout_valid(&lw))
1440 groups = gtk_ui_manager_get_action_groups(lw->ui_manager);
1443 actions = gtk_action_group_list_actions(GTK_ACTION_GROUP(groups->data));
1446 action = GTK_ACTION(actions->data);
1447 accel_path = gtk_action_get_accel_path(action);
1449 if (accel_path && gtk_accel_map_lookup_entry(accel_path, nullptr))
1451 g_object_get(action, "tooltip", &tooltip, "label", &label, NULL);
1453 action_name = g_path_get_basename(accel_path);
1455 /* Menu actions are irrelevant */
1456 if (g_strstr_len(action_name, -1, "Menu") == nullptr)
1458 action_item = g_new0(ActionItem, 1);
1460 /* .desktop items need the program name, Geeqie menu items need the tooltip */
1461 if (g_strstr_len(action_name, -1, ".desktop") == nullptr)
1464 /* Tooltips with newlines affect output format */
1465 if (tooltip && (g_strstr_len(tooltip, -1, "\n") == nullptr) )
1467 action_item->label = g_strdup(tooltip);
1471 action_item->label = g_strdup(label);
1476 action_item->label = g_strdup(label);
1479 action_item->name = g_strdup(action_name);
1480 action_item->icon_name = g_strdup(gtk_action_get_stock_id(action));
1482 /* Ignore duplicate entries */
1487 if (g_strcmp0(tooltip, static_cast<ActionItem *>(work->data)->label) == 0)
1497 g_free((gchar *)action_item->icon_name);
1498 g_free((gchar *)action_item->name);
1499 g_free((gchar *)action_item->label);
1500 g_free(action_item);
1504 list = g_list_prepend(list, action_item);
1506 g_free(action_name);
1511 actions = actions->next;
1514 groups = groups->next;
1517 list = g_list_sort(list, simple_sort_cb);
1522 gboolean defined_mouse_buttons(GtkWidget *, GdkEventButton *event, gpointer data)
1524 auto lw = static_cast<LayoutWindow *>(data);
1526 gboolean ret = FALSE;
1528 switch (event->button)
1530 case MOUSE_BUTTON_8:
1531 if (options->mouse_button_8)
1533 if (g_strstr_len(options->mouse_button_8, -1, ".desktop") != nullptr)
1535 file_util_start_editor_from_filelist(options->mouse_button_8, layout_selection_list(lw), layout_get_path(lw), lw->window);
1540 action = gtk_action_group_get_action(lw->action_group, options->mouse_button_8);
1543 gtk_action_activate(action);
1549 case MOUSE_BUTTON_9:
1550 if (options->mouse_button_9)
1552 if (g_strstr_len(options->mouse_button_9, -1, ".desktop") != nullptr)
1554 file_util_start_editor_from_filelist(options->mouse_button_9, layout_selection_list(lw), layout_get_path(lw), lw->window);
1558 action = gtk_action_group_get_action(lw->action_group, options->mouse_button_9);
1562 gtk_action_activate(action);
1575 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */