home *** CD-ROM | disk | FTP | other *** search
/ Super Net 1 / SUPERNET_1.iso / PC / OTROS / UNIX / ARCHIE / CLIENTS / NEXTARCH / CALENDAR.M < prev    next >
Encoding:
Text File  |  1992-01-19  |  4.2 KB  |  142 lines

  1. // CalendarView.m
  2. // By Jayson Adams, NeXT Developer Support Team
  3. // You may freely copy, distribute and reuse the code in this example.
  4. // NeXT disclaims any warranty of any kind, expressed or implied, as to its
  5. // fitness for any particular use.
  6. /* Modified by Scott Stark, Fri Jan 17 1992 */
  7.  
  8. #import <math.h>
  9. #import <appkit/NXImage.h>
  10.  
  11. #import "CalendarView.h"
  12. #import <Date.h>
  13.  
  14. @implementation CalendarView
  15.  
  16.  
  17. /* instance methods */
  18.  
  19. - initFrame : (NXRect *) frameRect
  20. {
  21. int i;
  22. char *numberNames[] = {"Zero", "One", "Two", "Three", "Four", "Five",
  23.                 "Six", "Seven", "Eight", "Nine", ""},
  24. *smallNumberNames[] = {"SmallZero", "SmallOne", "SmallTwo", "SmallThree", "SmallFour",
  25.                 "SmallFive","SmallSix", "SmallSeven", "SmallEight", "SmallNine", ""},
  26. *dayNames[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",""},
  27. *monthNames[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
  28.                 "Aug", "Sep", "Oct", "Nov", "Dec", ""};
  29.               
  30.     [super initFrame:frameRect];
  31.     
  32.     /* find all of the images */
  33.     calendarImage = [NXImage findImageNamed:"YearCalendar"];
  34.     
  35.     for (i = 0; *numberNames[i]; i++)
  36.     {
  37.         numbers[i] = [NXImage findImageNamed: numberNames[i]];
  38.         smallNumbers[i] = [NXImage findImageNamed: smallNumberNames[i]];
  39.     }
  40.  
  41.     for (i = 0; *dayNames[i]; i++) 
  42.         days[i] = [NXImage findImageNamed: dayNames[i]];
  43.     
  44.     for (i = 0; *monthNames[i]; i++) 
  45.         months[i] = [NXImage findImageNamed: monthNames[i]];
  46.     
  47.     /* Initialize the Date object to the current date */
  48.     dateObj = [[Date alloc] initFromNow];
  49.  
  50.     return self;
  51. }
  52.  
  53. - setDate : (short) day : (short) month : (short) year
  54. {
  55.     if([dateObj initFromDate : day : month : year] == nil)
  56.         return nil;
  57.     [self display];
  58.     return self;
  59. }
  60.  
  61. - today
  62. {
  63.     if([dateObj initFromNow] == nil)
  64.         return nil;
  65.     [self display];
  66.     return self;
  67. }
  68.  
  69. - drawSelf:(NXRect *)rects :(int)count
  70. {
  71. NXSize calendarSize, unitSize, tenSize, size;
  72. NXSize thousandSize,hundredSize;
  73. NXPoint position;
  74. float numberWidth,yOffset = 9.0;
  75. short day,month,year,dayOfWeek;
  76. short thousands,hundreds,tens,ones;
  77.  
  78.     day = [dateObj day];
  79.     month = [dateObj month];
  80.     year = [dateObj year];
  81.     dayOfWeek = [dateObj weekDay];
  82.  
  83.     [calendarImage getSize:&calendarSize];
  84.  
  85. /* draw the background */
  86.     [calendarImage composite:NX_COPY toPoint:&(bounds.origin)];
  87.  
  88. /* we want to center the date on the calendar;  compute its location */
  89.     [numbers[day % 10] getSize:&unitSize];
  90.     numberWidth = unitSize.width;
  91.     if (day / 10)
  92.     {
  93.         [numbers[day / 10] getSize:&tenSize];
  94.         numberWidth += tenSize.width;
  95.     }
  96.     position.x = floor((calendarSize.width - numberWidth) / 2.0) - 1.0;
  97.     position.y = 14.0 + yOffset;
  98.  
  99. /* draw the date */
  100.     if (day / 10)
  101.     {
  102.         [numbers[day / 10] composite:NX_COPY toPoint:&position];
  103.         position.x += tenSize.width;
  104.     }
  105.     [numbers[day % 10] composite:NX_COPY toPoint:&position];
  106.  
  107. /* draw the day */
  108.     [days[dayOfWeek] getSize:&size];
  109.     position.x = floor((calendarSize.width - size.width) / 2.0) - 2.0;
  110.     position.y = 33.0 + yOffset;
  111.     [days[dayOfWeek] composite:NX_COPY toPoint:&position];
  112.  
  113. /* draw the month */
  114.     [months[month] getSize:&size];
  115.     position.x = floor((calendarSize.width - size.width) / 2.0) - 2.0;
  116.     position.y = 8.0 + yOffset;
  117.     [months[month] composite:NX_COPY toPoint:&position];
  118.  
  119. /* Draw the year */
  120.     thousands = year / 1000;
  121.     hundreds = (year - 1000 * thousands) / 100;
  122.     tens = (year - 1000 * thousands - 100 * hundreds) / 10;
  123.     ones = (year - 1000 * thousands - 100 * hundreds - 10 * tens);
  124.     [smallNumbers[thousands] getSize: &thousandSize];
  125.     [smallNumbers[hundreds] getSize: &hundredSize];
  126.     [smallNumbers[tens] getSize: &tenSize];
  127.     [smallNumbers[ones] getSize: &unitSize];
  128.     numberWidth = thousandSize.width + hundredSize.width + tenSize.width + unitSize.width;
  129.     position.x = floor((calendarSize.width - numberWidth) / 2.0) - 2.0;
  130.     position.y = 1.0;
  131.     [smallNumbers[thousands] composite: NX_COPY toPoint: &position];
  132.     position.x += thousandSize.width;
  133.     [smallNumbers[hundreds] composite: NX_COPY toPoint: &position];
  134.     position.x += hundredSize.width;
  135.     [smallNumbers[tens] composite: NX_COPY toPoint: &position];
  136.     position.x += tenSize.width;
  137.     [smallNumbers[ones] composite: NX_COPY toPoint: &position];
  138.  
  139.     return self;
  140. }
  141.  
  142. @end