home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1994 June / NEBULA_SE.ISO / SourceCode / MiscKit / Examples / MiscTree_Browser / AppDelegate.m < prev    next >
Encoding:
Text File  |  1994-03-27  |  3.4 KB  |  145 lines

  1. //
  2. // AppDelegate.m -- Example of using category of MiscTree that allows a
  3. //        MiscTree to be a delegate for an NXBrowser
  4. //        Written by Scott Anguish Copyright (c) 1994 by Scott Anguish.
  5. //                Version 1.0.  All rights reserved.
  6. //
  7. //        This notice may not be removed from this source code.
  8. //
  9. //    This program is included in the MiscKit by permission from the author
  10. //    and its use is governed by the MiscKit license, found in the file
  11. //    "LICENSE.rtf" in the MiscKit distribution.  Please refer to that file
  12. //    for a list of all applicable permissions and restrictions.
  13. //    
  14.  
  15. #import "AppDelegate.h"
  16.  
  17. @implementation AppDelegate
  18.  
  19.     NXStream    *localStream;
  20.         id    theLine;
  21.         id    root;
  22.  
  23.  
  24. -appDidInit:sender
  25. {
  26.     [self loadTree:self];
  27.     [theBrowser setDelegate:root];
  28.     [theBrowser setTarget:self];
  29.     [theBrowser setAction:@selector(didClick:)];
  30.     [theBrowser loadColumnZero];
  31.  
  32.     return self;
  33. }
  34.  
  35. - didClick:sender
  36. {
  37.     id    theNode;
  38.     id    pathString;
  39.     
  40.     theNode=[root returnSelectedNode:sender];
  41.     [selectedLabelNode setStringValue:[theNode label]];
  42.     pathString=[root returnSelectedPath:sender];
  43.     [selectedLabelPath setStringValue:[pathString stringValue]];
  44.     
  45.     return self;
  46. }
  47.  
  48.     int    ParseTree(id lastLimb, int currentLevel, id lastPlanting);
  49.  
  50. - loadTree:sender
  51.     {
  52.         id    applicationLocation;
  53.         char path[MAXPATHLEN+1];
  54.         
  55.         applicationLocation=[NXBundle mainBundle];
  56.         if ([applicationLocation getPath:path forResource:"MiscKit" ofType:".tree"])
  57.             {
  58.             localStream=NXMapFile(path,NX_READONLY);
  59.             root = [[MiscTree alloc] init];
  60.             ParseTree(root,-1,root);
  61.             
  62.             NXClose(localStream);
  63.             }
  64.         return self;
  65.     };
  66.  
  67.  
  68.  
  69.  
  70.     char *NXGets(char *s, int n, NXStream *stream)
  71.     {
  72.         int c, i = 0;
  73.         
  74.         while (((c = NXGetc(stream)) != EOF) && (i < n-1)) {
  75.             s[i++] = c;
  76.             if (c == '\n')
  77.                 break;
  78.         }
  79.         s[i++] = 0;
  80.         
  81.         return i > 1 ? s : NULL;
  82.     }
  83.  
  84.     id NXGetsString(NXStream *stream)
  85.     {
  86.         char    theLine[BUFSIZ];
  87.         id    theString;
  88.  
  89.         theString=[[MiscString alloc] init];
  90.         [theString setStringValue:NXGets(theLine,BUFSIZ, stream)];
  91.         return (theString);
  92.     }
  93.     
  94.     
  95.     
  96.     int    ParseTree(id lastLimb, int currentLevel, id lastPlanting)
  97.     {
  98.         int    indentLevel;
  99.         id    newLeaf;
  100.         id    lastLeaf;
  101.         int    returnLevel;
  102.         
  103.         lastLeaf=lastPlanting;
  104.         while (YES)
  105.             {
  106.             
  107.             theLine=NXGetsString(localStream);
  108.             if ((!theLine) || ([theLine emptyString])) {return (-1);};
  109.             indentLevel=[theLine numOfChar:'\t'];
  110.             [theLine replaceEveryOccurrenceOfChars:"\t" with:""];
  111.             [theLine replaceEveryOccurrenceOfChars:"\n" with:""];
  112.             if (indentLevel < currentLevel) {return (indentLevel);};
  113.             if (indentLevel > currentLevel)
  114.                 {
  115.                     newLeaf=[[MiscTree alloc] initLabelString:theLine];
  116.                     [lastLeaf addBranch:newLeaf];
  117.                     returnLevel=ParseTree(lastLeaf,indentLevel,newLeaf);
  118.                     if (returnLevel != currentLevel) {return (returnLevel);};
  119.                     if ((!theLine) || ([theLine emptyString])) {return (-1);};
  120.                     //if we got here, then we need to take the currentLine
  121.                     //and add it anyways, so we fall through
  122.                     // My specific question is what you would suggest here... I'm using 
  123.                     // theLine as a variable in this class only... I have the other option
  124.                     // of passing the MiscString as a param in ParseTree, so I could trash that 
  125.                     // variable, and make it part of the MiscTree without having a 'global-ish' 
  126.                     // variable on my mind
  127.                 }
  128.             newLeaf=[[MiscTree alloc] initLabelString:theLine];
  129.             [lastLimb addBranch:newLeaf];
  130.             lastLeaf=newLeaf;
  131.             } // while(YES)
  132.     };
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144. @end
  145.