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