home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / Rhapsody / Graphics / Morph-2.0a / AppController.m < prev    next >
Encoding:
Text File  |  1998-01-25  |  3.9 KB  |  143 lines

  1. #import "AppController.h"
  2. #import "MorphDocument.h"
  3. #import "XMovieDocument.h"
  4. #import "InspectorController.h"
  5.  
  6. static NSString *curDirectory = nil;    /* Directory for open panel */
  7.  
  8. @implementation AppController
  9.  
  10. - init
  11. {
  12.     self = [super init];
  13.     documentClasses = [[NSMutableArray allocWithZone:[self zone]] initWithCapacity:3];
  14.     [documentClasses addObject:[MorphDocument class]];
  15.     [documentClasses addObject:[XMovieDocument class]];
  16.     return self;
  17. }
  18.  
  19. - (void)applicationDidFinishLaunching:(NSNotification *)notification
  20. {
  21.     if ([[NSApplication sharedApplication] mainWindow] == nil)
  22.         [self newDocument:nil];
  23. }
  24.  
  25. - (void)dealloc
  26. {
  27.     [[NSUserDefaults standardUserDefaults] synchronize];
  28.     [documentClasses release];
  29.     if ( infoPanel )
  30.         [infoPanel release];
  31.     if ( preferencesPanel )
  32.         [preferencesPanel release];
  33.     [super dealloc];
  34. }
  35.  
  36. - (NSArray *) documentClasses
  37. {
  38.     return documentClasses;
  39. }
  40.  
  41. - (void) registerDocumentClass:(Class) class
  42. {
  43.     [documentClasses addObject:documentClasses];
  44. }
  45.  
  46. - (void)showInfoPanel:sender
  47. {
  48.     if (!infoPanel)
  49.         [NSBundle loadNibNamed:@"Info.nib" owner:self];
  50.     [infoPanel makeKeyAndOrderFront:self];
  51. }
  52.  
  53. - (void)showInspectorPanel:sender
  54. {
  55.     InspectorController *insp = [InspectorController sharedInspectorController];
  56.     [[insp panel] orderFront:nil];
  57.     [insp revert:nil];
  58. }
  59.  
  60. - (void)showPreferencesPanel:sender
  61. {
  62.     if (!preferencesPanel)
  63.         [NSBundle loadNibNamed:@"Preferences.nib" owner:self];
  64.     [preferencesPanel makeKeyAndOrderFront:self];
  65. }
  66.  
  67. - (void) newDocument:sender
  68. {
  69.     Document *newDocument = nil;
  70.  
  71.     if ([documentClasses count] > 0)
  72.         newDocument = [[[documentClasses objectAtIndex:0] allocWithZone:[self zone]] init];
  73.     [newDocument show];
  74. }
  75.  
  76. - (void)openDocument:sender
  77. {
  78.     Document *newDocument;
  79.     NSOpenPanel *openPanel;
  80.     NSArray *filenames;
  81.     int i, count;
  82.     int fileNum, classNum;
  83.     NSMutableSet    *fileTypes;
  84.     NSString        *fileType;
  85.  
  86.     openPanel = [NSOpenPanel openPanel];
  87.     [openPanel setCanChooseFiles:YES];
  88.     [openPanel setAllowsMultipleSelection:YES];
  89.     if ( curDirectory != nil )
  90.         [openPanel setDirectory:curDirectory];
  91.  
  92.     fileTypes = [NSMutableSet setWithCapacity:10];
  93.  
  94.     for (classNum = 0; classNum < [documentClasses count]; ++classNum)
  95.         [fileTypes unionSet:[[documentClasses objectAtIndex:classNum] readableFileTypes]];
  96.  
  97.     i = [openPanel runModalForTypes:[fileTypes allObjects]];
  98.     if ( i != NSOKButton )
  99.         return;
  100.     filenames = [openPanel filenames];
  101.     count = [filenames count];
  102.     for ( fileNum = 0; fileNum < count; ++fileNum )
  103.     {
  104.         newDocument = nil;
  105.         fileType = [[filenames objectAtIndex:fileNum] pathExtension];
  106.         for (classNum = 0; newDocument == nil && classNum < [documentClasses count]; ++classNum)
  107.         {
  108.             if ([[[documentClasses objectAtIndex:classNum] readableFileTypes] containsObject:fileType])
  109.                 newDocument = [[[documentClasses objectAtIndex:classNum] alloc] initWithContentsOfFile:[filenames objectAtIndex:fileNum]];
  110.         }
  111.         [newDocument show];
  112.     }
  113.     // Keep track of the 'current directory'
  114.     [curDirectory autorelease];
  115.     curDirectory = [[[filenames objectAtIndex:(count - 1)]
  116.         stringByDeletingLastPathComponent] copy];
  117. }
  118.  
  119.  
  120. /* NSApplication delegate methods */
  121. - (BOOL)application:(NSApplication *)application
  122.            openFile:(NSString *)filename
  123. {
  124.     Document *newDocument = nil;
  125.     int classNum;
  126.     NSString *fileType = [filename pathExtension];
  127.  
  128.     for (classNum = 0; newDocument == nil && classNum < [documentClasses count]; ++classNum)
  129.     {
  130.         if ([[[documentClasses objectAtIndex:classNum] readableFileTypes] containsObject:fileType])
  131.             newDocument = [[[documentClasses objectAtIndex:classNum] alloc] initWithContentsOfFile:filename];
  132.     }
  133.     if ( newDocument == nil )
  134.     {
  135.         return NO;
  136.     }
  137.     [newDocument show];
  138.  
  139.     return YES;
  140. }
  141.  
  142. @end
  143.