1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
5 * Copyright (C) 1998 Cesar Miquel, Shawn T. Amundson and Mattias Groenlund
8 * Copyright (c) 1995, 1996, 1997, 1998 by Steffen Beyer
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free
22 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
27 * file for a list of people on the GTK+ Team. See the ChangeLog
28 * files for a list of changes. These files are distributed with
29 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
34 * @Short_description: Displays a calendar and allows the user to select a date
37 * #GtkCalendar is a widget that displays a calendar, one month at a time. It
38 * can be created with gtk_calendar_new().
40 * The month and year currently displayed can be altered with
41 * gtk_calendar_select_month(). The exact day can be selected from the displayed
42 * month using gtk_calendar_select_day().
44 * To place a visual marker on a particular day, use gtk_calendar_mark_day() and
45 * to remove the marker, gtk_calendar_unmark_day(). Alternative, all marks can
46 * be cleared with gtk_calendar_clear_marks().
48 * The way in which the calendar itself is displayed can be altered using
49 * gtk_calendar_set_display_options().
51 * The selected date can be retrieved from a #GtkCalendar using
52 * gtk_calendar_get_date().
57 #ifdef HAVE_SYS_TIME_H
60 #ifdef HAVE__NL_TIME_FIRST_WEEKDAY
73 #include "gtkcalendar.h"
77 #include "gtkmarshalers.h"
78 #include "gtktooltip.h"
79 #include "gtkprivate.h"
80 #include "gdk/gdkkeysyms.h"
82 /***************************************************************************/
83 /* The following date routines are taken from the lib_date package.
84 * They have been minimally edited to avoid conflict with types defined
88 static const guint month_length[2][13] =
90 { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
91 { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
94 static const guint days_in_months[2][14] =
96 { 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
97 { 0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
100 static glong calc_days(guint year, guint mm, guint dd);
101 static guint day_of_week(guint year, guint mm, guint dd);
102 static glong dates_difference(guint year1, guint mm1, guint dd1,
103 guint year2, guint mm2, guint dd2);
104 static guint weeks_in_year(guint year);
109 return((((year % 4) == 0) && ((year % 100) != 0)) || ((year % 400) == 0));
113 day_of_week (guint year, guint mm, guint dd)
117 days = calc_days(year, mm, dd);
124 return( (guint) days );
127 static guint weeks_in_year(guint year)
129 return(52 + ((day_of_week(year,1,1)==4) || (day_of_week(year,12,31)==4)));
133 check_date(guint year, guint mm, guint dd)
135 if (year < 1) return FALSE;
136 if ((mm < 1) || (mm > 12)) return FALSE;
137 if ((dd < 1) || (dd > month_length[leap(year)][mm])) return FALSE;
142 week_number(guint year, guint mm, guint dd)
146 first = day_of_week(year,1,1) - 1;
147 return( (guint) ( (dates_difference(year,1,1, year,mm,dd) + first) / 7L ) +
152 year_to_days(guint year)
154 return( year * 365L + (year / 4) - (year / 100) + (year / 400) );
159 calc_days(guint year, guint mm, guint dd)
163 if (year < 1) return(0L);
164 if ((mm < 1) || (mm > 12)) return(0L);
165 if ((dd < 1) || (dd > month_length[(lp = leap(year))][mm])) return(0L);
166 return( year_to_days(--year) + days_in_months[lp][mm] + dd );
170 week_of_year(guint *week, guint *year, guint mm, guint dd)
172 if (check_date(*year,mm,dd))
174 *week = week_number(*year,mm,dd);
176 *week = weeks_in_year(--(*year));
177 else if (*week > weeks_in_year(*year))
188 dates_difference(guint year1, guint mm1, guint dd1,
189 guint year2, guint mm2, guint dd2)
191 return( calc_days(year2, mm2, dd2) - calc_days(year1, mm1, dd1) );
194 /*** END OF lib_date routines ********************************************/
196 /* Spacing around day/week headers and main area, inside those windows */
197 #define CALENDAR_MARGIN 0
199 #define DAY_XSEP 0 /* not really good for small calendar */
200 #define DAY_YSEP 0 /* not really good for small calendar */
202 #define SCROLL_DELAY_FACTOR 5
205 #define HEADER_FG_COLOR(widget) (& gtk_widget_get_style (widget)->fg[gtk_widget_get_state (widget)])
206 #define HEADER_BG_COLOR(widget) (& gtk_widget_get_style (widget)->bg[gtk_widget_get_state (widget)])
207 #define SELECTED_BG_COLOR(widget) (& gtk_widget_get_style (widget)->base[gtk_widget_has_focus (widget) ? GTK_STATE_SELECTED : GTK_STATE_ACTIVE])
208 #define SELECTED_FG_COLOR(widget) (& gtk_widget_get_style (widget)->text[gtk_widget_has_focus (widget) ? GTK_STATE_SELECTED : GTK_STATE_ACTIVE])
209 #define NORMAL_DAY_COLOR(widget) (& gtk_widget_get_style (widget)->text[gtk_widget_get_state (widget)])
210 #define PREV_MONTH_COLOR(widget) (& gtk_widget_get_style (widget)->mid[gtk_widget_get_state (widget)])
211 #define NEXT_MONTH_COLOR(widget) (& gtk_widget_get_style (widget)->mid[gtk_widget_get_state (widget)])
212 #define MARKED_COLOR(widget) (& gtk_widget_get_style (widget)->text[gtk_widget_get_state (widget)])
213 #define BACKGROUND_COLOR(widget) (& gtk_widget_get_style (widget)->base[gtk_widget_get_state (widget)])
214 #define HIGHLIGHT_BACK_COLOR(widget) (& gtk_widget_get_style (widget)->mid[gtk_widget_get_state (widget)])
230 MONTH_CHANGED_SIGNAL,
232 DAY_SELECTED_DOUBLE_CLICK_SIGNAL,
248 PROP_NO_MONTH_CHANGE,
249 PROP_SHOW_WEEK_NUMBERS,
251 PROP_DETAIL_WIDTH_CHARS,
252 PROP_DETAIL_HEIGHT_ROWS
255 static guint gtk_calendar_signals[LAST_SIGNAL] = { 0 };
257 struct _GtkCalendarPrivate
259 GtkCalendarDisplayOptions display_flags;
260 GtkStyle *header_style;
261 GtkStyle *label_style;
263 GdkColor marked_date_color[31];
265 GdkWindow *arrow_win[4];
267 gchar grow_space [32];
273 gint day_month[6][7];
276 gint num_marked_dates;
277 gint marked_date[31];
286 guint arrow_state[4];
288 guint max_month_width;
289 guint max_year_width;
295 guint max_day_char_width;
296 guint max_day_char_ascent;
297 guint max_day_char_descent;
298 guint max_label_char_ascent;
299 guint max_label_char_descent;
300 guint max_week_char_width;
303 guint year_before : 1;
305 guint need_timer : 1;
308 guint drag_highlight : 1;
318 /* Optional callback, used to display extra information for each day. */
319 GtkCalendarDetailFunc detail_func;
320 gpointer detail_func_user_data;
321 GDestroyNotify detail_func_destroy;
323 /* Size requistion for details provided by the hook. */
324 gint detail_height_rows;
325 gint detail_width_chars;
326 gint detail_overflow[6];
329 #define GTK_CALENDAR_GET_PRIVATE(widget) (GTK_CALENDAR (widget)->priv)
331 static void gtk_calendar_finalize (GObject *calendar);
332 static void gtk_calendar_destroy (GtkWidget *widget);
333 static void gtk_calendar_set_property (GObject *object,
337 static void gtk_calendar_get_property (GObject *object,
342 static void gtk_calendar_realize (GtkWidget *widget);
343 static void gtk_calendar_unrealize (GtkWidget *widget);
344 static void gtk_calendar_map (GtkWidget *widget);
345 static void gtk_calendar_unmap (GtkWidget *widget);
346 static void gtk_calendar_get_preferred_width (GtkWidget *widget,
349 static void gtk_calendar_get_preferred_height (GtkWidget *widget,
352 static void gtk_calendar_size_allocate (GtkWidget *widget,
353 GtkAllocation *allocation);
354 static gboolean gtk_calendar_draw (GtkWidget *widget,
356 static gboolean gtk_calendar_button_press (GtkWidget *widget,
357 GdkEventButton *event);
358 static gboolean gtk_calendar_button_release (GtkWidget *widget,
359 GdkEventButton *event);
360 static gboolean gtk_calendar_motion_notify (GtkWidget *widget,
361 GdkEventMotion *event);
362 static gboolean gtk_calendar_enter_notify (GtkWidget *widget,
363 GdkEventCrossing *event);
364 static gboolean gtk_calendar_leave_notify (GtkWidget *widget,
365 GdkEventCrossing *event);
366 static gboolean gtk_calendar_scroll (GtkWidget *widget,
367 GdkEventScroll *event);
368 static gboolean gtk_calendar_key_press (GtkWidget *widget,
370 static gboolean gtk_calendar_focus_out (GtkWidget *widget,
371 GdkEventFocus *event);
372 static void gtk_calendar_grab_notify (GtkWidget *widget,
373 gboolean was_grabbed);
374 static void gtk_calendar_state_changed (GtkWidget *widget,
375 GtkStateType previous_state);
376 static gboolean gtk_calendar_query_tooltip (GtkWidget *widget,
379 gboolean keyboard_mode,
380 GtkTooltip *tooltip);
382 static void gtk_calendar_drag_data_get (GtkWidget *widget,
383 GdkDragContext *context,
384 GtkSelectionData *selection_data,
387 static void gtk_calendar_drag_data_received (GtkWidget *widget,
388 GdkDragContext *context,
391 GtkSelectionData *selection_data,
394 static gboolean gtk_calendar_drag_motion (GtkWidget *widget,
395 GdkDragContext *context,
399 static void gtk_calendar_drag_leave (GtkWidget *widget,
400 GdkDragContext *context,
402 static gboolean gtk_calendar_drag_drop (GtkWidget *widget,
403 GdkDragContext *context,
408 static void calendar_start_spinning (GtkCalendar *calendar,
410 static void calendar_stop_spinning (GtkCalendar *calendar);
412 static void calendar_invalidate_day (GtkCalendar *widget,
415 static void calendar_invalidate_day_num (GtkCalendar *widget,
417 static void calendar_invalidate_arrow (GtkCalendar *widget,
420 static void calendar_compute_days (GtkCalendar *calendar);
421 static gint calendar_get_xsep (GtkCalendar *calendar);
422 static gint calendar_get_ysep (GtkCalendar *calendar);
423 static gint calendar_get_inner_border (GtkCalendar *calendar);
425 static char *default_abbreviated_dayname[7];
426 static char *default_monthname[12];
428 G_DEFINE_TYPE (GtkCalendar, gtk_calendar, GTK_TYPE_WIDGET)
431 gtk_calendar_class_init (GtkCalendarClass *class)
433 GObjectClass *gobject_class;
434 GtkWidgetClass *widget_class;
436 gobject_class = (GObjectClass*) class;
437 widget_class = (GtkWidgetClass*) class;
439 gobject_class->set_property = gtk_calendar_set_property;
440 gobject_class->get_property = gtk_calendar_get_property;
441 gobject_class->finalize = gtk_calendar_finalize;
443 widget_class->destroy = gtk_calendar_destroy;
444 widget_class->realize = gtk_calendar_realize;
445 widget_class->unrealize = gtk_calendar_unrealize;
446 widget_class->map = gtk_calendar_map;
447 widget_class->unmap = gtk_calendar_unmap;
448 widget_class->draw = gtk_calendar_draw;
449 widget_class->get_preferred_width = gtk_calendar_get_preferred_width;
450 widget_class->get_preferred_height = gtk_calendar_get_preferred_height;
451 widget_class->size_allocate = gtk_calendar_size_allocate;
452 widget_class->button_press_event = gtk_calendar_button_press;
453 widget_class->button_release_event = gtk_calendar_button_release;
454 widget_class->motion_notify_event = gtk_calendar_motion_notify;
455 widget_class->enter_notify_event = gtk_calendar_enter_notify;
456 widget_class->leave_notify_event = gtk_calendar_leave_notify;
457 widget_class->key_press_event = gtk_calendar_key_press;
458 widget_class->scroll_event = gtk_calendar_scroll;
459 widget_class->state_changed = gtk_calendar_state_changed;
460 widget_class->grab_notify = gtk_calendar_grab_notify;
461 widget_class->focus_out_event = gtk_calendar_focus_out;
462 widget_class->query_tooltip = gtk_calendar_query_tooltip;
464 widget_class->drag_data_get = gtk_calendar_drag_data_get;
465 widget_class->drag_motion = gtk_calendar_drag_motion;
466 widget_class->drag_leave = gtk_calendar_drag_leave;
467 widget_class->drag_drop = gtk_calendar_drag_drop;
468 widget_class->drag_data_received = gtk_calendar_drag_data_received;
474 * This property gets initially set to the current year.
476 g_object_class_install_property (gobject_class,
478 g_param_spec_int ("year",
480 P_("The selected year"),
482 GTK_PARAM_READWRITE));
487 * The selected month (as a number between 0 and 11).
488 * This property gets initially set to the current month.
490 g_object_class_install_property (gobject_class,
492 g_param_spec_int ("month",
494 P_("The selected month (as a number between 0 and 11)"),
496 GTK_PARAM_READWRITE));
501 * The selected day (as a number between 1 and 31, or 0
502 * to unselect the currently selected day).
503 * This property gets initially set to the current day.
505 g_object_class_install_property (gobject_class,
507 g_param_spec_int ("day",
509 P_("The selected day (as a number between 1 and 31, or 0 to unselect the currently selected day)"),
511 GTK_PARAM_READWRITE));
514 * GtkCalendar:show-heading:
516 * Determines whether a heading is displayed.
520 g_object_class_install_property (gobject_class,
522 g_param_spec_boolean ("show-heading",
524 P_("If TRUE, a heading is displayed"),
526 GTK_PARAM_READWRITE));
529 * GtkCalendar:show-day-names:
531 * Determines whether day names are displayed.
535 g_object_class_install_property (gobject_class,
537 g_param_spec_boolean ("show-day-names",
538 P_("Show Day Names"),
539 P_("If TRUE, day names are displayed"),
541 GTK_PARAM_READWRITE));
543 * GtkCalendar:no-month-change:
545 * Determines whether the selected month can be changed.
549 g_object_class_install_property (gobject_class,
550 PROP_NO_MONTH_CHANGE,
551 g_param_spec_boolean ("no-month-change",
552 P_("No Month Change"),
553 P_("If TRUE, the selected month cannot be changed"),
555 GTK_PARAM_READWRITE));
558 * GtkCalendar:show-week-numbers:
560 * Determines whether week numbers are displayed.
564 g_object_class_install_property (gobject_class,
565 PROP_SHOW_WEEK_NUMBERS,
566 g_param_spec_boolean ("show-week-numbers",
567 P_("Show Week Numbers"),
568 P_("If TRUE, week numbers are displayed"),
570 GTK_PARAM_READWRITE));
573 * GtkCalendar:detail-width-chars:
575 * Width of a detail cell, in characters.
576 * A value of 0 allows any width. See gtk_calendar_set_detail_func().
580 g_object_class_install_property (gobject_class,
581 PROP_DETAIL_WIDTH_CHARS,
582 g_param_spec_int ("detail-width-chars",
584 P_("Details width in characters"),
586 GTK_PARAM_READWRITE));
589 * GtkCalendar:detail-height-rows:
591 * Height of a detail cell, in rows.
592 * A value of 0 allows any width. See gtk_calendar_set_detail_func().
596 g_object_class_install_property (gobject_class,
597 PROP_DETAIL_HEIGHT_ROWS,
598 g_param_spec_int ("detail-height-rows",
599 P_("Details Height"),
600 P_("Details height in rows"),
602 GTK_PARAM_READWRITE));
605 * GtkCalendar:show-details:
607 * Determines whether details are shown directly in the widget, or if they are
608 * available only as tooltip. When this property is set days with details are
613 g_object_class_install_property (gobject_class,
615 g_param_spec_boolean ("show-details",
617 P_("If TRUE, details are shown"),
619 GTK_PARAM_READWRITE));
623 * GtkCalendar:inner-border
625 * The spacing around the day/week headers and main area.
627 gtk_widget_class_install_style_property (widget_class,
628 g_param_spec_int ("inner-border",
630 P_("Inner border space"),
632 GTK_PARAM_READABLE));
635 * GtkCalndar:vertical-separation
637 * Separation between day headers and main area.
639 gtk_widget_class_install_style_property (widget_class,
640 g_param_spec_int ("vertical-separation",
641 P_("Vertical separation"),
642 P_("Space between day headers and main area"),
644 GTK_PARAM_READABLE));
647 * GtkCalendar:horizontal-separation
649 * Separation between week headers and main area.
651 gtk_widget_class_install_style_property (widget_class,
652 g_param_spec_int ("horizontal-separation",
653 P_("Horizontal separation"),
654 P_("Space between week headers and main area"),
656 GTK_PARAM_READABLE));
659 * GtkCalendar::month-changed:
660 * @calendar: the object which received the signal.
662 * Emitted when the user clicks a button to change the selected month on a
665 gtk_calendar_signals[MONTH_CHANGED_SIGNAL] =
666 g_signal_new (I_("month-changed"),
667 G_OBJECT_CLASS_TYPE (gobject_class),
669 G_STRUCT_OFFSET (GtkCalendarClass, month_changed),
671 _gtk_marshal_VOID__VOID,
675 * GtkCalendar::day-selected:
676 * @calendar: the object which received the signal.
678 * Emitted when the user selects a day.
680 gtk_calendar_signals[DAY_SELECTED_SIGNAL] =
681 g_signal_new (I_("day-selected"),
682 G_OBJECT_CLASS_TYPE (gobject_class),
684 G_STRUCT_OFFSET (GtkCalendarClass, day_selected),
686 _gtk_marshal_VOID__VOID,
690 * GtkCalendar::day-selected-double-click:
691 * @calendar: the object which received the signal.
693 * Emitted when the user double-clicks a day.
695 gtk_calendar_signals[DAY_SELECTED_DOUBLE_CLICK_SIGNAL] =
696 g_signal_new (I_("day-selected-double-click"),
697 G_OBJECT_CLASS_TYPE (gobject_class),
699 G_STRUCT_OFFSET (GtkCalendarClass, day_selected_double_click),
701 _gtk_marshal_VOID__VOID,
705 * GtkCalendar::prev-month:
706 * @calendar: the object which received the signal.
708 * Emitted when the user switched to the previous month.
710 gtk_calendar_signals[PREV_MONTH_SIGNAL] =
711 g_signal_new (I_("prev-month"),
712 G_OBJECT_CLASS_TYPE (gobject_class),
714 G_STRUCT_OFFSET (GtkCalendarClass, prev_month),
716 _gtk_marshal_VOID__VOID,
720 * GtkCalendar::next-month:
721 * @calendar: the object which received the signal.
723 * Emitted when the user switched to the next month.
725 gtk_calendar_signals[NEXT_MONTH_SIGNAL] =
726 g_signal_new (I_("next-month"),
727 G_OBJECT_CLASS_TYPE (gobject_class),
729 G_STRUCT_OFFSET (GtkCalendarClass, next_month),
731 _gtk_marshal_VOID__VOID,
735 * GtkCalendar::prev-year:
736 * @calendar: the object which received the signal.
738 * Emitted when user switched to the previous year.
740 gtk_calendar_signals[PREV_YEAR_SIGNAL] =
741 g_signal_new (I_("prev-year"),
742 G_OBJECT_CLASS_TYPE (gobject_class),
744 G_STRUCT_OFFSET (GtkCalendarClass, prev_year),
746 _gtk_marshal_VOID__VOID,
750 * GtkCalendar::next-year:
751 * @calendar: the object which received the signal.
753 * Emitted when user switched to the next year.
755 gtk_calendar_signals[NEXT_YEAR_SIGNAL] =
756 g_signal_new (I_("next-year"),
757 G_OBJECT_CLASS_TYPE (gobject_class),
759 G_STRUCT_OFFSET (GtkCalendarClass, next_year),
761 _gtk_marshal_VOID__VOID,
764 g_type_class_add_private (gobject_class, sizeof (GtkCalendarPrivate));
768 gtk_calendar_init (GtkCalendar *calendar)
770 GtkWidget *widget = GTK_WIDGET (calendar);
775 wchar_t wbuffer[100];
780 GtkCalendarPrivate *priv;
782 #ifdef HAVE__NL_TIME_FIRST_WEEKDAY
783 union { unsigned int word; char *string; } langinfo;
784 gint week_1stday = 0;
785 gint first_weekday = 1;
791 priv = calendar->priv = G_TYPE_INSTANCE_GET_PRIVATE (calendar,
795 gtk_widget_set_can_focus (widget, TRUE);
796 gtk_widget_set_has_window (widget, FALSE);
798 if (!default_abbreviated_dayname[0])
802 tmp_time= (i+3)*86400;
803 strftime ( buffer, sizeof (buffer), "%a", gmtime (&tmp_time));
804 default_abbreviated_dayname[i] = g_locale_to_utf8 (buffer, -1, NULL, NULL, NULL);
806 if (!GetLocaleInfoW (GetThreadLocale (), LOCALE_SABBREVDAYNAME1 + (i+6)%7,
807 wbuffer, G_N_ELEMENTS (wbuffer)))
808 default_abbreviated_dayname[i] = g_strdup_printf ("(%d)", i);
810 default_abbreviated_dayname[i] = g_utf16_to_utf8 (wbuffer, -1, NULL, NULL, NULL);
814 if (!default_monthname[0])
819 strftime ( buffer, sizeof (buffer), "%B", gmtime (&tmp_time));
820 default_monthname[i] = g_locale_to_utf8 (buffer, -1, NULL, NULL, NULL);
822 if (!GetLocaleInfoW (GetThreadLocale (), LOCALE_SMONTHNAME1 + i,
823 wbuffer, G_N_ELEMENTS (wbuffer)))
824 default_monthname[i] = g_strdup_printf ("(%d)", i);
826 default_monthname[i] = g_utf16_to_utf8 (wbuffer, -1, NULL, NULL, NULL);
832 tm = localtime (&secs);
833 priv->month = tm->tm_mon;
834 priv->year = 1900 + tm->tm_year;
837 priv->marked_date[i] = FALSE;
838 priv->num_marked_dates = 0;
839 priv->selected_day = tm->tm_mday;
841 priv->display_flags = (GTK_CALENDAR_SHOW_HEADING |
842 GTK_CALENDAR_SHOW_DAY_NAMES |
843 GTK_CALENDAR_SHOW_DETAILS);
845 priv->focus_row = -1;
846 priv->focus_col = -1;
848 priv->max_year_width = 0;
849 priv->max_month_width = 0;
850 priv->max_day_char_width = 0;
851 priv->max_week_char_width = 0;
853 priv->max_day_char_ascent = 0;
854 priv->max_day_char_descent = 0;
855 priv->max_label_char_ascent = 0;
856 priv->max_label_char_descent = 0;
858 priv->arrow_width = 10;
860 priv->need_timer = 0;
862 priv->click_child = -1;
865 priv->drag_highlight = 0;
867 gtk_drag_dest_set (widget, 0, NULL, 0, GDK_ACTION_COPY);
868 gtk_drag_dest_add_text_targets (widget);
870 priv->year_before = 0;
872 /* Translate to calendar:YM if you want years to be displayed
873 * before months; otherwise translate to calendar:MY.
874 * Do *not* translate it to anything else, if it
875 * it isn't calendar:YM or calendar:MY it will not work.
877 * Note that the ordering described here is logical order, which is
878 * further influenced by BIDI ordering. Thus, if you have a default
879 * text direction of RTL and specify "calendar:YM", then the year
880 * will appear to the right of the month.
882 year_before = _("calendar:MY");
883 if (strcmp (year_before, "calendar:YM") == 0)
884 priv->year_before = 1;
885 else if (strcmp (year_before, "calendar:MY") != 0)
886 g_warning ("Whoever translated calendar:MY did so wrongly.\n");
889 priv->week_start = 0;
892 if (GetLocaleInfoW (GetThreadLocale (), LOCALE_IFIRSTDAYOFWEEK,
893 wbuffer, G_N_ELEMENTS (wbuffer)))
894 week_start = g_utf16_to_utf8 (wbuffer, -1, NULL, NULL, NULL);
896 if (week_start != NULL)
898 priv->week_start = (week_start[0] - '0' + 1) % 7;
902 #ifdef HAVE__NL_TIME_FIRST_WEEKDAY
903 langinfo.string = nl_langinfo (_NL_TIME_FIRST_WEEKDAY);
904 first_weekday = langinfo.string[0];
905 langinfo.string = nl_langinfo (_NL_TIME_WEEK_1STDAY);
906 week_origin = langinfo.word;
907 if (week_origin == 19971130) /* Sunday */
909 else if (week_origin == 19971201) /* Monday */
912 g_warning ("Unknown value of _NL_TIME_WEEK_1STDAY.\n");
914 priv->week_start = (week_1stday + first_weekday - 1) % 7;
916 /* Translate to calendar:week_start:0 if you want Sunday to be the
917 * first day of the week to calendar:week_start:1 if you want Monday
918 * to be the first day of the week, and so on.
920 week_start = _("calendar:week_start:0");
922 if (strncmp (week_start, "calendar:week_start:", 20) == 0)
923 priv->week_start = *(week_start + 20) - '0';
925 priv->week_start = -1;
927 if (priv->week_start < 0 || priv->week_start > 6)
929 g_warning ("Whoever translated calendar:week_start:0 did so wrongly.\n");
930 priv->week_start = 0;
935 calendar_compute_days (calendar);
939 /****************************************
940 * Utility Functions *
941 ****************************************/
944 calendar_queue_refresh (GtkCalendar *calendar)
946 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
948 if (!(priv->detail_func) ||
949 !(priv->display_flags & GTK_CALENDAR_SHOW_DETAILS) ||
950 (priv->detail_width_chars && priv->detail_height_rows))
951 gtk_widget_queue_draw (GTK_WIDGET (calendar));
953 gtk_widget_queue_resize (GTK_WIDGET (calendar));
957 calendar_set_month_next (GtkCalendar *calendar)
960 GtkCalendarPrivate *priv = calendar->priv;
962 if (priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
965 if (priv->month == 11)
973 calendar_compute_days (calendar);
974 g_signal_emit (calendar,
975 gtk_calendar_signals[NEXT_MONTH_SIGNAL],
977 g_signal_emit (calendar,
978 gtk_calendar_signals[MONTH_CHANGED_SIGNAL],
981 month_len = month_length[leap (priv->year)][priv->month + 1];
983 if (month_len < priv->selected_day)
985 priv->selected_day = 0;
986 gtk_calendar_select_day (calendar, month_len);
989 gtk_calendar_select_day (calendar, priv->selected_day);
991 calendar_queue_refresh (calendar);
995 calendar_set_year_prev (GtkCalendar *calendar)
997 GtkCalendarPrivate *priv = calendar->priv;
1001 calendar_compute_days (calendar);
1002 g_signal_emit (calendar,
1003 gtk_calendar_signals[PREV_YEAR_SIGNAL],
1005 g_signal_emit (calendar,
1006 gtk_calendar_signals[MONTH_CHANGED_SIGNAL],
1009 month_len = month_length[leap (priv->year)][priv->month + 1];
1011 if (month_len < priv->selected_day)
1013 priv->selected_day = 0;
1014 gtk_calendar_select_day (calendar, month_len);
1017 gtk_calendar_select_day (calendar, priv->selected_day);
1019 calendar_queue_refresh (calendar);
1023 calendar_set_year_next (GtkCalendar *calendar)
1025 GtkCalendarPrivate *priv = calendar->priv;
1029 calendar_compute_days (calendar);
1030 g_signal_emit (calendar,
1031 gtk_calendar_signals[NEXT_YEAR_SIGNAL],
1033 g_signal_emit (calendar,
1034 gtk_calendar_signals[MONTH_CHANGED_SIGNAL],
1037 month_len = month_length[leap (priv->year)][priv->month + 1];
1039 if (month_len < priv->selected_day)
1041 priv->selected_day = 0;
1042 gtk_calendar_select_day (calendar, month_len);
1045 gtk_calendar_select_day (calendar, priv->selected_day);
1047 calendar_queue_refresh (calendar);
1051 calendar_compute_days (GtkCalendar *calendar)
1053 GtkCalendarPrivate *priv = calendar->priv;
1056 gint ndays_in_month;
1057 gint ndays_in_prev_month;
1064 month = priv->month + 1;
1066 ndays_in_month = month_length[leap (year)][month];
1068 first_day = day_of_week (year, month, 1);
1069 first_day = (first_day + 7 - priv->week_start) % 7;
1071 /* Compute days of previous month */
1073 ndays_in_prev_month = month_length[leap (year)][month-1];
1075 ndays_in_prev_month = month_length[leap (year)][12];
1076 day = ndays_in_prev_month - first_day + 1;
1081 for (col = 0; col < first_day; col++)
1083 priv->day[row][col] = day;
1084 priv->day_month[row][col] = MONTH_PREV;
1089 /* Compute days of current month */
1091 for (day = 1; day <= ndays_in_month; day++)
1093 priv->day[row][col] = day;
1094 priv->day_month[row][col] = MONTH_CURRENT;
1104 /* Compute days of next month */
1106 for (; row <= 5; row++)
1108 for (; col <= 6; col++)
1110 priv->day[row][col] = day;
1111 priv->day_month[row][col] = MONTH_NEXT;
1119 calendar_select_and_focus_day (GtkCalendar *calendar,
1122 GtkCalendarPrivate *priv = calendar->priv;
1123 gint old_focus_row = priv->focus_row;
1124 gint old_focus_col = priv->focus_col;
1128 for (row = 0; row < 6; row ++)
1129 for (col = 0; col < 7; col++)
1131 if (priv->day_month[row][col] == MONTH_CURRENT
1132 && priv->day[row][col] == day)
1134 priv->focus_row = row;
1135 priv->focus_col = col;
1139 if (old_focus_row != -1 && old_focus_col != -1)
1140 calendar_invalidate_day (calendar, old_focus_row, old_focus_col);
1142 gtk_calendar_select_day (calendar, day);
1146 /****************************************
1147 * Layout computation utilities *
1148 ****************************************/
1151 calendar_row_height (GtkCalendar *calendar)
1153 GtkCalendarPrivate *priv = calendar->priv;
1155 return (GTK_CALENDAR_GET_PRIVATE (calendar)->main_h - CALENDAR_MARGIN
1156 - ((priv->display_flags & GTK_CALENDAR_SHOW_DAY_NAMES)
1157 ? calendar_get_ysep (calendar) : CALENDAR_MARGIN)) / 6;
1161 /* calendar_left_x_for_column: returns the x coordinate
1162 * for the left of the column */
1164 calendar_left_x_for_column (GtkCalendar *calendar,
1167 GtkCalendarPrivate *priv = calendar->priv;
1171 gint calendar_xsep = calendar_get_xsep (calendar);
1173 gint inner_border = calendar_get_inner_border (calendar);
1175 style = gtk_widget_get_style (GTK_WIDGET (calendar));
1177 week_width = priv->week_width + style->xthickness + inner_border;
1179 if (gtk_widget_get_direction (GTK_WIDGET (calendar)) == GTK_TEXT_DIR_RTL)
1181 column = 6 - column;
1185 width = GTK_CALENDAR_GET_PRIVATE (calendar)->day_width;
1186 if (priv->display_flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
1187 x_left = week_width + calendar_xsep + (width + DAY_XSEP) * column;
1189 x_left = week_width + CALENDAR_MARGIN + (width + DAY_XSEP) * column;
1194 /* column_from_x: returns the column 0-6 that the
1195 * x pixel of the xwindow is in */
1197 calendar_column_from_x (GtkCalendar *calendar,
1201 gint x_left, x_right;
1205 for (c = 0; c < 7; c++)
1207 x_left = calendar_left_x_for_column (calendar, c);
1208 x_right = x_left + GTK_CALENDAR_GET_PRIVATE (calendar)->day_width;
1210 if (event_x >= x_left && event_x < x_right)
1220 /* calendar_top_y_for_row: returns the y coordinate
1221 * for the top of the row */
1223 calendar_top_y_for_row (GtkCalendar *calendar,
1227 GtkAllocation allocation;
1228 gint inner_border = calendar_get_inner_border (calendar);
1230 gtk_widget_get_allocation (GTK_WIDGET (calendar), &allocation);
1231 style = gtk_widget_get_style (GTK_WIDGET (calendar));
1233 return allocation.height
1234 - style->ythickness - inner_border
1235 - (CALENDAR_MARGIN + (6 - row)
1236 * calendar_row_height (calendar));
1239 /* row_from_y: returns the row 0-5 that the
1240 * y pixel of the xwindow is in */
1242 calendar_row_from_y (GtkCalendar *calendar,
1247 gint y_top, y_bottom;
1249 height = calendar_row_height (calendar);
1252 for (r = 0; r < 6; r++)
1254 y_top = calendar_top_y_for_row (calendar, r);
1255 y_bottom = y_top + height;
1257 if (event_y >= y_top && event_y < y_bottom)
1268 calendar_arrow_rectangle (GtkCalendar *calendar,
1272 GtkWidget *widget = GTK_WIDGET (calendar);
1273 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1274 GtkAllocation allocation;
1278 gtk_widget_get_allocation (widget, &allocation);
1279 style = gtk_widget_get_style (widget);
1281 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
1282 year_left = priv->year_before;
1284 year_left = !priv->year_before;
1287 rect->width = priv->arrow_width;
1288 rect->height = priv->header_h - 7;
1292 case ARROW_MONTH_LEFT:
1294 rect->x = (allocation.width - 2 * style->xthickness
1295 - (3 + 2 * priv->arrow_width + priv->max_month_width));
1299 case ARROW_MONTH_RIGHT:
1301 rect->x = (allocation.width - 2 * style->xthickness
1302 - 3 - priv->arrow_width);
1304 rect->x = (priv->arrow_width + priv->max_month_width);
1306 case ARROW_YEAR_LEFT:
1310 rect->x = (allocation.width - 2 * style->xthickness
1311 - (3 + 2 * priv->arrow_width + priv->max_year_width));
1313 case ARROW_YEAR_RIGHT:
1315 rect->x = (priv->arrow_width + priv->max_year_width);
1317 rect->x = (allocation.width - 2 * style->xthickness
1318 - 3 - priv->arrow_width);
1322 rect->x += style->xthickness;
1323 rect->y += style->ythickness;
1327 calendar_day_rectangle (GtkCalendar *calendar,
1332 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1334 rect->x = calendar_left_x_for_column (calendar, col);
1335 rect->y = calendar_top_y_for_row (calendar, row);
1336 rect->height = calendar_row_height (calendar);
1337 rect->width = priv->day_width;
1341 calendar_set_month_prev (GtkCalendar *calendar)
1343 GtkCalendarPrivate *priv = calendar->priv;
1346 if (priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
1349 if (priv->month == 0)
1357 month_len = month_length[leap (priv->year)][priv->month + 1];
1359 calendar_compute_days (calendar);
1361 g_signal_emit (calendar,
1362 gtk_calendar_signals[PREV_MONTH_SIGNAL],
1364 g_signal_emit (calendar,
1365 gtk_calendar_signals[MONTH_CHANGED_SIGNAL],
1368 if (month_len < priv->selected_day)
1370 priv->selected_day = 0;
1371 gtk_calendar_select_day (calendar, month_len);
1375 if (priv->selected_day < 0)
1376 priv->selected_day = priv->selected_day + 1 + month_length[leap (priv->year)][priv->month + 1];
1377 gtk_calendar_select_day (calendar, priv->selected_day);
1380 calendar_queue_refresh (calendar);
1384 /****************************************
1385 * Basic object methods *
1386 ****************************************/
1389 gtk_calendar_finalize (GObject *object)
1391 G_OBJECT_CLASS (gtk_calendar_parent_class)->finalize (object);
1395 gtk_calendar_destroy (GtkWidget *widget)
1397 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
1399 calendar_stop_spinning (GTK_CALENDAR (widget));
1401 /* Call the destroy function for the extra display callback: */
1402 if (priv->detail_func_destroy && priv->detail_func_user_data)
1404 priv->detail_func_destroy (priv->detail_func_user_data);
1405 priv->detail_func_user_data = NULL;
1406 priv->detail_func_destroy = NULL;
1409 GTK_WIDGET_CLASS (gtk_calendar_parent_class)->destroy (widget);
1414 calendar_set_display_option (GtkCalendar *calendar,
1415 GtkCalendarDisplayOptions flag,
1418 GtkCalendarPrivate *priv = calendar->priv;
1419 GtkCalendarDisplayOptions flags;
1422 flags = priv->display_flags | flag;
1424 flags = priv->display_flags & ~flag;
1425 gtk_calendar_set_display_options (calendar, flags);
1429 calendar_get_display_option (GtkCalendar *calendar,
1430 GtkCalendarDisplayOptions flag)
1432 GtkCalendarPrivate *priv = calendar->priv;
1434 return (priv->display_flags & flag) != 0;
1438 gtk_calendar_set_property (GObject *object,
1440 const GValue *value,
1443 GtkCalendar *calendar = GTK_CALENDAR (object);
1444 GtkCalendarPrivate *priv = calendar->priv;
1449 gtk_calendar_select_month (calendar,
1451 g_value_get_int (value));
1454 gtk_calendar_select_month (calendar,
1455 g_value_get_int (value),
1459 gtk_calendar_select_day (calendar,
1460 g_value_get_int (value));
1462 case PROP_SHOW_HEADING:
1463 calendar_set_display_option (calendar,
1464 GTK_CALENDAR_SHOW_HEADING,
1465 g_value_get_boolean (value));
1467 case PROP_SHOW_DAY_NAMES:
1468 calendar_set_display_option (calendar,
1469 GTK_CALENDAR_SHOW_DAY_NAMES,
1470 g_value_get_boolean (value));
1472 case PROP_NO_MONTH_CHANGE:
1473 calendar_set_display_option (calendar,
1474 GTK_CALENDAR_NO_MONTH_CHANGE,
1475 g_value_get_boolean (value));
1477 case PROP_SHOW_WEEK_NUMBERS:
1478 calendar_set_display_option (calendar,
1479 GTK_CALENDAR_SHOW_WEEK_NUMBERS,
1480 g_value_get_boolean (value));
1482 case PROP_SHOW_DETAILS:
1483 calendar_set_display_option (calendar,
1484 GTK_CALENDAR_SHOW_DETAILS,
1485 g_value_get_boolean (value));
1487 case PROP_DETAIL_WIDTH_CHARS:
1488 gtk_calendar_set_detail_width_chars (calendar,
1489 g_value_get_int (value));
1491 case PROP_DETAIL_HEIGHT_ROWS:
1492 gtk_calendar_set_detail_height_rows (calendar,
1493 g_value_get_int (value));
1496 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1502 gtk_calendar_get_property (GObject *object,
1507 GtkCalendar *calendar = GTK_CALENDAR (object);
1508 GtkCalendarPrivate *priv = calendar->priv;
1513 g_value_set_int (value, priv->year);
1516 g_value_set_int (value, priv->month);
1519 g_value_set_int (value, priv->selected_day);
1521 case PROP_SHOW_HEADING:
1522 g_value_set_boolean (value, calendar_get_display_option (calendar,
1523 GTK_CALENDAR_SHOW_HEADING));
1525 case PROP_SHOW_DAY_NAMES:
1526 g_value_set_boolean (value, calendar_get_display_option (calendar,
1527 GTK_CALENDAR_SHOW_DAY_NAMES));
1529 case PROP_NO_MONTH_CHANGE:
1530 g_value_set_boolean (value, calendar_get_display_option (calendar,
1531 GTK_CALENDAR_NO_MONTH_CHANGE));
1533 case PROP_SHOW_WEEK_NUMBERS:
1534 g_value_set_boolean (value, calendar_get_display_option (calendar,
1535 GTK_CALENDAR_SHOW_WEEK_NUMBERS));
1537 case PROP_SHOW_DETAILS:
1538 g_value_set_boolean (value, calendar_get_display_option (calendar,
1539 GTK_CALENDAR_SHOW_DETAILS));
1541 case PROP_DETAIL_WIDTH_CHARS:
1542 g_value_set_int (value, priv->detail_width_chars);
1544 case PROP_DETAIL_HEIGHT_ROWS:
1545 g_value_set_int (value, priv->detail_height_rows);
1548 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1554 /****************************************
1556 ****************************************/
1559 calendar_realize_arrows (GtkCalendar *calendar)
1561 GtkWidget *widget = GTK_WIDGET (calendar);
1562 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1563 GdkWindowAttr attributes;
1564 gint attributes_mask;
1566 GtkAllocation allocation;
1568 if (! (priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
1569 && (priv->display_flags & GTK_CALENDAR_SHOW_HEADING))
1571 gtk_widget_get_allocation (widget, &allocation);
1573 attributes.wclass = GDK_INPUT_ONLY;
1574 attributes.window_type = GDK_WINDOW_CHILD;
1575 attributes.event_mask = (gtk_widget_get_events (widget)
1576 | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
1577 | GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK);
1578 attributes_mask = GDK_WA_X | GDK_WA_Y;
1579 for (i = 0; i < 4; i++)
1582 calendar_arrow_rectangle (calendar, i, &rect);
1584 attributes.x = allocation.x + rect.x;
1585 attributes.y = allocation.y + rect.y;
1586 attributes.width = rect.width;
1587 attributes.height = rect.height;
1588 priv->arrow_win[i] = gdk_window_new (gtk_widget_get_window (widget),
1591 if (gtk_widget_is_sensitive (widget))
1592 priv->arrow_state[i] = GTK_STATE_NORMAL;
1594 priv->arrow_state[i] = GTK_STATE_INSENSITIVE;
1595 gdk_window_set_user_data (priv->arrow_win[i], widget);
1600 for (i = 0; i < 4; i++)
1601 priv->arrow_win[i] = NULL;
1606 calendar_unrealize_arrows (GtkCalendar *calendar)
1608 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1611 for (i = 0; i < 4; i++)
1613 if (priv->arrow_win[i])
1615 gdk_window_set_user_data (priv->arrow_win[i], NULL);
1616 gdk_window_destroy (priv->arrow_win[i]);
1617 priv->arrow_win[i] = NULL;
1624 calendar_get_inner_border (GtkCalendar *calendar)
1628 gtk_widget_style_get (GTK_WIDGET (calendar),
1629 "inner-border", &inner_border,
1632 return inner_border;
1636 calendar_get_xsep (GtkCalendar *calendar)
1640 gtk_widget_style_get (GTK_WIDGET (calendar),
1641 "horizontal-separation", &xsep,
1648 calendar_get_ysep (GtkCalendar *calendar)
1652 gtk_widget_style_get (GTK_WIDGET (calendar),
1653 "vertical-separation", &ysep,
1660 gtk_calendar_realize (GtkWidget *widget)
1662 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
1663 GdkWindowAttr attributes;
1664 gint attributes_mask;
1665 gint inner_border = calendar_get_inner_border (GTK_CALENDAR (widget));
1667 GtkAllocation allocation;
1669 style = gtk_widget_get_style (widget);
1670 gtk_widget_get_allocation (widget, &allocation);
1672 GTK_WIDGET_CLASS (gtk_calendar_parent_class)->realize (widget);
1674 attributes.wclass = GDK_INPUT_ONLY;
1675 attributes.window_type = GDK_WINDOW_CHILD;
1676 attributes.event_mask = (gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK
1677 | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
1678 | GDK_POINTER_MOTION_MASK | GDK_LEAVE_NOTIFY_MASK);
1680 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
1681 attributes.x = priv->week_width + style->ythickness + inner_border;
1683 attributes.x = style->ythickness + inner_border;
1685 attributes.y = priv->header_h + priv->day_name_h + style->ythickness + inner_border;
1686 attributes.width = allocation.width - attributes.x - (style->xthickness + inner_border);
1688 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1689 attributes.width -= priv->week_width;
1691 attributes.height = priv->main_h;
1692 attributes_mask = GDK_WA_X | GDK_WA_Y;
1694 attributes.x += allocation.x;
1695 attributes.y += allocation.y;
1697 priv->main_win = gdk_window_new (gtk_widget_get_window (widget),
1698 &attributes, attributes_mask);
1699 gdk_window_set_user_data (priv->main_win, widget);
1701 calendar_realize_arrows (GTK_CALENDAR (widget));
1705 gtk_calendar_unrealize (GtkWidget *widget)
1707 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
1709 calendar_unrealize_arrows (GTK_CALENDAR (widget));
1713 gdk_window_set_user_data (priv->main_win, NULL);
1714 gdk_window_destroy (priv->main_win);
1715 priv->main_win = NULL;
1718 GTK_WIDGET_CLASS (gtk_calendar_parent_class)->unrealize (widget);
1722 calendar_map_arrows (GtkCalendar *calendar)
1724 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1727 for (i = 0; i < 4; i++)
1729 if (priv->arrow_win[i])
1730 gdk_window_show (priv->arrow_win[i]);
1735 calendar_unmap_arrows (GtkCalendar *calendar)
1737 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1740 for (i = 0; i < 4; i++)
1742 if (priv->arrow_win[i])
1743 gdk_window_hide (priv->arrow_win[i]);
1748 gtk_calendar_map (GtkWidget *widget)
1750 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
1752 GTK_WIDGET_CLASS (gtk_calendar_parent_class)->map (widget);
1754 gdk_window_show (priv->main_win);
1756 calendar_map_arrows (GTK_CALENDAR (widget));
1760 gtk_calendar_unmap (GtkWidget *widget)
1762 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
1764 calendar_unmap_arrows (GTK_CALENDAR (widget));
1766 gdk_window_hide (priv->main_win);
1768 GTK_WIDGET_CLASS (gtk_calendar_parent_class)->unmap (widget);
1772 gtk_calendar_get_detail (GtkCalendar *calendar,
1776 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1779 if (priv->detail_func == NULL)
1783 month = priv->month + priv->day_month[row][column] - MONTH_CURRENT;
1790 else if (month > 11)
1796 return priv->detail_func (calendar,
1798 priv->day[row][column],
1799 priv->detail_func_user_data);
1803 gtk_calendar_query_tooltip (GtkWidget *widget,
1806 gboolean keyboard_mode,
1807 GtkTooltip *tooltip)
1809 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
1810 GtkCalendar *calendar = GTK_CALENDAR (widget);
1811 gchar *detail = NULL;
1812 GdkRectangle day_rect;
1815 col = calendar_column_from_x (calendar, x);
1816 row = calendar_row_from_y (calendar, y);
1818 if (col != -1 && row != -1 &&
1819 (0 != (priv->detail_overflow[row] & (1 << col)) ||
1820 0 == (priv->display_flags & GTK_CALENDAR_SHOW_DETAILS)))
1822 detail = gtk_calendar_get_detail (calendar, row, col);
1823 calendar_day_rectangle (calendar, row, col, &day_rect);
1828 gtk_tooltip_set_tip_area (tooltip, &day_rect);
1829 gtk_tooltip_set_markup (tooltip, detail);
1836 if (GTK_WIDGET_CLASS (gtk_calendar_parent_class)->query_tooltip)
1837 return GTK_WIDGET_CLASS (gtk_calendar_parent_class)->query_tooltip (widget, x, y, keyboard_mode, tooltip);
1843 /****************************************
1844 * Size Request and Allocate *
1845 ****************************************/
1848 gtk_calendar_size_request (GtkWidget *widget,
1849 GtkRequisition *requisition)
1851 GtkCalendar *calendar = GTK_CALENDAR (widget);
1852 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
1854 PangoLayout *layout;
1855 PangoRectangle logical_rect;
1859 gint calendar_margin = CALENDAR_MARGIN;
1860 gint header_width, main_width;
1861 gint max_header_height = 0;
1864 gint max_detail_height;
1865 gint inner_border = calendar_get_inner_border (calendar);
1866 gint calendar_ysep = calendar_get_ysep (calendar);
1867 gint calendar_xsep = calendar_get_xsep (calendar);
1869 gtk_widget_style_get (GTK_WIDGET (widget),
1870 "focus-line-width", &focus_width,
1871 "focus-padding", &focus_padding,
1874 layout = gtk_widget_create_pango_layout (widget, NULL);
1877 * Calculate the requisition width for the widget.
1882 if (priv->display_flags & GTK_CALENDAR_SHOW_HEADING)
1884 priv->max_month_width = 0;
1885 for (i = 0; i < 12; i++)
1887 pango_layout_set_text (layout, default_monthname[i], -1);
1888 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1889 priv->max_month_width = MAX (priv->max_month_width,
1890 logical_rect.width + 8);
1891 max_header_height = MAX (max_header_height, logical_rect.height);
1894 priv->max_year_width = 0;
1895 /* Translators: This is a text measurement template.
1896 * Translate it to the widest year text
1898 * If you don't understand this, leave it as "2000"
1900 pango_layout_set_text (layout, C_("year measurement template", "2000"), -1);
1901 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1902 priv->max_year_width = MAX (priv->max_year_width,
1903 logical_rect.width + 8);
1904 max_header_height = MAX (max_header_height, logical_rect.height);
1908 priv->max_month_width = 0;
1909 priv->max_year_width = 0;
1912 if (priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
1913 header_width = (priv->max_month_width
1914 + priv->max_year_width
1917 header_width = (priv->max_month_width
1918 + priv->max_year_width
1919 + 4 * priv->arrow_width + 3 * 3);
1921 /* Mainwindow labels width */
1923 priv->max_day_char_width = 0;
1924 priv->max_day_char_ascent = 0;
1925 priv->max_day_char_descent = 0;
1926 priv->min_day_width = 0;
1928 for (i = 0; i < 9; i++)
1931 g_snprintf (buffer, sizeof (buffer), C_("calendar:day:digits", "%d"), i * 11);
1932 pango_layout_set_text (layout, buffer, -1);
1933 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1934 priv->min_day_width = MAX (priv->min_day_width,
1935 logical_rect.width);
1937 priv->max_day_char_ascent = MAX (priv->max_day_char_ascent,
1938 PANGO_ASCENT (logical_rect));
1939 priv->max_day_char_descent = MAX (priv->max_day_char_descent,
1940 PANGO_DESCENT (logical_rect));
1943 priv->max_label_char_ascent = 0;
1944 priv->max_label_char_descent = 0;
1945 if (priv->display_flags & GTK_CALENDAR_SHOW_DAY_NAMES)
1946 for (i = 0; i < 7; i++)
1948 pango_layout_set_text (layout, default_abbreviated_dayname[i], -1);
1949 pango_layout_line_get_pixel_extents (pango_layout_get_lines_readonly (layout)->data, NULL, &logical_rect);
1951 priv->min_day_width = MAX (priv->min_day_width, logical_rect.width);
1952 priv->max_label_char_ascent = MAX (priv->max_label_char_ascent,
1953 PANGO_ASCENT (logical_rect));
1954 priv->max_label_char_descent = MAX (priv->max_label_char_descent,
1955 PANGO_DESCENT (logical_rect));
1958 priv->max_week_char_width = 0;
1959 if (priv->display_flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
1960 for (i = 0; i < 9; i++)
1963 g_snprintf (buffer, sizeof (buffer), C_("calendar:week:digits", "%d"), i * 11);
1964 pango_layout_set_text (layout, buffer, -1);
1965 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1966 priv->max_week_char_width = MAX (priv->max_week_char_width,
1967 logical_rect.width / 2);
1970 /* Calculate detail extents. Do this as late as possible since
1971 * pango_layout_set_markup is called which alters font settings. */
1972 max_detail_height = 0;
1974 if (priv->detail_func && (priv->display_flags & GTK_CALENDAR_SHOW_DETAILS))
1976 gchar *markup, *tail;
1978 if (priv->detail_width_chars || priv->detail_height_rows)
1980 gint rows = MAX (1, priv->detail_height_rows) - 1;
1981 gsize len = priv->detail_width_chars + rows + 16;
1983 markup = tail = g_alloca (len);
1985 memcpy (tail, "<small>", 7);
1988 memset (tail, 'm', priv->detail_width_chars);
1989 tail += priv->detail_width_chars;
1991 memset (tail, '\n', rows);
1994 memcpy (tail, "</small>", 9);
1997 g_assert (len == (tail - markup));
1999 pango_layout_set_markup (layout, markup, -1);
2000 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2002 if (priv->detail_width_chars)
2003 priv->min_day_width = MAX (priv->min_day_width, logical_rect.width);
2004 if (priv->detail_height_rows)
2005 max_detail_height = MAX (max_detail_height, logical_rect.height);
2008 if (!priv->detail_width_chars || !priv->detail_height_rows)
2009 for (r = 0; r < 6; r++)
2010 for (c = 0; c < 7; c++)
2012 gchar *detail = gtk_calendar_get_detail (calendar, r, c);
2016 markup = g_strconcat ("<small>", detail, "</small>", NULL);
2017 pango_layout_set_markup (layout, markup, -1);
2019 if (priv->detail_width_chars)
2021 pango_layout_set_wrap (layout, PANGO_WRAP_WORD_CHAR);
2022 pango_layout_set_width (layout, PANGO_SCALE * priv->min_day_width);
2025 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2027 if (!priv->detail_width_chars)
2028 priv->min_day_width = MAX (priv->min_day_width, logical_rect.width);
2029 if (!priv->detail_height_rows)
2030 max_detail_height = MAX (max_detail_height, logical_rect.height);
2038 /* We add one to max_day_char_width to be able to make the marked day "bold" */
2039 priv->max_day_char_width = priv->min_day_width / 2 + 1;
2041 main_width = (7 * (priv->min_day_width + (focus_padding + focus_width) * 2) + (DAY_XSEP * 6) + CALENDAR_MARGIN * 2
2042 + (priv->max_week_char_width
2043 ? priv->max_week_char_width * 2 + (focus_padding + focus_width) * 2 + calendar_xsep * 2
2046 style = gtk_widget_get_style (widget);
2048 requisition->width = MAX (header_width, main_width + inner_border * 2) + style->xthickness * 2;
2051 * Calculate the requisition height for the widget.
2054 if (priv->display_flags & GTK_CALENDAR_SHOW_HEADING)
2056 priv->header_h = (max_header_height + calendar_ysep * 2);
2063 if (priv->display_flags & GTK_CALENDAR_SHOW_DAY_NAMES)
2065 priv->day_name_h = (priv->max_label_char_ascent
2066 + priv->max_label_char_descent
2067 + 2 * (focus_padding + focus_width) + calendar_margin);
2068 calendar_margin = calendar_ysep;
2072 priv->day_name_h = 0;
2075 priv->main_h = (CALENDAR_MARGIN + calendar_margin
2076 + 6 * (priv->max_day_char_ascent
2077 + priv->max_day_char_descent
2079 + 2 * (focus_padding + focus_width))
2082 height = priv->header_h + priv->day_name_h + priv->main_h;
2084 requisition->height = height + (style->ythickness + inner_border) * 2;
2086 g_object_unref (layout);
2090 gtk_calendar_get_preferred_width (GtkWidget *widget,
2094 GtkRequisition requisition;
2096 gtk_calendar_size_request (widget, &requisition);
2098 *minimum = *natural = requisition.width;
2102 gtk_calendar_get_preferred_height (GtkWidget *widget,
2106 GtkRequisition requisition;
2108 gtk_calendar_size_request (widget, &requisition);
2110 *minimum = *natural = requisition.height;
2114 gtk_calendar_size_allocate (GtkWidget *widget,
2115 GtkAllocation *allocation)
2117 GtkCalendar *calendar = GTK_CALENDAR (widget);
2118 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
2120 gint xthickness, ythickness;
2122 gint inner_border = calendar_get_inner_border (calendar);
2123 gint calendar_xsep = calendar_get_xsep (calendar);
2125 style = gtk_widget_get_style (widget);
2126 xthickness = style->xthickness;
2127 ythickness = style->xthickness;
2129 gtk_widget_set_allocation (widget, allocation);
2131 if (priv->display_flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
2133 priv->day_width = (priv->min_day_width
2134 * ((allocation->width - (xthickness + inner_border) * 2
2135 - (CALENDAR_MARGIN * 2) - (DAY_XSEP * 6) - calendar_xsep * 2))
2136 / (7 * priv->min_day_width + priv->max_week_char_width * 2));
2137 priv->week_width = ((allocation->width - (xthickness + inner_border) * 2
2138 - (CALENDAR_MARGIN * 2) - (DAY_XSEP * 6) - calendar_xsep * 2 )
2139 - priv->day_width * 7 + CALENDAR_MARGIN + calendar_xsep);
2143 priv->day_width = (allocation->width
2144 - (xthickness + inner_border) * 2
2145 - (CALENDAR_MARGIN * 2)
2146 - (DAY_XSEP * 6))/7;
2147 priv->week_width = 0;
2150 if (gtk_widget_get_realized (widget))
2152 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
2153 gdk_window_move_resize (priv->main_win,
2155 + priv->week_width + xthickness + inner_border,
2157 + priv->header_h + priv->day_name_h
2158 + (style->ythickness + inner_border),
2159 allocation->width - priv->week_width
2160 - (xthickness + inner_border) * 2,
2163 gdk_window_move_resize (priv->main_win,
2165 + xthickness + inner_border,
2167 + priv->header_h + priv->day_name_h
2168 + style->ythickness + inner_border,
2169 allocation->width - priv->week_width
2170 - (xthickness + inner_border) * 2,
2173 for (i = 0 ; i < 4 ; i++)
2175 if (priv->arrow_win[i])
2178 calendar_arrow_rectangle (calendar, i, &rect);
2180 gdk_window_move_resize (priv->arrow_win[i],
2181 allocation->x + rect.x,
2182 allocation->y + rect.y,
2183 rect.width, rect.height);
2190 /****************************************
2192 ****************************************/
2195 calendar_paint_header (GtkCalendar *calendar, cairo_t *cr)
2197 GtkWidget *widget = GTK_WIDGET (calendar);
2198 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2199 GtkAllocation allocation;
2204 gint max_month_width;
2205 gint max_year_width;
2206 PangoLayout *layout;
2207 PangoRectangle logical_rect;
2213 style = gtk_widget_get_style (widget);
2216 cairo_translate (cr, style->xthickness, style->ythickness);
2218 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
2219 year_left = priv->year_before;
2221 year_left = !priv->year_before;
2223 gtk_widget_get_allocation (widget, &allocation);
2225 header_width = allocation.width - 2 * style->xthickness;
2227 max_month_width = priv->max_month_width;
2228 max_year_width = priv->max_year_width;
2230 gdk_cairo_set_source_color (cr, HEADER_BG_COLOR (widget));
2231 cairo_rectangle (cr, 0, 0, header_width, priv->header_h);
2234 gtk_paint_shadow (style, cr,
2235 GTK_STATE_NORMAL, GTK_SHADOW_OUT,
2237 0, 0, header_width, priv->header_h);
2239 tmp_time = 1; /* Jan 1 1970, 00:00:01 UTC */
2240 tm = gmtime (&tmp_time);
2241 tm->tm_year = priv->year - 1900;
2243 /* Translators: This dictates how the year is displayed in
2244 * gtkcalendar widget. See strftime() manual for the format.
2245 * Use only ASCII in the translation.
2247 * Also look for the msgid "2000".
2248 * Translate that entry to a year with the widest output of this
2251 * "%Y" is appropriate for most locales.
2253 strftime (buffer, sizeof (buffer), C_("calendar year format", "%Y"), tm);
2254 str = g_locale_to_utf8 (buffer, -1, NULL, NULL, NULL);
2255 layout = gtk_widget_create_pango_layout (widget, str);
2258 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2261 y = (priv->header_h - logical_rect.height) / 2;
2263 /* Draw year and its arrows */
2265 if (priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
2267 x = 3 + (max_year_width - logical_rect.width)/2;
2269 x = header_width - (3 + max_year_width
2270 - (max_year_width - logical_rect.width)/2);
2273 x = 3 + priv->arrow_width + (max_year_width - logical_rect.width)/2;
2275 x = header_width - (3 + priv->arrow_width + max_year_width
2276 - (max_year_width - logical_rect.width)/2);
2279 gdk_cairo_set_source_color (cr, HEADER_FG_COLOR (GTK_WIDGET (calendar)));
2280 cairo_move_to (cr, x, y);
2281 pango_cairo_show_layout (cr, layout);
2284 g_snprintf (buffer, sizeof (buffer), "%s", default_monthname[priv->month]);
2285 pango_layout_set_text (layout, buffer, -1);
2286 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2288 if (priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
2290 x = header_width - (3 + max_month_width
2291 - (max_month_width - logical_rect.width)/2);
2293 x = 3 + (max_month_width - logical_rect.width) / 2;
2296 x = header_width - (3 + priv->arrow_width + max_month_width
2297 - (max_month_width - logical_rect.width)/2);
2299 x = 3 + priv->arrow_width + (max_month_width - logical_rect.width)/2;
2301 cairo_move_to (cr, x, y);
2302 pango_cairo_show_layout (cr, layout);
2304 g_object_unref (layout);
2310 calendar_paint_day_names (GtkCalendar *calendar,
2313 GtkWidget *widget = GTK_WIDGET (calendar);
2314 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2316 GtkAllocation allocation;
2319 int day_width, cal_width;
2321 PangoLayout *layout;
2322 PangoRectangle logical_rect;
2325 gint calendar_ysep = calendar_get_ysep (calendar);
2326 gint calendar_xsep = calendar_get_xsep (calendar);
2327 gint inner_border = calendar_get_inner_border (calendar);
2329 style = gtk_widget_get_style (widget);
2333 cairo_translate (cr,
2334 style->xthickness + inner_border,
2335 priv->header_h + style->ythickness + inner_border);
2337 gtk_widget_style_get (GTK_WIDGET (widget),
2338 "focus-line-width", &focus_width,
2339 "focus-padding", &focus_padding,
2342 gtk_widget_get_allocation (widget, &allocation);
2344 day_width = priv->day_width;
2345 cal_width = allocation.width - (style->xthickness + inner_border) * 2;
2346 day_wid_sep = day_width + DAY_XSEP;
2349 * Draw rectangles as inverted background for the labels.
2352 gdk_cairo_set_source_color (cr, SELECTED_BG_COLOR (widget));
2353 cairo_rectangle (cr,
2354 CALENDAR_MARGIN, CALENDAR_MARGIN,
2355 cal_width - CALENDAR_MARGIN * 2,
2356 priv->day_name_h - CALENDAR_MARGIN);
2359 if (priv->display_flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
2361 cairo_rectangle (cr,
2363 priv->day_name_h - calendar_ysep,
2364 priv->week_width - calendar_ysep - CALENDAR_MARGIN,
2373 layout = gtk_widget_create_pango_layout (widget, NULL);
2375 gdk_cairo_set_source_color (cr, SELECTED_FG_COLOR (widget));
2376 for (i = 0; i < 7; i++)
2378 if (gtk_widget_get_direction (GTK_WIDGET (calendar)) == GTK_TEXT_DIR_RTL)
2382 day = (day + priv->week_start) % 7;
2383 g_snprintf (buffer, sizeof (buffer), "%s", default_abbreviated_dayname[day]);
2385 pango_layout_set_text (layout, buffer, -1);
2386 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2390 + (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR ?
2391 (priv->week_width + (priv->week_width ? calendar_xsep : 0))
2394 + (day_width - logical_rect.width)/2),
2395 CALENDAR_MARGIN + focus_width + focus_padding + logical_rect.y);
2396 pango_cairo_show_layout (cr, layout);
2399 g_object_unref (layout);
2405 calendar_paint_week_numbers (GtkCalendar *calendar,
2408 GtkWidget *widget = GTK_WIDGET (calendar);
2409 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2411 guint week = 0, year;
2412 gint row, x_loc, y_loc;
2415 PangoLayout *layout;
2416 PangoRectangle logical_rect;
2419 gint calendar_xsep = calendar_get_xsep (calendar);
2420 gint inner_border = calendar_get_inner_border (calendar);
2423 style = gtk_widget_get_style (widget);
2427 y = priv->header_h + priv->day_name_h + (style->ythickness + inner_border);
2428 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
2429 x = style->xthickness + inner_border;
2431 x = gtk_widget_get_allocated_width (widget) - priv->week_width - (style->xthickness + inner_border);
2433 gtk_widget_style_get (GTK_WIDGET (widget),
2434 "focus-line-width", &focus_width,
2435 "focus-padding", &focus_padding,
2439 * Draw a rectangle as inverted background for the labels.
2442 gdk_cairo_set_source_color (cr, SELECTED_BG_COLOR (widget));
2443 if (priv->display_flags & GTK_CALENDAR_SHOW_DAY_NAMES)
2444 cairo_rectangle (cr,
2445 x + CALENDAR_MARGIN,
2447 priv->week_width - CALENDAR_MARGIN,
2448 priv->main_h - CALENDAR_MARGIN);
2450 cairo_rectangle (cr,
2451 x + CALENDAR_MARGIN,
2452 y + CALENDAR_MARGIN,
2453 priv->week_width - CALENDAR_MARGIN,
2454 priv->main_h - 2 * CALENDAR_MARGIN);
2461 layout = gtk_widget_create_pango_layout (widget, NULL);
2463 gdk_cairo_set_source_color (cr, SELECTED_FG_COLOR (widget));
2464 day_height = calendar_row_height (calendar);
2465 for (row = 0; row < 6; row++)
2470 if (priv->day[row][6] < 15 && row > 3 && priv->month == 11)
2473 result = week_of_year (&week, &year,
2474 ((priv->day[row][6] < 15 && row > 3 ? 1 : 0)
2475 + priv->month) % 12 + 1, priv->day[row][6]);
2476 g_return_if_fail (result);
2478 /* Translators: this defines whether the week numbers should use
2479 * localized digits or the ones used in English (0123...).
2481 * Translate to "%Id" if you want to use localized digits, or
2482 * translate to "%d" otherwise.
2484 * Note that translating this doesn't guarantee that you get localized
2485 * digits. That needs support from your system and locale definition
2488 g_snprintf (buffer, sizeof (buffer), C_("calendar:week:digits", "%d"), week);
2489 pango_layout_set_text (layout, buffer, -1);
2490 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2492 y_loc = calendar_top_y_for_row (calendar, row) + (day_height - logical_rect.height) / 2;
2494 x_loc = x + (priv->week_width
2495 - logical_rect.width
2496 - calendar_xsep - focus_padding - focus_width);
2498 cairo_move_to (cr, x_loc, y_loc);
2499 pango_cairo_show_layout (cr, layout);
2502 g_object_unref (layout);
2508 calendar_invalidate_day_num (GtkCalendar *calendar,
2511 GtkCalendarPrivate *priv = calendar->priv;
2512 gint r, c, row, col;
2516 for (r = 0; r < 6; r++)
2517 for (c = 0; c < 7; c++)
2518 if (priv->day_month[r][c] == MONTH_CURRENT &&
2519 priv->day[r][c] == day)
2525 g_return_if_fail (row != -1);
2526 g_return_if_fail (col != -1);
2528 calendar_invalidate_day (calendar, row, col);
2532 calendar_invalidate_day (GtkCalendar *calendar,
2536 GdkRectangle day_rect;
2537 GtkAllocation allocation;
2539 gtk_widget_get_allocation (GTK_WIDGET (calendar), &allocation);
2540 calendar_day_rectangle (calendar, row, col, &day_rect);
2541 gtk_widget_queue_draw_area (GTK_WIDGET (calendar),
2542 allocation.x + day_rect.x,
2543 allocation.y + day_rect.y,
2544 day_rect.width, day_rect.height);
2548 is_color_attribute (PangoAttribute *attribute,
2551 return (attribute->klass->type == PANGO_ATTR_FOREGROUND ||
2552 attribute->klass->type == PANGO_ATTR_BACKGROUND);
2556 calendar_paint_day (GtkCalendar *calendar,
2561 GtkWidget *widget = GTK_WIDGET (calendar);
2562 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2564 GdkColor *text_color;
2569 GdkRectangle day_rect;
2571 PangoLayout *layout;
2572 PangoRectangle logical_rect;
2573 gboolean overflow = FALSE;
2574 gboolean show_details;
2576 g_return_if_fail (row < 6);
2577 g_return_if_fail (col < 7);
2579 style = gtk_widget_get_style (widget);
2581 day = priv->day[row][col];
2582 show_details = (priv->display_flags & GTK_CALENDAR_SHOW_DETAILS);
2584 calendar_day_rectangle (calendar, row, col, &day_rect);
2586 if (priv->day_month[row][col] == MONTH_PREV)
2588 text_color = PREV_MONTH_COLOR (widget);
2590 else if (priv->day_month[row][col] == MONTH_NEXT)
2592 text_color = NEXT_MONTH_COLOR (widget);
2596 if (priv->selected_day == day)
2598 gdk_cairo_set_source_color (cr, SELECTED_BG_COLOR (widget));
2599 gdk_cairo_rectangle (cr, &day_rect);
2602 if (priv->selected_day == day)
2603 text_color = SELECTED_FG_COLOR (widget);
2604 else if (priv->marked_date[day-1])
2605 text_color = MARKED_COLOR (widget);
2607 text_color = NORMAL_DAY_COLOR (widget);
2610 /* Translators: this defines whether the day numbers should use
2611 * localized digits or the ones used in English (0123...).
2613 * Translate to "%Id" if you want to use localized digits, or
2614 * translate to "%d" otherwise.
2616 * Note that translating this doesn't guarantee that you get localized
2617 * digits. That needs support from your system and locale definition
2620 g_snprintf (buffer, sizeof (buffer), C_("calendar:day:digits", "%d"), day);
2622 /* Get extra information to show, if any: */
2624 detail = gtk_calendar_get_detail (calendar, row, col);
2626 layout = gtk_widget_create_pango_layout (widget, buffer);
2627 pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
2628 pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2630 x_loc = day_rect.x + (day_rect.width - logical_rect.width) / 2;
2633 gdk_cairo_set_source_color (cr, text_color);
2634 cairo_move_to (cr, x_loc, y_loc);
2635 pango_cairo_show_layout (cr, layout);
2637 if (priv->day_month[row][col] == MONTH_CURRENT &&
2638 (priv->marked_date[day-1] || (detail && !show_details)))
2640 cairo_move_to (cr, x_loc - 1, y_loc);
2641 pango_cairo_show_layout (cr, layout);
2644 y_loc += priv->max_day_char_descent;
2646 if (priv->detail_func && show_details)
2650 if (priv->selected_day == day)
2651 gdk_cairo_set_source_color (cr, &style->text[GTK_STATE_ACTIVE]);
2652 else if (priv->day_month[row][col] == MONTH_CURRENT)
2653 gdk_cairo_set_source_color (cr, &style->base[GTK_STATE_ACTIVE]);
2655 gdk_cairo_set_source_color (cr, &style->base[GTK_STATE_INSENSITIVE]);
2657 cairo_set_line_width (cr, 1);
2658 cairo_move_to (cr, day_rect.x + 2, y_loc + 0.5);
2659 cairo_line_to (cr, day_rect.x + day_rect.width - 2, y_loc + 0.5);
2667 if (detail && show_details)
2669 gchar *markup = g_strconcat ("<small>", detail, "</small>", NULL);
2670 pango_layout_set_markup (layout, markup, -1);
2673 if (day == priv->selected_day)
2675 /* Stripping colors as they conflict with selection marking. */
2677 PangoAttrList *attrs = pango_layout_get_attributes (layout);
2678 PangoAttrList *colors = NULL;
2681 colors = pango_attr_list_filter (attrs, is_color_attribute, NULL);
2683 pango_attr_list_unref (colors);
2686 pango_layout_set_wrap (layout, PANGO_WRAP_WORD_CHAR);
2687 pango_layout_set_width (layout, PANGO_SCALE * day_rect.width);
2689 if (priv->detail_height_rows)
2691 gint dy = day_rect.height - (y_loc - day_rect.y);
2692 pango_layout_set_height (layout, PANGO_SCALE * dy);
2693 pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_END);
2696 cairo_move_to (cr, day_rect.x, y_loc);
2697 pango_cairo_show_layout (cr, layout);
2700 if (gtk_widget_has_focus (widget)
2701 && priv->focus_row == row && priv->focus_col == col)
2705 if (priv->selected_day == day)
2706 state = gtk_widget_has_focus (widget) ? GTK_STATE_SELECTED : GTK_STATE_ACTIVE;
2708 state = GTK_STATE_NORMAL;
2710 gtk_paint_focus (style, cr,
2711 state, widget, "calendar-day",
2712 day_rect.x, day_rect.y,
2713 day_rect.width, day_rect.height);
2717 priv->detail_overflow[row] |= (1 << col);
2719 priv->detail_overflow[row] &= ~(1 << col);
2721 g_object_unref (layout);
2726 calendar_paint_main (GtkCalendar *calendar,
2733 for (col = 0; col < 7; col++)
2734 for (row = 0; row < 6; row++)
2735 calendar_paint_day (calendar, cr, row, col);
2741 calendar_invalidate_arrow (GtkCalendar *calendar,
2744 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2746 if (priv->display_flags & GTK_CALENDAR_SHOW_HEADING &&
2747 priv->arrow_win[arrow])
2750 GtkAllocation allocation;
2752 calendar_arrow_rectangle (calendar, arrow, &rect);
2753 gtk_widget_get_allocation (GTK_WIDGET (calendar), &allocation);
2754 gtk_widget_queue_draw_area (GTK_WIDGET (calendar),
2755 allocation.x + rect.x,
2756 allocation.y + rect.y,
2757 rect.width, rect.height);
2762 calendar_paint_arrow (GtkCalendar *calendar,
2766 GtkWidget *widget = GTK_WIDGET (calendar);
2767 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
2772 if (!priv->arrow_win[arrow])
2775 calendar_arrow_rectangle (calendar, arrow, &rect);
2779 style = gtk_widget_get_style (widget);
2780 state = priv->arrow_state[arrow];
2782 cairo_rectangle (cr, rect.x, rect.y, rect.width, rect.height);
2783 gdk_cairo_set_source_color (cr, &style->bg[state]);
2786 if (arrow == ARROW_MONTH_LEFT || arrow == ARROW_YEAR_LEFT)
2787 gtk_paint_arrow (style, cr, state,
2788 GTK_SHADOW_OUT, widget, "calendar",
2789 GTK_ARROW_LEFT, TRUE,
2790 rect.x + (rect.width - 8) / 2,
2791 rect.y + (rect.height - 8) / 2,
2794 gtk_paint_arrow (style, cr, state,
2795 GTK_SHADOW_OUT, widget, "calendar",
2796 GTK_ARROW_RIGHT, TRUE,
2797 rect.x + (rect.width - 8) / 2,
2798 rect.y + (rect.height - 8) / 2,
2805 gtk_calendar_draw (GtkWidget *widget,
2808 GtkCalendar *calendar = GTK_CALENDAR (widget);
2809 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
2812 if (gtk_cairo_should_draw_window (cr, gtk_widget_get_window (widget)))
2814 gdk_cairo_set_source_color (cr, BACKGROUND_COLOR (widget));
2815 cairo_rectangle (cr,
2817 gtk_widget_get_allocated_width (widget),
2818 gtk_widget_get_allocated_height (widget));
2820 gtk_paint_shadow (gtk_widget_get_style (widget), cr,
2821 gtk_widget_get_state (widget), GTK_SHADOW_IN,
2824 gtk_widget_get_allocated_width (widget),
2825 gtk_widget_get_allocated_height (widget));
2828 calendar_paint_main (calendar, cr);
2830 if (priv->display_flags & GTK_CALENDAR_SHOW_HEADING)
2832 calendar_paint_header (calendar, cr);
2833 for (i = 0; i < 4; i++)
2834 calendar_paint_arrow (calendar, cr, i);
2837 if (priv->display_flags & GTK_CALENDAR_SHOW_DAY_NAMES)
2838 calendar_paint_day_names (calendar, cr);
2840 if (priv->display_flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
2841 calendar_paint_week_numbers (calendar, cr);
2847 /****************************************
2849 ****************************************/
2852 calendar_arrow_action (GtkCalendar *calendar,
2857 case ARROW_YEAR_LEFT:
2858 calendar_set_year_prev (calendar);
2860 case ARROW_YEAR_RIGHT:
2861 calendar_set_year_next (calendar);
2863 case ARROW_MONTH_LEFT:
2864 calendar_set_month_prev (calendar);
2866 case ARROW_MONTH_RIGHT:
2867 calendar_set_month_next (calendar);
2875 calendar_timer (gpointer data)
2877 GtkCalendar *calendar = data;
2878 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2879 gboolean retval = FALSE;
2883 calendar_arrow_action (calendar, priv->click_child);
2885 if (priv->need_timer)
2887 GtkSettings *settings;
2890 settings = gtk_widget_get_settings (GTK_WIDGET (calendar));
2891 g_object_get (settings, "gtk-timeout-repeat", &timeout, NULL);
2893 priv->need_timer = FALSE;
2894 priv->timer = gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT_IDLE,
2895 timeout * SCROLL_DELAY_FACTOR,
2896 (GSourceFunc) calendar_timer,
2897 (gpointer) calendar, NULL);
2907 calendar_start_spinning (GtkCalendar *calendar,
2910 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2912 priv->click_child = click_child;
2916 GtkSettings *settings;
2919 settings = gtk_widget_get_settings (GTK_WIDGET (calendar));
2920 g_object_get (settings, "gtk-timeout-initial", &timeout, NULL);
2922 priv->need_timer = TRUE;
2923 priv->timer = gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT_IDLE,
2925 (GSourceFunc) calendar_timer,
2926 (gpointer) calendar, NULL);
2931 calendar_stop_spinning (GtkCalendar *calendar)
2933 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2937 g_source_remove (priv->timer);
2939 priv->need_timer = FALSE;
2944 calendar_main_button_press (GtkCalendar *calendar,
2945 GdkEventButton *event)
2947 GtkWidget *widget = GTK_WIDGET (calendar);
2948 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2954 GtkAllocation allocation;
2956 x = (gint) (event->x);
2957 y = (gint) (event->y);
2959 gdk_window_get_position (priv->main_win, &win_x, &win_y);
2960 gtk_widget_get_allocation (widget, &allocation);
2962 row = calendar_row_from_y (calendar, y + win_y - allocation.y);
2963 col = calendar_column_from_x (calendar, x + win_x - allocation.x);
2965 /* If row or column isn't found, just return. */
2966 if (row == -1 || col == -1)
2969 day_month = priv->day_month[row][col];
2971 if (event->type == GDK_BUTTON_PRESS)
2973 day = priv->day[row][col];
2975 if (day_month == MONTH_PREV)
2976 calendar_set_month_prev (calendar);
2977 else if (day_month == MONTH_NEXT)
2978 calendar_set_month_next (calendar);
2980 if (!gtk_widget_has_focus (widget))
2981 gtk_widget_grab_focus (widget);
2983 if (event->button == 1)
2986 priv->drag_start_x = x;
2987 priv->drag_start_y = y;
2990 calendar_select_and_focus_day (calendar, day);
2992 else if (event->type == GDK_2BUTTON_PRESS)
2995 if (day_month == MONTH_CURRENT)
2996 g_signal_emit (calendar,
2997 gtk_calendar_signals[DAY_SELECTED_DOUBLE_CLICK_SIGNAL],
3003 gtk_calendar_button_press (GtkWidget *widget,
3004 GdkEventButton *event)
3006 GtkCalendar *calendar = GTK_CALENDAR (widget);
3007 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3010 if (event->window == priv->main_win)
3011 calendar_main_button_press (calendar, event);
3013 if (!gtk_widget_has_focus (widget))
3014 gtk_widget_grab_focus (widget);
3016 for (arrow = ARROW_YEAR_LEFT; arrow <= ARROW_MONTH_RIGHT; arrow++)
3018 if (event->window == priv->arrow_win[arrow])
3021 /* only call the action on single click, not double */
3022 if (event->type == GDK_BUTTON_PRESS)
3024 if (event->button == 1)
3025 calendar_start_spinning (calendar, arrow);
3027 calendar_arrow_action (calendar, arrow);
3038 gtk_calendar_button_release (GtkWidget *widget,
3039 GdkEventButton *event)
3041 GtkCalendar *calendar = GTK_CALENDAR (widget);
3042 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3044 if (event->button == 1)
3046 calendar_stop_spinning (calendar);
3056 gtk_calendar_motion_notify (GtkWidget *widget,
3057 GdkEventMotion *event)
3059 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3060 gint event_x, event_y;
3062 event_x = (gint) (event->x);
3063 event_y = (gint) (event->y);
3067 if (gtk_drag_check_threshold (widget,
3068 priv->drag_start_x, priv->drag_start_y,
3069 event->x, event->y))
3071 GdkDragContext *context;
3072 GtkTargetList *target_list = gtk_target_list_new (NULL, 0);
3073 gtk_target_list_add_text_targets (target_list, 0);
3074 context = gtk_drag_begin (widget, target_list, GDK_ACTION_COPY,
3075 1, (GdkEvent *)event);
3078 gtk_target_list_unref (target_list);
3079 gtk_drag_set_icon_default (context);
3087 gtk_calendar_enter_notify (GtkWidget *widget,
3088 GdkEventCrossing *event)
3090 GtkCalendar *calendar = GTK_CALENDAR (widget);
3091 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3093 if (event->window == priv->arrow_win[ARROW_MONTH_LEFT])
3095 priv->arrow_state[ARROW_MONTH_LEFT] = GTK_STATE_PRELIGHT;
3096 calendar_invalidate_arrow (calendar, ARROW_MONTH_LEFT);
3099 if (event->window == priv->arrow_win[ARROW_MONTH_RIGHT])
3101 priv->arrow_state[ARROW_MONTH_RIGHT] = GTK_STATE_PRELIGHT;
3102 calendar_invalidate_arrow (calendar, ARROW_MONTH_RIGHT);
3105 if (event->window == priv->arrow_win[ARROW_YEAR_LEFT])
3107 priv->arrow_state[ARROW_YEAR_LEFT] = GTK_STATE_PRELIGHT;
3108 calendar_invalidate_arrow (calendar, ARROW_YEAR_LEFT);
3111 if (event->window == priv->arrow_win[ARROW_YEAR_RIGHT])
3113 priv->arrow_state[ARROW_YEAR_RIGHT] = GTK_STATE_PRELIGHT;
3114 calendar_invalidate_arrow (calendar, ARROW_YEAR_RIGHT);
3121 gtk_calendar_leave_notify (GtkWidget *widget,
3122 GdkEventCrossing *event)
3124 GtkCalendar *calendar = GTK_CALENDAR (widget);
3125 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3127 if (event->window == priv->arrow_win[ARROW_MONTH_LEFT])
3129 priv->arrow_state[ARROW_MONTH_LEFT] = GTK_STATE_NORMAL;
3130 calendar_invalidate_arrow (calendar, ARROW_MONTH_LEFT);
3133 if (event->window == priv->arrow_win[ARROW_MONTH_RIGHT])
3135 priv->arrow_state[ARROW_MONTH_RIGHT] = GTK_STATE_NORMAL;
3136 calendar_invalidate_arrow (calendar, ARROW_MONTH_RIGHT);
3139 if (event->window == priv->arrow_win[ARROW_YEAR_LEFT])
3141 priv->arrow_state[ARROW_YEAR_LEFT] = GTK_STATE_NORMAL;
3142 calendar_invalidate_arrow (calendar, ARROW_YEAR_LEFT);
3145 if (event->window == priv->arrow_win[ARROW_YEAR_RIGHT])
3147 priv->arrow_state[ARROW_YEAR_RIGHT] = GTK_STATE_NORMAL;
3148 calendar_invalidate_arrow (calendar, ARROW_YEAR_RIGHT);
3155 gtk_calendar_scroll (GtkWidget *widget,
3156 GdkEventScroll *event)
3158 GtkCalendar *calendar = GTK_CALENDAR (widget);
3160 if (event->direction == GDK_SCROLL_UP)
3162 if (!gtk_widget_has_focus (widget))
3163 gtk_widget_grab_focus (widget);
3164 calendar_set_month_prev (calendar);
3166 else if (event->direction == GDK_SCROLL_DOWN)
3168 if (!gtk_widget_has_focus (widget))
3169 gtk_widget_grab_focus (widget);
3170 calendar_set_month_next (calendar);
3179 /****************************************
3181 ****************************************/
3184 move_focus (GtkCalendar *calendar,
3187 GtkCalendarPrivate *priv = calendar->priv;
3188 GtkTextDirection text_dir = gtk_widget_get_direction (GTK_WIDGET (calendar));
3190 if ((text_dir == GTK_TEXT_DIR_LTR && direction == -1) ||
3191 (text_dir == GTK_TEXT_DIR_RTL && direction == 1))
3193 if (priv->focus_col > 0)
3195 else if (priv->focus_row > 0)
3197 priv->focus_col = 6;
3201 if (priv->focus_col < 0)
3202 priv->focus_col = 6;
3203 if (priv->focus_row < 0)
3204 priv->focus_row = 5;
3208 if (priv->focus_col < 6)
3210 else if (priv->focus_row < 5)
3212 priv->focus_col = 0;
3216 if (priv->focus_col < 0)
3217 priv->focus_col = 0;
3218 if (priv->focus_row < 0)
3219 priv->focus_row = 0;
3224 gtk_calendar_key_press (GtkWidget *widget,
3227 GtkCalendar *calendar = GTK_CALENDAR (widget);
3228 GtkCalendarPrivate *priv = calendar->priv;
3236 old_focus_row = priv->focus_row;
3237 old_focus_col = priv->focus_col;
3239 switch (event->keyval)
3241 case GDK_KEY_KP_Left:
3244 if (event->state & GDK_CONTROL_MASK)
3245 calendar_set_month_prev (calendar);
3248 move_focus (calendar, -1);
3249 calendar_invalidate_day (calendar, old_focus_row, old_focus_col);
3250 calendar_invalidate_day (calendar, priv->focus_row,
3254 case GDK_KEY_KP_Right:
3257 if (event->state & GDK_CONTROL_MASK)
3258 calendar_set_month_next (calendar);
3261 move_focus (calendar, 1);
3262 calendar_invalidate_day (calendar, old_focus_row, old_focus_col);
3263 calendar_invalidate_day (calendar, priv->focus_row,
3270 if (event->state & GDK_CONTROL_MASK)
3271 calendar_set_year_prev (calendar);
3274 if (priv->focus_row > 0)
3276 if (priv->focus_row < 0)
3277 priv->focus_row = 5;
3278 if (priv->focus_col < 0)
3279 priv->focus_col = 6;
3280 calendar_invalidate_day (calendar, old_focus_row, old_focus_col);
3281 calendar_invalidate_day (calendar, priv->focus_row,
3285 case GDK_KEY_KP_Down:
3288 if (event->state & GDK_CONTROL_MASK)
3289 calendar_set_year_next (calendar);
3292 if (priv->focus_row < 5)
3294 if (priv->focus_col < 0)
3295 priv->focus_col = 0;
3296 calendar_invalidate_day (calendar, old_focus_row, old_focus_col);
3297 calendar_invalidate_day (calendar, priv->focus_row,
3301 case GDK_KEY_KP_Space:
3303 row = priv->focus_row;
3304 col = priv->focus_col;
3306 if (row > -1 && col > -1)
3310 day = priv->day[row][col];
3311 if (priv->day_month[row][col] == MONTH_PREV)
3312 calendar_set_month_prev (calendar);
3313 else if (priv->day_month[row][col] == MONTH_NEXT)
3314 calendar_set_month_next (calendar);
3316 calendar_select_and_focus_day (calendar, day);
3324 /****************************************
3325 * Misc widget methods *
3326 ****************************************/
3329 gtk_calendar_state_changed (GtkWidget *widget,
3330 GtkStateType previous_state)
3332 GtkCalendar *calendar = GTK_CALENDAR (widget);
3333 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3336 if (!gtk_widget_is_sensitive (widget))
3339 calendar_stop_spinning (calendar);
3342 for (i = 0; i < 4; i++)
3343 if (gtk_widget_is_sensitive (widget))
3344 priv->arrow_state[i] = GTK_STATE_NORMAL;
3346 priv->arrow_state[i] = GTK_STATE_INSENSITIVE;
3350 gtk_calendar_grab_notify (GtkWidget *widget,
3351 gboolean was_grabbed)
3354 calendar_stop_spinning (GTK_CALENDAR (widget));
3358 gtk_calendar_focus_out (GtkWidget *widget,
3359 GdkEventFocus *event)
3361 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3362 GtkCalendar *calendar = GTK_CALENDAR (widget);
3364 calendar_queue_refresh (calendar);
3365 calendar_stop_spinning (calendar);
3373 /****************************************
3375 ****************************************/
3378 gtk_calendar_drag_data_get (GtkWidget *widget,
3379 GdkDragContext *context,
3380 GtkSelectionData *selection_data,
3384 GtkCalendar *calendar = GTK_CALENDAR (widget);
3385 GtkCalendarPrivate *priv = calendar->priv;
3390 date = g_date_new_dmy (priv->selected_day, priv->month + 1, priv->year);
3391 len = g_date_strftime (str, 127, "%x", date);
3392 gtk_selection_data_set_text (selection_data, str, len);
3397 /* Get/set whether drag_motion requested the drag data and
3398 * drag_data_received should thus not actually insert the data,
3399 * since the data doesn't result from a drop.
3402 set_status_pending (GdkDragContext *context,
3403 GdkDragAction suggested_action)
3405 g_object_set_data (G_OBJECT (context),
3406 I_("gtk-calendar-status-pending"),
3407 GINT_TO_POINTER (suggested_action));
3410 static GdkDragAction
3411 get_status_pending (GdkDragContext *context)
3413 return GPOINTER_TO_INT (g_object_get_data (G_OBJECT (context),
3414 "gtk-calendar-status-pending"));
3418 gtk_calendar_drag_leave (GtkWidget *widget,
3419 GdkDragContext *context,
3422 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3424 priv->drag_highlight = 0;
3425 gtk_drag_unhighlight (widget);
3430 gtk_calendar_drag_motion (GtkWidget *widget,
3431 GdkDragContext *context,
3436 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3439 if (!priv->drag_highlight)
3441 priv->drag_highlight = 1;
3442 gtk_drag_highlight (widget);
3445 target = gtk_drag_dest_find_target (widget, context, NULL);
3446 if (target == GDK_NONE || gdk_drag_context_get_suggested_action (context) == 0)
3447 gdk_drag_status (context, 0, time);
3450 set_status_pending (context, gdk_drag_context_get_suggested_action (context));
3451 gtk_drag_get_data (widget, context, target, time);
3458 gtk_calendar_drag_drop (GtkWidget *widget,
3459 GdkDragContext *context,
3466 target = gtk_drag_dest_find_target (widget, context, NULL);
3467 if (target != GDK_NONE)
3469 gtk_drag_get_data (widget, context,
3479 gtk_calendar_drag_data_received (GtkWidget *widget,
3480 GdkDragContext *context,
3483 GtkSelectionData *selection_data,
3487 GtkCalendar *calendar = GTK_CALENDAR (widget);
3488 GtkCalendarPrivate *priv = calendar->priv;
3489 guint day, month, year;
3492 GdkDragAction suggested_action;
3494 suggested_action = get_status_pending (context);
3496 if (suggested_action)
3498 set_status_pending (context, 0);
3500 /* We are getting this data due to a request in drag_motion,
3501 * rather than due to a request in drag_drop, so we are just
3502 * supposed to call drag_status, not actually paste in the
3505 str = (gchar*) gtk_selection_data_get_text (selection_data);
3509 date = g_date_new ();
3510 g_date_set_parse (date, str);
3511 if (!g_date_valid (date))
3512 suggested_action = 0;
3517 suggested_action = 0;
3519 gdk_drag_status (context, suggested_action, time);
3524 date = g_date_new ();
3525 str = (gchar*) gtk_selection_data_get_text (selection_data);
3528 g_date_set_parse (date, str);
3532 if (!g_date_valid (date))
3534 g_warning ("Received invalid date data\n");
3536 gtk_drag_finish (context, FALSE, FALSE, time);
3540 day = g_date_get_day (date);
3541 month = g_date_get_month (date);
3542 year = g_date_get_year (date);
3545 gtk_drag_finish (context, TRUE, FALSE, time);
3548 g_object_freeze_notify (G_OBJECT (calendar));
3549 if (!(priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
3550 && (priv->display_flags & GTK_CALENDAR_SHOW_HEADING))
3551 gtk_calendar_select_month (calendar, month - 1, year);
3552 gtk_calendar_select_day (calendar, day);
3553 g_object_thaw_notify (G_OBJECT (calendar));
3557 /****************************************
3559 ****************************************/
3564 * Creates a new calendar, with the current date being selected.
3566 * Return value: a newly #GtkCalendar widget
3569 gtk_calendar_new (void)
3571 return g_object_new (GTK_TYPE_CALENDAR, NULL);
3575 * gtk_calendar_get_display_options:
3576 * @calendar: a #GtkCalendar
3578 * Returns the current display options of @calendar.
3580 * Return value: the display options.
3584 GtkCalendarDisplayOptions
3585 gtk_calendar_get_display_options (GtkCalendar *calendar)
3587 g_return_val_if_fail (GTK_IS_CALENDAR (calendar), 0);
3589 return calendar->priv->display_flags;
3593 * gtk_calendar_set_display_options:
3594 * @calendar: a #GtkCalendar
3595 * @flags: the display options to set
3597 * Sets display options (whether to display the heading and the month
3603 gtk_calendar_set_display_options (GtkCalendar *calendar,
3604 GtkCalendarDisplayOptions flags)
3606 GtkWidget *widget = GTK_WIDGET (calendar);
3607 GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
3609 GtkCalendarDisplayOptions old_flags;
3611 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3613 old_flags = priv->display_flags;
3615 if (gtk_widget_get_realized (widget))
3617 if ((flags ^ priv->display_flags) & GTK_CALENDAR_NO_MONTH_CHANGE)
3620 if (! (flags & GTK_CALENDAR_NO_MONTH_CHANGE)
3621 && (priv->display_flags & GTK_CALENDAR_SHOW_HEADING))
3623 priv->display_flags &= ~GTK_CALENDAR_NO_MONTH_CHANGE;
3624 calendar_realize_arrows (calendar);
3625 if (gtk_widget_get_mapped (widget))
3626 calendar_map_arrows (calendar);
3630 calendar_unrealize_arrows (calendar);
3634 if ((flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_HEADING)
3638 if (flags & GTK_CALENDAR_SHOW_HEADING)
3640 priv->display_flags |= GTK_CALENDAR_SHOW_HEADING;
3641 calendar_realize_arrows (calendar);
3642 if (gtk_widget_get_mapped (widget))
3643 calendar_map_arrows (calendar);
3647 calendar_unrealize_arrows (calendar);
3652 if ((flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_DAY_NAMES)
3656 if (flags & GTK_CALENDAR_SHOW_DAY_NAMES)
3657 priv->display_flags |= GTK_CALENDAR_SHOW_DAY_NAMES;
3660 if ((flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
3664 if (flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
3665 priv->display_flags |= GTK_CALENDAR_SHOW_WEEK_NUMBERS;
3668 if ((flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_DETAILS)
3671 priv->display_flags = flags;
3673 gtk_widget_queue_resize (GTK_WIDGET (calendar));
3676 priv->display_flags = flags;
3678 g_object_freeze_notify (G_OBJECT (calendar));
3679 if ((old_flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_HEADING)
3680 g_object_notify (G_OBJECT (calendar), "show-heading");
3681 if ((old_flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_DAY_NAMES)
3682 g_object_notify (G_OBJECT (calendar), "show-day-names");
3683 if ((old_flags ^ priv->display_flags) & GTK_CALENDAR_NO_MONTH_CHANGE)
3684 g_object_notify (G_OBJECT (calendar), "no-month-change");
3685 if ((old_flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
3686 g_object_notify (G_OBJECT (calendar), "show-week-numbers");
3687 g_object_thaw_notify (G_OBJECT (calendar));
3691 * gtk_calendar_select_month:
3692 * @calendar: a #GtkCalendar
3693 * @month: a month number between 0 and 11.
3694 * @year: the year the month is in.
3696 * Shifts the calendar to a different month.
3699 gtk_calendar_select_month (GtkCalendar *calendar,
3703 GtkCalendarPrivate *priv;
3705 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3706 g_return_if_fail (month <= 11);
3708 priv = calendar->priv;
3710 priv->month = month;
3713 calendar_compute_days (calendar);
3714 calendar_queue_refresh (calendar);
3716 g_object_freeze_notify (G_OBJECT (calendar));
3717 g_object_notify (G_OBJECT (calendar), "month");
3718 g_object_notify (G_OBJECT (calendar), "year");
3719 g_object_thaw_notify (G_OBJECT (calendar));
3721 g_signal_emit (calendar,
3722 gtk_calendar_signals[MONTH_CHANGED_SIGNAL],
3727 * gtk_calendar_select_day:
3728 * @calendar: a #GtkCalendar.
3729 * @day: the day number between 1 and 31, or 0 to unselect
3730 * the currently selected day.
3732 * Selects a day from the current month.
3735 gtk_calendar_select_day (GtkCalendar *calendar,
3738 GtkCalendarPrivate *priv;
3740 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3741 g_return_if_fail (day <= 31);
3743 priv = calendar->priv;
3745 /* Deselect the old day */
3746 if (priv->selected_day > 0)
3750 selected_day = priv->selected_day;
3751 priv->selected_day = 0;
3752 if (gtk_widget_is_drawable (GTK_WIDGET (calendar)))
3753 calendar_invalidate_day_num (calendar, selected_day);
3756 priv->selected_day = day;
3758 /* Select the new day */
3761 if (gtk_widget_is_drawable (GTK_WIDGET (calendar)))
3762 calendar_invalidate_day_num (calendar, day);
3765 g_object_notify (G_OBJECT (calendar), "day");
3767 g_signal_emit (calendar,
3768 gtk_calendar_signals[DAY_SELECTED_SIGNAL],
3773 * gtk_calendar_clear_marks:
3774 * @calendar: a #GtkCalendar
3776 * Remove all visual markers.
3779 gtk_calendar_clear_marks (GtkCalendar *calendar)
3781 GtkCalendarPrivate *priv;
3784 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3786 priv = calendar->priv;
3788 for (day = 0; day < 31; day++)
3790 priv->marked_date[day] = FALSE;
3793 priv->num_marked_dates = 0;
3794 calendar_queue_refresh (calendar);
3798 * gtk_calendar_mark_day:
3799 * @calendar: a #GtkCalendar
3800 * @day: the day number to mark between 1 and 31.
3802 * Places a visual marker on a particular day.
3805 gtk_calendar_mark_day (GtkCalendar *calendar,
3808 GtkCalendarPrivate *priv;
3810 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3812 priv = calendar->priv;
3814 if (day >= 1 && day <= 31 && !priv->marked_date[day-1])
3816 priv->marked_date[day - 1] = TRUE;
3817 priv->num_marked_dates++;
3818 calendar_invalidate_day_num (calendar, day);
3823 * gtk_calendar_get_day_is_marked:
3824 * @calendar: a #GtkCalendar
3825 * @day: the day number between 1 and 31.
3827 * Returns if the @day of the @calendar is already marked.
3829 * Returns: whether the day is marked.
3834 gtk_calendar_get_day_is_marked (GtkCalendar *calendar,
3837 GtkCalendarPrivate *priv;
3839 g_return_val_if_fail (GTK_IS_CALENDAR (calendar), FALSE);
3841 priv = calendar->priv;
3843 if (day >= 1 && day <= 31)
3844 return priv->marked_date[day - 1];
3850 * gtk_calendar_unmark_day:
3851 * @calendar: a #GtkCalendar.
3852 * @day: the day number to unmark between 1 and 31.
3854 * Removes the visual marker from a particular day.
3857 gtk_calendar_unmark_day (GtkCalendar *calendar,
3860 GtkCalendarPrivate *priv;
3862 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3864 priv = calendar->priv;
3866 if (day >= 1 && day <= 31 && priv->marked_date[day-1])
3868 priv->marked_date[day - 1] = FALSE;
3869 priv->num_marked_dates--;
3870 calendar_invalidate_day_num (calendar, day);
3875 * gtk_calendar_get_date:
3876 * @calendar: a #GtkCalendar
3877 * @year: (allow-none): location to store the year number, or %NULL
3878 * @month: (allow-none): location to store the month number (between 0 and 11), or %NULL
3879 * @day: (allow-none): location to store the day number (between 1 and 31), or %NULL
3881 * Obtains the selected date from a #GtkCalendar.
3884 gtk_calendar_get_date (GtkCalendar *calendar,
3889 GtkCalendarPrivate *priv;
3891 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3893 priv = calendar->priv;
3899 *month = priv->month;
3902 *day = priv->selected_day;
3906 * gtk_calendar_set_detail_func:
3907 * @calendar: a #GtkCalendar.
3908 * @func: a function providing details for each day.
3909 * @data: data to pass to @func invokations.
3910 * @destroy: a function for releasing @data.
3912 * Installs a function which provides Pango markup with detail information
3913 * for each day. Examples for such details are holidays or appointments. That
3914 * information is shown below each day when #GtkCalendar:show-details is set.
3915 * A tooltip containing with full detail information is provided, if the entire
3916 * text should not fit into the details area, or if #GtkCalendar:show-details
3919 * The size of the details area can be restricted by setting the
3920 * #GtkCalendar:detail-width-chars and #GtkCalendar:detail-height-rows
3926 gtk_calendar_set_detail_func (GtkCalendar *calendar,
3927 GtkCalendarDetailFunc func,
3929 GDestroyNotify destroy)
3931 GtkCalendarPrivate *priv;
3933 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3935 priv = GTK_CALENDAR_GET_PRIVATE (calendar);
3937 if (priv->detail_func_destroy)
3938 priv->detail_func_destroy (priv->detail_func_user_data);
3940 priv->detail_func = func;
3941 priv->detail_func_user_data = data;
3942 priv->detail_func_destroy = destroy;
3944 gtk_widget_set_has_tooltip (GTK_WIDGET (calendar),
3945 NULL != priv->detail_func);
3946 gtk_widget_queue_resize (GTK_WIDGET (calendar));
3950 * gtk_calendar_set_detail_width_chars:
3951 * @calendar: a #GtkCalendar.
3952 * @chars: detail width in characters.
3954 * Updates the width of detail cells.
3955 * See #GtkCalendar:detail-width-chars.
3960 gtk_calendar_set_detail_width_chars (GtkCalendar *calendar,
3963 GtkCalendarPrivate *priv;
3965 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3967 priv = GTK_CALENDAR_GET_PRIVATE (calendar);
3969 if (chars != priv->detail_width_chars)
3971 priv->detail_width_chars = chars;
3972 g_object_notify (G_OBJECT (calendar), "detail-width-chars");
3973 gtk_widget_queue_resize_no_redraw (GTK_WIDGET (calendar));
3978 * gtk_calendar_set_detail_height_rows:
3979 * @calendar: a #GtkCalendar.
3980 * @rows: detail height in rows.
3982 * Updates the height of detail cells.
3983 * See #GtkCalendar:detail-height-rows.
3988 gtk_calendar_set_detail_height_rows (GtkCalendar *calendar,
3991 GtkCalendarPrivate *priv;
3993 g_return_if_fail (GTK_IS_CALENDAR (calendar));
3995 priv = GTK_CALENDAR_GET_PRIVATE (calendar);
3997 if (rows != priv->detail_height_rows)
3999 priv->detail_height_rows = rows;
4000 g_object_notify (G_OBJECT (calendar), "detail-height-rows");
4001 gtk_widget_queue_resize_no_redraw (GTK_WIDGET (calendar));
4006 * gtk_calendar_get_detail_width_chars:
4007 * @calendar: a #GtkCalendar.
4009 * Queries the width of detail cells, in characters.
4010 * See #GtkCalendar:detail-width-chars.
4014 * Return value: The width of detail cells, in characters.
4017 gtk_calendar_get_detail_width_chars (GtkCalendar *calendar)
4019 g_return_val_if_fail (GTK_IS_CALENDAR (calendar), 0);
4020 return GTK_CALENDAR_GET_PRIVATE (calendar)->detail_width_chars;
4024 * gtk_calendar_get_detail_height_rows:
4025 * @calendar: a #GtkCalendar.
4027 * Queries the height of detail cells, in rows.
4028 * See #GtkCalendar:detail-width-chars.
4032 * Return value: The height of detail cells, in rows.
4035 gtk_calendar_get_detail_height_rows (GtkCalendar *calendar)
4037 g_return_val_if_fail (GTK_IS_CALENDAR (calendar), 0);
4038 return GTK_CALENDAR_GET_PRIVATE (calendar)->detail_height_rows;