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 void calendar_start_spinning (GtkCalendar *calendar,
398 static void calendar_stop_spinning (GtkCalendar *calendar);
400 static void calendar_invalidate_day (GtkCalendar *widget,
403 static void calendar_invalidate_day_num (GtkCalendar *widget,
405 static void calendar_invalidate_arrow (GtkCalendar *widget,
408 static void calendar_compute_days (GtkCalendar *calendar);
409 static gint calendar_get_xsep (GtkCalendar *calendar);
410 static gint calendar_get_ysep (GtkCalendar *calendar);
411 static gint calendar_get_inner_border (GtkCalendar *calendar);
413 static char *default_abbreviated_dayname[7];
414 static char *default_monthname[12];
416 G_DEFINE_TYPE (GtkCalendar, gtk_calendar, GTK_TYPE_WIDGET)
419 gtk_calendar_class_init (GtkCalendarClass *class)
421 GObjectClass *gobject_class;
422 GtkWidgetClass *widget_class;
424 gobject_class = (GObjectClass*) class;
425 widget_class = (GtkWidgetClass*) class;
427 gobject_class->set_property = gtk_calendar_set_property;
428 gobject_class->get_property = gtk_calendar_get_property;
429 gobject_class->finalize = gtk_calendar_finalize;
431 widget_class->destroy = gtk_calendar_destroy;
432 widget_class->realize = gtk_calendar_realize;
433 widget_class->unrealize = gtk_calendar_unrealize;
434 widget_class->map = gtk_calendar_map;
435 widget_class->unmap = gtk_calendar_unmap;
436 widget_class->draw = gtk_calendar_draw;
437 widget_class->get_preferred_width = gtk_calendar_get_preferred_width;
438 widget_class->get_preferred_height = gtk_calendar_get_preferred_height;
439 widget_class->size_allocate = gtk_calendar_size_allocate;
440 widget_class->button_press_event = gtk_calendar_button_press;
441 widget_class->button_release_event = gtk_calendar_button_release;
442 widget_class->motion_notify_event = gtk_calendar_motion_notify;
443 widget_class->enter_notify_event = gtk_calendar_enter_notify;
444 widget_class->leave_notify_event = gtk_calendar_leave_notify;
445 widget_class->key_press_event = gtk_calendar_key_press;
446 widget_class->scroll_event = gtk_calendar_scroll;
447 widget_class->state_flags_changed = gtk_calendar_state_flags_changed;
448 widget_class->grab_notify = gtk_calendar_grab_notify;
449 widget_class->focus_out_event = gtk_calendar_focus_out;
450 widget_class->query_tooltip = gtk_calendar_query_tooltip;
452 widget_class->drag_data_get = gtk_calendar_drag_data_get;
453 widget_class->drag_motion = gtk_calendar_drag_motion;
454 widget_class->drag_leave = gtk_calendar_drag_leave;
455 widget_class->drag_drop = gtk_calendar_drag_drop;
456 widget_class->drag_data_received = gtk_calendar_drag_data_received;
462 * This property gets initially set to the current year.
464 g_object_class_install_property (gobject_class,
466 g_param_spec_int ("year",
468 P_("The selected year"),
470 GTK_PARAM_READWRITE));
475 * The selected month (as a number between 0 and 11).
476 * This property gets initially set to the current month.
478 g_object_class_install_property (gobject_class,
480 g_param_spec_int ("month",
482 P_("The selected month (as a number between 0 and 11)"),
484 GTK_PARAM_READWRITE));
489 * The selected day (as a number between 1 and 31, or 0
490 * to unselect the currently selected day).
491 * This property gets initially set to the current day.
493 g_object_class_install_property (gobject_class,
495 g_param_spec_int ("day",
497 P_("The selected day (as a number between 1 and 31, or 0 to unselect the currently selected day)"),
499 GTK_PARAM_READWRITE));
502 * GtkCalendar:show-heading:
504 * Determines whether a heading is displayed.
508 g_object_class_install_property (gobject_class,
510 g_param_spec_boolean ("show-heading",
512 P_("If TRUE, a heading is displayed"),
514 GTK_PARAM_READWRITE));
517 * GtkCalendar:show-day-names:
519 * Determines whether day names are displayed.
523 g_object_class_install_property (gobject_class,
525 g_param_spec_boolean ("show-day-names",
526 P_("Show Day Names"),
527 P_("If TRUE, day names are displayed"),
529 GTK_PARAM_READWRITE));
531 * GtkCalendar:no-month-change:
533 * Determines whether the selected month can be changed.
537 g_object_class_install_property (gobject_class,
538 PROP_NO_MONTH_CHANGE,
539 g_param_spec_boolean ("no-month-change",
540 P_("No Month Change"),
541 P_("If TRUE, the selected month cannot be changed"),
543 GTK_PARAM_READWRITE));
546 * GtkCalendar:show-week-numbers:
548 * Determines whether week numbers are displayed.
552 g_object_class_install_property (gobject_class,
553 PROP_SHOW_WEEK_NUMBERS,
554 g_param_spec_boolean ("show-week-numbers",
555 P_("Show Week Numbers"),
556 P_("If TRUE, week numbers are displayed"),
558 GTK_PARAM_READWRITE));
561 * GtkCalendar:detail-width-chars:
563 * Width of a detail cell, in characters.
564 * A value of 0 allows any width. See gtk_calendar_set_detail_func().
568 g_object_class_install_property (gobject_class,
569 PROP_DETAIL_WIDTH_CHARS,
570 g_param_spec_int ("detail-width-chars",
572 P_("Details width in characters"),
574 GTK_PARAM_READWRITE));
577 * GtkCalendar:detail-height-rows:
579 * Height of a detail cell, in rows.
580 * A value of 0 allows any width. See gtk_calendar_set_detail_func().
584 g_object_class_install_property (gobject_class,
585 PROP_DETAIL_HEIGHT_ROWS,
586 g_param_spec_int ("detail-height-rows",
587 P_("Details Height"),
588 P_("Details height in rows"),
590 GTK_PARAM_READWRITE));
593 * GtkCalendar:show-details:
595 * Determines whether details are shown directly in the widget, or if they are
596 * available only as tooltip. When this property is set days with details are
601 g_object_class_install_property (gobject_class,
603 g_param_spec_boolean ("show-details",
605 P_("If TRUE, details are shown"),
607 GTK_PARAM_READWRITE));
611 * GtkCalendar:inner-border
613 * The spacing around the day/week headers and main area.
615 gtk_widget_class_install_style_property (widget_class,
616 g_param_spec_int ("inner-border",
618 P_("Inner border space"),
620 GTK_PARAM_READABLE));
623 * GtkCalndar:vertical-separation
625 * Separation between day headers and main area.
627 gtk_widget_class_install_style_property (widget_class,
628 g_param_spec_int ("vertical-separation",
629 P_("Vertical separation"),
630 P_("Space between day headers and main area"),
632 GTK_PARAM_READABLE));
635 * GtkCalendar:horizontal-separation
637 * Separation between week headers and main area.
639 gtk_widget_class_install_style_property (widget_class,
640 g_param_spec_int ("horizontal-separation",
641 P_("Horizontal separation"),
642 P_("Space between week headers and main area"),
644 GTK_PARAM_READABLE));
647 * GtkCalendar::month-changed:
648 * @calendar: the object which received the signal.
650 * Emitted when the user clicks a button to change the selected month on a
653 gtk_calendar_signals[MONTH_CHANGED_SIGNAL] =
654 g_signal_new (I_("month-changed"),
655 G_OBJECT_CLASS_TYPE (gobject_class),
657 G_STRUCT_OFFSET (GtkCalendarClass, month_changed),
659 _gtk_marshal_VOID__VOID,
663 * GtkCalendar::day-selected:
664 * @calendar: the object which received the signal.
666 * Emitted when the user selects a day.
668 gtk_calendar_signals[DAY_SELECTED_SIGNAL] =
669 g_signal_new (I_("day-selected"),
670 G_OBJECT_CLASS_TYPE (gobject_class),
672 G_STRUCT_OFFSET (GtkCalendarClass, day_selected),
674 _gtk_marshal_VOID__VOID,
678 * GtkCalendar::day-selected-double-click:
679 * @calendar: the object which received the signal.
681 * Emitted when the user double-clicks a day.
683 gtk_calendar_signals[DAY_SELECTED_DOUBLE_CLICK_SIGNAL] =
684 g_signal_new (I_("day-selected-double-click"),
685 G_OBJECT_CLASS_TYPE (gobject_class),
687 G_STRUCT_OFFSET (GtkCalendarClass, day_selected_double_click),
689 _gtk_marshal_VOID__VOID,
693 * GtkCalendar::prev-month:
694 * @calendar: the object which received the signal.
696 * Emitted when the user switched to the previous month.
698 gtk_calendar_signals[PREV_MONTH_SIGNAL] =
699 g_signal_new (I_("prev-month"),
700 G_OBJECT_CLASS_TYPE (gobject_class),
702 G_STRUCT_OFFSET (GtkCalendarClass, prev_month),
704 _gtk_marshal_VOID__VOID,
708 * GtkCalendar::next-month:
709 * @calendar: the object which received the signal.
711 * Emitted when the user switched to the next month.
713 gtk_calendar_signals[NEXT_MONTH_SIGNAL] =
714 g_signal_new (I_("next-month"),
715 G_OBJECT_CLASS_TYPE (gobject_class),
717 G_STRUCT_OFFSET (GtkCalendarClass, next_month),
719 _gtk_marshal_VOID__VOID,
723 * GtkCalendar::prev-year:
724 * @calendar: the object which received the signal.
726 * Emitted when user switched to the previous year.
728 gtk_calendar_signals[PREV_YEAR_SIGNAL] =
729 g_signal_new (I_("prev-year"),
730 G_OBJECT_CLASS_TYPE (gobject_class),
732 G_STRUCT_OFFSET (GtkCalendarClass, prev_year),
734 _gtk_marshal_VOID__VOID,
738 * GtkCalendar::next-year:
739 * @calendar: the object which received the signal.
741 * Emitted when user switched to the next year.
743 gtk_calendar_signals[NEXT_YEAR_SIGNAL] =
744 g_signal_new (I_("next-year"),
745 G_OBJECT_CLASS_TYPE (gobject_class),
747 G_STRUCT_OFFSET (GtkCalendarClass, next_year),
749 _gtk_marshal_VOID__VOID,
752 g_type_class_add_private (gobject_class, sizeof (GtkCalendarPrivate));
756 gtk_calendar_init (GtkCalendar *calendar)
758 GtkWidget *widget = GTK_WIDGET (calendar);
763 wchar_t wbuffer[100];
768 GtkCalendarPrivate *priv;
770 #ifdef HAVE__NL_TIME_FIRST_WEEKDAY
771 union { unsigned int word; char *string; } langinfo;
772 gint week_1stday = 0;
773 gint first_weekday = 1;
779 priv = calendar->priv = G_TYPE_INSTANCE_GET_PRIVATE (calendar,
783 gtk_widget_set_can_focus (widget, TRUE);
784 gtk_widget_set_has_window (widget, FALSE);
786 if (!default_abbreviated_dayname[0])
790 tmp_time= (i+3)*86400;
791 strftime ( buffer, sizeof (buffer), "%a", gmtime (&tmp_time));
792 default_abbreviated_dayname[i] = g_locale_to_utf8 (buffer, -1, NULL, NULL, NULL);
794 if (!GetLocaleInfoW (GetThreadLocale (), LOCALE_SABBREVDAYNAME1 + (i+6)%7,
795 wbuffer, G_N_ELEMENTS (wbuffer)))
796 default_abbreviated_dayname[i] = g_strdup_printf ("(%d)", i);
798 default_abbreviated_dayname[i] = g_utf16_to_utf8 (wbuffer, -1, NULL, NULL, NULL);
802 if (!default_monthname[0])
807 strftime ( buffer, sizeof (buffer), "%B", gmtime (&tmp_time));
808 default_monthname[i] = g_locale_to_utf8 (buffer, -1, NULL, NULL, NULL);
810 if (!GetLocaleInfoW (GetThreadLocale (), LOCALE_SMONTHNAME1 + i,
811 wbuffer, G_N_ELEMENTS (wbuffer)))
812 default_monthname[i] = g_strdup_printf ("(%d)", i);
814 default_monthname[i] = g_utf16_to_utf8 (wbuffer, -1, NULL, NULL, NULL);
820 tm = localtime (&secs);
821 priv->month = tm->tm_mon;
822 priv->year = 1900 + tm->tm_year;
825 priv->marked_date[i] = FALSE;
826 priv->num_marked_dates = 0;
827 priv->selected_day = tm->tm_mday;
829 priv->display_flags = (GTK_CALENDAR_SHOW_HEADING |
830 GTK_CALENDAR_SHOW_DAY_NAMES |
831 GTK_CALENDAR_SHOW_DETAILS);
833 priv->focus_row = -1;
834 priv->focus_col = -1;
836 priv->max_year_width = 0;
837 priv->max_month_width = 0;
838 priv->max_day_char_width = 0;
839 priv->max_week_char_width = 0;
841 priv->max_day_char_ascent = 0;
842 priv->max_day_char_descent = 0;
843 priv->max_label_char_ascent = 0;
844 priv->max_label_char_descent = 0;
846 priv->arrow_width = 10;
848 priv->need_timer = 0;
850 priv->click_child = -1;
853 priv->drag_highlight = 0;
855 gtk_drag_dest_set (widget, 0, NULL, 0, GDK_ACTION_COPY);
856 gtk_drag_dest_add_text_targets (widget);
858 priv->year_before = 0;
860 /* Translate to calendar:YM if you want years to be displayed
861 * before months; otherwise translate to calendar:MY.
862 * Do *not* translate it to anything else, if it
863 * it isn't calendar:YM or calendar:MY it will not work.
865 * Note that the ordering described here is logical order, which is
866 * further influenced by BIDI ordering. Thus, if you have a default
867 * text direction of RTL and specify "calendar:YM", then the year
868 * will appear to the right of the month.
870 year_before = _("calendar:MY");
871 if (strcmp (year_before, "calendar:YM") == 0)
872 priv->year_before = 1;
873 else if (strcmp (year_before, "calendar:MY") != 0)
874 g_warning ("Whoever translated calendar:MY did so wrongly.\n");
877 priv->week_start = 0;
880 if (GetLocaleInfoW (GetThreadLocale (), LOCALE_IFIRSTDAYOFWEEK,
881 wbuffer, G_N_ELEMENTS (wbuffer)))
882 week_start = g_utf16_to_utf8 (wbuffer, -1, NULL, NULL, NULL);
884 if (week_start != NULL)
886 priv->week_start = (week_start[0] - '0' + 1) % 7;
890 #ifdef HAVE__NL_TIME_FIRST_WEEKDAY
891 langinfo.string = nl_langinfo (_NL_TIME_FIRST_WEEKDAY);
892 first_weekday = langinfo.string[0];
893 langinfo.string = nl_langinfo (_NL_TIME_WEEK_1STDAY);
894 week_origin = langinfo.word;
895 if (week_origin == 19971130) /* Sunday */
897 else if (week_origin == 19971201) /* Monday */
900 g_warning ("Unknown value of _NL_TIME_WEEK_1STDAY.\n");
902 priv->week_start = (week_1stday + first_weekday - 1) % 7;
904 /* Translate to calendar:week_start:0 if you want Sunday to be the
905 * first day of the week to calendar:week_start:1 if you want Monday
906 * to be the first day of the week, and so on.
908 week_start = _("calendar:week_start:0");
910 if (strncmp (week_start, "calendar:week_start:", 20) == 0)
911 priv->week_start = *(week_start + 20) - '0';
913 priv->week_start = -1;
915 if (priv->week_start < 0 || priv->week_start > 6)
917 g_warning ("Whoever translated calendar:week_start:0 did so wrongly.\n");
918 priv->week_start = 0;
923 calendar_compute_days (calendar);
927 /****************************************
928 * Utility Functions *
929 ****************************************/
932 calendar_queue_refresh (GtkCalendar *calendar)
934 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
936 if (!(priv->detail_func) ||
937 !(priv->display_flags & GTK_CALENDAR_SHOW_DETAILS) ||
938 (priv->detail_width_chars && priv->detail_height_rows))
939 gtk_widget_queue_draw (GTK_WIDGET (calendar));
941 gtk_widget_queue_resize (GTK_WIDGET (calendar));
945 calendar_set_month_next (GtkCalendar *calendar)
948 GtkCalendarPrivate *priv = calendar->priv;
950 if (priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
953 if (priv->month == 11)
961 calendar_compute_days (calendar);
962 g_signal_emit (calendar,
963 gtk_calendar_signals[NEXT_MONTH_SIGNAL],
965 g_signal_emit (calendar,
966 gtk_calendar_signals[MONTH_CHANGED_SIGNAL],
969 month_len = month_length[leap (priv->year)][priv->month + 1];
971 if (month_len < priv->selected_day)
973 priv->selected_day = 0;
974 gtk_calendar_select_day (calendar, month_len);
977 gtk_calendar_select_day (calendar, priv->selected_day);
979 calendar_queue_refresh (calendar);
983 calendar_set_year_prev (GtkCalendar *calendar)
985 GtkCalendarPrivate *priv = calendar->priv;
989 calendar_compute_days (calendar);
990 g_signal_emit (calendar,
991 gtk_calendar_signals[PREV_YEAR_SIGNAL],
993 g_signal_emit (calendar,
994 gtk_calendar_signals[MONTH_CHANGED_SIGNAL],
997 month_len = month_length[leap (priv->year)][priv->month + 1];
999 if (month_len < priv->selected_day)
1001 priv->selected_day = 0;
1002 gtk_calendar_select_day (calendar, month_len);
1005 gtk_calendar_select_day (calendar, priv->selected_day);
1007 calendar_queue_refresh (calendar);
1011 calendar_set_year_next (GtkCalendar *calendar)
1013 GtkCalendarPrivate *priv = calendar->priv;
1017 calendar_compute_days (calendar);
1018 g_signal_emit (calendar,
1019 gtk_calendar_signals[NEXT_YEAR_SIGNAL],
1021 g_signal_emit (calendar,
1022 gtk_calendar_signals[MONTH_CHANGED_SIGNAL],
1025 month_len = month_length[leap (priv->year)][priv->month + 1];
1027 if (month_len < priv->selected_day)
1029 priv->selected_day = 0;
1030 gtk_calendar_select_day (calendar, month_len);
1033 gtk_calendar_select_day (calendar, priv->selected_day);
1035 calendar_queue_refresh (calendar);
1039 calendar_compute_days (GtkCalendar *calendar)
1041 GtkCalendarPrivate *priv = calendar->priv;
1044 gint ndays_in_month;
1045 gint ndays_in_prev_month;
1052 month = priv->month + 1;
1054 ndays_in_month = month_length[leap (year)][month];
1056 first_day = day_of_week (year, month, 1);
1057 first_day = (first_day + 7 - priv->week_start) % 7;
1059 /* Compute days of previous month */
1061 ndays_in_prev_month = month_length[leap (year)][month-1];
1063 ndays_in_prev_month = month_length[leap (year)][12];
1064 day = ndays_in_prev_month - first_day + 1;
1069 for (col = 0; col < first_day; col++)
1071 priv->day[row][col] = day;
1072 priv->day_month[row][col] = MONTH_PREV;
1077 /* Compute days of current month */
1079 for (day = 1; day <= ndays_in_month; day++)
1081 priv->day[row][col] = day;
1082 priv->day_month[row][col] = MONTH_CURRENT;
1092 /* Compute days of next month */
1094 for (; row <= 5; row++)
1096 for (; col <= 6; col++)
1098 priv->day[row][col] = day;
1099 priv->day_month[row][col] = MONTH_NEXT;
1107 calendar_select_and_focus_day (GtkCalendar *calendar,
1110 GtkCalendarPrivate *priv = calendar->priv;
1111 gint old_focus_row = priv->focus_row;
1112 gint old_focus_col = priv->focus_col;
1116 for (row = 0; row < 6; row ++)
1117 for (col = 0; col < 7; col++)
1119 if (priv->day_month[row][col] == MONTH_CURRENT
1120 && priv->day[row][col] == day)
1122 priv->focus_row = row;
1123 priv->focus_col = col;
1127 if (old_focus_row != -1 && old_focus_col != -1)
1128 calendar_invalidate_day (calendar, old_focus_row, old_focus_col);
1130 gtk_calendar_select_day (calendar, day);
1134 /****************************************
1135 * Layout computation utilities *
1136 ****************************************/
1139 calendar_row_height (GtkCalendar *calendar)
1141 GtkCalendarPrivate *priv = calendar->priv;
1143 return (GTK_CALENDAR_GET_PRIVATE (calendar)->main_h - CALENDAR_MARGIN
1144 - ((priv->display_flags & GTK_CALENDAR_SHOW_DAY_NAMES)
1145 ? calendar_get_ysep (calendar) : CALENDAR_MARGIN)) / 6;
1149 /* calendar_left_x_for_column: returns the x coordinate
1150 * for the left of the column */
1152 calendar_left_x_for_column (GtkCalendar *calendar,
1155 GtkCalendarPrivate *priv = calendar->priv;
1159 gint calendar_xsep = calendar_get_xsep (calendar);
1160 GtkStyleContext *context;
1161 GtkStateFlags state;
1162 gint inner_border = calendar_get_inner_border (calendar);
1165 context = gtk_widget_get_style_context (GTK_WIDGET (calendar));
1166 state = gtk_widget_get_state_flags (GTK_WIDGET (calendar));
1167 gtk_style_context_get_padding (context, state, &padding);
1169 week_width = priv->week_width + padding.left + inner_border;
1171 if (gtk_widget_get_direction (GTK_WIDGET (calendar)) == GTK_TEXT_DIR_RTL)
1173 column = 6 - column;
1177 width = GTK_CALENDAR_GET_PRIVATE (calendar)->day_width;
1178 if (priv->display_flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
1179 x_left = week_width + calendar_xsep + (width + DAY_XSEP) * column;
1181 x_left = week_width + CALENDAR_MARGIN + (width + DAY_XSEP) * column;
1186 /* column_from_x: returns the column 0-6 that the
1187 * x pixel of the xwindow is in */
1189 calendar_column_from_x (GtkCalendar *calendar,
1193 gint x_left, x_right;
1197 for (c = 0; c < 7; c++)
1199 x_left = calendar_left_x_for_column (calendar, c);
1200 x_right = x_left + GTK_CALENDAR_GET_PRIVATE (calendar)->day_width;
1202 if (event_x >= x_left && event_x < x_right)
1212 /* calendar_top_y_for_row: returns the y coordinate
1213 * for the top of the row */
1215 calendar_top_y_for_row (GtkCalendar *calendar,
1218 GtkStyleContext *context;
1219 GtkAllocation allocation;
1220 gint inner_border = calendar_get_inner_border (calendar);
1221 GtkStateFlags state;
1224 gtk_widget_get_allocation (GTK_WIDGET (calendar), &allocation);
1225 context = gtk_widget_get_style_context (GTK_WIDGET (calendar));
1226 state = gtk_widget_get_state_flags (GTK_WIDGET (calendar));
1228 gtk_style_context_get_padding (context, state, &padding);
1230 return allocation.height
1231 - padding.top - inner_border
1232 - (CALENDAR_MARGIN + (6 - row)
1233 * calendar_row_height (calendar));
1236 /* row_from_y: returns the row 0-5 that the
1237 * y pixel of the xwindow is in */
1239 calendar_row_from_y (GtkCalendar *calendar,
1244 gint y_top, y_bottom;
1246 height = calendar_row_height (calendar);
1249 for (r = 0; r < 6; r++)
1251 y_top = calendar_top_y_for_row (calendar, r);
1252 y_bottom = y_top + height;
1254 if (event_y >= y_top && event_y < y_bottom)
1265 calendar_arrow_rectangle (GtkCalendar *calendar,
1269 GtkWidget *widget = GTK_WIDGET (calendar);
1270 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1271 GtkAllocation allocation;
1272 GtkStyleContext *context;
1273 GtkStateFlags state;
1277 gtk_widget_get_allocation (widget, &allocation);
1278 context = gtk_widget_get_style_context (widget);
1279 state = gtk_widget_get_state_flags (widget);
1281 gtk_style_context_get_padding (context, state, &padding);
1283 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
1284 year_left = priv->year_before;
1286 year_left = !priv->year_before;
1289 rect->width = priv->arrow_width;
1290 rect->height = priv->header_h - 7;
1294 case ARROW_MONTH_LEFT:
1296 rect->x = (allocation.width - padding.left - padding.right
1297 - (3 + 2 * priv->arrow_width + priv->max_month_width));
1301 case ARROW_MONTH_RIGHT:
1303 rect->x = (allocation.width - padding.left - padding.right
1304 - 3 - priv->arrow_width);
1306 rect->x = (priv->arrow_width + priv->max_month_width);
1308 case ARROW_YEAR_LEFT:
1312 rect->x = (allocation.width - padding.left - padding.right
1313 - (3 + 2 * priv->arrow_width + priv->max_year_width));
1315 case ARROW_YEAR_RIGHT:
1317 rect->x = (priv->arrow_width + priv->max_year_width);
1319 rect->x = (allocation.width - padding.left - padding.right
1320 - 3 - priv->arrow_width);
1324 rect->x += padding.left;
1325 rect->y += padding.top;
1329 calendar_day_rectangle (GtkCalendar *calendar,
1334 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1336 rect->x = calendar_left_x_for_column (calendar, col);
1337 rect->y = calendar_top_y_for_row (calendar, row);
1338 rect->height = calendar_row_height (calendar);
1339 rect->width = priv->day_width;
1343 calendar_set_month_prev (GtkCalendar *calendar)
1345 GtkCalendarPrivate *priv = calendar->priv;
1348 if (priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
1351 if (priv->month == 0)
1359 month_len = month_length[leap (priv->year)][priv->month + 1];
1361 calendar_compute_days (calendar);
1363 g_signal_emit (calendar,
1364 gtk_calendar_signals[PREV_MONTH_SIGNAL],
1366 g_signal_emit (calendar,
1367 gtk_calendar_signals[MONTH_CHANGED_SIGNAL],
1370 if (month_len < priv->selected_day)
1372 priv->selected_day = 0;
1373 gtk_calendar_select_day (calendar, month_len);
1377 if (priv->selected_day < 0)
1378 priv->selected_day = priv->selected_day + 1 + month_length[leap (priv->year)][priv->month + 1];
1379 gtk_calendar_select_day (calendar, priv->selected_day);
1382 calendar_queue_refresh (calendar);
1386 /****************************************
1387 * Basic object methods *
1388 ****************************************/
1391 gtk_calendar_finalize (GObject *object)
1393 G_OBJECT_CLASS (gtk_calendar_parent_class)->finalize (object);
1397 gtk_calendar_destroy (GtkWidget *widget)
1399 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
1401 calendar_stop_spinning (GTK_CALENDAR (widget));
1403 /* Call the destroy function for the extra display callback: */
1404 if (priv->detail_func_destroy && priv->detail_func_user_data)
1406 priv->detail_func_destroy (priv->detail_func_user_data);
1407 priv->detail_func_user_data = NULL;
1408 priv->detail_func_destroy = NULL;
1411 GTK_WIDGET_CLASS (gtk_calendar_parent_class)->destroy (widget);
1416 calendar_set_display_option (GtkCalendar *calendar,
1417 GtkCalendarDisplayOptions flag,
1420 GtkCalendarPrivate *priv = calendar->priv;
1421 GtkCalendarDisplayOptions flags;
1424 flags = priv->display_flags | flag;
1426 flags = priv->display_flags & ~flag;
1427 gtk_calendar_set_display_options (calendar, flags);
1431 calendar_get_display_option (GtkCalendar *calendar,
1432 GtkCalendarDisplayOptions flag)
1434 GtkCalendarPrivate *priv = calendar->priv;
1436 return (priv->display_flags & flag) != 0;
1440 gtk_calendar_set_property (GObject *object,
1442 const GValue *value,
1445 GtkCalendar *calendar = GTK_CALENDAR (object);
1446 GtkCalendarPrivate *priv = calendar->priv;
1451 gtk_calendar_select_month (calendar,
1453 g_value_get_int (value));
1456 gtk_calendar_select_month (calendar,
1457 g_value_get_int (value),
1461 gtk_calendar_select_day (calendar,
1462 g_value_get_int (value));
1464 case PROP_SHOW_HEADING:
1465 calendar_set_display_option (calendar,
1466 GTK_CALENDAR_SHOW_HEADING,
1467 g_value_get_boolean (value));
1469 case PROP_SHOW_DAY_NAMES:
1470 calendar_set_display_option (calendar,
1471 GTK_CALENDAR_SHOW_DAY_NAMES,
1472 g_value_get_boolean (value));
1474 case PROP_NO_MONTH_CHANGE:
1475 calendar_set_display_option (calendar,
1476 GTK_CALENDAR_NO_MONTH_CHANGE,
1477 g_value_get_boolean (value));
1479 case PROP_SHOW_WEEK_NUMBERS:
1480 calendar_set_display_option (calendar,
1481 GTK_CALENDAR_SHOW_WEEK_NUMBERS,
1482 g_value_get_boolean (value));
1484 case PROP_SHOW_DETAILS:
1485 calendar_set_display_option (calendar,
1486 GTK_CALENDAR_SHOW_DETAILS,
1487 g_value_get_boolean (value));
1489 case PROP_DETAIL_WIDTH_CHARS:
1490 gtk_calendar_set_detail_width_chars (calendar,
1491 g_value_get_int (value));
1493 case PROP_DETAIL_HEIGHT_ROWS:
1494 gtk_calendar_set_detail_height_rows (calendar,
1495 g_value_get_int (value));
1498 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1504 gtk_calendar_get_property (GObject *object,
1509 GtkCalendar *calendar = GTK_CALENDAR (object);
1510 GtkCalendarPrivate *priv = calendar->priv;
1515 g_value_set_int (value, priv->year);
1518 g_value_set_int (value, priv->month);
1521 g_value_set_int (value, priv->selected_day);
1523 case PROP_SHOW_HEADING:
1524 g_value_set_boolean (value, calendar_get_display_option (calendar,
1525 GTK_CALENDAR_SHOW_HEADING));
1527 case PROP_SHOW_DAY_NAMES:
1528 g_value_set_boolean (value, calendar_get_display_option (calendar,
1529 GTK_CALENDAR_SHOW_DAY_NAMES));
1531 case PROP_NO_MONTH_CHANGE:
1532 g_value_set_boolean (value, calendar_get_display_option (calendar,
1533 GTK_CALENDAR_NO_MONTH_CHANGE));
1535 case PROP_SHOW_WEEK_NUMBERS:
1536 g_value_set_boolean (value, calendar_get_display_option (calendar,
1537 GTK_CALENDAR_SHOW_WEEK_NUMBERS));
1539 case PROP_SHOW_DETAILS:
1540 g_value_set_boolean (value, calendar_get_display_option (calendar,
1541 GTK_CALENDAR_SHOW_DETAILS));
1543 case PROP_DETAIL_WIDTH_CHARS:
1544 g_value_set_int (value, priv->detail_width_chars);
1546 case PROP_DETAIL_HEIGHT_ROWS:
1547 g_value_set_int (value, priv->detail_height_rows);
1550 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1556 /****************************************
1558 ****************************************/
1561 calendar_realize_arrows (GtkCalendar *calendar)
1563 GtkWidget *widget = GTK_WIDGET (calendar);
1564 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1565 GdkWindowAttr attributes;
1566 gint attributes_mask;
1568 GtkAllocation allocation;
1570 if (! (priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
1571 && (priv->display_flags & GTK_CALENDAR_SHOW_HEADING))
1573 gtk_widget_get_allocation (widget, &allocation);
1575 attributes.wclass = GDK_INPUT_ONLY;
1576 attributes.window_type = GDK_WINDOW_CHILD;
1577 attributes.event_mask = (gtk_widget_get_events (widget)
1578 | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
1579 | GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK);
1580 attributes_mask = GDK_WA_X | GDK_WA_Y;
1581 for (i = 0; i < 4; i++)
1584 calendar_arrow_rectangle (calendar, i, &rect);
1586 attributes.x = allocation.x + rect.x;
1587 attributes.y = allocation.y + rect.y;
1588 attributes.width = rect.width;
1589 attributes.height = rect.height;
1590 priv->arrow_win[i] = gdk_window_new (gtk_widget_get_window (widget),
1594 if (!gtk_widget_is_sensitive (widget))
1595 priv->arrow_state[i] = GTK_STATE_FLAG_INSENSITIVE;
1597 gdk_window_set_user_data (priv->arrow_win[i], widget);
1602 for (i = 0; i < 4; i++)
1603 priv->arrow_win[i] = NULL;
1608 calendar_unrealize_arrows (GtkCalendar *calendar)
1610 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1613 for (i = 0; i < 4; i++)
1615 if (priv->arrow_win[i])
1617 gdk_window_set_user_data (priv->arrow_win[i], NULL);
1618 gdk_window_destroy (priv->arrow_win[i]);
1619 priv->arrow_win[i] = NULL;
1626 calendar_get_inner_border (GtkCalendar *calendar)
1630 gtk_widget_style_get (GTK_WIDGET (calendar),
1631 "inner-border", &inner_border,
1634 return inner_border;
1638 calendar_get_xsep (GtkCalendar *calendar)
1642 gtk_widget_style_get (GTK_WIDGET (calendar),
1643 "horizontal-separation", &xsep,
1650 calendar_get_ysep (GtkCalendar *calendar)
1654 gtk_widget_style_get (GTK_WIDGET (calendar),
1655 "vertical-separation", &ysep,
1662 gtk_calendar_realize (GtkWidget *widget)
1664 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
1665 GdkWindowAttr attributes;
1666 gint attributes_mask;
1667 gint inner_border = calendar_get_inner_border (GTK_CALENDAR (widget));
1668 GtkStyleContext *context;
1669 GtkAllocation allocation;
1670 GtkStateFlags state = 0;
1673 context = gtk_widget_get_style_context (widget);
1674 state = gtk_widget_get_state_flags (widget);
1675 gtk_style_context_get_padding (context, state, &padding);
1677 gtk_widget_get_allocation (widget, &allocation);
1679 GTK_WIDGET_CLASS (gtk_calendar_parent_class)->realize (widget);
1681 attributes.wclass = GDK_INPUT_ONLY;
1682 attributes.window_type = GDK_WINDOW_CHILD;
1683 attributes.event_mask = (gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK
1684 | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
1685 | GDK_POINTER_MOTION_MASK | GDK_LEAVE_NOTIFY_MASK);
1687 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
1688 attributes.x = priv->week_width + padding.left + inner_border;
1690 attributes.x = padding.left + inner_border;
1692 attributes.y = priv->header_h + priv->day_name_h + padding.top + inner_border;
1693 attributes.width = allocation.width - attributes.x - (padding.right + inner_border);
1695 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1696 attributes.width -= priv->week_width;
1698 attributes.height = priv->main_h;
1699 attributes_mask = GDK_WA_X | GDK_WA_Y;
1701 attributes.x += allocation.x;
1702 attributes.y += allocation.y;
1704 priv->main_win = gdk_window_new (gtk_widget_get_window (widget),
1705 &attributes, attributes_mask);
1706 gdk_window_set_user_data (priv->main_win, widget);
1708 calendar_realize_arrows (GTK_CALENDAR (widget));
1712 gtk_calendar_unrealize (GtkWidget *widget)
1714 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
1716 calendar_unrealize_arrows (GTK_CALENDAR (widget));
1720 gdk_window_set_user_data (priv->main_win, NULL);
1721 gdk_window_destroy (priv->main_win);
1722 priv->main_win = NULL;
1725 GTK_WIDGET_CLASS (gtk_calendar_parent_class)->unrealize (widget);
1729 calendar_map_arrows (GtkCalendar *calendar)
1731 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1734 for (i = 0; i < 4; i++)
1736 if (priv->arrow_win[i])
1737 gdk_window_show (priv->arrow_win[i]);
1742 calendar_unmap_arrows (GtkCalendar *calendar)
1744 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1747 for (i = 0; i < 4; i++)
1749 if (priv->arrow_win[i])
1750 gdk_window_hide (priv->arrow_win[i]);
1755 gtk_calendar_map (GtkWidget *widget)
1757 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
1759 GTK_WIDGET_CLASS (gtk_calendar_parent_class)->map (widget);
1761 gdk_window_show (priv->main_win);
1763 calendar_map_arrows (GTK_CALENDAR (widget));
1767 gtk_calendar_unmap (GtkWidget *widget)
1769 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
1771 calendar_unmap_arrows (GTK_CALENDAR (widget));
1773 gdk_window_hide (priv->main_win);
1775 GTK_WIDGET_CLASS (gtk_calendar_parent_class)->unmap (widget);
1779 gtk_calendar_get_detail (GtkCalendar *calendar,
1783 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1786 if (priv->detail_func == NULL)
1790 month = priv->month + priv->day_month[row][column] - MONTH_CURRENT;
1797 else if (month > 11)
1803 return priv->detail_func (calendar,
1805 priv->day[row][column],
1806 priv->detail_func_user_data);
1810 gtk_calendar_query_tooltip (GtkWidget *widget,
1813 gboolean keyboard_mode,
1814 GtkTooltip *tooltip)
1816 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
1817 GtkCalendar *calendar = GTK_CALENDAR (widget);
1818 gchar *detail = NULL;
1819 GdkRectangle day_rect;
1822 col = calendar_column_from_x (calendar, x);
1823 row = calendar_row_from_y (calendar, y);
1825 if (col != -1 && row != -1 &&
1826 (0 != (priv->detail_overflow[row] & (1 << col)) ||
1827 0 == (priv->display_flags & GTK_CALENDAR_SHOW_DETAILS)))
1829 detail = gtk_calendar_get_detail (calendar, row, col);
1830 calendar_day_rectangle (calendar, row, col, &day_rect);
1835 gtk_tooltip_set_tip_area (tooltip, &day_rect);
1836 gtk_tooltip_set_markup (tooltip, detail);
1843 if (GTK_WIDGET_CLASS (gtk_calendar_parent_class)->query_tooltip)
1844 return GTK_WIDGET_CLASS (gtk_calendar_parent_class)->query_tooltip (widget, x, y, keyboard_mode, tooltip);
1850 /****************************************
1851 * Size Request and Allocate *
1852 ****************************************/
1855 gtk_calendar_size_request (GtkWidget *widget,
1856 GtkRequisition *requisition)
1858 GtkCalendar *calendar = GTK_CALENDAR (widget);
1859 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
1860 GtkStyleContext *context;
1861 GtkStateFlags state;
1863 PangoLayout *layout;
1864 PangoRectangle logical_rect;
1868 gint calendar_margin = CALENDAR_MARGIN;
1869 gint header_width, main_width;
1870 gint max_header_height = 0;
1873 gint max_detail_height;
1874 gint inner_border = calendar_get_inner_border (calendar);
1875 gint calendar_ysep = calendar_get_ysep (calendar);
1876 gint calendar_xsep = calendar_get_xsep (calendar);
1878 gtk_widget_style_get (GTK_WIDGET (widget),
1879 "focus-line-width", &focus_width,
1880 "focus-padding", &focus_padding,
1883 layout = gtk_widget_create_pango_layout (widget, NULL);
1886 * Calculate the requisition width for the widget.
1891 if (priv->display_flags & GTK_CALENDAR_SHOW_HEADING)
1893 priv->max_month_width = 0;
1894 for (i = 0; i < 12; i++)
1896 pango_layout_set_text (layout, default_monthname[i], -1);
1897 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1898 priv->max_month_width = MAX (priv->max_month_width,
1899 logical_rect.width + 8);
1900 max_header_height = MAX (max_header_height, logical_rect.height);
1903 priv->max_year_width = 0;
1904 /* Translators: This is a text measurement template.
1905 * Translate it to the widest year text
1907 * If you don't understand this, leave it as "2000"
1909 pango_layout_set_text (layout, C_("year measurement template", "2000"), -1);
1910 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1911 priv->max_year_width = MAX (priv->max_year_width,
1912 logical_rect.width + 8);
1913 max_header_height = MAX (max_header_height, logical_rect.height);
1917 priv->max_month_width = 0;
1918 priv->max_year_width = 0;
1921 if (priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
1922 header_width = (priv->max_month_width
1923 + priv->max_year_width
1926 header_width = (priv->max_month_width
1927 + priv->max_year_width
1928 + 4 * priv->arrow_width + 3 * 3);
1930 /* Mainwindow labels width */
1932 priv->max_day_char_width = 0;
1933 priv->max_day_char_ascent = 0;
1934 priv->max_day_char_descent = 0;
1935 priv->min_day_width = 0;
1937 for (i = 0; i < 9; i++)
1940 g_snprintf (buffer, sizeof (buffer), C_("calendar:day:digits", "%d"), i * 11);
1941 pango_layout_set_text (layout, buffer, -1);
1942 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1943 priv->min_day_width = MAX (priv->min_day_width,
1944 logical_rect.width);
1946 priv->max_day_char_ascent = MAX (priv->max_day_char_ascent,
1947 PANGO_ASCENT (logical_rect));
1948 priv->max_day_char_descent = MAX (priv->max_day_char_descent,
1949 PANGO_DESCENT (logical_rect));
1952 priv->max_label_char_ascent = 0;
1953 priv->max_label_char_descent = 0;
1954 if (priv->display_flags & GTK_CALENDAR_SHOW_DAY_NAMES)
1955 for (i = 0; i < 7; i++)
1957 pango_layout_set_text (layout, default_abbreviated_dayname[i], -1);
1958 pango_layout_line_get_pixel_extents (pango_layout_get_lines_readonly (layout)->data, NULL, &logical_rect);
1960 priv->min_day_width = MAX (priv->min_day_width, logical_rect.width);
1961 priv->max_label_char_ascent = MAX (priv->max_label_char_ascent,
1962 PANGO_ASCENT (logical_rect));
1963 priv->max_label_char_descent = MAX (priv->max_label_char_descent,
1964 PANGO_DESCENT (logical_rect));
1967 priv->max_week_char_width = 0;
1968 if (priv->display_flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
1969 for (i = 0; i < 9; i++)
1972 g_snprintf (buffer, sizeof (buffer), C_("calendar:week:digits", "%d"), i * 11);
1973 pango_layout_set_text (layout, buffer, -1);
1974 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1975 priv->max_week_char_width = MAX (priv->max_week_char_width,
1976 logical_rect.width / 2);
1979 /* Calculate detail extents. Do this as late as possible since
1980 * pango_layout_set_markup is called which alters font settings. */
1981 max_detail_height = 0;
1983 if (priv->detail_func && (priv->display_flags & GTK_CALENDAR_SHOW_DETAILS))
1985 gchar *markup, *tail;
1987 if (priv->detail_width_chars || priv->detail_height_rows)
1989 gint rows = MAX (1, priv->detail_height_rows) - 1;
1990 gsize len = priv->detail_width_chars + rows + 16;
1992 markup = tail = g_alloca (len);
1994 memcpy (tail, "<small>", 7);
1997 memset (tail, 'm', priv->detail_width_chars);
1998 tail += priv->detail_width_chars;
2000 memset (tail, '\n', rows);
2003 memcpy (tail, "</small>", 9);
2006 g_assert (len == (tail - markup));
2008 pango_layout_set_markup (layout, markup, -1);
2009 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2011 if (priv->detail_width_chars)
2012 priv->min_day_width = MAX (priv->min_day_width, logical_rect.width);
2013 if (priv->detail_height_rows)
2014 max_detail_height = MAX (max_detail_height, logical_rect.height);
2017 if (!priv->detail_width_chars || !priv->detail_height_rows)
2018 for (r = 0; r < 6; r++)
2019 for (c = 0; c < 7; c++)
2021 gchar *detail = gtk_calendar_get_detail (calendar, r, c);
2025 markup = g_strconcat ("<small>", detail, "</small>", NULL);
2026 pango_layout_set_markup (layout, markup, -1);
2028 if (priv->detail_width_chars)
2030 pango_layout_set_wrap (layout, PANGO_WRAP_WORD_CHAR);
2031 pango_layout_set_width (layout, PANGO_SCALE * priv->min_day_width);
2034 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2036 if (!priv->detail_width_chars)
2037 priv->min_day_width = MAX (priv->min_day_width, logical_rect.width);
2038 if (!priv->detail_height_rows)
2039 max_detail_height = MAX (max_detail_height, logical_rect.height);
2047 /* We add one to max_day_char_width to be able to make the marked day "bold" */
2048 priv->max_day_char_width = priv->min_day_width / 2 + 1;
2050 main_width = (7 * (priv->min_day_width + (focus_padding + focus_width) * 2) + (DAY_XSEP * 6) + CALENDAR_MARGIN * 2
2051 + (priv->max_week_char_width
2052 ? priv->max_week_char_width * 2 + (focus_padding + focus_width) * 2 + calendar_xsep * 2
2055 context = gtk_widget_get_style_context (widget);
2056 state = gtk_widget_get_state_flags (widget);
2057 gtk_style_context_get_padding (context, state, &padding);
2059 requisition->width = MAX (header_width, main_width + inner_border * 2) + padding.left + padding.right;
2062 * Calculate the requisition height for the widget.
2065 if (priv->display_flags & GTK_CALENDAR_SHOW_HEADING)
2067 priv->header_h = (max_header_height + calendar_ysep * 2);
2074 if (priv->display_flags & GTK_CALENDAR_SHOW_DAY_NAMES)
2076 priv->day_name_h = (priv->max_label_char_ascent
2077 + priv->max_label_char_descent
2078 + 2 * (focus_padding + focus_width) + calendar_margin);
2079 calendar_margin = calendar_ysep;
2083 priv->day_name_h = 0;
2086 priv->main_h = (CALENDAR_MARGIN + calendar_margin
2087 + 6 * (priv->max_day_char_ascent
2088 + priv->max_day_char_descent
2090 + 2 * (focus_padding + focus_width))
2093 height = priv->header_h + priv->day_name_h + priv->main_h;
2095 requisition->height = height + padding.top + padding.bottom + (inner_border * 2);
2097 g_object_unref (layout);
2101 gtk_calendar_get_preferred_width (GtkWidget *widget,
2105 GtkRequisition requisition;
2107 gtk_calendar_size_request (widget, &requisition);
2109 *minimum = *natural = requisition.width;
2113 gtk_calendar_get_preferred_height (GtkWidget *widget,
2117 GtkRequisition requisition;
2119 gtk_calendar_size_request (widget, &requisition);
2121 *minimum = *natural = requisition.height;
2125 gtk_calendar_size_allocate (GtkWidget *widget,
2126 GtkAllocation *allocation)
2128 GtkCalendar *calendar = GTK_CALENDAR (widget);
2129 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
2130 GtkStyleContext *context;
2131 GtkStateFlags state;
2134 gint inner_border = calendar_get_inner_border (calendar);
2135 gint calendar_xsep = calendar_get_xsep (calendar);
2137 context = gtk_widget_get_style_context (widget);
2138 state = gtk_widget_get_state_flags (widget);
2139 gtk_style_context_get_padding (context, state, &padding);
2141 gtk_widget_set_allocation (widget, allocation);
2143 if (priv->display_flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
2145 priv->day_width = (priv->min_day_width
2146 * ((allocation->width - (inner_border * 2) - padding.left - padding.right
2147 - (CALENDAR_MARGIN * 2) - (DAY_XSEP * 6) - calendar_xsep * 2))
2148 / (7 * priv->min_day_width + priv->max_week_char_width * 2));
2149 priv->week_width = ((allocation->width - (inner_border * 2) - padding.left - padding.right
2150 - (CALENDAR_MARGIN * 2) - (DAY_XSEP * 6) - calendar_xsep * 2 )
2151 - priv->day_width * 7 + CALENDAR_MARGIN + calendar_xsep);
2155 priv->day_width = (allocation->width
2156 - (inner_border * 2)
2157 - padding.left - padding.right
2158 - (CALENDAR_MARGIN * 2)
2159 - (DAY_XSEP * 6))/7;
2160 priv->week_width = 0;
2163 if (gtk_widget_get_realized (widget))
2165 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
2166 gdk_window_move_resize (priv->main_win,
2168 + priv->week_width + padding.left + inner_border,
2170 + priv->header_h + priv->day_name_h
2171 + (padding.top + inner_border),
2172 allocation->width - priv->week_width
2173 - (inner_border * 2) - padding.left - padding.right,
2176 gdk_window_move_resize (priv->main_win,
2178 + padding.left + inner_border,
2180 + priv->header_h + priv->day_name_h
2181 + padding.top + inner_border,
2182 allocation->width - priv->week_width
2183 - (inner_border * 2) - padding.left - padding.right,
2186 for (i = 0 ; i < 4 ; i++)
2188 if (priv->arrow_win[i])
2191 calendar_arrow_rectangle (calendar, i, &rect);
2193 gdk_window_move_resize (priv->arrow_win[i],
2194 allocation->x + rect.x,
2195 allocation->y + rect.y,
2196 rect.width, rect.height);
2203 /****************************************
2205 ****************************************/
2208 calendar_paint_header (GtkCalendar *calendar, cairo_t *cr)
2210 GtkWidget *widget = GTK_WIDGET (calendar);
2211 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2212 GtkAllocation allocation;
2213 GtkStyleContext *context;
2214 GtkStateFlags state;
2219 gint max_month_width;
2220 gint max_year_width;
2221 PangoLayout *layout;
2222 PangoRectangle logical_rect;
2228 context = gtk_widget_get_style_context (widget);
2229 state = gtk_widget_get_state_flags (widget);
2230 gtk_style_context_get_padding (context, state, &padding);
2233 cairo_translate (cr, padding.left, padding.top);
2235 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
2236 year_left = priv->year_before;
2238 year_left = !priv->year_before;
2240 gtk_widget_get_allocation (widget, &allocation);
2242 header_width = allocation.width - padding.left - padding.right;
2244 max_month_width = priv->max_month_width;
2245 max_year_width = priv->max_year_width;
2247 gtk_style_context_save (context);
2248 gtk_style_context_add_class (context, GTK_STYLE_CLASS_HEADER);
2250 gtk_render_background (context, cr, 0, 0, header_width, priv->header_h);
2251 gtk_render_frame (context, cr, 0, 0, header_width, priv->header_h);
2253 tmp_time = 1; /* Jan 1 1970, 00:00:01 UTC */
2254 tm = gmtime (&tmp_time);
2255 tm->tm_year = priv->year - 1900;
2257 /* Translators: This dictates how the year is displayed in
2258 * gtkcalendar widget. See strftime() manual for the format.
2259 * Use only ASCII in the translation.
2261 * Also look for the msgid "2000".
2262 * Translate that entry to a year with the widest output of this
2265 * "%Y" is appropriate for most locales.
2267 strftime (buffer, sizeof (buffer), C_("calendar year format", "%Y"), tm);
2268 str = g_locale_to_utf8 (buffer, -1, NULL, NULL, NULL);
2269 layout = gtk_widget_create_pango_layout (widget, str);
2272 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2275 y = (priv->header_h - logical_rect.height) / 2;
2277 /* Draw year and its arrows */
2279 if (priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
2281 x = 3 + (max_year_width - logical_rect.width)/2;
2283 x = header_width - (3 + max_year_width
2284 - (max_year_width - logical_rect.width)/2);
2287 x = 3 + priv->arrow_width + (max_year_width - logical_rect.width)/2;
2289 x = header_width - (3 + priv->arrow_width + max_year_width
2290 - (max_year_width - logical_rect.width)/2);
2292 gtk_render_layout (context, cr, x, y, layout);
2295 g_snprintf (buffer, sizeof (buffer), "%s", default_monthname[priv->month]);
2296 pango_layout_set_text (layout, buffer, -1);
2297 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2299 if (priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
2301 x = header_width - (3 + max_month_width
2302 - (max_month_width - logical_rect.width)/2);
2304 x = 3 + (max_month_width - logical_rect.width) / 2;
2307 x = header_width - (3 + priv->arrow_width + max_month_width
2308 - (max_month_width - logical_rect.width)/2);
2310 x = 3 + priv->arrow_width + (max_month_width - logical_rect.width)/2;
2312 gtk_render_layout (context, cr, x, y, layout);
2313 g_object_unref (layout);
2315 gtk_style_context_restore (context);
2320 calendar_paint_day_names (GtkCalendar *calendar,
2323 GtkWidget *widget = GTK_WIDGET (calendar);
2324 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2325 GtkStyleContext *context;
2326 GtkStateFlags state;
2328 GtkAllocation allocation;
2331 int day_width, cal_width;
2333 PangoLayout *layout;
2334 PangoRectangle logical_rect;
2337 gint calendar_ysep = calendar_get_ysep (calendar);
2338 gint calendar_xsep = calendar_get_xsep (calendar);
2339 gint inner_border = calendar_get_inner_border (calendar);
2341 context = gtk_widget_get_style_context (widget);
2342 state = gtk_widget_get_state_flags (widget);
2343 gtk_style_context_get_padding (context, state, &padding);
2347 cairo_translate (cr,
2348 padding.left + inner_border,
2349 priv->header_h + padding.top + inner_border);
2351 gtk_widget_style_get (GTK_WIDGET (widget),
2352 "focus-line-width", &focus_width,
2353 "focus-padding", &focus_padding,
2356 gtk_widget_get_allocation (widget, &allocation);
2358 day_width = priv->day_width;
2359 cal_width = allocation.width - (inner_border * 2) - padding.left - padding.right;
2360 day_wid_sep = day_width + DAY_XSEP;
2363 * Draw rectangles as inverted background for the labels.
2366 gtk_style_context_save (context);
2367 gtk_style_context_add_class (context, GTK_STYLE_CLASS_HIGHLIGHT);
2369 gtk_render_background (context, cr,
2370 CALENDAR_MARGIN, CALENDAR_MARGIN,
2371 cal_width - CALENDAR_MARGIN * 2,
2372 priv->day_name_h - CALENDAR_MARGIN);
2374 if (priv->display_flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
2375 gtk_render_background (context, cr,
2377 priv->day_name_h - calendar_ysep,
2378 priv->week_width - calendar_ysep - CALENDAR_MARGIN,
2384 layout = gtk_widget_create_pango_layout (widget, NULL);
2386 for (i = 0; i < 7; i++)
2388 if (gtk_widget_get_direction (GTK_WIDGET (calendar)) == GTK_TEXT_DIR_RTL)
2392 day = (day + priv->week_start) % 7;
2393 g_snprintf (buffer, sizeof (buffer), "%s", default_abbreviated_dayname[day]);
2395 pango_layout_set_text (layout, buffer, -1);
2396 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2398 gtk_render_layout (context, cr,
2400 + (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR ?
2401 (priv->week_width + (priv->week_width ? calendar_xsep : 0))
2404 + (day_width - logical_rect.width)/2),
2405 CALENDAR_MARGIN + focus_width + focus_padding + logical_rect.y,
2409 g_object_unref (layout);
2411 gtk_style_context_restore (context);
2416 calendar_paint_week_numbers (GtkCalendar *calendar,
2419 GtkWidget *widget = GTK_WIDGET (calendar);
2420 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2421 GtkStyleContext *context;
2422 GtkStateFlags state;
2424 guint week = 0, year;
2425 gint row, x_loc, y_loc;
2428 PangoLayout *layout;
2429 PangoRectangle logical_rect;
2432 gint calendar_xsep = calendar_get_xsep (calendar);
2433 gint inner_border = calendar_get_inner_border (calendar);
2436 context = gtk_widget_get_style_context (widget);
2437 state = gtk_widget_get_state_flags (widget);
2438 gtk_style_context_get_padding (context, state, &padding);
2442 y = priv->header_h + priv->day_name_h + (padding.top + inner_border);
2443 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
2444 x = padding.left + inner_border;
2446 x = gtk_widget_get_allocated_width (widget) - priv->week_width - (padding.right + inner_border);
2448 gtk_widget_style_get (GTK_WIDGET (widget),
2449 "focus-line-width", &focus_width,
2450 "focus-padding", &focus_padding,
2453 gtk_style_context_save (context);
2454 gtk_style_context_add_class (context, GTK_STYLE_CLASS_HIGHLIGHT);
2456 if (priv->display_flags & GTK_CALENDAR_SHOW_DAY_NAMES)
2457 gtk_render_background (context, cr,
2458 x + CALENDAR_MARGIN, y,
2459 priv->week_width - CALENDAR_MARGIN,
2460 priv->main_h - CALENDAR_MARGIN);
2462 gtk_render_background (context, cr,
2463 x + CALENDAR_MARGIN,
2464 y + CALENDAR_MARGIN,
2465 priv->week_width - CALENDAR_MARGIN,
2466 priv->main_h - 2 * CALENDAR_MARGIN);
2472 layout = gtk_widget_create_pango_layout (widget, NULL);
2473 day_height = calendar_row_height (calendar);
2475 for (row = 0; row < 6; row++)
2480 if (priv->day[row][6] < 15 && row > 3 && priv->month == 11)
2483 result = week_of_year (&week, &year,
2484 ((priv->day[row][6] < 15 && row > 3 ? 1 : 0)
2485 + priv->month) % 12 + 1, priv->day[row][6]);
2486 g_return_if_fail (result);
2488 /* Translators: this defines whether the week numbers should use
2489 * localized digits or the ones used in English (0123...).
2491 * Translate to "%Id" if you want to use localized digits, or
2492 * translate to "%d" otherwise.
2494 * Note that translating this doesn't guarantee that you get localized
2495 * digits. That needs support from your system and locale definition
2498 g_snprintf (buffer, sizeof (buffer), C_("calendar:week:digits", "%d"), week);
2499 pango_layout_set_text (layout, buffer, -1);
2500 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2502 y_loc = calendar_top_y_for_row (calendar, row) + (day_height - logical_rect.height) / 2;
2504 x_loc = x + (priv->week_width
2505 - logical_rect.width
2506 - calendar_xsep - focus_padding - focus_width);
2508 gtk_render_layout (context, cr, x_loc, y_loc, layout);
2511 g_object_unref (layout);
2513 gtk_style_context_restore (context);
2518 calendar_invalidate_day_num (GtkCalendar *calendar,
2521 GtkCalendarPrivate *priv = calendar->priv;
2522 gint r, c, row, col;
2526 for (r = 0; r < 6; r++)
2527 for (c = 0; c < 7; c++)
2528 if (priv->day_month[r][c] == MONTH_CURRENT &&
2529 priv->day[r][c] == day)
2535 g_return_if_fail (row != -1);
2536 g_return_if_fail (col != -1);
2538 calendar_invalidate_day (calendar, row, col);
2542 calendar_invalidate_day (GtkCalendar *calendar,
2546 GdkRectangle day_rect;
2547 GtkAllocation allocation;
2549 gtk_widget_get_allocation (GTK_WIDGET (calendar), &allocation);
2550 calendar_day_rectangle (calendar, row, col, &day_rect);
2551 gtk_widget_queue_draw_area (GTK_WIDGET (calendar),
2552 allocation.x + day_rect.x,
2553 allocation.y + day_rect.y,
2554 day_rect.width, day_rect.height);
2558 is_color_attribute (PangoAttribute *attribute,
2561 return (attribute->klass->type == PANGO_ATTR_FOREGROUND ||
2562 attribute->klass->type == PANGO_ATTR_BACKGROUND);
2566 calendar_paint_day (GtkCalendar *calendar,
2571 GtkWidget *widget = GTK_WIDGET (calendar);
2572 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2573 GtkStyleContext *context;
2574 GtkStateFlags state = 0;
2579 GdkRectangle day_rect;
2581 PangoLayout *layout;
2582 PangoRectangle logical_rect;
2583 gboolean overflow = FALSE;
2584 gboolean show_details;
2586 g_return_if_fail (row < 6);
2587 g_return_if_fail (col < 7);
2589 context = gtk_widget_get_style_context (widget);
2591 day = priv->day[row][col];
2592 show_details = (priv->display_flags & GTK_CALENDAR_SHOW_DETAILS);
2594 calendar_day_rectangle (calendar, row, col, &day_rect);
2596 gtk_style_context_save (context);
2598 if (!gtk_widget_get_sensitive (widget))
2599 state |= GTK_STATE_FLAG_INSENSITIVE;
2602 if (gtk_widget_has_focus (widget))
2603 state |= GTK_STATE_FLAG_FOCUSED;
2605 if (priv->day_month[row][col] == MONTH_PREV ||
2606 priv->day_month[row][col] == MONTH_NEXT)
2607 state |= GTK_STATE_FLAG_INCONSISTENT;
2610 if (priv->marked_date[day-1])
2611 state |= GTK_STATE_FLAG_ACTIVE;
2613 if (priv->selected_day == day)
2615 state |= GTK_STATE_FLAG_SELECTED;
2617 gtk_style_context_set_state (context, state);
2618 gtk_render_background (context, cr,
2619 day_rect.x, day_rect.y,
2620 day_rect.width, day_rect.height);
2625 gtk_style_context_set_state (context, state);
2627 /* Translators: this defines whether the day numbers should use
2628 * localized digits or the ones used in English (0123...).
2630 * Translate to "%Id" if you want to use localized digits, or
2631 * translate to "%d" otherwise.
2633 * Note that translating this doesn't guarantee that you get localized
2634 * digits. That needs support from your system and locale definition
2637 g_snprintf (buffer, sizeof (buffer), C_("calendar:day:digits", "%d"), day);
2639 /* Get extra information to show, if any: */
2641 detail = gtk_calendar_get_detail (calendar, row, col);
2643 layout = gtk_widget_create_pango_layout (widget, buffer);
2644 pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
2645 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2647 x_loc = day_rect.x + (day_rect.width - logical_rect.width) / 2;
2650 gtk_render_layout (context, cr, x_loc, y_loc, layout);
2652 if (priv->day_month[row][col] == MONTH_CURRENT &&
2653 (priv->marked_date[day-1] || (detail && !show_details)))
2654 gtk_render_layout (context, cr, x_loc - 1, y_loc, layout);
2656 y_loc += priv->max_day_char_descent;
2658 if (priv->detail_func && show_details)
2664 gtk_style_context_get_color (context, state, &color);
2665 gdk_cairo_set_source_rgba (cr, &color);
2667 cairo_set_line_width (cr, 1);
2668 cairo_move_to (cr, day_rect.x + 2, y_loc + 0.5);
2669 cairo_line_to (cr, day_rect.x + day_rect.width - 2, y_loc + 0.5);
2677 if (detail && show_details)
2679 gchar *markup = g_strconcat ("<small>", detail, "</small>", NULL);
2680 pango_layout_set_markup (layout, markup, -1);
2683 if (day == priv->selected_day)
2685 /* Stripping colors as they conflict with selection marking. */
2687 PangoAttrList *attrs = pango_layout_get_attributes (layout);
2688 PangoAttrList *colors = NULL;
2691 colors = pango_attr_list_filter (attrs, is_color_attribute, NULL);
2693 pango_attr_list_unref (colors);
2696 pango_layout_set_wrap (layout, PANGO_WRAP_WORD_CHAR);
2697 pango_layout_set_width (layout, PANGO_SCALE * day_rect.width);
2699 if (priv->detail_height_rows)
2701 gint dy = day_rect.height - (y_loc - day_rect.y);
2702 pango_layout_set_height (layout, PANGO_SCALE * dy);
2703 pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_END);
2706 cairo_move_to (cr, day_rect.x, y_loc);
2707 pango_cairo_show_layout (cr, layout);
2710 if (gtk_widget_has_focus (widget)
2711 && priv->focus_row == row && priv->focus_col == col)
2712 gtk_render_focus (context, cr,
2713 day_rect.x, day_rect.y,
2714 day_rect.width, day_rect.height);
2717 priv->detail_overflow[row] |= (1 << col);
2719 priv->detail_overflow[row] &= ~(1 << col);
2721 gtk_style_context_restore (context);
2722 g_object_unref (layout);
2727 calendar_paint_main (GtkCalendar *calendar,
2734 for (col = 0; col < 7; col++)
2735 for (row = 0; row < 6; row++)
2736 calendar_paint_day (calendar, cr, row, col);
2742 calendar_invalidate_arrow (GtkCalendar *calendar,
2745 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2747 if (priv->display_flags & GTK_CALENDAR_SHOW_HEADING &&
2748 priv->arrow_win[arrow])
2751 GtkAllocation allocation;
2753 calendar_arrow_rectangle (calendar, arrow, &rect);
2754 gtk_widget_get_allocation (GTK_WIDGET (calendar), &allocation);
2755 gtk_widget_queue_draw_area (GTK_WIDGET (calendar),
2756 allocation.x + rect.x,
2757 allocation.y + rect.y,
2758 rect.width, rect.height);
2763 calendar_paint_arrow (GtkCalendar *calendar,
2767 GtkWidget *widget = GTK_WIDGET (calendar);
2768 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
2769 GtkStyleContext *context;
2770 GtkStateFlags state;
2774 if (!priv->arrow_win[arrow])
2777 calendar_arrow_rectangle (calendar, arrow, &rect);
2781 context = gtk_widget_get_style_context (widget);
2782 state = priv->arrow_state[arrow];
2784 gtk_style_context_save (context);
2785 gtk_style_context_set_state (context, state);
2786 gtk_style_context_add_class (context, GTK_STYLE_CLASS_BUTTON);
2788 gtk_render_background (context, cr,
2790 rect.width, rect.height);
2792 if (arrow == ARROW_MONTH_LEFT || arrow == ARROW_YEAR_LEFT)
2793 angle = 3 * (G_PI / 2);
2797 gtk_render_arrow (context, cr, angle,
2798 rect.x + (rect.width - 8) / 2,
2799 rect.y + (rect.height - 8) / 2,
2802 gtk_style_context_restore (context);
2807 gtk_calendar_draw (GtkWidget *widget,
2810 GtkCalendar *calendar = GTK_CALENDAR (widget);
2811 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
2814 if (gtk_cairo_should_draw_window (cr, gtk_widget_get_window (widget)))
2816 GtkStyleContext *context;
2818 context = gtk_widget_get_style_context (widget);
2820 gtk_style_context_save (context);
2821 gtk_style_context_add_class (context, GTK_STYLE_CLASS_VIEW);
2823 gtk_render_background (context, cr, 0, 0,
2824 gtk_widget_get_allocated_width (widget),
2825 gtk_widget_get_allocated_height (widget));
2826 gtk_render_frame (context, cr, 0, 0,
2827 gtk_widget_get_allocated_width (widget),
2828 gtk_widget_get_allocated_height (widget));
2830 gtk_style_context_restore (context);
2833 calendar_paint_main (calendar, cr);
2835 if (priv->display_flags & GTK_CALENDAR_SHOW_HEADING)
2837 calendar_paint_header (calendar, cr);
2838 for (i = 0; i < 4; i++)
2839 calendar_paint_arrow (calendar, cr, i);
2842 if (priv->display_flags & GTK_CALENDAR_SHOW_DAY_NAMES)
2843 calendar_paint_day_names (calendar, cr);
2845 if (priv->display_flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
2846 calendar_paint_week_numbers (calendar, cr);
2852 /****************************************
2854 ****************************************/
2857 calendar_arrow_action (GtkCalendar *calendar,
2862 case ARROW_YEAR_LEFT:
2863 calendar_set_year_prev (calendar);
2865 case ARROW_YEAR_RIGHT:
2866 calendar_set_year_next (calendar);
2868 case ARROW_MONTH_LEFT:
2869 calendar_set_month_prev (calendar);
2871 case ARROW_MONTH_RIGHT:
2872 calendar_set_month_next (calendar);
2880 calendar_timer (gpointer data)
2882 GtkCalendar *calendar = data;
2883 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2884 gboolean retval = FALSE;
2888 calendar_arrow_action (calendar, priv->click_child);
2890 if (priv->need_timer)
2892 GtkSettings *settings;
2895 settings = gtk_widget_get_settings (GTK_WIDGET (calendar));
2896 g_object_get (settings, "gtk-timeout-repeat", &timeout, NULL);
2898 priv->need_timer = FALSE;
2899 priv->timer = gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT_IDLE,
2900 timeout * SCROLL_DELAY_FACTOR,
2901 (GSourceFunc) calendar_timer,
2902 (gpointer) calendar, NULL);
2912 calendar_start_spinning (GtkCalendar *calendar,
2915 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2917 priv->click_child = click_child;
2921 GtkSettings *settings;
2924 settings = gtk_widget_get_settings (GTK_WIDGET (calendar));
2925 g_object_get (settings, "gtk-timeout-initial", &timeout, NULL);
2927 priv->need_timer = TRUE;
2928 priv->timer = gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT_IDLE,
2930 (GSourceFunc) calendar_timer,
2931 (gpointer) calendar, NULL);
2936 calendar_stop_spinning (GtkCalendar *calendar)
2938 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2942 g_source_remove (priv->timer);
2944 priv->need_timer = FALSE;
2949 calendar_main_button_press (GtkCalendar *calendar,
2950 GdkEventButton *event)
2952 GtkWidget *widget = GTK_WIDGET (calendar);
2953 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2959 GtkAllocation allocation;
2961 x = (gint) (event->x);
2962 y = (gint) (event->y);
2964 gdk_window_get_position (priv->main_win, &win_x, &win_y);
2965 gtk_widget_get_allocation (widget, &allocation);
2967 row = calendar_row_from_y (calendar, y + win_y - allocation.y);
2968 col = calendar_column_from_x (calendar, x + win_x - allocation.x);
2970 /* If row or column isn't found, just return. */
2971 if (row == -1 || col == -1)
2974 day_month = priv->day_month[row][col];
2976 if (event->type == GDK_BUTTON_PRESS)
2978 day = priv->day[row][col];
2980 if (day_month == MONTH_PREV)
2981 calendar_set_month_prev (calendar);
2982 else if (day_month == MONTH_NEXT)
2983 calendar_set_month_next (calendar);
2985 if (!gtk_widget_has_focus (widget))
2986 gtk_widget_grab_focus (widget);
2988 if (event->button == 1)
2991 priv->drag_start_x = x;
2992 priv->drag_start_y = y;
2995 calendar_select_and_focus_day (calendar, day);
2997 else if (event->type == GDK_2BUTTON_PRESS)
3000 if (day_month == MONTH_CURRENT)
3001 g_signal_emit (calendar,
3002 gtk_calendar_signals[DAY_SELECTED_DOUBLE_CLICK_SIGNAL],
3008 gtk_calendar_button_press (GtkWidget *widget,
3009 GdkEventButton *event)
3011 GtkCalendar *calendar = GTK_CALENDAR (widget);
3012 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3015 if (event->window == priv->main_win)
3016 calendar_main_button_press (calendar, event);
3018 if (!gtk_widget_has_focus (widget))
3019 gtk_widget_grab_focus (widget);
3021 for (arrow = ARROW_YEAR_LEFT; arrow <= ARROW_MONTH_RIGHT; arrow++)
3023 if (event->window == priv->arrow_win[arrow])
3026 /* only call the action on single click, not double */
3027 if (event->type == GDK_BUTTON_PRESS)
3029 if (event->button == 1)
3030 calendar_start_spinning (calendar, arrow);
3032 calendar_arrow_action (calendar, arrow);
3043 gtk_calendar_button_release (GtkWidget *widget,
3044 GdkEventButton *event)
3046 GtkCalendar *calendar = GTK_CALENDAR (widget);
3047 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3049 if (event->button == 1)
3051 calendar_stop_spinning (calendar);
3061 gtk_calendar_motion_notify (GtkWidget *widget,
3062 GdkEventMotion *event)
3064 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3068 if (gtk_drag_check_threshold (widget,
3069 priv->drag_start_x, priv->drag_start_y,
3070 event->x, event->y))
3072 GdkDragContext *context;
3073 GtkTargetList *target_list = gtk_target_list_new (NULL, 0);
3074 gtk_target_list_add_text_targets (target_list, 0);
3075 context = gtk_drag_begin (widget, target_list, GDK_ACTION_COPY,
3076 1, (GdkEvent *)event);
3079 gtk_target_list_unref (target_list);
3080 gtk_drag_set_icon_default (context);
3088 gtk_calendar_enter_notify (GtkWidget *widget,
3089 GdkEventCrossing *event)
3091 GtkCalendar *calendar = GTK_CALENDAR (widget);
3092 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3094 if (event->window == priv->arrow_win[ARROW_MONTH_LEFT])
3096 priv->arrow_state[ARROW_MONTH_LEFT] |= GTK_STATE_FLAG_PRELIGHT;
3097 calendar_invalidate_arrow (calendar, ARROW_MONTH_LEFT);
3100 if (event->window == priv->arrow_win[ARROW_MONTH_RIGHT])
3102 priv->arrow_state[ARROW_MONTH_RIGHT] |= GTK_STATE_FLAG_PRELIGHT;
3103 calendar_invalidate_arrow (calendar, ARROW_MONTH_RIGHT);
3106 if (event->window == priv->arrow_win[ARROW_YEAR_LEFT])
3108 priv->arrow_state[ARROW_YEAR_LEFT] |= GTK_STATE_FLAG_PRELIGHT;
3109 calendar_invalidate_arrow (calendar, ARROW_YEAR_LEFT);
3112 if (event->window == priv->arrow_win[ARROW_YEAR_RIGHT])
3114 priv->arrow_state[ARROW_YEAR_RIGHT] |= GTK_STATE_FLAG_PRELIGHT;
3115 calendar_invalidate_arrow (calendar, ARROW_YEAR_RIGHT);
3122 gtk_calendar_leave_notify (GtkWidget *widget,
3123 GdkEventCrossing *event)
3125 GtkCalendar *calendar = GTK_CALENDAR (widget);
3126 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3128 if (event->window == priv->arrow_win[ARROW_MONTH_LEFT])
3130 priv->arrow_state[ARROW_MONTH_LEFT] &= ~(GTK_STATE_FLAG_PRELIGHT);
3131 calendar_invalidate_arrow (calendar, ARROW_MONTH_LEFT);
3134 if (event->window == priv->arrow_win[ARROW_MONTH_RIGHT])
3136 priv->arrow_state[ARROW_MONTH_RIGHT] &= ~(GTK_STATE_FLAG_PRELIGHT);
3137 calendar_invalidate_arrow (calendar, ARROW_MONTH_RIGHT);
3140 if (event->window == priv->arrow_win[ARROW_YEAR_LEFT])
3142 priv->arrow_state[ARROW_YEAR_LEFT] &= ~(GTK_STATE_FLAG_PRELIGHT);
3143 calendar_invalidate_arrow (calendar, ARROW_YEAR_LEFT);
3146 if (event->window == priv->arrow_win[ARROW_YEAR_RIGHT])
3148 priv->arrow_state[ARROW_YEAR_RIGHT] &= ~(GTK_STATE_FLAG_PRELIGHT);
3149 calendar_invalidate_arrow (calendar, ARROW_YEAR_RIGHT);
3156 gtk_calendar_scroll (GtkWidget *widget,
3157 GdkEventScroll *event)
3159 GtkCalendar *calendar = GTK_CALENDAR (widget);
3161 if (event->direction == GDK_SCROLL_UP)
3163 if (!gtk_widget_has_focus (widget))
3164 gtk_widget_grab_focus (widget);
3165 calendar_set_month_prev (calendar);
3167 else if (event->direction == GDK_SCROLL_DOWN)
3169 if (!gtk_widget_has_focus (widget))
3170 gtk_widget_grab_focus (widget);
3171 calendar_set_month_next (calendar);
3180 /****************************************
3182 ****************************************/
3185 move_focus (GtkCalendar *calendar,
3188 GtkCalendarPrivate *priv = calendar->priv;
3189 GtkTextDirection text_dir = gtk_widget_get_direction (GTK_WIDGET (calendar));
3191 if ((text_dir == GTK_TEXT_DIR_LTR && direction == -1) ||
3192 (text_dir == GTK_TEXT_DIR_RTL && direction == 1))
3194 if (priv->focus_col > 0)
3196 else if (priv->focus_row > 0)
3198 priv->focus_col = 6;
3202 if (priv->focus_col < 0)
3203 priv->focus_col = 6;
3204 if (priv->focus_row < 0)
3205 priv->focus_row = 5;
3209 if (priv->focus_col < 6)
3211 else if (priv->focus_row < 5)
3213 priv->focus_col = 0;
3217 if (priv->focus_col < 0)
3218 priv->focus_col = 0;
3219 if (priv->focus_row < 0)
3220 priv->focus_row = 0;
3225 gtk_calendar_key_press (GtkWidget *widget,
3228 GtkCalendar *calendar = GTK_CALENDAR (widget);
3229 GtkCalendarPrivate *priv = calendar->priv;
3237 old_focus_row = priv->focus_row;
3238 old_focus_col = priv->focus_col;
3240 switch (event->keyval)
3242 case GDK_KEY_KP_Left:
3245 if (event->state & GDK_CONTROL_MASK)
3246 calendar_set_month_prev (calendar);
3249 move_focus (calendar, -1);
3250 calendar_invalidate_day (calendar, old_focus_row, old_focus_col);
3251 calendar_invalidate_day (calendar, priv->focus_row,
3255 case GDK_KEY_KP_Right:
3258 if (event->state & GDK_CONTROL_MASK)
3259 calendar_set_month_next (calendar);
3262 move_focus (calendar, 1);
3263 calendar_invalidate_day (calendar, old_focus_row, old_focus_col);
3264 calendar_invalidate_day (calendar, priv->focus_row,
3271 if (event->state & GDK_CONTROL_MASK)
3272 calendar_set_year_prev (calendar);
3275 if (priv->focus_row > 0)
3277 if (priv->focus_row < 0)
3278 priv->focus_row = 5;
3279 if (priv->focus_col < 0)
3280 priv->focus_col = 6;
3281 calendar_invalidate_day (calendar, old_focus_row, old_focus_col);
3282 calendar_invalidate_day (calendar, priv->focus_row,
3286 case GDK_KEY_KP_Down:
3289 if (event->state & GDK_CONTROL_MASK)
3290 calendar_set_year_next (calendar);
3293 if (priv->focus_row < 5)
3295 if (priv->focus_col < 0)
3296 priv->focus_col = 0;
3297 calendar_invalidate_day (calendar, old_focus_row, old_focus_col);
3298 calendar_invalidate_day (calendar, priv->focus_row,
3302 case GDK_KEY_KP_Space:
3304 row = priv->focus_row;
3305 col = priv->focus_col;
3307 if (row > -1 && col > -1)
3311 day = priv->day[row][col];
3312 if (priv->day_month[row][col] == MONTH_PREV)
3313 calendar_set_month_prev (calendar);
3314 else if (priv->day_month[row][col] == MONTH_NEXT)
3315 calendar_set_month_next (calendar);
3317 calendar_select_and_focus_day (calendar, day);
3325 /****************************************
3326 * Misc widget methods *
3327 ****************************************/
3330 gtk_calendar_state_flags_changed (GtkWidget *widget,
3331 GtkStateFlags previous_state)
3333 GtkCalendar *calendar = GTK_CALENDAR (widget);
3334 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3337 if (!gtk_widget_is_sensitive (widget))
3340 calendar_stop_spinning (calendar);
3343 for (i = 0; i < 4; i++)
3344 if (gtk_widget_is_sensitive (widget))
3345 priv->arrow_state[i] &= ~(GTK_STATE_FLAG_INSENSITIVE);
3347 priv->arrow_state[i] |= GTK_STATE_FLAG_INSENSITIVE;
3351 gtk_calendar_grab_notify (GtkWidget *widget,
3352 gboolean was_grabbed)
3355 calendar_stop_spinning (GTK_CALENDAR (widget));
3359 gtk_calendar_focus_out (GtkWidget *widget,
3360 GdkEventFocus *event)
3362 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3363 GtkCalendar *calendar = GTK_CALENDAR (widget);
3365 calendar_queue_refresh (calendar);
3366 calendar_stop_spinning (calendar);
3374 /****************************************
3376 ****************************************/
3379 gtk_calendar_drag_data_get (GtkWidget *widget,
3380 GdkDragContext *context,
3381 GtkSelectionData *selection_data,
3385 GtkCalendar *calendar = GTK_CALENDAR (widget);
3386 GtkCalendarPrivate *priv = calendar->priv;
3391 date = g_date_new_dmy (priv->selected_day, priv->month + 1, priv->year);
3392 len = g_date_strftime (str, 127, "%x", date);
3393 gtk_selection_data_set_text (selection_data, str, len);
3398 /* Get/set whether drag_motion requested the drag data and
3399 * drag_data_received should thus not actually insert the data,
3400 * since the data doesn't result from a drop.
3403 set_status_pending (GdkDragContext *context,
3404 GdkDragAction suggested_action)
3406 g_object_set_data (G_OBJECT (context),
3407 I_("gtk-calendar-status-pending"),
3408 GINT_TO_POINTER (suggested_action));
3411 static GdkDragAction
3412 get_status_pending (GdkDragContext *context)
3414 return GPOINTER_TO_INT (g_object_get_data (G_OBJECT (context),
3415 "gtk-calendar-status-pending"));
3419 gtk_calendar_drag_leave (GtkWidget *widget,
3420 GdkDragContext *context,
3423 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3425 priv->drag_highlight = 0;
3426 gtk_drag_unhighlight (widget);
3431 gtk_calendar_drag_motion (GtkWidget *widget,
3432 GdkDragContext *context,
3437 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3440 if (!priv->drag_highlight)
3442 priv->drag_highlight = 1;
3443 gtk_drag_highlight (widget);
3446 target = gtk_drag_dest_find_target (widget, context, NULL);
3447 if (target == GDK_NONE || gdk_drag_context_get_suggested_action (context) == 0)
3448 gdk_drag_status (context, 0, time);
3451 set_status_pending (context, gdk_drag_context_get_suggested_action (context));
3452 gtk_drag_get_data (widget, context, target, time);
3459 gtk_calendar_drag_drop (GtkWidget *widget,
3460 GdkDragContext *context,
3467 target = gtk_drag_dest_find_target (widget, context, NULL);
3468 if (target != GDK_NONE)
3470 gtk_drag_get_data (widget, context,
3480 gtk_calendar_drag_data_received (GtkWidget *widget,
3481 GdkDragContext *context,
3484 GtkSelectionData *selection_data,
3488 GtkCalendar *calendar = GTK_CALENDAR (widget);
3489 GtkCalendarPrivate *priv = calendar->priv;
3490 guint day, month, year;
3493 GdkDragAction suggested_action;
3495 suggested_action = get_status_pending (context);
3497 if (suggested_action)
3499 set_status_pending (context, 0);
3501 /* We are getting this data due to a request in drag_motion,
3502 * rather than due to a request in drag_drop, so we are just
3503 * supposed to call drag_status, not actually paste in the
3506 str = (gchar*) gtk_selection_data_get_text (selection_data);
3510 date = g_date_new ();
3511 g_date_set_parse (date, str);
3512 if (!g_date_valid (date))
3513 suggested_action = 0;
3518 suggested_action = 0;
3520 gdk_drag_status (context, suggested_action, time);
3525 date = g_date_new ();
3526 str = (gchar*) gtk_selection_data_get_text (selection_data);
3529 g_date_set_parse (date, str);
3533 if (!g_date_valid (date))
3535 g_warning ("Received invalid date data\n");
3537 gtk_drag_finish (context, FALSE, FALSE, time);
3541 day = g_date_get_day (date);
3542 month = g_date_get_month (date);
3543 year = g_date_get_year (date);
3546 gtk_drag_finish (context, TRUE, FALSE, time);
3549 g_object_freeze_notify (G_OBJECT (calendar));
3550 if (!(priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
3551 && (priv->display_flags & GTK_CALENDAR_SHOW_HEADING))
3552 gtk_calendar_select_month (calendar, month - 1, year);
3553 gtk_calendar_select_day (calendar, day);
3554 g_object_thaw_notify (G_OBJECT (calendar));
3558 /****************************************
3560 ****************************************/
3565 * Creates a new calendar, with the current date being selected.
3567 * Return value: a newly #GtkCalendar widget
3570 gtk_calendar_new (void)
3572 return g_object_new (GTK_TYPE_CALENDAR, NULL);
3576 * gtk_calendar_get_display_options:
3577 * @calendar: a #GtkCalendar
3579 * Returns the current display options of @calendar.
3581 * Return value: the display options.
3585 GtkCalendarDisplayOptions
3586 gtk_calendar_get_display_options (GtkCalendar *calendar)
3588 g_return_val_if_fail (GTK_IS_CALENDAR (calendar), 0);
3590 return calendar->priv->display_flags;
3594 * gtk_calendar_set_display_options:
3595 * @calendar: a #GtkCalendar
3596 * @flags: the display options to set
3598 * Sets display options (whether to display the heading and the month
3604 gtk_calendar_set_display_options (GtkCalendar *calendar,
3605 GtkCalendarDisplayOptions flags)
3607 GtkWidget *widget = GTK_WIDGET (calendar);
3608 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
3610 GtkCalendarDisplayOptions old_flags;
3612 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3614 old_flags = priv->display_flags;
3616 if (gtk_widget_get_realized (widget))
3618 if ((flags ^ priv->display_flags) & GTK_CALENDAR_NO_MONTH_CHANGE)
3621 if (! (flags & GTK_CALENDAR_NO_MONTH_CHANGE)
3622 && (priv->display_flags & GTK_CALENDAR_SHOW_HEADING))
3624 priv->display_flags &= ~GTK_CALENDAR_NO_MONTH_CHANGE;
3625 calendar_realize_arrows (calendar);
3626 if (gtk_widget_get_mapped (widget))
3627 calendar_map_arrows (calendar);
3631 calendar_unrealize_arrows (calendar);
3635 if ((flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_HEADING)
3639 if (flags & GTK_CALENDAR_SHOW_HEADING)
3641 priv->display_flags |= GTK_CALENDAR_SHOW_HEADING;
3642 calendar_realize_arrows (calendar);
3643 if (gtk_widget_get_mapped (widget))
3644 calendar_map_arrows (calendar);
3648 calendar_unrealize_arrows (calendar);
3652 if ((flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_DAY_NAMES)
3656 if (flags & GTK_CALENDAR_SHOW_DAY_NAMES)
3657 priv->display_flags |= GTK_CALENDAR_SHOW_DAY_NAMES;
3660 if ((flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
3664 if (flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
3665 priv->display_flags |= GTK_CALENDAR_SHOW_WEEK_NUMBERS;
3668 if ((flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_DETAILS)
3671 priv->display_flags = flags;
3673 gtk_widget_queue_resize (GTK_WIDGET (calendar));
3676 priv->display_flags = flags;
3678 g_object_freeze_notify (G_OBJECT (calendar));
3679 if ((old_flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_HEADING)
3680 g_object_notify (G_OBJECT (calendar), "show-heading");
3681 if ((old_flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_DAY_NAMES)
3682 g_object_notify (G_OBJECT (calendar), "show-day-names");
3683 if ((old_flags ^ priv->display_flags) & GTK_CALENDAR_NO_MONTH_CHANGE)
3684 g_object_notify (G_OBJECT (calendar), "no-month-change");
3685 if ((old_flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
3686 g_object_notify (G_OBJECT (calendar), "show-week-numbers");
3687 g_object_thaw_notify (G_OBJECT (calendar));
3691 * gtk_calendar_select_month:
3692 * @calendar: a #GtkCalendar
3693 * @month: a month number between 0 and 11.
3694 * @year: the year the month is in.
3696 * Shifts the calendar to a different month.
3699 gtk_calendar_select_month (GtkCalendar *calendar,
3703 GtkCalendarPrivate *priv;
3705 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3706 g_return_if_fail (month <= 11);
3708 priv = calendar->priv;
3710 priv->month = month;
3713 calendar_compute_days (calendar);
3714 calendar_queue_refresh (calendar);
3716 g_object_freeze_notify (G_OBJECT (calendar));
3717 g_object_notify (G_OBJECT (calendar), "month");
3718 g_object_notify (G_OBJECT (calendar), "year");
3719 g_object_thaw_notify (G_OBJECT (calendar));
3721 g_signal_emit (calendar,
3722 gtk_calendar_signals[MONTH_CHANGED_SIGNAL],
3727 * gtk_calendar_select_day:
3728 * @calendar: a #GtkCalendar.
3729 * @day: the day number between 1 and 31, or 0 to unselect
3730 * the currently selected day.
3732 * Selects a day from the current month.
3735 gtk_calendar_select_day (GtkCalendar *calendar,
3738 GtkCalendarPrivate *priv;
3740 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3741 g_return_if_fail (day <= 31);
3743 priv = calendar->priv;
3745 /* Deselect the old day */
3746 if (priv->selected_day > 0)
3750 selected_day = priv->selected_day;
3751 priv->selected_day = 0;
3752 if (gtk_widget_is_drawable (GTK_WIDGET (calendar)))
3753 calendar_invalidate_day_num (calendar, selected_day);
3756 priv->selected_day = day;
3758 /* Select the new day */
3761 if (gtk_widget_is_drawable (GTK_WIDGET (calendar)))
3762 calendar_invalidate_day_num (calendar, day);
3765 g_object_notify (G_OBJECT (calendar), "day");
3767 g_signal_emit (calendar,
3768 gtk_calendar_signals[DAY_SELECTED_SIGNAL],
3773 * gtk_calendar_clear_marks:
3774 * @calendar: a #GtkCalendar
3776 * Remove all visual markers.
3779 gtk_calendar_clear_marks (GtkCalendar *calendar)
3781 GtkCalendarPrivate *priv;
3784 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3786 priv = calendar->priv;
3788 for (day = 0; day < 31; day++)
3790 priv->marked_date[day] = FALSE;
3793 priv->num_marked_dates = 0;
3794 calendar_queue_refresh (calendar);
3798 * gtk_calendar_mark_day:
3799 * @calendar: a #GtkCalendar
3800 * @day: the day number to mark between 1 and 31.
3802 * Places a visual marker on a particular day.
3805 gtk_calendar_mark_day (GtkCalendar *calendar,
3808 GtkCalendarPrivate *priv;
3810 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3812 priv = calendar->priv;
3814 if (day >= 1 && day <= 31 && !priv->marked_date[day-1])
3816 priv->marked_date[day - 1] = TRUE;
3817 priv->num_marked_dates++;
3818 calendar_invalidate_day_num (calendar, day);
3823 * gtk_calendar_get_day_is_marked:
3824 * @calendar: a #GtkCalendar
3825 * @day: the day number between 1 and 31.
3827 * Returns if the @day of the @calendar is already marked.
3829 * Returns: whether the day is marked.
3834 gtk_calendar_get_day_is_marked (GtkCalendar *calendar,
3837 GtkCalendarPrivate *priv;
3839 g_return_val_if_fail (GTK_IS_CALENDAR (calendar), FALSE);
3841 priv = calendar->priv;
3843 if (day >= 1 && day <= 31)
3844 return priv->marked_date[day - 1];
3850 * gtk_calendar_unmark_day:
3851 * @calendar: a #GtkCalendar.
3852 * @day: the day number to unmark between 1 and 31.
3854 * Removes the visual marker from a particular day.
3857 gtk_calendar_unmark_day (GtkCalendar *calendar,
3860 GtkCalendarPrivate *priv;
3862 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3864 priv = calendar->priv;
3866 if (day >= 1 && day <= 31 && priv->marked_date[day-1])
3868 priv->marked_date[day - 1] = FALSE;
3869 priv->num_marked_dates--;
3870 calendar_invalidate_day_num (calendar, day);
3875 * gtk_calendar_get_date:
3876 * @calendar: a #GtkCalendar
3877 * @year: (out) (allow-none): location to store the year as a decimal
3878 * number (e.g. 2011), or %NULL
3879 * @month: (out) (allow-none): location to store the month number
3880 * (between 0 and 11), or %NULL
3881 * @day: (out) (allow-none): location to store the day number (between
3882 * 1 and 31), or %NULL
3884 * Obtains the selected date from a #GtkCalendar.
3887 gtk_calendar_get_date (GtkCalendar *calendar,
3892 GtkCalendarPrivate *priv;
3894 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3896 priv = calendar->priv;
3902 *month = priv->month;
3905 *day = priv->selected_day;
3909 * gtk_calendar_set_detail_func:
3910 * @calendar: a #GtkCalendar.
3911 * @func: a function providing details for each day.
3912 * @data: data to pass to @func invokations.
3913 * @destroy: a function for releasing @data.
3915 * Installs a function which provides Pango markup with detail information
3916 * for each day. Examples for such details are holidays or appointments. That
3917 * information is shown below each day when #GtkCalendar:show-details is set.
3918 * A tooltip containing with full detail information is provided, if the entire
3919 * text should not fit into the details area, or if #GtkCalendar:show-details
3922 * The size of the details area can be restricted by setting the
3923 * #GtkCalendar:detail-width-chars and #GtkCalendar:detail-height-rows
3929 gtk_calendar_set_detail_func (GtkCalendar *calendar,
3930 GtkCalendarDetailFunc func,
3932 GDestroyNotify destroy)
3934 GtkCalendarPrivate *priv;
3936 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3938 priv = GTK_CALENDAR_GET_PRIVATE (calendar);
3940 if (priv->detail_func_destroy)
3941 priv->detail_func_destroy (priv->detail_func_user_data);
3943 priv->detail_func = func;
3944 priv->detail_func_user_data = data;
3945 priv->detail_func_destroy = destroy;
3947 gtk_widget_set_has_tooltip (GTK_WIDGET (calendar),
3948 NULL != priv->detail_func);
3949 gtk_widget_queue_resize (GTK_WIDGET (calendar));
3953 * gtk_calendar_set_detail_width_chars:
3954 * @calendar: a #GtkCalendar.
3955 * @chars: detail width in characters.
3957 * Updates the width of detail cells.
3958 * See #GtkCalendar:detail-width-chars.
3963 gtk_calendar_set_detail_width_chars (GtkCalendar *calendar,
3966 GtkCalendarPrivate *priv;
3968 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3970 priv = GTK_CALENDAR_GET_PRIVATE (calendar);
3972 if (chars != priv->detail_width_chars)
3974 priv->detail_width_chars = chars;
3975 g_object_notify (G_OBJECT (calendar), "detail-width-chars");
3976 gtk_widget_queue_resize_no_redraw (GTK_WIDGET (calendar));
3981 * gtk_calendar_set_detail_height_rows:
3982 * @calendar: a #GtkCalendar.
3983 * @rows: detail height in rows.
3985 * Updates the height of detail cells.
3986 * See #GtkCalendar:detail-height-rows.
3991 gtk_calendar_set_detail_height_rows (GtkCalendar *calendar,
3994 GtkCalendarPrivate *priv;
3996 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3998 priv = GTK_CALENDAR_GET_PRIVATE (calendar);
4000 if (rows != priv->detail_height_rows)
4002 priv->detail_height_rows = rows;
4003 g_object_notify (G_OBJECT (calendar), "detail-height-rows");
4004 gtk_widget_queue_resize_no_redraw (GTK_WIDGET (calendar));
4009 * gtk_calendar_get_detail_width_chars:
4010 * @calendar: a #GtkCalendar.
4012 * Queries the width of detail cells, in characters.
4013 * See #GtkCalendar:detail-width-chars.
4017 * Return value: The width of detail cells, in characters.
4020 gtk_calendar_get_detail_width_chars (GtkCalendar *calendar)
4022 g_return_val_if_fail (GTK_IS_CALENDAR (calendar), 0);
4023 return GTK_CALENDAR_GET_PRIVATE (calendar)->detail_width_chars;
4027 * gtk_calendar_get_detail_height_rows:
4028 * @calendar: a #GtkCalendar.
4030 * Queries the height of detail cells, in rows.
4031 * See #GtkCalendar:detail-width-chars.
4035 * Return value: The height of detail cells, in rows.
4038 gtk_calendar_get_detail_height_rows (GtkCalendar *calendar)
4040 g_return_val_if_fail (GTK_IS_CALENDAR (calendar), 0);
4041 return GTK_CALENDAR_GET_PRIVATE (calendar)->detail_height_rows;