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