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) (& (widget)->style->fg[gtk_widget_get_state (widget)])
206 #define HEADER_BG_COLOR(widget) (& (widget)->style->bg[gtk_widget_get_state (widget)])
207 #define SELECTED_BG_COLOR(widget) (& (widget)->style->base[gtk_widget_has_focus (widget) ? GTK_STATE_SELECTED : GTK_STATE_ACTIVE])
208 #define SELECTED_FG_COLOR(widget) (& (widget)->style->text[gtk_widget_has_focus (widget) ? GTK_STATE_SELECTED : GTK_STATE_ACTIVE])
209 #define NORMAL_DAY_COLOR(widget) (& (widget)->style->text[gtk_widget_get_state (widget)])
210 #define PREV_MONTH_COLOR(widget) (& (widget)->style->mid[gtk_widget_get_state (widget)])
211 #define NEXT_MONTH_COLOR(widget) (& (widget)->style->mid[gtk_widget_get_state (widget)])
212 #define MARKED_COLOR(widget) (& (widget)->style->text[gtk_widget_get_state (widget)])
213 #define BACKGROUND_COLOR(widget) (& (widget)->style->base[gtk_widget_get_state (widget)])
214 #define HIGHLIGHT_BACK_COLOR(widget) (& (widget)->style->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 GdkWindow *header_win;
260 GdkWindow *day_name_win;
263 GdkWindow *arrow_win[4];
269 guint arrow_state[4];
271 guint max_month_width;
272 guint max_year_width;
278 guint max_day_char_width;
279 guint max_day_char_ascent;
280 guint max_day_char_descent;
281 guint max_label_char_ascent;
282 guint max_label_char_descent;
283 guint max_week_char_width;
286 guint year_before : 1;
288 guint need_timer : 1;
291 guint drag_highlight : 1;
301 /* Optional callback, used to display extra information for each day. */
302 GtkCalendarDetailFunc detail_func;
303 gpointer detail_func_user_data;
304 GDestroyNotify detail_func_destroy;
306 /* Size requistion for details provided by the hook. */
307 gint detail_height_rows;
308 gint detail_width_chars;
309 gint detail_overflow[6];
312 #define GTK_CALENDAR_GET_PRIVATE(widget) (GTK_CALENDAR (widget)->priv)
314 static void gtk_calendar_finalize (GObject *calendar);
315 static void gtk_calendar_destroy (GtkObject *calendar);
316 static void gtk_calendar_set_property (GObject *object,
320 static void gtk_calendar_get_property (GObject *object,
325 static void gtk_calendar_realize (GtkWidget *widget);
326 static void gtk_calendar_unrealize (GtkWidget *widget);
327 static void gtk_calendar_size_request (GtkWidget *widget,
328 GtkRequisition *requisition);
329 static void gtk_calendar_size_allocate (GtkWidget *widget,
330 GtkAllocation *allocation);
331 static gboolean gtk_calendar_expose (GtkWidget *widget,
332 GdkEventExpose *event);
333 static gboolean gtk_calendar_button_press (GtkWidget *widget,
334 GdkEventButton *event);
335 static gboolean gtk_calendar_button_release (GtkWidget *widget,
336 GdkEventButton *event);
337 static gboolean gtk_calendar_motion_notify (GtkWidget *widget,
338 GdkEventMotion *event);
339 static gboolean gtk_calendar_enter_notify (GtkWidget *widget,
340 GdkEventCrossing *event);
341 static gboolean gtk_calendar_leave_notify (GtkWidget *widget,
342 GdkEventCrossing *event);
343 static gboolean gtk_calendar_scroll (GtkWidget *widget,
344 GdkEventScroll *event);
345 static gboolean gtk_calendar_key_press (GtkWidget *widget,
347 static gboolean gtk_calendar_focus_out (GtkWidget *widget,
348 GdkEventFocus *event);
349 static void gtk_calendar_grab_notify (GtkWidget *widget,
350 gboolean was_grabbed);
351 static void gtk_calendar_state_changed (GtkWidget *widget,
352 GtkStateType previous_state);
353 static void gtk_calendar_style_set (GtkWidget *widget,
354 GtkStyle *previous_style);
355 static gboolean gtk_calendar_query_tooltip (GtkWidget *widget,
358 gboolean keyboard_mode,
359 GtkTooltip *tooltip);
361 static void gtk_calendar_drag_data_get (GtkWidget *widget,
362 GdkDragContext *context,
363 GtkSelectionData *selection_data,
366 static void gtk_calendar_drag_data_received (GtkWidget *widget,
367 GdkDragContext *context,
370 GtkSelectionData *selection_data,
373 static gboolean gtk_calendar_drag_motion (GtkWidget *widget,
374 GdkDragContext *context,
378 static void gtk_calendar_drag_leave (GtkWidget *widget,
379 GdkDragContext *context,
381 static gboolean gtk_calendar_drag_drop (GtkWidget *widget,
382 GdkDragContext *context,
387 static void calendar_start_spinning (GtkCalendar *calendar,
389 static void calendar_stop_spinning (GtkCalendar *calendar);
391 static void calendar_invalidate_day (GtkCalendar *widget,
394 static void calendar_invalidate_day_num (GtkCalendar *widget,
396 static void calendar_invalidate_arrow (GtkCalendar *widget,
399 static void calendar_compute_days (GtkCalendar *calendar);
400 static gint calendar_get_xsep (GtkCalendar *calendar);
401 static gint calendar_get_ysep (GtkCalendar *calendar);
403 static char *default_abbreviated_dayname[7];
404 static char *default_monthname[12];
406 G_DEFINE_TYPE (GtkCalendar, gtk_calendar, GTK_TYPE_WIDGET)
409 gtk_calendar_class_init (GtkCalendarClass *class)
411 GObjectClass *gobject_class;
412 GtkObjectClass *object_class;
413 GtkWidgetClass *widget_class;
415 gobject_class = (GObjectClass*) class;
416 object_class = (GtkObjectClass*) class;
417 widget_class = (GtkWidgetClass*) class;
419 gobject_class->set_property = gtk_calendar_set_property;
420 gobject_class->get_property = gtk_calendar_get_property;
421 gobject_class->finalize = gtk_calendar_finalize;
423 object_class->destroy = gtk_calendar_destroy;
425 widget_class->realize = gtk_calendar_realize;
426 widget_class->unrealize = gtk_calendar_unrealize;
427 widget_class->expose_event = gtk_calendar_expose;
428 widget_class->size_request = gtk_calendar_size_request;
429 widget_class->size_allocate = gtk_calendar_size_allocate;
430 widget_class->button_press_event = gtk_calendar_button_press;
431 widget_class->button_release_event = gtk_calendar_button_release;
432 widget_class->motion_notify_event = gtk_calendar_motion_notify;
433 widget_class->enter_notify_event = gtk_calendar_enter_notify;
434 widget_class->leave_notify_event = gtk_calendar_leave_notify;
435 widget_class->key_press_event = gtk_calendar_key_press;
436 widget_class->scroll_event = gtk_calendar_scroll;
437 widget_class->style_set = gtk_calendar_style_set;
438 widget_class->state_changed = gtk_calendar_state_changed;
439 widget_class->grab_notify = gtk_calendar_grab_notify;
440 widget_class->focus_out_event = gtk_calendar_focus_out;
441 widget_class->query_tooltip = gtk_calendar_query_tooltip;
443 widget_class->drag_data_get = gtk_calendar_drag_data_get;
444 widget_class->drag_motion = gtk_calendar_drag_motion;
445 widget_class->drag_leave = gtk_calendar_drag_leave;
446 widget_class->drag_drop = gtk_calendar_drag_drop;
447 widget_class->drag_data_received = gtk_calendar_drag_data_received;
453 * This property gets initially set to the current year.
455 g_object_class_install_property (gobject_class,
457 g_param_spec_int ("year",
459 P_("The selected year"),
461 GTK_PARAM_READWRITE));
466 * The selected month (as a number between 0 and 11).
467 * This property gets initially set to the current month.
469 g_object_class_install_property (gobject_class,
471 g_param_spec_int ("month",
473 P_("The selected month (as a number between 0 and 11)"),
475 GTK_PARAM_READWRITE));
480 * The selected day (as a number between 1 and 31, or 0
481 * to unselect the currently selected day).
482 * This property gets initially set to the current day.
484 g_object_class_install_property (gobject_class,
486 g_param_spec_int ("day",
488 P_("The selected day (as a number between 1 and 31, or 0 to unselect the currently selected day)"),
490 GTK_PARAM_READWRITE));
493 * GtkCalendar:show-heading:
495 * Determines whether a heading is displayed.
499 g_object_class_install_property (gobject_class,
501 g_param_spec_boolean ("show-heading",
503 P_("If TRUE, a heading is displayed"),
505 GTK_PARAM_READWRITE));
508 * GtkCalendar:show-day-names:
510 * Determines whether day names are displayed.
514 g_object_class_install_property (gobject_class,
516 g_param_spec_boolean ("show-day-names",
517 P_("Show Day Names"),
518 P_("If TRUE, day names are displayed"),
520 GTK_PARAM_READWRITE));
522 * GtkCalendar:no-month-change:
524 * Determines whether the selected month can be changed.
528 g_object_class_install_property (gobject_class,
529 PROP_NO_MONTH_CHANGE,
530 g_param_spec_boolean ("no-month-change",
531 P_("No Month Change"),
532 P_("If TRUE, the selected month cannot be changed"),
534 GTK_PARAM_READWRITE));
537 * GtkCalendar:show-week-numbers:
539 * Determines whether week numbers are displayed.
543 g_object_class_install_property (gobject_class,
544 PROP_SHOW_WEEK_NUMBERS,
545 g_param_spec_boolean ("show-week-numbers",
546 P_("Show Week Numbers"),
547 P_("If TRUE, week numbers are displayed"),
549 GTK_PARAM_READWRITE));
552 * GtkCalendar:detail-width-chars:
554 * Width of a detail cell, in characters.
555 * A value of 0 allows any width. See gtk_calendar_set_detail_func().
559 g_object_class_install_property (gobject_class,
560 PROP_DETAIL_WIDTH_CHARS,
561 g_param_spec_int ("detail-width-chars",
563 P_("Details width in characters"),
565 GTK_PARAM_READWRITE));
568 * GtkCalendar:detail-height-rows:
570 * Height of a detail cell, in rows.
571 * A value of 0 allows any width. See gtk_calendar_set_detail_func().
575 g_object_class_install_property (gobject_class,
576 PROP_DETAIL_HEIGHT_ROWS,
577 g_param_spec_int ("detail-height-rows",
578 P_("Details Height"),
579 P_("Details height in rows"),
581 GTK_PARAM_READWRITE));
584 * GtkCalendar:show-details:
586 * Determines whether details are shown directly in the widget, or if they are
587 * available only as tooltip. When this property is set days with details are
592 g_object_class_install_property (gobject_class,
594 g_param_spec_boolean ("show-details",
596 P_("If TRUE, details are shown"),
598 GTK_PARAM_READWRITE));
602 * GtkCalendar:inner-border
604 * The spacing around the day/week headers and main area.
606 gtk_widget_class_install_style_property (widget_class,
607 g_param_spec_int ("inner-border",
609 P_("Inner border space"),
611 GTK_PARAM_READABLE));
614 * GtkCalndar:vertical-separation
616 * Separation between day headers and main area.
618 gtk_widget_class_install_style_property (widget_class,
619 g_param_spec_int ("vertical-separation",
620 P_("Vertical separation"),
621 P_("Space between day headers and main area"),
623 GTK_PARAM_READABLE));
626 * GtkCalendar:horizontal-separation
628 * Separation between week headers and main area.
630 gtk_widget_class_install_style_property (widget_class,
631 g_param_spec_int ("horizontal-separation",
632 P_("Horizontal separation"),
633 P_("Space between week headers and main area"),
635 GTK_PARAM_READABLE));
638 * GtkCalendar::month-changed:
639 * @calendar: the object which received the signal.
641 * Emitted when the user clicks a button to change the selected month on a
644 gtk_calendar_signals[MONTH_CHANGED_SIGNAL] =
645 g_signal_new (I_("month-changed"),
646 G_OBJECT_CLASS_TYPE (gobject_class),
648 G_STRUCT_OFFSET (GtkCalendarClass, month_changed),
650 _gtk_marshal_VOID__VOID,
654 * GtkCalendar::day-selected:
655 * @calendar: the object which received the signal.
657 * Emitted when the user selects a day.
659 gtk_calendar_signals[DAY_SELECTED_SIGNAL] =
660 g_signal_new (I_("day-selected"),
661 G_OBJECT_CLASS_TYPE (gobject_class),
663 G_STRUCT_OFFSET (GtkCalendarClass, day_selected),
665 _gtk_marshal_VOID__VOID,
669 * GtkCalendar::day-selected-double-click:
670 * @calendar: the object which received the signal.
672 * Emitted when the user double-clicks a day.
674 gtk_calendar_signals[DAY_SELECTED_DOUBLE_CLICK_SIGNAL] =
675 g_signal_new (I_("day-selected-double-click"),
676 G_OBJECT_CLASS_TYPE (gobject_class),
678 G_STRUCT_OFFSET (GtkCalendarClass, day_selected_double_click),
680 _gtk_marshal_VOID__VOID,
684 * GtkCalendar::prev-month:
685 * @calendar: the object which received the signal.
687 * Emitted when the user switched to the previous month.
689 gtk_calendar_signals[PREV_MONTH_SIGNAL] =
690 g_signal_new (I_("prev-month"),
691 G_OBJECT_CLASS_TYPE (gobject_class),
693 G_STRUCT_OFFSET (GtkCalendarClass, prev_month),
695 _gtk_marshal_VOID__VOID,
699 * GtkCalendar::next-month:
700 * @calendar: the object which received the signal.
702 * Emitted when the user switched to the next month.
704 gtk_calendar_signals[NEXT_MONTH_SIGNAL] =
705 g_signal_new (I_("next-month"),
706 G_OBJECT_CLASS_TYPE (gobject_class),
708 G_STRUCT_OFFSET (GtkCalendarClass, next_month),
710 _gtk_marshal_VOID__VOID,
714 * GtkCalendar::prev-year:
715 * @calendar: the object which received the signal.
717 * Emitted when user switched to the previous year.
719 gtk_calendar_signals[PREV_YEAR_SIGNAL] =
720 g_signal_new (I_("prev-year"),
721 G_OBJECT_CLASS_TYPE (gobject_class),
723 G_STRUCT_OFFSET (GtkCalendarClass, prev_year),
725 _gtk_marshal_VOID__VOID,
729 * GtkCalendar::next-year:
730 * @calendar: the object which received the signal.
732 * Emitted when user switched to the next year.
734 gtk_calendar_signals[NEXT_YEAR_SIGNAL] =
735 g_signal_new (I_("next-year"),
736 G_OBJECT_CLASS_TYPE (gobject_class),
738 G_STRUCT_OFFSET (GtkCalendarClass, next_year),
740 _gtk_marshal_VOID__VOID,
743 g_type_class_add_private (gobject_class, sizeof (GtkCalendarPrivate));
747 gtk_calendar_init (GtkCalendar *calendar)
749 GtkWidget *widget = GTK_WIDGET (calendar);
754 wchar_t wbuffer[100];
759 GtkCalendarPrivate *priv;
761 #ifdef HAVE__NL_TIME_FIRST_WEEKDAY
762 union { unsigned int word; char *string; } langinfo;
763 gint week_1stday = 0;
764 gint first_weekday = 1;
770 priv = calendar->priv = G_TYPE_INSTANCE_GET_PRIVATE (calendar,
774 gtk_widget_set_can_focus (widget, TRUE);
776 if (!default_abbreviated_dayname[0])
780 tmp_time= (i+3)*86400;
781 strftime ( buffer, sizeof (buffer), "%a", gmtime (&tmp_time));
782 default_abbreviated_dayname[i] = g_locale_to_utf8 (buffer, -1, NULL, NULL, NULL);
784 if (!GetLocaleInfoW (GetThreadLocale (), LOCALE_SABBREVDAYNAME1 + (i+6)%7,
785 wbuffer, G_N_ELEMENTS (wbuffer)))
786 default_abbreviated_dayname[i] = g_strdup_printf ("(%d)", i);
788 default_abbreviated_dayname[i] = g_utf16_to_utf8 (wbuffer, -1, NULL, NULL, NULL);
792 if (!default_monthname[0])
797 strftime ( buffer, sizeof (buffer), "%B", gmtime (&tmp_time));
798 default_monthname[i] = g_locale_to_utf8 (buffer, -1, NULL, NULL, NULL);
800 if (!GetLocaleInfoW (GetThreadLocale (), LOCALE_SMONTHNAME1 + i,
801 wbuffer, G_N_ELEMENTS (wbuffer)))
802 default_monthname[i] = g_strdup_printf ("(%d)", i);
804 default_monthname[i] = g_utf16_to_utf8 (wbuffer, -1, NULL, NULL, NULL);
810 tm = localtime (&secs);
811 calendar->month = tm->tm_mon;
812 calendar->year = 1900 + tm->tm_year;
815 calendar->marked_date[i] = FALSE;
816 calendar->num_marked_dates = 0;
817 calendar->selected_day = tm->tm_mday;
819 calendar->display_flags = (GTK_CALENDAR_SHOW_HEADING |
820 GTK_CALENDAR_SHOW_DAY_NAMES |
821 GTK_CALENDAR_SHOW_DETAILS);
823 calendar->highlight_row = -1;
824 calendar->highlight_col = -1;
826 calendar->focus_row = -1;
827 calendar->focus_col = -1;
829 priv->max_year_width = 0;
830 priv->max_month_width = 0;
831 priv->max_day_char_width = 0;
832 priv->max_week_char_width = 0;
834 priv->max_day_char_ascent = 0;
835 priv->max_day_char_descent = 0;
836 priv->max_label_char_ascent = 0;
837 priv->max_label_char_descent = 0;
839 priv->arrow_width = 10;
841 priv->need_timer = 0;
843 priv->click_child = -1;
846 priv->drag_highlight = 0;
848 gtk_drag_dest_set (widget, 0, NULL, 0, GDK_ACTION_COPY);
849 gtk_drag_dest_add_text_targets (widget);
851 priv->year_before = 0;
853 /* Translate to calendar:YM if you want years to be displayed
854 * before months; otherwise translate to calendar:MY.
855 * Do *not* translate it to anything else, if it
856 * it isn't calendar:YM or calendar:MY it will not work.
858 * Note that the ordering described here is logical order, which is
859 * further influenced by BIDI ordering. Thus, if you have a default
860 * text direction of RTL and specify "calendar:YM", then the year
861 * will appear to the right of the month.
863 year_before = _("calendar:MY");
864 if (strcmp (year_before, "calendar:YM") == 0)
865 priv->year_before = 1;
866 else if (strcmp (year_before, "calendar:MY") != 0)
867 g_warning ("Whoever translated calendar:MY did so wrongly.\n");
870 priv->week_start = 0;
873 if (GetLocaleInfoW (GetThreadLocale (), LOCALE_IFIRSTDAYOFWEEK,
874 wbuffer, G_N_ELEMENTS (wbuffer)))
875 week_start = g_utf16_to_utf8 (wbuffer, -1, NULL, NULL, NULL);
877 if (week_start != NULL)
879 priv->week_start = (week_start[0] - '0' + 1) % 7;
883 #ifdef HAVE__NL_TIME_FIRST_WEEKDAY
884 langinfo.string = nl_langinfo (_NL_TIME_FIRST_WEEKDAY);
885 first_weekday = langinfo.string[0];
886 langinfo.string = nl_langinfo (_NL_TIME_WEEK_1STDAY);
887 week_origin = langinfo.word;
888 if (week_origin == 19971130) /* Sunday */
890 else if (week_origin == 19971201) /* Monday */
893 g_warning ("Unknown value of _NL_TIME_WEEK_1STDAY.\n");
895 priv->week_start = (week_1stday + first_weekday - 1) % 7;
897 /* Translate to calendar:week_start:0 if you want Sunday to be the
898 * first day of the week to calendar:week_start:1 if you want Monday
899 * to be the first day of the week, and so on.
901 week_start = _("calendar:week_start:0");
903 if (strncmp (week_start, "calendar:week_start:", 20) == 0)
904 priv->week_start = *(week_start + 20) - '0';
906 priv->week_start = -1;
908 if (priv->week_start < 0 || priv->week_start > 6)
910 g_warning ("Whoever translated calendar:week_start:0 did so wrongly.\n");
911 priv->week_start = 0;
916 calendar_compute_days (calendar);
920 /****************************************
921 * Utility Functions *
922 ****************************************/
925 calendar_queue_refresh (GtkCalendar *calendar)
927 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
929 if (!(priv->detail_func) ||
930 !(calendar->display_flags & GTK_CALENDAR_SHOW_DETAILS) ||
931 (priv->detail_width_chars && priv->detail_height_rows))
932 gtk_widget_queue_draw (GTK_WIDGET (calendar));
934 gtk_widget_queue_resize (GTK_WIDGET (calendar));
938 calendar_set_month_next (GtkCalendar *calendar)
942 if (calendar->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
946 if (calendar->month == 11)
954 calendar_compute_days (calendar);
955 g_signal_emit (calendar,
956 gtk_calendar_signals[NEXT_MONTH_SIGNAL],
958 g_signal_emit (calendar,
959 gtk_calendar_signals[MONTH_CHANGED_SIGNAL],
962 month_len = month_length[leap (calendar->year)][calendar->month + 1];
964 if (month_len < calendar->selected_day)
966 calendar->selected_day = 0;
967 gtk_calendar_select_day (calendar, month_len);
970 gtk_calendar_select_day (calendar, calendar->selected_day);
972 calendar_queue_refresh (calendar);
976 calendar_set_year_prev (GtkCalendar *calendar)
981 calendar_compute_days (calendar);
982 g_signal_emit (calendar,
983 gtk_calendar_signals[PREV_YEAR_SIGNAL],
985 g_signal_emit (calendar,
986 gtk_calendar_signals[MONTH_CHANGED_SIGNAL],
989 month_len = month_length[leap (calendar->year)][calendar->month + 1];
991 if (month_len < calendar->selected_day)
993 calendar->selected_day = 0;
994 gtk_calendar_select_day (calendar, month_len);
997 gtk_calendar_select_day (calendar, calendar->selected_day);
999 calendar_queue_refresh (calendar);
1003 calendar_set_year_next (GtkCalendar *calendar)
1008 calendar_compute_days (calendar);
1009 g_signal_emit (calendar,
1010 gtk_calendar_signals[NEXT_YEAR_SIGNAL],
1012 g_signal_emit (calendar,
1013 gtk_calendar_signals[MONTH_CHANGED_SIGNAL],
1016 month_len = month_length[leap (calendar->year)][calendar->month + 1];
1018 if (month_len < calendar->selected_day)
1020 calendar->selected_day = 0;
1021 gtk_calendar_select_day (calendar, month_len);
1024 gtk_calendar_select_day (calendar, calendar->selected_day);
1026 calendar_queue_refresh (calendar);
1030 calendar_compute_days (GtkCalendar *calendar)
1032 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (GTK_WIDGET (calendar));
1035 gint ndays_in_month;
1036 gint ndays_in_prev_month;
1042 year = calendar->year;
1043 month = calendar->month + 1;
1045 ndays_in_month = month_length[leap (year)][month];
1047 first_day = day_of_week (year, month, 1);
1048 first_day = (first_day + 7 - priv->week_start) % 7;
1050 /* Compute days of previous month */
1052 ndays_in_prev_month = month_length[leap (year)][month-1];
1054 ndays_in_prev_month = month_length[leap (year)][12];
1055 day = ndays_in_prev_month - first_day + 1;
1060 for (col = 0; col < first_day; col++)
1062 calendar->day[row][col] = day;
1063 calendar->day_month[row][col] = MONTH_PREV;
1068 /* Compute days of current month */
1070 for (day = 1; day <= ndays_in_month; day++)
1072 calendar->day[row][col] = day;
1073 calendar->day_month[row][col] = MONTH_CURRENT;
1083 /* Compute days of next month */
1085 for (; row <= 5; row++)
1087 for (; col <= 6; col++)
1089 calendar->day[row][col] = day;
1090 calendar->day_month[row][col] = MONTH_NEXT;
1098 calendar_select_and_focus_day (GtkCalendar *calendar,
1101 gint old_focus_row = calendar->focus_row;
1102 gint old_focus_col = calendar->focus_col;
1106 for (row = 0; row < 6; row ++)
1107 for (col = 0; col < 7; col++)
1109 if (calendar->day_month[row][col] == MONTH_CURRENT
1110 && calendar->day[row][col] == day)
1112 calendar->focus_row = row;
1113 calendar->focus_col = col;
1117 if (old_focus_row != -1 && old_focus_col != -1)
1118 calendar_invalidate_day (calendar, old_focus_row, old_focus_col);
1120 gtk_calendar_select_day (calendar, day);
1124 /****************************************
1125 * Layout computation utilities *
1126 ****************************************/
1129 calendar_row_height (GtkCalendar *calendar)
1131 return (GTK_CALENDAR_GET_PRIVATE (calendar)->main_h - CALENDAR_MARGIN
1132 - ((calendar->display_flags & GTK_CALENDAR_SHOW_DAY_NAMES)
1133 ? calendar_get_ysep (calendar) : CALENDAR_MARGIN)) / 6;
1137 /* calendar_left_x_for_column: returns the x coordinate
1138 * for the left of the column */
1140 calendar_left_x_for_column (GtkCalendar *calendar,
1145 gint calendar_xsep = calendar_get_xsep (calendar);
1147 if (gtk_widget_get_direction (GTK_WIDGET (calendar)) == GTK_TEXT_DIR_RTL)
1148 column = 6 - column;
1150 width = GTK_CALENDAR_GET_PRIVATE (calendar)->day_width;
1151 if (calendar->display_flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
1152 x_left = calendar_xsep + (width + DAY_XSEP) * column;
1154 x_left = CALENDAR_MARGIN + (width + DAY_XSEP) * column;
1159 /* column_from_x: returns the column 0-6 that the
1160 * x pixel of the xwindow is in */
1162 calendar_column_from_x (GtkCalendar *calendar,
1166 gint x_left, x_right;
1170 for (c = 0; c < 7; c++)
1172 x_left = calendar_left_x_for_column (calendar, c);
1173 x_right = x_left + GTK_CALENDAR_GET_PRIVATE (calendar)->day_width;
1175 if (event_x >= x_left && event_x < x_right)
1185 /* calendar_top_y_for_row: returns the y coordinate
1186 * for the top of the row */
1188 calendar_top_y_for_row (GtkCalendar *calendar,
1192 return (GTK_CALENDAR_GET_PRIVATE (calendar)->main_h
1193 - (CALENDAR_MARGIN + (6 - row)
1194 * calendar_row_height (calendar)));
1197 /* row_from_y: returns the row 0-5 that the
1198 * y pixel of the xwindow is in */
1200 calendar_row_from_y (GtkCalendar *calendar,
1205 gint y_top, y_bottom;
1207 height = calendar_row_height (calendar);
1210 for (r = 0; r < 6; r++)
1212 y_top = calendar_top_y_for_row (calendar, r);
1213 y_bottom = y_top + height;
1215 if (event_y >= y_top && event_y < y_bottom)
1226 calendar_arrow_rectangle (GtkCalendar *calendar,
1230 GtkWidget *widget = GTK_WIDGET (calendar);
1231 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1234 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
1235 year_left = priv->year_before;
1237 year_left = !priv->year_before;
1240 rect->width = priv->arrow_width;
1241 rect->height = priv->header_h - 7;
1245 case ARROW_MONTH_LEFT:
1247 rect->x = (widget->allocation.width - 2 * widget->style->xthickness
1248 - (3 + 2*priv->arrow_width
1249 + priv->max_month_width));
1253 case ARROW_MONTH_RIGHT:
1255 rect->x = (widget->allocation.width - 2 * widget->style->xthickness
1256 - 3 - priv->arrow_width);
1258 rect->x = (priv->arrow_width
1259 + priv->max_month_width);
1261 case ARROW_YEAR_LEFT:
1265 rect->x = (widget->allocation.width - 2 * widget->style->xthickness
1266 - (3 + 2*priv->arrow_width
1267 + priv->max_year_width));
1269 case ARROW_YEAR_RIGHT:
1271 rect->x = (priv->arrow_width
1272 + priv->max_year_width);
1274 rect->x = (widget->allocation.width - 2 * widget->style->xthickness
1275 - 3 - priv->arrow_width);
1281 calendar_day_rectangle (GtkCalendar *calendar,
1286 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1288 rect->x = calendar_left_x_for_column (calendar, col);
1289 rect->y = calendar_top_y_for_row (calendar, row);
1290 rect->height = calendar_row_height (calendar);
1291 rect->width = priv->day_width;
1295 calendar_set_month_prev (GtkCalendar *calendar)
1299 if (calendar->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
1302 if (calendar->month == 0)
1304 calendar->month = 11;
1310 month_len = month_length[leap (calendar->year)][calendar->month + 1];
1312 calendar_compute_days (calendar);
1314 g_signal_emit (calendar,
1315 gtk_calendar_signals[PREV_MONTH_SIGNAL],
1317 g_signal_emit (calendar,
1318 gtk_calendar_signals[MONTH_CHANGED_SIGNAL],
1321 if (month_len < calendar->selected_day)
1323 calendar->selected_day = 0;
1324 gtk_calendar_select_day (calendar, month_len);
1328 if (calendar->selected_day < 0)
1329 calendar->selected_day = calendar->selected_day + 1 + month_length[leap (calendar->year)][calendar->month + 1];
1330 gtk_calendar_select_day (calendar, calendar->selected_day);
1333 calendar_queue_refresh (calendar);
1337 /****************************************
1338 * Basic object methods *
1339 ****************************************/
1342 gtk_calendar_finalize (GObject *object)
1344 G_OBJECT_CLASS (gtk_calendar_parent_class)->finalize (object);
1348 gtk_calendar_destroy (GtkObject *object)
1350 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (object);
1352 calendar_stop_spinning (GTK_CALENDAR (object));
1354 /* Call the destroy function for the extra display callback: */
1355 if (priv->detail_func_destroy && priv->detail_func_user_data)
1357 priv->detail_func_destroy (priv->detail_func_user_data);
1358 priv->detail_func_user_data = NULL;
1359 priv->detail_func_destroy = NULL;
1362 GTK_OBJECT_CLASS (gtk_calendar_parent_class)->destroy (object);
1367 calendar_set_display_option (GtkCalendar *calendar,
1368 GtkCalendarDisplayOptions flag,
1371 GtkCalendarDisplayOptions flags;
1373 flags = calendar->display_flags | flag;
1375 flags = calendar->display_flags & ~flag;
1376 gtk_calendar_set_display_options (calendar, flags);
1380 calendar_get_display_option (GtkCalendar *calendar,
1381 GtkCalendarDisplayOptions flag)
1383 return (calendar->display_flags & flag) != 0;
1387 gtk_calendar_set_property (GObject *object,
1389 const GValue *value,
1392 GtkCalendar *calendar;
1394 calendar = GTK_CALENDAR (object);
1399 gtk_calendar_select_month (calendar,
1401 g_value_get_int (value));
1404 gtk_calendar_select_month (calendar,
1405 g_value_get_int (value),
1409 gtk_calendar_select_day (calendar,
1410 g_value_get_int (value));
1412 case PROP_SHOW_HEADING:
1413 calendar_set_display_option (calendar,
1414 GTK_CALENDAR_SHOW_HEADING,
1415 g_value_get_boolean (value));
1417 case PROP_SHOW_DAY_NAMES:
1418 calendar_set_display_option (calendar,
1419 GTK_CALENDAR_SHOW_DAY_NAMES,
1420 g_value_get_boolean (value));
1422 case PROP_NO_MONTH_CHANGE:
1423 calendar_set_display_option (calendar,
1424 GTK_CALENDAR_NO_MONTH_CHANGE,
1425 g_value_get_boolean (value));
1427 case PROP_SHOW_WEEK_NUMBERS:
1428 calendar_set_display_option (calendar,
1429 GTK_CALENDAR_SHOW_WEEK_NUMBERS,
1430 g_value_get_boolean (value));
1432 case PROP_SHOW_DETAILS:
1433 calendar_set_display_option (calendar,
1434 GTK_CALENDAR_SHOW_DETAILS,
1435 g_value_get_boolean (value));
1437 case PROP_DETAIL_WIDTH_CHARS:
1438 gtk_calendar_set_detail_width_chars (calendar,
1439 g_value_get_int (value));
1441 case PROP_DETAIL_HEIGHT_ROWS:
1442 gtk_calendar_set_detail_height_rows (calendar,
1443 g_value_get_int (value));
1446 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1452 gtk_calendar_get_property (GObject *object,
1457 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (object);
1458 GtkCalendar *calendar = GTK_CALENDAR (object);
1463 g_value_set_int (value, calendar->year);
1466 g_value_set_int (value, calendar->month);
1469 g_value_set_int (value, calendar->selected_day);
1471 case PROP_SHOW_HEADING:
1472 g_value_set_boolean (value, calendar_get_display_option (calendar,
1473 GTK_CALENDAR_SHOW_HEADING));
1475 case PROP_SHOW_DAY_NAMES:
1476 g_value_set_boolean (value, calendar_get_display_option (calendar,
1477 GTK_CALENDAR_SHOW_DAY_NAMES));
1479 case PROP_NO_MONTH_CHANGE:
1480 g_value_set_boolean (value, calendar_get_display_option (calendar,
1481 GTK_CALENDAR_NO_MONTH_CHANGE));
1483 case PROP_SHOW_WEEK_NUMBERS:
1484 g_value_set_boolean (value, calendar_get_display_option (calendar,
1485 GTK_CALENDAR_SHOW_WEEK_NUMBERS));
1487 case PROP_SHOW_DETAILS:
1488 g_value_set_boolean (value, calendar_get_display_option (calendar,
1489 GTK_CALENDAR_SHOW_DETAILS));
1491 case PROP_DETAIL_WIDTH_CHARS:
1492 g_value_set_int (value, priv->detail_width_chars);
1494 case PROP_DETAIL_HEIGHT_ROWS:
1495 g_value_set_int (value, priv->detail_height_rows);
1498 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1504 /****************************************
1506 ****************************************/
1509 calendar_realize_arrows (GtkCalendar *calendar)
1511 GtkWidget *widget = GTK_WIDGET (calendar);
1512 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1513 GdkWindowAttr attributes;
1514 gint attributes_mask;
1517 /* Arrow windows ------------------------------------- */
1518 if (! (calendar->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
1519 && (calendar->display_flags & GTK_CALENDAR_SHOW_HEADING))
1521 attributes.wclass = GDK_INPUT_OUTPUT;
1522 attributes.window_type = GDK_WINDOW_CHILD;
1523 attributes.visual = gtk_widget_get_visual (widget);
1524 attributes.colormap = gtk_widget_get_colormap (widget);
1525 attributes.event_mask = (gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK
1526 | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
1527 | GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK);
1528 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
1529 for (i = 0; i < 4; i++)
1532 calendar_arrow_rectangle (calendar, i, &rect);
1534 attributes.x = rect.x;
1535 attributes.y = rect.y;
1536 attributes.width = rect.width;
1537 attributes.height = rect.height;
1538 priv->arrow_win[i] = gdk_window_new (priv->header_win,
1541 if (gtk_widget_is_sensitive (widget))
1542 priv->arrow_state[i] = GTK_STATE_NORMAL;
1544 priv->arrow_state[i] = GTK_STATE_INSENSITIVE;
1545 gdk_window_set_background (priv->arrow_win[i],
1546 HEADER_BG_COLOR (GTK_WIDGET (calendar)));
1547 gdk_window_show (priv->arrow_win[i]);
1548 gdk_window_set_user_data (priv->arrow_win[i], widget);
1553 for (i = 0; i < 4; i++)
1554 priv->arrow_win[i] = NULL;
1559 calendar_realize_header (GtkCalendar *calendar)
1561 GtkWidget *widget = GTK_WIDGET (calendar);
1562 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1563 GdkWindowAttr attributes;
1564 gint attributes_mask;
1566 /* Header window ------------------------------------- */
1567 if (calendar->display_flags & GTK_CALENDAR_SHOW_HEADING)
1569 attributes.wclass = GDK_INPUT_OUTPUT;
1570 attributes.window_type = GDK_WINDOW_CHILD;
1571 attributes.visual = gtk_widget_get_visual (widget);
1572 attributes.colormap = gtk_widget_get_colormap (widget);
1573 attributes.event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
1574 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
1575 attributes.x = widget->style->xthickness;
1576 attributes.y = widget->style->ythickness;
1577 attributes.width = widget->allocation.width - 2 * attributes.x;
1578 attributes.height = priv->header_h;
1579 priv->header_win = gdk_window_new (widget->window,
1580 &attributes, attributes_mask);
1582 gdk_window_set_background (priv->header_win,
1583 HEADER_BG_COLOR (GTK_WIDGET (calendar)));
1584 gdk_window_show (priv->header_win);
1585 gdk_window_set_user_data (priv->header_win, widget);
1590 priv->header_win = NULL;
1592 calendar_realize_arrows (calendar);
1596 calendar_get_inner_border (GtkCalendar *calendar)
1600 gtk_widget_style_get (GTK_WIDGET (calendar),
1601 "inner-border", &inner_border,
1604 return inner_border;
1608 calendar_get_xsep (GtkCalendar *calendar)
1612 gtk_widget_style_get (GTK_WIDGET (calendar),
1613 "horizontal-separation", &xsep,
1620 calendar_get_ysep (GtkCalendar *calendar)
1624 gtk_widget_style_get (GTK_WIDGET (calendar),
1625 "vertical-separation", &ysep,
1632 calendar_realize_day_names (GtkCalendar *calendar)
1634 GtkWidget *widget = GTK_WIDGET (calendar);
1635 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1636 GdkWindowAttr attributes;
1637 gint attributes_mask;
1638 gint inner_border = calendar_get_inner_border (calendar);
1640 /* Day names window --------------------------------- */
1641 if ( calendar->display_flags & GTK_CALENDAR_SHOW_DAY_NAMES)
1643 attributes.wclass = GDK_INPUT_OUTPUT;
1644 attributes.window_type = GDK_WINDOW_CHILD;
1645 attributes.visual = gtk_widget_get_visual (widget);
1646 attributes.colormap = gtk_widget_get_colormap (widget);
1647 attributes.event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
1648 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
1649 attributes.x = (widget->style->xthickness + inner_border);
1650 attributes.y = priv->header_h + (widget->style->ythickness
1652 attributes.width = (widget->allocation.width
1653 - (widget->style->xthickness + inner_border)
1655 attributes.height = priv->day_name_h;
1656 priv->day_name_win = gdk_window_new (widget->window,
1659 gdk_window_set_background (priv->day_name_win,
1660 BACKGROUND_COLOR ( GTK_WIDGET ( calendar)));
1661 gdk_window_show (priv->day_name_win);
1662 gdk_window_set_user_data (priv->day_name_win, widget);
1666 priv->day_name_win = NULL;
1671 calendar_realize_week_numbers (GtkCalendar *calendar)
1673 GtkWidget *widget = GTK_WIDGET (calendar);
1674 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1675 GdkWindowAttr attributes;
1676 gint attributes_mask;
1677 gint inner_border = calendar_get_inner_border (calendar);
1679 /* Week number window -------------------------------- */
1680 if (calendar->display_flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
1682 attributes.wclass = GDK_INPUT_OUTPUT;
1683 attributes.window_type = GDK_WINDOW_CHILD;
1684 attributes.visual = gtk_widget_get_visual (widget);
1685 attributes.colormap = gtk_widget_get_colormap (widget);
1686 attributes.event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
1688 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
1689 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
1690 attributes.x = widget->style->xthickness + inner_border;
1692 attributes.x = widget->allocation.width - priv->week_width - (widget->style->xthickness + inner_border);
1693 attributes.y = (priv->header_h + priv->day_name_h
1694 + (widget->style->ythickness + inner_border));
1695 attributes.width = priv->week_width;
1696 attributes.height = priv->main_h;
1697 priv->week_win = gdk_window_new (widget->window,
1698 &attributes, attributes_mask);
1699 gdk_window_set_background (priv->week_win,
1700 BACKGROUND_COLOR (GTK_WIDGET (calendar)));
1701 gdk_window_show (priv->week_win);
1702 gdk_window_set_user_data (priv->week_win, widget);
1706 priv->week_win = NULL;
1711 gtk_calendar_realize (GtkWidget *widget)
1713 GtkCalendar *calendar = GTK_CALENDAR (widget);
1714 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
1715 GdkWindowAttr attributes;
1716 gint attributes_mask;
1717 gint inner_border = calendar_get_inner_border (calendar);
1719 gtk_widget_set_realized (widget, TRUE);
1721 attributes.x = widget->allocation.x;
1722 attributes.y = widget->allocation.y;
1723 attributes.width = widget->allocation.width;
1724 attributes.height = widget->allocation.height;
1725 attributes.wclass = GDK_INPUT_OUTPUT;
1726 attributes.window_type = GDK_WINDOW_CHILD;
1727 attributes.event_mask = (gtk_widget_get_events (widget)
1728 | GDK_EXPOSURE_MASK |GDK_KEY_PRESS_MASK | GDK_SCROLL_MASK);
1729 attributes.visual = gtk_widget_get_visual (widget);
1730 attributes.colormap = gtk_widget_get_colormap (widget);
1732 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
1733 widget->window = gdk_window_new (widget->parent->window,
1734 &attributes, attributes_mask);
1736 widget->style = gtk_style_attach (widget->style, widget->window);
1738 /* Header window ------------------------------------- */
1739 calendar_realize_header (calendar);
1740 /* Day names window --------------------------------- */
1741 calendar_realize_day_names (calendar);
1742 /* Week number window -------------------------------- */
1743 calendar_realize_week_numbers (calendar);
1744 /* Main Window -------------------------------------- */
1745 attributes.event_mask = (gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK
1746 | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
1747 | GDK_POINTER_MOTION_MASK | GDK_LEAVE_NOTIFY_MASK);
1749 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
1750 attributes.x = priv->week_width + (widget->style->ythickness + inner_border);
1752 attributes.x = widget->style->ythickness + inner_border;
1754 attributes.y = (priv->header_h + priv->day_name_h
1755 + (widget->style->ythickness + inner_border));
1756 attributes.width = (widget->allocation.width - attributes.x
1757 - (widget->style->xthickness + inner_border));
1758 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1759 attributes.width -= priv->week_width;
1761 attributes.height = priv->main_h;
1762 priv->main_win = gdk_window_new (widget->window,
1763 &attributes, attributes_mask);
1764 gdk_window_set_background (priv->main_win,
1765 BACKGROUND_COLOR ( GTK_WIDGET ( calendar)));
1766 gdk_window_show (priv->main_win);
1767 gdk_window_set_user_data (priv->main_win, widget);
1768 gdk_window_set_background (widget->window, BACKGROUND_COLOR (widget));
1769 gdk_window_show (widget->window);
1770 gdk_window_set_user_data (widget->window, widget);
1774 gtk_calendar_unrealize (GtkWidget *widget)
1776 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
1779 if (priv->header_win)
1781 for (i = 0; i < 4; i++)
1783 if (priv->arrow_win[i])
1785 gdk_window_set_user_data (priv->arrow_win[i], NULL);
1786 gdk_window_destroy (priv->arrow_win[i]);
1787 priv->arrow_win[i] = NULL;
1790 gdk_window_set_user_data (priv->header_win, NULL);
1791 gdk_window_destroy (priv->header_win);
1792 priv->header_win = NULL;
1797 gdk_window_set_user_data (priv->week_win, NULL);
1798 gdk_window_destroy (priv->week_win);
1799 priv->week_win = NULL;
1804 gdk_window_set_user_data (priv->main_win, NULL);
1805 gdk_window_destroy (priv->main_win);
1806 priv->main_win = NULL;
1808 if (priv->day_name_win)
1810 gdk_window_set_user_data (priv->day_name_win, NULL);
1811 gdk_window_destroy (priv->day_name_win);
1812 priv->day_name_win = NULL;
1815 GTK_WIDGET_CLASS (gtk_calendar_parent_class)->unrealize (widget);
1819 gtk_calendar_get_detail (GtkCalendar *calendar,
1823 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1826 if (priv->detail_func == NULL)
1829 year = calendar->year;
1830 month = calendar->month + calendar->day_month[row][column] - MONTH_CURRENT;
1837 else if (month > 11)
1843 return priv->detail_func (calendar,
1845 calendar->day[row][column],
1846 priv->detail_func_user_data);
1850 gtk_calendar_query_tooltip (GtkWidget *widget,
1853 gboolean keyboard_mode,
1854 GtkTooltip *tooltip)
1856 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
1857 GtkCalendar *calendar = GTK_CALENDAR (widget);
1858 gchar *detail = NULL;
1859 GdkRectangle day_rect;
1863 gint x0, y0, row, col;
1865 gdk_window_get_position (priv->main_win, &x0, &y0);
1866 col = calendar_column_from_x (calendar, x - x0);
1867 row = calendar_row_from_y (calendar, y - y0);
1869 if (col != -1 && row != -1 &&
1870 (0 != (priv->detail_overflow[row] & (1 << col)) ||
1871 0 == (calendar->display_flags & GTK_CALENDAR_SHOW_DETAILS)))
1873 detail = gtk_calendar_get_detail (calendar, row, col);
1874 calendar_day_rectangle (calendar, row, col, &day_rect);
1883 gtk_tooltip_set_tip_area (tooltip, &day_rect);
1884 gtk_tooltip_set_markup (tooltip, detail);
1891 if (GTK_WIDGET_CLASS (gtk_calendar_parent_class)->query_tooltip)
1892 return GTK_WIDGET_CLASS (gtk_calendar_parent_class)->query_tooltip (widget, x, y, keyboard_mode, tooltip);
1898 /****************************************
1899 * Size Request and Allocate *
1900 ****************************************/
1903 gtk_calendar_size_request (GtkWidget *widget,
1904 GtkRequisition *requisition)
1906 GtkCalendar *calendar = GTK_CALENDAR (widget);
1907 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
1908 PangoLayout *layout;
1909 PangoRectangle logical_rect;
1913 gint calendar_margin = CALENDAR_MARGIN;
1914 gint header_width, main_width;
1915 gint max_header_height = 0;
1918 gint max_detail_height;
1919 gint inner_border = calendar_get_inner_border (calendar);
1920 gint calendar_ysep = calendar_get_ysep (calendar);
1921 gint calendar_xsep = calendar_get_xsep (calendar);
1923 gtk_widget_style_get (GTK_WIDGET (widget),
1924 "focus-line-width", &focus_width,
1925 "focus-padding", &focus_padding,
1928 layout = gtk_widget_create_pango_layout (widget, NULL);
1931 * Calculate the requisition width for the widget.
1936 if (calendar->display_flags & GTK_CALENDAR_SHOW_HEADING)
1938 priv->max_month_width = 0;
1939 for (i = 0; i < 12; i++)
1941 pango_layout_set_text (layout, default_monthname[i], -1);
1942 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1943 priv->max_month_width = MAX (priv->max_month_width,
1944 logical_rect.width + 8);
1945 max_header_height = MAX (max_header_height, logical_rect.height);
1948 priv->max_year_width = 0;
1949 /* Translators: This is a text measurement template.
1950 * Translate it to the widest year text
1952 * If you don't understand this, leave it as "2000"
1954 pango_layout_set_text (layout, C_("year measurement template", "2000"), -1);
1955 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1956 priv->max_year_width = MAX (priv->max_year_width,
1957 logical_rect.width + 8);
1958 max_header_height = MAX (max_header_height, logical_rect.height);
1962 priv->max_month_width = 0;
1963 priv->max_year_width = 0;
1966 if (calendar->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
1967 header_width = (priv->max_month_width
1968 + priv->max_year_width
1971 header_width = (priv->max_month_width
1972 + priv->max_year_width
1973 + 4 * priv->arrow_width + 3 * 3);
1975 /* Mainwindow labels width */
1977 priv->max_day_char_width = 0;
1978 priv->max_day_char_ascent = 0;
1979 priv->max_day_char_descent = 0;
1980 priv->min_day_width = 0;
1982 for (i = 0; i < 9; i++)
1985 g_snprintf (buffer, sizeof (buffer), C_("calendar:day:digits", "%d"), i * 11);
1986 pango_layout_set_text (layout, buffer, -1);
1987 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1988 priv->min_day_width = MAX (priv->min_day_width,
1989 logical_rect.width);
1991 priv->max_day_char_ascent = MAX (priv->max_day_char_ascent,
1992 PANGO_ASCENT (logical_rect));
1993 priv->max_day_char_descent = MAX (priv->max_day_char_descent,
1994 PANGO_DESCENT (logical_rect));
1997 priv->max_label_char_ascent = 0;
1998 priv->max_label_char_descent = 0;
1999 if (calendar->display_flags & GTK_CALENDAR_SHOW_DAY_NAMES)
2000 for (i = 0; i < 7; i++)
2002 pango_layout_set_text (layout, default_abbreviated_dayname[i], -1);
2003 pango_layout_line_get_pixel_extents (pango_layout_get_lines_readonly (layout)->data, NULL, &logical_rect);
2005 priv->min_day_width = MAX (priv->min_day_width, logical_rect.width);
2006 priv->max_label_char_ascent = MAX (priv->max_label_char_ascent,
2007 PANGO_ASCENT (logical_rect));
2008 priv->max_label_char_descent = MAX (priv->max_label_char_descent,
2009 PANGO_DESCENT (logical_rect));
2012 priv->max_week_char_width = 0;
2013 if (calendar->display_flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
2014 for (i = 0; i < 9; i++)
2017 g_snprintf (buffer, sizeof (buffer), C_("calendar:week:digits", "%d"), i * 11);
2018 pango_layout_set_text (layout, buffer, -1);
2019 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2020 priv->max_week_char_width = MAX (priv->max_week_char_width,
2021 logical_rect.width / 2);
2024 /* Calculate detail extents. Do this as late as possible since
2025 * pango_layout_set_markup is called which alters font settings. */
2026 max_detail_height = 0;
2028 if (priv->detail_func && (calendar->display_flags & GTK_CALENDAR_SHOW_DETAILS))
2030 gchar *markup, *tail;
2032 if (priv->detail_width_chars || priv->detail_height_rows)
2034 gint rows = MAX (1, priv->detail_height_rows) - 1;
2035 gsize len = priv->detail_width_chars + rows + 16;
2037 markup = tail = g_alloca (len);
2039 memcpy (tail, "<small>", 7);
2042 memset (tail, 'm', priv->detail_width_chars);
2043 tail += priv->detail_width_chars;
2045 memset (tail, '\n', rows);
2048 memcpy (tail, "</small>", 9);
2051 g_assert (len == (tail - markup));
2053 pango_layout_set_markup (layout, markup, -1);
2054 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2056 if (priv->detail_width_chars)
2057 priv->min_day_width = MAX (priv->min_day_width, logical_rect.width);
2058 if (priv->detail_height_rows)
2059 max_detail_height = MAX (max_detail_height, logical_rect.height);
2062 if (!priv->detail_width_chars || !priv->detail_height_rows)
2063 for (r = 0; r < 6; r++)
2064 for (c = 0; c < 7; c++)
2066 gchar *detail = gtk_calendar_get_detail (calendar, r, c);
2070 markup = g_strconcat ("<small>", detail, "</small>", NULL);
2071 pango_layout_set_markup (layout, markup, -1);
2073 if (priv->detail_width_chars)
2075 pango_layout_set_wrap (layout, PANGO_WRAP_WORD_CHAR);
2076 pango_layout_set_width (layout, PANGO_SCALE * priv->min_day_width);
2079 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2081 if (!priv->detail_width_chars)
2082 priv->min_day_width = MAX (priv->min_day_width, logical_rect.width);
2083 if (!priv->detail_height_rows)
2084 max_detail_height = MAX (max_detail_height, logical_rect.height);
2092 /* We add one to max_day_char_width to be able to make the marked day "bold" */
2093 priv->max_day_char_width = priv->min_day_width / 2 + 1;
2095 main_width = (7 * (priv->min_day_width + (focus_padding + focus_width) * 2) + (DAY_XSEP * 6) + CALENDAR_MARGIN * 2
2096 + (priv->max_week_char_width
2097 ? priv->max_week_char_width * 2 + (focus_padding + focus_width) * 2 + calendar_xsep * 2
2101 requisition->width = MAX (header_width, main_width + inner_border * 2) + widget->style->xthickness * 2;
2104 * Calculate the requisition height for the widget.
2107 if (calendar->display_flags & GTK_CALENDAR_SHOW_HEADING)
2109 priv->header_h = (max_header_height + calendar_ysep * 2);
2116 if (calendar->display_flags & GTK_CALENDAR_SHOW_DAY_NAMES)
2118 priv->day_name_h = (priv->max_label_char_ascent
2119 + priv->max_label_char_descent
2120 + 2 * (focus_padding + focus_width) + calendar_margin);
2121 calendar_margin = calendar_ysep;
2125 priv->day_name_h = 0;
2128 priv->main_h = (CALENDAR_MARGIN + calendar_margin
2129 + 6 * (priv->max_day_char_ascent
2130 + priv->max_day_char_descent
2132 + 2 * (focus_padding + focus_width))
2135 height = (priv->header_h + priv->day_name_h
2138 requisition->height = height + (widget->style->ythickness + inner_border) * 2;
2140 g_object_unref (layout);
2144 gtk_calendar_size_allocate (GtkWidget *widget,
2145 GtkAllocation *allocation)
2147 GtkCalendar *calendar = GTK_CALENDAR (widget);
2148 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
2149 gint xthickness = widget->style->xthickness;
2150 gint ythickness = widget->style->xthickness;
2152 gint inner_border = calendar_get_inner_border (calendar);
2153 gint calendar_xsep = calendar_get_xsep (calendar);
2155 widget->allocation = *allocation;
2157 if (calendar->display_flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
2159 priv->day_width = (priv->min_day_width
2160 * ((allocation->width - (xthickness + inner_border) * 2
2161 - (CALENDAR_MARGIN * 2) - (DAY_XSEP * 6) - calendar_xsep * 2))
2162 / (7 * priv->min_day_width + priv->max_week_char_width * 2));
2163 priv->week_width = ((allocation->width - (xthickness + inner_border) * 2
2164 - (CALENDAR_MARGIN * 2) - (DAY_XSEP * 6) - calendar_xsep * 2 )
2165 - priv->day_width * 7 + CALENDAR_MARGIN + calendar_xsep);
2169 priv->day_width = (allocation->width
2170 - (xthickness + inner_border) * 2
2171 - (CALENDAR_MARGIN * 2)
2172 - (DAY_XSEP * 6))/7;
2173 priv->week_width = 0;
2176 if (gtk_widget_get_realized (widget))
2178 gdk_window_move_resize (widget->window,
2179 allocation->x, allocation->y,
2180 allocation->width, allocation->height);
2181 if (priv->header_win)
2182 gdk_window_move_resize (priv->header_win,
2183 xthickness, ythickness,
2184 allocation->width - 2 * xthickness, priv->header_h);
2186 for (i = 0 ; i < 4 ; i++)
2188 if (priv->arrow_win[i])
2191 calendar_arrow_rectangle (calendar, i, &rect);
2193 gdk_window_move_resize (priv->arrow_win[i],
2194 rect.x, rect.y, rect.width, rect.height);
2198 if (priv->day_name_win)
2199 gdk_window_move_resize (priv->day_name_win,
2200 xthickness + inner_border,
2201 priv->header_h + (widget->style->ythickness + inner_border),
2202 allocation->width - (xthickness + inner_border) * 2,
2204 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
2207 gdk_window_move_resize (priv->week_win,
2208 (xthickness + inner_border),
2209 priv->header_h + priv->day_name_h
2210 + (widget->style->ythickness + inner_border),
2213 gdk_window_move_resize (priv->main_win,
2214 priv->week_width + (xthickness + inner_border),
2215 priv->header_h + priv->day_name_h
2216 + (widget->style->ythickness + inner_border),
2219 - (xthickness + inner_border) * 2,
2224 gdk_window_move_resize (priv->main_win,
2225 (xthickness + inner_border),
2226 priv->header_h + priv->day_name_h
2227 + (widget->style->ythickness + inner_border),
2230 - (xthickness + inner_border) * 2,
2233 gdk_window_move_resize (priv->week_win,
2236 - (xthickness + inner_border),
2237 priv->header_h + priv->day_name_h
2238 + (widget->style->ythickness + inner_border),
2246 /****************************************
2248 ****************************************/
2251 calendar_paint_header (GtkCalendar *calendar)
2253 GtkWidget *widget = GTK_WIDGET (calendar);
2254 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2259 gint max_month_width;
2260 gint max_year_width;
2261 PangoLayout *layout;
2262 PangoRectangle logical_rect;
2268 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
2269 year_left = priv->year_before;
2271 year_left = !priv->year_before;
2273 cr = gdk_cairo_create (priv->header_win);
2275 header_width = widget->allocation.width - 2 * widget->style->xthickness;
2277 max_month_width = priv->max_month_width;
2278 max_year_width = priv->max_year_width;
2280 gtk_paint_shadow (widget->style, priv->header_win,
2281 GTK_STATE_NORMAL, GTK_SHADOW_OUT,
2282 NULL, widget, "calendar",
2283 0, 0, header_width, priv->header_h);
2285 tmp_time = 1; /* Jan 1 1970, 00:00:01 UTC */
2286 tm = gmtime (&tmp_time);
2287 tm->tm_year = calendar->year - 1900;
2289 /* Translators: This dictates how the year is displayed in
2290 * gtkcalendar widget. See strftime() manual for the format.
2291 * Use only ASCII in the translation.
2293 * Also look for the msgid "2000".
2294 * Translate that entry to a year with the widest output of this
2297 * "%Y" is appropriate for most locales.
2299 strftime (buffer, sizeof (buffer), C_("calendar year format", "%Y"), tm);
2300 str = g_locale_to_utf8 (buffer, -1, NULL, NULL, NULL);
2301 layout = gtk_widget_create_pango_layout (widget, str);
2304 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2307 y = (priv->header_h - logical_rect.height) / 2;
2309 /* Draw year and its arrows */
2311 if (calendar->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
2313 x = 3 + (max_year_width - logical_rect.width)/2;
2315 x = header_width - (3 + max_year_width
2316 - (max_year_width - logical_rect.width)/2);
2319 x = 3 + priv->arrow_width + (max_year_width - logical_rect.width)/2;
2321 x = header_width - (3 + priv->arrow_width + max_year_width
2322 - (max_year_width - logical_rect.width)/2);
2325 gdk_cairo_set_source_color (cr, HEADER_FG_COLOR (GTK_WIDGET (calendar)));
2326 cairo_move_to (cr, x, y);
2327 pango_cairo_show_layout (cr, layout);
2330 g_snprintf (buffer, sizeof (buffer), "%s", default_monthname[calendar->month]);
2331 pango_layout_set_text (layout, buffer, -1);
2332 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2334 if (calendar->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
2336 x = header_width - (3 + max_month_width
2337 - (max_month_width - logical_rect.width)/2);
2339 x = 3 + (max_month_width - logical_rect.width) / 2;
2342 x = header_width - (3 + priv->arrow_width + max_month_width
2343 - (max_month_width - logical_rect.width)/2);
2345 x = 3 + priv->arrow_width + (max_month_width - logical_rect.width)/2;
2347 cairo_move_to (cr, x, y);
2348 pango_cairo_show_layout (cr, layout);
2350 g_object_unref (layout);
2355 calendar_paint_day_names (GtkCalendar *calendar)
2357 GtkWidget *widget = GTK_WIDGET (calendar);
2358 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2362 int day_width, cal_width;
2364 PangoLayout *layout;
2365 PangoRectangle logical_rect;
2368 gint calendar_ysep = calendar_get_ysep (calendar);
2369 gint calendar_xsep = calendar_get_xsep (calendar);
2371 cr = gdk_cairo_create (priv->day_name_win);
2373 gtk_widget_style_get (GTK_WIDGET (widget),
2374 "focus-line-width", &focus_width,
2375 "focus-padding", &focus_padding,
2378 day_width = priv->day_width;
2379 cal_width = widget->allocation.width;
2380 day_wid_sep = day_width + DAY_XSEP;
2383 * Draw rectangles as inverted background for the labels.
2386 gdk_cairo_set_source_color (cr, SELECTED_BG_COLOR (widget));
2387 cairo_rectangle (cr,
2388 CALENDAR_MARGIN, CALENDAR_MARGIN,
2389 cal_width-CALENDAR_MARGIN * 2,
2390 priv->day_name_h - CALENDAR_MARGIN);
2393 if (calendar->display_flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
2395 cairo_rectangle (cr,
2397 priv->day_name_h - calendar_ysep,
2398 priv->week_width - calendar_ysep - CALENDAR_MARGIN,
2407 layout = gtk_widget_create_pango_layout (widget, NULL);
2409 gdk_cairo_set_source_color (cr, SELECTED_FG_COLOR (widget));
2410 for (i = 0; i < 7; i++)
2412 if (gtk_widget_get_direction (GTK_WIDGET (calendar)) == GTK_TEXT_DIR_RTL)
2416 day = (day + priv->week_start) % 7;
2417 g_snprintf (buffer, sizeof (buffer), "%s", default_abbreviated_dayname[day]);
2419 pango_layout_set_text (layout, buffer, -1);
2420 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2424 + (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR ?
2425 (priv->week_width + (priv->week_width ? calendar_xsep : 0))
2428 + (day_width - logical_rect.width)/2),
2429 CALENDAR_MARGIN + focus_width + focus_padding + logical_rect.y);
2430 pango_cairo_show_layout (cr, layout);
2433 g_object_unref (layout);
2438 calendar_paint_week_numbers (GtkCalendar *calendar)
2440 GtkWidget *widget = GTK_WIDGET (calendar);
2441 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2444 guint week = 0, year;
2445 gint row, x_loc, y_loc;
2448 PangoLayout *layout;
2449 PangoRectangle logical_rect;
2452 gint calendar_xsep = calendar_get_xsep (calendar);
2454 cr = gdk_cairo_create (priv->week_win);
2456 gtk_widget_style_get (GTK_WIDGET (widget),
2457 "focus-line-width", &focus_width,
2458 "focus-padding", &focus_padding,
2462 * Draw a rectangle as inverted background for the labels.
2465 gdk_cairo_set_source_color (cr, SELECTED_BG_COLOR (widget));
2466 if (priv->day_name_win)
2467 cairo_rectangle (cr,
2470 priv->week_width - CALENDAR_MARGIN,
2471 priv->main_h - CALENDAR_MARGIN);
2473 cairo_rectangle (cr,
2476 priv->week_width - CALENDAR_MARGIN,
2477 priv->main_h - 2 * CALENDAR_MARGIN);
2484 layout = gtk_widget_create_pango_layout (widget, NULL);
2486 gdk_cairo_set_source_color (cr, SELECTED_FG_COLOR (widget));
2487 day_height = calendar_row_height (calendar);
2488 for (row = 0; row < 6; row++)
2492 year = calendar->year;
2493 if (calendar->day[row][6] < 15 && row > 3 && calendar->month == 11)
2496 result = week_of_year (&week, &year,
2497 ((calendar->day[row][6] < 15 && row > 3 ? 1 : 0)
2498 + calendar->month) % 12 + 1, calendar->day[row][6]);
2499 g_return_if_fail (result);
2501 /* Translators: this defines whether the week numbers should use
2502 * localized digits or the ones used in English (0123...).
2504 * Translate to "%Id" if you want to use localized digits, or
2505 * translate to "%d" otherwise.
2507 * Note that translating this doesn't guarantee that you get localized
2508 * digits. That needs support from your system and locale definition
2511 g_snprintf (buffer, sizeof (buffer), C_("calendar:week:digits", "%d"), week);
2512 pango_layout_set_text (layout, buffer, -1);
2513 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2515 y_loc = calendar_top_y_for_row (calendar, row) + (day_height - logical_rect.height) / 2;
2517 x_loc = (priv->week_width
2518 - logical_rect.width
2519 - calendar_xsep - focus_padding - focus_width);
2521 cairo_move_to (cr, x_loc, y_loc);
2522 pango_cairo_show_layout (cr, layout);
2525 g_object_unref (layout);
2530 calendar_invalidate_day_num (GtkCalendar *calendar,
2533 gint r, c, row, col;
2537 for (r = 0; r < 6; r++)
2538 for (c = 0; c < 7; c++)
2539 if (calendar->day_month[r][c] == MONTH_CURRENT &&
2540 calendar->day[r][c] == day)
2546 g_return_if_fail (row != -1);
2547 g_return_if_fail (col != -1);
2549 calendar_invalidate_day (calendar, row, col);
2553 calendar_invalidate_day (GtkCalendar *calendar,
2557 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2561 GdkRectangle day_rect;
2563 calendar_day_rectangle (calendar, row, col, &day_rect);
2564 gdk_window_invalidate_rect (priv->main_win, &day_rect, FALSE);
2569 is_color_attribute (PangoAttribute *attribute,
2572 return (attribute->klass->type == PANGO_ATTR_FOREGROUND ||
2573 attribute->klass->type == PANGO_ATTR_BACKGROUND);
2577 calendar_paint_day (GtkCalendar *calendar,
2581 GtkWidget *widget = GTK_WIDGET (calendar);
2582 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2584 GdkColor *text_color;
2589 GdkRectangle day_rect;
2591 PangoLayout *layout;
2592 PangoRectangle logical_rect;
2593 gboolean overflow = FALSE;
2594 gboolean show_details;
2596 g_return_if_fail (row < 6);
2597 g_return_if_fail (col < 7);
2599 cr = gdk_cairo_create (priv->main_win);
2601 day = calendar->day[row][col];
2602 show_details = (calendar->display_flags & GTK_CALENDAR_SHOW_DETAILS);
2604 calendar_day_rectangle (calendar, row, col, &day_rect);
2606 if (calendar->day_month[row][col] == MONTH_PREV)
2608 text_color = PREV_MONTH_COLOR (widget);
2610 else if (calendar->day_month[row][col] == MONTH_NEXT)
2612 text_color = NEXT_MONTH_COLOR (widget);
2617 if (calendar->highlight_row == row && calendar->highlight_col == col)
2619 cairo_set_source_color (cr, HIGHLIGHT_BG_COLOR (widget));
2620 gdk_cairo_rectangle (cr, &day_rect);
2624 if (calendar->selected_day == day)
2626 gdk_cairo_set_source_color (cr, SELECTED_BG_COLOR (widget));
2627 gdk_cairo_rectangle (cr, &day_rect);
2630 if (calendar->selected_day == day)
2631 text_color = SELECTED_FG_COLOR (widget);
2632 else if (calendar->marked_date[day-1])
2633 text_color = MARKED_COLOR (widget);
2635 text_color = NORMAL_DAY_COLOR (widget);
2638 /* Translators: this defines whether the day numbers should use
2639 * localized digits or the ones used in English (0123...).
2641 * Translate to "%Id" if you want to use localized digits, or
2642 * translate to "%d" otherwise.
2644 * Note that translating this doesn't guarantee that you get localized
2645 * digits. That needs support from your system and locale definition
2648 g_snprintf (buffer, sizeof (buffer), C_("calendar:day:digits", "%d"), day);
2650 /* Get extra information to show, if any: */
2652 detail = gtk_calendar_get_detail (calendar, row, col);
2654 layout = gtk_widget_create_pango_layout (widget, buffer);
2655 pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
2656 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2658 x_loc = day_rect.x + (day_rect.width - logical_rect.width) / 2;
2661 gdk_cairo_set_source_color (cr, text_color);
2662 cairo_move_to (cr, x_loc, y_loc);
2663 pango_cairo_show_layout (cr, layout);
2665 if (calendar->day_month[row][col] == MONTH_CURRENT &&
2666 (calendar->marked_date[day-1] || (detail && !show_details)))
2668 cairo_move_to (cr, x_loc - 1, y_loc);
2669 pango_cairo_show_layout (cr, layout);
2672 y_loc += priv->max_day_char_descent;
2674 if (priv->detail_func && show_details)
2678 if (calendar->selected_day == day)
2679 gdk_cairo_set_source_color (cr, &widget->style->text[GTK_STATE_ACTIVE]);
2680 else if (calendar->day_month[row][col] == MONTH_CURRENT)
2681 gdk_cairo_set_source_color (cr, &widget->style->base[GTK_STATE_ACTIVE]);
2683 gdk_cairo_set_source_color (cr, &widget->style->base[GTK_STATE_INSENSITIVE]);
2685 cairo_set_line_width (cr, 1);
2686 cairo_move_to (cr, day_rect.x + 2, y_loc + 0.5);
2687 cairo_line_to (cr, day_rect.x + day_rect.width - 2, y_loc + 0.5);
2695 if (detail && show_details)
2697 gchar *markup = g_strconcat ("<small>", detail, "</small>", NULL);
2698 pango_layout_set_markup (layout, markup, -1);
2701 if (day == calendar->selected_day)
2703 /* Stripping colors as they conflict with selection marking. */
2705 PangoAttrList *attrs = pango_layout_get_attributes (layout);
2706 PangoAttrList *colors = NULL;
2709 colors = pango_attr_list_filter (attrs, is_color_attribute, NULL);
2711 pango_attr_list_unref (colors);
2714 pango_layout_set_wrap (layout, PANGO_WRAP_WORD_CHAR);
2715 pango_layout_set_width (layout, PANGO_SCALE * day_rect.width);
2717 if (priv->detail_height_rows)
2719 gint dy = day_rect.height - (y_loc - day_rect.y);
2720 pango_layout_set_height (layout, PANGO_SCALE * dy);
2721 pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_END);
2724 cairo_move_to (cr, day_rect.x, y_loc);
2725 pango_cairo_show_layout (cr, layout);
2728 if (gtk_widget_has_focus (widget)
2729 && calendar->focus_row == row && calendar->focus_col == col)
2733 if (calendar->selected_day == day)
2734 state = gtk_widget_has_focus (widget) ? GTK_STATE_SELECTED : GTK_STATE_ACTIVE;
2736 state = GTK_STATE_NORMAL;
2738 gtk_paint_focus (widget->style,
2741 NULL, widget, "calendar-day",
2742 day_rect.x, day_rect.y,
2743 day_rect.width, day_rect.height);
2747 priv->detail_overflow[row] |= (1 << col);
2749 priv->detail_overflow[row] &= ~(1 << col);
2751 g_object_unref (layout);
2757 calendar_paint_main (GtkCalendar *calendar)
2761 for (col = 0; col < 7; col++)
2762 for (row = 0; row < 6; row++)
2763 calendar_paint_day (calendar, row, col);
2767 calendar_invalidate_arrow (GtkCalendar *calendar,
2770 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2773 window = priv->arrow_win[arrow];
2775 gdk_window_invalidate_rect (window, NULL, FALSE);
2779 calendar_paint_arrow (GtkCalendar *calendar,
2782 GtkWidget *widget = GTK_WIDGET (calendar);
2783 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
2786 window = priv->arrow_win[arrow];
2789 cairo_t *cr = gdk_cairo_create (window);
2793 state = priv->arrow_state[arrow];
2795 gdk_cairo_set_source_color (cr, &widget->style->bg[state]);
2799 gdk_drawable_get_size (window, &width, &height);
2800 if (arrow == ARROW_MONTH_LEFT || arrow == ARROW_YEAR_LEFT)
2801 gtk_paint_arrow (widget->style, window, state,
2802 GTK_SHADOW_OUT, NULL, widget, "calendar",
2803 GTK_ARROW_LEFT, TRUE,
2804 width/2 - 3, height/2 - 4, 8, 8);
2806 gtk_paint_arrow (widget->style, window, state,
2807 GTK_SHADOW_OUT, NULL, widget, "calendar",
2808 GTK_ARROW_RIGHT, TRUE,
2809 width/2 - 4, height/2 - 4, 8, 8);
2814 gtk_calendar_expose (GtkWidget *widget,
2815 GdkEventExpose *event)
2817 GtkCalendar *calendar = GTK_CALENDAR (widget);
2818 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
2821 if (gtk_widget_is_drawable (widget))
2823 if (event->window == priv->main_win)
2824 calendar_paint_main (calendar);
2826 if (event->window == priv->header_win)
2827 calendar_paint_header (calendar);
2829 for (i = 0; i < 4; i++)
2830 if (event->window == priv->arrow_win[i])
2831 calendar_paint_arrow (calendar, i);
2833 if (event->window == priv->day_name_win)
2834 calendar_paint_day_names (calendar);
2836 if (event->window == priv->week_win)
2837 calendar_paint_week_numbers (calendar);
2838 if (event->window == widget->window)
2840 gtk_paint_shadow (widget->style, widget->window, gtk_widget_get_state (widget),
2841 GTK_SHADOW_IN, NULL, widget, "calendar",
2842 0, 0, widget->allocation.width, widget->allocation.height);
2850 /****************************************
2852 ****************************************/
2855 calendar_arrow_action (GtkCalendar *calendar,
2860 case ARROW_YEAR_LEFT:
2861 calendar_set_year_prev (calendar);
2863 case ARROW_YEAR_RIGHT:
2864 calendar_set_year_next (calendar);
2866 case ARROW_MONTH_LEFT:
2867 calendar_set_month_prev (calendar);
2869 case ARROW_MONTH_RIGHT:
2870 calendar_set_month_next (calendar);
2878 calendar_timer (gpointer data)
2880 GtkCalendar *calendar = data;
2881 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2882 gboolean retval = FALSE;
2886 calendar_arrow_action (calendar, priv->click_child);
2888 if (priv->need_timer)
2890 GtkSettings *settings;
2893 settings = gtk_widget_get_settings (GTK_WIDGET (calendar));
2894 g_object_get (settings, "gtk-timeout-repeat", &timeout, NULL);
2896 priv->need_timer = FALSE;
2897 priv->timer = gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT_IDLE,
2898 timeout * SCROLL_DELAY_FACTOR,
2899 (GSourceFunc) calendar_timer,
2900 (gpointer) calendar, NULL);
2910 calendar_start_spinning (GtkCalendar *calendar,
2913 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2915 priv->click_child = click_child;
2919 GtkSettings *settings;
2922 settings = gtk_widget_get_settings (GTK_WIDGET (calendar));
2923 g_object_get (settings, "gtk-timeout-initial", &timeout, NULL);
2925 priv->need_timer = TRUE;
2926 priv->timer = gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT_IDLE,
2928 (GSourceFunc) calendar_timer,
2929 (gpointer) calendar, NULL);
2934 calendar_stop_spinning (GtkCalendar *calendar)
2936 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2940 g_source_remove (priv->timer);
2942 priv->need_timer = FALSE;
2947 calendar_main_button_press (GtkCalendar *calendar,
2948 GdkEventButton *event)
2950 GtkWidget *widget = GTK_WIDGET (calendar);
2951 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2957 x = (gint) (event->x);
2958 y = (gint) (event->y);
2960 row = calendar_row_from_y (calendar, y);
2961 col = calendar_column_from_x (calendar, x);
2963 /* If row or column isn't found, just return. */
2964 if (row == -1 || col == -1)
2967 day_month = calendar->day_month[row][col];
2969 if (event->type == GDK_BUTTON_PRESS)
2971 day = calendar->day[row][col];
2973 if (day_month == MONTH_PREV)
2974 calendar_set_month_prev (calendar);
2975 else if (day_month == MONTH_NEXT)
2976 calendar_set_month_next (calendar);
2978 if (!gtk_widget_has_focus (widget))
2979 gtk_widget_grab_focus (widget);
2981 if (event->button == 1)
2984 priv->drag_start_x = x;
2985 priv->drag_start_y = y;
2988 calendar_select_and_focus_day (calendar, day);
2990 else if (event->type == GDK_2BUTTON_PRESS)
2993 if (day_month == MONTH_CURRENT)
2994 g_signal_emit (calendar,
2995 gtk_calendar_signals[DAY_SELECTED_DOUBLE_CLICK_SIGNAL],
3001 gtk_calendar_button_press (GtkWidget *widget,
3002 GdkEventButton *event)
3004 GtkCalendar *calendar = GTK_CALENDAR (widget);
3005 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3008 if (event->window == priv->main_win)
3009 calendar_main_button_press (calendar, event);
3011 if (!gtk_widget_has_focus (widget))
3012 gtk_widget_grab_focus (widget);
3014 for (arrow = ARROW_YEAR_LEFT; arrow <= ARROW_MONTH_RIGHT; arrow++)
3016 if (event->window == priv->arrow_win[arrow])
3019 /* only call the action on single click, not double */
3020 if (event->type == GDK_BUTTON_PRESS)
3022 if (event->button == 1)
3023 calendar_start_spinning (calendar, arrow);
3025 calendar_arrow_action (calendar, arrow);
3036 gtk_calendar_button_release (GtkWidget *widget,
3037 GdkEventButton *event)
3039 GtkCalendar *calendar = GTK_CALENDAR (widget);
3040 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3042 if (event->button == 1)
3044 calendar_stop_spinning (calendar);
3054 gtk_calendar_motion_notify (GtkWidget *widget,
3055 GdkEventMotion *event)
3057 GtkCalendar *calendar = GTK_CALENDAR (widget);
3058 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3059 gint event_x, event_y;
3061 gint old_row, old_col;
3063 event_x = (gint) (event->x);
3064 event_y = (gint) (event->y);
3066 if (event->window == priv->main_win)
3071 if (gtk_drag_check_threshold (widget,
3072 priv->drag_start_x, priv->drag_start_y,
3073 event->x, event->y))
3075 GdkDragContext *context;
3076 GtkTargetList *target_list = gtk_target_list_new (NULL, 0);
3077 gtk_target_list_add_text_targets (target_list, 0);
3078 context = gtk_drag_begin (widget, target_list, GDK_ACTION_COPY,
3079 1, (GdkEvent *)event);
3084 gtk_target_list_unref (target_list);
3085 gtk_drag_set_icon_default (context);
3090 row = calendar_row_from_y (calendar, event_y);
3091 col = calendar_column_from_x (calendar, event_x);
3093 if (row != calendar->highlight_row || calendar->highlight_col != col)
3095 old_row = calendar->highlight_row;
3096 old_col = calendar->highlight_col;
3097 if (old_row > -1 && old_col > -1)
3099 calendar->highlight_row = -1;
3100 calendar->highlight_col = -1;
3101 calendar_invalidate_day (calendar, old_row, old_col);
3104 calendar->highlight_row = row;
3105 calendar->highlight_col = col;
3107 if (row > -1 && col > -1)
3108 calendar_invalidate_day (calendar, row, col);
3116 gtk_calendar_enter_notify (GtkWidget *widget,
3117 GdkEventCrossing *event)
3119 GtkCalendar *calendar = GTK_CALENDAR (widget);
3120 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3122 if (event->window == priv->arrow_win[ARROW_MONTH_LEFT])
3124 priv->arrow_state[ARROW_MONTH_LEFT] = GTK_STATE_PRELIGHT;
3125 calendar_invalidate_arrow (calendar, ARROW_MONTH_LEFT);
3128 if (event->window == priv->arrow_win[ARROW_MONTH_RIGHT])
3130 priv->arrow_state[ARROW_MONTH_RIGHT] = GTK_STATE_PRELIGHT;
3131 calendar_invalidate_arrow (calendar, ARROW_MONTH_RIGHT);
3134 if (event->window == priv->arrow_win[ARROW_YEAR_LEFT])
3136 priv->arrow_state[ARROW_YEAR_LEFT] = GTK_STATE_PRELIGHT;
3137 calendar_invalidate_arrow (calendar, ARROW_YEAR_LEFT);
3140 if (event->window == priv->arrow_win[ARROW_YEAR_RIGHT])
3142 priv->arrow_state[ARROW_YEAR_RIGHT] = GTK_STATE_PRELIGHT;
3143 calendar_invalidate_arrow (calendar, ARROW_YEAR_RIGHT);
3150 gtk_calendar_leave_notify (GtkWidget *widget,
3151 GdkEventCrossing *event)
3153 GtkCalendar *calendar = GTK_CALENDAR (widget);
3154 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3158 if (event->window == priv->main_win)
3160 row = calendar->highlight_row;
3161 col = calendar->highlight_col;
3162 calendar->highlight_row = -1;
3163 calendar->highlight_col = -1;
3164 if (row > -1 && col > -1)
3165 calendar_invalidate_day (calendar, row, col);
3168 if (event->window == priv->arrow_win[ARROW_MONTH_LEFT])
3170 priv->arrow_state[ARROW_MONTH_LEFT] = GTK_STATE_NORMAL;
3171 calendar_invalidate_arrow (calendar, ARROW_MONTH_LEFT);
3174 if (event->window == priv->arrow_win[ARROW_MONTH_RIGHT])
3176 priv->arrow_state[ARROW_MONTH_RIGHT] = GTK_STATE_NORMAL;
3177 calendar_invalidate_arrow (calendar, ARROW_MONTH_RIGHT);
3180 if (event->window == priv->arrow_win[ARROW_YEAR_LEFT])
3182 priv->arrow_state[ARROW_YEAR_LEFT] = GTK_STATE_NORMAL;
3183 calendar_invalidate_arrow (calendar, ARROW_YEAR_LEFT);
3186 if (event->window == priv->arrow_win[ARROW_YEAR_RIGHT])
3188 priv->arrow_state[ARROW_YEAR_RIGHT] = GTK_STATE_NORMAL;
3189 calendar_invalidate_arrow (calendar, ARROW_YEAR_RIGHT);
3196 gtk_calendar_scroll (GtkWidget *widget,
3197 GdkEventScroll *event)
3199 GtkCalendar *calendar = GTK_CALENDAR (widget);
3201 if (event->direction == GDK_SCROLL_UP)
3203 if (!gtk_widget_has_focus (widget))
3204 gtk_widget_grab_focus (widget);
3205 calendar_set_month_prev (calendar);
3207 else if (event->direction == GDK_SCROLL_DOWN)
3209 if (!gtk_widget_has_focus (widget))
3210 gtk_widget_grab_focus (widget);
3211 calendar_set_month_next (calendar);
3220 /****************************************
3222 ****************************************/
3225 move_focus (GtkCalendar *calendar,
3228 GtkTextDirection text_dir = gtk_widget_get_direction (GTK_WIDGET (calendar));
3230 if ((text_dir == GTK_TEXT_DIR_LTR && direction == -1) ||
3231 (text_dir == GTK_TEXT_DIR_RTL && direction == 1))
3233 if (calendar->focus_col > 0)
3234 calendar->focus_col--;
3235 else if (calendar->focus_row > 0)
3237 calendar->focus_col = 6;
3238 calendar->focus_row--;
3241 if (calendar->focus_col < 0)
3242 calendar->focus_col = 6;
3243 if (calendar->focus_row < 0)
3244 calendar->focus_row = 5;
3248 if (calendar->focus_col < 6)
3249 calendar->focus_col++;
3250 else if (calendar->focus_row < 5)
3252 calendar->focus_col = 0;
3253 calendar->focus_row++;
3256 if (calendar->focus_col < 0)
3257 calendar->focus_col = 0;
3258 if (calendar->focus_row < 0)
3259 calendar->focus_row = 0;
3264 gtk_calendar_key_press (GtkWidget *widget,
3267 GtkCalendar *calendar;
3273 calendar = GTK_CALENDAR (widget);
3276 old_focus_row = calendar->focus_row;
3277 old_focus_col = calendar->focus_col;
3279 switch (event->keyval)
3284 if (event->state & GDK_CONTROL_MASK)
3285 calendar_set_month_prev (calendar);
3288 move_focus (calendar, -1);
3289 calendar_invalidate_day (calendar, old_focus_row, old_focus_col);
3290 calendar_invalidate_day (calendar, calendar->focus_row,
3291 calendar->focus_col);
3297 if (event->state & GDK_CONTROL_MASK)
3298 calendar_set_month_next (calendar);
3301 move_focus (calendar, 1);
3302 calendar_invalidate_day (calendar, old_focus_row, old_focus_col);
3303 calendar_invalidate_day (calendar, calendar->focus_row,
3304 calendar->focus_col);
3310 if (event->state & GDK_CONTROL_MASK)
3311 calendar_set_year_prev (calendar);
3314 if (calendar->focus_row > 0)
3315 calendar->focus_row--;
3316 if (calendar->focus_row < 0)
3317 calendar->focus_row = 5;
3318 if (calendar->focus_col < 0)
3319 calendar->focus_col = 6;
3320 calendar_invalidate_day (calendar, old_focus_row, old_focus_col);
3321 calendar_invalidate_day (calendar, calendar->focus_row,
3322 calendar->focus_col);
3328 if (event->state & GDK_CONTROL_MASK)
3329 calendar_set_year_next (calendar);
3332 if (calendar->focus_row < 5)
3333 calendar->focus_row++;
3334 if (calendar->focus_col < 0)
3335 calendar->focus_col = 0;
3336 calendar_invalidate_day (calendar, old_focus_row, old_focus_col);
3337 calendar_invalidate_day (calendar, calendar->focus_row,
3338 calendar->focus_col);
3343 row = calendar->focus_row;
3344 col = calendar->focus_col;
3346 if (row > -1 && col > -1)
3350 day = calendar->day[row][col];
3351 if (calendar->day_month[row][col] == MONTH_PREV)
3352 calendar_set_month_prev (calendar);
3353 else if (calendar->day_month[row][col] == MONTH_NEXT)
3354 calendar_set_month_next (calendar);
3356 calendar_select_and_focus_day (calendar, day);
3364 /****************************************
3365 * Misc widget methods *
3366 ****************************************/
3369 calendar_set_background (GtkWidget *widget)
3371 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3374 if (gtk_widget_get_realized (widget))
3376 for (i = 0; i < 4; i++)
3378 if (priv->arrow_win[i])
3379 gdk_window_set_background (priv->arrow_win[i],
3380 HEADER_BG_COLOR (widget));
3382 if (priv->header_win)
3383 gdk_window_set_background (priv->header_win,
3384 HEADER_BG_COLOR (widget));
3385 if (priv->day_name_win)
3386 gdk_window_set_background (priv->day_name_win,
3387 BACKGROUND_COLOR (widget));
3389 gdk_window_set_background (priv->week_win,
3390 BACKGROUND_COLOR (widget));
3392 gdk_window_set_background (priv->main_win,
3393 BACKGROUND_COLOR (widget));
3395 gdk_window_set_background (widget->window,
3396 BACKGROUND_COLOR (widget));
3401 gtk_calendar_style_set (GtkWidget *widget,
3402 GtkStyle *previous_style)
3404 if (previous_style && gtk_widget_get_realized (widget))
3405 calendar_set_background (widget);
3409 gtk_calendar_state_changed (GtkWidget *widget,
3410 GtkStateType previous_state)
3412 GtkCalendar *calendar = GTK_CALENDAR (widget);
3413 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3416 if (!gtk_widget_is_sensitive (widget))
3419 calendar_stop_spinning (calendar);
3422 for (i = 0; i < 4; i++)
3423 if (gtk_widget_is_sensitive (widget))
3424 priv->arrow_state[i] = GTK_STATE_NORMAL;
3426 priv->arrow_state[i] = GTK_STATE_INSENSITIVE;
3428 calendar_set_background (widget);
3432 gtk_calendar_grab_notify (GtkWidget *widget,
3433 gboolean was_grabbed)
3436 calendar_stop_spinning (GTK_CALENDAR (widget));
3440 gtk_calendar_focus_out (GtkWidget *widget,
3441 GdkEventFocus *event)
3443 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3444 GtkCalendar *calendar = GTK_CALENDAR (widget);
3446 calendar_queue_refresh (calendar);
3447 calendar_stop_spinning (calendar);
3455 /****************************************
3457 ****************************************/
3460 gtk_calendar_drag_data_get (GtkWidget *widget,
3461 GdkDragContext *context,
3462 GtkSelectionData *selection_data,
3466 GtkCalendar *calendar = GTK_CALENDAR (widget);
3471 date = g_date_new_dmy (calendar->selected_day, calendar->month + 1, calendar->year);
3472 len = g_date_strftime (str, 127, "%x", date);
3473 gtk_selection_data_set_text (selection_data, str, len);
3478 /* Get/set whether drag_motion requested the drag data and
3479 * drag_data_received should thus not actually insert the data,
3480 * since the data doesn't result from a drop.
3483 set_status_pending (GdkDragContext *context,
3484 GdkDragAction suggested_action)
3486 g_object_set_data (G_OBJECT (context),
3487 I_("gtk-calendar-status-pending"),
3488 GINT_TO_POINTER (suggested_action));
3491 static GdkDragAction
3492 get_status_pending (GdkDragContext *context)
3494 return GPOINTER_TO_INT (g_object_get_data (G_OBJECT (context),
3495 "gtk-calendar-status-pending"));
3499 gtk_calendar_drag_leave (GtkWidget *widget,
3500 GdkDragContext *context,
3503 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3505 priv->drag_highlight = 0;
3506 gtk_drag_unhighlight (widget);
3511 gtk_calendar_drag_motion (GtkWidget *widget,
3512 GdkDragContext *context,
3517 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3520 if (!priv->drag_highlight)
3522 priv->drag_highlight = 1;
3523 gtk_drag_highlight (widget);
3526 target = gtk_drag_dest_find_target (widget, context, NULL);
3527 if (target == GDK_NONE || context->suggested_action == 0)
3528 gdk_drag_status (context, 0, time);
3531 set_status_pending (context, context->suggested_action);
3532 gtk_drag_get_data (widget, context, target, time);
3539 gtk_calendar_drag_drop (GtkWidget *widget,
3540 GdkDragContext *context,
3547 target = gtk_drag_dest_find_target (widget, context, NULL);
3548 if (target != GDK_NONE)
3550 gtk_drag_get_data (widget, context,
3560 gtk_calendar_drag_data_received (GtkWidget *widget,
3561 GdkDragContext *context,
3564 GtkSelectionData *selection_data,
3568 GtkCalendar *calendar = GTK_CALENDAR (widget);
3569 guint day, month, year;
3572 GdkDragAction suggested_action;
3574 suggested_action = get_status_pending (context);
3576 if (suggested_action)
3578 set_status_pending (context, 0);
3580 /* We are getting this data due to a request in drag_motion,
3581 * rather than due to a request in drag_drop, so we are just
3582 * supposed to call drag_status, not actually paste in the
3585 str = (gchar*) gtk_selection_data_get_text (selection_data);
3589 date = g_date_new ();
3590 g_date_set_parse (date, str);
3591 if (!g_date_valid (date))
3592 suggested_action = 0;
3597 suggested_action = 0;
3599 gdk_drag_status (context, suggested_action, time);
3604 date = g_date_new ();
3605 str = (gchar*) gtk_selection_data_get_text (selection_data);
3608 g_date_set_parse (date, str);
3612 if (!g_date_valid (date))
3614 g_warning ("Received invalid date data\n");
3616 gtk_drag_finish (context, FALSE, FALSE, time);
3620 day = g_date_get_day (date);
3621 month = g_date_get_month (date);
3622 year = g_date_get_year (date);
3625 gtk_drag_finish (context, TRUE, FALSE, time);
3628 g_object_freeze_notify (G_OBJECT (calendar));
3629 if (!(calendar->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
3630 && (calendar->display_flags & GTK_CALENDAR_SHOW_HEADING))
3631 gtk_calendar_select_month (calendar, month - 1, year);
3632 gtk_calendar_select_day (calendar, day);
3633 g_object_thaw_notify (G_OBJECT (calendar));
3637 /****************************************
3639 ****************************************/
3644 * Creates a new calendar, with the current date being selected.
3646 * Return value: a newly #GtkCalendar widget
3649 gtk_calendar_new (void)
3651 return g_object_new (GTK_TYPE_CALENDAR, NULL);
3655 * gtk_calendar_get_display_options:
3656 * @calendar: a #GtkCalendar
3658 * Returns the current display options of @calendar.
3660 * Return value: the display options.
3664 GtkCalendarDisplayOptions
3665 gtk_calendar_get_display_options (GtkCalendar *calendar)
3667 g_return_val_if_fail (GTK_IS_CALENDAR (calendar), 0);
3669 return calendar->display_flags;
3673 * gtk_calendar_set_display_options:
3674 * @calendar: a #GtkCalendar
3675 * @flags: the display options to set
3677 * Sets display options (whether to display the heading and the month
3683 gtk_calendar_set_display_options (GtkCalendar *calendar,
3684 GtkCalendarDisplayOptions flags)
3686 GtkWidget *widget = GTK_WIDGET (calendar);
3687 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
3690 GtkCalendarDisplayOptions old_flags;
3692 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3694 old_flags = calendar->display_flags;
3696 if (gtk_widget_get_realized (widget))
3698 if ((flags ^ calendar->display_flags) & GTK_CALENDAR_NO_MONTH_CHANGE)
3701 if (! (flags & GTK_CALENDAR_NO_MONTH_CHANGE)
3702 && (priv->header_win))
3704 calendar->display_flags &= ~GTK_CALENDAR_NO_MONTH_CHANGE;
3705 calendar_realize_arrows (calendar);
3709 for (i = 0; i < 4; i++)
3711 if (priv->arrow_win[i])
3713 gdk_window_set_user_data (priv->arrow_win[i],
3715 gdk_window_destroy (priv->arrow_win[i]);
3716 priv->arrow_win[i] = NULL;
3722 if ((flags ^ calendar->display_flags) & GTK_CALENDAR_SHOW_HEADING)
3726 if (flags & GTK_CALENDAR_SHOW_HEADING)
3728 calendar->display_flags |= GTK_CALENDAR_SHOW_HEADING;
3729 calendar_realize_header (calendar);
3733 for (i = 0; i < 4; i++)
3735 if (priv->arrow_win[i])
3737 gdk_window_set_user_data (priv->arrow_win[i],
3739 gdk_window_destroy (priv->arrow_win[i]);
3740 priv->arrow_win[i] = NULL;
3743 gdk_window_set_user_data (priv->header_win, NULL);
3744 gdk_window_destroy (priv->header_win);
3745 priv->header_win = NULL;
3750 if ((flags ^ calendar->display_flags) & GTK_CALENDAR_SHOW_DAY_NAMES)
3754 if (flags & GTK_CALENDAR_SHOW_DAY_NAMES)
3756 calendar->display_flags |= GTK_CALENDAR_SHOW_DAY_NAMES;
3757 calendar_realize_day_names (calendar);
3761 gdk_window_set_user_data (priv->day_name_win, NULL);
3762 gdk_window_destroy (priv->day_name_win);
3763 priv->day_name_win = NULL;
3767 if ((flags ^ calendar->display_flags) & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
3771 if (flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
3773 calendar->display_flags |= GTK_CALENDAR_SHOW_WEEK_NUMBERS;
3774 calendar_realize_week_numbers (calendar);
3778 gdk_window_set_user_data (priv->week_win, NULL);
3779 gdk_window_destroy (priv->week_win);
3780 priv->week_win = NULL;
3784 if ((flags ^ calendar->display_flags) & GTK_CALENDAR_WEEK_START_MONDAY)
3785 g_warning ("GTK_CALENDAR_WEEK_START_MONDAY is ignored; the first day of the week is determined from the locale");
3787 if ((flags ^ calendar->display_flags) & GTK_CALENDAR_SHOW_DETAILS)
3790 calendar->display_flags = flags;
3792 gtk_widget_queue_resize (GTK_WIDGET (calendar));
3796 calendar->display_flags = flags;
3798 g_object_freeze_notify (G_OBJECT (calendar));
3799 if ((old_flags ^ calendar->display_flags) & GTK_CALENDAR_SHOW_HEADING)
3800 g_object_notify (G_OBJECT (calendar), "show-heading");
3801 if ((old_flags ^ calendar->display_flags) & GTK_CALENDAR_SHOW_DAY_NAMES)
3802 g_object_notify (G_OBJECT (calendar), "show-day-names");
3803 if ((old_flags ^ calendar->display_flags) & GTK_CALENDAR_NO_MONTH_CHANGE)
3804 g_object_notify (G_OBJECT (calendar), "no-month-change");
3805 if ((old_flags ^ calendar->display_flags) & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
3806 g_object_notify (G_OBJECT (calendar), "show-week-numbers");
3807 g_object_thaw_notify (G_OBJECT (calendar));
3811 * gtk_calendar_select_month:
3812 * @calendar: a #GtkCalendar
3813 * @month: a month number between 0 and 11.
3814 * @year: the year the month is in.
3816 * Shifts the calendar to a different month.
3819 gtk_calendar_select_month (GtkCalendar *calendar,
3823 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3824 g_return_if_fail (month <= 11);
3826 calendar->month = month;
3827 calendar->year = year;
3829 calendar_compute_days (calendar);
3830 calendar_queue_refresh (calendar);
3832 g_object_freeze_notify (G_OBJECT (calendar));
3833 g_object_notify (G_OBJECT (calendar), "month");
3834 g_object_notify (G_OBJECT (calendar), "year");
3835 g_object_thaw_notify (G_OBJECT (calendar));
3837 g_signal_emit (calendar,
3838 gtk_calendar_signals[MONTH_CHANGED_SIGNAL],
3843 * gtk_calendar_select_day:
3844 * @calendar: a #GtkCalendar.
3845 * @day: the day number between 1 and 31, or 0 to unselect
3846 * the currently selected day.
3848 * Selects a day from the current month.
3851 gtk_calendar_select_day (GtkCalendar *calendar,
3854 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3855 g_return_if_fail (day <= 31);
3857 /* Deselect the old day */
3858 if (calendar->selected_day > 0)
3862 selected_day = calendar->selected_day;
3863 calendar->selected_day = 0;
3864 if (gtk_widget_is_drawable (GTK_WIDGET (calendar)))
3865 calendar_invalidate_day_num (calendar, selected_day);
3868 calendar->selected_day = day;
3870 /* Select the new day */
3873 if (gtk_widget_is_drawable (GTK_WIDGET (calendar)))
3874 calendar_invalidate_day_num (calendar, day);
3877 g_object_notify (G_OBJECT (calendar), "day");
3879 g_signal_emit (calendar,
3880 gtk_calendar_signals[DAY_SELECTED_SIGNAL],
3885 * gtk_calendar_clear_marks:
3886 * @calendar: a #GtkCalendar
3888 * Remove all visual markers.
3891 gtk_calendar_clear_marks (GtkCalendar *calendar)
3895 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3897 for (day = 0; day < 31; day++)
3899 calendar->marked_date[day] = FALSE;
3902 calendar->num_marked_dates = 0;
3903 calendar_queue_refresh (calendar);
3907 * gtk_calendar_mark_day:
3908 * @calendar: a #GtkCalendar
3909 * @day: the day number to mark between 1 and 31.
3911 * Places a visual marker on a particular day.
3914 gtk_calendar_mark_day (GtkCalendar *calendar,
3917 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3919 if (day >= 1 && day <= 31 && !calendar->marked_date[day-1])
3921 calendar->marked_date[day - 1] = TRUE;
3922 calendar->num_marked_dates++;
3923 calendar_invalidate_day_num (calendar, day);
3928 * gtk_calendar_unmark_day:
3929 * @calendar: a #GtkCalendar.
3930 * @day: the day number to unmark between 1 and 31.
3932 * Removes the visual marker from a particular day.
3935 gtk_calendar_unmark_day (GtkCalendar *calendar,
3938 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3940 if (day >= 1 && day <= 31 && calendar->marked_date[day-1])
3942 calendar->marked_date[day - 1] = FALSE;
3943 calendar->num_marked_dates--;
3944 calendar_invalidate_day_num (calendar, day);
3949 * gtk_calendar_get_date:
3950 * @calendar: a #GtkCalendar
3951 * @year: (allow-none): location to store the year number, or %NULL
3952 * @month: (allow-none): location to store the month number (between 0 and 11), or %NULL
3953 * @day: (allow-none): location to store the day number (between 1 and 31), or %NULL
3955 * Obtains the selected date from a #GtkCalendar.
3958 gtk_calendar_get_date (GtkCalendar *calendar,
3963 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3966 *year = calendar->year;
3969 *month = calendar->month;
3972 *day = calendar->selected_day;
3976 * gtk_calendar_set_detail_func:
3977 * @calendar: a #GtkCalendar.
3978 * @func: a function providing details for each day.
3979 * @data: data to pass to @func invokations.
3980 * @destroy: a function for releasing @data.
3982 * Installs a function which provides Pango markup with detail information
3983 * for each day. Examples for such details are holidays or appointments. That
3984 * information is shown below each day when #GtkCalendar:show-details is set.
3985 * A tooltip containing with full detail information is provided, if the entire
3986 * text should not fit into the details area, or if #GtkCalendar:show-details
3989 * The size of the details area can be restricted by setting the
3990 * #GtkCalendar:detail-width-chars and #GtkCalendar:detail-height-rows
3996 gtk_calendar_set_detail_func (GtkCalendar *calendar,
3997 GtkCalendarDetailFunc func,
3999 GDestroyNotify destroy)
4001 GtkCalendarPrivate *priv;
4003 g_return_if_fail (GTK_IS_CALENDAR (calendar));
4005 priv = GTK_CALENDAR_GET_PRIVATE (calendar);
4007 if (priv->detail_func_destroy)
4008 priv->detail_func_destroy (priv->detail_func_user_data);
4010 priv->detail_func = func;
4011 priv->detail_func_user_data = data;
4012 priv->detail_func_destroy = destroy;
4014 gtk_widget_set_has_tooltip (GTK_WIDGET (calendar),
4015 NULL != priv->detail_func);
4016 gtk_widget_queue_resize (GTK_WIDGET (calendar));
4020 * gtk_calendar_set_detail_width_chars:
4021 * @calendar: a #GtkCalendar.
4022 * @chars: detail width in characters.
4024 * Updates the width of detail cells.
4025 * See #GtkCalendar:detail-width-chars.
4030 gtk_calendar_set_detail_width_chars (GtkCalendar *calendar,
4033 GtkCalendarPrivate *priv;
4035 g_return_if_fail (GTK_IS_CALENDAR (calendar));
4037 priv = GTK_CALENDAR_GET_PRIVATE (calendar);
4039 if (chars != priv->detail_width_chars)
4041 priv->detail_width_chars = chars;
4042 g_object_notify (G_OBJECT (calendar), "detail-width-chars");
4043 gtk_widget_queue_resize_no_redraw (GTK_WIDGET (calendar));
4048 * gtk_calendar_set_detail_height_rows:
4049 * @calendar: a #GtkCalendar.
4050 * @rows: detail height in rows.
4052 * Updates the height of detail cells.
4053 * See #GtkCalendar:detail-height-rows.
4058 gtk_calendar_set_detail_height_rows (GtkCalendar *calendar,
4061 GtkCalendarPrivate *priv;
4063 g_return_if_fail (GTK_IS_CALENDAR (calendar));
4065 priv = GTK_CALENDAR_GET_PRIVATE (calendar);
4067 if (rows != priv->detail_height_rows)
4069 priv->detail_height_rows = rows;
4070 g_object_notify (G_OBJECT (calendar), "detail-height-rows");
4071 gtk_widget_queue_resize_no_redraw (GTK_WIDGET (calendar));
4076 * gtk_calendar_get_detail_width_chars:
4077 * @calendar: a #GtkCalendar.
4079 * Queries the width of detail cells, in characters.
4080 * See #GtkCalendar:detail-width-chars.
4084 * Return value: The width of detail cells, in characters.
4087 gtk_calendar_get_detail_width_chars (GtkCalendar *calendar)
4089 g_return_val_if_fail (GTK_IS_CALENDAR (calendar), 0);
4090 return GTK_CALENDAR_GET_PRIVATE (calendar)->detail_width_chars;
4094 * gtk_calendar_get_detail_height_rows:
4095 * @calendar: a #GtkCalendar.
4097 * Queries the height of detail cells, in rows.
4098 * See #GtkCalendar:detail-width-chars.
4102 * Return value: The height of detail cells, in rows.
4105 gtk_calendar_get_detail_height_rows (GtkCalendar *calendar)
4107 g_return_val_if_fail (GTK_IS_CALENDAR (calendar), 0);
4108 return GTK_CALENDAR_GET_PRIVATE (calendar)->detail_height_rows;