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