home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 4.2J (Developer) / os42jdev.iso / NextDeveloper / Examples / AppKit / ToDo / CalendarMatrix.m < prev    next >
Text File  |  1996-03-26  |  5KB  |  167 lines

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