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