aboutsummaryrefslogtreecommitdiffstats
path: root/calendar/gui/e-meeting-time-sel-item.c
blob: 00efabe029f74ab62c3c0d6893edfd128eefa013 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
/*
 * EMeetingTimeSelectorItem - A GnomeCanvasItem which is used for both the main
 * display canvas and the top display (with the dates, times & All Attendees).
 * I didn't make these separate GnomeCanvasItems since they share a lot of
 * code.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with the program; if not, see <http://www.gnu.org/licenses/>
 *
 * Authors:
 *      Damon Chaplin <damon@gtk.org>
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <time.h>
#include <glib.h>
#include <glib/gi18n.h>
#include "calendar-config.h"
#include "e-meeting-time-sel-item.h"
#include "e-meeting-time-sel.h"

/* Initially the grid lines were drawn at the bottom of cells, but this didn't
   line up well with the GtkEntry widgets, which in the default theme draw a
   black shadow line across the top. So I've switched our code to draw the
   lines across the top of cells. */
#define E_MEETING_TIME_SELECTOR_DRAW_GRID_LINES_AT_BOTTOM 0

static void e_meeting_time_selector_item_dispose (GObject *object);

static void e_meeting_time_selector_item_set_property (GObject *object,
                                                       guint property_id,
                                                       const GValue *value,
                                                       GParamSpec *pspec);
static void e_meeting_time_selector_item_realize (GnomeCanvasItem *item);
static void e_meeting_time_selector_item_unrealize (GnomeCanvasItem *item);
static void e_meeting_time_selector_item_update (GnomeCanvasItem *item,
                         double *affine,
                         ArtSVP *clip_path, gint flags);
static void e_meeting_time_selector_item_draw (GnomeCanvasItem *item,
                           GdkDrawable *drawable,
                           gint x, gint y,
                           gint width, gint height);
static double e_meeting_time_selector_item_point (GnomeCanvasItem *item,
                          double x, double y,
                          gint cx, gint cy,
                          GnomeCanvasItem **actual_item);
static gint e_meeting_time_selector_item_event (GnomeCanvasItem *item,
                        GdkEvent *event);
static gint e_meeting_time_selector_item_button_press (EMeetingTimeSelectorItem *mts_item,
                               GdkEvent *event);
static gint e_meeting_time_selector_item_button_release (EMeetingTimeSelectorItem *mts_item,
                             GdkEvent *event);
static gint e_meeting_time_selector_item_motion_notify (EMeetingTimeSelectorItem *mts_item,
                            GdkEvent *event);

static void e_meeting_time_selector_item_paint_day_top (EMeetingTimeSelectorItem *mts_item,
                            GdkDrawable *drawable,
                            GDate *date,
                            gint x, gint scroll_y,
                            gint width, gint height);
static void e_meeting_time_selector_item_paint_all_attendees_busy_periods (EMeetingTimeSelectorItem *mts_item, GdkDrawable *drawable, GDate *date, gint x, gint y, gint width, gint height);
static void e_meeting_time_selector_item_paint_day (EMeetingTimeSelectorItem *mts_item,
                            GdkDrawable *drawable,
                            GDate *date,
                            gint x, gint scroll_y,
                            gint width, gint height);
static void e_meeting_time_selector_item_paint_busy_periods (EMeetingTimeSelectorItem *mts_item, GdkDrawable *drawable, GDate *date, gint x, gint scroll_y, gint width, gint height);
static gint e_meeting_time_selector_item_find_first_busy_period (EMeetingTimeSelectorItem *mts_item, GDate *date, gint row);
static void e_meeting_time_selector_item_paint_attendee_busy_periods (EMeetingTimeSelectorItem *mts_item, GdkDrawable *drawable, gint row, gint x, gint y, gint width, gint first_period, EMeetingFreeBusyType busy_type, cairo_t *cr);

static EMeetingTimeSelectorPosition e_meeting_time_selector_item_get_drag_position (EMeetingTimeSelectorItem *mts_item, gint x, gint y);
static gboolean e_meeting_time_selector_item_calculate_busy_range (EMeetingTimeSelector *mts,
                                   gint row,
                                   gint x,
                                   gint width,
                                   gint *start_x,
                                   gint *end_x);

/* The arguments we take */
enum {
    PROP_0,
    PROP_MEETING_TIME_SELECTOR
};

G_DEFINE_TYPE (EMeetingTimeSelectorItem, e_meeting_time_selector_item, GNOME_TYPE_CANVAS_ITEM)

static void
e_meeting_time_selector_item_class_init (EMeetingTimeSelectorItemClass *class)
{
    GObjectClass *object_class;
    GnomeCanvasItemClass *item_class;

    object_class = G_OBJECT_CLASS (class);
    object_class->dispose = e_meeting_time_selector_item_dispose;
    object_class->set_property = e_meeting_time_selector_item_set_property;

    item_class = GNOME_CANVAS_ITEM_CLASS (class);
    item_class->realize = e_meeting_time_selector_item_realize;
    item_class->unrealize = e_meeting_time_selector_item_unrealize;
    item_class->update = e_meeting_time_selector_item_update;
    item_class->draw = e_meeting_time_selector_item_draw;
    item_class->point = e_meeting_time_selector_item_point;
    item_class->event = e_meeting_time_selector_item_event;

    g_object_class_install_property (
        object_class,
        PROP_MEETING_TIME_SELECTOR,
        g_param_spec_pointer (
            "meeting_time_selector",
            NULL,
            NULL,
            G_PARAM_WRITABLE));
}


