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

  1. // ClockView.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.  
  7. #import <math.h>
  8. #import <appkit/Application.h>
  9. #import <appkit/Slider.h>
  10. #import <appkit/NXImage.h>
  11. #import "ClockView.h"
  12. #import <Date.h>
  13.  
  14. typedef struct _TimerStruct {
  15.     Date *date;
  16.     ClockView *view;
  17. } TimerStruct, *TimerStructPtr;
  18. static TimerStruct t_timerStruct;
  19. static long t_start,t_now;
  20. static long t_min,t_sec;
  21.  
  22. #define ABS(x) (x < 0 ? -(x) : (x))
  23.  
  24. void MinuteSecondTimer(DPSTimedEntry teNumber, double now,
  25.     TimerStructPtr ts)
  26. {
  27.     /* Display the current time */
  28.     t_now = [ts->date sysTime];
  29.     
  30.     t_min = (t_now - t_start) / 60;
  31.     t_sec = t_now - t_start - 60 * t_min;
  32.     t_min %= 60;
  33.  
  34.     [ts->view display];
  35. }
  36.  
  37. @implementation ClockView
  38.  
  39.  
  40. /* instance methods */
  41.  
  42. - initFrame:(NXRect *)frameRect
  43. {
  44. NXSize oneSize, twoSize;
  45. int i;
  46. char *numberNames[] = {"DigitalZero", "DigitalOne", "DigitalTwo",
  47.                       "DigitalThree", "DigitalFour", "DigitalFive",
  48.                   "DigitalSix", "DigitalSeven", "DigitalEight",
  49.                   "DigitalNine", ""};
  50.                   
  51.     [super initFrame : frameRect];
  52.     
  53. /* get the images we'll use in drawSelf:: */
  54.     clockImage = [NXImage findImageNamed : "Clock"];
  55.     
  56.     for (i = 0; *numberNames[i]; i++)
  57.             numbers[i] = [NXImage findImageNamed:numberNames[i]];
  58.  
  59.     colon = [NXImage findImageNamed : "Colon"];
  60.     amImage = [NXImage findImageNamed : "AM"];
  61.     pmImage = [NXImage findImageNamed : "PM"];
  62.     
  63. /* compute the colon's location (it never changes) */
  64.     [numbers[1] getSize : &oneSize];
  65.     [numbers[2] getSize : &twoSize];
  66.     colonPosition_clock.x = oneSize.width + twoSize.width + 4.0;
  67.     colonPosition_clock.y = ceil((bounds.size.height - twoSize.height) / 2.0); 
  68.     /* Center the ':' for the timer */
  69.     colonPosition_timer.x = bounds.size.width / 2.0;
  70.     colonPosition_timer.y = colonPosition_clock.y;
  71.  
  72.     /* Init the date to the current time */
  73.     dateObj = [[Date alloc] initFromNow];
  74.     /* Check my time to see if it is AM or PM */
  75.     if ([dateObj hours] < 12)
  76.         meridianImage = amImage;
  77.     else
  78.         meridianImage = pmImage;
  79.  
  80.     return self;
  81. }
  82.  
  83. /* Date initialization methods */
  84. - setDate : (short) d : (short) m : (short) y
  85. {
  86.     if([dateObj initFromDate : d : m : y] == nil)
  87.         return nil;
  88.     /* Check my time to see if it is AM or PM */
  89.     if ([dateObj hours] < 12)
  90.         meridianImage = amImage;
  91.     else
  92.         meridianImage = pmImage;
  93.     [self display];
  94.     return self;
  95. }
  96.  
  97. - setTime : (short) hr : (short) min : (short) sec
  98. {
  99.     if([dateObj setTime : hr : min : sec] == nil)
  100.         return nil;
  101.     /* Check my time to see if it is AM or PM */
  102.     if ([dateObj hours] < 12)
  103.         meridianImage = amImage;
  104.     else
  105.         meridianImage = pmImage;
  106.     [self display];
  107.     return self;
  108. }
  109.  
  110. - now
  111. {
  112.     if([dateObj initFromNow] == nil)
  113.         return nil;
  114.     [self display];
  115.     return self;
  116. }
  117.  
  118. - startMinSecTimer : sender
  119. {
  120.     t_start = [dateObj sysTime];
  121.     t_timerStruct.date = dateObj;
  122.     t_timerStruct.view = self;
  123.     /* Start the proc which updates the display every second */
  124.     min_sec_timer = DPSAddTimedEntry(1.0, MinuteSecondTimer, &t_timerStruct,
  125.         NX_MODALRESPTHRESHOLD);
  126.     timerActive = YES;
  127.     [self display];
  128.  
  129.     return self;
  130. }
  131.  
  132. - endTimer : sender
  133. {
  134.     DPSRemoveTimedEntry(min_sec_timer);
  135.     timerActive = NO;
  136.     return self;
  137. }
  138.  
  139. - drawSelf:(NXRect *)rects :(int)count
  140. {
  141. NXPoint    position;
  142. NXSize    size, hoursTensSize = {0.0, 0.0}, hoursUnitsSize;
  143. int t0,t1;
  144.  
  145.     /* Set the hour & minute (minute & second) positions
  146.         depending on the timer state */
  147.     if(timerActive == YES)
  148.     {
  149.         t0 = t_min;
  150.         t1 = t_sec;
  151.     }
  152.     else
  153.     {
  154.         t0 = [dateObj hours];
  155.         if(t0 > 12)
  156.             t0 %= 12;
  157.         t1 = [dateObj minutes];
  158.     }
  159.  
  160. /* draw the background */
  161.     [clockImage composite:NX_COPY toPoint:&(bounds.origin)];
  162.  
  163. /* AM or PM */
  164.     if(timerActive == NO)
  165.     {
  166.         [meridianImage getSize:&size];
  167.         position.x = bounds.size.width - size.width - 4.0;
  168.         position.y = floor((bounds.size.height - size.height) / 2.0);
  169.         [meridianImage composite:NX_COPY toPoint:&position];
  170.     }
  171.  
  172. /* compute starting position for all drawing;  the colon is stationary */
  173.     if(timerActive == NO)
  174.         position = colonPosition_clock;
  175.     else
  176.         position = colonPosition_timer;
  177.  
  178.     if (t0 / 10)
  179.     {
  180.         [numbers[t0 / 10] getSize:&hoursTensSize];
  181.     }
  182.     [numbers[t0 % 10] getSize:&hoursUnitsSize];
  183.     position.x -= hoursTensSize.width + hoursUnitsSize.width;
  184.     
  185. /* draw the hours tens digit */
  186.     if (t0 / 10)
  187.     {
  188.         [numbers[t0 / 10] composite:NX_COPY toPoint:&position];
  189.         position.x += hoursTensSize.width;
  190.     }
  191.     
  192. /* hours units digit */
  193.     [numbers[t0 % 10] composite:NX_COPY toPoint:&position];
  194.     position.x += hoursUnitsSize.width;
  195.     
  196. /* draw the colon */  
  197.     [[colon getSize:&size] composite:NX_COPY toPoint:&position];
  198.     position.x += size.width;
  199.     
  200. /* tens digit of minutes */
  201.     [[numbers[t1 / 10] getSize:&size] composite:NX_COPY
  202.                                 toPoint:&position];
  203.     position.x += size.width;
  204.     
  205. /* minutes units digit */
  206.     [numbers[t1 % 10] composite:NX_COPY toPoint:&position];
  207.     
  208.     return self;
  209. }
  210.  
  211. @end
  212.