/*
- Copyright (C) 2000-2005 SKYRIX Software AG
+ Copyright (C) 2000-2008 SKYRIX Software AG
+ Copyright (C) 2008 Helge Hess
This file is part of SOPE.
WOAssociation *labelColor;
WOAssociation *contentColor;
-
+
@private
- NSMutableArray *matrix[MatrixSize];
+ /* Note: the matrix does NOT start at the 1st of a month! (the first row
+ * contains dates from the previous month, unless the 1st is Monday)
+ */
+ NSMutableArray *matrix[MatrixSize]; /* 42 mutable arrays (for each day) */
struct {
int firstDisplayedDay; // first day to be displayed Sun 0 .. Sat 6
tz = [self->timeZone valueInComponent:comp];
monthStart = [NSCalendarDate dateWithYear:y month:m day:1 hour:0 minute:0
- second:0 timeZone:tz];
+ second:0 timeZone:tz];
d = [monthStart dateByAddingYears:0 months:0 days:i];
d = [monthStart dateByAddingYears:0 months:0 days:i];
}
- firstDisplayedDay = (self->firstDay)
+ firstDisplayedDay = (self->firstDay != nil)
? ([self->firstDay intValueInComponent:comp] % 7)
: 1; // Monday
- firstIdx = (([monthStart dayOfWeek]-firstDisplayedDay)+7) % 7;
+ firstIdx = (([monthStart dayOfWeek] - firstDisplayedDay) + 7) % 7;
self->matrixInfo.weeks = ceil((float)(firstIdx + i) / 7);
self->matrixInfo.firstDisplayedDay = firstDisplayedDay;
// keep the timezone in the date
self->matrixInfo.start =
[[monthStart dateByAddingYears:0 months:0 days:-firstIdx] retain];
+
+#if HEAVY_DEBUG
+ NSLog(@"MONTH START: %@", monthStart);
+ NSLog(@"MATRIX START: %@", self->matrixInfo.start);
+#endif
}
+
+ /*
+ * The 'matrix start' is the day the grid starts. Eg if we are displaying
+ * Feb 2008, Feb 1st does not start on Monday. The matrix looks like:
+ *
+ * Week Mon Tue Wed Thu Fri Sat Sun
+ * 5 28 29 30 31 1 2 3 [starts in January]
+ * 6 4 5 6 7 8 9 10
+ * 7 11 12 13 14 15 16 17
+ * 8 18 19 20 21 22 23 24
+ * 9 25 26 27 28 29 1 2 [goes into march]
+ *
+ * Hence the matrix start will be 2008-01-28 00:00 [TZ].
+ */
for (i = 0, cnt = [array count]; i < cnt; i++) {
id app;
NSCalendarDate *sd, *ed;
- NSTimeInterval diff;
+ NSTimeInterval diff; // seconds since matrixInfo.start (1. 00:00)
int idx, idx2;
-
- app = [array objectAtIndex:i];
+ NSNumber *iNum;
+
+ app = [array objectAtIndex:i]; // appointment
sd = [app valueForKey:startKey]; // startDate
ed = [app valueForKey:endKey]; // endDate
-
+
if (sd == nil && ed == nil) continue;
-
+
+ /* startdate */
diff = [sd timeIntervalSinceDate:self->matrixInfo.start];
+ idx = floor(diff / SecondsPerDay); /* eg 0 */
+#if HEAVY_DEBUG
+ NSLog(@" DTSTART: %i (offset=%.3fs)", idx, diff);
+#endif
- idx = floor(diff / SecondsPerDay);
-
- if (0 <= idx && idx < MatrixSize) {
+ if (idx >= 0 && idx < MatrixSize) {
if (self->matrix[idx] == nil)
self->matrix[idx] = [[NSMutableArray alloc] initWithCapacity:4];
[self->matrix[idx] addObject:[NSNumber numberWithInt:i]];
}
idx = (idx < 0) ? 0 : idx + 1;
-
+
+ /* enddate */
diff = [ed timeIntervalSinceDate:self->matrixInfo.start];
idx2 = floor(diff / SecondsPerDay);
- idx2 = (idx2 > MatrixSize) ? MatrixSize : idx2;
+#if HEAVY_DEBUG
+ NSLog(@" DTEND: %i (offset=%.3fs)", idx2, diff);
+#endif
+ idx2 = (idx2 > MatrixSize) ? MatrixSize : idx2; /* limit length */
- while (idx < idx2) {
+ /* Add event to all columns covered. we store the indices! */
+ iNum = [[NSNumber alloc] initWithInt:i];
+ for (; idx <= idx2; idx++) { /* inclusive! */
+#if HEAVY_DEBUG
+ NSLog(@" ADD %i TO: %i", i, idx);
+#endif
if (self->matrix[idx] == nil)
self->matrix[idx] = [[NSMutableArray alloc] initWithCapacity:4];
-
- [self->matrix[idx] addObject:[NSNumber numberWithInt:i]];
- idx++;
+
+ [self->matrix[idx] addObject:iNum];
}
+ [iNum release]; iNum = nil;
}
-
+
+ /* fill up remaining slots with empty arrays */
for (i = 0; i < MatrixSize; i++) {
if (self->matrix[i] == nil)
self->matrix[i] = [[NSArray alloc] init];