static void
e_meeting_time_selector_item_init (EMeetingTimeSelectorItem *mts_item)
{
    GnomeCanvasItem *item = GNOME_CANVAS_ITEM (mts_item);

    mts_item->mts = NULL;

    mts_item->main_gc = NULL;
    mts_item->stipple_gc = NULL;

    /* Create the cursors. */
    mts_item->normal_cursor = gdk_cursor_new (GDK_LEFT_PTR);
    mts_item->resize_cursor = gdk_cursor_new (GDK_SB_H_DOUBLE_ARROW);
    mts_item->busy_cursor = gdk_cursor_new (GDK_WATCH);
    mts_item->last_cursor_set = NULL;

    item->x1 = 0;
    item->y1 = 0;
    item->x2 = 0;
    item->y2 = 0;
}


static void
e_meeting_time_selector_item_dispose (GObject *object)
{
    EMeetingTimeSelectorItem *mts_item;

    mts_item = E_MEETING_TIME_SELECTOR_ITEM (object);

    if (mts_item->normal_cursor) {
        gdk_cursor_unref (mts_item->normal_cursor);
        mts_item->normal_cursor = NULL;
    }
    if (mts_item->resize_cursor) {
        gdk_cursor_unref (mts_item->resize_cursor);
        mts_item->resize_cursor = NULL;
    }
    if (mts_item->busy_cursor) {
        gdk_cursor_unref (mts_item->busy_cursor);
        mts_item->busy_cursor = NULL;
    }

    G_OBJECT_CLASS (e_meeting_time_selector_item_parent_class)->dispose (object);
}


static void
e_meeting_time_selector_item_set_property (GObject *object,
                                           guint property_id,
                                           const GValue *value,
                                           GParamSpec *pspec)
{
    EMeetingTimeSelectorItem *mts_item;

    mts_item = E_MEETING_TIME_SELECTOR_ITEM (object);

    switch (property_id) {
    case PROP_MEETING_TIME_SELECTOR:
        mts_item->mts = g_value_get_pointer (value);
        return;
    }

    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}


static void
e_meeting_time_selector_item_realize (GnomeCanvasItem *item)
{
    GnomeCanvas *canvas;
    GdkWindow *window;
    EMeetingTimeSelectorItem *mts_item;

    if (GNOME_CANVAS_ITEM_CLASS (e_meeting_time_selector_item_parent_class)->realize)
        (*GNOME_CANVAS_ITEM_CLASS (e_meeting_time_selector_item_parent_class)->realize)(item);

    mts_item = E_MEETING_TIME_SELECTOR_ITEM (item);

    canvas = item->canvas;
    window = GTK_WIDGET (canvas)->window;

    mts_item->main_gc = gdk_gc_new (window);
    mts_item->stipple_gc = gdk_gc_new (window);
}


static void
e_meeting_time_selector_item_unrealize (GnomeCanvasItem *item)
{
    EMeetingTimeSelectorItem *mts_item;

    mts_item = E_MEETING_TIME_SELECTOR_ITEM (item);

    g_object_unref (mts_item->main_gc);
    mts_item->main_gc = NULL;
    g_object_unref (mts_item->stipple_gc);
    mts_item->stipple_gc = NULL;

    if (GNOME_CANVAS_ITEM_CLASS (e_meeting_time_selector_item_parent_class)->unrealize)
        (*GNOME_CANVAS_ITEM_CLASS (e_meeting_time_selector_item_parent_class)->unrealize)(item);
}


static void
e_meeting_time_selector_item_update (GnomeCanvasItem *item, double *affine, ArtSVP *clip_path, gint flags)
{
    if (GNOME_CANVAS_ITEM_CLASS (e_meeting_time_selector_item_parent_class)->update)
        (* GNOME_CANVAS_ITEM_CLASS (e_meeting_time_selector_item_parent_class)->update) (item, affine, clip_path, flags);

    /* The grid covers the entire canvas area. */
    item->x1 = 0;
    item->y1 = 0;
    item->x2 = INT_MAX;
    item->y2 = INT_MAX;
}


/*
 * DRAWING ROUTINES - functions to paint the canvas item.
 */

