home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.bin / SourceCode / AlexNeXTSTEPSource / Source / Chapter9_Text / Words / TextController.m < prev    next >
Encoding:
Text File  |  1993-03-21  |  2.7 KB  |  115 lines

  1. #import <appkit/appkit.h>
  2. #import "Document.h"
  3. #import "TextController.h"
  4.  
  5. // this class is responsible for managing
  6. // the savepanel and openpanel
  7. // it is also responsible for responding
  8. // to remote messages from the Workspace
  9.  
  10. @implementation TextController
  11.  
  12. // automatically sent after the .nib file
  13. // has finished loading
  14. - awakeFromNib
  15. {
  16.     // using the new method first creates
  17.     // the objects; since there is only one
  18.     // openpanel and savepanel per application,
  19.     // subsequent invocations return the
  20.     // existing savepanel and openpanel,
  21.     // as appropriate
  22.     id savePanel = [SavePanel new];
  23.     id openPanel = [OpenPanel new];
  24.     // by default, an openpanel only allows
  25.     // one file to be opened
  26.     [openPanel allowMultipleFiles:YES];
  27.     return self;
  28. }
  29.  
  30. // create a new document in response
  31. // to the New menu option
  32. - newDocument:sender
  33. {
  34.     id document = [[Document alloc] init];
  35.     [document showDocument];
  36.     [[document window] setTitle:UNTITLED];
  37.     return self;
  38. }
  39.  
  40. // open the document in response to
  41. // the Open menu option
  42. - showOpenPanel:sender
  43. {
  44.     const char *file, *directory;
  45.     const char *const *filenames;
  46.     static const char
  47.         *const wordTypes[] = {FILE_EXTENSION, NULL};
  48.     char fullPathName[MAXPATHLEN];
  49.     id openPanel = [OpenPanel new];
  50.  
  51.     // ensure that the openpanel allows multiple
  52.     // files to be selected
  53.     [openPanel allowMultipleFiles:YES];
  54.     // display only files with "word" extension
  55.     if ([openPanel runModalForTypes:wordTypes])
  56.         {
  57.         // get list of filename(s) selected
  58.         // the filenames method returns a
  59.         // a pointer to all the strings
  60.         filenames = [openPanel filenames];
  61.         // get directory first
  62.         directory = [openPanel directory];
  63.         do
  64.             {
  65.             // get filename
  66.             file = *(filenames++);
  67.             // construct entire pathname
  68.             strcpy(fullPathName, directory);
  69.             // append directory to filename
  70.             strcat(fullPathName, "/");
  71.             strcat(fullPathName, file);
  72.             [[Document alloc]
  73.                 initDocumentFromFile:fullPathName];
  74.             }
  75.         while (*(filenames) != NULL);
  76.             }
  77.     return self;
  78. }
  79.  
  80. // save the document in response to
  81. // the Save menu option
  82. - showSavePanel:sender
  83. {
  84.     const char *wordType = FILE_EXTENSION;
  85.     const char *fullPathName, *title;
  86.     id document = [[NXApp mainWindow] delegate];
  87.     id savePanel = [SavePanel new];
  88.  
  89.     // make sure there is a document first
  90.     if (document)
  91.         {
  92.         // if document has not been saved
  93.         // then present savepanel to get filename
  94.         title = [[document window] title];
  95.         if (strcmp(title, UNTITLED) == 0)
  96.             {
  97.             [savePanel setRequiredFileType:wordType];
  98.             if ([savePanel runModal])
  99.                 {
  100.                 fullPathName = [savePanel filename];
  101.                 return [document saveDocumentToFile:
  102.                     fullPathName];
  103.                 }
  104.             else
  105.                 return nil;
  106.             }
  107.         // else just save the file
  108.         else
  109.             return [document saveDocumentToFile:title];
  110.         }
  111.     return self;
  112. }
  113.  
  114. @end
  115.