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

/*
 * Author :
 *  Damon Chaplin <damon@ximian.com>
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 * 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 Day & Work-Week views of the calendar. It is also
 * used for printing.
 */

#include <config.h>

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

static void e_day_view_layout_long_event (EDayViewEvent   *event,
                      guint8      *grid,
                      gint         days_shown,
                      time_t      *day_starts,
                      gint        *rows_in_top_display);

static void e_day_view_layout_day_event (EDayViewEvent    *event,
                     guint8           *grid,
                     guint16      *group_starts,
                     guint8           *cols_per_row,
                     gint              rows,
                     gint              mins_per_row);
static void e_day_view_expand_day_event (EDayViewEvent    *event,
                     guint8           *grid,
                     guint8           *cols_per_row,
                     gint              mins_per_row);
static void e_day_view_recalc_cols_per_row (gint           rows,
                        guint8    *cols_per_row,
                        guint16       *group_starts);


void
e_day_view_layout_long_events (GArray      *events,
                   gint    days_shown,
                   time_t     *day_starts,
                   gint    *rows_in_top_display)
{
    EDayViewEvent *event;
    gint event_num;
    guint8 *grid;

    /* 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, events->len * E_DAY_VIEW_MAX_DAYS);

    /* Reset the number of rows in the top display to 0. It will be
       updated as events are layed out below. */
    *rows_in_top_display = 0;

    /* Iterate over the events, finding which days 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, EDayViewEvent, event_num);
        e_day_view_layout_long_event (event, grid,
                          days_shown, day_starts,
                          rows_in_top_display);
    }

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


static void
e_day_view_layout_long_event (EDayViewEvent *event,
                  guint8       *grid,
                  gint      days_shown,
                  time_t       *day_starts,
                  gint     *rows_in_top_display)
{
    gint start_day, end_day, free_row, day, row;

    event->num_columns = 0;

    if (!e_day_view_find_long_event_days (event,
                          days_shown, day_starts,
                          &start_day, &end_day))
        return;

    /* Try each row until we find a free one. */
    row = 0;
    do {
        free_row = row;
        for (day = start_day; day <= end_day; day++) {
            if (grid[row * E_DAY_VIEW_MAX_DAYS + day]) {
                free_row = -1;
                break;
            }
        }
        row++;
    } while (free_row == -1);

    event->start_row_or_col = free_row;
    event->num_columns = 1;

    /* Mark the cells as full. */
    for (day = start_day; day <= end_day; day++) {
        grid[free_row * E_DAY_VIEW_MAX_DAYS + day] = 1;
    }

    /* Update the number of rows in the top canvas if necessary. */
    *rows_in_top_display = MAX (*rows_in_top_display, free_row + 1);
}


