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