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