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"
85 /***************************************************************************/
86 /* The following date routines are taken from the lib_date package.
87 * They have been minimally edited to avoid conflict with types defined
91 static const guint month_length[2][13] =
93 { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
94 { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
97 static const guint days_in_months[2][14] =
99 { 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
100 { 0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
103 static glong calc_days(guint year, guint mm, guint dd);
104 static guint day_of_week(guint year, guint mm, guint dd);
105 static glong dates_difference(guint year1, guint mm1, guint dd1,
106 guint year2, guint mm2, guint dd2);
107 static guint weeks_in_year(guint year);
112 return((((year % 4) == 0) && ((year % 100) != 0)) || ((year % 400) == 0));
116 day_of_week (guint year, guint mm, guint dd)
120 days = calc_days(year, mm, dd);
127 return( (guint) days );
130 static guint weeks_in_year(guint year)
132 return(52 + ((day_of_week(year,1,1)==4) || (day_of_week(year,12,31)==4)));
136 check_date(guint year, guint mm, guint dd)
138 if (year < 1) return FALSE;
139 if ((mm < 1) || (mm > 12)) return FALSE;
140 if ((dd < 1) || (dd > month_length[leap(year)][mm])) return FALSE;
145 week_number(guint year, guint mm, guint dd)
149 first = day_of_week(year,1,1) - 1;
150 return( (guint) ( (dates_difference(year,1,1, year,mm,dd) + first) / 7L ) +
155 year_to_days(guint year)
157 return( year * 365L + (year / 4) - (year / 100) + (year / 400) );
162 calc_days(guint year, guint mm, guint dd)
166 if (year < 1) return(0L);
167 if ((mm < 1) || (mm > 12)) return(0L);
168 if ((dd < 1) || (dd > month_length[(lp = leap(year))][mm])) return(0L);
169 return( year_to_days(--year) + days_in_months[lp][mm] + dd );
173 week_of_year(guint *week, guint *year, guint mm, guint dd)
175 if (check_date(*year,mm,dd))
177 *week = week_number(*year,mm,dd);
179 *week = weeks_in_year(--(*year));
180 else if (*week > weeks_in_year(*year))
191 dates_difference(guint year1, guint mm1, guint dd1,
192 guint year2, guint mm2, guint dd2)
194 return( calc_days(year2, mm2, dd2) - calc_days(year1, mm1, dd1) );
197 /*** END OF lib_date routines ********************************************/
199 /* Spacing around day/week headers and main area, inside those windows */
200 #define CALENDAR_MARGIN 0
202 #define DAY_XSEP 0 /* not really good for small calendar */
203 #define DAY_YSEP 0 /* not really good for small calendar */
205 #define SCROLL_DELAY_FACTOR 5
221 MONTH_CHANGED_SIGNAL,
223 DAY_SELECTED_DOUBLE_CLICK_SIGNAL,
239 PROP_NO_MONTH_CHANGE,
240 PROP_SHOW_WEEK_NUMBERS,
242 PROP_DETAIL_WIDTH_CHARS,
243 PROP_DETAIL_HEIGHT_ROWS
246 static guint gtk_calendar_signals[LAST_SIGNAL] = { 0 };
248 struct _GtkCalendarPrivate
250 GtkCalendarDisplayOptions display_flags;
253 GdkWindow *arrow_win[4];
255 gchar grow_space [32];
261 gint day_month[6][7];
264 gint num_marked_dates;
265 gint marked_date[31];
274 guint arrow_state[4];
276 guint max_month_width;
277 guint max_year_width;
283 guint max_day_char_width;
284 guint max_day_char_ascent;
285 guint max_day_char_descent;
286 guint max_label_char_ascent;
287 guint max_label_char_descent;
288 guint max_week_char_width;
291 guint year_before : 1;
293 guint need_timer : 1;
296 guint drag_highlight : 1;
306 /* Optional callback, used to display extra information for each day. */
307 GtkCalendarDetailFunc detail_func;
308 gpointer detail_func_user_data;
309 GDestroyNotify detail_func_destroy;
311 /* Size requistion for details provided by the hook. */
312 gint detail_height_rows;
313 gint detail_width_chars;
314 gint detail_overflow[6];
317 #define GTK_CALENDAR_GET_PRIVATE(widget) (GTK_CALENDAR (widget)->priv)
319 static void gtk_calendar_finalize (GObject *calendar);
320 static void gtk_calendar_destroy (GtkWidget *widget);
321 static void gtk_calendar_set_property (GObject *object,
325 static void gtk_calendar_get_property (GObject *object,
330 static void gtk_calendar_realize (GtkWidget *widget);
331 static void gtk_calendar_unrealize (GtkWidget *widget);
332 static void gtk_calendar_map (GtkWidget *widget);
333 static void gtk_calendar_unmap (GtkWidget *widget);
334 static void gtk_calendar_get_preferred_width (GtkWidget *widget,
337 static void gtk_calendar_get_preferred_height (GtkWidget *widget,
340 static void gtk_calendar_size_allocate (GtkWidget *widget,
341 GtkAllocation *allocation);
342 static gboolean gtk_calendar_draw (GtkWidget *widget,
344 static gboolean gtk_calendar_button_press (GtkWidget *widget,
345 GdkEventButton *event);
346 static gboolean gtk_calendar_button_release (GtkWidget *widget,
347 GdkEventButton *event);
348 static gboolean gtk_calendar_motion_notify (GtkWidget *widget,
349 GdkEventMotion *event);
350 static gboolean gtk_calendar_enter_notify (GtkWidget *widget,
351 GdkEventCrossing *event);
352 static gboolean gtk_calendar_leave_notify (GtkWidget *widget,
353 GdkEventCrossing *event);
354 static gboolean gtk_calendar_scroll (GtkWidget *widget,
355 GdkEventScroll *event);
356 static gboolean gtk_calendar_key_press (GtkWidget *widget,
358 static gboolean gtk_calendar_focus_out (GtkWidget *widget,
359 GdkEventFocus *event);
360 static void gtk_calendar_grab_notify (GtkWidget *widget,
361 gboolean was_grabbed);
362 static void gtk_calendar_state_flags_changed (GtkWidget *widget,
363 GtkStateFlags previous_state);
364 static gboolean gtk_calendar_query_tooltip (GtkWidget *widget,
367 gboolean keyboard_mode,
368 GtkTooltip *tooltip);
370 static void gtk_calendar_drag_data_get (GtkWidget *widget,
371 GdkDragContext *context,
372 GtkSelectionData *selection_data,
375 static void gtk_calendar_drag_data_received (GtkWidget *widget,
376 GdkDragContext *context,
379 GtkSelectionData *selection_data,
382 static gboolean gtk_calendar_drag_motion (GtkWidget *widget,
383 GdkDragContext *context,
387 static void gtk_calendar_drag_leave (GtkWidget *widget,
388 GdkDragContext *context,
390 static gboolean gtk_calendar_drag_drop (GtkWidget *widget,
391 GdkDragContext *context,
396 static AtkObject *gtk_calendar_get_accessible (GtkWidget *widget);
398 static void calendar_start_spinning (GtkCalendar *calendar,
400 static void calendar_stop_spinning (GtkCalendar *calendar);
402 static void calendar_invalidate_day (GtkCalendar *widget,
405 static void calendar_invalidate_day_num (GtkCalendar *widget,
407 static void calendar_invalidate_arrow (GtkCalendar *widget,
410 static void calendar_compute_days (GtkCalendar *calendar);
411 static gint calendar_get_xsep (GtkCalendar *calendar);
412 static gint calendar_get_ysep (GtkCalendar *calendar);
413 static gint calendar_get_inner_border (GtkCalendar *calendar);
415 static char *default_abbreviated_dayname[7];
416 static char *default_monthname[12];
418 G_DEFINE_TYPE (GtkCalendar, gtk_calendar, GTK_TYPE_WIDGET)
421 gtk_calendar_class_init (GtkCalendarClass *class)
423 GObjectClass *gobject_class;
424 GtkWidgetClass *widget_class;
426 gobject_class = (GObjectClass*) class;
427 widget_class = (GtkWidgetClass*) class;
429 gobject_class->set_property = gtk_calendar_set_property;
430 gobject_class->get_property = gtk_calendar_get_property;
431 gobject_class->finalize = gtk_calendar_finalize;
433 widget_class->destroy = gtk_calendar_destroy;
434 widget_class->realize = gtk_calendar_realize;
435 widget_class->unrealize = gtk_calendar_unrealize;
436 widget_class->map = gtk_calendar_map;
437 widget_class->unmap = gtk_calendar_unmap;
438 widget_class->draw = gtk_calendar_draw;
439 widget_class->get_preferred_width = gtk_calendar_get_preferred_width;
440 widget_class->get_preferred_height = gtk_calendar_get_preferred_height;
441 widget_class->size_allocate = gtk_calendar_size_allocate;
442 widget_class->button_press_event = gtk_calendar_button_press;
443 widget_class->button_release_event = gtk_calendar_button_release;
444 widget_class->motion_notify_event = gtk_calendar_motion_notify;
445 widget_class->enter_notify_event = gtk_calendar_enter_notify;
446 widget_class->leave_notify_event = gtk_calendar_leave_notify;
447 widget_class->key_press_event = gtk_calendar_key_press;
448 widget_class->scroll_event = gtk_calendar_scroll;
449 widget_class->state_flags_changed = gtk_calendar_state_flags_changed;
450 widget_class->grab_notify = gtk_calendar_grab_notify;
451 widget_class->focus_out_event = gtk_calendar_focus_out;
452 widget_class->query_tooltip = gtk_calendar_query_tooltip;
454 widget_class->drag_data_get = gtk_calendar_drag_data_get;
455 widget_class->drag_motion = gtk_calendar_drag_motion;
456 widget_class->drag_leave = gtk_calendar_drag_leave;
457 widget_class->drag_drop = gtk_calendar_drag_drop;
458 widget_class->drag_data_received = gtk_calendar_drag_data_received;
460 widget_class->get_accessible = gtk_calendar_get_accessible;
466 * This property gets initially set to the current year.
468 g_object_class_install_property (gobject_class,
470 g_param_spec_int ("year",
472 P_("The selected year"),
474 GTK_PARAM_READWRITE));
479 * The selected month (as a number between 0 and 11).
480 * This property gets initially set to the current month.
482 g_object_class_install_property (gobject_class,
484 g_param_spec_int ("month",
486 P_("The selected month (as a number between 0 and 11)"),
488 GTK_PARAM_READWRITE));
493 * The selected day (as a number between 1 and 31, or 0
494 * to unselect the currently selected day).
495 * This property gets initially set to the current day.
497 g_object_class_install_property (gobject_class,
499 g_param_spec_int ("day",
501 P_("The selected day (as a number between 1 and 31, or 0 to unselect the currently selected day)"),
503 GTK_PARAM_READWRITE));
506 * GtkCalendar:show-heading:
508 * Determines whether a heading is displayed.
512 g_object_class_install_property (gobject_class,
514 g_param_spec_boolean ("show-heading",
516 P_("If TRUE, a heading is displayed"),
518 GTK_PARAM_READWRITE));
521 * GtkCalendar:show-day-names:
523 * Determines whether day names are displayed.
527 g_object_class_install_property (gobject_class,
529 g_param_spec_boolean ("show-day-names",
530 P_("Show Day Names"),
531 P_("If TRUE, day names are displayed"),
533 GTK_PARAM_READWRITE));
535 * GtkCalendar:no-month-change:
537 * Determines whether the selected month can be changed.
541 g_object_class_install_property (gobject_class,
542 PROP_NO_MONTH_CHANGE,
543 g_param_spec_boolean ("no-month-change",
544 P_("No Month Change"),
545 P_("If TRUE, the selected month cannot be changed"),
547 GTK_PARAM_READWRITE));
550 * GtkCalendar:show-week-numbers:
552 * Determines whether week numbers are displayed.
556 g_object_class_install_property (gobject_class,
557 PROP_SHOW_WEEK_NUMBERS,
558 g_param_spec_boolean ("show-week-numbers",
559 P_("Show Week Numbers"),
560 P_("If TRUE, week numbers are displayed"),
562 GTK_PARAM_READWRITE));
565 * GtkCalendar:detail-width-chars:
567 * Width of a detail cell, in characters.
568 * A value of 0 allows any width. See gtk_calendar_set_detail_func().
572 g_object_class_install_property (gobject_class,
573 PROP_DETAIL_WIDTH_CHARS,
574 g_param_spec_int ("detail-width-chars",
576 P_("Details width in characters"),
578 GTK_PARAM_READWRITE));
581 * GtkCalendar:detail-height-rows:
583 * Height of a detail cell, in rows.
584 * A value of 0 allows any width. See gtk_calendar_set_detail_func().
588 g_object_class_install_property (gobject_class,
589 PROP_DETAIL_HEIGHT_ROWS,
590 g_param_spec_int ("detail-height-rows",
591 P_("Details Height"),
592 P_("Details height in rows"),
594 GTK_PARAM_READWRITE));
597 * GtkCalendar:show-details:
599 * Determines whether details are shown directly in the widget, or if they are
600 * available only as tooltip. When this property is set days with details are
605 g_object_class_install_property (gobject_class,
607 g_param_spec_boolean ("show-details",
609 P_("If TRUE, details are shown"),
611 GTK_PARAM_READWRITE));
615 * GtkCalendar:inner-border
617 * The spacing around the day/week headers and main area.
619 gtk_widget_class_install_style_property (widget_class,
620 g_param_spec_int ("inner-border",
622 P_("Inner border space"),
624 GTK_PARAM_READABLE));
627 * GtkCalndar:vertical-separation
629 * Separation between day headers and main area.
631 gtk_widget_class_install_style_property (widget_class,
632 g_param_spec_int ("vertical-separation",
633 P_("Vertical separation"),
634 P_("Space between day headers and main area"),
636 GTK_PARAM_READABLE));
639 * GtkCalendar:horizontal-separation
641 * Separation between week headers and main area.
643 gtk_widget_class_install_style_property (widget_class,
644 g_param_spec_int ("horizontal-separation",
645 P_("Horizontal separation"),
646 P_("Space between week headers and main area"),
648 GTK_PARAM_READABLE));
651 * GtkCalendar::month-changed:
652 * @calendar: the object which received the signal.
654 * Emitted when the user clicks a button to change the selected month on a
657 gtk_calendar_signals[MONTH_CHANGED_SIGNAL] =
658 g_signal_new (I_("month-changed"),
659 G_OBJECT_CLASS_TYPE (gobject_class),
661 G_STRUCT_OFFSET (GtkCalendarClass, month_changed),
663 _gtk_marshal_VOID__VOID,
667 * GtkCalendar::day-selected:
668 * @calendar: the object which received the signal.
670 * Emitted when the user selects a day.
672 gtk_calendar_signals[DAY_SELECTED_SIGNAL] =
673 g_signal_new (I_("day-selected"),
674 G_OBJECT_CLASS_TYPE (gobject_class),
676 G_STRUCT_OFFSET (GtkCalendarClass, day_selected),
678 _gtk_marshal_VOID__VOID,
682 * GtkCalendar::day-selected-double-click:
683 * @calendar: the object which received the signal.
685 * Emitted when the user double-clicks a day.
687 gtk_calendar_signals[DAY_SELECTED_DOUBLE_CLICK_SIGNAL] =
688 g_signal_new (I_("day-selected-double-click"),
689 G_OBJECT_CLASS_TYPE (gobject_class),
691 G_STRUCT_OFFSET (GtkCalendarClass, day_selected_double_click),
693 _gtk_marshal_VOID__VOID,
697 * GtkCalendar::prev-month:
698 * @calendar: the object which received the signal.
700 * Emitted when the user switched to the previous month.
702 gtk_calendar_signals[PREV_MONTH_SIGNAL] =
703 g_signal_new (I_("prev-month"),
704 G_OBJECT_CLASS_TYPE (gobject_class),
706 G_STRUCT_OFFSET (GtkCalendarClass, prev_month),
708 _gtk_marshal_VOID__VOID,
712 * GtkCalendar::next-month:
713 * @calendar: the object which received the signal.
715 * Emitted when the user switched to the next month.
717 gtk_calendar_signals[NEXT_MONTH_SIGNAL] =
718 g_signal_new (I_("next-month"),
719 G_OBJECT_CLASS_TYPE (gobject_class),
721 G_STRUCT_OFFSET (GtkCalendarClass, next_month),
723 _gtk_marshal_VOID__VOID,
727 * GtkCalendar::prev-year:
728 * @calendar: the object which received the signal.
730 * Emitted when user switched to the previous year.
732 gtk_calendar_signals[PREV_YEAR_SIGNAL] =
733 g_signal_new (I_("prev-year"),
734 G_OBJECT_CLASS_TYPE (gobject_class),
736 G_STRUCT_OFFSET (GtkCalendarClass, prev_year),
738 _gtk_marshal_VOID__VOID,
742 * GtkCalendar::next-year:
743 * @calendar: the object which received the signal.
745 * Emitted when user switched to the next year.
747 gtk_calendar_signals[NEXT_YEAR_SIGNAL] =
748 g_signal_new (I_("next-year"),
749 G_OBJECT_CLASS_TYPE (gobject_class),
751 G_STRUCT_OFFSET (GtkCalendarClass, next_year),
753 _gtk_marshal_VOID__VOID,
756 g_type_class_add_private (gobject_class, sizeof (GtkCalendarPrivate));
760 gtk_calendar_init (GtkCalendar *calendar)
762 GtkWidget *widget = GTK_WIDGET (calendar);
767 wchar_t wbuffer[100];
772 GtkCalendarPrivate *priv;
774 #ifdef HAVE__NL_TIME_FIRST_WEEKDAY
775 union { unsigned int word; char *string; } langinfo;
776 gint week_1stday = 0;
777 gint first_weekday = 1;
783 priv = calendar->priv = G_TYPE_INSTANCE_GET_PRIVATE (calendar,
787 gtk_widget_set_can_focus (widget, TRUE);
788 gtk_widget_set_has_window (widget, FALSE);
790 if (!default_abbreviated_dayname[0])
794 tmp_time= (i+3)*86400;
795 strftime ( buffer, sizeof (buffer), "%a", gmtime (&tmp_time));
796 default_abbreviated_dayname[i] = g_locale_to_utf8 (buffer, -1, NULL, NULL, NULL);
798 if (!GetLocaleInfoW (GetThreadLocale (), LOCALE_SABBREVDAYNAME1 + (i+6)%7,
799 wbuffer, G_N_ELEMENTS (wbuffer)))
800 default_abbreviated_dayname[i] = g_strdup_printf ("(%d)", i);
802 default_abbreviated_dayname[i] = g_utf16_to_utf8 (wbuffer, -1, NULL, NULL, NULL);
806 if (!default_monthname[0])
811 strftime ( buffer, sizeof (buffer), "%B", gmtime (&tmp_time));
812 default_monthname[i] = g_locale_to_utf8 (buffer, -1, NULL, NULL, NULL);
814 if (!GetLocaleInfoW (GetThreadLocale (), LOCALE_SMONTHNAME1 + i,
815 wbuffer, G_N_ELEMENTS (wbuffer)))
816 default_monthname[i] = g_strdup_printf ("(%d)", i);
818 default_monthname[i] = g_utf16_to_utf8 (wbuffer, -1, NULL, NULL, NULL);
824 tm = localtime (&secs);
825 priv->month = tm->tm_mon;
826 priv->year = 1900 + tm->tm_year;
829 priv->marked_date[i] = FALSE;
830 priv->num_marked_dates = 0;
831 priv->selected_day = tm->tm_mday;
833 priv->display_flags = (GTK_CALENDAR_SHOW_HEADING |
834 GTK_CALENDAR_SHOW_DAY_NAMES |
835 GTK_CALENDAR_SHOW_DETAILS);
837 priv->focus_row = -1;
838 priv->focus_col = -1;
840 priv->max_year_width = 0;
841 priv->max_month_width = 0;
842 priv->max_day_char_width = 0;
843 priv->max_week_char_width = 0;
845 priv->max_day_char_ascent = 0;
846 priv->max_day_char_descent = 0;
847 priv->max_label_char_ascent = 0;
848 priv->max_label_char_descent = 0;
850 priv->arrow_width = 10;
852 priv->need_timer = 0;
854 priv->click_child = -1;
857 priv->drag_highlight = 0;
859 gtk_drag_dest_set (widget, 0, NULL, 0, GDK_ACTION_COPY);
860 gtk_drag_dest_add_text_targets (widget);
862 priv->year_before = 0;
864 /* Translate to calendar:YM if you want years to be displayed
865 * before months; otherwise translate to calendar:MY.
866 * Do *not* translate it to anything else, if it
867 * it isn't calendar:YM or calendar:MY it will not work.
869 * Note that the ordering described here is logical order, which is
870 * further influenced by BIDI ordering. Thus, if you have a default
871 * text direction of RTL and specify "calendar:YM", then the year
872 * will appear to the right of the month.
874 year_before = _("calendar:MY");
875 if (strcmp (year_before, "calendar:YM") == 0)
876 priv->year_before = 1;
877 else if (strcmp (year_before, "calendar:MY") != 0)
878 g_warning ("Whoever translated calendar:MY did so wrongly.\n");
881 priv->week_start = 0;
884 if (GetLocaleInfoW (GetThreadLocale (), LOCALE_IFIRSTDAYOFWEEK,
885 wbuffer, G_N_ELEMENTS (wbuffer)))
886 week_start = g_utf16_to_utf8 (wbuffer, -1, NULL, NULL, NULL);
888 if (week_start != NULL)
890 priv->week_start = (week_start[0] - '0' + 1) % 7;
894 #ifdef HAVE__NL_TIME_FIRST_WEEKDAY
895 langinfo.string = nl_langinfo (_NL_TIME_FIRST_WEEKDAY);
896 first_weekday = langinfo.string[0];
897 langinfo.string = nl_langinfo (_NL_TIME_WEEK_1STDAY);
898 week_origin = langinfo.word;
899 if (week_origin == 19971130) /* Sunday */
901 else if (week_origin == 19971201) /* Monday */
904 g_warning ("Unknown value of _NL_TIME_WEEK_1STDAY.\n");
906 priv->week_start = (week_1stday + first_weekday - 1) % 7;
908 /* Translate to calendar:week_start:0 if you want Sunday to be the
909 * first day of the week to calendar:week_start:1 if you want Monday
910 * to be the first day of the week, and so on.
912 week_start = _("calendar:week_start:0");
914 if (strncmp (week_start, "calendar:week_start:", 20) == 0)
915 priv->week_start = *(week_start + 20) - '0';
917 priv->week_start = -1;
919 if (priv->week_start < 0 || priv->week_start > 6)
921 g_warning ("Whoever translated calendar:week_start:0 did so wrongly.\n");
922 priv->week_start = 0;
927 calendar_compute_days (calendar);
931 /****************************************
932 * Utility Functions *
933 ****************************************/
936 calendar_queue_refresh (GtkCalendar *calendar)
938 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
940 if (!(priv->detail_func) ||
941 !(priv->display_flags & GTK_CALENDAR_SHOW_DETAILS) ||
942 (priv->detail_width_chars && priv->detail_height_rows))
943 gtk_widget_queue_draw (GTK_WIDGET (calendar));
945 gtk_widget_queue_resize (GTK_WIDGET (calendar));
949 calendar_set_month_next (GtkCalendar *calendar)
952 GtkCalendarPrivate *priv = calendar->priv;
954 if (priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
957 if (priv->month == 11)
965 calendar_compute_days (calendar);
966 g_signal_emit (calendar,
967 gtk_calendar_signals[NEXT_MONTH_SIGNAL],
969 g_signal_emit (calendar,
970 gtk_calendar_signals[MONTH_CHANGED_SIGNAL],
973 month_len = month_length[leap (priv->year)][priv->month + 1];
975 if (month_len < priv->selected_day)
977 priv->selected_day = 0;
978 gtk_calendar_select_day (calendar, month_len);
981 gtk_calendar_select_day (calendar, priv->selected_day);
983 calendar_queue_refresh (calendar);
987 calendar_set_year_prev (GtkCalendar *calendar)
989 GtkCalendarPrivate *priv = calendar->priv;
993 calendar_compute_days (calendar);
994 g_signal_emit (calendar,
995 gtk_calendar_signals[PREV_YEAR_SIGNAL],
997 g_signal_emit (calendar,
998 gtk_calendar_signals[MONTH_CHANGED_SIGNAL],
1001 month_len = month_length[leap (priv->year)][priv->month + 1];
1003 if (month_len < priv->selected_day)
1005 priv->selected_day = 0;
1006 gtk_calendar_select_day (calendar, month_len);
1009 gtk_calendar_select_day (calendar, priv->selected_day);
1011 calendar_queue_refresh (calendar);
1015 calendar_set_year_next (GtkCalendar *calendar)
1017 GtkCalendarPrivate *priv = calendar->priv;
1021 calendar_compute_days (calendar);
1022 g_signal_emit (calendar,
1023 gtk_calendar_signals[NEXT_YEAR_SIGNAL],
1025 g_signal_emit (calendar,
1026 gtk_calendar_signals[MONTH_CHANGED_SIGNAL],
1029 month_len = month_length[leap (priv->year)][priv->month + 1];
1031 if (month_len < priv->selected_day)
1033 priv->selected_day = 0;
1034 gtk_calendar_select_day (calendar, month_len);
1037 gtk_calendar_select_day (calendar, priv->selected_day);
1039 calendar_queue_refresh (calendar);
1043 calendar_compute_days (GtkCalendar *calendar)
1045 GtkCalendarPrivate *priv = calendar->priv;
1048 gint ndays_in_month;
1049 gint ndays_in_prev_month;
1056 month = priv->month + 1;
1058 ndays_in_month = month_length[leap (year)][month];
1060 first_day = day_of_week (year, month, 1);
1061 first_day = (first_day + 7 - priv->week_start) % 7;
1063 /* Compute days of previous month */
1065 ndays_in_prev_month = month_length[leap (year)][month-1];
1067 ndays_in_prev_month = month_length[leap (year)][12];
1068 day = ndays_in_prev_month - first_day + 1;
1073 for (col = 0; col < first_day; col++)
1075 priv->day[row][col] = day;
1076 priv->day_month[row][col] = MONTH_PREV;
1081 /* Compute days of current month */
1083 for (day = 1; day <= ndays_in_month; day++)
1085 priv->day[row][col] = day;
1086 priv->day_month[row][col] = MONTH_CURRENT;
1096 /* Compute days of next month */
1098 for (; row <= 5; row++)
1100 for (; col <= 6; col++)
1102 priv->day[row][col] = day;
1103 priv->day_month[row][col] = MONTH_NEXT;
1111 calendar_select_and_focus_day (GtkCalendar *calendar,
1114 GtkCalendarPrivate *priv = calendar->priv;
1115 gint old_focus_row = priv->focus_row;
1116 gint old_focus_col = priv->focus_col;
1120 for (row = 0; row < 6; row ++)
1121 for (col = 0; col < 7; col++)
1123 if (priv->day_month[row][col] == MONTH_CURRENT
1124 && priv->day[row][col] == day)
1126 priv->focus_row = row;
1127 priv->focus_col = col;
1131 if (old_focus_row != -1 && old_focus_col != -1)
1132 calendar_invalidate_day (calendar, old_focus_row, old_focus_col);
1134 gtk_calendar_select_day (calendar, day);
1138 /****************************************
1139 * Layout computation utilities *
1140 ****************************************/
1143 calendar_row_height (GtkCalendar *calendar)
1145 GtkCalendarPrivate *priv = calendar->priv;
1147 return (GTK_CALENDAR_GET_PRIVATE (calendar)->main_h - CALENDAR_MARGIN
1148 - ((priv->display_flags & GTK_CALENDAR_SHOW_DAY_NAMES)
1149 ? calendar_get_ysep (calendar) : CALENDAR_MARGIN)) / 6;
1153 /* calendar_left_x_for_column: returns the x coordinate
1154 * for the left of the column */
1156 calendar_left_x_for_column (GtkCalendar *calendar,
1159 GtkCalendarPrivate *priv = calendar->priv;
1163 gint calendar_xsep = calendar_get_xsep (calendar);
1164 GtkStyleContext *context;
1165 GtkStateFlags state;
1166 gint inner_border = calendar_get_inner_border (calendar);
1169 context = gtk_widget_get_style_context (GTK_WIDGET (calendar));
1170 state = gtk_widget_get_state_flags (GTK_WIDGET (calendar));
1171 gtk_style_context_get_padding (context, state, &padding);
1173 week_width = priv->week_width + padding.left + inner_border;
1175 if (gtk_widget_get_direction (GTK_WIDGET (calendar)) == GTK_TEXT_DIR_RTL)
1177 column = 6 - column;
1181 width = GTK_CALENDAR_GET_PRIVATE (calendar)->day_width;
1182 if (priv->display_flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
1183 x_left = week_width + calendar_xsep + (width + DAY_XSEP) * column;
1185 x_left = week_width + CALENDAR_MARGIN + (width + DAY_XSEP) * column;
1190 /* column_from_x: returns the column 0-6 that the
1191 * x pixel of the xwindow is in */
1193 calendar_column_from_x (GtkCalendar *calendar,
1197 gint x_left, x_right;
1201 for (c = 0; c < 7; c++)
1203 x_left = calendar_left_x_for_column (calendar, c);
1204 x_right = x_left + GTK_CALENDAR_GET_PRIVATE (calendar)->day_width;
1206 if (event_x >= x_left && event_x < x_right)
1216 /* calendar_top_y_for_row: returns the y coordinate
1217 * for the top of the row */
1219 calendar_top_y_for_row (GtkCalendar *calendar,
1222 GtkStyleContext *context;
1223 GtkAllocation allocation;
1224 gint inner_border = calendar_get_inner_border (calendar);
1225 GtkStateFlags state;
1228 gtk_widget_get_allocation (GTK_WIDGET (calendar), &allocation);
1229 context = gtk_widget_get_style_context (GTK_WIDGET (calendar));
1230 state = gtk_widget_get_state_flags (GTK_WIDGET (calendar));
1232 gtk_style_context_get_padding (context, state, &padding);
1234 return allocation.height
1235 - padding.top - inner_border
1236 - (CALENDAR_MARGIN + (6 - row)
1237 * calendar_row_height (calendar));
1240 /* row_from_y: returns the row 0-5 that the
1241 * y pixel of the xwindow is in */
1243 calendar_row_from_y (GtkCalendar *calendar,
1248 gint y_top, y_bottom;
1250 height = calendar_row_height (calendar);
1253 for (r = 0; r < 6; r++)
1255 y_top = calendar_top_y_for_row (calendar, r);
1256 y_bottom = y_top + height;
1258 if (event_y >= y_top && event_y < y_bottom)
1269 calendar_arrow_rectangle (GtkCalendar *calendar,
1273 GtkWidget *widget = GTK_WIDGET (calendar);
1274 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1275 GtkAllocation allocation;
1276 GtkStyleContext *context;
1277 GtkStateFlags state;
1281 gtk_widget_get_allocation (widget, &allocation);
1282 context = gtk_widget_get_style_context (widget);
1283 state = gtk_widget_get_state_flags (widget);
1285 gtk_style_context_get_padding (context, state, &padding);
1287 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
1288 year_left = priv->year_before;
1290 year_left = !priv->year_before;
1293 rect->width = priv->arrow_width;
1294 rect->height = priv->header_h - 7;
1298 case ARROW_MONTH_LEFT:
1300 rect->x = (allocation.width - padding.left - padding.right
1301 - (3 + 2 * priv->arrow_width + priv->max_month_width));
1305 case ARROW_MONTH_RIGHT:
1307 rect->x = (allocation.width - padding.left - padding.right
1308 - 3 - priv->arrow_width);
1310 rect->x = (priv->arrow_width + priv->max_month_width);
1312 case ARROW_YEAR_LEFT:
1316 rect->x = (allocation.width - padding.left - padding.right
1317 - (3 + 2 * priv->arrow_width + priv->max_year_width));
1319 case ARROW_YEAR_RIGHT:
1321 rect->x = (priv->arrow_width + priv->max_year_width);
1323 rect->x = (allocation.width - padding.left - padding.right
1324 - 3 - priv->arrow_width);
1328 rect->x += padding.left;
1329 rect->y += padding.top;
1333 calendar_day_rectangle (GtkCalendar *calendar,
1338 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1340 rect->x = calendar_left_x_for_column (calendar, col);
1341 rect->y = calendar_top_y_for_row (calendar, row);
1342 rect->height = calendar_row_height (calendar);
1343 rect->width = priv->day_width;
1347 calendar_set_month_prev (GtkCalendar *calendar)
1349 GtkCalendarPrivate *priv = calendar->priv;
1352 if (priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
1355 if (priv->month == 0)
1363 month_len = month_length[leap (priv->year)][priv->month + 1];
1365 calendar_compute_days (calendar);
1367 g_signal_emit (calendar,
1368 gtk_calendar_signals[PREV_MONTH_SIGNAL],
1370 g_signal_emit (calendar,
1371 gtk_calendar_signals[MONTH_CHANGED_SIGNAL],
1374 if (month_len < priv->selected_day)
1376 priv->selected_day = 0;
1377 gtk_calendar_select_day (calendar, month_len);
1381 if (priv->selected_day < 0)
1382 priv->selected_day = priv->selected_day + 1 + month_length[leap (priv->year)][priv->month + 1];
1383 gtk_calendar_select_day (calendar, priv->selected_day);
1386 calendar_queue_refresh (calendar);
1390 /****************************************
1391 * Basic object methods *
1392 ****************************************/
1395 gtk_calendar_finalize (GObject *object)
1397 G_OBJECT_CLASS (gtk_calendar_parent_class)->finalize (object);
1401 gtk_calendar_destroy (GtkWidget *widget)
1403 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
1405 calendar_stop_spinning (GTK_CALENDAR (widget));
1407 /* Call the destroy function for the extra display callback: */
1408 if (priv->detail_func_destroy && priv->detail_func_user_data)
1410 priv->detail_func_destroy (priv->detail_func_user_data);
1411 priv->detail_func_user_data = NULL;
1412 priv->detail_func_destroy = NULL;
1415 GTK_WIDGET_CLASS (gtk_calendar_parent_class)->destroy (widget);
1420 calendar_set_display_option (GtkCalendar *calendar,
1421 GtkCalendarDisplayOptions flag,
1424 GtkCalendarPrivate *priv = calendar->priv;
1425 GtkCalendarDisplayOptions flags;
1428 flags = priv->display_flags | flag;
1430 flags = priv->display_flags & ~flag;
1431 gtk_calendar_set_display_options (calendar, flags);
1435 calendar_get_display_option (GtkCalendar *calendar,
1436 GtkCalendarDisplayOptions flag)
1438 GtkCalendarPrivate *priv = calendar->priv;
1440 return (priv->display_flags & flag) != 0;
1444 gtk_calendar_set_property (GObject *object,
1446 const GValue *value,
1449 GtkCalendar *calendar = GTK_CALENDAR (object);
1450 GtkCalendarPrivate *priv = calendar->priv;
1455 gtk_calendar_select_month (calendar,
1457 g_value_get_int (value));
1460 gtk_calendar_select_month (calendar,
1461 g_value_get_int (value),
1465 gtk_calendar_select_day (calendar,
1466 g_value_get_int (value));
1468 case PROP_SHOW_HEADING:
1469 calendar_set_display_option (calendar,
1470 GTK_CALENDAR_SHOW_HEADING,
1471 g_value_get_boolean (value));
1473 case PROP_SHOW_DAY_NAMES:
1474 calendar_set_display_option (calendar,
1475 GTK_CALENDAR_SHOW_DAY_NAMES,
1476 g_value_get_boolean (value));
1478 case PROP_NO_MONTH_CHANGE:
1479 calendar_set_display_option (calendar,
1480 GTK_CALENDAR_NO_MONTH_CHANGE,
1481 g_value_get_boolean (value));
1483 case PROP_SHOW_WEEK_NUMBERS:
1484 calendar_set_display_option (calendar,
1485 GTK_CALENDAR_SHOW_WEEK_NUMBERS,
1486 g_value_get_boolean (value));
1488 case PROP_SHOW_DETAILS:
1489 calendar_set_display_option (calendar,
1490 GTK_CALENDAR_SHOW_DETAILS,
1491 g_value_get_boolean (value));
1493 case PROP_DETAIL_WIDTH_CHARS:
1494 gtk_calendar_set_detail_width_chars (calendar,
1495 g_value_get_int (value));
1497 case PROP_DETAIL_HEIGHT_ROWS:
1498 gtk_calendar_set_detail_height_rows (calendar,
1499 g_value_get_int (value));
1502 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1508 gtk_calendar_get_property (GObject *object,
1513 GtkCalendar *calendar = GTK_CALENDAR (object);
1514 GtkCalendarPrivate *priv = calendar->priv;
1519 g_value_set_int (value, priv->year);
1522 g_value_set_int (value, priv->month);
1525 g_value_set_int (value, priv->selected_day);
1527 case PROP_SHOW_HEADING:
1528 g_value_set_boolean (value, calendar_get_display_option (calendar,
1529 GTK_CALENDAR_SHOW_HEADING));
1531 case PROP_SHOW_DAY_NAMES:
1532 g_value_set_boolean (value, calendar_get_display_option (calendar,
1533 GTK_CALENDAR_SHOW_DAY_NAMES));
1535 case PROP_NO_MONTH_CHANGE:
1536 g_value_set_boolean (value, calendar_get_display_option (calendar,
1537 GTK_CALENDAR_NO_MONTH_CHANGE));
1539 case PROP_SHOW_WEEK_NUMBERS:
1540 g_value_set_boolean (value, calendar_get_display_option (calendar,
1541 GTK_CALENDAR_SHOW_WEEK_NUMBERS));
1543 case PROP_SHOW_DETAILS:
1544 g_value_set_boolean (value, calendar_get_display_option (calendar,
1545 GTK_CALENDAR_SHOW_DETAILS));
1547 case PROP_DETAIL_WIDTH_CHARS:
1548 g_value_set_int (value, priv->detail_width_chars);
1550 case PROP_DETAIL_HEIGHT_ROWS:
1551 g_value_set_int (value, priv->detail_height_rows);
1554 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1560 /****************************************
1562 ****************************************/
1565 calendar_realize_arrows (GtkCalendar *calendar)
1567 GtkWidget *widget = GTK_WIDGET (calendar);
1568 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1569 GdkWindowAttr attributes;
1570 gint attributes_mask;
1572 GtkAllocation allocation;
1574 if (! (priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
1575 && (priv->display_flags & GTK_CALENDAR_SHOW_HEADING))
1577 gtk_widget_get_allocation (widget, &allocation);
1579 attributes.wclass = GDK_INPUT_ONLY;
1580 attributes.window_type = GDK_WINDOW_CHILD;
1581 attributes.event_mask = (gtk_widget_get_events (widget)
1582 | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
1583 | GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK);
1584 attributes_mask = GDK_WA_X | GDK_WA_Y;
1585 for (i = 0; i < 4; i++)
1588 calendar_arrow_rectangle (calendar, i, &rect);
1590 attributes.x = allocation.x + rect.x;
1591 attributes.y = allocation.y + rect.y;
1592 attributes.width = rect.width;
1593 attributes.height = rect.height;
1594 priv->arrow_win[i] = gdk_window_new (gtk_widget_get_window (widget),
1598 if (!gtk_widget_is_sensitive (widget))
1599 priv->arrow_state[i] = GTK_STATE_FLAG_INSENSITIVE;
1601 gdk_window_set_user_data (priv->arrow_win[i], widget);
1606 for (i = 0; i < 4; i++)
1607 priv->arrow_win[i] = NULL;
1612 calendar_unrealize_arrows (GtkCalendar *calendar)
1614 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1617 for (i = 0; i < 4; i++)
1619 if (priv->arrow_win[i])
1621 gdk_window_set_user_data (priv->arrow_win[i], NULL);
1622 gdk_window_destroy (priv->arrow_win[i]);
1623 priv->arrow_win[i] = NULL;
1630 calendar_get_inner_border (GtkCalendar *calendar)
1634 gtk_widget_style_get (GTK_WIDGET (calendar),
1635 "inner-border", &inner_border,
1638 return inner_border;
1642 calendar_get_xsep (GtkCalendar *calendar)
1646 gtk_widget_style_get (GTK_WIDGET (calendar),
1647 "horizontal-separation", &xsep,
1654 calendar_get_ysep (GtkCalendar *calendar)
1658 gtk_widget_style_get (GTK_WIDGET (calendar),
1659 "vertical-separation", &ysep,
1666 gtk_calendar_realize (GtkWidget *widget)
1668 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
1669 GdkWindowAttr attributes;
1670 gint attributes_mask;
1671 gint inner_border = calendar_get_inner_border (GTK_CALENDAR (widget));
1672 GtkStyleContext *context;
1673 GtkAllocation allocation;
1674 GtkStateFlags state = 0;
1677 context = gtk_widget_get_style_context (widget);
1678 state = gtk_widget_get_state_flags (widget);
1679 gtk_style_context_get_padding (context, state, &padding);
1681 gtk_widget_get_allocation (widget, &allocation);
1683 GTK_WIDGET_CLASS (gtk_calendar_parent_class)->realize (widget);
1685 attributes.wclass = GDK_INPUT_ONLY;
1686 attributes.window_type = GDK_WINDOW_CHILD;
1687 attributes.event_mask = (gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK
1688 | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
1689 | GDK_POINTER_MOTION_MASK | GDK_LEAVE_NOTIFY_MASK);
1691 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
1692 attributes.x = priv->week_width + padding.left + inner_border;
1694 attributes.x = padding.left + inner_border;
1696 attributes.y = priv->header_h + priv->day_name_h + padding.top + inner_border;
1697 attributes.width = allocation.width - attributes.x - (padding.right + inner_border);
1699 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1700 attributes.width -= priv->week_width;
1702 attributes.height = priv->main_h;
1703 attributes_mask = GDK_WA_X | GDK_WA_Y;
1705 attributes.x += allocation.x;
1706 attributes.y += allocation.y;
1708 priv->main_win = gdk_window_new (gtk_widget_get_window (widget),
1709 &attributes, attributes_mask);
1710 gdk_window_set_user_data (priv->main_win, widget);
1712 calendar_realize_arrows (GTK_CALENDAR (widget));
1716 gtk_calendar_unrealize (GtkWidget *widget)
1718 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
1720 calendar_unrealize_arrows (GTK_CALENDAR (widget));
1724 gdk_window_set_user_data (priv->main_win, NULL);
1725 gdk_window_destroy (priv->main_win);
1726 priv->main_win = NULL;
1729 GTK_WIDGET_CLASS (gtk_calendar_parent_class)->unrealize (widget);
1733 calendar_map_arrows (GtkCalendar *calendar)
1735 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1738 for (i = 0; i < 4; i++)
1740 if (priv->arrow_win[i])
1741 gdk_window_show (priv->arrow_win[i]);
1746 calendar_unmap_arrows (GtkCalendar *calendar)
1748 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1751 for (i = 0; i < 4; i++)
1753 if (priv->arrow_win[i])
1754 gdk_window_hide (priv->arrow_win[i]);
1759 gtk_calendar_map (GtkWidget *widget)
1761 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
1763 GTK_WIDGET_CLASS (gtk_calendar_parent_class)->map (widget);
1765 gdk_window_show (priv->main_win);
1767 calendar_map_arrows (GTK_CALENDAR (widget));
1771 gtk_calendar_unmap (GtkWidget *widget)
1773 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
1775 calendar_unmap_arrows (GTK_CALENDAR (widget));
1777 gdk_window_hide (priv->main_win);
1779 GTK_WIDGET_CLASS (gtk_calendar_parent_class)->unmap (widget);
1783 gtk_calendar_get_detail (GtkCalendar *calendar,
1787 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1790 if (priv->detail_func == NULL)
1794 month = priv->month + priv->day_month[row][column] - MONTH_CURRENT;
1801 else if (month > 11)
1807 return priv->detail_func (calendar,
1809 priv->day[row][column],
1810 priv->detail_func_user_data);
1814 gtk_calendar_query_tooltip (GtkWidget *widget,
1817 gboolean keyboard_mode,
1818 GtkTooltip *tooltip)
1820 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
1821 GtkCalendar *calendar = GTK_CALENDAR (widget);
1822 gchar *detail = NULL;
1823 GdkRectangle day_rect;
1826 col = calendar_column_from_x (calendar, x);
1827 row = calendar_row_from_y (calendar, y);
1829 if (col != -1 && row != -1 &&
1830 (0 != (priv->detail_overflow[row] & (1 << col)) ||
1831 0 == (priv->display_flags & GTK_CALENDAR_SHOW_DETAILS)))
1833 detail = gtk_calendar_get_detail (calendar, row, col);
1834 calendar_day_rectangle (calendar, row, col, &day_rect);
1839 gtk_tooltip_set_tip_area (tooltip, &day_rect);
1840 gtk_tooltip_set_markup (tooltip, detail);
1847 if (GTK_WIDGET_CLASS (gtk_calendar_parent_class)->query_tooltip)
1848 return GTK_WIDGET_CLASS (gtk_calendar_parent_class)->query_tooltip (widget, x, y, keyboard_mode, tooltip);
1854 /****************************************
1855 * Size Request and Allocate *
1856 ****************************************/
1859 gtk_calendar_size_request (GtkWidget *widget,
1860 GtkRequisition *requisition)
1862 GtkCalendar *calendar = GTK_CALENDAR (widget);
1863 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
1864 GtkStyleContext *context;
1865 GtkStateFlags state;
1867 PangoLayout *layout;
1868 PangoRectangle logical_rect;
1872 gint calendar_margin = CALENDAR_MARGIN;
1873 gint header_width, main_width;
1874 gint max_header_height = 0;
1877 gint max_detail_height;
1878 gint inner_border = calendar_get_inner_border (calendar);
1879 gint calendar_ysep = calendar_get_ysep (calendar);
1880 gint calendar_xsep = calendar_get_xsep (calendar);
1882 gtk_widget_style_get (GTK_WIDGET (widget),
1883 "focus-line-width", &focus_width,
1884 "focus-padding", &focus_padding,
1887 layout = gtk_widget_create_pango_layout (widget, NULL);
1890 * Calculate the requisition width for the widget.
1895 if (priv->display_flags & GTK_CALENDAR_SHOW_HEADING)
1897 priv->max_month_width = 0;
1898 for (i = 0; i < 12; i++)
1900 pango_layout_set_text (layout, default_monthname[i], -1);
1901 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1902 priv->max_month_width = MAX (priv->max_month_width,
1903 logical_rect.width + 8);
1904 max_header_height = MAX (max_header_height, logical_rect.height);
1907 priv->max_year_width = 0;
1908 /* Translators: This is a text measurement template.
1909 * Translate it to the widest year text
1911 * If you don't understand this, leave it as "2000"
1913 pango_layout_set_text (layout, C_("year measurement template", "2000"), -1);
1914 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1915 priv->max_year_width = MAX (priv->max_year_width,
1916 logical_rect.width + 8);
1917 max_header_height = MAX (max_header_height, logical_rect.height);
1921 priv->max_month_width = 0;
1922 priv->max_year_width = 0;
1925 if (priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
1926 header_width = (priv->max_month_width
1927 + priv->max_year_width
1930 header_width = (priv->max_month_width
1931 + priv->max_year_width
1932 + 4 * priv->arrow_width + 3 * 3);
1934 /* Mainwindow labels width */
1936 priv->max_day_char_width = 0;
1937 priv->max_day_char_ascent = 0;
1938 priv->max_day_char_descent = 0;
1939 priv->min_day_width = 0;
1941 for (i = 0; i < 9; i++)
1944 g_snprintf (buffer, sizeof (buffer), C_("calendar:day:digits", "%d"), i * 11);
1945 pango_layout_set_text (layout, buffer, -1);
1946 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1947 priv->min_day_width = MAX (priv->min_day_width,
1948 logical_rect.width);
1950 priv->max_day_char_ascent = MAX (priv->max_day_char_ascent,
1951 PANGO_ASCENT (logical_rect));
1952 priv->max_day_char_descent = MAX (priv->max_day_char_descent,
1953 PANGO_DESCENT (logical_rect));
1956 priv->max_label_char_ascent = 0;
1957 priv->max_label_char_descent = 0;
1958 if (priv->display_flags & GTK_CALENDAR_SHOW_DAY_NAMES)
1959 for (i = 0; i < 7; i++)
1961 pango_layout_set_text (layout, default_abbreviated_dayname[i], -1);
1962 pango_layout_line_get_pixel_extents (pango_layout_get_lines_readonly (layout)->data, NULL, &logical_rect);
1964 priv->min_day_width = MAX (priv->min_day_width, logical_rect.width);
1965 priv->max_label_char_ascent = MAX (priv->max_label_char_ascent,
1966 PANGO_ASCENT (logical_rect));
1967 priv->max_label_char_descent = MAX (priv->max_label_char_descent,
1968 PANGO_DESCENT (logical_rect));
1971 priv->max_week_char_width = 0;
1972 if (priv->display_flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
1973 for (i = 0; i < 9; i++)
1976 g_snprintf (buffer, sizeof (buffer), C_("calendar:week:digits", "%d"), i * 11);
1977 pango_layout_set_text (layout, buffer, -1);
1978 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1979 priv->max_week_char_width = MAX (priv->max_week_char_width,
1980 logical_rect.width / 2);
1983 /* Calculate detail extents. Do this as late as possible since
1984 * pango_layout_set_markup is called which alters font settings. */
1985 max_detail_height = 0;
1987 if (priv->detail_func && (priv->display_flags & GTK_CALENDAR_SHOW_DETAILS))
1989 gchar *markup, *tail;
1991 if (priv->detail_width_chars || priv->detail_height_rows)
1993 gint rows = MAX (1, priv->detail_height_rows) - 1;
1994 gsize len = priv->detail_width_chars + rows + 16;
1996 markup = tail = g_alloca (len);
1998 memcpy (tail, "<small>", 7);
2001 memset (tail, 'm', priv->detail_width_chars);
2002 tail += priv->detail_width_chars;
2004 memset (tail, '\n', rows);
2007 memcpy (tail, "</small>", 9);
2010 g_assert (len == (tail - markup));
2012 pango_layout_set_markup (layout, markup, -1);
2013 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2015 if (priv->detail_width_chars)
2016 priv->min_day_width = MAX (priv->min_day_width, logical_rect.width);
2017 if (priv->detail_height_rows)
2018 max_detail_height = MAX (max_detail_height, logical_rect.height);
2021 if (!priv->detail_width_chars || !priv->detail_height_rows)
2022 for (r = 0; r < 6; r++)
2023 for (c = 0; c < 7; c++)
2025 gchar *detail = gtk_calendar_get_detail (calendar, r, c);
2029 markup = g_strconcat ("<small>", detail, "</small>", NULL);
2030 pango_layout_set_markup (layout, markup, -1);
2032 if (priv->detail_width_chars)
2034 pango_layout_set_wrap (layout, PANGO_WRAP_WORD_CHAR);
2035 pango_layout_set_width (layout, PANGO_SCALE * priv->min_day_width);
2038 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2040 if (!priv->detail_width_chars)
2041 priv->min_day_width = MAX (priv->min_day_width, logical_rect.width);
2042 if (!priv->detail_height_rows)
2043 max_detail_height = MAX (max_detail_height, logical_rect.height);
2051 /* We add one to max_day_char_width to be able to make the marked day "bold" */
2052 priv->max_day_char_width = priv->min_day_width / 2 + 1;
2054 main_width = (7 * (priv->min_day_width + (focus_padding + focus_width) * 2) + (DAY_XSEP * 6) + CALENDAR_MARGIN * 2
2055 + (priv->max_week_char_width
2056 ? priv->max_week_char_width * 2 + (focus_padding + focus_width) * 2 + calendar_xsep * 2
2059 context = gtk_widget_get_style_context (widget);
2060 state = gtk_widget_get_state_flags (widget);
2061 gtk_style_context_get_padding (context, state, &padding);
2063 requisition->width = MAX (header_width, main_width + inner_border * 2) + padding.left + padding.right;
2066 * Calculate the requisition height for the widget.
2069 if (priv->display_flags & GTK_CALENDAR_SHOW_HEADING)
2071 priv->header_h = (max_header_height + calendar_ysep * 2);
2078 if (priv->display_flags & GTK_CALENDAR_SHOW_DAY_NAMES)
2080 priv->day_name_h = (priv->max_label_char_ascent
2081 + priv->max_label_char_descent
2082 + 2 * (focus_padding + focus_width) + calendar_margin);
2083 calendar_margin = calendar_ysep;
2087 priv->day_name_h = 0;
2090 priv->main_h = (CALENDAR_MARGIN + calendar_margin
2091 + 6 * (priv->max_day_char_ascent
2092 + priv->max_day_char_descent
2094 + 2 * (focus_padding + focus_width))
2097 height = priv->header_h + priv->day_name_h + priv->main_h;
2099 requisition->height = height + padding.top + padding.bottom + (inner_border * 2);
2101 g_object_unref (layout);
2105 gtk_calendar_get_preferred_width (GtkWidget *widget,
2109 GtkRequisition requisition;
2111 gtk_calendar_size_request (widget, &requisition);
2113 *minimum = *natural = requisition.width;
2117 gtk_calendar_get_preferred_height (GtkWidget *widget,
2121 GtkRequisition requisition;
2123 gtk_calendar_size_request (widget, &requisition);
2125 *minimum = *natural = requisition.height;
2129 gtk_calendar_size_allocate (GtkWidget *widget,
2130 GtkAllocation *allocation)
2132 GtkCalendar *calendar = GTK_CALENDAR (widget);
2133 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
2134 GtkStyleContext *context;
2135 GtkStateFlags state;
2138 gint inner_border = calendar_get_inner_border (calendar);
2139 gint calendar_xsep = calendar_get_xsep (calendar);
2141 context = gtk_widget_get_style_context (widget);
2142 state = gtk_widget_get_state_flags (widget);
2143 gtk_style_context_get_padding (context, state, &padding);
2145 gtk_widget_set_allocation (widget, allocation);
2147 if (priv->display_flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
2149 priv->day_width = (priv->min_day_width
2150 * ((allocation->width - (inner_border * 2) - padding.left - padding.right
2151 - (CALENDAR_MARGIN * 2) - (DAY_XSEP * 6) - calendar_xsep * 2))
2152 / (7 * priv->min_day_width + priv->max_week_char_width * 2));
2153 priv->week_width = ((allocation->width - (inner_border * 2) - padding.left - padding.right
2154 - (CALENDAR_MARGIN * 2) - (DAY_XSEP * 6) - calendar_xsep * 2 )
2155 - priv->day_width * 7 + CALENDAR_MARGIN + calendar_xsep);
2159 priv->day_width = (allocation->width
2160 - (inner_border * 2)
2161 - padding.left - padding.right
2162 - (CALENDAR_MARGIN * 2)
2163 - (DAY_XSEP * 6))/7;
2164 priv->week_width = 0;
2167 if (gtk_widget_get_realized (widget))
2169 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
2170 gdk_window_move_resize (priv->main_win,
2172 + priv->week_width + padding.left + inner_border,
2174 + priv->header_h + priv->day_name_h
2175 + (padding.top + inner_border),
2176 allocation->width - priv->week_width
2177 - (inner_border * 2) - padding.left - padding.right,
2180 gdk_window_move_resize (priv->main_win,
2182 + padding.left + inner_border,
2184 + priv->header_h + priv->day_name_h
2185 + padding.top + inner_border,
2186 allocation->width - priv->week_width
2187 - (inner_border * 2) - padding.left - padding.right,
2190 for (i = 0 ; i < 4 ; i++)
2192 if (priv->arrow_win[i])
2195 calendar_arrow_rectangle (calendar, i, &rect);
2197 gdk_window_move_resize (priv->arrow_win[i],
2198 allocation->x + rect.x,
2199 allocation->y + rect.y,
2200 rect.width, rect.height);
2207 /****************************************
2209 ****************************************/
2212 calendar_paint_header (GtkCalendar *calendar, cairo_t *cr)
2214 GtkWidget *widget = GTK_WIDGET (calendar);
2215 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2216 GtkAllocation allocation;
2217 GtkStyleContext *context;
2218 GtkStateFlags state;
2223 gint max_month_width;
2224 gint max_year_width;
2225 PangoLayout *layout;
2226 PangoRectangle logical_rect;
2232 context = gtk_widget_get_style_context (widget);
2233 state = gtk_widget_get_state_flags (widget);
2234 gtk_style_context_get_padding (context, state, &padding);
2237 cairo_translate (cr, padding.left, padding.top);
2239 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
2240 year_left = priv->year_before;
2242 year_left = !priv->year_before;
2244 gtk_widget_get_allocation (widget, &allocation);
2246 header_width = allocation.width - padding.left - padding.right;
2248 max_month_width = priv->max_month_width;
2249 max_year_width = priv->max_year_width;
2251 gtk_style_context_save (context);
2252 gtk_style_context_add_class (context, GTK_STYLE_CLASS_HEADER);
2254 gtk_render_background (context, cr, 0, 0, header_width, priv->header_h);
2255 gtk_render_frame (context, cr, 0, 0, header_width, priv->header_h);
2257 tmp_time = 1; /* Jan 1 1970, 00:00:01 UTC */
2258 tm = gmtime (&tmp_time);
2259 tm->tm_year = priv->year - 1900;
2261 /* Translators: This dictates how the year is displayed in
2262 * gtkcalendar widget. See strftime() manual for the format.
2263 * Use only ASCII in the translation.
2265 * Also look for the msgid "2000".
2266 * Translate that entry to a year with the widest output of this
2269 * "%Y" is appropriate for most locales.
2271 strftime (buffer, sizeof (buffer), C_("calendar year format", "%Y"), tm);
2272 str = g_locale_to_utf8 (buffer, -1, NULL, NULL, NULL);
2273 layout = gtk_widget_create_pango_layout (widget, str);
2276 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2279 y = (priv->header_h - logical_rect.height) / 2;
2281 /* Draw year and its arrows */
2283 if (priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
2285 x = 3 + (max_year_width - logical_rect.width)/2;
2287 x = header_width - (3 + max_year_width
2288 - (max_year_width - logical_rect.width)/2);
2291 x = 3 + priv->arrow_width + (max_year_width - logical_rect.width)/2;
2293 x = header_width - (3 + priv->arrow_width + max_year_width
2294 - (max_year_width - logical_rect.width)/2);
2296 gtk_render_layout (context, cr, x, y, layout);
2299 g_snprintf (buffer, sizeof (buffer), "%s", default_monthname[priv->month]);
2300 pango_layout_set_text (layout, buffer, -1);
2301 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2303 if (priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
2305 x = header_width - (3 + max_month_width
2306 - (max_month_width - logical_rect.width)/2);
2308 x = 3 + (max_month_width - logical_rect.width) / 2;
2311 x = header_width - (3 + priv->arrow_width + max_month_width
2312 - (max_month_width - logical_rect.width)/2);
2314 x = 3 + priv->arrow_width + (max_month_width - logical_rect.width)/2;
2316 gtk_render_layout (context, cr, x, y, layout);
2317 g_object_unref (layout);
2319 gtk_style_context_restore (context);
2324 calendar_paint_day_names (GtkCalendar *calendar,
2327 GtkWidget *widget = GTK_WIDGET (calendar);
2328 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2329 GtkStyleContext *context;
2330 GtkStateFlags state;
2332 GtkAllocation allocation;
2335 int day_width, cal_width;
2337 PangoLayout *layout;
2338 PangoRectangle logical_rect;
2341 gint calendar_ysep = calendar_get_ysep (calendar);
2342 gint calendar_xsep = calendar_get_xsep (calendar);
2343 gint inner_border = calendar_get_inner_border (calendar);
2345 context = gtk_widget_get_style_context (widget);
2346 state = gtk_widget_get_state_flags (widget);
2347 gtk_style_context_get_padding (context, state, &padding);
2351 cairo_translate (cr,
2352 padding.left + inner_border,
2353 priv->header_h + padding.top + inner_border);
2355 gtk_widget_style_get (GTK_WIDGET (widget),
2356 "focus-line-width", &focus_width,
2357 "focus-padding", &focus_padding,
2360 gtk_widget_get_allocation (widget, &allocation);
2362 day_width = priv->day_width;
2363 cal_width = allocation.width - (inner_border * 2) - padding.left - padding.right;
2364 day_wid_sep = day_width + DAY_XSEP;
2367 * Draw rectangles as inverted background for the labels.
2370 gtk_style_context_save (context);
2371 gtk_style_context_add_class (context, GTK_STYLE_CLASS_HIGHLIGHT);
2373 gtk_render_background (context, cr,
2374 CALENDAR_MARGIN, CALENDAR_MARGIN,
2375 cal_width - CALENDAR_MARGIN * 2,
2376 priv->day_name_h - CALENDAR_MARGIN);
2378 if (priv->display_flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
2379 gtk_render_background (context, cr,
2381 priv->day_name_h - calendar_ysep,
2382 priv->week_width - calendar_ysep - CALENDAR_MARGIN,
2388 layout = gtk_widget_create_pango_layout (widget, NULL);
2390 for (i = 0; i < 7; i++)
2392 if (gtk_widget_get_direction (GTK_WIDGET (calendar)) == GTK_TEXT_DIR_RTL)
2396 day = (day + priv->week_start) % 7;
2397 g_snprintf (buffer, sizeof (buffer), "%s", default_abbreviated_dayname[day]);
2399 pango_layout_set_text (layout, buffer, -1);
2400 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2402 gtk_render_layout (context, cr,
2404 + (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR ?
2405 (priv->week_width + (priv->week_width ? calendar_xsep : 0))
2408 + (day_width - logical_rect.width)/2),
2409 CALENDAR_MARGIN + focus_width + focus_padding + logical_rect.y,
2413 g_object_unref (layout);
2415 gtk_style_context_restore (context);
2420 calendar_paint_week_numbers (GtkCalendar *calendar,
2423 GtkWidget *widget = GTK_WIDGET (calendar);
2424 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2425 GtkStyleContext *context;
2426 GtkStateFlags state;
2428 guint week = 0, year;
2429 gint row, x_loc, y_loc;
2432 PangoLayout *layout;
2433 PangoRectangle logical_rect;
2436 gint calendar_xsep = calendar_get_xsep (calendar);
2437 gint inner_border = calendar_get_inner_border (calendar);
2440 context = gtk_widget_get_style_context (widget);
2441 state = gtk_widget_get_state_flags (widget);
2442 gtk_style_context_get_padding (context, state, &padding);
2446 y = priv->header_h + priv->day_name_h + (padding.top + inner_border);
2447 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
2448 x = padding.left + inner_border;
2450 x = gtk_widget_get_allocated_width (widget) - priv->week_width - (padding.right + inner_border);
2452 gtk_widget_style_get (GTK_WIDGET (widget),
2453 "focus-line-width", &focus_width,
2454 "focus-padding", &focus_padding,
2457 gtk_style_context_save (context);
2458 gtk_style_context_add_class (context, GTK_STYLE_CLASS_HIGHLIGHT);
2460 if (priv->display_flags & GTK_CALENDAR_SHOW_DAY_NAMES)
2461 gtk_render_background (context, cr,
2462 x + CALENDAR_MARGIN, y,
2463 priv->week_width - CALENDAR_MARGIN,
2464 priv->main_h - CALENDAR_MARGIN);
2466 gtk_render_background (context, cr,
2467 x + CALENDAR_MARGIN,
2468 y + CALENDAR_MARGIN,
2469 priv->week_width - CALENDAR_MARGIN,
2470 priv->main_h - 2 * CALENDAR_MARGIN);
2476 layout = gtk_widget_create_pango_layout (widget, NULL);
2477 day_height = calendar_row_height (calendar);
2479 for (row = 0; row < 6; row++)
2484 if (priv->day[row][6] < 15 && row > 3 && priv->month == 11)
2487 result = week_of_year (&week, &year,
2488 ((priv->day[row][6] < 15 && row > 3 ? 1 : 0)
2489 + priv->month) % 12 + 1, priv->day[row][6]);
2490 g_return_if_fail (result);
2492 /* Translators: this defines whether the week numbers should use
2493 * localized digits or the ones used in English (0123...).
2495 * Translate to "%Id" if you want to use localized digits, or
2496 * translate to "%d" otherwise.
2498 * Note that translating this doesn't guarantee that you get localized
2499 * digits. That needs support from your system and locale definition
2502 g_snprintf (buffer, sizeof (buffer), C_("calendar:week:digits", "%d"), week);
2503 pango_layout_set_text (layout, buffer, -1);
2504 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2506 y_loc = calendar_top_y_for_row (calendar, row) + (day_height - logical_rect.height) / 2;
2508 x_loc = x + (priv->week_width
2509 - logical_rect.width
2510 - calendar_xsep - focus_padding - focus_width);
2512 gtk_render_layout (context, cr, x_loc, y_loc, layout);
2515 g_object_unref (layout);
2517 gtk_style_context_restore (context);
2522 calendar_invalidate_day_num (GtkCalendar *calendar,
2525 GtkCalendarPrivate *priv = calendar->priv;
2526 gint r, c, row, col;
2530 for (r = 0; r < 6; r++)
2531 for (c = 0; c < 7; c++)
2532 if (priv->day_month[r][c] == MONTH_CURRENT &&
2533 priv->day[r][c] == day)
2539 g_return_if_fail (row != -1);
2540 g_return_if_fail (col != -1);
2542 calendar_invalidate_day (calendar, row, col);
2546 calendar_invalidate_day (GtkCalendar *calendar,
2550 GdkRectangle day_rect;
2551 GtkAllocation allocation;
2553 gtk_widget_get_allocation (GTK_WIDGET (calendar), &allocation);
2554 calendar_day_rectangle (calendar, row, col, &day_rect);
2555 gtk_widget_queue_draw_area (GTK_WIDGET (calendar),
2556 allocation.x + day_rect.x,
2557 allocation.y + day_rect.y,
2558 day_rect.width, day_rect.height);
2562 is_color_attribute (PangoAttribute *attribute,
2565 return (attribute->klass->type == PANGO_ATTR_FOREGROUND ||
2566 attribute->klass->type == PANGO_ATTR_BACKGROUND);
2570 calendar_paint_day (GtkCalendar *calendar,
2575 GtkWidget *widget = GTK_WIDGET (calendar);
2576 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2577 GtkStyleContext *context;
2578 GtkStateFlags state = 0;
2583 GdkRectangle day_rect;
2585 PangoLayout *layout;
2586 PangoRectangle logical_rect;
2587 gboolean overflow = FALSE;
2588 gboolean show_details;
2590 g_return_if_fail (row < 6);
2591 g_return_if_fail (col < 7);
2593 context = gtk_widget_get_style_context (widget);
2595 day = priv->day[row][col];
2596 show_details = (priv->display_flags & GTK_CALENDAR_SHOW_DETAILS);
2598 calendar_day_rectangle (calendar, row, col, &day_rect);
2600 gtk_style_context_save (context);
2602 if (!gtk_widget_get_sensitive (widget))
2603 state |= GTK_STATE_FLAG_INSENSITIVE;
2606 if (gtk_widget_has_focus (widget))
2607 state |= GTK_STATE_FLAG_FOCUSED;
2609 if (priv->day_month[row][col] == MONTH_PREV ||
2610 priv->day_month[row][col] == MONTH_NEXT)
2611 state |= GTK_STATE_FLAG_INCONSISTENT;
2614 if (priv->marked_date[day-1])
2615 state |= GTK_STATE_FLAG_ACTIVE;
2617 if (priv->selected_day == day)
2619 state |= GTK_STATE_FLAG_SELECTED;
2621 gtk_style_context_set_state (context, state);
2622 gtk_render_background (context, cr,
2623 day_rect.x, day_rect.y,
2624 day_rect.width, day_rect.height);
2629 gtk_style_context_set_state (context, state);
2631 /* Translators: this defines whether the day numbers should use
2632 * localized digits or the ones used in English (0123...).
2634 * Translate to "%Id" if you want to use localized digits, or
2635 * translate to "%d" otherwise.
2637 * Note that translating this doesn't guarantee that you get localized
2638 * digits. That needs support from your system and locale definition
2641 g_snprintf (buffer, sizeof (buffer), C_("calendar:day:digits", "%d"), day);
2643 /* Get extra information to show, if any: */
2645 detail = gtk_calendar_get_detail (calendar, row, col);
2647 layout = gtk_widget_create_pango_layout (widget, buffer);
2648 pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
2649 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2651 x_loc = day_rect.x + (day_rect.width - logical_rect.width) / 2;
2654 gtk_render_layout (context, cr, x_loc, y_loc, layout);
2656 if (priv->day_month[row][col] == MONTH_CURRENT &&
2657 (priv->marked_date[day-1] || (detail && !show_details)))
2658 gtk_render_layout (context, cr, x_loc - 1, y_loc, layout);
2660 y_loc += priv->max_day_char_descent;
2662 if (priv->detail_func && show_details)
2668 gtk_style_context_get_color (context, state, &color);
2669 gdk_cairo_set_source_rgba (cr, &color);
2671 cairo_set_line_width (cr, 1);
2672 cairo_move_to (cr, day_rect.x + 2, y_loc + 0.5);
2673 cairo_line_to (cr, day_rect.x + day_rect.width - 2, y_loc + 0.5);
2681 if (detail && show_details)
2683 gchar *markup = g_strconcat ("<small>", detail, "</small>", NULL);
2684 pango_layout_set_markup (layout, markup, -1);
2687 if (day == priv->selected_day)
2689 /* Stripping colors as they conflict with selection marking. */
2691 PangoAttrList *attrs = pango_layout_get_attributes (layout);
2692 PangoAttrList *colors = NULL;
2695 colors = pango_attr_list_filter (attrs, is_color_attribute, NULL);
2697 pango_attr_list_unref (colors);
2700 pango_layout_set_wrap (layout, PANGO_WRAP_WORD_CHAR);
2701 pango_layout_set_width (layout, PANGO_SCALE * day_rect.width);
2703 if (priv->detail_height_rows)
2705 gint dy = day_rect.height - (y_loc - day_rect.y);
2706 pango_layout_set_height (layout, PANGO_SCALE * dy);
2707 pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_END);
2710 cairo_move_to (cr, day_rect.x, y_loc);
2711 pango_cairo_show_layout (cr, layout);
2714 if (gtk_widget_has_focus (widget)
2715 && priv->focus_row == row && priv->focus_col == col)
2716 gtk_render_focus (context, cr,
2717 day_rect.x, day_rect.y,
2718 day_rect.width, day_rect.height);
2721 priv->detail_overflow[row] |= (1 << col);
2723 priv->detail_overflow[row] &= ~(1 << col);
2725 gtk_style_context_restore (context);
2726 g_object_unref (layout);
2731 calendar_paint_main (GtkCalendar *calendar,
2738 for (col = 0; col < 7; col++)
2739 for (row = 0; row < 6; row++)
2740 calendar_paint_day (calendar, cr, row, col);
2746 calendar_invalidate_arrow (GtkCalendar *calendar,
2749 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2751 if (priv->display_flags & GTK_CALENDAR_SHOW_HEADING &&
2752 priv->arrow_win[arrow])
2755 GtkAllocation allocation;
2757 calendar_arrow_rectangle (calendar, arrow, &rect);
2758 gtk_widget_get_allocation (GTK_WIDGET (calendar), &allocation);
2759 gtk_widget_queue_draw_area (GTK_WIDGET (calendar),
2760 allocation.x + rect.x,
2761 allocation.y + rect.y,
2762 rect.width, rect.height);
2767 calendar_paint_arrow (GtkCalendar *calendar,
2771 GtkWidget *widget = GTK_WIDGET (calendar);
2772 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
2773 GtkStyleContext *context;
2774 GtkStateFlags state;
2778 if (!priv->arrow_win[arrow])
2781 calendar_arrow_rectangle (calendar, arrow, &rect);
2785 context = gtk_widget_get_style_context (widget);
2786 state = priv->arrow_state[arrow];
2788 gtk_style_context_save (context);
2789 gtk_style_context_set_state (context, state);
2790 gtk_style_context_add_class (context, GTK_STYLE_CLASS_BUTTON);
2792 gtk_render_background (context, cr,
2794 rect.width, rect.height);
2796 if (arrow == ARROW_MONTH_LEFT || arrow == ARROW_YEAR_LEFT)
2797 angle = 3 * (G_PI / 2);
2801 gtk_render_arrow (context, cr, angle,
2802 rect.x + (rect.width - 8) / 2,
2803 rect.y + (rect.height - 8) / 2,
2806 gtk_style_context_restore (context);
2811 gtk_calendar_draw (GtkWidget *widget,
2814 GtkCalendar *calendar = GTK_CALENDAR (widget);
2815 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
2818 if (gtk_cairo_should_draw_window (cr, gtk_widget_get_window (widget)))
2820 GtkStyleContext *context;
2822 context = gtk_widget_get_style_context (widget);
2824 gtk_style_context_save (context);
2825 gtk_style_context_add_class (context, GTK_STYLE_CLASS_VIEW);
2827 gtk_render_background (context, cr, 0, 0,
2828 gtk_widget_get_allocated_width (widget),
2829 gtk_widget_get_allocated_height (widget));
2830 gtk_render_frame (context, cr, 0, 0,
2831 gtk_widget_get_allocated_width (widget),
2832 gtk_widget_get_allocated_height (widget));
2834 gtk_style_context_restore (context);
2837 calendar_paint_main (calendar, cr);
2839 if (priv->display_flags & GTK_CALENDAR_SHOW_HEADING)
2841 calendar_paint_header (calendar, cr);
2842 for (i = 0; i < 4; i++)
2843 calendar_paint_arrow (calendar, cr, i);
2846 if (priv->display_flags & GTK_CALENDAR_SHOW_DAY_NAMES)
2847 calendar_paint_day_names (calendar, cr);
2849 if (priv->display_flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
2850 calendar_paint_week_numbers (calendar, cr);
2856 /****************************************
2858 ****************************************/
2861 calendar_arrow_action (GtkCalendar *calendar,
2866 case ARROW_YEAR_LEFT:
2867 calendar_set_year_prev (calendar);
2869 case ARROW_YEAR_RIGHT:
2870 calendar_set_year_next (calendar);
2872 case ARROW_MONTH_LEFT:
2873 calendar_set_month_prev (calendar);
2875 case ARROW_MONTH_RIGHT:
2876 calendar_set_month_next (calendar);
2884 calendar_timer (gpointer data)
2886 GtkCalendar *calendar = data;
2887 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2888 gboolean retval = FALSE;
2892 calendar_arrow_action (calendar, priv->click_child);
2894 if (priv->need_timer)
2896 GtkSettings *settings;
2899 settings = gtk_widget_get_settings (GTK_WIDGET (calendar));
2900 g_object_get (settings, "gtk-timeout-repeat", &timeout, NULL);
2902 priv->need_timer = FALSE;
2903 priv->timer = gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT_IDLE,
2904 timeout * SCROLL_DELAY_FACTOR,
2905 (GSourceFunc) calendar_timer,
2906 (gpointer) calendar, NULL);
2916 calendar_start_spinning (GtkCalendar *calendar,
2919 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2921 priv->click_child = click_child;
2925 GtkSettings *settings;
2928 settings = gtk_widget_get_settings (GTK_WIDGET (calendar));
2929 g_object_get (settings, "gtk-timeout-initial", &timeout, NULL);
2931 priv->need_timer = TRUE;
2932 priv->timer = gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT_IDLE,
2934 (GSourceFunc) calendar_timer,
2935 (gpointer) calendar, NULL);
2940 calendar_stop_spinning (GtkCalendar *calendar)
2942 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2946 g_source_remove (priv->timer);
2948 priv->need_timer = FALSE;
2953 calendar_main_button_press (GtkCalendar *calendar,
2954 GdkEventButton *event)
2956 GtkWidget *widget = GTK_WIDGET (calendar);
2957 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2963 GtkAllocation allocation;
2965 x = (gint) (event->x);
2966 y = (gint) (event->y);
2968 gdk_window_get_position (priv->main_win, &win_x, &win_y);
2969 gtk_widget_get_allocation (widget, &allocation);
2971 row = calendar_row_from_y (calendar, y + win_y - allocation.y);
2972 col = calendar_column_from_x (calendar, x + win_x - allocation.x);
2974 /* If row or column isn't found, just return. */
2975 if (row == -1 || col == -1)
2978 day_month = priv->day_month[row][col];
2980 if (event->type == GDK_BUTTON_PRESS)
2982 day = priv->day[row][col];
2984 if (day_month == MONTH_PREV)
2985 calendar_set_month_prev (calendar);
2986 else if (day_month == MONTH_NEXT)
2987 calendar_set_month_next (calendar);
2989 if (!gtk_widget_has_focus (widget))
2990 gtk_widget_grab_focus (widget);
2992 if (event->button == 1)
2995 priv->drag_start_x = x;
2996 priv->drag_start_y = y;
2999 calendar_select_and_focus_day (calendar, day);
3001 else if (event->type == GDK_2BUTTON_PRESS)
3004 if (day_month == MONTH_CURRENT)
3005 g_signal_emit (calendar,
3006 gtk_calendar_signals[DAY_SELECTED_DOUBLE_CLICK_SIGNAL],
3012 gtk_calendar_button_press (GtkWidget *widget,
3013 GdkEventButton *event)
3015 GtkCalendar *calendar = GTK_CALENDAR (widget);
3016 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3019 if (event->window == priv->main_win)
3020 calendar_main_button_press (calendar, event);
3022 if (!gtk_widget_has_focus (widget))
3023 gtk_widget_grab_focus (widget);
3025 for (arrow = ARROW_YEAR_LEFT; arrow <= ARROW_MONTH_RIGHT; arrow++)
3027 if (event->window == priv->arrow_win[arrow])
3030 /* only call the action on single click, not double */
3031 if (event->type == GDK_BUTTON_PRESS)
3033 if (event->button == 1)
3034 calendar_start_spinning (calendar, arrow);
3036 calendar_arrow_action (calendar, arrow);
3047 gtk_calendar_button_release (GtkWidget *widget,
3048 GdkEventButton *event)
3050 GtkCalendar *calendar = GTK_CALENDAR (widget);
3051 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3053 if (event->button == 1)
3055 calendar_stop_spinning (calendar);
3065 gtk_calendar_motion_notify (GtkWidget *widget,
3066 GdkEventMotion *event)
3068 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3072 if (gtk_drag_check_threshold (widget,
3073 priv->drag_start_x, priv->drag_start_y,
3074 event->x, event->y))
3076 GdkDragContext *context;
3077 GtkTargetList *target_list = gtk_target_list_new (NULL, 0);
3078 gtk_target_list_add_text_targets (target_list, 0);
3079 context = gtk_drag_begin (widget, target_list, GDK_ACTION_COPY,
3080 1, (GdkEvent *)event);
3083 gtk_target_list_unref (target_list);
3084 gtk_drag_set_icon_default (context);
3092 gtk_calendar_enter_notify (GtkWidget *widget,
3093 GdkEventCrossing *event)
3095 GtkCalendar *calendar = GTK_CALENDAR (widget);
3096 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3098 if (event->window == priv->arrow_win[ARROW_MONTH_LEFT])
3100 priv->arrow_state[ARROW_MONTH_LEFT] |= GTK_STATE_FLAG_PRELIGHT;
3101 calendar_invalidate_arrow (calendar, ARROW_MONTH_LEFT);
3104 if (event->window == priv->arrow_win[ARROW_MONTH_RIGHT])
3106 priv->arrow_state[ARROW_MONTH_RIGHT] |= GTK_STATE_FLAG_PRELIGHT;
3107 calendar_invalidate_arrow (calendar, ARROW_MONTH_RIGHT);
3110 if (event->window == priv->arrow_win[ARROW_YEAR_LEFT])
3112 priv->arrow_state[ARROW_YEAR_LEFT] |= GTK_STATE_FLAG_PRELIGHT;
3113 calendar_invalidate_arrow (calendar, ARROW_YEAR_LEFT);
3116 if (event->window == priv->arrow_win[ARROW_YEAR_RIGHT])
3118 priv->arrow_state[ARROW_YEAR_RIGHT] |= GTK_STATE_FLAG_PRELIGHT;
3119 calendar_invalidate_arrow (calendar, ARROW_YEAR_RIGHT);
3126 gtk_calendar_leave_notify (GtkWidget *widget,
3127 GdkEventCrossing *event)
3129 GtkCalendar *calendar = GTK_CALENDAR (widget);
3130 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3132 if (event->window == priv->arrow_win[ARROW_MONTH_LEFT])
3134 priv->arrow_state[ARROW_MONTH_LEFT] &= ~(GTK_STATE_FLAG_PRELIGHT);
3135 calendar_invalidate_arrow (calendar, ARROW_MONTH_LEFT);
3138 if (event->window == priv->arrow_win[ARROW_MONTH_RIGHT])
3140 priv->arrow_state[ARROW_MONTH_RIGHT] &= ~(GTK_STATE_FLAG_PRELIGHT);
3141 calendar_invalidate_arrow (calendar, ARROW_MONTH_RIGHT);
3144 if (event->window == priv->arrow_win[ARROW_YEAR_LEFT])
3146 priv->arrow_state[ARROW_YEAR_LEFT] &= ~(GTK_STATE_FLAG_PRELIGHT);
3147 calendar_invalidate_arrow (calendar, ARROW_YEAR_LEFT);
3150 if (event->window == priv->arrow_win[ARROW_YEAR_RIGHT])
3152 priv->arrow_state[ARROW_YEAR_RIGHT] &= ~(GTK_STATE_FLAG_PRELIGHT);
3153 calendar_invalidate_arrow (calendar, ARROW_YEAR_RIGHT);
3160 gtk_calendar_scroll (GtkWidget *widget,
3161 GdkEventScroll *event)
3163 GtkCalendar *calendar = GTK_CALENDAR (widget);
3165 if (event->direction == GDK_SCROLL_UP)
3167 if (!gtk_widget_has_focus (widget))
3168 gtk_widget_grab_focus (widget);
3169 calendar_set_month_prev (calendar);
3171 else if (event->direction == GDK_SCROLL_DOWN)
3173 if (!gtk_widget_has_focus (widget))
3174 gtk_widget_grab_focus (widget);
3175 calendar_set_month_next (calendar);
3184 /****************************************
3186 ****************************************/
3189 move_focus (GtkCalendar *calendar,
3192 GtkCalendarPrivate *priv = calendar->priv;
3193 GtkTextDirection text_dir = gtk_widget_get_direction (GTK_WIDGET (calendar));
3195 if ((text_dir == GTK_TEXT_DIR_LTR && direction == -1) ||
3196 (text_dir == GTK_TEXT_DIR_RTL && direction == 1))
3198 if (priv->focus_col > 0)
3200 else if (priv->focus_row > 0)
3202 priv->focus_col = 6;
3206 if (priv->focus_col < 0)
3207 priv->focus_col = 6;
3208 if (priv->focus_row < 0)
3209 priv->focus_row = 5;
3213 if (priv->focus_col < 6)
3215 else if (priv->focus_row < 5)
3217 priv->focus_col = 0;
3221 if (priv->focus_col < 0)
3222 priv->focus_col = 0;
3223 if (priv->focus_row < 0)
3224 priv->focus_row = 0;
3229 gtk_calendar_key_press (GtkWidget *widget,
3232 GtkCalendar *calendar = GTK_CALENDAR (widget);
3233 GtkCalendarPrivate *priv = calendar->priv;
3241 old_focus_row = priv->focus_row;
3242 old_focus_col = priv->focus_col;
3244 switch (event->keyval)
3246 case GDK_KEY_KP_Left:
3249 if (event->state & GDK_CONTROL_MASK)
3250 calendar_set_month_prev (calendar);
3253 move_focus (calendar, -1);
3254 calendar_invalidate_day (calendar, old_focus_row, old_focus_col);
3255 calendar_invalidate_day (calendar, priv->focus_row,
3259 case GDK_KEY_KP_Right:
3262 if (event->state & GDK_CONTROL_MASK)
3263 calendar_set_month_next (calendar);
3266 move_focus (calendar, 1);
3267 calendar_invalidate_day (calendar, old_focus_row, old_focus_col);
3268 calendar_invalidate_day (calendar, priv->focus_row,
3275 if (event->state & GDK_CONTROL_MASK)
3276 calendar_set_year_prev (calendar);
3279 if (priv->focus_row > 0)
3281 if (priv->focus_row < 0)
3282 priv->focus_row = 5;
3283 if (priv->focus_col < 0)
3284 priv->focus_col = 6;
3285 calendar_invalidate_day (calendar, old_focus_row, old_focus_col);
3286 calendar_invalidate_day (calendar, priv->focus_row,
3290 case GDK_KEY_KP_Down:
3293 if (event->state & GDK_CONTROL_MASK)
3294 calendar_set_year_next (calendar);
3297 if (priv->focus_row < 5)
3299 if (priv->focus_col < 0)
3300 priv->focus_col = 0;
3301 calendar_invalidate_day (calendar, old_focus_row, old_focus_col);
3302 calendar_invalidate_day (calendar, priv->focus_row,
3306 case GDK_KEY_KP_Space:
3308 row = priv->focus_row;
3309 col = priv->focus_col;
3311 if (row > -1 && col > -1)
3315 day = priv->day[row][col];
3316 if (priv->day_month[row][col] == MONTH_PREV)
3317 calendar_set_month_prev (calendar);
3318 else if (priv->day_month[row][col] == MONTH_NEXT)
3319 calendar_set_month_next (calendar);
3321 calendar_select_and_focus_day (calendar, day);
3329 /****************************************
3330 * Misc widget methods *
3331 ****************************************/
3334 gtk_calendar_state_flags_changed (GtkWidget *widget,
3335 GtkStateFlags previous_state)
3337 GtkCalendar *calendar = GTK_CALENDAR (widget);
3338 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3341 if (!gtk_widget_is_sensitive (widget))
3344 calendar_stop_spinning (calendar);
3347 for (i = 0; i < 4; i++)
3348 if (gtk_widget_is_sensitive (widget))
3349 priv->arrow_state[i] &= ~(GTK_STATE_FLAG_INSENSITIVE);
3351 priv->arrow_state[i] |= GTK_STATE_FLAG_INSENSITIVE;
3355 gtk_calendar_grab_notify (GtkWidget *widget,
3356 gboolean was_grabbed)
3359 calendar_stop_spinning (GTK_CALENDAR (widget));
3363 gtk_calendar_focus_out (GtkWidget *widget,
3364 GdkEventFocus *event)
3366 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3367 GtkCalendar *calendar = GTK_CALENDAR (widget);
3369 calendar_queue_refresh (calendar);
3370 calendar_stop_spinning (calendar);
3378 /****************************************
3380 ****************************************/
3383 gtk_calendar_drag_data_get (GtkWidget *widget,
3384 GdkDragContext *context,
3385 GtkSelectionData *selection_data,
3389 GtkCalendar *calendar = GTK_CALENDAR (widget);
3390 GtkCalendarPrivate *priv = calendar->priv;
3395 date = g_date_new_dmy (priv->selected_day, priv->month + 1, priv->year);
3396 len = g_date_strftime (str, 127, "%x", date);
3397 gtk_selection_data_set_text (selection_data, str, len);
3402 /* Get/set whether drag_motion requested the drag data and
3403 * drag_data_received should thus not actually insert the data,
3404 * since the data doesn't result from a drop.
3407 set_status_pending (GdkDragContext *context,
3408 GdkDragAction suggested_action)
3410 g_object_set_data (G_OBJECT (context),
3411 I_("gtk-calendar-status-pending"),
3412 GINT_TO_POINTER (suggested_action));
3415 static GdkDragAction
3416 get_status_pending (GdkDragContext *context)
3418 return GPOINTER_TO_INT (g_object_get_data (G_OBJECT (context),
3419 "gtk-calendar-status-pending"));
3423 gtk_calendar_drag_leave (GtkWidget *widget,
3424 GdkDragContext *context,
3427 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3429 priv->drag_highlight = 0;
3430 gtk_drag_unhighlight (widget);
3435 gtk_calendar_drag_motion (GtkWidget *widget,
3436 GdkDragContext *context,
3441 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3444 if (!priv->drag_highlight)
3446 priv->drag_highlight = 1;
3447 gtk_drag_highlight (widget);
3450 target = gtk_drag_dest_find_target (widget, context, NULL);
3451 if (target == GDK_NONE || gdk_drag_context_get_suggested_action (context) == 0)
3452 gdk_drag_status (context, 0, time);
3455 set_status_pending (context, gdk_drag_context_get_suggested_action (context));
3456 gtk_drag_get_data (widget, context, target, time);
3463 gtk_calendar_drag_drop (GtkWidget *widget,
3464 GdkDragContext *context,
3471 target = gtk_drag_dest_find_target (widget, context, NULL);
3472 if (target != GDK_NONE)
3474 gtk_drag_get_data (widget, context,
3484 gtk_calendar_drag_data_received (GtkWidget *widget,
3485 GdkDragContext *context,
3488 GtkSelectionData *selection_data,
3492 GtkCalendar *calendar = GTK_CALENDAR (widget);
3493 GtkCalendarPrivate *priv = calendar->priv;
3494 guint day, month, year;
3497 GdkDragAction suggested_action;
3499 suggested_action = get_status_pending (context);
3501 if (suggested_action)
3503 set_status_pending (context, 0);
3505 /* We are getting this data due to a request in drag_motion,
3506 * rather than due to a request in drag_drop, so we are just
3507 * supposed to call drag_status, not actually paste in the
3510 str = (gchar*) gtk_selection_data_get_text (selection_data);
3514 date = g_date_new ();
3515 g_date_set_parse (date, str);
3516 if (!g_date_valid (date))
3517 suggested_action = 0;
3522 suggested_action = 0;
3524 gdk_drag_status (context, suggested_action, time);
3529 date = g_date_new ();
3530 str = (gchar*) gtk_selection_data_get_text (selection_data);
3533 g_date_set_parse (date, str);
3537 if (!g_date_valid (date))
3539 g_warning ("Received invalid date data\n");
3541 gtk_drag_finish (context, FALSE, FALSE, time);
3545 day = g_date_get_day (date);
3546 month = g_date_get_month (date);
3547 year = g_date_get_year (date);
3550 gtk_drag_finish (context, TRUE, FALSE, time);
3553 g_object_freeze_notify (G_OBJECT (calendar));
3554 if (!(priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
3555 && (priv->display_flags & GTK_CALENDAR_SHOW_HEADING))
3556 gtk_calendar_select_month (calendar, month - 1, year);
3557 gtk_calendar_select_day (calendar, day);
3558 g_object_thaw_notify (G_OBJECT (calendar));
3562 gtk_calendar_get_accessible (GtkWidget *widget)
3566 obj = GTK_WIDGET_CLASS (gtk_calendar_parent_class)->get_accessible (widget);
3568 atk_object_set_role (obj, ATK_ROLE_CALENDAR);
3574 /****************************************
3576 ****************************************/
3581 * Creates a new calendar, with the current date being selected.
3583 * Return value: a newly #GtkCalendar widget
3586 gtk_calendar_new (void)
3588 return g_object_new (GTK_TYPE_CALENDAR, NULL);
3592 * gtk_calendar_get_display_options:
3593 * @calendar: a #GtkCalendar
3595 * Returns the current display options of @calendar.
3597 * Return value: the display options.
3601 GtkCalendarDisplayOptions
3602 gtk_calendar_get_display_options (GtkCalendar *calendar)
3604 g_return_val_if_fail (GTK_IS_CALENDAR (calendar), 0);
3606 return calendar->priv->display_flags;
3610 * gtk_calendar_set_display_options:
3611 * @calendar: a #GtkCalendar
3612 * @flags: the display options to set
3614 * Sets display options (whether to display the heading and the month
3620 gtk_calendar_set_display_options (GtkCalendar *calendar,
3621 GtkCalendarDisplayOptions flags)
3623 GtkWidget *widget = GTK_WIDGET (calendar);
3624 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
3626 GtkCalendarDisplayOptions old_flags;
3628 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3630 old_flags = priv->display_flags;
3632 if (gtk_widget_get_realized (widget))
3634 if ((flags ^ priv->display_flags) & GTK_CALENDAR_NO_MONTH_CHANGE)
3637 if (! (flags & GTK_CALENDAR_NO_MONTH_CHANGE)
3638 && (priv->display_flags & GTK_CALENDAR_SHOW_HEADING))
3640 priv->display_flags &= ~GTK_CALENDAR_NO_MONTH_CHANGE;
3641 calendar_realize_arrows (calendar);
3642 if (gtk_widget_get_mapped (widget))
3643 calendar_map_arrows (calendar);
3647 calendar_unrealize_arrows (calendar);
3651 if ((flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_HEADING)
3655 if (flags & GTK_CALENDAR_SHOW_HEADING)
3657 priv->display_flags |= GTK_CALENDAR_SHOW_HEADING;
3658 calendar_realize_arrows (calendar);
3659 if (gtk_widget_get_mapped (widget))
3660 calendar_map_arrows (calendar);
3664 calendar_unrealize_arrows (calendar);
3668 if ((flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_DAY_NAMES)
3672 if (flags & GTK_CALENDAR_SHOW_DAY_NAMES)
3673 priv->display_flags |= GTK_CALENDAR_SHOW_DAY_NAMES;
3676 if ((flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
3680 if (flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
3681 priv->display_flags |= GTK_CALENDAR_SHOW_WEEK_NUMBERS;
3684 if ((flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_DETAILS)
3687 priv->display_flags = flags;
3689 gtk_widget_queue_resize (GTK_WIDGET (calendar));
3692 priv->display_flags = flags;
3694 g_object_freeze_notify (G_OBJECT (calendar));
3695 if ((old_flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_HEADING)
3696 g_object_notify (G_OBJECT (calendar), "show-heading");
3697 if ((old_flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_DAY_NAMES)
3698 g_object_notify (G_OBJECT (calendar), "show-day-names");
3699 if ((old_flags ^ priv->display_flags) & GTK_CALENDAR_NO_MONTH_CHANGE)
3700 g_object_notify (G_OBJECT (calendar), "no-month-change");
3701 if ((old_flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
3702 g_object_notify (G_OBJECT (calendar), "show-week-numbers");
3703 g_object_thaw_notify (G_OBJECT (calendar));
3707 * gtk_calendar_select_month:
3708 * @calendar: a #GtkCalendar
3709 * @month: a month number between 0 and 11.
3710 * @year: the year the month is in.
3712 * Shifts the calendar to a different month.
3715 gtk_calendar_select_month (GtkCalendar *calendar,
3719 GtkCalendarPrivate *priv;
3721 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3722 g_return_if_fail (month <= 11);
3724 priv = calendar->priv;
3726 priv->month = month;
3729 calendar_compute_days (calendar);
3730 calendar_queue_refresh (calendar);
3732 g_object_freeze_notify (G_OBJECT (calendar));
3733 g_object_notify (G_OBJECT (calendar), "month");
3734 g_object_notify (G_OBJECT (calendar), "year");
3735 g_object_thaw_notify (G_OBJECT (calendar));
3737 g_signal_emit (calendar,
3738 gtk_calendar_signals[MONTH_CHANGED_SIGNAL],
3743 * gtk_calendar_select_day:
3744 * @calendar: a #GtkCalendar.
3745 * @day: the day number between 1 and 31, or 0 to unselect
3746 * the currently selected day.
3748 * Selects a day from the current month.
3751 gtk_calendar_select_day (GtkCalendar *calendar,
3754 GtkCalendarPrivate *priv;
3756 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3757 g_return_if_fail (day <= 31);
3759 priv = calendar->priv;
3761 /* Deselect the old day */
3762 if (priv->selected_day > 0)
3766 selected_day = priv->selected_day;
3767 priv->selected_day = 0;
3768 if (gtk_widget_is_drawable (GTK_WIDGET (calendar)))
3769 calendar_invalidate_day_num (calendar, selected_day);
3772 priv->selected_day = day;
3774 /* Select the new day */
3777 if (gtk_widget_is_drawable (GTK_WIDGET (calendar)))
3778 calendar_invalidate_day_num (calendar, day);
3781 g_object_notify (G_OBJECT (calendar), "day");
3783 g_signal_emit (calendar,
3784 gtk_calendar_signals[DAY_SELECTED_SIGNAL],
3789 * gtk_calendar_clear_marks:
3790 * @calendar: a #GtkCalendar
3792 * Remove all visual markers.
3795 gtk_calendar_clear_marks (GtkCalendar *calendar)
3797 GtkCalendarPrivate *priv;
3800 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3802 priv = calendar->priv;
3804 for (day = 0; day < 31; day++)
3806 priv->marked_date[day] = FALSE;
3809 priv->num_marked_dates = 0;
3810 calendar_queue_refresh (calendar);
3814 * gtk_calendar_mark_day:
3815 * @calendar: a #GtkCalendar
3816 * @day: the day number to mark between 1 and 31.
3818 * Places a visual marker on a particular day.
3821 gtk_calendar_mark_day (GtkCalendar *calendar,
3824 GtkCalendarPrivate *priv;
3826 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3828 priv = calendar->priv;
3830 if (day >= 1 && day <= 31 && !priv->marked_date[day-1])
3832 priv->marked_date[day - 1] = TRUE;
3833 priv->num_marked_dates++;
3834 calendar_invalidate_day_num (calendar, day);
3839 * gtk_calendar_get_day_is_marked:
3840 * @calendar: a #GtkCalendar
3841 * @day: the day number between 1 and 31.
3843 * Returns if the @day of the @calendar is already marked.
3845 * Returns: whether the day is marked.
3850 gtk_calendar_get_day_is_marked (GtkCalendar *calendar,
3853 GtkCalendarPrivate *priv;
3855 g_return_val_if_fail (GTK_IS_CALENDAR (calendar), FALSE);
3857 priv = calendar->priv;
3859 if (day >= 1 && day <= 31)
3860 return priv->marked_date[day - 1];
3866 * gtk_calendar_unmark_day:
3867 * @calendar: a #GtkCalendar.
3868 * @day: the day number to unmark between 1 and 31.
3870 * Removes the visual marker from a particular day.
3873 gtk_calendar_unmark_day (GtkCalendar *calendar,
3876 GtkCalendarPrivate *priv;
3878 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3880 priv = calendar->priv;
3882 if (day >= 1 && day <= 31 && priv->marked_date[day-1])
3884 priv->marked_date[day - 1] = FALSE;
3885 priv->num_marked_dates--;
3886 calendar_invalidate_day_num (calendar, day);
3891 * gtk_calendar_get_date:
3892 * @calendar: a #GtkCalendar
3893 * @year: (out) (allow-none): location to store the year as a decimal
3894 * number (e.g. 2011), or %NULL
3895 * @month: (out) (allow-none): location to store the month number
3896 * (between 0 and 11), or %NULL
3897 * @day: (out) (allow-none): location to store the day number (between
3898 * 1 and 31), or %NULL
3900 * Obtains the selected date from a #GtkCalendar.
3903 gtk_calendar_get_date (GtkCalendar *calendar,
3908 GtkCalendarPrivate *priv;
3910 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3912 priv = calendar->priv;
3918 *month = priv->month;
3921 *day = priv->selected_day;
3925 * gtk_calendar_set_detail_func:
3926 * @calendar: a #GtkCalendar.
3927 * @func: a function providing details for each day.
3928 * @data: data to pass to @func invokations.
3929 * @destroy: a function for releasing @data.
3931 * Installs a function which provides Pango markup with detail information
3932 * for each day. Examples for such details are holidays or appointments. That
3933 * information is shown below each day when #GtkCalendar:show-details is set.
3934 * A tooltip containing with full detail information is provided, if the entire
3935 * text should not fit into the details area, or if #GtkCalendar:show-details
3938 * The size of the details area can be restricted by setting the
3939 * #GtkCalendar:detail-width-chars and #GtkCalendar:detail-height-rows
3945 gtk_calendar_set_detail_func (GtkCalendar *calendar,
3946 GtkCalendarDetailFunc func,
3948 GDestroyNotify destroy)
3950 GtkCalendarPrivate *priv;
3952 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3954 priv = GTK_CALENDAR_GET_PRIVATE (calendar);
3956 if (priv->detail_func_destroy)
3957 priv->detail_func_destroy (priv->detail_func_user_data);
3959 priv->detail_func = func;
3960 priv->detail_func_user_data = data;
3961 priv->detail_func_destroy = destroy;
3963 gtk_widget_set_has_tooltip (GTK_WIDGET (calendar),
3964 NULL != priv->detail_func);
3965 gtk_widget_queue_resize (GTK_WIDGET (calendar));
3969 * gtk_calendar_set_detail_width_chars:
3970 * @calendar: a #GtkCalendar.
3971 * @chars: detail width in characters.
3973 * Updates the width of detail cells.
3974 * See #GtkCalendar:detail-width-chars.
3979 gtk_calendar_set_detail_width_chars (GtkCalendar *calendar,
3982 GtkCalendarPrivate *priv;
3984 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3986 priv = GTK_CALENDAR_GET_PRIVATE (calendar);
3988 if (chars != priv->detail_width_chars)
3990 priv->detail_width_chars = chars;
3991 g_object_notify (G_OBJECT (calendar), "detail-width-chars");
3992 gtk_widget_queue_resize_no_redraw (GTK_WIDGET (calendar));
3997 * gtk_calendar_set_detail_height_rows:
3998 * @calendar: a #GtkCalendar.
3999 * @rows: detail height in rows.
4001 * Updates the height of detail cells.
4002 * See #GtkCalendar:detail-height-rows.
4007 gtk_calendar_set_detail_height_rows (GtkCalendar *calendar,
4010 GtkCalendarPrivate *priv;
4012 g_return_if_fail (GTK_IS_CALENDAR (calendar));
4014 priv = GTK_CALENDAR_GET_PRIVATE (calendar);
4016 if (rows != priv->detail_height_rows)
4018 priv->detail_height_rows = rows;
4019 g_object_notify (G_OBJECT (calendar), "detail-height-rows");
4020 gtk_widget_queue_resize_no_redraw (GTK_WIDGET (calendar));
4025 * gtk_calendar_get_detail_width_chars:
4026 * @calendar: a #GtkCalendar.
4028 * Queries the width of detail cells, in characters.
4029 * See #GtkCalendar:detail-width-chars.
4033 * Return value: The width of detail cells, in characters.
4036 gtk_calendar_get_detail_width_chars (GtkCalendar *calendar)
4038 g_return_val_if_fail (GTK_IS_CALENDAR (calendar), 0);
4039 return GTK_CALENDAR_GET_PRIVATE (calendar)->detail_width_chars;
4043 * gtk_calendar_get_detail_height_rows:
4044 * @calendar: a #GtkCalendar.
4046 * Queries the height of detail cells, in rows.
4047 * See #GtkCalendar:detail-width-chars.
4051 * Return value: The height of detail cells, in rows.
4054 gtk_calendar_get_detail_height_rows (GtkCalendar *calendar)
4056 g_return_val_if_fail (GTK_IS_CALENDAR (calendar), 0);
4057 return GTK_CALENDAR_GET_PRIVATE (calendar)->detail_height_rows;