home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 4.2J (Developer) / os42jdev.iso / NextDeveloper / Examples / AppKit / Draw / Ruler.m < prev    next >
Text File  |  1996-04-11  |  7KB  |  252 lines

  1. #import "draw.h"
  2.  
  3. /*
  4.  * This Ruler should really look at the NSMeasurementUnit default
  5.  * and, based on that, use the proper units (whether centimeters
  6.  * or inches) rather than just always using inches.
  7.  */
  8.  
  9. #define LINE_X (15.0)
  10. #define WHOLE_HT (10.0)
  11. #define HALF_HT (8.0)
  12. #define QUARTER_HT (4.0)
  13. #define EIGHTH_HT (2.0)
  14. #define NUM_X (3.0)
  15.  
  16. #define WHOLE (72)
  17. #define HALF (WHOLE/2)
  18. #define QUARTER (WHOLE/4)
  19. #define EIGHTH (WHOLE/8)
  20.  
  21. @implementation Ruler
  22.  
  23. + (float)width
  24. {
  25.     return 23.0;
  26. }
  27.  
  28. - (id)initWithFrame:(NSRect)frameRect {
  29.     [super initWithFrame:frameRect];
  30.     [self setFont:[NSFont systemFontOfSize:8.0]];
  31.     startX = [[self class] width];
  32.     zeroAtViewOrigin = YES;
  33.     return self;
  34. }
  35.  
  36. - setZeroAtViewOrigin:(BOOL)flag
  37. /*
  38.  * Tells whether the ruler has "0" at
  39.  * the origin of the view or not.
  40.  */
  41. {
  42.     zeroAtViewOrigin = flag ? YES : NO;
  43.     return self;
  44. }
  45.  
  46. - setFont:(NSFont *)aFont
  47. {
  48.     float as, lh;
  49.  
  50.     font = aFont;
  51.     // This function is defined in the old NSCStringText class,
  52.     // but it doesn't actually have anything to do with the text object.
  53.     // It uses only the font info.
  54.     NSTextFontInfo(aFont, &as, &descender, &lh);
  55.     if (descender < 0.0) descender = -1.0 * descender;
  56.  
  57.     return self;
  58. }
  59.  
  60. - drawHorizontal:(NSRect)rect
  61. {
  62.     NSRect line, clip;
  63.     int curPos, last, mod, i, j;
  64.  
  65.     PSsetgray(NSLightGray);
  66.     NSRectFill(rect);
  67.  
  68.     if (lastlp >= rect.origin.x && lastlp < rect.origin.x + rect.size.width) lastlp = - 1.0;
  69.     if (lasthp >= rect.origin.x && lasthp < rect.origin.x + rect.size.width) lasthp = - 1.0;
  70.  
  71.     line = _bounds;                /* draw bottom line */
  72.     line.size.height = 1.0;
  73.     PSsetgray(NSDarkGray);
  74.     if (!NSIsEmptyRect(line = NSIntersectionRect(rect, line))) NSRectFill(line);
  75.  
  76.     line = _bounds;
  77.     line.size.width = 1.0;
  78.     line.origin.x = startX - 1.0;
  79.     if (!NSIsEmptyRect(line = NSIntersectionRect(rect, line))) NSRectFill(line);
  80.  
  81.     line = _bounds;                /* draw ruler line */
  82.     line.origin.y = LINE_X;
  83.     line.size.height = 1.0;
  84.     line.origin.x = startX;
  85.     line.size.width = _bounds.size.width - startX;
  86.  
  87.     PSsetgray(NSBlack);
  88.     if (!NSIsEmptyRect(line = NSIntersectionRect(rect, line))) NSRectFill(line);
  89.  
  90.     clip = rect;
  91.     clip.origin.x = startX;
  92.     clip.size.width = _bounds.size.width - startX;
  93.     if (!NSIsEmptyRect(clip = NSIntersectionRect(rect, clip))) {
  94.     curPos = (int)(clip.origin.x - startX);
  95.     last = (int)(NSMaxX(clip) - startX);
  96.     if ((mod = (curPos % EIGHTH))) curPos -= mod;
  97.     if ((mod = (last % EIGHTH))) last -= mod;
  98.     line.size.width = 1.0;
  99.     [font set];
  100.     for (j = curPos; j <= last; j += EIGHTH) {
  101.         i = !zeroAtViewOrigin ? _bounds.size.width - j : j;
  102.         line.origin.x =  startX + (float)i - (!zeroAtViewOrigin ? 1.0 : 0.0);
  103.         if (!(i % WHOLE)) {
  104.         NSString *formatString;
  105.         
  106.         line.origin.y = LINE_X - WHOLE_HT;
  107.         line.size.height = WHOLE_HT;
  108.         NSRectFill(line);
  109.         PSmoveto(((float) j + NUM_X) + startX, descender + line.origin.y - 2.0);
  110.         formatString = [NSString stringWithFormat:@"%d", i / WHOLE];
  111.         PSshow([formatString cString]);
  112.         } else if (!(i % HALF)) {
  113.         line.origin.y = LINE_X - HALF_HT;
  114.         line.size.height = HALF_HT;
  115.         NSRectFill(line);
  116.         } else if (!(i % QUARTER)) {
  117.         line.origin.y = LINE_X - QUARTER_HT;
  118.         line.size.height = QUARTER_HT;
  119.         NSRectFill(line);
  120.         } else if (!(i % EIGHTH)) {
  121.         line.origin.y = LINE_X - EIGHTH_HT;
  122.         line.size.height = EIGHTH_HT;
  123.         NSRectFill(line);
  124.         }
  125.     }
  126.     }
  127.  
  128.     return self;
  129. }
  130.  
  131.  
  132. - drawVertical:(NSRect)rect
  133. {
  134.     NSRect line, clip;
  135.     int curPos, last, mod, i, j;
  136.  
  137.     PSsetgray(NSLightGray);
  138.     NSRectFill(rect);
  139.  
  140.     if (lastlp >= rect.origin.y && lastlp < rect.origin.y + rect.size.height) lastlp = - 1.0;
  141.     if (lasthp >= rect.origin.y && lasthp < rect.origin.y + rect.size.height) lasthp = - 1.0;
  142.  
  143.     line = _bounds;                /* draw bottom line */
  144.     line.origin.x = _bounds.size.width - 1.0;
  145.     line.size.width = 1.0;
  146.     PSsetgray(NSDarkGray);
  147.     if (!NSIsEmptyRect(line = NSIntersectionRect(rect, line))) NSRectFill(line);
  148.  
  149.     line = _bounds;                /* draw ruler line */
  150.     line.origin.x = _bounds.size.width - LINE_X - 2.0;
  151.     line.size.width = 1.0;
  152.     PSsetgray(NSBlack);
  153.     if (!NSIsEmptyRect(line = NSIntersectionRect(rect, line))) NSRectFill(line);
  154.  
  155.     clip = rect;
  156.     line.origin.x++;
  157.     if (!NSIsEmptyRect(clip = NSIntersectionRect(rect, clip))) {
  158.     curPos = (int)(clip.origin.y);
  159.     last = (int)(NSMaxY(clip));
  160.     if (!zeroAtViewOrigin) {
  161.         if ((mod = ((int)(_bounds.size.height - curPos) % EIGHTH))) curPos += mod;
  162.         if ((mod = ((int)(_bounds.size.height - last) % EIGHTH))) last += mod;
  163.     } else {
  164.         if ((mod = (curPos % EIGHTH))) curPos -= mod;
  165.         if ((mod = (last % EIGHTH))) last -= mod;
  166.     }
  167.     line.size.height = 1.0;
  168.     [font set];
  169.     for (j = curPos; j <= last; j += EIGHTH) {
  170.         i = !zeroAtViewOrigin ? _bounds.size.height - j : j;
  171.         line.origin.y = (float)j - (!zeroAtViewOrigin ? 1.0 : 0.0);
  172.         if (!(i % WHOLE)) {
  173.         NSString *formatString;
  174.         line.size.width = WHOLE_HT;
  175.         NSRectFill(line);
  176.         PSmoveto(line.origin.x + 5.0, (float)j + (!zeroAtViewOrigin ? - 10.0 : 2.0));
  177.         formatString = [NSString stringWithFormat:@"%d", i / WHOLE];
  178.         PSshow([formatString cString]);
  179.         } else if (!(i % HALF)) {
  180.         line.size.width = HALF_HT;
  181.         NSRectFill(line);
  182.         } else if (!(i % QUARTER)) {
  183.         line.size.width = QUARTER_HT;
  184.         NSRectFill(line);
  185.         } else if (!(i % EIGHTH)) {
  186.         line.size.width = EIGHTH_HT;
  187.         NSRectFill(line);
  188.         }
  189.     }
  190.     }
  191.  
  192.     return self;
  193. }
  194.  
  195. - (void)drawRect:(NSRect)rect
  196. {
  197.     if (_frame.size.width < _frame.size.height) {
  198.     [self drawVertical:rect];
  199.     } else {
  200.     [self drawHorizontal:rect];
  201.     }
  202. }
  203.  
  204.  
  205. #define SETPOSITION(value) (isVertical ? (rect.origin.y = value - (absolute ? 0.0 : 1.0)) : (rect.origin.x = value + (absolute ? 0.0 : startX)))
  206. #define SETSIZE(value) (isVertical ? (rect.size.height = value) : (rect.size.width = value))
  207. #define SIZE (isVertical ? rect.size.height : rect.size.width)
  208.  
  209. - doShowPosition:(float)lp :(float)hp absolute:(BOOL)absolute
  210. {
  211.     NSRect rect;
  212.     BOOL isVertical = (_frame.size.width < _frame.size.height);
  213.  
  214.     rect = _bounds;
  215.  
  216.     if (!absolute && !isVertical) {
  217.     if (lp < 0.0) lp -= startX;
  218.     if (hp < 0.0) hp -= startX;
  219.     }
  220.  
  221.     SETSIZE(1.0);
  222.     lastlp = SETPOSITION(lp);
  223.     NSHighlightRect(rect);
  224.     lasthp = SETPOSITION(hp);
  225.     NSHighlightRect(rect);
  226.  
  227.     return self;
  228. }
  229.  
  230. - showPosition:(float)lp :(float)hp
  231. {
  232.     [self lockFocus];
  233.     if (notHidden) [self doShowPosition:lastlp :lasthp absolute:YES];
  234.     [self doShowPosition:lp :hp absolute:NO];
  235.     [self unlockFocus];
  236.     notHidden = YES;
  237.     return self;
  238. }
  239.  
  240. - hidePosition
  241. {
  242.     if (notHidden) {
  243.     [self lockFocus];
  244.     [self doShowPosition:lastlp :lasthp absolute:YES];
  245.     [self unlockFocus];
  246.     notHidden = NO;
  247.     }
  248.     return self;
  249. }
  250.  
  251. @end
  252.