1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
5 * Copyright (C) 1998 Cesar Miquel, Shawn T. Amundson and Mattias Groenlund
8 * Copyright (c) 1995, 1996, 1997, 1998 by Steffen Beyer
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free
22 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
27 * file for a list of people on the GTK+ Team. See the ChangeLog
28 * files for a list of changes. These files are distributed with
29 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
34 * @Short_description: Displays a calendar and allows the user to select a date
37 * #GtkCalendar is a widget that displays a Gregorian calendar, one month
38 * at a time. It can be created with gtk_calendar_new().
40 * The month and year currently displayed can be altered with
41 * gtk_calendar_select_month(). The exact day can be selected from the
42 * displayed month using gtk_calendar_select_day().
44 * To place a visual marker on a particular day, use gtk_calendar_mark_day()
45 * and to remove the marker, gtk_calendar_unmark_day(). Alternative, all
46 * marks can be cleared with gtk_calendar_clear_marks().
48 * The way in which the calendar itself is displayed can be altered using
49 * gtk_calendar_set_display_options().
51 * The selected date can be retrieved from a #GtkCalendar using
52 * gtk_calendar_get_date().
54 * Users should be aware that, although the Gregorian calendar is the legal
55 * calendar in most countries, it was adopted progressively between 1582 and
56 * 1929. Display before these dates is likely to be historically incorrect.
61 #ifdef HAVE_SYS_TIME_H
64 #ifdef HAVE__NL_TIME_FIRST_WEEKDAY
77 #include "gtkcalendar.h"
81 #include "gtkmarshalers.h"
82 #include "gtktooltip.h"
83 #include "gtkprivate.h"
84 #include "gdk/gdkkeysyms.h"
86 /***************************************************************************/
87 /* The following date routines are taken from the lib_date package.
88 * They have been minimally edited to avoid conflict with types defined
92 static const guint month_length[2][13] =
94 { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
95 { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
98 static const guint days_in_months[2][14] =
100 { 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
101 { 0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
104 static glong calc_days(guint year, guint mm, guint dd);
105 static guint day_of_week(guint year, guint mm, guint dd);
106 static glong dates_difference(guint year1, guint mm1, guint dd1,
107 guint year2, guint mm2, guint dd2);
108 static guint weeks_in_year(guint year);
113 return((((year % 4) == 0) && ((year % 100) != 0)) || ((year % 400) == 0));
117 day_of_week (guint year, guint mm, guint dd)
121 days = calc_days(year, mm, dd);
128 return( (guint) days );
131 static guint weeks_in_year(guint year)
133 return(52 + ((day_of_week(year,1,1)==4) || (day_of_week(year,12,31)==4)));
137 check_date(guint year, guint mm, guint dd)
139 if (year < 1) return FALSE;
140 if ((mm < 1) || (mm > 12)) return FALSE;
141 if ((dd < 1) || (dd > month_length[leap(year)][mm])) return FALSE;
146 week_number(guint year, guint mm, guint dd)
150 first = day_of_week(year,1,1) - 1;
151 return( (guint) ( (dates_difference(year,1,1, year,mm,dd) + first) / 7L ) +
156 year_to_days(guint year)
158 return( year * 365L + (year / 4) - (year / 100) + (year / 400) );
163 calc_days(guint year, guint mm, guint dd)
167 if (year < 1) return(0L);
168 if ((mm < 1) || (mm > 12)) return(0L);
169 if ((dd < 1) || (dd > month_length[(lp = leap(year))][mm])) return(0L);
170 return( year_to_days(--year) + days_in_months[lp][mm] + dd );
174 week_of_year(guint *week, guint *year, guint mm, guint dd)
176 if (check_date(*year,mm,dd))
178 *week = week_number(*year,mm,dd);
180 *week = weeks_in_year(--(*year));
181 else if (*week > weeks_in_year(*year))
192 dates_difference(guint year1, guint mm1, guint dd1,
193 guint year2, guint mm2, guint dd2)
195 return( calc_days(year2, mm2, dd2) - calc_days(year1, mm1, dd1) );
198 /*** END OF lib_date routines ********************************************/
200 /* Spacing around day/week headers and main area, inside those windows */
201 #define CALENDAR_MARGIN 0
203 #define DAY_XSEP 0 /* not really good for small calendar */
204 #define DAY_YSEP 0 /* not really good for small calendar */
206 #define SCROLL_DELAY_FACTOR 5
209 #define HEADER_FG_COLOR(widget) (& gtk_widget_get_style (widget)->fg[gtk_widget_get_state (widget)])
210 #define HEADER_BG_COLOR(widget) (& gtk_widget_get_style (widget)->bg[gtk_widget_get_state (widget)])
211 #define SELECTED_BG_COLOR(widget) (& gtk_widget_get_style (widget)->base[gtk_widget_has_focus (widget) ? GTK_STATE_SELECTED : GTK_STATE_ACTIVE])
212 #define SELECTED_FG_COLOR(widget) (& gtk_widget_get_style (widget)->text[gtk_widget_has_focus (widget) ? GTK_STATE_SELECTED : GTK_STATE_ACTIVE])
213 #define NORMAL_DAY_COLOR(widget) (& gtk_widget_get_style (widget)->text[gtk_widget_get_state (widget)])
214 #define PREV_MONTH_COLOR(widget) (& gtk_widget_get_style (widget)->mid[gtk_widget_get_state (widget)])
215 #define NEXT_MONTH_COLOR(widget) (& gtk_widget_get_style (widget)->mid[gtk_widget_get_state (widget)])
216 #define MARKED_COLOR(widget) (& gtk_widget_get_style (widget)->text[gtk_widget_get_state (widget)])
217 #define BACKGROUND_COLOR(widget) (& gtk_widget_get_style (widget)->base[gtk_widget_get_state (widget)])
218 #define HIGHLIGHT_BACK_COLOR(widget) (& gtk_widget_get_style (widget)->mid[gtk_widget_get_state (widget)])
234 MONTH_CHANGED_SIGNAL,
236 DAY_SELECTED_DOUBLE_CLICK_SIGNAL,
252 PROP_NO_MONTH_CHANGE,
253 PROP_SHOW_WEEK_NUMBERS,
255 PROP_DETAIL_WIDTH_CHARS,
256 PROP_DETAIL_HEIGHT_ROWS
259 static guint gtk_calendar_signals[LAST_SIGNAL] = { 0 };
261 struct _GtkCalendarPrivate
263 GtkCalendarDisplayOptions display_flags;
264 GtkStyle *header_style;
265 GtkStyle *label_style;
267 GdkColor marked_date_color[31];
269 GdkWindow *arrow_win[4];
271 gchar grow_space [32];
277 gint day_month[6][7];
280 gint num_marked_dates;
281 gint marked_date[31];
290 guint arrow_state[4];
292 guint max_month_width;
293 guint max_year_width;
299 guint max_day_char_width;
300 guint max_day_char_ascent;
301 guint max_day_char_descent;
302 guint max_label_char_ascent;
303 guint max_label_char_descent;
304 guint max_week_char_width;
307 guint year_before : 1;
309 guint need_timer : 1;
312 guint drag_highlight : 1;
322 /* Optional callback, used to display extra information for each day. */
323 GtkCalendarDetailFunc detail_func;
324 gpointer detail_func_user_data;
325 GDestroyNotify detail_func_destroy;
327 /* Size requistion for details provided by the hook. */
328 gint detail_height_rows;
329 gint detail_width_chars;
330 gint detail_overflow[6];
333 #define GTK_CALENDAR_GET_PRIVATE(widget) (GTK_CALENDAR (widget)->priv)
335 static void gtk_calendar_finalize (GObject *calendar);
336 static void gtk_calendar_destroy (GtkWidget *widget);
337 static void gtk_calendar_set_property (GObject *object,
341 static void gtk_calendar_get_property (GObject *object,
346 static void gtk_calendar_realize (GtkWidget *widget);
347 static void gtk_calendar_unrealize (GtkWidget *widget);
348 static void gtk_calendar_map (GtkWidget *widget);
349 static void gtk_calendar_unmap (GtkWidget *widget);
350 static void gtk_calendar_get_preferred_width (GtkWidget *widget,
353 static void gtk_calendar_get_preferred_height (GtkWidget *widget,
356 static void gtk_calendar_size_allocate (GtkWidget *widget,
357 GtkAllocation *allocation);
358 static gboolean gtk_calendar_draw (GtkWidget *widget,
360 static gboolean gtk_calendar_button_press (GtkWidget *widget,
361 GdkEventButton *event);
362 static gboolean gtk_calendar_button_release (GtkWidget *widget,
363 GdkEventButton *event);
364 static gboolean gtk_calendar_motion_notify (GtkWidget *widget,
365 GdkEventMotion *event);
366 static gboolean gtk_calendar_enter_notify (GtkWidget *widget,
367 GdkEventCrossing *event);
368 static gboolean gtk_calendar_leave_notify (GtkWidget *widget,
369 GdkEventCrossing *event);
370 static gboolean gtk_calendar_scroll (GtkWidget *widget,
371 GdkEventScroll *event);
372 static gboolean gtk_calendar_key_press (GtkWidget *widget,
374 static gboolean gtk_calendar_focus_out (GtkWidget *widget,
375 GdkEventFocus *event);
376 static void gtk_calendar_grab_notify (GtkWidget *widget,
377 gboolean was_grabbed);
378 static void gtk_calendar_state_changed (GtkWidget *widget,
379 GtkStateType previous_state);
380 static gboolean gtk_calendar_query_tooltip (GtkWidget *widget,
383 gboolean keyboard_mode,
384 GtkTooltip *tooltip);
386 static void gtk_calendar_drag_data_get (GtkWidget *widget,
387 GdkDragContext *context,
388 GtkSelectionData *selection_data,
391 static void gtk_calendar_drag_data_received (GtkWidget *widget,
392 GdkDragContext *context,
395 GtkSelectionData *selection_data,
398 static gboolean gtk_calendar_drag_motion (GtkWidget *widget,
399 GdkDragContext *context,
403 static void gtk_calendar_drag_leave (GtkWidget *widget,
404 GdkDragContext *context,
406 static gboolean gtk_calendar_drag_drop (GtkWidget *widget,
407 GdkDragContext *context,
412 static void calendar_start_spinning (GtkCalendar *calendar,
414 static void calendar_stop_spinning (GtkCalendar *calendar);
416 static void calendar_invalidate_day (GtkCalendar *widget,
419 static void calendar_invalidate_day_num (GtkCalendar *widget,
421 static void calendar_invalidate_arrow (GtkCalendar *widget,
424 static void calendar_compute_days (GtkCalendar *calendar);
425 static gint calendar_get_xsep (GtkCalendar *calendar);
426 static gint calendar_get_ysep (GtkCalendar *calendar);
427 static gint calendar_get_inner_border (GtkCalendar *calendar);
429 static char *default_abbreviated_dayname[7];
430 static char *default_monthname[12];
432 G_DEFINE_TYPE (GtkCalendar, gtk_calendar, GTK_TYPE_WIDGET)
435 gtk_calendar_class_init (GtkCalendarClass *class)
437 GObjectClass *gobject_class;
438 GtkWidgetClass *widget_class;
440 gobject_class = (GObjectClass*) class;
441 widget_class = (GtkWidgetClass*) class;
443 gobject_class->set_property = gtk_calendar_set_property;
444 gobject_class->get_property = gtk_calendar_get_property;
445 gobject_class->finalize = gtk_calendar_finalize;
447 widget_class->destroy = gtk_calendar_destroy;
448 widget_class->realize = gtk_calendar_realize;
449 widget_class->unrealize = gtk_calendar_unrealize;
450 widget_class->map = gtk_calendar_map;
451 widget_class->unmap = gtk_calendar_unmap;
452 widget_class->draw = gtk_calendar_draw;
453 widget_class->get_preferred_width = gtk_calendar_get_preferred_width;
454 widget_class->get_preferred_height = gtk_calendar_get_preferred_height;
455 widget_class->size_allocate = gtk_calendar_size_allocate;
456 widget_class->button_press_event = gtk_calendar_button_press;
457 widget_class->button_release_event = gtk_calendar_button_release;
458 widget_class->motion_notify_event = gtk_calendar_motion_notify;
459 widget_class->enter_notify_event = gtk_calendar_enter_notify;
460 widget_class->leave_notify_event = gtk_calendar_leave_notify;
461 widget_class->key_press_event = gtk_calendar_key_press;
462 widget_class->scroll_event = gtk_calendar_scroll;
463 widget_class->state_changed = gtk_calendar_state_changed;
464 widget_class->grab_notify = gtk_calendar_grab_notify;
465 widget_class->focus_out_event = gtk_calendar_focus_out;
466 widget_class->query_tooltip = gtk_calendar_query_tooltip;
468 widget_class->drag_data_get = gtk_calendar_drag_data_get;
469 widget_class->drag_motion = gtk_calendar_drag_motion;
470 widget_class->drag_leave = gtk_calendar_drag_leave;
471 widget_class->drag_drop = gtk_calendar_drag_drop;
472 widget_class->drag_data_received = gtk_calendar_drag_data_received;
478 * This property gets initially set to the current year.
480 g_object_class_install_property (gobject_class,
482 g_param_spec_int ("year",
484 P_("The selected year"),
486 GTK_PARAM_READWRITE));
491 * The selected month (as a number between 0 and 11).
492 * This property gets initially set to the current month.
494 g_object_class_install_property (gobject_class,
496 g_param_spec_int ("month",
498 P_("The selected month (as a number between 0 and 11)"),
500 GTK_PARAM_READWRITE));
505 * The selected day (as a number between 1 and 31, or 0
506 * to unselect the currently selected day).
507 * This property gets initially set to the current day.
509 g_object_class_install_property (gobject_class,
511 g_param_spec_int ("day",
513 P_("The selected day (as a number between 1 and 31, or 0 to unselect the currently selected day)"),
515 GTK_PARAM_READWRITE));
518 * GtkCalendar:show-heading:
520 * Determines whether a heading is displayed.
524 g_object_class_install_property (gobject_class,
526 g_param_spec_boolean ("show-heading",
528 P_("If TRUE, a heading is displayed"),
530 GTK_PARAM_READWRITE));
533 * GtkCalendar:show-day-names:
535 * Determines whether day names are displayed.
539 g_object_class_install_property (gobject_class,
541 g_param_spec_boolean ("show-day-names",
542 P_("Show Day Names"),
543 P_("If TRUE, day names are displayed"),
545 GTK_PARAM_READWRITE));
547 * GtkCalendar:no-month-change:
549 * Determines whether the selected month can be changed.
553 g_object_class_install_property (gobject_class,
554 PROP_NO_MONTH_CHANGE,
555 g_param_spec_boolean ("no-month-change",
556 P_("No Month Change"),
557 P_("If TRUE, the selected month cannot be changed"),
559 GTK_PARAM_READWRITE));
562 * GtkCalendar:show-week-numbers:
564 * Determines whether week numbers are displayed.
568 g_object_class_install_property (gobject_class,
569 PROP_SHOW_WEEK_NUMBERS,
570 g_param_spec_boolean ("show-week-numbers",
571 P_("Show Week Numbers"),
572 P_("If TRUE, week numbers are displayed"),
574 GTK_PARAM_READWRITE));
577 * GtkCalendar:detail-width-chars:
579 * Width of a detail cell, in characters.
580 * A value of 0 allows any width. See gtk_calendar_set_detail_func().
584 g_object_class_install_property (gobject_class,
585 PROP_DETAIL_WIDTH_CHARS,
586 g_param_spec_int ("detail-width-chars",
588 P_("Details width in characters"),
590 GTK_PARAM_READWRITE));
593 * GtkCalendar:detail-height-rows:
595 * Height of a detail cell, in rows.
596 * A value of 0 allows any width. See gtk_calendar_set_detail_func().
600 g_object_class_install_property (gobject_class,
601 PROP_DETAIL_HEIGHT_ROWS,
602 g_param_spec_int ("detail-height-rows",
603 P_("Details Height"),
604 P_("Details height in rows"),
606 GTK_PARAM_READWRITE));
609 * GtkCalendar:show-details:
611 * Determines whether details are shown directly in the widget, or if they are
612 * available only as tooltip. When this property is set days with details are
617 g_object_class_install_property (gobject_class,
619 g_param_spec_boolean ("show-details",
621 P_("If TRUE, details are shown"),
623 GTK_PARAM_READWRITE));
627 * GtkCalendar:inner-border
629 * The spacing around the day/week headers and main area.
631 gtk_widget_class_install_style_property (widget_class,
632 g_param_spec_int ("inner-border",
634 P_("Inner border space"),
636 GTK_PARAM_READABLE));
639 * GtkCalndar:vertical-separation
641 * Separation between day headers and main area.
643 gtk_widget_class_install_style_property (widget_class,
644 g_param_spec_int ("vertical-separation",
645 P_("Vertical separation"),
646 P_("Space between day headers and main area"),
648 GTK_PARAM_READABLE));
651 * GtkCalendar:horizontal-separation
653 * Separation between week headers and main area.
655 gtk_widget_class_install_style_property (widget_class,
656 g_param_spec_int ("horizontal-separation",
657 P_("Horizontal separation"),
658 P_("Space between week headers and main area"),
660 GTK_PARAM_READABLE));
663 * GtkCalendar::month-changed:
664 * @calendar: the object which received the signal.
666 * Emitted when the user clicks a button to change the selected month on a
669 gtk_calendar_signals[MONTH_CHANGED_SIGNAL] =
670 g_signal_new (I_("month-changed"),
671 G_OBJECT_CLASS_TYPE (gobject_class),
673 G_STRUCT_OFFSET (GtkCalendarClass, month_changed),
675 _gtk_marshal_VOID__VOID,
679 * GtkCalendar::day-selected:
680 * @calendar: the object which received the signal.
682 * Emitted when the user selects a day.
684 gtk_calendar_signals[DAY_SELECTED_SIGNAL] =
685 g_signal_new (I_("day-selected"),
686 G_OBJECT_CLASS_TYPE (gobject_class),
688 G_STRUCT_OFFSET (GtkCalendarClass, day_selected),
690 _gtk_marshal_VOID__VOID,
694 * GtkCalendar::day-selected-double-click:
695 * @calendar: the object which received the signal.
697 * Emitted when the user double-clicks a day.
699 gtk_calendar_signals[DAY_SELECTED_DOUBLE_CLICK_SIGNAL] =
700 g_signal_new (I_("day-selected-double-click"),
701 G_OBJECT_CLASS_TYPE (gobject_class),
703 G_STRUCT_OFFSET (GtkCalendarClass, day_selected_double_click),
705 _gtk_marshal_VOID__VOID,
709 * GtkCalendar::prev-month:
710 * @calendar: the object which received the signal.
712 * Emitted when the user switched to the previous month.
714 gtk_calendar_signals[PREV_MONTH_SIGNAL] =
715 g_signal_new (I_("prev-month"),
716 G_OBJECT_CLASS_TYPE (gobject_class),
718 G_STRUCT_OFFSET (GtkCalendarClass, prev_month),
720 _gtk_marshal_VOID__VOID,
724 * GtkCalendar::next-month:
725 * @calendar: the object which received the signal.
727 * Emitted when the user switched to the next month.
729 gtk_calendar_signals[NEXT_MONTH_SIGNAL] =
730 g_signal_new (I_("next-month"),
731 G_OBJECT_CLASS_TYPE (gobject_class),
733 G_STRUCT_OFFSET (GtkCalendarClass, next_month),
735 _gtk_marshal_VOID__VOID,
739 * GtkCalendar::prev-year:
740 * @calendar: the object which received the signal.
742 * Emitted when user switched to the previous year.
744 gtk_calendar_signals[PREV_YEAR_SIGNAL] =
745 g_signal_new (I_("prev-year"),
746 G_OBJECT_CLASS_TYPE (gobject_class),
748 G_STRUCT_OFFSET (GtkCalendarClass, prev_year),
750 _gtk_marshal_VOID__VOID,
754 * GtkCalendar::next-year:
755 * @calendar: the object which received the signal.
757 * Emitted when user switched to the next year.
759 gtk_calendar_signals[NEXT_YEAR_SIGNAL] =
760 g_signal_new (I_("next-year"),
761 G_OBJECT_CLASS_TYPE (gobject_class),
763 G_STRUCT_OFFSET (GtkCalendarClass, next_year),
765 _gtk_marshal_VOID__VOID,
768 g_type_class_add_private (gobject_class, sizeof (GtkCalendarPrivate));
772 gtk_calendar_init (GtkCalendar *calendar)
774 GtkWidget *widget = GTK_WIDGET (calendar);
779 wchar_t wbuffer[100];
784 GtkCalendarPrivate *priv;
786 #ifdef HAVE__NL_TIME_FIRST_WEEKDAY
787 union { unsigned int word; char *string; } langinfo;
788 gint week_1stday = 0;
789 gint first_weekday = 1;
795 priv = calendar->priv = G_TYPE_INSTANCE_GET_PRIVATE (calendar,
799 gtk_widget_set_can_focus (widget, TRUE);
800 gtk_widget_set_has_window (widget, FALSE);
802 if (!default_abbreviated_dayname[0])
806 tmp_time= (i+3)*86400;
807 strftime ( buffer, sizeof (buffer), "%a", gmtime (&tmp_time));
808 default_abbreviated_dayname[i] = g_locale_to_utf8 (buffer, -1, NULL, NULL, NULL);
810 if (!GetLocaleInfoW (GetThreadLocale (), LOCALE_SABBREVDAYNAME1 + (i+6)%7,
811 wbuffer, G_N_ELEMENTS (wbuffer)))
812 default_abbreviated_dayname[i] = g_strdup_printf ("(%d)", i);
814 default_abbreviated_dayname[i] = g_utf16_to_utf8 (wbuffer, -1, NULL, NULL, NULL);
818 if (!default_monthname[0])
823 strftime ( buffer, sizeof (buffer), "%B", gmtime (&tmp_time));
824 default_monthname[i] = g_locale_to_utf8 (buffer, -1, NULL, NULL, NULL);
826 if (!GetLocaleInfoW (GetThreadLocale (), LOCALE_SMONTHNAME1 + i,
827 wbuffer, G_N_ELEMENTS (wbuffer)))
828 default_monthname[i] = g_strdup_printf ("(%d)", i);
830 default_monthname[i] = g_utf16_to_utf8 (wbuffer, -1, NULL, NULL, NULL);
836 tm = localtime (&secs);
837 priv->month = tm->tm_mon;
838 priv->year = 1900 + tm->tm_year;
841 priv->marked_date[i] = FALSE;
842 priv->num_marked_dates = 0;
843 priv->selected_day = tm->tm_mday;
845 priv->display_flags = (GTK_CALENDAR_SHOW_HEADING |
846 GTK_CALENDAR_SHOW_DAY_NAMES |
847 GTK_CALENDAR_SHOW_DETAILS);
849 priv->focus_row = -1;
850 priv->focus_col = -1;
852 priv->max_year_width = 0;
853 priv->max_month_width = 0;
854 priv->max_day_char_width = 0;
855 priv->max_week_char_width = 0;
857 priv->max_day_char_ascent = 0;
858 priv->max_day_char_descent = 0;
859 priv->max_label_char_ascent = 0;
860 priv->max_label_char_descent = 0;
862 priv->arrow_width = 10;
864 priv->need_timer = 0;
866 priv->click_child = -1;
869 priv->drag_highlight = 0;
871 gtk_drag_dest_set (widget, 0, NULL, 0, GDK_ACTION_COPY);
872 gtk_drag_dest_add_text_targets (widget);
874 priv->year_before = 0;
876 /* Translate to calendar:YM if you want years to be displayed
877 * before months; otherwise translate to calendar:MY.
878 * Do *not* translate it to anything else, if it
879 * it isn't calendar:YM or calendar:MY it will not work.
881 * Note that the ordering described here is logical order, which is
882 * further influenced by BIDI ordering. Thus, if you have a default
883 * text direction of RTL and specify "calendar:YM", then the year
884 * will appear to the right of the month.
886 year_before = _("calendar:MY");
887 if (strcmp (year_before, "calendar:YM") == 0)
888 priv->year_before = 1;
889 else if (strcmp (year_before, "calendar:MY") != 0)
890 g_warning ("Whoever translated calendar:MY did so wrongly.\n");
893 priv->week_start = 0;
896 if (GetLocaleInfoW (GetThreadLocale (), LOCALE_IFIRSTDAYOFWEEK,
897 wbuffer, G_N_ELEMENTS (wbuffer)))
898 week_start = g_utf16_to_utf8 (wbuffer, -1, NULL, NULL, NULL);
900 if (week_start != NULL)
902 priv->week_start = (week_start[0] - '0' + 1) % 7;
906 #ifdef HAVE__NL_TIME_FIRST_WEEKDAY
907 langinfo.string = nl_langinfo (_NL_TIME_FIRST_WEEKDAY);
908 first_weekday = langinfo.string[0];
909 langinfo.string = nl_langinfo (_NL_TIME_WEEK_1STDAY);
910 week_origin = langinfo.word;
911 if (week_origin == 19971130) /* Sunday */
913 else if (week_origin == 19971201) /* Monday */
916 g_warning ("Unknown value of _NL_TIME_WEEK_1STDAY.\n");
918 priv->week_start = (week_1stday + first_weekday - 1) % 7;
920 /* Translate to calendar:week_start:0 if you want Sunday to be the
921 * first day of the week to calendar:week_start:1 if you want Monday
922 * to be the first day of the week, and so on.
924 week_start = _("calendar:week_start:0");
926 if (strncmp (week_start, "calendar:week_start:", 20) == 0)
927 priv->week_start = *(week_start + 20) - '0';
929 priv->week_start = -1;
931 if (priv->week_start < 0 || priv->week_start > 6)
933 g_warning ("Whoever translated calendar:week_start:0 did so wrongly.\n");
934 priv->week_start = 0;
939 calendar_compute_days (calendar);
943 /****************************************
944 * Utility Functions *
945 ****************************************/
948 calendar_queue_refresh (GtkCalendar *calendar)
950 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
952 if (!(priv->detail_func) ||
953 !(priv->display_flags & GTK_CALENDAR_SHOW_DETAILS) ||
954 (priv->detail_width_chars && priv->detail_height_rows))
955 gtk_widget_queue_draw (GTK_WIDGET (calendar));
957 gtk_widget_queue_resize (GTK_WIDGET (calendar));
961 calendar_set_month_next (GtkCalendar *calendar)
964 GtkCalendarPrivate *priv = calendar->priv;
966 if (priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
969 if (priv->month == 11)
977 calendar_compute_days (calendar);
978 g_signal_emit (calendar,
979 gtk_calendar_signals[NEXT_MONTH_SIGNAL],
981 g_signal_emit (calendar,
982 gtk_calendar_signals[MONTH_CHANGED_SIGNAL],
985 month_len = month_length[leap (priv->year)][priv->month + 1];
987 if (month_len < priv->selected_day)
989 priv->selected_day = 0;
990 gtk_calendar_select_day (calendar, month_len);
993 gtk_calendar_select_day (calendar, priv->selected_day);
995 calendar_queue_refresh (calendar);
999 calendar_set_year_prev (GtkCalendar *calendar)
1001 GtkCalendarPrivate *priv = calendar->priv;
1005 calendar_compute_days (calendar);
1006 g_signal_emit (calendar,
1007 gtk_calendar_signals[PREV_YEAR_SIGNAL],
1009 g_signal_emit (calendar,
1010 gtk_calendar_signals[MONTH_CHANGED_SIGNAL],
1013 month_len = month_length[leap (priv->year)][priv->month + 1];
1015 if (month_len < priv->selected_day)
1017 priv->selected_day = 0;
1018 gtk_calendar_select_day (calendar, month_len);
1021 gtk_calendar_select_day (calendar, priv->selected_day);
1023 calendar_queue_refresh (calendar);
1027 calendar_set_year_next (GtkCalendar *calendar)
1029 GtkCalendarPrivate *priv = calendar->priv;
1033 calendar_compute_days (calendar);
1034 g_signal_emit (calendar,
1035 gtk_calendar_signals[NEXT_YEAR_SIGNAL],
1037 g_signal_emit (calendar,
1038 gtk_calendar_signals[MONTH_CHANGED_SIGNAL],
1041 month_len = month_length[leap (priv->year)][priv->month + 1];
1043 if (month_len < priv->selected_day)
1045 priv->selected_day = 0;
1046 gtk_calendar_select_day (calendar, month_len);
1049 gtk_calendar_select_day (calendar, priv->selected_day);
1051 calendar_queue_refresh (calendar);
1055 calendar_compute_days (GtkCalendar *calendar)
1057 GtkCalendarPrivate *priv = calendar->priv;
1060 gint ndays_in_month;
1061 gint ndays_in_prev_month;
1068 month = priv->month + 1;
1070 ndays_in_month = month_length[leap (year)][month];
1072 first_day = day_of_week (year, month, 1);
1073 first_day = (first_day + 7 - priv->week_start) % 7;
1075 /* Compute days of previous month */
1077 ndays_in_prev_month = month_length[leap (year)][month-1];
1079 ndays_in_prev_month = month_length[leap (year)][12];
1080 day = ndays_in_prev_month - first_day + 1;
1085 for (col = 0; col < first_day; col++)
1087 priv->day[row][col] = day;
1088 priv->day_month[row][col] = MONTH_PREV;
1093 /* Compute days of current month */
1095 for (day = 1; day <= ndays_in_month; day++)
1097 priv->day[row][col] = day;
1098 priv->day_month[row][col] = MONTH_CURRENT;
1108 /* Compute days of next month */
1110 for (; row <= 5; row++)
1112 for (; col <= 6; col++)
1114 priv->day[row][col] = day;
1115 priv->day_month[row][col] = MONTH_NEXT;
1123 calendar_select_and_focus_day (GtkCalendar *calendar,
1126 GtkCalendarPrivate *priv = calendar->priv;
1127 gint old_focus_row = priv->focus_row;
1128 gint old_focus_col = priv->focus_col;
1132 for (row = 0; row < 6; row ++)
1133 for (col = 0; col < 7; col++)
1135 if (priv->day_month[row][col] == MONTH_CURRENT
1136 && priv->day[row][col] == day)
1138 priv->focus_row = row;
1139 priv->focus_col = col;
1143 if (old_focus_row != -1 && old_focus_col != -1)
1144 calendar_invalidate_day (calendar, old_focus_row, old_focus_col);
1146 gtk_calendar_select_day (calendar, day);
1150 /****************************************
1151 * Layout computation utilities *
1152 ****************************************/
1155 calendar_row_height (GtkCalendar *calendar)
1157 GtkCalendarPrivate *priv = calendar->priv;
1159 return (GTK_CALENDAR_GET_PRIVATE (calendar)->main_h - CALENDAR_MARGIN
1160 - ((priv->display_flags & GTK_CALENDAR_SHOW_DAY_NAMES)
1161 ? calendar_get_ysep (calendar) : CALENDAR_MARGIN)) / 6;
1165 /* calendar_left_x_for_column: returns the x coordinate
1166 * for the left of the column */
1168 calendar_left_x_for_column (GtkCalendar *calendar,
1171 GtkCalendarPrivate *priv = calendar->priv;
1175 gint calendar_xsep = calendar_get_xsep (calendar);
1177 gint inner_border = calendar_get_inner_border (calendar);
1179 style = gtk_widget_get_style (GTK_WIDGET (calendar));
1181 week_width = priv->week_width + style->xthickness + inner_border;
1183 if (gtk_widget_get_direction (GTK_WIDGET (calendar)) == GTK_TEXT_DIR_RTL)
1185 column = 6 - column;
1189 width = GTK_CALENDAR_GET_PRIVATE (calendar)->day_width;
1190 if (priv->display_flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
1191 x_left = week_width + calendar_xsep + (width + DAY_XSEP) * column;
1193 x_left = week_width + CALENDAR_MARGIN + (width + DAY_XSEP) * column;
1198 /* column_from_x: returns the column 0-6 that the
1199 * x pixel of the xwindow is in */
1201 calendar_column_from_x (GtkCalendar *calendar,
1205 gint x_left, x_right;
1209 for (c = 0; c < 7; c++)
1211 x_left = calendar_left_x_for_column (calendar, c);
1212 x_right = x_left + GTK_CALENDAR_GET_PRIVATE (calendar)->day_width;
1214 if (event_x >= x_left && event_x < x_right)
1224 /* calendar_top_y_for_row: returns the y coordinate
1225 * for the top of the row */
1227 calendar_top_y_for_row (GtkCalendar *calendar,
1231 GtkAllocation allocation;
1232 gint inner_border = calendar_get_inner_border (calendar);
1234 gtk_widget_get_allocation (GTK_WIDGET (calendar), &allocation);
1235 style = gtk_widget_get_style (GTK_WIDGET (calendar));
1237 return allocation.height
1238 - style->ythickness - inner_border
1239 - (CALENDAR_MARGIN + (6 - row)
1240 * calendar_row_height (calendar));
1243 /* row_from_y: returns the row 0-5 that the
1244 * y pixel of the xwindow is in */
1246 calendar_row_from_y (GtkCalendar *calendar,
1251 gint y_top, y_bottom;
1253 height = calendar_row_height (calendar);
1256 for (r = 0; r < 6; r++)
1258 y_top = calendar_top_y_for_row (calendar, r);
1259 y_bottom = y_top + height;
1261 if (event_y >= y_top && event_y < y_bottom)
1272 calendar_arrow_rectangle (GtkCalendar *calendar,
1276 GtkWidget *widget = GTK_WIDGET (calendar);
1277 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1278 GtkAllocation allocation;
1282 gtk_widget_get_allocation (widget, &allocation);
1283 style = gtk_widget_get_style (widget);
1285 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
1286 year_left = priv->year_before;
1288 year_left = !priv->year_before;
1291 rect->width = priv->arrow_width;
1292 rect->height = priv->header_h - 7;
1296 case ARROW_MONTH_LEFT:
1298 rect->x = (allocation.width - 2 * style->xthickness
1299 - (3 + 2 * priv->arrow_width + priv->max_month_width));
1303 case ARROW_MONTH_RIGHT:
1305 rect->x = (allocation.width - 2 * style->xthickness
1306 - 3 - priv->arrow_width);
1308 rect->x = (priv->arrow_width + priv->max_month_width);
1310 case ARROW_YEAR_LEFT:
1314 rect->x = (allocation.width - 2 * style->xthickness
1315 - (3 + 2 * priv->arrow_width + priv->max_year_width));
1317 case ARROW_YEAR_RIGHT:
1319 rect->x = (priv->arrow_width + priv->max_year_width);
1321 rect->x = (allocation.width - 2 * style->xthickness
1322 - 3 - priv->arrow_width);
1326 rect->x += style->xthickness;
1327 rect->y += style->ythickness;
1331 calendar_day_rectangle (GtkCalendar *calendar,
1336 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1338 rect->x = calendar_left_x_for_column (calendar, col);
1339 rect->y = calendar_top_y_for_row (calendar, row);
1340 rect->height = calendar_row_height (calendar);
1341 rect->width = priv->day_width;
1345 calendar_set_month_prev (GtkCalendar *calendar)
1347 GtkCalendarPrivate *priv = calendar->priv;
1350 if (priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
1353 if (priv->month == 0)
1361 month_len = month_length[leap (priv->year)][priv->month + 1];
1363 calendar_compute_days (calendar);
1365 g_signal_emit (calendar,
1366 gtk_calendar_signals[PREV_MONTH_SIGNAL],
1368 g_signal_emit (calendar,
1369 gtk_calendar_signals[MONTH_CHANGED_SIGNAL],
1372 if (month_len < priv->selected_day)
1374 priv->selected_day = 0;
1375 gtk_calendar_select_day (calendar, month_len);
1379 if (priv->selected_day < 0)
1380 priv->selected_day = priv->selected_day + 1 + month_length[leap (priv->year)][priv->month + 1];
1381 gtk_calendar_select_day (calendar, priv->selected_day);
1384 calendar_queue_refresh (calendar);
1388 /****************************************
1389 * Basic object methods *
1390 ****************************************/
1393 gtk_calendar_finalize (GObject *object)
1395 G_OBJECT_CLASS (gtk_calendar_parent_class)->finalize (object);
1399 gtk_calendar_destroy (GtkWidget *widget)
1401 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
1403 calendar_stop_spinning (GTK_CALENDAR (widget));
1405 /* Call the destroy function for the extra display callback: */
1406 if (priv->detail_func_destroy && priv->detail_func_user_data)
1408 priv->detail_func_destroy (priv->detail_func_user_data);
1409 priv->detail_func_user_data = NULL;
1410 priv->detail_func_destroy = NULL;
1413 GTK_WIDGET_CLASS (gtk_calendar_parent_class)->destroy (widget);
1418 calendar_set_display_option (GtkCalendar *calendar,
1419 GtkCalendarDisplayOptions flag,
1422 GtkCalendarPrivate *priv = calendar->priv;
1423 GtkCalendarDisplayOptions flags;
1426 flags = priv->display_flags | flag;
1428 flags = priv->display_flags & ~flag;
1429 gtk_calendar_set_display_options (calendar, flags);
1433 calendar_get_display_option (GtkCalendar *calendar,
1434 GtkCalendarDisplayOptions flag)
1436 GtkCalendarPrivate *priv = calendar->priv;
1438 return (priv->display_flags & flag) != 0;
1442 gtk_calendar_set_property (GObject *object,
1444 const GValue *value,
1447 GtkCalendar *calendar = GTK_CALENDAR (object);
1448 GtkCalendarPrivate *priv = calendar->priv;
1453 gtk_calendar_select_month (calendar,
1455 g_value_get_int (value));
1458 gtk_calendar_select_month (calendar,
1459 g_value_get_int (value),
1463 gtk_calendar_select_day (calendar,
1464 g_value_get_int (value));
1466 case PROP_SHOW_HEADING:
1467 calendar_set_display_option (calendar,
1468 GTK_CALENDAR_SHOW_HEADING,
1469 g_value_get_boolean (value));
1471 case PROP_SHOW_DAY_NAMES:
1472 calendar_set_display_option (calendar,
1473 GTK_CALENDAR_SHOW_DAY_NAMES,
1474 g_value_get_boolean (value));
1476 case PROP_NO_MONTH_CHANGE:
1477 calendar_set_display_option (calendar,
1478 GTK_CALENDAR_NO_MONTH_CHANGE,
1479 g_value_get_boolean (value));
1481 case PROP_SHOW_WEEK_NUMBERS:
1482 calendar_set_display_option (calendar,
1483 GTK_CALENDAR_SHOW_WEEK_NUMBERS,
1484 g_value_get_boolean (value));
1486 case PROP_SHOW_DETAILS:
1487 calendar_set_display_option (calendar,
1488 GTK_CALENDAR_SHOW_DETAILS,
1489 g_value_get_boolean (value));
1491 case PROP_DETAIL_WIDTH_CHARS:
1492 gtk_calendar_set_detail_width_chars (calendar,
1493 g_value_get_int (value));
1495 case PROP_DETAIL_HEIGHT_ROWS:
1496 gtk_calendar_set_detail_height_rows (calendar,
1497 g_value_get_int (value));
1500 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1506 gtk_calendar_get_property (GObject *object,
1511 GtkCalendar *calendar = GTK_CALENDAR (object);
1512 GtkCalendarPrivate *priv = calendar->priv;
1517 g_value_set_int (value, priv->year);
1520 g_value_set_int (value, priv->month);
1523 g_value_set_int (value, priv->selected_day);
1525 case PROP_SHOW_HEADING:
1526 g_value_set_boolean (value, calendar_get_display_option (calendar,
1527 GTK_CALENDAR_SHOW_HEADING));
1529 case PROP_SHOW_DAY_NAMES:
1530 g_value_set_boolean (value, calendar_get_display_option (calendar,
1531 GTK_CALENDAR_SHOW_DAY_NAMES));
1533 case PROP_NO_MONTH_CHANGE:
1534 g_value_set_boolean (value, calendar_get_display_option (calendar,
1535 GTK_CALENDAR_NO_MONTH_CHANGE));
1537 case PROP_SHOW_WEEK_NUMBERS:
1538 g_value_set_boolean (value, calendar_get_display_option (calendar,
1539 GTK_CALENDAR_SHOW_WEEK_NUMBERS));
1541 case PROP_SHOW_DETAILS:
1542 g_value_set_boolean (value, calendar_get_display_option (calendar,
1543 GTK_CALENDAR_SHOW_DETAILS));
1545 case PROP_DETAIL_WIDTH_CHARS:
1546 g_value_set_int (value, priv->detail_width_chars);
1548 case PROP_DETAIL_HEIGHT_ROWS:
1549 g_value_set_int (value, priv->detail_height_rows);
1552 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1558 /****************************************
1560 ****************************************/
1563 calendar_realize_arrows (GtkCalendar *calendar)
1565 GtkWidget *widget = GTK_WIDGET (calendar);
1566 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1567 GdkWindowAttr attributes;
1568 gint attributes_mask;
1570 GtkAllocation allocation;
1572 if (! (priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
1573 && (priv->display_flags & GTK_CALENDAR_SHOW_HEADING))
1575 gtk_widget_get_allocation (widget, &allocation);
1577 attributes.wclass = GDK_INPUT_ONLY;
1578 attributes.window_type = GDK_WINDOW_CHILD;
1579 attributes.event_mask = (gtk_widget_get_events (widget)
1580 | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
1581 | GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK);
1582 attributes_mask = GDK_WA_X | GDK_WA_Y;
1583 for (i = 0; i < 4; i++)
1586 calendar_arrow_rectangle (calendar, i, &rect);
1588 attributes.x = allocation.x + rect.x;
1589 attributes.y = allocation.y + rect.y;
1590 attributes.width = rect.width;
1591 attributes.height = rect.height;
1592 priv->arrow_win[i] = gdk_window_new (gtk_widget_get_window (widget),
1595 if (gtk_widget_is_sensitive (widget))
1596 priv->arrow_state[i] = GTK_STATE_NORMAL;
1598 priv->arrow_state[i] = GTK_STATE_INSENSITIVE;
1599 gdk_window_set_user_data (priv->arrow_win[i], widget);
1604 for (i = 0; i < 4; i++)
1605 priv->arrow_win[i] = NULL;
1610 calendar_unrealize_arrows (GtkCalendar *calendar)
1612 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1615 for (i = 0; i < 4; i++)
1617 if (priv->arrow_win[i])
1619 gdk_window_set_user_data (priv->arrow_win[i], NULL);
1620 gdk_window_destroy (priv->arrow_win[i]);
1621 priv->arrow_win[i] = NULL;
1628 calendar_get_inner_border (GtkCalendar *calendar)
1632 gtk_widget_style_get (GTK_WIDGET (calendar),
1633 "inner-border", &inner_border,
1636 return inner_border;
1640 calendar_get_xsep (GtkCalendar *calendar)
1644 gtk_widget_style_get (GTK_WIDGET (calendar),
1645 "horizontal-separation", &xsep,
1652 calendar_get_ysep (GtkCalendar *calendar)
1656 gtk_widget_style_get (GTK_WIDGET (calendar),
1657 "vertical-separation", &ysep,
1664 gtk_calendar_realize (GtkWidget *widget)
1666 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
1667 GdkWindowAttr attributes;
1668 gint attributes_mask;
1669 gint inner_border = calendar_get_inner_border (GTK_CALENDAR (widget));
1671 GtkAllocation allocation;
1673 style = gtk_widget_get_style (widget);
1674 gtk_widget_get_allocation (widget, &allocation);
1676 GTK_WIDGET_CLASS (gtk_calendar_parent_class)->realize (widget);
1678 attributes.wclass = GDK_INPUT_ONLY;
1679 attributes.window_type = GDK_WINDOW_CHILD;
1680 attributes.event_mask = (gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK
1681 | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
1682 | GDK_POINTER_MOTION_MASK | GDK_LEAVE_NOTIFY_MASK);
1684 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
1685 attributes.x = priv->week_width + style->ythickness + inner_border;
1687 attributes.x = style->ythickness + inner_border;
1689 attributes.y = priv->header_h + priv->day_name_h + style->ythickness + inner_border;
1690 attributes.width = allocation.width - attributes.x - (style->xthickness + inner_border);
1692 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1693 attributes.width -= priv->week_width;
1695 attributes.height = priv->main_h;
1696 attributes_mask = GDK_WA_X | GDK_WA_Y;
1698 attributes.x += allocation.x;
1699 attributes.y += allocation.y;
1701 priv->main_win = gdk_window_new (gtk_widget_get_window (widget),
1702 &attributes, attributes_mask);
1703 gdk_window_set_user_data (priv->main_win, widget);
1705 calendar_realize_arrows (GTK_CALENDAR (widget));
1709 gtk_calendar_unrealize (GtkWidget *widget)
1711 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
1713 calendar_unrealize_arrows (GTK_CALENDAR (widget));
1717 gdk_window_set_user_data (priv->main_win, NULL);
1718 gdk_window_destroy (priv->main_win);
1719 priv->main_win = NULL;
1722 GTK_WIDGET_CLASS (gtk_calendar_parent_class)->unrealize (widget);
1726 calendar_map_arrows (GtkCalendar *calendar)
1728 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1731 for (i = 0; i < 4; i++)
1733 if (priv->arrow_win[i])
1734 gdk_window_show (priv->arrow_win[i]);
1739 calendar_unmap_arrows (GtkCalendar *calendar)
1741 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1744 for (i = 0; i < 4; i++)
1746 if (priv->arrow_win[i])
1747 gdk_window_hide (priv->arrow_win[i]);
1752 gtk_calendar_map (GtkWidget *widget)
1754 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
1756 GTK_WIDGET_CLASS (gtk_calendar_parent_class)->map (widget);
1758 gdk_window_show (priv->main_win);
1760 calendar_map_arrows (GTK_CALENDAR (widget));
1764 gtk_calendar_unmap (GtkWidget *widget)
1766 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
1768 calendar_unmap_arrows (GTK_CALENDAR (widget));
1770 gdk_window_hide (priv->main_win);
1772 GTK_WIDGET_CLASS (gtk_calendar_parent_class)->unmap (widget);
1776 gtk_calendar_get_detail (GtkCalendar *calendar,
1780 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1783 if (priv->detail_func == NULL)
1787 month = priv->month + priv->day_month[row][column] - MONTH_CURRENT;
1794 else if (month > 11)
1800 return priv->detail_func (calendar,
1802 priv->day[row][column],
1803 priv->detail_func_user_data);
1807 gtk_calendar_query_tooltip (GtkWidget *widget,
1810 gboolean keyboard_mode,
1811 GtkTooltip *tooltip)
1813 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
1814 GtkCalendar *calendar = GTK_CALENDAR (widget);
1815 gchar *detail = NULL;
1816 GdkRectangle day_rect;
1819 col = calendar_column_from_x (calendar, x);
1820 row = calendar_row_from_y (calendar, y);
1822 if (col != -1 && row != -1 &&
1823 (0 != (priv->detail_overflow[row] & (1 << col)) ||
1824 0 == (priv->display_flags & GTK_CALENDAR_SHOW_DETAILS)))
1826 detail = gtk_calendar_get_detail (calendar, row, col);
1827 calendar_day_rectangle (calendar, row, col, &day_rect);
1832 gtk_tooltip_set_tip_area (tooltip, &day_rect);
1833 gtk_tooltip_set_markup (tooltip, detail);
1840 if (GTK_WIDGET_CLASS (gtk_calendar_parent_class)->query_tooltip)
1841 return GTK_WIDGET_CLASS (gtk_calendar_parent_class)->query_tooltip (widget, x, y, keyboard_mode, tooltip);
1847 /****************************************
1848 * Size Request and Allocate *
1849 ****************************************/
1852 gtk_calendar_size_request (GtkWidget *widget,
1853 GtkRequisition *requisition)
1855 GtkCalendar *calendar = GTK_CALENDAR (widget);
1856 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
1858 PangoLayout *layout;
1859 PangoRectangle logical_rect;
1863 gint calendar_margin = CALENDAR_MARGIN;
1864 gint header_width, main_width;
1865 gint max_header_height = 0;
1868 gint max_detail_height;
1869 gint inner_border = calendar_get_inner_border (calendar);
1870 gint calendar_ysep = calendar_get_ysep (calendar);
1871 gint calendar_xsep = calendar_get_xsep (calendar);
1873 gtk_widget_style_get (GTK_WIDGET (widget),
1874 "focus-line-width", &focus_width,
1875 "focus-padding", &focus_padding,
1878 layout = gtk_widget_create_pango_layout (widget, NULL);
1881 * Calculate the requisition width for the widget.
1886 if (priv->display_flags & GTK_CALENDAR_SHOW_HEADING)
1888 priv->max_month_width = 0;
1889 for (i = 0; i < 12; i++)
1891 pango_layout_set_text (layout, default_monthname[i], -1);
1892 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1893 priv->max_month_width = MAX (priv->max_month_width,
1894 logical_rect.width + 8);
1895 max_header_height = MAX (max_header_height, logical_rect.height);
1898 priv->max_year_width = 0;
1899 /* Translators: This is a text measurement template.
1900 * Translate it to the widest year text
1902 * If you don't understand this, leave it as "2000"
1904 pango_layout_set_text (layout, C_("year measurement template", "2000"), -1);
1905 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1906 priv->max_year_width = MAX (priv->max_year_width,
1907 logical_rect.width + 8);
1908 max_header_height = MAX (max_header_height, logical_rect.height);
1912 priv->max_month_width = 0;
1913 priv->max_year_width = 0;
1916 if (priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
1917 header_width = (priv->max_month_width
1918 + priv->max_year_width
1921 header_width = (priv->max_month_width
1922 + priv->max_year_width
1923 + 4 * priv->arrow_width + 3 * 3);
1925 /* Mainwindow labels width */
1927 priv->max_day_char_width = 0;
1928 priv->max_day_char_ascent = 0;
1929 priv->max_day_char_descent = 0;
1930 priv->min_day_width = 0;
1932 for (i = 0; i < 9; i++)
1935 g_snprintf (buffer, sizeof (buffer), C_("calendar:day:digits", "%d"), i * 11);
1936 pango_layout_set_text (layout, buffer, -1);
1937 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1938 priv->min_day_width = MAX (priv->min_day_width,
1939 logical_rect.width);
1941 priv->max_day_char_ascent = MAX (priv->max_day_char_ascent,
1942 PANGO_ASCENT (logical_rect));
1943 priv->max_day_char_descent = MAX (priv->max_day_char_descent,
1944 PANGO_DESCENT (logical_rect));
1947 priv->max_label_char_ascent = 0;
1948 priv->max_label_char_descent = 0;
1949 if (priv->display_flags & GTK_CALENDAR_SHOW_DAY_NAMES)
1950 for (i = 0; i < 7; i++)
1952 pango_layout_set_text (layout, default_abbreviated_dayname[i], -1);
1953 pango_layout_line_get_pixel_extents (pango_layout_get_lines_readonly (layout)->data, NULL, &logical_rect);
1955 priv->min_day_width = MAX (priv->min_day_width, logical_rect.width);
1956 priv->max_label_char_ascent = MAX (priv->max_label_char_ascent,
1957 PANGO_ASCENT (logical_rect));
1958 priv->max_label_char_descent = MAX (priv->max_label_char_descent,
1959 PANGO_DESCENT (logical_rect));
1962 priv->max_week_char_width = 0;
1963 if (priv->display_flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
1964 for (i = 0; i < 9; i++)
1967 g_snprintf (buffer, sizeof (buffer), C_("calendar:week:digits", "%d"), i * 11);
1968 pango_layout_set_text (layout, buffer, -1);
1969 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1970 priv->max_week_char_width = MAX (priv->max_week_char_width,
1971 logical_rect.width / 2);
1974 /* Calculate detail extents. Do this as late as possible since
1975 * pango_layout_set_markup is called which alters font settings. */
1976 max_detail_height = 0;
1978 if (priv->detail_func && (priv->display_flags & GTK_CALENDAR_SHOW_DETAILS))
1980 gchar *markup, *tail;
1982 if (priv->detail_width_chars || priv->detail_height_rows)
1984 gint rows = MAX (1, priv->detail_height_rows) - 1;
1985 gsize len = priv->detail_width_chars + rows + 16;
1987 markup = tail = g_alloca (len);
1989 memcpy (tail, "<small>", 7);
1992 memset (tail, 'm', priv->detail_width_chars);
1993 tail += priv->detail_width_chars;
1995 memset (tail, '\n', rows);
1998 memcpy (tail, "</small>", 9);
2001 g_assert (len == (tail - markup));
2003 pango_layout_set_markup (layout, markup, -1);
2004 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2006 if (priv->detail_width_chars)
2007 priv->min_day_width = MAX (priv->min_day_width, logical_rect.width);
2008 if (priv->detail_height_rows)
2009 max_detail_height = MAX (max_detail_height, logical_rect.height);
2012 if (!priv->detail_width_chars || !priv->detail_height_rows)
2013 for (r = 0; r < 6; r++)
2014 for (c = 0; c < 7; c++)
2016 gchar *detail = gtk_calendar_get_detail (calendar, r, c);
2020 markup = g_strconcat ("<small>", detail, "</small>", NULL);
2021 pango_layout_set_markup (layout, markup, -1);
2023 if (priv->detail_width_chars)
2025 pango_layout_set_wrap (layout, PANGO_WRAP_WORD_CHAR);
2026 pango_layout_set_width (layout, PANGO_SCALE * priv->min_day_width);
2029 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2031 if (!priv->detail_width_chars)
2032 priv->min_day_width = MAX (priv->min_day_width, logical_rect.width);
2033 if (!priv->detail_height_rows)
2034 max_detail_height = MAX (max_detail_height, logical_rect.height);
2042 /* We add one to max_day_char_width to be able to make the marked day "bold" */
2043 priv->max_day_char_width = priv->min_day_width / 2 + 1;
2045 main_width = (7 * (priv->min_day_width + (focus_padding + focus_width) * 2) + (DAY_XSEP * 6) + CALENDAR_MARGIN * 2
2046 + (priv->max_week_char_width
2047 ? priv->max_week_char_width * 2 + (focus_padding + focus_width) * 2 + calendar_xsep * 2
2050 style = gtk_widget_get_style (widget);
2052 requisition->width = MAX (header_width, main_width + inner_border * 2) + style->xthickness * 2;
2055 * Calculate the requisition height for the widget.
2058 if (priv->display_flags & GTK_CALENDAR_SHOW_HEADING)
2060 priv->header_h = (max_header_height + calendar_ysep * 2);
2067 if (priv->display_flags & GTK_CALENDAR_SHOW_DAY_NAMES)
2069 priv->day_name_h = (priv->max_label_char_ascent
2070 + priv->max_label_char_descent
2071 + 2 * (focus_padding + focus_width) + calendar_margin);
2072 calendar_margin = calendar_ysep;
2076 priv->day_name_h = 0;
2079 priv->main_h = (CALENDAR_MARGIN + calendar_margin
2080 + 6 * (priv->max_day_char_ascent
2081 + priv->max_day_char_descent
2083 + 2 * (focus_padding + focus_width))
2086 height = priv->header_h + priv->day_name_h + priv->main_h;
2088 requisition->height = height + (style->ythickness + inner_border) * 2;
2090 g_object_unref (layout);
2094 gtk_calendar_get_preferred_width (GtkWidget *widget,
2098 GtkRequisition requisition;
2100 gtk_calendar_size_request (widget, &requisition);
2102 *minimum = *natural = requisition.width;
2106 gtk_calendar_get_preferred_height (GtkWidget *widget,
2110 GtkRequisition requisition;
2112 gtk_calendar_size_request (widget, &requisition);
2114 *minimum = *natural = requisition.height;
2118 gtk_calendar_size_allocate (GtkWidget *widget,
2119 GtkAllocation *allocation)
2121 GtkCalendar *calendar = GTK_CALENDAR (widget);
2122 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
2124 gint xthickness, ythickness;
2126 gint inner_border = calendar_get_inner_border (calendar);
2127 gint calendar_xsep = calendar_get_xsep (calendar);
2129 style = gtk_widget_get_style (widget);
2130 xthickness = style->xthickness;
2131 ythickness = style->xthickness;
2133 gtk_widget_set_allocation (widget, allocation);
2135 if (priv->display_flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
2137 priv->day_width = (priv->min_day_width
2138 * ((allocation->width - (xthickness + inner_border) * 2
2139 - (CALENDAR_MARGIN * 2) - (DAY_XSEP * 6) - calendar_xsep * 2))
2140 / (7 * priv->min_day_width + priv->max_week_char_width * 2));
2141 priv->week_width = ((allocation->width - (xthickness + inner_border) * 2
2142 - (CALENDAR_MARGIN * 2) - (DAY_XSEP * 6) - calendar_xsep * 2 )
2143 - priv->day_width * 7 + CALENDAR_MARGIN + calendar_xsep);
2147 priv->day_width = (allocation->width
2148 - (xthickness + inner_border) * 2
2149 - (CALENDAR_MARGIN * 2)
2150 - (DAY_XSEP * 6))/7;
2151 priv->week_width = 0;
2154 if (gtk_widget_get_realized (widget))
2156 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
2157 gdk_window_move_resize (priv->main_win,
2159 + priv->week_width + xthickness + inner_border,
2161 + priv->header_h + priv->day_name_h
2162 + (style->ythickness + inner_border),
2163 allocation->width - priv->week_width
2164 - (xthickness + inner_border) * 2,
2167 gdk_window_move_resize (priv->main_win,
2169 + xthickness + inner_border,
2171 + priv->header_h + priv->day_name_h
2172 + style->ythickness + inner_border,
2173 allocation->width - priv->week_width
2174 - (xthickness + inner_border) * 2,
2177 for (i = 0 ; i < 4 ; i++)
2179 if (priv->arrow_win[i])
2182 calendar_arrow_rectangle (calendar, i, &rect);
2184 gdk_window_move_resize (priv->arrow_win[i],
2185 allocation->x + rect.x,
2186 allocation->y + rect.y,
2187 rect.width, rect.height);
2194 /****************************************
2196 ****************************************/
2199 calendar_paint_header (GtkCalendar *calendar, cairo_t *cr)
2201 GtkWidget *widget = GTK_WIDGET (calendar);
2202 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2203 GtkAllocation allocation;
2208 gint max_month_width;
2209 gint max_year_width;
2210 PangoLayout *layout;
2211 PangoRectangle logical_rect;
2217 style = gtk_widget_get_style (widget);
2220 cairo_translate (cr, style->xthickness, style->ythickness);
2222 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
2223 year_left = priv->year_before;
2225 year_left = !priv->year_before;
2227 gtk_widget_get_allocation (widget, &allocation);
2229 header_width = allocation.width - 2 * style->xthickness;
2231 max_month_width = priv->max_month_width;
2232 max_year_width = priv->max_year_width;
2234 gdk_cairo_set_source_color (cr, HEADER_BG_COLOR (widget));
2235 cairo_rectangle (cr, 0, 0, header_width, priv->header_h);
2238 gtk_paint_shadow (style, cr,
2239 GTK_STATE_NORMAL, GTK_SHADOW_OUT,
2241 0, 0, header_width, priv->header_h);
2243 tmp_time = 1; /* Jan 1 1970, 00:00:01 UTC */
2244 tm = gmtime (&tmp_time);
2245 tm->tm_year = priv->year - 1900;
2247 /* Translators: This dictates how the year is displayed in
2248 * gtkcalendar widget. See strftime() manual for the format.
2249 * Use only ASCII in the translation.
2251 * Also look for the msgid "2000".
2252 * Translate that entry to a year with the widest output of this
2255 * "%Y" is appropriate for most locales.
2257 strftime (buffer, sizeof (buffer), C_("calendar year format", "%Y"), tm);
2258 str = g_locale_to_utf8 (buffer, -1, NULL, NULL, NULL);
2259 layout = gtk_widget_create_pango_layout (widget, str);
2262 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2265 y = (priv->header_h - logical_rect.height) / 2;
2267 /* Draw year and its arrows */
2269 if (priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
2271 x = 3 + (max_year_width - logical_rect.width)/2;
2273 x = header_width - (3 + max_year_width
2274 - (max_year_width - logical_rect.width)/2);
2277 x = 3 + priv->arrow_width + (max_year_width - logical_rect.width)/2;
2279 x = header_width - (3 + priv->arrow_width + max_year_width
2280 - (max_year_width - logical_rect.width)/2);
2283 gdk_cairo_set_source_color (cr, HEADER_FG_COLOR (GTK_WIDGET (calendar)));
2284 cairo_move_to (cr, x, y);
2285 pango_cairo_show_layout (cr, layout);
2288 g_snprintf (buffer, sizeof (buffer), "%s", default_monthname[priv->month]);
2289 pango_layout_set_text (layout, buffer, -1);
2290 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2292 if (priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
2294 x = header_width - (3 + max_month_width
2295 - (max_month_width - logical_rect.width)/2);
2297 x = 3 + (max_month_width - logical_rect.width) / 2;
2300 x = header_width - (3 + priv->arrow_width + max_month_width
2301 - (max_month_width - logical_rect.width)/2);
2303 x = 3 + priv->arrow_width + (max_month_width - logical_rect.width)/2;
2305 cairo_move_to (cr, x, y);
2306 pango_cairo_show_layout (cr, layout);
2308 g_object_unref (layout);
2314 calendar_paint_day_names (GtkCalendar *calendar,
2317 GtkWidget *widget = GTK_WIDGET (calendar);
2318 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2320 GtkAllocation allocation;
2323 int day_width, cal_width;
2325 PangoLayout *layout;
2326 PangoRectangle logical_rect;
2329 gint calendar_ysep = calendar_get_ysep (calendar);
2330 gint calendar_xsep = calendar_get_xsep (calendar);
2331 gint inner_border = calendar_get_inner_border (calendar);
2333 style = gtk_widget_get_style (widget);
2337 cairo_translate (cr,
2338 style->xthickness + inner_border,
2339 priv->header_h + style->ythickness + inner_border);
2341 gtk_widget_style_get (GTK_WIDGET (widget),
2342 "focus-line-width", &focus_width,
2343 "focus-padding", &focus_padding,
2346 gtk_widget_get_allocation (widget, &allocation);
2348 day_width = priv->day_width;
2349 cal_width = allocation.width - (style->xthickness + inner_border) * 2;
2350 day_wid_sep = day_width + DAY_XSEP;
2353 * Draw rectangles as inverted background for the labels.
2356 gdk_cairo_set_source_color (cr, SELECTED_BG_COLOR (widget));
2357 cairo_rectangle (cr,
2358 CALENDAR_MARGIN, CALENDAR_MARGIN,
2359 cal_width - CALENDAR_MARGIN * 2,
2360 priv->day_name_h - CALENDAR_MARGIN);
2363 if (priv->display_flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
2365 cairo_rectangle (cr,
2367 priv->day_name_h - calendar_ysep,
2368 priv->week_width - calendar_ysep - CALENDAR_MARGIN,
2377 layout = gtk_widget_create_pango_layout (widget, NULL);
2379 gdk_cairo_set_source_color (cr, SELECTED_FG_COLOR (widget));
2380 for (i = 0; i < 7; i++)
2382 if (gtk_widget_get_direction (GTK_WIDGET (calendar)) == GTK_TEXT_DIR_RTL)
2386 day = (day + priv->week_start) % 7;
2387 g_snprintf (buffer, sizeof (buffer), "%s", default_abbreviated_dayname[day]);
2389 pango_layout_set_text (layout, buffer, -1);
2390 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2394 + (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR ?
2395 (priv->week_width + (priv->week_width ? calendar_xsep : 0))
2398 + (day_width - logical_rect.width)/2),
2399 CALENDAR_MARGIN + focus_width + focus_padding + logical_rect.y);
2400 pango_cairo_show_layout (cr, layout);
2403 g_object_unref (layout);
2409 calendar_paint_week_numbers (GtkCalendar *calendar,
2412 GtkWidget *widget = GTK_WIDGET (calendar);
2413 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2415 guint week = 0, year;
2416 gint row, x_loc, y_loc;
2419 PangoLayout *layout;
2420 PangoRectangle logical_rect;
2423 gint calendar_xsep = calendar_get_xsep (calendar);
2424 gint inner_border = calendar_get_inner_border (calendar);
2427 style = gtk_widget_get_style (widget);
2431 y = priv->header_h + priv->day_name_h + (style->ythickness + inner_border);
2432 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
2433 x = style->xthickness + inner_border;
2435 x = gtk_widget_get_allocated_width (widget) - priv->week_width - (style->xthickness + inner_border);
2437 gtk_widget_style_get (GTK_WIDGET (widget),
2438 "focus-line-width", &focus_width,
2439 "focus-padding", &focus_padding,
2443 * Draw a rectangle as inverted background for the labels.
2446 gdk_cairo_set_source_color (cr, SELECTED_BG_COLOR (widget));
2447 if (priv->display_flags & GTK_CALENDAR_SHOW_DAY_NAMES)
2448 cairo_rectangle (cr,
2449 x + CALENDAR_MARGIN,
2451 priv->week_width - CALENDAR_MARGIN,
2452 priv->main_h - CALENDAR_MARGIN);
2454 cairo_rectangle (cr,
2455 x + CALENDAR_MARGIN,
2456 y + CALENDAR_MARGIN,
2457 priv->week_width - CALENDAR_MARGIN,
2458 priv->main_h - 2 * CALENDAR_MARGIN);
2465 layout = gtk_widget_create_pango_layout (widget, NULL);
2467 gdk_cairo_set_source_color (cr, SELECTED_FG_COLOR (widget));
2468 day_height = calendar_row_height (calendar);
2469 for (row = 0; row < 6; row++)
2474 if (priv->day[row][6] < 15 && row > 3 && priv->month == 11)
2477 result = week_of_year (&week, &year,
2478 ((priv->day[row][6] < 15 && row > 3 ? 1 : 0)
2479 + priv->month) % 12 + 1, priv->day[row][6]);
2480 g_return_if_fail (result);
2482 /* Translators: this defines whether the week numbers should use
2483 * localized digits or the ones used in English (0123...).
2485 * Translate to "%Id" if you want to use localized digits, or
2486 * translate to "%d" otherwise.
2488 * Note that translating this doesn't guarantee that you get localized
2489 * digits. That needs support from your system and locale definition
2492 g_snprintf (buffer, sizeof (buffer), C_("calendar:week:digits", "%d"), week);
2493 pango_layout_set_text (layout, buffer, -1);
2494 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2496 y_loc = calendar_top_y_for_row (calendar, row) + (day_height - logical_rect.height) / 2;
2498 x_loc = x + (priv->week_width
2499 - logical_rect.width
2500 - calendar_xsep - focus_padding - focus_width);
2502 cairo_move_to (cr, x_loc, y_loc);
2503 pango_cairo_show_layout (cr, layout);
2506 g_object_unref (layout);
2512 calendar_invalidate_day_num (GtkCalendar *calendar,
2515 GtkCalendarPrivate *priv = calendar->priv;
2516 gint r, c, row, col;
2520 for (r = 0; r < 6; r++)
2521 for (c = 0; c < 7; c++)
2522 if (priv->day_month[r][c] == MONTH_CURRENT &&
2523 priv->day[r][c] == day)
2529 g_return_if_fail (row != -1);
2530 g_return_if_fail (col != -1);
2532 calendar_invalidate_day (calendar, row, col);
2536 calendar_invalidate_day (GtkCalendar *calendar,
2540 GdkRectangle day_rect;
2541 GtkAllocation allocation;
2543 gtk_widget_get_allocation (GTK_WIDGET (calendar), &allocation);
2544 calendar_day_rectangle (calendar, row, col, &day_rect);
2545 gtk_widget_queue_draw_area (GTK_WIDGET (calendar),
2546 allocation.x + day_rect.x,
2547 allocation.y + day_rect.y,
2548 day_rect.width, day_rect.height);
2552 is_color_attribute (PangoAttribute *attribute,
2555 return (attribute->klass->type == PANGO_ATTR_FOREGROUND ||
2556 attribute->klass->type == PANGO_ATTR_BACKGROUND);
2560 calendar_paint_day (GtkCalendar *calendar,
2565 GtkWidget *widget = GTK_WIDGET (calendar);
2566 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2568 GdkColor *text_color;
2573 GdkRectangle day_rect;
2575 PangoLayout *layout;
2576 PangoRectangle logical_rect;
2577 gboolean overflow = FALSE;
2578 gboolean show_details;
2580 g_return_if_fail (row < 6);
2581 g_return_if_fail (col < 7);
2583 style = gtk_widget_get_style (widget);
2585 day = priv->day[row][col];
2586 show_details = (priv->display_flags & GTK_CALENDAR_SHOW_DETAILS);
2588 calendar_day_rectangle (calendar, row, col, &day_rect);
2590 if (priv->day_month[row][col] == MONTH_PREV)
2592 text_color = PREV_MONTH_COLOR (widget);
2594 else if (priv->day_month[row][col] == MONTH_NEXT)
2596 text_color = NEXT_MONTH_COLOR (widget);
2600 if (priv->selected_day == day)
2602 gdk_cairo_set_source_color (cr, SELECTED_BG_COLOR (widget));
2603 gdk_cairo_rectangle (cr, &day_rect);
2606 if (priv->selected_day == day)
2607 text_color = SELECTED_FG_COLOR (widget);
2608 else if (priv->marked_date[day-1])
2609 text_color = MARKED_COLOR (widget);
2611 text_color = NORMAL_DAY_COLOR (widget);
2614 /* Translators: this defines whether the day numbers should use
2615 * localized digits or the ones used in English (0123...).
2617 * Translate to "%Id" if you want to use localized digits, or
2618 * translate to "%d" otherwise.
2620 * Note that translating this doesn't guarantee that you get localized
2621 * digits. That needs support from your system and locale definition
2624 g_snprintf (buffer, sizeof (buffer), C_("calendar:day:digits", "%d"), day);
2626 /* Get extra information to show, if any: */
2628 detail = gtk_calendar_get_detail (calendar, row, col);
2630 layout = gtk_widget_create_pango_layout (widget, buffer);
2631 pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
2632 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2634 x_loc = day_rect.x + (day_rect.width - logical_rect.width) / 2;
2637 gdk_cairo_set_source_color (cr, text_color);
2638 cairo_move_to (cr, x_loc, y_loc);
2639 pango_cairo_show_layout (cr, layout);
2641 if (priv->day_month[row][col] == MONTH_CURRENT &&
2642 (priv->marked_date[day-1] || (detail && !show_details)))
2644 cairo_move_to (cr, x_loc - 1, y_loc);
2645 pango_cairo_show_layout (cr, layout);
2648 y_loc += priv->max_day_char_descent;
2650 if (priv->detail_func && show_details)
2654 if (priv->selected_day == day)
2655 gdk_cairo_set_source_color (cr, &style->text[GTK_STATE_ACTIVE]);
2656 else if (priv->day_month[row][col] == MONTH_CURRENT)
2657 gdk_cairo_set_source_color (cr, &style->base[GTK_STATE_ACTIVE]);
2659 gdk_cairo_set_source_color (cr, &style->base[GTK_STATE_INSENSITIVE]);
2661 cairo_set_line_width (cr, 1);
2662 cairo_move_to (cr, day_rect.x + 2, y_loc + 0.5);
2663 cairo_line_to (cr, day_rect.x + day_rect.width - 2, y_loc + 0.5);
2671 if (detail && show_details)
2673 gchar *markup = g_strconcat ("<small>", detail, "</small>", NULL);
2674 pango_layout_set_markup (layout, markup, -1);
2677 if (day == priv->selected_day)
2679 /* Stripping colors as they conflict with selection marking. */
2681 PangoAttrList *attrs = pango_layout_get_attributes (layout);
2682 PangoAttrList *colors = NULL;
2685 colors = pango_attr_list_filter (attrs, is_color_attribute, NULL);
2687 pango_attr_list_unref (colors);
2690 pango_layout_set_wrap (layout, PANGO_WRAP_WORD_CHAR);
2691 pango_layout_set_width (layout, PANGO_SCALE * day_rect.width);
2693 if (priv->detail_height_rows)
2695 gint dy = day_rect.height - (y_loc - day_rect.y);
2696 pango_layout_set_height (layout, PANGO_SCALE * dy);
2697 pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_END);
2700 cairo_move_to (cr, day_rect.x, y_loc);
2701 pango_cairo_show_layout (cr, layout);
2704 if (gtk_widget_has_focus (widget)
2705 && priv->focus_row == row && priv->focus_col == col)
2709 if (priv->selected_day == day)
2710 state = gtk_widget_has_focus (widget) ? GTK_STATE_SELECTED : GTK_STATE_ACTIVE;
2712 state = GTK_STATE_NORMAL;
2714 gtk_paint_focus (style, cr,
2715 state, widget, "calendar-day",
2716 day_rect.x, day_rect.y,
2717 day_rect.width, day_rect.height);
2721 priv->detail_overflow[row] |= (1 << col);
2723 priv->detail_overflow[row] &= ~(1 << col);
2725 g_object_unref (layout);
2730 calendar_paint_main (GtkCalendar *calendar,
2737 for (col = 0; col < 7; col++)
2738 for (row = 0; row < 6; row++)
2739 calendar_paint_day (calendar, cr, row, col);
2745 calendar_invalidate_arrow (GtkCalendar *calendar,
2748 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2750 if (priv->display_flags & GTK_CALENDAR_SHOW_HEADING &&
2751 priv->arrow_win[arrow])
2754 GtkAllocation allocation;
2756 calendar_arrow_rectangle (calendar, arrow, &rect);
2757 gtk_widget_get_allocation (GTK_WIDGET (calendar), &allocation);
2758 gtk_widget_queue_draw_area (GTK_WIDGET (calendar),
2759 allocation.x + rect.x,
2760 allocation.y + rect.y,
2761 rect.width, rect.height);
2766 calendar_paint_arrow (GtkCalendar *calendar,
2770 GtkWidget *widget = GTK_WIDGET (calendar);
2771 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
2776 if (!priv->arrow_win[arrow])
2779 calendar_arrow_rectangle (calendar, arrow, &rect);
2783 style = gtk_widget_get_style (widget);
2784 state = priv->arrow_state[arrow];
2786 cairo_rectangle (cr, rect.x, rect.y, rect.width, rect.height);
2787 gdk_cairo_set_source_color (cr, &style->bg[state]);
2790 if (arrow == ARROW_MONTH_LEFT || arrow == ARROW_YEAR_LEFT)
2791 gtk_paint_arrow (style, cr, state,
2792 GTK_SHADOW_OUT, widget, "calendar",
2793 GTK_ARROW_LEFT, TRUE,
2794 rect.x + (rect.width - 8) / 2,
2795 rect.y + (rect.height - 8) / 2,
2798 gtk_paint_arrow (style, cr, state,
2799 GTK_SHADOW_OUT, widget, "calendar",
2800 GTK_ARROW_RIGHT, TRUE,
2801 rect.x + (rect.width - 8) / 2,
2802 rect.y + (rect.height - 8) / 2,
2809 gtk_calendar_draw (GtkWidget *widget,
2812 GtkCalendar *calendar = GTK_CALENDAR (widget);
2813 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
2816 if (gtk_cairo_should_draw_window (cr, gtk_widget_get_window (widget)))
2818 gdk_cairo_set_source_color (cr, BACKGROUND_COLOR (widget));
2819 cairo_rectangle (cr,
2821 gtk_widget_get_allocated_width (widget),
2822 gtk_widget_get_allocated_height (widget));
2824 gtk_paint_shadow (gtk_widget_get_style (widget), cr,
2825 gtk_widget_get_state (widget), GTK_SHADOW_IN,
2828 gtk_widget_get_allocated_width (widget),
2829 gtk_widget_get_allocated_height (widget));
2832 calendar_paint_main (calendar, cr);
2834 if (priv->display_flags & GTK_CALENDAR_SHOW_HEADING)
2836 calendar_paint_header (calendar, cr);
2837 for (i = 0; i < 4; i++)
2838 calendar_paint_arrow (calendar, cr, i);
2841 if (priv->display_flags & GTK_CALENDAR_SHOW_DAY_NAMES)
2842 calendar_paint_day_names (calendar, cr);
2844 if (priv->display_flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
2845 calendar_paint_week_numbers (calendar, cr);
2851 /****************************************
2853 ****************************************/
2856 calendar_arrow_action (GtkCalendar *calendar,
2861 case ARROW_YEAR_LEFT:
2862 calendar_set_year_prev (calendar);
2864 case ARROW_YEAR_RIGHT:
2865 calendar_set_year_next (calendar);
2867 case ARROW_MONTH_LEFT:
2868 calendar_set_month_prev (calendar);
2870 case ARROW_MONTH_RIGHT:
2871 calendar_set_month_next (calendar);
2879 calendar_timer (gpointer data)
2881 GtkCalendar *calendar = data;
2882 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2883 gboolean retval = FALSE;
2887 calendar_arrow_action (calendar, priv->click_child);
2889 if (priv->need_timer)
2891 GtkSettings *settings;
2894 settings = gtk_widget_get_settings (GTK_WIDGET (calendar));
2895 g_object_get (settings, "gtk-timeout-repeat", &timeout, NULL);
2897 priv->need_timer = FALSE;
2898 priv->timer = gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT_IDLE,
2899 timeout * SCROLL_DELAY_FACTOR,
2900 (GSourceFunc) calendar_timer,
2901 (gpointer) calendar, NULL);
2911 calendar_start_spinning (GtkCalendar *calendar,
2914 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2916 priv->click_child = click_child;
2920 GtkSettings *settings;
2923 settings = gtk_widget_get_settings (GTK_WIDGET (calendar));
2924 g_object_get (settings, "gtk-timeout-initial", &timeout, NULL);
2926 priv->need_timer = TRUE;
2927 priv->timer = gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT_IDLE,
2929 (GSourceFunc) calendar_timer,
2930 (gpointer) calendar, NULL);
2935 calendar_stop_spinning (GtkCalendar *calendar)
2937 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2941 g_source_remove (priv->timer);
2943 priv->need_timer = FALSE;
2948 calendar_main_button_press (GtkCalendar *calendar,
2949 GdkEventButton *event)
2951 GtkWidget *widget = GTK_WIDGET (calendar);
2952 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2958 GtkAllocation allocation;
2960 x = (gint) (event->x);
2961 y = (gint) (event->y);
2963 gdk_window_get_position (priv->main_win, &win_x, &win_y);
2964 gtk_widget_get_allocation (widget, &allocation);
2966 row = calendar_row_from_y (calendar, y + win_y - allocation.y);
2967 col = calendar_column_from_x (calendar, x + win_x - allocation.x);
2969 /* If row or column isn't found, just return. */
2970 if (row == -1 || col == -1)
2973 day_month = priv->day_month[row][col];
2975 if (event->type == GDK_BUTTON_PRESS)
2977 day = priv->day[row][col];
2979 if (day_month == MONTH_PREV)
2980 calendar_set_month_prev (calendar);
2981 else if (day_month == MONTH_NEXT)
2982 calendar_set_month_next (calendar);
2984 if (!gtk_widget_has_focus (widget))
2985 gtk_widget_grab_focus (widget);
2987 if (event->button == 1)
2990 priv->drag_start_x = x;
2991 priv->drag_start_y = y;
2994 calendar_select_and_focus_day (calendar, day);
2996 else if (event->type == GDK_2BUTTON_PRESS)
2999 if (day_month == MONTH_CURRENT)
3000 g_signal_emit (calendar,
3001 gtk_calendar_signals[DAY_SELECTED_DOUBLE_CLICK_SIGNAL],
3007 gtk_calendar_button_press (GtkWidget *widget,
3008 GdkEventButton *event)
3010 GtkCalendar *calendar = GTK_CALENDAR (widget);
3011 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3014 if (event->window == priv->main_win)
3015 calendar_main_button_press (calendar, event);
3017 if (!gtk_widget_has_focus (widget))
3018 gtk_widget_grab_focus (widget);
3020 for (arrow = ARROW_YEAR_LEFT; arrow <= ARROW_MONTH_RIGHT; arrow++)
3022 if (event->window == priv->arrow_win[arrow])
3025 /* only call the action on single click, not double */
3026 if (event->type == GDK_BUTTON_PRESS)
3028 if (event->button == 1)
3029 calendar_start_spinning (calendar, arrow);
3031 calendar_arrow_action (calendar, arrow);
3042 gtk_calendar_button_release (GtkWidget *widget,
3043 GdkEventButton *event)
3045 GtkCalendar *calendar = GTK_CALENDAR (widget);
3046 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3048 if (event->button == 1)
3050 calendar_stop_spinning (calendar);
3060 gtk_calendar_motion_notify (GtkWidget *widget,
3061 GdkEventMotion *event)
3063 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3064 gint event_x, event_y;
3066 event_x = (gint) (event->x);
3067 event_y = (gint) (event->y);
3071 if (gtk_drag_check_threshold (widget,
3072 priv->drag_start_x, priv->drag_start_y,
3073 event->x, event->y))
3075 GdkDragContext *context;
3076 GtkTargetList *target_list = gtk_target_list_new (NULL, 0);
3077 gtk_target_list_add_text_targets (target_list, 0);
3078 context = gtk_drag_begin (widget, target_list, GDK_ACTION_COPY,
3079 1, (GdkEvent *)event);
3082 gtk_target_list_unref (target_list);
3083 gtk_drag_set_icon_default (context);
3091 gtk_calendar_enter_notify (GtkWidget *widget,
3092 GdkEventCrossing *event)
3094 GtkCalendar *calendar = GTK_CALENDAR (widget);
3095 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3097 if (event->window == priv->arrow_win[ARROW_MONTH_LEFT])
3099 priv->arrow_state[ARROW_MONTH_LEFT] = GTK_STATE_PRELIGHT;
3100 calendar_invalidate_arrow (calendar, ARROW_MONTH_LEFT);
3103 if (event->window == priv->arrow_win[ARROW_MONTH_RIGHT])
3105 priv->arrow_state[ARROW_MONTH_RIGHT] = GTK_STATE_PRELIGHT;
3106 calendar_invalidate_arrow (calendar, ARROW_MONTH_RIGHT);
3109 if (event->window == priv->arrow_win[ARROW_YEAR_LEFT])
3111 priv->arrow_state[ARROW_YEAR_LEFT] = GTK_STATE_PRELIGHT;
3112 calendar_invalidate_arrow (calendar, ARROW_YEAR_LEFT);
3115 if (event->window == priv->arrow_win[ARROW_YEAR_RIGHT])
3117 priv->arrow_state[ARROW_YEAR_RIGHT] = GTK_STATE_PRELIGHT;
3118 calendar_invalidate_arrow (calendar, ARROW_YEAR_RIGHT);
3125 gtk_calendar_leave_notify (GtkWidget *widget,
3126 GdkEventCrossing *event)
3128 GtkCalendar *calendar = GTK_CALENDAR (widget);
3129 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3131 if (event->window == priv->arrow_win[ARROW_MONTH_LEFT])
3133 priv->arrow_state[ARROW_MONTH_LEFT] = GTK_STATE_NORMAL;
3134 calendar_invalidate_arrow (calendar, ARROW_MONTH_LEFT);
3137 if (event->window == priv->arrow_win[ARROW_MONTH_RIGHT])
3139 priv->arrow_state[ARROW_MONTH_RIGHT] = GTK_STATE_NORMAL;
3140 calendar_invalidate_arrow (calendar, ARROW_MONTH_RIGHT);
3143 if (event->window == priv->arrow_win[ARROW_YEAR_LEFT])
3145 priv->arrow_state[ARROW_YEAR_LEFT] = GTK_STATE_NORMAL;
3146 calendar_invalidate_arrow (calendar, ARROW_YEAR_LEFT);
3149 if (event->window == priv->arrow_win[ARROW_YEAR_RIGHT])
3151 priv->arrow_state[ARROW_YEAR_RIGHT] = GTK_STATE_NORMAL;
3152 calendar_invalidate_arrow (calendar, ARROW_YEAR_RIGHT);
3159 gtk_calendar_scroll (GtkWidget *widget,
3160 GdkEventScroll *event)
3162 GtkCalendar *calendar = GTK_CALENDAR (widget);
3164 if (event->direction == GDK_SCROLL_UP)
3166 if (!gtk_widget_has_focus (widget))
3167 gtk_widget_grab_focus (widget);
3168 calendar_set_month_prev (calendar);
3170 else if (event->direction == GDK_SCROLL_DOWN)
3172 if (!gtk_widget_has_focus (widget))
3173 gtk_widget_grab_focus (widget);
3174 calendar_set_month_next (calendar);
3183 /****************************************
3185 ****************************************/
3188 move_focus (GtkCalendar *calendar,
3191 GtkCalendarPrivate *priv = calendar->priv;
3192 GtkTextDirection text_dir = gtk_widget_get_direction (GTK_WIDGET (calendar));
3194 if ((text_dir == GTK_TEXT_DIR_LTR && direction == -1) ||
3195 (text_dir == GTK_TEXT_DIR_RTL && direction == 1))
3197 if (priv->focus_col > 0)
3199 else if (priv->focus_row > 0)
3201 priv->focus_col = 6;
3205 if (priv->focus_col < 0)
3206 priv->focus_col = 6;
3207 if (priv->focus_row < 0)
3208 priv->focus_row = 5;
3212 if (priv->focus_col < 6)
3214 else if (priv->focus_row < 5)
3216 priv->focus_col = 0;
3220 if (priv->focus_col < 0)
3221 priv->focus_col = 0;
3222 if (priv->focus_row < 0)
3223 priv->focus_row = 0;
3228 gtk_calendar_key_press (GtkWidget *widget,
3231 GtkCalendar *calendar = GTK_CALENDAR (widget);
3232 GtkCalendarPrivate *priv = calendar->priv;
3240 old_focus_row = priv->focus_row;
3241 old_focus_col = priv->focus_col;
3243 switch (event->keyval)
3245 case GDK_KEY_KP_Left:
3248 if (event->state & GDK_CONTROL_MASK)
3249 calendar_set_month_prev (calendar);
3252 move_focus (calendar, -1);
3253 calendar_invalidate_day (calendar, old_focus_row, old_focus_col);
3254 calendar_invalidate_day (calendar, priv->focus_row,
3258 case GDK_KEY_KP_Right:
3261 if (event->state & GDK_CONTROL_MASK)
3262 calendar_set_month_next (calendar);
3265 move_focus (calendar, 1);
3266 calendar_invalidate_day (calendar, old_focus_row, old_focus_col);
3267 calendar_invalidate_day (calendar, priv->focus_row,
3274 if (event->state & GDK_CONTROL_MASK)
3275 calendar_set_year_prev (calendar);
3278 if (priv->focus_row > 0)
3280 if (priv->focus_row < 0)
3281 priv->focus_row = 5;
3282 if (priv->focus_col < 0)
3283 priv->focus_col = 6;
3284 calendar_invalidate_day (calendar, old_focus_row, old_focus_col);
3285 calendar_invalidate_day (calendar, priv->focus_row,
3289 case GDK_KEY_KP_Down:
3292 if (event->state & GDK_CONTROL_MASK)
3293 calendar_set_year_next (calendar);
3296 if (priv->focus_row < 5)
3298 if (priv->focus_col < 0)
3299 priv->focus_col = 0;
3300 calendar_invalidate_day (calendar, old_focus_row, old_focus_col);
3301 calendar_invalidate_day (calendar, priv->focus_row,
3305 case GDK_KEY_KP_Space:
3307 row = priv->focus_row;
3308 col = priv->focus_col;
3310 if (row > -1 && col > -1)
3314 day = priv->day[row][col];
3315 if (priv->day_month[row][col] == MONTH_PREV)
3316 calendar_set_month_prev (calendar);
3317 else if (priv->day_month[row][col] == MONTH_NEXT)
3318 calendar_set_month_next (calendar);
3320 calendar_select_and_focus_day (calendar, day);
3328 /****************************************
3329 * Misc widget methods *
3330 ****************************************/
3333 gtk_calendar_state_changed (GtkWidget *widget,
3334 GtkStateType previous_state)
3336 GtkCalendar *calendar = GTK_CALENDAR (widget);
3337 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3340 if (!gtk_widget_is_sensitive (widget))
3343 calendar_stop_spinning (calendar);
3346 for (i = 0; i < 4; i++)
3347 if (gtk_widget_is_sensitive (widget))
3348 priv->arrow_state[i] = GTK_STATE_NORMAL;
3350 priv->arrow_state[i] = GTK_STATE_INSENSITIVE;
3354 gtk_calendar_grab_notify (GtkWidget *widget,
3355 gboolean was_grabbed)
3358 calendar_stop_spinning (GTK_CALENDAR (widget));
3362 gtk_calendar_focus_out (GtkWidget *widget,
3363 GdkEventFocus *event)
3365 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3366 GtkCalendar *calendar = GTK_CALENDAR (widget);
3368 calendar_queue_refresh (calendar);
3369 calendar_stop_spinning (calendar);
3377 /****************************************
3379 ****************************************/
3382 gtk_calendar_drag_data_get (GtkWidget *widget,
3383 GdkDragContext *context,
3384 GtkSelectionData *selection_data,
3388 GtkCalendar *calendar = GTK_CALENDAR (widget);
3389 GtkCalendarPrivate *priv = calendar->priv;
3394 date = g_date_new_dmy (priv->selected_day, priv->month + 1, priv->year);
3395 len = g_date_strftime (str, 127, "%x", date);
3396 gtk_selection_data_set_text (selection_data, str, len);
3401 /* Get/set whether drag_motion requested the drag data and
3402 * drag_data_received should thus not actually insert the data,
3403 * since the data doesn't result from a drop.
3406 set_status_pending (GdkDragContext *context,
3407 GdkDragAction suggested_action)
3409 g_object_set_data (G_OBJECT (context),
3410 I_("gtk-calendar-status-pending"),
3411 GINT_TO_POINTER (suggested_action));
3414 static GdkDragAction
3415 get_status_pending (GdkDragContext *context)
3417 return GPOINTER_TO_INT (g_object_get_data (G_OBJECT (context),
3418 "gtk-calendar-status-pending"));
3422 gtk_calendar_drag_leave (GtkWidget *widget,
3423 GdkDragContext *context,
3426 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3428 priv->drag_highlight = 0;
3429 gtk_drag_unhighlight (widget);
3434 gtk_calendar_drag_motion (GtkWidget *widget,
3435 GdkDragContext *context,
3440 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3443 if (!priv->drag_highlight)
3445 priv->drag_highlight = 1;
3446 gtk_drag_highlight (widget);
3449 target = gtk_drag_dest_find_target (widget, context, NULL);
3450 if (target == GDK_NONE || gdk_drag_context_get_suggested_action (context) == 0)
3451 gdk_drag_status (context, 0, time);
3454 set_status_pending (context, gdk_drag_context_get_suggested_action (context));
3455 gtk_drag_get_data (widget, context, target, time);
3462 gtk_calendar_drag_drop (GtkWidget *widget,
3463 GdkDragContext *context,
3470 target = gtk_drag_dest_find_target (widget, context, NULL);
3471 if (target != GDK_NONE)
3473 gtk_drag_get_data (widget, context,
3483 gtk_calendar_drag_data_received (GtkWidget *widget,
3484 GdkDragContext *context,
3487 GtkSelectionData *selection_data,
3491 GtkCalendar *calendar = GTK_CALENDAR (widget);
3492 GtkCalendarPrivate *priv = calendar->priv;
3493 guint day, month, year;
3496 GdkDragAction suggested_action;
3498 suggested_action = get_status_pending (context);
3500 if (suggested_action)
3502 set_status_pending (context, 0);
3504 /* We are getting this data due to a request in drag_motion,
3505 * rather than due to a request in drag_drop, so we are just
3506 * supposed to call drag_status, not actually paste in the
3509 str = (gchar*) gtk_selection_data_get_text (selection_data);
3513 date = g_date_new ();
3514 g_date_set_parse (date, str);
3515 if (!g_date_valid (date))
3516 suggested_action = 0;
3521 suggested_action = 0;
3523 gdk_drag_status (context, suggested_action, time);
3528 date = g_date_new ();
3529 str = (gchar*) gtk_selection_data_get_text (selection_data);
3532 g_date_set_parse (date, str);
3536 if (!g_date_valid (date))
3538 g_warning ("Received invalid date data\n");
3540 gtk_drag_finish (context, FALSE, FALSE, time);
3544 day = g_date_get_day (date);
3545 month = g_date_get_month (date);
3546 year = g_date_get_year (date);
3549 gtk_drag_finish (context, TRUE, FALSE, time);
3552 g_object_freeze_notify (G_OBJECT (calendar));
3553 if (!(priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
3554 && (priv->display_flags & GTK_CALENDAR_SHOW_HEADING))
3555 gtk_calendar_select_month (calendar, month - 1, year);
3556 gtk_calendar_select_day (calendar, day);
3557 g_object_thaw_notify (G_OBJECT (calendar));
3561 /****************************************
3563 ****************************************/
3568 * Creates a new calendar, with the current date being selected.
3570 * Return value: a newly #GtkCalendar widget
3573 gtk_calendar_new (void)
3575 return g_object_new (GTK_TYPE_CALENDAR, NULL);
3579 * gtk_calendar_get_display_options:
3580 * @calendar: a #GtkCalendar
3582 * Returns the current display options of @calendar.
3584 * Return value: the display options.
3588 GtkCalendarDisplayOptions
3589 gtk_calendar_get_display_options (GtkCalendar *calendar)
3591 g_return_val_if_fail (GTK_IS_CALENDAR (calendar), 0);
3593 return calendar->priv->display_flags;
3597 * gtk_calendar_set_display_options:
3598 * @calendar: a #GtkCalendar
3599 * @flags: the display options to set
3601 * Sets display options (whether to display the heading and the month
3607 gtk_calendar_set_display_options (GtkCalendar *calendar,
3608 GtkCalendarDisplayOptions flags)
3610 GtkWidget *widget = GTK_WIDGET (calendar);
3611 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
3613 GtkCalendarDisplayOptions old_flags;
3615 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3617 old_flags = priv->display_flags;
3619 if (gtk_widget_get_realized (widget))
3621 if ((flags ^ priv->display_flags) & GTK_CALENDAR_NO_MONTH_CHANGE)
3624 if (! (flags & GTK_CALENDAR_NO_MONTH_CHANGE)
3625 && (priv->display_flags & GTK_CALENDAR_SHOW_HEADING))
3627 priv->display_flags &= ~GTK_CALENDAR_NO_MONTH_CHANGE;
3628 calendar_realize_arrows (calendar);
3629 if (gtk_widget_get_mapped (widget))
3630 calendar_map_arrows (calendar);
3634 calendar_unrealize_arrows (calendar);
3638 if ((flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_HEADING)
3642 if (flags & GTK_CALENDAR_SHOW_HEADING)
3644 priv->display_flags |= GTK_CALENDAR_SHOW_HEADING;
3645 calendar_realize_arrows (calendar);
3646 if (gtk_widget_get_mapped (widget))
3647 calendar_map_arrows (calendar);
3651 calendar_unrealize_arrows (calendar);
3656 if ((flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_DAY_NAMES)
3660 if (flags & GTK_CALENDAR_SHOW_DAY_NAMES)
3661 priv->display_flags |= GTK_CALENDAR_SHOW_DAY_NAMES;
3664 if ((flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
3668 if (flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
3669 priv->display_flags |= GTK_CALENDAR_SHOW_WEEK_NUMBERS;
3672 if ((flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_DETAILS)
3675 priv->display_flags = flags;
3677 gtk_widget_queue_resize (GTK_WIDGET (calendar));
3680 priv->display_flags = flags;
3682 g_object_freeze_notify (G_OBJECT (calendar));
3683 if ((old_flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_HEADING)
3684 g_object_notify (G_OBJECT (calendar), "show-heading");
3685 if ((old_flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_DAY_NAMES)
3686 g_object_notify (G_OBJECT (calendar), "show-day-names");
3687 if ((old_flags ^ priv->display_flags) & GTK_CALENDAR_NO_MONTH_CHANGE)
3688 g_object_notify (G_OBJECT (calendar), "no-month-change");
3689 if ((old_flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
3690 g_object_notify (G_OBJECT (calendar), "show-week-numbers");
3691 g_object_thaw_notify (G_OBJECT (calendar));
3695 * gtk_calendar_select_month:
3696 * @calendar: a #GtkCalendar
3697 * @month: a month number between 0 and 11.
3698 * @year: the year the month is in.
3700 * Shifts the calendar to a different month.
3703 gtk_calendar_select_month (GtkCalendar *calendar,
3707 GtkCalendarPrivate *priv;
3709 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3710 g_return_if_fail (month <= 11);
3712 priv = calendar->priv;
3714 priv->month = month;
3717 calendar_compute_days (calendar);
3718 calendar_queue_refresh (calendar);
3720 g_object_freeze_notify (G_OBJECT (calendar));
3721 g_object_notify (G_OBJECT (calendar), "month");
3722 g_object_notify (G_OBJECT (calendar), "year");
3723 g_object_thaw_notify (G_OBJECT (calendar));
3725 g_signal_emit (calendar,
3726 gtk_calendar_signals[MONTH_CHANGED_SIGNAL],
3731 * gtk_calendar_select_day:
3732 * @calendar: a #GtkCalendar.
3733 * @day: the day number between 1 and 31, or 0 to unselect
3734 * the currently selected day.
3736 * Selects a day from the current month.
3739 gtk_calendar_select_day (GtkCalendar *calendar,
3742 GtkCalendarPrivate *priv;
3744 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3745 g_return_if_fail (day <= 31);
3747 priv = calendar->priv;
3749 /* Deselect the old day */
3750 if (priv->selected_day > 0)
3754 selected_day = priv->selected_day;
3755 priv->selected_day = 0;
3756 if (gtk_widget_is_drawable (GTK_WIDGET (calendar)))
3757 calendar_invalidate_day_num (calendar, selected_day);
3760 priv->selected_day = day;
3762 /* Select the new day */
3765 if (gtk_widget_is_drawable (GTK_WIDGET (calendar)))
3766 calendar_invalidate_day_num (calendar, day);
3769 g_object_notify (G_OBJECT (calendar), "day");
3771 g_signal_emit (calendar,
3772 gtk_calendar_signals[DAY_SELECTED_SIGNAL],
3777 * gtk_calendar_clear_marks:
3778 * @calendar: a #GtkCalendar
3780 * Remove all visual markers.
3783 gtk_calendar_clear_marks (GtkCalendar *calendar)
3785 GtkCalendarPrivate *priv;
3788 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3790 priv = calendar->priv;
3792 for (day = 0; day < 31; day++)
3794 priv->marked_date[day] = FALSE;
3797 priv->num_marked_dates = 0;
3798 calendar_queue_refresh (calendar);
3802 * gtk_calendar_mark_day:
3803 * @calendar: a #GtkCalendar
3804 * @day: the day number to mark between 1 and 31.
3806 * Places a visual marker on a particular day.
3809 gtk_calendar_mark_day (GtkCalendar *calendar,
3812 GtkCalendarPrivate *priv;
3814 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3816 priv = calendar->priv;
3818 if (day >= 1 && day <= 31 && !priv->marked_date[day-1])
3820 priv->marked_date[day - 1] = TRUE;
3821 priv->num_marked_dates++;
3822 calendar_invalidate_day_num (calendar, day);
3827 * gtk_calendar_get_day_is_marked:
3828 * @calendar: a #GtkCalendar
3829 * @day: the day number between 1 and 31.
3831 * Returns if the @day of the @calendar is already marked.
3833 * Returns: whether the day is marked.
3838 gtk_calendar_get_day_is_marked (GtkCalendar *calendar,
3841 GtkCalendarPrivate *priv;
3843 g_return_val_if_fail (GTK_IS_CALENDAR (calendar), FALSE);
3845 priv = calendar->priv;
3847 if (day >= 1 && day <= 31)
3848 return priv->marked_date[day - 1];
3854 * gtk_calendar_unmark_day:
3855 * @calendar: a #GtkCalendar.
3856 * @day: the day number to unmark between 1 and 31.
3858 * Removes the visual marker from a particular day.
3861 gtk_calendar_unmark_day (GtkCalendar *calendar,
3864 GtkCalendarPrivate *priv;
3866 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3868 priv = calendar->priv;
3870 if (day >= 1 && day <= 31 && priv->marked_date[day-1])
3872 priv->marked_date[day - 1] = FALSE;
3873 priv->num_marked_dates--;
3874 calendar_invalidate_day_num (calendar, day);
3879 * gtk_calendar_get_date:
3880 * @calendar: a #GtkCalendar
3881 * @year: (allow-none): location to store the year as a decimal number (e.g. 2011), or %NULL
3882 * @month: (allow-none): location to store the month number (between 0 and 11), or %NULL
3883 * @day: (allow-none): location to store the day number (between 1 and 31), or %NULL
3885 * Obtains the selected date from a #GtkCalendar.
3888 gtk_calendar_get_date (GtkCalendar *calendar,
3893 GtkCalendarPrivate *priv;
3895 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3897 priv = calendar->priv;
3903 *month = priv->month;
3906 *day = priv->selected_day;
3910 * gtk_calendar_set_detail_func:
3911 * @calendar: a #GtkCalendar.
3912 * @func: a function providing details for each day.
3913 * @data: data to pass to @func invokations.
3914 * @destroy: a function for releasing @data.
3916 * Installs a function which provides Pango markup with detail information
3917 * for each day. Examples for such details are holidays or appointments. That
3918 * information is shown below each day when #GtkCalendar:show-details is set.
3919 * A tooltip containing with full detail information is provided, if the entire
3920 * text should not fit into the details area, or if #GtkCalendar:show-details
3923 * The size of the details area can be restricted by setting the
3924 * #GtkCalendar:detail-width-chars and #GtkCalendar:detail-height-rows
3930 gtk_calendar_set_detail_func (GtkCalendar *calendar,
3931 GtkCalendarDetailFunc func,
3933 GDestroyNotify destroy)
3935 GtkCalendarPrivate *priv;
3937 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3939 priv = GTK_CALENDAR_GET_PRIVATE (calendar);
3941 if (priv->detail_func_destroy)
3942 priv->detail_func_destroy (priv->detail_func_user_data);
3944 priv->detail_func = func;
3945 priv->detail_func_user_data = data;
3946 priv->detail_func_destroy = destroy;
3948 gtk_widget_set_has_tooltip (GTK_WIDGET (calendar),
3949 NULL != priv->detail_func);
3950 gtk_widget_queue_resize (GTK_WIDGET (calendar));
3954 * gtk_calendar_set_detail_width_chars:
3955 * @calendar: a #GtkCalendar.
3956 * @chars: detail width in characters.
3958 * Updates the width of detail cells.
3959 * See #GtkCalendar:detail-width-chars.
3964 gtk_calendar_set_detail_width_chars (GtkCalendar *calendar,
3967 GtkCalendarPrivate *priv;
3969 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3971 priv = GTK_CALENDAR_GET_PRIVATE (calendar);
3973 if (chars != priv->detail_width_chars)
3975 priv->detail_width_chars = chars;
3976 g_object_notify (G_OBJECT (calendar), "detail-width-chars");
3977 gtk_widget_queue_resize_no_redraw (GTK_WIDGET (calendar));
3982 * gtk_calendar_set_detail_height_rows:
3983 * @calendar: a #GtkCalendar.
3984 * @rows: detail height in rows.
3986 * Updates the height of detail cells.
3987 * See #GtkCalendar:detail-height-rows.
3992 gtk_calendar_set_detail_height_rows (GtkCalendar *calendar,
3995 GtkCalendarPrivate *priv;
3997 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3999 priv = GTK_CALENDAR_GET_PRIVATE (calendar);
4001 if (rows != priv->detail_height_rows)
4003 priv->detail_height_rows = rows;
4004 g_object_notify (G_OBJECT (calendar), "detail-height-rows");
4005 gtk_widget_queue_resize_no_redraw (GTK_WIDGET (calendar));
4010 * gtk_calendar_get_detail_width_chars:
4011 * @calendar: a #GtkCalendar.
4013 * Queries the width of detail cells, in characters.
4014 * See #GtkCalendar:detail-width-chars.
4018 * Return value: The width of detail cells, in characters.
4021 gtk_calendar_get_detail_width_chars (GtkCalendar *calendar)
4023 g_return_val_if_fail (GTK_IS_CALENDAR (calendar), 0);
4024 return GTK_CALENDAR_GET_PRIVATE (calendar)->detail_width_chars;
4028 * gtk_calendar_get_detail_height_rows:
4029 * @calendar: a #GtkCalendar.
4031 * Queries the height of detail cells, in rows.
4032 * See #GtkCalendar:detail-width-chars.
4036 * Return value: The height of detail cells, in rows.
4039 gtk_calendar_get_detail_height_rows (GtkCalendar *calendar)
4041 g_return_val_if_fail (GTK_IS_CALENDAR (calendar), 0);
4042 return GTK_CALENDAR_GET_PRIVATE (calendar)->detail_height_rows;