static void
e_meeting_time_selector_item_draw (GnomeCanvasItem *item, GdkDrawable *drawable, gint x, gint y, gint width, gint height)
{
    EMeetingTimeSelector *mts;
    EMeetingTimeSelectorItem *mts_item;
    EMeetingAttendee *ia;
    gint day_x, meeting_start_x, meeting_end_x, bar_y, bar_height;
    gint row, row_y, start_x, end_x;
    GDate date, last_date, current_date;
    gboolean is_display_top, show_meeting_time;
    GdkGC *gc, *stipple_gc;
    cairo_t *cr;

    mts_item = E_MEETING_TIME_SELECTOR_ITEM (item);
    mts = mts_item->mts;
    g_return_if_fail (mts != NULL);
    gc = mts_item->main_gc;
    stipple_gc = mts_item->stipple_gc;
    cr = gdk_cairo_create (drawable);

    is_display_top = (GTK_WIDGET (item->canvas) == mts->display_top)
        ? TRUE : FALSE;

    /* Calculate the first and last visible days and positions. */
    e_meeting_time_selector_calculate_day_and_position (mts, x,
                                &date, &day_x);
    e_meeting_time_selector_calculate_day_and_position (mts, x + width,
                                &last_date, NULL);

    /* For the top display draw the 'All Attendees' row background. */
    cairo_save (cr);
    if (is_display_top) {
        gdk_cairo_set_source_color (cr, &mts->all_attendees_bg_color);
        cairo_rectangle (cr, 0, mts->row_height * 2 - y,
                    width, mts->row_height);
        cairo_fill (cr);
    } else {
        gdk_cairo_set_source_color (cr, &mts->bg_color);
        cairo_rectangle (cr,  0, 0, width, height);
        cairo_fill (cr);
    }
    cairo_restore (cr);

    /* Calculate the x coordinates of the meeting time. */
    show_meeting_time = e_meeting_time_selector_get_meeting_time_positions (mts, &meeting_start_x, &meeting_end_x);

    /* Draw the meeting time background. */
    if (show_meeting_time
        && (meeting_end_x - 1 >= x) && (meeting_start_x + 1 < x + width)
        && (meeting_end_x - meeting_start_x > 2)) {
        cairo_save (cr);
        gdk_cairo_set_source_color (cr, &mts->meeting_time_bg_color);
        if (is_display_top){
            cairo_rectangle (cr, meeting_start_x + 1 - x, mts->row_height * 2 - y,
                        meeting_end_x - meeting_start_x - 2, mts->row_height);
            cairo_fill (cr);
        } else {
            cairo_rectangle (cr, meeting_start_x + 1 - x, 0,
                        meeting_end_x - meeting_start_x - 2, height);
            cairo_fill (cr);
        }
        cairo_restore (cr);
    }

    /* For the main display draw the stipple background for attendee's
       that have no calendar information. */
    if (!is_display_top) {
        gdk_cairo_set_source_color (cr, &mts->grid_color);
        gdk_gc_set_foreground (gc, &mts->grid_color);
        gdk_gc_set_foreground (stipple_gc, &mts->grid_color);
        gdk_gc_set_background (stipple_gc, &mts->stipple_bg_color);
        gdk_gc_set_stipple (stipple_gc, mts->stipple);
        gnome_canvas_set_stipple_origin (item->canvas, stipple_gc);
        gdk_gc_set_fill (stipple_gc, GDK_OPAQUE_STIPPLED);
        row = y / mts->row_height;
        row_y = row * mts->row_height - y;
        while (row < e_meeting_store_count_actual_attendees (mts->model) && row_y < height) {
            ia = e_meeting_store_find_attendee_at_row (mts->model, row);

            if (e_meeting_attendee_get_has_calendar_info (ia)) {
                if (e_meeting_time_selector_item_calculate_busy_range (mts, row, x, width, &start_x, &end_x)) {
                    if (start_x >= width || end_x <= 0) {
                        gdk_draw_rectangle (drawable, stipple_gc, TRUE, 0, row_y, width, mts->row_height);
                    } else {
                        if (start_x >= 0) {
                            gdk_draw_rectangle (drawable, stipple_gc, TRUE, 0, row_y, start_x, mts->row_height);
                            cairo_move_to (cr, start_x, row_y);
                            cairo_line_to (cr, start_x, row_y + mts->row_height);
                            cairo_stroke (cr);
                        }
                        if (end_x <= width) {
                            gdk_draw_rectangle (drawable, stipple_gc, TRUE, end_x, row_y, width - end_x, mts->row_height);
                            cairo_move_to (cr, end_x, row_y);
                            cairo_line_to (cr, end_x, row_y + mts->row_height);
                            cairo_stroke (cr);
                        }
                    }
                }
            } else {
                gdk_draw_rectangle (drawable, stipple_gc, TRUE,
                            0, row_y,
                            width, mts->row_height);
            }
            row++;
            row_y += mts->row_height;
        }
        gdk_gc_set_fill (gc, GDK_SOLID);
    }

    /* Now paint the visible days one by one. */
    current_date = date;
    for (;;) {
        /* Currently we use the same GnomeCanvasItem class for the
           top display and the main display. We may use separate
           classes in future if necessary. */
        if (is_display_top)
            e_meeting_time_selector_item_paint_day_top (mts_item, drawable, &current_date, day_x, y, width, height);
        else
            e_meeting_time_selector_item_paint_day (mts_item, drawable, &current_date, day_x, y, width, height);

        day_x += mts_item->mts->day_width;
        if (g_date_compare (&current_date, &last_date) == 0)
            break;
        g_date_add_days (&current_date, 1);
    }

    /* Draw the busy periods. */
    if (is_display_top)
        e_meeting_time_selector_item_paint_all_attendees_busy_periods (mts_item, drawable, &date, x, y, width, height);
    else
        e_meeting_time_selector_item_paint_busy_periods (mts_item, drawable, &date, x, y, width, height);


    /* Draw the currently-selected meeting time vertical bars. */
    if (show_meeting_time) {
        if (is_display_top) {
            bar_y = mts->row_height * 2 - y;
            bar_height = mts->row_height;
        } else {
            bar_y = 0;
            bar_height = height;
        }

        cairo_save (cr);
        gdk_cairo_set_source_color (cr, &mts->grid_color);

        if ((meeting_start_x + 2 >= x)
            && (meeting_start_x - 2 < x + width)) {
            cairo_rectangle (cr, meeting_start_x - 2 - x, bar_y,
                        5, bar_height);
            cairo_fill (cr);
        }

        if ((meeting_end_x + 2 >= x)
            && (meeting_end_x - 2 < x + width)) {
            cairo_rectangle (cr, meeting_end_x - 2 - x, bar_y,
                        5, bar_height);
            cairo_fill (cr);

        }
        cairo_restore (cr);
    }
    cairo_destroy (cr);
}


static void
e_meeting_time_selector_item_paint_day_top (EMeetingTimeSelectorItem *mts_item,
                        GdkDrawable *drawable, GDate *date,
                        gint x, gint scroll_y,
                        gint width, gint height)
{
    EMeetingTimeSelector *mts;
    GdkGC *gc;
    gint y, grid_x;
    gchar buffer[128], *format;
    gint hour, hour_x, hour_y;
    GdkRectangle clip_rect;
    PangoLayout *layout;

    mts = mts_item->mts;
    gc = mts_item->main_gc;

    gdk_gc_set_foreground (gc, &mts->grid_color);
    layout = gtk_widget_create_pango_layout (GTK_WIDGET (mts), NULL);

    /* Draw the horizontal lines. */
    y = mts->row_height - 1 - scroll_y;
    gdk_draw_line (drawable, gc, x, y, x + mts->day_width - 1, y);
    gdk_gc_set_foreground (gc, &mts->grid_shadow_color);
    gdk_draw_line (drawable, gc, x, y + 1, x + mts->day_width - 1, y + 1);
    gdk_gc_set_foreground (gc, &mts->grid_color);
    y += mts->row_height;
    gdk_draw_line (drawable, gc, x, y, x + mts->day_width - 1, y);
    y += mts->row_height;
    gdk_draw_line (drawable, gc, x, y, x + mts->day_width - 1, y);

    /* Draw the vertical grid lines. */
    for (grid_x = mts->col_width - 1;
         grid_x < mts->day_width - mts->col_width;
         grid_x += mts->col_width) {
        gdk_draw_line (drawable, gc,
                   x + grid_x, mts->row_height * 2 - 1 - scroll_y,
                   x + grid_x, height);
    }
    grid_x = mts->day_width - 2;
    gdk_draw_line (drawable, gc, x + grid_x, 0, x + grid_x, height);
    grid_x++;
    gdk_draw_line (drawable, gc, x + grid_x, 0, x + grid_x, height);

    /* Draw the date. Set a clipping rectangle so we don't draw over the
       next day. */
    if (mts->date_format == E_MEETING_TIME_SELECTOR_DATE_FULL)
        /* This is a strftime() format string %A = full weekday name,
           %B = full month name, %d = month day, %Y = full year. */
        format = _("%A, %B %d, %Y");
    else if (mts->date_format == E_MEETING_TIME_SELECTOR_DATE_ABBREVIATED_DAY)
        /* This is a strftime() format string %a = abbreviated weekday
           name, %m = month number, %d = month day, %Y = full year. */
        format = _("%a %m/%d/%Y");
    else
        /* This is a strftime() format string %m = month number,
           %d = month day, %Y = full year. */
        format = _("%m/%d/%Y");

    g_date_strftime (buffer, sizeof (buffer), format, date);

    clip_rect.x = x;
    clip_rect.y = -scroll_y;
    clip_rect.width = mts->day_width - 2;
    clip_rect.height = mts->row_height - 2;
    gdk_gc_set_clip_rectangle (gc, &clip_rect);
    pango_layout_set_text (layout, buffer, -1);
    gdk_draw_layout (drawable, gc,
             x + 2,
             4 - scroll_y,
             layout);
    gdk_gc_set_clip_rectangle (gc, NULL);

    /* Draw the hours. */
    hour = mts->first_hour_shown;
    hour_x = x + 2;
    hour_y = mts->row_height + 4 - scroll_y;
    while (hour < mts->last_hour_shown) {
        if (calendar_config_get_24_hour_format ())
            pango_layout_set_text (layout, EMeetingTimeSelectorHours [hour], -1);
        else
            pango_layout_set_text (layout, EMeetingTimeSelectorHours12 [hour], -1);

        gdk_draw_layout (drawable, gc,
                 hour_x,
                 hour_y,
                 layout);

        hour += mts->zoomed_out ? 3 : 1;
        hour_x += mts->col_width;
    }

    g_object_unref (layout);
}


/* This paints the colored bars representing busy periods for the combined
   list of attendees. For now we just paint the bars for each attendee of
   each other. If we want to speed it up we could optimise it later. */
