home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.mdf / SourceCode / Tutorial / Cookbook / 14.pie_matrix / PieView.m < prev    next >
Encoding:
Text File  |  1993-01-18  |  4.0 KB  |  199 lines

  1.  
  2. /* Generated by Interface Builder */
  3.  
  4. #import "PieView.h"
  5. #import "stdio.h"
  6. #import <dpsclient/wraps.h>
  7. #import <appkit/Font.h>
  8. #import <appkit/Cell.h>
  9. #import <appkit/Matrix.h>
  10. #import <appkit/Slider.h>
  11. #import <appkit/OpenPanel.h>
  12. #import <appkit/Panel.h>
  13. #import <strings.h>
  14. #import "slice.h"
  15. #import "math.h"
  16.  
  17. @implementation PieView
  18.  
  19. +newFrame:(const NXRect *)tF
  20. {
  21.    int i;
  22.    float c_x, c_y;  // X and Y center of view
  23.    
  24.    // we are using the Appkit "newFrame" plus our own additions
  25.    self = [super newFrame:tF];
  26.    
  27.    nSlices = 9;
  28.    
  29.    for (i=0; i<=nSlices; i++)
  30.     {
  31.        // shuffle the grays so adjecent ones are different
  32.        gray[i] = .1*(i*(i%2) + (10-i)*((i+1)%2));
  33.        myFloat[i] = (float) (i+1)*5.0;
  34.    }
  35.    strcpy(myLabel[0],"ABC");
  36.    strcpy(myLabel[1],"DEF");
  37.    strcpy(myLabel[2],"GHI");
  38.    strcpy(myLabel[3],"JKL");
  39.    strcpy(myLabel[4],"MNO");
  40.    strcpy(myLabel[5],"PQR");
  41.    strcpy(myLabel[6],"STU");
  42.    strcpy(myLabel[7],"VWX");
  43.    strcpy(myLabel[8],"YZa");
  44.    strcpy(myLabel[9],"bcd");
  45.    
  46.    myFont = [Font newFont:"Courier-Bold"
  47.                   size:20.0
  48.           style:0
  49.           matrix:NX_IDENTITYMATRIX];
  50.           
  51.  
  52.    pieSize = 100.0;
  53.    pieFontSize = 20.0;
  54.    openReq = [OpenPanel new];
  55.    c_x = bounds.size.width/2.0;
  56.    c_y = bounds.size.height/2.0;
  57.    [self translate:c_x :c_y];
  58.    return self;
  59. }
  60.  
  61. - getValue:sender
  62. {
  63.     int i;
  64.     i = [sender selectedRow];
  65.     myFloat[i] = [sender floatValue];
  66.     // printf("Row = %d  Value = %f\n", i, myFloat[i]);
  67.     [self display];
  68.     return self;
  69. }
  70.  
  71. - getLabel:sender
  72. {
  73.     int i;
  74.     i = [sender selectedRow];
  75.     strcpy(myLabel[i],[sender stringValue]);
  76.     // printf("Row = %d  String = %s\n", i, myLabel[i]);
  77.     [self display];
  78.     return self;
  79. }
  80.  
  81. - getnSlices:sender
  82. {
  83.     nSlices = [sender intValue];
  84.     [self display];
  85.     return self;
  86. }
  87.  
  88. - getPieSize:sender
  89. {
  90.     pieSize = [sender floatValue];
  91.     [self display];
  92.     return self;
  93. }
  94.  
  95. - getPieFontSize:sender
  96. {
  97.     pieFontSize = [sender floatValue];
  98.     myFont = [Font newFont:"Courier-Bold"
  99.                   size:pieFontSize
  100.           style:0
  101.           matrix:NX_IDENTITYMATRIX];
  102.     [self display];
  103.     return self;
  104. }
  105.  
  106. - setValueMatrix:anObject
  107. {
  108.    int i;
  109.    id myCell;
  110.    valueMatrix = anObject;
  111.    for (i=0; i<=nSlices; i++) {
  112.       myCell = [valueMatrix cellAt:i:0];
  113.       [myCell setFloatValue:myFloat[i]];
  114.    }
  115.    [valueMatrix display];
  116.    return self;
  117. }
  118.  
  119. - setLabelMatrix:anObject
  120. {
  121.    int i;
  122.    id myCell;
  123.    labelMatrix = anObject;
  124.    for (i=0; i<=nSlices; i++) {
  125.       myCell = [labelMatrix cellAt:i:0];
  126.       [myCell setStringValue:myLabel[i]];
  127.    }
  128.    [labelMatrix display];
  129.    return self;
  130. }
  131.  
  132. - showError: (char *)errorMessage
  133. {
  134.   NXRunAlertPanel("Pie_Matrix", errorMessage,"OK",NULL,NULL);
  135. }
  136.  
  137. - openRequest:sender
  138. {
  139.     const char *fileName;
  140.     const char *const types[4] = {"data",NULL};
  141.     int ok;
  142.  
  143.     if ([openReq runModalForTypes:types] && (fileName =[openReq filename])) {
  144.     [self openFile:fileName];
  145.     }
  146.     else
  147.     [self showError:"No file chosen or could not open file"];
  148.     return self;
  149. }
  150.  
  151. -(int) openFile:(const char *)fileName
  152. {
  153.   int i=0;
  154.   char buf[MAX_CHARS_PER_LINE];
  155.   FILE *input_fp;
  156.   printf("Opening File = %s\n", fileName);
  157.   
  158.   if (( input_fp = fopen(fileName, "r") ) == NULL ) {
  159.      [self showError:"Could not open file"];
  160.      fprintf(stderr, "File: %s Could not be opened.\n", fileName);
  161.      return NO;
  162.   }
  163.   
  164.   while (fgets(buf, MAX_CHARS_PER_LABEL, input_fp) != NULL)
  165.   {
  166.      if (i >= MAX_SLICES) break;
  167.      sscanf(buf, "%f %s", &myFloat[i], myLabel[i]);
  168.      // printf("Value = %f  Label = %s\n", myFloat[i], myLabel[i]);
  169.      i++;
  170.   }
  171.   printf("%d data points read\n", i);
  172.   nSlices = i;
  173.   fclose(input_fp);
  174.   [self display];
  175.   [self setLabelMatrix:labelMatrix];
  176.   [self setValueMatrix:valueMatrix];
  177.   return YES;
  178.  
  179. }
  180.  
  181.  
  182. - drawSelf:(NXRect*)r :(int)c
  183. {
  184.     int i;
  185.     float total = 0.0;
  186.     [myFont set];
  187.     PSsetgray(1.0);
  188.     NXRectFill(r);
  189.     PSsetgray(0.0);
  190.     for (i=0; i<nSlices; i++)
  191.     {
  192.      drawSlice(gray[i], pieSize, total, total+myFloat[i], pieFontSize, myLabel[i]);
  193.        total = total + myFloat[i];
  194.     }
  195.     return self;
  196. }
  197.  
  198. @end
  199.