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