home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Educational / CurveGrader / Source / GradeSlider.m < prev    next >
Encoding:
Text File  |  1995-06-12  |  3.6 KB  |  129 lines

  1. /*
  2.  *    A 4 button slider.  There are some optimizations that need to be done. 
  3.  *    Rob Ferrante, 2/92
  4.  */
  5. #import "GradeSlider.h"
  6. #import <appkit/View.h>
  7. #import <appkit/NXImage.h>
  8. #import <dpsclient/wraps.h>
  9. #import <appkit/Application.h>
  10. #import <appkit/graphics.h>
  11.  
  12. @implementation GradeSlider
  13.  
  14.  
  15. - initFrame: (NXRect *)r
  16. {    
  17.     NXSize  knobSize;
  18.     
  19.     self = [super initFrame:r];
  20.     knobA = [[NXImage alloc] initFromSection:"A.tiff"];
  21.     knobB = [[NXImage alloc] initFromSection:"B.tiff"];
  22.     knobC = [[NXImage alloc] initFromSection:"C.tiff"];
  23.     knobD = [[NXImage alloc] initFromSection:"D.tiff"];
  24.     [knobA getSize:&knobSize];
  25.     knobOff = knobSize.width/2.0;
  26.     NXSetRect(&rectA, 90,0,knobSize.width, knobSize.height);
  27.     NXSetRect(&rectB, 190,0,knobSize.width, knobSize.height);
  28.     NXSetRect(&rectC, 290,0,knobSize.width, knobSize.height);
  29.     NXSetRect(&rectD, 390,0,knobSize.width, knobSize.height);
  30.     return self;
  31. }
  32.  
  33. - setAction:(SEL)a
  34. {
  35.     action = a;
  36.     return self;
  37. }
  38.  
  39. - setTarget:(id)t
  40. {
  41.     target = t;
  42.     return self;
  43. }
  44. - drawSelf:(NXRect *)list:(int)count
  45. {
  46.     PSsetgray(NX_DKGRAY);
  47.     NXRectFill(&bounds);
  48.     [knobA composite:NX_SOVER toPoint:&(rectA.origin)];
  49.     [knobB composite:NX_SOVER toPoint:&(rectB.origin)];
  50.     [knobC composite:NX_SOVER toPoint:&(rectC.origin)];
  51.     [knobD composite:NX_SOVER toPoint:&(rectD.origin)];
  52.     return self;
  53. }
  54.  
  55. - mouseDown: (NXEvent *)e
  56. {
  57.     NXPoint    mLoc, oldLoc;
  58.     NXRect  *currentKnobRect, oldRect, penRect = bounds;
  59.     id  currentKnobImage;
  60.     NXEvent    *nextEvent;
  61.     int    oldMask, checkMask;
  62.     
  63.     
  64.     oldMask = [window eventMask];
  65.     checkMask = NX_MOUSEUPMASK | NX_MOUSEDRAGGEDMASK;
  66.     [window setEventMask:( oldMask|checkMask)];
  67.     oldLoc = e->location;
  68.     [self convertPoint:&oldLoc fromView:nil];
  69.  
  70. /*    Check if the cursor clicked on a button, and set up if so  */
  71.  
  72.     if (NXMouseInRect(&oldLoc, &rectA, NO)) {
  73.         currentKnobImage = knobA;
  74.         currentKnobRect = &rectA;
  75.         penRect.origin.x = bounds.origin.x;
  76.         penRect.size.width = rectB.origin.x - penRect.origin.x;
  77.         action = @selector(moveA:);
  78.     } else if (NXMouseInRect(&oldLoc, &rectB, NO)) {    
  79.         currentKnobImage = knobB;
  80.         currentKnobRect = &rectB;
  81.         penRect.origin.x = rectA.origin.x + rectA.size.width;
  82.         penRect.size.width = rectC.origin.x - penRect.origin.x;
  83.         action = @selector(moveB:);
  84.     } else if (NXMouseInRect(&oldLoc, &rectC, NO)) {    
  85.         currentKnobImage = knobC;
  86.         currentKnobRect = &rectC;
  87.         penRect.origin.x = rectB.origin.x + rectB.size.width;
  88.         penRect.size.width = rectD.origin.x - penRect.origin.x;
  89.         action = @selector(moveC:);
  90.     } else if (NXMouseInRect(&oldLoc, &rectD, NO)) {    
  91.         currentKnobImage = knobD;
  92.         currentKnobRect = &rectD;
  93.         penRect.origin.x = rectC.origin.x + rectC.size.width;
  94.         penRect.size.width = bounds.size.width - penRect.origin.x;
  95.         action = @selector(moveD:);
  96.     } else return self;
  97.     
  98.     [self lockFocus];
  99.     PSsetgray(NX_DKGRAY);
  100.  
  101. /*    Loop while dragging the slider */
  102.  
  103.     while ((nextEvent = [NXApp getNextEvent:checkMask])->type !=NX_MOUSEUP) {
  104.         mLoc = nextEvent->location;
  105.         [self convertPoint:&mLoc fromView:nil];
  106.         oldRect = *currentKnobRect;
  107.         NXOffsetRect(currentKnobRect, mLoc.x - oldLoc.x, 0);
  108.         if ( NXContainsRect(&penRect, currentKnobRect)==YES) {
  109.             NXRectFill(&oldRect);
  110.             [currentKnobImage composite:NX_COPY toPoint:
  111.                 ¤tKnobRect->origin];
  112.             [window flushWindow]; // so cursor doesn't flicker
  113.             [self  sendAction:action to:target];
  114.             oldLoc = mLoc;
  115.         } else *currentKnobRect =oldRect;
  116.     }
  117.     [self unlockFocus];
  118.     [window setEventMask:oldMask];
  119.     return self;
  120. }
  121.  
  122.  
  123. - (float)posA { return rectA.origin.x + knobOff; }
  124. - (float)posB { return rectB.origin.x + knobOff; }
  125. - (float)posC { return rectC.origin.x + knobOff; }
  126. - (float)posD { return rectD.origin.x + knobOff; }
  127.  
  128. @end
  129.