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 calendar, one month at a time. It
38 * 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 displayed
42 * month using gtk_calendar_select_day().
44 * To place a visual marker on a particular day, use gtk_calendar_mark_day() and
45 * to remove the marker, gtk_calendar_unmark_day(). Alternative, all marks can
46 * 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().
57 #ifdef HAVE_SYS_TIME_H
60 #ifdef HAVE__NL_TIME_FIRST_WEEKDAY
73 #include "gtkcalendar.h"
77 #include "gtkmarshalers.h"
78 #include "gtktooltip.h"
79 #include "gtkprivate.h"
80 #include "gdk/gdkkeysyms.h"
82 /***************************************************************************/
83 /* The following date routines are taken from the lib_date package.
84 * They have been minimally edited to avoid conflict with types defined
88 static const guint month_length[2][13] =
90 { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
91 { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
94 static const guint days_in_months[2][14] =
96 { 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
97 { 0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
100 static glong calc_days(guint year, guint mm, guint dd);
101 static guint day_of_week(guint year, guint mm, guint dd);
102 static glong dates_difference(guint year1, guint mm1, guint dd1,
103 guint year2, guint mm2, guint dd2);
104 static guint weeks_in_year(guint year);
109 return((((year % 4) == 0) && ((year % 100) != 0)) || ((year % 400) == 0));
113 day_of_week (guint year, guint mm, guint dd)
117 days = calc_days(year, mm, dd);
124 return( (guint) days );
127 static guint weeks_in_year(guint year)
129 return(52 + ((day_of_week(year,1,1)==4) || (day_of_week(year,12,31)==4)));
133 check_date(guint year, guint mm, guint dd)
135 if (year < 1) return FALSE;
136 if ((mm < 1) || (mm > 12)) return FALSE;
137 if ((dd < 1) || (dd > month_length[leap(year)][mm])) return FALSE;
142 week_number(guint year, guint mm, guint dd)
146 first = day_of_week(year,1,1) - 1;
147 return( (guint) ( (dates_difference(year,1,1, year,mm,dd) + first) / 7L ) +
152 year_to_days(guint year)
154 return( year * 365L + (year / 4) - (year / 100) + (year / 400) );
159 calc_days(guint year, guint mm, guint dd)
163 if (year < 1) return(0L);
164 if ((mm < 1) || (mm > 12)) return(0L);
165 if ((dd < 1) || (dd > month_length[(lp = leap(year))][mm])) return(0L);
166 return( year_to_days(--year) + days_in_months[lp][mm] + dd );
170 week_of_year(guint *week, guint *year, guint mm, guint dd)
172 if (check_date(*year,mm,dd))
174 *week = week_number(*year,mm,dd);
176 *week = weeks_in_year(--(*year));
177 else if (*week > weeks_in_year(*year))
188 dates_difference(guint year1, guint mm1, guint dd1,
189 guint year2, guint mm2, guint dd2)
191 return( calc_days(year2, mm2, dd2) - calc_days(year1, mm1, dd1) );
194 /*** END OF lib_date routines ********************************************/
196 /* Spacing around day/week headers and main area, inside those windows */
197 #define CALENDAR_MARGIN 0
199 #define DAY_XSEP 0 /* not really good for small calendar */
200 #define DAY_YSEP 0 /* not really good for small calendar */
202 #define SCROLL_DELAY_FACTOR 5
205 #define HEADER_FG_COLOR(widget) (& gtk_widget_get_style (widget)->fg[gtk_widget_get_state (widget)])
206 #define HEADER_BG_COLOR(widget) (& gtk_widget_get_style (widget)->bg[gtk_widget_get_state (widget)])
207 #define SELECTED_BG_COLOR(widget) (& gtk_widget_get_style (widget)->base[gtk_widget_has_focus (widget) ? GTK_STATE_SELECTED : GTK_STATE_ACTIVE])
208 #define SELECTED_FG_COLOR(widget) (& gtk_widget_get_style (widget)->text[gtk_widget_has_focus (widget) ? GTK_STATE_SELECTED : GTK_STATE_ACTIVE])
209 #define NORMAL_DAY_COLOR(widget) (& gtk_widget_get_style (widget)->text[gtk_widget_get_state (widget)])
210 #define PREV_MONTH_COLOR(widget) (& gtk_widget_get_style (widget)->mid[gtk_widget_get_state (widget)])
211 #define NEXT_MONTH_COLOR(widget) (& gtk_widget_get_style (widget)->mid[gtk_widget_get_state (widget)])
212 #define MARKED_COLOR(widget) (& gtk_widget_get_style (widget)->text[gtk_widget_get_state (widget)])
213 #define BACKGROUND_COLOR(widget) (& gtk_widget_get_style (widget)->base[gtk_widget_get_state (widget)])
214 #define HIGHLIGHT_BACK_COLOR(widget) (& gtk_widget_get_style (widget)->mid[gtk_widget_get_state (widget)])
230 MONTH_CHANGED_SIGNAL,
232 DAY_SELECTED_DOUBLE_CLICK_SIGNAL,
248 PROP_NO_MONTH_CHANGE,
249 PROP_SHOW_WEEK_NUMBERS,
251 PROP_DETAIL_WIDTH_CHARS,
252 PROP_DETAIL_HEIGHT_ROWS
255 static guint gtk_calendar_signals[LAST_SIGNAL] = { 0 };
257 struct _GtkCalendarPrivate
259 GtkCalendarDisplayOptions display_flags;
260 GtkStyle *header_style;
261 GtkStyle *label_style;
263 GdkColor marked_date_color[31];
265 GdkWindow *arrow_win[4];
267 gchar grow_space [32];
273 gint day_month[6][7];
276 gint num_marked_dates;
277 gint marked_date[31];
286 guint arrow_state[4];
288 guint max_month_width;
289 guint max_year_width;
295 guint max_day_char_width;
296 guint max_day_char_ascent;
297 guint max_day_char_descent;
298 guint max_label_char_ascent;
299 guint max_label_char_descent;
300 guint max_week_char_width;
303 guint year_before : 1;
305 guint need_timer : 1;
308 guint drag_highlight : 1;
318 /* Optional callback, used to display extra information for each day. */
319 GtkCalendarDetailFunc detail_func;
320 gpointer detail_func_user_data;
321 GDestroyNotify detail_func_destroy;
323 /* Size requistion for details provided by the hook. */
324 gint detail_height_rows;
325 gint detail_width_chars;
326 gint detail_overflow[6];
329 #define GTK_CALENDAR_GET_PRIVATE(widget) (GTK_CALENDAR (widget)->priv)
331 static void gtk_calendar_finalize (GObject *calendar);
332 static void gtk_calendar_destroy (GtkWidget *widget);
333 static void gtk_calendar_set_property (GObject *object,
337 static void gtk_calendar_get_property (GObject *object,
342 static void gtk_calendar_realize (GtkWidget *widget);
343 static void gtk_calendar_unrealize (GtkWidget *widget);
344 static void gtk_calendar_get_preferred_width (GtkWidget *widget,
347 static void gtk_calendar_get_preferred_height (GtkWidget *widget,
350 static void gtk_calendar_size_allocate (GtkWidget *widget,
351 GtkAllocation *allocation);
352 static gboolean gtk_calendar_draw (GtkWidget *widget,
354 static gboolean gtk_calendar_button_press (GtkWidget *widget,
355 GdkEventButton *event);
356 static gboolean gtk_calendar_button_release (GtkWidget *widget,
357 GdkEventButton *event);
358 static gboolean gtk_calendar_motion_notify (GtkWidget *widget,
359 GdkEventMotion *event);
360 static gboolean gtk_calendar_enter_notify (GtkWidget *widget,
361 GdkEventCrossing *event);
362 static gboolean gtk_calendar_leave_notify (GtkWidget *widget,
363 GdkEventCrossing *event);
364 static gboolean gtk_calendar_scroll (GtkWidget *widget,
365 GdkEventScroll *event);
366 static gboolean gtk_calendar_key_press (GtkWidget *widget,
368 static gboolean gtk_calendar_focus_out (GtkWidget *widget,
369 GdkEventFocus *event);
370 static void gtk_calendar_grab_notify (GtkWidget *widget,
371 gboolean was_grabbed);
372 static void gtk_calendar_state_changed (GtkWidget *widget,
373 GtkStateType previous_state);
374 static gboolean gtk_calendar_query_tooltip (GtkWidget *widget,
377 gboolean keyboard_mode,
378 GtkTooltip *tooltip);
380 static void gtk_calendar_drag_data_get (GtkWidget *widget,
381 GdkDragContext *context,
382 GtkSelectionData *selection_data,
385 static void gtk_calendar_drag_data_received (GtkWidget *widget,
386 GdkDragContext *context,
389 GtkSelectionData *selection_data,
392 static gboolean gtk_calendar_drag_motion (GtkWidget *widget,
393 GdkDragContext *context,
397 static void gtk_calendar_drag_leave (GtkWidget *widget,
398 GdkDragContext *context,
400 static gboolean gtk_calendar_drag_drop (GtkWidget *widget,
401 GdkDragContext *context,
406 static void calendar_start_spinning (GtkCalendar *calendar,
408 static void calendar_stop_spinning (GtkCalendar *calendar);
410 static void calendar_invalidate_day (GtkCalendar *widget,
413 static void calendar_invalidate_day_num (GtkCalendar *widget,
415 static void calendar_invalidate_arrow (GtkCalendar *widget,
418 static void calendar_compute_days (GtkCalendar *calendar);
419 static gint calendar_get_xsep (GtkCalendar *calendar);
420 static gint calendar_get_ysep (GtkCalendar *calendar);
421 static gint calendar_get_inner_border (GtkCalendar *calendar);
423 static char *default_abbreviated_dayname[7];
424 static char *default_monthname[12];
426 G_DEFINE_TYPE (GtkCalendar, gtk_calendar, GTK_TYPE_WIDGET)
429 gtk_calendar_class_init (GtkCalendarClass *class)
431 GObjectClass *gobject_class;
432 GtkWidgetClass *widget_class;
434 gobject_class = (GObjectClass*) class;
435 widget_class = (GtkWidgetClass*) class;
437 gobject_class->set_property = gtk_calendar_set_property;
438 gobject_class->get_property = gtk_calendar_get_property;
439 gobject_class->finalize = gtk_calendar_finalize;
441 widget_class->destroy = gtk_calendar_destroy;
442 widget_class->realize = gtk_calendar_realize;
443 widget_class->unrealize = gtk_calendar_unrealize;
444 widget_class->draw = gtk_calendar_draw;
445 widget_class->get_preferred_width = gtk_calendar_get_preferred_width;
446 widget_class->get_preferred_height = gtk_calendar_get_preferred_height;
447 widget_class->size_allocate = gtk_calendar_size_allocate;
448 widget_class->button_press_event = gtk_calendar_button_press;
449 widget_class->button_release_event = gtk_calendar_button_release;
450 widget_class->motion_notify_event = gtk_calendar_motion_notify;
451 widget_class->enter_notify_event = gtk_calendar_enter_notify;
452 widget_class->leave_notify_event = gtk_calendar_leave_notify;
453 widget_class->key_press_event = gtk_calendar_key_press;
454 widget_class->scroll_event = gtk_calendar_scroll;
455 widget_class->state_changed = gtk_calendar_state_changed;
456 widget_class->grab_notify = gtk_calendar_grab_notify;
457 widget_class->focus_out_event = gtk_calendar_focus_out;
458 widget_class->query_tooltip = gtk_calendar_query_tooltip;
460 widget_class->drag_data_get = gtk_calendar_drag_data_get;
461 widget_class->drag_motion = gtk_calendar_drag_motion;
462 widget_class->drag_leave = gtk_calendar_drag_leave;
463 widget_class->drag_drop = gtk_calendar_drag_drop;
464 widget_class->drag_data_received = gtk_calendar_drag_data_received;
470 * This property gets initially set to the current year.
472 g_object_class_install_property (gobject_class,
474 g_param_spec_int ("year",
476 P_("The selected year"),
478 GTK_PARAM_READWRITE));
483 * The selected month (as a number between 0 and 11).
484 * This property gets initially set to the current month.
486 g_object_class_install_property (gobject_class,
488 g_param_spec_int ("month",
490 P_("The selected month (as a number between 0 and 11)"),
492 GTK_PARAM_READWRITE));
497 * The selected day (as a number between 1 and 31, or 0
498 * to unselect the currently selected day).
499 * This property gets initially set to the current day.
501 g_object_class_install_property (gobject_class,
503 g_param_spec_int ("day",
505 P_("The selected day (as a number between 1 and 31, or 0 to unselect the currently selected day)"),
507 GTK_PARAM_READWRITE));
510 * GtkCalendar:show-heading:
512 * Determines whether a heading is displayed.
516 g_object_class_install_property (gobject_class,
518 g_param_spec_boolean ("show-heading",
520 P_("If TRUE, a heading is displayed"),
522 GTK_PARAM_READWRITE));
525 * GtkCalendar:show-day-names:
527 * Determines whether day names are displayed.
531 g_object_class_install_property (gobject_class,
533 g_param_spec_boolean ("show-day-names",
534 P_("Show Day Names"),
535 P_("If TRUE, day names are displayed"),
537 GTK_PARAM_READWRITE));
539 * GtkCalendar:no-month-change:
541 * Determines whether the selected month can be changed.
545 g_object_class_install_property (gobject_class,
546 PROP_NO_MONTH_CHANGE,
547 g_param_spec_boolean ("no-month-change",
548 P_("No Month Change"),
549 P_("If TRUE, the selected month cannot be changed"),
551 GTK_PARAM_READWRITE));
554 * GtkCalendar:show-week-numbers:
556 * Determines whether week numbers are displayed.
560 g_object_class_install_property (gobject_class,
561 PROP_SHOW_WEEK_NUMBERS,
562 g_param_spec_boolean ("show-week-numbers",
563 P_("Show Week Numbers"),
564 P_("If TRUE, week numbers are displayed"),
566 GTK_PARAM_READWRITE));
569 * GtkCalendar:detail-width-chars:
571 * Width of a detail cell, in characters.
572 * A value of 0 allows any width. See gtk_calendar_set_detail_func().
576 g_object_class_install_property (gobject_class,
577 PROP_DETAIL_WIDTH_CHARS,
578 g_param_spec_int ("detail-width-chars",
580 P_("Details width in characters"),
582 GTK_PARAM_READWRITE));
585 * GtkCalendar:detail-height-rows:
587 * Height of a detail cell, in rows.
588 * A value of 0 allows any width. See gtk_calendar_set_detail_func().
592 g_object_class_install_property (gobject_class,
593 PROP_DETAIL_HEIGHT_ROWS,
594 g_param_spec_int ("detail-height-rows",
595 P_("Details Height"),
596 P_("Details height in rows"),
598 GTK_PARAM_READWRITE));
601 * GtkCalendar:show-details:
603 * Determines whether details are shown directly in the widget, or if they are
604 * available only as tooltip. When this property is set days with details are
609 g_object_class_install_property (gobject_class,
611 g_param_spec_boolean ("show-details",
613 P_("If TRUE, details are shown"),
615 GTK_PARAM_READWRITE));
619 * GtkCalendar:inner-border
621 * The spacing around the day/week headers and main area.
623 gtk_widget_class_install_style_property (widget_class,
624 g_param_spec_int ("inner-border",
626 P_("Inner border space"),
628 GTK_PARAM_READABLE));
631 * GtkCalndar:vertical-separation
633 * Separation between day headers and main area.
635 gtk_widget_class_install_style_property (widget_class,
636 g_param_spec_int ("vertical-separation",
637 P_("Vertical separation"),
638 P_("Space between day headers and main area"),
640 GTK_PARAM_READABLE));
643 * GtkCalendar:horizontal-separation
645 * Separation between week headers and main area.
647 gtk_widget_class_install_style_property (widget_class,
648 g_param_spec_int ("horizontal-separation",
649 P_("Horizontal separation"),
650 P_("Space between week headers and main area"),
652 GTK_PARAM_READABLE));
655 * GtkCalendar::month-changed:
656 * @calendar: the object which received the signal.
658 * Emitted when the user clicks a button to change the selected month on a
661 gtk_calendar_signals[MONTH_CHANGED_SIGNAL] =
662 g_signal_new (I_("month-changed"),
663 G_OBJECT_CLASS_TYPE (gobject_class),
665 G_STRUCT_OFFSET (GtkCalendarClass, month_changed),
667 _gtk_marshal_VOID__VOID,
671 * GtkCalendar::day-selected:
672 * @calendar: the object which received the signal.
674 * Emitted when the user selects a day.
676 gtk_calendar_signals[DAY_SELECTED_SIGNAL] =
677 g_signal_new (I_("day-selected"),
678 G_OBJECT_CLASS_TYPE (gobject_class),
680 G_STRUCT_OFFSET (GtkCalendarClass, day_selected),
682 _gtk_marshal_VOID__VOID,
686 * GtkCalendar::day-selected-double-click:
687 * @calendar: the object which received the signal.
689 * Emitted when the user double-clicks a day.
691 gtk_calendar_signals[DAY_SELECTED_DOUBLE_CLICK_SIGNAL] =
692 g_signal_new (I_("day-selected-double-click"),
693 G_OBJECT_CLASS_TYPE (gobject_class),
695 G_STRUCT_OFFSET (GtkCalendarClass, day_selected_double_click),
697 _gtk_marshal_VOID__VOID,
701 * GtkCalendar::prev-month:
702 * @calendar: the object which received the signal.
704 * Emitted when the user switched to the previous month.
706 gtk_calendar_signals[PREV_MONTH_SIGNAL] =
707 g_signal_new (I_("prev-month"),
708 G_OBJECT_CLASS_TYPE (gobject_class),
710 G_STRUCT_OFFSET (GtkCalendarClass, prev_month),
712 _gtk_marshal_VOID__VOID,
716 * GtkCalendar::next-month:
717 * @calendar: the object which received the signal.
719 * Emitted when the user switched to the next month.
721 gtk_calendar_signals[NEXT_MONTH_SIGNAL] =
722 g_signal_new (I_("next-month"),
723 G_OBJECT_CLASS_TYPE (gobject_class),
725 G_STRUCT_OFFSET (GtkCalendarClass, next_month),
727 _gtk_marshal_VOID__VOID,
731 * GtkCalendar::prev-year:
732 * @calendar: the object which received the signal.
734 * Emitted when user switched to the previous year.
736 gtk_calendar_signals[PREV_YEAR_SIGNAL] =
737 g_signal_new (I_("prev-year"),
738 G_OBJECT_CLASS_TYPE (gobject_class),
740 G_STRUCT_OFFSET (GtkCalendarClass, prev_year),
742 _gtk_marshal_VOID__VOID,
746 * GtkCalendar::next-year:
747 * @calendar: the object which received the signal.
749 * Emitted when user switched to the next year.
751 gtk_calendar_signals[NEXT_YEAR_SIGNAL] =
752 g_signal_new (I_("next-year"),
753 G_OBJECT_CLASS_TYPE (gobject_class),
755 G_STRUCT_OFFSET (GtkCalendarClass, next_year),
757 _gtk_marshal_VOID__VOID,
760 g_type_class_add_private (gobject_class, sizeof (GtkCalendarPrivate));
764 gtk_calendar_init (GtkCalendar *calendar)
766 GtkWidget *widget = GTK_WIDGET (calendar);
771 wchar_t wbuffer[100];
776 GtkCalendarPrivate *priv;
778 #ifdef HAVE__NL_TIME_FIRST_WEEKDAY
779 union { unsigned int word; char *string; } langinfo;
780 gint week_1stday = 0;
781 gint first_weekday = 1;
787 priv = calendar->priv = G_TYPE_INSTANCE_GET_PRIVATE (calendar,
791 gtk_widget_set_can_focus (widget, TRUE);
792 gtk_widget_set_has_window (widget, FALSE);
794 if (!default_abbreviated_dayname[0])
798 tmp_time= (i+3)*86400;
799 strftime ( buffer, sizeof (buffer), "%a", gmtime (&tmp_time));
800 default_abbreviated_dayname[i] = g_locale_to_utf8 (buffer, -1, NULL, NULL, NULL);
802 if (!GetLocaleInfoW (GetThreadLocale (), LOCALE_SABBREVDAYNAME1 + (i+6)%7,
803 wbuffer, G_N_ELEMENTS (wbuffer)))
804 default_abbreviated_dayname[i] = g_strdup_printf ("(%d)", i);
806 default_abbreviated_dayname[i] = g_utf16_to_utf8 (wbuffer, -1, NULL, NULL, NULL);
810 if (!default_monthname[0])
815 strftime ( buffer, sizeof (buffer), "%B", gmtime (&tmp_time));
816 default_monthname[i] = g_locale_to_utf8 (buffer, -1, NULL, NULL, NULL);
818 if (!GetLocaleInfoW (GetThreadLocale (), LOCALE_SMONTHNAME1 + i,
819 wbuffer, G_N_ELEMENTS (wbuffer)))
820 default_monthname[i] = g_strdup_printf ("(%d)", i);
822 default_monthname[i] = g_utf16_to_utf8 (wbuffer, -1, NULL, NULL, NULL);
828 tm = localtime (&secs);
829 priv->month = tm->tm_mon;
830 priv->year = 1900 + tm->tm_year;
833 priv->marked_date[i] = FALSE;
834 priv->num_marked_dates = 0;
835 priv->selected_day = tm->tm_mday;
837 priv->display_flags = (GTK_CALENDAR_SHOW_HEADING |
838 GTK_CALENDAR_SHOW_DAY_NAMES |
839 GTK_CALENDAR_SHOW_DETAILS);
841 priv->focus_row = -1;
842 priv->focus_col = -1;
844 priv->max_year_width = 0;
845 priv->max_month_width = 0;
846 priv->max_day_char_width = 0;
847 priv->max_week_char_width = 0;
849 priv->max_day_char_ascent = 0;
850 priv->max_day_char_descent = 0;
851 priv->max_label_char_ascent = 0;
852 priv->max_label_char_descent = 0;
854 priv->arrow_width = 10;
856 priv->need_timer = 0;
858 priv->click_child = -1;
861 priv->drag_highlight = 0;
863 gtk_drag_dest_set (widget, 0, NULL, 0, GDK_ACTION_COPY);
864 gtk_drag_dest_add_text_targets (widget);
866 priv->year_before = 0;
868 /* Translate to calendar:YM if you want years to be displayed
869 * before months; otherwise translate to calendar:MY.
870 * Do *not* translate it to anything else, if it
871 * it isn't calendar:YM or calendar:MY it will not work.
873 * Note that the ordering described here is logical order, which is
874 * further influenced by BIDI ordering. Thus, if you have a default
875 * text direction of RTL and specify "calendar:YM", then the year
876 * will appear to the right of the month.
878 year_before = _("calendar:MY");
879 if (strcmp (year_before, "calendar:YM") == 0)
880 priv->year_before = 1;
881 else if (strcmp (year_before, "calendar:MY") != 0)
882 g_warning ("Whoever translated calendar:MY did so wrongly.\n");
885 priv->week_start = 0;
888 if (GetLocaleInfoW (GetThreadLocale (), LOCALE_IFIRSTDAYOFWEEK,
889 wbuffer, G_N_ELEMENTS (wbuffer)))
890 week_start = g_utf16_to_utf8 (wbuffer, -1, NULL, NULL, NULL);
892 if (week_start != NULL)
894 priv->week_start = (week_start[0] - '0' + 1) % 7;
898 #ifdef HAVE__NL_TIME_FIRST_WEEKDAY
899 langinfo.string = nl_langinfo (_NL_TIME_FIRST_WEEKDAY);
900 first_weekday = langinfo.string[0];
901 langinfo.string = nl_langinfo (_NL_TIME_WEEK_1STDAY);
902 week_origin = langinfo.word;
903 if (week_origin == 19971130) /* Sunday */
905 else if (week_origin == 19971201) /* Monday */
908 g_warning ("Unknown value of _NL_TIME_WEEK_1STDAY.\n");
910 priv->week_start = (week_1stday + first_weekday - 1) % 7;
912 /* Translate to calendar:week_start:0 if you want Sunday to be the
913 * first day of the week to calendar:week_start:1 if you want Monday
914 * to be the first day of the week, and so on.
916 week_start = _("calendar:week_start:0");
918 if (strncmp (week_start, "calendar:week_start:", 20) == 0)
919 priv->week_start = *(week_start + 20) - '0';
921 priv->week_start = -1;
923 if (priv->week_start < 0 || priv->week_start > 6)
925 g_warning ("Whoever translated calendar:week_start:0 did so wrongly.\n");
926 priv->week_start = 0;
931 calendar_compute_days (calendar);
935 /****************************************
936 * Utility Functions *
937 ****************************************/
940 calendar_queue_refresh (GtkCalendar *calendar)
942 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
944 if (!(priv->detail_func) ||
945 !(priv->display_flags & GTK_CALENDAR_SHOW_DETAILS) ||
946 (priv->detail_width_chars && priv->detail_height_rows))
947 gtk_widget_queue_draw (GTK_WIDGET (calendar));
949 gtk_widget_queue_resize (GTK_WIDGET (calendar));
953 calendar_set_month_next (GtkCalendar *calendar)
956 GtkCalendarPrivate *priv = calendar->priv;
958 if (priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
961 if (priv->month == 11)
969 calendar_compute_days (calendar);
970 g_signal_emit (calendar,
971 gtk_calendar_signals[NEXT_MONTH_SIGNAL],
973 g_signal_emit (calendar,
974 gtk_calendar_signals[MONTH_CHANGED_SIGNAL],
977 month_len = month_length[leap (priv->year)][priv->month + 1];
979 if (month_len < priv->selected_day)
981 priv->selected_day = 0;
982 gtk_calendar_select_day (calendar, month_len);
985 gtk_calendar_select_day (calendar, priv->selected_day);
987 calendar_queue_refresh (calendar);
991 calendar_set_year_prev (GtkCalendar *calendar)
993 GtkCalendarPrivate *priv = calendar->priv;
997 calendar_compute_days (calendar);
998 g_signal_emit (calendar,
999 gtk_calendar_signals[PREV_YEAR_SIGNAL],
1001 g_signal_emit (calendar,
1002 gtk_calendar_signals[MONTH_CHANGED_SIGNAL],
1005 month_len = month_length[leap (priv->year)][priv->month + 1];
1007 if (month_len < priv->selected_day)
1009 priv->selected_day = 0;
1010 gtk_calendar_select_day (calendar, month_len);
1013 gtk_calendar_select_day (calendar, priv->selected_day);
1015 calendar_queue_refresh (calendar);
1019 calendar_set_year_next (GtkCalendar *calendar)
1021 GtkCalendarPrivate *priv = calendar->priv;
1025 calendar_compute_days (calendar);
1026 g_signal_emit (calendar,
1027 gtk_calendar_signals[NEXT_YEAR_SIGNAL],
1029 g_signal_emit (calendar,
1030 gtk_calendar_signals[MONTH_CHANGED_SIGNAL],
1033 month_len = month_length[leap (priv->year)][priv->month + 1];
1035 if (month_len < priv->selected_day)
1037 priv->selected_day = 0;
1038 gtk_calendar_select_day (calendar, month_len);
1041 gtk_calendar_select_day (calendar, priv->selected_day);
1043 calendar_queue_refresh (calendar);
1047 calendar_compute_days (GtkCalendar *calendar)
1049 GtkCalendarPrivate *priv = calendar->priv;
1052 gint ndays_in_month;
1053 gint ndays_in_prev_month;
1060 month = priv->month + 1;
1062 ndays_in_month = month_length[leap (year)][month];
1064 first_day = day_of_week (year, month, 1);
1065 first_day = (first_day + 7 - priv->week_start) % 7;
1067 /* Compute days of previous month */
1069 ndays_in_prev_month = month_length[leap (year)][month-1];
1071 ndays_in_prev_month = month_length[leap (year)][12];
1072 day = ndays_in_prev_month - first_day + 1;
1077 for (col = 0; col < first_day; col++)
1079 priv->day[row][col] = day;
1080 priv->day_month[row][col] = MONTH_PREV;
1085 /* Compute days of current month */
1087 for (day = 1; day <= ndays_in_month; day++)
1089 priv->day[row][col] = day;
1090 priv->day_month[row][col] = MONTH_CURRENT;
1100 /* Compute days of next month */
1102 for (; row <= 5; row++)
1104 for (; col <= 6; col++)
1106 priv->day[row][col] = day;
1107 priv->day_month[row][col] = MONTH_NEXT;
1115 calendar_select_and_focus_day (GtkCalendar *calendar,
1118 GtkCalendarPrivate *priv = calendar->priv;
1119 gint old_focus_row = priv->focus_row;
1120 gint old_focus_col = priv->focus_col;
1124 for (row = 0; row < 6; row ++)
1125 for (col = 0; col < 7; col++)
1127 if (priv->day_month[row][col] == MONTH_CURRENT
1128 && priv->day[row][col] == day)
1130 priv->focus_row = row;
1131 priv->focus_col = col;
1135 if (old_focus_row != -1 && old_focus_col != -1)
1136 calendar_invalidate_day (calendar, old_focus_row, old_focus_col);
1138 gtk_calendar_select_day (calendar, day);
1142 /****************************************
1143 * Layout computation utilities *
1144 ****************************************/
1147 calendar_row_height (GtkCalendar *calendar)
1149 GtkCalendarPrivate *priv = calendar->priv;
1151 return (GTK_CALENDAR_GET_PRIVATE (calendar)->main_h - CALENDAR_MARGIN
1152 - ((priv->display_flags & GTK_CALENDAR_SHOW_DAY_NAMES)
1153 ? calendar_get_ysep (calendar) : CALENDAR_MARGIN)) / 6;
1157 /* calendar_left_x_for_column: returns the x coordinate
1158 * for the left of the column */
1160 calendar_left_x_for_column (GtkCalendar *calendar,
1163 GtkCalendarPrivate *priv = calendar->priv;
1167 gint calendar_xsep = calendar_get_xsep (calendar);
1169 gint inner_border = calendar_get_inner_border (calendar);
1171 style = gtk_widget_get_style (GTK_WIDGET (calendar));
1173 week_width = priv->week_width + style->xthickness + inner_border;
1175 if (gtk_widget_get_direction (GTK_WIDGET (calendar)) == GTK_TEXT_DIR_RTL)
1177 column = 6 - column;
1181 width = GTK_CALENDAR_GET_PRIVATE (calendar)->day_width;
1182 if (priv->display_flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
1183 x_left = week_width + calendar_xsep + (width + DAY_XSEP) * column;
1185 x_left = week_width + CALENDAR_MARGIN + (width + DAY_XSEP) * column;
1190 /* column_from_x: returns the column 0-6 that the
1191 * x pixel of the xwindow is in */
1193 calendar_column_from_x (GtkCalendar *calendar,
1197 gint x_left, x_right;
1201 for (c = 0; c < 7; c++)
1203 x_left = calendar_left_x_for_column (calendar, c);
1204 x_right = x_left + GTK_CALENDAR_GET_PRIVATE (calendar)->day_width;
1206 if (event_x >= x_left && event_x < x_right)
1216 /* calendar_top_y_for_row: returns the y coordinate
1217 * for the top of the row */
1219 calendar_top_y_for_row (GtkCalendar *calendar,
1223 GtkAllocation allocation;
1224 gint inner_border = calendar_get_inner_border (calendar);
1226 gtk_widget_get_allocation (GTK_WIDGET (calendar), &allocation);
1227 style = gtk_widget_get_style (GTK_WIDGET (calendar));
1229 return allocation.height
1230 - style->ythickness - inner_border
1231 - (CALENDAR_MARGIN + (6 - row)
1232 * calendar_row_height (calendar));
1235 /* row_from_y: returns the row 0-5 that the
1236 * y pixel of the xwindow is in */
1238 calendar_row_from_y (GtkCalendar *calendar,
1243 gint y_top, y_bottom;
1245 height = calendar_row_height (calendar);
1248 for (r = 0; r < 6; r++)
1250 y_top = calendar_top_y_for_row (calendar, r);
1251 y_bottom = y_top + height;
1253 if (event_y >= y_top && event_y < y_bottom)
1264 calendar_arrow_rectangle (GtkCalendar *calendar,
1268 GtkWidget *widget = GTK_WIDGET (calendar);
1269 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1270 GtkAllocation allocation;
1274 gtk_widget_get_allocation (widget, &allocation);
1275 style = gtk_widget_get_style (widget);
1277 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
1278 year_left = priv->year_before;
1280 year_left = !priv->year_before;
1283 rect->width = priv->arrow_width;
1284 rect->height = priv->header_h - 7;
1288 case ARROW_MONTH_LEFT:
1290 rect->x = (allocation.width - 2 * style->xthickness
1291 - (3 + 2 * priv->arrow_width + priv->max_month_width));
1295 case ARROW_MONTH_RIGHT:
1297 rect->x = (allocation.width - 2 * style->xthickness
1298 - 3 - priv->arrow_width);
1300 rect->x = (priv->arrow_width + priv->max_month_width);
1302 case ARROW_YEAR_LEFT:
1306 rect->x = (allocation.width - 2 * style->xthickness
1307 - (3 + 2 * priv->arrow_width + priv->max_year_width));
1309 case ARROW_YEAR_RIGHT:
1311 rect->x = (priv->arrow_width + priv->max_year_width);
1313 rect->x = (allocation.width - 2 * style->xthickness
1314 - 3 - priv->arrow_width);
1318 rect->x += style->xthickness;
1319 rect->y += style->ythickness;
1323 calendar_day_rectangle (GtkCalendar *calendar,
1328 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1330 rect->x = calendar_left_x_for_column (calendar, col);
1331 rect->y = calendar_top_y_for_row (calendar, row);
1332 rect->height = calendar_row_height (calendar);
1333 rect->width = priv->day_width;
1337 calendar_set_month_prev (GtkCalendar *calendar)
1339 GtkCalendarPrivate *priv = calendar->priv;
1342 if (priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
1345 if (priv->month == 0)
1353 month_len = month_length[leap (priv->year)][priv->month + 1];
1355 calendar_compute_days (calendar);
1357 g_signal_emit (calendar,
1358 gtk_calendar_signals[PREV_MONTH_SIGNAL],
1360 g_signal_emit (calendar,
1361 gtk_calendar_signals[MONTH_CHANGED_SIGNAL],
1364 if (month_len < priv->selected_day)
1366 priv->selected_day = 0;
1367 gtk_calendar_select_day (calendar, month_len);
1371 if (priv->selected_day < 0)
1372 priv->selected_day = priv->selected_day + 1 + month_length[leap (priv->year)][priv->month + 1];
1373 gtk_calendar_select_day (calendar, priv->selected_day);
1376 calendar_queue_refresh (calendar);
1380 /****************************************
1381 * Basic object methods *
1382 ****************************************/
1385 gtk_calendar_finalize (GObject *object)
1387 G_OBJECT_CLASS (gtk_calendar_parent_class)->finalize (object);
1391 gtk_calendar_destroy (GtkWidget *widget)
1393 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
1395 calendar_stop_spinning (GTK_CALENDAR (widget));
1397 /* Call the destroy function for the extra display callback: */
1398 if (priv->detail_func_destroy && priv->detail_func_user_data)
1400 priv->detail_func_destroy (priv->detail_func_user_data);
1401 priv->detail_func_user_data = NULL;
1402 priv->detail_func_destroy = NULL;
1405 GTK_WIDGET_CLASS (gtk_calendar_parent_class)->destroy (widget);
1410 calendar_set_display_option (GtkCalendar *calendar,
1411 GtkCalendarDisplayOptions flag,
1414 GtkCalendarPrivate *priv = calendar->priv;
1415 GtkCalendarDisplayOptions flags;
1418 flags = priv->display_flags | flag;
1420 flags = priv->display_flags & ~flag;
1421 gtk_calendar_set_display_options (calendar, flags);
1425 calendar_get_display_option (GtkCalendar *calendar,
1426 GtkCalendarDisplayOptions flag)
1428 GtkCalendarPrivate *priv = calendar->priv;
1430 return (priv->display_flags & flag) != 0;
1434 gtk_calendar_set_property (GObject *object,
1436 const GValue *value,
1439 GtkCalendar *calendar = GTK_CALENDAR (object);
1440 GtkCalendarPrivate *priv = calendar->priv;
1445 gtk_calendar_select_month (calendar,
1447 g_value_get_int (value));
1450 gtk_calendar_select_month (calendar,
1451 g_value_get_int (value),
1455 gtk_calendar_select_day (calendar,
1456 g_value_get_int (value));
1458 case PROP_SHOW_HEADING:
1459 calendar_set_display_option (calendar,
1460 GTK_CALENDAR_SHOW_HEADING,
1461 g_value_get_boolean (value));
1463 case PROP_SHOW_DAY_NAMES:
1464 calendar_set_display_option (calendar,
1465 GTK_CALENDAR_SHOW_DAY_NAMES,
1466 g_value_get_boolean (value));
1468 case PROP_NO_MONTH_CHANGE:
1469 calendar_set_display_option (calendar,
1470 GTK_CALENDAR_NO_MONTH_CHANGE,
1471 g_value_get_boolean (value));
1473 case PROP_SHOW_WEEK_NUMBERS:
1474 calendar_set_display_option (calendar,
1475 GTK_CALENDAR_SHOW_WEEK_NUMBERS,
1476 g_value_get_boolean (value));
1478 case PROP_SHOW_DETAILS:
1479 calendar_set_display_option (calendar,
1480 GTK_CALENDAR_SHOW_DETAILS,
1481 g_value_get_boolean (value));
1483 case PROP_DETAIL_WIDTH_CHARS:
1484 gtk_calendar_set_detail_width_chars (calendar,
1485 g_value_get_int (value));
1487 case PROP_DETAIL_HEIGHT_ROWS:
1488 gtk_calendar_set_detail_height_rows (calendar,
1489 g_value_get_int (value));
1492 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1498 gtk_calendar_get_property (GObject *object,
1503 GtkCalendar *calendar = GTK_CALENDAR (object);
1504 GtkCalendarPrivate *priv = calendar->priv;
1509 g_value_set_int (value, priv->year);
1512 g_value_set_int (value, priv->month);
1515 g_value_set_int (value, priv->selected_day);
1517 case PROP_SHOW_HEADING:
1518 g_value_set_boolean (value, calendar_get_display_option (calendar,
1519 GTK_CALENDAR_SHOW_HEADING));
1521 case PROP_SHOW_DAY_NAMES:
1522 g_value_set_boolean (value, calendar_get_display_option (calendar,
1523 GTK_CALENDAR_SHOW_DAY_NAMES));
1525 case PROP_NO_MONTH_CHANGE:
1526 g_value_set_boolean (value, calendar_get_display_option (calendar,
1527 GTK_CALENDAR_NO_MONTH_CHANGE));
1529 case PROP_SHOW_WEEK_NUMBERS:
1530 g_value_set_boolean (value, calendar_get_display_option (calendar,
1531 GTK_CALENDAR_SHOW_WEEK_NUMBERS));
1533 case PROP_SHOW_DETAILS:
1534 g_value_set_boolean (value, calendar_get_display_option (calendar,
1535 GTK_CALENDAR_SHOW_DETAILS));
1537 case PROP_DETAIL_WIDTH_CHARS:
1538 g_value_set_int (value, priv->detail_width_chars);
1540 case PROP_DETAIL_HEIGHT_ROWS:
1541 g_value_set_int (value, priv->detail_height_rows);
1544 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1550 /****************************************
1552 ****************************************/
1555 calendar_realize_arrows (GtkCalendar *calendar)
1557 GtkWidget *widget = GTK_WIDGET (calendar);
1558 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1559 GdkWindowAttr attributes;
1560 gint attributes_mask;
1562 GtkAllocation allocation;
1564 if (! (priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
1565 && (priv->display_flags & GTK_CALENDAR_SHOW_HEADING))
1567 gtk_widget_get_allocation (widget, &allocation);
1569 attributes.wclass = GDK_INPUT_ONLY;
1570 attributes.window_type = GDK_WINDOW_CHILD;
1571 attributes.event_mask = (gtk_widget_get_events (widget)
1572 | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
1573 | GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK);
1574 attributes_mask = GDK_WA_X | GDK_WA_Y;
1575 for (i = 0; i < 4; i++)
1578 calendar_arrow_rectangle (calendar, i, &rect);
1580 attributes.x = allocation.x + rect.x;
1581 attributes.y = allocation.y + rect.y;
1582 attributes.width = rect.width;
1583 attributes.height = rect.height;
1584 priv->arrow_win[i] = gdk_window_new (gtk_widget_get_window (widget),
1587 if (gtk_widget_is_sensitive (widget))
1588 priv->arrow_state[i] = GTK_STATE_NORMAL;
1590 priv->arrow_state[i] = GTK_STATE_INSENSITIVE;
1591 gdk_window_show (priv->arrow_win[i]);
1592 gdk_window_set_user_data (priv->arrow_win[i], widget);
1597 for (i = 0; i < 4; i++)
1598 priv->arrow_win[i] = NULL;
1603 calendar_unrealize_arrows (GtkCalendar *calendar)
1605 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1608 for (i = 0; i < 4; i++)
1610 if (priv->arrow_win[i])
1612 gdk_window_set_user_data (priv->arrow_win[i], NULL);
1613 gdk_window_destroy (priv->arrow_win[i]);
1614 priv->arrow_win[i] = NULL;
1621 calendar_get_inner_border (GtkCalendar *calendar)
1625 gtk_widget_style_get (GTK_WIDGET (calendar),
1626 "inner-border", &inner_border,
1629 return inner_border;
1633 calendar_get_xsep (GtkCalendar *calendar)
1637 gtk_widget_style_get (GTK_WIDGET (calendar),
1638 "horizontal-separation", &xsep,
1645 calendar_get_ysep (GtkCalendar *calendar)
1649 gtk_widget_style_get (GTK_WIDGET (calendar),
1650 "vertical-separation", &ysep,
1657 gtk_calendar_realize (GtkWidget *widget)
1659 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
1660 GdkWindowAttr attributes;
1661 gint attributes_mask;
1662 gint inner_border = calendar_get_inner_border (GTK_CALENDAR (widget));
1664 GtkAllocation allocation;
1666 style = gtk_widget_get_style (widget);
1667 gtk_widget_get_allocation (widget, &allocation);
1669 GTK_WIDGET_CLASS (gtk_calendar_parent_class)->realize (widget);
1671 attributes.wclass = GDK_INPUT_ONLY;
1672 attributes.window_type = GDK_WINDOW_CHILD;
1673 attributes.event_mask = (gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK
1674 | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
1675 | GDK_POINTER_MOTION_MASK | GDK_LEAVE_NOTIFY_MASK);
1677 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
1678 attributes.x = priv->week_width + style->ythickness + inner_border;
1680 attributes.x = style->ythickness + inner_border;
1682 attributes.y = priv->header_h + priv->day_name_h + style->ythickness + inner_border;
1683 attributes.width = allocation.width - attributes.x - (style->xthickness + inner_border);
1685 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1686 attributes.width -= priv->week_width;
1688 attributes.height = priv->main_h;
1689 attributes_mask = GDK_WA_X | GDK_WA_Y;
1691 attributes.x += allocation.x;
1692 attributes.y += allocation.y;
1694 priv->main_win = gdk_window_new (gtk_widget_get_window (widget),
1695 &attributes, attributes_mask);
1696 gdk_window_show (priv->main_win);
1697 gdk_window_set_user_data (priv->main_win, widget);
1699 calendar_realize_arrows (GTK_CALENDAR (widget));
1703 gtk_calendar_unrealize (GtkWidget *widget)
1705 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
1707 calendar_unrealize_arrows (GTK_CALENDAR (widget));
1711 gdk_window_set_user_data (priv->main_win, NULL);
1712 gdk_window_destroy (priv->main_win);
1713 priv->main_win = NULL;
1716 GTK_WIDGET_CLASS (gtk_calendar_parent_class)->unrealize (widget);
1720 gtk_calendar_get_detail (GtkCalendar *calendar,
1724 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1727 if (priv->detail_func == NULL)
1731 month = priv->month + priv->day_month[row][column] - MONTH_CURRENT;
1738 else if (month > 11)
1744 return priv->detail_func (calendar,
1746 priv->day[row][column],
1747 priv->detail_func_user_data);
1751 gtk_calendar_query_tooltip (GtkWidget *widget,
1754 gboolean keyboard_mode,
1755 GtkTooltip *tooltip)
1757 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
1758 GtkCalendar *calendar = GTK_CALENDAR (widget);
1759 gchar *detail = NULL;
1760 GdkRectangle day_rect;
1763 col = calendar_column_from_x (calendar, x);
1764 row = calendar_row_from_y (calendar, y);
1766 if (col != -1 && row != -1 &&
1767 (0 != (priv->detail_overflow[row] & (1 << col)) ||
1768 0 == (priv->display_flags & GTK_CALENDAR_SHOW_DETAILS)))
1770 detail = gtk_calendar_get_detail (calendar, row, col);
1771 calendar_day_rectangle (calendar, row, col, &day_rect);
1776 gtk_tooltip_set_tip_area (tooltip, &day_rect);
1777 gtk_tooltip_set_markup (tooltip, detail);
1784 if (GTK_WIDGET_CLASS (gtk_calendar_parent_class)->query_tooltip)
1785 return GTK_WIDGET_CLASS (gtk_calendar_parent_class)->query_tooltip (widget, x, y, keyboard_mode, tooltip);
1791 /****************************************
1792 * Size Request and Allocate *
1793 ****************************************/
1796 gtk_calendar_size_request (GtkWidget *widget,
1797 GtkRequisition *requisition)
1799 GtkCalendar *calendar = GTK_CALENDAR (widget);
1800 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
1802 PangoLayout *layout;
1803 PangoRectangle logical_rect;
1807 gint calendar_margin = CALENDAR_MARGIN;
1808 gint header_width, main_width;
1809 gint max_header_height = 0;
1812 gint max_detail_height;
1813 gint inner_border = calendar_get_inner_border (calendar);
1814 gint calendar_ysep = calendar_get_ysep (calendar);
1815 gint calendar_xsep = calendar_get_xsep (calendar);
1817 gtk_widget_style_get (GTK_WIDGET (widget),
1818 "focus-line-width", &focus_width,
1819 "focus-padding", &focus_padding,
1822 layout = gtk_widget_create_pango_layout (widget, NULL);
1825 * Calculate the requisition width for the widget.
1830 if (priv->display_flags & GTK_CALENDAR_SHOW_HEADING)
1832 priv->max_month_width = 0;
1833 for (i = 0; i < 12; i++)
1835 pango_layout_set_text (layout, default_monthname[i], -1);
1836 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1837 priv->max_month_width = MAX (priv->max_month_width,
1838 logical_rect.width + 8);
1839 max_header_height = MAX (max_header_height, logical_rect.height);
1842 priv->max_year_width = 0;
1843 /* Translators: This is a text measurement template.
1844 * Translate it to the widest year text
1846 * If you don't understand this, leave it as "2000"
1848 pango_layout_set_text (layout, C_("year measurement template", "2000"), -1);
1849 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1850 priv->max_year_width = MAX (priv->max_year_width,
1851 logical_rect.width + 8);
1852 max_header_height = MAX (max_header_height, logical_rect.height);
1856 priv->max_month_width = 0;
1857 priv->max_year_width = 0;
1860 if (priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
1861 header_width = (priv->max_month_width
1862 + priv->max_year_width
1865 header_width = (priv->max_month_width
1866 + priv->max_year_width
1867 + 4 * priv->arrow_width + 3 * 3);
1869 /* Mainwindow labels width */
1871 priv->max_day_char_width = 0;
1872 priv->max_day_char_ascent = 0;
1873 priv->max_day_char_descent = 0;
1874 priv->min_day_width = 0;
1876 for (i = 0; i < 9; i++)
1879 g_snprintf (buffer, sizeof (buffer), C_("calendar:day:digits", "%d"), i * 11);
1880 pango_layout_set_text (layout, buffer, -1);
1881 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1882 priv->min_day_width = MAX (priv->min_day_width,
1883 logical_rect.width);
1885 priv->max_day_char_ascent = MAX (priv->max_day_char_ascent,
1886 PANGO_ASCENT (logical_rect));
1887 priv->max_day_char_descent = MAX (priv->max_day_char_descent,
1888 PANGO_DESCENT (logical_rect));
1891 priv->max_label_char_ascent = 0;
1892 priv->max_label_char_descent = 0;
1893 if (priv->display_flags & GTK_CALENDAR_SHOW_DAY_NAMES)
1894 for (i = 0; i < 7; i++)
1896 pango_layout_set_text (layout, default_abbreviated_dayname[i], -1);
1897 pango_layout_line_get_pixel_extents (pango_layout_get_lines_readonly (layout)->data, NULL, &logical_rect);
1899 priv->min_day_width = MAX (priv->min_day_width, logical_rect.width);
1900 priv->max_label_char_ascent = MAX (priv->max_label_char_ascent,
1901 PANGO_ASCENT (logical_rect));
1902 priv->max_label_char_descent = MAX (priv->max_label_char_descent,
1903 PANGO_DESCENT (logical_rect));
1906 priv->max_week_char_width = 0;
1907 if (priv->display_flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
1908 for (i = 0; i < 9; i++)
1911 g_snprintf (buffer, sizeof (buffer), C_("calendar:week:digits", "%d"), i * 11);
1912 pango_layout_set_text (layout, buffer, -1);
1913 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1914 priv->max_week_char_width = MAX (priv->max_week_char_width,
1915 logical_rect.width / 2);
1918 /* Calculate detail extents. Do this as late as possible since
1919 * pango_layout_set_markup is called which alters font settings. */
1920 max_detail_height = 0;
1922 if (priv->detail_func && (priv->display_flags & GTK_CALENDAR_SHOW_DETAILS))
1924 gchar *markup, *tail;
1926 if (priv->detail_width_chars || priv->detail_height_rows)
1928 gint rows = MAX (1, priv->detail_height_rows) - 1;
1929 gsize len = priv->detail_width_chars + rows + 16;
1931 markup = tail = g_alloca (len);
1933 memcpy (tail, "<small>", 7);
1936 memset (tail, 'm', priv->detail_width_chars);
1937 tail += priv->detail_width_chars;
1939 memset (tail, '\n', rows);
1942 memcpy (tail, "</small>", 9);
1945 g_assert (len == (tail - markup));
1947 pango_layout_set_markup (layout, markup, -1);
1948 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1950 if (priv->detail_width_chars)
1951 priv->min_day_width = MAX (priv->min_day_width, logical_rect.width);
1952 if (priv->detail_height_rows)
1953 max_detail_height = MAX (max_detail_height, logical_rect.height);
1956 if (!priv->detail_width_chars || !priv->detail_height_rows)
1957 for (r = 0; r < 6; r++)
1958 for (c = 0; c < 7; c++)
1960 gchar *detail = gtk_calendar_get_detail (calendar, r, c);
1964 markup = g_strconcat ("<small>", detail, "</small>", NULL);
1965 pango_layout_set_markup (layout, markup, -1);
1967 if (priv->detail_width_chars)
1969 pango_layout_set_wrap (layout, PANGO_WRAP_WORD_CHAR);
1970 pango_layout_set_width (layout, PANGO_SCALE * priv->min_day_width);
1973 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1975 if (!priv->detail_width_chars)
1976 priv->min_day_width = MAX (priv->min_day_width, logical_rect.width);
1977 if (!priv->detail_height_rows)
1978 max_detail_height = MAX (max_detail_height, logical_rect.height);
1986 /* We add one to max_day_char_width to be able to make the marked day "bold" */
1987 priv->max_day_char_width = priv->min_day_width / 2 + 1;
1989 main_width = (7 * (priv->min_day_width + (focus_padding + focus_width) * 2) + (DAY_XSEP * 6) + CALENDAR_MARGIN * 2
1990 + (priv->max_week_char_width
1991 ? priv->max_week_char_width * 2 + (focus_padding + focus_width) * 2 + calendar_xsep * 2
1994 style = gtk_widget_get_style (widget);
1996 requisition->width = MAX (header_width, main_width + inner_border * 2) + style->xthickness * 2;
1999 * Calculate the requisition height for the widget.
2002 if (priv->display_flags & GTK_CALENDAR_SHOW_HEADING)
2004 priv->header_h = (max_header_height + calendar_ysep * 2);
2011 if (priv->display_flags & GTK_CALENDAR_SHOW_DAY_NAMES)
2013 priv->day_name_h = (priv->max_label_char_ascent
2014 + priv->max_label_char_descent
2015 + 2 * (focus_padding + focus_width) + calendar_margin);
2016 calendar_margin = calendar_ysep;
2020 priv->day_name_h = 0;
2023 priv->main_h = (CALENDAR_MARGIN + calendar_margin
2024 + 6 * (priv->max_day_char_ascent
2025 + priv->max_day_char_descent
2027 + 2 * (focus_padding + focus_width))
2030 height = priv->header_h + priv->day_name_h + priv->main_h;
2032 requisition->height = height + (style->ythickness + inner_border) * 2;
2034 g_object_unref (layout);
2038 gtk_calendar_get_preferred_width (GtkWidget *widget,
2042 GtkRequisition requisition;
2044 gtk_calendar_size_request (widget, &requisition);
2046 *minimum = *natural = requisition.width;
2050 gtk_calendar_get_preferred_height (GtkWidget *widget,
2054 GtkRequisition requisition;
2056 gtk_calendar_size_request (widget, &requisition);
2058 *minimum = *natural = requisition.height;
2062 gtk_calendar_size_allocate (GtkWidget *widget,
2063 GtkAllocation *allocation)
2065 GtkCalendar *calendar = GTK_CALENDAR (widget);
2066 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
2068 gint xthickness, ythickness;
2070 gint inner_border = calendar_get_inner_border (calendar);
2071 gint calendar_xsep = calendar_get_xsep (calendar);
2073 style = gtk_widget_get_style (widget);
2074 xthickness = style->xthickness;
2075 ythickness = style->xthickness;
2077 gtk_widget_set_allocation (widget, allocation);
2079 if (priv->display_flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
2081 priv->day_width = (priv->min_day_width
2082 * ((allocation->width - (xthickness + inner_border) * 2
2083 - (CALENDAR_MARGIN * 2) - (DAY_XSEP * 6) - calendar_xsep * 2))
2084 / (7 * priv->min_day_width + priv->max_week_char_width * 2));
2085 priv->week_width = ((allocation->width - (xthickness + inner_border) * 2
2086 - (CALENDAR_MARGIN * 2) - (DAY_XSEP * 6) - calendar_xsep * 2 )
2087 - priv->day_width * 7 + CALENDAR_MARGIN + calendar_xsep);
2091 priv->day_width = (allocation->width
2092 - (xthickness + inner_border) * 2
2093 - (CALENDAR_MARGIN * 2)
2094 - (DAY_XSEP * 6))/7;
2095 priv->week_width = 0;
2098 if (gtk_widget_get_realized (widget))
2100 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
2101 gdk_window_move_resize (priv->main_win,
2102 priv->week_width + xthickness + inner_border,
2103 priv->header_h + priv->day_name_h
2104 + (style->ythickness + inner_border),
2105 allocation->width - priv->week_width
2106 - (xthickness + inner_border) * 2,
2109 gdk_window_move_resize (priv->main_win,
2110 xthickness + inner_border,
2111 priv->header_h + priv->day_name_h
2112 + style->ythickness + inner_border,
2113 allocation->width - priv->week_width
2114 - (xthickness + inner_border) * 2,
2117 for (i = 0 ; i < 4 ; i++)
2119 if (priv->arrow_win[i])
2122 calendar_arrow_rectangle (calendar, i, &rect);
2124 gdk_window_move_resize (priv->arrow_win[i],
2125 allocation->x + rect.x,
2126 allocation->y + rect.y,
2127 rect.width, rect.height);
2134 /****************************************
2136 ****************************************/
2139 calendar_paint_header (GtkCalendar *calendar, cairo_t *cr)
2141 GtkWidget *widget = GTK_WIDGET (calendar);
2142 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2143 GtkAllocation allocation;
2148 gint max_month_width;
2149 gint max_year_width;
2150 PangoLayout *layout;
2151 PangoRectangle logical_rect;
2157 style = gtk_widget_get_style (widget);
2160 cairo_translate (cr, style->xthickness, style->ythickness);
2162 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
2163 year_left = priv->year_before;
2165 year_left = !priv->year_before;
2167 gtk_widget_get_allocation (widget, &allocation);
2169 header_width = allocation.width - 2 * style->xthickness;
2171 max_month_width = priv->max_month_width;
2172 max_year_width = priv->max_year_width;
2174 gdk_cairo_set_source_color (cr, HEADER_BG_COLOR (widget));
2175 cairo_rectangle (cr, 0, 0, header_width, priv->header_h);
2178 gtk_paint_shadow (style, cr,
2179 GTK_STATE_NORMAL, GTK_SHADOW_OUT,
2181 0, 0, header_width, priv->header_h);
2183 tmp_time = 1; /* Jan 1 1970, 00:00:01 UTC */
2184 tm = gmtime (&tmp_time);
2185 tm->tm_year = priv->year - 1900;
2187 /* Translators: This dictates how the year is displayed in
2188 * gtkcalendar widget. See strftime() manual for the format.
2189 * Use only ASCII in the translation.
2191 * Also look for the msgid "2000".
2192 * Translate that entry to a year with the widest output of this
2195 * "%Y" is appropriate for most locales.
2197 strftime (buffer, sizeof (buffer), C_("calendar year format", "%Y"), tm);
2198 str = g_locale_to_utf8 (buffer, -1, NULL, NULL, NULL);
2199 layout = gtk_widget_create_pango_layout (widget, str);
2202 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2205 y = (priv->header_h - logical_rect.height) / 2;
2207 /* Draw year and its arrows */
2209 if (priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
2211 x = 3 + (max_year_width - logical_rect.width)/2;
2213 x = header_width - (3 + max_year_width
2214 - (max_year_width - logical_rect.width)/2);
2217 x = 3 + priv->arrow_width + (max_year_width - logical_rect.width)/2;
2219 x = header_width - (3 + priv->arrow_width + max_year_width
2220 - (max_year_width - logical_rect.width)/2);
2223 gdk_cairo_set_source_color (cr, HEADER_FG_COLOR (GTK_WIDGET (calendar)));
2224 cairo_move_to (cr, x, y);
2225 pango_cairo_show_layout (cr, layout);
2228 g_snprintf (buffer, sizeof (buffer), "%s", default_monthname[priv->month]);
2229 pango_layout_set_text (layout, buffer, -1);
2230 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2232 if (priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
2234 x = header_width - (3 + max_month_width
2235 - (max_month_width - logical_rect.width)/2);
2237 x = 3 + (max_month_width - logical_rect.width) / 2;
2240 x = header_width - (3 + priv->arrow_width + max_month_width
2241 - (max_month_width - logical_rect.width)/2);
2243 x = 3 + priv->arrow_width + (max_month_width - logical_rect.width)/2;
2245 cairo_move_to (cr, x, y);
2246 pango_cairo_show_layout (cr, layout);
2248 g_object_unref (layout);
2254 calendar_paint_day_names (GtkCalendar *calendar,
2257 GtkWidget *widget = GTK_WIDGET (calendar);
2258 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2260 GtkAllocation allocation;
2263 int day_width, cal_width;
2265 PangoLayout *layout;
2266 PangoRectangle logical_rect;
2269 gint calendar_ysep = calendar_get_ysep (calendar);
2270 gint calendar_xsep = calendar_get_xsep (calendar);
2271 gint inner_border = calendar_get_inner_border (calendar);
2273 style = gtk_widget_get_style (widget);
2277 cairo_translate (cr,
2278 style->xthickness + inner_border,
2279 priv->header_h + style->ythickness + inner_border);
2281 gtk_widget_style_get (GTK_WIDGET (widget),
2282 "focus-line-width", &focus_width,
2283 "focus-padding", &focus_padding,
2286 gtk_widget_get_allocation (widget, &allocation);
2288 day_width = priv->day_width;
2289 cal_width = allocation.width - (style->xthickness + inner_border) * 2;
2290 day_wid_sep = day_width + DAY_XSEP;
2293 * Draw rectangles as inverted background for the labels.
2296 gdk_cairo_set_source_color (cr, SELECTED_BG_COLOR (widget));
2297 cairo_rectangle (cr,
2298 CALENDAR_MARGIN, CALENDAR_MARGIN,
2299 cal_width - CALENDAR_MARGIN * 2,
2300 priv->day_name_h - CALENDAR_MARGIN);
2303 if (priv->display_flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
2305 cairo_rectangle (cr,
2307 priv->day_name_h - calendar_ysep,
2308 priv->week_width - calendar_ysep - CALENDAR_MARGIN,
2317 layout = gtk_widget_create_pango_layout (widget, NULL);
2319 gdk_cairo_set_source_color (cr, SELECTED_FG_COLOR (widget));
2320 for (i = 0; i < 7; i++)
2322 if (gtk_widget_get_direction (GTK_WIDGET (calendar)) == GTK_TEXT_DIR_RTL)
2326 day = (day + priv->week_start) % 7;
2327 g_snprintf (buffer, sizeof (buffer), "%s", default_abbreviated_dayname[day]);
2329 pango_layout_set_text (layout, buffer, -1);
2330 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2334 + (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR ?
2335 (priv->week_width + (priv->week_width ? calendar_xsep : 0))
2338 + (day_width - logical_rect.width)/2),
2339 CALENDAR_MARGIN + focus_width + focus_padding + logical_rect.y);
2340 pango_cairo_show_layout (cr, layout);
2343 g_object_unref (layout);
2349 calendar_paint_week_numbers (GtkCalendar *calendar,
2352 GtkWidget *widget = GTK_WIDGET (calendar);
2353 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2355 guint week = 0, year;
2356 gint row, x_loc, y_loc;
2359 PangoLayout *layout;
2360 PangoRectangle logical_rect;
2363 gint calendar_xsep = calendar_get_xsep (calendar);
2364 gint inner_border = calendar_get_inner_border (calendar);
2367 style = gtk_widget_get_style (widget);
2371 y = priv->header_h + priv->day_name_h + (style->ythickness + inner_border);
2372 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
2373 x = style->xthickness + inner_border;
2375 x = gtk_widget_get_allocated_width (widget) - priv->week_width - (style->xthickness + inner_border);
2377 gtk_widget_style_get (GTK_WIDGET (widget),
2378 "focus-line-width", &focus_width,
2379 "focus-padding", &focus_padding,
2383 * Draw a rectangle as inverted background for the labels.
2386 gdk_cairo_set_source_color (cr, SELECTED_BG_COLOR (widget));
2387 if (priv->display_flags & GTK_CALENDAR_SHOW_DAY_NAMES)
2388 cairo_rectangle (cr,
2389 x + CALENDAR_MARGIN,
2391 priv->week_width - CALENDAR_MARGIN,
2392 priv->main_h - CALENDAR_MARGIN);
2394 cairo_rectangle (cr,
2395 x + CALENDAR_MARGIN,
2396 y + CALENDAR_MARGIN,
2397 priv->week_width - CALENDAR_MARGIN,
2398 priv->main_h - 2 * CALENDAR_MARGIN);
2405 layout = gtk_widget_create_pango_layout (widget, NULL);
2407 gdk_cairo_set_source_color (cr, SELECTED_FG_COLOR (widget));
2408 day_height = calendar_row_height (calendar);
2409 for (row = 0; row < 6; row++)
2414 if (priv->day[row][6] < 15 && row > 3 && priv->month == 11)
2417 result = week_of_year (&week, &year,
2418 ((priv->day[row][6] < 15 && row > 3 ? 1 : 0)
2419 + priv->month) % 12 + 1, priv->day[row][6]);
2420 g_return_if_fail (result);
2422 /* Translators: this defines whether the week numbers should use
2423 * localized digits or the ones used in English (0123...).
2425 * Translate to "%Id" if you want to use localized digits, or
2426 * translate to "%d" otherwise.
2428 * Note that translating this doesn't guarantee that you get localized
2429 * digits. That needs support from your system and locale definition
2432 g_snprintf (buffer, sizeof (buffer), C_("calendar:week:digits", "%d"), week);
2433 pango_layout_set_text (layout, buffer, -1);
2434 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2436 y_loc = calendar_top_y_for_row (calendar, row) + (day_height - logical_rect.height) / 2;
2438 x_loc = x + (priv->week_width
2439 - logical_rect.width
2440 - calendar_xsep - focus_padding - focus_width);
2442 cairo_move_to (cr, x_loc, y_loc);
2443 pango_cairo_show_layout (cr, layout);
2446 g_object_unref (layout);
2452 calendar_invalidate_day_num (GtkCalendar *calendar,
2455 GtkCalendarPrivate *priv = calendar->priv;
2456 gint r, c, row, col;
2460 for (r = 0; r < 6; r++)
2461 for (c = 0; c < 7; c++)
2462 if (priv->day_month[r][c] == MONTH_CURRENT &&
2463 priv->day[r][c] == day)
2469 g_return_if_fail (row != -1);
2470 g_return_if_fail (col != -1);
2472 calendar_invalidate_day (calendar, row, col);
2476 calendar_invalidate_day (GtkCalendar *calendar,
2480 GdkRectangle day_rect;
2481 GtkAllocation allocation;
2483 gtk_widget_get_allocation (GTK_WIDGET (calendar), &allocation);
2484 calendar_day_rectangle (calendar, row, col, &day_rect);
2485 gtk_widget_queue_draw_area (GTK_WIDGET (calendar),
2486 allocation.x + day_rect.x,
2487 allocation.y + day_rect.y,
2488 day_rect.width, day_rect.height);
2492 is_color_attribute (PangoAttribute *attribute,
2495 return (attribute->klass->type == PANGO_ATTR_FOREGROUND ||
2496 attribute->klass->type == PANGO_ATTR_BACKGROUND);
2500 calendar_paint_day (GtkCalendar *calendar,
2505 GtkWidget *widget = GTK_WIDGET (calendar);
2506 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2508 GdkColor *text_color;
2513 GdkRectangle day_rect;
2515 PangoLayout *layout;
2516 PangoRectangle logical_rect;
2517 gboolean overflow = FALSE;
2518 gboolean show_details;
2520 g_return_if_fail (row < 6);
2521 g_return_if_fail (col < 7);
2523 style = gtk_widget_get_style (widget);
2525 day = priv->day[row][col];
2526 show_details = (priv->display_flags & GTK_CALENDAR_SHOW_DETAILS);
2528 calendar_day_rectangle (calendar, row, col, &day_rect);
2530 if (priv->day_month[row][col] == MONTH_PREV)
2532 text_color = PREV_MONTH_COLOR (widget);
2534 else if (priv->day_month[row][col] == MONTH_NEXT)
2536 text_color = NEXT_MONTH_COLOR (widget);
2540 if (priv->selected_day == day)
2542 gdk_cairo_set_source_color (cr, SELECTED_BG_COLOR (widget));
2543 gdk_cairo_rectangle (cr, &day_rect);
2546 if (priv->selected_day == day)
2547 text_color = SELECTED_FG_COLOR (widget);
2548 else if (priv->marked_date[day-1])
2549 text_color = MARKED_COLOR (widget);
2551 text_color = NORMAL_DAY_COLOR (widget);
2554 /* Translators: this defines whether the day numbers should use
2555 * localized digits or the ones used in English (0123...).
2557 * Translate to "%Id" if you want to use localized digits, or
2558 * translate to "%d" otherwise.
2560 * Note that translating this doesn't guarantee that you get localized
2561 * digits. That needs support from your system and locale definition
2564 g_snprintf (buffer, sizeof (buffer), C_("calendar:day:digits", "%d"), day);
2566 /* Get extra information to show, if any: */
2568 detail = gtk_calendar_get_detail (calendar, row, col);
2570 layout = gtk_widget_create_pango_layout (widget, buffer);
2571 pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
2572 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2574 x_loc = day_rect.x + (day_rect.width - logical_rect.width) / 2;
2577 gdk_cairo_set_source_color (cr, text_color);
2578 cairo_move_to (cr, x_loc, y_loc);
2579 pango_cairo_show_layout (cr, layout);
2581 if (priv->day_month[row][col] == MONTH_CURRENT &&
2582 (priv->marked_date[day-1] || (detail && !show_details)))
2584 cairo_move_to (cr, x_loc - 1, y_loc);
2585 pango_cairo_show_layout (cr, layout);
2588 y_loc += priv->max_day_char_descent;
2590 if (priv->detail_func && show_details)
2594 if (priv->selected_day == day)
2595 gdk_cairo_set_source_color (cr, &style->text[GTK_STATE_ACTIVE]);
2596 else if (priv->day_month[row][col] == MONTH_CURRENT)
2597 gdk_cairo_set_source_color (cr, &style->base[GTK_STATE_ACTIVE]);
2599 gdk_cairo_set_source_color (cr, &style->base[GTK_STATE_INSENSITIVE]);
2601 cairo_set_line_width (cr, 1);
2602 cairo_move_to (cr, day_rect.x + 2, y_loc + 0.5);
2603 cairo_line_to (cr, day_rect.x + day_rect.width - 2, y_loc + 0.5);
2611 if (detail && show_details)
2613 gchar *markup = g_strconcat ("<small>", detail, "</small>", NULL);
2614 pango_layout_set_markup (layout, markup, -1);
2617 if (day == priv->selected_day)
2619 /* Stripping colors as they conflict with selection marking. */
2621 PangoAttrList *attrs = pango_layout_get_attributes (layout);
2622 PangoAttrList *colors = NULL;
2625 colors = pango_attr_list_filter (attrs, is_color_attribute, NULL);
2627 pango_attr_list_unref (colors);
2630 pango_layout_set_wrap (layout, PANGO_WRAP_WORD_CHAR);
2631 pango_layout_set_width (layout, PANGO_SCALE * day_rect.width);
2633 if (priv->detail_height_rows)
2635 gint dy = day_rect.height - (y_loc - day_rect.y);
2636 pango_layout_set_height (layout, PANGO_SCALE * dy);
2637 pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_END);
2640 cairo_move_to (cr, day_rect.x, y_loc);
2641 pango_cairo_show_layout (cr, layout);
2644 if (gtk_widget_has_focus (widget)
2645 && priv->focus_row == row && priv->focus_col == col)
2649 if (priv->selected_day == day)
2650 state = gtk_widget_has_focus (widget) ? GTK_STATE_SELECTED : GTK_STATE_ACTIVE;
2652 state = GTK_STATE_NORMAL;
2654 gtk_paint_focus (style, cr,
2655 state, widget, "calendar-day",
2656 day_rect.x, day_rect.y,
2657 day_rect.width, day_rect.height);
2661 priv->detail_overflow[row] |= (1 << col);
2663 priv->detail_overflow[row] &= ~(1 << col);
2665 g_object_unref (layout);
2670 calendar_paint_main (GtkCalendar *calendar,
2677 for (col = 0; col < 7; col++)
2678 for (row = 0; row < 6; row++)
2679 calendar_paint_day (calendar, cr, row, col);
2685 calendar_invalidate_arrow (GtkCalendar *calendar,
2688 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2690 if (priv->display_flags & GTK_CALENDAR_SHOW_HEADING &&
2691 priv->arrow_win[arrow])
2694 GtkAllocation allocation;
2696 calendar_arrow_rectangle (calendar, arrow, &rect);
2697 gtk_widget_get_allocation (GTK_WIDGET (calendar), &allocation);
2698 gtk_widget_queue_draw_area (GTK_WIDGET (calendar),
2699 allocation.x + rect.x,
2700 allocation.y + rect.y,
2701 rect.width, rect.height);
2706 calendar_paint_arrow (GtkCalendar *calendar,
2710 GtkWidget *widget = GTK_WIDGET (calendar);
2711 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
2716 if (!priv->arrow_win[arrow])
2719 calendar_arrow_rectangle (calendar, arrow, &rect);
2723 style = gtk_widget_get_style (widget);
2724 state = priv->arrow_state[arrow];
2726 cairo_rectangle (cr, rect.x, rect.y, rect.width, rect.height);
2727 gdk_cairo_set_source_color (cr, &style->bg[state]);
2730 if (arrow == ARROW_MONTH_LEFT || arrow == ARROW_YEAR_LEFT)
2731 gtk_paint_arrow (style, cr, state,
2732 GTK_SHADOW_OUT, widget, "calendar",
2733 GTK_ARROW_LEFT, TRUE,
2734 rect.x + (rect.width - 8) / 2,
2735 rect.y + (rect.height - 8) / 2,
2738 gtk_paint_arrow (style, cr, state,
2739 GTK_SHADOW_OUT, widget, "calendar",
2740 GTK_ARROW_RIGHT, TRUE,
2741 rect.x + (rect.width - 8) / 2,
2742 rect.y + (rect.height - 8) / 2,
2749 gtk_calendar_draw (GtkWidget *widget,
2752 GtkCalendar *calendar = GTK_CALENDAR (widget);
2753 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
2756 if (gtk_cairo_should_draw_window (cr, gtk_widget_get_window (widget)))
2758 gdk_cairo_set_source_color (cr, BACKGROUND_COLOR (widget));
2759 cairo_rectangle (cr,
2761 gtk_widget_get_allocated_width (widget),
2762 gtk_widget_get_allocated_height (widget));
2764 gtk_paint_shadow (gtk_widget_get_style (widget), cr,
2765 gtk_widget_get_state (widget), GTK_SHADOW_IN,
2768 gtk_widget_get_allocated_width (widget),
2769 gtk_widget_get_allocated_height (widget));
2772 calendar_paint_main (calendar, cr);
2774 if (priv->display_flags & GTK_CALENDAR_SHOW_HEADING)
2776 calendar_paint_header (calendar, cr);
2777 for (i = 0; i < 4; i++)
2778 calendar_paint_arrow (calendar, cr, i);
2781 if (priv->display_flags & GTK_CALENDAR_SHOW_DAY_NAMES)
2782 calendar_paint_day_names (calendar, cr);
2784 if (priv->display_flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
2785 calendar_paint_week_numbers (calendar, cr);
2791 /****************************************
2793 ****************************************/
2796 calendar_arrow_action (GtkCalendar *calendar,
2801 case ARROW_YEAR_LEFT:
2802 calendar_set_year_prev (calendar);
2804 case ARROW_YEAR_RIGHT:
2805 calendar_set_year_next (calendar);
2807 case ARROW_MONTH_LEFT:
2808 calendar_set_month_prev (calendar);
2810 case ARROW_MONTH_RIGHT:
2811 calendar_set_month_next (calendar);
2819 calendar_timer (gpointer data)
2821 GtkCalendar *calendar = data;
2822 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2823 gboolean retval = FALSE;
2827 calendar_arrow_action (calendar, priv->click_child);
2829 if (priv->need_timer)
2831 GtkSettings *settings;
2834 settings = gtk_widget_get_settings (GTK_WIDGET (calendar));
2835 g_object_get (settings, "gtk-timeout-repeat", &timeout, NULL);
2837 priv->need_timer = FALSE;
2838 priv->timer = gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT_IDLE,
2839 timeout * SCROLL_DELAY_FACTOR,
2840 (GSourceFunc) calendar_timer,
2841 (gpointer) calendar, NULL);
2851 calendar_start_spinning (GtkCalendar *calendar,
2854 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2856 priv->click_child = click_child;
2860 GtkSettings *settings;
2863 settings = gtk_widget_get_settings (GTK_WIDGET (calendar));
2864 g_object_get (settings, "gtk-timeout-initial", &timeout, NULL);
2866 priv->need_timer = TRUE;
2867 priv->timer = gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT_IDLE,
2869 (GSourceFunc) calendar_timer,
2870 (gpointer) calendar, NULL);
2875 calendar_stop_spinning (GtkCalendar *calendar)
2877 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2881 g_source_remove (priv->timer);
2883 priv->need_timer = FALSE;
2888 calendar_main_button_press (GtkCalendar *calendar,
2889 GdkEventButton *event)
2891 GtkWidget *widget = GTK_WIDGET (calendar);
2892 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2898 GtkAllocation allocation;
2900 x = (gint) (event->x);
2901 y = (gint) (event->y);
2903 gdk_window_get_position (priv->main_win, &win_x, &win_y);
2904 gtk_widget_get_allocation (widget, &allocation);
2906 row = calendar_row_from_y (calendar, y + win_y - allocation.y);
2907 col = calendar_column_from_x (calendar, x + win_x - allocation.x);
2909 /* If row or column isn't found, just return. */
2910 if (row == -1 || col == -1)
2913 day_month = priv->day_month[row][col];
2915 if (event->type == GDK_BUTTON_PRESS)
2917 day = priv->day[row][col];
2919 if (day_month == MONTH_PREV)
2920 calendar_set_month_prev (calendar);
2921 else if (day_month == MONTH_NEXT)
2922 calendar_set_month_next (calendar);
2924 if (!gtk_widget_has_focus (widget))
2925 gtk_widget_grab_focus (widget);
2927 if (event->button == 1)
2930 priv->drag_start_x = x;
2931 priv->drag_start_y = y;
2934 calendar_select_and_focus_day (calendar, day);
2936 else if (event->type == GDK_2BUTTON_PRESS)
2939 if (day_month == MONTH_CURRENT)
2940 g_signal_emit (calendar,
2941 gtk_calendar_signals[DAY_SELECTED_DOUBLE_CLICK_SIGNAL],
2947 gtk_calendar_button_press (GtkWidget *widget,
2948 GdkEventButton *event)
2950 GtkCalendar *calendar = GTK_CALENDAR (widget);
2951 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
2954 if (event->window == priv->main_win)
2955 calendar_main_button_press (calendar, event);
2957 if (!gtk_widget_has_focus (widget))
2958 gtk_widget_grab_focus (widget);
2960 for (arrow = ARROW_YEAR_LEFT; arrow <= ARROW_MONTH_RIGHT; arrow++)
2962 if (event->window == priv->arrow_win[arrow])
2965 /* only call the action on single click, not double */
2966 if (event->type == GDK_BUTTON_PRESS)
2968 if (event->button == 1)
2969 calendar_start_spinning (calendar, arrow);
2971 calendar_arrow_action (calendar, arrow);
2982 gtk_calendar_button_release (GtkWidget *widget,
2983 GdkEventButton *event)
2985 GtkCalendar *calendar = GTK_CALENDAR (widget);
2986 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
2988 if (event->button == 1)
2990 calendar_stop_spinning (calendar);
3000 gtk_calendar_motion_notify (GtkWidget *widget,
3001 GdkEventMotion *event)
3003 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3004 gint event_x, event_y;
3006 event_x = (gint) (event->x);
3007 event_y = (gint) (event->y);
3011 if (gtk_drag_check_threshold (widget,
3012 priv->drag_start_x, priv->drag_start_y,
3013 event->x, event->y))
3015 GdkDragContext *context;
3016 GtkTargetList *target_list = gtk_target_list_new (NULL, 0);
3017 gtk_target_list_add_text_targets (target_list, 0);
3018 context = gtk_drag_begin (widget, target_list, GDK_ACTION_COPY,
3019 1, (GdkEvent *)event);
3022 gtk_target_list_unref (target_list);
3023 gtk_drag_set_icon_default (context);
3031 gtk_calendar_enter_notify (GtkWidget *widget,
3032 GdkEventCrossing *event)
3034 GtkCalendar *calendar = GTK_CALENDAR (widget);
3035 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3037 if (event->window == priv->arrow_win[ARROW_MONTH_LEFT])
3039 priv->arrow_state[ARROW_MONTH_LEFT] = GTK_STATE_PRELIGHT;
3040 calendar_invalidate_arrow (calendar, ARROW_MONTH_LEFT);
3043 if (event->window == priv->arrow_win[ARROW_MONTH_RIGHT])
3045 priv->arrow_state[ARROW_MONTH_RIGHT] = GTK_STATE_PRELIGHT;
3046 calendar_invalidate_arrow (calendar, ARROW_MONTH_RIGHT);
3049 if (event->window == priv->arrow_win[ARROW_YEAR_LEFT])
3051 priv->arrow_state[ARROW_YEAR_LEFT] = GTK_STATE_PRELIGHT;
3052 calendar_invalidate_arrow (calendar, ARROW_YEAR_LEFT);
3055 if (event->window == priv->arrow_win[ARROW_YEAR_RIGHT])
3057 priv->arrow_state[ARROW_YEAR_RIGHT] = GTK_STATE_PRELIGHT;
3058 calendar_invalidate_arrow (calendar, ARROW_YEAR_RIGHT);
3065 gtk_calendar_leave_notify (GtkWidget *widget,
3066 GdkEventCrossing *event)
3068 GtkCalendar *calendar = GTK_CALENDAR (widget);
3069 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3071 if (event->window == priv->arrow_win[ARROW_MONTH_LEFT])
3073 priv->arrow_state[ARROW_MONTH_LEFT] = GTK_STATE_NORMAL;
3074 calendar_invalidate_arrow (calendar, ARROW_MONTH_LEFT);
3077 if (event->window == priv->arrow_win[ARROW_MONTH_RIGHT])
3079 priv->arrow_state[ARROW_MONTH_RIGHT] = GTK_STATE_NORMAL;
3080 calendar_invalidate_arrow (calendar, ARROW_MONTH_RIGHT);
3083 if (event->window == priv->arrow_win[ARROW_YEAR_LEFT])
3085 priv->arrow_state[ARROW_YEAR_LEFT] = GTK_STATE_NORMAL;
3086 calendar_invalidate_arrow (calendar, ARROW_YEAR_LEFT);
3089 if (event->window == priv->arrow_win[ARROW_YEAR_RIGHT])
3091 priv->arrow_state[ARROW_YEAR_RIGHT] = GTK_STATE_NORMAL;
3092 calendar_invalidate_arrow (calendar, ARROW_YEAR_RIGHT);
3099 gtk_calendar_scroll (GtkWidget *widget,
3100 GdkEventScroll *event)
3102 GtkCalendar *calendar = GTK_CALENDAR (widget);
3104 if (event->direction == GDK_SCROLL_UP)
3106 if (!gtk_widget_has_focus (widget))
3107 gtk_widget_grab_focus (widget);
3108 calendar_set_month_prev (calendar);
3110 else if (event->direction == GDK_SCROLL_DOWN)
3112 if (!gtk_widget_has_focus (widget))
3113 gtk_widget_grab_focus (widget);
3114 calendar_set_month_next (calendar);
3123 /****************************************
3125 ****************************************/
3128 move_focus (GtkCalendar *calendar,
3131 GtkCalendarPrivate *priv = calendar->priv;
3132 GtkTextDirection text_dir = gtk_widget_get_direction (GTK_WIDGET (calendar));
3134 if ((text_dir == GTK_TEXT_DIR_LTR && direction == -1) ||
3135 (text_dir == GTK_TEXT_DIR_RTL && direction == 1))
3137 if (priv->focus_col > 0)
3139 else if (priv->focus_row > 0)
3141 priv->focus_col = 6;
3145 if (priv->focus_col < 0)
3146 priv->focus_col = 6;
3147 if (priv->focus_row < 0)
3148 priv->focus_row = 5;
3152 if (priv->focus_col < 6)
3154 else if (priv->focus_row < 5)
3156 priv->focus_col = 0;
3160 if (priv->focus_col < 0)
3161 priv->focus_col = 0;
3162 if (priv->focus_row < 0)
3163 priv->focus_row = 0;
3168 gtk_calendar_key_press (GtkWidget *widget,
3171 GtkCalendar *calendar = GTK_CALENDAR (widget);
3172 GtkCalendarPrivate *priv = calendar->priv;
3180 old_focus_row = priv->focus_row;
3181 old_focus_col = priv->focus_col;
3183 switch (event->keyval)
3185 case GDK_KEY_KP_Left:
3188 if (event->state & GDK_CONTROL_MASK)
3189 calendar_set_month_prev (calendar);
3192 move_focus (calendar, -1);
3193 calendar_invalidate_day (calendar, old_focus_row, old_focus_col);
3194 calendar_invalidate_day (calendar, priv->focus_row,
3198 case GDK_KEY_KP_Right:
3201 if (event->state & GDK_CONTROL_MASK)
3202 calendar_set_month_next (calendar);
3205 move_focus (calendar, 1);
3206 calendar_invalidate_day (calendar, old_focus_row, old_focus_col);
3207 calendar_invalidate_day (calendar, priv->focus_row,
3214 if (event->state & GDK_CONTROL_MASK)
3215 calendar_set_year_prev (calendar);
3218 if (priv->focus_row > 0)
3220 if (priv->focus_row < 0)
3221 priv->focus_row = 5;
3222 if (priv->focus_col < 0)
3223 priv->focus_col = 6;
3224 calendar_invalidate_day (calendar, old_focus_row, old_focus_col);
3225 calendar_invalidate_day (calendar, priv->focus_row,
3229 case GDK_KEY_KP_Down:
3232 if (event->state & GDK_CONTROL_MASK)
3233 calendar_set_year_next (calendar);
3236 if (priv->focus_row < 5)
3238 if (priv->focus_col < 0)
3239 priv->focus_col = 0;
3240 calendar_invalidate_day (calendar, old_focus_row, old_focus_col);
3241 calendar_invalidate_day (calendar, priv->focus_row,
3245 case GDK_KEY_KP_Space:
3247 row = priv->focus_row;
3248 col = priv->focus_col;
3250 if (row > -1 && col > -1)
3254 day = priv->day[row][col];
3255 if (priv->day_month[row][col] == MONTH_PREV)
3256 calendar_set_month_prev (calendar);
3257 else if (priv->day_month[row][col] == MONTH_NEXT)
3258 calendar_set_month_next (calendar);
3260 calendar_select_and_focus_day (calendar, day);
3268 /****************************************
3269 * Misc widget methods *
3270 ****************************************/
3273 gtk_calendar_state_changed (GtkWidget *widget,
3274 GtkStateType previous_state)
3276 GtkCalendar *calendar = GTK_CALENDAR (widget);
3277 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3280 if (!gtk_widget_is_sensitive (widget))
3283 calendar_stop_spinning (calendar);
3286 for (i = 0; i < 4; i++)
3287 if (gtk_widget_is_sensitive (widget))
3288 priv->arrow_state[i] = GTK_STATE_NORMAL;
3290 priv->arrow_state[i] = GTK_STATE_INSENSITIVE;
3294 gtk_calendar_grab_notify (GtkWidget *widget,
3295 gboolean was_grabbed)
3298 calendar_stop_spinning (GTK_CALENDAR (widget));
3302 gtk_calendar_focus_out (GtkWidget *widget,
3303 GdkEventFocus *event)
3305 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3306 GtkCalendar *calendar = GTK_CALENDAR (widget);
3308 calendar_queue_refresh (calendar);
3309 calendar_stop_spinning (calendar);
3317 /****************************************
3319 ****************************************/
3322 gtk_calendar_drag_data_get (GtkWidget *widget,
3323 GdkDragContext *context,
3324 GtkSelectionData *selection_data,
3328 GtkCalendar *calendar = GTK_CALENDAR (widget);
3329 GtkCalendarPrivate *priv = calendar->priv;
3334 date = g_date_new_dmy (priv->selected_day, priv->month + 1, priv->year);
3335 len = g_date_strftime (str, 127, "%x", date);
3336 gtk_selection_data_set_text (selection_data, str, len);
3341 /* Get/set whether drag_motion requested the drag data and
3342 * drag_data_received should thus not actually insert the data,
3343 * since the data doesn't result from a drop.
3346 set_status_pending (GdkDragContext *context,
3347 GdkDragAction suggested_action)
3349 g_object_set_data (G_OBJECT (context),
3350 I_("gtk-calendar-status-pending"),
3351 GINT_TO_POINTER (suggested_action));
3354 static GdkDragAction
3355 get_status_pending (GdkDragContext *context)
3357 return GPOINTER_TO_INT (g_object_get_data (G_OBJECT (context),
3358 "gtk-calendar-status-pending"));
3362 gtk_calendar_drag_leave (GtkWidget *widget,
3363 GdkDragContext *context,
3366 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3368 priv->drag_highlight = 0;
3369 gtk_drag_unhighlight (widget);
3374 gtk_calendar_drag_motion (GtkWidget *widget,
3375 GdkDragContext *context,
3380 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3383 if (!priv->drag_highlight)
3385 priv->drag_highlight = 1;
3386 gtk_drag_highlight (widget);
3389 target = gtk_drag_dest_find_target (widget, context, NULL);
3390 if (target == GDK_NONE || gdk_drag_context_get_suggested_action (context) == 0)
3391 gdk_drag_status (context, 0, time);
3394 set_status_pending (context, gdk_drag_context_get_suggested_action (context));
3395 gtk_drag_get_data (widget, context, target, time);
3402 gtk_calendar_drag_drop (GtkWidget *widget,
3403 GdkDragContext *context,
3410 target = gtk_drag_dest_find_target (widget, context, NULL);
3411 if (target != GDK_NONE)
3413 gtk_drag_get_data (widget, context,
3423 gtk_calendar_drag_data_received (GtkWidget *widget,
3424 GdkDragContext *context,
3427 GtkSelectionData *selection_data,
3431 GtkCalendar *calendar = GTK_CALENDAR (widget);
3432 GtkCalendarPrivate *priv = calendar->priv;
3433 guint day, month, year;
3436 GdkDragAction suggested_action;
3438 suggested_action = get_status_pending (context);
3440 if (suggested_action)
3442 set_status_pending (context, 0);
3444 /* We are getting this data due to a request in drag_motion,
3445 * rather than due to a request in drag_drop, so we are just
3446 * supposed to call drag_status, not actually paste in the
3449 str = (gchar*) gtk_selection_data_get_text (selection_data);
3453 date = g_date_new ();
3454 g_date_set_parse (date, str);
3455 if (!g_date_valid (date))
3456 suggested_action = 0;
3461 suggested_action = 0;
3463 gdk_drag_status (context, suggested_action, time);
3468 date = g_date_new ();
3469 str = (gchar*) gtk_selection_data_get_text (selection_data);
3472 g_date_set_parse (date, str);
3476 if (!g_date_valid (date))
3478 g_warning ("Received invalid date data\n");
3480 gtk_drag_finish (context, FALSE, FALSE, time);
3484 day = g_date_get_day (date);
3485 month = g_date_get_month (date);
3486 year = g_date_get_year (date);
3489 gtk_drag_finish (context, TRUE, FALSE, time);
3492 g_object_freeze_notify (G_OBJECT (calendar));
3493 if (!(priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
3494 && (priv->display_flags & GTK_CALENDAR_SHOW_HEADING))
3495 gtk_calendar_select_month (calendar, month - 1, year);
3496 gtk_calendar_select_day (calendar, day);
3497 g_object_thaw_notify (G_OBJECT (calendar));
3501 /****************************************
3503 ****************************************/
3508 * Creates a new calendar, with the current date being selected.
3510 * Return value: a newly #GtkCalendar widget
3513 gtk_calendar_new (void)
3515 return g_object_new (GTK_TYPE_CALENDAR, NULL);
3519 * gtk_calendar_get_display_options:
3520 * @calendar: a #GtkCalendar
3522 * Returns the current display options of @calendar.
3524 * Return value: the display options.
3528 GtkCalendarDisplayOptions
3529 gtk_calendar_get_display_options (GtkCalendar *calendar)
3531 g_return_val_if_fail (GTK_IS_CALENDAR (calendar), 0);
3533 return calendar->priv->display_flags;
3537 * gtk_calendar_set_display_options:
3538 * @calendar: a #GtkCalendar
3539 * @flags: the display options to set
3541 * Sets display options (whether to display the heading and the month
3547 gtk_calendar_set_display_options (GtkCalendar *calendar,
3548 GtkCalendarDisplayOptions flags)
3550 GtkWidget *widget = GTK_WIDGET (calendar);
3551 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
3553 GtkCalendarDisplayOptions old_flags;
3555 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3557 old_flags = priv->display_flags;
3559 if (gtk_widget_get_realized (widget))
3561 if ((flags ^ priv->display_flags) & GTK_CALENDAR_NO_MONTH_CHANGE)
3564 if (! (flags & GTK_CALENDAR_NO_MONTH_CHANGE)
3565 && (priv->display_flags & GTK_CALENDAR_SHOW_HEADING))
3567 priv->display_flags &= ~GTK_CALENDAR_NO_MONTH_CHANGE;
3568 calendar_realize_arrows (calendar);
3572 calendar_unrealize_arrows (calendar);
3576 if ((flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_HEADING)
3580 if (flags & GTK_CALENDAR_SHOW_HEADING)
3582 priv->display_flags |= GTK_CALENDAR_SHOW_HEADING;
3583 calendar_realize_arrows (calendar);
3587 calendar_unrealize_arrows (calendar);
3592 if ((flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_DAY_NAMES)
3596 if (flags & GTK_CALENDAR_SHOW_DAY_NAMES)
3597 priv->display_flags |= GTK_CALENDAR_SHOW_DAY_NAMES;
3600 if ((flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
3604 if (flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
3605 priv->display_flags |= GTK_CALENDAR_SHOW_WEEK_NUMBERS;
3608 if ((flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_DETAILS)
3611 priv->display_flags = flags;
3613 gtk_widget_queue_resize (GTK_WIDGET (calendar));
3616 priv->display_flags = flags;
3618 g_object_freeze_notify (G_OBJECT (calendar));
3619 if ((old_flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_HEADING)
3620 g_object_notify (G_OBJECT (calendar), "show-heading");
3621 if ((old_flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_DAY_NAMES)
3622 g_object_notify (G_OBJECT (calendar), "show-day-names");
3623 if ((old_flags ^ priv->display_flags) & GTK_CALENDAR_NO_MONTH_CHANGE)
3624 g_object_notify (G_OBJECT (calendar), "no-month-change");
3625 if ((old_flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
3626 g_object_notify (G_OBJECT (calendar), "show-week-numbers");
3627 g_object_thaw_notify (G_OBJECT (calendar));
3631 * gtk_calendar_select_month:
3632 * @calendar: a #GtkCalendar
3633 * @month: a month number between 0 and 11.
3634 * @year: the year the month is in.
3636 * Shifts the calendar to a different month.
3639 gtk_calendar_select_month (GtkCalendar *calendar,
3643 GtkCalendarPrivate *priv;
3645 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3646 g_return_if_fail (month <= 11);
3648 priv = calendar->priv;
3650 priv->month = month;
3653 calendar_compute_days (calendar);
3654 calendar_queue_refresh (calendar);
3656 g_object_freeze_notify (G_OBJECT (calendar));
3657 g_object_notify (G_OBJECT (calendar), "month");
3658 g_object_notify (G_OBJECT (calendar), "year");
3659 g_object_thaw_notify (G_OBJECT (calendar));
3661 g_signal_emit (calendar,
3662 gtk_calendar_signals[MONTH_CHANGED_SIGNAL],
3667 * gtk_calendar_select_day:
3668 * @calendar: a #GtkCalendar.
3669 * @day: the day number between 1 and 31, or 0 to unselect
3670 * the currently selected day.
3672 * Selects a day from the current month.
3675 gtk_calendar_select_day (GtkCalendar *calendar,
3678 GtkCalendarPrivate *priv;
3680 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3681 g_return_if_fail (day <= 31);
3683 priv = calendar->priv;
3685 /* Deselect the old day */
3686 if (priv->selected_day > 0)
3690 selected_day = priv->selected_day;
3691 priv->selected_day = 0;
3692 if (gtk_widget_is_drawable (GTK_WIDGET (calendar)))
3693 calendar_invalidate_day_num (calendar, selected_day);
3696 priv->selected_day = day;
3698 /* Select the new day */
3701 if (gtk_widget_is_drawable (GTK_WIDGET (calendar)))
3702 calendar_invalidate_day_num (calendar, day);
3705 g_object_notify (G_OBJECT (calendar), "day");
3707 g_signal_emit (calendar,
3708 gtk_calendar_signals[DAY_SELECTED_SIGNAL],
3713 * gtk_calendar_clear_marks:
3714 * @calendar: a #GtkCalendar
3716 * Remove all visual markers.
3719 gtk_calendar_clear_marks (GtkCalendar *calendar)
3721 GtkCalendarPrivate *priv;
3724 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3726 priv = calendar->priv;
3728 for (day = 0; day < 31; day++)
3730 priv->marked_date[day] = FALSE;
3733 priv->num_marked_dates = 0;
3734 calendar_queue_refresh (calendar);
3738 * gtk_calendar_mark_day:
3739 * @calendar: a #GtkCalendar
3740 * @day: the day number to mark between 1 and 31.
3742 * Places a visual marker on a particular day.
3745 gtk_calendar_mark_day (GtkCalendar *calendar,
3748 GtkCalendarPrivate *priv;
3750 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3752 priv = calendar->priv;
3754 if (day >= 1 && day <= 31 && !priv->marked_date[day-1])
3756 priv->marked_date[day - 1] = TRUE;
3757 priv->num_marked_dates++;
3758 calendar_invalidate_day_num (calendar, day);
3763 * gtk_calendar_get_day_is_marked:
3764 * @calendar: a #GtkCalendar
3765 * @day: the day number between 1 and 31.
3767 * Returns if the @day of the @calendar is already marked.
3769 * Returns: whether the day is marked.
3774 gtk_calendar_get_day_is_marked (GtkCalendar *calendar,
3777 GtkCalendarPrivate *priv;
3779 g_return_val_if_fail (GTK_IS_CALENDAR (calendar), FALSE);
3781 priv = calendar->priv;
3783 if (day >= 1 && day <= 31)
3784 return priv->marked_date[day - 1];
3790 * gtk_calendar_unmark_day:
3791 * @calendar: a #GtkCalendar.
3792 * @day: the day number to unmark between 1 and 31.
3794 * Removes the visual marker from a particular day.
3797 gtk_calendar_unmark_day (GtkCalendar *calendar,
3800 GtkCalendarPrivate *priv;
3802 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3804 priv = calendar->priv;
3806 if (day >= 1 && day <= 31 && priv->marked_date[day-1])
3808 priv->marked_date[day - 1] = FALSE;
3809 priv->num_marked_dates--;
3810 calendar_invalidate_day_num (calendar, day);
3815 * gtk_calendar_get_date:
3816 * @calendar: a #GtkCalendar
3817 * @year: (allow-none): location to store the year number, or %NULL
3818 * @month: (allow-none): location to store the month number (between 0 and 11), or %NULL
3819 * @day: (allow-none): location to store the day number (between 1 and 31), or %NULL
3821 * Obtains the selected date from a #GtkCalendar.
3824 gtk_calendar_get_date (GtkCalendar *calendar,
3829 GtkCalendarPrivate *priv;
3831 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3833 priv = calendar->priv;
3839 *month = priv->month;
3842 *day = priv->selected_day;
3846 * gtk_calendar_set_detail_func:
3847 * @calendar: a #GtkCalendar.
3848 * @func: a function providing details for each day.
3849 * @data: data to pass to @func invokations.
3850 * @destroy: a function for releasing @data.
3852 * Installs a function which provides Pango markup with detail information
3853 * for each day. Examples for such details are holidays or appointments. That
3854 * information is shown below each day when #GtkCalendar:show-details is set.
3855 * A tooltip containing with full detail information is provided, if the entire
3856 * text should not fit into the details area, or if #GtkCalendar:show-details
3859 * The size of the details area can be restricted by setting the
3860 * #GtkCalendar:detail-width-chars and #GtkCalendar:detail-height-rows
3866 gtk_calendar_set_detail_func (GtkCalendar *calendar,
3867 GtkCalendarDetailFunc func,
3869 GDestroyNotify destroy)
3871 GtkCalendarPrivate *priv;
3873 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3875 priv = GTK_CALENDAR_GET_PRIVATE (calendar);
3877 if (priv->detail_func_destroy)
3878 priv->detail_func_destroy (priv->detail_func_user_data);
3880 priv->detail_func = func;
3881 priv->detail_func_user_data = data;
3882 priv->detail_func_destroy = destroy;
3884 gtk_widget_set_has_tooltip (GTK_WIDGET (calendar),
3885 NULL != priv->detail_func);
3886 gtk_widget_queue_resize (GTK_WIDGET (calendar));
3890 * gtk_calendar_set_detail_width_chars:
3891 * @calendar: a #GtkCalendar.
3892 * @chars: detail width in characters.
3894 * Updates the width of detail cells.
3895 * See #GtkCalendar:detail-width-chars.
3900 gtk_calendar_set_detail_width_chars (GtkCalendar *calendar,
3903 GtkCalendarPrivate *priv;
3905 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3907 priv = GTK_CALENDAR_GET_PRIVATE (calendar);
3909 if (chars != priv->detail_width_chars)
3911 priv->detail_width_chars = chars;
3912 g_object_notify (G_OBJECT (calendar), "detail-width-chars");
3913 gtk_widget_queue_resize_no_redraw (GTK_WIDGET (calendar));
3918 * gtk_calendar_set_detail_height_rows:
3919 * @calendar: a #GtkCalendar.
3920 * @rows: detail height in rows.
3922 * Updates the height of detail cells.
3923 * See #GtkCalendar:detail-height-rows.
3928 gtk_calendar_set_detail_height_rows (GtkCalendar *calendar,
3931 GtkCalendarPrivate *priv;
3933 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3935 priv = GTK_CALENDAR_GET_PRIVATE (calendar);
3937 if (rows != priv->detail_height_rows)
3939 priv->detail_height_rows = rows;
3940 g_object_notify (G_OBJECT (calendar), "detail-height-rows");
3941 gtk_widget_queue_resize_no_redraw (GTK_WIDGET (calendar));
3946 * gtk_calendar_get_detail_width_chars:
3947 * @calendar: a #GtkCalendar.
3949 * Queries the width of detail cells, in characters.
3950 * See #GtkCalendar:detail-width-chars.
3954 * Return value: The width of detail cells, in characters.
3957 gtk_calendar_get_detail_width_chars (GtkCalendar *calendar)
3959 g_return_val_if_fail (GTK_IS_CALENDAR (calendar), 0);
3960 return GTK_CALENDAR_GET_PRIVATE (calendar)->detail_width_chars;
3964 * gtk_calendar_get_detail_height_rows:
3965 * @calendar: a #GtkCalendar.
3967 * Queries the height of detail cells, in rows.
3968 * See #GtkCalendar:detail-width-chars.
3972 * Return value: The height of detail cells, in rows.
3975 gtk_calendar_get_detail_height_rows (GtkCalendar *calendar)
3977 g_return_val_if_fail (GTK_IS_CALENDAR (calendar), 0);
3978 return GTK_CALENDAR_GET_PRIVATE (calendar)->detail_height_rows;