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.
29 #include <pango/pango.h>
35 #include "history-list.h"
37 #include "main-defines.h"
44 *-----------------------------------------------------------------------------
45 * widget and layout utilities
46 *-----------------------------------------------------------------------------
49 GtkWidget *pref_box_new(GtkWidget *parent_box, gboolean fill,
50 GtkOrientation orientation, gboolean padding)
54 if (orientation == GTK_ORIENTATION_HORIZONTAL)
56 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, padding);
60 box = gtk_box_new(GTK_ORIENTATION_VERTICAL, padding);
63 gq_gtk_box_pack_start(GTK_BOX(parent_box), box, fill, fill, 0);
69 GtkWidget *pref_group_new(GtkWidget *parent_box, gboolean fill,
70 const gchar *text, GtkOrientation orientation)
77 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
79 /* add additional spacing if necessary */
80 if (GTK_IS_ORIENTABLE(parent_box) &&
81 gtk_orientable_get_orientation(GTK_ORIENTABLE(parent_box)) == GTK_ORIENTATION_VERTICAL)
83 GList *list = gtk_container_get_children(GTK_CONTAINER(parent_box));
86 pref_spacer(vbox, PREF_PAD_GROUP - PREF_PAD_GAP);
91 gq_gtk_box_pack_start(GTK_BOX(parent_box), vbox, fill, fill, 0);
92 gtk_widget_show(vbox);
94 label = gtk_label_new(text);
95 gtk_label_set_xalign(GTK_LABEL(label), 0.0);
96 gtk_label_set_yalign(GTK_LABEL(label), 0.5);
97 pref_label_bold(label, TRUE, FALSE);
99 gq_gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
100 gtk_widget_show(label);
102 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_INDENT);
103 gq_gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 0);
104 gtk_widget_show(hbox);
106 /* indent using empty box */
107 pref_spacer(hbox, 0);
109 if (orientation == GTK_ORIENTATION_HORIZONTAL)
111 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_SPACE);
115 box = gtk_box_new(GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
117 gq_gtk_box_pack_start(GTK_BOX(hbox), box, TRUE, TRUE, 0);
118 gtk_widget_show(box);
120 g_object_set_data(G_OBJECT(box), "pref_group", vbox);
125 GtkWidget *pref_group_parent(GtkWidget *child)
134 group = static_cast<GtkWidget *>(g_object_get_data(G_OBJECT(parent), "pref_group"));
135 if (group && GTK_IS_WIDGET(group)) return group;
137 parent = gtk_widget_get_parent(parent);
143 GtkWidget *pref_frame_new(GtkWidget *parent_box, gboolean fill,
145 GtkOrientation orientation, gboolean padding)
148 GtkWidget *frame = nullptr;
150 frame = gtk_frame_new(text);
151 gq_gtk_box_pack_start(GTK_BOX(parent_box), frame, fill, fill, 0);
152 gtk_widget_show(frame);
154 if (orientation == GTK_ORIENTATION_HORIZONTAL)
156 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, padding);
160 box = gtk_box_new(GTK_ORIENTATION_VERTICAL, padding);
162 gq_gtk_container_add(GTK_WIDGET(frame), box);
163 gtk_container_set_border_width(GTK_CONTAINER(box), PREF_PAD_BORDER);
164 gtk_widget_show(box);
169 GtkWidget *pref_spacer(GtkWidget *parent_box, gboolean padding)
173 spacer = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
174 gq_gtk_box_pack_start(GTK_BOX(parent_box), spacer, FALSE, FALSE, padding / 2);
175 gtk_widget_show(spacer);
180 GtkWidget *pref_line(GtkWidget *parent_box, gboolean padding)
182 GtkOrientation orientation;
185 orientation = gtk_orientable_get_orientation(GTK_ORIENTABLE(parent_box));
186 spacer = gtk_separator_new((orientation == GTK_ORIENTATION_HORIZONTAL) ? GTK_ORIENTATION_VERTICAL : GTK_ORIENTATION_HORIZONTAL);
187 gq_gtk_box_pack_start(GTK_BOX(parent_box), spacer, FALSE, FALSE, padding / 2);
188 gtk_widget_show(spacer);
193 GtkWidget *pref_label_new(GtkWidget *parent_box, const gchar *text)
197 label = gtk_label_new(text);
198 gq_gtk_box_pack_start(GTK_BOX(parent_box), label, FALSE, FALSE, 0);
199 gtk_widget_show(label);
204 GtkWidget *pref_label_new_mnemonic(GtkWidget *parent_box, const gchar *text, GtkWidget *widget)
208 label = gtk_label_new_with_mnemonic(text);
209 gtk_label_set_mnemonic_widget(GTK_LABEL(label), widget);
210 gq_gtk_box_pack_start(GTK_BOX(parent_box), label, FALSE, FALSE, 0);
211 gtk_widget_show(label);
216 void pref_label_bold(GtkWidget *label, gboolean bold, gboolean increase_size)
221 if (!bold && !increase_size) return;
223 pal = pango_attr_list_new();
227 pa = pango_attr_weight_new(PANGO_WEIGHT_BOLD);
229 pa->end_index = G_MAXINT;
230 pango_attr_list_insert(pal, pa);
235 pa = pango_attr_scale_new(PANGO_SCALE_LARGE);
237 pa->end_index = G_MAXINT;
238 pango_attr_list_insert(pal, pa);
241 gtk_label_set_attributes(GTK_LABEL(label), pal);
242 pango_attr_list_unref(pal);
245 GtkWidget *pref_button_new(GtkWidget *parent_box, const gchar *icon_name,
246 const gchar *text, GCallback func, gpointer data)
252 button = gtk_button_new_from_icon_name(icon_name, GTK_ICON_SIZE_BUTTON);
256 button = gtk_button_new();
261 gtk_button_set_use_underline(GTK_BUTTON(button), TRUE);
262 gtk_button_set_label(GTK_BUTTON(button), text);
265 if (func) g_signal_connect(G_OBJECT(button), "clicked", func, data);
269 gq_gtk_box_pack_start(GTK_BOX(parent_box), button, FALSE, FALSE, 0);
270 gtk_widget_show(button);
276 static GtkWidget *real_pref_checkbox_new(GtkWidget *parent_box, const gchar *text, gboolean mnemonic_text,
277 gboolean active, GCallback func, gpointer data)
283 button = gtk_check_button_new_with_mnemonic(text);
287 button = gtk_check_button_new_with_label(text);
289 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), active);
290 if (func) g_signal_connect(G_OBJECT(button), "clicked", func, data);
292 gq_gtk_box_pack_start(GTK_BOX(parent_box), button, FALSE, FALSE, 0);
293 gtk_widget_show(button);
298 GtkWidget *pref_checkbox_new(GtkWidget *parent_box, const gchar *text, gboolean active,
299 GCallback func, gpointer data)
301 return real_pref_checkbox_new(parent_box, text, FALSE, active, func, data);
304 #pragma GCC diagnostic push
305 #pragma GCC diagnostic ignored "-Wunused-function"
306 GtkWidget *pref_checkbox_new_mnemonic_unused(GtkWidget *parent_box, const gchar *text, gboolean active,
307 GCallback func, gpointer data)
309 return real_pref_checkbox_new(parent_box, text, TRUE, active, func, data);
311 #pragma GCC diagnostic pop
313 static void pref_checkbox_int_cb(GtkWidget *widget, gpointer data)
315 auto result = static_cast<gboolean *>(data);
317 *result = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
320 GtkWidget *pref_checkbox_new_int(GtkWidget *parent_box, const gchar *text, gboolean active,
325 button = pref_checkbox_new(parent_box, text, active,
326 G_CALLBACK(pref_checkbox_int_cb), result);
332 static void pref_checkbox_link_sensitivity_cb(GtkWidget *button, gpointer data)
334 auto widget = static_cast<GtkWidget *>(data);
336 gtk_widget_set_sensitive(widget, gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button)));
339 void pref_checkbox_link_sensitivity(GtkWidget *button, GtkWidget *widget)
341 g_signal_connect(G_OBJECT(button), "toggled",
342 G_CALLBACK(pref_checkbox_link_sensitivity_cb), widget);
344 pref_checkbox_link_sensitivity_cb(button, widget);
347 #pragma GCC diagnostic push
348 #pragma GCC diagnostic ignored "-Wunused-function"
349 static void pref_checkbox_link_sensitivity_swap_cb_unused(GtkWidget *button, gpointer data)
351 auto *widget = static_cast<GtkWidget *>(data);
353 gtk_widget_set_sensitive(widget, !gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button)));
356 void pref_checkbox_link_sensitivity_swap_unused(GtkWidget *button, GtkWidget *widget)
358 g_signal_connect(G_OBJECT(button), "toggled",
359 G_CALLBACK(pref_checkbox_link_sensitivity_swap_cb_unused), widget);
361 pref_checkbox_link_sensitivity_swap_cb_unused(button, widget);
363 #pragma GCC diagnostic pop
365 static GtkWidget *real_pref_radiobutton_new(GtkWidget *parent_box, GtkWidget *sibling,
366 const gchar *text, gboolean mnemonic_text, gboolean active,
367 GCallback func, gpointer data)
371 GtkToggleButton *group;;
381 group = gtk_radio_button_get_group(GTK_RADIO_BUTTON(sibling));
392 button = gtk_toggle_button_new_with_mnemonic(text);
393 gtk_toggle_button_set_group(button, group);
395 button = gtk_radio_button_new_with_mnemonic(group, text);
401 button = gtk_toggle_button_new_with_label(text);
402 gtk_toggle_button_set_group(button, group);
404 button = gtk_radio_button_new_with_label(group, text);
408 if (active) gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), active);
409 if (func) g_signal_connect(G_OBJECT(button), "clicked", func, data);
411 gq_gtk_box_pack_start(GTK_BOX(parent_box), button, FALSE, FALSE, 0);
412 gtk_widget_show(button);
417 GtkWidget *pref_radiobutton_new(GtkWidget *parent_box, GtkWidget *sibling,
418 const gchar *text, gboolean active,
419 GCallback func, gpointer data)
421 return real_pref_radiobutton_new(parent_box, sibling, text, FALSE, active, func, data);
424 #pragma GCC diagnostic push
425 #pragma GCC diagnostic ignored "-Wunused-function"
426 GtkWidget *pref_radiobutton_new_mnemonic_unused(GtkWidget *parent_box, GtkWidget *sibling,
427 const gchar *text, gboolean active,
428 GCallback func, gpointer data)
430 return real_pref_radiobutton_new(parent_box, sibling, text, TRUE, active, func, data);
433 #define PREF_RADIO_VALUE_KEY "pref_radio_value"
435 static void pref_radiobutton_int_cb_unused(GtkWidget *widget, gpointer data)
437 auto *result = static_cast<gboolean *>(data);
439 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
441 *result = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), PREF_RADIO_VALUE_KEY));
445 GtkWidget *pref_radiobutton_new_int_unused(GtkWidget *parent_box, GtkWidget *sibling,
446 const gchar *text, gboolean active,
447 gboolean *result, gboolean value,
452 button = pref_radiobutton_new(parent_box, sibling, text, active,
453 G_CALLBACK(pref_radiobutton_int_cb_unused), result);
454 g_object_set_data(G_OBJECT(button), PREF_RADIO_VALUE_KEY, GINT_TO_POINTER(value));
455 if (active) *result = value;
459 #pragma GCC diagnostic pop
461 static GtkWidget *real_pref_spin_new(GtkWidget *parent_box, const gchar *text, const gchar *suffix,
462 gboolean mnemonic_text,
463 gdouble min, gdouble max, gdouble step, gint digits,
465 GCallback func, gpointer data)
471 box = pref_box_new(parent_box, FALSE, GTK_ORIENTATION_HORIZONTAL, PREF_PAD_SPACE);
473 spin = gtk_spin_button_new_with_range(min, max, step);
474 gtk_spin_button_set_digits(GTK_SPIN_BUTTON(spin), digits);
475 gtk_spin_button_set_value(GTK_SPIN_BUTTON(spin), value);
479 g_signal_connect(G_OBJECT(spin), "value_changed", G_CALLBACK(func), data);
486 label = pref_label_new_mnemonic(box, text, spin);
490 label = pref_label_new(box, text);
492 pref_link_sensitivity(label, spin);
495 gq_gtk_box_pack_start(GTK_BOX(box), spin, FALSE, FALSE, 0);
496 gtk_widget_show(spin);
498 /* perhaps this should only be PREF_PAD_GAP distance from spinbutton ? */
501 label = pref_label_new(box, suffix);
502 pref_link_sensitivity(label, spin);
508 GtkWidget *pref_spin_new(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, FALSE,
514 min, max, step, digits, value, func, data);
517 #pragma GCC diagnostic push
518 #pragma GCC diagnostic ignored "-Wunused-function"
519 GtkWidget *pref_spin_new_mnemonic_unused(GtkWidget *parent_box, const gchar *text, const gchar *suffix,
520 gdouble min, gdouble max, gdouble step, gint digits,
522 GCallback func, gpointer data)
524 return real_pref_spin_new(parent_box, text, suffix, TRUE,
525 min, max, step, digits, value, func, data);
527 #pragma GCC diagnostic pop
529 static void pref_spin_int_cb(GtkWidget *widget, gpointer data)
531 auto var = static_cast<gint *>(data);
532 *var = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(widget));
535 GtkWidget *pref_spin_new_int(GtkWidget *parent_box, const gchar *text, const gchar *suffix,
536 gint min, gint max, gint step,
537 gint value, gint *value_var)
540 return pref_spin_new(parent_box, text, suffix,
541 static_cast<gdouble>(min), static_cast<gdouble>(max), static_cast<gdouble>(step), 0,
543 G_CALLBACK(pref_spin_int_cb), value_var);
546 static void pref_link_sensitivity_cb(GtkWidget *watch, GtkStateType, gpointer data)
548 auto widget = static_cast<GtkWidget *>(data);
550 gtk_widget_set_sensitive(widget, gtk_widget_is_sensitive(watch));
553 void pref_link_sensitivity(GtkWidget *widget, GtkWidget *watch)
555 g_signal_connect(G_OBJECT(watch), "state_changed",
556 G_CALLBACK(pref_link_sensitivity_cb), widget);
559 void pref_signal_block_data(GtkWidget *widget, gpointer data)
561 g_signal_handlers_block_matched(widget, G_SIGNAL_MATCH_DATA,
562 0, 0, nullptr, nullptr, data);
565 void pref_signal_unblock_data(GtkWidget *widget, gpointer data)
567 g_signal_handlers_unblock_matched(widget, G_SIGNAL_MATCH_DATA,
568 0, 0, nullptr, nullptr, data);
571 GtkWidget *pref_table_new(GtkWidget *parent_box, gint, gint, gboolean, gboolean fill)
575 table = gtk_grid_new();
576 gtk_grid_set_row_spacing(GTK_GRID(table), PREF_PAD_GAP);
577 gtk_grid_set_column_spacing(GTK_GRID(table), PREF_PAD_SPACE);
581 gq_gtk_box_pack_start(GTK_BOX(parent_box), table, fill, fill, 0);
582 gtk_widget_show(table);
588 GtkWidget *pref_table_box(GtkWidget *table, gint column, gint row,
589 GtkOrientation orientation, const gchar *text)
596 shell = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
597 box = pref_group_new(shell, TRUE, text, orientation);
601 if (orientation == GTK_ORIENTATION_HORIZONTAL)
603 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_SPACE);
607 box = gtk_box_new(GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
612 gq_gtk_grid_attach(GTK_GRID(table), shell, column, column + 1, row, row + 1, static_cast<GtkAttachOptions>(GTK_EXPAND | GTK_FILL), static_cast<GtkAttachOptions>(0), 0, 0);
614 gtk_widget_show(shell);
619 GtkWidget *pref_table_label(GtkWidget *table, gint column, gint row,
620 const gchar *text, GtkAlign alignment)
624 label = gtk_label_new(text);
625 gtk_widget_set_halign(label, alignment);
626 gtk_widget_set_valign(label, GTK_ALIGN_CENTER);
627 gq_gtk_grid_attach(GTK_GRID(table), label, column, column + 1, row, row + 1, GTK_FILL, static_cast<GtkAttachOptions>(0), 0, 0);
628 gtk_widget_show(label);
633 GtkWidget *pref_table_button(GtkWidget *table, gint column, gint row,
634 const gchar *stock_id, const gchar *text,
635 GCallback func, gpointer data)
639 button = pref_button_new(nullptr, stock_id, text, func, data);
640 gq_gtk_grid_attach(GTK_GRID(table), button, column, column + 1, row, row + 1, GTK_FILL, static_cast<GtkAttachOptions>(0), 0, 0);
641 gtk_widget_show(button);
646 GtkWidget *pref_table_spin(GtkWidget *table, gint column, gint row,
647 const gchar *text, const gchar *suffix,
648 gdouble min, gdouble max, gdouble step, gint digits,
650 GCallback func, gpointer data)
656 spin = gtk_spin_button_new_with_range(min, max, step);
657 gtk_spin_button_set_digits(GTK_SPIN_BUTTON(spin), digits);
658 gtk_spin_button_set_value(GTK_SPIN_BUTTON(spin), value);
661 g_signal_connect(G_OBJECT(spin), "value_changed", G_CALLBACK(func), data);
666 label = pref_table_label(table, column, row, text, GTK_ALIGN_END);
667 pref_link_sensitivity(label, spin);
673 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_SPACE);
674 gq_gtk_box_pack_start(GTK_BOX(box), spin, FALSE, FALSE, 0);
675 gtk_widget_show(spin);
677 label = pref_label_new(box, suffix);
678 pref_link_sensitivity(label, spin);
685 gq_gtk_grid_attach(GTK_GRID(table), box, column, column + 1, row, row + 1, static_cast<GtkAttachOptions>(GTK_EXPAND | GTK_FILL), static_cast<GtkAttachOptions>(GTK_EXPAND | GTK_FILL), 0, 0);
686 gtk_widget_show(box);
691 GtkWidget *pref_table_spin_new_int(GtkWidget *table, gint column, gint row,
692 const gchar *text, const gchar *suffix,
693 gint min, gint max, gint step,
694 gint value, gint *value_var)
697 return pref_table_spin(table, column, row,
699 static_cast<gdouble>(min), static_cast<gdouble>(max), static_cast<gdouble>(step), 0,
701 G_CALLBACK(pref_spin_int_cb), value_var);
705 GtkWidget *pref_toolbar_new(GtkWidget *parent_box)
709 tbar = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
713 gq_gtk_box_pack_start(GTK_BOX(parent_box), tbar, FALSE, FALSE, 0);
714 gtk_widget_show(tbar);
719 GtkWidget *pref_toolbar_button(GtkWidget *toolbar,
720 const gchar *icon_name, const gchar *label, gboolean toggle,
721 const gchar *description,
722 GCallback func, gpointer data)
726 if (toggle) // TODO: TG seems no function uses toggle now
728 item = GTK_WIDGET(gtk_toggle_tool_button_new());
729 if (icon_name) gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(item), icon_name);
730 if (label) gtk_tool_button_set_label(GTK_TOOL_BUTTON(item), label);
734 GtkWidget *icon = nullptr;
737 icon = gtk_image_new_from_icon_name(icon_name, GTK_ICON_SIZE_LARGE_TOOLBAR); // TODO: TG which size?
738 gtk_widget_show(icon);
740 item = GTK_WIDGET(gtk_tool_button_new(icon, label));
742 gtk_tool_button_set_use_underline(GTK_TOOL_BUTTON(item), TRUE);
744 if (func) g_signal_connect(item, "clicked", func, data);
745 gq_gtk_container_add(GTK_WIDGET(toolbar), item);
746 gtk_widget_show(item);
750 gtk_widget_set_tooltip_text(item, description);
756 #pragma GCC diagnostic push
757 #pragma GCC diagnostic ignored "-Wunused-function"
758 void pref_toolbar_button_set_icon_unused(GtkWidget *button, GtkWidget *widget, const gchar *icon_name)
762 gtk_tool_button_set_icon_widget(GTK_TOOL_BUTTON(button), widget);
766 gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(button), icon_name);
770 GtkWidget *pref_toolbar_spacer_unused(GtkWidget *toolbar)
774 item = GTK_WIDGET(gtk_separator_tool_item_new());
775 gq_gtk_container_add(GTK_WIDGET(toolbar), item);
776 gtk_widget_show(item);
780 #pragma GCC diagnostic pop
784 *-----------------------------------------------------------------------------
785 * date selection entry
786 *-----------------------------------------------------------------------------
789 #define DATE_SELECION_KEY "date_selection_data"
807 static void date_selection_popup_hide(DateSelection *ds)
809 if (!ds->window) return;
811 if (gtk_widget_has_grab(ds->window))
813 gtk_grab_remove(ds->window);
814 gdk_keyboard_ungrab(GDK_CURRENT_TIME);
815 gdk_pointer_ungrab(GDK_CURRENT_TIME);
818 gtk_widget_hide(ds->window);
820 gq_gtk_widget_destroy(ds->window);
821 ds->window = nullptr;
822 ds->calendar = nullptr;
824 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ds->button), FALSE);
827 static gboolean date_selection_popup_release_cb(GtkWidget *, GdkEventButton *, gpointer data)
829 auto ds = static_cast<DateSelection *>(data);
831 date_selection_popup_hide(ds);
835 static gboolean date_selection_popup_press_cb(GtkWidget *, GdkEventButton *event, gpointer data)
837 auto ds = static_cast<DateSelection *>(data);
846 xr = static_cast<gint>(event->x_root);
847 yr = static_cast<gint>(event->y_root);
849 window = gtk_widget_get_window(ds->window);
850 gdk_window_get_origin(window, &x, &y);
851 w = gdk_window_get_width(window);
852 h = gdk_window_get_height(window);
854 if (xr < x || yr < y || xr > x + w || yr > y + h)
856 g_signal_connect(G_OBJECT(ds->window), "button_release_event",
857 G_CALLBACK(date_selection_popup_release_cb), ds);
864 static void date_selection_popup_sync(DateSelection *ds)
871 GDateTime *date_selected;
873 date_selected = gtk_calendar_get_date(GTK_CALENDAR(ds->calendar));
874 g_date_time_get_ymd(date_selected, static_cast<guint>(&year), static_cast<guint>(&month), static_cast<guint>(&day));
876 g_date_time_unref(date_selected);
878 gtk_calendar_get_date(GTK_CALENDAR(ds->calendar), &year, &month, &day);
879 /* month is range 0 to 11 */
882 date_selection_set(ds->box, day, month, year);
885 static gboolean date_selection_popup_keypress_cb(GtkWidget *, GdkEventKey *event, gpointer data)
887 auto ds = static_cast<DateSelection *>(data);
889 switch (event->keyval)
892 case GDK_KEY_KP_Enter:
894 case GDK_KEY_ISO_Left_Tab:
895 date_selection_popup_sync(ds);
896 date_selection_popup_hide(ds);
899 date_selection_popup_hide(ds);
908 static void date_selection_day_cb(GtkWidget *, gpointer data)
910 auto ds = static_cast<DateSelection *>(data);
912 date_selection_popup_sync(ds);
915 static void date_selection_doubleclick_cb(GtkWidget *, gpointer data)
917 auto ds = static_cast<DateSelection *>(data);
919 date_selection_popup_hide(ds);
922 static void date_selection_popup(DateSelection *ds)
929 GtkAllocation button_allocation;
930 GtkAllocation window_allocation;
932 if (ds->window) return;
934 ds->window = gtk_window_new(GTK_WINDOW_POPUP);
935 gtk_window_set_resizable(GTK_WINDOW(ds->window), FALSE);
936 g_signal_connect(G_OBJECT(ds->window), "button_press_event",
937 G_CALLBACK(date_selection_popup_press_cb), ds);
938 g_signal_connect(G_OBJECT(ds->window), "key_press_event",
939 G_CALLBACK(date_selection_popup_keypress_cb), ds);
941 ds->calendar = gtk_calendar_new();
942 gq_gtk_container_add(GTK_WIDGET(ds->window), ds->calendar);
943 gtk_widget_show(ds->calendar);
945 date = date_selection_get(ds->box);
947 gtk_calendar_select_day(GTK_CALENDAR(ds->calendar), date);
949 gtk_calendar_select_month(GTK_CALENDAR(ds->calendar), g_date_time_get_month(date), g_date_time_get_year(date));
950 gtk_calendar_select_day(GTK_CALENDAR(ds->calendar), g_date_time_get_day_of_month(date));
952 g_date_time_unref(date);
954 g_signal_connect(G_OBJECT(ds->calendar), "day_selected",
955 G_CALLBACK(date_selection_day_cb), ds);
956 g_signal_connect(G_OBJECT(ds->calendar), "day_selected_double_click",
957 G_CALLBACK(date_selection_doubleclick_cb), ds);
959 gtk_widget_realize(ds->window);
961 gdk_window_get_origin(gtk_widget_get_window(ds->button), &wx, &wy);
963 gtk_widget_get_allocation(ds->button, &button_allocation);
964 gtk_widget_get_allocation(ds->window, &window_allocation);
966 x = wx + button_allocation.x + button_allocation.width - window_allocation.width;
967 y = wy + button_allocation.y + button_allocation.height;
969 if (y + window_allocation.height > gdk_screen_height())
971 y = wy + button_allocation.y - window_allocation.height;
976 gq_gtk_window_move(GTK_WINDOW(ds->window), x, y);
977 gtk_widget_show(ds->window);
979 gtk_widget_grab_focus(ds->calendar);
980 gdk_pointer_grab(gtk_widget_get_window(ds->window), TRUE,
981 static_cast<GdkEventMask>(GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_MOTION_MASK),
982 nullptr, nullptr, GDK_CURRENT_TIME);
983 gdk_keyboard_grab(gtk_widget_get_window(ds->window), TRUE, GDK_CURRENT_TIME);
984 gtk_grab_add(ds->window);
986 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ds->button), TRUE);
989 static void date_selection_button_cb(GtkWidget *, gpointer data)
991 auto ds = static_cast<DateSelection *>(data);
993 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(ds->button)) == (!ds->window))
995 date_selection_popup(ds);
999 static void button_size_allocate_cb(GtkWidget *button, GtkAllocation *allocation, gpointer data)
1001 auto spin = static_cast<GtkWidget *>(data);
1002 GtkRequisition spin_requisition;
1003 gtk_widget_get_requisition(spin, &spin_requisition);
1005 if (allocation->height > spin_requisition.height)
1007 GtkAllocation button_allocation;
1008 GtkAllocation spin_allocation;
1010 gtk_widget_get_allocation(button, &button_allocation);
1011 gtk_widget_get_allocation(spin, &spin_allocation);
1012 button_allocation.height = spin_requisition.height;
1013 button_allocation.y = spin_allocation.y +
1014 (spin_allocation.height - spin_requisition.height) / 2;
1015 gtk_widget_size_allocate(button, &button_allocation);
1019 static void spin_increase(GtkWidget *spin, gint value)
1023 gtk_widget_size_request(spin, &req);
1024 gtk_widget_set_size_request(spin, req.width + value, -1);
1027 static void date_selection_destroy_cb(GtkWidget *, gpointer data)
1029 auto ds = static_cast<DateSelection *>(data);
1031 date_selection_popup_hide(ds);
1036 GtkWidget *date_selection_new()
1041 ds = g_new0(DateSelection, 1);
1045 ds->box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
1046 g_signal_connect(G_OBJECT(ds->box), "destroy",
1047 G_CALLBACK(date_selection_destroy_cb), ds);
1049 date_format = nl_langinfo(D_FMT);
1051 if (strlen(date_format) == 8)
1053 for (i=1; i<8; i=i+3)
1055 switch (date_format[i])
1058 ds->spin_d = pref_spin_new(ds->box, nullptr, nullptr, 1, 31, 1, 0, 1, nullptr, nullptr);
1061 ds->spin_m = pref_spin_new(ds->box, nullptr, nullptr, 1, 12, 1, 0, 1, nullptr, nullptr);
1064 ds->spin_y = pref_spin_new(ds->box, nullptr, nullptr, 1900, 9999, 1, 0, 1900, nullptr, nullptr);
1067 log_printf("Warning: Date locale %s is unknown", date_format);
1074 ds->spin_m = pref_spin_new(ds->box, nullptr, nullptr, 1, 12, 1, 0, 1, nullptr, nullptr);
1075 ds->spin_d = pref_spin_new(ds->box, nullptr, nullptr, 1, 31, 1, 0, 1, nullptr, nullptr);
1076 ds->spin_y = pref_spin_new(ds->box, nullptr, nullptr, 1900, 9999, 1, 0, 1900, nullptr, nullptr);
1079 spin_increase(ds->spin_y, 5);
1081 ds->button = gtk_toggle_button_new();
1082 g_signal_connect(G_OBJECT(ds->button), "size_allocate",
1083 G_CALLBACK(button_size_allocate_cb), ds->spin_y);
1085 icon = gtk_image_new_from_icon_name(GQ_ICON_PAN_DOWN, GTK_ICON_SIZE_BUTTON);
1086 gq_gtk_container_add(GTK_WIDGET(ds->button), icon);
1087 gtk_widget_show(icon);
1089 gq_gtk_box_pack_start(GTK_BOX(ds->box), ds->button, FALSE, FALSE, 0);
1090 g_signal_connect(G_OBJECT(ds->button), "clicked",
1091 G_CALLBACK(date_selection_button_cb), ds);
1092 gtk_widget_show(ds->button);
1094 g_object_set_data(G_OBJECT(ds->box), DATE_SELECION_KEY, ds);
1099 void date_selection_set(GtkWidget *widget, gint day, gint month, gint year)
1103 ds = static_cast<DateSelection *>(g_object_get_data(G_OBJECT(widget), DATE_SELECION_KEY));
1106 gtk_spin_button_set_value(GTK_SPIN_BUTTON(ds->spin_d), static_cast<gdouble>(day));
1107 gtk_spin_button_set_value(GTK_SPIN_BUTTON(ds->spin_m), static_cast<gdouble>(month));
1108 gtk_spin_button_set_value(GTK_SPIN_BUTTON(ds->spin_y), static_cast<gdouble>(year));
1112 * @brief Returns date structure set to value of spin buttons
1113 * @param widget #DateSelection
1116 * Free returned structure with g_date_time_unref();
1118 GDateTime *date_selection_get(GtkWidget *widget)
1126 ds = static_cast<DateSelection *>(g_object_get_data(G_OBJECT(widget), DATE_SELECION_KEY));
1132 day = gtk_spin_button_get_value(GTK_SPIN_BUTTON(ds->spin_d));
1133 month = gtk_spin_button_get_value(GTK_SPIN_BUTTON(ds->spin_m));
1134 year = gtk_spin_button_get_value(GTK_SPIN_BUTTON(ds->spin_y));
1136 date = g_date_time_new_local(year, month, day, 0, 0, 0);
1141 void date_selection_time_set(GtkWidget *widget, time_t t)
1148 date_selection_set(widget, lt->tm_mday, lt->tm_mon + 1, lt->tm_year + 1900);
1151 #pragma GCC diagnostic push
1152 #pragma GCC diagnostic ignored "-Wunused-function"
1153 time_t date_selection_time_get_unused(GtkWidget *widget)
1160 date_selection_get(widget);
1166 lt.tm_mon = month - 1;
1167 lt.tm_year = year - 1900;
1172 #pragma GCC diagnostic pop
1175 *-----------------------------------------------------------------------------
1176 * storing data in a history list with key,data pairs
1177 *-----------------------------------------------------------------------------
1180 #define PREF_LIST_MARKER_INT "[INT]:"
1181 #define PREF_LIST_MARKER_DOUBLE "[DOUBLE]:"
1182 #define PREF_LIST_MARKER_STRING "[STRING]:"
1184 static GList *pref_list_find(const gchar *group, const gchar *token)
1191 work = history_list_get_by_key(group);
1194 auto text = static_cast<const gchar *>(work->data);
1196 if (strncmp(text, token, l) == 0) return work;
1204 static gboolean pref_list_get(const gchar *group, const gchar *key, const gchar *marker, const gchar **result)
1210 if (!group || !key || !marker)
1216 token = g_strconcat(key, marker, NULL);
1218 work = pref_list_find(group, token);
1221 *result = static_cast<const gchar *>(work->data) + strlen(token);
1222 if (strlen(*result) == 0) *result = nullptr;
1236 static void pref_list_set(const gchar *group, const gchar *key, const gchar *marker, const gchar *text)
1242 if (!group || !key || !marker) return;
1244 token = g_strconcat(key, marker, NULL);
1245 path = g_strconcat(token, text, NULL);
1247 work = pref_list_find(group, token);
1250 auto old_path = static_cast<gchar *>(work->data);
1261 history_list_item_remove(group, old_path);
1266 history_list_add_to_key(group, path, 0);
1273 void pref_list_int_set(const gchar *group, const gchar *key, gint value)
1277 text = g_strdup_printf("%d", value);
1278 pref_list_set(group, key, PREF_LIST_MARKER_INT, text);
1282 gboolean pref_list_int_get(const gchar *group, const gchar *key, gint *result)
1292 if (pref_list_get(group, key, PREF_LIST_MARKER_INT, &text) && text)
1294 *result = static_cast<gint>(strtol(text, nullptr, 10));
1302 #pragma GCC diagnostic push
1303 #pragma GCC diagnostic ignored "-Wunused-function"
1304 void pref_list_double_set_unused(const gchar *group, const gchar *key, gdouble value)
1306 gchar text[G_ASCII_DTOSTR_BUF_SIZE];
1308 g_ascii_dtostr(text, sizeof(text), value);
1309 pref_list_set(group, key, PREF_LIST_MARKER_DOUBLE, text);
1312 gboolean pref_list_double_get_unused(const gchar *group, const gchar *key, gdouble *result)
1322 if (pref_list_get(group, key, PREF_LIST_MARKER_DOUBLE, &text) && text)
1324 *result = g_ascii_strtod(text, nullptr);
1332 void pref_list_string_set_unused(const gchar *group, const gchar *key, const gchar *value)
1334 pref_list_set(group, key, PREF_LIST_MARKER_STRING, value);
1337 gboolean pref_list_string_get_unused(const gchar *group, const gchar *key, const gchar **result)
1339 return pref_list_get(group, key, PREF_LIST_MARKER_STRING, result);
1341 #pragma GCC diagnostic pop
1343 void pref_color_button_set_cb(GtkWidget *widget, gpointer data)
1345 auto color = static_cast<GdkRGBA *>(data);
1347 gtk_color_chooser_get_rgba(GTK_COLOR_CHOOSER(widget), color);
1350 GtkWidget *pref_color_button_new(GtkWidget *parent_box, const gchar *title, GdkRGBA *color, GCallback func, gpointer data)
1356 button = gtk_color_button_new_with_rgba(color);
1360 button = gtk_color_button_new();
1363 if (func) g_signal_connect(G_OBJECT(button), "color-set", func, data);
1370 gtk_color_button_set_title(GTK_COLOR_BUTTON(button), title);
1371 label = gtk_label_new(title);
1373 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
1374 gq_gtk_box_pack_start(GTK_BOX(parent_box), hbox, TRUE, TRUE, 0);
1376 gq_gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 0);
1377 gq_gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
1379 gq_gtk_widget_show_all(hbox);
1383 gtk_widget_show(button);
1390 *-----------------------------------------------------------------------------
1392 *-----------------------------------------------------------------------------
1395 gchar *text_widget_text_pull(GtkWidget *text_widget)
1397 if (GTK_IS_TEXT_VIEW(text_widget))
1399 GtkTextBuffer *buffer;
1403 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_widget));
1404 gtk_text_buffer_get_bounds(buffer, &start, &end);
1406 return gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
1409 if (GTK_IS_ENTRY(text_widget))
1411 return g_strdup(gq_gtk_entry_get_text(GTK_ENTRY(text_widget)));
1419 gchar *text_widget_text_pull_selected(GtkWidget *text_widget)
1421 if (GTK_IS_TEXT_VIEW(text_widget))
1423 GtkTextBuffer *buffer;
1427 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_widget));
1428 gtk_text_buffer_get_bounds(buffer, &start, &end);
1430 if (gtk_text_buffer_get_selection_bounds(buffer, &start, &end))
1432 gtk_text_iter_set_line_offset(&start, 0);
1433 gtk_text_iter_forward_to_line_end(&end);
1436 return gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
1439 if (GTK_IS_ENTRY(text_widget))
1441 return g_strdup(gq_gtk_entry_get_text(GTK_ENTRY(text_widget)));
1448 static gint simple_sort_cb(gconstpointer a, gconstpointer b)
1450 const ActionItem *a_action;
1451 const ActionItem *b_action;
1453 a_action = static_cast<const ActionItem *>(a);
1454 b_action = static_cast<const ActionItem *>(b);
1456 return g_strcmp0(a_action->name, b_action->name);
1459 void free_action_items_cb(gpointer data)
1461 ActionItem *action_item;
1463 action_item = static_cast<ActionItem *>(data);
1464 g_free((gchar *)action_item->icon_name);
1465 g_free((gchar *)action_item->name);
1466 g_free((gchar *)action_item->label);
1467 g_free(action_item);
1470 void action_items_free(GList *list)
1472 g_list_free_full(list, free_action_items_cb);
1476 * @brief Get a list of menu actions
1478 * @returns GList ActionItem
1480 * Free returned list with action_items_free(list)
1482 * The list generated is used in the --remote --action-list command and
1483 * programmable mouse buttons 8 and 9.
1485 GList* get_action_items()
1487 ActionItem *action_item;
1488 const gchar *accel_path;
1495 GList *list_duplicates = nullptr;
1496 GList *list_unique = nullptr;
1500 LayoutWindow *lw = nullptr;
1502 if (!layout_valid(&lw))
1507 groups = gtk_ui_manager_get_action_groups(lw->ui_manager);
1510 actions = gtk_action_group_list_actions(GTK_ACTION_GROUP(groups->data));
1513 action = GTK_ACTION(actions->data);
1514 accel_path = gtk_action_get_accel_path(action);
1516 if (accel_path && gtk_accel_map_lookup_entry(accel_path, nullptr))
1518 g_object_get(action, "tooltip", &tooltip, "label", &label, NULL);
1520 action_name = g_path_get_basename(accel_path);
1522 /* Menu actions are irrelevant */
1523 if (g_strstr_len(action_name, -1, "Menu") == nullptr)
1525 action_item = g_new0(ActionItem, 1);
1527 /* .desktop items need the program name, Geeqie menu items need the tooltip */
1528 if (g_strstr_len(action_name, -1, ".desktop") == nullptr)
1531 /* Tooltips with newlines affect output format */
1532 if (tooltip && (g_strstr_len(tooltip, -1, "\n") == nullptr) )
1534 action_item->label = g_strdup(tooltip);
1538 action_item->label = g_strdup(label);
1543 action_item->label = g_strdup(label);
1546 action_item->name = action_name;
1547 action_item->icon_name = g_strdup(gtk_action_get_stock_id(action));
1549 list_duplicates = g_list_prepend(list_duplicates, action_item);
1552 actions = actions->next;
1555 groups = groups->next;
1558 /* Use the shortest name i.e. ignore -Alt versions. Sort makes the shortest first in the list */
1559 list_duplicates = g_list_sort(list_duplicates, simple_sort_cb);
1561 /* Ignore duplicate entries */
1562 work1 = list_duplicates;
1566 work2 = list_unique;
1567 /* The first entry must be unique, list_unique is null so control bypasses the while */
1570 if (g_strcmp0(static_cast<ActionItem *>(work2->data)->label, static_cast<ActionItem *>(work1->data)->label) == 0)
1575 work2 = work2->next;
1580 action_item = g_new0(ActionItem, 1);
1581 action_item->name = g_strdup(static_cast<ActionItem *>(work1->data)->name);
1582 action_item->label = g_strdup(static_cast<ActionItem *>(work1->data)->label);
1583 action_item->icon_name = g_strdup(static_cast<ActionItem *>(work1->data)->icon_name);
1584 list_unique = g_list_append(list_unique, action_item);
1586 work1 = work1->next;
1589 g_list_free_full(list_duplicates, free_action_items_cb);
1594 gboolean defined_mouse_buttons(GtkWidget *, GdkEventButton *event, gpointer data)
1596 auto lw = static_cast<LayoutWindow *>(data);
1598 gboolean ret = FALSE;
1600 switch (event->button)
1602 case MOUSE_BUTTON_8:
1603 if (options->mouse_button_8)
1605 if (g_strstr_len(options->mouse_button_8, -1, ".desktop") != nullptr)
1607 file_util_start_editor_from_filelist(options->mouse_button_8, layout_selection_list(lw), layout_get_path(lw), lw->window);
1612 action = gtk_action_group_get_action(lw->action_group, options->mouse_button_8);
1615 gtk_action_activate(action);
1621 case MOUSE_BUTTON_9:
1622 if (options->mouse_button_9)
1624 if (g_strstr_len(options->mouse_button_9, -1, ".desktop") != nullptr)
1626 file_util_start_editor_from_filelist(options->mouse_button_9, layout_selection_list(lw), layout_get_path(lw), lw->window);
1630 action = gtk_action_group_get_action(lw->action_group, options->mouse_button_9);
1634 gtk_action_activate(action);
1647 GdkPixbuf *gq_gtk_icon_theme_load_icon_copy(GtkIconTheme *icon_theme, const gchar *icon_name, gint size, GtkIconLookupFlags flags)
1649 GError *error = nullptr;
1650 GdkPixbuf *icon = gtk_icon_theme_load_icon(icon_theme, icon_name, size, flags, &error);
1651 if (error) return nullptr;
1653 GdkPixbuf *pixbuf = gdk_pixbuf_copy(icon);
1654 g_object_unref(icon);
1658 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */