home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1994 June / NEBULA_SE.ISO / SourceCode / MiscKit / Palettes / MiscClockPalette / MiscClockView.subproj / MiscClockView.m < prev    next >
Encoding:
Text File  |  1993-10-01  |  10.2 KB  |  362 lines

  1. //
  2. //    MiscClockView.m -- a simple view class for displaying date/time
  3. //        Written by Scott Anguish (c) 1993 by Scott Anguish.
  4. //                Version 1.0.  All rights reserved.
  5. //
  6. //        This notice may not be removed from this source code.
  7. //
  8. //    This object is included in the MiscKit by permission from the author
  9. //    and its use is governed by the MiscKit license, found in the file
  10. //    "LICENSE.rtf" in the MiscKit distribution.  Please refer to that file
  11. //    for a list of all applicable permissions and restrictions.
  12. //    
  13.  
  14. #import <sys/time.h>    
  15.  
  16. #import "MiscClockView.h"
  17.  
  18. @interface MiscClockView(private)
  19. - _setUp;
  20. @end
  21.  
  22. @implementation MiscClockView
  23.  
  24.  
  25. #define Misc_BuildImage(a, b, c, d, e)     NXSetRect(theRect, (b), (c), (d), (e)); \
  26.     a = [[NXImage alloc] initFromImage:theImage rect:theRect];
  27.  
  28.  
  29.  
  30. - _setUp
  31. {
  32.     char buf[MAXPATHLEN+1]="";
  33.     NXRect    rectStorage,*theRect=&rectStorage;
  34.     NXSize    oneSize,twoSize;
  35.  
  36.     if (!theImage) {
  37.         [[NXBundle bundleForClass:[self class]] getPath:buf forResource:"MiscClockStuff" ofType:"tiff"];
  38.         theImage =[[NXImage alloc] initFromFile:buf ];
  39.         }
  40.  
  41.  
  42.     /*    Yes this is ugly, and there is alot of extra junk in here, but to a large degree at this point
  43.         I didn't know where else to store these co-ordinates where I wouldn't loose them! */
  44.     
  45.     Misc_BuildImage(imageA, 65.0, 0.0, 9.0, 11.0);
  46.     Misc_BuildImage(imageP, 74.0, 0.0, 9.0, 11.0);
  47.     Misc_BuildImage(DigitalNumbers[1], 89.0, 0.0, 4.0, 11.0); 
  48.     Misc_BuildImage(DigitalNumbers[2], 93.0, 0.0, 9.0, 11.0); 
  49.     Misc_BuildImage(DigitalNumbers[3], 104.0, 0.0, 8.0, 11.0); 
  50.     Misc_BuildImage(DigitalNumbers[4], 112.0, 0.0, 9.0, 11.0); 
  51.     Misc_BuildImage(DigitalNumbers[5], 121.0, 0.0, 9.0, 11.0); 
  52.     Misc_BuildImage(DigitalNumbers[6], 130.0, 0.0, 9.0, 11.0); 
  53.     Misc_BuildImage(DigitalNumbers[7], 140.0, 0.0, 8.0, 11.0); 
  54.     Misc_BuildImage(DigitalNumbers[8], 148.0, 0.0, 9.0, 11.0); 
  55.     Misc_BuildImage(DigitalNumbers[9], 157.0, 0.0, 9.0, 11.0); 
  56.     Misc_BuildImage(DigitalNumbers[0], 166.0, 0.0, 9.0, 11.0); 
  57.     Misc_BuildImage(imageColon, 175.0, 0.0, 3.0, 11.0); 
  58.     Misc_BuildImage(imageAM, 179.0, 0.0, 12.0, 6.0); 
  59.     Misc_BuildImage(imagePM, 192.0, 0.0, 11.0, 6.0); 
  60.                             
  61.     Misc_BuildImage(DayNames[1], 0, 65, 19, 5);
  62.     Misc_BuildImage(DayNames[2], 0, 59, 19, 5);
  63.     Misc_BuildImage(DayNames[3], 0, 53, 19, 5);
  64.     Misc_BuildImage(DayNames[4], 0, 47, 19, 5);
  65.     Misc_BuildImage(DayNames[5], 0, 41, 19, 5);
  66.     Misc_BuildImage(DayNames[6], 0, 35, 19, 5);
  67.     Misc_BuildImage(DayNames[0], 0, 29, 19, 5);
  68.                             
  69.     Misc_BuildImage(MonthNames[0],  40, 66, 22, 5);
  70.     Misc_BuildImage(MonthNames[1],  40, 60, 22, 5);
  71.     Misc_BuildImage(MonthNames[2],  40, 54, 22, 5);
  72.     Misc_BuildImage(MonthNames[3],  40, 48, 22, 5);
  73.     Misc_BuildImage(MonthNames[4],  40, 42, 22, 5);
  74.     Misc_BuildImage(MonthNames[5],  40, 36, 22, 5);
  75.     Misc_BuildImage(MonthNames[6],  40, 30, 22, 5);
  76.     Misc_BuildImage(MonthNames[7],  40, 24, 22, 5);
  77.     Misc_BuildImage(MonthNames[8],  40, 18, 22, 5);
  78.     Misc_BuildImage(MonthNames[9],  40, 12, 22, 5);
  79.     Misc_BuildImage(MonthNames[10], 40, 6, 22, 5);
  80.     Misc_BuildImage(MonthNames[11], 40, 0, 22, 5);
  81.  
  82.     Misc_BuildImage(DateNumbers[0], 172.0, 14.0, 10.0, 17.0);
  83.     Misc_BuildImage(DateNumbers[1], 65.0, 14.0, 8.0, 17.0);
  84.     Misc_BuildImage(DateNumbers[2], 76.0, 14.0, 10.0, 17.0);
  85.     Misc_BuildImage(DateNumbers[3], 88.0, 14.0, 9.0, 17.0);
  86.     Misc_BuildImage(DateNumbers[4], 100.0, 14.0, 10.0, 17.0);
  87.     Misc_BuildImage(DateNumbers[5], 112.0, 14.0, 10.0, 17.0);
  88.     Misc_BuildImage(DateNumbers[6], 124.0, 14.0, 10.0, 17.0);
  89.     Misc_BuildImage(DateNumbers[7], 136.0, 14.0, 10.0, 17.0);
  90.     Misc_BuildImage(DateNumbers[8], 148.0, 14.0, 10.0, 17.0);
  91.     Misc_BuildImage(DateNumbers[9], 160.0, 14.0, 10.0, 17.0);
  92.     
  93.     Misc_BuildImage(YearNumbers[0], 12.0, 8.0, 6.0, 5.0);
  94.     Misc_BuildImage(YearNumbers[1], 0.0, 20.0, 2.0, 5.0);
  95.     Misc_BuildImage(YearNumbers[2], 2.0, 20.0, 5.0, 5.0);
  96.     Misc_BuildImage(YearNumbers[3], 7.0, 20.0, 5.0, 5.0);
  97.     Misc_BuildImage(YearNumbers[4], 12.0, 20.0, 5.0, 5.0);
  98.     Misc_BuildImage(YearNumbers[5], 0.0, 14.0, 5.0, 5.0);
  99.     Misc_BuildImage(YearNumbers[6], 5.0, 14.0, 6.0, 5.0);
  100.     Misc_BuildImage(YearNumbers[7], 11.0, 14.0, 5.0, 5.0);
  101.     Misc_BuildImage(YearNumbers[8], 0.0, 8.0, 6.0, 5.0);
  102.     Misc_BuildImage(YearNumbers[9], 6.0, 8.0, 6.0, 5.0);
  103.     Misc_BuildImage(YearColon,       18.0, 25.0, 1.0, 5.0);
  104.     
  105.     Misc_BuildImage(clockbase, 64, 72, 64, 64);
  106.                             
  107.                         
  108.  
  109.     [DigitalNumbers[1] getSize:&oneSize];
  110.     [DigitalNumbers[2] getSize:&twoSize];
  111.     colonPosition.x = oneSize.width + twoSize.width + 7.0;
  112.     return self;
  113. }
  114.  
  115.  
  116. - initFrame:(const NXRect *)frameRect
  117. {
  118.         
  119.     [super initFrame:frameRect];
  120.     
  121.     [self _setUp];
  122.     return self;
  123.  
  124. };
  125.  
  126. - awake
  127. {
  128.     id ret = [super awake];
  129.     [self _setUp];
  130.     return ret;
  131. }
  132.  
  133. - awakeFromNib
  134. {
  135.     id ret = [super awake];
  136.     [self _setUp];
  137.     return ret;
  138. }
  139.  
  140. - drawSelf:(const NXRect *)rects :(int)rectCount
  141. {
  142.     NXPoint imageOrigin;
  143.     NXSize size, theSize, tensSize = {0.0, 0.0}, onesSize;
  144.     id imageAyear,imageByear,imageCyear,imageDyear;
  145.     float width,widthA,widthB,widthC;
  146.     float imageAdjust;
  147.     int    currentYear;
  148.     
  149.     if (showYear) 
  150.         {
  151.         imageAdjust=5;
  152.         colonPosition.y = 51;
  153.         } else 
  154.         {
  155.         imageAdjust=0;
  156.         colonPosition.y = 46;
  157.         }
  158.  
  159.  
  160.     imageOrigin.x=0; 
  161.     imageOrigin.y=0+imageAdjust;         //(0)
  162.     [clockbase composite:NX_COPY toPoint:&imageOrigin];
  163.  
  164.  
  165.     [imageAM getSize:&size];
  166.     imageOrigin.x = 44;
  167.     imageOrigin.y = 48+imageAdjust;// (48)
  168.     if (meridian) 
  169.         {
  170.             imageOrigin.x = 45;
  171.             [imagePM composite:NX_COPY toPoint:&imageOrigin];
  172.         }
  173.         else [imageAM composite:NX_COPY toPoint:&imageOrigin];
  174.     
  175.     imageOrigin = colonPosition;
  176.     if (hours / 10)  [DigitalNumbers[hours / 10] getSize:&tensSize];
  177.     
  178.     
  179.     [DigitalNumbers[hours % 10] getSize:&onesSize];
  180.     imageOrigin.x -= tensSize.width + onesSize.width;
  181.     
  182.     if (hours / 10) {
  183.     [DigitalNumbers[hours / 10] composite:NX_COPY toPoint:&imageOrigin];
  184.     imageOrigin.x += tensSize.width;
  185.     }
  186.     
  187.     [DigitalNumbers[hours % 10] composite:NX_COPY toPoint:&imageOrigin]; imageOrigin.x += onesSize.width;
  188.     
  189.     [[imageColon getSize:&size] composite:NX_COPY toPoint:&imageOrigin]; imageOrigin.x += size.width;
  190.     
  191.     [[DigitalNumbers[minutes / 10] getSize:&size] composite:NX_COPY toPoint:&imageOrigin];
  192.     imageOrigin.x += size.width;
  193.     [DigitalNumbers[minutes % 10] composite:NX_COPY toPoint:&imageOrigin];
  194.  
  195.     imageOrigin.x = 21;
  196.     imageOrigin.y = 34+imageAdjust; // (34)
  197.     [DayNames[weekday] composite:NX_COPY toPoint:&imageOrigin];
  198.     
  199.     imageOrigin.x = 19;
  200.     imageOrigin.y = 9+imageAdjust; //(9)
  201.     [MonthNames[month] composite:NX_COPY toPoint:&imageOrigin];
  202.     
  203.     tensSize.width=0.0; tensSize.height=0.0;
  204.  
  205.  
  206.     imageOrigin.x = 30;
  207.     imageOrigin.y = 15+imageAdjust;//        (15)
  208.     if (date/10) [DateNumbers[date / 10] getSize:&tensSize];
  209.     [DateNumbers[date % 10] getSize:&onesSize];
  210.     imageOrigin.x -= (tensSize.width+onesSize.width)/2;
  211.     if (date/10) [DateNumbers[date / 10] composite:NX_COPY toPoint:&imageOrigin];
  212.     imageOrigin.x += tensSize.width+1;
  213.     [DateNumbers[date % 10] composite:NX_COPY toPoint:&imageOrigin];
  214.     
  215.     // Draw the year
  216.     if (showYear)
  217.         {
  218.         currentYear=[self year]+1993;
  219.         imageAyear=YearNumbers[currentYear/1000];
  220.         currentYear=currentYear % 1000;
  221.         
  222.         imageByear=YearNumbers[currentYear/100];
  223.         currentYear=currentYear % 100;
  224.             
  225.         imageCyear=YearNumbers[currentYear/10];
  226.         currentYear=currentYear % 10;
  227.             
  228.         imageDyear=YearNumbers[currentYear];
  229.         
  230.         [imageAyear getSize:&theSize];
  231.         widthA=theSize.width;
  232.         [imageByear getSize:&theSize];
  233.         widthB=theSize.width;
  234.         [imageCyear getSize:&theSize];
  235.         widthC=theSize.width;
  236.         [imageDyear getSize:&theSize];
  237.         width=widthA+widthB+widthC+theSize.width;
  238.         
  239.         imageOrigin.x=32-(width/2.0); imageOrigin.y=0;
  240.         [imageAyear composite:NX_COPY toPoint:&imageOrigin];
  241.         imageOrigin.x=imageOrigin.x+widthA;
  242.         [imageByear composite:NX_COPY toPoint:&imageOrigin];
  243.         imageOrigin.x=imageOrigin.x+widthB;
  244.         [imageCyear composite:NX_COPY toPoint:&imageOrigin];
  245.         imageOrigin.x=imageOrigin.x+widthC;
  246.         [imageDyear composite:NX_COPY toPoint:&imageOrigin];
  247.         }
  248.     return self;
  249. };
  250.  
  251. - setSeconds:(int)theSeconds { seconds=theSeconds; return self;};
  252. - setMinutes:(int)theMinutes { minutes=theMinutes; return self;};
  253. - setHours:(int)theHours { hours=theHours; return self;};
  254.  
  255. - setWeekday:(int)theWeekday { weekday=theWeekday; return self;};
  256.  
  257. - setDate:(int)theDate { date=theDate; return self;};
  258. - setMonth:(int)theMonth { month=theMonth; return self;};
  259. - setYear:(int)theYear { year=theYear; return self;};
  260. - setMeridian:(int)theMeridian { meridian=theMeridian; return self;};
  261.  
  262. - takeMinutesFrom:sender    { [self setMinutes:[sender intValue]]; [self display]; return self;};
  263. - takeHoursFrom:sender    
  264.     int localhours=[sender intValue]; 
  265.     if (localhours <= 12) {
  266.         [self setMeridian:0];
  267.         if (localhours==0) localhours=12;
  268.         [self setHours:localhours];
  269.         }
  270.         else {
  271.         [self setMeridian:1];
  272.         localhours = localhours-12;
  273.         if (localhours==0) localhours=12;
  274.         [self setHours:localhours];
  275.         };
  276.     [self display]; return self;
  277. }
  278.     
  279. - takeWeekdayFrom:sender    { [self setWeekday:[sender intValue]]; [self display]; return self;}
  280. - takeDateFrom:sender    { [self setDate:[sender intValue]]; [self display]; return self;}
  281. - takeMonthFrom:sender    { [self setMonth:[sender intValue]]; [self display]; return self;}
  282.  
  283.  
  284. - setTime: (struct tm*)theTime
  285. {
  286.  
  287.     seconds = theTime->tm_sec;
  288.     minutes = theTime->tm_min;
  289.     hours = theTime->tm_hour;
  290.     if (hours <= 12) {
  291.         meridian=0;
  292.         if (hours==0) hours=12;
  293.         }
  294.         else {
  295.         meridian=1;
  296.         hours = hours-12;
  297.         if (hours==0) hours=12;
  298.         };
  299.     date = theTime->tm_mday;
  300.     month = theTime->tm_mon;
  301.     year = theTime->tm_year;
  302.     weekday = theTime->tm_wday;
  303.     return self;
  304. };
  305.  
  306.  
  307.  
  308. - (const char *)getInspectorClassName { return "MiscClockViewInspector"; }
  309.  
  310. - (int)year { return year; }
  311. - (int)month { return month; }
  312. - (int)weekday { return weekday; }
  313. - (int)date { return date; }
  314. - (int)hours { return hours; }
  315. - (int)minutes { return minutes; }
  316. - (int)seconds { return seconds; }
  317.  
  318.  
  319. - (BOOL)militaryTime
  320. {
  321.     return militaryTime;
  322. }
  323.  
  324. - (BOOL)showYear
  325. {
  326.     return showYear;
  327. }
  328.  
  329. - setShowYear:(BOOL)state
  330. {
  331.     showYear=state;
  332.     return self;
  333. }
  334.  
  335.  
  336. - setMilitaryTime:(BOOL)state
  337. {
  338.     militaryTime=state;
  339.     return self;
  340. }
  341.  
  342.  
  343. - read:(NXTypedStream *)stream
  344. {
  345.     [super read:stream];
  346.     NXReadTypes(stream, "iiiiiiiiii", &seconds, &minutes, &hours,
  347.             &date, &month, &year, &weekday, &meridian, &showYear,&militaryTime);
  348.     return self;
  349. }
  350.  
  351. - write:(NXTypedStream *)stream
  352. {
  353.     [super write:stream];
  354.     NXWriteTypes(stream, "iiiiiiiiii", &seconds, &minutes, &hours,
  355.             &date, &month, &year, &weekday, &meridian, &showYear,&militaryTime);
  356.     return self;
  357. }
  358.  
  359.  
  360. @end
  361.