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.
28 #include "history-list.h"
33 *-----------------------------------------------------------------------------
34 * widget and layout utilities
35 *-----------------------------------------------------------------------------
38 GtkWidget *pref_box_new(GtkWidget *parent_box, gboolean fill,
39 GtkOrientation orientation, gboolean padding)
43 if (orientation == GTK_ORIENTATION_HORIZONTAL)
45 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, padding);
49 box = gtk_box_new(GTK_ORIENTATION_VERTICAL, padding);
52 gtk_box_pack_start(GTK_BOX(parent_box), box, fill, fill, 0);
58 GtkWidget *pref_group_new(GtkWidget *parent_box, gboolean fill,
59 const gchar *text, GtkOrientation orientation)
66 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
68 /* add additional spacing if necessary */
69 if (GTK_IS_VBOX(parent_box))
71 GList *list = gtk_container_get_children(GTK_CONTAINER(parent_box));
74 pref_spacer(vbox, PREF_PAD_GROUP - PREF_PAD_GAP);
79 gtk_box_pack_start(GTK_BOX(parent_box), vbox, fill, fill, 0);
80 gtk_widget_show(vbox);
82 label = gtk_label_new(text);
83 gtk_label_set_xalign(GTK_LABEL(label), 0.0);
84 gtk_label_set_yalign(GTK_LABEL(label), 0.5);
85 pref_label_bold(label, TRUE, FALSE);
87 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
88 gtk_widget_show(label);
90 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_INDENT);
91 gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 0);
92 gtk_widget_show(hbox);
94 /* indent using empty box */
97 if (orientation == GTK_ORIENTATION_HORIZONTAL)
99 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_SPACE);
103 box = gtk_box_new(GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
105 gtk_box_pack_start(GTK_BOX(hbox), box, TRUE, TRUE, 0);
106 gtk_widget_show(box);
108 g_object_set_data(G_OBJECT(box), "pref_group", vbox);
113 GtkWidget *pref_group_parent(GtkWidget *child)
122 group = static_cast<GtkWidget *>(g_object_get_data(G_OBJECT(parent), "pref_group"));
123 if (group && GTK_IS_WIDGET(group)) return group;
125 parent = gtk_widget_get_parent(parent);
131 GtkWidget *pref_frame_new(GtkWidget *parent_box, gboolean fill,
133 GtkOrientation orientation, gboolean padding)
136 GtkWidget *frame = nullptr;
138 frame = gtk_frame_new(text);
139 gtk_box_pack_start(GTK_BOX(parent_box), frame, fill, fill, 0);
140 gtk_widget_show(frame);
142 if (orientation == GTK_ORIENTATION_HORIZONTAL)
144 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, padding);
148 box = gtk_box_new(GTK_ORIENTATION_VERTICAL, padding);
150 gtk_container_add(GTK_CONTAINER(frame), box);
151 gtk_container_set_border_width(GTK_CONTAINER(box), PREF_PAD_BORDER);
152 gtk_widget_show(box);
157 GtkWidget *pref_spacer(GtkWidget *parent_box, gboolean padding)
161 spacer = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
162 gtk_box_pack_start(GTK_BOX(parent_box), spacer, FALSE, FALSE, padding / 2);
163 gtk_widget_show(spacer);
168 GtkWidget *pref_line(GtkWidget *parent_box, gboolean padding)
172 if (GTK_IS_HBOX(parent_box))
174 spacer = gtk_vseparator_new();
178 spacer = gtk_hseparator_new();
181 gtk_box_pack_start(GTK_BOX(parent_box), spacer, FALSE, FALSE, padding / 2);
182 gtk_widget_show(spacer);
187 GtkWidget *pref_label_new(GtkWidget *parent_box, const gchar *text)
191 label = gtk_label_new(text);
192 gtk_box_pack_start(GTK_BOX(parent_box), label, FALSE, FALSE, 0);
193 gtk_widget_show(label);
198 GtkWidget *pref_label_new_mnemonic(GtkWidget *parent_box, const gchar *text, GtkWidget *widget)
202 label = gtk_label_new_with_mnemonic(text);
203 gtk_label_set_mnemonic_widget(GTK_LABEL(label), widget);
204 gtk_box_pack_start(GTK_BOX(parent_box), label, FALSE, FALSE, 0);
205 gtk_widget_show(label);
210 void pref_label_bold(GtkWidget *label, gboolean bold, gboolean increase_size)
215 if (!bold && !increase_size) return;
217 pal = pango_attr_list_new();
221 pa = pango_attr_weight_new(PANGO_WEIGHT_BOLD);
223 pa->end_index = G_MAXINT;
224 pango_attr_list_insert(pal, pa);
229 pa = pango_attr_scale_new(PANGO_SCALE_LARGE);
231 pa->end_index = G_MAXINT;
232 pango_attr_list_insert(pal, pa);
235 gtk_label_set_attributes(GTK_LABEL(label), pal);
236 pango_attr_list_unref(pal);
239 GtkWidget *pref_button_new(GtkWidget *parent_box, const gchar *stock_id,
240 const gchar *text, gboolean hide_stock_text,
241 GCallback func, gpointer data)
245 if (stock_id && !text && !hide_stock_text)
247 button = gtk_button_new_from_stock(stock_id);
251 GtkWidget *image = nullptr;
252 GtkWidget *label = nullptr;
254 button = gtk_button_new();
258 image = gtk_image_new_from_stock(stock_id, GTK_ICON_SIZE_BUTTON);
263 label = gtk_label_new_with_mnemonic(text);
264 gtk_label_set_xalign(GTK_LABEL(label), 0.5);
265 gtk_label_set_yalign(GTK_LABEL(label), 0.5);
266 gtk_label_set_mnemonic_widget(GTK_LABEL(label), button);
274 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_BUTTON_ICON_GAP);
276 align = gtk_alignment_new(0.5, 0.5, 0.0, 0.0);
277 gtk_container_add(GTK_CONTAINER(button), align);
278 gtk_widget_show(align);
280 gtk_container_add(GTK_CONTAINER(align), hbox);
281 gtk_widget_show(hbox);
283 gtk_box_pack_start(GTK_BOX(hbox), image, FALSE, FALSE, 0);
284 gtk_box_pack_end(GTK_BOX(hbox), label, FALSE, FALSE, 0);
290 gtk_container_add(GTK_CONTAINER(button), image);
294 gtk_container_add(GTK_CONTAINER(button), label);
298 if (image) gtk_widget_show(image);
299 if (label) gtk_widget_show(label);
302 if (func) g_signal_connect(G_OBJECT(button), "clicked", func, data);
306 gtk_box_pack_start(GTK_BOX(parent_box), button, FALSE, FALSE, 0);
307 gtk_widget_show(button);
313 static GtkWidget *real_pref_checkbox_new(GtkWidget *parent_box, const gchar *text, gboolean mnemonic_text,
314 gboolean active, GCallback func, gpointer data)
320 button = gtk_check_button_new_with_mnemonic(text);
324 button = gtk_check_button_new_with_label(text);
326 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), active);
327 if (func) g_signal_connect(G_OBJECT(button), "clicked", func, data);
329 gtk_box_pack_start(GTK_BOX(parent_box), button, FALSE, FALSE, 0);
330 gtk_widget_show(button);
335 GtkWidget *pref_checkbox_new(GtkWidget *parent_box, const gchar *text, gboolean active,
336 GCallback func, gpointer data)
338 return real_pref_checkbox_new(parent_box, text, FALSE, active, func, data);
341 #pragma GCC diagnostic push
342 #pragma GCC diagnostic ignored "-Wunused-function"
343 GtkWidget *pref_checkbox_new_mnemonic_unused(GtkWidget *parent_box, const gchar *text, gboolean active,
344 GCallback func, gpointer data)
346 return real_pref_checkbox_new(parent_box, text, TRUE, active, func, data);
348 #pragma GCC diagnostic pop
350 static void pref_checkbox_int_cb(GtkWidget *widget, gpointer data)
352 auto result = static_cast<gboolean *>(data);
354 *result = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
357 GtkWidget *pref_checkbox_new_int(GtkWidget *parent_box, const gchar *text, gboolean active,
362 button = pref_checkbox_new(parent_box, text, active,
363 G_CALLBACK(pref_checkbox_int_cb), result);
369 static void pref_checkbox_link_sensitivity_cb(GtkWidget *button, gpointer data)
371 auto widget = static_cast<GtkWidget *>(data);
373 gtk_widget_set_sensitive(widget, gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button)));
376 void pref_checkbox_link_sensitivity(GtkWidget *button, GtkWidget *widget)
378 g_signal_connect(G_OBJECT(button), "toggled",
379 G_CALLBACK(pref_checkbox_link_sensitivity_cb), widget);
381 pref_checkbox_link_sensitivity_cb(button, widget);
384 #pragma GCC diagnostic push
385 #pragma GCC diagnostic ignored "-Wunused-function"
386 static void pref_checkbox_link_sensitivity_swap_cb_unused(GtkWidget *button, gpointer data)
388 GtkWidget *widget = static_cast<GtkWidget *>(data);
390 gtk_widget_set_sensitive(widget, !gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button)));
393 void pref_checkbox_link_sensitivity_swap_unused(GtkWidget *button, GtkWidget *widget)
395 g_signal_connect(G_OBJECT(button), "toggled",
396 G_CALLBACK(pref_checkbox_link_sensitivity_swap_cb_unused), widget);
398 pref_checkbox_link_sensitivity_swap_cb_unused(button, widget);
400 #pragma GCC diagnostic pop
402 static GtkWidget *real_pref_radiobutton_new(GtkWidget *parent_box, GtkWidget *sibling,
403 const gchar *text, gboolean mnemonic_text, gboolean active,
404 GCallback func, gpointer data)
411 group = gtk_radio_button_get_group(GTK_RADIO_BUTTON(sibling));
420 button = gtk_radio_button_new_with_mnemonic(group, text);
424 button = gtk_radio_button_new_with_label(group, text);
427 if (active) gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), active);
428 if (func) g_signal_connect(G_OBJECT(button), "clicked", func, data);
430 gtk_box_pack_start(GTK_BOX(parent_box), button, FALSE, FALSE, 0);
431 gtk_widget_show(button);
436 GtkWidget *pref_radiobutton_new(GtkWidget *parent_box, GtkWidget *sibling,
437 const gchar *text, gboolean active,
438 GCallback func, gpointer data)
440 return real_pref_radiobutton_new(parent_box, sibling, text, FALSE, active, func, data);
443 #pragma GCC diagnostic push
444 #pragma GCC diagnostic ignored "-Wunused-function"
445 GtkWidget *pref_radiobutton_new_mnemonic_unused(GtkWidget *parent_box, GtkWidget *sibling,
446 const gchar *text, gboolean active,
447 GCallback func, gpointer data)
449 return real_pref_radiobutton_new(parent_box, sibling, text, TRUE, active, func, data);
452 #define PREF_RADIO_VALUE_KEY "pref_radio_value"
454 static void pref_radiobutton_int_cb_unused(GtkWidget *widget, gpointer data)
456 gboolean *result = static_cast<gboolean *>(data);
458 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
460 *result = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), PREF_RADIO_VALUE_KEY));
464 GtkWidget *pref_radiobutton_new_int_unused(GtkWidget *parent_box, GtkWidget *sibling,
465 const gchar *text, gboolean active,
466 gboolean *result, gboolean value,
467 GCallback UNUSED(func), gpointer UNUSED(data))
471 button = pref_radiobutton_new(parent_box, sibling, text, active,
472 G_CALLBACK(pref_radiobutton_int_cb_unused), result);
473 g_object_set_data(G_OBJECT(button), PREF_RADIO_VALUE_KEY, GINT_TO_POINTER(value));
474 if (active) *result = value;
478 #pragma GCC diagnostic pop
480 static GtkWidget *real_pref_spin_new(GtkWidget *parent_box, const gchar *text, const gchar *suffix,
481 gboolean mnemonic_text,
482 gdouble min, gdouble max, gdouble step, gint digits,
484 GCallback func, gpointer data)
490 box = pref_box_new(parent_box, FALSE, GTK_ORIENTATION_HORIZONTAL, PREF_PAD_SPACE);
492 spin = gtk_spin_button_new_with_range(min, max, step);
493 gtk_spin_button_set_digits(GTK_SPIN_BUTTON(spin), digits);
494 gtk_spin_button_set_value(GTK_SPIN_BUTTON(spin), value);
498 g_signal_connect(G_OBJECT(spin), "value_changed", G_CALLBACK(func), data);
505 label = pref_label_new_mnemonic(box, text, spin);
509 label = pref_label_new(box, text);
511 pref_link_sensitivity(label, spin);
514 gtk_box_pack_start(GTK_BOX(box), spin, FALSE, FALSE, 0);
515 gtk_widget_show(spin);
517 /* perhaps this should only be PREF_PAD_GAP distance from spinbutton ? */
520 label = pref_label_new(box, suffix);
521 pref_link_sensitivity(label, spin);
527 GtkWidget *pref_spin_new(GtkWidget *parent_box, const gchar *text, const gchar *suffix,
528 gdouble min, gdouble max, gdouble step, gint digits,
530 GCallback func, gpointer data)
532 return real_pref_spin_new(parent_box, text, suffix, FALSE,
533 min, max, step, digits, value, func, data);
536 #pragma GCC diagnostic push
537 #pragma GCC diagnostic ignored "-Wunused-function"
538 GtkWidget *pref_spin_new_mnemonic_unused(GtkWidget *parent_box, const gchar *text, const gchar *suffix,
539 gdouble min, gdouble max, gdouble step, gint digits,
541 GCallback func, gpointer data)
543 return real_pref_spin_new(parent_box, text, suffix, TRUE,
544 min, max, step, digits, value, func, data);
546 #pragma GCC diagnostic pop
548 static void pref_spin_int_cb(GtkWidget *widget, gpointer data)
550 auto var = static_cast<gint *>(data);
551 *var = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(widget));
554 GtkWidget *pref_spin_new_int(GtkWidget *parent_box, const gchar *text, const gchar *suffix,
555 gint min, gint max, gint step,
556 gint value, gint *value_var)
559 return pref_spin_new(parent_box, text, suffix,
560 static_cast<gdouble>(min), static_cast<gdouble>(max), static_cast<gdouble>(step), 0,
562 G_CALLBACK(pref_spin_int_cb), value_var);
565 static void pref_link_sensitivity_cb(GtkWidget *watch, GtkStateType UNUSED(prev_state), gpointer data)
567 auto widget = static_cast<GtkWidget *>(data);
569 gtk_widget_set_sensitive(widget, gtk_widget_is_sensitive(watch));
572 void pref_link_sensitivity(GtkWidget *widget, GtkWidget *watch)
574 g_signal_connect(G_OBJECT(watch), "state_changed",
575 G_CALLBACK(pref_link_sensitivity_cb), widget);
578 void pref_signal_block_data(GtkWidget *widget, gpointer data)
580 g_signal_handlers_block_matched(widget, G_SIGNAL_MATCH_DATA,
581 0, 0, nullptr, nullptr, data);
584 void pref_signal_unblock_data(GtkWidget *widget, gpointer data)
586 g_signal_handlers_unblock_matched(widget, G_SIGNAL_MATCH_DATA,
587 0, 0, nullptr, nullptr, data);
590 GtkWidget *pref_table_new(GtkWidget *parent_box, gint columns, gint rows,
591 gboolean homogeneous, gboolean fill)
595 table = gtk_table_new(rows, columns, homogeneous);
596 gtk_table_set_row_spacings(GTK_TABLE(table), PREF_PAD_GAP);
597 gtk_table_set_col_spacings(GTK_TABLE(table), PREF_PAD_SPACE);
601 gtk_box_pack_start(GTK_BOX(parent_box), table, fill, fill, 0);
602 gtk_widget_show(table);
608 GtkWidget *pref_table_box(GtkWidget *table, gint column, gint row,
609 GtkOrientation orientation, const gchar *text)
616 shell = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
617 box = pref_group_new(shell, TRUE, text, orientation);
621 if (orientation == GTK_ORIENTATION_HORIZONTAL)
623 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_SPACE);
627 box = gtk_box_new(GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
632 gtk_table_attach(GTK_TABLE(table), shell, column, column + 1, row, row + 1,
633 static_cast<GtkAttachOptions>(GTK_EXPAND | GTK_FILL), static_cast<GtkAttachOptions>(0), 0, 0);
635 gtk_widget_show(shell);
640 GtkWidget *pref_table_label(GtkWidget *table, gint column, gint row,
641 const gchar *text, gfloat alignment)
646 align = gtk_alignment_new(alignment, 0.50, 0.0, 0.0);
647 gtk_table_attach(GTK_TABLE(table), align, column, column + 1, row, row + 1,
648 GTK_FILL, static_cast<GtkAttachOptions>(0), 0, 0);
649 gtk_widget_show(align);
650 label = gtk_label_new(text);
651 gtk_container_add(GTK_CONTAINER(align), label);
652 gtk_widget_show(label);
657 GtkWidget *pref_table_button(GtkWidget *table, gint column, gint row,
658 const gchar *stock_id, const gchar *text, gboolean hide_stock_text,
659 GCallback func, gpointer data)
663 button = pref_button_new(nullptr, stock_id, text, hide_stock_text, func, data);
664 gtk_table_attach(GTK_TABLE(table), button, column, column + 1, row, row + 1,
665 GTK_FILL, static_cast<GtkAttachOptions>(0), 0, 0);
666 gtk_widget_show(button);
671 GtkWidget *pref_table_spin(GtkWidget *table, gint column, gint row,
672 const gchar *text, const gchar *suffix,
673 gdouble min, gdouble max, gdouble step, gint digits,
675 GCallback func, gpointer data)
681 spin = gtk_spin_button_new_with_range(min, max, step);
682 gtk_spin_button_set_digits(GTK_SPIN_BUTTON(spin), digits);
683 gtk_spin_button_set_value(GTK_SPIN_BUTTON(spin), value);
686 g_signal_connect(G_OBJECT(spin), "value_changed", G_CALLBACK(func), data);
691 label = pref_table_label(table, column, row, text, 1.0);
692 pref_link_sensitivity(label, spin);
698 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_SPACE);
699 gtk_box_pack_start(GTK_BOX(box), spin, FALSE, FALSE, 0);
700 gtk_widget_show(spin);
702 label = pref_label_new(box, suffix);
703 pref_link_sensitivity(label, spin);
710 gtk_table_attach(GTK_TABLE(table), box, column, column + 1, row, row + 1,
711 static_cast<GtkAttachOptions>(GTK_EXPAND | GTK_FILL), static_cast<GtkAttachOptions>(GTK_EXPAND | GTK_FILL), 0, 0);
712 gtk_widget_show(box);
717 GtkWidget *pref_table_spin_new_int(GtkWidget *table, gint column, gint row,
718 const gchar *text, const gchar *suffix,
719 gint min, gint max, gint step,
720 gint value, gint *value_var)
723 return pref_table_spin(table, column, row,
725 static_cast<gdouble>(min), static_cast<gdouble>(max), static_cast<gdouble>(step), 0,
727 G_CALLBACK(pref_spin_int_cb), value_var);
731 GtkWidget *pref_toolbar_new(GtkWidget *parent_box, GtkToolbarStyle style)
735 tbar = gtk_toolbar_new();
736 gtk_toolbar_set_style(GTK_TOOLBAR(tbar), style);
740 gtk_box_pack_start(GTK_BOX(parent_box), tbar, FALSE, FALSE, 0);
741 gtk_widget_show(tbar);
746 GtkWidget *pref_toolbar_button(GtkWidget *toolbar,
747 const gchar *stock_id, const gchar *label, gboolean toggle,
748 const gchar *description,
749 GCallback func, gpointer data)
757 item = GTK_WIDGET(gtk_toggle_tool_button_new_from_stock(stock_id));
761 item = GTK_WIDGET(gtk_toggle_tool_button_new());
768 item = GTK_WIDGET(gtk_tool_button_new_from_stock(stock_id));
772 item = GTK_WIDGET(gtk_tool_button_new(nullptr, nullptr));
775 gtk_tool_button_set_use_underline(GTK_TOOL_BUTTON(item), TRUE);
777 if (label) gtk_tool_button_set_label(GTK_TOOL_BUTTON(item), label);
779 if (func) g_signal_connect(item, "clicked", func, data);
780 gtk_container_add(GTK_CONTAINER(toolbar), item);
781 gtk_widget_show(item);
785 gtk_widget_set_tooltip_text(item, description);
791 #pragma GCC diagnostic push
792 #pragma GCC diagnostic ignored "-Wunused-function"
793 void pref_toolbar_button_set_icon_unused(GtkWidget *button, GtkWidget *widget, const gchar *stock_id)
797 gtk_tool_button_set_icon_widget(GTK_TOOL_BUTTON(button), widget);
801 gtk_tool_button_set_stock_id(GTK_TOOL_BUTTON(button), stock_id);
805 GtkWidget *pref_toolbar_spacer_unused(GtkWidget *toolbar)
809 item = GTK_WIDGET(gtk_separator_tool_item_new());
810 gtk_container_add(GTK_CONTAINER(toolbar), item);
811 gtk_widget_show(item);
815 #pragma GCC diagnostic pop
819 *-----------------------------------------------------------------------------
820 * date selection entry
821 *-----------------------------------------------------------------------------
824 #define DATE_SELECION_KEY "date_selection_data"
842 static void date_selection_popup_hide(DateSelection *ds)
844 if (!ds->window) return;
846 if (gtk_widget_has_grab(ds->window))
848 gtk_grab_remove(ds->window);
849 gdk_keyboard_ungrab(GDK_CURRENT_TIME);
850 gdk_pointer_ungrab(GDK_CURRENT_TIME);
853 gtk_widget_hide(ds->window);
855 gtk_widget_destroy(ds->window);
856 ds->window = nullptr;
857 ds->calendar = nullptr;
859 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ds->button), FALSE);
862 static gboolean date_selection_popup_release_cb(GtkWidget *UNUSED(widget), GdkEventButton *UNUSED(event), gpointer data)
864 auto ds = static_cast<DateSelection *>(data);
866 date_selection_popup_hide(ds);
870 static gboolean date_selection_popup_press_cb(GtkWidget *UNUSED(widget), GdkEventButton *event, gpointer data)
872 auto ds = static_cast<DateSelection *>(data);
878 xr = static_cast<gint>(event->x_root);
879 yr = static_cast<gint>(event->y_root);
881 window = gtk_widget_get_window(ds->window);
882 gdk_window_get_origin(window, &x, &y);
883 w = gdk_window_get_width(window);
884 h = gdk_window_get_height(window);
886 if (xr < x || yr < y || xr > x + w || yr > y + h)
888 g_signal_connect(G_OBJECT(ds->window), "button_release_event",
889 G_CALLBACK(date_selection_popup_release_cb), ds);
896 static void date_selection_popup_sync(DateSelection *ds)
898 guint day, month, year;
900 gtk_calendar_get_date(GTK_CALENDAR(ds->calendar), &year, &month, &day);
901 date_selection_set(ds->box, day, month + 1, year);
904 static gboolean date_selection_popup_keypress_cb(GtkWidget *UNUSED(widget), GdkEventKey *event, gpointer data)
906 auto ds = static_cast<DateSelection *>(data);
908 switch (event->keyval)
911 case GDK_KEY_KP_Enter:
913 case GDK_KEY_ISO_Left_Tab:
914 date_selection_popup_sync(ds);
915 date_selection_popup_hide(ds);
918 date_selection_popup_hide(ds);
927 static void date_selection_day_cb(GtkWidget *UNUSED(widget), gpointer data)
929 auto ds = static_cast<DateSelection *>(data);
931 date_selection_popup_sync(ds);
934 static void date_selection_doubleclick_cb(GtkWidget *UNUSED(widget), gpointer data)
936 auto ds = static_cast<DateSelection *>(data);
938 date_selection_popup_hide(ds);
941 static void date_selection_popup(DateSelection *ds)
945 gint day, month, year;
946 GtkAllocation button_allocation;
947 GtkAllocation window_allocation;
949 if (ds->window) return;
951 ds->window = gtk_window_new(GTK_WINDOW_POPUP);
952 gtk_window_set_resizable(GTK_WINDOW(ds->window), FALSE);
953 g_signal_connect(G_OBJECT(ds->window), "button_press_event",
954 G_CALLBACK(date_selection_popup_press_cb), ds);
955 g_signal_connect(G_OBJECT(ds->window), "key_press_event",
956 G_CALLBACK(date_selection_popup_keypress_cb), ds);
958 ds->calendar = gtk_calendar_new();
959 gtk_container_add(GTK_CONTAINER(ds->window), ds->calendar);
960 gtk_widget_show(ds->calendar);
962 date_selection_get(ds->box, &day, &month, &year);
963 gtk_calendar_select_month(GTK_CALENDAR(ds->calendar), month - 1, year);
964 gtk_calendar_select_day(GTK_CALENDAR(ds->calendar), day);
966 g_signal_connect(G_OBJECT(ds->calendar), "day_selected",
967 G_CALLBACK(date_selection_day_cb), ds);
968 g_signal_connect(G_OBJECT(ds->calendar), "day_selected_double_click",
969 G_CALLBACK(date_selection_doubleclick_cb), ds);
971 gtk_widget_realize(ds->window);
973 gdk_window_get_origin(gtk_widget_get_window(ds->button), &wx, &wy);
975 gtk_widget_get_allocation(ds->button, &button_allocation);
976 gtk_widget_get_allocation(ds->window, &window_allocation);
978 x = wx + button_allocation.x + button_allocation.width - window_allocation.width;
979 y = wy + button_allocation.y + button_allocation.height;
981 if (y + window_allocation.height > gdk_screen_height())
983 y = wy + button_allocation.y - window_allocation.height;
988 gtk_window_move(GTK_WINDOW(ds->window), x, y);
989 gtk_widget_show(ds->window);
991 gtk_widget_grab_focus(ds->calendar);
992 gdk_pointer_grab(gtk_widget_get_window(ds->window), TRUE,
993 static_cast<GdkEventMask>(GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_MOTION_MASK),
994 nullptr, nullptr, GDK_CURRENT_TIME);
995 gdk_keyboard_grab(gtk_widget_get_window(ds->window), TRUE, GDK_CURRENT_TIME);
996 gtk_grab_add(ds->window);
998 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ds->button), TRUE);
1001 static void date_selection_button_cb(GtkWidget *UNUSED(widget), gpointer data)
1003 auto ds = static_cast<DateSelection *>(data);
1005 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(ds->button)) == (!ds->window))
1007 date_selection_popup(ds);
1011 static void button_size_allocate_cb(GtkWidget *button, GtkAllocation *allocation, gpointer data)
1013 auto spin = static_cast<GtkWidget *>(data);
1014 GtkRequisition spin_requisition;
1015 gtk_widget_get_requisition(spin, &spin_requisition);
1017 if (allocation->height > spin_requisition.height)
1019 GtkAllocation button_allocation;
1020 GtkAllocation spin_allocation;
1022 gtk_widget_get_allocation(button, &button_allocation);
1023 gtk_widget_get_allocation(spin, &spin_allocation);
1024 button_allocation.height = spin_requisition.height;
1025 button_allocation.y = spin_allocation.y +
1026 (spin_allocation.height - spin_requisition.height) / 2;
1027 gtk_widget_size_allocate(button, &button_allocation);
1031 static void spin_increase(GtkWidget *spin, gint value)
1035 gtk_widget_size_request(spin, &req);
1036 gtk_widget_set_size_request(spin, req.width + value, -1);
1039 static void date_selection_destroy_cb(GtkWidget *UNUSED(widget), gpointer data)
1041 auto ds = static_cast<DateSelection *>(data);
1043 date_selection_popup_hide(ds);
1048 GtkWidget *date_selection_new()
1053 ds = g_new0(DateSelection, 1);
1057 ds->box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
1058 g_signal_connect(G_OBJECT(ds->box), "destroy",
1059 G_CALLBACK(date_selection_destroy_cb), ds);
1061 date_format = nl_langinfo(D_FMT);
1063 if (strlen(date_format) == 8)
1065 for (i=1; i<8; i=i+3)
1067 switch (date_format[i])
1070 ds->spin_d = pref_spin_new(ds->box, nullptr, nullptr, 1, 31, 1, 0, 1, nullptr, nullptr);
1073 ds->spin_m = pref_spin_new(ds->box, nullptr, nullptr, 1, 12, 1, 0, 1, nullptr, nullptr);
1076 ds->spin_y = pref_spin_new(ds->box, nullptr, nullptr, 1900, 9999, 1, 0, 1900, nullptr, nullptr);
1079 log_printf("Warning: Date locale %s is unknown", date_format);
1086 ds->spin_m = pref_spin_new(ds->box, nullptr, nullptr, 1, 12, 1, 0, 1, nullptr, nullptr);
1087 ds->spin_d = pref_spin_new(ds->box, nullptr, nullptr, 1, 31, 1, 0, 1, nullptr, nullptr);
1088 ds->spin_y = pref_spin_new(ds->box, nullptr, nullptr, 1900, 9999, 1, 0, 1900, nullptr, nullptr);
1091 spin_increase(ds->spin_y, 5);
1093 ds->button = gtk_toggle_button_new();
1094 g_signal_connect(G_OBJECT(ds->button), "size_allocate",
1095 G_CALLBACK(button_size_allocate_cb), ds->spin_y);
1097 arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_NONE);
1098 gtk_container_add(GTK_CONTAINER(ds->button), arrow);
1099 gtk_widget_show(arrow);
1101 gtk_box_pack_start(GTK_BOX(ds->box), ds->button, FALSE, FALSE, 0);
1102 g_signal_connect(G_OBJECT(ds->button), "clicked",
1103 G_CALLBACK(date_selection_button_cb), ds);
1104 gtk_widget_show(ds->button);
1106 g_object_set_data(G_OBJECT(ds->box), DATE_SELECION_KEY, ds);
1111 void date_selection_set(GtkWidget *widget, gint day, gint month, gint year)
1115 ds = static_cast<DateSelection *>(g_object_get_data(G_OBJECT(widget), DATE_SELECION_KEY));
1118 gtk_spin_button_set_value(GTK_SPIN_BUTTON(ds->spin_d), static_cast<gdouble>(day));
1119 gtk_spin_button_set_value(GTK_SPIN_BUTTON(ds->spin_m), static_cast<gdouble>(month));
1120 gtk_spin_button_set_value(GTK_SPIN_BUTTON(ds->spin_y), static_cast<gdouble>(year));
1124 void date_selection_get(GtkWidget *widget, gint *day, gint *month, gint *year)
1128 ds = static_cast<DateSelection *>(g_object_get_data(G_OBJECT(widget), DATE_SELECION_KEY));
1131 if (day) *day = gtk_spin_button_get_value(GTK_SPIN_BUTTON(ds->spin_d));
1132 if (month) *month = gtk_spin_button_get_value(GTK_SPIN_BUTTON(ds->spin_m));
1133 if (year) *year = gtk_spin_button_get_value(GTK_SPIN_BUTTON(ds->spin_y));
1136 void date_selection_time_set(GtkWidget *widget, time_t t)
1143 date_selection_set(widget, lt->tm_mday, lt->tm_mon + 1, lt->tm_year + 1900);
1146 #pragma GCC diagnostic push
1147 #pragma GCC diagnostic ignored "-Wunused-function"
1148 time_t date_selection_time_get_unused(GtkWidget *widget)
1155 date_selection_get(widget, &day, &month ,&year);
1161 lt.tm_mon = month - 1;
1162 lt.tm_year = year - 1900;
1167 #pragma GCC diagnostic pop
1170 *-----------------------------------------------------------------------------
1171 * storing data in a history list with key,data pairs
1172 *-----------------------------------------------------------------------------
1175 #define PREF_LIST_MARKER_INT "[INT]:"
1176 #define PREF_LIST_MARKER_DOUBLE "[DOUBLE]:"
1177 #define PREF_LIST_MARKER_STRING "[STRING]:"
1179 static GList *pref_list_find(const gchar *group, const gchar *token)
1186 work = history_list_get_by_key(group);
1189 auto text = static_cast<const gchar *>(work->data);
1191 if (strncmp(text, token, l) == 0) return work;
1199 static gboolean pref_list_get(const gchar *group, const gchar *key, const gchar *marker, const gchar **result)
1205 if (!group || !key || !marker)
1211 token = g_strconcat(key, marker, NULL);
1213 work = pref_list_find(group, token);
1216 *result = static_cast<const gchar *>(work->data) + strlen(token);
1217 if (strlen(*result) == 0) *result = nullptr;
1231 static void pref_list_set(const gchar *group, const gchar *key, const gchar *marker, const gchar *text)
1237 if (!group || !key || !marker) return;
1239 token = g_strconcat(key, marker, NULL);
1240 path = g_strconcat(token, text, NULL);
1242 work = pref_list_find(group, token);
1245 auto old_path = static_cast<gchar *>(work->data);
1256 history_list_item_remove(group, old_path);
1261 history_list_add_to_key(group, path, 0);
1268 void pref_list_int_set(const gchar *group, const gchar *key, gint value)
1272 text = g_strdup_printf("%d", value);
1273 pref_list_set(group, key, PREF_LIST_MARKER_INT, text);
1277 gboolean pref_list_int_get(const gchar *group, const gchar *key, gint *result)
1287 if (pref_list_get(group, key, PREF_LIST_MARKER_INT, &text) && text)
1289 *result = static_cast<gint>(strtol(text, nullptr, 10));
1297 #pragma GCC diagnostic push
1298 #pragma GCC diagnostic ignored "-Wunused-function"
1299 void pref_list_double_set_unused(const gchar *group, const gchar *key, gdouble value)
1301 gchar text[G_ASCII_DTOSTR_BUF_SIZE];
1303 g_ascii_dtostr(text, sizeof(text), value);
1304 pref_list_set(group, key, PREF_LIST_MARKER_DOUBLE, text);
1307 gboolean pref_list_double_get_unused(const gchar *group, const gchar *key, gdouble *result)
1317 if (pref_list_get(group, key, PREF_LIST_MARKER_DOUBLE, &text) && text)
1319 *result = g_ascii_strtod(text, NULL);
1327 void pref_list_string_set_unused(const gchar *group, const gchar *key, const gchar *value)
1329 pref_list_set(group, key, PREF_LIST_MARKER_STRING, value);
1332 gboolean pref_list_string_get_unused(const gchar *group, const gchar *key, const gchar **result)
1334 return pref_list_get(group, key, PREF_LIST_MARKER_STRING, result);
1336 #pragma GCC diagnostic pop
1338 void pref_color_button_set_cb(GtkWidget *widget, gpointer data)
1340 auto color = static_cast<GdkColor *>(data);
1342 gtk_color_button_get_color(GTK_COLOR_BUTTON(widget), color);
1345 GtkWidget *pref_color_button_new(GtkWidget *parent_box,
1346 const gchar *title, const GdkColor *color,
1347 GCallback func, gpointer data)
1353 button = gtk_color_button_new_with_color(color);
1357 button = gtk_color_button_new();
1360 if (func) g_signal_connect(G_OBJECT(button), "color-set", func, data);
1367 gtk_color_button_set_title(GTK_COLOR_BUTTON(button), title);
1368 label = gtk_label_new(title);
1370 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
1371 gtk_box_pack_start(GTK_BOX(parent_box), hbox, TRUE, TRUE, 0);
1373 gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 0);
1374 gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
1376 gtk_widget_show_all(hbox);
1380 gtk_widget_show(button);
1387 *-----------------------------------------------------------------------------
1389 *-----------------------------------------------------------------------------
1392 gchar *text_widget_text_pull(GtkWidget *text_widget)
1394 if (GTK_IS_TEXT_VIEW(text_widget))
1396 GtkTextBuffer *buffer;
1397 GtkTextIter start, end;
1399 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_widget));
1400 gtk_text_buffer_get_bounds(buffer, &start, &end);
1402 return gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
1404 else if (GTK_IS_ENTRY(text_widget))
1406 return g_strdup(gtk_entry_get_text(GTK_ENTRY(text_widget)));
1415 gchar *text_widget_text_pull_selected(GtkWidget *text_widget)
1417 if (GTK_IS_TEXT_VIEW(text_widget))
1419 GtkTextBuffer *buffer;
1420 GtkTextIter start, end;
1422 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_widget));
1423 gtk_text_buffer_get_bounds(buffer, &start, &end);
1425 if (gtk_text_buffer_get_selection_bounds(buffer, &start, &end))
1427 gtk_text_iter_set_line_offset(&start, 0);
1428 gtk_text_iter_forward_to_line_end(&end);
1431 return gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
1433 else if (GTK_IS_ENTRY(text_widget))
1435 return g_strdup(gtk_entry_get_text(GTK_ENTRY(text_widget)));
1443 gboolean defined_mouse_buttons(GtkWidget *UNUSED(widget), GdkEventButton *event, gpointer data)
1445 auto lw = static_cast<LayoutWindow *>(data);
1447 gboolean ret = FALSE;
1449 switch (event->button)
1451 case MOUSE_BUTTON_8:
1452 if (options->mouse_button_8)
1454 action = gtk_action_group_get_action(lw->action_group, options->mouse_button_8);
1457 gtk_action_activate(action);
1462 case MOUSE_BUTTON_9:
1463 if (options->mouse_button_9)
1465 action = gtk_action_group_get_action(lw->action_group, options->mouse_button_9);
1468 gtk_action_activate(action);
1480 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */