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

  1.  
  2. #import "app.h"
  3.  
  4. #define GRAPH_NAME    "MyGraph3"
  5.  
  6.  
  7. void aTimerProc(DPSTimedEntry teNumber, double now, char *userdata)
  8. {
  9.     [(id)userdata doYourThing: now];
  10. }
  11.  
  12.  
  13. @implementation GraphityAPIDemo3
  14.  
  15. - reportError: (const char *)message;
  16. {
  17.     NXRunAlertPanel("Problem!", message, "OK", NULL, NULL);
  18.     return self;
  19. }
  20.  
  21. - initSpeaker;
  22. {
  23.     port_t aPort;
  24.     
  25.     if(theSpeaker == nil) 
  26.         theSpeaker = [[Graphity_APISpeaker allocFromZone:[self zone]]
  27.                     init];
  28.     
  29.     if([theSpeaker sendPort] != PORT_NULL) return self;
  30.     
  31.     aPort = NXPortFromName("Graphity", NULL);
  32.     if(aPort == PORT_NULL) {
  33.         [self reportError: "Could not launch Graphity!"];
  34.         return nil;
  35.     }
  36.     [theSpeaker setSendPort:aPort];
  37.     return self;
  38. }
  39.  
  40. - newDocument:sender
  41. {
  42.     int x, error;
  43.     const char *name = [theNameField stringValue];
  44.     
  45.     if([self initSpeaker] == nil)
  46.         return nil;
  47.         
  48.     x = [theSpeaker API_newDocument: (char *)name error:&error];
  49.     if(x != 0) {
  50.         [self reportError: "Communication problem!"];
  51.         return nil;
  52.     }
  53.     
  54.     if(error != GRAPHITY_OK) {
  55.         [self reportError: "Graphity could not create new doc!"];
  56.         return nil;
  57.     }
  58.     
  59.     [self shapeDocument:nil];
  60.     [theNameField setEnabled:NO];
  61.     [theAddButton setEnabled:YES];
  62.     
  63.         return self; 
  64. }
  65.  
  66. - addGraph:sender
  67. {
  68.     int x, error;
  69.     const char *name = GRAPH_NAME;
  70.     const char *docname = [theNameField stringValue];
  71.     
  72.     if([self initSpeaker] == nil)
  73.         return nil;
  74.         
  75.     x = [theSpeaker API_addGraph: GRAPHITY_BAR_GRAPH
  76.             with: (char *)name
  77.             in: (char *)docname 
  78.             error:&error];
  79.     if(x != 0) {
  80.         [self reportError: "Communication problem!"];
  81.         return nil;
  82.     }
  83.     
  84.     if(error != GRAPHITY_OK) {
  85.         [self reportError: "Graphity could not create new graph!"];
  86.         return nil;
  87.     }
  88.  
  89.     [theSpeaker API_redisplay:(char *)name
  90.             in: (char *)docname 
  91.             error:&error];
  92.     
  93.     [theStartButton setEnabled:YES];
  94.     
  95.         return self;
  96. }
  97.  
  98.  
  99. - shapeDocument:sender
  100. {
  101.     int a, error;
  102.     const char *docname = [theNameField stringValue];
  103.     float x = 0;
  104.     float y = 0;
  105.     float w = 550;
  106.     float h = 700;
  107.     
  108.     if([self initSpeaker] == nil)
  109.         return nil;
  110.         
  111.     a = [theSpeaker API_shapeDocument:(char *)docname
  112.             x:x
  113.             y: y
  114.             width:w
  115.             height:h
  116.             error: &error];
  117.     if(a != 0) {
  118.         [self reportError: "Communication problem!"];
  119.         return nil;
  120.     }
  121.     
  122.     if(error != GRAPHITY_OK) {
  123.         [self reportError: "Graphity could not shape document!"];
  124.         return nil;
  125.     }
  126.     
  127.         return self;
  128. }
  129.  
  130. - graphData:sender
  131. {
  132.     int x, error;
  133.     const char *name = GRAPH_NAME;
  134.     const char *docname = [theNameField stringValue];
  135.     const char *tmpFile = "/tmp/GraphityDemo3.data";    // ugly
  136.     const char *aScript = [theScript stringValue];    
  137.     int rowCount = -1;
  138.     int serieCount = -1;
  139.     char aBuf[512];
  140.     
  141.     // run shell script and place output in tmpFile
  142.     
  143.     sprintf(aBuf, "%s > %s", aScript, tmpFile);
  144.     system(aBuf);
  145.     
  146.     // init speaker
  147.     
  148.     if([self initSpeaker] == nil)
  149.         return nil;
  150.         
  151.     // tell Graphity to parse data in tmpFile
  152.     
  153.     x = [theSpeaker API_readDataFrom:(char *)tmpFile
  154.             graph:(char *)name
  155.             in:(char *)docname
  156.             error: &error];
  157.     if(x != 0) {
  158.         [self reportError: "Communication problem!"];
  159.         return nil;
  160.     }
  161.     
  162.     if(error != GRAPHITY_OK) {
  163.         [self reportError: "Graphity could not parse data file!"];
  164.         return nil;
  165.     }
  166.     
  167.     // get the size of the data
  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. - startTimer:sender;
  181. {
  182.     [self startTheTimer:YES];
  183.     [theStopButton setEnabled:YES];
  184.     [theStartButton setEnabled:NO];
  185.     [self doYourThing:0];
  186.     return self;
  187. }
  188.  
  189. - stopTimer:sender;
  190. {
  191.     [self startTheTimer:NO];
  192.     [theStopButton setEnabled:NO];
  193.     [theStartButton setEnabled:YES];
  194.     return self;
  195. }
  196.  
  197. - startTheTimer:(BOOL)flag;
  198. {
  199.     double period = [theFreq doubleValue];
  200.     
  201.     if(!flag) {
  202.         if(theTimer != NULL)
  203.             DPSRemoveTimedEntry(theTimer);
  204.         theTimer = (DPSTimedEntry)NULL;    
  205.         return self;
  206.     }
  207.     if(theTimer != NULL)
  208.             DPSRemoveTimedEntry(theTimer);
  209.     theTimer = DPSAddTimedEntry(period, 
  210.                 (DPSTimedEntryProc)aTimerProc,
  211.                 (void *)self, NX_BASETHRESHOLD);
  212.     return self;
  213. }
  214.  
  215. - doYourThing:(double)now;
  216. {
  217.     [theMessage setStringValue: "Sending data to Graphity..."];
  218.     NXPing();
  219.     [self graphData:nil];
  220.     [theMessage setStringValue: ""];
  221.     return self;
  222. }
  223.  
  224. @end
  225.