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_size_request (GtkWidget *widget,
345 GtkRequisition *requisition);
346 static void gtk_calendar_size_allocate (GtkWidget *widget,
347 GtkAllocation *allocation);
348 static gboolean gtk_calendar_draw (GtkWidget *widget,
350 static gboolean gtk_calendar_button_press (GtkWidget *widget,
351 GdkEventButton *event);
352 static gboolean gtk_calendar_button_release (GtkWidget *widget,
353 GdkEventButton *event);
354 static gboolean gtk_calendar_motion_notify (GtkWidget *widget,
355 GdkEventMotion *event);
356 static gboolean gtk_calendar_enter_notify (GtkWidget *widget,
357 GdkEventCrossing *event);
358 static gboolean gtk_calendar_leave_notify (GtkWidget *widget,
359 GdkEventCrossing *event);
360 static gboolean gtk_calendar_scroll (GtkWidget *widget,
361 GdkEventScroll *event);
362 static gboolean gtk_calendar_key_press (GtkWidget *widget,
364 static gboolean gtk_calendar_focus_out (GtkWidget *widget,
365 GdkEventFocus *event);
366 static void gtk_calendar_grab_notify (GtkWidget *widget,
367 gboolean was_grabbed);
368 static void gtk_calendar_state_changed (GtkWidget *widget,
369 GtkStateType previous_state);
370 static gboolean gtk_calendar_query_tooltip (GtkWidget *widget,
373 gboolean keyboard_mode,
374 GtkTooltip *tooltip);
376 static void gtk_calendar_drag_data_get (GtkWidget *widget,
377 GdkDragContext *context,
378 GtkSelectionData *selection_data,
381 static void gtk_calendar_drag_data_received (GtkWidget *widget,
382 GdkDragContext *context,
385 GtkSelectionData *selection_data,
388 static gboolean gtk_calendar_drag_motion (GtkWidget *widget,
389 GdkDragContext *context,
393 static void gtk_calendar_drag_leave (GtkWidget *widget,
394 GdkDragContext *context,
396 static gboolean gtk_calendar_drag_drop (GtkWidget *widget,
397 GdkDragContext *context,
402 static void calendar_start_spinning (GtkCalendar *calendar,
404 static void calendar_stop_spinning (GtkCalendar *calendar);
406 static void calendar_invalidate_day (GtkCalendar *widget,
409 static void calendar_invalidate_day_num (GtkCalendar *widget,
411 static void calendar_invalidate_arrow (GtkCalendar *widget,
414 static void calendar_compute_days (GtkCalendar *calendar);
415 static gint calendar_get_xsep (GtkCalendar *calendar);
416 static gint calendar_get_ysep (GtkCalendar *calendar);
417 static gint calendar_get_inner_border (GtkCalendar *calendar);
419 static char *default_abbreviated_dayname[7];
420 static char *default_monthname[12];
422 G_DEFINE_TYPE (GtkCalendar, gtk_calendar, GTK_TYPE_WIDGET)
425 gtk_calendar_class_init (GtkCalendarClass *class)
427 GObjectClass *gobject_class;
428 GtkWidgetClass *widget_class;
430 gobject_class = (GObjectClass*) class;
431 widget_class = (GtkWidgetClass*) class;
433 gobject_class->set_property = gtk_calendar_set_property;
434 gobject_class->get_property = gtk_calendar_get_property;
435 gobject_class->finalize = gtk_calendar_finalize;
437 widget_class->destroy = gtk_calendar_destroy;
438 widget_class->realize = gtk_calendar_realize;
439 widget_class->unrealize = gtk_calendar_unrealize;
440 widget_class->draw = gtk_calendar_draw;
441 widget_class->size_request = gtk_calendar_size_request;
442 widget_class->size_allocate = gtk_calendar_size_allocate;
443 widget_class->button_press_event = gtk_calendar_button_press;
444 widget_class->button_release_event = gtk_calendar_button_release;
445 widget_class->motion_notify_event = gtk_calendar_motion_notify;
446 widget_class->enter_notify_event = gtk_calendar_enter_notify;
447 widget_class->leave_notify_event = gtk_calendar_leave_notify;
448 widget_class->key_press_event = gtk_calendar_key_press;
449 widget_class->scroll_event = gtk_calendar_scroll;
450 widget_class->state_changed = gtk_calendar_state_changed;
451 widget_class->grab_notify = gtk_calendar_grab_notify;
452 widget_class->focus_out_event = gtk_calendar_focus_out;
453 widget_class->query_tooltip = gtk_calendar_query_tooltip;
455 widget_class->drag_data_get = gtk_calendar_drag_data_get;
456 widget_class->drag_motion = gtk_calendar_drag_motion;
457 widget_class->drag_leave = gtk_calendar_drag_leave;
458 widget_class->drag_drop = gtk_calendar_drag_drop;
459 widget_class->drag_data_received = gtk_calendar_drag_data_received;
465 * This property gets initially set to the current year.
467 g_object_class_install_property (gobject_class,
469 g_param_spec_int ("year",
471 P_("The selected year"),
473 GTK_PARAM_READWRITE));
478 * The selected month (as a number between 0 and 11).
479 * This property gets initially set to the current month.
481 g_object_class_install_property (gobject_class,
483 g_param_spec_int ("month",
485 P_("The selected month (as a number between 0 and 11)"),
487 GTK_PARAM_READWRITE));
492 * The selected day (as a number between 1 and 31, or 0
493 * to unselect the currently selected day).
494 * This property gets initially set to the current day.
496 g_object_class_install_property (gobject_class,
498 g_param_spec_int ("day",
500 P_("The selected day (as a number between 1 and 31, or 0 to unselect the currently selected day)"),
502 GTK_PARAM_READWRITE));
505 * GtkCalendar:show-heading:
507 * Determines whether a heading is displayed.
511 g_object_class_install_property (gobject_class,
513 g_param_spec_boolean ("show-heading",
515 P_("If TRUE, a heading is displayed"),
517 GTK_PARAM_READWRITE));
520 * GtkCalendar:show-day-names:
522 * Determines whether day names are displayed.
526 g_object_class_install_property (gobject_class,
528 g_param_spec_boolean ("show-day-names",
529 P_("Show Day Names"),
530 P_("If TRUE, day names are displayed"),
532 GTK_PARAM_READWRITE));
534 * GtkCalendar:no-month-change:
536 * Determines whether the selected month can be changed.
540 g_object_class_install_property (gobject_class,
541 PROP_NO_MONTH_CHANGE,
542 g_param_spec_boolean ("no-month-change",
543 P_("No Month Change"),
544 P_("If TRUE, the selected month cannot be changed"),
546 GTK_PARAM_READWRITE));
549 * GtkCalendar:show-week-numbers:
551 * Determines whether week numbers are displayed.
555 g_object_class_install_property (gobject_class,
556 PROP_SHOW_WEEK_NUMBERS,
557 g_param_spec_boolean ("show-week-numbers",
558 P_("Show Week Numbers"),
559 P_("If TRUE, week numbers are displayed"),
561 GTK_PARAM_READWRITE));
564 * GtkCalendar:detail-width-chars:
566 * Width of a detail cell, in characters.
567 * A value of 0 allows any width. See gtk_calendar_set_detail_func().
571 g_object_class_install_property (gobject_class,
572 PROP_DETAIL_WIDTH_CHARS,
573 g_param_spec_int ("detail-width-chars",
575 P_("Details width in characters"),
577 GTK_PARAM_READWRITE));
580 * GtkCalendar:detail-height-rows:
582 * Height of a detail cell, in rows.
583 * A value of 0 allows any width. See gtk_calendar_set_detail_func().
587 g_object_class_install_property (gobject_class,
588 PROP_DETAIL_HEIGHT_ROWS,
589 g_param_spec_int ("detail-height-rows",
590 P_("Details Height"),
591 P_("Details height in rows"),
593 GTK_PARAM_READWRITE));
596 * GtkCalendar:show-details:
598 * Determines whether details are shown directly in the widget, or if they are
599 * available only as tooltip. When this property is set days with details are
604 g_object_class_install_property (gobject_class,
606 g_param_spec_boolean ("show-details",
608 P_("If TRUE, details are shown"),
610 GTK_PARAM_READWRITE));
614 * GtkCalendar:inner-border
616 * The spacing around the day/week headers and main area.
618 gtk_widget_class_install_style_property (widget_class,
619 g_param_spec_int ("inner-border",
621 P_("Inner border space"),
623 GTK_PARAM_READABLE));
626 * GtkCalndar:vertical-separation
628 * Separation between day headers and main area.
630 gtk_widget_class_install_style_property (widget_class,
631 g_param_spec_int ("vertical-separation",
632 P_("Vertical separation"),
633 P_("Space between day headers and main area"),
635 GTK_PARAM_READABLE));
638 * GtkCalendar:horizontal-separation
640 * Separation between week headers and main area.
642 gtk_widget_class_install_style_property (widget_class,
643 g_param_spec_int ("horizontal-separation",
644 P_("Horizontal separation"),
645 P_("Space between week headers and main area"),
647 GTK_PARAM_READABLE));
650 * GtkCalendar::month-changed:
651 * @calendar: the object which received the signal.
653 * Emitted when the user clicks a button to change the selected month on a
656 gtk_calendar_signals[MONTH_CHANGED_SIGNAL] =
657 g_signal_new (I_("month-changed"),
658 G_OBJECT_CLASS_TYPE (gobject_class),
660 G_STRUCT_OFFSET (GtkCalendarClass, month_changed),
662 _gtk_marshal_VOID__VOID,
666 * GtkCalendar::day-selected:
667 * @calendar: the object which received the signal.
669 * Emitted when the user selects a day.
671 gtk_calendar_signals[DAY_SELECTED_SIGNAL] =
672 g_signal_new (I_("day-selected"),
673 G_OBJECT_CLASS_TYPE (gobject_class),
675 G_STRUCT_OFFSET (GtkCalendarClass, day_selected),
677 _gtk_marshal_VOID__VOID,
681 * GtkCalendar::day-selected-double-click:
682 * @calendar: the object which received the signal.
684 * Emitted when the user double-clicks a day.
686 gtk_calendar_signals[DAY_SELECTED_DOUBLE_CLICK_SIGNAL] =
687 g_signal_new (I_("day-selected-double-click"),
688 G_OBJECT_CLASS_TYPE (gobject_class),
690 G_STRUCT_OFFSET (GtkCalendarClass, day_selected_double_click),
692 _gtk_marshal_VOID__VOID,
696 * GtkCalendar::prev-month:
697 * @calendar: the object which received the signal.
699 * Emitted when the user switched to the previous month.
701 gtk_calendar_signals[PREV_MONTH_SIGNAL] =
702 g_signal_new (I_("prev-month"),
703 G_OBJECT_CLASS_TYPE (gobject_class),
705 G_STRUCT_OFFSET (GtkCalendarClass, prev_month),
707 _gtk_marshal_VOID__VOID,
711 * GtkCalendar::next-month:
712 * @calendar: the object which received the signal.
714 * Emitted when the user switched to the next month.
716 gtk_calendar_signals[NEXT_MONTH_SIGNAL] =
717 g_signal_new (I_("next-month"),
718 G_OBJECT_CLASS_TYPE (gobject_class),
720 G_STRUCT_OFFSET (GtkCalendarClass, next_month),
722 _gtk_marshal_VOID__VOID,
726 * GtkCalendar::prev-year:
727 * @calendar: the object which received the signal.
729 * Emitted when user switched to the previous year.
731 gtk_calendar_signals[PREV_YEAR_SIGNAL] =
732 g_signal_new (I_("prev-year"),
733 G_OBJECT_CLASS_TYPE (gobject_class),
735 G_STRUCT_OFFSET (GtkCalendarClass, prev_year),
737 _gtk_marshal_VOID__VOID,
741 * GtkCalendar::next-year:
742 * @calendar: the object which received the signal.
744 * Emitted when user switched to the next year.
746 gtk_calendar_signals[NEXT_YEAR_SIGNAL] =
747 g_signal_new (I_("next-year"),
748 G_OBJECT_CLASS_TYPE (gobject_class),
750 G_STRUCT_OFFSET (GtkCalendarClass, next_year),
752 _gtk_marshal_VOID__VOID,
755 g_type_class_add_private (gobject_class, sizeof (GtkCalendarPrivate));
759 gtk_calendar_init (GtkCalendar *calendar)
761 GtkWidget *widget = GTK_WIDGET (calendar);
766 wchar_t wbuffer[100];
771 GtkCalendarPrivate *priv;
773 #ifdef HAVE__NL_TIME_FIRST_WEEKDAY
774 union { unsigned int word; char *string; } langinfo;
775 gint week_1stday = 0;
776 gint first_weekday = 1;
782 priv = calendar->priv = G_TYPE_INSTANCE_GET_PRIVATE (calendar,
786 gtk_widget_set_can_focus (widget, TRUE);
787 gtk_widget_set_has_window (widget, FALSE);
789 if (!default_abbreviated_dayname[0])
793 tmp_time= (i+3)*86400;
794 strftime ( buffer, sizeof (buffer), "%a", gmtime (&tmp_time));
795 default_abbreviated_dayname[i] = g_locale_to_utf8 (buffer, -1, NULL, NULL, NULL);
797 if (!GetLocaleInfoW (GetThreadLocale (), LOCALE_SABBREVDAYNAME1 + (i+6)%7,
798 wbuffer, G_N_ELEMENTS (wbuffer)))
799 default_abbreviated_dayname[i] = g_strdup_printf ("(%d)", i);
801 default_abbreviated_dayname[i] = g_utf16_to_utf8 (wbuffer, -1, NULL, NULL, NULL);
805 if (!default_monthname[0])
810 strftime ( buffer, sizeof (buffer), "%B", gmtime (&tmp_time));
811 default_monthname[i] = g_locale_to_utf8 (buffer, -1, NULL, NULL, NULL);
813 if (!GetLocaleInfoW (GetThreadLocale (), LOCALE_SMONTHNAME1 + i,
814 wbuffer, G_N_ELEMENTS (wbuffer)))
815 default_monthname[i] = g_strdup_printf ("(%d)", i);
817 default_monthname[i] = g_utf16_to_utf8 (wbuffer, -1, NULL, NULL, NULL);
823 tm = localtime (&secs);
824 priv->month = tm->tm_mon;
825 priv->year = 1900 + tm->tm_year;
828 priv->marked_date[i] = FALSE;
829 priv->num_marked_dates = 0;
830 priv->selected_day = tm->tm_mday;
832 priv->display_flags = (GTK_CALENDAR_SHOW_HEADING |
833 GTK_CALENDAR_SHOW_DAY_NAMES |
834 GTK_CALENDAR_SHOW_DETAILS);
836 priv->focus_row = -1;
837 priv->focus_col = -1;
839 priv->max_year_width = 0;
840 priv->max_month_width = 0;
841 priv->max_day_char_width = 0;
842 priv->max_week_char_width = 0;
844 priv->max_day_char_ascent = 0;
845 priv->max_day_char_descent = 0;
846 priv->max_label_char_ascent = 0;
847 priv->max_label_char_descent = 0;
849 priv->arrow_width = 10;
851 priv->need_timer = 0;
853 priv->click_child = -1;
856 priv->drag_highlight = 0;
858 gtk_drag_dest_set (widget, 0, NULL, 0, GDK_ACTION_COPY);
859 gtk_drag_dest_add_text_targets (widget);
861 priv->year_before = 0;
863 /* Translate to calendar:YM if you want years to be displayed
864 * before months; otherwise translate to calendar:MY.
865 * Do *not* translate it to anything else, if it
866 * it isn't calendar:YM or calendar:MY it will not work.
868 * Note that the ordering described here is logical order, which is
869 * further influenced by BIDI ordering. Thus, if you have a default
870 * text direction of RTL and specify "calendar:YM", then the year
871 * will appear to the right of the month.
873 year_before = _("calendar:MY");
874 if (strcmp (year_before, "calendar:YM") == 0)
875 priv->year_before = 1;
876 else if (strcmp (year_before, "calendar:MY") != 0)
877 g_warning ("Whoever translated calendar:MY did so wrongly.\n");
880 priv->week_start = 0;
883 if (GetLocaleInfoW (GetThreadLocale (), LOCALE_IFIRSTDAYOFWEEK,
884 wbuffer, G_N_ELEMENTS (wbuffer)))
885 week_start = g_utf16_to_utf8 (wbuffer, -1, NULL, NULL, NULL);
887 if (week_start != NULL)
889 priv->week_start = (week_start[0] - '0' + 1) % 7;
893 #ifdef HAVE__NL_TIME_FIRST_WEEKDAY
894 langinfo.string = nl_langinfo (_NL_TIME_FIRST_WEEKDAY);
895 first_weekday = langinfo.string[0];
896 langinfo.string = nl_langinfo (_NL_TIME_WEEK_1STDAY);
897 week_origin = langinfo.word;
898 if (week_origin == 19971130) /* Sunday */
900 else if (week_origin == 19971201) /* Monday */
903 g_warning ("Unknown value of _NL_TIME_WEEK_1STDAY.\n");
905 priv->week_start = (week_1stday + first_weekday - 1) % 7;
907 /* Translate to calendar:week_start:0 if you want Sunday to be the
908 * first day of the week to calendar:week_start:1 if you want Monday
909 * to be the first day of the week, and so on.
911 week_start = _("calendar:week_start:0");
913 if (strncmp (week_start, "calendar:week_start:", 20) == 0)
914 priv->week_start = *(week_start + 20) - '0';
916 priv->week_start = -1;
918 if (priv->week_start < 0 || priv->week_start > 6)
920 g_warning ("Whoever translated calendar:week_start:0 did so wrongly.\n");
921 priv->week_start = 0;
926 calendar_compute_days (calendar);
930 /****************************************
931 * Utility Functions *
932 ****************************************/
935 calendar_queue_refresh (GtkCalendar *calendar)
937 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
939 if (!(priv->detail_func) ||
940 !(priv->display_flags & GTK_CALENDAR_SHOW_DETAILS) ||
941 (priv->detail_width_chars && priv->detail_height_rows))
942 gtk_widget_queue_draw (GTK_WIDGET (calendar));
944 gtk_widget_queue_resize (GTK_WIDGET (calendar));
948 calendar_set_month_next (GtkCalendar *calendar)
951 GtkCalendarPrivate *priv = calendar->priv;
953 if (priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
956 if (priv->month == 11)
964 calendar_compute_days (calendar);
965 g_signal_emit (calendar,
966 gtk_calendar_signals[NEXT_MONTH_SIGNAL],
968 g_signal_emit (calendar,
969 gtk_calendar_signals[MONTH_CHANGED_SIGNAL],
972 month_len = month_length[leap (priv->year)][priv->month + 1];
974 if (month_len < priv->selected_day)
976 priv->selected_day = 0;
977 gtk_calendar_select_day (calendar, month_len);
980 gtk_calendar_select_day (calendar, priv->selected_day);
982 calendar_queue_refresh (calendar);
986 calendar_set_year_prev (GtkCalendar *calendar)
988 GtkCalendarPrivate *priv = calendar->priv;
992 calendar_compute_days (calendar);
993 g_signal_emit (calendar,
994 gtk_calendar_signals[PREV_YEAR_SIGNAL],
996 g_signal_emit (calendar,
997 gtk_calendar_signals[MONTH_CHANGED_SIGNAL],
1000 month_len = month_length[leap (priv->year)][priv->month + 1];
1002 if (month_len < priv->selected_day)
1004 priv->selected_day = 0;
1005 gtk_calendar_select_day (calendar, month_len);
1008 gtk_calendar_select_day (calendar, priv->selected_day);
1010 calendar_queue_refresh (calendar);
1014 calendar_set_year_next (GtkCalendar *calendar)
1016 GtkCalendarPrivate *priv = calendar->priv;
1020 calendar_compute_days (calendar);
1021 g_signal_emit (calendar,
1022 gtk_calendar_signals[NEXT_YEAR_SIGNAL],
1024 g_signal_emit (calendar,
1025 gtk_calendar_signals[MONTH_CHANGED_SIGNAL],
1028 month_len = month_length[leap (priv->year)][priv->month + 1];
1030 if (month_len < priv->selected_day)
1032 priv->selected_day = 0;
1033 gtk_calendar_select_day (calendar, month_len);
1036 gtk_calendar_select_day (calendar, priv->selected_day);
1038 calendar_queue_refresh (calendar);
1042 calendar_compute_days (GtkCalendar *calendar)
1044 GtkCalendarPrivate *priv = calendar->priv;
1047 gint ndays_in_month;
1048 gint ndays_in_prev_month;
1055 month = priv->month + 1;
1057 ndays_in_month = month_length[leap (year)][month];
1059 first_day = day_of_week (year, month, 1);
1060 first_day = (first_day + 7 - priv->week_start) % 7;
1062 /* Compute days of previous month */
1064 ndays_in_prev_month = month_length[leap (year)][month-1];
1066 ndays_in_prev_month = month_length[leap (year)][12];
1067 day = ndays_in_prev_month - first_day + 1;
1072 for (col = 0; col < first_day; col++)
1074 priv->day[row][col] = day;
1075 priv->day_month[row][col] = MONTH_PREV;
1080 /* Compute days of current month */
1082 for (day = 1; day <= ndays_in_month; day++)
1084 priv->day[row][col] = day;
1085 priv->day_month[row][col] = MONTH_CURRENT;
1095 /* Compute days of next month */
1097 for (; row <= 5; row++)
1099 for (; col <= 6; col++)
1101 priv->day[row][col] = day;
1102 priv->day_month[row][col] = MONTH_NEXT;
1110 calendar_select_and_focus_day (GtkCalendar *calendar,
1113 GtkCalendarPrivate *priv = calendar->priv;
1114 gint old_focus_row = priv->focus_row;
1115 gint old_focus_col = priv->focus_col;
1119 for (row = 0; row < 6; row ++)
1120 for (col = 0; col < 7; col++)
1122 if (priv->day_month[row][col] == MONTH_CURRENT
1123 && priv->day[row][col] == day)
1125 priv->focus_row = row;
1126 priv->focus_col = col;
1130 if (old_focus_row != -1 && old_focus_col != -1)
1131 calendar_invalidate_day (calendar, old_focus_row, old_focus_col);
1133 gtk_calendar_select_day (calendar, day);
1137 /****************************************
1138 * Layout computation utilities *
1139 ****************************************/
1142 calendar_row_height (GtkCalendar *calendar)
1144 GtkCalendarPrivate *priv = calendar->priv;
1146 return (GTK_CALENDAR_GET_PRIVATE (calendar)->main_h - CALENDAR_MARGIN
1147 - ((priv->display_flags & GTK_CALENDAR_SHOW_DAY_NAMES)
1148 ? calendar_get_ysep (calendar) : CALENDAR_MARGIN)) / 6;
1152 /* calendar_left_x_for_column: returns the x coordinate
1153 * for the left of the column */
1155 calendar_left_x_for_column (GtkCalendar *calendar,
1158 GtkCalendarPrivate *priv = calendar->priv;
1162 gint calendar_xsep = calendar_get_xsep (calendar);
1164 gint inner_border = calendar_get_inner_border (calendar);
1166 style = gtk_widget_get_style (GTK_WIDGET (calendar));
1168 week_width = priv->week_width + style->xthickness + inner_border;
1170 if (gtk_widget_get_direction (GTK_WIDGET (calendar)) == GTK_TEXT_DIR_RTL)
1172 column = 6 - column;
1176 width = GTK_CALENDAR_GET_PRIVATE (calendar)->day_width;
1177 if (priv->display_flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
1178 x_left = week_width + calendar_xsep + (width + DAY_XSEP) * column;
1180 x_left = week_width + CALENDAR_MARGIN + (width + DAY_XSEP) * column;
1185 /* column_from_x: returns the column 0-6 that the
1186 * x pixel of the xwindow is in */
1188 calendar_column_from_x (GtkCalendar *calendar,
1192 gint x_left, x_right;
1196 for (c = 0; c < 7; c++)
1198 x_left = calendar_left_x_for_column (calendar, c);
1199 x_right = x_left + GTK_CALENDAR_GET_PRIVATE (calendar)->day_width;
1201 if (event_x >= x_left && event_x < x_right)
1211 /* calendar_top_y_for_row: returns the y coordinate
1212 * for the top of the row */
1214 calendar_top_y_for_row (GtkCalendar *calendar,
1218 GtkAllocation allocation;
1219 gint inner_border = calendar_get_inner_border (calendar);
1221 gtk_widget_get_allocation (GTK_WIDGET (calendar), &allocation);
1222 style = gtk_widget_get_style (GTK_WIDGET (calendar));
1224 return allocation.height
1225 - style->ythickness - inner_border
1226 - (CALENDAR_MARGIN + (6 - row)
1227 * calendar_row_height (calendar));
1230 /* row_from_y: returns the row 0-5 that the
1231 * y pixel of the xwindow is in */
1233 calendar_row_from_y (GtkCalendar *calendar,
1238 gint y_top, y_bottom;
1240 height = calendar_row_height (calendar);
1243 for (r = 0; r < 6; r++)
1245 y_top = calendar_top_y_for_row (calendar, r);
1246 y_bottom = y_top + height;
1248 if (event_y >= y_top && event_y < y_bottom)
1259 calendar_arrow_rectangle (GtkCalendar *calendar,
1263 GtkWidget *widget = GTK_WIDGET (calendar);
1264 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1265 GtkAllocation allocation;
1269 gtk_widget_get_allocation (widget, &allocation);
1270 style = gtk_widget_get_style (widget);
1272 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
1273 year_left = priv->year_before;
1275 year_left = !priv->year_before;
1278 rect->width = priv->arrow_width;
1279 rect->height = priv->header_h - 7;
1283 case ARROW_MONTH_LEFT:
1285 rect->x = (allocation.width - 2 * style->xthickness
1286 - (3 + 2 * priv->arrow_width + priv->max_month_width));
1290 case ARROW_MONTH_RIGHT:
1292 rect->x = (allocation.width - 2 * style->xthickness
1293 - 3 - priv->arrow_width);
1295 rect->x = (priv->arrow_width + priv->max_month_width);
1297 case ARROW_YEAR_LEFT:
1301 rect->x = (allocation.width - 2 * style->xthickness
1302 - (3 + 2 * priv->arrow_width + priv->max_year_width));
1304 case ARROW_YEAR_RIGHT:
1306 rect->x = (priv->arrow_width + priv->max_year_width);
1308 rect->x = (allocation.width - 2 * style->xthickness
1309 - 3 - priv->arrow_width);
1313 rect->x += style->xthickness;
1314 rect->y += style->ythickness;
1318 calendar_day_rectangle (GtkCalendar *calendar,
1323 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1325 rect->x = calendar_left_x_for_column (calendar, col);
1326 rect->y = calendar_top_y_for_row (calendar, row);
1327 rect->height = calendar_row_height (calendar);
1328 rect->width = priv->day_width;
1332 calendar_set_month_prev (GtkCalendar *calendar)
1334 GtkCalendarPrivate *priv = calendar->priv;
1337 if (priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
1340 if (priv->month == 0)
1348 month_len = month_length[leap (priv->year)][priv->month + 1];
1350 calendar_compute_days (calendar);
1352 g_signal_emit (calendar,
1353 gtk_calendar_signals[PREV_MONTH_SIGNAL],
1355 g_signal_emit (calendar,
1356 gtk_calendar_signals[MONTH_CHANGED_SIGNAL],
1359 if (month_len < priv->selected_day)
1361 priv->selected_day = 0;
1362 gtk_calendar_select_day (calendar, month_len);
1366 if (priv->selected_day < 0)
1367 priv->selected_day = priv->selected_day + 1 + month_length[leap (priv->year)][priv->month + 1];
1368 gtk_calendar_select_day (calendar, priv->selected_day);
1371 calendar_queue_refresh (calendar);
1375 /****************************************
1376 * Basic object methods *
1377 ****************************************/
1380 gtk_calendar_finalize (GObject *object)
1382 G_OBJECT_CLASS (gtk_calendar_parent_class)->finalize (object);
1386 gtk_calendar_destroy (GtkWidget *widget)
1388 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
1390 calendar_stop_spinning (GTK_CALENDAR (widget));
1392 /* Call the destroy function for the extra display callback: */
1393 if (priv->detail_func_destroy && priv->detail_func_user_data)
1395 priv->detail_func_destroy (priv->detail_func_user_data);
1396 priv->detail_func_user_data = NULL;
1397 priv->detail_func_destroy = NULL;
1400 GTK_WIDGET_CLASS (gtk_calendar_parent_class)->destroy (widget);
1405 calendar_set_display_option (GtkCalendar *calendar,
1406 GtkCalendarDisplayOptions flag,
1409 GtkCalendarPrivate *priv = calendar->priv;
1410 GtkCalendarDisplayOptions flags;
1413 flags = priv->display_flags | flag;
1415 flags = priv->display_flags & ~flag;
1416 gtk_calendar_set_display_options (calendar, flags);
1420 calendar_get_display_option (GtkCalendar *calendar,
1421 GtkCalendarDisplayOptions flag)
1423 GtkCalendarPrivate *priv = calendar->priv;
1425 return (priv->display_flags & flag) != 0;
1429 gtk_calendar_set_property (GObject *object,
1431 const GValue *value,
1434 GtkCalendar *calendar = GTK_CALENDAR (object);
1435 GtkCalendarPrivate *priv = calendar->priv;
1440 gtk_calendar_select_month (calendar,
1442 g_value_get_int (value));
1445 gtk_calendar_select_month (calendar,
1446 g_value_get_int (value),
1450 gtk_calendar_select_day (calendar,
1451 g_value_get_int (value));
1453 case PROP_SHOW_HEADING:
1454 calendar_set_display_option (calendar,
1455 GTK_CALENDAR_SHOW_HEADING,
1456 g_value_get_boolean (value));
1458 case PROP_SHOW_DAY_NAMES:
1459 calendar_set_display_option (calendar,
1460 GTK_CALENDAR_SHOW_DAY_NAMES,
1461 g_value_get_boolean (value));
1463 case PROP_NO_MONTH_CHANGE:
1464 calendar_set_display_option (calendar,
1465 GTK_CALENDAR_NO_MONTH_CHANGE,
1466 g_value_get_boolean (value));
1468 case PROP_SHOW_WEEK_NUMBERS:
1469 calendar_set_display_option (calendar,
1470 GTK_CALENDAR_SHOW_WEEK_NUMBERS,
1471 g_value_get_boolean (value));
1473 case PROP_SHOW_DETAILS:
1474 calendar_set_display_option (calendar,
1475 GTK_CALENDAR_SHOW_DETAILS,
1476 g_value_get_boolean (value));
1478 case PROP_DETAIL_WIDTH_CHARS:
1479 gtk_calendar_set_detail_width_chars (calendar,
1480 g_value_get_int (value));
1482 case PROP_DETAIL_HEIGHT_ROWS:
1483 gtk_calendar_set_detail_height_rows (calendar,
1484 g_value_get_int (value));
1487 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1493 gtk_calendar_get_property (GObject *object,
1498 GtkCalendar *calendar = GTK_CALENDAR (object);
1499 GtkCalendarPrivate *priv = calendar->priv;
1504 g_value_set_int (value, priv->year);
1507 g_value_set_int (value, priv->month);
1510 g_value_set_int (value, priv->selected_day);
1512 case PROP_SHOW_HEADING:
1513 g_value_set_boolean (value, calendar_get_display_option (calendar,
1514 GTK_CALENDAR_SHOW_HEADING));
1516 case PROP_SHOW_DAY_NAMES:
1517 g_value_set_boolean (value, calendar_get_display_option (calendar,
1518 GTK_CALENDAR_SHOW_DAY_NAMES));
1520 case PROP_NO_MONTH_CHANGE:
1521 g_value_set_boolean (value, calendar_get_display_option (calendar,
1522 GTK_CALENDAR_NO_MONTH_CHANGE));
1524 case PROP_SHOW_WEEK_NUMBERS:
1525 g_value_set_boolean (value, calendar_get_display_option (calendar,
1526 GTK_CALENDAR_SHOW_WEEK_NUMBERS));
1528 case PROP_SHOW_DETAILS:
1529 g_value_set_boolean (value, calendar_get_display_option (calendar,
1530 GTK_CALENDAR_SHOW_DETAILS));
1532 case PROP_DETAIL_WIDTH_CHARS:
1533 g_value_set_int (value, priv->detail_width_chars);
1535 case PROP_DETAIL_HEIGHT_ROWS:
1536 g_value_set_int (value, priv->detail_height_rows);
1539 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1545 /****************************************
1547 ****************************************/
1550 calendar_realize_arrows (GtkCalendar *calendar)
1552 GtkWidget *widget = GTK_WIDGET (calendar);
1553 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1554 GdkWindowAttr attributes;
1555 gint attributes_mask;
1557 GtkAllocation allocation;
1559 if (! (priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
1560 && (priv->display_flags & GTK_CALENDAR_SHOW_HEADING))
1562 gtk_widget_get_allocation (widget, &allocation);
1564 attributes.wclass = GDK_INPUT_ONLY;
1565 attributes.window_type = GDK_WINDOW_CHILD;
1566 attributes.event_mask = (gtk_widget_get_events (widget)
1567 | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
1568 | GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK);
1569 attributes_mask = GDK_WA_X | GDK_WA_Y;
1570 for (i = 0; i < 4; i++)
1573 calendar_arrow_rectangle (calendar, i, &rect);
1575 attributes.x = allocation.x + rect.x;
1576 attributes.y = allocation.y + rect.y;
1577 attributes.width = rect.width;
1578 attributes.height = rect.height;
1579 priv->arrow_win[i] = gdk_window_new (gtk_widget_get_window (widget),
1582 if (gtk_widget_is_sensitive (widget))
1583 priv->arrow_state[i] = GTK_STATE_NORMAL;
1585 priv->arrow_state[i] = GTK_STATE_INSENSITIVE;
1586 gdk_window_show (priv->arrow_win[i]);
1587 gdk_window_set_user_data (priv->arrow_win[i], widget);
1592 for (i = 0; i < 4; i++)
1593 priv->arrow_win[i] = NULL;
1598 calendar_unrealize_arrows (GtkCalendar *calendar)
1600 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1603 for (i = 0; i < 4; i++)
1605 if (priv->arrow_win[i])
1607 gdk_window_set_user_data (priv->arrow_win[i], NULL);
1608 gdk_window_destroy (priv->arrow_win[i]);
1609 priv->arrow_win[i] = NULL;
1616 calendar_get_inner_border (GtkCalendar *calendar)
1620 gtk_widget_style_get (GTK_WIDGET (calendar),
1621 "inner-border", &inner_border,
1624 return inner_border;
1628 calendar_get_xsep (GtkCalendar *calendar)
1632 gtk_widget_style_get (GTK_WIDGET (calendar),
1633 "horizontal-separation", &xsep,
1640 calendar_get_ysep (GtkCalendar *calendar)
1644 gtk_widget_style_get (GTK_WIDGET (calendar),
1645 "vertical-separation", &ysep,
1652 gtk_calendar_realize (GtkWidget *widget)
1654 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
1655 GdkWindowAttr attributes;
1656 gint attributes_mask;
1657 gint inner_border = calendar_get_inner_border (GTK_CALENDAR (widget));
1659 GtkAllocation allocation;
1661 style = gtk_widget_get_style (widget);
1662 gtk_widget_get_allocation (widget, &allocation);
1664 GTK_WIDGET_CLASS (gtk_calendar_parent_class)->realize (widget);
1666 attributes.wclass = GDK_INPUT_ONLY;
1667 attributes.window_type = GDK_WINDOW_CHILD;
1668 attributes.event_mask = (gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK
1669 | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
1670 | GDK_POINTER_MOTION_MASK | GDK_LEAVE_NOTIFY_MASK);
1672 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
1673 attributes.x = priv->week_width + style->ythickness + inner_border;
1675 attributes.x = style->ythickness + inner_border;
1677 attributes.y = priv->header_h + priv->day_name_h + style->ythickness + inner_border;
1678 attributes.width = allocation.width - attributes.x - (style->xthickness + inner_border);
1680 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1681 attributes.width -= priv->week_width;
1683 attributes.height = priv->main_h;
1684 attributes_mask = GDK_WA_X | GDK_WA_Y;
1686 attributes.x += allocation.x;
1687 attributes.y += allocation.y;
1689 priv->main_win = gdk_window_new (gtk_widget_get_window (widget),
1690 &attributes, attributes_mask);
1691 gdk_window_show (priv->main_win);
1692 gdk_window_set_user_data (priv->main_win, widget);
1694 calendar_realize_arrows (GTK_CALENDAR (widget));
1698 gtk_calendar_unrealize (GtkWidget *widget)
1700 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
1702 calendar_unrealize_arrows (GTK_CALENDAR (widget));
1706 gdk_window_set_user_data (priv->main_win, NULL);
1707 gdk_window_destroy (priv->main_win);
1708 priv->main_win = NULL;
1711 GTK_WIDGET_CLASS (gtk_calendar_parent_class)->unrealize (widget);
1715 gtk_calendar_get_detail (GtkCalendar *calendar,
1719 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1722 if (priv->detail_func == NULL)
1726 month = priv->month + priv->day_month[row][column] - MONTH_CURRENT;
1733 else if (month > 11)
1739 return priv->detail_func (calendar,
1741 priv->day[row][column],
1742 priv->detail_func_user_data);
1746 gtk_calendar_query_tooltip (GtkWidget *widget,
1749 gboolean keyboard_mode,
1750 GtkTooltip *tooltip)
1752 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
1753 GtkCalendar *calendar = GTK_CALENDAR (widget);
1754 gchar *detail = NULL;
1755 GdkRectangle day_rect;
1758 col = calendar_column_from_x (calendar, x);
1759 row = calendar_row_from_y (calendar, y);
1761 if (col != -1 && row != -1 &&
1762 (0 != (priv->detail_overflow[row] & (1 << col)) ||
1763 0 == (priv->display_flags & GTK_CALENDAR_SHOW_DETAILS)))
1765 detail = gtk_calendar_get_detail (calendar, row, col);
1766 calendar_day_rectangle (calendar, row, col, &day_rect);
1771 gtk_tooltip_set_tip_area (tooltip, &day_rect);
1772 gtk_tooltip_set_markup (tooltip, detail);
1779 if (GTK_WIDGET_CLASS (gtk_calendar_parent_class)->query_tooltip)
1780 return GTK_WIDGET_CLASS (gtk_calendar_parent_class)->query_tooltip (widget, x, y, keyboard_mode, tooltip);
1786 /****************************************
1787 * Size Request and Allocate *
1788 ****************************************/
1791 gtk_calendar_size_request (GtkWidget *widget,
1792 GtkRequisition *requisition)
1794 GtkCalendar *calendar = GTK_CALENDAR (widget);
1795 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
1797 PangoLayout *layout;
1798 PangoRectangle logical_rect;
1802 gint calendar_margin = CALENDAR_MARGIN;
1803 gint header_width, main_width;
1804 gint max_header_height = 0;
1807 gint max_detail_height;
1808 gint inner_border = calendar_get_inner_border (calendar);
1809 gint calendar_ysep = calendar_get_ysep (calendar);
1810 gint calendar_xsep = calendar_get_xsep (calendar);
1812 gtk_widget_style_get (GTK_WIDGET (widget),
1813 "focus-line-width", &focus_width,
1814 "focus-padding", &focus_padding,
1817 layout = gtk_widget_create_pango_layout (widget, NULL);
1820 * Calculate the requisition width for the widget.
1825 if (priv->display_flags & GTK_CALENDAR_SHOW_HEADING)
1827 priv->max_month_width = 0;
1828 for (i = 0; i < 12; i++)
1830 pango_layout_set_text (layout, default_monthname[i], -1);
1831 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1832 priv->max_month_width = MAX (priv->max_month_width,
1833 logical_rect.width + 8);
1834 max_header_height = MAX (max_header_height, logical_rect.height);
1837 priv->max_year_width = 0;
1838 /* Translators: This is a text measurement template.
1839 * Translate it to the widest year text
1841 * If you don't understand this, leave it as "2000"
1843 pango_layout_set_text (layout, C_("year measurement template", "2000"), -1);
1844 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1845 priv->max_year_width = MAX (priv->max_year_width,
1846 logical_rect.width + 8);
1847 max_header_height = MAX (max_header_height, logical_rect.height);
1851 priv->max_month_width = 0;
1852 priv->max_year_width = 0;
1855 if (priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
1856 header_width = (priv->max_month_width
1857 + priv->max_year_width
1860 header_width = (priv->max_month_width
1861 + priv->max_year_width
1862 + 4 * priv->arrow_width + 3 * 3);
1864 /* Mainwindow labels width */
1866 priv->max_day_char_width = 0;
1867 priv->max_day_char_ascent = 0;
1868 priv->max_day_char_descent = 0;
1869 priv->min_day_width = 0;
1871 for (i = 0; i < 9; i++)
1874 g_snprintf (buffer, sizeof (buffer), C_("calendar:day:digits", "%d"), i * 11);
1875 pango_layout_set_text (layout, buffer, -1);
1876 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1877 priv->min_day_width = MAX (priv->min_day_width,
1878 logical_rect.width);
1880 priv->max_day_char_ascent = MAX (priv->max_day_char_ascent,
1881 PANGO_ASCENT (logical_rect));
1882 priv->max_day_char_descent = MAX (priv->max_day_char_descent,
1883 PANGO_DESCENT (logical_rect));
1886 priv->max_label_char_ascent = 0;
1887 priv->max_label_char_descent = 0;
1888 if (priv->display_flags & GTK_CALENDAR_SHOW_DAY_NAMES)
1889 for (i = 0; i < 7; i++)
1891 pango_layout_set_text (layout, default_abbreviated_dayname[i], -1);
1892 pango_layout_line_get_pixel_extents (pango_layout_get_lines_readonly (layout)->data, NULL, &logical_rect);
1894 priv->min_day_width = MAX (priv->min_day_width, logical_rect.width);
1895 priv->max_label_char_ascent = MAX (priv->max_label_char_ascent,
1896 PANGO_ASCENT (logical_rect));
1897 priv->max_label_char_descent = MAX (priv->max_label_char_descent,
1898 PANGO_DESCENT (logical_rect));
1901 priv->max_week_char_width = 0;
1902 if (priv->display_flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
1903 for (i = 0; i < 9; i++)
1906 g_snprintf (buffer, sizeof (buffer), C_("calendar:week:digits", "%d"), i * 11);
1907 pango_layout_set_text (layout, buffer, -1);
1908 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1909 priv->max_week_char_width = MAX (priv->max_week_char_width,
1910 logical_rect.width / 2);
1913 /* Calculate detail extents. Do this as late as possible since
1914 * pango_layout_set_markup is called which alters font settings. */
1915 max_detail_height = 0;
1917 if (priv->detail_func && (priv->display_flags & GTK_CALENDAR_SHOW_DETAILS))
1919 gchar *markup, *tail;
1921 if (priv->detail_width_chars || priv->detail_height_rows)
1923 gint rows = MAX (1, priv->detail_height_rows) - 1;
1924 gsize len = priv->detail_width_chars + rows + 16;
1926 markup = tail = g_alloca (len);
1928 memcpy (tail, "<small>", 7);
1931 memset (tail, 'm', priv->detail_width_chars);
1932 tail += priv->detail_width_chars;
1934 memset (tail, '\n', rows);
1937 memcpy (tail, "</small>", 9);
1940 g_assert (len == (tail - markup));
1942 pango_layout_set_markup (layout, markup, -1);
1943 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1945 if (priv->detail_width_chars)
1946 priv->min_day_width = MAX (priv->min_day_width, logical_rect.width);
1947 if (priv->detail_height_rows)
1948 max_detail_height = MAX (max_detail_height, logical_rect.height);
1951 if (!priv->detail_width_chars || !priv->detail_height_rows)
1952 for (r = 0; r < 6; r++)
1953 for (c = 0; c < 7; c++)
1955 gchar *detail = gtk_calendar_get_detail (calendar, r, c);
1959 markup = g_strconcat ("<small>", detail, "</small>", NULL);
1960 pango_layout_set_markup (layout, markup, -1);
1962 if (priv->detail_width_chars)
1964 pango_layout_set_wrap (layout, PANGO_WRAP_WORD_CHAR);
1965 pango_layout_set_width (layout, PANGO_SCALE * priv->min_day_width);
1968 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1970 if (!priv->detail_width_chars)
1971 priv->min_day_width = MAX (priv->min_day_width, logical_rect.width);
1972 if (!priv->detail_height_rows)
1973 max_detail_height = MAX (max_detail_height, logical_rect.height);
1981 /* We add one to max_day_char_width to be able to make the marked day "bold" */
1982 priv->max_day_char_width = priv->min_day_width / 2 + 1;
1984 main_width = (7 * (priv->min_day_width + (focus_padding + focus_width) * 2) + (DAY_XSEP * 6) + CALENDAR_MARGIN * 2
1985 + (priv->max_week_char_width
1986 ? priv->max_week_char_width * 2 + (focus_padding + focus_width) * 2 + calendar_xsep * 2
1989 style = gtk_widget_get_style (widget);
1991 requisition->width = MAX (header_width, main_width + inner_border * 2) + style->xthickness * 2;
1994 * Calculate the requisition height for the widget.
1997 if (priv->display_flags & GTK_CALENDAR_SHOW_HEADING)
1999 priv->header_h = (max_header_height + calendar_ysep * 2);
2006 if (priv->display_flags & GTK_CALENDAR_SHOW_DAY_NAMES)
2008 priv->day_name_h = (priv->max_label_char_ascent
2009 + priv->max_label_char_descent
2010 + 2 * (focus_padding + focus_width) + calendar_margin);
2011 calendar_margin = calendar_ysep;
2015 priv->day_name_h = 0;
2018 priv->main_h = (CALENDAR_MARGIN + calendar_margin
2019 + 6 * (priv->max_day_char_ascent
2020 + priv->max_day_char_descent
2022 + 2 * (focus_padding + focus_width))
2025 height = priv->header_h + priv->day_name_h + priv->main_h;
2027 requisition->height = height + (style->ythickness + inner_border) * 2;
2029 g_object_unref (layout);
2033 gtk_calendar_size_allocate (GtkWidget *widget,
2034 GtkAllocation *allocation)
2036 GtkCalendar *calendar = GTK_CALENDAR (widget);
2037 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
2039 gint xthickness, ythickness;
2041 gint inner_border = calendar_get_inner_border (calendar);
2042 gint calendar_xsep = calendar_get_xsep (calendar);
2044 style = gtk_widget_get_style (widget);
2045 xthickness = style->xthickness;
2046 ythickness = style->xthickness;
2048 gtk_widget_set_allocation (widget, allocation);
2050 if (priv->display_flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
2052 priv->day_width = (priv->min_day_width
2053 * ((allocation->width - (xthickness + inner_border) * 2
2054 - (CALENDAR_MARGIN * 2) - (DAY_XSEP * 6) - calendar_xsep * 2))
2055 / (7 * priv->min_day_width + priv->max_week_char_width * 2));
2056 priv->week_width = ((allocation->width - (xthickness + inner_border) * 2
2057 - (CALENDAR_MARGIN * 2) - (DAY_XSEP * 6) - calendar_xsep * 2 )
2058 - priv->day_width * 7 + CALENDAR_MARGIN + calendar_xsep);
2062 priv->day_width = (allocation->width
2063 - (xthickness + inner_border) * 2
2064 - (CALENDAR_MARGIN * 2)
2065 - (DAY_XSEP * 6))/7;
2066 priv->week_width = 0;
2069 if (gtk_widget_get_realized (widget))
2071 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
2072 gdk_window_move_resize (priv->main_win,
2073 priv->week_width + xthickness + inner_border,
2074 priv->header_h + priv->day_name_h
2075 + (style->ythickness + inner_border),
2076 allocation->width - priv->week_width
2077 - (xthickness + inner_border) * 2,
2080 gdk_window_move_resize (priv->main_win,
2081 xthickness + inner_border,
2082 priv->header_h + priv->day_name_h
2083 + style->ythickness + inner_border,
2084 allocation->width - priv->week_width
2085 - (xthickness + inner_border) * 2,
2088 for (i = 0 ; i < 4 ; i++)
2090 if (priv->arrow_win[i])
2093 calendar_arrow_rectangle (calendar, i, &rect);
2095 gdk_window_move_resize (priv->arrow_win[i],
2096 allocation->x + rect.x,
2097 allocation->y + rect.y,
2098 rect.width, rect.height);
2105 /****************************************
2107 ****************************************/
2110 calendar_paint_header (GtkCalendar *calendar, cairo_t *cr)
2112 GtkWidget *widget = GTK_WIDGET (calendar);
2113 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2114 GtkAllocation allocation;
2119 gint max_month_width;
2120 gint max_year_width;
2121 PangoLayout *layout;
2122 PangoRectangle logical_rect;
2128 style = gtk_widget_get_style (widget);
2131 cairo_translate (cr, style->xthickness, style->ythickness);
2133 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
2134 year_left = priv->year_before;
2136 year_left = !priv->year_before;
2138 gtk_widget_get_allocation (widget, &allocation);
2140 header_width = allocation.width - 2 * style->xthickness;
2142 max_month_width = priv->max_month_width;
2143 max_year_width = priv->max_year_width;
2145 gdk_cairo_set_source_color (cr, HEADER_BG_COLOR (widget));
2146 cairo_rectangle (cr, 0, 0, header_width, priv->header_h);
2149 gtk_paint_shadow (style, cr,
2150 GTK_STATE_NORMAL, GTK_SHADOW_OUT,
2152 0, 0, header_width, priv->header_h);
2154 tmp_time = 1; /* Jan 1 1970, 00:00:01 UTC */
2155 tm = gmtime (&tmp_time);
2156 tm->tm_year = priv->year - 1900;
2158 /* Translators: This dictates how the year is displayed in
2159 * gtkcalendar widget. See strftime() manual for the format.
2160 * Use only ASCII in the translation.
2162 * Also look for the msgid "2000".
2163 * Translate that entry to a year with the widest output of this
2166 * "%Y" is appropriate for most locales.
2168 strftime (buffer, sizeof (buffer), C_("calendar year format", "%Y"), tm);
2169 str = g_locale_to_utf8 (buffer, -1, NULL, NULL, NULL);
2170 layout = gtk_widget_create_pango_layout (widget, str);
2173 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2176 y = (priv->header_h - logical_rect.height) / 2;
2178 /* Draw year and its arrows */
2180 if (priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
2182 x = 3 + (max_year_width - logical_rect.width)/2;
2184 x = header_width - (3 + max_year_width
2185 - (max_year_width - logical_rect.width)/2);
2188 x = 3 + priv->arrow_width + (max_year_width - logical_rect.width)/2;
2190 x = header_width - (3 + priv->arrow_width + max_year_width
2191 - (max_year_width - logical_rect.width)/2);
2194 gdk_cairo_set_source_color (cr, HEADER_FG_COLOR (GTK_WIDGET (calendar)));
2195 cairo_move_to (cr, x, y);
2196 pango_cairo_show_layout (cr, layout);
2199 g_snprintf (buffer, sizeof (buffer), "%s", default_monthname[priv->month]);
2200 pango_layout_set_text (layout, buffer, -1);
2201 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2203 if (priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
2205 x = header_width - (3 + max_month_width
2206 - (max_month_width - logical_rect.width)/2);
2208 x = 3 + (max_month_width - logical_rect.width) / 2;
2211 x = header_width - (3 + priv->arrow_width + max_month_width
2212 - (max_month_width - logical_rect.width)/2);
2214 x = 3 + priv->arrow_width + (max_month_width - logical_rect.width)/2;
2216 cairo_move_to (cr, x, y);
2217 pango_cairo_show_layout (cr, layout);
2219 g_object_unref (layout);
2225 calendar_paint_day_names (GtkCalendar *calendar,
2228 GtkWidget *widget = GTK_WIDGET (calendar);
2229 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2231 GtkAllocation allocation;
2234 int day_width, cal_width;
2236 PangoLayout *layout;
2237 PangoRectangle logical_rect;
2240 gint calendar_ysep = calendar_get_ysep (calendar);
2241 gint calendar_xsep = calendar_get_xsep (calendar);
2242 gint inner_border = calendar_get_inner_border (calendar);
2244 style = gtk_widget_get_style (widget);
2248 cairo_translate (cr,
2249 style->xthickness + inner_border,
2250 priv->header_h + style->ythickness + inner_border);
2252 gtk_widget_style_get (GTK_WIDGET (widget),
2253 "focus-line-width", &focus_width,
2254 "focus-padding", &focus_padding,
2257 gtk_widget_get_allocation (widget, &allocation);
2259 day_width = priv->day_width;
2260 cal_width = allocation.width - (style->xthickness + inner_border) * 2;
2261 day_wid_sep = day_width + DAY_XSEP;
2264 * Draw rectangles as inverted background for the labels.
2267 gdk_cairo_set_source_color (cr, SELECTED_BG_COLOR (widget));
2268 cairo_rectangle (cr,
2269 CALENDAR_MARGIN, CALENDAR_MARGIN,
2270 cal_width - CALENDAR_MARGIN * 2,
2271 priv->day_name_h - CALENDAR_MARGIN);
2274 if (priv->display_flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
2276 cairo_rectangle (cr,
2278 priv->day_name_h - calendar_ysep,
2279 priv->week_width - calendar_ysep - CALENDAR_MARGIN,
2288 layout = gtk_widget_create_pango_layout (widget, NULL);
2290 gdk_cairo_set_source_color (cr, SELECTED_FG_COLOR (widget));
2291 for (i = 0; i < 7; i++)
2293 if (gtk_widget_get_direction (GTK_WIDGET (calendar)) == GTK_TEXT_DIR_RTL)
2297 day = (day + priv->week_start) % 7;
2298 g_snprintf (buffer, sizeof (buffer), "%s", default_abbreviated_dayname[day]);
2300 pango_layout_set_text (layout, buffer, -1);
2301 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2305 + (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR ?
2306 (priv->week_width + (priv->week_width ? calendar_xsep : 0))
2309 + (day_width - logical_rect.width)/2),
2310 CALENDAR_MARGIN + focus_width + focus_padding + logical_rect.y);
2311 pango_cairo_show_layout (cr, layout);
2314 g_object_unref (layout);
2320 calendar_paint_week_numbers (GtkCalendar *calendar,
2323 GtkWidget *widget = GTK_WIDGET (calendar);
2324 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2326 guint week = 0, year;
2327 gint row, x_loc, y_loc;
2330 PangoLayout *layout;
2331 PangoRectangle logical_rect;
2334 gint calendar_xsep = calendar_get_xsep (calendar);
2335 gint inner_border = calendar_get_inner_border (calendar);
2338 style = gtk_widget_get_style (widget);
2342 y = priv->header_h + priv->day_name_h + (style->ythickness + inner_border);
2343 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
2344 x = style->xthickness + inner_border;
2346 x = gtk_widget_get_allocated_width (widget) - priv->week_width - (style->xthickness + inner_border);
2348 gtk_widget_style_get (GTK_WIDGET (widget),
2349 "focus-line-width", &focus_width,
2350 "focus-padding", &focus_padding,
2354 * Draw a rectangle as inverted background for the labels.
2357 gdk_cairo_set_source_color (cr, SELECTED_BG_COLOR (widget));
2358 if (priv->display_flags & GTK_CALENDAR_SHOW_DAY_NAMES)
2359 cairo_rectangle (cr,
2360 x + CALENDAR_MARGIN,
2362 priv->week_width - CALENDAR_MARGIN,
2363 priv->main_h - CALENDAR_MARGIN);
2365 cairo_rectangle (cr,
2366 x + CALENDAR_MARGIN,
2367 y + CALENDAR_MARGIN,
2368 priv->week_width - CALENDAR_MARGIN,
2369 priv->main_h - 2 * CALENDAR_MARGIN);
2376 layout = gtk_widget_create_pango_layout (widget, NULL);
2378 gdk_cairo_set_source_color (cr, SELECTED_FG_COLOR (widget));
2379 day_height = calendar_row_height (calendar);
2380 for (row = 0; row < 6; row++)
2385 if (priv->day[row][6] < 15 && row > 3 && priv->month == 11)
2388 result = week_of_year (&week, &year,
2389 ((priv->day[row][6] < 15 && row > 3 ? 1 : 0)
2390 + priv->month) % 12 + 1, priv->day[row][6]);
2391 g_return_if_fail (result);
2393 /* Translators: this defines whether the week numbers should use
2394 * localized digits or the ones used in English (0123...).
2396 * Translate to "%Id" if you want to use localized digits, or
2397 * translate to "%d" otherwise.
2399 * Note that translating this doesn't guarantee that you get localized
2400 * digits. That needs support from your system and locale definition
2403 g_snprintf (buffer, sizeof (buffer), C_("calendar:week:digits", "%d"), week);
2404 pango_layout_set_text (layout, buffer, -1);
2405 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2407 y_loc = calendar_top_y_for_row (calendar, row) + (day_height - logical_rect.height) / 2;
2409 x_loc = x + (priv->week_width
2410 - logical_rect.width
2411 - calendar_xsep - focus_padding - focus_width);
2413 cairo_move_to (cr, x_loc, y_loc);
2414 pango_cairo_show_layout (cr, layout);
2417 g_object_unref (layout);
2423 calendar_invalidate_day_num (GtkCalendar *calendar,
2426 GtkCalendarPrivate *priv = calendar->priv;
2427 gint r, c, row, col;
2431 for (r = 0; r < 6; r++)
2432 for (c = 0; c < 7; c++)
2433 if (priv->day_month[r][c] == MONTH_CURRENT &&
2434 priv->day[r][c] == day)
2440 g_return_if_fail (row != -1);
2441 g_return_if_fail (col != -1);
2443 calendar_invalidate_day (calendar, row, col);
2447 calendar_invalidate_day (GtkCalendar *calendar,
2451 GdkRectangle day_rect;
2452 GtkAllocation allocation;
2454 gtk_widget_get_allocation (GTK_WIDGET (calendar), &allocation);
2455 calendar_day_rectangle (calendar, row, col, &day_rect);
2456 gtk_widget_queue_draw_area (GTK_WIDGET (calendar),
2457 allocation.x + day_rect.x,
2458 allocation.y + day_rect.y,
2459 day_rect.width, day_rect.height);
2463 is_color_attribute (PangoAttribute *attribute,
2466 return (attribute->klass->type == PANGO_ATTR_FOREGROUND ||
2467 attribute->klass->type == PANGO_ATTR_BACKGROUND);
2471 calendar_paint_day (GtkCalendar *calendar,
2476 GtkWidget *widget = GTK_WIDGET (calendar);
2477 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2479 GdkColor *text_color;
2484 GdkRectangle day_rect;
2486 PangoLayout *layout;
2487 PangoRectangle logical_rect;
2488 gboolean overflow = FALSE;
2489 gboolean show_details;
2491 g_return_if_fail (row < 6);
2492 g_return_if_fail (col < 7);
2494 style = gtk_widget_get_style (widget);
2496 day = priv->day[row][col];
2497 show_details = (priv->display_flags & GTK_CALENDAR_SHOW_DETAILS);
2499 calendar_day_rectangle (calendar, row, col, &day_rect);
2501 if (priv->day_month[row][col] == MONTH_PREV)
2503 text_color = PREV_MONTH_COLOR (widget);
2505 else if (priv->day_month[row][col] == MONTH_NEXT)
2507 text_color = NEXT_MONTH_COLOR (widget);
2511 if (priv->selected_day == day)
2513 gdk_cairo_set_source_color (cr, SELECTED_BG_COLOR (widget));
2514 gdk_cairo_rectangle (cr, &day_rect);
2517 if (priv->selected_day == day)
2518 text_color = SELECTED_FG_COLOR (widget);
2519 else if (priv->marked_date[day-1])
2520 text_color = MARKED_COLOR (widget);
2522 text_color = NORMAL_DAY_COLOR (widget);
2525 /* Translators: this defines whether the day numbers should use
2526 * localized digits or the ones used in English (0123...).
2528 * Translate to "%Id" if you want to use localized digits, or
2529 * translate to "%d" otherwise.
2531 * Note that translating this doesn't guarantee that you get localized
2532 * digits. That needs support from your system and locale definition
2535 g_snprintf (buffer, sizeof (buffer), C_("calendar:day:digits", "%d"), day);
2537 /* Get extra information to show, if any: */
2539 detail = gtk_calendar_get_detail (calendar, row, col);
2541 layout = gtk_widget_create_pango_layout (widget, buffer);
2542 pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
2543 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2545 x_loc = day_rect.x + (day_rect.width - logical_rect.width) / 2;
2548 gdk_cairo_set_source_color (cr, text_color);
2549 cairo_move_to (cr, x_loc, y_loc);
2550 pango_cairo_show_layout (cr, layout);
2552 if (priv->day_month[row][col] == MONTH_CURRENT &&
2553 (priv->marked_date[day-1] || (detail && !show_details)))
2555 cairo_move_to (cr, x_loc - 1, y_loc);
2556 pango_cairo_show_layout (cr, layout);
2559 y_loc += priv->max_day_char_descent;
2561 if (priv->detail_func && show_details)
2565 if (priv->selected_day == day)
2566 gdk_cairo_set_source_color (cr, &style->text[GTK_STATE_ACTIVE]);
2567 else if (priv->day_month[row][col] == MONTH_CURRENT)
2568 gdk_cairo_set_source_color (cr, &style->base[GTK_STATE_ACTIVE]);
2570 gdk_cairo_set_source_color (cr, &style->base[GTK_STATE_INSENSITIVE]);
2572 cairo_set_line_width (cr, 1);
2573 cairo_move_to (cr, day_rect.x + 2, y_loc + 0.5);
2574 cairo_line_to (cr, day_rect.x + day_rect.width - 2, y_loc + 0.5);
2582 if (detail && show_details)
2584 gchar *markup = g_strconcat ("<small>", detail, "</small>", NULL);
2585 pango_layout_set_markup (layout, markup, -1);
2588 if (day == priv->selected_day)
2590 /* Stripping colors as they conflict with selection marking. */
2592 PangoAttrList *attrs = pango_layout_get_attributes (layout);
2593 PangoAttrList *colors = NULL;
2596 colors = pango_attr_list_filter (attrs, is_color_attribute, NULL);
2598 pango_attr_list_unref (colors);
2601 pango_layout_set_wrap (layout, PANGO_WRAP_WORD_CHAR);
2602 pango_layout_set_width (layout, PANGO_SCALE * day_rect.width);
2604 if (priv->detail_height_rows)
2606 gint dy = day_rect.height - (y_loc - day_rect.y);
2607 pango_layout_set_height (layout, PANGO_SCALE * dy);
2608 pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_END);
2611 cairo_move_to (cr, day_rect.x, y_loc);
2612 pango_cairo_show_layout (cr, layout);
2615 if (gtk_widget_has_focus (widget)
2616 && priv->focus_row == row && priv->focus_col == col)
2620 if (priv->selected_day == day)
2621 state = gtk_widget_has_focus (widget) ? GTK_STATE_SELECTED : GTK_STATE_ACTIVE;
2623 state = GTK_STATE_NORMAL;
2625 gtk_paint_focus (style, cr,
2626 state, widget, "calendar-day",
2627 day_rect.x, day_rect.y,
2628 day_rect.width, day_rect.height);
2632 priv->detail_overflow[row] |= (1 << col);
2634 priv->detail_overflow[row] &= ~(1 << col);
2636 g_object_unref (layout);
2641 calendar_paint_main (GtkCalendar *calendar,
2648 for (col = 0; col < 7; col++)
2649 for (row = 0; row < 6; row++)
2650 calendar_paint_day (calendar, cr, row, col);
2656 calendar_invalidate_arrow (GtkCalendar *calendar,
2659 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2661 if (priv->display_flags & GTK_CALENDAR_SHOW_HEADING &&
2662 priv->arrow_win[arrow])
2665 GtkAllocation allocation;
2667 calendar_arrow_rectangle (calendar, arrow, &rect);
2668 gtk_widget_get_allocation (GTK_WIDGET (calendar), &allocation);
2669 gtk_widget_queue_draw_area (GTK_WIDGET (calendar),
2670 allocation.x + rect.x,
2671 allocation.y + rect.y,
2672 rect.width, rect.height);
2677 calendar_paint_arrow (GtkCalendar *calendar,
2681 GtkWidget *widget = GTK_WIDGET (calendar);
2682 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
2687 if (!priv->arrow_win[arrow])
2690 calendar_arrow_rectangle (calendar, arrow, &rect);
2694 style = gtk_widget_get_style (widget);
2695 state = priv->arrow_state[arrow];
2697 cairo_rectangle (cr, rect.x, rect.y, rect.width, rect.height);
2698 gdk_cairo_set_source_color (cr, &style->bg[state]);
2701 if (arrow == ARROW_MONTH_LEFT || arrow == ARROW_YEAR_LEFT)
2702 gtk_paint_arrow (style, cr, state,
2703 GTK_SHADOW_OUT, widget, "calendar",
2704 GTK_ARROW_LEFT, TRUE,
2705 rect.x + (rect.width - 8) / 2,
2706 rect.y + (rect.height - 8) / 2,
2709 gtk_paint_arrow (style, cr, state,
2710 GTK_SHADOW_OUT, widget, "calendar",
2711 GTK_ARROW_RIGHT, TRUE,
2712 rect.x + (rect.width - 8) / 2,
2713 rect.y + (rect.height - 8) / 2,
2720 gtk_calendar_draw (GtkWidget *widget,
2723 GtkCalendar *calendar = GTK_CALENDAR (widget);
2724 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
2727 if (gtk_cairo_should_draw_window (cr, gtk_widget_get_window (widget)))
2729 gdk_cairo_set_source_color (cr, BACKGROUND_COLOR (widget));
2730 cairo_rectangle (cr,
2732 gtk_widget_get_allocated_width (widget),
2733 gtk_widget_get_allocated_height (widget));
2735 gtk_paint_shadow (gtk_widget_get_style (widget), cr,
2736 gtk_widget_get_state (widget), GTK_SHADOW_IN,
2739 gtk_widget_get_allocated_width (widget),
2740 gtk_widget_get_allocated_height (widget));
2743 calendar_paint_main (calendar, cr);
2745 if (priv->display_flags & GTK_CALENDAR_SHOW_HEADING)
2747 calendar_paint_header (calendar, cr);
2748 for (i = 0; i < 4; i++)
2749 calendar_paint_arrow (calendar, cr, i);
2752 if (priv->display_flags & GTK_CALENDAR_SHOW_DAY_NAMES)
2753 calendar_paint_day_names (calendar, cr);
2755 if (priv->display_flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
2756 calendar_paint_week_numbers (calendar, cr);
2762 /****************************************
2764 ****************************************/
2767 calendar_arrow_action (GtkCalendar *calendar,
2772 case ARROW_YEAR_LEFT:
2773 calendar_set_year_prev (calendar);
2775 case ARROW_YEAR_RIGHT:
2776 calendar_set_year_next (calendar);
2778 case ARROW_MONTH_LEFT:
2779 calendar_set_month_prev (calendar);
2781 case ARROW_MONTH_RIGHT:
2782 calendar_set_month_next (calendar);
2790 calendar_timer (gpointer data)
2792 GtkCalendar *calendar = data;
2793 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2794 gboolean retval = FALSE;
2798 calendar_arrow_action (calendar, priv->click_child);
2800 if (priv->need_timer)
2802 GtkSettings *settings;
2805 settings = gtk_widget_get_settings (GTK_WIDGET (calendar));
2806 g_object_get (settings, "gtk-timeout-repeat", &timeout, NULL);
2808 priv->need_timer = FALSE;
2809 priv->timer = gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT_IDLE,
2810 timeout * SCROLL_DELAY_FACTOR,
2811 (GSourceFunc) calendar_timer,
2812 (gpointer) calendar, NULL);
2822 calendar_start_spinning (GtkCalendar *calendar,
2825 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2827 priv->click_child = click_child;
2831 GtkSettings *settings;
2834 settings = gtk_widget_get_settings (GTK_WIDGET (calendar));
2835 g_object_get (settings, "gtk-timeout-initial", &timeout, NULL);
2837 priv->need_timer = TRUE;
2838 priv->timer = gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT_IDLE,
2840 (GSourceFunc) calendar_timer,
2841 (gpointer) calendar, NULL);
2846 calendar_stop_spinning (GtkCalendar *calendar)
2848 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2852 g_source_remove (priv->timer);
2854 priv->need_timer = FALSE;
2859 calendar_main_button_press (GtkCalendar *calendar,
2860 GdkEventButton *event)
2862 GtkWidget *widget = GTK_WIDGET (calendar);
2863 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2869 GtkAllocation allocation;
2871 x = (gint) (event->x);
2872 y = (gint) (event->y);
2874 gdk_window_get_position (priv->main_win, &win_x, &win_y);
2875 gtk_widget_get_allocation (widget, &allocation);
2877 row = calendar_row_from_y (calendar, y + win_y - allocation.y);
2878 col = calendar_column_from_x (calendar, x + win_x - allocation.x);
2880 /* If row or column isn't found, just return. */
2881 if (row == -1 || col == -1)
2884 day_month = priv->day_month[row][col];
2886 if (event->type == GDK_BUTTON_PRESS)
2888 day = priv->day[row][col];
2890 if (day_month == MONTH_PREV)
2891 calendar_set_month_prev (calendar);
2892 else if (day_month == MONTH_NEXT)
2893 calendar_set_month_next (calendar);
2895 if (!gtk_widget_has_focus (widget))
2896 gtk_widget_grab_focus (widget);
2898 if (event->button == 1)
2901 priv->drag_start_x = x;
2902 priv->drag_start_y = y;
2905 calendar_select_and_focus_day (calendar, day);
2907 else if (event->type == GDK_2BUTTON_PRESS)
2910 if (day_month == MONTH_CURRENT)
2911 g_signal_emit (calendar,
2912 gtk_calendar_signals[DAY_SELECTED_DOUBLE_CLICK_SIGNAL],
2918 gtk_calendar_button_press (GtkWidget *widget,
2919 GdkEventButton *event)
2921 GtkCalendar *calendar = GTK_CALENDAR (widget);
2922 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
2925 if (event->window == priv->main_win)
2926 calendar_main_button_press (calendar, event);
2928 if (!gtk_widget_has_focus (widget))
2929 gtk_widget_grab_focus (widget);
2931 for (arrow = ARROW_YEAR_LEFT; arrow <= ARROW_MONTH_RIGHT; arrow++)
2933 if (event->window == priv->arrow_win[arrow])
2936 /* only call the action on single click, not double */
2937 if (event->type == GDK_BUTTON_PRESS)
2939 if (event->button == 1)
2940 calendar_start_spinning (calendar, arrow);
2942 calendar_arrow_action (calendar, arrow);
2953 gtk_calendar_button_release (GtkWidget *widget,
2954 GdkEventButton *event)
2956 GtkCalendar *calendar = GTK_CALENDAR (widget);
2957 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
2959 if (event->button == 1)
2961 calendar_stop_spinning (calendar);
2971 gtk_calendar_motion_notify (GtkWidget *widget,
2972 GdkEventMotion *event)
2974 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
2975 gint event_x, event_y;
2977 event_x = (gint) (event->x);
2978 event_y = (gint) (event->y);
2982 if (gtk_drag_check_threshold (widget,
2983 priv->drag_start_x, priv->drag_start_y,
2984 event->x, event->y))
2986 GdkDragContext *context;
2987 GtkTargetList *target_list = gtk_target_list_new (NULL, 0);
2988 gtk_target_list_add_text_targets (target_list, 0);
2989 context = gtk_drag_begin (widget, target_list, GDK_ACTION_COPY,
2990 1, (GdkEvent *)event);
2993 gtk_target_list_unref (target_list);
2994 gtk_drag_set_icon_default (context);
3002 gtk_calendar_enter_notify (GtkWidget *widget,
3003 GdkEventCrossing *event)
3005 GtkCalendar *calendar = GTK_CALENDAR (widget);
3006 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3008 if (event->window == priv->arrow_win[ARROW_MONTH_LEFT])
3010 priv->arrow_state[ARROW_MONTH_LEFT] = GTK_STATE_PRELIGHT;
3011 calendar_invalidate_arrow (calendar, ARROW_MONTH_LEFT);
3014 if (event->window == priv->arrow_win[ARROW_MONTH_RIGHT])
3016 priv->arrow_state[ARROW_MONTH_RIGHT] = GTK_STATE_PRELIGHT;
3017 calendar_invalidate_arrow (calendar, ARROW_MONTH_RIGHT);
3020 if (event->window == priv->arrow_win[ARROW_YEAR_LEFT])
3022 priv->arrow_state[ARROW_YEAR_LEFT] = GTK_STATE_PRELIGHT;
3023 calendar_invalidate_arrow (calendar, ARROW_YEAR_LEFT);
3026 if (event->window == priv->arrow_win[ARROW_YEAR_RIGHT])
3028 priv->arrow_state[ARROW_YEAR_RIGHT] = GTK_STATE_PRELIGHT;
3029 calendar_invalidate_arrow (calendar, ARROW_YEAR_RIGHT);
3036 gtk_calendar_leave_notify (GtkWidget *widget,
3037 GdkEventCrossing *event)
3039 GtkCalendar *calendar = GTK_CALENDAR (widget);
3040 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3042 if (event->window == priv->arrow_win[ARROW_MONTH_LEFT])
3044 priv->arrow_state[ARROW_MONTH_LEFT] = GTK_STATE_NORMAL;
3045 calendar_invalidate_arrow (calendar, ARROW_MONTH_LEFT);
3048 if (event->window == priv->arrow_win[ARROW_MONTH_RIGHT])
3050 priv->arrow_state[ARROW_MONTH_RIGHT] = GTK_STATE_NORMAL;
3051 calendar_invalidate_arrow (calendar, ARROW_MONTH_RIGHT);
3054 if (event->window == priv->arrow_win[ARROW_YEAR_LEFT])
3056 priv->arrow_state[ARROW_YEAR_LEFT] = GTK_STATE_NORMAL;
3057 calendar_invalidate_arrow (calendar, ARROW_YEAR_LEFT);
3060 if (event->window == priv->arrow_win[ARROW_YEAR_RIGHT])
3062 priv->arrow_state[ARROW_YEAR_RIGHT] = GTK_STATE_NORMAL;
3063 calendar_invalidate_arrow (calendar, ARROW_YEAR_RIGHT);
3070 gtk_calendar_scroll (GtkWidget *widget,
3071 GdkEventScroll *event)
3073 GtkCalendar *calendar = GTK_CALENDAR (widget);
3075 if (event->direction == GDK_SCROLL_UP)
3077 if (!gtk_widget_has_focus (widget))
3078 gtk_widget_grab_focus (widget);
3079 calendar_set_month_prev (calendar);
3081 else if (event->direction == GDK_SCROLL_DOWN)
3083 if (!gtk_widget_has_focus (widget))
3084 gtk_widget_grab_focus (widget);
3085 calendar_set_month_next (calendar);
3094 /****************************************
3096 ****************************************/
3099 move_focus (GtkCalendar *calendar,
3102 GtkCalendarPrivate *priv = calendar->priv;
3103 GtkTextDirection text_dir = gtk_widget_get_direction (GTK_WIDGET (calendar));
3105 if ((text_dir == GTK_TEXT_DIR_LTR && direction == -1) ||
3106 (text_dir == GTK_TEXT_DIR_RTL && direction == 1))
3108 if (priv->focus_col > 0)
3110 else if (priv->focus_row > 0)
3112 priv->focus_col = 6;
3116 if (priv->focus_col < 0)
3117 priv->focus_col = 6;
3118 if (priv->focus_row < 0)
3119 priv->focus_row = 5;
3123 if (priv->focus_col < 6)
3125 else if (priv->focus_row < 5)
3127 priv->focus_col = 0;
3131 if (priv->focus_col < 0)
3132 priv->focus_col = 0;
3133 if (priv->focus_row < 0)
3134 priv->focus_row = 0;
3139 gtk_calendar_key_press (GtkWidget *widget,
3142 GtkCalendar *calendar = GTK_CALENDAR (widget);
3143 GtkCalendarPrivate *priv = calendar->priv;
3151 old_focus_row = priv->focus_row;
3152 old_focus_col = priv->focus_col;
3154 switch (event->keyval)
3156 case GDK_KEY_KP_Left:
3159 if (event->state & GDK_CONTROL_MASK)
3160 calendar_set_month_prev (calendar);
3163 move_focus (calendar, -1);
3164 calendar_invalidate_day (calendar, old_focus_row, old_focus_col);
3165 calendar_invalidate_day (calendar, priv->focus_row,
3169 case GDK_KEY_KP_Right:
3172 if (event->state & GDK_CONTROL_MASK)
3173 calendar_set_month_next (calendar);
3176 move_focus (calendar, 1);
3177 calendar_invalidate_day (calendar, old_focus_row, old_focus_col);
3178 calendar_invalidate_day (calendar, priv->focus_row,
3185 if (event->state & GDK_CONTROL_MASK)
3186 calendar_set_year_prev (calendar);
3189 if (priv->focus_row > 0)
3191 if (priv->focus_row < 0)
3192 priv->focus_row = 5;
3193 if (priv->focus_col < 0)
3194 priv->focus_col = 6;
3195 calendar_invalidate_day (calendar, old_focus_row, old_focus_col);
3196 calendar_invalidate_day (calendar, priv->focus_row,
3200 case GDK_KEY_KP_Down:
3203 if (event->state & GDK_CONTROL_MASK)
3204 calendar_set_year_next (calendar);
3207 if (priv->focus_row < 5)
3209 if (priv->focus_col < 0)
3210 priv->focus_col = 0;
3211 calendar_invalidate_day (calendar, old_focus_row, old_focus_col);
3212 calendar_invalidate_day (calendar, priv->focus_row,
3216 case GDK_KEY_KP_Space:
3218 row = priv->focus_row;
3219 col = priv->focus_col;
3221 if (row > -1 && col > -1)
3225 day = priv->day[row][col];
3226 if (priv->day_month[row][col] == MONTH_PREV)
3227 calendar_set_month_prev (calendar);
3228 else if (priv->day_month[row][col] == MONTH_NEXT)
3229 calendar_set_month_next (calendar);
3231 calendar_select_and_focus_day (calendar, day);
3239 /****************************************
3240 * Misc widget methods *
3241 ****************************************/
3244 gtk_calendar_state_changed (GtkWidget *widget,
3245 GtkStateType previous_state)
3247 GtkCalendar *calendar = GTK_CALENDAR (widget);
3248 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3251 if (!gtk_widget_is_sensitive (widget))
3254 calendar_stop_spinning (calendar);
3257 for (i = 0; i < 4; i++)
3258 if (gtk_widget_is_sensitive (widget))
3259 priv->arrow_state[i] = GTK_STATE_NORMAL;
3261 priv->arrow_state[i] = GTK_STATE_INSENSITIVE;
3265 gtk_calendar_grab_notify (GtkWidget *widget,
3266 gboolean was_grabbed)
3269 calendar_stop_spinning (GTK_CALENDAR (widget));
3273 gtk_calendar_focus_out (GtkWidget *widget,
3274 GdkEventFocus *event)
3276 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3277 GtkCalendar *calendar = GTK_CALENDAR (widget);
3279 calendar_queue_refresh (calendar);
3280 calendar_stop_spinning (calendar);
3288 /****************************************
3290 ****************************************/
3293 gtk_calendar_drag_data_get (GtkWidget *widget,
3294 GdkDragContext *context,
3295 GtkSelectionData *selection_data,
3299 GtkCalendar *calendar = GTK_CALENDAR (widget);
3300 GtkCalendarPrivate *priv = calendar->priv;
3305 date = g_date_new_dmy (priv->selected_day, priv->month + 1, priv->year);
3306 len = g_date_strftime (str, 127, "%x", date);
3307 gtk_selection_data_set_text (selection_data, str, len);
3312 /* Get/set whether drag_motion requested the drag data and
3313 * drag_data_received should thus not actually insert the data,
3314 * since the data doesn't result from a drop.
3317 set_status_pending (GdkDragContext *context,
3318 GdkDragAction suggested_action)
3320 g_object_set_data (G_OBJECT (context),
3321 I_("gtk-calendar-status-pending"),
3322 GINT_TO_POINTER (suggested_action));
3325 static GdkDragAction
3326 get_status_pending (GdkDragContext *context)
3328 return GPOINTER_TO_INT (g_object_get_data (G_OBJECT (context),
3329 "gtk-calendar-status-pending"));
3333 gtk_calendar_drag_leave (GtkWidget *widget,
3334 GdkDragContext *context,
3337 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3339 priv->drag_highlight = 0;
3340 gtk_drag_unhighlight (widget);
3345 gtk_calendar_drag_motion (GtkWidget *widget,
3346 GdkDragContext *context,
3351 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3354 if (!priv->drag_highlight)
3356 priv->drag_highlight = 1;
3357 gtk_drag_highlight (widget);
3360 target = gtk_drag_dest_find_target (widget, context, NULL);
3361 if (target == GDK_NONE || context->suggested_action == 0)
3362 gdk_drag_status (context, 0, time);
3365 set_status_pending (context, context->suggested_action);
3366 gtk_drag_get_data (widget, context, target, time);
3373 gtk_calendar_drag_drop (GtkWidget *widget,
3374 GdkDragContext *context,
3381 target = gtk_drag_dest_find_target (widget, context, NULL);
3382 if (target != GDK_NONE)
3384 gtk_drag_get_data (widget, context,
3394 gtk_calendar_drag_data_received (GtkWidget *widget,
3395 GdkDragContext *context,
3398 GtkSelectionData *selection_data,
3402 GtkCalendar *calendar = GTK_CALENDAR (widget);
3403 GtkCalendarPrivate *priv = calendar->priv;
3404 guint day, month, year;
3407 GdkDragAction suggested_action;
3409 suggested_action = get_status_pending (context);
3411 if (suggested_action)
3413 set_status_pending (context, 0);
3415 /* We are getting this data due to a request in drag_motion,
3416 * rather than due to a request in drag_drop, so we are just
3417 * supposed to call drag_status, not actually paste in the
3420 str = (gchar*) gtk_selection_data_get_text (selection_data);
3424 date = g_date_new ();
3425 g_date_set_parse (date, str);
3426 if (!g_date_valid (date))
3427 suggested_action = 0;
3432 suggested_action = 0;
3434 gdk_drag_status (context, suggested_action, time);
3439 date = g_date_new ();
3440 str = (gchar*) gtk_selection_data_get_text (selection_data);
3443 g_date_set_parse (date, str);
3447 if (!g_date_valid (date))
3449 g_warning ("Received invalid date data\n");
3451 gtk_drag_finish (context, FALSE, FALSE, time);
3455 day = g_date_get_day (date);
3456 month = g_date_get_month (date);
3457 year = g_date_get_year (date);
3460 gtk_drag_finish (context, TRUE, FALSE, time);
3463 g_object_freeze_notify (G_OBJECT (calendar));
3464 if (!(priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
3465 && (priv->display_flags & GTK_CALENDAR_SHOW_HEADING))
3466 gtk_calendar_select_month (calendar, month - 1, year);
3467 gtk_calendar_select_day (calendar, day);
3468 g_object_thaw_notify (G_OBJECT (calendar));
3472 /****************************************
3474 ****************************************/
3479 * Creates a new calendar, with the current date being selected.
3481 * Return value: a newly #GtkCalendar widget
3484 gtk_calendar_new (void)
3486 return g_object_new (GTK_TYPE_CALENDAR, NULL);
3490 * gtk_calendar_get_display_options:
3491 * @calendar: a #GtkCalendar
3493 * Returns the current display options of @calendar.
3495 * Return value: the display options.
3499 GtkCalendarDisplayOptions
3500 gtk_calendar_get_display_options (GtkCalendar *calendar)
3502 g_return_val_if_fail (GTK_IS_CALENDAR (calendar), 0);
3504 return calendar->priv->display_flags;
3508 * gtk_calendar_set_display_options:
3509 * @calendar: a #GtkCalendar
3510 * @flags: the display options to set
3512 * Sets display options (whether to display the heading and the month
3518 gtk_calendar_set_display_options (GtkCalendar *calendar,
3519 GtkCalendarDisplayOptions flags)
3521 GtkWidget *widget = GTK_WIDGET (calendar);
3522 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
3524 GtkCalendarDisplayOptions old_flags;
3526 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3528 old_flags = priv->display_flags;
3530 if (gtk_widget_get_realized (widget))
3532 if ((flags ^ priv->display_flags) & GTK_CALENDAR_NO_MONTH_CHANGE)
3535 if (! (flags & GTK_CALENDAR_NO_MONTH_CHANGE)
3536 && (priv->display_flags & GTK_CALENDAR_SHOW_HEADING))
3538 priv->display_flags &= ~GTK_CALENDAR_NO_MONTH_CHANGE;
3539 calendar_realize_arrows (calendar);
3543 calendar_unrealize_arrows (calendar);
3547 if ((flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_HEADING)
3551 if (flags & GTK_CALENDAR_SHOW_HEADING)
3553 priv->display_flags |= GTK_CALENDAR_SHOW_HEADING;
3554 calendar_realize_arrows (calendar);
3558 calendar_unrealize_arrows (calendar);
3563 if ((flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_DAY_NAMES)
3567 if (flags & GTK_CALENDAR_SHOW_DAY_NAMES)
3568 priv->display_flags |= GTK_CALENDAR_SHOW_DAY_NAMES;
3571 if ((flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
3575 if (flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
3576 priv->display_flags |= GTK_CALENDAR_SHOW_WEEK_NUMBERS;
3579 if ((flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_DETAILS)
3582 priv->display_flags = flags;
3584 gtk_widget_queue_resize (GTK_WIDGET (calendar));
3587 priv->display_flags = flags;
3589 g_object_freeze_notify (G_OBJECT (calendar));
3590 if ((old_flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_HEADING)
3591 g_object_notify (G_OBJECT (calendar), "show-heading");
3592 if ((old_flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_DAY_NAMES)
3593 g_object_notify (G_OBJECT (calendar), "show-day-names");
3594 if ((old_flags ^ priv->display_flags) & GTK_CALENDAR_NO_MONTH_CHANGE)
3595 g_object_notify (G_OBJECT (calendar), "no-month-change");
3596 if ((old_flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
3597 g_object_notify (G_OBJECT (calendar), "show-week-numbers");
3598 g_object_thaw_notify (G_OBJECT (calendar));
3602 * gtk_calendar_select_month:
3603 * @calendar: a #GtkCalendar
3604 * @month: a month number between 0 and 11.
3605 * @year: the year the month is in.
3607 * Shifts the calendar to a different month.
3610 gtk_calendar_select_month (GtkCalendar *calendar,
3614 GtkCalendarPrivate *priv;
3616 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3617 g_return_if_fail (month <= 11);
3619 priv = calendar->priv;
3621 priv->month = month;
3624 calendar_compute_days (calendar);
3625 calendar_queue_refresh (calendar);
3627 g_object_freeze_notify (G_OBJECT (calendar));
3628 g_object_notify (G_OBJECT (calendar), "month");
3629 g_object_notify (G_OBJECT (calendar), "year");
3630 g_object_thaw_notify (G_OBJECT (calendar));
3632 g_signal_emit (calendar,
3633 gtk_calendar_signals[MONTH_CHANGED_SIGNAL],
3638 * gtk_calendar_select_day:
3639 * @calendar: a #GtkCalendar.
3640 * @day: the day number between 1 and 31, or 0 to unselect
3641 * the currently selected day.
3643 * Selects a day from the current month.
3646 gtk_calendar_select_day (GtkCalendar *calendar,
3649 GtkCalendarPrivate *priv;
3651 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3652 g_return_if_fail (day <= 31);
3654 priv = calendar->priv;
3656 /* Deselect the old day */
3657 if (priv->selected_day > 0)
3661 selected_day = priv->selected_day;
3662 priv->selected_day = 0;
3663 if (gtk_widget_is_drawable (GTK_WIDGET (calendar)))
3664 calendar_invalidate_day_num (calendar, selected_day);
3667 priv->selected_day = day;
3669 /* Select the new day */
3672 if (gtk_widget_is_drawable (GTK_WIDGET (calendar)))
3673 calendar_invalidate_day_num (calendar, day);
3676 g_object_notify (G_OBJECT (calendar), "day");
3678 g_signal_emit (calendar,
3679 gtk_calendar_signals[DAY_SELECTED_SIGNAL],
3684 * gtk_calendar_clear_marks:
3685 * @calendar: a #GtkCalendar
3687 * Remove all visual markers.
3690 gtk_calendar_clear_marks (GtkCalendar *calendar)
3692 GtkCalendarPrivate *priv;
3695 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3697 priv = calendar->priv;
3699 for (day = 0; day < 31; day++)
3701 priv->marked_date[day] = FALSE;
3704 priv->num_marked_dates = 0;
3705 calendar_queue_refresh (calendar);
3709 * gtk_calendar_mark_day:
3710 * @calendar: a #GtkCalendar
3711 * @day: the day number to mark between 1 and 31.
3713 * Places a visual marker on a particular day.
3716 gtk_calendar_mark_day (GtkCalendar *calendar,
3719 GtkCalendarPrivate *priv;
3721 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3723 priv = calendar->priv;
3725 if (day >= 1 && day <= 31 && !priv->marked_date[day-1])
3727 priv->marked_date[day - 1] = TRUE;
3728 priv->num_marked_dates++;
3729 calendar_invalidate_day_num (calendar, day);
3734 * gtk_calendar_get_day_is_marked:
3735 * @calendar: a #GtkCalendar
3736 * @day: the day number between 1 and 31.
3738 * Returns if the @day of the @calendar is already marked.
3740 * Returns: whether the day is marked.
3745 gtk_calendar_get_day_is_marked (GtkCalendar *calendar,
3748 GtkCalendarPrivate *priv;
3750 g_return_val_if_fail (GTK_IS_CALENDAR (calendar), FALSE);
3752 priv = calendar->priv;
3754 if (day >= 1 && day <= 31)
3755 return priv->marked_date[day - 1];
3761 * gtk_calendar_unmark_day:
3762 * @calendar: a #GtkCalendar.
3763 * @day: the day number to unmark between 1 and 31.
3765 * Removes the visual marker from a particular day.
3768 gtk_calendar_unmark_day (GtkCalendar *calendar,
3771 GtkCalendarPrivate *priv;
3773 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3775 priv = calendar->priv;
3777 if (day >= 1 && day <= 31 && priv->marked_date[day-1])
3779 priv->marked_date[day - 1] = FALSE;
3780 priv->num_marked_dates--;
3781 calendar_invalidate_day_num (calendar, day);
3786 * gtk_calendar_get_date:
3787 * @calendar: a #GtkCalendar
3788 * @year: (allow-none): location to store the year number, or %NULL
3789 * @month: (allow-none): location to store the month number (between 0 and 11), or %NULL
3790 * @day: (allow-none): location to store the day number (between 1 and 31), or %NULL
3792 * Obtains the selected date from a #GtkCalendar.
3795 gtk_calendar_get_date (GtkCalendar *calendar,
3800 GtkCalendarPrivate *priv;
3802 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3804 priv = calendar->priv;
3810 *month = priv->month;
3813 *day = priv->selected_day;
3817 * gtk_calendar_set_detail_func:
3818 * @calendar: a #GtkCalendar.
3819 * @func: a function providing details for each day.
3820 * @data: data to pass to @func invokations.
3821 * @destroy: a function for releasing @data.
3823 * Installs a function which provides Pango markup with detail information
3824 * for each day. Examples for such details are holidays or appointments. That
3825 * information is shown below each day when #GtkCalendar:show-details is set.
3826 * A tooltip containing with full detail information is provided, if the entire
3827 * text should not fit into the details area, or if #GtkCalendar:show-details
3830 * The size of the details area can be restricted by setting the
3831 * #GtkCalendar:detail-width-chars and #GtkCalendar:detail-height-rows
3837 gtk_calendar_set_detail_func (GtkCalendar *calendar,
3838 GtkCalendarDetailFunc func,
3840 GDestroyNotify destroy)
3842 GtkCalendarPrivate *priv;
3844 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3846 priv = GTK_CALENDAR_GET_PRIVATE (calendar);
3848 if (priv->detail_func_destroy)
3849 priv->detail_func_destroy (priv->detail_func_user_data);
3851 priv->detail_func = func;
3852 priv->detail_func_user_data = data;
3853 priv->detail_func_destroy = destroy;
3855 gtk_widget_set_has_tooltip (GTK_WIDGET (calendar),
3856 NULL != priv->detail_func);
3857 gtk_widget_queue_resize (GTK_WIDGET (calendar));
3861 * gtk_calendar_set_detail_width_chars:
3862 * @calendar: a #GtkCalendar.
3863 * @chars: detail width in characters.
3865 * Updates the width of detail cells.
3866 * See #GtkCalendar:detail-width-chars.
3871 gtk_calendar_set_detail_width_chars (GtkCalendar *calendar,
3874 GtkCalendarPrivate *priv;
3876 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3878 priv = GTK_CALENDAR_GET_PRIVATE (calendar);
3880 if (chars != priv->detail_width_chars)
3882 priv->detail_width_chars = chars;
3883 g_object_notify (G_OBJECT (calendar), "detail-width-chars");
3884 gtk_widget_queue_resize_no_redraw (GTK_WIDGET (calendar));
3889 * gtk_calendar_set_detail_height_rows:
3890 * @calendar: a #GtkCalendar.
3891 * @rows: detail height in rows.
3893 * Updates the height of detail cells.
3894 * See #GtkCalendar:detail-height-rows.
3899 gtk_calendar_set_detail_height_rows (GtkCalendar *calendar,
3902 GtkCalendarPrivate *priv;
3904 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3906 priv = GTK_CALENDAR_GET_PRIVATE (calendar);
3908 if (rows != priv->detail_height_rows)
3910 priv->detail_height_rows = rows;
3911 g_object_notify (G_OBJECT (calendar), "detail-height-rows");
3912 gtk_widget_queue_resize_no_redraw (GTK_WIDGET (calendar));
3917 * gtk_calendar_get_detail_width_chars:
3918 * @calendar: a #GtkCalendar.
3920 * Queries the width of detail cells, in characters.
3921 * See #GtkCalendar:detail-width-chars.
3925 * Return value: The width of detail cells, in characters.
3928 gtk_calendar_get_detail_width_chars (GtkCalendar *calendar)
3930 g_return_val_if_fail (GTK_IS_CALENDAR (calendar), 0);
3931 return GTK_CALENDAR_GET_PRIVATE (calendar)->detail_width_chars;
3935 * gtk_calendar_get_detail_height_rows:
3936 * @calendar: a #GtkCalendar.
3938 * Queries the height of detail cells, in rows.
3939 * See #GtkCalendar:detail-width-chars.
3943 * Return value: The height of detail cells, in rows.
3946 gtk_calendar_get_detail_height_rows (GtkCalendar *calendar)
3948 g_return_val_if_fail (GTK_IS_CALENDAR (calendar), 0);
3949 return GTK_CALENDAR_GET_PRIVATE (calendar)->detail_height_rows;