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.
31 #include "history-list.h"
33 #include "main-defines.h"
40 *-----------------------------------------------------------------------------
41 * widget and layout utilities
42 *-----------------------------------------------------------------------------
45 GtkWidget *pref_box_new(GtkWidget *parent_box, gboolean fill,
46 GtkOrientation orientation, gboolean padding)
50 if (orientation == GTK_ORIENTATION_HORIZONTAL)
52 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, padding);
56 box = gtk_box_new(GTK_ORIENTATION_VERTICAL, padding);
59 gq_gtk_box_pack_start(GTK_BOX(parent_box), box, fill, fill, 0);
65 GtkWidget *pref_group_new(GtkWidget *parent_box, gboolean fill,
66 const gchar *text, GtkOrientation orientation)
73 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
75 /* add additional spacing if necessary */
76 if (GTK_IS_VBOX(parent_box))
78 GList *list = gtk_container_get_children(GTK_CONTAINER(parent_box));
81 pref_spacer(vbox, PREF_PAD_GROUP - PREF_PAD_GAP);
86 gq_gtk_box_pack_start(GTK_BOX(parent_box), vbox, fill, fill, 0);
87 gtk_widget_show(vbox);
89 label = gtk_label_new(text);
90 gtk_label_set_xalign(GTK_LABEL(label), 0.0);
91 gtk_label_set_yalign(GTK_LABEL(label), 0.5);
92 pref_label_bold(label, TRUE, FALSE);
94 gq_gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
95 gtk_widget_show(label);
97 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_INDENT);
98 gq_gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 0);
99 gtk_widget_show(hbox);
101 /* indent using empty box */
102 pref_spacer(hbox, 0);
104 if (orientation == GTK_ORIENTATION_HORIZONTAL)
106 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_SPACE);
110 box = gtk_box_new(GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
112 gq_gtk_box_pack_start(GTK_BOX(hbox), box, TRUE, TRUE, 0);
113 gtk_widget_show(box);
115 g_object_set_data(G_OBJECT(box), "pref_group", vbox);
120 GtkWidget *pref_group_parent(GtkWidget *child)
129 group = static_cast<GtkWidget *>(g_object_get_data(G_OBJECT(parent), "pref_group"));
130 if (group && GTK_IS_WIDGET(group)) return group;
132 parent = gtk_widget_get_parent(parent);
138 GtkWidget *pref_frame_new(GtkWidget *parent_box, gboolean fill,
140 GtkOrientation orientation, gboolean padding)
143 GtkWidget *frame = nullptr;
145 frame = gtk_frame_new(text);
146 gq_gtk_box_pack_start(GTK_BOX(parent_box), frame, fill, fill, 0);
147 gtk_widget_show(frame);
149 if (orientation == GTK_ORIENTATION_HORIZONTAL)
151 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, padding);
155 box = gtk_box_new(GTK_ORIENTATION_VERTICAL, padding);
157 gq_gtk_container_add(GTK_WIDGET(frame), box);
158 gtk_container_set_border_width(GTK_CONTAINER(box), PREF_PAD_BORDER);
159 gtk_widget_show(box);
164 GtkWidget *pref_spacer(GtkWidget *parent_box, gboolean padding)
168 spacer = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
169 gq_gtk_box_pack_start(GTK_BOX(parent_box), spacer, FALSE, FALSE, padding / 2);
170 gtk_widget_show(spacer);
175 GtkWidget *pref_line(GtkWidget *parent_box, gboolean padding)
179 spacer = gtk_separator_new(GTK_IS_HBOX(parent_box) ? GTK_ORIENTATION_VERTICAL : GTK_ORIENTATION_HORIZONTAL);
180 gq_gtk_box_pack_start(GTK_BOX(parent_box), spacer, FALSE, FALSE, padding / 2);
181 gtk_widget_show(spacer);
186 GtkWidget *pref_label_new(GtkWidget *parent_box, const gchar *text)
190 label = gtk_label_new(text);
191 gq_gtk_box_pack_start(GTK_BOX(parent_box), label, FALSE, FALSE, 0);
192 gtk_widget_show(label);
197 GtkWidget *pref_label_new_mnemonic(GtkWidget *parent_box, const gchar *text, GtkWidget *widget)
201 label = gtk_label_new_with_mnemonic(text);
202 gtk_label_set_mnemonic_widget(GTK_LABEL(label), widget);
203 gq_gtk_box_pack_start(GTK_BOX(parent_box), label, FALSE, FALSE, 0);
204 gtk_widget_show(label);
209 void pref_label_bold(GtkWidget *label, gboolean bold, gboolean increase_size)
214 if (!bold && !increase_size) return;
216 pal = pango_attr_list_new();
220 pa = pango_attr_weight_new(PANGO_WEIGHT_BOLD);
222 pa->end_index = G_MAXINT;
223 pango_attr_list_insert(pal, pa);
228 pa = pango_attr_scale_new(PANGO_SCALE_LARGE);
230 pa->end_index = G_MAXINT;
231 pango_attr_list_insert(pal, pa);
234 gtk_label_set_attributes(GTK_LABEL(label), pal);
235 pango_attr_list_unref(pal);
238 GtkWidget *pref_button_new(GtkWidget *parent_box, const gchar *icon_name,
239 const gchar *text, GCallback func, gpointer data)
245 button = gtk_button_new_from_icon_name(icon_name, GTK_ICON_SIZE_BUTTON);
249 button = gtk_button_new();
254 gtk_button_set_use_underline(GTK_BUTTON(button), TRUE);
255 gtk_button_set_label(GTK_BUTTON(button), text);
258 if (func) g_signal_connect(G_OBJECT(button), "clicked", func, data);
262 gq_gtk_box_pack_start(GTK_BOX(parent_box), button, FALSE, FALSE, 0);
263 gtk_widget_show(button);
269 static GtkWidget *real_pref_checkbox_new(GtkWidget *parent_box, const gchar *text, gboolean mnemonic_text,
270 gboolean active, GCallback func, gpointer data)
276 button = gtk_check_button_new_with_mnemonic(text);
280 button = gtk_check_button_new_with_label(text);
282 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), active);
283 if (func) g_signal_connect(G_OBJECT(button), "clicked", func, data);
285 gq_gtk_box_pack_start(GTK_BOX(parent_box), button, FALSE, FALSE, 0);
286 gtk_widget_show(button);
291 GtkWidget *pref_checkbox_new(GtkWidget *parent_box, const gchar *text, gboolean active,
292 GCallback func, gpointer data)
294 return real_pref_checkbox_new(parent_box, text, FALSE, active, func, data);
297 #pragma GCC diagnostic push
298 #pragma GCC diagnostic ignored "-Wunused-function"
299 GtkWidget *pref_checkbox_new_mnemonic_unused(GtkWidget *parent_box, const gchar *text, gboolean active,
300 GCallback func, gpointer data)
302 return real_pref_checkbox_new(parent_box, text, TRUE, active, func, data);
304 #pragma GCC diagnostic pop
306 static void pref_checkbox_int_cb(GtkWidget *widget, gpointer data)
308 auto result = static_cast<gboolean *>(data);
310 *result = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
313 GtkWidget *pref_checkbox_new_int(GtkWidget *parent_box, const gchar *text, gboolean active,
318 button = pref_checkbox_new(parent_box, text, active,
319 G_CALLBACK(pref_checkbox_int_cb), result);
325 static void pref_checkbox_link_sensitivity_cb(GtkWidget *button, gpointer data)
327 auto widget = static_cast<GtkWidget *>(data);
329 gtk_widget_set_sensitive(widget, gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button)));
332 void pref_checkbox_link_sensitivity(GtkWidget *button, GtkWidget *widget)
334 g_signal_connect(G_OBJECT(button), "toggled",
335 G_CALLBACK(pref_checkbox_link_sensitivity_cb), widget);
337 pref_checkbox_link_sensitivity_cb(button, widget);
340 #pragma GCC diagnostic push
341 #pragma GCC diagnostic ignored "-Wunused-function"
342 static void pref_checkbox_link_sensitivity_swap_cb_unused(GtkWidget *button, gpointer data)
344 auto *widget = static_cast<GtkWidget *>(data);
346 gtk_widget_set_sensitive(widget, !gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button)));
349 void pref_checkbox_link_sensitivity_swap_unused(GtkWidget *button, GtkWidget *widget)
351 g_signal_connect(G_OBJECT(button), "toggled",
352 G_CALLBACK(pref_checkbox_link_sensitivity_swap_cb_unused), widget);
354 pref_checkbox_link_sensitivity_swap_cb_unused(button, widget);
356 #pragma GCC diagnostic pop
358 static GtkWidget *real_pref_radiobutton_new(GtkWidget *parent_box, GtkWidget *sibling,
359 const gchar *text, gboolean mnemonic_text, gboolean active,
360 GCallback func, gpointer data)
364 GtkToggleButton *group;;
374 group = gtk_radio_button_get_group(GTK_RADIO_BUTTON(sibling));
385 button = gtk_toggle_button_new_with_mnemonic(text);
386 gtk_toggle_button_set_group(button, group);
388 button = gtk_radio_button_new_with_mnemonic(group, text);
394 button = gtk_toggle_button_new_with_label(text);
395 gtk_toggle_button_set_group(button, group);
397 button = gtk_radio_button_new_with_label(group, text);
401 if (active) gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), active);
402 if (func) g_signal_connect(G_OBJECT(button), "clicked", func, data);
404 gq_gtk_box_pack_start(GTK_BOX(parent_box), button, FALSE, FALSE, 0);
405 gtk_widget_show(button);
410 GtkWidget *pref_radiobutton_new(GtkWidget *parent_box, GtkWidget *sibling,
411 const gchar *text, gboolean active,
412 GCallback func, gpointer data)
414 return real_pref_radiobutton_new(parent_box, sibling, text, FALSE, active, func, data);
417 #pragma GCC diagnostic push
418 #pragma GCC diagnostic ignored "-Wunused-function"
419 GtkWidget *pref_radiobutton_new_mnemonic_unused(GtkWidget *parent_box, GtkWidget *sibling,
420 const gchar *text, gboolean active,
421 GCallback func, gpointer data)
423 return real_pref_radiobutton_new(parent_box, sibling, text, TRUE, active, func, data);
426 #define PREF_RADIO_VALUE_KEY "pref_radio_value"
428 static void pref_radiobutton_int_cb_unused(GtkWidget *widget, gpointer data)
430 auto *result = static_cast<gboolean *>(data);
432 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
434 *result = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), PREF_RADIO_VALUE_KEY));
438 GtkWidget *pref_radiobutton_new_int_unused(GtkWidget *parent_box, GtkWidget *sibling,
439 const gchar *text, gboolean active,
440 gboolean *result, gboolean value,
445 button = pref_radiobutton_new(parent_box, sibling, text, active,
446 G_CALLBACK(pref_radiobutton_int_cb_unused), result);
447 g_object_set_data(G_OBJECT(button), PREF_RADIO_VALUE_KEY, GINT_TO_POINTER(value));
448 if (active) *result = value;
452 #pragma GCC diagnostic pop
454 static GtkWidget *real_pref_spin_new(GtkWidget *parent_box, const gchar *text, const gchar *suffix,
455 gboolean mnemonic_text,
456 gdouble min, gdouble max, gdouble step, gint digits,
458 GCallback func, gpointer data)
464 box = pref_box_new(parent_box, FALSE, GTK_ORIENTATION_HORIZONTAL, PREF_PAD_SPACE);
466 spin = gtk_spin_button_new_with_range(min, max, step);
467 gtk_spin_button_set_digits(GTK_SPIN_BUTTON(spin), digits);
468 gtk_spin_button_set_value(GTK_SPIN_BUTTON(spin), value);
472 g_signal_connect(G_OBJECT(spin), "value_changed", G_CALLBACK(func), data);
479 label = pref_label_new_mnemonic(box, text, spin);
483 label = pref_label_new(box, text);
485 pref_link_sensitivity(label, spin);
488 gq_gtk_box_pack_start(GTK_BOX(box), spin, FALSE, FALSE, 0);
489 gtk_widget_show(spin);
491 /* perhaps this should only be PREF_PAD_GAP distance from spinbutton ? */
494 label = pref_label_new(box, suffix);
495 pref_link_sensitivity(label, spin);
501 GtkWidget *pref_spin_new(GtkWidget *parent_box, const gchar *text, const gchar *suffix,
502 gdouble min, gdouble max, gdouble step, gint digits,
504 GCallback func, gpointer data)
506 return real_pref_spin_new(parent_box, text, suffix, FALSE,
507 min, max, step, digits, value, func, data);
510 #pragma GCC diagnostic push
511 #pragma GCC diagnostic ignored "-Wunused-function"
512 GtkWidget *pref_spin_new_mnemonic_unused(GtkWidget *parent_box, const gchar *text, const gchar *suffix,
513 gdouble min, gdouble max, gdouble step, gint digits,
515 GCallback func, gpointer data)
517 return real_pref_spin_new(parent_box, text, suffix, TRUE,
518 min, max, step, digits, value, func, data);
520 #pragma GCC diagnostic pop
522 static void pref_spin_int_cb(GtkWidget *widget, gpointer data)
524 auto var = static_cast<gint *>(data);
525 *var = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(widget));
528 GtkWidget *pref_spin_new_int(GtkWidget *parent_box, const gchar *text, const gchar *suffix,
529 gint min, gint max, gint step,
530 gint value, gint *value_var)
533 return pref_spin_new(parent_box, text, suffix,
534 static_cast<gdouble>(min), static_cast<gdouble>(max), static_cast<gdouble>(step), 0,
536 G_CALLBACK(pref_spin_int_cb), value_var);
539 static void pref_link_sensitivity_cb(GtkWidget *watch, GtkStateType, gpointer data)
541 auto widget = static_cast<GtkWidget *>(data);
543 gtk_widget_set_sensitive(widget, gtk_widget_is_sensitive(watch));
546 void pref_link_sensitivity(GtkWidget *widget, GtkWidget *watch)
548 g_signal_connect(G_OBJECT(watch), "state_changed",
549 G_CALLBACK(pref_link_sensitivity_cb), widget);
552 void pref_signal_block_data(GtkWidget *widget, gpointer data)
554 g_signal_handlers_block_matched(widget, G_SIGNAL_MATCH_DATA,
555 0, 0, nullptr, nullptr, data);
558 void pref_signal_unblock_data(GtkWidget *widget, gpointer data)
560 g_signal_handlers_unblock_matched(widget, G_SIGNAL_MATCH_DATA,
561 0, 0, nullptr, nullptr, data);
564 GtkWidget *pref_table_new(GtkWidget *parent_box, gint, gint, gboolean, gboolean fill)
568 table = gtk_grid_new();
569 gtk_grid_set_row_spacing(GTK_GRID(table), PREF_PAD_GAP);
570 gtk_grid_set_column_spacing(GTK_GRID(table), PREF_PAD_SPACE);
574 gq_gtk_box_pack_start(GTK_BOX(parent_box), table, fill, fill, 0);
575 gtk_widget_show(table);
581 GtkWidget *pref_table_box(GtkWidget *table, gint column, gint row,
582 GtkOrientation orientation, const gchar *text)
589 shell = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
590 box = pref_group_new(shell, TRUE, text, orientation);
594 if (orientation == GTK_ORIENTATION_HORIZONTAL)
596 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_SPACE);
600 box = gtk_box_new(GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
605 gq_gtk_grid_attach(GTK_GRID(table), shell, column, column + 1, row, row + 1, static_cast<GtkAttachOptions>(GTK_EXPAND | GTK_FILL), static_cast<GtkAttachOptions>(0), 0, 0);
607 gtk_widget_show(shell);
612 GtkWidget *pref_table_label(GtkWidget *table, gint column, gint row,
613 const gchar *text, GtkAlign alignment)
617 label = gtk_label_new(text);
618 gtk_widget_set_halign(label, alignment);
619 gtk_widget_set_valign(label, GTK_ALIGN_CENTER);
620 gq_gtk_grid_attach(GTK_GRID(table), label, column, column + 1, row, row + 1, GTK_FILL, static_cast<GtkAttachOptions>(0), 0, 0);
621 gtk_widget_show(label);
626 GtkWidget *pref_table_button(GtkWidget *table, gint column, gint row,
627 const gchar *stock_id, const gchar *text,
628 GCallback func, gpointer data)
632 button = pref_button_new(nullptr, stock_id, text, func, data);
633 gq_gtk_grid_attach(GTK_GRID(table), button, column, column + 1, row, row + 1, GTK_FILL, static_cast<GtkAttachOptions>(0), 0, 0);
634 gtk_widget_show(button);
639 GtkWidget *pref_table_spin(GtkWidget *table, gint column, gint row,
640 const gchar *text, const gchar *suffix,
641 gdouble min, gdouble max, gdouble step, gint digits,
643 GCallback func, gpointer data)
649 spin = gtk_spin_button_new_with_range(min, max, step);
650 gtk_spin_button_set_digits(GTK_SPIN_BUTTON(spin), digits);
651 gtk_spin_button_set_value(GTK_SPIN_BUTTON(spin), value);
654 g_signal_connect(G_OBJECT(spin), "value_changed", G_CALLBACK(func), data);
659 label = pref_table_label(table, column, row, text, GTK_ALIGN_END);
660 pref_link_sensitivity(label, spin);
666 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_SPACE);
667 gq_gtk_box_pack_start(GTK_BOX(box), spin, FALSE, FALSE, 0);
668 gtk_widget_show(spin);
670 label = pref_label_new(box, suffix);
671 pref_link_sensitivity(label, spin);
678 gq_gtk_grid_attach(GTK_GRID(table), box, column, column + 1, row, row + 1, static_cast<GtkAttachOptions>(GTK_EXPAND | GTK_FILL), static_cast<GtkAttachOptions>(GTK_EXPAND | GTK_FILL), 0, 0);
679 gtk_widget_show(box);
684 GtkWidget *pref_table_spin_new_int(GtkWidget *table, gint column, gint row,
685 const gchar *text, const gchar *suffix,
686 gint min, gint max, gint step,
687 gint value, gint *value_var)
690 return pref_table_spin(table, column, row,
692 static_cast<gdouble>(min), static_cast<gdouble>(max), static_cast<gdouble>(step), 0,
694 G_CALLBACK(pref_spin_int_cb), value_var);
698 GtkWidget *pref_toolbar_new(GtkWidget *parent_box)
702 tbar = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
706 gq_gtk_box_pack_start(GTK_BOX(parent_box), tbar, FALSE, FALSE, 0);
707 gtk_widget_show(tbar);
712 GtkWidget *pref_toolbar_button(GtkWidget *toolbar,
713 const gchar *icon_name, const gchar *label, gboolean toggle,
714 const gchar *description,
715 GCallback func, gpointer data)
719 if (toggle) // TODO: TG seems no function uses toggle now
721 item = GTK_WIDGET(gtk_toggle_tool_button_new());
722 if (icon_name) gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(item), icon_name);
723 if (label) gtk_tool_button_set_label(GTK_TOOL_BUTTON(item), label);
727 GtkWidget *icon = nullptr;
730 icon = gtk_image_new_from_icon_name(icon_name, GTK_ICON_SIZE_LARGE_TOOLBAR); // TODO: TG which size?
731 gtk_widget_show(icon);
733 item = GTK_WIDGET(gtk_tool_button_new(icon, label));
735 gtk_tool_button_set_use_underline(GTK_TOOL_BUTTON(item), TRUE);
737 if (func) g_signal_connect(item, "clicked", func, data);
738 gq_gtk_container_add(GTK_WIDGET(toolbar), item);
739 gtk_widget_show(item);
743 gtk_widget_set_tooltip_text(item, description);
749 #pragma GCC diagnostic push
750 #pragma GCC diagnostic ignored "-Wunused-function"
751 void pref_toolbar_button_set_icon_unused(GtkWidget *button, GtkWidget *widget, const gchar *stock_id)
755 gtk_tool_button_set_icon_widget(GTK_TOOL_BUTTON(button), widget);
759 gtk_tool_button_set_stock_id(GTK_TOOL_BUTTON(button), stock_id);
763 GtkWidget *pref_toolbar_spacer_unused(GtkWidget *toolbar)
767 item = GTK_WIDGET(gtk_separator_tool_item_new());
768 gq_gtk_container_add(GTK_WIDGET(toolbar), item);
769 gtk_widget_show(item);
773 #pragma GCC diagnostic pop
777 *-----------------------------------------------------------------------------
778 * date selection entry
779 *-----------------------------------------------------------------------------
782 #define DATE_SELECION_KEY "date_selection_data"
800 static void date_selection_popup_hide(DateSelection *ds)
802 if (!ds->window) return;
804 if (gtk_widget_has_grab(ds->window))
806 gtk_grab_remove(ds->window);
807 gdk_keyboard_ungrab(GDK_CURRENT_TIME);
808 gdk_pointer_ungrab(GDK_CURRENT_TIME);
811 gtk_widget_hide(ds->window);
813 g_object_unref(ds->window);
814 ds->window = nullptr;
815 ds->calendar = nullptr;
817 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ds->button), FALSE);
820 static gboolean date_selection_popup_release_cb(GtkWidget *, GdkEventButton *, gpointer data)
822 auto ds = static_cast<DateSelection *>(data);
824 date_selection_popup_hide(ds);
828 static gboolean date_selection_popup_press_cb(GtkWidget *, GdkEventButton *event, gpointer data)
830 auto ds = static_cast<DateSelection *>(data);
839 xr = static_cast<gint>(event->x_root);
840 yr = static_cast<gint>(event->y_root);
842 window = gtk_widget_get_window(ds->window);
843 gdk_window_get_origin(window, &x, &y);
844 w = gdk_window_get_width(window);
845 h = gdk_window_get_height(window);
847 if (xr < x || yr < y || xr > x + w || yr > y + h)
849 g_signal_connect(G_OBJECT(ds->window), "button_release_event",
850 G_CALLBACK(date_selection_popup_release_cb), ds);
857 static void date_selection_popup_sync(DateSelection *ds)
864 GDateTime *date_selected;
866 date_selected = gtk_calendar_get_date(GTK_CALENDAR(ds->calendar));
867 g_date_time_get_ymd(date_selected, static_cast<guint>(&year), static_cast<guint>(&month), static_cast<guint>(&day));
869 g_date_time_unref(date_selected);
871 gtk_calendar_get_date(GTK_CALENDAR(ds->calendar), &year, &month, &day);
872 /* month is range 0 to 11 */
875 date_selection_set(ds->box, day, month, year);
878 static gboolean date_selection_popup_keypress_cb(GtkWidget *, GdkEventKey *event, gpointer data)
880 auto ds = static_cast<DateSelection *>(data);
882 switch (event->keyval)
885 case GDK_KEY_KP_Enter:
887 case GDK_KEY_ISO_Left_Tab:
888 date_selection_popup_sync(ds);
889 date_selection_popup_hide(ds);
892 date_selection_popup_hide(ds);
901 static void date_selection_day_cb(GtkWidget *, gpointer data)
903 auto ds = static_cast<DateSelection *>(data);
905 date_selection_popup_sync(ds);
908 static void date_selection_doubleclick_cb(GtkWidget *, gpointer data)
910 auto ds = static_cast<DateSelection *>(data);
912 date_selection_popup_hide(ds);
915 static void date_selection_popup(DateSelection *ds)
922 GtkAllocation button_allocation;
923 GtkAllocation window_allocation;
925 if (ds->window) return;
927 ds->window = gtk_window_new(GTK_WINDOW_POPUP);
928 gtk_window_set_resizable(GTK_WINDOW(ds->window), FALSE);
929 g_signal_connect(G_OBJECT(ds->window), "button_press_event",
930 G_CALLBACK(date_selection_popup_press_cb), ds);
931 g_signal_connect(G_OBJECT(ds->window), "key_press_event",
932 G_CALLBACK(date_selection_popup_keypress_cb), ds);
934 ds->calendar = gtk_calendar_new();
935 gq_gtk_container_add(GTK_WIDGET(ds->window), ds->calendar);
936 gtk_widget_show(ds->calendar);
938 date = date_selection_get(ds->box);
940 gtk_calendar_select_day(GTK_CALENDAR(ds->calendar), date);
942 gtk_calendar_select_month(GTK_CALENDAR(ds->calendar), g_date_time_get_month(date), g_date_time_get_year(date));
943 gtk_calendar_select_day(GTK_CALENDAR(ds->calendar), g_date_time_get_day_of_month(date));
945 g_date_time_unref(date);
947 g_signal_connect(G_OBJECT(ds->calendar), "day_selected",
948 G_CALLBACK(date_selection_day_cb), ds);
949 g_signal_connect(G_OBJECT(ds->calendar), "day_selected_double_click",
950 G_CALLBACK(date_selection_doubleclick_cb), ds);
952 gtk_widget_realize(ds->window);
954 gdk_window_get_origin(gtk_widget_get_window(ds->button), &wx, &wy);
956 gtk_widget_get_allocation(ds->button, &button_allocation);
957 gtk_widget_get_allocation(ds->window, &window_allocation);
959 x = wx + button_allocation.x + button_allocation.width - window_allocation.width;
960 y = wy + button_allocation.y + button_allocation.height;
962 if (y + window_allocation.height > gdk_screen_height())
964 y = wy + button_allocation.y - window_allocation.height;
969 gq_gtk_window_move(GTK_WINDOW(ds->window), x, y);
970 gtk_widget_show(ds->window);
972 gtk_widget_grab_focus(ds->calendar);
973 gdk_pointer_grab(gtk_widget_get_window(ds->window), TRUE,
974 static_cast<GdkEventMask>(GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_MOTION_MASK),
975 nullptr, nullptr, GDK_CURRENT_TIME);
976 gdk_keyboard_grab(gtk_widget_get_window(ds->window), TRUE, GDK_CURRENT_TIME);
977 gtk_grab_add(ds->window);
979 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ds->button), TRUE);
982 static void date_selection_button_cb(GtkWidget *, gpointer data)
984 auto ds = static_cast<DateSelection *>(data);
986 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(ds->button)) == (!ds->window))
988 date_selection_popup(ds);
992 static void button_size_allocate_cb(GtkWidget *button, GtkAllocation *allocation, gpointer data)
994 auto spin = static_cast<GtkWidget *>(data);
995 GtkRequisition spin_requisition;
996 gtk_widget_get_requisition(spin, &spin_requisition);
998 if (allocation->height > spin_requisition.height)
1000 GtkAllocation button_allocation;
1001 GtkAllocation spin_allocation;
1003 gtk_widget_get_allocation(button, &button_allocation);
1004 gtk_widget_get_allocation(spin, &spin_allocation);
1005 button_allocation.height = spin_requisition.height;
1006 button_allocation.y = spin_allocation.y +
1007 (spin_allocation.height - spin_requisition.height) / 2;
1008 gtk_widget_size_allocate(button, &button_allocation);
1012 static void spin_increase(GtkWidget *spin, gint value)
1016 gtk_widget_size_request(spin, &req);
1017 gtk_widget_set_size_request(spin, req.width + value, -1);
1020 static void date_selection_destroy_cb(GtkWidget *, gpointer data)
1022 auto ds = static_cast<DateSelection *>(data);
1024 date_selection_popup_hide(ds);
1029 GtkWidget *date_selection_new()
1034 ds = g_new0(DateSelection, 1);
1038 ds->box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
1039 g_signal_connect(G_OBJECT(ds->box), "destroy",
1040 G_CALLBACK(date_selection_destroy_cb), ds);
1042 date_format = nl_langinfo(D_FMT);
1044 if (strlen(date_format) == 8)
1046 for (i=1; i<8; i=i+3)
1048 switch (date_format[i])
1051 ds->spin_d = pref_spin_new(ds->box, nullptr, nullptr, 1, 31, 1, 0, 1, nullptr, nullptr);
1054 ds->spin_m = pref_spin_new(ds->box, nullptr, nullptr, 1, 12, 1, 0, 1, nullptr, nullptr);
1057 ds->spin_y = pref_spin_new(ds->box, nullptr, nullptr, 1900, 9999, 1, 0, 1900, nullptr, nullptr);
1060 log_printf("Warning: Date locale %s is unknown", date_format);
1067 ds->spin_m = pref_spin_new(ds->box, nullptr, nullptr, 1, 12, 1, 0, 1, nullptr, nullptr);
1068 ds->spin_d = pref_spin_new(ds->box, nullptr, nullptr, 1, 31, 1, 0, 1, nullptr, nullptr);
1069 ds->spin_y = pref_spin_new(ds->box, nullptr, nullptr, 1900, 9999, 1, 0, 1900, nullptr, nullptr);
1072 spin_increase(ds->spin_y, 5);
1074 ds->button = gtk_toggle_button_new();
1075 g_signal_connect(G_OBJECT(ds->button), "size_allocate",
1076 G_CALLBACK(button_size_allocate_cb), ds->spin_y);
1078 icon = gtk_image_new_from_icon_name(GQ_ICON_PAN_DOWN, GTK_ICON_SIZE_BUTTON);
1079 gq_gtk_container_add(GTK_WIDGET(ds->button), icon);
1080 gtk_widget_show(icon);
1082 gq_gtk_box_pack_start(GTK_BOX(ds->box), ds->button, FALSE, FALSE, 0);
1083 g_signal_connect(G_OBJECT(ds->button), "clicked",
1084 G_CALLBACK(date_selection_button_cb), ds);
1085 gtk_widget_show(ds->button);
1087 g_object_set_data(G_OBJECT(ds->box), DATE_SELECION_KEY, ds);
1092 void date_selection_set(GtkWidget *widget, gint day, gint month, gint year)
1096 ds = static_cast<DateSelection *>(g_object_get_data(G_OBJECT(widget), DATE_SELECION_KEY));
1099 gtk_spin_button_set_value(GTK_SPIN_BUTTON(ds->spin_d), static_cast<gdouble>(day));
1100 gtk_spin_button_set_value(GTK_SPIN_BUTTON(ds->spin_m), static_cast<gdouble>(month));
1101 gtk_spin_button_set_value(GTK_SPIN_BUTTON(ds->spin_y), static_cast<gdouble>(year));
1105 * @brief Returns date structure set to value of spin buttons
1106 * @param widget #DateSelection
1109 * Free returned structure with g_date_time_unref();
1111 GDateTime *date_selection_get(GtkWidget *widget)
1119 ds = static_cast<DateSelection *>(g_object_get_data(G_OBJECT(widget), DATE_SELECION_KEY));
1125 day = gtk_spin_button_get_value(GTK_SPIN_BUTTON(ds->spin_d));
1126 month = gtk_spin_button_get_value(GTK_SPIN_BUTTON(ds->spin_m));
1127 year = gtk_spin_button_get_value(GTK_SPIN_BUTTON(ds->spin_y));
1129 date = g_date_time_new_local(year, month, day, 0, 0, 0);
1134 void date_selection_time_set(GtkWidget *widget, time_t t)
1141 date_selection_set(widget, lt->tm_mday, lt->tm_mon + 1, lt->tm_year + 1900);
1144 #pragma GCC diagnostic push
1145 #pragma GCC diagnostic ignored "-Wunused-function"
1146 time_t date_selection_time_get_unused(GtkWidget *widget)
1153 date_selection_get(widget);
1159 lt.tm_mon = month - 1;
1160 lt.tm_year = year - 1900;
1165 #pragma GCC diagnostic pop
1168 *-----------------------------------------------------------------------------
1169 * storing data in a history list with key,data pairs
1170 *-----------------------------------------------------------------------------
1173 #define PREF_LIST_MARKER_INT "[INT]:"
1174 #define PREF_LIST_MARKER_DOUBLE "[DOUBLE]:"
1175 #define PREF_LIST_MARKER_STRING "[STRING]:"
1177 static GList *pref_list_find(const gchar *group, const gchar *token)
1184 work = history_list_get_by_key(group);
1187 auto text = static_cast<const gchar *>(work->data);
1189 if (strncmp(text, token, l) == 0) return work;
1197 static gboolean pref_list_get(const gchar *group, const gchar *key, const gchar *marker, const gchar **result)
1203 if (!group || !key || !marker)
1209 token = g_strconcat(key, marker, NULL);
1211 work = pref_list_find(group, token);
1214 *result = static_cast<const gchar *>(work->data) + strlen(token);
1215 if (strlen(*result) == 0) *result = nullptr;
1229 static void pref_list_set(const gchar *group, const gchar *key, const gchar *marker, const gchar *text)
1235 if (!group || !key || !marker) return;
1237 token = g_strconcat(key, marker, NULL);
1238 path = g_strconcat(token, text, NULL);
1240 work = pref_list_find(group, token);
1243 auto old_path = static_cast<gchar *>(work->data);
1254 history_list_item_remove(group, old_path);
1259 history_list_add_to_key(group, path, 0);
1266 void pref_list_int_set(const gchar *group, const gchar *key, gint value)
1270 text = g_strdup_printf("%d", value);
1271 pref_list_set(group, key, PREF_LIST_MARKER_INT, text);
1275 gboolean pref_list_int_get(const gchar *group, const gchar *key, gint *result)
1285 if (pref_list_get(group, key, PREF_LIST_MARKER_INT, &text) && text)
1287 *result = static_cast<gint>(strtol(text, nullptr, 10));
1295 #pragma GCC diagnostic push
1296 #pragma GCC diagnostic ignored "-Wunused-function"
1297 void pref_list_double_set_unused(const gchar *group, const gchar *key, gdouble value)
1299 gchar text[G_ASCII_DTOSTR_BUF_SIZE];
1301 g_ascii_dtostr(text, sizeof(text), value);
1302 pref_list_set(group, key, PREF_LIST_MARKER_DOUBLE, text);
1305 gboolean pref_list_double_get_unused(const gchar *group, const gchar *key, gdouble *result)
1315 if (pref_list_get(group, key, PREF_LIST_MARKER_DOUBLE, &text) && text)
1317 *result = g_ascii_strtod(text, nullptr);
1325 void pref_list_string_set_unused(const gchar *group, const gchar *key, const gchar *value)
1327 pref_list_set(group, key, PREF_LIST_MARKER_STRING, value);
1330 gboolean pref_list_string_get_unused(const gchar *group, const gchar *key, const gchar **result)
1332 return pref_list_get(group, key, PREF_LIST_MARKER_STRING, result);
1334 #pragma GCC diagnostic pop
1336 void pref_color_button_set_cb(GtkWidget *widget, gpointer data)
1338 auto color = static_cast<GdkRGBA *>(data);
1340 gtk_color_chooser_get_rgba(GTK_COLOR_CHOOSER(widget), color);
1343 GtkWidget *pref_color_button_new(GtkWidget *parent_box, const gchar *title, GdkRGBA *color, GCallback func, gpointer data)
1349 button = gtk_color_button_new_with_rgba(color);
1353 button = gtk_color_button_new();
1356 if (func) g_signal_connect(G_OBJECT(button), "color-set", func, data);
1363 gtk_color_button_set_title(GTK_COLOR_BUTTON(button), title);
1364 label = gtk_label_new(title);
1366 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
1367 gq_gtk_box_pack_start(GTK_BOX(parent_box), hbox, TRUE, TRUE, 0);
1369 gq_gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 0);
1370 gq_gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
1372 gq_gtk_widget_show_all(hbox);
1376 gtk_widget_show(button);
1383 *-----------------------------------------------------------------------------
1385 *-----------------------------------------------------------------------------
1388 gchar *text_widget_text_pull(GtkWidget *text_widget)
1390 if (GTK_IS_TEXT_VIEW(text_widget))
1392 GtkTextBuffer *buffer;
1396 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_widget));
1397 gtk_text_buffer_get_bounds(buffer, &start, &end);
1399 return gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
1402 if (GTK_IS_ENTRY(text_widget))
1404 return g_strdup(gq_gtk_entry_get_text(GTK_ENTRY(text_widget)));
1412 gchar *text_widget_text_pull_selected(GtkWidget *text_widget)
1414 if (GTK_IS_TEXT_VIEW(text_widget))
1416 GtkTextBuffer *buffer;
1420 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_widget));
1421 gtk_text_buffer_get_bounds(buffer, &start, &end);
1423 if (gtk_text_buffer_get_selection_bounds(buffer, &start, &end))
1425 gtk_text_iter_set_line_offset(&start, 0);
1426 gtk_text_iter_forward_to_line_end(&end);
1429 return gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
1432 if (GTK_IS_ENTRY(text_widget))
1434 return g_strdup(gq_gtk_entry_get_text(GTK_ENTRY(text_widget)));
1441 static gint simple_sort_cb(gconstpointer a, gconstpointer b)
1443 const ActionItem *a_action;
1444 const ActionItem *b_action;
1446 a_action = static_cast<const ActionItem *>(a);
1447 b_action = static_cast<const ActionItem *>(b);
1449 return g_strcmp0(a_action->name, b_action->name);
1452 void free_action_items_cb(gpointer data)
1454 ActionItem *action_item;
1456 action_item = static_cast<ActionItem *>(data);
1457 g_free((gchar *)action_item->icon_name);
1458 g_free((gchar *)action_item->name);
1459 g_free((gchar *)action_item->label);
1460 g_free(action_item);
1463 void action_items_free(GList *list)
1465 g_list_free_full(list, free_action_items_cb);
1469 * @brief Get a list of menu actions
1471 * @returns GList ActionItem
1473 * Free returned list with action_items_free(list)
1475 * The list generated is used in the --remote --action-list command and
1476 * programmable mouse buttons 8 and 9.
1478 GList* get_action_items()
1480 ActionItem *action_item;
1481 const gchar *accel_path;
1488 GList *list_duplicates = nullptr;
1489 GList *list_unique = nullptr;
1493 LayoutWindow *lw = nullptr;
1495 if (!layout_valid(&lw))
1500 groups = gtk_ui_manager_get_action_groups(lw->ui_manager);
1503 actions = gtk_action_group_list_actions(GTK_ACTION_GROUP(groups->data));
1506 action = GTK_ACTION(actions->data);
1507 accel_path = gtk_action_get_accel_path(action);
1509 if (accel_path && gtk_accel_map_lookup_entry(accel_path, nullptr))
1511 g_object_get(action, "tooltip", &tooltip, "label", &label, NULL);
1513 action_name = g_path_get_basename(accel_path);
1515 /* Menu actions are irrelevant */
1516 if (g_strstr_len(action_name, -1, "Menu") == nullptr)
1518 action_item = g_new0(ActionItem, 1);
1520 /* .desktop items need the program name, Geeqie menu items need the tooltip */
1521 if (g_strstr_len(action_name, -1, ".desktop") == nullptr)
1524 /* Tooltips with newlines affect output format */
1525 if (tooltip && (g_strstr_len(tooltip, -1, "\n") == nullptr) )
1527 action_item->label = g_strdup(tooltip);
1531 action_item->label = g_strdup(label);
1536 action_item->label = g_strdup(label);
1539 action_item->name = action_name;
1540 action_item->icon_name = g_strdup(gtk_action_get_stock_id(action));
1542 list_duplicates = g_list_prepend(list_duplicates, action_item);
1545 actions = actions->next;
1548 groups = groups->next;
1551 /* Use the shortest name i.e. ignore -Alt versions. Sort makes the shortest first in the list */
1552 list_duplicates = g_list_sort(list_duplicates, simple_sort_cb);
1554 /* Ignore duplicate entries */
1555 work1 = list_duplicates;
1559 work2 = list_unique;
1560 /* The first entry must be unique, list_unique is null so control bypasses the while */
1563 if (g_strcmp0(static_cast<ActionItem *>(work2->data)->label, static_cast<ActionItem *>(work1->data)->label) == 0)
1568 work2 = work2->next;
1573 action_item = g_new0(ActionItem, 1);
1574 action_item->name = g_strdup(static_cast<ActionItem *>(work1->data)->name);
1575 action_item->label = g_strdup(static_cast<ActionItem *>(work1->data)->label);
1576 action_item->icon_name = g_strdup(static_cast<ActionItem *>(work1->data)->icon_name);
1577 list_unique = g_list_append(list_unique, action_item);
1579 work1 = work1->next;
1582 g_list_free_full(list_duplicates, free_action_items_cb);
1587 gboolean defined_mouse_buttons(GtkWidget *, GdkEventButton *event, gpointer data)
1589 auto lw = static_cast<LayoutWindow *>(data);
1591 gboolean ret = FALSE;
1593 switch (event->button)
1595 case MOUSE_BUTTON_8:
1596 if (options->mouse_button_8)
1598 if (g_strstr_len(options->mouse_button_8, -1, ".desktop") != nullptr)
1600 file_util_start_editor_from_filelist(options->mouse_button_8, layout_selection_list(lw), layout_get_path(lw), lw->window);
1605 action = gtk_action_group_get_action(lw->action_group, options->mouse_button_8);
1608 gtk_action_activate(action);
1614 case MOUSE_BUTTON_9:
1615 if (options->mouse_button_9)
1617 if (g_strstr_len(options->mouse_button_9, -1, ".desktop") != nullptr)
1619 file_util_start_editor_from_filelist(options->mouse_button_9, layout_selection_list(lw), layout_get_path(lw), lw->window);
1623 action = gtk_action_group_get_action(lw->action_group, options->mouse_button_9);
1627 gtk_action_activate(action);
1640 GdkPixbuf *gq_gtk_icon_theme_load_icon_copy(GtkIconTheme *icon_theme, const gchar *icon_name, gint size, GtkIconLookupFlags flags)
1642 GError *error = nullptr;
1643 GdkPixbuf *icon = gtk_icon_theme_load_icon(icon_theme, icon_name, size, flags, &error);
1644 if (error) return nullptr;
1646 GdkPixbuf *pixbuf = gdk_pixbuf_copy(icon);
1647 g_object_unref(icon);
1651 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */