aboutsummaryrefslogtreecommitdiffstats
path: root/calendar/gui/e-week-view-layout.c
blob: c2089a7f915944d87eb5028c5c3f9544abf1c000 (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */

/*
 * Author :
 *  Damon Chaplin <damon@ximian.com>
 *
 * Copyright 2001, Ximian, Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General Public
 * License as published by the Free Software Foundation.
 *
 * 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
 * USA
 */

/*
 * Lays out events for the Week & Month views of the calendar. It is also
 * used for printing.
 */

#include <config.h>

#include "e-week-view-layout.h"


static void e_week_view_layout_event    (EWeekViewEvent *event,
                     guint8     *grid,
                     GArray     *spans,
                     GArray     *old_spans,
                     gboolean    multi_week_view,
                     gint        weeks_shown,
                     gboolean    compress_weekend,
                     gint        start_weekday,
                     time_t     *day_starts,
                     gint       *rows_per_day);
static gint e_week_view_find_day    (time_t      time_to_find,
                     gboolean    include_midnight_in_prev_day,
                     gint        days_shown,
                     time_t     *day_starts);
static gint e_week_view_find_span_end   (gboolean    multi_week_view,
                     gboolean    compress_weekend,
                     gint        display_start_day,
                     gint        day);


GArray*
e_week_view_layout_events   (GArray     *events,
                 GArray     *old_spans,
                 gboolean    multi_week_view,
                 gint        weeks_shown,
                 gboolean    compress_weekend,
                 gint        start_weekday,
                 time_t     *day_starts,
                 gint       *rows_per_day)
{
    EWeekViewEvent *event;
    EWeekViewEventSpan *span;
    gint num_days, day, event_num, span_num;
    guint8 *grid;
    GArray *spans;

    /* This is a temporary 2-d grid which is used to place events.
       Each element is 0 if the position is empty, or 1 if occupied.
       We allocate the maximum size possible here, assuming that each
       event will need its own row. */
    grid = g_new0 (guint8, E_WEEK_VIEW_MAX_ROWS_PER_CELL * 7
               * E_WEEK_VIEW_MAX_WEEKS);

    /* We create a new array of spans, which will replace the old one. */
    spans = g_array_new (FALSE, FALSE, sizeof (EWeekViewEventSpan));

    /* Clear the number of rows used per day. */
    num_days = multi_week_view ? weeks_shown * 7 : 7;
    for (day = 0; day < num_days; day++) {
        rows_per_day[day] = 0;
    }

    /* Iterate over the events, finding which weeks they cover, and putting
       them in the first free row available. */
    for (event_num = 0; event_num < events->len; event_num++) {
        event = &g_array_index (events, EWeekViewEvent, event_num);
        e_week_view_layout_event (event, grid, spans, old_spans,
                      multi_week_view,
                      weeks_shown, compress_weekend,
                      start_weekday, day_starts,
                      rows_per_day);
    }

    /* Free the grid. */
    g_free (grid);

    /* Destroy the old spans array, destroying any unused canvas items. */
    if (old_spans) {
        for (span_num = 0; span_num < old_spans->len; span_num++) {
            span = &g_array_index (old_spans, EWeekViewEventSpan,
                           span_num);
            if (span->background_item)
                gtk_object_destroy (GTK_OBJECT (span->background_item));
            if (span->text_item)
                gtk_object_destroy (GTK_OBJECT (span->text_item));
        }
        g_array_free (old_spans, TRUE);
    }

    return spans;
}


static void
e_week_view_layout_event    (EWeekViewEvent *event,
                 guint8     *grid,
                 GArray     *spans,
                 GArray     *old_spans,
                 gboolean    multi_week_view,
                 gint        weeks_shown,
                 gboolean    compress_weekend,
                 gint        start_weekday,
                 time_t     *day_starts,
                 gint       *rows_per_day)
{
    gint start_day, end_day, span_start_day, span_end_day, rows_per_cell;
    gint free_row, row, day, span_num, spans_index, num_spans, days_shown;
    EWeekViewEventSpan span, *old_span;

    days_shown = multi_week_view ? weeks_shown * 7 : 7;
    start_day = e_week_view_find_day (event->start, FALSE, days_shown,
                      day_starts);
    end_day = e_week_view_find_day (event->end, TRUE, days_shown,
                    day_starts);
    start_day = CLAMP (start_day, 0, days_shown - 1);
    end_day = CLAMP (end_day, 0, days_shown - 1);

#if 0
    g_print ("In e_week_view_layout_event Start:%i End: %i\n",
         start_day, end_day);
#endif

    /* Iterate through each of the spans of the event, where each span
       is a sequence of 1 or more days displayed next to each other. */
    span_start_day = start_day;
    rows_per_cell = E_WEEK_VIEW_MAX_ROWS_PER_CELL;
    span_num = 0;
    spans_index = spans->len;
    num_spans = 0;
    while (span_start_day <= end_day) {
        span_end_day = e_week_view_find_span_end (multi_week_view,
                              compress_weekend,
                              start_weekday,
                              span_start_day);
        span_end_day = MIN (span_end_day, end_day);
#if 0
        g_print ("  Span start:%i end:%i\n", span_start_day,
             span_end_day);
#endif
        /* Try each row until we find a free one or we fall off the
           bottom of the available rows. */
        row = 0;
        free_row = -1;
        while (free_row == -1 && row < rows_per_cell) {
            free_row = row;
            for (day = span_start_day; day <= span_end_day;
                 day++) {
                if (grid[day * rows_per_cell + row]) {
                    free_row = -1;
                    break;
                }
            }
            row++;
        };

        if (free_row != -1) {
            /* Mark the cells as full. */
            for (day = span_start_day; day <= span_end_day;
                 day++) {
                grid[day * rows_per_cell + free_row] = 1;
                rows_per_day[day] = MAX (rows_per_day[day],
                             free_row + 1);
            }
#if 0
            g_print ("  Span start:%i end:%i row:%i\n",
                 span_start_day, span_end_day, free_row);
#endif
            /* Add the span to the array, and try to reuse any
               canvas items from the old spans. */
            span.start_day = span_start_day;
            span.num_days = span_end_day - span_start_day + 1;
            span.row = free_row;
            span.background_item = NULL;
            span.text_item = NULL;
            if (event->num_spans > span_num) {
                old_span = &g_array_index (old_spans, EWeekViewEventSpan, event->spans_index + span_num);
                span.background_item = old_span->background_item;
                span.text_item = old_span->text_item;
                old_span->background_item = NULL;
                old_span->text_item = NULL;
            }

            g_array_append_val (spans, span);
            num_spans++;
        }

        span_start_day = span_end_day + 1;
        span_num++;
    }

    /* Set the event's spans. */
    event->spans_index = spans_index;
    event->num_spans = num_spans;
}


/* Finds the day containing the given time.
   If include_midnight_in_prev_day is TRUE then if the time exactly
   matches the start of a day the previous day is returned. This is useful
   when calculating the end day of an event. */
static gint
e_week_view_find_day        (time_t      time_to_find,
                 gboolean    include_midnight_in_prev_day,
                 gint        days_shown,
                 time_t     *day_starts)
{
    gint day;

    if (time_to_find < day_starts[0])
        return -1;
    if (time_to_find > day_starts[days_shown])
        return days_shown;

    for (day = 1; day <= days_shown; day++) {
        if (time_to_find <= day_starts[day]) {
            if (time_to_find == day_starts[day]
                && !include_midnight_in_prev_day)
                return day;
            return day - 1;
        }
    }

    g_assert_not_reached ();
    return days_shown;
}


/* This returns the last possible day in the same span as the given day.
   A span is all the days which are displayed next to each other from left to
   right. In the week view all spans are only 1 day, since Tuesday is below
   Monday rather than beside it etc. In the month view, if the weekends are not
   compressed then each week is a span, otherwise we have to break a span up
   on Saturday, use a separate span for Sunday, and start again on Monday. */
static gint
e_week_view_find_span_end   (gboolean    multi_week_view,
                 gboolean    compress_weekend,
                 gint        display_start_day,
                 gint        day)
{
    gint week, col, sat_col, end_col;

    if (multi_week_view) {
        week = day / 7;
        col = day % 7;

        /* We default to the last column in the row. */
        end_col = 6;

        /* If the weekend is compressed we must end any spans on
           Saturday and Sunday. */
        if (compress_weekend) {
            sat_col = (5 + 7 - display_start_day) % 7;
            if (col <= sat_col)
                end_col = sat_col;
            else if (col == sat_col + 1)
                end_col = sat_col + 1;
        }

        return week * 7 + end_col;
    } else {
        return day;
    }
}


void
e_week_view_layout_get_day_position (gint        day,
                     gboolean    multi_week_view,
                     gint        weeks_shown,
                     gint        display_start_day,
                     gboolean    compress_weekend,
                     gint       *day_x,
                     gint       *day_y,
                     gint       *rows)
{
    gint week, day_of_week, row, col, weekend_col, box, weekend_box;

    *day_x = *day_y = *rows = 0;
    g_return_if_fail (day >= 0);

    if (multi_week_view) {
        g_return_if_fail (day < weeks_shown * 7);

        week = day / 7;
        col = day % 7;
        day_of_week = (display_start_day + day) % 7;
        if (compress_weekend && day_of_week >= 5) {
            /* In the compressed view Saturday is above Sunday and
               both have just one row as opposed to 2 for all the
               other days. */
            if (day_of_week == 5) {
                *day_y = week * 2;
                *rows = 1;
            } else {
                *day_y = week * 2 + 1;
                *rows = 1;
                col--;
            }
            /* Both Saturday and Sunday are in the same column. */
            *day_x = col;
        } else {
            /* If the weekend is compressed and the day is after
               the weekend we have to move back a column. */
            if (compress_weekend) {
                /* Calculate where the weekend column is.
                   Note that 5 is Saturday. */
                weekend_col = (5 + 7 - display_start_day) % 7;
                if (col > weekend_col)
                    col--;
            }

            *day_y = week * 2;
            *rows = 2;
            *day_x = col;
        }
    } else {
        g_return_if_fail (day < 7);

        /* Calculate which box to place the day in, from 0-5.
           Note that in the week view the weekends are always
           compressed and share a box. */
        box = day;
        day_of_week = (display_start_day + day) % 7;
        weekend_box = (5 + 7 - display_start_day) % 7;
        if (box > weekend_box)
            box--;

        if (box < 3)
            *day_x = 0;
        else
            *day_x = 1;

        row = (box % 3) * 2;
        if (day_of_week < 5) {
            *day_y = row;
            *rows = 2;
        } else if (day_of_week == 5) {
            /* Saturday. */
            *day_y = row;
            *rows = 1;

        } else {
            /* Sunday. */
            *day_y = row + 1;
            *rows = 1;
        }
    }
}


/* Returns TRUE if the event span is visible or FALSE if it isn't.
   It also returns the number of days of the span that are visible.
   Usually this can easily be determined by the start & end days and row of
   the span, which are set in e_week_view_layout_event(). Though we need a
   special case for the weekends when they are compressed, since the span may
   not fit. */
gboolean
e_week_view_layout_get_span_position    (EWeekViewEvent *event,
                     EWeekViewEventSpan *span,
                     gint        rows_per_cell,
                     gint        rows_per_compressed_cell,
                     gint        display_start_day,
                     gboolean    multi_week_view,
                     gboolean    compress_weekend,
                     gint       *span_num_days)
{
    gint end_day_of_week;

    if (span->row >= rows_per_cell)
        return FALSE;

    end_day_of_week = (display_start_day + span->start_day
               + span->num_days - 1) % 7;
    *span_num_days = span->num_days;
    /* Check if the row will not be visible in compressed cells. */
    if (span->row >= rows_per_compressed_cell) {
        if (multi_week_view) {
            if (compress_weekend) {
                /* If it ends on a Saturday and is 1 day long
                   we skip it, else we shorten it. If it ends
                   on a Sunday it must be 1 day long and we
                   skip it. */
                if (end_day_of_week == 5) {    /* Sat */
                    if (*span_num_days == 1) {
                        return FALSE;
                    } else {
                        (*span_num_days)--;
                    }
                } else if (end_day_of_week == 6) { /* Sun */
                    return FALSE;
                }
            }
        } else {
            /* All spans are 1 day long in the week view, so we
               just skip it. */
            if (end_day_of_week > 4)
                return FALSE;
        }
    }

    return TRUE;
}