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];
264 GdkWindow *header_win;
265 GdkWindow *day_name_win;
268 GdkWindow *arrow_win[4];
270 gchar grow_space [32];
276 gint day_month[6][7];
279 gint num_marked_dates;
280 gint marked_date[31];
292 guint arrow_state[4];
294 guint max_month_width;
295 guint max_year_width;
301 guint max_day_char_width;
302 guint max_day_char_ascent;
303 guint max_day_char_descent;
304 guint max_label_char_ascent;
305 guint max_label_char_descent;
306 guint max_week_char_width;
309 guint year_before : 1;
311 guint need_timer : 1;
314 guint drag_highlight : 1;
324 /* Optional callback, used to display extra information for each day. */
325 GtkCalendarDetailFunc detail_func;
326 gpointer detail_func_user_data;
327 GDestroyNotify detail_func_destroy;
329 /* Size requistion for details provided by the hook. */
330 gint detail_height_rows;
331 gint detail_width_chars;
332 gint detail_overflow[6];
335 #define GTK_CALENDAR_GET_PRIVATE(widget) (GTK_CALENDAR (widget)->priv)
337 static void gtk_calendar_finalize (GObject *calendar);
338 static void gtk_calendar_destroy (GtkObject *calendar);
339 static void gtk_calendar_set_property (GObject *object,
343 static void gtk_calendar_get_property (GObject *object,
348 static void gtk_calendar_realize (GtkWidget *widget);
349 static void gtk_calendar_unrealize (GtkWidget *widget);
350 static void gtk_calendar_size_request (GtkWidget *widget,
351 GtkRequisition *requisition);
352 static void gtk_calendar_size_allocate (GtkWidget *widget,
353 GtkAllocation *allocation);
354 static gboolean gtk_calendar_draw (GtkWidget *widget,
356 static gboolean gtk_calendar_button_press (GtkWidget *widget,
357 GdkEventButton *event);
358 static gboolean gtk_calendar_button_release (GtkWidget *widget,
359 GdkEventButton *event);
360 static gboolean gtk_calendar_motion_notify (GtkWidget *widget,
361 GdkEventMotion *event);
362 static gboolean gtk_calendar_enter_notify (GtkWidget *widget,
363 GdkEventCrossing *event);
364 static gboolean gtk_calendar_leave_notify (GtkWidget *widget,
365 GdkEventCrossing *event);
366 static gboolean gtk_calendar_scroll (GtkWidget *widget,
367 GdkEventScroll *event);
368 static gboolean gtk_calendar_key_press (GtkWidget *widget,
370 static gboolean gtk_calendar_focus_out (GtkWidget *widget,
371 GdkEventFocus *event);
372 static void gtk_calendar_grab_notify (GtkWidget *widget,
373 gboolean was_grabbed);
374 static void gtk_calendar_state_changed (GtkWidget *widget,
375 GtkStateType previous_state);
376 static void gtk_calendar_style_set (GtkWidget *widget,
377 GtkStyle *previous_style);
378 static gboolean gtk_calendar_query_tooltip (GtkWidget *widget,
381 gboolean keyboard_mode,
382 GtkTooltip *tooltip);
384 static void gtk_calendar_drag_data_get (GtkWidget *widget,
385 GdkDragContext *context,
386 GtkSelectionData *selection_data,
389 static void gtk_calendar_drag_data_received (GtkWidget *widget,
390 GdkDragContext *context,
393 GtkSelectionData *selection_data,
396 static gboolean gtk_calendar_drag_motion (GtkWidget *widget,
397 GdkDragContext *context,
401 static void gtk_calendar_drag_leave (GtkWidget *widget,
402 GdkDragContext *context,
404 static gboolean gtk_calendar_drag_drop (GtkWidget *widget,
405 GdkDragContext *context,
410 static void calendar_start_spinning (GtkCalendar *calendar,
412 static void calendar_stop_spinning (GtkCalendar *calendar);
414 static void calendar_invalidate_day (GtkCalendar *widget,
417 static void calendar_invalidate_day_num (GtkCalendar *widget,
419 static void calendar_invalidate_arrow (GtkCalendar *widget,
422 static void calendar_compute_days (GtkCalendar *calendar);
423 static gint calendar_get_xsep (GtkCalendar *calendar);
424 static gint calendar_get_ysep (GtkCalendar *calendar);
426 static char *default_abbreviated_dayname[7];
427 static char *default_monthname[12];
429 G_DEFINE_TYPE (GtkCalendar, gtk_calendar, GTK_TYPE_WIDGET)
432 gtk_calendar_class_init (GtkCalendarClass *class)
434 GObjectClass *gobject_class;
435 GtkObjectClass *object_class;
436 GtkWidgetClass *widget_class;
438 gobject_class = (GObjectClass*) class;
439 object_class = (GtkObjectClass*) class;
440 widget_class = (GtkWidgetClass*) class;
442 gobject_class->set_property = gtk_calendar_set_property;
443 gobject_class->get_property = gtk_calendar_get_property;
444 gobject_class->finalize = gtk_calendar_finalize;
446 object_class->destroy = gtk_calendar_destroy;
448 widget_class->realize = gtk_calendar_realize;
449 widget_class->unrealize = gtk_calendar_unrealize;
450 widget_class->draw = gtk_calendar_draw;
451 widget_class->size_request = gtk_calendar_size_request;
452 widget_class->size_allocate = gtk_calendar_size_allocate;
453 widget_class->button_press_event = gtk_calendar_button_press;
454 widget_class->button_release_event = gtk_calendar_button_release;
455 widget_class->motion_notify_event = gtk_calendar_motion_notify;
456 widget_class->enter_notify_event = gtk_calendar_enter_notify;
457 widget_class->leave_notify_event = gtk_calendar_leave_notify;
458 widget_class->key_press_event = gtk_calendar_key_press;
459 widget_class->scroll_event = gtk_calendar_scroll;
460 widget_class->style_set = gtk_calendar_style_set;
461 widget_class->state_changed = gtk_calendar_state_changed;
462 widget_class->grab_notify = gtk_calendar_grab_notify;
463 widget_class->focus_out_event = gtk_calendar_focus_out;
464 widget_class->query_tooltip = gtk_calendar_query_tooltip;
466 widget_class->drag_data_get = gtk_calendar_drag_data_get;
467 widget_class->drag_motion = gtk_calendar_drag_motion;
468 widget_class->drag_leave = gtk_calendar_drag_leave;
469 widget_class->drag_drop = gtk_calendar_drag_drop;
470 widget_class->drag_data_received = gtk_calendar_drag_data_received;
476 * This property gets initially set to the current year.
478 g_object_class_install_property (gobject_class,
480 g_param_spec_int ("year",
482 P_("The selected year"),
484 GTK_PARAM_READWRITE));
489 * The selected month (as a number between 0 and 11).
490 * This property gets initially set to the current month.
492 g_object_class_install_property (gobject_class,
494 g_param_spec_int ("month",
496 P_("The selected month (as a number between 0 and 11)"),
498 GTK_PARAM_READWRITE));
503 * The selected day (as a number between 1 and 31, or 0
504 * to unselect the currently selected day).
505 * This property gets initially set to the current day.
507 g_object_class_install_property (gobject_class,
509 g_param_spec_int ("day",
511 P_("The selected day (as a number between 1 and 31, or 0 to unselect the currently selected day)"),
513 GTK_PARAM_READWRITE));
516 * GtkCalendar:show-heading:
518 * Determines whether a heading is displayed.
522 g_object_class_install_property (gobject_class,
524 g_param_spec_boolean ("show-heading",
526 P_("If TRUE, a heading is displayed"),
528 GTK_PARAM_READWRITE));
531 * GtkCalendar:show-day-names:
533 * Determines whether day names are displayed.
537 g_object_class_install_property (gobject_class,
539 g_param_spec_boolean ("show-day-names",
540 P_("Show Day Names"),
541 P_("If TRUE, day names are displayed"),
543 GTK_PARAM_READWRITE));
545 * GtkCalendar:no-month-change:
547 * Determines whether the selected month can be changed.
551 g_object_class_install_property (gobject_class,
552 PROP_NO_MONTH_CHANGE,
553 g_param_spec_boolean ("no-month-change",
554 P_("No Month Change"),
555 P_("If TRUE, the selected month cannot be changed"),
557 GTK_PARAM_READWRITE));
560 * GtkCalendar:show-week-numbers:
562 * Determines whether week numbers are displayed.
566 g_object_class_install_property (gobject_class,
567 PROP_SHOW_WEEK_NUMBERS,
568 g_param_spec_boolean ("show-week-numbers",
569 P_("Show Week Numbers"),
570 P_("If TRUE, week numbers are displayed"),
572 GTK_PARAM_READWRITE));
575 * GtkCalendar:detail-width-chars:
577 * Width of a detail cell, in characters.
578 * A value of 0 allows any width. See gtk_calendar_set_detail_func().
582 g_object_class_install_property (gobject_class,
583 PROP_DETAIL_WIDTH_CHARS,
584 g_param_spec_int ("detail-width-chars",
586 P_("Details width in characters"),
588 GTK_PARAM_READWRITE));
591 * GtkCalendar:detail-height-rows:
593 * Height of a detail cell, in rows.
594 * A value of 0 allows any width. See gtk_calendar_set_detail_func().
598 g_object_class_install_property (gobject_class,
599 PROP_DETAIL_HEIGHT_ROWS,
600 g_param_spec_int ("detail-height-rows",
601 P_("Details Height"),
602 P_("Details height in rows"),
604 GTK_PARAM_READWRITE));
607 * GtkCalendar:show-details:
609 * Determines whether details are shown directly in the widget, or if they are
610 * available only as tooltip. When this property is set days with details are
615 g_object_class_install_property (gobject_class,
617 g_param_spec_boolean ("show-details",
619 P_("If TRUE, details are shown"),
621 GTK_PARAM_READWRITE));
625 * GtkCalendar:inner-border
627 * The spacing around the day/week headers and main area.
629 gtk_widget_class_install_style_property (widget_class,
630 g_param_spec_int ("inner-border",
632 P_("Inner border space"),
634 GTK_PARAM_READABLE));
637 * GtkCalndar:vertical-separation
639 * Separation between day headers and main area.
641 gtk_widget_class_install_style_property (widget_class,
642 g_param_spec_int ("vertical-separation",
643 P_("Vertical separation"),
644 P_("Space between day headers and main area"),
646 GTK_PARAM_READABLE));
649 * GtkCalendar:horizontal-separation
651 * Separation between week headers and main area.
653 gtk_widget_class_install_style_property (widget_class,
654 g_param_spec_int ("horizontal-separation",
655 P_("Horizontal separation"),
656 P_("Space between week headers and main area"),
658 GTK_PARAM_READABLE));
661 * GtkCalendar::month-changed:
662 * @calendar: the object which received the signal.
664 * Emitted when the user clicks a button to change the selected month on a
667 gtk_calendar_signals[MONTH_CHANGED_SIGNAL] =
668 g_signal_new (I_("month-changed"),
669 G_OBJECT_CLASS_TYPE (gobject_class),
671 G_STRUCT_OFFSET (GtkCalendarClass, month_changed),
673 _gtk_marshal_VOID__VOID,
677 * GtkCalendar::day-selected:
678 * @calendar: the object which received the signal.
680 * Emitted when the user selects a day.
682 gtk_calendar_signals[DAY_SELECTED_SIGNAL] =
683 g_signal_new (I_("day-selected"),
684 G_OBJECT_CLASS_TYPE (gobject_class),
686 G_STRUCT_OFFSET (GtkCalendarClass, day_selected),
688 _gtk_marshal_VOID__VOID,
692 * GtkCalendar::day-selected-double-click:
693 * @calendar: the object which received the signal.
695 * Emitted when the user double-clicks a day.
697 gtk_calendar_signals[DAY_SELECTED_DOUBLE_CLICK_SIGNAL] =
698 g_signal_new (I_("day-selected-double-click"),
699 G_OBJECT_CLASS_TYPE (gobject_class),
701 G_STRUCT_OFFSET (GtkCalendarClass, day_selected_double_click),
703 _gtk_marshal_VOID__VOID,
707 * GtkCalendar::prev-month:
708 * @calendar: the object which received the signal.
710 * Emitted when the user switched to the previous month.
712 gtk_calendar_signals[PREV_MONTH_SIGNAL] =
713 g_signal_new (I_("prev-month"),
714 G_OBJECT_CLASS_TYPE (gobject_class),
716 G_STRUCT_OFFSET (GtkCalendarClass, prev_month),
718 _gtk_marshal_VOID__VOID,
722 * GtkCalendar::next-month:
723 * @calendar: the object which received the signal.
725 * Emitted when the user switched to the next month.
727 gtk_calendar_signals[NEXT_MONTH_SIGNAL] =
728 g_signal_new (I_("next-month"),
729 G_OBJECT_CLASS_TYPE (gobject_class),
731 G_STRUCT_OFFSET (GtkCalendarClass, next_month),
733 _gtk_marshal_VOID__VOID,
737 * GtkCalendar::prev-year:
738 * @calendar: the object which received the signal.
740 * Emitted when user switched to the previous year.
742 gtk_calendar_signals[PREV_YEAR_SIGNAL] =
743 g_signal_new (I_("prev-year"),
744 G_OBJECT_CLASS_TYPE (gobject_class),
746 G_STRUCT_OFFSET (GtkCalendarClass, prev_year),
748 _gtk_marshal_VOID__VOID,
752 * GtkCalendar::next-year:
753 * @calendar: the object which received the signal.
755 * Emitted when user switched to the next year.
757 gtk_calendar_signals[NEXT_YEAR_SIGNAL] =
758 g_signal_new (I_("next-year"),
759 G_OBJECT_CLASS_TYPE (gobject_class),
761 G_STRUCT_OFFSET (GtkCalendarClass, next_year),
763 _gtk_marshal_VOID__VOID,
766 g_type_class_add_private (gobject_class, sizeof (GtkCalendarPrivate));
770 gtk_calendar_init (GtkCalendar *calendar)
772 GtkWidget *widget = GTK_WIDGET (calendar);
777 wchar_t wbuffer[100];
782 GtkCalendarPrivate *priv;
784 #ifdef HAVE__NL_TIME_FIRST_WEEKDAY
785 union { unsigned int word; char *string; } langinfo;
786 gint week_1stday = 0;
787 gint first_weekday = 1;
793 priv = calendar->priv = G_TYPE_INSTANCE_GET_PRIVATE (calendar,
797 gtk_widget_set_can_focus (widget, TRUE);
799 if (!default_abbreviated_dayname[0])
803 tmp_time= (i+3)*86400;
804 strftime ( buffer, sizeof (buffer), "%a", gmtime (&tmp_time));
805 default_abbreviated_dayname[i] = g_locale_to_utf8 (buffer, -1, NULL, NULL, NULL);
807 if (!GetLocaleInfoW (GetThreadLocale (), LOCALE_SABBREVDAYNAME1 + (i+6)%7,
808 wbuffer, G_N_ELEMENTS (wbuffer)))
809 default_abbreviated_dayname[i] = g_strdup_printf ("(%d)", i);
811 default_abbreviated_dayname[i] = g_utf16_to_utf8 (wbuffer, -1, NULL, NULL, NULL);
815 if (!default_monthname[0])
820 strftime ( buffer, sizeof (buffer), "%B", gmtime (&tmp_time));
821 default_monthname[i] = g_locale_to_utf8 (buffer, -1, NULL, NULL, NULL);
823 if (!GetLocaleInfoW (GetThreadLocale (), LOCALE_SMONTHNAME1 + i,
824 wbuffer, G_N_ELEMENTS (wbuffer)))
825 default_monthname[i] = g_strdup_printf ("(%d)", i);
827 default_monthname[i] = g_utf16_to_utf8 (wbuffer, -1, NULL, NULL, NULL);
833 tm = localtime (&secs);
834 priv->month = tm->tm_mon;
835 priv->year = 1900 + tm->tm_year;
838 priv->marked_date[i] = FALSE;
839 priv->num_marked_dates = 0;
840 priv->selected_day = tm->tm_mday;
842 priv->display_flags = (GTK_CALENDAR_SHOW_HEADING |
843 GTK_CALENDAR_SHOW_DAY_NAMES |
844 GTK_CALENDAR_SHOW_DETAILS);
846 priv->highlight_row = -1;
847 priv->highlight_col = -1;
849 priv->focus_row = -1;
850 priv->focus_col = -1;
852 priv->max_year_width = 0;
853 priv->max_month_width = 0;
854 priv->max_day_char_width = 0;
855 priv->max_week_char_width = 0;
857 priv->max_day_char_ascent = 0;
858 priv->max_day_char_descent = 0;
859 priv->max_label_char_ascent = 0;
860 priv->max_label_char_descent = 0;
862 priv->arrow_width = 10;
864 priv->need_timer = 0;
866 priv->click_child = -1;
869 priv->drag_highlight = 0;
871 gtk_drag_dest_set (widget, 0, NULL, 0, GDK_ACTION_COPY);
872 gtk_drag_dest_add_text_targets (widget);
874 priv->year_before = 0;
876 /* Translate to calendar:YM if you want years to be displayed
877 * before months; otherwise translate to calendar:MY.
878 * Do *not* translate it to anything else, if it
879 * it isn't calendar:YM or calendar:MY it will not work.
881 * Note that the ordering described here is logical order, which is
882 * further influenced by BIDI ordering. Thus, if you have a default
883 * text direction of RTL and specify "calendar:YM", then the year
884 * will appear to the right of the month.
886 year_before = _("calendar:MY");
887 if (strcmp (year_before, "calendar:YM") == 0)
888 priv->year_before = 1;
889 else if (strcmp (year_before, "calendar:MY") != 0)
890 g_warning ("Whoever translated calendar:MY did so wrongly.\n");
893 priv->week_start = 0;
896 if (GetLocaleInfoW (GetThreadLocale (), LOCALE_IFIRSTDAYOFWEEK,
897 wbuffer, G_N_ELEMENTS (wbuffer)))
898 week_start = g_utf16_to_utf8 (wbuffer, -1, NULL, NULL, NULL);
900 if (week_start != NULL)
902 priv->week_start = (week_start[0] - '0' + 1) % 7;
906 #ifdef HAVE__NL_TIME_FIRST_WEEKDAY
907 langinfo.string = nl_langinfo (_NL_TIME_FIRST_WEEKDAY);
908 first_weekday = langinfo.string[0];
909 langinfo.string = nl_langinfo (_NL_TIME_WEEK_1STDAY);
910 week_origin = langinfo.word;
911 if (week_origin == 19971130) /* Sunday */
913 else if (week_origin == 19971201) /* Monday */
916 g_warning ("Unknown value of _NL_TIME_WEEK_1STDAY.\n");
918 priv->week_start = (week_1stday + first_weekday - 1) % 7;
920 /* Translate to calendar:week_start:0 if you want Sunday to be the
921 * first day of the week to calendar:week_start:1 if you want Monday
922 * to be the first day of the week, and so on.
924 week_start = _("calendar:week_start:0");
926 if (strncmp (week_start, "calendar:week_start:", 20) == 0)
927 priv->week_start = *(week_start + 20) - '0';
929 priv->week_start = -1;
931 if (priv->week_start < 0 || priv->week_start > 6)
933 g_warning ("Whoever translated calendar:week_start:0 did so wrongly.\n");
934 priv->week_start = 0;
939 calendar_compute_days (calendar);
943 /****************************************
944 * Utility Functions *
945 ****************************************/
948 calendar_queue_refresh (GtkCalendar *calendar)
950 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
952 if (!(priv->detail_func) ||
953 !(priv->display_flags & GTK_CALENDAR_SHOW_DETAILS) ||
954 (priv->detail_width_chars && priv->detail_height_rows))
955 gtk_widget_queue_draw (GTK_WIDGET (calendar));
957 gtk_widget_queue_resize (GTK_WIDGET (calendar));
961 calendar_set_month_next (GtkCalendar *calendar)
964 GtkCalendarPrivate *priv = calendar->priv;
966 if (priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
969 if (priv->month == 11)
977 calendar_compute_days (calendar);
978 g_signal_emit (calendar,
979 gtk_calendar_signals[NEXT_MONTH_SIGNAL],
981 g_signal_emit (calendar,
982 gtk_calendar_signals[MONTH_CHANGED_SIGNAL],
985 month_len = month_length[leap (priv->year)][priv->month + 1];
987 if (month_len < priv->selected_day)
989 priv->selected_day = 0;
990 gtk_calendar_select_day (calendar, month_len);
993 gtk_calendar_select_day (calendar, priv->selected_day);
995 calendar_queue_refresh (calendar);
999 calendar_set_year_prev (GtkCalendar *calendar)
1001 GtkCalendarPrivate *priv = calendar->priv;
1005 calendar_compute_days (calendar);
1006 g_signal_emit (calendar,
1007 gtk_calendar_signals[PREV_YEAR_SIGNAL],
1009 g_signal_emit (calendar,
1010 gtk_calendar_signals[MONTH_CHANGED_SIGNAL],
1013 month_len = month_length[leap (priv->year)][priv->month + 1];
1015 if (month_len < priv->selected_day)
1017 priv->selected_day = 0;
1018 gtk_calendar_select_day (calendar, month_len);
1021 gtk_calendar_select_day (calendar, priv->selected_day);
1023 calendar_queue_refresh (calendar);
1027 calendar_set_year_next (GtkCalendar *calendar)
1029 GtkCalendarPrivate *priv = calendar->priv;
1033 calendar_compute_days (calendar);
1034 g_signal_emit (calendar,
1035 gtk_calendar_signals[NEXT_YEAR_SIGNAL],
1037 g_signal_emit (calendar,
1038 gtk_calendar_signals[MONTH_CHANGED_SIGNAL],
1041 month_len = month_length[leap (priv->year)][priv->month + 1];
1043 if (month_len < priv->selected_day)
1045 priv->selected_day = 0;
1046 gtk_calendar_select_day (calendar, month_len);
1049 gtk_calendar_select_day (calendar, priv->selected_day);
1051 calendar_queue_refresh (calendar);
1055 calendar_compute_days (GtkCalendar *calendar)
1057 GtkCalendarPrivate *priv = calendar->priv;
1060 gint ndays_in_month;
1061 gint ndays_in_prev_month;
1068 month = priv->month + 1;
1070 ndays_in_month = month_length[leap (year)][month];
1072 first_day = day_of_week (year, month, 1);
1073 first_day = (first_day + 7 - priv->week_start) % 7;
1075 /* Compute days of previous month */
1077 ndays_in_prev_month = month_length[leap (year)][month-1];
1079 ndays_in_prev_month = month_length[leap (year)][12];
1080 day = ndays_in_prev_month - first_day + 1;
1085 for (col = 0; col < first_day; col++)
1087 priv->day[row][col] = day;
1088 priv->day_month[row][col] = MONTH_PREV;
1093 /* Compute days of current month */
1095 for (day = 1; day <= ndays_in_month; day++)
1097 priv->day[row][col] = day;
1098 priv->day_month[row][col] = MONTH_CURRENT;
1108 /* Compute days of next month */
1110 for (; row <= 5; row++)
1112 for (; col <= 6; col++)
1114 priv->day[row][col] = day;
1115 priv->day_month[row][col] = MONTH_NEXT;
1123 calendar_select_and_focus_day (GtkCalendar *calendar,
1126 GtkCalendarPrivate *priv = calendar->priv;
1127 gint old_focus_row = priv->focus_row;
1128 gint old_focus_col = priv->focus_col;
1132 for (row = 0; row < 6; row ++)
1133 for (col = 0; col < 7; col++)
1135 if (priv->day_month[row][col] == MONTH_CURRENT
1136 && priv->day[row][col] == day)
1138 priv->focus_row = row;
1139 priv->focus_col = col;
1143 if (old_focus_row != -1 && old_focus_col != -1)
1144 calendar_invalidate_day (calendar, old_focus_row, old_focus_col);
1146 gtk_calendar_select_day (calendar, day);
1150 /****************************************
1151 * Layout computation utilities *
1152 ****************************************/
1155 calendar_row_height (GtkCalendar *calendar)
1157 GtkCalendarPrivate *priv = calendar->priv;
1159 return (GTK_CALENDAR_GET_PRIVATE (calendar)->main_h - CALENDAR_MARGIN
1160 - ((priv->display_flags & GTK_CALENDAR_SHOW_DAY_NAMES)
1161 ? calendar_get_ysep (calendar) : CALENDAR_MARGIN)) / 6;
1165 /* calendar_left_x_for_column: returns the x coordinate
1166 * for the left of the column */
1168 calendar_left_x_for_column (GtkCalendar *calendar,
1171 GtkCalendarPrivate *priv = calendar->priv;
1174 gint calendar_xsep = calendar_get_xsep (calendar);
1176 if (gtk_widget_get_direction (GTK_WIDGET (calendar)) == GTK_TEXT_DIR_RTL)
1177 column = 6 - column;
1179 width = GTK_CALENDAR_GET_PRIVATE (calendar)->day_width;
1180 if (priv->display_flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
1181 x_left = calendar_xsep + (width + DAY_XSEP) * column;
1183 x_left = CALENDAR_MARGIN + (width + DAY_XSEP) * column;
1188 /* column_from_x: returns the column 0-6 that the
1189 * x pixel of the xwindow is in */
1191 calendar_column_from_x (GtkCalendar *calendar,
1195 gint x_left, x_right;
1199 for (c = 0; c < 7; c++)
1201 x_left = calendar_left_x_for_column (calendar, c);
1202 x_right = x_left + GTK_CALENDAR_GET_PRIVATE (calendar)->day_width;
1204 if (event_x >= x_left && event_x < x_right)
1214 /* calendar_top_y_for_row: returns the y coordinate
1215 * for the top of the row */
1217 calendar_top_y_for_row (GtkCalendar *calendar,
1221 return (GTK_CALENDAR_GET_PRIVATE (calendar)->main_h
1222 - (CALENDAR_MARGIN + (6 - row)
1223 * calendar_row_height (calendar)));
1226 /* row_from_y: returns the row 0-5 that the
1227 * y pixel of the xwindow is in */
1229 calendar_row_from_y (GtkCalendar *calendar,
1234 gint y_top, y_bottom;
1236 height = calendar_row_height (calendar);
1239 for (r = 0; r < 6; r++)
1241 y_top = calendar_top_y_for_row (calendar, r);
1242 y_bottom = y_top + height;
1244 if (event_y >= y_top && event_y < y_bottom)
1255 calendar_arrow_rectangle (GtkCalendar *calendar,
1259 GtkWidget *widget = GTK_WIDGET (calendar);
1260 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1261 GtkAllocation allocation;
1265 gtk_widget_get_allocation (widget, &allocation);
1266 style = gtk_widget_get_style (widget);
1268 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
1269 year_left = priv->year_before;
1271 year_left = !priv->year_before;
1274 rect->width = priv->arrow_width;
1275 rect->height = priv->header_h - 7;
1279 case ARROW_MONTH_LEFT:
1281 rect->x = (allocation.width - 2 * style->xthickness
1282 - (3 + 2 * priv->arrow_width + priv->max_month_width));
1286 case ARROW_MONTH_RIGHT:
1288 rect->x = (allocation.width - 2 * style->xthickness
1289 - 3 - priv->arrow_width);
1291 rect->x = (priv->arrow_width + priv->max_month_width);
1293 case ARROW_YEAR_LEFT:
1297 rect->x = (allocation.width - 2 * style->xthickness
1298 - (3 + 2 * priv->arrow_width + priv->max_year_width));
1300 case ARROW_YEAR_RIGHT:
1302 rect->x = (priv->arrow_width + priv->max_year_width);
1304 rect->x = (allocation.width - 2 * style->xthickness
1305 - 3 - priv->arrow_width);
1311 calendar_day_rectangle (GtkCalendar *calendar,
1316 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1318 rect->x = calendar_left_x_for_column (calendar, col);
1319 rect->y = calendar_top_y_for_row (calendar, row);
1320 rect->height = calendar_row_height (calendar);
1321 rect->width = priv->day_width;
1325 calendar_set_month_prev (GtkCalendar *calendar)
1327 GtkCalendarPrivate *priv = calendar->priv;
1330 if (priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
1333 if (priv->month == 0)
1341 month_len = month_length[leap (priv->year)][priv->month + 1];
1343 calendar_compute_days (calendar);
1345 g_signal_emit (calendar,
1346 gtk_calendar_signals[PREV_MONTH_SIGNAL],
1348 g_signal_emit (calendar,
1349 gtk_calendar_signals[MONTH_CHANGED_SIGNAL],
1352 if (month_len < priv->selected_day)
1354 priv->selected_day = 0;
1355 gtk_calendar_select_day (calendar, month_len);
1359 if (priv->selected_day < 0)
1360 priv->selected_day = priv->selected_day + 1 + month_length[leap (priv->year)][priv->month + 1];
1361 gtk_calendar_select_day (calendar, priv->selected_day);
1364 calendar_queue_refresh (calendar);
1368 /****************************************
1369 * Basic object methods *
1370 ****************************************/
1373 gtk_calendar_finalize (GObject *object)
1375 G_OBJECT_CLASS (gtk_calendar_parent_class)->finalize (object);
1379 gtk_calendar_destroy (GtkObject *object)
1381 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (object);
1383 calendar_stop_spinning (GTK_CALENDAR (object));
1385 /* Call the destroy function for the extra display callback: */
1386 if (priv->detail_func_destroy && priv->detail_func_user_data)
1388 priv->detail_func_destroy (priv->detail_func_user_data);
1389 priv->detail_func_user_data = NULL;
1390 priv->detail_func_destroy = NULL;
1393 GTK_OBJECT_CLASS (gtk_calendar_parent_class)->destroy (object);
1398 calendar_set_display_option (GtkCalendar *calendar,
1399 GtkCalendarDisplayOptions flag,
1402 GtkCalendarPrivate *priv = calendar->priv;
1403 GtkCalendarDisplayOptions flags;
1406 flags = priv->display_flags | flag;
1408 flags = priv->display_flags & ~flag;
1409 gtk_calendar_set_display_options (calendar, flags);
1413 calendar_get_display_option (GtkCalendar *calendar,
1414 GtkCalendarDisplayOptions flag)
1416 GtkCalendarPrivate *priv = calendar->priv;
1418 return (priv->display_flags & flag) != 0;
1422 gtk_calendar_set_property (GObject *object,
1424 const GValue *value,
1427 GtkCalendar *calendar = GTK_CALENDAR (object);
1428 GtkCalendarPrivate *priv = calendar->priv;
1433 gtk_calendar_select_month (calendar,
1435 g_value_get_int (value));
1438 gtk_calendar_select_month (calendar,
1439 g_value_get_int (value),
1443 gtk_calendar_select_day (calendar,
1444 g_value_get_int (value));
1446 case PROP_SHOW_HEADING:
1447 calendar_set_display_option (calendar,
1448 GTK_CALENDAR_SHOW_HEADING,
1449 g_value_get_boolean (value));
1451 case PROP_SHOW_DAY_NAMES:
1452 calendar_set_display_option (calendar,
1453 GTK_CALENDAR_SHOW_DAY_NAMES,
1454 g_value_get_boolean (value));
1456 case PROP_NO_MONTH_CHANGE:
1457 calendar_set_display_option (calendar,
1458 GTK_CALENDAR_NO_MONTH_CHANGE,
1459 g_value_get_boolean (value));
1461 case PROP_SHOW_WEEK_NUMBERS:
1462 calendar_set_display_option (calendar,
1463 GTK_CALENDAR_SHOW_WEEK_NUMBERS,
1464 g_value_get_boolean (value));
1466 case PROP_SHOW_DETAILS:
1467 calendar_set_display_option (calendar,
1468 GTK_CALENDAR_SHOW_DETAILS,
1469 g_value_get_boolean (value));
1471 case PROP_DETAIL_WIDTH_CHARS:
1472 gtk_calendar_set_detail_width_chars (calendar,
1473 g_value_get_int (value));
1475 case PROP_DETAIL_HEIGHT_ROWS:
1476 gtk_calendar_set_detail_height_rows (calendar,
1477 g_value_get_int (value));
1480 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1486 gtk_calendar_get_property (GObject *object,
1491 GtkCalendar *calendar = GTK_CALENDAR (object);
1492 GtkCalendarPrivate *priv = calendar->priv;
1497 g_value_set_int (value, priv->year);
1500 g_value_set_int (value, priv->month);
1503 g_value_set_int (value, priv->selected_day);
1505 case PROP_SHOW_HEADING:
1506 g_value_set_boolean (value, calendar_get_display_option (calendar,
1507 GTK_CALENDAR_SHOW_HEADING));
1509 case PROP_SHOW_DAY_NAMES:
1510 g_value_set_boolean (value, calendar_get_display_option (calendar,
1511 GTK_CALENDAR_SHOW_DAY_NAMES));
1513 case PROP_NO_MONTH_CHANGE:
1514 g_value_set_boolean (value, calendar_get_display_option (calendar,
1515 GTK_CALENDAR_NO_MONTH_CHANGE));
1517 case PROP_SHOW_WEEK_NUMBERS:
1518 g_value_set_boolean (value, calendar_get_display_option (calendar,
1519 GTK_CALENDAR_SHOW_WEEK_NUMBERS));
1521 case PROP_SHOW_DETAILS:
1522 g_value_set_boolean (value, calendar_get_display_option (calendar,
1523 GTK_CALENDAR_SHOW_DETAILS));
1525 case PROP_DETAIL_WIDTH_CHARS:
1526 g_value_set_int (value, priv->detail_width_chars);
1528 case PROP_DETAIL_HEIGHT_ROWS:
1529 g_value_set_int (value, priv->detail_height_rows);
1532 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1538 /****************************************
1540 ****************************************/
1543 calendar_realize_arrows (GtkCalendar *calendar)
1545 GtkWidget *widget = GTK_WIDGET (calendar);
1546 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1547 GdkWindowAttr attributes;
1548 gint attributes_mask;
1551 /* Arrow windows ------------------------------------- */
1552 if (! (priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
1553 && (priv->display_flags & GTK_CALENDAR_SHOW_HEADING))
1555 attributes.wclass = GDK_INPUT_OUTPUT;
1556 attributes.window_type = GDK_WINDOW_CHILD;
1557 attributes.visual = gtk_widget_get_visual (widget);
1558 attributes.event_mask = (gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK
1559 | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
1560 | GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK);
1561 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
1562 for (i = 0; i < 4; i++)
1565 calendar_arrow_rectangle (calendar, i, &rect);
1567 attributes.x = rect.x;
1568 attributes.y = rect.y;
1569 attributes.width = rect.width;
1570 attributes.height = rect.height;
1571 priv->arrow_win[i] = gdk_window_new (priv->header_win,
1574 if (gtk_widget_is_sensitive (widget))
1575 priv->arrow_state[i] = GTK_STATE_NORMAL;
1577 priv->arrow_state[i] = GTK_STATE_INSENSITIVE;
1578 gdk_window_set_background (priv->arrow_win[i],
1579 HEADER_BG_COLOR (GTK_WIDGET (calendar)));
1580 gdk_window_show (priv->arrow_win[i]);
1581 gdk_window_set_user_data (priv->arrow_win[i], widget);
1586 for (i = 0; i < 4; i++)
1587 priv->arrow_win[i] = NULL;
1592 calendar_realize_header (GtkCalendar *calendar)
1594 GtkWidget *widget = GTK_WIDGET (calendar);
1595 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1596 GdkWindowAttr attributes;
1597 gint attributes_mask;
1599 /* Header window ------------------------------------- */
1600 if (priv->display_flags & GTK_CALENDAR_SHOW_HEADING)
1602 GtkAllocation allocation;
1605 style = gtk_widget_get_style (widget);
1606 gtk_widget_get_allocation (widget, &allocation);
1608 attributes.wclass = GDK_INPUT_OUTPUT;
1609 attributes.window_type = GDK_WINDOW_CHILD;
1610 attributes.visual = gtk_widget_get_visual (widget);
1611 attributes.event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
1612 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
1613 attributes.x = style->xthickness;
1614 attributes.y = style->ythickness;
1615 attributes.width = allocation.width - 2 * attributes.x;
1616 attributes.height = priv->header_h;
1617 priv->header_win = gdk_window_new (gtk_widget_get_window (widget),
1618 &attributes, attributes_mask);
1620 gdk_window_set_background (priv->header_win,
1621 HEADER_BG_COLOR (GTK_WIDGET (calendar)));
1622 gdk_window_show (priv->header_win);
1623 gdk_window_set_user_data (priv->header_win, widget);
1628 priv->header_win = NULL;
1630 calendar_realize_arrows (calendar);
1634 calendar_get_inner_border (GtkCalendar *calendar)
1638 gtk_widget_style_get (GTK_WIDGET (calendar),
1639 "inner-border", &inner_border,
1642 return inner_border;
1646 calendar_get_xsep (GtkCalendar *calendar)
1650 gtk_widget_style_get (GTK_WIDGET (calendar),
1651 "horizontal-separation", &xsep,
1658 calendar_get_ysep (GtkCalendar *calendar)
1662 gtk_widget_style_get (GTK_WIDGET (calendar),
1663 "vertical-separation", &ysep,
1670 calendar_realize_day_names (GtkCalendar *calendar)
1672 GtkWidget *widget = GTK_WIDGET (calendar);
1673 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1674 GdkWindowAttr attributes;
1675 gint attributes_mask;
1676 gint inner_border = calendar_get_inner_border (calendar);
1678 /* Day names window --------------------------------- */
1679 if ( priv->display_flags & GTK_CALENDAR_SHOW_DAY_NAMES)
1681 GtkAllocation allocation;
1684 style = gtk_widget_get_style (widget);
1685 gtk_widget_get_allocation (widget, &allocation);
1687 attributes.wclass = GDK_INPUT_OUTPUT;
1688 attributes.window_type = GDK_WINDOW_CHILD;
1689 attributes.visual = gtk_widget_get_visual (widget);
1690 attributes.event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
1691 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
1692 attributes.x = style->xthickness + inner_border;
1693 attributes.y = priv->header_h + (style->ythickness + inner_border);
1694 attributes.width = allocation.width - (style->xthickness + inner_border) * 2;
1695 attributes.height = priv->day_name_h;
1696 priv->day_name_win = gdk_window_new (gtk_widget_get_window (widget),
1699 gdk_window_set_background (priv->day_name_win,
1700 BACKGROUND_COLOR ( GTK_WIDGET ( calendar)));
1701 gdk_window_show (priv->day_name_win);
1702 gdk_window_set_user_data (priv->day_name_win, widget);
1706 priv->day_name_win = NULL;
1711 calendar_realize_week_numbers (GtkCalendar *calendar)
1713 GtkWidget *widget = GTK_WIDGET (calendar);
1714 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1715 GdkWindowAttr attributes;
1716 gint attributes_mask;
1717 gint inner_border = calendar_get_inner_border (calendar);
1719 /* Week number window -------------------------------- */
1720 if (priv->display_flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
1724 style = gtk_widget_get_style (widget);
1726 attributes.wclass = GDK_INPUT_OUTPUT;
1727 attributes.window_type = GDK_WINDOW_CHILD;
1728 attributes.visual = gtk_widget_get_visual (widget);
1729 attributes.event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
1730 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
1731 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
1733 attributes.x = style->xthickness + inner_border;
1737 GtkAllocation allocation;
1739 gtk_widget_get_allocation (widget, &allocation);
1740 attributes.x = allocation.width - priv->week_width - (style->xthickness + inner_border);
1742 attributes.y = priv->header_h + priv->day_name_h + (style->ythickness + inner_border);
1743 attributes.width = priv->week_width;
1744 attributes.height = priv->main_h;
1745 priv->week_win = gdk_window_new (gtk_widget_get_window (widget),
1746 &attributes, attributes_mask);
1747 gdk_window_set_background (priv->week_win,
1748 BACKGROUND_COLOR (GTK_WIDGET (calendar)));
1749 gdk_window_show (priv->week_win);
1750 gdk_window_set_user_data (priv->week_win, widget);
1754 priv->week_win = NULL;
1759 gtk_calendar_realize (GtkWidget *widget)
1761 GtkCalendar *calendar = GTK_CALENDAR (widget);
1762 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
1763 GtkAllocation allocation;
1766 GdkWindowAttr attributes;
1767 gint attributes_mask;
1768 gint inner_border = calendar_get_inner_border (calendar);
1770 gtk_widget_get_allocation (widget, &allocation);
1771 style = gtk_widget_get_style (widget);
1773 gtk_widget_set_realized (widget, TRUE);
1775 attributes.x = allocation.x;
1776 attributes.y = allocation.y;
1777 attributes.width = allocation.width;
1778 attributes.height = allocation.height;
1779 attributes.wclass = GDK_INPUT_OUTPUT;
1780 attributes.window_type = GDK_WINDOW_CHILD;
1781 attributes.event_mask = (gtk_widget_get_events (widget)
1782 | GDK_EXPOSURE_MASK |GDK_KEY_PRESS_MASK | GDK_SCROLL_MASK);
1783 attributes.visual = gtk_widget_get_visual (widget);
1785 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
1787 window = gdk_window_new (gtk_widget_get_parent_window (widget),
1788 &attributes, attributes_mask);
1789 gtk_widget_set_window (widget, window);
1791 gtk_widget_style_attach (widget);
1793 /* Header window ------------------------------------- */
1794 calendar_realize_header (calendar);
1795 /* Day names window --------------------------------- */
1796 calendar_realize_day_names (calendar);
1797 /* Week number window -------------------------------- */
1798 calendar_realize_week_numbers (calendar);
1799 /* Main Window -------------------------------------- */
1800 attributes.event_mask = (gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK
1801 | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
1802 | GDK_POINTER_MOTION_MASK | GDK_LEAVE_NOTIFY_MASK);
1804 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
1805 attributes.x = priv->week_width + (style->ythickness + inner_border);
1807 attributes.x = style->ythickness + inner_border;
1809 attributes.y = priv->header_h + priv->day_name_h + (style->ythickness + inner_border);
1810 attributes.width = allocation.width - attributes.x - (style->xthickness + inner_border);
1812 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1813 attributes.width -= priv->week_width;
1815 attributes.height = priv->main_h;
1816 priv->main_win = gdk_window_new (window,
1817 &attributes, attributes_mask);
1818 gdk_window_set_background (priv->main_win,
1819 BACKGROUND_COLOR ( GTK_WIDGET ( calendar)));
1820 gdk_window_show (priv->main_win);
1821 gdk_window_set_user_data (priv->main_win, widget);
1822 gdk_window_set_background (window, BACKGROUND_COLOR (widget));
1823 gdk_window_show (window);
1824 gdk_window_set_user_data (window, widget);
1828 gtk_calendar_unrealize (GtkWidget *widget)
1830 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
1833 if (priv->header_win)
1835 for (i = 0; i < 4; i++)
1837 if (priv->arrow_win[i])
1839 gdk_window_set_user_data (priv->arrow_win[i], NULL);
1840 gdk_window_destroy (priv->arrow_win[i]);
1841 priv->arrow_win[i] = NULL;
1844 gdk_window_set_user_data (priv->header_win, NULL);
1845 gdk_window_destroy (priv->header_win);
1846 priv->header_win = NULL;
1851 gdk_window_set_user_data (priv->week_win, NULL);
1852 gdk_window_destroy (priv->week_win);
1853 priv->week_win = NULL;
1858 gdk_window_set_user_data (priv->main_win, NULL);
1859 gdk_window_destroy (priv->main_win);
1860 priv->main_win = NULL;
1862 if (priv->day_name_win)
1864 gdk_window_set_user_data (priv->day_name_win, NULL);
1865 gdk_window_destroy (priv->day_name_win);
1866 priv->day_name_win = NULL;
1869 GTK_WIDGET_CLASS (gtk_calendar_parent_class)->unrealize (widget);
1873 gtk_calendar_get_detail (GtkCalendar *calendar,
1877 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1880 if (priv->detail_func == NULL)
1884 month = priv->month + priv->day_month[row][column] - MONTH_CURRENT;
1891 else if (month > 11)
1897 return priv->detail_func (calendar,
1899 priv->day[row][column],
1900 priv->detail_func_user_data);
1904 gtk_calendar_query_tooltip (GtkWidget *widget,
1907 gboolean keyboard_mode,
1908 GtkTooltip *tooltip)
1910 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
1911 GtkCalendar *calendar = GTK_CALENDAR (widget);
1912 gchar *detail = NULL;
1913 GdkRectangle day_rect;
1917 gint x0, y0, row, col;
1919 gdk_window_get_position (priv->main_win, &x0, &y0);
1920 col = calendar_column_from_x (calendar, x - x0);
1921 row = calendar_row_from_y (calendar, y - y0);
1923 if (col != -1 && row != -1 &&
1924 (0 != (priv->detail_overflow[row] & (1 << col)) ||
1925 0 == (priv->display_flags & GTK_CALENDAR_SHOW_DETAILS)))
1927 detail = gtk_calendar_get_detail (calendar, row, col);
1928 calendar_day_rectangle (calendar, row, col, &day_rect);
1937 gtk_tooltip_set_tip_area (tooltip, &day_rect);
1938 gtk_tooltip_set_markup (tooltip, detail);
1945 if (GTK_WIDGET_CLASS (gtk_calendar_parent_class)->query_tooltip)
1946 return GTK_WIDGET_CLASS (gtk_calendar_parent_class)->query_tooltip (widget, x, y, keyboard_mode, tooltip);
1952 /****************************************
1953 * Size Request and Allocate *
1954 ****************************************/
1957 gtk_calendar_size_request (GtkWidget *widget,
1958 GtkRequisition *requisition)
1960 GtkCalendar *calendar = GTK_CALENDAR (widget);
1961 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
1963 PangoLayout *layout;
1964 PangoRectangle logical_rect;
1968 gint calendar_margin = CALENDAR_MARGIN;
1969 gint header_width, main_width;
1970 gint max_header_height = 0;
1973 gint max_detail_height;
1974 gint inner_border = calendar_get_inner_border (calendar);
1975 gint calendar_ysep = calendar_get_ysep (calendar);
1976 gint calendar_xsep = calendar_get_xsep (calendar);
1978 gtk_widget_style_get (GTK_WIDGET (widget),
1979 "focus-line-width", &focus_width,
1980 "focus-padding", &focus_padding,
1983 layout = gtk_widget_create_pango_layout (widget, NULL);
1986 * Calculate the requisition width for the widget.
1991 if (priv->display_flags & GTK_CALENDAR_SHOW_HEADING)
1993 priv->max_month_width = 0;
1994 for (i = 0; i < 12; i++)
1996 pango_layout_set_text (layout, default_monthname[i], -1);
1997 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1998 priv->max_month_width = MAX (priv->max_month_width,
1999 logical_rect.width + 8);
2000 max_header_height = MAX (max_header_height, logical_rect.height);
2003 priv->max_year_width = 0;
2004 /* Translators: This is a text measurement template.
2005 * Translate it to the widest year text
2007 * If you don't understand this, leave it as "2000"
2009 pango_layout_set_text (layout, C_("year measurement template", "2000"), -1);
2010 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2011 priv->max_year_width = MAX (priv->max_year_width,
2012 logical_rect.width + 8);
2013 max_header_height = MAX (max_header_height, logical_rect.height);
2017 priv->max_month_width = 0;
2018 priv->max_year_width = 0;
2021 if (priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
2022 header_width = (priv->max_month_width
2023 + priv->max_year_width
2026 header_width = (priv->max_month_width
2027 + priv->max_year_width
2028 + 4 * priv->arrow_width + 3 * 3);
2030 /* Mainwindow labels width */
2032 priv->max_day_char_width = 0;
2033 priv->max_day_char_ascent = 0;
2034 priv->max_day_char_descent = 0;
2035 priv->min_day_width = 0;
2037 for (i = 0; i < 9; i++)
2040 g_snprintf (buffer, sizeof (buffer), C_("calendar:day:digits", "%d"), i * 11);
2041 pango_layout_set_text (layout, buffer, -1);
2042 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2043 priv->min_day_width = MAX (priv->min_day_width,
2044 logical_rect.width);
2046 priv->max_day_char_ascent = MAX (priv->max_day_char_ascent,
2047 PANGO_ASCENT (logical_rect));
2048 priv->max_day_char_descent = MAX (priv->max_day_char_descent,
2049 PANGO_DESCENT (logical_rect));
2052 priv->max_label_char_ascent = 0;
2053 priv->max_label_char_descent = 0;
2054 if (priv->display_flags & GTK_CALENDAR_SHOW_DAY_NAMES)
2055 for (i = 0; i < 7; i++)
2057 pango_layout_set_text (layout, default_abbreviated_dayname[i], -1);
2058 pango_layout_line_get_pixel_extents (pango_layout_get_lines_readonly (layout)->data, NULL, &logical_rect);
2060 priv->min_day_width = MAX (priv->min_day_width, logical_rect.width);
2061 priv->max_label_char_ascent = MAX (priv->max_label_char_ascent,
2062 PANGO_ASCENT (logical_rect));
2063 priv->max_label_char_descent = MAX (priv->max_label_char_descent,
2064 PANGO_DESCENT (logical_rect));
2067 priv->max_week_char_width = 0;
2068 if (priv->display_flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
2069 for (i = 0; i < 9; i++)
2072 g_snprintf (buffer, sizeof (buffer), C_("calendar:week:digits", "%d"), i * 11);
2073 pango_layout_set_text (layout, buffer, -1);
2074 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2075 priv->max_week_char_width = MAX (priv->max_week_char_width,
2076 logical_rect.width / 2);
2079 /* Calculate detail extents. Do this as late as possible since
2080 * pango_layout_set_markup is called which alters font settings. */
2081 max_detail_height = 0;
2083 if (priv->detail_func && (priv->display_flags & GTK_CALENDAR_SHOW_DETAILS))
2085 gchar *markup, *tail;
2087 if (priv->detail_width_chars || priv->detail_height_rows)
2089 gint rows = MAX (1, priv->detail_height_rows) - 1;
2090 gsize len = priv->detail_width_chars + rows + 16;
2092 markup = tail = g_alloca (len);
2094 memcpy (tail, "<small>", 7);
2097 memset (tail, 'm', priv->detail_width_chars);
2098 tail += priv->detail_width_chars;
2100 memset (tail, '\n', rows);
2103 memcpy (tail, "</small>", 9);
2106 g_assert (len == (tail - markup));
2108 pango_layout_set_markup (layout, markup, -1);
2109 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2111 if (priv->detail_width_chars)
2112 priv->min_day_width = MAX (priv->min_day_width, logical_rect.width);
2113 if (priv->detail_height_rows)
2114 max_detail_height = MAX (max_detail_height, logical_rect.height);
2117 if (!priv->detail_width_chars || !priv->detail_height_rows)
2118 for (r = 0; r < 6; r++)
2119 for (c = 0; c < 7; c++)
2121 gchar *detail = gtk_calendar_get_detail (calendar, r, c);
2125 markup = g_strconcat ("<small>", detail, "</small>", NULL);
2126 pango_layout_set_markup (layout, markup, -1);
2128 if (priv->detail_width_chars)
2130 pango_layout_set_wrap (layout, PANGO_WRAP_WORD_CHAR);
2131 pango_layout_set_width (layout, PANGO_SCALE * priv->min_day_width);
2134 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2136 if (!priv->detail_width_chars)
2137 priv->min_day_width = MAX (priv->min_day_width, logical_rect.width);
2138 if (!priv->detail_height_rows)
2139 max_detail_height = MAX (max_detail_height, logical_rect.height);
2147 /* We add one to max_day_char_width to be able to make the marked day "bold" */
2148 priv->max_day_char_width = priv->min_day_width / 2 + 1;
2150 main_width = (7 * (priv->min_day_width + (focus_padding + focus_width) * 2) + (DAY_XSEP * 6) + CALENDAR_MARGIN * 2
2151 + (priv->max_week_char_width
2152 ? priv->max_week_char_width * 2 + (focus_padding + focus_width) * 2 + calendar_xsep * 2
2155 style = gtk_widget_get_style (widget);
2157 requisition->width = MAX (header_width, main_width + inner_border * 2) + style->xthickness * 2;
2160 * Calculate the requisition height for the widget.
2163 if (priv->display_flags & GTK_CALENDAR_SHOW_HEADING)
2165 priv->header_h = (max_header_height + calendar_ysep * 2);
2172 if (priv->display_flags & GTK_CALENDAR_SHOW_DAY_NAMES)
2174 priv->day_name_h = (priv->max_label_char_ascent
2175 + priv->max_label_char_descent
2176 + 2 * (focus_padding + focus_width) + calendar_margin);
2177 calendar_margin = calendar_ysep;
2181 priv->day_name_h = 0;
2184 priv->main_h = (CALENDAR_MARGIN + calendar_margin
2185 + 6 * (priv->max_day_char_ascent
2186 + priv->max_day_char_descent
2188 + 2 * (focus_padding + focus_width))
2191 height = (priv->header_h + priv->day_name_h
2194 requisition->height = height + (style->ythickness + inner_border) * 2;
2196 g_object_unref (layout);
2200 gtk_calendar_size_allocate (GtkWidget *widget,
2201 GtkAllocation *allocation)
2203 GtkCalendar *calendar = GTK_CALENDAR (widget);
2204 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
2206 gint xthickness, ythickness;
2208 gint inner_border = calendar_get_inner_border (calendar);
2209 gint calendar_xsep = calendar_get_xsep (calendar);
2211 style = gtk_widget_get_style (widget);
2212 xthickness = style->xthickness;
2213 ythickness = style->xthickness;
2215 gtk_widget_set_allocation (widget, allocation);
2217 if (priv->display_flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
2219 priv->day_width = (priv->min_day_width
2220 * ((allocation->width - (xthickness + inner_border) * 2
2221 - (CALENDAR_MARGIN * 2) - (DAY_XSEP * 6) - calendar_xsep * 2))
2222 / (7 * priv->min_day_width + priv->max_week_char_width * 2));
2223 priv->week_width = ((allocation->width - (xthickness + inner_border) * 2
2224 - (CALENDAR_MARGIN * 2) - (DAY_XSEP * 6) - calendar_xsep * 2 )
2225 - priv->day_width * 7 + CALENDAR_MARGIN + calendar_xsep);
2229 priv->day_width = (allocation->width
2230 - (xthickness + inner_border) * 2
2231 - (CALENDAR_MARGIN * 2)
2232 - (DAY_XSEP * 6))/7;
2233 priv->week_width = 0;
2236 if (gtk_widget_get_realized (widget))
2238 gdk_window_move_resize (gtk_widget_get_window (widget),
2239 allocation->x, allocation->y,
2240 allocation->width, allocation->height);
2241 if (priv->header_win)
2242 gdk_window_move_resize (priv->header_win,
2243 xthickness, ythickness,
2244 allocation->width - 2 * xthickness, priv->header_h);
2246 for (i = 0 ; i < 4 ; i++)
2248 if (priv->arrow_win[i])
2251 calendar_arrow_rectangle (calendar, i, &rect);
2253 gdk_window_move_resize (priv->arrow_win[i],
2254 rect.x, rect.y, rect.width, rect.height);
2258 if (priv->day_name_win)
2259 gdk_window_move_resize (priv->day_name_win,
2260 xthickness + inner_border,
2261 priv->header_h + (style->ythickness + inner_border),
2262 allocation->width - (xthickness + inner_border) * 2,
2264 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
2267 gdk_window_move_resize (priv->week_win,
2268 (xthickness + inner_border),
2269 priv->header_h + priv->day_name_h
2270 + (style->ythickness + inner_border),
2273 gdk_window_move_resize (priv->main_win,
2274 priv->week_width + (xthickness + inner_border),
2275 priv->header_h + priv->day_name_h
2276 + (style->ythickness + inner_border),
2279 - (xthickness + inner_border) * 2,
2284 gdk_window_move_resize (priv->main_win,
2285 (xthickness + inner_border),
2286 priv->header_h + priv->day_name_h
2287 + (style->ythickness + inner_border),
2290 - (xthickness + inner_border) * 2,
2293 gdk_window_move_resize (priv->week_win,
2296 - (xthickness + inner_border),
2297 priv->header_h + priv->day_name_h
2298 + (style->ythickness + inner_border),
2306 /****************************************
2308 ****************************************/
2311 calendar_paint_header (GtkCalendar *calendar, cairo_t *cr)
2313 GtkWidget *widget = GTK_WIDGET (calendar);
2314 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2315 GtkAllocation allocation;
2320 gint max_month_width;
2321 gint max_year_width;
2322 PangoLayout *layout;
2323 PangoRectangle logical_rect;
2330 gtk_cairo_transform_to_window (cr, widget, priv->header_win);
2332 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
2333 year_left = priv->year_before;
2335 year_left = !priv->year_before;
2337 gtk_widget_get_allocation (widget, &allocation);
2338 style = gtk_widget_get_style (widget);
2340 header_width = allocation.width - 2 * style->xthickness;
2342 max_month_width = priv->max_month_width;
2343 max_year_width = priv->max_year_width;
2345 gtk_paint_shadow (style, cr,
2346 GTK_STATE_NORMAL, GTK_SHADOW_OUT,
2348 0, 0, header_width, priv->header_h);
2350 tmp_time = 1; /* Jan 1 1970, 00:00:01 UTC */
2351 tm = gmtime (&tmp_time);
2352 tm->tm_year = priv->year - 1900;
2354 /* Translators: This dictates how the year is displayed in
2355 * gtkcalendar widget. See strftime() manual for the format.
2356 * Use only ASCII in the translation.
2358 * Also look for the msgid "2000".
2359 * Translate that entry to a year with the widest output of this
2362 * "%Y" is appropriate for most locales.
2364 strftime (buffer, sizeof (buffer), C_("calendar year format", "%Y"), tm);
2365 str = g_locale_to_utf8 (buffer, -1, NULL, NULL, NULL);
2366 layout = gtk_widget_create_pango_layout (widget, str);
2369 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2372 y = (priv->header_h - logical_rect.height) / 2;
2374 /* Draw year and its arrows */
2376 if (priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
2378 x = 3 + (max_year_width - logical_rect.width)/2;
2380 x = header_width - (3 + max_year_width
2381 - (max_year_width - logical_rect.width)/2);
2384 x = 3 + priv->arrow_width + (max_year_width - logical_rect.width)/2;
2386 x = header_width - (3 + priv->arrow_width + max_year_width
2387 - (max_year_width - logical_rect.width)/2);
2390 gdk_cairo_set_source_color (cr, HEADER_FG_COLOR (GTK_WIDGET (calendar)));
2391 cairo_move_to (cr, x, y);
2392 pango_cairo_show_layout (cr, layout);
2395 g_snprintf (buffer, sizeof (buffer), "%s", default_monthname[priv->month]);
2396 pango_layout_set_text (layout, buffer, -1);
2397 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2399 if (priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
2401 x = header_width - (3 + max_month_width
2402 - (max_month_width - logical_rect.width)/2);
2404 x = 3 + (max_month_width - logical_rect.width) / 2;
2407 x = header_width - (3 + priv->arrow_width + max_month_width
2408 - (max_month_width - logical_rect.width)/2);
2410 x = 3 + priv->arrow_width + (max_month_width - logical_rect.width)/2;
2412 cairo_move_to (cr, x, y);
2413 pango_cairo_show_layout (cr, layout);
2415 g_object_unref (layout);
2421 calendar_paint_day_names (GtkCalendar *calendar,
2424 GtkWidget *widget = GTK_WIDGET (calendar);
2425 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2426 GtkAllocation allocation;
2429 int day_width, cal_width;
2431 PangoLayout *layout;
2432 PangoRectangle logical_rect;
2435 gint calendar_ysep = calendar_get_ysep (calendar);
2436 gint calendar_xsep = calendar_get_xsep (calendar);
2439 gtk_cairo_transform_to_window (cr, widget, priv->day_name_win);
2441 gtk_widget_style_get (GTK_WIDGET (widget),
2442 "focus-line-width", &focus_width,
2443 "focus-padding", &focus_padding,
2446 gtk_widget_get_allocation (widget, &allocation);
2448 day_width = priv->day_width;
2449 cal_width = allocation.width;
2450 day_wid_sep = day_width + DAY_XSEP;
2453 * Draw rectangles as inverted background for the labels.
2456 gdk_cairo_set_source_color (cr, SELECTED_BG_COLOR (widget));
2457 cairo_rectangle (cr,
2458 CALENDAR_MARGIN, CALENDAR_MARGIN,
2459 cal_width-CALENDAR_MARGIN * 2,
2460 priv->day_name_h - CALENDAR_MARGIN);
2463 if (priv->display_flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
2465 cairo_rectangle (cr,
2467 priv->day_name_h - calendar_ysep,
2468 priv->week_width - calendar_ysep - CALENDAR_MARGIN,
2477 layout = gtk_widget_create_pango_layout (widget, NULL);
2479 gdk_cairo_set_source_color (cr, SELECTED_FG_COLOR (widget));
2480 for (i = 0; i < 7; i++)
2482 if (gtk_widget_get_direction (GTK_WIDGET (calendar)) == GTK_TEXT_DIR_RTL)
2486 day = (day + priv->week_start) % 7;
2487 g_snprintf (buffer, sizeof (buffer), "%s", default_abbreviated_dayname[day]);
2489 pango_layout_set_text (layout, buffer, -1);
2490 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2494 + (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR ?
2495 (priv->week_width + (priv->week_width ? calendar_xsep : 0))
2498 + (day_width - logical_rect.width)/2),
2499 CALENDAR_MARGIN + focus_width + focus_padding + logical_rect.y);
2500 pango_cairo_show_layout (cr, layout);
2503 g_object_unref (layout);
2509 calendar_paint_week_numbers (GtkCalendar *calendar,
2512 GtkWidget *widget = GTK_WIDGET (calendar);
2513 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2514 guint week = 0, year;
2515 gint row, x_loc, y_loc;
2518 PangoLayout *layout;
2519 PangoRectangle logical_rect;
2522 gint calendar_xsep = calendar_get_xsep (calendar);
2525 gtk_cairo_transform_to_window (cr, widget, priv->week_win);
2527 gtk_widget_style_get (GTK_WIDGET (widget),
2528 "focus-line-width", &focus_width,
2529 "focus-padding", &focus_padding,
2533 * Draw a rectangle as inverted background for the labels.
2536 gdk_cairo_set_source_color (cr, SELECTED_BG_COLOR (widget));
2537 if (priv->day_name_win)
2538 cairo_rectangle (cr,
2541 priv->week_width - CALENDAR_MARGIN,
2542 priv->main_h - CALENDAR_MARGIN);
2544 cairo_rectangle (cr,
2547 priv->week_width - CALENDAR_MARGIN,
2548 priv->main_h - 2 * CALENDAR_MARGIN);
2555 layout = gtk_widget_create_pango_layout (widget, NULL);
2557 gdk_cairo_set_source_color (cr, SELECTED_FG_COLOR (widget));
2558 day_height = calendar_row_height (calendar);
2559 for (row = 0; row < 6; row++)
2564 if (priv->day[row][6] < 15 && row > 3 && priv->month == 11)
2567 result = week_of_year (&week, &year,
2568 ((priv->day[row][6] < 15 && row > 3 ? 1 : 0)
2569 + priv->month) % 12 + 1, priv->day[row][6]);
2570 g_return_if_fail (result);
2572 /* Translators: this defines whether the week numbers should use
2573 * localized digits or the ones used in English (0123...).
2575 * Translate to "%Id" if you want to use localized digits, or
2576 * translate to "%d" otherwise.
2578 * Note that translating this doesn't guarantee that you get localized
2579 * digits. That needs support from your system and locale definition
2582 g_snprintf (buffer, sizeof (buffer), C_("calendar:week:digits", "%d"), week);
2583 pango_layout_set_text (layout, buffer, -1);
2584 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2586 y_loc = calendar_top_y_for_row (calendar, row) + (day_height - logical_rect.height) / 2;
2588 x_loc = (priv->week_width
2589 - logical_rect.width
2590 - calendar_xsep - focus_padding - focus_width);
2592 cairo_move_to (cr, x_loc, y_loc);
2593 pango_cairo_show_layout (cr, layout);
2596 g_object_unref (layout);
2602 calendar_invalidate_day_num (GtkCalendar *calendar,
2605 GtkCalendarPrivate *priv = calendar->priv;
2606 gint r, c, row, col;
2610 for (r = 0; r < 6; r++)
2611 for (c = 0; c < 7; c++)
2612 if (priv->day_month[r][c] == MONTH_CURRENT &&
2613 priv->day[r][c] == day)
2619 g_return_if_fail (row != -1);
2620 g_return_if_fail (col != -1);
2622 calendar_invalidate_day (calendar, row, col);
2626 calendar_invalidate_day (GtkCalendar *calendar,
2630 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2634 GdkRectangle day_rect;
2636 calendar_day_rectangle (calendar, row, col, &day_rect);
2637 gdk_window_invalidate_rect (priv->main_win, &day_rect, FALSE);
2642 is_color_attribute (PangoAttribute *attribute,
2645 return (attribute->klass->type == PANGO_ATTR_FOREGROUND ||
2646 attribute->klass->type == PANGO_ATTR_BACKGROUND);
2650 calendar_paint_day (GtkCalendar *calendar,
2655 GtkWidget *widget = GTK_WIDGET (calendar);
2656 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2658 GdkColor *text_color;
2663 GdkRectangle day_rect;
2665 PangoLayout *layout;
2666 PangoRectangle logical_rect;
2667 gboolean overflow = FALSE;
2668 gboolean show_details;
2670 g_return_if_fail (row < 6);
2671 g_return_if_fail (col < 7);
2673 style = gtk_widget_get_style (widget);
2675 day = priv->day[row][col];
2676 show_details = (priv->display_flags & GTK_CALENDAR_SHOW_DETAILS);
2678 calendar_day_rectangle (calendar, row, col, &day_rect);
2680 if (priv->day_month[row][col] == MONTH_PREV)
2682 text_color = PREV_MONTH_COLOR (widget);
2684 else if (priv->day_month[row][col] == MONTH_NEXT)
2686 text_color = NEXT_MONTH_COLOR (widget);
2691 if (priv->highlight_row == row && priv->highlight_col == col)
2693 cairo_set_source_color (cr, HIGHLIGHT_BG_COLOR (widget));
2694 gdk_cairo_rectangle (cr, &day_rect);
2698 if (priv->selected_day == day)
2700 gdk_cairo_set_source_color (cr, SELECTED_BG_COLOR (widget));
2701 gdk_cairo_rectangle (cr, &day_rect);
2704 if (priv->selected_day == day)
2705 text_color = SELECTED_FG_COLOR (widget);
2706 else if (priv->marked_date[day-1])
2707 text_color = MARKED_COLOR (widget);
2709 text_color = NORMAL_DAY_COLOR (widget);
2712 /* Translators: this defines whether the day numbers should use
2713 * localized digits or the ones used in English (0123...).
2715 * Translate to "%Id" if you want to use localized digits, or
2716 * translate to "%d" otherwise.
2718 * Note that translating this doesn't guarantee that you get localized
2719 * digits. That needs support from your system and locale definition
2722 g_snprintf (buffer, sizeof (buffer), C_("calendar:day:digits", "%d"), day);
2724 /* Get extra information to show, if any: */
2726 detail = gtk_calendar_get_detail (calendar, row, col);
2728 layout = gtk_widget_create_pango_layout (widget, buffer);
2729 pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
2730 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2732 x_loc = day_rect.x + (day_rect.width - logical_rect.width) / 2;
2735 gdk_cairo_set_source_color (cr, text_color);
2736 cairo_move_to (cr, x_loc, y_loc);
2737 pango_cairo_show_layout (cr, layout);
2739 if (priv->day_month[row][col] == MONTH_CURRENT &&
2740 (priv->marked_date[day-1] || (detail && !show_details)))
2742 cairo_move_to (cr, x_loc - 1, y_loc);
2743 pango_cairo_show_layout (cr, layout);
2746 y_loc += priv->max_day_char_descent;
2748 if (priv->detail_func && show_details)
2752 if (priv->selected_day == day)
2753 gdk_cairo_set_source_color (cr, &style->text[GTK_STATE_ACTIVE]);
2754 else if (priv->day_month[row][col] == MONTH_CURRENT)
2755 gdk_cairo_set_source_color (cr, &style->base[GTK_STATE_ACTIVE]);
2757 gdk_cairo_set_source_color (cr, &style->base[GTK_STATE_INSENSITIVE]);
2759 cairo_set_line_width (cr, 1);
2760 cairo_move_to (cr, day_rect.x + 2, y_loc + 0.5);
2761 cairo_line_to (cr, day_rect.x + day_rect.width - 2, y_loc + 0.5);
2769 if (detail && show_details)
2771 gchar *markup = g_strconcat ("<small>", detail, "</small>", NULL);
2772 pango_layout_set_markup (layout, markup, -1);
2775 if (day == priv->selected_day)
2777 /* Stripping colors as they conflict with selection marking. */
2779 PangoAttrList *attrs = pango_layout_get_attributes (layout);
2780 PangoAttrList *colors = NULL;
2783 colors = pango_attr_list_filter (attrs, is_color_attribute, NULL);
2785 pango_attr_list_unref (colors);
2788 pango_layout_set_wrap (layout, PANGO_WRAP_WORD_CHAR);
2789 pango_layout_set_width (layout, PANGO_SCALE * day_rect.width);
2791 if (priv->detail_height_rows)
2793 gint dy = day_rect.height - (y_loc - day_rect.y);
2794 pango_layout_set_height (layout, PANGO_SCALE * dy);
2795 pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_END);
2798 cairo_move_to (cr, day_rect.x, y_loc);
2799 pango_cairo_show_layout (cr, layout);
2802 if (gtk_widget_has_focus (widget)
2803 && priv->focus_row == row && priv->focus_col == col)
2807 if (priv->selected_day == day)
2808 state = gtk_widget_has_focus (widget) ? GTK_STATE_SELECTED : GTK_STATE_ACTIVE;
2810 state = GTK_STATE_NORMAL;
2812 gtk_paint_focus (style, cr,
2813 state, widget, "calendar-day",
2814 day_rect.x, day_rect.y,
2815 day_rect.width, day_rect.height);
2819 priv->detail_overflow[row] |= (1 << col);
2821 priv->detail_overflow[row] &= ~(1 << col);
2823 g_object_unref (layout);
2828 calendar_paint_main (GtkCalendar *calendar,
2834 gtk_cairo_transform_to_window (cr,
2835 GTK_WIDGET (calendar),
2836 calendar->priv->main_win);
2838 for (col = 0; col < 7; col++)
2839 for (row = 0; row < 6; row++)
2840 calendar_paint_day (calendar, cr, row, col);
2846 calendar_invalidate_arrow (GtkCalendar *calendar,
2849 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2852 window = priv->arrow_win[arrow];
2854 gdk_window_invalidate_rect (window, NULL, FALSE);
2858 calendar_paint_arrow (GtkCalendar *calendar,
2862 GtkWidget *widget = GTK_WIDGET (calendar);
2863 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
2867 gtk_cairo_transform_to_window (cr, widget, priv->arrow_win[arrow]);
2869 window = priv->arrow_win[arrow];
2875 style = gtk_widget_get_style (widget);
2876 state = priv->arrow_state[arrow];
2878 gdk_cairo_set_source_color (cr, &style->bg[state]);
2881 if (arrow == ARROW_MONTH_LEFT || arrow == ARROW_YEAR_LEFT)
2882 gtk_paint_arrow (style, cr, state,
2883 GTK_SHADOW_OUT, widget, "calendar",
2884 GTK_ARROW_LEFT, TRUE,
2885 gdk_window_get_width (window) / 2 - 3,
2886 gdk_window_get_height (window) / 2 - 4,
2889 gtk_paint_arrow (style, cr, state,
2890 GTK_SHADOW_OUT, widget, "calendar",
2891 GTK_ARROW_RIGHT, TRUE,
2892 gdk_window_get_width (window) / 2 - 4,
2893 gdk_window_get_height (window) / 2 - 4,
2901 gtk_calendar_draw (GtkWidget *widget,
2904 GtkCalendar *calendar = GTK_CALENDAR (widget);
2905 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
2908 if (gtk_cairo_should_draw_window (cr, gtk_widget_get_window (widget)))
2910 gtk_paint_shadow (gtk_widget_get_style (widget), cr,
2911 gtk_widget_get_state (widget), GTK_SHADOW_IN,
2914 gtk_widget_get_allocated_width (widget),
2915 gtk_widget_get_allocated_height (widget));
2918 if (gtk_cairo_should_draw_window (cr, priv->main_win))
2919 calendar_paint_main (calendar, cr);
2921 if (priv->header_win &&
2922 gtk_cairo_should_draw_window (cr, priv->header_win))
2923 calendar_paint_header (calendar, cr);
2925 for (i = 0; i < 4; i++)
2926 if (priv->arrow_win[i] &&
2927 gtk_cairo_should_draw_window (cr, priv->arrow_win[i]))
2928 calendar_paint_arrow (calendar, cr, i);
2930 if (priv->day_name_win &&
2931 gtk_cairo_should_draw_window (cr, priv->day_name_win))
2932 calendar_paint_day_names (calendar, cr);
2934 if (priv->week_win &&
2935 gtk_cairo_should_draw_window (cr, priv->week_win))
2936 calendar_paint_week_numbers (calendar, cr);
2942 /****************************************
2944 ****************************************/
2947 calendar_arrow_action (GtkCalendar *calendar,
2952 case ARROW_YEAR_LEFT:
2953 calendar_set_year_prev (calendar);
2955 case ARROW_YEAR_RIGHT:
2956 calendar_set_year_next (calendar);
2958 case ARROW_MONTH_LEFT:
2959 calendar_set_month_prev (calendar);
2961 case ARROW_MONTH_RIGHT:
2962 calendar_set_month_next (calendar);
2970 calendar_timer (gpointer data)
2972 GtkCalendar *calendar = data;
2973 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2974 gboolean retval = FALSE;
2978 calendar_arrow_action (calendar, priv->click_child);
2980 if (priv->need_timer)
2982 GtkSettings *settings;
2985 settings = gtk_widget_get_settings (GTK_WIDGET (calendar));
2986 g_object_get (settings, "gtk-timeout-repeat", &timeout, NULL);
2988 priv->need_timer = FALSE;
2989 priv->timer = gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT_IDLE,
2990 timeout * SCROLL_DELAY_FACTOR,
2991 (GSourceFunc) calendar_timer,
2992 (gpointer) calendar, NULL);
3002 calendar_start_spinning (GtkCalendar *calendar,
3005 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
3007 priv->click_child = click_child;
3011 GtkSettings *settings;
3014 settings = gtk_widget_get_settings (GTK_WIDGET (calendar));
3015 g_object_get (settings, "gtk-timeout-initial", &timeout, NULL);
3017 priv->need_timer = TRUE;
3018 priv->timer = gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT_IDLE,
3020 (GSourceFunc) calendar_timer,
3021 (gpointer) calendar, NULL);
3026 calendar_stop_spinning (GtkCalendar *calendar)
3028 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
3032 g_source_remove (priv->timer);
3034 priv->need_timer = FALSE;
3039 calendar_main_button_press (GtkCalendar *calendar,
3040 GdkEventButton *event)
3042 GtkWidget *widget = GTK_WIDGET (calendar);
3043 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
3049 x = (gint) (event->x);
3050 y = (gint) (event->y);
3052 row = calendar_row_from_y (calendar, y);
3053 col = calendar_column_from_x (calendar, x);
3055 /* If row or column isn't found, just return. */
3056 if (row == -1 || col == -1)
3059 day_month = priv->day_month[row][col];
3061 if (event->type == GDK_BUTTON_PRESS)
3063 day = priv->day[row][col];
3065 if (day_month == MONTH_PREV)
3066 calendar_set_month_prev (calendar);
3067 else if (day_month == MONTH_NEXT)
3068 calendar_set_month_next (calendar);
3070 if (!gtk_widget_has_focus (widget))
3071 gtk_widget_grab_focus (widget);
3073 if (event->button == 1)
3076 priv->drag_start_x = x;
3077 priv->drag_start_y = y;
3080 calendar_select_and_focus_day (calendar, day);
3082 else if (event->type == GDK_2BUTTON_PRESS)
3085 if (day_month == MONTH_CURRENT)
3086 g_signal_emit (calendar,
3087 gtk_calendar_signals[DAY_SELECTED_DOUBLE_CLICK_SIGNAL],
3093 gtk_calendar_button_press (GtkWidget *widget,
3094 GdkEventButton *event)
3096 GtkCalendar *calendar = GTK_CALENDAR (widget);
3097 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3100 if (event->window == priv->main_win)
3101 calendar_main_button_press (calendar, event);
3103 if (!gtk_widget_has_focus (widget))
3104 gtk_widget_grab_focus (widget);
3106 for (arrow = ARROW_YEAR_LEFT; arrow <= ARROW_MONTH_RIGHT; arrow++)
3108 if (event->window == priv->arrow_win[arrow])
3111 /* only call the action on single click, not double */
3112 if (event->type == GDK_BUTTON_PRESS)
3114 if (event->button == 1)
3115 calendar_start_spinning (calendar, arrow);
3117 calendar_arrow_action (calendar, arrow);
3128 gtk_calendar_button_release (GtkWidget *widget,
3129 GdkEventButton *event)
3131 GtkCalendar *calendar = GTK_CALENDAR (widget);
3132 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3134 if (event->button == 1)
3136 calendar_stop_spinning (calendar);
3146 gtk_calendar_motion_notify (GtkWidget *widget,
3147 GdkEventMotion *event)
3149 GtkCalendar *calendar = GTK_CALENDAR (widget);
3150 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3151 gint event_x, event_y;
3153 gint old_row, old_col;
3155 event_x = (gint) (event->x);
3156 event_y = (gint) (event->y);
3158 if (event->window == priv->main_win)
3163 if (gtk_drag_check_threshold (widget,
3164 priv->drag_start_x, priv->drag_start_y,
3165 event->x, event->y))
3167 GdkDragContext *context;
3168 GtkTargetList *target_list = gtk_target_list_new (NULL, 0);
3169 gtk_target_list_add_text_targets (target_list, 0);
3170 context = gtk_drag_begin (widget, target_list, GDK_ACTION_COPY,
3171 1, (GdkEvent *)event);
3176 gtk_target_list_unref (target_list);
3177 gtk_drag_set_icon_default (context);
3182 row = calendar_row_from_y (calendar, event_y);
3183 col = calendar_column_from_x (calendar, event_x);
3185 if (row != priv->highlight_row || priv->highlight_col != col)
3187 old_row = priv->highlight_row;
3188 old_col = priv->highlight_col;
3189 if (old_row > -1 && old_col > -1)
3191 priv->highlight_row = -1;
3192 priv->highlight_col = -1;
3193 calendar_invalidate_day (calendar, old_row, old_col);
3196 priv->highlight_row = row;
3197 priv->highlight_col = col;
3199 if (row > -1 && col > -1)
3200 calendar_invalidate_day (calendar, row, col);
3208 gtk_calendar_enter_notify (GtkWidget *widget,
3209 GdkEventCrossing *event)
3211 GtkCalendar *calendar = GTK_CALENDAR (widget);
3212 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3214 if (event->window == priv->arrow_win[ARROW_MONTH_LEFT])
3216 priv->arrow_state[ARROW_MONTH_LEFT] = GTK_STATE_PRELIGHT;
3217 calendar_invalidate_arrow (calendar, ARROW_MONTH_LEFT);
3220 if (event->window == priv->arrow_win[ARROW_MONTH_RIGHT])
3222 priv->arrow_state[ARROW_MONTH_RIGHT] = GTK_STATE_PRELIGHT;
3223 calendar_invalidate_arrow (calendar, ARROW_MONTH_RIGHT);
3226 if (event->window == priv->arrow_win[ARROW_YEAR_LEFT])
3228 priv->arrow_state[ARROW_YEAR_LEFT] = GTK_STATE_PRELIGHT;
3229 calendar_invalidate_arrow (calendar, ARROW_YEAR_LEFT);
3232 if (event->window == priv->arrow_win[ARROW_YEAR_RIGHT])
3234 priv->arrow_state[ARROW_YEAR_RIGHT] = GTK_STATE_PRELIGHT;
3235 calendar_invalidate_arrow (calendar, ARROW_YEAR_RIGHT);
3242 gtk_calendar_leave_notify (GtkWidget *widget,
3243 GdkEventCrossing *event)
3245 GtkCalendar *calendar = GTK_CALENDAR (widget);
3246 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3250 if (event->window == priv->main_win)
3252 row = priv->highlight_row;
3253 col = priv->highlight_col;
3254 priv->highlight_row = -1;
3255 priv->highlight_col = -1;
3256 if (row > -1 && col > -1)
3257 calendar_invalidate_day (calendar, row, col);
3260 if (event->window == priv->arrow_win[ARROW_MONTH_LEFT])
3262 priv->arrow_state[ARROW_MONTH_LEFT] = GTK_STATE_NORMAL;
3263 calendar_invalidate_arrow (calendar, ARROW_MONTH_LEFT);
3266 if (event->window == priv->arrow_win[ARROW_MONTH_RIGHT])
3268 priv->arrow_state[ARROW_MONTH_RIGHT] = GTK_STATE_NORMAL;
3269 calendar_invalidate_arrow (calendar, ARROW_MONTH_RIGHT);
3272 if (event->window == priv->arrow_win[ARROW_YEAR_LEFT])
3274 priv->arrow_state[ARROW_YEAR_LEFT] = GTK_STATE_NORMAL;
3275 calendar_invalidate_arrow (calendar, ARROW_YEAR_LEFT);
3278 if (event->window == priv->arrow_win[ARROW_YEAR_RIGHT])
3280 priv->arrow_state[ARROW_YEAR_RIGHT] = GTK_STATE_NORMAL;
3281 calendar_invalidate_arrow (calendar, ARROW_YEAR_RIGHT);
3288 gtk_calendar_scroll (GtkWidget *widget,
3289 GdkEventScroll *event)
3291 GtkCalendar *calendar = GTK_CALENDAR (widget);
3293 if (event->direction == GDK_SCROLL_UP)
3295 if (!gtk_widget_has_focus (widget))
3296 gtk_widget_grab_focus (widget);
3297 calendar_set_month_prev (calendar);
3299 else if (event->direction == GDK_SCROLL_DOWN)
3301 if (!gtk_widget_has_focus (widget))
3302 gtk_widget_grab_focus (widget);
3303 calendar_set_month_next (calendar);
3312 /****************************************
3314 ****************************************/
3317 move_focus (GtkCalendar *calendar,
3320 GtkCalendarPrivate *priv = calendar->priv;
3321 GtkTextDirection text_dir = gtk_widget_get_direction (GTK_WIDGET (calendar));
3323 if ((text_dir == GTK_TEXT_DIR_LTR && direction == -1) ||
3324 (text_dir == GTK_TEXT_DIR_RTL && direction == 1))
3326 if (priv->focus_col > 0)
3328 else if (priv->focus_row > 0)
3330 priv->focus_col = 6;
3334 if (priv->focus_col < 0)
3335 priv->focus_col = 6;
3336 if (priv->focus_row < 0)
3337 priv->focus_row = 5;
3341 if (priv->focus_col < 6)
3343 else if (priv->focus_row < 5)
3345 priv->focus_col = 0;
3349 if (priv->focus_col < 0)
3350 priv->focus_col = 0;
3351 if (priv->focus_row < 0)
3352 priv->focus_row = 0;
3357 gtk_calendar_key_press (GtkWidget *widget,
3360 GtkCalendar *calendar = GTK_CALENDAR (widget);
3361 GtkCalendarPrivate *priv = calendar->priv;
3369 old_focus_row = priv->focus_row;
3370 old_focus_col = priv->focus_col;
3372 switch (event->keyval)
3374 case GDK_KEY_KP_Left:
3377 if (event->state & GDK_CONTROL_MASK)
3378 calendar_set_month_prev (calendar);
3381 move_focus (calendar, -1);
3382 calendar_invalidate_day (calendar, old_focus_row, old_focus_col);
3383 calendar_invalidate_day (calendar, priv->focus_row,
3387 case GDK_KEY_KP_Right:
3390 if (event->state & GDK_CONTROL_MASK)
3391 calendar_set_month_next (calendar);
3394 move_focus (calendar, 1);
3395 calendar_invalidate_day (calendar, old_focus_row, old_focus_col);
3396 calendar_invalidate_day (calendar, priv->focus_row,
3403 if (event->state & GDK_CONTROL_MASK)
3404 calendar_set_year_prev (calendar);
3407 if (priv->focus_row > 0)
3409 if (priv->focus_row < 0)
3410 priv->focus_row = 5;
3411 if (priv->focus_col < 0)
3412 priv->focus_col = 6;
3413 calendar_invalidate_day (calendar, old_focus_row, old_focus_col);
3414 calendar_invalidate_day (calendar, priv->focus_row,
3418 case GDK_KEY_KP_Down:
3421 if (event->state & GDK_CONTROL_MASK)
3422 calendar_set_year_next (calendar);
3425 if (priv->focus_row < 5)
3427 if (priv->focus_col < 0)
3428 priv->focus_col = 0;
3429 calendar_invalidate_day (calendar, old_focus_row, old_focus_col);
3430 calendar_invalidate_day (calendar, priv->focus_row,
3434 case GDK_KEY_KP_Space:
3436 row = priv->focus_row;
3437 col = priv->focus_col;
3439 if (row > -1 && col > -1)
3443 day = priv->day[row][col];
3444 if (priv->day_month[row][col] == MONTH_PREV)
3445 calendar_set_month_prev (calendar);
3446 else if (priv->day_month[row][col] == MONTH_NEXT)
3447 calendar_set_month_next (calendar);
3449 calendar_select_and_focus_day (calendar, day);
3457 /****************************************
3458 * Misc widget methods *
3459 ****************************************/
3462 calendar_set_background (GtkWidget *widget)
3464 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3468 if (gtk_widget_get_realized (widget))
3470 for (i = 0; i < 4; i++)
3472 if (priv->arrow_win[i])
3473 gdk_window_set_background (priv->arrow_win[i],
3474 HEADER_BG_COLOR (widget));
3476 if (priv->header_win)
3477 gdk_window_set_background (priv->header_win,
3478 HEADER_BG_COLOR (widget));
3479 if (priv->day_name_win)
3480 gdk_window_set_background (priv->day_name_win,
3481 BACKGROUND_COLOR (widget));
3483 gdk_window_set_background (priv->week_win,
3484 BACKGROUND_COLOR (widget));
3486 gdk_window_set_background (priv->main_win,
3487 BACKGROUND_COLOR (widget));
3489 window = gtk_widget_get_window (widget);
3491 gdk_window_set_background (window,
3492 BACKGROUND_COLOR (widget));
3497 gtk_calendar_style_set (GtkWidget *widget,
3498 GtkStyle *previous_style)
3500 if (previous_style && gtk_widget_get_realized (widget))
3501 calendar_set_background (widget);
3505 gtk_calendar_state_changed (GtkWidget *widget,
3506 GtkStateType previous_state)
3508 GtkCalendar *calendar = GTK_CALENDAR (widget);
3509 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3512 if (!gtk_widget_is_sensitive (widget))
3515 calendar_stop_spinning (calendar);
3518 for (i = 0; i < 4; i++)
3519 if (gtk_widget_is_sensitive (widget))
3520 priv->arrow_state[i] = GTK_STATE_NORMAL;
3522 priv->arrow_state[i] = GTK_STATE_INSENSITIVE;
3524 calendar_set_background (widget);
3528 gtk_calendar_grab_notify (GtkWidget *widget,
3529 gboolean was_grabbed)
3532 calendar_stop_spinning (GTK_CALENDAR (widget));
3536 gtk_calendar_focus_out (GtkWidget *widget,
3537 GdkEventFocus *event)
3539 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3540 GtkCalendar *calendar = GTK_CALENDAR (widget);
3542 calendar_queue_refresh (calendar);
3543 calendar_stop_spinning (calendar);
3551 /****************************************
3553 ****************************************/
3556 gtk_calendar_drag_data_get (GtkWidget *widget,
3557 GdkDragContext *context,
3558 GtkSelectionData *selection_data,
3562 GtkCalendar *calendar = GTK_CALENDAR (widget);
3563 GtkCalendarPrivate *priv = calendar->priv;
3568 date = g_date_new_dmy (priv->selected_day, priv->month + 1, priv->year);
3569 len = g_date_strftime (str, 127, "%x", date);
3570 gtk_selection_data_set_text (selection_data, str, len);
3575 /* Get/set whether drag_motion requested the drag data and
3576 * drag_data_received should thus not actually insert the data,
3577 * since the data doesn't result from a drop.
3580 set_status_pending (GdkDragContext *context,
3581 GdkDragAction suggested_action)
3583 g_object_set_data (G_OBJECT (context),
3584 I_("gtk-calendar-status-pending"),
3585 GINT_TO_POINTER (suggested_action));
3588 static GdkDragAction
3589 get_status_pending (GdkDragContext *context)
3591 return GPOINTER_TO_INT (g_object_get_data (G_OBJECT (context),
3592 "gtk-calendar-status-pending"));
3596 gtk_calendar_drag_leave (GtkWidget *widget,
3597 GdkDragContext *context,
3600 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3602 priv->drag_highlight = 0;
3603 gtk_drag_unhighlight (widget);
3608 gtk_calendar_drag_motion (GtkWidget *widget,
3609 GdkDragContext *context,
3614 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3617 if (!priv->drag_highlight)
3619 priv->drag_highlight = 1;
3620 gtk_drag_highlight (widget);
3623 target = gtk_drag_dest_find_target (widget, context, NULL);
3624 if (target == GDK_NONE || context->suggested_action == 0)
3625 gdk_drag_status (context, 0, time);
3628 set_status_pending (context, context->suggested_action);
3629 gtk_drag_get_data (widget, context, target, time);
3636 gtk_calendar_drag_drop (GtkWidget *widget,
3637 GdkDragContext *context,
3644 target = gtk_drag_dest_find_target (widget, context, NULL);
3645 if (target != GDK_NONE)
3647 gtk_drag_get_data (widget, context,
3657 gtk_calendar_drag_data_received (GtkWidget *widget,
3658 GdkDragContext *context,
3661 GtkSelectionData *selection_data,
3665 GtkCalendar *calendar = GTK_CALENDAR (widget);
3666 GtkCalendarPrivate *priv = calendar->priv;
3667 guint day, month, year;
3670 GdkDragAction suggested_action;
3672 suggested_action = get_status_pending (context);
3674 if (suggested_action)
3676 set_status_pending (context, 0);
3678 /* We are getting this data due to a request in drag_motion,
3679 * rather than due to a request in drag_drop, so we are just
3680 * supposed to call drag_status, not actually paste in the
3683 str = (gchar*) gtk_selection_data_get_text (selection_data);
3687 date = g_date_new ();
3688 g_date_set_parse (date, str);
3689 if (!g_date_valid (date))
3690 suggested_action = 0;
3695 suggested_action = 0;
3697 gdk_drag_status (context, suggested_action, time);
3702 date = g_date_new ();
3703 str = (gchar*) gtk_selection_data_get_text (selection_data);
3706 g_date_set_parse (date, str);
3710 if (!g_date_valid (date))
3712 g_warning ("Received invalid date data\n");
3714 gtk_drag_finish (context, FALSE, FALSE, time);
3718 day = g_date_get_day (date);
3719 month = g_date_get_month (date);
3720 year = g_date_get_year (date);
3723 gtk_drag_finish (context, TRUE, FALSE, time);
3726 g_object_freeze_notify (G_OBJECT (calendar));
3727 if (!(priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
3728 && (priv->display_flags & GTK_CALENDAR_SHOW_HEADING))
3729 gtk_calendar_select_month (calendar, month - 1, year);
3730 gtk_calendar_select_day (calendar, day);
3731 g_object_thaw_notify (G_OBJECT (calendar));
3735 /****************************************
3737 ****************************************/
3742 * Creates a new calendar, with the current date being selected.
3744 * Return value: a newly #GtkCalendar widget
3747 gtk_calendar_new (void)
3749 return g_object_new (GTK_TYPE_CALENDAR, NULL);
3753 * gtk_calendar_get_display_options:
3754 * @calendar: a #GtkCalendar
3756 * Returns the current display options of @calendar.
3758 * Return value: the display options.
3762 GtkCalendarDisplayOptions
3763 gtk_calendar_get_display_options (GtkCalendar *calendar)
3765 g_return_val_if_fail (GTK_IS_CALENDAR (calendar), 0);
3767 return calendar->priv->display_flags;
3771 * gtk_calendar_set_display_options:
3772 * @calendar: a #GtkCalendar
3773 * @flags: the display options to set
3775 * Sets display options (whether to display the heading and the month
3781 gtk_calendar_set_display_options (GtkCalendar *calendar,
3782 GtkCalendarDisplayOptions flags)
3784 GtkWidget *widget = GTK_WIDGET (calendar);
3785 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
3788 GtkCalendarDisplayOptions old_flags;
3790 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3792 old_flags = priv->display_flags;
3794 if (gtk_widget_get_realized (widget))
3796 if ((flags ^ priv->display_flags) & GTK_CALENDAR_NO_MONTH_CHANGE)
3799 if (! (flags & GTK_CALENDAR_NO_MONTH_CHANGE)
3800 && (priv->header_win))
3802 priv->display_flags &= ~GTK_CALENDAR_NO_MONTH_CHANGE;
3803 calendar_realize_arrows (calendar);
3807 for (i = 0; i < 4; i++)
3809 if (priv->arrow_win[i])
3811 gdk_window_set_user_data (priv->arrow_win[i],
3813 gdk_window_destroy (priv->arrow_win[i]);
3814 priv->arrow_win[i] = NULL;
3820 if ((flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_HEADING)
3824 if (flags & GTK_CALENDAR_SHOW_HEADING)
3826 priv->display_flags |= GTK_CALENDAR_SHOW_HEADING;
3827 calendar_realize_header (calendar);
3831 for (i = 0; i < 4; i++)
3833 if (priv->arrow_win[i])
3835 gdk_window_set_user_data (priv->arrow_win[i],
3837 gdk_window_destroy (priv->arrow_win[i]);
3838 priv->arrow_win[i] = NULL;
3841 gdk_window_set_user_data (priv->header_win, NULL);
3842 gdk_window_destroy (priv->header_win);
3843 priv->header_win = NULL;
3848 if ((flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_DAY_NAMES)
3852 if (flags & GTK_CALENDAR_SHOW_DAY_NAMES)
3854 priv->display_flags |= GTK_CALENDAR_SHOW_DAY_NAMES;
3855 calendar_realize_day_names (calendar);
3859 gdk_window_set_user_data (priv->day_name_win, NULL);
3860 gdk_window_destroy (priv->day_name_win);
3861 priv->day_name_win = NULL;
3865 if ((flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
3869 if (flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
3871 priv->display_flags |= GTK_CALENDAR_SHOW_WEEK_NUMBERS;
3872 calendar_realize_week_numbers (calendar);
3876 gdk_window_set_user_data (priv->week_win, NULL);
3877 gdk_window_destroy (priv->week_win);
3878 priv->week_win = NULL;
3882 if ((flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_DETAILS)
3885 priv->display_flags = flags;
3887 gtk_widget_queue_resize (GTK_WIDGET (calendar));
3890 priv->display_flags = flags;
3892 g_object_freeze_notify (G_OBJECT (calendar));
3893 if ((old_flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_HEADING)
3894 g_object_notify (G_OBJECT (calendar), "show-heading");
3895 if ((old_flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_DAY_NAMES)
3896 g_object_notify (G_OBJECT (calendar), "show-day-names");
3897 if ((old_flags ^ priv->display_flags) & GTK_CALENDAR_NO_MONTH_CHANGE)
3898 g_object_notify (G_OBJECT (calendar), "no-month-change");
3899 if ((old_flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
3900 g_object_notify (G_OBJECT (calendar), "show-week-numbers");
3901 g_object_thaw_notify (G_OBJECT (calendar));
3905 * gtk_calendar_select_month:
3906 * @calendar: a #GtkCalendar
3907 * @month: a month number between 0 and 11.
3908 * @year: the year the month is in.
3910 * Shifts the calendar to a different month.
3913 gtk_calendar_select_month (GtkCalendar *calendar,
3917 GtkCalendarPrivate *priv;
3919 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3920 g_return_if_fail (month <= 11);
3922 priv = calendar->priv;
3924 priv->month = month;
3927 calendar_compute_days (calendar);
3928 calendar_queue_refresh (calendar);
3930 g_object_freeze_notify (G_OBJECT (calendar));
3931 g_object_notify (G_OBJECT (calendar), "month");
3932 g_object_notify (G_OBJECT (calendar), "year");
3933 g_object_thaw_notify (G_OBJECT (calendar));
3935 g_signal_emit (calendar,
3936 gtk_calendar_signals[MONTH_CHANGED_SIGNAL],
3941 * gtk_calendar_select_day:
3942 * @calendar: a #GtkCalendar.
3943 * @day: the day number between 1 and 31, or 0 to unselect
3944 * the currently selected day.
3946 * Selects a day from the current month.
3949 gtk_calendar_select_day (GtkCalendar *calendar,
3952 GtkCalendarPrivate *priv;
3954 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3955 g_return_if_fail (day <= 31);
3957 priv = calendar->priv;
3959 /* Deselect the old day */
3960 if (priv->selected_day > 0)
3964 selected_day = priv->selected_day;
3965 priv->selected_day = 0;
3966 if (gtk_widget_is_drawable (GTK_WIDGET (calendar)))
3967 calendar_invalidate_day_num (calendar, selected_day);
3970 priv->selected_day = day;
3972 /* Select the new day */
3975 if (gtk_widget_is_drawable (GTK_WIDGET (calendar)))
3976 calendar_invalidate_day_num (calendar, day);
3979 g_object_notify (G_OBJECT (calendar), "day");
3981 g_signal_emit (calendar,
3982 gtk_calendar_signals[DAY_SELECTED_SIGNAL],
3987 * gtk_calendar_clear_marks:
3988 * @calendar: a #GtkCalendar
3990 * Remove all visual markers.
3993 gtk_calendar_clear_marks (GtkCalendar *calendar)
3995 GtkCalendarPrivate *priv;
3998 g_return_if_fail (GTK_IS_CALENDAR (calendar));
4000 priv = calendar->priv;
4002 for (day = 0; day < 31; day++)
4004 priv->marked_date[day] = FALSE;
4007 priv->num_marked_dates = 0;
4008 calendar_queue_refresh (calendar);
4012 * gtk_calendar_mark_day:
4013 * @calendar: a #GtkCalendar
4014 * @day: the day number to mark between 1 and 31.
4016 * Places a visual marker on a particular day.
4019 gtk_calendar_mark_day (GtkCalendar *calendar,
4022 GtkCalendarPrivate *priv;
4024 g_return_if_fail (GTK_IS_CALENDAR (calendar));
4026 priv = calendar->priv;
4028 if (day >= 1 && day <= 31 && !priv->marked_date[day-1])
4030 priv->marked_date[day - 1] = TRUE;
4031 priv->num_marked_dates++;
4032 calendar_invalidate_day_num (calendar, day);
4037 * gtk_calendar_get_day_is_marked:
4038 * @calendar: a #GtkCalendar
4039 * @day: the day number between 1 and 31.
4041 * Returns if the @day of the @calendar is already marked.
4043 * Returns: whether the day is marked.
4048 gtk_calendar_get_day_is_marked (GtkCalendar *calendar,
4051 GtkCalendarPrivate *priv;
4053 g_return_val_if_fail (GTK_IS_CALENDAR (calendar), FALSE);
4055 priv = calendar->priv;
4057 if (day >= 1 && day <= 31)
4058 return priv->marked_date[day - 1];
4064 * gtk_calendar_unmark_day:
4065 * @calendar: a #GtkCalendar.
4066 * @day: the day number to unmark between 1 and 31.
4068 * Removes the visual marker from a particular day.
4071 gtk_calendar_unmark_day (GtkCalendar *calendar,
4074 GtkCalendarPrivate *priv;
4076 g_return_if_fail (GTK_IS_CALENDAR (calendar));
4078 priv = calendar->priv;
4080 if (day >= 1 && day <= 31 && priv->marked_date[day-1])
4082 priv->marked_date[day - 1] = FALSE;
4083 priv->num_marked_dates--;
4084 calendar_invalidate_day_num (calendar, day);
4089 * gtk_calendar_get_date:
4090 * @calendar: a #GtkCalendar
4091 * @year: (allow-none): location to store the year number, or %NULL
4092 * @month: (allow-none): location to store the month number (between 0 and 11), or %NULL
4093 * @day: (allow-none): location to store the day number (between 1 and 31), or %NULL
4095 * Obtains the selected date from a #GtkCalendar.
4098 gtk_calendar_get_date (GtkCalendar *calendar,
4103 GtkCalendarPrivate *priv;
4105 g_return_if_fail (GTK_IS_CALENDAR (calendar));
4107 priv = calendar->priv;
4113 *month = priv->month;
4116 *day = priv->selected_day;
4120 * gtk_calendar_set_detail_func:
4121 * @calendar: a #GtkCalendar.
4122 * @func: a function providing details for each day.
4123 * @data: data to pass to @func invokations.
4124 * @destroy: a function for releasing @data.
4126 * Installs a function which provides Pango markup with detail information
4127 * for each day. Examples for such details are holidays or appointments. That
4128 * information is shown below each day when #GtkCalendar:show-details is set.
4129 * A tooltip containing with full detail information is provided, if the entire
4130 * text should not fit into the details area, or if #GtkCalendar:show-details
4133 * The size of the details area can be restricted by setting the
4134 * #GtkCalendar:detail-width-chars and #GtkCalendar:detail-height-rows
4140 gtk_calendar_set_detail_func (GtkCalendar *calendar,
4141 GtkCalendarDetailFunc func,
4143 GDestroyNotify destroy)
4145 GtkCalendarPrivate *priv;
4147 g_return_if_fail (GTK_IS_CALENDAR (calendar));
4149 priv = GTK_CALENDAR_GET_PRIVATE (calendar);
4151 if (priv->detail_func_destroy)
4152 priv->detail_func_destroy (priv->detail_func_user_data);
4154 priv->detail_func = func;
4155 priv->detail_func_user_data = data;
4156 priv->detail_func_destroy = destroy;
4158 gtk_widget_set_has_tooltip (GTK_WIDGET (calendar),
4159 NULL != priv->detail_func);
4160 gtk_widget_queue_resize (GTK_WIDGET (calendar));
4164 * gtk_calendar_set_detail_width_chars:
4165 * @calendar: a #GtkCalendar.
4166 * @chars: detail width in characters.
4168 * Updates the width of detail cells.
4169 * See #GtkCalendar:detail-width-chars.
4174 gtk_calendar_set_detail_width_chars (GtkCalendar *calendar,
4177 GtkCalendarPrivate *priv;
4179 g_return_if_fail (GTK_IS_CALENDAR (calendar));
4181 priv = GTK_CALENDAR_GET_PRIVATE (calendar);
4183 if (chars != priv->detail_width_chars)
4185 priv->detail_width_chars = chars;
4186 g_object_notify (G_OBJECT (calendar), "detail-width-chars");
4187 gtk_widget_queue_resize_no_redraw (GTK_WIDGET (calendar));
4192 * gtk_calendar_set_detail_height_rows:
4193 * @calendar: a #GtkCalendar.
4194 * @rows: detail height in rows.
4196 * Updates the height of detail cells.
4197 * See #GtkCalendar:detail-height-rows.
4202 gtk_calendar_set_detail_height_rows (GtkCalendar *calendar,
4205 GtkCalendarPrivate *priv;
4207 g_return_if_fail (GTK_IS_CALENDAR (calendar));
4209 priv = GTK_CALENDAR_GET_PRIVATE (calendar);
4211 if (rows != priv->detail_height_rows)
4213 priv->detail_height_rows = rows;
4214 g_object_notify (G_OBJECT (calendar), "detail-height-rows");
4215 gtk_widget_queue_resize_no_redraw (GTK_WIDGET (calendar));
4220 * gtk_calendar_get_detail_width_chars:
4221 * @calendar: a #GtkCalendar.
4223 * Queries the width of detail cells, in characters.
4224 * See #GtkCalendar:detail-width-chars.
4228 * Return value: The width of detail cells, in characters.
4231 gtk_calendar_get_detail_width_chars (GtkCalendar *calendar)
4233 g_return_val_if_fail (GTK_IS_CALENDAR (calendar), 0);
4234 return GTK_CALENDAR_GET_PRIVATE (calendar)->detail_width_chars;
4238 * gtk_calendar_get_detail_height_rows:
4239 * @calendar: a #GtkCalendar.
4241 * Queries the height of detail cells, in rows.
4242 * See #GtkCalendar:detail-width-chars.
4246 * Return value: The height of detail cells, in rows.
4249 gtk_calendar_get_detail_height_rows (GtkCalendar *calendar)
4251 g_return_val_if_fail (GTK_IS_CALENDAR (calendar), 0);
4252 return GTK_CALENDAR_GET_PRIVATE (calendar)->detail_height_rows;