static void
e_meeting_time_selector_item_paint_all_attendees_busy_periods (EMeetingTimeSelectorItem *mts_item, GdkDrawable *drawable, GDate *date, gint x, gint scroll_y, gint width, gint height)
{
    EMeetingTimeSelector *mts;
    EMeetingAttendee *ia;
    EMeetingFreeBusyType busy_type;
    gint row, y;
    GdkGC *gc;
    gint *first_periods;
    cairo_t *cr;
    guint16 red, green, blue;
    gdouble cc = 65535.0;

    mts = mts_item->mts;
    gc = mts_item->main_gc;
    cr = gdk_cairo_create (drawable);

    /* Calculate the y coordinate to paint the row at in the drawable. */
    y = 2 * mts->row_height - scroll_y - 1;

    /* Get the first visible busy periods for all the attendees. */
    first_periods = g_new (gint, e_meeting_store_count_actual_attendees (mts->model));
    for (row = 0; row < e_meeting_store_count_actual_attendees (mts->model); row++) {
        /* This is never used */
        ia = e_meeting_store_find_attendee_at_row (mts->model, row);
        first_periods[row] = e_meeting_time_selector_item_find_first_busy_period (mts_item, date, row);
    }

    for (busy_type = 0;
         busy_type < E_MEETING_FREE_BUSY_LAST;
         busy_type++) {
        gdk_gc_set_foreground (gc, &mts->busy_colors[busy_type]);
        red = mts->busy_colors[busy_type].red;
        green = mts->busy_colors[busy_type].green;
        blue = mts->busy_colors[busy_type].blue;
        cairo_set_source_rgba (cr, red/cc, green/cc, blue/cc, 0.8);
        for (row = 0; row < e_meeting_store_count_actual_attendees (mts->model); row++) {
            if (first_periods[row] == -1)
                continue;
            e_meeting_time_selector_item_paint_attendee_busy_periods (mts_item, drawable, x, y, width, row, first_periods[row], busy_type, cr);
        }
    }

    g_free (first_periods);
    cairo_destroy (cr);
}


