home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.bin / SourceCode / Classes / Gauges / BarGauge.m < prev    next >
Encoding:
Text File  |  1993-01-18  |  5.7 KB  |  184 lines

  1.  
  2. /* Generated by Interface Builder */
  3. /* the current font size calculation is totally wrong and should be changed */
  4. /* it should actually put a string into a certain font size, see how big it is compared to the space and
  5. /* rescale the font to fit the space */
  6.  
  7.  
  8. #import "BarGauge.h"
  9. #import "strings.h"
  10.  
  11. #define HEIGHT_LABLES    0.30
  12. #define HEIGHT_NOLABEL    0.10
  13. #define WIDTH_LABLES    0.30
  14. #define WIDTH_NOLABEL    0.10
  15. #define SPACING        3.0    /* spacing between labels and drawing */
  16. #define    TICBAR        4.0    /* thickness of a tic marker */
  17.  
  18. @implementation BarGauge
  19.  
  20. + newFrame: (NXRect *) r
  21. {
  22.     self = [super newFrame:r];
  23.     GaugeFlags.borderType = NX_BEZEL;
  24.     [self refreshBackground:self];
  25.     return self;
  26. }
  27.  
  28. - refreshBackground:sender;
  29. /* for some reason the vertical bars (height greater than width work as expect)
  30. /* but the horizonatal bars seem to have a reverse coordinate system 
  31. /* sometimes (always?) */
  32. /* could it be that locking focus on a bitmap causes view to have inveted 
  33. /* coordinate systems?  Maybe! */
  34. {
  35.     float    LabelHOff,LabelWOff;
  36.     float    TicSpacing;
  37.     int    i;
  38.     NXRect    ticRect;    /* to do fancy tics (which are really boxes) */
  39.     char    labelStr[20];    /* The could never be this big */
  40.                 /* (famous last words) */
  41.     int    probTicSize;    /*prob. max tic str size based on labelTicPat*/
  42.     id    ticFont;
  43.     
  44.     sprintf(labelStr,GaugeDat.labelTicPat,GAUGEMAX);
  45.     probTicSize = strlen(labelStr);
  46.     if (!GaugeDat.labelTicPat || !*GaugeDat.labelTicPat)
  47.         GaugeDat.labelTicPat = "%1.1f";
  48.     FaceRect = bounds;
  49.     FaceRect.origin.x += SPACING;
  50.     FaceRect.origin.y += SPACING;
  51.     FaceRect.size.height -= 2*SPACING;
  52.     FaceRect.size.width  -= 2*SPACING;
  53.     if (GaugeFlags.labelTicsOn){
  54.         LabelHOff = (HEIGHT_LABLES * NX_HEIGHT(&bounds)) + SPACING;
  55.         LabelWOff = (WIDTH_LABLES * NX_WIDTH(&bounds)) + SPACING;
  56.     } else {
  57.         LabelHOff = (HEIGHT_NOLABEL * NX_HEIGHT(&bounds));
  58.         LabelWOff = (WIDTH_NOLABEL * NX_WIDTH(&bounds));
  59.     }
  60.     [[GaugeDat.bm contentView] lockFocus];
  61.     PSsetlinewidth(0.0);
  62.     PSsetgray(GaugeDat.backGray);
  63.     NXRectFill(&bounds);    /* do this in case no border */
  64.     PSsetgray(GaugeDat.faceGray);
  65.     NXRectFill(&FaceRect);    /* don't know if i should do this but */
  66.     FancyBack(&bounds);
  67.     if (bounds.size.width > bounds.size.height){
  68.         /* do horizontal bar */
  69.         if (GaugeFlags.ticsOn){
  70.             FaceRect.origin.y += LabelHOff;
  71.             FaceRect.size.height -= LabelHOff;
  72.         }
  73.         TicSpacing = (NX_WIDTH(&FaceRect)-TICBAR) / (GaugeDat.numMajor-1);
  74.         ticFont = [Font newFont:GaugeDat.ticFontName
  75.                     size:(1.5*TicSpacing/probTicSize) /* 1.5 times is total kludge for now */
  76.                     style:0
  77.                     matrix:NX_IDENTITYMATRIX];
  78.         [ticFont set];
  79.         for (i = 0 ; i < GaugeDat.numMajor; i ++){
  80.             NX_X(&ticRect) = NX_X(&FaceRect)+(i*TicSpacing);
  81.             NX_WIDTH(&ticRect) = TICBAR;
  82.             if (GaugeFlags.labelTicsOn)
  83.                 NX_Y(&ticRect)=NX_Y(&FaceRect) - SPACING;
  84.             else    NX_Y(&ticRect)=NX_Y(&FaceRect) - LabelHOff;
  85.             NX_HEIGHT(&ticRect)= NX_MAXY(&FaceRect)-NX_Y(&ticRect);
  86.             if (GaugeFlags.labelTicsOn){
  87.                 PSsetgray(GaugeDat.textGray);
  88.                    sprintf(labelStr,GaugeDat.labelTicPat,
  89.                      (i)*GaugeDat.ticSize + GaugeDat.startValue);
  90.                 PSmoveto(NX_X(&ticRect)- 
  91.                   ((float)i/GaugeDat.numMajor)*
  92.                  ([ticFont getWidthOf:labelStr] -  NX_WIDTH(&ticRect)),
  93.                   NX_Y(&bounds)+SPACING);
  94.                 PSshow(labelStr);
  95.             }
  96.             PSsetgray(GaugeDat.ticGray);
  97.             if (GaugeFlags.borderType == NX_NOBORDER)
  98.                 NXFrameRect(&ticRect);
  99.             else {FancyRect(&ticRect)}
  100.         }
  101.         if ((!GaugeFlags.labelTicsOn) && GaugeFlags.ticsOn)
  102.             FaceRect.origin.y -= (LabelHOff / 2.0);
  103.     }else{
  104.         /* do vertical bar */
  105.         if (GaugeFlags.ticsOn){
  106.             FaceRect.origin.x += LabelWOff;
  107.             FaceRect.size.width -= LabelWOff;
  108.         }
  109.         TicSpacing = (NX_HEIGHT(&FaceRect)-TICBAR)/ (GaugeDat.numMajor-1);
  110.         ticFont = [Font newFont:GaugeDat.ticFontName
  111.                     size:(1.5* TicSpacing/probTicSize) /* 1.5* is total kludge for now */
  112.                     style:0
  113.                     matrix:NX_IDENTITYMATRIX];
  114.         [ticFont set];
  115.         for (i=0; i < GaugeDat.numMajor; i++){
  116.             NX_Y(&ticRect) = NX_Y(&FaceRect) +(i*TicSpacing);
  117.             NX_HEIGHT(&ticRect) = TICBAR;
  118.             if (GaugeFlags.labelTicsOn)
  119.                 NX_X(&ticRect) = NX_X(&FaceRect)-SPACING;
  120.             else    NX_X(&ticRect) = NX_X(&FaceRect)-LabelWOff;
  121.             NX_WIDTH(&ticRect)=NX_MAXX(&FaceRect)-NX_X(&ticRect);
  122.             if (GaugeFlags.labelTicsOn){
  123.                 PSsetgray(GaugeDat.textGray);
  124.                    sprintf(labelStr,GaugeDat.labelTicPat,
  125.                       i*GaugeDat.ticSize + GaugeDat.startValue);
  126.                 PSmoveto(NX_X(&bounds)+SPACING,NX_Y(&ticRect)-
  127.                     (((float)i/GaugeDat.numMajor)*
  128.                     ([ticFont pointSize]- NX_HEIGHT(&ticRect))));
  129.                 PSshow(labelStr);
  130.             }
  131.             PSsetgray(GaugeDat.ticGray);
  132.             if (GaugeFlags.borderType == NX_NOBORDER)
  133.                 NXFrameRect(&ticRect);
  134.             else {FancyRect(&ticRect)}
  135.         }
  136.         if (!GaugeFlags.labelTicsOn && GaugeFlags.ticsOn)
  137.             FaceRect.origin.x -= ( LabelWOff / 2.0);
  138.     }
  139.     if ((GaugeFlags.borderType == NX_NOBORDER) 
  140.             || (GaugeFlags.borderType == NX_LINE)){
  141.         PSsetlinewidth(SPACING);    /* didn't affect it? */
  142.         NXFrameRect(&bounds);
  143.     }
  144.     PSstroke();
  145.     [[GaugeDat.bm contentView] unlockFocus];
  146.     [self display];
  147.     return self;
  148. }
  149.  
  150. -drawSelf:(NXRect *)drawRects :(int)rectCount
  151. {
  152.     NXRect    dataRect;
  153.     
  154.     dataRect = FaceRect;
  155.     PScomposite(0.0,0.0,bounds.size.width,bounds.size.height,
  156.         GaugeDat.bmgState,0.0, 0.0,NX_COPY);
  157.     /* now draw the bar */
  158.     PSsetgray(GaugeDat.ptGray);
  159.     if (NX_WIDTH(&bounds) > NX_HEIGHT(&bounds)){
  160.         /* do horizontal bar */
  161.         dataRect.size.width = ((GaugeDat.value - GaugeDat.startValue) 
  162.                 / GAUGESIZE) 
  163.                 * NX_WIDTH(&dataRect);
  164.     }else{
  165.         /* do vertical bar */
  166.         dataRect.size.height = ((GaugeDat.value - GaugeDat.startValue)
  167.                 / GAUGESIZE)
  168.                 * NX_HEIGHT(&dataRect);
  169.  
  170.     }
  171.     if (GaugeFlags.borderType == NX_NOBORDER) {
  172.         NXRectFill(&dataRect);
  173.         if (GaugeFlags.overFlow) NXHighlightRect(&bounds);
  174.         return self; /* exit quick */
  175.     }
  176.     PSsetgray(GaugeDat.ticGray);
  177.     FancyRect(&dataRect);
  178.     if (GaugeFlags.overFlow) NXHighlightRect(&bounds);
  179.     NXPing();
  180.     return self;
  181. }
  182.  
  183. @end
  184.