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 = NULL;
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 = NULL;
252 GtkWidget *label = NULL;
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 //GtkWidget *pref_checkbox_new_mnemonic(GtkWidget *parent_box, const gchar *text, gboolean active,
342 //GCallback func, gpointer data)
344 //return real_pref_checkbox_new(parent_box, text, TRUE, active, func, data);
347 static void pref_checkbox_int_cb(GtkWidget *widget, gpointer data)
349 gboolean *result = static_cast<gboolean *>(data);
351 *result = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
354 GtkWidget *pref_checkbox_new_int(GtkWidget *parent_box, const gchar *text, gboolean active,
359 button = pref_checkbox_new(parent_box, text, active,
360 G_CALLBACK(pref_checkbox_int_cb), result);
366 static void pref_checkbox_link_sensitivity_cb(GtkWidget *button, gpointer data)
368 GtkWidget *widget = static_cast<GtkWidget *>(data);
370 gtk_widget_set_sensitive(widget, gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button)));
373 void pref_checkbox_link_sensitivity(GtkWidget *button, GtkWidget *widget)
375 g_signal_connect(G_OBJECT(button), "toggled",
376 G_CALLBACK(pref_checkbox_link_sensitivity_cb), widget);
378 pref_checkbox_link_sensitivity_cb(button, widget);
381 //static void pref_checkbox_link_sensitivity_swap_cb(GtkWidget *button, gpointer data)
383 //GtkWidget *widget = static_cast<GtkWidget *>(data);
385 //gtk_widget_set_sensitive(widget, !gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button)));
388 //void pref_checkbox_link_sensitivity_swap(GtkWidget *button, GtkWidget *widget)
390 //g_signal_connect(G_OBJECT(button), "toggled",
391 //G_CALLBACK(pref_checkbox_link_sensitivity_swap_cb), widget);
393 //pref_checkbox_link_sensitivity_swap_cb(button, widget);
396 static GtkWidget *real_pref_radiobutton_new(GtkWidget *parent_box, GtkWidget *sibling,
397 const gchar *text, gboolean mnemonic_text, gboolean active,
398 GCallback func, gpointer data)
405 group = gtk_radio_button_get_group(GTK_RADIO_BUTTON(sibling));
414 button = gtk_radio_button_new_with_mnemonic(group, text);
418 button = gtk_radio_button_new_with_label(group, text);
421 if (active) gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), active);
422 if (func) g_signal_connect(G_OBJECT(button), "clicked", func, data);
424 gtk_box_pack_start(GTK_BOX(parent_box), button, FALSE, FALSE, 0);
425 gtk_widget_show(button);
430 GtkWidget *pref_radiobutton_new(GtkWidget *parent_box, GtkWidget *sibling,
431 const gchar *text, gboolean active,
432 GCallback func, gpointer data)
434 return real_pref_radiobutton_new(parent_box, sibling, text, FALSE, active, func, data);
437 //GtkWidget *pref_radiobutton_new_mnemonic(GtkWidget *parent_box, GtkWidget *sibling,
438 //const gchar *text, gboolean active,
439 //GCallback func, gpointer data)
441 //return real_pref_radiobutton_new(parent_box, sibling, text, TRUE, active, func, data);
444 #define PREF_RADIO_VALUE_KEY "pref_radio_value"
446 //static void pref_radiobutton_int_cb(GtkWidget *widget, gpointer data)
448 //gboolean *result = static_cast<gboolean *>(data);
450 //if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
452 //*result = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), PREF_RADIO_VALUE_KEY));
456 //GtkWidget *pref_radiobutton_new_int(GtkWidget *parent_box, GtkWidget *sibling,
457 //const gchar *text, gboolean active,
458 //gboolean *result, gboolean value,
459 //GCallback UNUSED(func), gpointer UNUSED(data))
463 //button = pref_radiobutton_new(parent_box, sibling, text, active,
464 //G_CALLBACK(pref_radiobutton_int_cb), result);
465 //g_object_set_data(G_OBJECT(button), PREF_RADIO_VALUE_KEY, GINT_TO_POINTER(value));
466 //if (active) *result = value;
471 static GtkWidget *real_pref_spin_new(GtkWidget *parent_box, const gchar *text, const gchar *suffix,
472 gboolean mnemonic_text,
473 gdouble min, gdouble max, gdouble step, gint digits,
475 GCallback func, gpointer data)
481 box = pref_box_new(parent_box, FALSE, GTK_ORIENTATION_HORIZONTAL, PREF_PAD_SPACE);
483 spin = gtk_spin_button_new_with_range(min, max, step);
484 gtk_spin_button_set_digits(GTK_SPIN_BUTTON(spin), digits);
485 gtk_spin_button_set_value(GTK_SPIN_BUTTON(spin), value);
489 g_signal_connect(G_OBJECT(spin), "value_changed", G_CALLBACK(func), data);
496 label = pref_label_new_mnemonic(box, text, spin);
500 label = pref_label_new(box, text);
502 pref_link_sensitivity(label, spin);
505 gtk_box_pack_start(GTK_BOX(box), spin, FALSE, FALSE, 0);
506 gtk_widget_show(spin);
508 /* perhaps this should only be PREF_PAD_GAP distance from spinbutton ? */
511 label = pref_label_new(box, suffix);
512 pref_link_sensitivity(label, spin);
518 GtkWidget *pref_spin_new(GtkWidget *parent_box, const gchar *text, const gchar *suffix,
519 gdouble min, gdouble max, gdouble step, gint digits,
521 GCallback func, gpointer data)
523 return real_pref_spin_new(parent_box, text, suffix, FALSE,
524 min, max, step, digits, value, func, data);
527 //GtkWidget *pref_spin_new_mnemonic(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, TRUE,
533 //min, max, step, digits, value, func, data);
536 static void pref_spin_int_cb(GtkWidget *widget, gpointer data)
538 gint *var = static_cast<gint *>(data);
539 *var = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(widget));
542 GtkWidget *pref_spin_new_int(GtkWidget *parent_box, const gchar *text, const gchar *suffix,
543 gint min, gint max, gint step,
544 gint value, gint *value_var)
547 return pref_spin_new(parent_box, text, suffix,
548 (gdouble)min, (gdouble)max, (gdouble)step, 0,
550 G_CALLBACK(pref_spin_int_cb), value_var);
553 static void pref_link_sensitivity_cb(GtkWidget *watch, GtkStateType UNUSED(prev_state), gpointer data)
555 GtkWidget *widget = static_cast<GtkWidget *>(data);
557 gtk_widget_set_sensitive(widget, gtk_widget_is_sensitive(watch));
560 void pref_link_sensitivity(GtkWidget *widget, GtkWidget *watch)
562 g_signal_connect(G_OBJECT(watch), "state_changed",
563 G_CALLBACK(pref_link_sensitivity_cb), widget);
566 void pref_signal_block_data(GtkWidget *widget, gpointer data)
568 g_signal_handlers_block_matched(widget, G_SIGNAL_MATCH_DATA,
569 0, 0, NULL, NULL, data);
572 void pref_signal_unblock_data(GtkWidget *widget, gpointer data)
574 g_signal_handlers_unblock_matched(widget, G_SIGNAL_MATCH_DATA,
575 0, 0, NULL, NULL, data);
578 GtkWidget *pref_table_new(GtkWidget *parent_box, gint columns, gint rows,
579 gboolean homogeneous, gboolean fill)
583 table = gtk_table_new(rows, columns, homogeneous);
584 gtk_table_set_row_spacings(GTK_TABLE(table), PREF_PAD_GAP);
585 gtk_table_set_col_spacings(GTK_TABLE(table), PREF_PAD_SPACE);
589 gtk_box_pack_start(GTK_BOX(parent_box), table, fill, fill, 0);
590 gtk_widget_show(table);
596 GtkWidget *pref_table_box(GtkWidget *table, gint column, gint row,
597 GtkOrientation orientation, const gchar *text)
604 shell = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
605 box = pref_group_new(shell, TRUE, text, orientation);
609 if (orientation == GTK_ORIENTATION_HORIZONTAL)
611 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_SPACE);
615 box = gtk_box_new(GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
620 gtk_table_attach(GTK_TABLE(table), shell, column, column + 1, row, row + 1,
621 static_cast<GtkAttachOptions>(GTK_EXPAND | GTK_FILL), static_cast<GtkAttachOptions>(0), 0, 0);
623 gtk_widget_show(shell);
628 GtkWidget *pref_table_label(GtkWidget *table, gint column, gint row,
629 const gchar *text, gfloat alignment)
634 align = gtk_alignment_new(alignment, 0.50, 0.0, 0.0);
635 gtk_table_attach(GTK_TABLE(table), align, column, column + 1, row, row + 1,
636 GTK_FILL, static_cast<GtkAttachOptions>(0), 0, 0);
637 gtk_widget_show(align);
638 label = gtk_label_new(text);
639 gtk_container_add(GTK_CONTAINER(align), label);
640 gtk_widget_show(label);
645 GtkWidget *pref_table_button(GtkWidget *table, gint column, gint row,
646 const gchar *stock_id, const gchar *text, gboolean hide_stock_text,
647 GCallback func, gpointer data)
651 button = pref_button_new(NULL, stock_id, text, hide_stock_text, func, data);
652 gtk_table_attach(GTK_TABLE(table), button, column, column + 1, row, row + 1,
653 GTK_FILL, static_cast<GtkAttachOptions>(0), 0, 0);
654 gtk_widget_show(button);
659 GtkWidget *pref_table_spin(GtkWidget *table, gint column, gint row,
660 const gchar *text, const gchar *suffix,
661 gdouble min, gdouble max, gdouble step, gint digits,
663 GCallback func, gpointer data)
669 spin = gtk_spin_button_new_with_range(min, max, step);
670 gtk_spin_button_set_digits(GTK_SPIN_BUTTON(spin), digits);
671 gtk_spin_button_set_value(GTK_SPIN_BUTTON(spin), value);
674 g_signal_connect(G_OBJECT(spin), "value_changed", G_CALLBACK(func), data);
679 label = pref_table_label(table, column, row, text, 1.0);
680 pref_link_sensitivity(label, spin);
686 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_SPACE);
687 gtk_box_pack_start(GTK_BOX(box), spin, FALSE, FALSE, 0);
688 gtk_widget_show(spin);
690 label = pref_label_new(box, suffix);
691 pref_link_sensitivity(label, spin);
698 gtk_table_attach(GTK_TABLE(table), box, column, column + 1, row, row + 1,
699 static_cast<GtkAttachOptions>(GTK_EXPAND | GTK_FILL), static_cast<GtkAttachOptions>(GTK_EXPAND | GTK_FILL), 0, 0);
700 gtk_widget_show(box);
705 GtkWidget *pref_table_spin_new_int(GtkWidget *table, gint column, gint row,
706 const gchar *text, const gchar *suffix,
707 gint min, gint max, gint step,
708 gint value, gint *value_var)
711 return pref_table_spin(table, column, row,
713 (gdouble)min, (gdouble)max, (gdouble)step, 0,
715 G_CALLBACK(pref_spin_int_cb), value_var);
719 GtkWidget *pref_toolbar_new(GtkWidget *parent_box, GtkToolbarStyle style)
723 tbar = gtk_toolbar_new();
724 gtk_toolbar_set_style(GTK_TOOLBAR(tbar), style);
728 gtk_box_pack_start(GTK_BOX(parent_box), tbar, FALSE, FALSE, 0);
729 gtk_widget_show(tbar);
734 GtkWidget *pref_toolbar_button(GtkWidget *toolbar,
735 const gchar *stock_id, const gchar *label, gboolean toggle,
736 const gchar *description,
737 GCallback func, gpointer data)
745 item = GTK_WIDGET(gtk_toggle_tool_button_new_from_stock(stock_id));
749 item = GTK_WIDGET(gtk_toggle_tool_button_new());
756 item = GTK_WIDGET(gtk_tool_button_new_from_stock(stock_id));
760 item = GTK_WIDGET(gtk_tool_button_new(NULL, NULL));
763 gtk_tool_button_set_use_underline(GTK_TOOL_BUTTON(item), TRUE);
765 if (label) gtk_tool_button_set_label(GTK_TOOL_BUTTON(item), label);
767 if (func) g_signal_connect(item, "clicked", func, data);
768 gtk_container_add(GTK_CONTAINER(toolbar), item);
769 gtk_widget_show(item);
773 gtk_widget_set_tooltip_text(item, description);
779 //void pref_toolbar_button_set_icon(GtkWidget *button, GtkWidget *widget, const gchar *stock_id)
783 //gtk_tool_button_set_icon_widget(GTK_TOOL_BUTTON(button), widget);
787 //gtk_tool_button_set_stock_id(GTK_TOOL_BUTTON(button), stock_id);
791 //GtkWidget *pref_toolbar_spacer(GtkWidget *toolbar)
795 //item = GTK_WIDGET(gtk_separator_tool_item_new());
796 //gtk_container_add(GTK_CONTAINER(toolbar), item);
797 //gtk_widget_show(item);
804 *-----------------------------------------------------------------------------
805 * date selection entry
806 *-----------------------------------------------------------------------------
809 #define DATE_SELECION_KEY "date_selection_data"
812 typedef struct _DateSelection DateSelection;
813 struct _DateSelection
828 static void date_selection_popup_hide(DateSelection *ds)
830 if (!ds->window) return;
832 if (gtk_widget_has_grab(ds->window))
834 gtk_grab_remove(ds->window);
835 gdk_keyboard_ungrab(GDK_CURRENT_TIME);
836 gdk_pointer_ungrab(GDK_CURRENT_TIME);
839 gtk_widget_hide(ds->window);
841 gtk_widget_destroy(ds->window);
845 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ds->button), FALSE);
848 static gboolean date_selection_popup_release_cb(GtkWidget *UNUSED(widget), GdkEventButton *UNUSED(event), gpointer data)
850 DateSelection *ds = static_cast<DateSelection *>(data);
852 date_selection_popup_hide(ds);
856 static gboolean date_selection_popup_press_cb(GtkWidget *UNUSED(widget), GdkEventButton *event, gpointer data)
858 DateSelection *ds = static_cast<DateSelection *>(data);
864 xr = (gint)event->x_root;
865 yr = (gint)event->y_root;
867 window = gtk_widget_get_window(ds->window);
868 gdk_window_get_origin(window, &x, &y);
869 w = gdk_window_get_width(window);
870 h = gdk_window_get_height(window);
872 if (xr < x || yr < y || xr > x + w || yr > y + h)
874 g_signal_connect(G_OBJECT(ds->window), "button_release_event",
875 G_CALLBACK(date_selection_popup_release_cb), ds);
882 static void date_selection_popup_sync(DateSelection *ds)
884 guint day, month, year;
886 gtk_calendar_get_date(GTK_CALENDAR(ds->calendar), &year, &month, &day);
887 date_selection_set(ds->box, day, month + 1, year);
890 static gboolean date_selection_popup_keypress_cb(GtkWidget *UNUSED(widget), GdkEventKey *event, gpointer data)
892 DateSelection *ds = static_cast<DateSelection *>(data);
894 switch (event->keyval)
897 case GDK_KEY_KP_Enter:
899 case GDK_KEY_ISO_Left_Tab:
900 date_selection_popup_sync(ds);
901 date_selection_popup_hide(ds);
904 date_selection_popup_hide(ds);
913 static void date_selection_day_cb(GtkWidget *UNUSED(widget), gpointer data)
915 DateSelection *ds = static_cast<DateSelection *>(data);
917 date_selection_popup_sync(ds);
920 static void date_selection_doubleclick_cb(GtkWidget *UNUSED(widget), gpointer data)
922 DateSelection *ds = static_cast<DateSelection *>(data);
924 date_selection_popup_hide(ds);
927 static void date_selection_popup(DateSelection *ds)
931 gint day, month, year;
932 GtkAllocation button_allocation;
933 GtkAllocation window_allocation;
935 if (ds->window) return;
937 ds->window = gtk_window_new(GTK_WINDOW_POPUP);
938 gtk_window_set_resizable(GTK_WINDOW(ds->window), FALSE);
939 g_signal_connect(G_OBJECT(ds->window), "button_press_event",
940 G_CALLBACK(date_selection_popup_press_cb), ds);
941 g_signal_connect(G_OBJECT(ds->window), "key_press_event",
942 G_CALLBACK(date_selection_popup_keypress_cb), ds);
944 ds->calendar = gtk_calendar_new();
945 gtk_container_add(GTK_CONTAINER(ds->window), ds->calendar);
946 gtk_widget_show(ds->calendar);
948 date_selection_get(ds->box, &day, &month, &year);
949 gtk_calendar_select_month(GTK_CALENDAR(ds->calendar), month - 1, year);
950 gtk_calendar_select_day(GTK_CALENDAR(ds->calendar), day);
952 g_signal_connect(G_OBJECT(ds->calendar), "day_selected",
953 G_CALLBACK(date_selection_day_cb), ds);
954 g_signal_connect(G_OBJECT(ds->calendar), "day_selected_double_click",
955 G_CALLBACK(date_selection_doubleclick_cb), ds);
957 gtk_widget_realize(ds->window);
959 gdk_window_get_origin(gtk_widget_get_window(ds->button), &wx, &wy);
961 gtk_widget_get_allocation(ds->button, &button_allocation);
962 gtk_widget_get_allocation(ds->window, &window_allocation);
964 x = wx + button_allocation.x + button_allocation.width - window_allocation.width;
965 y = wy + button_allocation.y + button_allocation.height;
967 if (y + window_allocation.height > gdk_screen_height())
969 y = wy + button_allocation.y - window_allocation.height;
974 gtk_window_move(GTK_WINDOW(ds->window), x, y);
975 gtk_widget_show(ds->window);
977 gtk_widget_grab_focus(ds->calendar);
978 gdk_pointer_grab(gtk_widget_get_window(ds->window), TRUE,
979 static_cast<GdkEventMask>(GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_MOTION_MASK),
980 NULL, NULL, GDK_CURRENT_TIME);
981 gdk_keyboard_grab(gtk_widget_get_window(ds->window), TRUE, GDK_CURRENT_TIME);
982 gtk_grab_add(ds->window);
984 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ds->button), TRUE);
987 static void date_selection_button_cb(GtkWidget *UNUSED(widget), gpointer data)
989 DateSelection *ds = static_cast<DateSelection *>(data);
991 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(ds->button)) == (!ds->window))
993 date_selection_popup(ds);
997 static void button_size_allocate_cb(GtkWidget *button, GtkAllocation *allocation, gpointer data)
999 GtkWidget *spin = static_cast<GtkWidget *>(data);
1000 GtkRequisition spin_requisition;
1001 gtk_widget_get_requisition(spin, &spin_requisition);
1003 if (allocation->height > spin_requisition.height)
1005 GtkAllocation button_allocation;
1006 GtkAllocation spin_allocation;
1008 gtk_widget_get_allocation(button, &button_allocation);
1009 gtk_widget_get_allocation(spin, &spin_allocation);
1010 button_allocation.height = spin_requisition.height;
1011 button_allocation.y = spin_allocation.y +
1012 (spin_allocation.height - spin_requisition.height) / 2;
1013 gtk_widget_size_allocate(button, &button_allocation);
1017 static void spin_increase(GtkWidget *spin, gint value)
1021 gtk_widget_size_request(spin, &req);
1022 gtk_widget_set_size_request(spin, req.width + value, -1);
1025 static void date_selection_destroy_cb(GtkWidget *UNUSED(widget), gpointer data)
1027 DateSelection *ds = static_cast<DateSelection *>(data);
1029 date_selection_popup_hide(ds);
1034 GtkWidget *date_selection_new(void)
1039 ds = g_new0(DateSelection, 1);
1043 ds->box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
1044 g_signal_connect(G_OBJECT(ds->box), "destroy",
1045 G_CALLBACK(date_selection_destroy_cb), ds);
1047 date_format = nl_langinfo(D_FMT);
1049 if (strlen(date_format) == 8)
1051 for (i=1; i<8; i=i+3)
1053 switch (date_format[i])
1056 ds->spin_d = pref_spin_new(ds->box, NULL, NULL, 1, 31, 1, 0, 1, NULL, NULL);
1059 ds->spin_m = pref_spin_new(ds->box, NULL, NULL, 1, 12, 1, 0, 1, NULL, NULL);
1062 ds->spin_y = pref_spin_new(ds->box, NULL, NULL, 1900, 9999, 1, 0, 1900, NULL, NULL);
1065 log_printf("Warning: Date locale %s is unknown", date_format);
1072 ds->spin_m = pref_spin_new(ds->box, NULL, NULL, 1, 12, 1, 0, 1, NULL, NULL);
1073 ds->spin_d = pref_spin_new(ds->box, NULL, NULL, 1, 31, 1, 0, 1, NULL, NULL);
1074 ds->spin_y = pref_spin_new(ds->box, NULL, NULL, 1900, 9999, 1, 0, 1900, NULL, NULL);
1077 spin_increase(ds->spin_y, 5);
1079 ds->button = gtk_toggle_button_new();
1080 g_signal_connect(G_OBJECT(ds->button), "size_allocate",
1081 G_CALLBACK(button_size_allocate_cb), ds->spin_y);
1083 arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_NONE);
1084 gtk_container_add(GTK_CONTAINER(ds->button), arrow);
1085 gtk_widget_show(arrow);
1087 gtk_box_pack_start(GTK_BOX(ds->box), ds->button, FALSE, FALSE, 0);
1088 g_signal_connect(G_OBJECT(ds->button), "clicked",
1089 G_CALLBACK(date_selection_button_cb), ds);
1090 gtk_widget_show(ds->button);
1092 g_object_set_data(G_OBJECT(ds->box), DATE_SELECION_KEY, ds);
1097 void date_selection_set(GtkWidget *widget, gint day, gint month, gint year)
1101 ds = static_cast<DateSelection *>(g_object_get_data(G_OBJECT(widget), DATE_SELECION_KEY));
1104 gtk_spin_button_set_value(GTK_SPIN_BUTTON(ds->spin_d), (gdouble)day);
1105 gtk_spin_button_set_value(GTK_SPIN_BUTTON(ds->spin_m), (gdouble)month);
1106 gtk_spin_button_set_value(GTK_SPIN_BUTTON(ds->spin_y), (gdouble)year);
1110 void date_selection_get(GtkWidget *widget, gint *day, gint *month, gint *year)
1114 ds = static_cast<DateSelection *>(g_object_get_data(G_OBJECT(widget), DATE_SELECION_KEY));
1117 if (day) *day = gtk_spin_button_get_value(GTK_SPIN_BUTTON(ds->spin_d));
1118 if (month) *month = gtk_spin_button_get_value(GTK_SPIN_BUTTON(ds->spin_m));
1119 if (year) *year = gtk_spin_button_get_value(GTK_SPIN_BUTTON(ds->spin_y));
1122 void date_selection_time_set(GtkWidget *widget, time_t t)
1129 date_selection_set(widget, lt->tm_mday, lt->tm_mon + 1, lt->tm_year + 1900);
1132 //time_t date_selection_time_get(GtkWidget *widget)
1139 //date_selection_get(widget, &day, &month ,&year);
1145 //lt.tm_mon = month - 1;
1146 //lt.tm_year = year - 1900;
1149 //return mktime(<);
1153 *-----------------------------------------------------------------------------
1154 * storing data in a history list with key,data pairs
1155 *-----------------------------------------------------------------------------
1158 #define PREF_LIST_MARKER_INT "[INT]:"
1159 #define PREF_LIST_MARKER_DOUBLE "[DOUBLE]:"
1160 #define PREF_LIST_MARKER_STRING "[STRING]:"
1162 static GList *pref_list_find(const gchar *group, const gchar *token)
1169 work = history_list_get_by_key(group);
1172 const gchar *text = static_cast<const gchar *>(work->data);
1174 if (strncmp(text, token, l) == 0) return work;
1182 static gboolean pref_list_get(const gchar *group, const gchar *key, const gchar *marker, const gchar **result)
1188 if (!group || !key || !marker)
1194 token = g_strconcat(key, marker, NULL);
1196 work = pref_list_find(group, token);
1199 *result = (const gchar *)work->data + strlen(token);
1200 if (strlen(*result) == 0) *result = NULL;
1214 static void pref_list_set(const gchar *group, const gchar *key, const gchar *marker, const gchar *text)
1220 if (!group || !key || !marker) return;
1222 token = g_strconcat(key, marker, NULL);
1223 path = g_strconcat(token, text, NULL);
1225 work = pref_list_find(group, token);
1228 gchar *old_path = static_cast<gchar *>(work->data);
1239 history_list_item_remove(group, old_path);
1244 history_list_add_to_key(group, path, 0);
1251 void pref_list_int_set(const gchar *group, const gchar *key, gint value)
1255 text = g_strdup_printf("%d", value);
1256 pref_list_set(group, key, PREF_LIST_MARKER_INT, text);
1260 gboolean pref_list_int_get(const gchar *group, const gchar *key, gint *result)
1270 if (pref_list_get(group, key, PREF_LIST_MARKER_INT, &text) && text)
1272 *result = (gint)strtol(text, NULL, 10);
1280 //void pref_list_double_set(const gchar *group, const gchar *key, gdouble value)
1282 //gchar text[G_ASCII_DTOSTR_BUF_SIZE];
1284 //g_ascii_dtostr(text, sizeof(text), value);
1285 //pref_list_set(group, key, PREF_LIST_MARKER_DOUBLE, text);
1288 //gboolean pref_list_double_get(const gchar *group, const gchar *key, gdouble *result)
1290 //const gchar *text;
1292 //if (!group || !key)
1298 //if (pref_list_get(group, key, PREF_LIST_MARKER_DOUBLE, &text) && text)
1300 //*result = g_ascii_strtod(text, NULL);
1308 //void pref_list_string_set(const gchar *group, const gchar *key, const gchar *value)
1310 //pref_list_set(group, key, PREF_LIST_MARKER_STRING, value);
1313 //gboolean pref_list_string_get(const gchar *group, const gchar *key, const gchar **result)
1315 //return pref_list_get(group, key, PREF_LIST_MARKER_STRING, result);
1319 void pref_color_button_set_cb(GtkWidget *widget, gpointer data)
1321 GdkColor *color = static_cast<GdkColor *>(data);
1323 gtk_color_button_get_color(GTK_COLOR_BUTTON(widget), color);
1326 GtkWidget *pref_color_button_new(GtkWidget *parent_box,
1327 const gchar *title, const GdkColor *color,
1328 GCallback func, gpointer data)
1334 button = gtk_color_button_new_with_color(color);
1338 button = gtk_color_button_new();
1341 if (func) g_signal_connect(G_OBJECT(button), "color-set", func, data);
1348 gtk_color_button_set_title(GTK_COLOR_BUTTON(button), title);
1349 label = gtk_label_new(title);
1351 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
1352 gtk_box_pack_start(GTK_BOX(parent_box), hbox, TRUE, TRUE, 0);
1354 gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 0);
1355 gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
1357 gtk_widget_show_all(hbox);
1361 gtk_widget_show(button);
1368 *-----------------------------------------------------------------------------
1370 *-----------------------------------------------------------------------------
1373 gchar *text_widget_text_pull(GtkWidget *text_widget)
1375 if (GTK_IS_TEXT_VIEW(text_widget))
1377 GtkTextBuffer *buffer;
1378 GtkTextIter start, end;
1380 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_widget));
1381 gtk_text_buffer_get_bounds(buffer, &start, &end);
1383 return gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
1385 else if (GTK_IS_ENTRY(text_widget))
1387 return g_strdup(gtk_entry_get_text(GTK_ENTRY(text_widget)));
1396 gchar *text_widget_text_pull_selected(GtkWidget *text_widget)
1398 if (GTK_IS_TEXT_VIEW(text_widget))
1400 GtkTextBuffer *buffer;
1401 GtkTextIter start, end;
1403 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_widget));
1404 gtk_text_buffer_get_bounds(buffer, &start, &end);
1406 if (gtk_text_buffer_get_selection_bounds(buffer, &start, &end))
1408 gtk_text_iter_set_line_offset(&start, 0);
1409 gtk_text_iter_forward_to_line_end(&end);
1412 return gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
1414 else if (GTK_IS_ENTRY(text_widget))
1416 return g_strdup(gtk_entry_get_text(GTK_ENTRY(text_widget)));
1424 gboolean defined_mouse_buttons(GtkWidget *UNUSED(widget), GdkEventButton *event, gpointer data)
1426 LayoutWindow *lw = static_cast<LayoutWindow *>(data);
1428 gboolean ret = FALSE;
1430 switch (event->button)
1432 case MOUSE_BUTTON_8:
1433 if (options->mouse_button_8)
1435 action = gtk_action_group_get_action(lw->action_group, options->mouse_button_8);
1438 gtk_action_activate(action);
1443 case MOUSE_BUTTON_9:
1444 if (options->mouse_button_9)
1446 action = gtk_action_group_get_action(lw->action_group, options->mouse_button_9);
1449 gtk_action_activate(action);
1461 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */