home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / UNIX / Programming / GDBbundle-1.0-MIS / src / TextEdit / Controller.m < prev    next >
Encoding:
Text File  |  1997-08-16  |  7.0 KB  |  211 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. #if 0
  80. - (BOOL)applicationOpenUntitledFile:(NSApplication *)sender {
  81.     return [Document openUntitled];
  82. }
  83. #endif
  84.  
  85. - (BOOL)application:(NSApplication *)sender printFile:(NSString *)filename {
  86.     BOOL retval = NO;
  87.     BOOL releaseDoc = NO;
  88.     Document *document = [Document documentForPath:filename];
  89.     
  90.     if (!document) {
  91.         document =  [[Document alloc] initWithPath:filename encoding:UnknownStringEncoding uniqueZone:NO];
  92.         releaseDoc = YES;
  93.     }
  94.     if (document) {
  95.         BOOL useUI = [NSPrintInfo defaultPrinter] ? NO : YES;
  96.  
  97.         [document printDocumentUsingPrintPanel:useUI];
  98.         retval = YES;
  99.  
  100.         if (releaseDoc) {
  101.             // If we created it, we get rid of it.
  102.             [document release];
  103.         }
  104.     }
  105.     return retval;
  106. }
  107.  
  108. - (void)createNew:(id)sender {
  109.     (void)[Document openUntitled];
  110. }
  111.  
  112. - (void)open:(id)sender {
  113.     (void)[Document open:sender];
  114. }
  115.  
  116. - (void)saveAll:(id)sender {
  117.     NSArray *windows = [NSApp windows];
  118.     unsigned count = [windows count];
  119.     while (count--) {
  120.         NSWindow *window = [windows objectAtIndex:count];
  121.         Document *document = [Document documentForWindow:window];
  122.         if (document) {
  123.             if ([document isDocumentEdited]) {
  124.                 if (![document saveDocument:NO]) return;
  125.             }
  126.         }
  127.     }
  128. }
  129.  
  130. /*** Info Panel related stuff ***/
  131.  
  132. - (void)showInfoPanel:(id)sender {
  133.     if (!infoPanel) {
  134.         if (![NSBundle loadNibNamed:@"Info" owner:self])  {
  135.             NSLog(@"Failed to load Info.nib");
  136.             NSBeep();
  137.             return;
  138.         }
  139.     [infoPanel center];
  140.     }
  141.     [infoPanel makeKeyAndOrderFront:nil];
  142. }
  143.  
  144. - (void)setVersionField:(id)versionField {
  145.     extern char TextEdit_VERS_NUM[];
  146.     if (strlen(TextEdit_VERS_NUM) > 0) {
  147.         NSString *versionString = [NSString stringWithFormat:NSLocalizedString(@"Release 4 (v%s)", @"Version string.  %s is replaced by the version number."), TextEdit_VERS_NUM];
  148.         [versionField setStringValue:versionString];
  149.     }
  150. }
  151.  
  152. - (void)applicationWillFinishLaunching:(NSNotification *)aNotification
  153. {
  154.     NSString* name = @"TextEdit";
  155.  
  156.     gdbRootConnection = [NSConnection new];
  157.     [gdbRootConnection setRootObject:self];
  158.     if (![gdbRootConnection registerName:name])
  159.         NSLog (@"cannot register name %@", name);
  160.     [gdbRootConnection setDelegate:self];
  161. }
  162.  
  163. - (BOOL)connection:(NSConnection*)parentConnection
  164.   shouldMakeNewConnection:(NSConnection*)newConnnection
  165. {
  166.     if (!gdbDisplayController) {
  167.         NSBundle* mainBundle = [NSBundle mainBundle];
  168.         id path = [mainBundle pathForResource:@"GdbBundle" ofType:@"bundle"];
  169.         Class gdbDisplayControllerClass;
  170.         NSBundle* gdbBundle;
  171.  
  172.         gdbBundle = [NSBundle bundleWithPath:path];
  173.         if (!gdbBundle) {
  174.             NSLog (@"Cannot find the GDB bundle!");
  175.             return NO;
  176.         }
  177.         gdbDisplayControllerClass
  178.             = [gdbBundle classNamed:@"GdbDisplayController"];
  179.         gdbDisplayController = [gdbDisplayControllerClass new];
  180.         if (![NSBundle loadNibNamed:@"GdbController"
  181.                               owner:gdbDisplayController]) {
  182.             NSLog (@"Cannot load GdbController NIB file!");
  183.             return NO;
  184.         }
  185.     }
  186.     return YES;
  187. }
  188.  
  189. - (void)connectionDidDie:(NSNotification*)notification
  190. {
  191.     NSConnection* connection = [[notification userInfo] objectForKey:@"object"];
  192.     id mainMenu = [NSApp mainMenu];
  193.  
  194.     NSLog (@"connection died: connection = %x", connection);
  195.     [[NSNotificationCenter defaultCenter] removeObserver:self];
  196.     [mainMenu removeItem:[mainMenu itemWithTitle:GDB_MENU_ITEM]];
  197.     [gdbDisplayController close];
  198.     [gdbDisplayController release];
  199.     gdbDisplayController = nil;
  200. }
  201.  
  202. @end
  203.  
  204. /*
  205.  
  206.  1/28/95 aozer    Created for Edit II.
  207.  7/21/95 aozer    Command line file names
  208.  
  209. */
  210.  
  211.