home *** CD-ROM | disk | FTP | other *** search
/ NeXT 3rd Party Products 1993 / NEXT_3rd_party_products_93.iso / Xanthus / Graphity_Demo / API / Demo2 / GraphityAPIDemo2.m < prev    next >
Text File  |  1993-05-07  |  4KB  |  181 lines

  1.  
  2. #import "app.h"
  3.  
  4. #define GRAPH_NAME    "MyGraph2"
  5.  
  6. @implementation GraphityAPIDemo2
  7.  
  8. - reportError: (const char *)message;
  9. {
  10.     NXRunAlertPanel("Problem!", message, "OK", NULL, NULL);
  11.     return self;
  12. }
  13.  
  14. - initSpeaker;
  15. {
  16.     port_t aPort;
  17.     
  18.     if(theSpeaker == nil) 
  19.         theSpeaker = [[Graphity_APISpeaker allocFromZone:[self zone]]
  20.                     init];
  21.     
  22.     if([theSpeaker sendPort] != PORT_NULL) return self;
  23.     
  24.     aPort = NXPortFromName("Graphity", NULL);
  25.     if(aPort == PORT_NULL) {
  26.         [self reportError: "Could not launch Graphity!"];
  27.         return nil;
  28.     }
  29.     [theSpeaker setSendPort:aPort];
  30.     return self;
  31. }
  32.  
  33. - newDocument:sender
  34. {
  35.     int x, error;
  36.     const char *name = [theNameField stringValue];
  37.     
  38.     if([self initSpeaker] == nil)
  39.         return nil;
  40.         
  41.     x = [theSpeaker API_newDocument: (char *)name error:&error];
  42.     if(x != 0) {
  43.         [self reportError: "Communication problem!"];
  44.         return nil;
  45.     }
  46.     
  47.     if(error != GRAPHITY_OK) {
  48.         [self reportError: "Graphity could not create new doc!"];
  49.         return nil;
  50.     }
  51.     
  52.     [self shapeDocument:nil];
  53.     [theNameField setEnabled:NO];
  54.     [theAddButton setEnabled:YES];
  55.     
  56.         return self; 
  57. }
  58.  
  59. - addGraph:sender
  60. {
  61.     int x, error;
  62.     const char *name = GRAPH_NAME;
  63.     const char *docname = [theNameField stringValue];
  64.     
  65.     if([self initSpeaker] == nil)
  66.         return nil;
  67.         
  68.     x = [theSpeaker API_addGraph: GRAPHITY_BAR_GRAPH
  69.             with: (char *)name
  70.             in: (char *)docname 
  71.             error:&error];
  72.     if(x != 0) {
  73.         [self reportError: "Communication problem!"];
  74.         return nil;
  75.     }
  76.     
  77.     if(error != GRAPHITY_OK) {
  78.         [self reportError: "Graphity could not create new graph!"];
  79.         return nil;
  80.     }
  81.  
  82.     [theSpeaker API_redisplay:(char *)name
  83.             in: (char *)docname 
  84.             error:&error];
  85.     
  86.     [theGraphButton setEnabled:YES];
  87.     
  88.         return self;
  89. }
  90.  
  91.  
  92. - shapeDocument:sender
  93. {
  94.     int a, error;
  95.     const char *docname = [theNameField stringValue];
  96.     float x = 0;
  97.     float y = 0;
  98.     float w = 550;
  99.     float h = 700;
  100.     
  101.     if([self initSpeaker] == nil)
  102.         return nil;
  103.         
  104.     a = [theSpeaker API_shapeDocument:(char *)docname
  105.             x:x
  106.             y: y
  107.             width:w
  108.             height:h
  109.             error: &error];
  110.     if(a != 0) {
  111.         [self reportError: "Communication problem!"];
  112.         return nil;
  113.     }
  114.     
  115.     if(error != GRAPHITY_OK) {
  116.         [self reportError: "Graphity could not shape document!"];
  117.         return nil;
  118.     }
  119.     
  120.         return self;
  121. }
  122.  
  123. - saveDataToFile:(const char *)datafile;
  124. {
  125.     NXStream *s = NXOpenMemory(NULL, 0, NX_WRITEONLY);
  126.     int a;
  127.     
  128.     [[theDataView docView] writeText:s];
  129.     a = NXSaveToFile(s, datafile);
  130.     
  131.     if(a != 0) {
  132.         [self reportError: "Could not create datafile!"];
  133.         if(s != NULL)NXCloseMemory(s, NX_FREEBUFFER);
  134.         return nil;
  135.     }
  136.     if(s != NULL)NXCloseMemory(s, NX_FREEBUFFER);
  137.     return self;
  138. }
  139.  
  140. - graphData:sender
  141. {
  142.     int x, error;
  143.     const char *name = GRAPH_NAME;
  144.     const char *docname = [theNameField stringValue];
  145.     const char *tmpFile = "/tmp/GraphityDemo2.data";    // ugly
  146.     int rowCount = -1;
  147.     int serieCount = -1;
  148.     
  149.     if([self saveDataToFile: tmpFile] == nil)
  150.         return nil;
  151.     
  152.     if([self initSpeaker] == nil)
  153.         return nil;
  154.         
  155.     x = [theSpeaker API_readDataFrom:(char *)tmpFile
  156.             graph:(char *)name
  157.             in:(char *)docname
  158.             error: &error];
  159.     if(x != 0) {
  160.         [self reportError: "Communication problem!"];
  161.         return nil;
  162.     }
  163.     
  164.     if(error != GRAPHITY_OK) {
  165.         [self reportError: "Graphity could not parse data file!"];
  166.         return nil;
  167.     }
  168.     
  169.     [theSpeaker API_getGraphSize:(char *)name
  170.         in:(char *)docname
  171.         rowCount: &rowCount
  172.         serieCount: &serieCount
  173.         error: &error];
  174.     [[theSizeForm findCellWithTag: 1] setIntValue: serieCount];
  175.     [[theSizeForm findCellWithTag: 0] setIntValue: rowCount];
  176.     
  177.         return self;
  178. }
  179.  
  180. @end
  181.