home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / OPENSTEP / Connectivity / OmniDNSHelper-1.0b3 / WindowController.m < prev   
Encoding:
Text File  |  1997-10-25  |  4.6 KB  |  247 lines

  1. /* WindowController.m created by andrew on Sat 05-Jul-1997 */
  2.  
  3.  
  4. #import "WindowController.h"
  5.  
  6. #import <Foundation/Foundation.h>
  7. #import <AppKit/AppKit.h>
  8.  
  9.  
  10. @implementation WindowController
  11.  
  12. #define DOCUMENT_START_X    (150.0)
  13. #define DOCUMENT_START_Y    ( 20.0)
  14. #define DOCUMENT_DELTA_X    ( 20.0)
  15. #define DOCUMENT_DELTA_Y    ( 25.0)
  16.  
  17. static NSString *saveString = nil;
  18. static NSString *abandonChangesString = nil;
  19. static NSString *cancelString = nil;
  20. static NSString *closingUnsavedWindowString = nil;
  21.  
  22. + (void)initialize;
  23. {
  24.     static BOOL initialized = NO;
  25.  
  26.     [super initialize];
  27.  
  28.     if (initialized)
  29.         return;
  30.  
  31.     initialized = YES;
  32.  
  33.     saveString = NSLocalizedString (@"Save", save prompt);
  34.     abandonChangesString = NSLocalizedString (@"Don't Save", abandon changes prompt);
  35.     cancelString = NSLocalizedString (@"Cancel", cancel prompt);
  36.     closingUnsavedWindowString = NSLocalizedString (@"Save changes to %@?", alert user that window is closing and it has unsaved changes);
  37. }
  38.  
  39. + (NSPoint)locationForWindow:(NSWindow *)docWindow;
  40. {
  41.     static int count  = 0;
  42.     NSRect windowFrame;
  43.     NSRect mainScreenFrame;
  44.     NSPoint docPoint;
  45.  
  46.     mainScreenFrame = [[NSScreen mainScreen] frame];
  47.  
  48.     windowFrame = [docWindow frame];
  49.  
  50.     docPoint.x = DOCUMENT_START_X + (DOCUMENT_DELTA_X * count);
  51.     docPoint.y = mainScreenFrame.size.height - (windowFrame.size.height + DOCUMENT_START_Y + DOCUMENT_DELTA_Y * count);
  52.  
  53.     if (docPoint.x + windowFrame.size.width > mainScreenFrame.size.width || docPoint.y < 0.0) {
  54.         count = 0;
  55.         docPoint.x = mainScreenFrame.size.width + DOCUMENT_START_X;
  56.         docPoint.y = mainScreenFrame.size.height - (windowFrame.size.height + DOCUMENT_START_Y);
  57.     }
  58.     count++;
  59.  
  60.     return docPoint;
  61. }
  62.  
  63. + (NSString *)nibName;
  64. {
  65.     return @"ResolverWindow";
  66. }
  67.  
  68. + (NSString *)miniwindowImageName;
  69. {
  70.     return @"ResolverWindow";
  71. }
  72.  
  73. - init;
  74. {
  75.     [self initLazily];
  76.     [self loadIfNecessary];
  77.     
  78.     return self;
  79. }
  80.  
  81. - initLazily;
  82. {
  83.     [super init];
  84.     
  85.     return self;
  86. }
  87.  
  88. - (void)dealloc;
  89. {
  90.     [window release];
  91.     [miniwindowImage release];
  92.     [super dealloc];
  93. }
  94.  
  95. - (void)load;
  96. {
  97.     NSPoint aPoint;
  98.     NSString *imageName;
  99.  
  100.     [NSBundle loadNibNamed:[[self class] nibName] owner:self];
  101.     aPoint = [[self class] locationForWindow:window];
  102.     [window setFrameOrigin:aPoint];
  103.     [window setDelegate:self];
  104.  
  105.     imageName = [[self class] miniwindowImageName];
  106.     if (imageName)
  107.         miniwindowImage = [[NSImage imageNamed:imageName] retain];
  108. }
  109.  
  110. - (void)loadIfNecessary;
  111. {
  112.     if (!window)
  113.         [self load];
  114. }
  115.  
  116. - (void)display;
  117. {
  118.     [self loadIfNecessary];
  119.     [window makeKeyAndOrderFront:nil];
  120. }
  121.  
  122. - (BOOL)shouldClose;
  123. {
  124.     return [self windowShouldClose:window];
  125. }
  126.  
  127. - (void)close;
  128. {
  129.     [window close];
  130. }
  131.  
  132. // save support
  133.  
  134. - (void)prepareToSaveChanges;
  135. {
  136. }
  137.  
  138. - (void)saveChanges;
  139. {
  140. }
  141.  
  142. - (void)finishedSavingChanges;
  143. {
  144.     [window setDocumentEdited:NO];
  145. }
  146.  
  147. - (void)save;
  148. {
  149.     NS_DURING {
  150.         [self prepareToSaveChanges];
  151.         [self saveChanges];
  152.         [self finishedSavingChanges];
  153.     } NS_HANDLER {
  154.         NSLog (@"Save failed: %@", [localException reason]);
  155.     } NS_ENDHANDLER;
  156. }
  157.  
  158. - (void)save:sender;
  159. {
  160.     NS_DURING {
  161.         [self save];
  162.     } NS_HANDLER {
  163.     } NS_ENDHANDLER;
  164. }
  165.  
  166. // revert support
  167.  
  168. - (void)prepareToRevertChanges;
  169. {
  170. }
  171.  
  172. - (void)revertChanges;
  173. {
  174. }
  175.  
  176. - (void)finishedRevertingChanges;
  177. {
  178.     [window setDocumentEdited:NO];
  179. }
  180.  
  181. - (void)revert;
  182. {
  183.     [self prepareToRevertChanges];
  184.     [self revertChanges];
  185.     [self finishedRevertingChanges];
  186. }
  187.  
  188. - (void)revert:sender;
  189. {
  190.     NS_DURING {
  191.         [self revert];
  192.     } NS_HANDLER {
  193.     } NS_ENDHANDLER;
  194. }
  195.  
  196. @end
  197.  
  198.  
  199. @implementation WindowController (NSControlDelegate)
  200.  
  201. - (void)controlTextDidBeginEditing:(NSNotification *)notification;
  202. {
  203.     [window setDocumentEdited:YES];
  204. }
  205.  
  206. @end
  207.  
  208.  
  209. @implementation WindowController (NSWindowDelegate)
  210.  
  211. - (BOOL)windowShouldClose:sender;
  212. {
  213.     BOOL shouldClose;
  214.  
  215.     shouldClose = YES;
  216.  
  217.     [window makeFirstResponder:nil];
  218.     if ([window isDocumentEdited]) {
  219.         [window makeKeyAndOrderFront:nil];
  220.         switch (NSRunAlertPanel (nil, closingUnsavedWindowString, saveString, abandonChangesString, cancelString, [window title])) {
  221.             
  222.             case NSAlertDefaultReturn:
  223.                 NS_DURING {
  224.                     [self save];
  225.                 } NS_HANDLER {
  226.                     shouldClose = NO;
  227.                 } NS_ENDHANDLER;
  228.                 break;
  229.                 
  230.             case NSAlertAlternateReturn:
  231.                 break;
  232.                 
  233.             default:    // cancel
  234.                 shouldClose = NO;
  235.                 break;
  236.         }
  237.     }
  238.             
  239.     return shouldClose;
  240. }
  241.  
  242. - (void)windowWillClose:(NSNotification *)notification;
  243. {
  244. }
  245.  
  246. @end
  247.