home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 4.2J (Developer) / os42jdev.iso / NextDeveloper / Examples / AppKit / TextEdit / Controller.m < prev    next >
Text File  |  1996-08-12  |  5KB  |  159 lines

  1. /*
  2.         Controller.m
  3.     Copyright (c) 1995-1996, NeXT Software, Inc.
  4.         All rights reserved.
  5.         Author: Ali Ozer
  6.  
  7.     You may freely copy, distribute and reuse the code in this example.
  8.     NeXT disclaims any warranty of any kind, expressed or implied,
  9.     as to its fitness for any particular use.
  10.  
  11.        Central controller object for Edit...
  12. */
  13.  
  14. #import <AppKit/AppKit.h>
  15. #import "Controller.h"
  16. #import "Document.h"
  17. #import "Preferences.h"
  18.  
  19. @implementation Controller
  20.  
  21. - (BOOL)applicationShouldTerminate:(NSApplication *)app {
  22.     NSArray *windows = [app windows];
  23.     unsigned count = [windows count];
  24.     BOOL needsSaving = NO;
  25.  
  26.     // Determine if there are any unsaved documents...
  27.  
  28.     while (!needsSaving && count--) {
  29.         NSWindow *window = [windows objectAtIndex:count];
  30.         Document *document = [Document documentForWindow:window];
  31.         if (document && [document isDocumentEdited]) needsSaving = YES;
  32.     }
  33.  
  34.     if (needsSaving) {
  35.         int choice = NSRunAlertPanel(NSLocalizedString(@"Quit", @"Title of alert panel which comes up when user chooses Quit and there are unsaved documents."), 
  36.             NSLocalizedString(@"You have unsaved documents.", @"Message in the alert panel which comes up when user chooses Quit and there are unsaved documents."), 
  37.             NSLocalizedString(@"Review Unsaved", @"Choice (on a button) given to user which allows him/her to review all unsaved documents if he/she quits the application without saving them all first."), 
  38.             NSLocalizedString(@"Quit Anyway", @"Choice (on a button) given to user which allows him/her to quit the application even though there are unsaved documents."), 
  39.             NSLocalizedString(@"Cancel", @"Button choice allowing user to cancel."));
  40.         if (choice == NSAlertOtherReturn)  {        /* Cancel */
  41.             return NO;
  42.         } else if (choice != NSAlertAlternateReturn) {    /* Review unsaved; Quit Anyway falls through */
  43.             count = [windows count];
  44.             while (count--) {
  45.                 NSWindow *window = [windows objectAtIndex:count];
  46.                 Document *document = [Document documentForWindow:window];
  47.                 if (document) {
  48.                     [window makeKeyAndOrderFront:nil];
  49.                     if (![document canCloseDocument]) {
  50.                         return NO;
  51.                     }            
  52.                 }
  53.             }
  54.         }
  55.     }
  56.     [Preferences saveDefaults];
  57.     return YES;
  58. }
  59.  
  60. - (BOOL)application:(NSApplication *)sender openFile:(NSString *)filename {
  61. #ifdef WIN32
  62.     /* If the document is in a .rtfd and it's name is TXT.rtf or index.rtf, open the parent dir... This is because on windows it doesn't seem trivial to double-click to open folders as documents.
  63.     */
  64.     NSString *parentDir = [filename stringByDeletingLastPathComponent];
  65.     if ([[[parentDir pathExtension] lowercaseString] isEqualToString:@"rtfd"]) {
  66.         NSString *lastPathComponent = [[filename lastPathComponent] lowercaseString];
  67.         if ([lastPathComponent isEqualToString:@"txt.rtf"] || [lastPathComponent isEqualToString:@"index.rtf"]) {
  68.         filename = parentDir;
  69.         }
  70.     }
  71. #endif
  72.     return [Document openDocumentWithPath:filename encoding:UnknownStringEncoding] ? YES : NO;
  73. }
  74.  
  75. - (BOOL)application:(NSApplication *)sender openTempFile:(NSString *)filename {    /* ??? Why? */
  76.     return [Document openDocumentWithPath:filename encoding:UnknownStringEncoding] ? YES : NO;
  77. }
  78.  
  79. - (BOOL)applicationOpenUntitledFile:(NSApplication *)sender {
  80.     return [Document openUntitled];
  81. }
  82.  
  83. - (BOOL)application:(NSApplication *)sender printFile:(NSString *)filename {
  84.     BOOL retval = NO;
  85.     BOOL releaseDoc = NO;
  86.     Document *document = [Document documentForPath:filename];
  87.     
  88.     if (!document) {
  89.         document =  [[Document alloc] initWithPath:filename encoding:UnknownStringEncoding uniqueZone:NO];
  90.         releaseDoc = YES;
  91.     }
  92.     if (document) {
  93.         BOOL useUI = [NSPrintInfo defaultPrinter] ? NO : YES;
  94.  
  95.         [document printDocumentUsingPrintPanel:useUI];
  96.         retval = YES;
  97.  
  98.         if (releaseDoc) {
  99.             // If we created it, we get rid of it.
  100.             [document release];
  101.         }
  102.     }
  103.     return retval;
  104. }
  105.  
  106. - (void)createNew:(id)sender {
  107.     (void)[Document openUntitled];
  108. }
  109.  
  110. - (void)open:(id)sender {
  111.     (void)[Document open:sender];
  112. }
  113.  
  114. - (void)saveAll:(id)sender {
  115.     NSArray *windows = [NSApp windows];
  116.     unsigned count = [windows count];
  117.     while (count--) {
  118.         NSWindow *window = [windows objectAtIndex:count];
  119.         Document *document = [Document documentForWindow:window];
  120.         if (document) {
  121.             if ([document isDocumentEdited]) {
  122.                 if (![document saveDocument:NO]) return;
  123.             }
  124.         }
  125.     }
  126. }
  127.  
  128. /*** Info Panel related stuff ***/
  129.  
  130. - (void)showInfoPanel:(id)sender {
  131.     if (!infoPanel) {
  132.         if (![NSBundle loadNibNamed:@"Info" owner:self])  {
  133.             NSLog(@"Failed to load Info.nib");
  134.             NSBeep();
  135.             return;
  136.         }
  137.     [infoPanel center];
  138.     }
  139.     [infoPanel makeKeyAndOrderFront:nil];
  140. }
  141.  
  142. - (void)setVersionField:(id)versionField {
  143.     extern char TextEdit_VERS_NUM[];
  144.     if (strlen(TextEdit_VERS_NUM) > 0) {
  145.         NSString *versionString = [NSString stringWithFormat:NSLocalizedString(@"Release 4 (v%s)", @"Version string.  %s is replaced by the version number."), TextEdit_VERS_NUM];
  146.         [versionField setStringValue:versionString];
  147.     }
  148. }
  149.  
  150. @end
  151.  
  152. /*
  153.  
  154.  1/28/95 aozer    Created for Edit II.
  155.  7/21/95 aozer    Command line file names
  156.  
  157. */
  158.  
  159.