]> Pileus Git - ~andy/gtk/blob - gtk/gtkcalendar.c
Merge branch 'gdk-backend-wayland'
[~andy/gtk] / gtk / gtkcalendar.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * GTK Calendar Widget
5  * Copyright (C) 1998 Cesar Miquel, Shawn T. Amundson and Mattias Groenlund
6  *
7  * lib_date routines
8  * Copyright (c) 1995, 1996, 1997, 1998 by Steffen Beyer
9  *
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.
14  *
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.
19  *
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.
23  */
24
25 /*
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/.
30  */
31
32 /**
33  * SECTION:gtkcalendar
34  * @Short_description: Displays a calendar and allows the user to select a date
35  * @Title: GtkCalendar
36  *
37  * #GtkCalendar is a widget that displays a Gregorian calendar, one month
38  * at a time. It can be created with gtk_calendar_new().
39  *
40  * The month and year currently displayed can be altered with
41  * gtk_calendar_select_month(). The exact day can be selected from the
42  * displayed month using gtk_calendar_select_day().
43  *
44  * To place a visual marker on a particular day, use gtk_calendar_mark_day()
45  * and to remove the marker, gtk_calendar_unmark_day(). Alternative, all
46  * marks can be cleared with gtk_calendar_clear_marks().
47  *
48  * The way in which the calendar itself is displayed can be altered using
49  * gtk_calendar_set_display_options().
50  *
51  * The selected date can be retrieved from a #GtkCalendar using
52  * gtk_calendar_get_date().
53  *
54  * Users should be aware that, although the Gregorian calendar is the legal
55  * calendar in most countries, it was adopted progressively between 1582 and
56  * 1929. Display before these dates is likely to be historically incorrect.
57  */
58
59 #include "config.h"
60
61 #ifdef HAVE_SYS_TIME_H
62 #include <sys/time.h>
63 #endif
64 #ifdef HAVE__NL_TIME_FIRST_WEEKDAY
65 #include <langinfo.h>
66 #endif
67 #include <string.h>
68 #include <stdlib.h>
69 #include <time.h>
70
71 #include <glib.h>
72
73 #ifdef G_OS_WIN32
74 #include <windows.h>
75 #endif
76
77 #include "gtkcalendar.h"
78 #include "gtkdnd.h"
79 #include "gtkintl.h"
80 #include "gtkmain.h"
81 #include "gtkmarshalers.h"
82 #include "gtktooltip.h"
83 #include "gtkprivate.h"
84
85 /***************************************************************************/
86 /* The following date routines are taken from the lib_date package.
87  * They have been minimally edited to avoid conflict with types defined
88  * in win32 headers.
89  */
90
91 static const guint month_length[2][13] =
92 {
93   { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
94   { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
95 };
96
97 static const guint days_in_months[2][14] =
98 {
99   { 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
100   { 0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
101 };
102
103 static glong  calc_days(guint year, guint mm, guint dd);
104 static guint  day_of_week(guint year, guint mm, guint dd);
105 static glong  dates_difference(guint year1, guint mm1, guint dd1,
106                                guint year2, guint mm2, guint dd2);
107 static guint  weeks_in_year(guint year);
108
109 static gboolean
110 leap (guint year)
111 {
112   return((((year % 4) == 0) && ((year % 100) != 0)) || ((year % 400) == 0));
113 }
114
115 static guint
116 day_of_week (guint year, guint mm, guint dd)
117 {
118   glong  days;
119
120   days = calc_days(year, mm, dd);
121   if (days > 0L)
122     {
123       days--;
124       days %= 7L;
125       days++;
126     }
127   return( (guint) days );
128 }
129
130 static guint weeks_in_year(guint year)
131 {
132   return(52 + ((day_of_week(year,1,1)==4) || (day_of_week(year,12,31)==4)));
133 }
134
135 static gboolean
136 check_date(guint year, guint mm, guint dd)
137 {
138   if (year < 1) return FALSE;
139   if ((mm < 1) || (mm > 12)) return FALSE;
140   if ((dd < 1) || (dd > month_length[leap(year)][mm])) return FALSE;
141   return TRUE;
142 }
143
144 static guint
145 week_number(guint year, guint mm, guint dd)
146 {
147   guint first;
148
149   first = day_of_week(year,1,1) - 1;
150   return( (guint) ( (dates_difference(year,1,1, year,mm,dd) + first) / 7L ) +
151           (first < 4) );
152 }
153
154 static glong
155 year_to_days(guint year)
156 {
157   return( year * 365L + (year / 4) - (year / 100) + (year / 400) );
158 }
159
160
161 static glong
162 calc_days(guint year, guint mm, guint dd)
163 {
164   gboolean lp;
165
166   if (year < 1) return(0L);
167   if ((mm < 1) || (mm > 12)) return(0L);
168   if ((dd < 1) || (dd > month_length[(lp = leap(year))][mm])) return(0L);
169   return( year_to_days(--year) + days_in_months[lp][mm] + dd );
170 }
171
172 static gboolean
173 week_of_year(guint *week, guint *year, guint mm, guint dd)
174 {
175   if (check_date(*year,mm,dd))
176     {
177       *week = week_number(*year,mm,dd);
178       if (*week == 0)
179         *week = weeks_in_year(--(*year));
180       else if (*week > weeks_in_year(*year))
181         {
182           *week = 1;
183           (*year)++;
184         }
185       return TRUE;
186     }
187   return FALSE;
188 }
189
190 static glong
191 dates_difference(guint year1, guint mm1, guint dd1,
192                  guint year2, guint mm2, guint dd2)
193 {
194   return( calc_days(year2, mm2, dd2) - calc_days(year1, mm1, dd1) );
195 }
196
197 /*** END OF lib_date routines ********************************************/
198
199 /* Spacing around day/week headers and main area, inside those windows */
200 #define CALENDAR_MARGIN          0
201
202 #define DAY_XSEP                 0 /* not really good for small calendar */
203 #define DAY_YSEP                 0 /* not really good for small calendar */
204
205 #define SCROLL_DELAY_FACTOR      5
206
207 enum {
208   ARROW_YEAR_LEFT,
209   ARROW_YEAR_RIGHT,
210   ARROW_MONTH_LEFT,
211   ARROW_MONTH_RIGHT
212 };
213
214 enum {
215   MONTH_PREV,
216   MONTH_CURRENT,
217   MONTH_NEXT
218 };
219
220 enum {
221   MONTH_CHANGED_SIGNAL,
222   DAY_SELECTED_SIGNAL,
223   DAY_SELECTED_DOUBLE_CLICK_SIGNAL,
224   PREV_MONTH_SIGNAL,
225   NEXT_MONTH_SIGNAL,
226   PREV_YEAR_SIGNAL,
227   NEXT_YEAR_SIGNAL,
228   LAST_SIGNAL
229 };
230
231 enum
232 {
233   PROP_0,
234   PROP_YEAR,
235   PROP_MONTH,
236   PROP_DAY,
237   PROP_SHOW_HEADING,
238   PROP_SHOW_DAY_NAMES,
239   PROP_NO_MONTH_CHANGE,
240   PROP_SHOW_WEEK_NUMBERS,
241   PROP_SHOW_DETAILS,
242   PROP_DETAIL_WIDTH_CHARS,
243   PROP_DETAIL_HEIGHT_ROWS
244 };
245
246 static guint gtk_calendar_signals[LAST_SIGNAL] = { 0 };
247
248 struct _GtkCalendarPrivate
249 {
250   GtkCalendarDisplayOptions display_flags;
251
252   GdkColor marked_date_color[31];
253   GdkWindow *main_win;
254   GdkWindow *arrow_win[4];
255
256   gchar grow_space [32];
257
258   gint  month;
259   gint  year;
260   gint  selected_day;
261
262   gint  day_month[6][7];
263   gint  day[6][7];
264
265   gint  num_marked_dates;
266   gint  marked_date[31];
267
268   gint  focus_row;
269   gint  focus_col;
270
271   guint header_h;
272   guint day_name_h;
273   guint main_h;
274
275   guint arrow_state[4];
276   guint arrow_width;
277   guint max_month_width;
278   guint max_year_width;
279
280   guint day_width;
281   guint week_width;
282
283   guint min_day_width;
284   guint max_day_char_width;
285   guint max_day_char_ascent;
286   guint max_day_char_descent;
287   guint max_label_char_ascent;
288   guint max_label_char_descent;
289   guint max_week_char_width;
290
291   /* flags */
292   guint year_before : 1;
293
294   guint need_timer  : 1;
295
296   guint in_drag : 1;
297   guint drag_highlight : 1;
298
299   guint32 timer;
300   gint click_child;
301
302   gint week_start;
303
304   gint drag_start_x;
305   gint drag_start_y;
306
307   /* Optional callback, used to display extra information for each day. */
308   GtkCalendarDetailFunc detail_func;
309   gpointer              detail_func_user_data;
310   GDestroyNotify        detail_func_destroy;
311
312   /* Size requistion for details provided by the hook. */
313   gint detail_height_rows;
314   gint detail_width_chars;
315   gint detail_overflow[6];
316 };
317
318 #define GTK_CALENDAR_GET_PRIVATE(widget)  (GTK_CALENDAR (widget)->priv)
319
320 static void gtk_calendar_finalize     (GObject      *calendar);
321 static void gtk_calendar_destroy      (GtkWidget    *widget);
322 static void gtk_calendar_set_property (GObject      *object,
323                                        guint         prop_id,
324                                        const GValue *value,
325                                        GParamSpec   *pspec);
326 static void gtk_calendar_get_property (GObject      *object,
327                                        guint         prop_id,
328                                        GValue       *value,
329                                        GParamSpec   *pspec);
330
331 static void     gtk_calendar_realize        (GtkWidget        *widget);
332 static void     gtk_calendar_unrealize      (GtkWidget        *widget);
333 static void     gtk_calendar_map            (GtkWidget        *widget);
334 static void     gtk_calendar_unmap          (GtkWidget        *widget);
335 static void     gtk_calendar_get_preferred_width  (GtkWidget   *widget,
336                                                    gint        *minimum,
337                                                    gint        *natural);
338 static void     gtk_calendar_get_preferred_height (GtkWidget   *widget,
339                                                    gint        *minimum,
340                                                    gint        *natural);
341 static void     gtk_calendar_size_allocate  (GtkWidget        *widget,
342                                              GtkAllocation    *allocation);
343 static gboolean gtk_calendar_draw           (GtkWidget        *widget,
344                                              cairo_t          *cr);
345 static gboolean gtk_calendar_button_press   (GtkWidget        *widget,
346                                              GdkEventButton   *event);
347 static gboolean gtk_calendar_button_release (GtkWidget        *widget,
348                                              GdkEventButton   *event);
349 static gboolean gtk_calendar_motion_notify  (GtkWidget        *widget,
350                                              GdkEventMotion   *event);
351 static gboolean gtk_calendar_enter_notify   (GtkWidget        *widget,
352                                              GdkEventCrossing *event);
353 static gboolean gtk_calendar_leave_notify   (GtkWidget        *widget,
354                                              GdkEventCrossing *event);
355 static gboolean gtk_calendar_scroll         (GtkWidget        *widget,
356                                              GdkEventScroll   *event);
357 static gboolean gtk_calendar_key_press      (GtkWidget        *widget,
358                                              GdkEventKey      *event);
359 static gboolean gtk_calendar_focus_out      (GtkWidget        *widget,
360                                              GdkEventFocus    *event);
361 static void     gtk_calendar_grab_notify    (GtkWidget        *widget,
362                                              gboolean          was_grabbed);
363 static void     gtk_calendar_state_flags_changed  (GtkWidget     *widget,
364                                                    GtkStateFlags  previous_state);
365 static gboolean gtk_calendar_query_tooltip  (GtkWidget        *widget,
366                                              gint              x,
367                                              gint              y,
368                                              gboolean          keyboard_mode,
369                                              GtkTooltip       *tooltip);
370
371 static void     gtk_calendar_drag_data_get      (GtkWidget        *widget,
372                                                  GdkDragContext   *context,
373                                                  GtkSelectionData *selection_data,
374                                                  guint             info,
375                                                  guint             time);
376 static void     gtk_calendar_drag_data_received (GtkWidget        *widget,
377                                                  GdkDragContext   *context,
378                                                  gint              x,
379                                                  gint              y,
380                                                  GtkSelectionData *selection_data,
381                                                  guint             info,
382                                                  guint             time);
383 static gboolean gtk_calendar_drag_motion        (GtkWidget        *widget,
384                                                  GdkDragContext   *context,
385                                                  gint              x,
386                                                  gint              y,
387                                                  guint             time);
388 static void     gtk_calendar_drag_leave         (GtkWidget        *widget,
389                                                  GdkDragContext   *context,
390                                                  guint             time);
391 static gboolean gtk_calendar_drag_drop          (GtkWidget        *widget,
392                                                  GdkDragContext   *context,
393                                                  gint              x,
394                                                  gint              y,
395                                                  guint             time);
396
397 static void calendar_start_spinning (GtkCalendar *calendar,
398                                      gint         click_child);
399 static void calendar_stop_spinning  (GtkCalendar *calendar);
400
401 static void calendar_invalidate_day     (GtkCalendar *widget,
402                                          gint       row,
403                                          gint       col);
404 static void calendar_invalidate_day_num (GtkCalendar *widget,
405                                          gint       day);
406 static void calendar_invalidate_arrow   (GtkCalendar *widget,
407                                          guint      arrow);
408
409 static void calendar_compute_days      (GtkCalendar *calendar);
410 static gint calendar_get_xsep          (GtkCalendar *calendar);
411 static gint calendar_get_ysep          (GtkCalendar *calendar);
412 static gint calendar_get_inner_border  (GtkCalendar *calendar);
413
414 static char    *default_abbreviated_dayname[7];
415 static char    *default_monthname[12];
416
417 G_DEFINE_TYPE (GtkCalendar, gtk_calendar, GTK_TYPE_WIDGET)
418
419 static void
420 gtk_calendar_class_init (GtkCalendarClass *class)
421 {
422   GObjectClass   *gobject_class;
423   GtkWidgetClass *widget_class;
424
425   gobject_class = (GObjectClass*)  class;
426   widget_class = (GtkWidgetClass*) class;
427
428   gobject_class->set_property = gtk_calendar_set_property;
429   gobject_class->get_property = gtk_calendar_get_property;
430   gobject_class->finalize = gtk_calendar_finalize;
431
432   widget_class->destroy = gtk_calendar_destroy;
433   widget_class->realize = gtk_calendar_realize;
434   widget_class->unrealize = gtk_calendar_unrealize;
435   widget_class->map = gtk_calendar_map;
436   widget_class->unmap = gtk_calendar_unmap;
437   widget_class->draw = gtk_calendar_draw;
438   widget_class->get_preferred_width = gtk_calendar_get_preferred_width;
439   widget_class->get_preferred_height = gtk_calendar_get_preferred_height;
440   widget_class->size_allocate = gtk_calendar_size_allocate;
441   widget_class->button_press_event = gtk_calendar_button_press;
442   widget_class->button_release_event = gtk_calendar_button_release;
443   widget_class->motion_notify_event = gtk_calendar_motion_notify;
444   widget_class->enter_notify_event = gtk_calendar_enter_notify;
445   widget_class->leave_notify_event = gtk_calendar_leave_notify;
446   widget_class->key_press_event = gtk_calendar_key_press;
447   widget_class->scroll_event = gtk_calendar_scroll;
448   widget_class->state_flags_changed = gtk_calendar_state_flags_changed;
449   widget_class->grab_notify = gtk_calendar_grab_notify;
450   widget_class->focus_out_event = gtk_calendar_focus_out;
451   widget_class->query_tooltip = gtk_calendar_query_tooltip;
452
453   widget_class->drag_data_get = gtk_calendar_drag_data_get;
454   widget_class->drag_motion = gtk_calendar_drag_motion;
455   widget_class->drag_leave = gtk_calendar_drag_leave;
456   widget_class->drag_drop = gtk_calendar_drag_drop;
457   widget_class->drag_data_received = gtk_calendar_drag_data_received;
458
459   /**
460    * GtkCalendar:year:
461    *
462    * The selected year.
463    * This property gets initially set to the current year.
464    */
465   g_object_class_install_property (gobject_class,
466                                    PROP_YEAR,
467                                    g_param_spec_int ("year",
468                                                      P_("Year"),
469                                                      P_("The selected year"),
470                                                      0, G_MAXINT >> 9, 0,
471                                                      GTK_PARAM_READWRITE));
472
473   /**
474    * GtkCalendar:month:
475    *
476    * The selected month (as a number between 0 and 11).
477    * This property gets initially set to the current month.
478    */
479   g_object_class_install_property (gobject_class,
480                                    PROP_MONTH,
481                                    g_param_spec_int ("month",
482                                                      P_("Month"),
483                                                      P_("The selected month (as a number between 0 and 11)"),
484                                                      0, 11, 0,
485                                                      GTK_PARAM_READWRITE));
486
487   /**
488    * GtkCalendar:day:
489    *
490    * The selected day (as a number between 1 and 31, or 0
491    * to unselect the currently selected day).
492    * This property gets initially set to the current day.
493    */
494   g_object_class_install_property (gobject_class,
495                                    PROP_DAY,
496                                    g_param_spec_int ("day",
497                                                      P_("Day"),
498                                                      P_("The selected day (as a number between 1 and 31, or 0 to unselect the currently selected day)"),
499                                                      0, 31, 0,
500                                                      GTK_PARAM_READWRITE));
501
502 /**
503  * GtkCalendar:show-heading:
504  *
505  * Determines whether a heading is displayed.
506  *
507  * Since: 2.4
508  */
509   g_object_class_install_property (gobject_class,
510                                    PROP_SHOW_HEADING,
511                                    g_param_spec_boolean ("show-heading",
512                                                          P_("Show Heading"),
513                                                          P_("If TRUE, a heading is displayed"),
514                                                          TRUE,
515                                                          GTK_PARAM_READWRITE));
516
517 /**
518  * GtkCalendar:show-day-names:
519  *
520  * Determines whether day names are displayed.
521  *
522  * Since: 2.4
523  */
524   g_object_class_install_property (gobject_class,
525                                    PROP_SHOW_DAY_NAMES,
526                                    g_param_spec_boolean ("show-day-names",
527                                                          P_("Show Day Names"),
528                                                          P_("If TRUE, day names are displayed"),
529                                                          TRUE,
530                                                          GTK_PARAM_READWRITE));
531 /**
532  * GtkCalendar:no-month-change:
533  *
534  * Determines whether the selected month can be changed.
535  *
536  * Since: 2.4
537  */
538   g_object_class_install_property (gobject_class,
539                                    PROP_NO_MONTH_CHANGE,
540                                    g_param_spec_boolean ("no-month-change",
541                                                          P_("No Month Change"),
542                                                          P_("If TRUE, the selected month cannot be changed"),
543                                                          FALSE,
544                                                          GTK_PARAM_READWRITE));
545
546 /**
547  * GtkCalendar:show-week-numbers:
548  *
549  * Determines whether week numbers are displayed.
550  *
551  * Since: 2.4
552  */
553   g_object_class_install_property (gobject_class,
554                                    PROP_SHOW_WEEK_NUMBERS,
555                                    g_param_spec_boolean ("show-week-numbers",
556                                                          P_("Show Week Numbers"),
557                                                          P_("If TRUE, week numbers are displayed"),
558                                                          FALSE,
559                                                          GTK_PARAM_READWRITE));
560
561 /**
562  * GtkCalendar:detail-width-chars:
563  *
564  * Width of a detail cell, in characters.
565  * A value of 0 allows any width. See gtk_calendar_set_detail_func().
566  *
567  * Since: 2.14
568  */
569   g_object_class_install_property (gobject_class,
570                                    PROP_DETAIL_WIDTH_CHARS,
571                                    g_param_spec_int ("detail-width-chars",
572                                                      P_("Details Width"),
573                                                      P_("Details width in characters"),
574                                                      0, 127, 0,
575                                                      GTK_PARAM_READWRITE));
576
577 /**
578  * GtkCalendar:detail-height-rows:
579  *
580  * Height of a detail cell, in rows.
581  * A value of 0 allows any width. See gtk_calendar_set_detail_func().
582  *
583  * Since: 2.14
584  */
585   g_object_class_install_property (gobject_class,
586                                    PROP_DETAIL_HEIGHT_ROWS,
587                                    g_param_spec_int ("detail-height-rows",
588                                                      P_("Details Height"),
589                                                      P_("Details height in rows"),
590                                                      0, 127, 0,
591                                                      GTK_PARAM_READWRITE));
592
593 /**
594  * GtkCalendar:show-details:
595  *
596  * Determines whether details are shown directly in the widget, or if they are
597  * available only as tooltip. When this property is set days with details are
598  * marked.
599  *
600  * Since: 2.14
601  */
602   g_object_class_install_property (gobject_class,
603                                    PROP_SHOW_DETAILS,
604                                    g_param_spec_boolean ("show-details",
605                                                          P_("Show Details"),
606                                                          P_("If TRUE, details are shown"),
607                                                          TRUE,
608                                                          GTK_PARAM_READWRITE));
609
610
611   /**
612    * GtkCalendar:inner-border
613    *
614    * The spacing around the day/week headers and main area.
615    */
616   gtk_widget_class_install_style_property (widget_class,
617                                            g_param_spec_int ("inner-border",
618                                                              P_("Inner border"),
619                                                              P_("Inner border space"),
620                                                              0, G_MAXINT, 4,
621                                                              GTK_PARAM_READABLE));
622
623   /**
624    * GtkCalndar:vertical-separation
625    *
626    * Separation between day headers and main area.
627    */
628   gtk_widget_class_install_style_property (widget_class,
629                                            g_param_spec_int ("vertical-separation",
630                                                              P_("Vertical separation"),
631                                                              P_("Space between day headers and main area"),
632                                                              0, G_MAXINT, 4,
633                                                              GTK_PARAM_READABLE));
634
635   /**
636    * GtkCalendar:horizontal-separation
637    *
638    * Separation between week headers and main area.
639    */
640   gtk_widget_class_install_style_property (widget_class,
641                                            g_param_spec_int ("horizontal-separation",
642                                                              P_("Horizontal separation"),
643                                                              P_("Space between week headers and main area"),
644                                                              0, G_MAXINT, 4,
645                                                              GTK_PARAM_READABLE));
646
647   /**
648    * GtkCalendar::month-changed:
649    * @calendar: the object which received the signal.
650    *
651    * Emitted when the user clicks a button to change the selected month on a
652    * calendar.
653    */
654   gtk_calendar_signals[MONTH_CHANGED_SIGNAL] =
655     g_signal_new (I_("month-changed"),
656                   G_OBJECT_CLASS_TYPE (gobject_class),
657                   G_SIGNAL_RUN_FIRST,
658                   G_STRUCT_OFFSET (GtkCalendarClass, month_changed),
659                   NULL, NULL,
660                   _gtk_marshal_VOID__VOID,
661                   G_TYPE_NONE, 0);
662
663   /**
664    * GtkCalendar::day-selected:
665    * @calendar: the object which received the signal.
666    *
667    * Emitted when the user selects a day.
668    */
669   gtk_calendar_signals[DAY_SELECTED_SIGNAL] =
670     g_signal_new (I_("day-selected"),
671                   G_OBJECT_CLASS_TYPE (gobject_class),
672                   G_SIGNAL_RUN_FIRST,
673                   G_STRUCT_OFFSET (GtkCalendarClass, day_selected),
674                   NULL, NULL,
675                   _gtk_marshal_VOID__VOID,
676                   G_TYPE_NONE, 0);
677
678   /**
679    * GtkCalendar::day-selected-double-click:
680    * @calendar: the object which received the signal.
681    *
682    * Emitted when the user double-clicks a day.
683    */
684   gtk_calendar_signals[DAY_SELECTED_DOUBLE_CLICK_SIGNAL] =
685     g_signal_new (I_("day-selected-double-click"),
686                   G_OBJECT_CLASS_TYPE (gobject_class),
687                   G_SIGNAL_RUN_FIRST,
688                   G_STRUCT_OFFSET (GtkCalendarClass, day_selected_double_click),
689                   NULL, NULL,
690                   _gtk_marshal_VOID__VOID,
691                   G_TYPE_NONE, 0);
692
693   /**
694    * GtkCalendar::prev-month:
695    * @calendar: the object which received the signal.
696    *
697    * Emitted when the user switched to the previous month.
698    */
699   gtk_calendar_signals[PREV_MONTH_SIGNAL] =
700     g_signal_new (I_("prev-month"),
701                   G_OBJECT_CLASS_TYPE (gobject_class),
702                   G_SIGNAL_RUN_FIRST,
703                   G_STRUCT_OFFSET (GtkCalendarClass, prev_month),
704                   NULL, NULL,
705                   _gtk_marshal_VOID__VOID,
706                   G_TYPE_NONE, 0);
707
708   /**
709    * GtkCalendar::next-month:
710    * @calendar: the object which received the signal.
711    *
712    * Emitted when the user switched to the next month.
713    */
714   gtk_calendar_signals[NEXT_MONTH_SIGNAL] =
715     g_signal_new (I_("next-month"),
716                   G_OBJECT_CLASS_TYPE (gobject_class),
717                   G_SIGNAL_RUN_FIRST,
718                   G_STRUCT_OFFSET (GtkCalendarClass, next_month),
719                   NULL, NULL,
720                   _gtk_marshal_VOID__VOID,
721                   G_TYPE_NONE, 0);
722
723   /**
724    * GtkCalendar::prev-year:
725    * @calendar: the object which received the signal.
726    *
727    * Emitted when user switched to the previous year.
728    */
729   gtk_calendar_signals[PREV_YEAR_SIGNAL] =
730     g_signal_new (I_("prev-year"),
731                   G_OBJECT_CLASS_TYPE (gobject_class),
732                   G_SIGNAL_RUN_FIRST,
733                   G_STRUCT_OFFSET (GtkCalendarClass, prev_year),
734                   NULL, NULL,
735                   _gtk_marshal_VOID__VOID,
736                   G_TYPE_NONE, 0);
737
738   /**
739    * GtkCalendar::next-year:
740    * @calendar: the object which received the signal.
741    *
742    * Emitted when user switched to the next year.
743    */
744   gtk_calendar_signals[NEXT_YEAR_SIGNAL] =
745     g_signal_new (I_("next-year"),
746                   G_OBJECT_CLASS_TYPE (gobject_class),
747                   G_SIGNAL_RUN_FIRST,
748                   G_STRUCT_OFFSET (GtkCalendarClass, next_year),
749                   NULL, NULL,
750                   _gtk_marshal_VOID__VOID,
751                   G_TYPE_NONE, 0);
752
753   g_type_class_add_private (gobject_class, sizeof (GtkCalendarPrivate));
754 }
755
756 static void
757 gtk_calendar_init (GtkCalendar *calendar)
758 {
759   GtkWidget *widget = GTK_WIDGET (calendar);
760   time_t secs;
761   struct tm *tm;
762   gint i;
763 #ifdef G_OS_WIN32
764   wchar_t wbuffer[100];
765 #else
766   char buffer[255];
767   time_t tmp_time;
768 #endif
769   GtkCalendarPrivate *priv;
770   gchar *year_before;
771 #ifdef HAVE__NL_TIME_FIRST_WEEKDAY
772   union { unsigned int word; char *string; } langinfo;
773   gint week_1stday = 0;
774   gint first_weekday = 1;
775   guint week_origin;
776 #else
777   gchar *week_start;
778 #endif
779
780   priv = calendar->priv = G_TYPE_INSTANCE_GET_PRIVATE (calendar,
781                                                        GTK_TYPE_CALENDAR,
782                                                        GtkCalendarPrivate);
783
784   gtk_widget_set_can_focus (widget, TRUE);
785   gtk_widget_set_has_window (widget, FALSE);
786
787   if (!default_abbreviated_dayname[0])
788     for (i=0; i<7; i++)
789       {
790 #ifndef G_OS_WIN32
791         tmp_time= (i+3)*86400;
792         strftime ( buffer, sizeof (buffer), "%a", gmtime (&tmp_time));
793         default_abbreviated_dayname[i] = g_locale_to_utf8 (buffer, -1, NULL, NULL, NULL);
794 #else
795         if (!GetLocaleInfoW (GetThreadLocale (), LOCALE_SABBREVDAYNAME1 + (i+6)%7,
796                              wbuffer, G_N_ELEMENTS (wbuffer)))
797           default_abbreviated_dayname[i] = g_strdup_printf ("(%d)", i);
798         else
799           default_abbreviated_dayname[i] = g_utf16_to_utf8 (wbuffer, -1, NULL, NULL, NULL);
800 #endif
801       }
802
803   if (!default_monthname[0])
804     for (i=0; i<12; i++)
805       {
806 #ifndef G_OS_WIN32
807         tmp_time=i*2764800;
808         strftime ( buffer, sizeof (buffer), "%B", gmtime (&tmp_time));
809         default_monthname[i] = g_locale_to_utf8 (buffer, -1, NULL, NULL, NULL);
810 #else
811         if (!GetLocaleInfoW (GetThreadLocale (), LOCALE_SMONTHNAME1 + i,
812                              wbuffer, G_N_ELEMENTS (wbuffer)))
813           default_monthname[i] = g_strdup_printf ("(%d)", i);
814         else
815           default_monthname[i] = g_utf16_to_utf8 (wbuffer, -1, NULL, NULL, NULL);
816 #endif
817       }
818
819   /* Set defaults */
820   secs = time (NULL);
821   tm = localtime (&secs);
822   priv->month = tm->tm_mon;
823   priv->year  = 1900 + tm->tm_year;
824
825   for (i=0;i<31;i++)
826     priv->marked_date[i] = FALSE;
827   priv->num_marked_dates = 0;
828   priv->selected_day = tm->tm_mday;
829
830   priv->display_flags = (GTK_CALENDAR_SHOW_HEADING |
831                              GTK_CALENDAR_SHOW_DAY_NAMES |
832                              GTK_CALENDAR_SHOW_DETAILS);
833
834   priv->focus_row = -1;
835   priv->focus_col = -1;
836
837   priv->max_year_width = 0;
838   priv->max_month_width = 0;
839   priv->max_day_char_width = 0;
840   priv->max_week_char_width = 0;
841
842   priv->max_day_char_ascent = 0;
843   priv->max_day_char_descent = 0;
844   priv->max_label_char_ascent = 0;
845   priv->max_label_char_descent = 0;
846
847   priv->arrow_width = 10;
848
849   priv->need_timer = 0;
850   priv->timer = 0;
851   priv->click_child = -1;
852
853   priv->in_drag = 0;
854   priv->drag_highlight = 0;
855
856   gtk_drag_dest_set (widget, 0, NULL, 0, GDK_ACTION_COPY);
857   gtk_drag_dest_add_text_targets (widget);
858
859   priv->year_before = 0;
860
861   /* Translate to calendar:YM if you want years to be displayed
862    * before months; otherwise translate to calendar:MY.
863    * Do *not* translate it to anything else, if it
864    * it isn't calendar:YM or calendar:MY it will not work.
865    *
866    * Note that the ordering described here is logical order, which is
867    * further influenced by BIDI ordering. Thus, if you have a default
868    * text direction of RTL and specify "calendar:YM", then the year
869    * will appear to the right of the month.
870    */
871   year_before = _("calendar:MY");
872   if (strcmp (year_before, "calendar:YM") == 0)
873     priv->year_before = 1;
874   else if (strcmp (year_before, "calendar:MY") != 0)
875     g_warning ("Whoever translated calendar:MY did so wrongly.\n");
876
877 #ifdef G_OS_WIN32
878   priv->week_start = 0;
879   week_start = NULL;
880
881   if (GetLocaleInfoW (GetThreadLocale (), LOCALE_IFIRSTDAYOFWEEK,
882                       wbuffer, G_N_ELEMENTS (wbuffer)))
883     week_start = g_utf16_to_utf8 (wbuffer, -1, NULL, NULL, NULL);
884
885   if (week_start != NULL)
886     {
887       priv->week_start = (week_start[0] - '0' + 1) % 7;
888       g_free(week_start);
889     }
890 #else
891 #ifdef HAVE__NL_TIME_FIRST_WEEKDAY
892   langinfo.string = nl_langinfo (_NL_TIME_FIRST_WEEKDAY);
893   first_weekday = langinfo.string[0];
894   langinfo.string = nl_langinfo (_NL_TIME_WEEK_1STDAY);
895   week_origin = langinfo.word;
896   if (week_origin == 19971130) /* Sunday */
897     week_1stday = 0;
898   else if (week_origin == 19971201) /* Monday */
899     week_1stday = 1;
900   else
901     g_warning ("Unknown value of _NL_TIME_WEEK_1STDAY.\n");
902
903   priv->week_start = (week_1stday + first_weekday - 1) % 7;
904 #else
905   /* Translate to calendar:week_start:0 if you want Sunday to be the
906    * first day of the week to calendar:week_start:1 if you want Monday
907    * to be the first day of the week, and so on.
908    */
909   week_start = _("calendar:week_start:0");
910
911   if (strncmp (week_start, "calendar:week_start:", 20) == 0)
912     priv->week_start = *(week_start + 20) - '0';
913   else
914     priv->week_start = -1;
915
916   if (priv->week_start < 0 || priv->week_start > 6)
917     {
918       g_warning ("Whoever translated calendar:week_start:0 did so wrongly.\n");
919       priv->week_start = 0;
920     }
921 #endif
922 #endif
923
924   calendar_compute_days (calendar);
925 }
926
927 \f
928 /****************************************
929  *          Utility Functions           *
930  ****************************************/
931
932 static void
933 calendar_queue_refresh (GtkCalendar *calendar)
934 {
935   GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
936
937   if (!(priv->detail_func) ||
938       !(priv->display_flags & GTK_CALENDAR_SHOW_DETAILS) ||
939        (priv->detail_width_chars && priv->detail_height_rows))
940     gtk_widget_queue_draw (GTK_WIDGET (calendar));
941   else
942     gtk_widget_queue_resize (GTK_WIDGET (calendar));
943 }
944
945 static void
946 calendar_set_month_next (GtkCalendar *calendar)
947 {
948   gint month_len;
949   GtkCalendarPrivate *priv = calendar->priv;
950
951   if (priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
952     return;
953
954   if (priv->month == 11)
955     {
956       priv->month = 0;
957       priv->year++;
958     }
959   else
960     priv->month++;
961
962   calendar_compute_days (calendar);
963   g_signal_emit (calendar,
964                  gtk_calendar_signals[NEXT_MONTH_SIGNAL],
965                  0);
966   g_signal_emit (calendar,
967                  gtk_calendar_signals[MONTH_CHANGED_SIGNAL],
968                  0);
969
970   month_len = month_length[leap (priv->year)][priv->month + 1];
971
972   if (month_len < priv->selected_day)
973     {
974       priv->selected_day = 0;
975       gtk_calendar_select_day (calendar, month_len);
976     }
977   else
978     gtk_calendar_select_day (calendar, priv->selected_day);
979
980   calendar_queue_refresh (calendar);
981 }
982
983 static void
984 calendar_set_year_prev (GtkCalendar *calendar)
985 {
986   GtkCalendarPrivate *priv = calendar->priv;
987   gint month_len;
988
989   priv->year--;
990   calendar_compute_days (calendar);
991   g_signal_emit (calendar,
992                  gtk_calendar_signals[PREV_YEAR_SIGNAL],
993                  0);
994   g_signal_emit (calendar,
995                  gtk_calendar_signals[MONTH_CHANGED_SIGNAL],
996                  0);
997
998   month_len = month_length[leap (priv->year)][priv->month + 1];
999
1000   if (month_len < priv->selected_day)
1001     {
1002       priv->selected_day = 0;
1003       gtk_calendar_select_day (calendar, month_len);
1004     }
1005   else
1006     gtk_calendar_select_day (calendar, priv->selected_day);
1007
1008   calendar_queue_refresh (calendar);
1009 }
1010
1011 static void
1012 calendar_set_year_next (GtkCalendar *calendar)
1013 {
1014   GtkCalendarPrivate *priv = calendar->priv;
1015   gint month_len;
1016
1017   priv->year++;
1018   calendar_compute_days (calendar);
1019   g_signal_emit (calendar,
1020                  gtk_calendar_signals[NEXT_YEAR_SIGNAL],
1021                  0);
1022   g_signal_emit (calendar,
1023                  gtk_calendar_signals[MONTH_CHANGED_SIGNAL],
1024                  0);
1025
1026   month_len = month_length[leap (priv->year)][priv->month + 1];
1027
1028   if (month_len < priv->selected_day)
1029     {
1030       priv->selected_day = 0;
1031       gtk_calendar_select_day (calendar, month_len);
1032     }
1033   else
1034     gtk_calendar_select_day (calendar, priv->selected_day);
1035
1036   calendar_queue_refresh (calendar);
1037 }
1038
1039 static void
1040 calendar_compute_days (GtkCalendar *calendar)
1041 {
1042   GtkCalendarPrivate *priv = calendar->priv;
1043   gint month;
1044   gint year;
1045   gint ndays_in_month;
1046   gint ndays_in_prev_month;
1047   gint first_day;
1048   gint row;
1049   gint col;
1050   gint day;
1051
1052   year = priv->year;
1053   month = priv->month + 1;
1054
1055   ndays_in_month = month_length[leap (year)][month];
1056
1057   first_day = day_of_week (year, month, 1);
1058   first_day = (first_day + 7 - priv->week_start) % 7;
1059
1060   /* Compute days of previous month */
1061   if (month > 1)
1062     ndays_in_prev_month = month_length[leap (year)][month-1];
1063   else
1064     ndays_in_prev_month = month_length[leap (year)][12];
1065   day = ndays_in_prev_month - first_day + 1;
1066
1067   row = 0;
1068   if (first_day > 0)
1069     {
1070       for (col = 0; col < first_day; col++)
1071         {
1072           priv->day[row][col] = day;
1073           priv->day_month[row][col] = MONTH_PREV;
1074           day++;
1075         }
1076     }
1077
1078   /* Compute days of current month */
1079   col = first_day;
1080   for (day = 1; day <= ndays_in_month; day++)
1081     {
1082       priv->day[row][col] = day;
1083       priv->day_month[row][col] = MONTH_CURRENT;
1084
1085       col++;
1086       if (col == 7)
1087         {
1088           row++;
1089           col = 0;
1090         }
1091     }
1092
1093   /* Compute days of next month */
1094   day = 1;
1095   for (; row <= 5; row++)
1096     {
1097       for (; col <= 6; col++)
1098         {
1099           priv->day[row][col] = day;
1100           priv->day_month[row][col] = MONTH_NEXT;
1101           day++;
1102         }
1103       col = 0;
1104     }
1105 }
1106
1107 static void
1108 calendar_select_and_focus_day (GtkCalendar *calendar,
1109                                guint        day)
1110 {
1111   GtkCalendarPrivate *priv = calendar->priv;
1112   gint old_focus_row = priv->focus_row;
1113   gint old_focus_col = priv->focus_col;
1114   gint row;
1115   gint col;
1116
1117   for (row = 0; row < 6; row ++)
1118     for (col = 0; col < 7; col++)
1119       {
1120         if (priv->day_month[row][col] == MONTH_CURRENT
1121             && priv->day[row][col] == day)
1122           {
1123             priv->focus_row = row;
1124             priv->focus_col = col;
1125           }
1126       }
1127
1128   if (old_focus_row != -1 && old_focus_col != -1)
1129     calendar_invalidate_day (calendar, old_focus_row, old_focus_col);
1130
1131   gtk_calendar_select_day (calendar, day);
1132 }
1133
1134 \f
1135 /****************************************
1136  *     Layout computation utilities     *
1137  ****************************************/
1138
1139 static gint
1140 calendar_row_height (GtkCalendar *calendar)
1141 {
1142   GtkCalendarPrivate *priv = calendar->priv;
1143
1144   return (GTK_CALENDAR_GET_PRIVATE (calendar)->main_h - CALENDAR_MARGIN
1145           - ((priv->display_flags & GTK_CALENDAR_SHOW_DAY_NAMES)
1146              ? calendar_get_ysep (calendar) : CALENDAR_MARGIN)) / 6;
1147 }
1148
1149
1150 /* calendar_left_x_for_column: returns the x coordinate
1151  * for the left of the column */
1152 static gint
1153 calendar_left_x_for_column (GtkCalendar *calendar,
1154                             gint         column)
1155 {
1156   GtkCalendarPrivate *priv = calendar->priv;
1157   gint width;
1158   gint x_left;
1159   gint week_width;
1160   gint calendar_xsep = calendar_get_xsep (calendar);
1161   GtkStyleContext *context;
1162   GtkStateFlags state;
1163   gint inner_border = calendar_get_inner_border (calendar);
1164   GtkBorder padding;
1165
1166   context = gtk_widget_get_style_context (GTK_WIDGET (calendar));
1167   state = gtk_widget_get_state_flags (GTK_WIDGET (calendar));
1168   gtk_style_context_get_padding (context, state, &padding);
1169
1170   week_width = priv->week_width + padding.left + inner_border;
1171
1172   if (gtk_widget_get_direction (GTK_WIDGET (calendar)) == GTK_TEXT_DIR_RTL)
1173     {
1174       column = 6 - column;
1175       week_width = 0;
1176     }
1177
1178   width = GTK_CALENDAR_GET_PRIVATE (calendar)->day_width;
1179   if (priv->display_flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
1180     x_left = week_width + calendar_xsep + (width + DAY_XSEP) * column;
1181   else
1182     x_left = week_width + CALENDAR_MARGIN + (width + DAY_XSEP) * column;
1183
1184   return x_left;
1185 }
1186
1187 /* column_from_x: returns the column 0-6 that the
1188  * x pixel of the xwindow is in */
1189 static gint
1190 calendar_column_from_x (GtkCalendar *calendar,
1191                         gint         event_x)
1192 {
1193   gint c, column;
1194   gint x_left, x_right;
1195
1196   column = -1;
1197
1198   for (c = 0; c < 7; c++)
1199     {
1200       x_left = calendar_left_x_for_column (calendar, c);
1201       x_right = x_left + GTK_CALENDAR_GET_PRIVATE (calendar)->day_width;
1202
1203       if (event_x >= x_left && event_x < x_right)
1204         {
1205           column = c;
1206           break;
1207         }
1208     }
1209
1210   return column;
1211 }
1212
1213 /* calendar_top_y_for_row: returns the y coordinate
1214  * for the top of the row */
1215 static gint
1216 calendar_top_y_for_row (GtkCalendar *calendar,
1217                         gint         row)
1218 {
1219   GtkStyleContext *context;
1220   GtkAllocation allocation;
1221   gint inner_border = calendar_get_inner_border (calendar);
1222   GtkStateFlags state;
1223   GtkBorder padding;
1224
1225   gtk_widget_get_allocation (GTK_WIDGET (calendar), &allocation);
1226   context = gtk_widget_get_style_context (GTK_WIDGET (calendar));
1227   state = gtk_widget_get_state_flags (GTK_WIDGET (calendar));
1228
1229   gtk_style_context_get_padding (context, state, &padding);
1230
1231   return  allocation.height
1232           - padding.top - inner_border
1233           - (CALENDAR_MARGIN + (6 - row)
1234              * calendar_row_height (calendar));
1235 }
1236
1237 /* row_from_y: returns the row 0-5 that the
1238  * y pixel of the xwindow is in */
1239 static gint
1240 calendar_row_from_y (GtkCalendar *calendar,
1241                      gint         event_y)
1242 {
1243   gint r, row;
1244   gint height;
1245   gint y_top, y_bottom;
1246
1247   height = calendar_row_height (calendar);
1248   row = -1;
1249
1250   for (r = 0; r < 6; r++)
1251     {
1252       y_top = calendar_top_y_for_row (calendar, r);
1253       y_bottom = y_top + height;
1254
1255       if (event_y >= y_top && event_y < y_bottom)
1256         {
1257           row = r;
1258           break;
1259         }
1260     }
1261
1262   return row;
1263 }
1264
1265 static void
1266 calendar_arrow_rectangle (GtkCalendar  *calendar,
1267                           guint         arrow,
1268                           GdkRectangle *rect)
1269 {
1270   GtkWidget *widget = GTK_WIDGET (calendar);
1271   GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1272   GtkAllocation allocation;
1273   GtkStyleContext *context;
1274   GtkStateFlags state;
1275   GtkBorder padding;
1276   gboolean year_left;
1277
1278   gtk_widget_get_allocation (widget, &allocation);
1279   context = gtk_widget_get_style_context (widget);
1280   state = gtk_widget_get_state_flags (widget);
1281
1282   gtk_style_context_get_padding (context, state, &padding);
1283
1284   if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
1285     year_left = priv->year_before;
1286   else
1287     year_left = !priv->year_before;
1288
1289   rect->y = 3;
1290   rect->width = priv->arrow_width;
1291   rect->height = priv->header_h - 7;
1292
1293   switch (arrow)
1294     {
1295     case ARROW_MONTH_LEFT:
1296       if (year_left)
1297         rect->x = (allocation.width - padding.left - padding.right
1298                    - (3 + 2 * priv->arrow_width + priv->max_month_width));
1299       else
1300         rect->x = 3;
1301       break;
1302     case ARROW_MONTH_RIGHT:
1303       if (year_left)
1304         rect->x = (allocation.width - padding.left - padding.right
1305                    - 3 - priv->arrow_width);
1306       else
1307         rect->x = (priv->arrow_width + priv->max_month_width);
1308       break;
1309     case ARROW_YEAR_LEFT:
1310       if (year_left)
1311         rect->x = 3;
1312       else
1313         rect->x = (allocation.width - padding.left - padding.right
1314                    - (3 + 2 * priv->arrow_width + priv->max_year_width));
1315       break;
1316     case ARROW_YEAR_RIGHT:
1317       if (year_left)
1318         rect->x = (priv->arrow_width + priv->max_year_width);
1319       else
1320         rect->x = (allocation.width - padding.left - padding.right
1321                    - 3 - priv->arrow_width);
1322       break;
1323     }
1324
1325   rect->x += padding.left;
1326   rect->y += padding.top;
1327 }
1328
1329 static void
1330 calendar_day_rectangle (GtkCalendar  *calendar,
1331                         gint          row,
1332                         gint          col,
1333                         GdkRectangle *rect)
1334 {
1335   GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1336
1337   rect->x = calendar_left_x_for_column (calendar, col);
1338   rect->y = calendar_top_y_for_row (calendar, row);
1339   rect->height = calendar_row_height (calendar);
1340   rect->width = priv->day_width;
1341 }
1342
1343 static void
1344 calendar_set_month_prev (GtkCalendar *calendar)
1345 {
1346   GtkCalendarPrivate *priv = calendar->priv;
1347   gint month_len;
1348
1349   if (priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
1350     return;
1351
1352   if (priv->month == 0)
1353     {
1354       priv->month = 11;
1355       priv->year--;
1356     }
1357   else
1358     priv->month--;
1359
1360   month_len = month_length[leap (priv->year)][priv->month + 1];
1361
1362   calendar_compute_days (calendar);
1363
1364   g_signal_emit (calendar,
1365                  gtk_calendar_signals[PREV_MONTH_SIGNAL],
1366                  0);
1367   g_signal_emit (calendar,
1368                  gtk_calendar_signals[MONTH_CHANGED_SIGNAL],
1369                  0);
1370
1371   if (month_len < priv->selected_day)
1372     {
1373       priv->selected_day = 0;
1374       gtk_calendar_select_day (calendar, month_len);
1375     }
1376   else
1377     {
1378       if (priv->selected_day < 0)
1379         priv->selected_day = priv->selected_day + 1 + month_length[leap (priv->year)][priv->month + 1];
1380       gtk_calendar_select_day (calendar, priv->selected_day);
1381     }
1382
1383   calendar_queue_refresh (calendar);
1384 }
1385
1386 \f
1387 /****************************************
1388  *           Basic object methods       *
1389  ****************************************/
1390
1391 static void
1392 gtk_calendar_finalize (GObject *object)
1393 {
1394   G_OBJECT_CLASS (gtk_calendar_parent_class)->finalize (object);
1395 }
1396
1397 static void
1398 gtk_calendar_destroy (GtkWidget *widget)
1399 {
1400   GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
1401
1402   calendar_stop_spinning (GTK_CALENDAR (widget));
1403
1404   /* Call the destroy function for the extra display callback: */
1405   if (priv->detail_func_destroy && priv->detail_func_user_data)
1406     {
1407       priv->detail_func_destroy (priv->detail_func_user_data);
1408       priv->detail_func_user_data = NULL;
1409       priv->detail_func_destroy = NULL;
1410     }
1411
1412   GTK_WIDGET_CLASS (gtk_calendar_parent_class)->destroy (widget);
1413 }
1414
1415
1416 static void
1417 calendar_set_display_option (GtkCalendar              *calendar,
1418                              GtkCalendarDisplayOptions flag,
1419                              gboolean                  setting)
1420 {
1421   GtkCalendarPrivate *priv = calendar->priv;
1422   GtkCalendarDisplayOptions flags;
1423
1424   if (setting)
1425     flags = priv->display_flags | flag;
1426   else
1427     flags = priv->display_flags & ~flag;
1428   gtk_calendar_set_display_options (calendar, flags);
1429 }
1430
1431 static gboolean
1432 calendar_get_display_option (GtkCalendar              *calendar,
1433                              GtkCalendarDisplayOptions flag)
1434 {
1435   GtkCalendarPrivate *priv = calendar->priv;
1436
1437   return (priv->display_flags & flag) != 0;
1438 }
1439
1440 static void
1441 gtk_calendar_set_property (GObject      *object,
1442                            guint         prop_id,
1443                            const GValue *value,
1444                            GParamSpec   *pspec)
1445 {
1446   GtkCalendar *calendar = GTK_CALENDAR (object);
1447   GtkCalendarPrivate *priv = calendar->priv;
1448
1449   switch (prop_id)
1450     {
1451     case PROP_YEAR:
1452       gtk_calendar_select_month (calendar,
1453                                  priv->month,
1454                                  g_value_get_int (value));
1455       break;
1456     case PROP_MONTH:
1457       gtk_calendar_select_month (calendar,
1458                                  g_value_get_int (value),
1459                                  priv->year);
1460       break;
1461     case PROP_DAY:
1462       gtk_calendar_select_day (calendar,
1463                                g_value_get_int (value));
1464       break;
1465     case PROP_SHOW_HEADING:
1466       calendar_set_display_option (calendar,
1467                                    GTK_CALENDAR_SHOW_HEADING,
1468                                    g_value_get_boolean (value));
1469       break;
1470     case PROP_SHOW_DAY_NAMES:
1471       calendar_set_display_option (calendar,
1472                                    GTK_CALENDAR_SHOW_DAY_NAMES,
1473                                    g_value_get_boolean (value));
1474       break;
1475     case PROP_NO_MONTH_CHANGE:
1476       calendar_set_display_option (calendar,
1477                                    GTK_CALENDAR_NO_MONTH_CHANGE,
1478                                    g_value_get_boolean (value));
1479       break;
1480     case PROP_SHOW_WEEK_NUMBERS:
1481       calendar_set_display_option (calendar,
1482                                    GTK_CALENDAR_SHOW_WEEK_NUMBERS,
1483                                    g_value_get_boolean (value));
1484       break;
1485     case PROP_SHOW_DETAILS:
1486       calendar_set_display_option (calendar,
1487                                    GTK_CALENDAR_SHOW_DETAILS,
1488                                    g_value_get_boolean (value));
1489       break;
1490     case PROP_DETAIL_WIDTH_CHARS:
1491       gtk_calendar_set_detail_width_chars (calendar,
1492                                            g_value_get_int (value));
1493       break;
1494     case PROP_DETAIL_HEIGHT_ROWS:
1495       gtk_calendar_set_detail_height_rows (calendar,
1496                                            g_value_get_int (value));
1497       break;
1498     default:
1499       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1500       break;
1501     }
1502 }
1503
1504 static void
1505 gtk_calendar_get_property (GObject      *object,
1506                            guint         prop_id,
1507                            GValue       *value,
1508                            GParamSpec   *pspec)
1509 {
1510   GtkCalendar *calendar = GTK_CALENDAR (object);
1511   GtkCalendarPrivate *priv = calendar->priv;
1512
1513   switch (prop_id)
1514     {
1515     case PROP_YEAR:
1516       g_value_set_int (value, priv->year);
1517       break;
1518     case PROP_MONTH:
1519       g_value_set_int (value, priv->month);
1520       break;
1521     case PROP_DAY:
1522       g_value_set_int (value, priv->selected_day);
1523       break;
1524     case PROP_SHOW_HEADING:
1525       g_value_set_boolean (value, calendar_get_display_option (calendar,
1526                                                                GTK_CALENDAR_SHOW_HEADING));
1527       break;
1528     case PROP_SHOW_DAY_NAMES:
1529       g_value_set_boolean (value, calendar_get_display_option (calendar,
1530                                                                GTK_CALENDAR_SHOW_DAY_NAMES));
1531       break;
1532     case PROP_NO_MONTH_CHANGE:
1533       g_value_set_boolean (value, calendar_get_display_option (calendar,
1534                                                                GTK_CALENDAR_NO_MONTH_CHANGE));
1535       break;
1536     case PROP_SHOW_WEEK_NUMBERS:
1537       g_value_set_boolean (value, calendar_get_display_option (calendar,
1538                                                                GTK_CALENDAR_SHOW_WEEK_NUMBERS));
1539       break;
1540     case PROP_SHOW_DETAILS:
1541       g_value_set_boolean (value, calendar_get_display_option (calendar,
1542                                                                GTK_CALENDAR_SHOW_DETAILS));
1543       break;
1544     case PROP_DETAIL_WIDTH_CHARS:
1545       g_value_set_int (value, priv->detail_width_chars);
1546       break;
1547     case PROP_DETAIL_HEIGHT_ROWS:
1548       g_value_set_int (value, priv->detail_height_rows);
1549       break;
1550     default:
1551       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1552       break;
1553     }
1554 }
1555
1556 \f
1557 /****************************************
1558  *             Realization              *
1559  ****************************************/
1560
1561 static void
1562 calendar_realize_arrows (GtkCalendar *calendar)
1563 {
1564   GtkWidget *widget = GTK_WIDGET (calendar);
1565   GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1566   GdkWindowAttr attributes;
1567   gint attributes_mask;
1568   gint i;
1569   GtkAllocation allocation;
1570
1571   if (! (priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
1572       && (priv->display_flags & GTK_CALENDAR_SHOW_HEADING))
1573     {
1574       gtk_widget_get_allocation (widget, &allocation);
1575
1576       attributes.wclass = GDK_INPUT_ONLY;
1577       attributes.window_type = GDK_WINDOW_CHILD;
1578       attributes.event_mask = (gtk_widget_get_events (widget)
1579                                | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
1580                                | GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK);
1581       attributes_mask = GDK_WA_X | GDK_WA_Y;
1582       for (i = 0; i < 4; i++)
1583         {
1584           GdkRectangle rect;
1585           calendar_arrow_rectangle (calendar, i, &rect);
1586
1587           attributes.x = allocation.x + rect.x;
1588           attributes.y = allocation.y + rect.y;
1589           attributes.width = rect.width;
1590           attributes.height = rect.height;
1591           priv->arrow_win[i] = gdk_window_new (gtk_widget_get_window (widget),
1592                                                &attributes,
1593                                                attributes_mask);
1594
1595           if (!gtk_widget_is_sensitive (widget))
1596             priv->arrow_state[i] = GTK_STATE_FLAG_INSENSITIVE;
1597
1598           gdk_window_set_user_data (priv->arrow_win[i], widget);
1599         }
1600     }
1601   else
1602     {
1603       for (i = 0; i < 4; i++)
1604         priv->arrow_win[i] = NULL;
1605     }
1606 }
1607
1608 static void
1609 calendar_unrealize_arrows (GtkCalendar *calendar)
1610 {
1611   GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1612   gint i;
1613
1614   for (i = 0; i < 4; i++)
1615     {
1616       if (priv->arrow_win[i])
1617         {
1618           gdk_window_set_user_data (priv->arrow_win[i], NULL);
1619           gdk_window_destroy (priv->arrow_win[i]);
1620           priv->arrow_win[i] = NULL;
1621         }
1622     }
1623
1624 }
1625
1626 static gint
1627 calendar_get_inner_border (GtkCalendar *calendar)
1628 {
1629   gint inner_border;
1630
1631   gtk_widget_style_get (GTK_WIDGET (calendar),
1632                         "inner-border", &inner_border,
1633                         NULL);
1634
1635   return inner_border;
1636 }
1637
1638 static gint
1639 calendar_get_xsep (GtkCalendar *calendar)
1640 {
1641   gint xsep;
1642
1643   gtk_widget_style_get (GTK_WIDGET (calendar),
1644                         "horizontal-separation", &xsep,
1645                         NULL);
1646
1647   return xsep;
1648 }
1649
1650 static gint
1651 calendar_get_ysep (GtkCalendar *calendar)
1652 {
1653   gint ysep;
1654
1655   gtk_widget_style_get (GTK_WIDGET (calendar),
1656                         "vertical-separation", &ysep,
1657                         NULL);
1658
1659   return ysep;
1660 }
1661
1662 static void
1663 gtk_calendar_realize (GtkWidget *widget)
1664 {
1665   GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
1666   GdkWindowAttr attributes;
1667   gint attributes_mask;
1668   gint inner_border = calendar_get_inner_border (GTK_CALENDAR (widget));
1669   GtkStyleContext *context;
1670   GtkAllocation allocation;
1671   GtkStateFlags state = 0;
1672   GtkBorder padding;
1673
1674   context = gtk_widget_get_style_context (widget);
1675   state = gtk_widget_get_state_flags (widget);
1676   gtk_style_context_get_padding (context, state, &padding);
1677
1678   gtk_widget_get_allocation (widget, &allocation);
1679
1680   GTK_WIDGET_CLASS (gtk_calendar_parent_class)->realize (widget);
1681
1682   attributes.wclass = GDK_INPUT_ONLY;
1683   attributes.window_type = GDK_WINDOW_CHILD;
1684   attributes.event_mask = (gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK
1685                            | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
1686                            | GDK_POINTER_MOTION_MASK | GDK_LEAVE_NOTIFY_MASK);
1687
1688   if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
1689     attributes.x = priv->week_width + padding.left + inner_border;
1690   else
1691     attributes.x = padding.left + inner_border;
1692
1693   attributes.y = priv->header_h + priv->day_name_h + padding.top + inner_border;
1694   attributes.width = allocation.width - attributes.x - (padding.right + inner_border);
1695
1696   if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1697     attributes.width -= priv->week_width;
1698
1699   attributes.height = priv->main_h;
1700   attributes_mask = GDK_WA_X | GDK_WA_Y;
1701
1702   attributes.x += allocation.x;
1703   attributes.y += allocation.y;
1704
1705   priv->main_win = gdk_window_new (gtk_widget_get_window (widget),
1706                                    &attributes, attributes_mask);
1707   gdk_window_set_user_data (priv->main_win, widget);
1708
1709   calendar_realize_arrows (GTK_CALENDAR (widget));
1710 }
1711
1712 static void
1713 gtk_calendar_unrealize (GtkWidget *widget)
1714 {
1715   GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
1716
1717   calendar_unrealize_arrows (GTK_CALENDAR (widget));
1718
1719   if (priv->main_win)
1720     {
1721       gdk_window_set_user_data (priv->main_win, NULL);
1722       gdk_window_destroy (priv->main_win);
1723       priv->main_win = NULL;
1724     }
1725
1726   GTK_WIDGET_CLASS (gtk_calendar_parent_class)->unrealize (widget);
1727 }
1728
1729 static void
1730 calendar_map_arrows (GtkCalendar *calendar)
1731 {
1732   GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1733   gint i;
1734
1735   for (i = 0; i < 4; i++)
1736     {
1737       if (priv->arrow_win[i])
1738         gdk_window_show (priv->arrow_win[i]);
1739     }
1740 }
1741
1742 static void
1743 calendar_unmap_arrows (GtkCalendar *calendar)
1744 {
1745   GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1746   gint i;
1747
1748   for (i = 0; i < 4; i++)
1749     {
1750       if (priv->arrow_win[i])
1751         gdk_window_hide (priv->arrow_win[i]);
1752     }
1753 }
1754
1755 static void
1756 gtk_calendar_map (GtkWidget *widget)
1757 {
1758   GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
1759
1760   GTK_WIDGET_CLASS (gtk_calendar_parent_class)->map (widget);
1761
1762   gdk_window_show (priv->main_win);
1763
1764   calendar_map_arrows (GTK_CALENDAR (widget));
1765 }
1766
1767 static void
1768 gtk_calendar_unmap (GtkWidget *widget)
1769 {
1770   GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
1771
1772   calendar_unmap_arrows (GTK_CALENDAR (widget));
1773
1774   gdk_window_hide (priv->main_win);
1775
1776   GTK_WIDGET_CLASS (gtk_calendar_parent_class)->unmap (widget);
1777 }
1778
1779 static gchar*
1780 gtk_calendar_get_detail (GtkCalendar *calendar,
1781                          gint         row,
1782                          gint         column)
1783 {
1784   GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
1785   gint year, month;
1786
1787   if (priv->detail_func == NULL)
1788     return NULL;
1789
1790   year = priv->year;
1791   month = priv->month + priv->day_month[row][column] - MONTH_CURRENT;
1792
1793   if (month < 0)
1794     {
1795       month += 12;
1796       year -= 1;
1797     }
1798   else if (month > 11)
1799     {
1800       month -= 12;
1801       year += 1;
1802     }
1803
1804   return priv->detail_func (calendar,
1805                             year, month,
1806                             priv->day[row][column],
1807                             priv->detail_func_user_data);
1808 }
1809
1810 static gboolean
1811 gtk_calendar_query_tooltip (GtkWidget  *widget,
1812                             gint        x,
1813                             gint        y,
1814                             gboolean    keyboard_mode,
1815                             GtkTooltip *tooltip)
1816 {
1817   GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
1818   GtkCalendar *calendar = GTK_CALENDAR (widget);
1819   gchar *detail = NULL;
1820   GdkRectangle day_rect;
1821   gint row, col;
1822
1823   col = calendar_column_from_x (calendar, x);
1824   row = calendar_row_from_y (calendar, y);
1825
1826   if (col != -1 && row != -1 &&
1827       (0 != (priv->detail_overflow[row] & (1 << col)) ||
1828       0 == (priv->display_flags & GTK_CALENDAR_SHOW_DETAILS)))
1829     {
1830       detail = gtk_calendar_get_detail (calendar, row, col);
1831       calendar_day_rectangle (calendar, row, col, &day_rect);
1832     }
1833
1834   if (detail)
1835     {
1836       gtk_tooltip_set_tip_area (tooltip, &day_rect);
1837       gtk_tooltip_set_markup (tooltip, detail);
1838
1839       g_free (detail);
1840
1841       return TRUE;
1842     }
1843
1844   if (GTK_WIDGET_CLASS (gtk_calendar_parent_class)->query_tooltip)
1845     return GTK_WIDGET_CLASS (gtk_calendar_parent_class)->query_tooltip (widget, x, y, keyboard_mode, tooltip);
1846
1847   return FALSE;
1848 }
1849
1850 \f
1851 /****************************************
1852  *       Size Request and Allocate      *
1853  ****************************************/
1854
1855 static void
1856 gtk_calendar_size_request (GtkWidget      *widget,
1857                            GtkRequisition *requisition)
1858 {
1859   GtkCalendar *calendar = GTK_CALENDAR (widget);
1860   GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
1861   GtkStyleContext *context;
1862   GtkStateFlags state;
1863   GtkBorder padding;
1864   PangoLayout *layout;
1865   PangoRectangle logical_rect;
1866
1867   gint height;
1868   gint i, r, c;
1869   gint calendar_margin = CALENDAR_MARGIN;
1870   gint header_width, main_width;
1871   gint max_header_height = 0;
1872   gint focus_width;
1873   gint focus_padding;
1874   gint max_detail_height;
1875   gint inner_border = calendar_get_inner_border (calendar);
1876   gint calendar_ysep = calendar_get_ysep (calendar);
1877   gint calendar_xsep = calendar_get_xsep (calendar);
1878
1879   gtk_widget_style_get (GTK_WIDGET (widget),
1880                         "focus-line-width", &focus_width,
1881                         "focus-padding", &focus_padding,
1882                         NULL);
1883
1884   layout = gtk_widget_create_pango_layout (widget, NULL);
1885
1886   /*
1887    * Calculate the requisition  width for the widget.
1888    */
1889
1890   /* Header width */
1891
1892   if (priv->display_flags & GTK_CALENDAR_SHOW_HEADING)
1893     {
1894       priv->max_month_width = 0;
1895       for (i = 0; i < 12; i++)
1896         {
1897           pango_layout_set_text (layout, default_monthname[i], -1);
1898           pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1899           priv->max_month_width = MAX (priv->max_month_width,
1900                                                logical_rect.width + 8);
1901           max_header_height = MAX (max_header_height, logical_rect.height);
1902         }
1903
1904       priv->max_year_width = 0;
1905       /* Translators:  This is a text measurement template.
1906        * Translate it to the widest year text
1907        *
1908        * If you don't understand this, leave it as "2000"
1909        */
1910       pango_layout_set_text (layout, C_("year measurement template", "2000"), -1);
1911       pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1912       priv->max_year_width = MAX (priv->max_year_width,
1913                                   logical_rect.width + 8);
1914       max_header_height = MAX (max_header_height, logical_rect.height);
1915     }
1916   else
1917     {
1918       priv->max_month_width = 0;
1919       priv->max_year_width = 0;
1920     }
1921
1922   if (priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
1923     header_width = (priv->max_month_width
1924                     + priv->max_year_width
1925                     + 3 * 3);
1926   else
1927     header_width = (priv->max_month_width
1928                     + priv->max_year_width
1929                     + 4 * priv->arrow_width + 3 * 3);
1930
1931   /* Mainwindow labels width */
1932
1933   priv->max_day_char_width = 0;
1934   priv->max_day_char_ascent = 0;
1935   priv->max_day_char_descent = 0;
1936   priv->min_day_width = 0;
1937
1938   for (i = 0; i < 9; i++)
1939     {
1940       gchar buffer[32];
1941       g_snprintf (buffer, sizeof (buffer), C_("calendar:day:digits", "%d"), i * 11);
1942       pango_layout_set_text (layout, buffer, -1);
1943       pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1944       priv->min_day_width = MAX (priv->min_day_width,
1945                                          logical_rect.width);
1946
1947       priv->max_day_char_ascent = MAX (priv->max_day_char_ascent,
1948                                                PANGO_ASCENT (logical_rect));
1949       priv->max_day_char_descent = MAX (priv->max_day_char_descent,
1950                                                 PANGO_DESCENT (logical_rect));
1951     }
1952
1953   priv->max_label_char_ascent = 0;
1954   priv->max_label_char_descent = 0;
1955   if (priv->display_flags & GTK_CALENDAR_SHOW_DAY_NAMES)
1956     for (i = 0; i < 7; i++)
1957       {
1958         pango_layout_set_text (layout, default_abbreviated_dayname[i], -1);
1959         pango_layout_line_get_pixel_extents (pango_layout_get_lines_readonly (layout)->data, NULL, &logical_rect);
1960
1961         priv->min_day_width = MAX (priv->min_day_width, logical_rect.width);
1962         priv->max_label_char_ascent = MAX (priv->max_label_char_ascent,
1963                                                    PANGO_ASCENT (logical_rect));
1964         priv->max_label_char_descent = MAX (priv->max_label_char_descent,
1965                                                     PANGO_DESCENT (logical_rect));
1966       }
1967
1968   priv->max_week_char_width = 0;
1969   if (priv->display_flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
1970     for (i = 0; i < 9; i++)
1971       {
1972         gchar buffer[32];
1973         g_snprintf (buffer, sizeof (buffer), C_("calendar:week:digits", "%d"), i * 11);
1974         pango_layout_set_text (layout, buffer, -1);
1975         pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1976         priv->max_week_char_width = MAX (priv->max_week_char_width,
1977                                            logical_rect.width / 2);
1978       }
1979
1980   /* Calculate detail extents. Do this as late as possible since
1981    * pango_layout_set_markup is called which alters font settings. */
1982   max_detail_height = 0;
1983
1984   if (priv->detail_func && (priv->display_flags & GTK_CALENDAR_SHOW_DETAILS))
1985     {
1986       gchar *markup, *tail;
1987
1988       if (priv->detail_width_chars || priv->detail_height_rows)
1989         {
1990           gint rows = MAX (1, priv->detail_height_rows) - 1;
1991           gsize len = priv->detail_width_chars + rows + 16;
1992
1993           markup = tail = g_alloca (len);
1994
1995           memcpy (tail,     "<small>", 7);
1996           tail += 7;
1997
1998           memset (tail, 'm', priv->detail_width_chars);
1999           tail += priv->detail_width_chars;
2000
2001           memset (tail, '\n', rows);
2002           tail += rows;
2003
2004           memcpy (tail,     "</small>", 9);
2005           tail += 9;
2006
2007           g_assert (len == (tail - markup));
2008
2009           pango_layout_set_markup (layout, markup, -1);
2010           pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2011
2012           if (priv->detail_width_chars)
2013             priv->min_day_width = MAX (priv->min_day_width, logical_rect.width);
2014           if (priv->detail_height_rows)
2015             max_detail_height = MAX (max_detail_height, logical_rect.height);
2016         }
2017
2018       if (!priv->detail_width_chars || !priv->detail_height_rows)
2019         for (r = 0; r < 6; r++)
2020           for (c = 0; c < 7; c++)
2021             {
2022               gchar *detail = gtk_calendar_get_detail (calendar, r, c);
2023
2024               if (detail)
2025                 {
2026                   markup = g_strconcat ("<small>", detail, "</small>", NULL);
2027                   pango_layout_set_markup (layout, markup, -1);
2028
2029                   if (priv->detail_width_chars)
2030                     {
2031                       pango_layout_set_wrap (layout, PANGO_WRAP_WORD_CHAR);
2032                       pango_layout_set_width (layout, PANGO_SCALE * priv->min_day_width);
2033                     }
2034
2035                   pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2036
2037                   if (!priv->detail_width_chars)
2038                     priv->min_day_width = MAX (priv->min_day_width, logical_rect.width);
2039                   if (!priv->detail_height_rows)
2040                     max_detail_height = MAX (max_detail_height, logical_rect.height);
2041
2042                   g_free (markup);
2043                   g_free (detail);
2044                 }
2045             }
2046     }
2047
2048   /* We add one to max_day_char_width to be able to make the marked day "bold" */
2049   priv->max_day_char_width = priv->min_day_width / 2 + 1;
2050
2051   main_width = (7 * (priv->min_day_width + (focus_padding + focus_width) * 2) + (DAY_XSEP * 6) + CALENDAR_MARGIN * 2
2052                 + (priv->max_week_char_width
2053                    ? priv->max_week_char_width * 2 + (focus_padding + focus_width) * 2 + calendar_xsep * 2
2054                    : 0));
2055
2056   context = gtk_widget_get_style_context (widget);
2057   state = gtk_widget_get_state_flags (widget);
2058   gtk_style_context_get_padding (context, state, &padding);
2059
2060   requisition->width = MAX (header_width, main_width + inner_border * 2) + padding.left + padding.right;
2061
2062   /*
2063    * Calculate the requisition height for the widget.
2064    */
2065
2066   if (priv->display_flags & GTK_CALENDAR_SHOW_HEADING)
2067     {
2068       priv->header_h = (max_header_height + calendar_ysep * 2);
2069     }
2070   else
2071     {
2072       priv->header_h = 0;
2073     }
2074
2075   if (priv->display_flags & GTK_CALENDAR_SHOW_DAY_NAMES)
2076     {
2077       priv->day_name_h = (priv->max_label_char_ascent
2078                                   + priv->max_label_char_descent
2079                                   + 2 * (focus_padding + focus_width) + calendar_margin);
2080       calendar_margin = calendar_ysep;
2081     }
2082   else
2083     {
2084       priv->day_name_h = 0;
2085     }
2086
2087   priv->main_h = (CALENDAR_MARGIN + calendar_margin
2088                           + 6 * (priv->max_day_char_ascent
2089                                  + priv->max_day_char_descent
2090                                  + max_detail_height
2091                                  + 2 * (focus_padding + focus_width))
2092                           + DAY_YSEP * 5);
2093
2094   height = priv->header_h + priv->day_name_h + priv->main_h;
2095
2096   requisition->height = height + padding.top + padding.bottom + (inner_border * 2);
2097
2098   g_object_unref (layout);
2099 }
2100
2101 static void
2102 gtk_calendar_get_preferred_width (GtkWidget *widget,
2103                                   gint      *minimum,
2104                                   gint      *natural)
2105 {
2106   GtkRequisition requisition;
2107
2108   gtk_calendar_size_request (widget, &requisition);
2109
2110   *minimum = *natural = requisition.width;
2111 }
2112
2113 static void
2114 gtk_calendar_get_preferred_height (GtkWidget *widget,
2115                                    gint      *minimum,
2116                                    gint      *natural)
2117 {
2118   GtkRequisition requisition;
2119
2120   gtk_calendar_size_request (widget, &requisition);
2121
2122   *minimum = *natural = requisition.height;
2123 }
2124
2125 static void
2126 gtk_calendar_size_allocate (GtkWidget     *widget,
2127                             GtkAllocation *allocation)
2128 {
2129   GtkCalendar *calendar = GTK_CALENDAR (widget);
2130   GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
2131   GtkStyleContext *context;
2132   GtkStateFlags state;
2133   GtkBorder padding;
2134   guint i;
2135   gint inner_border = calendar_get_inner_border (calendar);
2136   gint calendar_xsep = calendar_get_xsep (calendar);
2137
2138   context = gtk_widget_get_style_context (widget);
2139   state = gtk_widget_get_state_flags (widget);
2140   gtk_style_context_get_padding (context, state, &padding);
2141
2142   gtk_widget_set_allocation (widget, allocation);
2143
2144   if (priv->display_flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
2145     {
2146       priv->day_width = (priv->min_day_width
2147                          * ((allocation->width - (inner_border * 2) - padding.left - padding.right
2148                              - (CALENDAR_MARGIN * 2) -  (DAY_XSEP * 6) - calendar_xsep * 2))
2149                          / (7 * priv->min_day_width + priv->max_week_char_width * 2));
2150       priv->week_width = ((allocation->width - (inner_border * 2) - padding.left - padding.right
2151                            - (CALENDAR_MARGIN * 2) - (DAY_XSEP * 6) - calendar_xsep * 2 )
2152                           - priv->day_width * 7 + CALENDAR_MARGIN + calendar_xsep);
2153     }
2154   else
2155     {
2156       priv->day_width = (allocation->width
2157                          - (inner_border * 2)
2158                          - padding.left - padding.right
2159                          - (CALENDAR_MARGIN * 2)
2160                          - (DAY_XSEP * 6))/7;
2161       priv->week_width = 0;
2162     }
2163
2164   if (gtk_widget_get_realized (widget))
2165     {
2166       if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
2167         gdk_window_move_resize (priv->main_win,
2168                                 allocation->x
2169                                  + priv->week_width + padding.left + inner_border,
2170                                 allocation->y
2171                                  + priv->header_h + priv->day_name_h
2172                                  + (padding.top + inner_border),
2173                                 allocation->width - priv->week_width
2174                                 - (inner_border * 2) - padding.left - padding.right,
2175                                 priv->main_h);
2176       else
2177         gdk_window_move_resize (priv->main_win,
2178                                 allocation->x
2179                                  + padding.left + inner_border,
2180                                 allocation->y
2181                                  + priv->header_h + priv->day_name_h
2182                                  + padding.top + inner_border,
2183                                 allocation->width - priv->week_width
2184                                 - (inner_border * 2) - padding.left - padding.right,
2185                                 priv->main_h);
2186
2187       for (i = 0 ; i < 4 ; i++)
2188         {
2189           if (priv->arrow_win[i])
2190             {
2191               GdkRectangle rect;
2192               calendar_arrow_rectangle (calendar, i, &rect);
2193
2194               gdk_window_move_resize (priv->arrow_win[i],
2195                                       allocation->x + rect.x,
2196                                       allocation->y + rect.y,
2197                                       rect.width, rect.height);
2198             }
2199         }
2200     }
2201 }
2202
2203 \f
2204 /****************************************
2205  *              Repainting              *
2206  ****************************************/
2207
2208 static void
2209 calendar_paint_header (GtkCalendar *calendar, cairo_t *cr)
2210 {
2211   GtkWidget *widget = GTK_WIDGET (calendar);
2212   GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2213   GtkAllocation allocation;
2214   GtkStyleContext *context;
2215   GtkStateFlags state;
2216   GtkBorder padding;
2217   char buffer[255];
2218   gint x, y;
2219   gint header_width;
2220   gint max_month_width;
2221   gint max_year_width;
2222   PangoLayout *layout;
2223   PangoRectangle logical_rect;
2224   gboolean year_left;
2225   time_t tmp_time;
2226   struct tm *tm;
2227   gchar *str;
2228
2229   context = gtk_widget_get_style_context (widget);
2230   state = gtk_widget_get_state_flags (widget);
2231   gtk_style_context_get_padding (context, state, &padding);
2232
2233   cairo_save (cr);
2234   cairo_translate (cr, padding.left, padding.top);
2235
2236   if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
2237     year_left = priv->year_before;
2238   else
2239     year_left = !priv->year_before;
2240
2241   gtk_widget_get_allocation (widget, &allocation);
2242
2243   header_width = allocation.width - padding.left - padding.right;
2244
2245   max_month_width = priv->max_month_width;
2246   max_year_width = priv->max_year_width;
2247
2248   gtk_style_context_save (context);
2249   gtk_style_context_add_class (context, GTK_STYLE_CLASS_HEADER);
2250
2251   gtk_render_background (context, cr, 0, 0, header_width, priv->header_h);
2252   gtk_render_frame (context, cr, 0, 0, header_width, priv->header_h);
2253
2254   tmp_time = 1;  /* Jan 1 1970, 00:00:01 UTC */
2255   tm = gmtime (&tmp_time);
2256   tm->tm_year = priv->year - 1900;
2257
2258   /* Translators: This dictates how the year is displayed in
2259    * gtkcalendar widget.  See strftime() manual for the format.
2260    * Use only ASCII in the translation.
2261    *
2262    * Also look for the msgid "2000".
2263    * Translate that entry to a year with the widest output of this
2264    * msgid.
2265    *
2266    * "%Y" is appropriate for most locales.
2267    */
2268   strftime (buffer, sizeof (buffer), C_("calendar year format", "%Y"), tm);
2269   str = g_locale_to_utf8 (buffer, -1, NULL, NULL, NULL);
2270   layout = gtk_widget_create_pango_layout (widget, str);
2271   g_free (str);
2272
2273   pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2274
2275   /* Draw title */
2276   y = (priv->header_h - logical_rect.height) / 2;
2277
2278   /* Draw year and its arrows */
2279
2280   if (priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
2281     if (year_left)
2282       x = 3 + (max_year_width - logical_rect.width)/2;
2283     else
2284       x = header_width - (3 + max_year_width
2285                           - (max_year_width - logical_rect.width)/2);
2286   else
2287     if (year_left)
2288       x = 3 + priv->arrow_width + (max_year_width - logical_rect.width)/2;
2289     else
2290       x = header_width - (3 + priv->arrow_width + max_year_width
2291                           - (max_year_width - logical_rect.width)/2);
2292
2293   gtk_render_layout (context, cr, x, y, layout);
2294
2295   /* Draw month */
2296   g_snprintf (buffer, sizeof (buffer), "%s", default_monthname[priv->month]);
2297   pango_layout_set_text (layout, buffer, -1);
2298   pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2299
2300   if (priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
2301     if (year_left)
2302       x = header_width - (3 + max_month_width
2303                           - (max_month_width - logical_rect.width)/2);
2304     else
2305     x = 3 + (max_month_width - logical_rect.width) / 2;
2306   else
2307     if (year_left)
2308       x = header_width - (3 + priv->arrow_width + max_month_width
2309                           - (max_month_width - logical_rect.width)/2);
2310     else
2311     x = 3 + priv->arrow_width + (max_month_width - logical_rect.width)/2;
2312
2313   gtk_render_layout (context, cr, x, y, layout);
2314   g_object_unref (layout);
2315
2316   gtk_style_context_restore (context);
2317   cairo_restore (cr);
2318 }
2319
2320 static void
2321 calendar_paint_day_names (GtkCalendar *calendar,
2322                           cairo_t     *cr)
2323 {
2324   GtkWidget *widget = GTK_WIDGET (calendar);
2325   GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2326   GtkStyleContext *context;
2327   GtkStateFlags state;
2328   GtkBorder padding;
2329   GtkAllocation allocation;
2330   char buffer[255];
2331   int day,i;
2332   int day_width, cal_width;
2333   int day_wid_sep;
2334   PangoLayout *layout;
2335   PangoRectangle logical_rect;
2336   gint focus_padding;
2337   gint focus_width;
2338   gint calendar_ysep = calendar_get_ysep (calendar);
2339   gint calendar_xsep = calendar_get_xsep (calendar);
2340   gint inner_border = calendar_get_inner_border (calendar);
2341
2342   context = gtk_widget_get_style_context (widget);
2343   state = gtk_widget_get_state_flags (widget);
2344   gtk_style_context_get_padding (context, state, &padding);
2345
2346   cairo_save (cr);
2347
2348   cairo_translate (cr,
2349                    padding.left + inner_border,
2350                    priv->header_h + padding.top + inner_border);
2351
2352   gtk_widget_style_get (GTK_WIDGET (widget),
2353                         "focus-line-width", &focus_width,
2354                         "focus-padding", &focus_padding,
2355                         NULL);
2356
2357   gtk_widget_get_allocation (widget, &allocation);
2358
2359   day_width = priv->day_width;
2360   cal_width = allocation.width - (inner_border * 2) - padding.left - padding.right;
2361   day_wid_sep = day_width + DAY_XSEP;
2362
2363   /*
2364    * Draw rectangles as inverted background for the labels.
2365    */
2366
2367   gtk_style_context_save (context);
2368   gtk_style_context_add_class (context, GTK_STYLE_CLASS_HIGHLIGHT);
2369
2370   gtk_render_background (context, cr,
2371                          CALENDAR_MARGIN, CALENDAR_MARGIN,
2372                          cal_width - CALENDAR_MARGIN * 2,
2373                          priv->day_name_h - CALENDAR_MARGIN);
2374
2375   if (priv->display_flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
2376     gtk_render_background (context, cr,
2377                            CALENDAR_MARGIN,
2378                            priv->day_name_h - calendar_ysep,
2379                            priv->week_width - calendar_ysep - CALENDAR_MARGIN,
2380                            calendar_ysep);
2381
2382   /*
2383    * Write the labels
2384    */
2385   layout = gtk_widget_create_pango_layout (widget, NULL);
2386
2387   for (i = 0; i < 7; i++)
2388     {
2389       if (gtk_widget_get_direction (GTK_WIDGET (calendar)) == GTK_TEXT_DIR_RTL)
2390         day = 6 - i;
2391       else
2392         day = i;
2393       day = (day + priv->week_start) % 7;
2394       g_snprintf (buffer, sizeof (buffer), "%s", default_abbreviated_dayname[day]);
2395
2396       pango_layout_set_text (layout, buffer, -1);
2397       pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2398
2399       gtk_render_layout (context, cr,
2400                          (CALENDAR_MARGIN +
2401                           + (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR ?
2402                              (priv->week_width + (priv->week_width ? calendar_xsep : 0))
2403                              : 0)
2404                           + day_wid_sep * i
2405                           + (day_width - logical_rect.width)/2),
2406                          CALENDAR_MARGIN + focus_width + focus_padding + logical_rect.y,
2407                          layout);
2408     }
2409
2410   g_object_unref (layout);
2411
2412   gtk_style_context_restore (context);
2413   cairo_restore (cr);
2414 }
2415
2416 static void
2417 calendar_paint_week_numbers (GtkCalendar *calendar,
2418                              cairo_t     *cr)
2419 {
2420   GtkWidget *widget = GTK_WIDGET (calendar);
2421   GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2422   GtkStyleContext *context;
2423   GtkStateFlags state;
2424   GtkBorder padding;
2425   guint week = 0, year;
2426   gint row, x_loc, y_loc;
2427   gint day_height;
2428   char buffer[32];
2429   PangoLayout *layout;
2430   PangoRectangle logical_rect;
2431   gint focus_padding;
2432   gint focus_width;
2433   gint calendar_xsep = calendar_get_xsep (calendar);
2434   gint inner_border = calendar_get_inner_border (calendar);
2435   gint x, y;
2436
2437   context = gtk_widget_get_style_context (widget);
2438   state = gtk_widget_get_state_flags (widget);
2439   gtk_style_context_get_padding (context, state, &padding);
2440
2441   cairo_save (cr);
2442
2443   y = priv->header_h + priv->day_name_h + (padding.top + inner_border);
2444   if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
2445     x = padding.left + inner_border;
2446   else
2447     x = gtk_widget_get_allocated_width (widget) - priv->week_width - (padding.right + inner_border);
2448
2449   gtk_widget_style_get (GTK_WIDGET (widget),
2450                         "focus-line-width", &focus_width,
2451                         "focus-padding", &focus_padding,
2452                         NULL);
2453
2454   gtk_style_context_save (context);
2455   gtk_style_context_add_class (context, GTK_STYLE_CLASS_HIGHLIGHT);
2456
2457   if (priv->display_flags & GTK_CALENDAR_SHOW_DAY_NAMES)
2458     gtk_render_background (context, cr,
2459                            x + CALENDAR_MARGIN, y,
2460                            priv->week_width - CALENDAR_MARGIN,
2461                            priv->main_h - CALENDAR_MARGIN);
2462   else
2463     gtk_render_background (context, cr,
2464                            x + CALENDAR_MARGIN,
2465                            y + CALENDAR_MARGIN,
2466                            priv->week_width - CALENDAR_MARGIN,
2467                            priv->main_h - 2 * CALENDAR_MARGIN);
2468
2469   /*
2470    * Write the labels
2471    */
2472
2473   layout = gtk_widget_create_pango_layout (widget, NULL);
2474   day_height = calendar_row_height (calendar);
2475
2476   for (row = 0; row < 6; row++)
2477     {
2478       gboolean result;
2479
2480       year = priv->year;
2481       if (priv->day[row][6] < 15 && row > 3 && priv->month == 11)
2482         year++;
2483
2484       result = week_of_year (&week, &year,
2485                              ((priv->day[row][6] < 15 && row > 3 ? 1 : 0)
2486                               + priv->month) % 12 + 1, priv->day[row][6]);
2487       g_return_if_fail (result);
2488
2489       /* Translators: this defines whether the week numbers should use
2490        * localized digits or the ones used in English (0123...).
2491        *
2492        * Translate to "%Id" if you want to use localized digits, or
2493        * translate to "%d" otherwise.
2494        *
2495        * Note that translating this doesn't guarantee that you get localized
2496        * digits. That needs support from your system and locale definition
2497        * too.
2498        */
2499       g_snprintf (buffer, sizeof (buffer), C_("calendar:week:digits", "%d"), week);
2500       pango_layout_set_text (layout, buffer, -1);
2501       pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2502
2503       y_loc = calendar_top_y_for_row (calendar, row) + (day_height - logical_rect.height) / 2;
2504
2505       x_loc = x + (priv->week_width
2506                    - logical_rect.width
2507                    - calendar_xsep - focus_padding - focus_width);
2508
2509       gtk_render_layout (context, cr, x_loc, y_loc, layout);
2510     }
2511
2512   g_object_unref (layout);
2513
2514   gtk_style_context_restore (context);
2515   cairo_restore (cr);
2516 }
2517
2518 static void
2519 calendar_invalidate_day_num (GtkCalendar *calendar,
2520                              gint         day)
2521 {
2522   GtkCalendarPrivate *priv = calendar->priv;
2523   gint r, c, row, col;
2524
2525   row = -1;
2526   col = -1;
2527   for (r = 0; r < 6; r++)
2528     for (c = 0; c < 7; c++)
2529       if (priv->day_month[r][c] == MONTH_CURRENT &&
2530           priv->day[r][c] == day)
2531         {
2532           row = r;
2533           col = c;
2534         }
2535
2536   g_return_if_fail (row != -1);
2537   g_return_if_fail (col != -1);
2538
2539   calendar_invalidate_day (calendar, row, col);
2540 }
2541
2542 static void
2543 calendar_invalidate_day (GtkCalendar *calendar,
2544                          gint         row,
2545                          gint         col)
2546 {
2547   GdkRectangle day_rect;
2548   GtkAllocation allocation;
2549
2550   gtk_widget_get_allocation (GTK_WIDGET (calendar), &allocation);
2551   calendar_day_rectangle (calendar, row, col, &day_rect);
2552   gtk_widget_queue_draw_area (GTK_WIDGET (calendar),
2553                               allocation.x + day_rect.x,
2554                               allocation.y + day_rect.y,
2555                               day_rect.width, day_rect.height);
2556 }
2557
2558 static gboolean
2559 is_color_attribute (PangoAttribute *attribute,
2560                     gpointer        data)
2561 {
2562   return (attribute->klass->type == PANGO_ATTR_FOREGROUND ||
2563           attribute->klass->type == PANGO_ATTR_BACKGROUND);
2564 }
2565
2566 static void
2567 calendar_paint_day (GtkCalendar *calendar,
2568                     cairo_t     *cr,
2569                     gint         row,
2570                     gint         col)
2571 {
2572   GtkWidget *widget = GTK_WIDGET (calendar);
2573   GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2574   GtkStyleContext *context;
2575   GtkStateFlags state = 0;
2576   gchar *detail;
2577   gchar buffer[32];
2578   gint day;
2579   gint x_loc, y_loc;
2580   GdkRectangle day_rect;
2581
2582   PangoLayout *layout;
2583   PangoRectangle logical_rect;
2584   gboolean overflow = FALSE;
2585   gboolean show_details;
2586
2587   g_return_if_fail (row < 6);
2588   g_return_if_fail (col < 7);
2589
2590   context = gtk_widget_get_style_context (widget);
2591
2592   day = priv->day[row][col];
2593   show_details = (priv->display_flags & GTK_CALENDAR_SHOW_DETAILS);
2594
2595   calendar_day_rectangle (calendar, row, col, &day_rect);
2596
2597   gtk_style_context_save (context);
2598
2599   if (!gtk_widget_get_sensitive (widget))
2600     state |= GTK_STATE_FLAG_INSENSITIVE;
2601   else
2602     {
2603       if (gtk_widget_has_focus (widget))
2604         state |= GTK_STATE_FLAG_FOCUSED;
2605
2606       if (priv->day_month[row][col] == MONTH_PREV ||
2607           priv->day_month[row][col] == MONTH_NEXT)
2608         state |= GTK_STATE_FLAG_INCONSISTENT;
2609       else
2610         {
2611           if (priv->marked_date[day-1])
2612             state |= GTK_STATE_FLAG_ACTIVE;
2613
2614           if (priv->selected_day == day)
2615             {
2616               state |= GTK_STATE_FLAG_SELECTED;
2617
2618               gtk_style_context_set_state (context, state);
2619               gtk_render_background (context, cr,
2620                                      day_rect.x, day_rect.y,
2621                                      day_rect.width, day_rect.height);
2622             }
2623         }
2624     }
2625
2626   gtk_style_context_set_state (context, state);
2627
2628   /* Translators: this defines whether the day numbers should use
2629    * localized digits or the ones used in English (0123...).
2630    *
2631    * Translate to "%Id" if you want to use localized digits, or
2632    * translate to "%d" otherwise.
2633    *
2634    * Note that translating this doesn't guarantee that you get localized
2635    * digits. That needs support from your system and locale definition
2636    * too.
2637    */
2638   g_snprintf (buffer, sizeof (buffer), C_("calendar:day:digits", "%d"), day);
2639
2640   /* Get extra information to show, if any: */
2641
2642   detail = gtk_calendar_get_detail (calendar, row, col);
2643
2644   layout = gtk_widget_create_pango_layout (widget, buffer);
2645   pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
2646   pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2647
2648   x_loc = day_rect.x + (day_rect.width - logical_rect.width) / 2;
2649   y_loc = day_rect.y;
2650
2651   gtk_render_layout (context, cr, x_loc, y_loc, layout);
2652
2653   if (priv->day_month[row][col] == MONTH_CURRENT &&
2654      (priv->marked_date[day-1] || (detail && !show_details)))
2655     gtk_render_layout (context, cr, x_loc - 1, y_loc, layout);
2656
2657   y_loc += priv->max_day_char_descent;
2658
2659   if (priv->detail_func && show_details)
2660     {
2661       GdkRGBA color;
2662
2663       cairo_save (cr);
2664
2665       gtk_style_context_get_color (context, state, &color);
2666       gdk_cairo_set_source_rgba (cr, &color);
2667
2668       cairo_set_line_width (cr, 1);
2669       cairo_move_to (cr, day_rect.x + 2, y_loc + 0.5);
2670       cairo_line_to (cr, day_rect.x + day_rect.width - 2, y_loc + 0.5);
2671       cairo_stroke (cr);
2672
2673       cairo_restore (cr);
2674
2675       y_loc += 2;
2676     }
2677
2678   if (detail && show_details)
2679     {
2680       gchar *markup = g_strconcat ("<small>", detail, "</small>", NULL);
2681       pango_layout_set_markup (layout, markup, -1);
2682       g_free (markup);
2683
2684       if (day == priv->selected_day)
2685         {
2686           /* Stripping colors as they conflict with selection marking. */
2687
2688           PangoAttrList *attrs = pango_layout_get_attributes (layout);
2689           PangoAttrList *colors = NULL;
2690
2691           if (attrs)
2692             colors = pango_attr_list_filter (attrs, is_color_attribute, NULL);
2693           if (colors)
2694             pango_attr_list_unref (colors);
2695         }
2696
2697       pango_layout_set_wrap (layout, PANGO_WRAP_WORD_CHAR);
2698       pango_layout_set_width (layout, PANGO_SCALE * day_rect.width);
2699
2700       if (priv->detail_height_rows)
2701         {
2702           gint dy = day_rect.height - (y_loc - day_rect.y);
2703           pango_layout_set_height (layout, PANGO_SCALE * dy);
2704           pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_END);
2705         }
2706
2707       cairo_move_to (cr, day_rect.x, y_loc);
2708       pango_cairo_show_layout (cr, layout);
2709     }
2710
2711   if (gtk_widget_has_focus (widget)
2712       && priv->focus_row == row && priv->focus_col == col)
2713     gtk_render_focus (context, cr,
2714                       day_rect.x, day_rect.y,
2715                       day_rect.width, day_rect.height);
2716
2717   if (overflow)
2718     priv->detail_overflow[row] |= (1 << col);
2719   else
2720     priv->detail_overflow[row] &= ~(1 << col);
2721
2722   gtk_style_context_restore (context);
2723   g_object_unref (layout);
2724   g_free (detail);
2725 }
2726
2727 static void
2728 calendar_paint_main (GtkCalendar *calendar,
2729                      cairo_t     *cr)
2730 {
2731   gint row, col;
2732
2733   cairo_save (cr);
2734
2735   for (col = 0; col < 7; col++)
2736     for (row = 0; row < 6; row++)
2737       calendar_paint_day (calendar, cr, row, col);
2738
2739   cairo_restore (cr);
2740 }
2741
2742 static void
2743 calendar_invalidate_arrow (GtkCalendar *calendar,
2744                            guint        arrow)
2745 {
2746   GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2747
2748   if (priv->display_flags & GTK_CALENDAR_SHOW_HEADING &&
2749       priv->arrow_win[arrow])
2750     {
2751       GdkRectangle rect;
2752       GtkAllocation allocation;
2753
2754       calendar_arrow_rectangle (calendar, arrow, &rect);
2755       gtk_widget_get_allocation (GTK_WIDGET (calendar), &allocation);
2756       gtk_widget_queue_draw_area (GTK_WIDGET (calendar),
2757                                   allocation.x + rect.x,
2758                                   allocation.y + rect.y,
2759                                   rect.width, rect.height);
2760     }
2761 }
2762
2763 static void
2764 calendar_paint_arrow (GtkCalendar *calendar,
2765                       cairo_t     *cr,
2766                       guint        arrow)
2767 {
2768   GtkWidget *widget = GTK_WIDGET (calendar);
2769   GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
2770   GtkStyleContext *context;
2771   GtkStateFlags state;
2772   GdkRectangle rect;
2773   gdouble angle;
2774
2775   if (!priv->arrow_win[arrow])
2776     return;
2777
2778   calendar_arrow_rectangle (calendar, arrow, &rect);
2779
2780   cairo_save (cr);
2781
2782   context = gtk_widget_get_style_context (widget);
2783   state = priv->arrow_state[arrow];
2784
2785   gtk_style_context_save (context);
2786   gtk_style_context_set_state (context, state);
2787   gtk_style_context_add_class (context, GTK_STYLE_CLASS_BUTTON);
2788
2789   gtk_render_background (context, cr,
2790                          rect.x, rect.y,
2791                          rect.width, rect.height);
2792
2793   if (arrow == ARROW_MONTH_LEFT || arrow == ARROW_YEAR_LEFT)
2794     angle = 3 * (G_PI / 2);
2795   else
2796     angle = G_PI / 2;
2797
2798   gtk_render_arrow (context, cr, angle,
2799                     rect.x + (rect.width - 8) / 2,
2800                     rect.y + (rect.height - 8) / 2,
2801                     8);
2802
2803   gtk_style_context_restore (context);
2804   cairo_restore (cr);
2805 }
2806
2807 static gboolean
2808 gtk_calendar_draw (GtkWidget *widget,
2809                    cairo_t   *cr)
2810 {
2811   GtkCalendar *calendar = GTK_CALENDAR (widget);
2812   GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
2813   int i;
2814
2815   if (gtk_cairo_should_draw_window (cr, gtk_widget_get_window (widget)))
2816     {
2817       GtkStyleContext *context;
2818
2819       context = gtk_widget_get_style_context (widget);
2820
2821       gtk_style_context_save (context);
2822       gtk_style_context_add_class (context, GTK_STYLE_CLASS_VIEW);
2823
2824       gtk_render_background (context, cr, 0, 0,
2825                              gtk_widget_get_allocated_width (widget),
2826                              gtk_widget_get_allocated_height (widget));
2827       gtk_render_frame (context, cr, 0, 0,
2828                         gtk_widget_get_allocated_width (widget),
2829                         gtk_widget_get_allocated_height (widget));
2830
2831       gtk_style_context_restore (context);
2832     }
2833
2834   calendar_paint_main (calendar, cr);
2835
2836   if (priv->display_flags & GTK_CALENDAR_SHOW_HEADING)
2837     {
2838       calendar_paint_header (calendar, cr);
2839       for (i = 0; i < 4; i++)
2840         calendar_paint_arrow (calendar, cr, i);
2841     }
2842
2843   if (priv->display_flags & GTK_CALENDAR_SHOW_DAY_NAMES)
2844     calendar_paint_day_names (calendar, cr);
2845
2846   if (priv->display_flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
2847     calendar_paint_week_numbers (calendar, cr);
2848
2849   return FALSE;
2850 }
2851
2852
2853 /****************************************
2854  *           Mouse handling             *
2855  ****************************************/
2856
2857 static void
2858 calendar_arrow_action (GtkCalendar *calendar,
2859                        guint        arrow)
2860 {
2861   switch (arrow)
2862     {
2863     case ARROW_YEAR_LEFT:
2864       calendar_set_year_prev (calendar);
2865       break;
2866     case ARROW_YEAR_RIGHT:
2867       calendar_set_year_next (calendar);
2868       break;
2869     case ARROW_MONTH_LEFT:
2870       calendar_set_month_prev (calendar);
2871       break;
2872     case ARROW_MONTH_RIGHT:
2873       calendar_set_month_next (calendar);
2874       break;
2875     default:;
2876       /* do nothing */
2877     }
2878 }
2879
2880 static gboolean
2881 calendar_timer (gpointer data)
2882 {
2883   GtkCalendar *calendar = data;
2884   GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2885   gboolean retval = FALSE;
2886
2887   if (priv->timer)
2888     {
2889       calendar_arrow_action (calendar, priv->click_child);
2890
2891       if (priv->need_timer)
2892         {
2893           GtkSettings *settings;
2894           guint        timeout;
2895
2896           settings = gtk_widget_get_settings (GTK_WIDGET (calendar));
2897           g_object_get (settings, "gtk-timeout-repeat", &timeout, NULL);
2898
2899           priv->need_timer = FALSE;
2900           priv->timer = gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT_IDLE,
2901                                             timeout * SCROLL_DELAY_FACTOR,
2902                                             (GSourceFunc) calendar_timer,
2903                                             (gpointer) calendar, NULL);
2904         }
2905       else
2906         retval = TRUE;
2907     }
2908
2909   return retval;
2910 }
2911
2912 static void
2913 calendar_start_spinning (GtkCalendar *calendar,
2914                          gint         click_child)
2915 {
2916   GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2917
2918   priv->click_child = click_child;
2919
2920   if (!priv->timer)
2921     {
2922       GtkSettings *settings;
2923       guint        timeout;
2924
2925       settings = gtk_widget_get_settings (GTK_WIDGET (calendar));
2926       g_object_get (settings, "gtk-timeout-initial", &timeout, NULL);
2927
2928       priv->need_timer = TRUE;
2929       priv->timer = gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT_IDLE,
2930                                         timeout,
2931                                         (GSourceFunc) calendar_timer,
2932                                         (gpointer) calendar, NULL);
2933     }
2934 }
2935
2936 static void
2937 calendar_stop_spinning (GtkCalendar *calendar)
2938 {
2939   GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2940
2941   if (priv->timer)
2942     {
2943       g_source_remove (priv->timer);
2944       priv->timer = 0;
2945       priv->need_timer = FALSE;
2946     }
2947 }
2948
2949 static void
2950 calendar_main_button_press (GtkCalendar    *calendar,
2951                             GdkEventButton *event)
2952 {
2953   GtkWidget *widget = GTK_WIDGET (calendar);
2954   GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
2955   gint x, y;
2956   gint win_x, win_y;
2957   gint row, col;
2958   gint day_month;
2959   gint day;
2960   GtkAllocation allocation;
2961
2962   x = (gint) (event->x);
2963   y = (gint) (event->y);
2964
2965   gdk_window_get_position (priv->main_win, &win_x, &win_y);
2966   gtk_widget_get_allocation (widget, &allocation);
2967
2968   row = calendar_row_from_y (calendar, y + win_y - allocation.y);
2969   col = calendar_column_from_x (calendar, x + win_x - allocation.x);
2970
2971   /* If row or column isn't found, just return. */
2972   if (row == -1 || col == -1)
2973     return;
2974
2975   day_month = priv->day_month[row][col];
2976
2977   if (event->type == GDK_BUTTON_PRESS)
2978     {
2979       day = priv->day[row][col];
2980
2981       if (day_month == MONTH_PREV)
2982         calendar_set_month_prev (calendar);
2983       else if (day_month == MONTH_NEXT)
2984         calendar_set_month_next (calendar);
2985
2986       if (!gtk_widget_has_focus (widget))
2987         gtk_widget_grab_focus (widget);
2988
2989       if (event->button == 1)
2990         {
2991           priv->in_drag = 1;
2992           priv->drag_start_x = x;
2993           priv->drag_start_y = y;
2994         }
2995
2996       calendar_select_and_focus_day (calendar, day);
2997     }
2998   else if (event->type == GDK_2BUTTON_PRESS)
2999     {
3000       priv->in_drag = 0;
3001       if (day_month == MONTH_CURRENT)
3002         g_signal_emit (calendar,
3003                        gtk_calendar_signals[DAY_SELECTED_DOUBLE_CLICK_SIGNAL],
3004                        0);
3005     }
3006 }
3007
3008 static gboolean
3009 gtk_calendar_button_press (GtkWidget      *widget,
3010                            GdkEventButton *event)
3011 {
3012   GtkCalendar *calendar = GTK_CALENDAR (widget);
3013   GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3014   gint arrow = -1;
3015
3016   if (event->window == priv->main_win)
3017     calendar_main_button_press (calendar, event);
3018
3019   if (!gtk_widget_has_focus (widget))
3020     gtk_widget_grab_focus (widget);
3021
3022   for (arrow = ARROW_YEAR_LEFT; arrow <= ARROW_MONTH_RIGHT; arrow++)
3023     {
3024       if (event->window == priv->arrow_win[arrow])
3025         {
3026
3027           /* only call the action on single click, not double */
3028           if (event->type == GDK_BUTTON_PRESS)
3029             {
3030               if (event->button == 1)
3031                 calendar_start_spinning (calendar, arrow);
3032
3033               calendar_arrow_action (calendar, arrow);
3034             }
3035
3036           return TRUE;
3037         }
3038     }
3039
3040   return FALSE;
3041 }
3042
3043 static gboolean
3044 gtk_calendar_button_release (GtkWidget    *widget,
3045                              GdkEventButton *event)
3046 {
3047   GtkCalendar *calendar = GTK_CALENDAR (widget);
3048   GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3049
3050   if (event->button == 1)
3051     {
3052       calendar_stop_spinning (calendar);
3053
3054       if (priv->in_drag)
3055         priv->in_drag = 0;
3056     }
3057
3058   return TRUE;
3059 }
3060
3061 static gboolean
3062 gtk_calendar_motion_notify (GtkWidget      *widget,
3063                             GdkEventMotion *event)
3064 {
3065   GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3066
3067   if (priv->in_drag)
3068     {
3069       if (gtk_drag_check_threshold (widget,
3070                                     priv->drag_start_x, priv->drag_start_y,
3071                                     event->x, event->y))
3072         {
3073           GdkDragContext *context;
3074           GtkTargetList *target_list = gtk_target_list_new (NULL, 0);
3075           gtk_target_list_add_text_targets (target_list, 0);
3076           context = gtk_drag_begin (widget, target_list, GDK_ACTION_COPY,
3077                                     1, (GdkEvent *)event);
3078
3079           priv->in_drag = 0;
3080           gtk_target_list_unref (target_list);
3081           gtk_drag_set_icon_default (context);
3082         }
3083     }
3084
3085   return TRUE;
3086 }
3087
3088 static gboolean
3089 gtk_calendar_enter_notify (GtkWidget        *widget,
3090                            GdkEventCrossing *event)
3091 {
3092   GtkCalendar *calendar = GTK_CALENDAR (widget);
3093   GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3094
3095   if (event->window == priv->arrow_win[ARROW_MONTH_LEFT])
3096     {
3097       priv->arrow_state[ARROW_MONTH_LEFT] |= GTK_STATE_FLAG_PRELIGHT;
3098       calendar_invalidate_arrow (calendar, ARROW_MONTH_LEFT);
3099     }
3100
3101   if (event->window == priv->arrow_win[ARROW_MONTH_RIGHT])
3102     {
3103       priv->arrow_state[ARROW_MONTH_RIGHT] |= GTK_STATE_FLAG_PRELIGHT;
3104       calendar_invalidate_arrow (calendar, ARROW_MONTH_RIGHT);
3105     }
3106
3107   if (event->window == priv->arrow_win[ARROW_YEAR_LEFT])
3108     {
3109       priv->arrow_state[ARROW_YEAR_LEFT] |= GTK_STATE_FLAG_PRELIGHT;
3110       calendar_invalidate_arrow (calendar, ARROW_YEAR_LEFT);
3111     }
3112
3113   if (event->window == priv->arrow_win[ARROW_YEAR_RIGHT])
3114     {
3115       priv->arrow_state[ARROW_YEAR_RIGHT] |= GTK_STATE_FLAG_PRELIGHT;
3116       calendar_invalidate_arrow (calendar, ARROW_YEAR_RIGHT);
3117     }
3118
3119   return TRUE;
3120 }
3121
3122 static gboolean
3123 gtk_calendar_leave_notify (GtkWidget        *widget,
3124                            GdkEventCrossing *event)
3125 {
3126   GtkCalendar *calendar = GTK_CALENDAR (widget);
3127   GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3128
3129   if (event->window == priv->arrow_win[ARROW_MONTH_LEFT])
3130     {
3131       priv->arrow_state[ARROW_MONTH_LEFT] &= ~(GTK_STATE_FLAG_PRELIGHT);
3132       calendar_invalidate_arrow (calendar, ARROW_MONTH_LEFT);
3133     }
3134
3135   if (event->window == priv->arrow_win[ARROW_MONTH_RIGHT])
3136     {
3137       priv->arrow_state[ARROW_MONTH_RIGHT] &= ~(GTK_STATE_FLAG_PRELIGHT);
3138       calendar_invalidate_arrow (calendar, ARROW_MONTH_RIGHT);
3139     }
3140
3141   if (event->window == priv->arrow_win[ARROW_YEAR_LEFT])
3142     {
3143       priv->arrow_state[ARROW_YEAR_LEFT] &= ~(GTK_STATE_FLAG_PRELIGHT);
3144       calendar_invalidate_arrow (calendar, ARROW_YEAR_LEFT);
3145     }
3146
3147   if (event->window == priv->arrow_win[ARROW_YEAR_RIGHT])
3148     {
3149       priv->arrow_state[ARROW_YEAR_RIGHT] &= ~(GTK_STATE_FLAG_PRELIGHT);
3150       calendar_invalidate_arrow (calendar, ARROW_YEAR_RIGHT);
3151     }
3152
3153   return TRUE;
3154 }
3155
3156 static gboolean
3157 gtk_calendar_scroll (GtkWidget      *widget,
3158                      GdkEventScroll *event)
3159 {
3160   GtkCalendar *calendar = GTK_CALENDAR (widget);
3161
3162   if (event->direction == GDK_SCROLL_UP)
3163     {
3164       if (!gtk_widget_has_focus (widget))
3165         gtk_widget_grab_focus (widget);
3166       calendar_set_month_prev (calendar);
3167     }
3168   else if (event->direction == GDK_SCROLL_DOWN)
3169     {
3170       if (!gtk_widget_has_focus (widget))
3171         gtk_widget_grab_focus (widget);
3172       calendar_set_month_next (calendar);
3173     }
3174   else
3175     return FALSE;
3176
3177   return TRUE;
3178 }
3179
3180 \f
3181 /****************************************
3182  *             Key handling              *
3183  ****************************************/
3184
3185 static void
3186 move_focus (GtkCalendar *calendar,
3187             gint         direction)
3188 {
3189   GtkCalendarPrivate *priv = calendar->priv;
3190   GtkTextDirection text_dir = gtk_widget_get_direction (GTK_WIDGET (calendar));
3191
3192   if ((text_dir == GTK_TEXT_DIR_LTR && direction == -1) ||
3193       (text_dir == GTK_TEXT_DIR_RTL && direction == 1))
3194     {
3195       if (priv->focus_col > 0)
3196           priv->focus_col--;
3197       else if (priv->focus_row > 0)
3198         {
3199           priv->focus_col = 6;
3200           priv->focus_row--;
3201         }
3202
3203       if (priv->focus_col < 0)
3204         priv->focus_col = 6;
3205       if (priv->focus_row < 0)
3206         priv->focus_row = 5;
3207     }
3208   else
3209     {
3210       if (priv->focus_col < 6)
3211         priv->focus_col++;
3212       else if (priv->focus_row < 5)
3213         {
3214           priv->focus_col = 0;
3215           priv->focus_row++;
3216         }
3217
3218       if (priv->focus_col < 0)
3219         priv->focus_col = 0;
3220       if (priv->focus_row < 0)
3221         priv->focus_row = 0;
3222     }
3223 }
3224
3225 static gboolean
3226 gtk_calendar_key_press (GtkWidget   *widget,
3227                         GdkEventKey *event)
3228 {
3229   GtkCalendar *calendar = GTK_CALENDAR (widget);
3230   GtkCalendarPrivate *priv = calendar->priv;
3231   gint return_val;
3232   gint old_focus_row;
3233   gint old_focus_col;
3234   gint row, col, day;
3235
3236   return_val = FALSE;
3237
3238   old_focus_row = priv->focus_row;
3239   old_focus_col = priv->focus_col;
3240
3241   switch (event->keyval)
3242     {
3243     case GDK_KEY_KP_Left:
3244     case GDK_KEY_Left:
3245       return_val = TRUE;
3246       if (event->state & GDK_CONTROL_MASK)
3247         calendar_set_month_prev (calendar);
3248       else
3249         {
3250           move_focus (calendar, -1);
3251           calendar_invalidate_day (calendar, old_focus_row, old_focus_col);
3252           calendar_invalidate_day (calendar, priv->focus_row,
3253                                    priv->focus_col);
3254         }
3255       break;
3256     case GDK_KEY_KP_Right:
3257     case GDK_KEY_Right:
3258       return_val = TRUE;
3259       if (event->state & GDK_CONTROL_MASK)
3260         calendar_set_month_next (calendar);
3261       else
3262         {
3263           move_focus (calendar, 1);
3264           calendar_invalidate_day (calendar, old_focus_row, old_focus_col);
3265           calendar_invalidate_day (calendar, priv->focus_row,
3266                                    priv->focus_col);
3267         }
3268       break;
3269     case GDK_KEY_KP_Up:
3270     case GDK_KEY_Up:
3271       return_val = TRUE;
3272       if (event->state & GDK_CONTROL_MASK)
3273         calendar_set_year_prev (calendar);
3274       else
3275         {
3276           if (priv->focus_row > 0)
3277             priv->focus_row--;
3278           if (priv->focus_row < 0)
3279             priv->focus_row = 5;
3280           if (priv->focus_col < 0)
3281             priv->focus_col = 6;
3282           calendar_invalidate_day (calendar, old_focus_row, old_focus_col);
3283           calendar_invalidate_day (calendar, priv->focus_row,
3284                                    priv->focus_col);
3285         }
3286       break;
3287     case GDK_KEY_KP_Down:
3288     case GDK_KEY_Down:
3289       return_val = TRUE;
3290       if (event->state & GDK_CONTROL_MASK)
3291         calendar_set_year_next (calendar);
3292       else
3293         {
3294           if (priv->focus_row < 5)
3295             priv->focus_row++;
3296           if (priv->focus_col < 0)
3297             priv->focus_col = 0;
3298           calendar_invalidate_day (calendar, old_focus_row, old_focus_col);
3299           calendar_invalidate_day (calendar, priv->focus_row,
3300                                    priv->focus_col);
3301         }
3302       break;
3303     case GDK_KEY_KP_Space:
3304     case GDK_KEY_space:
3305       row = priv->focus_row;
3306       col = priv->focus_col;
3307
3308       if (row > -1 && col > -1)
3309         {
3310           return_val = TRUE;
3311
3312           day = priv->day[row][col];
3313           if (priv->day_month[row][col] == MONTH_PREV)
3314             calendar_set_month_prev (calendar);
3315           else if (priv->day_month[row][col] == MONTH_NEXT)
3316             calendar_set_month_next (calendar);
3317
3318           calendar_select_and_focus_day (calendar, day);
3319         }
3320     }
3321
3322   return return_val;
3323 }
3324
3325 \f
3326 /****************************************
3327  *           Misc widget methods        *
3328  ****************************************/
3329
3330 static void
3331 gtk_calendar_state_flags_changed (GtkWidget     *widget,
3332                                   GtkStateFlags  previous_state)
3333 {
3334   GtkCalendar *calendar = GTK_CALENDAR (widget);
3335   GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3336   int i;
3337
3338   if (!gtk_widget_is_sensitive (widget))
3339     {
3340       priv->in_drag = 0;
3341       calendar_stop_spinning (calendar);
3342     }
3343
3344   for (i = 0; i < 4; i++)
3345     if (gtk_widget_is_sensitive (widget))
3346       priv->arrow_state[i] &= ~(GTK_STATE_FLAG_INSENSITIVE);
3347     else
3348       priv->arrow_state[i] |= GTK_STATE_FLAG_INSENSITIVE;
3349 }
3350
3351 static void
3352 gtk_calendar_grab_notify (GtkWidget *widget,
3353                           gboolean   was_grabbed)
3354 {
3355   if (!was_grabbed)
3356     calendar_stop_spinning (GTK_CALENDAR (widget));
3357 }
3358
3359 static gboolean
3360 gtk_calendar_focus_out (GtkWidget     *widget,
3361                         GdkEventFocus *event)
3362 {
3363   GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3364   GtkCalendar *calendar = GTK_CALENDAR (widget);
3365
3366   calendar_queue_refresh (calendar);
3367   calendar_stop_spinning (calendar);
3368
3369   priv->in_drag = 0;
3370
3371   return FALSE;
3372 }
3373
3374 \f
3375 /****************************************
3376  *          Drag and Drop               *
3377  ****************************************/
3378
3379 static void
3380 gtk_calendar_drag_data_get (GtkWidget        *widget,
3381                             GdkDragContext   *context,
3382                             GtkSelectionData *selection_data,
3383                             guint             info,
3384                             guint             time)
3385 {
3386   GtkCalendar *calendar = GTK_CALENDAR (widget);
3387   GtkCalendarPrivate *priv = calendar->priv;
3388   GDate *date;
3389   gchar str[128];
3390   gsize len;
3391
3392   date = g_date_new_dmy (priv->selected_day, priv->month + 1, priv->year);
3393   len = g_date_strftime (str, 127, "%x", date);
3394   gtk_selection_data_set_text (selection_data, str, len);
3395
3396   g_free (date);
3397 }
3398
3399 /* Get/set whether drag_motion requested the drag data and
3400  * drag_data_received should thus not actually insert the data,
3401  * since the data doesn't result from a drop.
3402  */
3403 static void
3404 set_status_pending (GdkDragContext *context,
3405                     GdkDragAction   suggested_action)
3406 {
3407   g_object_set_data (G_OBJECT (context),
3408                      I_("gtk-calendar-status-pending"),
3409                      GINT_TO_POINTER (suggested_action));
3410 }
3411
3412 static GdkDragAction
3413 get_status_pending (GdkDragContext *context)
3414 {
3415   return GPOINTER_TO_INT (g_object_get_data (G_OBJECT (context),
3416                                              "gtk-calendar-status-pending"));
3417 }
3418
3419 static void
3420 gtk_calendar_drag_leave (GtkWidget      *widget,
3421                          GdkDragContext *context,
3422                          guint           time)
3423 {
3424   GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3425
3426   priv->drag_highlight = 0;
3427   gtk_drag_unhighlight (widget);
3428
3429 }
3430
3431 static gboolean
3432 gtk_calendar_drag_motion (GtkWidget      *widget,
3433                           GdkDragContext *context,
3434                           gint            x,
3435                           gint            y,
3436                           guint           time)
3437 {
3438   GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (widget);
3439   GdkAtom target;
3440
3441   if (!priv->drag_highlight)
3442     {
3443       priv->drag_highlight = 1;
3444       gtk_drag_highlight (widget);
3445     }
3446
3447   target = gtk_drag_dest_find_target (widget, context, NULL);
3448   if (target == GDK_NONE || gdk_drag_context_get_suggested_action (context) == 0)
3449     gdk_drag_status (context, 0, time);
3450   else
3451     {
3452       set_status_pending (context, gdk_drag_context_get_suggested_action (context));
3453       gtk_drag_get_data (widget, context, target, time);
3454     }
3455
3456   return TRUE;
3457 }
3458
3459 static gboolean
3460 gtk_calendar_drag_drop (GtkWidget      *widget,
3461                         GdkDragContext *context,
3462                         gint            x,
3463                         gint            y,
3464                         guint           time)
3465 {
3466   GdkAtom target;
3467
3468   target = gtk_drag_dest_find_target (widget, context, NULL);
3469   if (target != GDK_NONE)
3470     {
3471       gtk_drag_get_data (widget, context,
3472                          target,
3473                          time);
3474       return TRUE;
3475     }
3476
3477   return FALSE;
3478 }
3479
3480 static void
3481 gtk_calendar_drag_data_received (GtkWidget        *widget,
3482                                  GdkDragContext   *context,
3483                                  gint              x,
3484                                  gint              y,
3485                                  GtkSelectionData *selection_data,
3486                                  guint             info,
3487                                  guint             time)
3488 {
3489   GtkCalendar *calendar = GTK_CALENDAR (widget);
3490   GtkCalendarPrivate *priv = calendar->priv;
3491   guint day, month, year;
3492   gchar *str;
3493   GDate *date;
3494   GdkDragAction suggested_action;
3495
3496   suggested_action = get_status_pending (context);
3497
3498   if (suggested_action)
3499     {
3500       set_status_pending (context, 0);
3501
3502       /* We are getting this data due to a request in drag_motion,
3503        * rather than due to a request in drag_drop, so we are just
3504        * supposed to call drag_status, not actually paste in the
3505        * data.
3506        */
3507       str = (gchar*) gtk_selection_data_get_text (selection_data);
3508
3509       if (str)
3510         {
3511           date = g_date_new ();
3512           g_date_set_parse (date, str);
3513           if (!g_date_valid (date))
3514               suggested_action = 0;
3515           g_date_free (date);
3516           g_free (str);
3517         }
3518       else
3519         suggested_action = 0;
3520
3521       gdk_drag_status (context, suggested_action, time);
3522
3523       return;
3524     }
3525
3526   date = g_date_new ();
3527   str = (gchar*) gtk_selection_data_get_text (selection_data);
3528   if (str)
3529     {
3530       g_date_set_parse (date, str);
3531       g_free (str);
3532     }
3533
3534   if (!g_date_valid (date))
3535     {
3536       g_warning ("Received invalid date data\n");
3537       g_date_free (date);
3538       gtk_drag_finish (context, FALSE, FALSE, time);
3539       return;
3540     }
3541
3542   day = g_date_get_day (date);
3543   month = g_date_get_month (date);
3544   year = g_date_get_year (date);
3545   g_date_free (date);
3546
3547   gtk_drag_finish (context, TRUE, FALSE, time);
3548
3549
3550   g_object_freeze_notify (G_OBJECT (calendar));
3551   if (!(priv->display_flags & GTK_CALENDAR_NO_MONTH_CHANGE)
3552       && (priv->display_flags & GTK_CALENDAR_SHOW_HEADING))
3553     gtk_calendar_select_month (calendar, month - 1, year);
3554   gtk_calendar_select_day (calendar, day);
3555   g_object_thaw_notify (G_OBJECT (calendar));
3556 }
3557
3558 \f
3559 /****************************************
3560  *              Public API              *
3561  ****************************************/
3562
3563 /**
3564  * gtk_calendar_new:
3565  *
3566  * Creates a new calendar, with the current date being selected.
3567  *
3568  * Return value: a newly #GtkCalendar widget
3569  **/
3570 GtkWidget*
3571 gtk_calendar_new (void)
3572 {
3573   return g_object_new (GTK_TYPE_CALENDAR, NULL);
3574 }
3575
3576 /**
3577  * gtk_calendar_get_display_options:
3578  * @calendar: a #GtkCalendar
3579  *
3580  * Returns the current display options of @calendar.
3581  *
3582  * Return value: the display options.
3583  *
3584  * Since: 2.4
3585  **/
3586 GtkCalendarDisplayOptions
3587 gtk_calendar_get_display_options (GtkCalendar         *calendar)
3588 {
3589   g_return_val_if_fail (GTK_IS_CALENDAR (calendar), 0);
3590
3591   return calendar->priv->display_flags;
3592 }
3593
3594 /**
3595  * gtk_calendar_set_display_options:
3596  * @calendar: a #GtkCalendar
3597  * @flags: the display options to set
3598  *
3599  * Sets display options (whether to display the heading and the month
3600  * headings).
3601  *
3602  * Since: 2.4
3603  **/
3604 void
3605 gtk_calendar_set_display_options (GtkCalendar          *calendar,
3606                                   GtkCalendarDisplayOptions flags)
3607 {
3608   GtkWidget *widget = GTK_WIDGET (calendar);
3609   GtkCalendarPrivate *priv = GTK_CALENDAR_GET_PRIVATE (calendar);
3610   gint resize = 0;
3611   GtkCalendarDisplayOptions old_flags;
3612
3613   g_return_if_fail (GTK_IS_CALENDAR (calendar));
3614
3615   old_flags = priv->display_flags;
3616
3617   if (gtk_widget_get_realized (widget))
3618     {
3619       if ((flags ^ priv->display_flags) & GTK_CALENDAR_NO_MONTH_CHANGE)
3620         {
3621           resize ++;
3622           if (! (flags & GTK_CALENDAR_NO_MONTH_CHANGE)
3623               && (priv->display_flags & GTK_CALENDAR_SHOW_HEADING))
3624             {
3625               priv->display_flags &= ~GTK_CALENDAR_NO_MONTH_CHANGE;
3626               calendar_realize_arrows (calendar);
3627               if (gtk_widget_get_mapped (widget))
3628                 calendar_map_arrows (calendar);
3629             }
3630           else
3631             {
3632               calendar_unrealize_arrows (calendar);
3633             }
3634         }
3635
3636       if ((flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_HEADING)
3637         {
3638           resize++;
3639
3640           if (flags & GTK_CALENDAR_SHOW_HEADING)
3641             {
3642               priv->display_flags |= GTK_CALENDAR_SHOW_HEADING;
3643               calendar_realize_arrows (calendar);
3644               if (gtk_widget_get_mapped (widget))
3645                 calendar_map_arrows (calendar);
3646             }
3647           else
3648             {
3649               calendar_unrealize_arrows (calendar);
3650             }
3651         }
3652
3653       if ((flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_DAY_NAMES)
3654         {
3655           resize++;
3656
3657           if (flags & GTK_CALENDAR_SHOW_DAY_NAMES)
3658             priv->display_flags |= GTK_CALENDAR_SHOW_DAY_NAMES;
3659         }
3660
3661       if ((flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
3662         {
3663           resize++;
3664
3665           if (flags & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
3666             priv->display_flags |= GTK_CALENDAR_SHOW_WEEK_NUMBERS;
3667         }
3668
3669       if ((flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_DETAILS)
3670         resize++;
3671
3672       priv->display_flags = flags;
3673       if (resize)
3674         gtk_widget_queue_resize (GTK_WIDGET (calendar));
3675     }
3676   else
3677     priv->display_flags = flags;
3678
3679   g_object_freeze_notify (G_OBJECT (calendar));
3680   if ((old_flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_HEADING)
3681     g_object_notify (G_OBJECT (calendar), "show-heading");
3682   if ((old_flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_DAY_NAMES)
3683     g_object_notify (G_OBJECT (calendar), "show-day-names");
3684   if ((old_flags ^ priv->display_flags) & GTK_CALENDAR_NO_MONTH_CHANGE)
3685     g_object_notify (G_OBJECT (calendar), "no-month-change");
3686   if ((old_flags ^ priv->display_flags) & GTK_CALENDAR_SHOW_WEEK_NUMBERS)
3687     g_object_notify (G_OBJECT (calendar), "show-week-numbers");
3688   g_object_thaw_notify (G_OBJECT (calendar));
3689 }
3690
3691 /**
3692  * gtk_calendar_select_month:
3693  * @calendar: a #GtkCalendar
3694  * @month: a month number between 0 and 11.
3695  * @year: the year the month is in.
3696  *
3697  * Shifts the calendar to a different month.
3698  **/
3699 void
3700 gtk_calendar_select_month (GtkCalendar *calendar,
3701                            guint        month,
3702                            guint        year)
3703 {
3704   GtkCalendarPrivate *priv;
3705
3706   g_return_if_fail (GTK_IS_CALENDAR (calendar));
3707   g_return_if_fail (month <= 11);
3708
3709   priv = calendar->priv;
3710
3711   priv->month = month;
3712   priv->year  = year;
3713
3714   calendar_compute_days (calendar);
3715   calendar_queue_refresh (calendar);
3716
3717   g_object_freeze_notify (G_OBJECT (calendar));
3718   g_object_notify (G_OBJECT (calendar), "month");
3719   g_object_notify (G_OBJECT (calendar), "year");
3720   g_object_thaw_notify (G_OBJECT (calendar));
3721
3722   g_signal_emit (calendar,
3723                  gtk_calendar_signals[MONTH_CHANGED_SIGNAL],
3724                  0);
3725 }
3726
3727 /**
3728  * gtk_calendar_select_day:
3729  * @calendar: a #GtkCalendar.
3730  * @day: the day number between 1 and 31, or 0 to unselect
3731  *   the currently selected day.
3732  *
3733  * Selects a day from the current month.
3734  **/
3735 void
3736 gtk_calendar_select_day (GtkCalendar *calendar,
3737                          guint        day)
3738 {
3739   GtkCalendarPrivate *priv;
3740
3741   g_return_if_fail (GTK_IS_CALENDAR (calendar));
3742   g_return_if_fail (day <= 31);
3743
3744   priv = calendar->priv;
3745
3746   /* Deselect the old day */
3747   if (priv->selected_day > 0)
3748     {
3749       gint selected_day;
3750
3751       selected_day = priv->selected_day;
3752       priv->selected_day = 0;
3753       if (gtk_widget_is_drawable (GTK_WIDGET (calendar)))
3754         calendar_invalidate_day_num (calendar, selected_day);
3755     }
3756
3757   priv->selected_day = day;
3758
3759   /* Select the new day */
3760   if (day != 0)
3761     {
3762       if (gtk_widget_is_drawable (GTK_WIDGET (calendar)))
3763         calendar_invalidate_day_num (calendar, day);
3764     }
3765
3766   g_object_notify (G_OBJECT (calendar), "day");
3767
3768   g_signal_emit (calendar,
3769                  gtk_calendar_signals[DAY_SELECTED_SIGNAL],
3770                  0);
3771 }
3772
3773 /**
3774  * gtk_calendar_clear_marks:
3775  * @calendar: a #GtkCalendar
3776  *
3777  * Remove all visual markers.
3778  **/
3779 void
3780 gtk_calendar_clear_marks (GtkCalendar *calendar)
3781 {
3782   GtkCalendarPrivate *priv;
3783   guint day;
3784
3785   g_return_if_fail (GTK_IS_CALENDAR (calendar));
3786
3787   priv = calendar->priv;
3788
3789   for (day = 0; day < 31; day++)
3790     {
3791       priv->marked_date[day] = FALSE;
3792     }
3793
3794   priv->num_marked_dates = 0;
3795   calendar_queue_refresh (calendar);
3796 }
3797
3798 /**
3799  * gtk_calendar_mark_day:
3800  * @calendar: a #GtkCalendar
3801  * @day: the day number to mark between 1 and 31.
3802  *
3803  * Places a visual marker on a particular day.
3804  */
3805 void
3806 gtk_calendar_mark_day (GtkCalendar *calendar,
3807                        guint        day)
3808 {
3809   GtkCalendarPrivate *priv;
3810
3811   g_return_if_fail (GTK_IS_CALENDAR (calendar));
3812
3813   priv = calendar->priv;
3814
3815   if (day >= 1 && day <= 31 && !priv->marked_date[day-1])
3816     {
3817       priv->marked_date[day - 1] = TRUE;
3818       priv->num_marked_dates++;
3819       calendar_invalidate_day_num (calendar, day);
3820     }
3821 }
3822
3823 /**
3824  * gtk_calendar_get_day_is_marked:
3825  * @calendar: a #GtkCalendar
3826  * @day: the day number between 1 and 31.
3827  *
3828  * Returns if the @day of the @calendar is already marked.
3829  *
3830  * Returns: whether the day is marked.
3831  *
3832  * Since: 3.0
3833  */
3834 gboolean
3835 gtk_calendar_get_day_is_marked (GtkCalendar *calendar,
3836                                 guint        day)
3837 {
3838   GtkCalendarPrivate *priv;
3839
3840   g_return_val_if_fail (GTK_IS_CALENDAR (calendar), FALSE);
3841
3842   priv = calendar->priv;
3843
3844   if (day >= 1 && day <= 31)
3845     return priv->marked_date[day - 1];
3846
3847   return FALSE;
3848 }
3849
3850 /**
3851  * gtk_calendar_unmark_day:
3852  * @calendar: a #GtkCalendar.
3853  * @day: the day number to unmark between 1 and 31.
3854  *
3855  * Removes the visual marker from a particular day.
3856  */
3857 void
3858 gtk_calendar_unmark_day (GtkCalendar *calendar,
3859                          guint        day)
3860 {
3861   GtkCalendarPrivate *priv;
3862
3863   g_return_if_fail (GTK_IS_CALENDAR (calendar));
3864
3865   priv = calendar->priv;
3866
3867   if (day >= 1 && day <= 31 && priv->marked_date[day-1])
3868     {
3869       priv->marked_date[day - 1] = FALSE;
3870       priv->num_marked_dates--;
3871       calendar_invalidate_day_num (calendar, day);
3872     }
3873 }
3874
3875 /**
3876  * gtk_calendar_get_date:
3877  * @calendar: a #GtkCalendar
3878  * @year: (out) (allow-none): location to store the year as a decimal
3879  *     number (e.g. 2011), or %NULL
3880  * @month: (out) (allow-none): location to store the month number
3881  *     (between 0 and 11), or %NULL
3882  * @day: (out) (allow-none): location to store the day number (between
3883  *     1 and 31), or %NULL
3884  *
3885  * Obtains the selected date from a #GtkCalendar.
3886  */
3887 void
3888 gtk_calendar_get_date (GtkCalendar *calendar,
3889                        guint       *year,
3890                        guint       *month,
3891                        guint       *day)
3892 {
3893   GtkCalendarPrivate *priv;
3894
3895   g_return_if_fail (GTK_IS_CALENDAR (calendar));
3896
3897   priv = calendar->priv;
3898
3899   if (year)
3900     *year = priv->year;
3901
3902   if (month)
3903     *month = priv->month;
3904
3905   if (day)
3906     *day = priv->selected_day;
3907 }
3908
3909 /**
3910  * gtk_calendar_set_detail_func:
3911  * @calendar: a #GtkCalendar.
3912  * @func: a function providing details for each day.
3913  * @data: data to pass to @func invokations.
3914  * @destroy: a function for releasing @data.
3915  *
3916  * Installs a function which provides Pango markup with detail information
3917  * for each day. Examples for such details are holidays or appointments. That
3918  * information is shown below each day when #GtkCalendar:show-details is set.
3919  * A tooltip containing with full detail information is provided, if the entire
3920  * text should not fit into the details area, or if #GtkCalendar:show-details
3921  * is not set.
3922  *
3923  * The size of the details area can be restricted by setting the
3924  * #GtkCalendar:detail-width-chars and #GtkCalendar:detail-height-rows
3925  * properties.
3926  *
3927  * Since: 2.14
3928  */
3929 void
3930 gtk_calendar_set_detail_func (GtkCalendar           *calendar,
3931                               GtkCalendarDetailFunc  func,
3932                               gpointer               data,
3933                               GDestroyNotify         destroy)
3934 {
3935   GtkCalendarPrivate *priv;
3936
3937   g_return_if_fail (GTK_IS_CALENDAR (calendar));
3938
3939   priv = GTK_CALENDAR_GET_PRIVATE (calendar);
3940
3941   if (priv->detail_func_destroy)
3942     priv->detail_func_destroy (priv->detail_func_user_data);
3943
3944   priv->detail_func = func;
3945   priv->detail_func_user_data = data;
3946   priv->detail_func_destroy = destroy;
3947
3948   gtk_widget_set_has_tooltip (GTK_WIDGET (calendar),
3949                               NULL != priv->detail_func);
3950   gtk_widget_queue_resize (GTK_WIDGET (calendar));
3951 }
3952
3953 /**
3954  * gtk_calendar_set_detail_width_chars:
3955  * @calendar: a #GtkCalendar.
3956  * @chars: detail width in characters.
3957  *
3958  * Updates the width of detail cells.
3959  * See #GtkCalendar:detail-width-chars.
3960  *
3961  * Since: 2.14
3962  */
3963 void
3964 gtk_calendar_set_detail_width_chars (GtkCalendar *calendar,
3965                                      gint         chars)
3966 {
3967   GtkCalendarPrivate *priv;
3968
3969   g_return_if_fail (GTK_IS_CALENDAR (calendar));
3970
3971   priv = GTK_CALENDAR_GET_PRIVATE (calendar);
3972
3973   if (chars != priv->detail_width_chars)
3974     {
3975       priv->detail_width_chars = chars;
3976       g_object_notify (G_OBJECT (calendar), "detail-width-chars");
3977       gtk_widget_queue_resize_no_redraw (GTK_WIDGET (calendar));
3978     }
3979 }
3980
3981 /**
3982  * gtk_calendar_set_detail_height_rows:
3983  * @calendar: a #GtkCalendar.
3984  * @rows: detail height in rows.
3985  *
3986  * Updates the height of detail cells.
3987  * See #GtkCalendar:detail-height-rows.
3988  *
3989  * Since: 2.14
3990  */
3991 void
3992 gtk_calendar_set_detail_height_rows (GtkCalendar *calendar,
3993                                      gint         rows)
3994 {
3995   GtkCalendarPrivate *priv;
3996
3997   g_return_if_fail (GTK_IS_CALENDAR (calendar));
3998
3999   priv = GTK_CALENDAR_GET_PRIVATE (calendar);
4000
4001   if (rows != priv->detail_height_rows)
4002     {
4003       priv->detail_height_rows = rows;
4004       g_object_notify (G_OBJECT (calendar), "detail-height-rows");
4005       gtk_widget_queue_resize_no_redraw (GTK_WIDGET (calendar));
4006     }
4007 }
4008
4009 /**
4010  * gtk_calendar_get_detail_width_chars:
4011  * @calendar: a #GtkCalendar.
4012  *
4013  * Queries the width of detail cells, in characters.
4014  * See #GtkCalendar:detail-width-chars.
4015  *
4016  * Since: 2.14
4017  *
4018  * Return value: The width of detail cells, in characters.
4019  */
4020 gint
4021 gtk_calendar_get_detail_width_chars (GtkCalendar *calendar)
4022 {
4023   g_return_val_if_fail (GTK_IS_CALENDAR (calendar), 0);
4024   return GTK_CALENDAR_GET_PRIVATE (calendar)->detail_width_chars;
4025 }
4026
4027 /**
4028  * gtk_calendar_get_detail_height_rows:
4029  * @calendar: a #GtkCalendar.
4030  *
4031  * Queries the height of detail cells, in rows.
4032  * See #GtkCalendar:detail-width-chars.
4033  *
4034  * Since: 2.14
4035  *
4036  * Return value: The height of detail cells, in rows.
4037  */
4038 gint
4039 gtk_calendar_get_detail_height_rows (GtkCalendar *calendar)
4040 {
4041   g_return_val_if_fail (GTK_IS_CALENDAR (calendar), 0);
4042   return GTK_CALENDAR_GET_PRIVATE (calendar)->detail_height_rows;
4043 }