home *** CD-ROM | disk | FTP | other *** search
/ OpenStep (Enterprise) / OpenStepENTCD.toast / OEDEV / DEV.Z / ToDoInspector.m < prev    next >
Encoding:
Text File  |  1996-07-30  |  9.0 KB  |  278 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 "ToDoInspector.h"
  8.  
  9. enum { notifTag = 0, reschedTag, notesTag };
  10.  
  11. static void clearButtonMatrix(id matrix);
  12.  
  13. @implementation ToDoInspector
  14.  
  15.  
  16. - (void)awakeFromNib
  17. {
  18.     [inspPopUp selectItemAtIndex:0];
  19.     [inspNotes setDelegate:self];
  20.  
  21.     /* remove inspector content views from superview, retain them, */
  22.     /* and zap off-screen window */
  23.  
  24.  
  25.     [[notifView contentView] removeFromSuperview];
  26.     notifView = [[notifView contentView] retain];
  27.     [[reschedView contentView] removeFromSuperview];
  28.     reschedView = [[reschedView contentView] retain];
  29.     [[notesView contentView] removeFromSuperview];
  30.     notesView = [[notesView contentView] retain];
  31.     [inspectorViews release];
  32.     [self newInspectorView:self];
  33.     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(currentItemChanged:) name:ToDoItemChangedNotification object:nil];
  34. }
  35.  
  36. - (void)dealloc
  37. {
  38.     [notifView release];
  39.     [reschedView release];
  40.     [notesView release];
  41. }
  42.  
  43. /* ************ INSPECTOR MANAGEMENT **************************
  44. /* When an item is added, changed, or deleted, or when a new item is
  45. /* selected in the document, currentItem and itemidx are updated via
  46. /* notification.
  47.    *********************************************************** */
  48.  
  49. /* Update the currentItem with the values on the current inspector view */
  50. /*
  51.    
  52. /* update the currentItem, then switch to the chosen inspector view */
  53.  
  54. - (void)newInspectorView:(id)sender
  55. {
  56.     NSBox *newView=nil;
  57.     NSView *cView = [[inspPopUp window] contentView];
  58.     int selected = [[inspPopUp selectedItem] tag];
  59.  
  60.     switch(selected){
  61.       case notifTag:
  62.         newView = notifView;
  63.         break;
  64.       case reschedTag:
  65.         newView = reschedView;
  66.         break;
  67.       case notesTag:
  68.         newView = notesView;
  69.         break;
  70.     }  
  71.     if ([[cView subviews] containsObject:newView]) return;
  72.     [dummyView setContentView:newView];
  73.     if (newView == notifView) [inspNotifHour selectText:self];
  74.     if (newView == notesView) [inspNotes setSelectedRange:NSMakeRange(0,0)];
  75.     [self updateInspector:currentItem];
  76.     [cView setNeedsDisplay:YES];
  77. }
  78.  
  79. /* Opposite of updateItem: takes the appropriate values from the currentItem (ToDoItem) */
  80. /* and updates the current inspector view with it */
  81.  
  82. - (void)updateInspector:(ToDoItem *)newItem
  83. {
  84.     int minute=0, hour=0, selected=0;
  85.     selected = [[inspPopUp selectedItem] tag];
  86.     [[inspPopUp window] orderFront:self];
  87.     if (newItem && [newItem isKindOfClass:[ToDoItem class]]) {
  88.         // add info pertinent to display from currentItem
  89.         [inspItem setStringValue:[newItem itemName]];
  90.         [inspDate setStringValue:[[newItem day] descriptionWithCalendarFormat:@"%a, %b %d %Y" timeZone:[NSTimeZone localTimeZone] locale:nil]];
  91.  
  92.         switch(selected) {
  93.             case notifTag: {
  94.                 BOOL ampm;
  95.                 long notifSecs, dueSecs = [newItem secsUntilDue];
  96.                 ampm = ConvertSecondsToTime(dueSecs, &hour, &minute);
  97.                 [[inspNotifAMPM cellAtRow:0 column:0] setState:!ampm];
  98.                 [[inspNotifAMPM cellAtRow:0 column:1] setState:ampm];
  99.                 [inspNotifHour setIntValue:hour];
  100.                 [inspNotifMinute setIntValue:minute];
  101.                 notifSecs = dueSecs - [newItem secsUntilNotif];
  102.                 if (notifSecs == dueSecs) notifSecs = 0;
  103.                 clearButtonMatrix(inspNotifSwitchMatrix);
  104.                 switch(notifSecs) {
  105.                   case 0:
  106.                     [[inspNotifSwitchMatrix cellAtRow:0 column:0] setState:YES];
  107.                     break;
  108.                   case (hrInSecs/4):
  109.                     [[inspNotifSwitchMatrix cellAtRow:1 column:0] setState:YES];
  110.                     break;
  111.                   case (hrInSecs):
  112.                     [[inspNotifSwitchMatrix cellAtRow:2 column:0] setState:YES];
  113.                     break;
  114.                   case (dayInSecs):
  115.                     [[inspNotifSwitchMatrix cellAtRow:3 column:0] setState:YES];
  116.                     break;
  117.                   default:  /* Other */
  118.                     [[inspNotifSwitchMatrix cellAtRow:4 column:0] setState:YES];
  119.                     [inspNotifOtherHours setIntValue:((dueSecs-notifSecs)/hrInSecs)];
  120.                     break;
  121.                 }
  122.                 break;
  123.             }
  124.           case reschedTag:
  125.             break;
  126.  
  127.           case notesTag:            
  128.             [inspNotes setString:[newItem notes]];
  129.             [inspNotes setSelectedRange:NSMakeRange(0,0)];
  130.         }
  131.     }
  132.     else if (!newItem) { /* newItem is nil */
  133.       /* blank out fields */
  134.         [inspItem setStringValue:@""];
  135.         [inspDate setStringValue:@""];
  136.         [inspNotifHour setStringValue:@""];
  137.         [inspNotifMinute setStringValue:@""];
  138.         [[inspNotifAMPM cellAtRow:0 column:0] setState:YES];
  139.         [[inspNotifAMPM cellAtRow:0 column:1] setState:NO];
  140.         clearButtonMatrix(inspNotifSwitchMatrix);
  141.         [[inspNotifSwitchMatrix cellAtRow:0 column:0] setState:YES];
  142.         [inspNotifOtherHours setStringValue:@""];
  143.         [inspNotes setString:@""];
  144.     }
  145. }
  146.  
  147. - (void)switchChecked:(id)sender
  148. {
  149.     long tmpSecs=0;
  150.     int idx = 0;
  151.     id doc = [[NSApp mainWindow] delegate];
  152.     if (sender == inspNotifAMPM) {
  153.         if ([inspNotifHour intValue]) {
  154.             tmpSecs = ConvertTimeToSeconds([inspNotifHour intValue], [inspNotifMinute intValue], [[sender cellAtRow:0 column:1] state]);
  155.             [currentItem setSecsUntilDue:tmpSecs];
  156.             [[NSApp mainWindow] setDocumentEdited:YES];
  157.             [doc updateMatrix];
  158.         }
  159.     } else if (sender == inspNotifSwitchMatrix) {
  160.         idx = [inspNotifSwitchMatrix selectedRow];
  161.         tmpSecs = [currentItem secsUntilDue];
  162.         switch(idx) {
  163.           case 0:
  164.             [currentItem setSecsUntilNotif:0];
  165.             break;
  166.           case 1:
  167.             [currentItem setSecsUntilNotif:tmpSecs-(hrInSecs/4)];
  168.             break;
  169.           case 2:
  170.             [currentItem setSecsUntilNotif:tmpSecs-hrInSecs];
  171.             break;
  172.           case 3:
  173.             [currentItem setSecsUntilNotif:tmpSecs-dayInSecs];
  174.             break;
  175.           case 4:  // Other
  176.             [currentItem setSecsUntilNotif:([inspNotifOtherHours intValue] * hrInSecs)];
  177.             break;
  178.           default:
  179.             NSLog(@"Error in selectedRow");
  180.             break;
  181.         }
  182.         [[NSApp mainWindow] setDocumentEdited:YES];
  183.         [doc setTimerForItem:currentItem];
  184.         
  185.     } else if (sender == inspSchedComplete) {
  186.         [currentItem setItemStatus:complete];
  187.         [doc updateMatrix];
  188.         [[NSApp mainWindow] setDocumentEdited:YES];
  189.  
  190.     } else if (sender == inspSchedMatrix) {
  191.  
  192.     }
  193. }
  194.  
  195. - (void)resetNotifSwitch
  196. {
  197.     clearButtonMatrix(inspNotifSwitchMatrix);
  198.     [[inspNotifSwitchMatrix cellAtRow:0 column:0] setState:YES];
  199.     [inspNotifSwitchMatrix display];
  200. }
  201.  
  202. - (void)setCurrentItem:(ToDoItem *)newItem
  203. {
  204.     if (currentItem) [currentItem autorelease];
  205.     if (newItem)
  206.         currentItem = [newItem retain];
  207.     else
  208.         currentItem = nil;
  209.     [self updateInspector:currentItem];
  210. }
  211.  
  212. - (ToDoItem *)currentItem { return currentItem; }
  213.  
  214. - (void)textDidEndEditing:(NSNotification *)notif
  215. {
  216.     if ([notif object] == inspNotes) {
  217.         [currentItem setNotes:[inspNotes string]];
  218.         [[NSApp mainWindow] setDocumentEdited:YES];
  219.     }
  220. }
  221.  
  222. - (void)controlTextDidEndEditing:(NSNotification *)notif
  223. {
  224.     long tmpSecs=0;
  225.     if ([notif object] == inspNotifHour ||
  226.         [notif object] == inspNotifMinute) {
  227.  
  228.         tmpSecs = ConvertTimeToSeconds([inspNotifHour intValue], [inspNotifMinute intValue], [[inspNotifAMPM cellAtRow:0 column:1] state]);
  229.         [currentItem setSecsUntilDue:tmpSecs];
  230.         [[[NSApp mainWindow] delegate] updateMatrix];
  231.         [[NSApp mainWindow] setDocumentEdited:YES];
  232.     } else if ([notif object] == inspNotifOtherHours) {
  233.         if ([inspNotifSwitchMatrix selectedRow] == 4) {
  234.             [currentItem setSecsUntilNotif:([inspNotifOtherHours intValue] * hrInSecs)];
  235.             [[[NSApp mainWindow] delegate] setTimerForItem:currentItem];
  236.             [[NSApp mainWindow] setDocumentEdited:YES];
  237.         }
  238.     } else if ([notif object] == inspSchedDate) {
  239.  
  240.     }
  241. }
  242.  
  243. - (void)windowDidResignMain:(NSNotification *)notif
  244. {
  245.     id doc = [[NSApp mainWindow] delegate];
  246.     if ([doc isKindOfClass:[ToDoDoc class]]) {
  247.         [[NSApp mainWindow] makeFirstResponder:[NSApp mainWindow]];
  248.         [doc saveDocItems];
  249.     }
  250. }
  251.  
  252. - (BOOL)windowShouldClose:(id)sender  /* Inspector window */
  253. {
  254.     id doc = [[NSApp mainWindow] delegate];
  255.     if ([doc isKindOfClass:[ToDoDoc class]]) { 
  256.         [[NSApp mainWindow] makeFirstResponder:[NSApp mainWindow]];
  257.         [doc saveDocItems];
  258.     }
  259.     return YES;
  260. }
  261.  
  262. - (void)currentItemChanged:(NSNotification *)notif
  263. {
  264.     [self setCurrentItem:[notif object]];
  265. }
  266.  
  267. /* Utility routine */
  268. void clearButtonMatrix(id matrix)
  269. {
  270.     int i, rows, cols;
  271.     [matrix getNumberOfRows:&rows columns:&cols];
  272.     for(i=0; i<rows; i++)
  273.         [[matrix cellAtRow:i column:0] setState:NO];
  274. }
  275.  
  276. @end
  277.  
  278.