home *** CD-ROM | disk | FTP | other *** search
/ OpenStep (Enterprise) / OpenStepENTCD.toast / OEDEV / DEV.Z / ToDoController.m < prev    next >
Encoding:
Text File  |  1996-07-30  |  7.3 KB  |  239 lines

  1. /*
  2.   You may freely copy, distribute, and reuse the code in this example.
  3.   NeXT disclaims any warranty of any kind, expressed or implied, as to its
  4.   fitness for any particular use.
  5. */
  6.  
  7. #import "ToDoController.h"
  8. #import "ToDoDoc.h"
  9. #import "ToDoInspector.h"
  10. #import "ToDoItem.h"
  11.  
  12. extern void clearButtonMatrix(id matrix);
  13.  
  14. @implementation ToDoController
  15.  
  16. /******* DOCUMENT MANAGEMENT **********************************/
  17.  
  18. /******* Target/Action ****************************************/
  19.    
  20. - (void)newDoc:(id)sender
  21. {
  22.     id currentDoc = [[ToDoDoc alloc] initWithFile:nil];
  23.     [currentDoc activateDoc];
  24. }
  25.  
  26. - (void)openDoc:(id)sender
  27. {
  28.     int result;
  29.     NSString *selected, *startDir;
  30.     NSArray *fileTypes = [NSArray arrayWithObject:@"td"];
  31.     NSOpenPanel *oPanel = [NSOpenPanel openPanel];
  32.  
  33.     [oPanel setAllowsMultipleSelection:YES];
  34.     if ([[[NSApp keyWindow] delegate] isKindOfClass:[ToDoDoc class]]) 
  35.         startDir = [[[NSApp keyWindow] representedFilename] stringByDeletingLastPathComponent];
  36.     else
  37.         startDir = NSHomeDirectory();    
  38.  
  39.     result = [oPanel runModalForDirectory:startDir file:nil types:fileTypes];
  40.     if (result == NSOKButton) {
  41.         int i;
  42.         id currentDoc;
  43.         for (i=0; i<[[oPanel filenames] count]; i++) {
  44.               selected = [[oPanel filenames] objectAtIndex:i];
  45.               currentDoc = [[ToDoDoc alloc] initWithFile:selected];
  46.               [currentDoc activateDoc];
  47.               [currentDoc selectItem:0];
  48.         }
  49.     }
  50. }
  51.  
  52.  
  53. - (void)closeDoc:(id)sender
  54. {
  55.     if (inspector) [[inspector delegate] setCurrentItem:nil];
  56.     [[NSApp mainWindow] performClose:self];
  57. }
  58.  
  59.  
  60. - (void)saveDoc:(id)sender
  61. {
  62.     id currentDoc = [[NSApp mainWindow] delegate];
  63.     if (currentDoc) 
  64.         [currentDoc saveDoc];
  65. }
  66.  
  67. - (void)showInspector:(id)sender
  68. {
  69.     id currentDoc = [[NSApp mainWindow] delegate];
  70.     if (!inspector)
  71.         if(![NSBundle loadNibNamed:@"ToDoInspector" owner:self])
  72.             return;
  73.     if ([currentDoc isKindOfClass:[ToDoDoc class]])
  74.         [currentDoc selectItem:[[currentDoc itemMatrix] selectedRow]];            
  75.     [inspector makeKeyAndOrderFront:self];
  76. }
  77.  
  78. - (void)showPreferences:(id)sender
  79. {
  80.     /* Exercise */
  81.     
  82. }
  83.  
  84. - (void)showInfo:(id)sender
  85. {
  86.     /* Exercise? */
  87. }
  88.  
  89. - (id)inspector { return inspector; }
  90.  
  91. /******* Delegation ****************************************/
  92.  
  93. - (BOOL)applicationOpenUntitledFile:(NSApplication *)sender
  94. {
  95.     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  96.     NSString *docToOpen = [defaults stringForKey:@"ToDoDocumentLastSaved"];
  97.     if (![docToOpen isEqualToString:@"UNTITLED"] &&
  98.         [[NSFileManager defaultManager] fileExistsAtPath:docToOpen] &&
  99.         [[docToOpen pathExtension] isEqualToString:@"td"]) {
  100.         ToDoDoc *thisDoc = [[ToDoDoc alloc] initWithFile:docToOpen];
  101.         [thisDoc activateDoc];
  102.         [thisDoc selectItem:0];
  103.         return YES;
  104.     }
  105.     [self newDoc:self];    
  106.     return YES;
  107. }
  108.  
  109. - (BOOL)applicationShouldTerminate:(id)sender
  110. {
  111.     NSString *repfile = nil;
  112.     NSArray *appWindows = [NSApp windows];
  113.     NSEnumerator *enumerator = [appWindows objectEnumerator];
  114.     id object;
  115.  
  116.     while (object = [enumerator nextObject]) {
  117.         int result;
  118.         id doc;
  119.         if ((doc = [object delegate]) &&
  120.             [doc isKindOfClass:[ToDoDoc class]] &&
  121.             [object isDocumentEdited] ) {
  122.             repfile = [[NSApp keyWindow] representedFilename];
  123.             result = NSRunAlertPanel(@"To Do", @"Save %@?", @"Save", @"Don't Save", @"Cancel", ([repfile isEqualToString:@""]?@"UNTITLED":repfile));
  124.             switch(result) {
  125.               case NSAlertDefaultReturn:
  126.                 [doc saveDocItems];
  127.                 [doc saveDoc];
  128.                 break;
  129.               case NSAlertAlternateReturn:
  130.                 [[NSApp keyWindow] close];
  131.                 break;
  132.               case NSAlertOtherReturn:
  133.                 return NO;
  134.             }
  135.         }
  136.     }
  137.     return YES;
  138. }
  139.  
  140. - (void)applicationWillResignActive:(NSNotification *)notif
  141. {
  142.     id doc = [[NSApp keyWindow] delegate];
  143.     if ([doc isKindOfClass:[ToDoDoc class]]) {
  144.         [doc saveDocItems];
  145.     }
  146.  
  147. }
  148.  
  149. - (BOOL)application:(id)sender openFile:(NSString *)filename
  150. {
  151.     id currentDoc = [[ToDoDoc alloc] initWithFile:filename];
  152.     if (currentDoc) {
  153.         [currentDoc activateDoc];
  154.         return YES;
  155.     } else {
  156.         return NO;
  157.     }
  158. }
  159.  
  160. /*************** STARTUP *********************************************/
  161. + (void)initialize
  162. {
  163.     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  164.     NSDictionary *regdom = [NSDictionary dictionaryWithObject:@"UNTITLED" forKey:@"ToDoDocumentLastSaved"];
  165.     [defaults registerDefaults:regdom];
  166.  }
  167.  
  168. - init
  169. {
  170.     [super init];
  171.     [NSApp setDelegate:self];
  172.     return self;
  173. }
  174.  
  175. /******************************************************************************
  176. **  PASTEBOARD COPY/PASTE
  177. **  The following two methods enable users to cut or copy an item name in a  **
  178. **  text field and when they paste it in another field (in the same or       **
  179. **  other document) the associated ToDoItem is transferred as well. This     **
  180. **  capability depends on the interaction of the pasteboard server and the   **
  181. **  NSPasteboard class. To learn about the conceptual underpinnings of       **
  182. **  pasteboard copy/pase, read the NSPasteboard specification.               **
  183. **  NOTE: To enable this feature, the targets of the items on the Edit menu  **
  184. **  (Cut, Copy, Paste) have been redirected to ToDoController and these      **
  185. **  are the defined action methods.                                          **
  186. ******************************************************************************/
  187.  
  188. - (void)copyItem:sender
  189. {
  190.     BOOL result;
  191.     int row;
  192.     id selItem, doc = [[NSApp mainWindow] delegate];
  193.     if ([doc isKindOfClass:[ToDoDoc class]]) {
  194.         row = [[doc itemMatrix] selectedRow];
  195.         selItem = [[doc currentItems] objectAtIndex:row];
  196.         if ([selItem isKindOfClass:[ToDoItem class]]) {
  197.             NSData *selData = [NSArchiver archivedDataWithRootObject:selItem];
  198.             NSPasteboard *itempb = [NSPasteboard pasteboardWithName:NSGeneralPboard];
  199.             result = [itempb declareTypes:[NSArray arrayWithObject:@"ToDoItemPBType"] owner:nil];
  200.             [itempb setData:selData forType:@"ToDoItemPBType"];
  201.         }
  202.     }
  203. }
  204.  
  205. - (void)cutItem:sender
  206. {
  207.     id doc = [[NSApp mainWindow] delegate];
  208.     [self copyItem:self];
  209.     [[doc currentItems] replaceObjectAtIndex:[[doc itemMatrix] selectedRow] withObject:@""];
  210.     [doc updateMatrix];
  211. }
  212.  
  213. - (void)pasteItem:sender
  214. {
  215.     id doc = [[NSApp mainWindow] delegate];
  216.     if ([doc isKindOfClass:[ToDoDoc class]]) {
  217.         int row;
  218.         NSPasteboard *itempb;
  219.         NSData *itemData=nil;
  220.         ToDoItem *newItem;
  221.         row = [[doc itemMatrix] selectedRow];
  222.         itempb = [NSPasteboard pasteboardWithName:NSGeneralPboard];
  223.         if ([[itempb types] containsObject:@"ToDoItemPBType"])
  224.             itemData = [itempb dataForType:@"ToDoItemPBType"];
  225.         newItem = [NSUnarchiver unarchiveObjectWithData:itemData];
  226.         [newItem setDay:[[doc calendar] selectedDay]];
  227.         if ([newItem secsUntilNotif])
  228.             [doc setTimerForItem:newItem];
  229.         [[inspector delegate] setCurrentItem:newItem];
  230.         [[doc currentItems] replaceObjectAtIndex:row withObject:newItem];
  231.         [doc updateMatrix];
  232.     }
  233. }
  234.  
  235.  
  236.  
  237. @end
  238.  
  239.