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();
256 if (stock_id) image = gtk_image_new_from_icon_name(stock_id, GTK_ICON_SIZE_BUTTON);
259 label = gtk_label_new_with_mnemonic(text);
260 gtk_label_set_xalign(GTK_LABEL(label), 0.5);
261 gtk_label_set_yalign(GTK_LABEL(label), 0.5);
262 gtk_label_set_mnemonic_widget(GTK_LABEL(label), button);
270 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_BUTTON_ICON_GAP);
272 align = gtk_alignment_new(0.5, 0.5, 0.0, 0.0);
273 gtk_container_add(GTK_CONTAINER(button), align);
274 gtk_widget_show(align);
276 gtk_container_add(GTK_CONTAINER(align), hbox);
277 gtk_widget_show(hbox);
279 gtk_box_pack_start(GTK_BOX(hbox), image, FALSE, FALSE, 0);
280 gtk_box_pack_end(GTK_BOX(hbox), label, FALSE, FALSE, 0);
286 gtk_container_add(GTK_CONTAINER(button), image);
290 gtk_container_add(GTK_CONTAINER(button), label);
294 if (image) gtk_widget_show(image);
295 if (label) gtk_widget_show(label);
298 if (func) g_signal_connect(G_OBJECT(button), "clicked", func, data);
302 gtk_box_pack_start(GTK_BOX(parent_box), button, FALSE, FALSE, 0);
303 gtk_widget_show(button);
309 static GtkWidget *real_pref_checkbox_new(GtkWidget *parent_box, const gchar *text, gboolean mnemonic_text,
310 gboolean active, GCallback func, gpointer data)
316 button = gtk_check_button_new_with_mnemonic(text);
320 button = gtk_check_button_new_with_label(text);
322 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), active);
323 if (func) g_signal_connect(G_OBJECT(button), "clicked", func, data);
325 gtk_box_pack_start(GTK_BOX(parent_box), button, FALSE, FALSE, 0);
326 gtk_widget_show(button);
331 GtkWidget *pref_checkbox_new(GtkWidget *parent_box, const gchar *text, gboolean active,
332 GCallback func, gpointer data)
334 return real_pref_checkbox_new(parent_box, text, FALSE, active, func, data);
337 //GtkWidget *pref_checkbox_new_mnemonic(GtkWidget *parent_box, const gchar *text, gboolean active,
338 //GCallback func, gpointer data)
340 //return real_pref_checkbox_new(parent_box, text, TRUE, active, func, data);
343 static void pref_checkbox_int_cb(GtkWidget *widget, gpointer data)
345 gboolean *result = static_cast<gboolean *>(data);
347 *result = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
350 GtkWidget *pref_checkbox_new_int(GtkWidget *parent_box, const gchar *text, gboolean active,
355 button = pref_checkbox_new(parent_box, text, active,
356 G_CALLBACK(pref_checkbox_int_cb), result);
362 static void pref_checkbox_link_sensitivity_cb(GtkWidget *button, gpointer data)
364 GtkWidget *widget = static_cast<GtkWidget *>(data);
366 gtk_widget_set_sensitive(widget, gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button)));
369 void pref_checkbox_link_sensitivity(GtkWidget *button, GtkWidget *widget)
371 g_signal_connect(G_OBJECT(button), "toggled",
372 G_CALLBACK(pref_checkbox_link_sensitivity_cb), widget);
374 pref_checkbox_link_sensitivity_cb(button, widget);
377 //static void pref_checkbox_link_sensitivity_swap_cb(GtkWidget *button, gpointer data)
379 //GtkWidget *widget = static_cast<GtkWidget *>(data);
381 //gtk_widget_set_sensitive(widget, !gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button)));
384 //void pref_checkbox_link_sensitivity_swap(GtkWidget *button, GtkWidget *widget)
386 //g_signal_connect(G_OBJECT(button), "toggled",
387 //G_CALLBACK(pref_checkbox_link_sensitivity_swap_cb), widget);
389 //pref_checkbox_link_sensitivity_swap_cb(button, widget);
392 static GtkWidget *real_pref_radiobutton_new(GtkWidget *parent_box, GtkWidget *sibling,
393 const gchar *text, gboolean mnemonic_text, gboolean active,
394 GCallback func, gpointer data)
401 group = gtk_radio_button_get_group(GTK_RADIO_BUTTON(sibling));
410 button = gtk_radio_button_new_with_mnemonic(group, text);
414 button = gtk_radio_button_new_with_label(group, text);
417 if (active) gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), active);
418 if (func) g_signal_connect(G_OBJECT(button), "clicked", func, data);
420 gtk_box_pack_start(GTK_BOX(parent_box), button, FALSE, FALSE, 0);
421 gtk_widget_show(button);
426 GtkWidget *pref_radiobutton_new(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, FALSE, active, func, data);
433 //GtkWidget *pref_radiobutton_new_mnemonic(GtkWidget *parent_box, GtkWidget *sibling,
434 //const gchar *text, gboolean active,
435 //GCallback func, gpointer data)
437 //return real_pref_radiobutton_new(parent_box, sibling, text, TRUE, active, func, data);
440 #define PREF_RADIO_VALUE_KEY "pref_radio_value"
442 //static void pref_radiobutton_int_cb(GtkWidget *widget, gpointer data)
444 //gboolean *result = static_cast<gboolean *>(data);
446 //if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
448 //*result = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), PREF_RADIO_VALUE_KEY));
452 //GtkWidget *pref_radiobutton_new_int(GtkWidget *parent_box, GtkWidget *sibling,
453 //const gchar *text, gboolean active,
454 //gboolean *result, gboolean value,
455 //GCallback UNUSED(func), gpointer UNUSED(data))
459 //button = pref_radiobutton_new(parent_box, sibling, text, active,
460 //G_CALLBACK(pref_radiobutton_int_cb), result);
461 //g_object_set_data(G_OBJECT(button), PREF_RADIO_VALUE_KEY, GINT_TO_POINTER(value));
462 //if (active) *result = value;
467 static GtkWidget *real_pref_spin_new(GtkWidget *parent_box, const gchar *text, const gchar *suffix,
468 gboolean mnemonic_text,
469 gdouble min, gdouble max, gdouble step, gint digits,
471 GCallback func, gpointer data)
477 box = pref_box_new(parent_box, FALSE, GTK_ORIENTATION_HORIZONTAL, PREF_PAD_SPACE);
479 spin = gtk_spin_button_new_with_range(min, max, step);
480 gtk_spin_button_set_digits(GTK_SPIN_BUTTON(spin), digits);
481 gtk_spin_button_set_value(GTK_SPIN_BUTTON(spin), value);
485 g_signal_connect(G_OBJECT(spin), "value_changed", G_CALLBACK(func), data);
492 label = pref_label_new_mnemonic(box, text, spin);
496 label = pref_label_new(box, text);
498 pref_link_sensitivity(label, spin);
501 gtk_box_pack_start(GTK_BOX(box), spin, FALSE, FALSE, 0);
502 gtk_widget_show(spin);
504 /* perhaps this should only be PREF_PAD_GAP distance from spinbutton ? */
507 label = pref_label_new(box, suffix);
508 pref_link_sensitivity(label, spin);
514 GtkWidget *pref_spin_new(GtkWidget *parent_box, const gchar *text, const gchar *suffix,
515 gdouble min, gdouble max, gdouble step, gint digits,
517 GCallback func, gpointer data)
519 return real_pref_spin_new(parent_box, text, suffix, FALSE,
520 min, max, step, digits, value, func, data);
523 //GtkWidget *pref_spin_new_mnemonic(GtkWidget *parent_box, const gchar *text, const gchar *suffix,
524 //gdouble min, gdouble max, gdouble step, gint digits,
526 //GCallback func, gpointer data)
528 //return real_pref_spin_new(parent_box, text, suffix, TRUE,
529 //min, max, step, digits, value, func, data);
532 static void pref_spin_int_cb(GtkWidget *widget, gpointer data)
534 gint *var = static_cast<gint *>(data);
535 *var = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(widget));
538 GtkWidget *pref_spin_new_int(GtkWidget *parent_box, const gchar *text, const gchar *suffix,
539 gint min, gint max, gint step,
540 gint value, gint *value_var)
543 return pref_spin_new(parent_box, text, suffix,
544 (gdouble)min, (gdouble)max, (gdouble)step, 0,
546 G_CALLBACK(pref_spin_int_cb), value_var);
549 static void pref_link_sensitivity_cb(GtkWidget *watch, GtkStateType UNUSED(prev_state), gpointer data)
551 GtkWidget *widget = static_cast<GtkWidget *>(data);
553 gtk_widget_set_sensitive(widget, gtk_widget_is_sensitive(watch));
556 void pref_link_sensitivity(GtkWidget *widget, GtkWidget *watch)
558 g_signal_connect(G_OBJECT(watch), "state_changed",
559 G_CALLBACK(pref_link_sensitivity_cb), widget);
562 void pref_signal_block_data(GtkWidget *widget, gpointer data)
564 g_signal_handlers_block_matched(widget, G_SIGNAL_MATCH_DATA,
565 0, 0, NULL, NULL, data);
568 void pref_signal_unblock_data(GtkWidget *widget, gpointer data)
570 g_signal_handlers_unblock_matched(widget, G_SIGNAL_MATCH_DATA,
571 0, 0, NULL, NULL, data);
574 GtkWidget *pref_table_new(GtkWidget *parent_box, gint columns, gint rows,
575 gboolean homogeneous, gboolean fill)
579 table = gtk_table_new(rows, columns, homogeneous);
580 gtk_table_set_row_spacings(GTK_TABLE(table), PREF_PAD_GAP);
581 gtk_table_set_col_spacings(GTK_TABLE(table), PREF_PAD_SPACE);
585 gtk_box_pack_start(GTK_BOX(parent_box), table, fill, fill, 0);
586 gtk_widget_show(table);
592 GtkWidget *pref_table_box(GtkWidget *table, gint column, gint row,
593 GtkOrientation orientation, const gchar *text)
600 shell = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
601 box = pref_group_new(shell, TRUE, text, orientation);
605 if (orientation == GTK_ORIENTATION_HORIZONTAL)
607 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_SPACE);
611 box = gtk_box_new(GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
616 gtk_table_attach(GTK_TABLE(table), shell, column, column + 1, row, row + 1,
617 static_cast<GtkAttachOptions>(GTK_EXPAND | GTK_FILL), static_cast<GtkAttachOptions>(0), 0, 0);
619 gtk_widget_show(shell);
624 GtkWidget *pref_table_label(GtkWidget *table, gint column, gint row,
625 const gchar *text, gfloat alignment)
630 align = gtk_alignment_new(alignment, 0.50, 0.0, 0.0);
631 gtk_table_attach(GTK_TABLE(table), align, column, column + 1, row, row + 1,
632 GTK_FILL, static_cast<GtkAttachOptions>(0), 0, 0);
633 gtk_widget_show(align);
634 label = gtk_label_new(text);
635 gtk_container_add(GTK_CONTAINER(align), label);
636 gtk_widget_show(label);
641 GtkWidget *pref_table_button(GtkWidget *table, gint column, gint row,
642 const gchar *stock_id, const gchar *text, gboolean hide_stock_text,
643 GCallback func, gpointer data)
647 button = pref_button_new(NULL, stock_id, text, hide_stock_text, func, data);
648 gtk_table_attach(GTK_TABLE(table), button, column, column + 1, row, row + 1,
649 GTK_FILL, static_cast<GtkAttachOptions>(0), 0, 0);
650 gtk_widget_show(button);
655 GtkWidget *pref_table_spin(GtkWidget *table, gint column, gint row,
656 const gchar *text, const gchar *suffix,
657 gdouble min, gdouble max, gdouble step, gint digits,
659 GCallback func, gpointer data)
665 spin = gtk_spin_button_new_with_range(min, max, step);
666 gtk_spin_button_set_digits(GTK_SPIN_BUTTON(spin), digits);
667 gtk_spin_button_set_value(GTK_SPIN_BUTTON(spin), value);
670 g_signal_connect(G_OBJECT(spin), "value_changed", G_CALLBACK(func), data);
675 label = pref_table_label(table, column, row, text, 1.0);
676 pref_link_sensitivity(label, spin);
682 box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_SPACE);
683 gtk_box_pack_start(GTK_BOX(box), spin, FALSE, FALSE, 0);
684 gtk_widget_show(spin);
686 label = pref_label_new(box, suffix);
687 pref_link_sensitivity(label, spin);
694 gtk_table_attach(GTK_TABLE(table), box, column, column + 1, row, row + 1,
695 static_cast<GtkAttachOptions>(GTK_EXPAND | GTK_FILL), static_cast<GtkAttachOptions>(GTK_EXPAND | GTK_FILL), 0, 0);
696 gtk_widget_show(box);
701 GtkWidget *pref_table_spin_new_int(GtkWidget *table, gint column, gint row,
702 const gchar *text, const gchar *suffix,
703 gint min, gint max, gint step,
704 gint value, gint *value_var)
707 return pref_table_spin(table, column, row,
709 (gdouble)min, (gdouble)max, (gdouble)step, 0,
711 G_CALLBACK(pref_spin_int_cb), value_var);
715 GtkWidget *pref_toolbar_new(GtkWidget *parent_box, GtkToolbarStyle style)
719 tbar = gtk_toolbar_new();
720 gtk_toolbar_set_style(GTK_TOOLBAR(tbar), style);
724 gtk_box_pack_start(GTK_BOX(parent_box), tbar, FALSE, FALSE, 0);
725 gtk_widget_show(tbar);
730 GtkWidget *pref_toolbar_button(GtkWidget *toolbar,
731 const gchar *stock_id, const gchar *label, gboolean toggle,
732 const gchar *description,
733 GCallback func, gpointer data)
741 item = GTK_WIDGET(gtk_toggle_tool_button_new_from_stock(stock_id));
745 item = GTK_WIDGET(gtk_toggle_tool_button_new());
752 item = GTK_WIDGET(gtk_tool_button_new_from_stock(stock_id));
756 item = GTK_WIDGET(gtk_tool_button_new(NULL, NULL));
759 gtk_tool_button_set_use_underline(GTK_TOOL_BUTTON(item), TRUE);
761 if (label) gtk_tool_button_set_label(GTK_TOOL_BUTTON(item), label);
763 if (func) g_signal_connect(item, "clicked", func, data);
764 gtk_container_add(GTK_CONTAINER(toolbar), item);
765 gtk_widget_show(item);
769 gtk_widget_set_tooltip_text(item, description);
775 //void pref_toolbar_button_set_icon(GtkWidget *button, GtkWidget *widget, const gchar *stock_id)
779 //gtk_tool_button_set_icon_widget(GTK_TOOL_BUTTON(button), widget);
783 //gtk_tool_button_set_stock_id(GTK_TOOL_BUTTON(button), stock_id);
787 //GtkWidget *pref_toolbar_spacer(GtkWidget *toolbar)
791 //item = GTK_WIDGET(gtk_separator_tool_item_new());
792 //gtk_container_add(GTK_CONTAINER(toolbar), item);
793 //gtk_widget_show(item);
800 *-----------------------------------------------------------------------------
801 * date selection entry
802 *-----------------------------------------------------------------------------
805 #define DATE_SELECION_KEY "date_selection_data"
808 typedef struct _DateSelection DateSelection;
809 struct _DateSelection
824 static void date_selection_popup_hide(DateSelection *ds)
826 if (!ds->window) return;
828 if (gtk_widget_has_grab(ds->window))
830 gtk_grab_remove(ds->window);
831 gdk_keyboard_ungrab(GDK_CURRENT_TIME);
832 gdk_pointer_ungrab(GDK_CURRENT_TIME);
835 gtk_widget_hide(ds->window);
837 gtk_widget_destroy(ds->window);
841 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ds->button), FALSE);
844 static gboolean date_selection_popup_release_cb(GtkWidget *UNUSED(widget), GdkEventButton *UNUSED(event), gpointer data)
846 DateSelection *ds = static_cast<DateSelection *>(data);
848 date_selection_popup_hide(ds);
852 static gboolean date_selection_popup_press_cb(GtkWidget *UNUSED(widget), GdkEventButton *event, gpointer data)
854 DateSelection *ds = static_cast<DateSelection *>(data);
860 xr = (gint)event->x_root;
861 yr = (gint)event->y_root;
863 window = gtk_widget_get_window(ds->window);
864 gdk_window_get_origin(window, &x, &y);
865 w = gdk_window_get_width(window);
866 h = gdk_window_get_height(window);
868 if (xr < x || yr < y || xr > x + w || yr > y + h)
870 g_signal_connect(G_OBJECT(ds->window), "button_release_event",
871 G_CALLBACK(date_selection_popup_release_cb), ds);
878 static void date_selection_popup_sync(DateSelection *ds)
880 guint day, month, year;
882 gtk_calendar_get_date(GTK_CALENDAR(ds->calendar), &year, &month, &day);
883 date_selection_set(ds->box, day, month + 1, year);
886 static gboolean date_selection_popup_keypress_cb(GtkWidget *UNUSED(widget), GdkEventKey *event, gpointer data)
888 DateSelection *ds = static_cast<DateSelection *>(data);
890 switch (event->keyval)
893 case GDK_KEY_KP_Enter:
895 case GDK_KEY_ISO_Left_Tab:
896 date_selection_popup_sync(ds);
897 date_selection_popup_hide(ds);
900 date_selection_popup_hide(ds);
909 static void date_selection_day_cb(GtkWidget *UNUSED(widget), gpointer data)
911 DateSelection *ds = static_cast<DateSelection *>(data);
913 date_selection_popup_sync(ds);
916 static void date_selection_doubleclick_cb(GtkWidget *UNUSED(widget), gpointer data)
918 DateSelection *ds = static_cast<DateSelection *>(data);
920 date_selection_popup_hide(ds);
923 static void date_selection_popup(DateSelection *ds)
927 gint day, month, year;
928 GtkAllocation button_allocation;
929 GtkAllocation window_allocation;
931 if (ds->window) return;
933 ds->window = gtk_window_new(GTK_WINDOW_POPUP);
934 gtk_window_set_resizable(GTK_WINDOW(ds->window), FALSE);
935 g_signal_connect(G_OBJECT(ds->window), "button_press_event",
936 G_CALLBACK(date_selection_popup_press_cb), ds);
937 g_signal_connect(G_OBJECT(ds->window), "key_press_event",
938 G_CALLBACK(date_selection_popup_keypress_cb), ds);
940 ds->calendar = gtk_calendar_new();
941 gtk_container_add(GTK_CONTAINER(ds->window), ds->calendar);
942 gtk_widget_show(ds->calendar);
944 date_selection_get(ds->box, &day, &month, &year);
945 gtk_calendar_select_month(GTK_CALENDAR(ds->calendar), month - 1, year);
946 gtk_calendar_select_day(GTK_CALENDAR(ds->calendar), day);
948 g_signal_connect(G_OBJECT(ds->calendar), "day_selected",
949 G_CALLBACK(date_selection_day_cb), ds);
950 g_signal_connect(G_OBJECT(ds->calendar), "day_selected_double_click",
951 G_CALLBACK(date_selection_doubleclick_cb), ds);
953 gtk_widget_realize(ds->window);
955 gdk_window_get_origin(gtk_widget_get_window(ds->button), &wx, &wy);
957 gtk_widget_get_allocation(ds->button, &button_allocation);
958 gtk_widget_get_allocation(ds->window, &window_allocation);
960 x = wx + button_allocation.x + button_allocation.width - window_allocation.width;
961 y = wy + button_allocation.y + button_allocation.height;
963 if (y + window_allocation.height > gdk_screen_height())
965 y = wy + button_allocation.y - window_allocation.height;
970 gtk_window_move(GTK_WINDOW(ds->window), x, y);
971 gtk_widget_show(ds->window);
973 gtk_widget_grab_focus(ds->calendar);
974 gdk_pointer_grab(gtk_widget_get_window(ds->window), TRUE,
975 static_cast<GdkEventMask>(GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_MOTION_MASK),
976 NULL, NULL, GDK_CURRENT_TIME);
977 gdk_keyboard_grab(gtk_widget_get_window(ds->window), TRUE, GDK_CURRENT_TIME);
978 gtk_grab_add(ds->window);
980 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ds->button), TRUE);
983 static void date_selection_button_cb(GtkWidget *UNUSED(widget), gpointer data)
985 DateSelection *ds = static_cast<DateSelection *>(data);
987 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(ds->button)) == (!ds->window))
989 date_selection_popup(ds);
993 static void button_size_allocate_cb(GtkWidget *button, GtkAllocation *allocation, gpointer data)
995 GtkWidget *spin = static_cast<GtkWidget *>(data);
996 GtkRequisition spin_requisition;
997 gtk_widget_get_requisition(spin, &spin_requisition);
999 if (allocation->height > spin_requisition.height)
1001 GtkAllocation button_allocation;
1002 GtkAllocation spin_allocation;
1004 gtk_widget_get_allocation(button, &button_allocation);
1005 gtk_widget_get_allocation(spin, &spin_allocation);
1006 button_allocation.height = spin_requisition.height;
1007 button_allocation.y = spin_allocation.y +
1008 (spin_allocation.height - spin_requisition.height) / 2;
1009 gtk_widget_size_allocate(button, &button_allocation);
1013 static void spin_increase(GtkWidget *spin, gint value)
1017 gtk_widget_size_request(spin, &req);
1018 gtk_widget_set_size_request(spin, req.width + value, -1);
1021 static void date_selection_destroy_cb(GtkWidget *UNUSED(widget), gpointer data)
1023 DateSelection *ds = static_cast<DateSelection *>(data);
1025 date_selection_popup_hide(ds);
1030 GtkWidget *date_selection_new(void)
1035 ds = g_new0(DateSelection, 1);
1039 ds->box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
1040 g_signal_connect(G_OBJECT(ds->box), "destroy",
1041 G_CALLBACK(date_selection_destroy_cb), ds);
1043 date_format = nl_langinfo(D_FMT);
1045 if (strlen(date_format) == 8)
1047 for (i=1; i<8; i=i+3)
1049 switch (date_format[i])
1052 ds->spin_d = pref_spin_new(ds->box, NULL, NULL, 1, 31, 1, 0, 1, NULL, NULL);
1055 ds->spin_m = pref_spin_new(ds->box, NULL, NULL, 1, 12, 1, 0, 1, NULL, NULL);
1058 ds->spin_y = pref_spin_new(ds->box, NULL, NULL, 1900, 9999, 1, 0, 1900, NULL, NULL);
1061 log_printf("Warning: Date locale %s is unknown", date_format);
1068 ds->spin_m = pref_spin_new(ds->box, NULL, NULL, 1, 12, 1, 0, 1, NULL, NULL);
1069 ds->spin_d = pref_spin_new(ds->box, NULL, NULL, 1, 31, 1, 0, 1, NULL, NULL);
1070 ds->spin_y = pref_spin_new(ds->box, NULL, NULL, 1900, 9999, 1, 0, 1900, NULL, NULL);
1073 spin_increase(ds->spin_y, 5);
1075 ds->button = gtk_toggle_button_new();
1076 g_signal_connect(G_OBJECT(ds->button), "size_allocate",
1077 G_CALLBACK(button_size_allocate_cb), ds->spin_y);
1079 arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_NONE);
1080 gtk_container_add(GTK_CONTAINER(ds->button), arrow);
1081 gtk_widget_show(arrow);
1083 gtk_box_pack_start(GTK_BOX(ds->box), ds->button, FALSE, FALSE, 0);
1084 g_signal_connect(G_OBJECT(ds->button), "clicked",
1085 G_CALLBACK(date_selection_button_cb), ds);
1086 gtk_widget_show(ds->button);
1088 g_object_set_data(G_OBJECT(ds->box), DATE_SELECION_KEY, ds);
1093 void date_selection_set(GtkWidget *widget, gint day, gint month, gint year)
1097 ds = static_cast<DateSelection *>(g_object_get_data(G_OBJECT(widget), DATE_SELECION_KEY));
1100 gtk_spin_button_set_value(GTK_SPIN_BUTTON(ds->spin_d), (gdouble)day);
1101 gtk_spin_button_set_value(GTK_SPIN_BUTTON(ds->spin_m), (gdouble)month);
1102 gtk_spin_button_set_value(GTK_SPIN_BUTTON(ds->spin_y), (gdouble)year);
1106 void date_selection_get(GtkWidget *widget, gint *day, gint *month, gint *year)
1110 ds = static_cast<DateSelection *>(g_object_get_data(G_OBJECT(widget), DATE_SELECION_KEY));
1113 if (day) *day = gtk_spin_button_get_value(GTK_SPIN_BUTTON(ds->spin_d));
1114 if (month) *month = gtk_spin_button_get_value(GTK_SPIN_BUTTON(ds->spin_m));
1115 if (year) *year = gtk_spin_button_get_value(GTK_SPIN_BUTTON(ds->spin_y));
1118 void date_selection_time_set(GtkWidget *widget, time_t t)
1125 date_selection_set(widget, lt->tm_mday, lt->tm_mon + 1, lt->tm_year + 1900);
1128 //time_t date_selection_time_get(GtkWidget *widget)
1135 //date_selection_get(widget, &day, &month ,&year);
1141 //lt.tm_mon = month - 1;
1142 //lt.tm_year = year - 1900;
1145 //return mktime(<);
1149 *-----------------------------------------------------------------------------
1150 * storing data in a history list with key,data pairs
1151 *-----------------------------------------------------------------------------
1154 #define PREF_LIST_MARKER_INT "[INT]:"
1155 #define PREF_LIST_MARKER_DOUBLE "[DOUBLE]:"
1156 #define PREF_LIST_MARKER_STRING "[STRING]:"
1158 static GList *pref_list_find(const gchar *group, const gchar *token)
1165 work = history_list_get_by_key(group);
1168 const gchar *text = static_cast<const gchar *>(work->data);
1170 if (strncmp(text, token, l) == 0) return work;
1178 static gboolean pref_list_get(const gchar *group, const gchar *key, const gchar *marker, const gchar **result)
1184 if (!group || !key || !marker)
1190 token = g_strconcat(key, marker, NULL);
1192 work = pref_list_find(group, token);
1195 *result = (const gchar *)work->data + strlen(token);
1196 if (strlen(*result) == 0) *result = NULL;
1210 static void pref_list_set(const gchar *group, const gchar *key, const gchar *marker, const gchar *text)
1216 if (!group || !key || !marker) return;
1218 token = g_strconcat(key, marker, NULL);
1219 path = g_strconcat(token, text, NULL);
1221 work = pref_list_find(group, token);
1224 gchar *old_path = static_cast<gchar *>(work->data);
1235 history_list_item_remove(group, old_path);
1240 history_list_add_to_key(group, path, 0);
1247 void pref_list_int_set(const gchar *group, const gchar *key, gint value)
1251 text = g_strdup_printf("%d", value);
1252 pref_list_set(group, key, PREF_LIST_MARKER_INT, text);
1256 gboolean pref_list_int_get(const gchar *group, const gchar *key, gint *result)
1266 if (pref_list_get(group, key, PREF_LIST_MARKER_INT, &text) && text)
1268 *result = (gint)strtol(text, NULL, 10);
1276 //void pref_list_double_set(const gchar *group, const gchar *key, gdouble value)
1278 //gchar text[G_ASCII_DTOSTR_BUF_SIZE];
1280 //g_ascii_dtostr(text, sizeof(text), value);
1281 //pref_list_set(group, key, PREF_LIST_MARKER_DOUBLE, text);
1284 //gboolean pref_list_double_get(const gchar *group, const gchar *key, gdouble *result)
1286 //const gchar *text;
1288 //if (!group || !key)
1294 //if (pref_list_get(group, key, PREF_LIST_MARKER_DOUBLE, &text) && text)
1296 //*result = g_ascii_strtod(text, NULL);
1304 //void pref_list_string_set(const gchar *group, const gchar *key, const gchar *value)
1306 //pref_list_set(group, key, PREF_LIST_MARKER_STRING, value);
1309 //gboolean pref_list_string_get(const gchar *group, const gchar *key, const gchar **result)
1311 //return pref_list_get(group, key, PREF_LIST_MARKER_STRING, result);
1315 void pref_color_button_set_cb(GtkWidget *widget, gpointer data)
1317 GdkColor *color = static_cast<GdkColor *>(data);
1319 gtk_color_button_get_color(GTK_COLOR_BUTTON(widget), color);
1322 GtkWidget *pref_color_button_new(GtkWidget *parent_box,
1323 const gchar *title, const GdkColor *color,
1324 GCallback func, gpointer data)
1330 button = gtk_color_button_new_with_color(color);
1334 button = gtk_color_button_new();
1337 if (func) g_signal_connect(G_OBJECT(button), "color-set", func, data);
1344 gtk_color_button_set_title(GTK_COLOR_BUTTON(button), title);
1345 label = gtk_label_new(title);
1347 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
1348 gtk_box_pack_start(GTK_BOX(parent_box), hbox, TRUE, TRUE, 0);
1350 gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 0);
1351 gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
1353 gtk_widget_show_all(hbox);
1357 gtk_widget_show(button);
1364 *-----------------------------------------------------------------------------
1366 *-----------------------------------------------------------------------------
1369 gchar *text_widget_text_pull(GtkWidget *text_widget)
1371 if (GTK_IS_TEXT_VIEW(text_widget))
1373 GtkTextBuffer *buffer;
1374 GtkTextIter start, end;
1376 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_widget));
1377 gtk_text_buffer_get_bounds(buffer, &start, &end);
1379 return gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
1381 else if (GTK_IS_ENTRY(text_widget))
1383 return g_strdup(gtk_entry_get_text(GTK_ENTRY(text_widget)));
1392 gchar *text_widget_text_pull_selected(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 if (gtk_text_buffer_get_selection_bounds(buffer, &start, &end))
1404 gtk_text_iter_set_line_offset(&start, 0);
1405 gtk_text_iter_forward_to_line_end(&end);
1408 return gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
1410 else if (GTK_IS_ENTRY(text_widget))
1412 return g_strdup(gtk_entry_get_text(GTK_ENTRY(text_widget)));
1420 gboolean defined_mouse_buttons(GtkWidget *UNUSED(widget), GdkEventButton *event, gpointer data)
1422 LayoutWindow *lw = static_cast<LayoutWindow *>(data);
1424 gboolean ret = FALSE;
1426 switch (event->button)
1428 case MOUSE_BUTTON_8:
1429 if (options->mouse_button_8)
1431 action = gtk_action_group_get_action(lw->action_group, options->mouse_button_8);
1434 gtk_action_activate(action);
1439 case MOUSE_BUTTON_9:
1440 if (options->mouse_button_9)
1442 action = gtk_action_group_get_action(lw->action_group, options->mouse_button_9);
1445 gtk_action_activate(action);
1457 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */