home *** CD-ROM | disk | FTP | other *** search
/ Super Net 1 / SUPERNET_1.iso / PC / OTROS / UNIX / ARCHIE / CLIENTS / NEXTARCH / MYOPENPA.M < prev    next >
Encoding:
Text File  |  1992-01-21  |  6.7 KB  |  298 lines

  1. #import "MyOpenPanel.h"
  2.  
  3. #import <appkit/Application.h>
  4. #import <appkit/Button.h>
  5. #import <appkit/Matrix.h>
  6. #import <appkit/NXBrowser.h>
  7. #import <appkit/NXBrowserCell.h>
  8. #import <appkit/SavePanel.h>
  9. #import <appkit/Text.h>
  10. #import <appkit/TextField.h>
  11. #import <appkit/appkit.h>
  12. #import <appkit/nextstd.h>
  13. #import <string.h>
  14. #import <sys/types.h>
  15. #import <sys/stat.h>
  16. #import <stdlib.h>
  17.  
  18. #define CANCEL 0
  19. #define OK 1
  20.  
  21. static int lastColumn = 0;
  22. static char path[MAXPATHLEN], completedPath[MAXPATHLEN];
  23. static char *completionName;
  24.  
  25. @implementation MyOpenPanel
  26.  
  27. - init
  28. {
  29.     [super init];
  30.     [NXApp loadNibSection: "OpenPanel.nib" owner: self];
  31.  
  32.     dirLevel = 0;
  33.     fileCount = 0;
  34.  
  35.     return self;
  36. }
  37.     
  38. - goHome: sender
  39. {
  40.     strcpy(pathName,NXHomeDirectory());
  41.     [browser setPath : pathName];
  42.     [form setStringValue : NULL at : 0];
  43.     return self;
  44. }
  45.  
  46. - cancel:sender
  47. {
  48.     [NXApp stopModal : CANCEL];
  49.     return self;
  50. }
  51.  
  52. - ok:sender
  53. {
  54.     [NXApp stopModal : OK];
  55.     return self;
  56. }
  57.  
  58. - doubleClick
  59. {
  60.     if(isDirectory == YES && dirFlag == NO)
  61.         return self;
  62.     return [self ok : self];
  63. }
  64.  
  65. - setTitle : (char *) theTitle
  66. {
  67.     [panelTitle setStringValue : theTitle];
  68.     return self;
  69. }
  70.  
  71. - (const char *) pathName
  72. {
  73.     return (const char *) pathName;
  74. }
  75.  
  76. - (int) runModal: (BOOL) selectDirs
  77. {
  78.     return [self runModal: NULL : selectDirs];
  79. }
  80.  
  81. - (int) runModal: (char *) directory :(BOOL) selectDirs
  82. {
  83. int rtnVal;
  84. char *currentDir;
  85.  
  86.     dirFlag = selectDirs;
  87.     if(directory == NULL)
  88.         getwd(pathName);
  89.     else
  90.         strcpy(pathName,directory);
  91.     [form setStringValue : NULL at : 0];
  92.     [browser useScrollButtons : YES];
  93.     [browser setTarget : self];
  94.     [browser setAction : @selector(getSelectionPath)];
  95.     [browser setDoubleAction : @selector(doubleClick)];
  96.     [browser loadColumnZero];
  97.     [browser setPath: pathName];
  98.  
  99.     currentDir = strrchr(pathName,'/');
  100.     [form setStringValue : ++currentDir at : 0];
  101.     [form selectTextAt: 0];
  102.  
  103.     [panel orderFront : self];
  104.     while ( (rtnVal = [NXApp runModalFor : panel]) == NX_RUNCONTINUES );
  105.     [panel orderOut : self];
  106.  
  107.     return rtnVal;
  108. }
  109.  
  110. /* Return only the normally visible files */
  111. int SelectFiles(struct direct *entry)
  112. {
  113.     if(entry->d_name[0] == '.')
  114.         return 0;
  115.     else
  116.         return 1;
  117. }
  118.  
  119. /* Case insenseitive alpha sort */
  120. int AlphaSort(struct direct **d1,struct direct **d2)
  121. {
  122.     return strcasecmp((*d1)->d_name,(*d2)->d_name);
  123. }
  124.  
  125. /* Browser delegate methods */
  126. /* This method returns the number of visible files in the directory corresponding to column */
  127. - (int) browser : sender getNumRowsInColumn : (int) column
  128. {
  129. struct direct **newList;
  130.  
  131.     if(column > 0)
  132.     {
  133.         [sender getPath : path toColumn : column];
  134.         fileCount = scandir(path, &newList,SelectFiles, AlphaSort);
  135.     }
  136.     else
  137.         fileCount = scandir("/", &newList,SelectFiles, AlphaSort);
  138.  
  139.     if(fileCount >= 0)
  140.     {
  141.     int i;
  142.         if(column > lastColumn)
  143.             lastColumn = column;
  144.         if(nameList[column])
  145.             for(i = 0; i < nameCount[column]; i ++)
  146.                 free(nameList[column][i]);
  147.         nameList[column] = newList;
  148.         nameCount[column] = fileCount;
  149.     }
  150.  
  151.     return fileCount;
  152. }
  153.  
  154. /* Lazy creation of the browser items */
  155.  - browser : sender loadCell : cell atRow : (int)row inColumn : (int)column
  156. {
  157. struct stat fileInfo;
  158. char fullPathname[MAXPATHLEN];
  159.  
  160.     if(column > 0)
  161.     {
  162.         [sender getPath : path toColumn : column];
  163.         sprintf(fullPathname,"%s/%s",path,nameList[column][row]->d_name);
  164.     }
  165.     else
  166.         sprintf(fullPathname,"/%s",nameList[column][row]->d_name);
  167.  
  168.     /* A check to see if this file exits, and it should */
  169.     if(stat(fullPathname,&fileInfo) != 0)
  170.     {
  171.         perror(fullPathname);
  172.         NXBeep();
  173.         printf("col = %d\n",column);
  174.         return self;
  175.     }
  176.  
  177.     [cell setLoaded : YES];
  178.     [cell setStringValue : nameList[column][row]->d_name];
  179.     if( fileInfo.st_mode & S_IFDIR)    /* A directory file */
  180.         [cell setLeaf : NO];
  181.     else if(dirFlag == YES)
  182.     {                                /* Disable normal files */
  183.         [cell setLeaf : YES];
  184.         [cell setEnabled: NO];
  185.     }
  186.     else
  187.         [cell setLeaf : YES];
  188.  
  189.     return self;
  190. }
  191.  
  192. - getSelectionPath
  193. {
  194. int column;
  195. id matrix;
  196. struct stat fileInfo;
  197.  
  198.     column = [browser selectedColumn];
  199.     [browser getPath : path toColumn : column];
  200.     matrix = [browser matrixInColumn : column];
  201.     sprintf(pathName,"%s/%s",path,[[matrix selectedCell] stringValue]);
  202.     /* See if the file is a directory */
  203.     isDirectory = NO;
  204.     if(stat(pathName,&fileInfo) != 0)
  205.         perror(pathName);
  206.     if(fileInfo.st_mode & S_IFDIR)
  207.     {    /* A directory file */
  208.         isDirectory = YES;
  209.         if(dirFlag == YES)    /* Display it if directory selection is allowed */
  210.             [form setStringValue : [[matrix selectedCell] stringValue] at : 0];
  211.     }
  212.     else
  213.         [form setStringValue : [[matrix selectedCell] stringValue] at : 0];
  214.     [form selectTextAt: 0];
  215.  
  216.     return self;
  217. }
  218.  
  219. /* Form's text delegate methods */
  220. - (BOOL) textWillEnd : sender
  221. {
  222. int length,column;
  223. char subdirName[128];
  224. struct stat fileInfo;
  225.  
  226. NXLogError("textWillEnd_0: %s\n",pathName);
  227.     length = [sender textLength];
  228.     if(length > 0)
  229.         [sender getSubstring: subdirName start: 0 length: 127];
  230.     else
  231.         return YES;
  232.  
  233.     subdirName[128] = '\0';
  234.  
  235. NXLogError("textWillEnd_1: %s/%s\n",pathName, subdirName);
  236.     column = [browser selectedColumn];
  237.     [browser getPath : path toColumn : column];
  238.     sprintf(pathName,"%s/%s",path, subdirName);
  239.     /* Check the path to make sure that what the user typed is valid */
  240.     if(stat(pathName,&fileInfo) != 0)
  241.     {
  242. NXLogError("textWillEnd_1: %s/%s\n",pathName, subdirName);
  243.         /* See if the user tried to specify a full pathname */
  244.         if(stat(subdirName,&fileInfo) != 0)
  245.             return YES;
  246.         strcpy(pathName, subdirName);
  247.     }
  248.     return NO;
  249. }
  250.  
  251. #ifdef FILE_COMPLETION
  252. - textDidGetKeys: sender isEmpty: (BOOL)flag
  253. {
  254. char name[32],*cellName;
  255. int count;
  256.     fprintf(stderr,"length = %d\n",[sender getSubstring: name start: 0 length: 128]);
  257.     sprintf(completedPath,"%s/%s",pathName,name);
  258.     count = NXCompleteFilename(completedPath, MAXPATHLEN);
  259.     cellName = strrchr(completedPath,'/');
  260.     if(count == 0 || flag == YES)
  261.     {
  262.         completionName = NULL;
  263.         [okButton setEnabled: NO];
  264.         return self;
  265.     }
  266.     completionName = ++cellName;
  267. fprintf(stderr,"textDidGetKeys: partial = %s; count = %d", completedPath,count);
  268.     if(cellName[1] != '\0' && count > 0)
  269.         [browser setPath: completedPath];
  270.     return self;
  271. }
  272.  
  273. - (BOOL)browser:sender selectCell:(const char *)entry inColumn:(int)column
  274. {
  275. id cell,matrix;
  276. int row;
  277. fprintf(stderr,"selectCell: %s\n",entry);
  278.     row = 0;
  279.     cell = [browser getLoadedCellAtRow: row inColumn: column];
  280.     while( cell != nil && strcmp([cell stringValue],entry) != 0)
  281.     {
  282.         cell = [sender getLoadedCellAtRow: ++row inColumn: column];
  283.     }
  284.     if(cell == nil)
  285.         return NO;
  286.         
  287.     matrix = [browser matrixInColumn: column];
  288.     [matrix selectCellAt: row : 0];
  289.     
  290.     if(completionName != NULL && strcmp(completionName,entry) == 0)
  291.         return NO;
  292.  
  293.     return YES;
  294. }
  295. #endif
  296.  
  297. @end
  298.