static void
e_meeting_time_selector_item_paint_day (EMeetingTimeSelectorItem *mts_item,
                    GdkDrawable *drawable, GDate *date,
                    gint x, gint scroll_y,
                    gint width, gint height)
{
    EMeetingTimeSelector *mts;
    GdkGC *gc;
    gint grid_x, grid_y, attendee_index, unused_y;

    mts = mts_item->mts;
    gc = mts_item->main_gc;

    /* Draw the grid lines. The grid lines around unused rows are drawn in
       a different color. */

    /* Draw the horizontal grid lines. */
    attendee_index = scroll_y / mts->row_height;
#if E_MEETING_TIME_SELECTOR_DRAW_GRID_LINES_AT_BOTTOM
    for (grid_y = mts->row_height - 1 - (scroll_y % mts->row_height);
#else
    for (grid_y = - (scroll_y % mts->row_height);
#endif
         grid_y < height;
         grid_y += mts->row_height)
      {
          if (attendee_index <= e_meeting_store_count_actual_attendees (mts->model)) {
              gdk_gc_set_foreground (gc, &mts->grid_color);
              gdk_draw_line (drawable, gc, 0, grid_y,
                     width, grid_y);
          } else {
              gdk_gc_set_foreground (gc, &mts->grid_unused_color);
              gdk_draw_line (drawable, gc, 0, grid_y,
                     width, grid_y);
          }
          attendee_index++;
      }

    /* Draw the vertical grid lines. */
    unused_y = (e_meeting_store_count_actual_attendees (mts->model) * mts->row_height) - scroll_y;
    if (unused_y >= 0) {
        gdk_gc_set_foreground (gc, &mts->grid_color);
        for (grid_x = mts->col_width - 1;
             grid_x < mts->day_width - mts->col_width;
             grid_x += mts->col_width)
            {
                gdk_draw_line (drawable, gc,
                           x + grid_x, 0,
                           x + grid_x, unused_y - 1);
            }
        gdk_draw_rectangle (drawable, gc, TRUE,
                    x + mts->day_width - 2, 0,
                    2, unused_y);
    }

    if (unused_y < height) {
        gdk_gc_set_foreground (gc, &mts->grid_unused_color);
        for (grid_x = mts->col_width - 1;
             grid_x < mts->day_width - mts->col_width;
             grid_x += mts->col_width)
            {
                gdk_draw_line (drawable, gc,
                           x + grid_x, unused_y,
                           x + grid_x, height);
            }
        gdk_draw_rectangle (drawable, gc, TRUE,
                    x + mts->day_width - 2, unused_y,
                    2, height - unused_y);
    }


}


/* This paints the colored bars representing busy periods for the individual
   attendees. */
static void
e_meeting_time_selector_item_paint_busy_periods (EMeetingTimeSelectorItem *mts_item, GdkDrawable *drawable, GDate *date, gint x, gint scroll_y, gint width, gint height)
{
    EMeetingTimeSelector *mts;
    EMeetingFreeBusyType busy_type;
    gint row, y, first_period;
    GdkGC *gc;
    cairo_t *cr;
    guint16 red, green, blue;
    gdouble cc = 65535.0;

    mts = mts_item->mts;
    gc = mts_item->main_gc;
    cr = gdk_cairo_create (drawable);

    /* Calculate the first visible attendee row. */
    row = scroll_y / mts->row_height;

    /* Calculate the y coordinate to paint the row at in the drawable. */
    y = row * mts->row_height - scroll_y;

    /* Step through the attendees painting the busy periods. */
    while (y < height && row < e_meeting_store_count_actual_attendees (mts->model)) {

        /* Find the first visible busy period. */
        first_period = e_meeting_time_selector_item_find_first_busy_period (mts_item, date, row);
        if (first_period != -1) {
            /* Paint the different types of busy periods, in
               reverse order of precedence, so the highest
               precedences are displayed. */
            for (busy_type = 0;
                 busy_type < E_MEETING_FREE_BUSY_LAST;
                 busy_type++) {
                gdk_gc_set_foreground (gc, &mts->busy_colors[busy_type]);
                red = mts->busy_colors[busy_type].red;
                green = mts->busy_colors[busy_type].green;
                blue = mts->busy_colors[busy_type].blue;
                cairo_set_source_rgba (cr, red/cc, green/cc, blue/cc, 0.8);
                e_meeting_time_selector_item_paint_attendee_busy_periods (mts_item, drawable, x, y, width, row, first_period, busy_type, cr);
            }
        }
        y += mts->row_height;
        row++;
    }
    cairo_destroy (cr);
}


/* This subtracts the attendees longest_period_in_days from the given date,
   and does a binary search of the attendee's busy periods array to find the
   first one which could possible end on the given day or later.
   If none are found it returns -1. */
static gint
e_meeting_time_selector_item_find_first_busy_period (EMeetingTimeSelectorItem *mts_item, GDate *date, gint row)
{
    EMeetingTimeSelector *mts;
    EMeetingAttendee *ia;
    const GArray *busy_periods;
    EMeetingFreeBusyPeriod *period;
    gint period_num;

    mts = mts_item->mts;

    ia = e_meeting_store_find_attendee_at_row (mts->model, row);

    period_num = e_meeting_attendee_find_first_busy_period (ia, date);
    if (period_num == -1)
        return -1;

    /* Check if the period starts after the end of the current canvas
       scroll area. */
    busy_periods = e_meeting_attendee_get_busy_periods (ia);
    period = &g_array_index (busy_periods, EMeetingFreeBusyPeriod, period_num);
    if (g_date_compare (&mts->last_date_shown, &period->start.date) < 0)
        return -1;

    return period_num;
}


/* This paints the visible busy periods for one attendee which are of a certain
   busy type, e.g out of office. It is passed the index of the first visible
   busy period of the attendee and continues until it runs off the screen. */
static void
e_meeting_time_selector_item_paint_attendee_busy_periods (EMeetingTimeSelectorItem *mts_item, GdkDrawable *drawable, gint x, gint y, gint width, gint row, gint first_period, EMeetingFreeBusyType busy_type, cairo_t *cr)
{
    EMeetingTimeSelector *mts;
    EMeetingAttendee *ia;
    const GArray *busy_periods;
    EMeetingFreeBusyPeriod *period;
    GdkGC *gc;
    gint period_num, x1, x2, x2_within_day, x2_within_col;

    mts = mts_item->mts;
    gc = mts_item->main_gc;

    ia = e_meeting_store_find_attendee_at_row (mts->model, row);

    busy_periods = e_meeting_attendee_get_busy_periods (ia);
    for (period_num = first_period;
         period_num < busy_periods->len;
         period_num++) {
        period = &g_array_index (busy_periods, EMeetingFreeBusyPeriod, period_num);

        if (period->busy_type != busy_type)
            continue;

        /* Convert the period start and end times to x coordinates. */
        x1 = e_meeting_time_selector_calculate_time_position (mts, &period->start);
        /* If the period is off the right of the area being drawn, we
           are finished. */
        if (x1 >= x + width)
            return;

        x2 = e_meeting_time_selector_calculate_time_position (mts, &period->end);
        /* If the period is off the left edge of the area skip it. */
        if (x2 <= x)
            continue;

        /* We paint from x1 to x2 - 1, so that for example a time
           from 5:00-6:00 is distinct from 6:00-7:00.
           We never finish on a grid line separating days, and we only
           ever paint on a normal vertical grid line if the period is
           only 1 pixel wide. */
        x2_within_day = x2 % mts->day_width;
        if (x2_within_day == 0) {
            x2 -= 2;
        } else if (x2_within_day == mts->day_width - 1) {
            x2 -= 1;
        } else {
            x2_within_col = x2_within_day % mts->col_width;
            if (x2_within_col == 0 && x2 > x1 + 1)
                x2 -= 1;
        }

        /* Paint the rectangle. We leave a gap of 2 pixels at the
         top and bottom, remembering that the grid is painted along
         the top/bottom line of each row. */
        if (x2 - x1 > 0) {
#if E_MEETING_TIME_SELECTOR_DRAW_GRID_LINES_AT_BOTTOM
            cairo_rectangle (cr, x1 - x, y + 2,
                        x2 - x1, mts->row_height - 5);
#else
            cairo_rectangle (cr, x1 - x, y + 3,
                        x2 - x1, mts->row_height - 5);
#endif
            cairo_fill (cr);
        }
    }
}


/*
 * CANVAS ITEM ROUTINES - functions to be a GnomeCanvasItem.
 */

/* This is supposed to return the nearest item the the point and the distance.
   Since we are the only item we just return ourself and 0 for the distance.
   This is needed so that we get button/motion events. */
static double
e_meeting_time_selector_item_point (GnomeCanvasItem *item, double x, double y,
                    gint cx, gint cy,
                    GnomeCanvasItem **actual_item)
{
    *actual_item = item;
    return 0.0;
}


static gint
e_meeting_time_selector_item_event (GnomeCanvasItem *item, GdkEvent *event)
{
    EMeetingTimeSelectorItem *mts_item;

    mts_item = E_MEETING_TIME_SELECTOR_ITEM (item);

    switch (event->type) {
    case GDK_BUTTON_PRESS:
        return e_meeting_time_selector_item_button_press (mts_item,
                                  event);
    case GDK_BUTTON_RELEASE:
        return e_meeting_time_selector_item_button_release (mts_item,
                                    event);
    case GDK_MOTION_NOTIFY:
        return e_meeting_time_selector_item_motion_notify (mts_item,
                                   event);
    default:
        break;
    }

    return FALSE;
}


/* This handles all button press events for the item. If the cursor is over
   one of the meeting time vertical bars we start a drag. If not we set the
   meeting time to the nearest half-hour interval.
   Note that GnomeCanvas converts the event coords to world coords,
   i.e. relative to the entire canvas scroll area. */
static gint
e_meeting_time_selector_item_button_press (EMeetingTimeSelectorItem *mts_item,
                       GdkEvent *event)
{
    EMeetingTimeSelector *mts;
    EMeetingTime start_time, end_time;
    EMeetingTimeSelectorPosition position;
    GDate *start_date, *end_date;
    gint x, y;

    mts = mts_item->mts;
    x = (gint) event->button.x;
    y = (gint) event->button.y;

    /* Check if we are starting a drag of the vertical meeting time bars.*/
    position = e_meeting_time_selector_item_get_drag_position (mts_item,
                                   x, y);
    if (position != E_MEETING_TIME_SELECTOR_POS_NONE) {
        if (gnome_canvas_item_grab (GNOME_CANVAS_ITEM (mts_item),
                        GDK_POINTER_MOTION_MASK
                        | GDK_BUTTON_RELEASE_MASK,
                        mts_item->resize_cursor,
                        event->button.time) == 0 /*Success*/) {
            mts->dragging_position = position;
            return TRUE;
        }
    }

    /* Convert the x coordinate into a EMeetingTimeSelectorTime. */
    e_meeting_time_selector_calculate_time (mts, x, &start_time);
    start_date = &start_time.date;
    end_date = &end_time.date;

    /* Find the nearest half-hour or hour interval, depending on whether
       zoomed_out is set. */
    if (!mts->all_day) {
        gint astart_year, astart_month, astart_day, astart_hour, astart_minute;
        gint aend_year, aend_month, aend_day, aend_hour, aend_minute;
        gint hdiff, mdiff;
        GDate asdate, aedate;

        e_meeting_time_selector_get_meeting_time (mts_item->mts,
                           &astart_year,
                           &astart_month,
                           &astart_day,
                           &astart_hour,
                           &astart_minute,
                           &aend_year,
                           &aend_month,
                           &aend_day,
                           &aend_hour,
                           &aend_minute);
        if (mts->zoomed_out)
            start_time.minute = 0;
        else
            start_time.minute -= start_time.minute % 30;

        g_date_set_dmy (&asdate, astart_day, astart_month, astart_year);
        g_date_set_dmy (&aedate, aend_day, aend_month, aend_year);
        end_time = start_time;
        mdiff = end_time.minute + aend_minute - astart_minute;
        hdiff = end_time.hour + aend_hour - astart_hour + (24 * g_date_days_between (&asdate, &aedate));
        while (mdiff < 0) {
            mdiff += 60;
            hdiff -= 1;
        }
        while (mdiff > 60) {
            mdiff -= 60;
            hdiff += 1;
        }
        while (hdiff < 0) {
            hdiff += 24;
            g_date_subtract_days (end_date, 1);
        }
        while (hdiff >= 24) {
            hdiff -= 24;
            g_date_add_days (end_date, 1);
        }
        end_time.minute = mdiff;
        end_time.hour = hdiff;
    } else {
        start_time.hour = 0;
        start_time.minute = 0;
        end_time = start_time;
        g_date_add_days (&end_time.date, 1);
    }

    /* Fix any overflows. */
    e_meeting_time_selector_fix_time_overflows (&end_time);

    /* Set the new meeting time. */
    e_meeting_time_selector_set_meeting_time (mts_item->mts,
                          g_date_get_year (start_date),
                          g_date_get_month (start_date),
                          g_date_get_day (start_date),
                          start_time.hour,
                          start_time.minute,
                          g_date_get_year (end_date),
                          g_date_get_month (end_date),
                          g_date_get_day (end_date),
                          end_time.hour,
                          end_time.minute);


    return FALSE;
}


/* This handles all button release events for the item. If we were dragging,
   we finish the drag. */
static gint
e_meeting_time_selector_item_button_release (EMeetingTimeSelectorItem *mts_item,
                         GdkEvent *event)
{
    EMeetingTimeSelector *mts;

    mts = mts_item->mts;

    /* Reset any drag. */
    if (mts->dragging_position != E_MEETING_TIME_SELECTOR_POS_NONE) {
        mts->dragging_position = E_MEETING_TIME_SELECTOR_POS_NONE;
        e_meeting_time_selector_remove_timeout (mts);
        gnome_canvas_item_ungrab (GNOME_CANVAS_ITEM (mts_item),
                      event->button.time);
    }

    return FALSE;
}


/* This handles all motion notify events for the item. If button1 is pressed
   we check if a drag is in progress. If not, we set the cursor if we are over
   the meeting time vertical bars. Note that GnomeCanvas doesn't use motion
   hints, which may affect performance. */
static gint
e_meeting_time_selector_item_motion_notify (EMeetingTimeSelectorItem *mts_item,
                        GdkEvent *event)
{
    EMeetingTimeSelector *mts;
    EMeetingTimeSelectorPosition position;
    GdkCursor *cursor;
    gint x, y;

    mts = mts_item->mts;
    x = (gint) event->motion.x;
    y = (gint) event->motion.y;

    if (mts->dragging_position != E_MEETING_TIME_SELECTOR_POS_NONE) {
        e_meeting_time_selector_drag_meeting_time (mts, x);
        return TRUE;
    }

    position = e_meeting_time_selector_item_get_drag_position (mts_item,
                                   x, y);

    /* Determine which cursor should be used. */
    if (position != E_MEETING_TIME_SELECTOR_POS_NONE)
        cursor = mts_item->resize_cursor;
    /* If the Main window shows busy cursor show the same */
    else if (mts_item->mts->last_cursor_set == GDK_WATCH)
        cursor = mts_item->busy_cursor;
    else
        cursor = mts_item->normal_cursor;

    /* Only set the cursor if it is different to the last one we set. */
    if (mts_item->last_cursor_set != cursor) {
        mts_item->last_cursor_set = cursor;
        gdk_window_set_cursor (GTK_WIDGET (GNOME_CANVAS_ITEM (mts_item)->canvas)->window, cursor);
    }

    return FALSE;
}


static EMeetingTimeSelectorPosition
e_meeting_time_selector_item_get_drag_position (EMeetingTimeSelectorItem *mts_item,
                        gint x, gint y)
{
    EMeetingTimeSelector *mts;
    gboolean is_display_top;
    gint meeting_start_x, meeting_end_x;

    mts = mts_item->mts;

    is_display_top = (GTK_WIDGET (GNOME_CANVAS_ITEM (mts_item)->canvas) == mts->display_top) ? TRUE : FALSE;

    if (is_display_top && y < mts->row_height * 2)
        return E_MEETING_TIME_SELECTOR_POS_NONE;

    if (!e_meeting_time_selector_get_meeting_time_positions (mts, &meeting_start_x, &meeting_end_x))
        return E_MEETING_TIME_SELECTOR_POS_NONE;

    if (x >= meeting_end_x - 2 && x <= meeting_end_x + 2)
        return E_MEETING_TIME_SELECTOR_POS_END;

    if (x >= meeting_start_x - 2 && x <= meeting_start_x + 2)
        return E_MEETING_TIME_SELECTOR_POS_START;

    return E_MEETING_TIME_SELECTOR_POS_NONE;
}


static gboolean
e_meeting_time_selector_item_calculate_busy_range (EMeetingTimeSelector *mts,
                           gint row,
                           gint x,
                           gint width,
                           gint *start_x,
                           gint *end_x)
{
    EMeetingAttendee *ia;
    EMeetingTime busy_periods_start;
    EMeetingTime busy_periods_end;

    ia = e_meeting_store_find_attendee_at_row (mts->model, row);
    busy_periods_start = e_meeting_attendee_get_start_busy_range (ia);
    busy_periods_end = e_meeting_attendee_get_end_busy_range (ia);

    *start_x = -1;
    *end_x = -1;

    if (!g_date_valid (&busy_periods_start.date)
        || !g_date_valid (&busy_periods_end.date))
        return FALSE;

    *start_x = e_meeting_time_selector_calculate_time_position (mts, &busy_periods_start) - x - 1;

    *end_x = e_meeting_time_selector_calculate_time_position (mts, &busy_periods_end) - x;

    return TRUE;
}

void
e_meeting_time_selector_item_set_normal_cursor (EMeetingTimeSelectorItem *mts_item)
{
    g_return_if_fail (IS_E_MEETING_TIME_SELECTOR_ITEM (mts_item));

    gdk_window_set_cursor (GTK_WIDGET (GNOME_CANVAS_ITEM (mts_item)->canvas)->window, mts_item->normal_cursor);
}