-
Notifications
You must be signed in to change notification settings - Fork 3
/
CalendarView.m
executable file
·236 lines (188 loc) · 6.75 KB
/
CalendarView.m
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
#import "CalendarView.h"
#import "GNUstepGUI/GSTheme.h"
@implementation CalendarView
#define isLeapYear(year) (((year % 4) == 0 && ((year % 100) != 0)) || (year % 400) == 0)
static short numberOfDaysInMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
+ (NSSize) size
{
return NSMakeSize(240, 270);
}
- (NSCalendarDate *) date
{
return date;
}
- (void) setDate: (NSCalendarDate *) newDate
{
int i, currentDay, currentMonth, currentYear;
int daysInMonth, startDayOfWeek, day;
NSCalendarDate *firstDayOfMonth;
NSButtonCell *tempCell;
ASSIGN(date, newDate);
[yearLabel setStringValue: [date descriptionWithCalendarFormat: @"%Y"]];
currentMonth = [date monthOfYear];
[monthMatrix selectCellWithTag: currentMonth-1];
currentYear = [date yearOfCommonEra];
firstDayOfMonth = [NSCalendarDate dateWithYear: currentYear
month: currentMonth
day: 1
hour: 0
minute: 0
second: 0
timeZone: [NSTimeZone localTimeZone]];
daysInMonth = numberOfDaysInMonth[currentMonth - 1];
if ((currentMonth == 2) && (isLeapYear(currentYear)))
daysInMonth++;
startDayOfWeek = [firstDayOfMonth dayOfWeek];
day = 1;
for (i = 0; i < 42; i++)
{
tempCell = [dayMatrix cellWithTag: i];
if (i < startDayOfWeek || i >= (daysInMonth + startDayOfWeek))
{
[tempCell setEnabled: NO];
[tempCell setTitle: @""];
}
else
{
[tempCell setEnabled: YES];
[tempCell setTitle: [NSString stringWithFormat: @"%d", day++]];
}
}
currentDay = [date dayOfMonth];
[dayMatrix selectCellWithTag: startDayOfWeek + currentDay - 1];
}
- (id) initWithFrame: (NSRect) rect
{
int i, j, count=0;
NSImage *rightArrow, *leftArrow;
NSButtonCell *monthCell, *dayCell, *tempCell;
NSArray *weekArray;
[super initWithFrame: rect];
calendarBox = [[NSBox alloc] initWithFrame: NSMakeRect(0, 0, 240, 220)];
[calendarBox setBorderType: NSNoBorder];
[calendarBox setTitlePosition: NSAtTop];
[calendarBox setTitle: _(@"Calendar")];
yearLabel = [[NSTextField alloc] initWithFrame: NSMakeRect(85, 170, 60, 20)];
[yearLabel setStringValue: @"This Year"];
[yearLabel setBezeled: NO];
[yearLabel setBackgroundColor: [[GSTheme theme] menuItemBackgroundColor]];
[yearLabel setEditable: NO];
[yearLabel setSelectable: NO];
[yearLabel setAlignment: NSCenterTextAlignment];
leftArrow = [NSImage imageNamed: @"common_ArrowLeft.tiff"];
rightArrow = [NSImage imageNamed: @"common_ArrowRight.tiff"];
lastYearButton = [[NSButton alloc] initWithFrame: NSMakeRect(10, 170, 22, 22)];
[lastYearButton setImage: leftArrow];
[lastYearButton setImagePosition: NSImageOnly];
[lastYearButton setBordered: NO];
nextYearButton = [[NSButton alloc] initWithFrame: NSMakeRect(198, 170, 22, 22)];
[nextYearButton setImage: rightArrow];
[nextYearButton setImagePosition: NSImageOnly];
[nextYearButton setBordered: NO];
[lastYearButton setTarget: self];
[lastYearButton setAction: @selector(updateDate:)];
[nextYearButton setTarget: self];
[nextYearButton setAction: @selector(updateDate:)];
[calendarBox addSubview: yearLabel];
[calendarBox addSubview: lastYearButton];
[calendarBox addSubview: nextYearButton];
RELEASE(yearLabel);
RELEASE(lastYearButton);
RELEASE(nextYearButton);
monthArray = [[NSArray alloc] initWithObjects:
_(@"Jan"), _(@"Feb"), _(@"Mar"),_(@"Apr"), _(@"May"), _(@"Jun"),
_(@"Jul"), _(@"Aug"), _(@"Sep"), _(@"Oct"), _(@"Nov"), _(@"Dec"), nil];
monthCell = [[NSButtonCell alloc] initTextCell: @""];
[monthCell setBordered: NO];
[monthCell setShowsStateBy: NSOnOffButton];
[monthCell setAlignment: NSCenterTextAlignment];
monthMatrix = [[NSMatrix alloc] initWithFrame: NSMakeRect(10, 130, 220, 40)
mode: NSRadioModeMatrix
prototype: monthCell
numberOfRows: 2
numberOfColumns: 6];
for (i = 0; i < 2; i++)
for (j = 0; j < 6; j++)
{
tempCell = [monthMatrix cellAtRow: i column: j];
[tempCell setTag: count];
[tempCell setTitle: [monthArray objectAtIndex: count]];
count++;
}
RELEASE(monthCell);
weekArray = [NSArray arrayWithObjects: _(@"Sun"), _(@"Mon"), _(@"Tue"), _(@"Wed"),
_(@"Thr"), _(@"Fri"), _(@"Sat"), nil];
dayCell = [[NSButtonCell alloc] initTextCell: @""];
[dayCell setBordered: NO];
[dayCell setShowsStateBy: NSOnOffButton];
[dayCell setAlignment: NSCenterTextAlignment];
dayMatrix = [[NSMatrix alloc] initWithFrame: NSMakeRect(10, 10, 210, 110)
mode: NSRadioModeMatrix
prototype: dayCell
numberOfRows: 7
numberOfColumns: 7];
for (j = 0; j < 7; j++)
{
tempCell = [dayMatrix cellAtRow: 0 column: j];
[tempCell setTitle: [weekArray objectAtIndex: j]];
[tempCell setAlignment: NSCenterTextAlignment];
[tempCell setEnabled: NO];
}
RELEASE(dayCell);
count = 0;
for (i = 1; i < 7; i++)
for (j = 0; j < 7; j++)
{
[[dayMatrix cellAtRow: i column: j] setTag: count++];
}
[monthMatrix setTarget: self];
[monthMatrix setAction: @selector(updateDate:)];
[dayMatrix setTarget: self];
[dayMatrix setAction: @selector(updateDate:)];
[calendarBox addSubview: monthMatrix];
[calendarBox addSubview: dayMatrix];
RELEASE(monthMatrix);
RELEASE(dayMatrix);
[self addSubview: calendarBox];
RELEASE(calendarBox);
return self;
}
- (void) updateDate: (id) sender
{
int i=0, j=0, k=0;
NSCalendarDate *newDate;
if (sender == lastYearButton)
{
i = -1;
}
else if (sender == nextYearButton)
{
i = 1;
}
else if (sender == monthMatrix)
{
j = [[[sender selectedCells] lastObject] tag] + 1 - [date monthOfYear];
NSLog(@"Mois : %i",j);
}
else if (sender == dayMatrix)
{
k = [[[[sender selectedCells] lastObject] stringValue] intValue] - [date dayOfMonth];
/* NSLog(@" Day : %i", [date dayOfMonth]);
NSLog(@"intValue : %i", [[[sender selectedCells] lastObject] intValue]);
NSLog(@"Jour : %i", k);*/ //FIXME : wrong intValue
}
newDate = [date addYear: i
month: j
day: k
hour: 0
minute: 0
second: 0];
[self setDate: newDate];
}
- (void) dealloc
{
DESTROY(date);
DESTROY(monthArray);
[super dealloc];
}
@end