home *** CD-ROM | disk | FTP | other *** search
/ OpenStep (Enterprise) / OpenStepENTCD.toast / OEDEV / DEV.Z / CalendarMatrix.m < prev    next >
Text File  |  1996-07-30  |  6KB  |  172 lines

  1. /*
  2.   You may freely copy, distribute, and reuse the code in this example.
  3.   NeXT disclaims any warranty of any kind, expressed or implied, as to its
  4.   fitness for any particular use.
  5. */
  6.  
  7. #import "ToDoDoc.h"
  8. #import "ToDoController.h"
  9. #import "ToDoItem.h"
  10. #import "CalendarMatrix.h"
  11.  
  12. static short MonthDays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  13. #define isLeap(year) (((((year) % 4) == 0 && (((year) % 100) != 0)) || ((year) % 400) == 0))
  14.  
  15. @implementation CalendarMatrix
  16.  
  17. - (void)refreshCalendar
  18. {
  19.     NSCalendarDate *firstOfMonth, *selDate = [self selectedDay], *now = [NSCalendarDate date];
  20.     int i, j, currentMonth = [selDate monthOfYear];
  21.     unsigned int currentYear = [selDate yearOfCommonEra];
  22.     short daysInMonth;
  23.     id cell;
  24.  
  25.     firstOfMonth = [NSCalendarDate dateWithYear:currentYear
  26.                                           month:currentMonth
  27.                                             day:1 hour:0 minute:0 second:0
  28.                                        timeZone:[NSTimeZone localTimeZone]];
  29.     [monthName setStringValue:[firstOfMonth descriptionWithCalendarFormat:@"%B %Y"]];
  30.     daysInMonth = MonthDays[currentMonth-1]+1;
  31.     /* correct Feb for leap year */
  32.     if ((currentMonth == 2) && (isLeap(currentYear))) daysInMonth++;
  33.     startOffset = [firstOfMonth dayOfWeek];
  34.  
  35.     /* "remove" leading cells */
  36.     for (i=0; i<startOffset; i++) {
  37.         cell = [self cellWithTag:i];
  38.         [cell setBordered:NO];
  39.         [cell setEnabled:NO];
  40.         [cell setTitle:@""];
  41.         [cell setCellAttribute:NSCellHighlighted to:NO];
  42.     }
  43.     for (j=1; j < daysInMonth; i++, j++) {
  44.         cell = [self cellWithTag:i];
  45.         [cell setBordered:YES];
  46.         [cell setEnabled:YES];
  47.         [cell setFont:[NSFont systemFontOfSize:12]];
  48.         [cell setTitle:[NSString stringWithFormat:@"%d", j]];
  49.         [cell setCellAttribute:NSCellHighlighted to:NO];
  50.     }
  51.     for (;i<42;i++) {
  52.         cell = [self cellWithTag:i];
  53.         [cell setBordered:NO];
  54.         [cell setEnabled:NO];
  55.         [cell setTitle:@""];
  56.         [cell setCellAttribute:NSCellHighlighted to:NO];
  57.     }
  58.        if ((currentYear == [now yearOfCommonEra])
  59.            && (currentMonth == [now monthOfYear])) {
  60.         [[self cellWithTag:([now dayOfMonth]+startOffset)-1]
  61.         setCellAttribute:NSCellHighlighted to:YES];
  62.         [[self cellWithTag:([now dayOfMonth]+startOffset)-1]
  63.         setHighlightsBy:NSMomentaryChangeButton];
  64.        }
  65. }
  66.    
  67. - (void)monthChanged:sender
  68. {
  69.     NSCalendarDate *thisDate = [self selectedDay];
  70.     int currentYear = [thisDate yearOfCommonEra];
  71.     unsigned int currentMonth = [thisDate monthOfYear];
  72.     
  73.     if (sender == rightButton) {
  74.         if (currentMonth == 12) {
  75.             currentMonth = 1;
  76.             currentYear++;
  77.         } else {
  78.             currentMonth++;
  79.         }
  80.     } else {
  81.         if (currentMonth == 1) {
  82.             currentMonth = 12;
  83.             currentYear--;
  84.         } else {
  85.             currentMonth--;
  86.         }
  87.     }
  88.     [self setSelectedDay: [NSCalendarDate dateWithYear:currentYear
  89.                                                   month:currentMonth
  90.                            day:1 hour:0 minute:0 second:0
  91.                                                timeZone:[NSTimeZone localTimeZone]]];
  92.     [self refreshCalendar];
  93.     [[self delegate] calendarMatrix:self didChangeToMonth:currentMonth year:currentYear];
  94. }
  95.  
  96. - (void)awakeFromNib
  97. {
  98.     [monthName setAlignment:NSCenterTextAlignment];
  99.     [self setTarget:self];
  100.     [self setAction:@selector(choseDay:)];
  101.     [self setAutosizesCells:YES];
  102.     [self refreshCalendar];
  103. }
  104.  
  105. - (id)initWithFrame:(NSRect)frameRect
  106. {
  107.     int i, j, cnt=0;
  108.     id cell = [[NSButtonCell alloc] initTextCell:@""];
  109.     NSCalendarDate *now = [NSCalendarDate date];
  110.  
  111.     [super initWithFrame:frameRect
  112.                     mode:NSRadioModeMatrix
  113.                prototype:cell
  114.             numberOfRows:6
  115.          numberOfColumns:7];
  116.     // set cell tags
  117.     for (i=0; i<6; i++) {
  118.         for (j=0; j<7; j++) {
  119.             [[self cellAtRow:i column:j] setTag:cnt++];
  120.         }
  121.     }
  122.     [cell release];
  123.     selectedDay = [[NSCalendarDate dateWithYear:[now yearOfCommonEra]
  124.                                           month:[now monthOfYear]
  125.                                             day:[now dayOfMonth]
  126.                                            hour:0 minute:0 second:0
  127.                                        timeZone:[NSTimeZone localTimeZone]]
  128.         copy];
  129.  
  130.     return self;
  131. }
  132.  
  133.  
  134. - (void)dealloc
  135. {
  136.     [selectedDay release];
  137.     [super dealloc];
  138. }
  139.  
  140.  
  141. - (void)choseDay:sender
  142. {
  143.     NSCalendarDate *selDate, *thisDate = [self selectedDay];
  144.     unsigned int selDay = [[self selectedCell] tag]-startOffset+1;
  145.  
  146.     // compose date 
  147.     selDate = [NSCalendarDate dateWithYear:[thisDate yearOfCommonEra]
  148.                                      month:[thisDate monthOfYear]
  149.          day:selDay hour:0 minute:0 second:0
  150.                                   timeZone:[NSTimeZone localTimeZone]];
  151.     [[self cellWithTag:[thisDate dayOfMonth]+startOffset-1] setFont:[NSFont systemFontOfSize:12]];
  152.     [[self cellWithTag:selDay+startOffset-1] setFont:[NSFont boldSystemFontOfSize:12]];
  153.  
  154.     [self setSelectedDay:selDate];
  155.     [[self delegate] calendarMatrix:self didChangeToDate:selDate];
  156. }
  157.  
  158. - (void)setSelectedDay:(NSCalendarDate *)newDay
  159. {
  160.     if (selectedDay)
  161.         [selectedDay autorelease];
  162.     if (newDay) {
  163.         selectedDay = [newDay copy];
  164.     } else {
  165.         selectedDay = nil;
  166.     }      
  167. }
  168.  
  169. - (NSCalendarDate *)selectedDay { return selectedDay; }
  170.  
  171. @end
  172.