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;
252 GdkColor marked_date_color[31];
254 GdkWindow *arrow_win[4];
256 gchar grow_space [32];
262 gint day_month[6][7];
265 gint num_marked_dates;
266 gint marked_date[31];
275 guint arrow_state[4];
277 guint max_month_width;
278 guint max_year_width;
284 guint max_day_char_width;
285 guint max_day_char_ascent;
286 guint max_day_char_descent;
287 guint max_label_char_ascent;
288 guint max_label_char_descent;
289 guint max_week_char_width;
292 guint year_before : 1;
294 guint need_timer : 1;
297 guint drag_highlight : 1;
307 /* Optional callback, used to display extra information for each day. */
308 GtkCalendarDetailFunc detail_func;
309 gpointer detail_func_user_data;
310 GDestroyNotify detail_func_destroy;
312 /* Size requistion for details provided by the hook. */
313 gint detail_height_rows;
314 gint detail_width_chars;
315 gint detail_overflow[6];
318 #define GTK_CALENDAR_GET_PRIVATE(widget) (GTK_CALENDAR (widget)->priv)
320 static void gtk_calendar_finalize (GObject *calendar);
321 static void gtk_calendar_destroy (GtkWidget *widget);
322 static void gtk_calendar_set_property (GObject *object,
326 static void gtk_calendar_get_property (GObject *object,
331 static void gtk_calendar_realize (GtkWidget *widget);
332 static void gtk_calendar_unrealize (GtkWidget *widget);
333 static void gtk_calendar_map (GtkWidget *widget);
334 static void gtk_calendar_unmap (GtkWidget *widget);
335 static void gtk_calendar_get_preferred_width (GtkWidget *widget,
338 static void gtk_calendar_get_preferred_height (GtkWidget *widget,
341 static void gtk_calendar_size_allocate (GtkWidget *widget,
342 GtkAllocation *allocation);
343 static gboolean gtk_calendar_draw (GtkWidget *widget,
345 static gboolean gtk_calendar_button_press (GtkWidget *widget,
346 GdkEventButton *event);
347 static gboolean gtk_calendar_button_release (GtkWidget *widget,
348 GdkEventButton *event);
349 static gboolean gtk_calendar_motion_notify (GtkWidget *widget,
350 GdkEventMotion *event);
351 static gboolean gtk_calendar_enter_notify (GtkWidget *widget,
352 GdkEventCrossing *event);
353 static gboolean gtk_calendar_leave_notify (GtkWidget *widget,
354 GdkEventCrossing *event);
355 static gboolean gtk_calendar_scroll (GtkWidget *widget,
356 GdkEventScroll *event);
357 static gboolean gtk_calendar_key_press (GtkWidget *widget,
359 static gboolean gtk_calendar_focus_out (GtkWidget *widget,
360 GdkEventFocus *event);
361 static void gtk_calendar_grab_notify (GtkWidget *widget,
362 gboolean was_grabbed);
363 static void gtk_calendar_state_flags_changed (GtkWidget *widget,
364 GtkStateFlags previous_state);
365 static gboolean gtk_calendar_query_tooltip (GtkWidget *widget,
368 gboolean keyboard_mode,
369 GtkTooltip *tooltip);
371 static void gtk_calendar_drag_data_get (GtkWidget *widget,
372 GdkDragContext *context,
373 GtkSelectionData *selection_data,
376 static void gtk_calendar_drag_data_received (GtkWidget *widget,
377 GdkDragContext *context,
380 GtkSelectionData *selection_data,
383 static gboolean gtk_calendar_drag_motion (GtkWidget *widget,
384 GdkDragContext *context,
388 static void gtk_calendar_drag_leave (GtkWidget *widget,
389 GdkDragContext *context,
391 static gboolean gtk_calendar_drag_drop (GtkWidget *widget,
392 GdkDragContext *context,
397 static void calendar_start_spinning (GtkCalendar *calendar,
399 static void calendar_stop_spinning (GtkCalendar *calendar);
401 static void calendar_invalidate_day (GtkCalendar *widget,
404 static void calendar_invalidate_day_num (GtkCalendar *widget,
406 static void calendar_invalidate_arrow (GtkCalendar *widget,
409 static void calendar_compute_days (GtkCalendar *calendar);
410 static gint calendar_get_xsep (GtkCalendar *calendar);
411 static gint calendar_get_ysep (GtkCalendar *calendar);
412 static gint calendar_get_inner_border (GtkCalendar *calendar);
414 static char *default_abbreviated_dayname[7];
415 static char *default_monthname[12];
417 G_DEFINE_TYPE (GtkCalendar, gtk_calendar, GTK_TYPE_WIDGET)
420 gtk_calendar_class_init (GtkCalendarClass *class)
422 GObjectClass *gobject_class;
423 GtkWidgetClass *widget_class;
425 gobject_class = (GObjectClass*) class;
426 widget_class = (GtkWidgetClass*) class;
428 gobject_class->set_property = gtk_calendar_set_property;
429 gobject_class->get_property = gtk_calendar_get_property;
430 gobject_class->finalize = gtk_calendar_finalize;
432 widget_class->destroy = gtk_calendar_destroy;
433 widget_class->realize = gtk_calendar_realize;
434 widget_class->unrealize = gtk_calendar_unrealize;
435 widget_class->map = gtk_calendar_map;
436 widget_class->unmap = gtk_calendar_unmap;
437 widget_class->draw = gtk_calendar_draw;
438 widget_class->get_preferred_width = gtk_calendar_get_preferred_width;
439 widget_class->get_preferred_height = gtk_calendar_get_preferred_height;
440 widget_class->size_allocate = gtk_calendar_size_allocate;
441 widget_class->button_press_event = gtk_calendar_button_press;
442 widget_class->button_release_event = gtk_calendar_button_release;
443 widget_class->motion_notify_event = gtk_calendar_motion_notify;
444 widget_class->enter_notify_event = gtk_calendar_enter_notify;
445 widget_class->leave_notify_event = gtk_calendar_leave_notify;
446 widget_class->key_press_event = gtk_calendar_key_press;
447 widget_class->scroll_event = gtk_calendar_scroll;
448 widget_class->state_flags_changed = gtk_calendar_state_flags_changed;
449 widget_class->grab_notify = gtk_calendar_grab_notify;
450 widget_class->focus_out_event = gtk_calendar_focus_out;
451 widget_class->query_tooltip = gtk_calendar_query_tooltip;
453 widget_class->drag_data_get = gtk_calendar_drag_data_get;
454 widget_class->drag_motion = gtk_calendar_drag_motion;
455 widget_class->drag_leave = gtk_calendar_drag_leave;
456 widget_class->drag_drop = gtk_calendar_drag_drop;
457 widget_class->drag_data_received = gtk_calendar_drag_data_received;
463 * This property gets initially set to the current year.
465 g_object_class_install_property (gobject_class,
467 g_param_spec_int ("year",
469 P_("The selected year"),
471 GTK_PARAM_READWRITE));
476 * The selected month (as a number between 0 and 11).
477 * This property gets initially set to the current month.
479 g_object_class_install_property (gobject_class,
481 g_param_spec_int ("month",
483 P_("The selected month (as a number between 0 and 11)"),
485 GTK_PARAM_READWRITE));
490 * The selected day (as a number between 1 and 31, or 0
491 * to unselect the currently selected day).
492 * This property gets initially set to the current day.
494 g_object_class_install_property (gobject_class,
496 g_param_spec_int ("day",
498 P_("The selected day (as a number between 1 and 31, or 0 to unselect the currently selected day)"),
500 GTK_PARAM_READWRITE));
503 * GtkCalendar:show-heading:
505 * Determines whether a heading is displayed.
509 g_object_class_install_property (gobject_class,
511 g_param_spec_boolean ("show-heading",
513 P_("If TRUE, a heading is displayed"),
515 GTK_PARAM_READWRITE));
518 * GtkCalendar:show-day-names:
520 * Determines whether day names are displayed.
524 g_object_class_install_property (gobject_class,
526 g_param_spec_boolean ("show-day-names",
527 P_("Show Day Names"),
528 P_("If TRUE, day names are displayed"),
530 GTK_PARAM_READWRITE));
532 * GtkCalendar:no-month-change:
534 * Determines whether the selected month can be changed.
538 g_object_class_install_property (gobject_class,
539 PROP_NO_MONTH_CHANGE,
540 g_param_spec_boolean ("no-month-change",
541 P_("No Month Change"),
542 P_("If TRUE, the selected month cannot be changed"),
544 GTK_PARAM_READWRITE));
547 * GtkCalendar:show-week-numbers:
549 * Determines whether week numbers are displayed.
553 g_object_class_install_property (gobject_class,
554 PROP_SHOW_WEEK_NUMBERS,
555 g_param_spec_boolean ("show-week-numbers",
556 P_("Show Week Numbers"),
557 P_("If TRUE, week numbers are displayed"),
559 GTK_PARAM_READWRITE));
562 * GtkCalendar:detail-width-chars:
564 * Width of a detail cell, in characters.
565 * A value of 0 allows any width. See gtk_calendar_set_detail_func().
569 g_object_class_install_property (gobject_class,
570 PROP_DETAIL_WIDTH_CHARS,
571 g_param_spec_int ("detail-width-chars",
573 P_("Details width in characters"),
575 GTK_PARAM_READWRITE));
578 * GtkCalendar:detail-height-rows:
580 * Height of a detail cell, in rows.
581 * A value of 0 allows any width. See gtk_calendar_set_detail_func().
585 g_object_class_install_property (gobject_class,
586 PROP_DETAIL_HEIGHT_ROWS,
587 g_param_spec_int ("detail-height-rows",
588 P_("Details Height"),
589 P_("Details height in rows"),
591 GTK_PARAM_READWRITE));
594 * GtkCalendar:show-details:
596 * Determines whether details are shown directly in the widget, or if they are
597 * available only as tooltip. When this property is set days with details are
602 g_object_class_install_property (gobject_class,
604 g_param_spec_boolean ("show-details",
606 P_("If TRUE, details are shown"),
608 GTK_PARAM_READWRITE));
612 * GtkCalendar:inner-border
614 * The spacing around the day/week headers and main area.
616 gtk_widget_class_install_style_property (widget_class,
617 g_param_spec_int ("inner-border",
619 P_("Inner border space"),
621 GTK_PARAM_READABLE));
624 * GtkCalndar:vertical-separation
626 * Separation between day headers and main area.
628 gtk_widget_class_install_style_property (widget_class,
629 g_param_spec_int ("vertical-separation",
630 P_("Vertical separation"),
631 P_("Space between day headers and main area"),
633 GTK_PARAM_READABLE));
636 * GtkCalendar:horizontal-separation
638 * Separation between week headers and main area.
640 gtk_widget_class_install_style_property (widget_class,
641 g_param_spec_int ("horizontal-separation",
642 P_("Horizontal separation"),
643 P_("Space between week headers and main area"),
645 GTK_PARAM_READABLE));
648 * GtkCalendar::month-changed:
649 * @calendar: the object which received the signal.
651 * Emitted when the user clicks a button to change the selected month on a
654 gtk_calendar_signals[MONTH_CHANGED_SIGNAL] =
655 g_signal_new (I_("month-changed"),
656 G_OBJECT_CLASS_TYPE (gobject_class),
658 G_STRUCT_OFFSET (GtkCalendarClass, month_changed),
660 _gtk_marshal_VOID__VOID,
664 * GtkCalendar::day-selected:
665 * @calendar: the object which received the signal.
667 * Emitted when the user selects a day.
669 gtk_calendar_signals[DAY_SELECTED_SIGNAL] =
670 g_signal_new (I_("day-selected"),
671 G_OBJECT_CLASS_TYPE (gobject_class),
673 G_STRUCT_OFFSET (GtkCalendarClass, day_selected),
675 _gtk_marshal_VOID__VOID,
679 * GtkCalendar::day-selected-double-click:
680 * @calendar: the object which received the signal.
682 * Emitted when the user double-clicks a day.
684 gtk_calendar_signals[DAY_SELECTED_DOUBLE_CLICK_SIGNAL] =
685 g_signal_new (I_("day-selected-double-click"),
686 G_OBJECT_CLASS_TYPE (gobject_class),
688 G_STRUCT_OFFSET (GtkCalendarClass, day_selected_double_click),
690 _gtk_marshal_VOID__VOID,
694 * GtkCalendar::prev-month:
695 * @calendar: the object which received the signal.
697 * Emitted when the user switched to the previous month.
699 gtk_calendar_signals[PREV_MONTH_SIGNAL] =
700 g_signal_new (I_("prev-month"),
701 G_OBJECT_CLASS_TYPE (gobject_class),
703 G_STRUCT_OFFSET (GtkCalendarClass, prev_month),
705 _gtk_marshal_VOID__VOID,
709 * GtkCalendar::next-month:
710 * @calendar: the object which received the signal.
712 * Emitted when the user switched to the next month.
714 gtk_calendar_signals[NEXT_MONTH_SIGNAL] =
715 g_signal_new (I_("next-month"),
716 G_OBJECT_CLASS_TYPE (gobject_class),
718 G_STRUCT_OFFSET (GtkCalendarClass, next_month),
720 _gtk_marshal_VOID__VOID,
724 * GtkCalendar::prev-year:
725 * @calendar: the object which received the signal.
727 * Emitted when user switched to the previous year.
729 gtk_calendar_signals[PREV_YEAR_SIGNAL] =
730 g_signal_new (I_("prev-year"),
731 G_OBJECT_CLASS_TYPE (gobject_class),
733 G_STRUCT_OFFSET (GtkCalendarClass, prev_year),
735 _gtk_marshal_VOID__VOID,
739 * GtkCalendar::next-year:
740 * @calendar: the object which received the signal.
742 * Emitted when user switched to the next year.
744 gtk_calendar_signals[NEXT_YEAR_SIGNAL] =
745 g_signal_new (I_("next-year"),
746 G_OBJECT_CLASS_TYPE (gobject_class),
748 G_STRUCT_OFFSET (GtkCalendarClass, next_year),
750 _gtk_marshal_VOID__VOID,
753 g_type_class_add_private (gobject_class, sizeof (GtkCalendarPrivate));
757 gtk_calendar_init (GtkCalendar *calendar)
759 GtkWidget *widget = GTK_WIDGET (calendar);
764 wchar_t wbuffer[100];
769 GtkCalendarPrivate *priv;
771 #ifdef HAVE__NL_TIME_FIRST_WEEKDAY
772 union { unsigned int word; char *string; } langinfo;
773 gint week_1stday = 0;
774 gint first_weekday = 1;
780 priv = calendar->priv = G_TYPE_INSTANCE_GET_PRIVATE (calendar,
784 gtk_widget_set_can_focus (widget, TRUE);
785 gtk_widget_set_has_window (widget, FALSE);
787 if (!default_abbreviated_dayname[0])
791 tmp_time= (i+3)*86400;
792 strftime ( buffer, sizeof (buffer), "%a", gmtime (&tmp_time));
793 default_abbreviated_dayname[i] = g_locale_to_utf8 (buffer, -1, NULL, NULL, NULL);
795 if (!GetLocaleInfoW (GetThreadLocale (), LOCALE_SABBREVDAYNAME1 + (i+6)%7,
796 wbuffer, G_N_ELEMENTS (wbuffer)))
797 default_abbreviated_dayname[i] = g_strdup_printf ("(%d)", i);
799 default_abbreviated_dayname[i] = g_utf16_to_utf8 (wbuffer, -1, NULL, NULL, NULL);
803 if (!default_monthname[0])
808 strftime ( buffer, sizeof (buffer), "%B", gmtime (&tmp_time));
809 default_monthname[i] = g_locale_to_utf8 (buffer, -1, NULL, NULL, NULL);
811 if (!GetLocaleInfoW (GetThreadLocale (), LOCALE_SMONTHNAME1 + i,
812 wbuffer, G_N_ELEMENTS (wbuffer)))
813 default_monthname[i] = g_strdup_printf ("(%d)", i);
815 default_monthname[i] = g_utf16_to_utf8 (wbuffer, -1, NULL, NULL, NULL);
821 tm = localtime (&secs);
822 priv->month = tm->tm_mon;
823 priv->year = 1900 + tm->tm_year;
826 priv->marked_date[i] = FALSE;
827 priv->num_marked_dates = 0;
828 priv->selected_day = tm->tm_mday;
830 priv->display_flags = (GTK_CALENDAR_SHOW_HEADING |
831 GTK_CALENDAR_SHOW_DAY_NAMES |
832 GTK_CALENDAR_SHOW_DETAILS);
834 priv->focus_row = -1;
835 priv->focus_col = -1;
837 priv->max_year_width = 0;
838 priv->max_month_width = 0;
839 priv->max_day_char_width = 0;
840 priv->max_week_char_width = 0;
842 priv->max_day_char_ascent = 0;
843 priv->max_day_char_descent = 0;
844 priv->max_label_char_ascent = 0;
845 priv->max_label_char_descent = 0;
847 priv->arrow_width = 10;
849 priv->need_timer = 0;
851 priv->click_child = -1;
854 priv->drag_highlight = 0;
856 gtk_drag_dest_set (widget, 0, NULL, 0, GDK_ACTION_COPY);
857 gtk_drag_dest_add_text_targets (widget);
859 priv->year_before = 0;
861 /* Translate to calendar:YM if you want years to be displayed
862 * before months; otherwise translate to calendar:MY.
863 * Do *not* translate it to anything else, if it
864 * it isn't calendar:YM or calendar:MY it will not work.
866 * Note that the ordering described here is logical order, which is
867 * further influenced by BIDI ordering. Thus, if you have a default
868 * text direction of RTL and specify "calendar:YM", then the year
869 * will appear to the right of the month.
871 year_before = _("calendar:MY");
872 if (strcmp (year_before, "calendar:YM") == 0)
873 priv->year_before = 1;
874 else if (strcmp (year_before, "calendar:MY") != 0)
875 g_warning ("Whoever translated calendar:MY did so wrongly.\n");
878 priv->week_start = 0;
881 if (GetLocaleInfoW (GetThreadLocale (), LOCALE_IFIRSTDAYOFWEEK,
882 wbuffer, G_N_ELEMENTS (wbuffer)))
883 week_start = g_utf16_to_utf8 (wbuffer, -1, NULL, NULL, NULL);
885 if (week_start != NULL)
887 priv->week_start = (week_start[0] - '0' + 1) % 7;
891 #ifdef HAVE__NL_TIME_FIRST_WEEKDAY
892 langinfo.string = nl_langinfo (_NL_TIME_FIRST_WEEKDAY);
893 first_weekday = langinfo.string[0];
894 langinfo.string = nl_langinfo (_NL_TIME_WEEK_1STDAY);
895 week_origin = langinfo.word;
896 if (week_origin == 19971130) /* Sunday */
898 else if (week_origin == 19971201) /* Monday */
901 g_warning ("Unknown value of _NL_TIME_WEEK_1STDAY.\n");
903 priv->week_start = (week_1stday + first_weekday - 1) % 7;
905 /* Translate to calendar:week_start:0 if you want Sunday to be the
906 * first day of the week to calendar:week_start:1 if you want Monday
907 * to be the first day of the week, and so on.
909 week_start = _("calendar:week_start:0");
911 if (strncmp (week_start, "calendar:week_start:", 20) == 0)
912 priv->week_start = *(week_start + 20) - '0';
914 priv->week_start = -1;
916 if (priv->week_start < 0 || priv->week_start > 6)
918 g_warning ("Whoever translated calendar:week_start:0 did so wrongly.\n");
919 priv->week_start = 0;
924 calendar_compute_days (calendar);
928 /****************************************
929 * Utility Functions *
930 ****************************************/
933 calendar_queue_refresh (GtkCalendar *calendar)
935 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
937 if (!(priv->detail_func) ||
938 !(priv->display_flags & GTK_CALENDAR_SHOW_DETAILS) ||
939 (priv->detail_width_chars && priv->detail_height_rows))
940 gtk_widget_queue_draw (GTK_WIDGET (calendar));
942 gtk_widget_queue_resize (GTK_WIDGET (calendar));
946 calendar_set_month_next (GtkCalendar *calendar)
949 GtkCalendarPrivate *priv = calendar->priv;
951 if (priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
954 if (priv->month == 11)
962 calendar_compute_days (calendar);
963 g_signal_emit (calendar,
964 gtk_calendar_signals[NEXT_MONTH_SIGNAL],
966 g_signal_emit (calendar,
967 gtk_calendar_signals[MONTH_CHANGED_SIGNAL],
970 month_len = month_length[leap (priv->year)][priv->month + 1];
972 if (month_len < priv->selected_day)
974 priv->selected_day = 0;
975 gtk_calendar_select_day (calendar, month_len);
978 gtk_calendar_select_day (calendar, priv->selected_day);
980 calendar_queue_refresh (calendar);
984 calendar_set_year_prev (GtkCalendar *calendar)
986 GtkCalendarPrivate *priv = calendar->priv;
990 calendar_compute_days (calendar);
991 g_signal_emit (calendar,
992 gtk_calendar_signals[PREV_YEAR_SIGNAL],
994 g_signal_emit (calendar,
995 gtk_calendar_signals[MONTH_CHANGED_SIGNAL],
998 month_len = month_length[leap (priv->year)][priv->month + 1];
1000 if (month_len < priv->selected_day)
1002 priv->selected_day = 0;
1003 gtk_calendar_select_day (calendar, month_len);
1006 gtk_calendar_select_day (calendar, priv->selected_day);
1008 calendar_queue_refresh (calendar);
1012 calendar_set_year_next (GtkCalendar *calendar)
1014 GtkCalendarPrivate *priv = calendar->priv;
1018 calendar_compute_days (calendar);
1019 g_signal_emit (calendar,
1020 gtk_calendar_signals[NEXT_YEAR_SIGNAL],
1022 g_signal_emit (calendar,
1023 gtk_calendar_signals[MONTH_CHANGED_SIGNAL],
1026 month_len = month_length[leap (priv->year)][priv->month + 1];
1028 if (month_len < priv->selected_day)
1030 priv->selected_day = 0;
1031 gtk_calendar_select_day (calendar, month_len);
1034 gtk_calendar_select_day (calendar, priv->selected_day);
1036 calendar_queue_refresh (calendar);
1040 calendar_compute_days (GtkCalendar *calendar)
1042 GtkCalendarPrivate *priv = calendar->priv;
1045 gint ndays_in_month;
1046 gint ndays_in_prev_month;
1053 month = priv->month + 1;
1055 ndays_in_month = month_length[leap (year)][month];
1057 first_day = day_of_week (year, month, 1);
1058 first_day = (first_day + 7 - priv->week_start) % 7;
1060 /* Compute days of previous month */
1062 ndays_in_prev_month = month_length[leap (year)][month-1];
1064 ndays_in_prev_month = month_length[leap (year)][12];
1065 day = ndays_in_prev_month - first_day + 1;
1070 for (col = 0; col < first_day; col++)
1072 priv->day[row][col] = day;
1073 priv->day_month[row][col] = MONTH_PREV;
1078 /* Compute days of current month */
1080 for (day = 1; day <= ndays_in_month; day++)
1082 priv->day[row][col] = day;
1083 priv->day_month[row][col] = MONTH_CURRENT;
1093 /* Compute days of next month */
1095 for (; row <= 5; row++)
1097 for (; col <= 6; col++)
1099 priv->day[row][col] = day;
1100 priv->day_month[row][col] = MONTH_NEXT;
1108 calendar_select_and_focus_day (GtkCalendar *calendar,
1111 GtkCalendarPrivate *priv = calendar->priv;
1112 gint old_focus_row = priv->focus_row;
1113 gint old_focus_col = priv->focus_col;
1117 for (row = 0; row < 6; row ++)
1118 for (col = 0; col < 7; col++)
1120 if (priv->day_month[row][col] == MONTH_CURRENT
1121 && priv->day[row][col] == day)
1123 priv->focus_row = row;
1124 priv->focus_col = col;
1128 if (old_focus_row != -1 && old_focus_col != -1)
1129 calendar_invalidate_day (calendar, old_focus_row, old_focus_col);
1131 gtk_calendar_select_day (calendar, day);
1135 /****************************************
1136 * Layout computation utilities *
1137 ****************************************/
1140 calendar_row_height (GtkCalendar *calendar)
1142 GtkCalendarPrivate *priv = calendar->priv;
1144 return (GTK_CALENDAR_GET_PRIVATE (calendar)->main_h - CALENDAR_MARGIN
1145 - ((priv->display_flags & GTK_CALENDAR_SHOW_DAY_NAMES)
1146 ? calendar_get_ysep (calendar) : CALENDAR_MARGIN)) / 6;
1150 /* calendar_left_x_for_column: returns the x coordinate
1151 * for the left of the column */
1153 calendar_left_x_for_column (GtkCalendar *calendar,
1156 GtkCalendarPrivate *priv = calendar->priv;
1160 gint calendar_xsep = calendar_get_xsep (calendar);
1161 GtkStyleContext *context;
1162 GtkStateFlags state;
1163 gint inner_border = calendar_get_inner_border (calendar);
1166 context = gtk_widget_get_style_context (GTK_WIDGET (calendar));
1167 state = gtk_widget_get_state_flags (GTK_WIDGET (calendar));
1168 gtk_style_context_get_padding (context, state, &padding);
1170 week_width = priv->week_width + padding.left + inner_border;
1172 if (gtk_widget_get_direction (GTK_WIDGET (calendar)) == GTK_TEXT_DIR_RTL)
1174 column = 6 - column;
1178 width = GTK_CALENDAR_GET_PRIVATE (calendar)->day_width;
1179 if (priv->display_flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
1180 x_left = week_width + calendar_xsep + (width + DAY_XSEP) * column;
1182 x_left = week_width + CALENDAR_MARGIN + (width + DAY_XSEP) * column;
1187 /* column_from_x: returns the column 0-6 that the
1188 * x pixel of the xwindow is in */
1190 calendar_column_from_x (GtkCalendar *calendar,
1194 gint x_left, x_right;
1198 for (c = 0; c < 7; c++)
1200 x_left = calendar_left_x_for_column (calendar, c);
1201 x_right = x_left + GTK_CALENDAR_GET_PRIVATE (calendar)->day_width;
1203 if (event_x >= x_left && event_x < x_right)
1213 /* calendar_top_y_for_row: returns the y coordinate
1214 * for the top of the row */
1216 calendar_top_y_for_row (GtkCalendar *calendar,
1219 GtkStyleContext *context;
1220 GtkAllocation allocation;
1221 gint inner_border = calendar_get_inner_border (calendar);
1222 GtkStateFlags state;
1225 gtk_widget_get_allocation (GTK_WIDGET (calendar), &allocation);
1226 context = gtk_widget_get_style_context (GTK_WIDGET (calendar));
1227 state = gtk_widget_get_state_flags (GTK_WIDGET (calendar));
1229 gtk_style_context_get_padding (context, state, &padding);
1231 return allocation.height
1232 - padding.top - inner_border
1233 - (CALENDAR_MARGIN + (6 - row)
1234 * calendar_row_height (calendar));
1237 /* row_from_y: returns the row 0-5 that the
1238 * y pixel of the xwindow is in */
1240 calendar_row_from_y (GtkCalendar *calendar,
1245 gint y_top, y_bottom;
1247 height = calendar_row_height (calendar);
1250 for (r = 0; r < 6; r++)
1252 y_top = calendar_top_y_for_row (calendar, r);
1253 y_bottom = y_top + height;
1255 if (event_y >= y_top && event_y < y_bottom)
1266 calendar_arrow_rectangle (GtkCalendar *calendar,
1270 GtkWidget *widget = GTK_WIDGET (calendar);
1271 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1272 GtkAllocation allocation;
1273 GtkStyleContext *context;
1274 GtkStateFlags state;
1278 gtk_widget_get_allocation (widget, &allocation);
1279 context = gtk_widget_get_style_context (widget);
1280 state = gtk_widget_get_state_flags (widget);
1282 gtk_style_context_get_padding (context, state, &padding);
1284 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
1285 year_left = priv->year_before;
1287 year_left = !priv->year_before;
1290 rect->width = priv->arrow_width;
1291 rect->height = priv->header_h - 7;
1295 case ARROW_MONTH_LEFT:
1297 rect->x = (allocation.width - padding.left - padding.right
1298 - (3 + 2 * priv->arrow_width + priv->max_month_width));
1302 case ARROW_MONTH_RIGHT:
1304 rect->x = (allocation.width - padding.left - padding.right
1305 - 3 - priv->arrow_width);
1307 rect->x = (priv->arrow_width + priv->max_month_width);
1309 case ARROW_YEAR_LEFT:
1313 rect->x = (allocation.width - padding.left - padding.right
1314 - (3 + 2 * priv->arrow_width + priv->max_year_width));
1316 case ARROW_YEAR_RIGHT:
1318 rect->x = (priv->arrow_width + priv->max_year_width);
1320 rect->x = (allocation.width - padding.left - padding.right
1321 - 3 - priv->arrow_width);
1325 rect->x += padding.left;
1326 rect->y += padding.top;
1330 calendar_day_rectangle (GtkCalendar *calendar,
1335 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1337 rect->x = calendar_left_x_for_column (calendar, col);
1338 rect->y = calendar_top_y_for_row (calendar, row);
1339 rect->height = calendar_row_height (calendar);
1340 rect->width = priv->day_width;
1344 calendar_set_month_prev (GtkCalendar *calendar)
1346 GtkCalendarPrivate *priv = calendar->priv;
1349 if (priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
1352 if (priv->month == 0)
1360 month_len = month_length[leap (priv->year)][priv->month + 1];
1362 calendar_compute_days (calendar);
1364 g_signal_emit (calendar,
1365 gtk_calendar_signals[PREV_MONTH_SIGNAL],
1367 g_signal_emit (calendar,
1368 gtk_calendar_signals[MONTH_CHANGED_SIGNAL],
1371 if (month_len < priv->selected_day)
1373 priv->selected_day = 0;
1374 gtk_calendar_select_day (calendar, month_len);
1378 if (priv->selected_day < 0)
1379 priv->selected_day = priv->selected_day + 1 + month_length[leap (priv->year)][priv->month + 1];
1380 gtk_calendar_select_day (calendar, priv->selected_day);
1383 calendar_queue_refresh (calendar);
1387 /****************************************
1388 * Basic object methods *
1389 ****************************************/
1392 gtk_calendar_finalize (GObject *object)
1394 G_OBJECT_CLASS (gtk_calendar_parent_class)->finalize (object);
1398 gtk_calendar_destroy (GtkWidget *widget)
1400 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
1402 calendar_stop_spinning (GTK_CALENDAR (widget));
1404 /* Call the destroy function for the extra display callback: */
1405 if (priv->detail_func_destroy && priv->detail_func_user_data)
1407 priv->detail_func_destroy (priv->detail_func_user_data);
1408 priv->detail_func_user_data = NULL;
1409 priv->detail_func_destroy = NULL;
1412 GTK_WIDGET_CLASS (gtk_calendar_parent_class)->destroy (widget);
1417 calendar_set_display_option (GtkCalendar *calendar,
1418 GtkCalendarDisplayOptions flag,
1421 GtkCalendarPrivate *priv = calendar->priv;
1422 GtkCalendarDisplayOptions flags;
1425 flags = priv->display_flags | flag;
1427 flags = priv->display_flags & ~flag;
1428 gtk_calendar_set_display_options (calendar, flags);
1432 calendar_get_display_option (GtkCalendar *calendar,
1433 GtkCalendarDisplayOptions flag)
1435 GtkCalendarPrivate *priv = calendar->priv;
1437 return (priv->display_flags & flag) != 0;
1441 gtk_calendar_set_property (GObject *object,
1443 const GValue *value,
1446 GtkCalendar *calendar = GTK_CALENDAR (object);
1447 GtkCalendarPrivate *priv = calendar->priv;
1452 gtk_calendar_select_month (calendar,
1454 g_value_get_int (value));
1457 gtk_calendar_select_month (calendar,
1458 g_value_get_int (value),
1462 gtk_calendar_select_day (calendar,
1463 g_value_get_int (value));
1465 case PROP_SHOW_HEADING:
1466 calendar_set_display_option (calendar,
1467 GTK_CALENDAR_SHOW_HEADING,
1468 g_value_get_boolean (value));
1470 case PROP_SHOW_DAY_NAMES:
1471 calendar_set_display_option (calendar,
1472 GTK_CALENDAR_SHOW_DAY_NAMES,
1473 g_value_get_boolean (value));
1475 case PROP_NO_MONTH_CHANGE:
1476 calendar_set_display_option (calendar,
1477 GTK_CALENDAR_NO_MONTH_CHANGE,
1478 g_value_get_boolean (value));
1480 case PROP_SHOW_WEEK_NUMBERS:
1481 calendar_set_display_option (calendar,
1482 GTK_CALENDAR_SHOW_WEEK_NUMBERS,
1483 g_value_get_boolean (value));
1485 case PROP_SHOW_DETAILS:
1486 calendar_set_display_option (calendar,
1487 GTK_CALENDAR_SHOW_DETAILS,
1488 g_value_get_boolean (value));
1490 case PROP_DETAIL_WIDTH_CHARS:
1491 gtk_calendar_set_detail_width_chars (calendar,
1492 g_value_get_int (value));
1494 case PROP_DETAIL_HEIGHT_ROWS:
1495 gtk_calendar_set_detail_height_rows (calendar,
1496 g_value_get_int (value));
1499 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1505 gtk_calendar_get_property (GObject *object,
1510 GtkCalendar *calendar = GTK_CALENDAR (object);
1511 GtkCalendarPrivate *priv = calendar->priv;
1516 g_value_set_int (value, priv->year);
1519 g_value_set_int (value, priv->month);
1522 g_value_set_int (value, priv->selected_day);
1524 case PROP_SHOW_HEADING:
1525 g_value_set_boolean (value, calendar_get_display_option (calendar,
1526 GTK_CALENDAR_SHOW_HEADING));
1528 case PROP_SHOW_DAY_NAMES:
1529 g_value_set_boolean (value, calendar_get_display_option (calendar,
1530 GTK_CALENDAR_SHOW_DAY_NAMES));
1532 case PROP_NO_MONTH_CHANGE:
1533 g_value_set_boolean (value, calendar_get_display_option (calendar,
1534 GTK_CALENDAR_NO_MONTH_CHANGE));
1536 case PROP_SHOW_WEEK_NUMBERS:
1537 g_value_set_boolean (value, calendar_get_display_option (calendar,
1538 GTK_CALENDAR_SHOW_WEEK_NUMBERS));
1540 case PROP_SHOW_DETAILS:
1541 g_value_set_boolean (value, calendar_get_display_option (calendar,
1542 GTK_CALENDAR_SHOW_DETAILS));
1544 case PROP_DETAIL_WIDTH_CHARS:
1545 g_value_set_int (value, priv->detail_width_chars);
1547 case PROP_DETAIL_HEIGHT_ROWS:
1548 g_value_set_int (value, priv->detail_height_rows);
1551 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1557 /****************************************
1559 ****************************************/
1562 calendar_realize_arrows (GtkCalendar *calendar)
1564 GtkWidget *widget = GTK_WIDGET (calendar);
1565 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1566 GdkWindowAttr attributes;
1567 gint attributes_mask;
1569 GtkAllocation allocation;
1571 if (! (priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
1572 && (priv->display_flags & GTK_CALENDAR_SHOW_HEADING))
1574 gtk_widget_get_allocation (widget, &allocation);
1576 attributes.wclass = GDK_INPUT_ONLY;
1577 attributes.window_type = GDK_WINDOW_CHILD;
1578 attributes.event_mask = (gtk_widget_get_events (widget)
1579 | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
1580 | GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK);
1581 attributes_mask = GDK_WA_X | GDK_WA_Y;
1582 for (i = 0; i < 4; i++)
1585 calendar_arrow_rectangle (calendar, i, &rect);
1587 attributes.x = allocation.x + rect.x;
1588 attributes.y = allocation.y + rect.y;
1589 attributes.width = rect.width;
1590 attributes.height = rect.height;
1591 priv->arrow_win[i] = gdk_window_new (gtk_widget_get_window (widget),
1595 if (!gtk_widget_is_sensitive (widget))
1596 priv->arrow_state[i] = GTK_STATE_FLAG_INSENSITIVE;
1598 gdk_window_set_user_data (priv->arrow_win[i], widget);
1603 for (i = 0; i < 4; i++)
1604 priv->arrow_win[i] = NULL;
1609 calendar_unrealize_arrows (GtkCalendar *calendar)
1611 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1614 for (i = 0; i < 4; i++)
1616 if (priv->arrow_win[i])
1618 gdk_window_set_user_data (priv->arrow_win[i], NULL);
1619 gdk_window_destroy (priv->arrow_win[i]);
1620 priv->arrow_win[i] = NULL;
1627 calendar_get_inner_border (GtkCalendar *calendar)
1631 gtk_widget_style_get (GTK_WIDGET (calendar),
1632 "inner-border", &inner_border,
1635 return inner_border;
1639 calendar_get_xsep (GtkCalendar *calendar)
1643 gtk_widget_style_get (GTK_WIDGET (calendar),
1644 "horizontal-separation", &xsep,
1651 calendar_get_ysep (GtkCalendar *calendar)
1655 gtk_widget_style_get (GTK_WIDGET (calendar),
1656 "vertical-separation", &ysep,
1663 gtk_calendar_realize (GtkWidget *widget)
1665 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
1666 GdkWindowAttr attributes;
1667 gint attributes_mask;
1668 gint inner_border = calendar_get_inner_border (GTK_CALENDAR (widget));
1669 GtkStyleContext *context;
1670 GtkAllocation allocation;
1671 GtkStateFlags state = 0;
1674 context = gtk_widget_get_style_context (widget);
1675 state = gtk_widget_get_state_flags (widget);
1676 gtk_style_context_get_padding (context, state, &padding);
1678 gtk_widget_get_allocation (widget, &allocation);
1680 GTK_WIDGET_CLASS (gtk_calendar_parent_class)->realize (widget);
1682 attributes.wclass = GDK_INPUT_ONLY;
1683 attributes.window_type = GDK_WINDOW_CHILD;
1684 attributes.event_mask = (gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK
1685 | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
1686 | GDK_POINTER_MOTION_MASK | GDK_LEAVE_NOTIFY_MASK);
1688 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
1689 attributes.x = priv->week_width + padding.left + inner_border;
1691 attributes.x = padding.left + inner_border;
1693 attributes.y = priv->header_h + priv->day_name_h + padding.top + inner_border;
1694 attributes.width = allocation.width - attributes.x - (padding.right + inner_border);
1696 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1697 attributes.width -= priv->week_width;
1699 attributes.height = priv->main_h;
1700 attributes_mask = GDK_WA_X | GDK_WA_Y;
1702 attributes.x += allocation.x;
1703 attributes.y += allocation.y;
1705 priv->main_win = gdk_window_new (gtk_widget_get_window (widget),
1706 &attributes, attributes_mask);
1707 gdk_window_set_user_data (priv->main_win, widget);
1709 calendar_realize_arrows (GTK_CALENDAR (widget));
1713 gtk_calendar_unrealize (GtkWidget *widget)
1715 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
1717 calendar_unrealize_arrows (GTK_CALENDAR (widget));
1721 gdk_window_set_user_data (priv->main_win, NULL);
1722 gdk_window_destroy (priv->main_win);
1723 priv->main_win = NULL;
1726 GTK_WIDGET_CLASS (gtk_calendar_parent_class)->unrealize (widget);
1730 calendar_map_arrows (GtkCalendar *calendar)
1732 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1735 for (i = 0; i < 4; i++)
1737 if (priv->arrow_win[i])
1738 gdk_window_show (priv->arrow_win[i]);
1743 calendar_unmap_arrows (GtkCalendar *calendar)
1745 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1748 for (i = 0; i < 4; i++)
1750 if (priv->arrow_win[i])
1751 gdk_window_hide (priv->arrow_win[i]);
1756 gtk_calendar_map (GtkWidget *widget)
1758 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
1760 GTK_WIDGET_CLASS (gtk_calendar_parent_class)->map (widget);
1762 gdk_window_show (priv->main_win);
1764 calendar_map_arrows (GTK_CALENDAR (widget));
1768 gtk_calendar_unmap (GtkWidget *widget)
1770 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
1772 calendar_unmap_arrows (GTK_CALENDAR (widget));
1774 gdk_window_hide (priv->main_win);
1776 GTK_WIDGET_CLASS (gtk_calendar_parent_class)->unmap (widget);
1780 gtk_calendar_get_detail (GtkCalendar *calendar,
1784 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1787 if (priv->detail_func == NULL)
1791 month = priv->month + priv->day_month[row][column] - MONTH_CURRENT;
1798 else if (month > 11)
1804 return priv->detail_func (calendar,
1806 priv->day[row][column],
1807 priv->detail_func_user_data);
1811 gtk_calendar_query_tooltip (GtkWidget *widget,
1814 gboolean keyboard_mode,
1815 GtkTooltip *tooltip)
1817 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
1818 GtkCalendar *calendar = GTK_CALENDAR (widget);
1819 gchar *detail = NULL;
1820 GdkRectangle day_rect;
1823 col = calendar_column_from_x (calendar, x);
1824 row = calendar_row_from_y (calendar, y);
1826 if (col != -1 && row != -1 &&
1827 (0 != (priv->detail_overflow[row] & (1 << col)) ||
1828 0 == (priv->display_flags & GTK_CALENDAR_SHOW_DETAILS)))
1830 detail = gtk_calendar_get_detail (calendar, row, col);
1831 calendar_day_rectangle (calendar, row, col, &day_rect);
1836 gtk_tooltip_set_tip_area (tooltip, &day_rect);
1837 gtk_tooltip_set_markup (tooltip, detail);
1844 if (GTK_WIDGET_CLASS (gtk_calendar_parent_class)->query_tooltip)
1845 return GTK_WIDGET_CLASS (gtk_calendar_parent_class)->query_tooltip (widget, x, y, keyboard_mode, tooltip);
1851 /****************************************
1852 * Size Request and Allocate *
1853 ****************************************/
1856 gtk_calendar_size_request (GtkWidget *widget,
1857 GtkRequisition *requisition)
1859 GtkCalendar *calendar = GTK_CALENDAR (widget);
1860 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
1861 GtkStyleContext *context;
1862 GtkStateFlags state;
1864 PangoLayout *layout;
1865 PangoRectangle logical_rect;
1869 gint calendar_margin = CALENDAR_MARGIN;
1870 gint header_width, main_width;
1871 gint max_header_height = 0;
1874 gint max_detail_height;
1875 gint inner_border = calendar_get_inner_border (calendar);
1876 gint calendar_ysep = calendar_get_ysep (calendar);
1877 gint calendar_xsep = calendar_get_xsep (calendar);
1879 gtk_widget_style_get (GTK_WIDGET (widget),
1880 "focus-line-width", &focus_width,
1881 "focus-padding", &focus_padding,
1884 layout = gtk_widget_create_pango_layout (widget, NULL);
1887 * Calculate the requisition width for the widget.
1892 if (priv->display_flags & GTK_CALENDAR_SHOW_HEADING)
1894 priv->max_month_width = 0;
1895 for (i = 0; i < 12; i++)
1897 pango_layout_set_text (layout, default_monthname[i], -1);
1898 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1899 priv->max_month_width = MAX (priv->max_month_width,
1900 logical_rect.width + 8);
1901 max_header_height = MAX (max_header_height, logical_rect.height);
1904 priv->max_year_width = 0;
1905 /* Translators: This is a text measurement template.
1906 * Translate it to the widest year text
1908 * If you don't understand this, leave it as "2000"
1910 pango_layout_set_text (layout, C_("year measurement template", "2000"), -1);
1911 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1912 priv->max_year_width = MAX (priv->max_year_width,
1913 logical_rect.width + 8);
1914 max_header_height = MAX (max_header_height, logical_rect.height);
1918 priv->max_month_width = 0;
1919 priv->max_year_width = 0;
1922 if (priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
1923 header_width = (priv->max_month_width
1924 + priv->max_year_width
1927 header_width = (priv->max_month_width
1928 + priv->max_year_width
1929 + 4 * priv->arrow_width + 3 * 3);
1931 /* Mainwindow labels width */
1933 priv->max_day_char_width = 0;
1934 priv->max_day_char_ascent = 0;
1935 priv->max_day_char_descent = 0;
1936 priv->min_day_width = 0;
1938 for (i = 0; i < 9; i++)
1941 g_snprintf (buffer, sizeof (buffer), C_("calendar:day:digits", "%d"), i * 11);
1942 pango_layout_set_text (layout, buffer, -1);
1943 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1944 priv->min_day_width = MAX (priv->min_day_width,
1945 logical_rect.width);
1947 priv->max_day_char_ascent = MAX (priv->max_day_char_ascent,
1948 PANGO_ASCENT (logical_rect));
1949 priv->max_day_char_descent = MAX (priv->max_day_char_descent,
1950 PANGO_DESCENT (logical_rect));
1953 priv->max_label_char_ascent = 0;
1954 priv->max_label_char_descent = 0;
1955 if (priv->display_flags & GTK_CALENDAR_SHOW_DAY_NAMES)
1956 for (i = 0; i < 7; i++)
1958 pango_layout_set_text (layout, default_abbreviated_dayname[i], -1);
1959 pango_layout_line_get_pixel_extents (pango_layout_get_lines_readonly (layout)->data, NULL, &logical_rect);
1961 priv->min_day_width = MAX (priv->min_day_width, logical_rect.width);
1962 priv->max_label_char_ascent = MAX (priv->max_label_char_ascent,
1963 PANGO_ASCENT (logical_rect));
1964 priv->max_label_char_descent = MAX (priv->max_label_char_descent,
1965 PANGO_DESCENT (logical_rect));
1968 priv->max_week_char_width = 0;
1969 if (priv->display_flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
1970 for (i = 0; i < 9; i++)
1973 g_snprintf (buffer, sizeof (buffer), C_("calendar:week:digits", "%d"), i * 11);
1974 pango_layout_set_text (layout, buffer, -1);
1975 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1976 priv->max_week_char_width = MAX (priv->max_week_char_width,
1977 logical_rect.width / 2);
1980 /* Calculate detail extents. Do this as late as possible since
1981 * pango_layout_set_markup is called which alters font settings. */
1982 max_detail_height = 0;
1984 if (priv->detail_func && (priv->display_flags & GTK_CALENDAR_SHOW_DETAILS))
1986 gchar *markup, *tail;
1988 if (priv->detail_width_chars || priv->detail_height_rows)
1990 gint rows = MAX (1, priv->detail_height_rows) - 1;
1991 gsize len = priv->detail_width_chars + rows + 16;
1993 markup = tail = g_alloca (len);
1995 memcpy (tail, "<small>", 7);
1998 memset (tail, 'm', priv->detail_width_chars);
1999 tail += priv->detail_width_chars;
2001 memset (tail, '\n', rows);
2004 memcpy (tail, "</small>", 9);
2007 g_assert (len == (tail - markup));
2009 pango_layout_set_markup (layout, markup, -1);
2010 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2012 if (priv->detail_width_chars)
2013 priv->min_day_width = MAX (priv->min_day_width, logical_rect.width);
2014 if (priv->detail_height_rows)
2015 max_detail_height = MAX (max_detail_height, logical_rect.height);
2018 if (!priv->detail_width_chars || !priv->detail_height_rows)
2019 for (r = 0; r < 6; r++)
2020 for (c = 0; c < 7; c++)
2022 gchar *detail = gtk_calendar_get_detail (calendar, r, c);
2026 markup = g_strconcat ("<small>", detail, "</small>", NULL);
2027 pango_layout_set_markup (layout, markup, -1);
2029 if (priv->detail_width_chars)
2031 pango_layout_set_wrap (layout, PANGO_WRAP_WORD_CHAR);
2032 pango_layout_set_width (layout, PANGO_SCALE * priv->min_day_width);
2035 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2037 if (!priv->detail_width_chars)
2038 priv->min_day_width = MAX (priv->min_day_width, logical_rect.width);
2039 if (!priv->detail_height_rows)
2040 max_detail_height = MAX (max_detail_height, logical_rect.height);
2048 /* We add one to max_day_char_width to be able to make the marked day "bold" */
2049 priv->max_day_char_width = priv->min_day_width / 2 + 1;
2051 main_width = (7 * (priv->min_day_width + (focus_padding + focus_width) * 2) + (DAY_XSEP * 6) + CALENDAR_MARGIN * 2
2052 + (priv->max_week_char_width
2053 ? priv->max_week_char_width * 2 + (focus_padding + focus_width) * 2 + calendar_xsep * 2
2056 context = gtk_widget_get_style_context (widget);
2057 state = gtk_widget_get_state_flags (widget);
2058 gtk_style_context_get_padding (context, state, &padding);
2060 requisition->width = MAX (header_width, main_width + inner_border * 2) + padding.left + padding.right;
2063 * Calculate the requisition height for the widget.
2066 if (priv->display_flags & GTK_CALENDAR_SHOW_HEADING)
2068 priv->header_h = (max_header_height + calendar_ysep * 2);
2075 if (priv->display_flags & GTK_CALENDAR_SHOW_DAY_NAMES)
2077 priv->day_name_h = (priv->max_label_char_ascent
2078 + priv->max_label_char_descent
2079 + 2 * (focus_padding + focus_width) + calendar_margin);
2080 calendar_margin = calendar_ysep;
2084 priv->day_name_h = 0;
2087 priv->main_h = (CALENDAR_MARGIN + calendar_margin
2088 + 6 * (priv->max_day_char_ascent
2089 + priv->max_day_char_descent
2091 + 2 * (focus_padding + focus_width))
2094 height = priv->header_h + priv->day_name_h + priv->main_h;
2096 requisition->height = height + padding.top + padding.bottom + (inner_border * 2);
2098 g_object_unref (layout);
2102 gtk_calendar_get_preferred_width (GtkWidget *widget,
2106 GtkRequisition requisition;
2108 gtk_calendar_size_request (widget, &requisition);
2110 *minimum = *natural = requisition.width;
2114 gtk_calendar_get_preferred_height (GtkWidget *widget,
2118 GtkRequisition requisition;
2120 gtk_calendar_size_request (widget, &requisition);
2122 *minimum = *natural = requisition.height;
2126 gtk_calendar_size_allocate (GtkWidget *widget,
2127 GtkAllocation *allocation)
2129 GtkCalendar *calendar = GTK_CALENDAR (widget);
2130 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
2131 GtkStyleContext *context;
2132 GtkStateFlags state;
2135 gint inner_border = calendar_get_inner_border (calendar);
2136 gint calendar_xsep = calendar_get_xsep (calendar);
2138 context = gtk_widget_get_style_context (widget);
2139 state = gtk_widget_get_state_flags (widget);
2140 gtk_style_context_get_padding (context, state, &padding);
2142 gtk_widget_set_allocation (widget, allocation);
2144 if (priv->display_flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
2146 priv->day_width = (priv->min_day_width
2147 * ((allocation->width - (inner_border * 2) - padding.left - padding.right
2148 - (CALENDAR_MARGIN * 2) - (DAY_XSEP * 6) - calendar_xsep * 2))
2149 / (7 * priv->min_day_width + priv->max_week_char_width * 2));
2150 priv->week_width = ((allocation->width - (inner_border * 2) - padding.left - padding.right
2151 - (CALENDAR_MARGIN * 2) - (DAY_XSEP * 6) - calendar_xsep * 2 )
2152 - priv->day_width * 7 + CALENDAR_MARGIN + calendar_xsep);
2156 priv->day_width = (allocation->width
2157 - (inner_border * 2)
2158 - padding.left - padding.right
2159 - (CALENDAR_MARGIN * 2)
2160 - (DAY_XSEP * 6))/7;
2161 priv->week_width = 0;
2164 if (gtk_widget_get_realized (widget))
2166 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
2167 gdk_window_move_resize (priv->main_win,
2169 + priv->week_width + padding.left + inner_border,
2171 + priv->header_h + priv->day_name_h
2172 + (padding.top + inner_border),
2173 allocation->width - priv->week_width
2174 - (inner_border * 2) - padding.left - padding.right,
2177 gdk_window_move_resize (priv->main_win,
2179 + padding.left + inner_border,
2181 + priv->header_h + priv->day_name_h
2182 + padding.top + inner_border,
2183 allocation->width - priv->week_width
2184 - (inner_border * 2) - padding.left - padding.right,
2187 for (i = 0 ; i < 4 ; i++)
2189 if (priv->arrow_win[i])
2192 calendar_arrow_rectangle (calendar, i, &rect);
2194 gdk_window_move_resize (priv->arrow_win[i],
2195 allocation->x + rect.x,
2196 allocation->y + rect.y,
2197 rect.width, rect.height);
2204 /****************************************
2206 ****************************************/
2209 calendar_paint_header (GtkCalendar *calendar, cairo_t *cr)
2211 GtkWidget *widget = GTK_WIDGET (calendar);
2212 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2213 GtkAllocation allocation;
2214 GtkStyleContext *context;
2215 GtkStateFlags state;
2220 gint max_month_width;
2221 gint max_year_width;
2222 PangoLayout *layout;
2223 PangoRectangle logical_rect;
2229 context = gtk_widget_get_style_context (widget);
2230 state = gtk_widget_get_state_flags (widget);
2231 gtk_style_context_get_padding (context, state, &padding);
2234 cairo_translate (cr, padding.left, padding.top);
2236 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
2237 year_left = priv->year_before;
2239 year_left = !priv->year_before;
2241 gtk_widget_get_allocation (widget, &allocation);
2243 header_width = allocation.width - padding.left - padding.right;
2245 max_month_width = priv->max_month_width;
2246 max_year_width = priv->max_year_width;
2248 gtk_style_context_save (context);
2249 gtk_style_context_add_class (context, GTK_STYLE_CLASS_HEADER);
2251 gtk_render_background (context, cr, 0, 0, header_width, priv->header_h);
2252 gtk_render_frame (context, cr, 0, 0, header_width, priv->header_h);
2254 tmp_time = 1; /* Jan 1 1970, 00:00:01 UTC */
2255 tm = gmtime (&tmp_time);
2256 tm->tm_year = priv->year - 1900;
2258 /* Translators: This dictates how the year is displayed in
2259 * gtkcalendar widget. See strftime() manual for the format.
2260 * Use only ASCII in the translation.
2262 * Also look for the msgid "2000".
2263 * Translate that entry to a year with the widest output of this
2266 * "%Y" is appropriate for most locales.
2268 strftime (buffer, sizeof (buffer), C_("calendar year format", "%Y"), tm);
2269 str = g_locale_to_utf8 (buffer, -1, NULL, NULL, NULL);
2270 layout = gtk_widget_create_pango_layout (widget, str);
2273 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2276 y = (priv->header_h - logical_rect.height) / 2;
2278 /* Draw year and its arrows */
2280 if (priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
2282 x = 3 + (max_year_width - logical_rect.width)/2;
2284 x = header_width - (3 + max_year_width
2285 - (max_year_width - logical_rect.width)/2);
2288 x = 3 + priv->arrow_width + (max_year_width - logical_rect.width)/2;
2290 x = header_width - (3 + priv->arrow_width + max_year_width
2291 - (max_year_width - logical_rect.width)/2);
2293 gtk_render_layout (context, cr, x, y, layout);
2296 g_snprintf (buffer, sizeof (buffer), "%s", default_monthname[priv->month]);
2297 pango_layout_set_text (layout, buffer, -1);
2298 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2300 if (priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
2302 x = header_width - (3 + max_month_width
2303 - (max_month_width - logical_rect.width)/2);
2305 x = 3 + (max_month_width - logical_rect.width) / 2;
2308 x = header_width - (3 + priv->arrow_width + max_month_width
2309 - (max_month_width - logical_rect.width)/2);
2311 x = 3 + priv->arrow_width + (max_month_width - logical_rect.width)/2;
2313 gtk_render_layout (context, cr, x, y, layout);
2314 g_object_unref (layout);
2316 gtk_style_context_restore (context);
2321 calendar_paint_day_names (GtkCalendar *calendar,
2324 GtkWidget *widget = GTK_WIDGET (calendar);
2325 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2326 GtkStyleContext *context;
2327 GtkStateFlags state;
2329 GtkAllocation allocation;
2332 int day_width, cal_width;
2334 PangoLayout *layout;
2335 PangoRectangle logical_rect;
2338 gint calendar_ysep = calendar_get_ysep (calendar);
2339 gint calendar_xsep = calendar_get_xsep (calendar);
2340 gint inner_border = calendar_get_inner_border (calendar);
2342 context = gtk_widget_get_style_context (widget);
2343 state = gtk_widget_get_state_flags (widget);
2344 gtk_style_context_get_padding (context, state, &padding);
2348 cairo_translate (cr,
2349 padding.left + inner_border,
2350 priv->header_h + padding.top + inner_border);
2352 gtk_widget_style_get (GTK_WIDGET (widget),
2353 "focus-line-width", &focus_width,
2354 "focus-padding", &focus_padding,
2357 gtk_widget_get_allocation (widget, &allocation);
2359 day_width = priv->day_width;
2360 cal_width = allocation.width - (inner_border * 2) - padding.left - padding.right;
2361 day_wid_sep = day_width + DAY_XSEP;
2364 * Draw rectangles as inverted background for the labels.
2367 gtk_style_context_save (context);
2368 gtk_style_context_add_class (context, GTK_STYLE_CLASS_HIGHLIGHT);
2370 gtk_render_background (context, cr,
2371 CALENDAR_MARGIN, CALENDAR_MARGIN,
2372 cal_width - CALENDAR_MARGIN * 2,
2373 priv->day_name_h - CALENDAR_MARGIN);
2375 if (priv->display_flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
2376 gtk_render_background (context, cr,
2378 priv->day_name_h - calendar_ysep,
2379 priv->week_width - calendar_ysep - CALENDAR_MARGIN,
2385 layout = gtk_widget_create_pango_layout (widget, NULL);
2387 for (i = 0; i < 7; i++)
2389 if (gtk_widget_get_direction (GTK_WIDGET (calendar)) == GTK_TEXT_DIR_RTL)
2393 day = (day + priv->week_start) % 7;
2394 g_snprintf (buffer, sizeof (buffer), "%s", default_abbreviated_dayname[day]);
2396 pango_layout_set_text (layout, buffer, -1);
2397 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2399 gtk_render_layout (context, cr,
2401 + (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR ?
2402 (priv->week_width + (priv->week_width ? calendar_xsep : 0))
2405 + (day_width - logical_rect.width)/2),
2406 CALENDAR_MARGIN + focus_width + focus_padding + logical_rect.y,
2410 g_object_unref (layout);
2412 gtk_style_context_restore (context);
2417 calendar_paint_week_numbers (GtkCalendar *calendar,
2420 GtkWidget *widget = GTK_WIDGET (calendar);
2421 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2422 GtkStyleContext *context;
2423 GtkStateFlags state;
2425 guint week = 0, year;
2426 gint row, x_loc, y_loc;
2429 PangoLayout *layout;
2430 PangoRectangle logical_rect;
2433 gint calendar_xsep = calendar_get_xsep (calendar);
2434 gint inner_border = calendar_get_inner_border (calendar);
2437 context = gtk_widget_get_style_context (widget);
2438 state = gtk_widget_get_state_flags (widget);
2439 gtk_style_context_get_padding (context, state, &padding);
2443 y = priv->header_h + priv->day_name_h + (padding.top + inner_border);
2444 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
2445 x = padding.left + inner_border;
2447 x = gtk_widget_get_allocated_width (widget) - priv->week_width - (padding.right + inner_border);
2449 gtk_widget_style_get (GTK_WIDGET (widget),
2450 "focus-line-width", &focus_width,
2451 "focus-padding", &focus_padding,
2454 gtk_style_context_save (context);
2455 gtk_style_context_add_class (context, GTK_STYLE_CLASS_HIGHLIGHT);
2457 if (priv->display_flags & GTK_CALENDAR_SHOW_DAY_NAMES)
2458 gtk_render_background (context, cr,
2459 x + CALENDAR_MARGIN, y,
2460 priv->week_width - CALENDAR_MARGIN,
2461 priv->main_h - CALENDAR_MARGIN);
2463 gtk_render_background (context, cr,
2464 x + CALENDAR_MARGIN,
2465 y + CALENDAR_MARGIN,
2466 priv->week_width - CALENDAR_MARGIN,
2467 priv->main_h - 2 * CALENDAR_MARGIN);
2473 layout = gtk_widget_create_pango_layout (widget, NULL);
2474 day_height = calendar_row_height (calendar);
2476 for (row = 0; row < 6; row++)
2481 if (priv->day[row][6] < 15 && row > 3 && priv->month == 11)
2484 result = week_of_year (&week, &year,
2485 ((priv->day[row][6] < 15 && row > 3 ? 1 : 0)
2486 + priv->month) % 12 + 1, priv->day[row][6]);
2487 g_return_if_fail (result);
2489 /* Translators: this defines whether the week numbers should use
2490 * localized digits or the ones used in English (0123...).
2492 * Translate to "%Id" if you want to use localized digits, or
2493 * translate to "%d" otherwise.
2495 * Note that translating this doesn't guarantee that you get localized
2496 * digits. That needs support from your system and locale definition
2499 g_snprintf (buffer, sizeof (buffer), C_("calendar:week:digits", "%d"), week);
2500 pango_layout_set_text (layout, buffer, -1);
2501 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2503 y_loc = calendar_top_y_for_row (calendar, row) + (day_height - logical_rect.height) / 2;
2505 x_loc = x + (priv->week_width
2506 - logical_rect.width
2507 - calendar_xsep - focus_padding - focus_width);
2509 gtk_render_layout (context, cr, x_loc, y_loc, layout);
2512 g_object_unref (layout);
2514 gtk_style_context_restore (context);
2519 calendar_invalidate_day_num (GtkCalendar *calendar,
2522 GtkCalendarPrivate *priv = calendar->priv;
2523 gint r, c, row, col;
2527 for (r = 0; r < 6; r++)
2528 for (c = 0; c < 7; c++)
2529 if (priv->day_month[r][c] == MONTH_CURRENT &&
2530 priv->day[r][c] == day)
2536 g_return_if_fail (row != -1);
2537 g_return_if_fail (col != -1);
2539 calendar_invalidate_day (calendar, row, col);
2543 calendar_invalidate_day (GtkCalendar *calendar,
2547 GdkRectangle day_rect;
2548 GtkAllocation allocation;
2550 gtk_widget_get_allocation (GTK_WIDGET (calendar), &allocation);
2551 calendar_day_rectangle (calendar, row, col, &day_rect);
2552 gtk_widget_queue_draw_area (GTK_WIDGET (calendar),
2553 allocation.x + day_rect.x,
2554 allocation.y + day_rect.y,
2555 day_rect.width, day_rect.height);
2559 is_color_attribute (PangoAttribute *attribute,
2562 return (attribute->klass->type == PANGO_ATTR_FOREGROUND ||
2563 attribute->klass->type == PANGO_ATTR_BACKGROUND);
2567 calendar_paint_day (GtkCalendar *calendar,
2572 GtkWidget *widget = GTK_WIDGET (calendar);
2573 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2574 GtkStyleContext *context;
2575 GtkStateFlags state = 0;
2580 GdkRectangle day_rect;
2582 PangoLayout *layout;
2583 PangoRectangle logical_rect;
2584 gboolean overflow = FALSE;
2585 gboolean show_details;
2587 g_return_if_fail (row < 6);
2588 g_return_if_fail (col < 7);
2590 context = gtk_widget_get_style_context (widget);
2592 day = priv->day[row][col];
2593 show_details = (priv->display_flags & GTK_CALENDAR_SHOW_DETAILS);
2595 calendar_day_rectangle (calendar, row, col, &day_rect);
2597 gtk_style_context_save (context);
2599 if (!gtk_widget_get_sensitive (widget))
2600 state |= GTK_STATE_FLAG_INSENSITIVE;
2603 if (gtk_widget_has_focus (widget))
2604 state |= GTK_STATE_FLAG_FOCUSED;
2606 if (priv->day_month[row][col] == MONTH_PREV ||
2607 priv->day_month[row][col] == MONTH_NEXT)
2608 state |= GTK_STATE_FLAG_INCONSISTENT;
2611 if (priv->marked_date[day-1])
2612 state |= GTK_STATE_FLAG_ACTIVE;
2614 if (priv->selected_day == day)
2616 state |= GTK_STATE_FLAG_SELECTED;
2618 gtk_style_context_set_state (context, state);
2619 gtk_render_background (context, cr,
2620 day_rect.x, day_rect.y,
2621 day_rect.width, day_rect.height);
2626 gtk_style_context_set_state (context, state);
2628 /* Translators: this defines whether the day numbers should use
2629 * localized digits or the ones used in English (0123...).
2631 * Translate to "%Id" if you want to use localized digits, or
2632 * translate to "%d" otherwise.
2634 * Note that translating this doesn't guarantee that you get localized
2635 * digits. That needs support from your system and locale definition
2638 g_snprintf (buffer, sizeof (buffer), C_("calendar:day:digits", "%d"), day);
2640 /* Get extra information to show, if any: */
2642 detail = gtk_calendar_get_detail (calendar, row, col);
2644 layout = gtk_widget_create_pango_layout (widget, buffer);
2645 pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
2646 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2648 x_loc = day_rect.x + (day_rect.width - logical_rect.width) / 2;
2651 gtk_render_layout (context, cr, x_loc, y_loc, layout);
2653 if (priv->day_month[row][col] == MONTH_CURRENT &&
2654 (priv->marked_date[day-1] || (detail && !show_details)))
2655 gtk_render_layout (context, cr, x_loc - 1, y_loc, layout);
2657 y_loc += priv->max_day_char_descent;
2659 if (priv->detail_func && show_details)
2665 gtk_style_context_get_color (context, state, &color);
2666 gdk_cairo_set_source_rgba (cr, &color);
2668 cairo_set_line_width (cr, 1);
2669 cairo_move_to (cr, day_rect.x + 2, y_loc + 0.5);
2670 cairo_line_to (cr, day_rect.x + day_rect.width - 2, y_loc + 0.5);
2678 if (detail && show_details)
2680 gchar *markup = g_strconcat ("<small>", detail, "</small>", NULL);
2681 pango_layout_set_markup (layout, markup, -1);
2684 if (day == priv->selected_day)
2686 /* Stripping colors as they conflict with selection marking. */
2688 PangoAttrList *attrs = pango_layout_get_attributes (layout);
2689 PangoAttrList *colors = NULL;
2692 colors = pango_attr_list_filter (attrs, is_color_attribute, NULL);
2694 pango_attr_list_unref (colors);
2697 pango_layout_set_wrap (layout, PANGO_WRAP_WORD_CHAR);
2698 pango_layout_set_width (layout, PANGO_SCALE * day_rect.width);
2700 if (priv->detail_height_rows)
2702 gint dy = day_rect.height - (y_loc - day_rect.y);
2703 pango_layout_set_height (layout, PANGO_SCALE * dy);
2704 pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_END);
2707 cairo_move_to (cr, day_rect.x, y_loc);
2708 pango_cairo_show_layout (cr, layout);
2711 if (gtk_widget_has_focus (widget)
2712 && priv->focus_row == row && priv->focus_col == col)
2713 gtk_render_focus (context, cr,
2714 day_rect.x, day_rect.y,
2715 day_rect.width, day_rect.height);
2718 priv->detail_overflow[row] |= (1 << col);
2720 priv->detail_overflow[row] &= ~(1 << col);
2722 gtk_style_context_restore (context);
2723 g_object_unref (layout);
2728 calendar_paint_main (GtkCalendar *calendar,
2735 for (col = 0; col < 7; col++)
2736 for (row = 0; row < 6; row++)
2737 calendar_paint_day (calendar, cr, row, col);
2743 calendar_invalidate_arrow (GtkCalendar *calendar,
2746 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2748 if (priv->display_flags & GTK_CALENDAR_SHOW_HEADING &&
2749 priv->arrow_win[arrow])
2752 GtkAllocation allocation;
2754 calendar_arrow_rectangle (calendar, arrow, &rect);
2755 gtk_widget_get_allocation (GTK_WIDGET (calendar), &allocation);
2756 gtk_widget_queue_draw_area (GTK_WIDGET (calendar),
2757 allocation.x + rect.x,
2758 allocation.y + rect.y,
2759 rect.width, rect.height);
2764 calendar_paint_arrow (GtkCalendar *calendar,
2768 GtkWidget *widget = GTK_WIDGET (calendar);
2769 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
2770 GtkStyleContext *context;
2771 GtkStateFlags state;
2775 if (!priv->arrow_win[arrow])
2778 calendar_arrow_rectangle (calendar, arrow, &rect);
2782 context = gtk_widget_get_style_context (widget);
2783 state = priv->arrow_state[arrow];
2785 gtk_style_context_save (context);
2786 gtk_style_context_set_state (context, state);
2787 gtk_style_context_add_class (context, GTK_STYLE_CLASS_BUTTON);
2789 gtk_render_background (context, cr,
2791 rect.width, rect.height);
2793 if (arrow == ARROW_MONTH_LEFT || arrow == ARROW_YEAR_LEFT)
2794 angle = 3 * (G_PI / 2);
2798 gtk_render_arrow (context, cr, angle,
2799 rect.x + (rect.width - 8) / 2,
2800 rect.y + (rect.height - 8) / 2,
2803 gtk_style_context_restore (context);
2808 gtk_calendar_draw (GtkWidget *widget,
2811 GtkCalendar *calendar = GTK_CALENDAR (widget);
2812 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
2815 if (gtk_cairo_should_draw_window (cr, gtk_widget_get_window (widget)))
2817 GtkStyleContext *context;
2819 context = gtk_widget_get_style_context (widget);
2821 gtk_style_context_save (context);
2822 gtk_style_context_add_class (context, GTK_STYLE_CLASS_VIEW);
2824 gtk_render_background (context, cr, 0, 0,
2825 gtk_widget_get_allocated_width (widget),
2826 gtk_widget_get_allocated_height (widget));
2827 gtk_render_frame (context, cr, 0, 0,
2828 gtk_widget_get_allocated_width (widget),
2829 gtk_widget_get_allocated_height (widget));
2832 calendar_paint_main (calendar, cr);
2834 if (priv->display_flags & GTK_CALENDAR_SHOW_HEADING)
2836 calendar_paint_header (calendar, cr);
2837 for (i = 0; i < 4; i++)
2838 calendar_paint_arrow (calendar, cr, i);
2841 if (priv->display_flags & GTK_CALENDAR_SHOW_DAY_NAMES)
2842 calendar_paint_day_names (calendar, cr);
2844 if (priv->display_flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
2845 calendar_paint_week_numbers (calendar, cr);
2851 /****************************************
2853 ****************************************/
2856 calendar_arrow_action (GtkCalendar *calendar,
2861 case ARROW_YEAR_LEFT:
2862 calendar_set_year_prev (calendar);
2864 case ARROW_YEAR_RIGHT:
2865 calendar_set_year_next (calendar);
2867 case ARROW_MONTH_LEFT:
2868 calendar_set_month_prev (calendar);
2870 case ARROW_MONTH_RIGHT:
2871 calendar_set_month_next (calendar);
2879 calendar_timer (gpointer data)
2881 GtkCalendar *calendar = data;
2882 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2883 gboolean retval = FALSE;
2887 calendar_arrow_action (calendar, priv->click_child);
2889 if (priv->need_timer)
2891 GtkSettings *settings;
2894 settings = gtk_widget_get_settings (GTK_WIDGET (calendar));
2895 g_object_get (settings, "gtk-timeout-repeat", &timeout, NULL);
2897 priv->need_timer = FALSE;
2898 priv->timer = gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT_IDLE,
2899 timeout * SCROLL_DELAY_FACTOR,
2900 (GSourceFunc) calendar_timer,
2901 (gpointer) calendar, NULL);
2911 calendar_start_spinning (GtkCalendar *calendar,
2914 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2916 priv->click_child = click_child;
2920 GtkSettings *settings;
2923 settings = gtk_widget_get_settings (GTK_WIDGET (calendar));
2924 g_object_get (settings, "gtk-timeout-initial", &timeout, NULL);
2926 priv->need_timer = TRUE;
2927 priv->timer = gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT_IDLE,
2929 (GSourceFunc) calendar_timer,
2930 (gpointer) calendar, NULL);
2935 calendar_stop_spinning (GtkCalendar *calendar)
2937 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2941 g_source_remove (priv->timer);
2943 priv->need_timer = FALSE;
2948 calendar_main_button_press (GtkCalendar *calendar,
2949 GdkEventButton *event)
2951 GtkWidget *widget = GTK_WIDGET (calendar);
2952 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2958 GtkAllocation allocation;
2960 x = (gint) (event->x);
2961 y = (gint) (event->y);
2963 gdk_window_get_position (priv->main_win, &win_x, &win_y);
2964 gtk_widget_get_allocation (widget, &allocation);
2966 row = calendar_row_from_y (calendar, y + win_y - allocation.y);
2967 col = calendar_column_from_x (calendar, x + win_x - allocation.x);
2969 /* If row or column isn't found, just return. */
2970 if (row == -1 || col == -1)
2973 day_month = priv->day_month[row][col];
2975 if (event->type == GDK_BUTTON_PRESS)
2977 day = priv->day[row][col];
2979 if (day_month == MONTH_PREV)
2980 calendar_set_month_prev (calendar);
2981 else if (day_month == MONTH_NEXT)
2982 calendar_set_month_next (calendar);
2984 if (!gtk_widget_has_focus (widget))
2985 gtk_widget_grab_focus (widget);
2987 if (event->button == 1)
2990 priv->drag_start_x = x;
2991 priv->drag_start_y = y;
2994 calendar_select_and_focus_day (calendar, day);
2996 else if (event->type == GDK_2BUTTON_PRESS)
2999 if (day_month == MONTH_CURRENT)
3000 g_signal_emit (calendar,
3001 gtk_calendar_signals[DAY_SELECTED_DOUBLE_CLICK_SIGNAL],
3007 gtk_calendar_button_press (GtkWidget *widget,
3008 GdkEventButton *event)
3010 GtkCalendar *calendar = GTK_CALENDAR (widget);
3011 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3014 if (event->window == priv->main_win)
3015 calendar_main_button_press (calendar, event);
3017 if (!gtk_widget_has_focus (widget))
3018 gtk_widget_grab_focus (widget);
3020 for (arrow = ARROW_YEAR_LEFT; arrow <= ARROW_MONTH_RIGHT; arrow++)
3022 if (event->window == priv->arrow_win[arrow])
3025 /* only call the action on single click, not double */
3026 if (event->type == GDK_BUTTON_PRESS)
3028 if (event->button == 1)
3029 calendar_start_spinning (calendar, arrow);
3031 calendar_arrow_action (calendar, arrow);
3042 gtk_calendar_button_release (GtkWidget *widget,
3043 GdkEventButton *event)
3045 GtkCalendar *calendar = GTK_CALENDAR (widget);
3046 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3048 if (event->button == 1)
3050 calendar_stop_spinning (calendar);
3060 gtk_calendar_motion_notify (GtkWidget *widget,
3061 GdkEventMotion *event)
3063 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3064 gint event_x, event_y;
3066 event_x = (gint) (event->x);
3067 event_y = (gint) (event->y);
3071 if (gtk_drag_check_threshold (widget,
3072 priv->drag_start_x, priv->drag_start_y,
3073 event->x, event->y))
3075 GdkDragContext *context;
3076 GtkTargetList *target_list = gtk_target_list_new (NULL, 0);
3077 gtk_target_list_add_text_targets (target_list, 0);
3078 context = gtk_drag_begin (widget, target_list, GDK_ACTION_COPY,
3079 1, (GdkEvent *)event);
3082 gtk_target_list_unref (target_list);
3083 gtk_drag_set_icon_default (context);
3091 gtk_calendar_enter_notify (GtkWidget *widget,
3092 GdkEventCrossing *event)
3094 GtkCalendar *calendar = GTK_CALENDAR (widget);
3095 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3097 if (event->window == priv->arrow_win[ARROW_MONTH_LEFT])
3099 priv->arrow_state[ARROW_MONTH_LEFT] |= GTK_STATE_FLAG_PRELIGHT;
3100 calendar_invalidate_arrow (calendar, ARROW_MONTH_LEFT);
3103 if (event->window == priv->arrow_win[ARROW_MONTH_RIGHT])
3105 priv->arrow_state[ARROW_MONTH_RIGHT] |= GTK_STATE_FLAG_PRELIGHT;
3106 calendar_invalidate_arrow (calendar, ARROW_MONTH_RIGHT);
3109 if (event->window == priv->arrow_win[ARROW_YEAR_LEFT])
3111 priv->arrow_state[ARROW_YEAR_LEFT] |= GTK_STATE_FLAG_PRELIGHT;
3112 calendar_invalidate_arrow (calendar, ARROW_YEAR_LEFT);
3115 if (event->window == priv->arrow_win[ARROW_YEAR_RIGHT])
3117 priv->arrow_state[ARROW_YEAR_RIGHT] |= GTK_STATE_FLAG_PRELIGHT;
3118 calendar_invalidate_arrow (calendar, ARROW_YEAR_RIGHT);
3125 gtk_calendar_leave_notify (GtkWidget *widget,
3126 GdkEventCrossing *event)
3128 GtkCalendar *calendar = GTK_CALENDAR (widget);
3129 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3131 if (event->window == priv->arrow_win[ARROW_MONTH_LEFT])
3133 priv->arrow_state[ARROW_MONTH_LEFT] &= ~(GTK_STATE_FLAG_PRELIGHT);
3134 calendar_invalidate_arrow (calendar, ARROW_MONTH_LEFT);
3137 if (event->window == priv->arrow_win[ARROW_MONTH_RIGHT])
3139 priv->arrow_state[ARROW_MONTH_RIGHT] &= ~(GTK_STATE_FLAG_PRELIGHT);
3140 calendar_invalidate_arrow (calendar, ARROW_MONTH_RIGHT);
3143 if (event->window == priv->arrow_win[ARROW_YEAR_LEFT])
3145 priv->arrow_state[ARROW_YEAR_LEFT] &= ~(GTK_STATE_FLAG_PRELIGHT);
3146 calendar_invalidate_arrow (calendar, ARROW_YEAR_LEFT);
3149 if (event->window == priv->arrow_win[ARROW_YEAR_RIGHT])
3151 priv->arrow_state[ARROW_YEAR_RIGHT] &= ~(GTK_STATE_FLAG_PRELIGHT);
3152 calendar_invalidate_arrow (calendar, ARROW_YEAR_RIGHT);
3159 gtk_calendar_scroll (GtkWidget *widget,
3160 GdkEventScroll *event)
3162 GtkCalendar *calendar = GTK_CALENDAR (widget);
3164 if (event->direction == GDK_SCROLL_UP)
3166 if (!gtk_widget_has_focus (widget))
3167 gtk_widget_grab_focus (widget);
3168 calendar_set_month_prev (calendar);
3170 else if (event->direction == GDK_SCROLL_DOWN)
3172 if (!gtk_widget_has_focus (widget))
3173 gtk_widget_grab_focus (widget);
3174 calendar_set_month_next (calendar);
3183 /****************************************
3185 ****************************************/
3188 move_focus (GtkCalendar *calendar,
3191 GtkCalendarPrivate *priv = calendar->priv;
3192 GtkTextDirection text_dir = gtk_widget_get_direction (GTK_WIDGET (calendar));
3194 if ((text_dir == GTK_TEXT_DIR_LTR && direction == -1) ||
3195 (text_dir == GTK_TEXT_DIR_RTL && direction == 1))
3197 if (priv->focus_col > 0)
3199 else if (priv->focus_row > 0)
3201 priv->focus_col = 6;
3205 if (priv->focus_col < 0)
3206 priv->focus_col = 6;
3207 if (priv->focus_row < 0)
3208 priv->focus_row = 5;
3212 if (priv->focus_col < 6)
3214 else if (priv->focus_row < 5)
3216 priv->focus_col = 0;
3220 if (priv->focus_col < 0)
3221 priv->focus_col = 0;
3222 if (priv->focus_row < 0)
3223 priv->focus_row = 0;
3228 gtk_calendar_key_press (GtkWidget *widget,
3231 GtkCalendar *calendar = GTK_CALENDAR (widget);
3232 GtkCalendarPrivate *priv = calendar->priv;
3240 old_focus_row = priv->focus_row;
3241 old_focus_col = priv->focus_col;
3243 switch (event->keyval)
3245 case GDK_KEY_KP_Left:
3248 if (event->state & GDK_CONTROL_MASK)
3249 calendar_set_month_prev (calendar);
3252 move_focus (calendar, -1);
3253 calendar_invalidate_day (calendar, old_focus_row, old_focus_col);
3254 calendar_invalidate_day (calendar, priv->focus_row,
3258 case GDK_KEY_KP_Right:
3261 if (event->state & GDK_CONTROL_MASK)
3262 calendar_set_month_next (calendar);
3265 move_focus (calendar, 1);
3266 calendar_invalidate_day (calendar, old_focus_row, old_focus_col);
3267 calendar_invalidate_day (calendar, priv->focus_row,
3274 if (event->state & GDK_CONTROL_MASK)
3275 calendar_set_year_prev (calendar);
3278 if (priv->focus_row > 0)
3280 if (priv->focus_row < 0)
3281 priv->focus_row = 5;
3282 if (priv->focus_col < 0)
3283 priv->focus_col = 6;
3284 calendar_invalidate_day (calendar, old_focus_row, old_focus_col);
3285 calendar_invalidate_day (calendar, priv->focus_row,
3289 case GDK_KEY_KP_Down:
3292 if (event->state & GDK_CONTROL_MASK)
3293 calendar_set_year_next (calendar);
3296 if (priv->focus_row < 5)
3298 if (priv->focus_col < 0)
3299 priv->focus_col = 0;
3300 calendar_invalidate_day (calendar, old_focus_row, old_focus_col);
3301 calendar_invalidate_day (calendar, priv->focus_row,
3305 case GDK_KEY_KP_Space:
3307 row = priv->focus_row;
3308 col = priv->focus_col;
3310 if (row > -1 && col > -1)
3314 day = priv->day[row][col];
3315 if (priv->day_month[row][col] == MONTH_PREV)
3316 calendar_set_month_prev (calendar);
3317 else if (priv->day_month[row][col] == MONTH_NEXT)
3318 calendar_set_month_next (calendar);
3320 calendar_select_and_focus_day (calendar, day);
3328 /****************************************
3329 * Misc widget methods *
3330 ****************************************/
3333 gtk_calendar_state_flags_changed (GtkWidget *widget,
3334 GtkStateFlags previous_state)
3336 GtkCalendar *calendar = GTK_CALENDAR (widget);
3337 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3340 if (!gtk_widget_is_sensitive (widget))
3343 calendar_stop_spinning (calendar);
3346 for (i = 0; i < 4; i++)
3347 if (gtk_widget_is_sensitive (widget))
3348 priv->arrow_state[i] &= ~(GTK_STATE_FLAG_INSENSITIVE);
3350 priv->arrow_state[i] |= GTK_STATE_FLAG_INSENSITIVE;
3354 gtk_calendar_grab_notify (GtkWidget *widget,
3355 gboolean was_grabbed)
3358 calendar_stop_spinning (GTK_CALENDAR (widget));
3362 gtk_calendar_focus_out (GtkWidget *widget,
3363 GdkEventFocus *event)
3365 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3366 GtkCalendar *calendar = GTK_CALENDAR (widget);
3368 calendar_queue_refresh (calendar);
3369 calendar_stop_spinning (calendar);
3377 /****************************************
3379 ****************************************/
3382 gtk_calendar_drag_data_get (GtkWidget *widget,
3383 GdkDragContext *context,
3384 GtkSelectionData *selection_data,
3388 GtkCalendar *calendar = GTK_CALENDAR (widget);
3389 GtkCalendarPrivate *priv = calendar->priv;
3394 date = g_date_new_dmy (priv->selected_day, priv->month + 1, priv->year);
3395 len = g_date_strftime (str, 127, "%x", date);
3396 gtk_selection_data_set_text (selection_data, str, len);
3401 /* Get/set whether drag_motion requested the drag data and
3402 * drag_data_received should thus not actually insert the data,
3403 * since the data doesn't result from a drop.
3406 set_status_pending (GdkDragContext *context,
3407 GdkDragAction suggested_action)
3409 g_object_set_data (G_OBJECT (context),
3410 I_("gtk-calendar-status-pending"),
3411 GINT_TO_POINTER (suggested_action));
3414 static GdkDragAction
3415 get_status_pending (GdkDragContext *context)
3417 return GPOINTER_TO_INT (g_object_get_data (G_OBJECT (context),
3418 "gtk-calendar-status-pending"));
3422 gtk_calendar_drag_leave (GtkWidget *widget,
3423 GdkDragContext *context,
3426 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3428 priv->drag_highlight = 0;
3429 gtk_drag_unhighlight (widget);
3434 gtk_calendar_drag_motion (GtkWidget *widget,
3435 GdkDragContext *context,
3440 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3443 if (!priv->drag_highlight)
3445 priv->drag_highlight = 1;
3446 gtk_drag_highlight (widget);
3449 target = gtk_drag_dest_find_target (widget, context, NULL);
3450 if (target == GDK_NONE || gdk_drag_context_get_suggested_action (context) == 0)
3451 gdk_drag_status (context, 0, time);
3454 set_status_pending (context, gdk_drag_context_get_suggested_action (context));
3455 gtk_drag_get_data (widget, context, target, time);
3462 gtk_calendar_drag_drop (GtkWidget *widget,
3463 GdkDragContext *context,
3470 target = gtk_drag_dest_find_target (widget, context, NULL);
3471 if (target != GDK_NONE)
3473 gtk_drag_get_data (widget, context,
3483 gtk_calendar_drag_data_received (GtkWidget *widget,
3484 GdkDragContext *context,
3487 GtkSelectionData *selection_data,
3491 GtkCalendar *calendar = GTK_CALENDAR (widget);
3492 GtkCalendarPrivate *priv = calendar->priv;
3493 guint day, month, year;
3496 GdkDragAction suggested_action;
3498 suggested_action = get_status_pending (context);
3500 if (suggested_action)
3502 set_status_pending (context, 0);
3504 /* We are getting this data due to a request in drag_motion,
3505 * rather than due to a request in drag_drop, so we are just
3506 * supposed to call drag_status, not actually paste in the
3509 str = (gchar*) gtk_selection_data_get_text (selection_data);
3513 date = g_date_new ();
3514 g_date_set_parse (date, str);
3515 if (!g_date_valid (date))
3516 suggested_action = 0;
3521 suggested_action = 0;
3523 gdk_drag_status (context, suggested_action, time);
3528 date = g_date_new ();
3529 str = (gchar*) gtk_selection_data_get_text (selection_data);
3532 g_date_set_parse (date, str);
3536 if (!g_date_valid (date))
3538 g_warning ("Received invalid date data\n");
3540 gtk_drag_finish (context, FALSE, FALSE, time);
3544 day = g_date_get_day (date);
3545 month = g_date_get_month (date);
3546 year = g_date_get_year (date);
3549 gtk_drag_finish (context, TRUE, FALSE, time);
3552 g_object_freeze_notify (G_OBJECT (calendar));
3553 if (!(priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
3554 && (priv->display_flags & GTK_CALENDAR_SHOW_HEADING))
3555 gtk_calendar_select_month (calendar, month - 1, year);
3556 gtk_calendar_select_day (calendar, day);
3557 g_object_thaw_notify (G_OBJECT (calendar));
3561 /****************************************
3563 ****************************************/
3568 * Creates a new calendar, with the current date being selected.
3570 * Return value: a newly #GtkCalendar widget
3573 gtk_calendar_new (void)
3575 return g_object_new (GTK_TYPE_CALENDAR, NULL);
3579 * gtk_calendar_get_display_options:
3580 * @calendar: a #GtkCalendar
3582 * Returns the current display options of @calendar.
3584 * Return value: the display options.
3588 GtkCalendarDisplayOptions
3589 gtk_calendar_get_display_options (GtkCalendar *calendar)
3591 g_return_val_if_fail (GTK_IS_CALENDAR (calendar), 0);
3593 return calendar->priv->display_flags;
3597 * gtk_calendar_set_display_options:
3598 * @calendar: a #GtkCalendar
3599 * @flags: the display options to set
3601 * Sets display options (whether to display the heading and the month
3607 gtk_calendar_set_display_options (GtkCalendar *calendar,
3608 GtkCalendarDisplayOptions flags)
3610 GtkWidget *widget = GTK_WIDGET (calendar);
3611 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
3613 GtkCalendarDisplayOptions old_flags;
3615 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3617 old_flags = priv->display_flags;
3619 if (gtk_widget_get_realized (widget))
3621 if ((flags ^ priv->display_flags) & GTK_CALENDAR_NO_MONTH_CHANGE)
3624 if (! (flags & GTK_CALENDAR_NO_MONTH_CHANGE)
3625 && (priv->display_flags & GTK_CALENDAR_SHOW_HEADING))
3627 priv->display_flags &= ~GTK_CALENDAR_NO_MONTH_CHANGE;
3628 calendar_realize_arrows (calendar);
3629 if (gtk_widget_get_mapped (widget))
3630 calendar_map_arrows (calendar);
3634 calendar_unrealize_arrows (calendar);
3638 if ((flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_HEADING)
3642 if (flags & GTK_CALENDAR_SHOW_HEADING)
3644 priv->display_flags |= GTK_CALENDAR_SHOW_HEADING;
3645 calendar_realize_arrows (calendar);
3646 if (gtk_widget_get_mapped (widget))
3647 calendar_map_arrows (calendar);
3651 calendar_unrealize_arrows (calendar);
3656 if ((flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_DAY_NAMES)
3660 if (flags & GTK_CALENDAR_SHOW_DAY_NAMES)
3661 priv->display_flags |= GTK_CALENDAR_SHOW_DAY_NAMES;
3664 if ((flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
3668 if (flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
3669 priv->display_flags |= GTK_CALENDAR_SHOW_WEEK_NUMBERS;
3672 if ((flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_DETAILS)
3675 priv->display_flags = flags;
3677 gtk_widget_queue_resize (GTK_WIDGET (calendar));
3680 priv->display_flags = flags;
3682 g_object_freeze_notify (G_OBJECT (calendar));
3683 if ((old_flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_HEADING)
3684 g_object_notify (G_OBJECT (calendar), "show-heading");
3685 if ((old_flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_DAY_NAMES)
3686 g_object_notify (G_OBJECT (calendar), "show-day-names");
3687 if ((old_flags ^ priv->display_flags) & GTK_CALENDAR_NO_MONTH_CHANGE)
3688 g_object_notify (G_OBJECT (calendar), "no-month-change");
3689 if ((old_flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
3690 g_object_notify (G_OBJECT (calendar), "show-week-numbers");
3691 g_object_thaw_notify (G_OBJECT (calendar));
3695 * gtk_calendar_select_month:
3696 * @calendar: a #GtkCalendar
3697 * @month: a month number between 0 and 11.
3698 * @year: the year the month is in.
3700 * Shifts the calendar to a different month.
3703 gtk_calendar_select_month (GtkCalendar *calendar,
3707 GtkCalendarPrivate *priv;
3709 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3710 g_return_if_fail (month <= 11);
3712 priv = calendar->priv;
3714 priv->month = month;
3717 calendar_compute_days (calendar);
3718 calendar_queue_refresh (calendar);
3720 g_object_freeze_notify (G_OBJECT (calendar));
3721 g_object_notify (G_OBJECT (calendar), "month");
3722 g_object_notify (G_OBJECT (calendar), "year");
3723 g_object_thaw_notify (G_OBJECT (calendar));
3725 g_signal_emit (calendar,
3726 gtk_calendar_signals[MONTH_CHANGED_SIGNAL],
3731 * gtk_calendar_select_day:
3732 * @calendar: a #GtkCalendar.
3733 * @day: the day number between 1 and 31, or 0 to unselect
3734 * the currently selected day.
3736 * Selects a day from the current month.
3739 gtk_calendar_select_day (GtkCalendar *calendar,
3742 GtkCalendarPrivate *priv;
3744 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3745 g_return_if_fail (day <= 31);
3747 priv = calendar->priv;
3749 /* Deselect the old day */
3750 if (priv->selected_day > 0)
3754 selected_day = priv->selected_day;
3755 priv->selected_day = 0;
3756 if (gtk_widget_is_drawable (GTK_WIDGET (calendar)))
3757 calendar_invalidate_day_num (calendar, selected_day);
3760 priv->selected_day = day;
3762 /* Select the new day */
3765 if (gtk_widget_is_drawable (GTK_WIDGET (calendar)))
3766 calendar_invalidate_day_num (calendar, day);
3769 g_object_notify (G_OBJECT (calendar), "day");
3771 g_signal_emit (calendar,
3772 gtk_calendar_signals[DAY_SELECTED_SIGNAL],
3777 * gtk_calendar_clear_marks:
3778 * @calendar: a #GtkCalendar
3780 * Remove all visual markers.
3783 gtk_calendar_clear_marks (GtkCalendar *calendar)
3785 GtkCalendarPrivate *priv;
3788 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3790 priv = calendar->priv;
3792 for (day = 0; day < 31; day++)
3794 priv->marked_date[day] = FALSE;
3797 priv->num_marked_dates = 0;
3798 calendar_queue_refresh (calendar);
3802 * gtk_calendar_mark_day:
3803 * @calendar: a #GtkCalendar
3804 * @day: the day number to mark between 1 and 31.
3806 * Places a visual marker on a particular day.
3809 gtk_calendar_mark_day (GtkCalendar *calendar,
3812 GtkCalendarPrivate *priv;
3814 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3816 priv = calendar->priv;
3818 if (day >= 1 && day <= 31 && !priv->marked_date[day-1])
3820 priv->marked_date[day - 1] = TRUE;
3821 priv->num_marked_dates++;
3822 calendar_invalidate_day_num (calendar, day);
3827 * gtk_calendar_get_day_is_marked:
3828 * @calendar: a #GtkCalendar
3829 * @day: the day number between 1 and 31.
3831 * Returns if the @day of the @calendar is already marked.
3833 * Returns: whether the day is marked.
3838 gtk_calendar_get_day_is_marked (GtkCalendar *calendar,
3841 GtkCalendarPrivate *priv;
3843 g_return_val_if_fail (GTK_IS_CALENDAR (calendar), FALSE);
3845 priv = calendar->priv;
3847 if (day >= 1 && day <= 31)
3848 return priv->marked_date[day - 1];
3854 * gtk_calendar_unmark_day:
3855 * @calendar: a #GtkCalendar.
3856 * @day: the day number to unmark between 1 and 31.
3858 * Removes the visual marker from a particular day.
3861 gtk_calendar_unmark_day (GtkCalendar *calendar,
3864 GtkCalendarPrivate *priv;
3866 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3868 priv = calendar->priv;
3870 if (day >= 1 && day <= 31 && priv->marked_date[day-1])
3872 priv->marked_date[day - 1] = FALSE;
3873 priv->num_marked_dates--;
3874 calendar_invalidate_day_num (calendar, day);
3879 * gtk_calendar_get_date:
3880 * @calendar: a #GtkCalendar
3881 * @year: (allow-none): location to store the year as a decimal number (e.g. 2011), or %NULL
3882 * @month: (allow-none): location to store the month number (between 0 and 11), or %NULL
3883 * @day: (allow-none): location to store the day number (between 1 and 31), or %NULL
3885 * Obtains the selected date from a #GtkCalendar.
3888 gtk_calendar_get_date (GtkCalendar *calendar,
3893 GtkCalendarPrivate *priv;
3895 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3897 priv = calendar->priv;
3903 *month = priv->month;
3906 *day = priv->selected_day;
3910 * gtk_calendar_set_detail_func:
3911 * @calendar: a #GtkCalendar.
3912 * @func: a function providing details for each day.
3913 * @data: data to pass to @func invokations.
3914 * @destroy: a function for releasing @data.
3916 * Installs a function which provides Pango markup with detail information
3917 * for each day. Examples for such details are holidays or appointments. That
3918 * information is shown below each day when #GtkCalendar:show-details is set.
3919 * A tooltip containing with full detail information is provided, if the entire
3920 * text should not fit into the details area, or if #GtkCalendar:show-details
3923 * The size of the details area can be restricted by setting the
3924 * #GtkCalendar:detail-width-chars and #GtkCalendar:detail-height-rows
3930 gtk_calendar_set_detail_func (GtkCalendar *calendar,
3931 GtkCalendarDetailFunc func,
3933 GDestroyNotify destroy)
3935 GtkCalendarPrivate *priv;
3937 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3939 priv = GTK_CALENDAR_GET_PRIVATE (calendar);
3941 if (priv->detail_func_destroy)
3942 priv->detail_func_destroy (priv->detail_func_user_data);
3944 priv->detail_func = func;
3945 priv->detail_func_user_data = data;
3946 priv->detail_func_destroy = destroy;
3948 gtk_widget_set_has_tooltip (GTK_WIDGET (calendar),
3949 NULL != priv->detail_func);
3950 gtk_widget_queue_resize (GTK_WIDGET (calendar));
3954 * gtk_calendar_set_detail_width_chars:
3955 * @calendar: a #GtkCalendar.
3956 * @chars: detail width in characters.
3958 * Updates the width of detail cells.
3959 * See #GtkCalendar:detail-width-chars.
3964 gtk_calendar_set_detail_width_chars (GtkCalendar *calendar,
3967 GtkCalendarPrivate *priv;
3969 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3971 priv = GTK_CALENDAR_GET_PRIVATE (calendar);
3973 if (chars != priv->detail_width_chars)
3975 priv->detail_width_chars = chars;
3976 g_object_notify (G_OBJECT (calendar), "detail-width-chars");
3977 gtk_widget_queue_resize_no_redraw (GTK_WIDGET (calendar));
3982 * gtk_calendar_set_detail_height_rows:
3983 * @calendar: a #GtkCalendar.
3984 * @rows: detail height in rows.
3986 * Updates the height of detail cells.
3987 * See #GtkCalendar:detail-height-rows.
3992 gtk_calendar_set_detail_height_rows (GtkCalendar *calendar,
3995 GtkCalendarPrivate *priv;
3997 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3999 priv = GTK_CALENDAR_GET_PRIVATE (calendar);
4001 if (rows != priv->detail_height_rows)
4003 priv->detail_height_rows = rows;
4004 g_object_notify (G_OBJECT (calendar), "detail-height-rows");
4005 gtk_widget_queue_resize_no_redraw (GTK_WIDGET (calendar));
4010 * gtk_calendar_get_detail_width_chars:
4011 * @calendar: a #GtkCalendar.
4013 * Queries the width of detail cells, in characters.
4014 * See #GtkCalendar:detail-width-chars.
4018 * Return value: The width of detail cells, in characters.
4021 gtk_calendar_get_detail_width_chars (GtkCalendar *calendar)
4023 g_return_val_if_fail (GTK_IS_CALENDAR (calendar), 0);
4024 return GTK_CALENDAR_GET_PRIVATE (calendar)->detail_width_chars;
4028 * gtk_calendar_get_detail_height_rows:
4029 * @calendar: a #GtkCalendar.
4031 * Queries the height of detail cells, in rows.
4032 * See #GtkCalendar:detail-width-chars.
4036 * Return value: The height of detail cells, in rows.
4039 gtk_calendar_get_detail_height_rows (GtkCalendar *calendar)
4041 g_return_val_if_fail (GTK_IS_CALENDAR (calendar), 0);
4042 return GTK_CALENDAR_GET_PRIVATE (calendar)->detail_height_rows;