void
e_day_view_layout_day_events (GArray       *events,
                  gint      rows,
                  gint      mins_per_row,
                  guint8       *cols_per_row)
{
    EDayViewEvent *event;
    gint row, event_num;
    guint8 *grid;

    /* This is a temporary array which keeps track of rows which are
       connected. When an appointment spans multiple rows then the number
       of columns in each of these rows must be the same (i.e. the maximum
       of all of them). Each element in the array corresponds to one row
       and contains the index of the first row in the group of connected
       rows. */
    guint16 group_starts[12 * 24];

    /* Reset the cols_per_row array, and initialize the connected rows so
       that all rows are not connected - each row is the start of a new
       group. */
    for (row = 0; row < rows; row++) {
        cols_per_row[row] = 0;
        group_starts[row] = row;
    }

    /* 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. */
    grid = g_new0 (guint8, rows * E_DAY_VIEW_MAX_COLUMNS);


    /* Iterate over the events, finding which rows they cover, and putting
       them in the first free column available. Increment the number of
       events in each of the rows it covers, and make sure they are all
       in one group. */
    for (event_num = 0; event_num < events->len; event_num++) {
        event = &g_array_index (events, EDayViewEvent, event_num);

        e_day_view_layout_day_event (event, grid, group_starts,
                         cols_per_row, rows, mins_per_row);
    }

    /* Recalculate the number of columns needed in each row. */
    e_day_view_recalc_cols_per_row (rows, cols_per_row, group_starts);

    /* Iterate over the events again, trying to expand events horizontally
       if there is enough space. */
    for (event_num = 0; event_num < events->len; event_num++) {
        event = &g_array_index (events, EDayViewEvent, event_num);
        e_day_view_expand_day_event (event, grid, cols_per_row,
                         mins_per_row);
    }

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


/* Finds the first free position to place the event in.
   Increments the number of events in each of the rows it covers, and makes
   sure they are all in one group. */
static void
e_day_view_layout_day_event (EDayViewEvent *event,
                 guint8    *grid,
                 guint16       *group_starts,
                 guint8    *cols_per_row,
                 gint       rows,
                 gint       mins_per_row)
{
    gint start_row, end_row, free_col, col, row, group_start;

    start_row = event->start_minute / mins_per_row;
    end_row = (event->end_minute - 1) / mins_per_row;
    if (end_row < start_row)
        end_row = start_row;

    event->num_columns = 0;

    /* If the event can't currently be seen, just return. */
    if (start_row >= rows || end_row < 0)
        return;

    /* Make sure we don't go outside the visible times. */
    start_row = CLAMP (start_row, 0, rows - 1);
    end_row = CLAMP (end_row, 0, rows - 1);

    /* Try each column until we find a free one. */
    for (col = 0; col < E_DAY_VIEW_MAX_COLUMNS; col++) {
        free_col = col;
        for (row = start_row; row <= end_row; row++) {
            if (grid[row * E_DAY_VIEW_MAX_COLUMNS + col]) {
                free_col = -1;
                break;
            }
        }

        if (free_col != -1)
            break;
    }

    /* If we can't find space for the event, just return. */
    if (free_col == -1)
        return;

    /* The event is assigned 1 col initially, but may be expanded later. */
    event->start_row_or_col = free_col;
    event->num_columns = 1;

    /* Determine the start index of the group. */
    group_start = group_starts[start_row];

    /* Increment number of events in each of the rows the event covers.
       We use the cols_per_row array for this. It will be sorted out after
       all the events have been layed out. Also make sure all the rows that
       the event covers are in one group. */
    for (row = start_row; row <= end_row; row++) {
        grid[row * E_DAY_VIEW_MAX_COLUMNS + free_col] = 1;
        cols_per_row[row]++;
        group_starts[row] = group_start;
    }

    /* If any following rows should be in the same group, add them. */
    for (row = end_row + 1; row < rows; row++) {
        if (group_starts[row] > end_row)
            break;
        group_starts[row] = group_start;
    }
}


/* For each group of rows, find the max number of events in all the
   rows, and set the number of cols in each of the rows to that. */
static void
e_day_view_recalc_cols_per_row (gint      rows,
                guint8   *cols_per_row,
                guint16  *group_starts)
{
    gint start_row = 0, row, next_start_row, max_events;

    while (start_row < rows) {
        max_events = 0;
        for (row = start_row; row < rows && group_starts[row] == start_row; row++)
            max_events = MAX (max_events, cols_per_row[row]);

        next_start_row = row;

        for (row = start_row; row < next_start_row; row++)
            cols_per_row[row] = max_events;

        start_row = next_start_row;
    }
}


/* Expands the event horizontally to fill any free space. */
static void
e_day_view_expand_day_event (EDayViewEvent *event,
                 guint8    *grid,
                 guint8    *cols_per_row,
                 gint       mins_per_row)
{
    gint start_row, end_row, col, row;
    gboolean clashed;

    start_row = event->start_minute / mins_per_row;
    end_row = (event->end_minute - 1) / mins_per_row;
    if (end_row < start_row)
        end_row = start_row;

    /* Try each column until we find a free one. */
    clashed = FALSE;
    for (col = event->start_row_or_col + 1; col < cols_per_row[start_row]; col++) {
        for (row = start_row; row <= end_row; row++) {
            if (grid[row * E_DAY_VIEW_MAX_COLUMNS + col]) {
                clashed = TRUE;
                break;
            }
        }

        if (clashed)
            break;

        event->num_columns++;
    }
}


/* Find the start and end days for the event. */
gboolean
e_day_view_find_long_event_days (EDayViewEvent  *event,
                 gint        days_shown,
                 time_t     *day_starts,
                 gint       *start_day_return,
                 gint       *end_day_return)
{
    gint day, start_day, end_day;

    start_day = -1;
    end_day = -1;

    for (day = 0; day < days_shown; day++) {
        if (start_day == -1
            && event->start < day_starts[day + 1])
            start_day = day;
        if (event->end > day_starts[day])
            end_day = day;
    }

    /* Sanity check. */
    if (start_day < 0 || start_day >= days_shown
        || end_day < 0 || end_day >= days_shown
        || end_day < start_day) {
        g_warning ("Invalid date range for event");
        return FALSE;
    }

    *start_day_return = start_day;
    *end_day_return = end_day;

    return TRUE;
}