home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Scheduling / Cassandra / Source / WeekWindow.m < prev    next >
Encoding:
Text File  |  1991-08-18  |  8.1 KB  |  372 lines

  1. //
  2. // WeekWindow.m
  3. // Copyright (c) 1989, 1990,  1991 by Jiro Nakamura 
  4. // All rights reserved
  5. //
  6. //    by Jiro Nakamura (jiro@shaman.com)
  7. //
  8. // RCS Information
  9. // Revision Number->    $Revision: 1.3 $
  10. // Last Revised->    $Date: 91/02/17 22:03:21 $
  11. //
  12. static char rcsid[] = "$Id: WeekWindow.m,v 1.3 91/02/17 22:03:21 jiro Exp Locker: jiro $";
  13.  
  14. #import "Global.h"
  15. #import "Event.h"
  16. #import "cass.h"    // for TODAYICON
  17. #import "calendar.h"    // for ascMyDate()
  18. #import <appkit/Application.h>        // For NXApp 
  19. #import <appkit/ScrollView.h>
  20. #import <appkit/Text.h>
  21. #import <appkit/TextField.h>
  22. #import <appkit/PopUpList.h>
  23. #import <appkit/Matrix.h>
  24. #import "WeekWindow.h"
  25.  
  26. #import <sys/file.h>
  27. #import <libc.h>
  28. #import <strings.h>
  29.  
  30.  
  31.  
  32. // The minimum dimensions of the week window
  33. #define MIN_WIDTH        870.0
  34. #define MIN_HEIGHT        100.0
  35.  
  36.  
  37. @implementation WeekWindow
  38.  
  39. - open:sender
  40. {
  41.     static alreadyInited = FALSE;
  42.     
  43.     if( !alreadyInited)
  44.         {
  45.         alreadyInited = TRUE;
  46.         [self placeWindow: [global weekFrame]];
  47.         thisWeek = *timeNow();
  48.         thisWeek.tm_mday -= thisWeek.tm_wday;
  49.         thisWeek.tm_hour = thisWeek.tm_min = thisWeek.tm_sec = 0;
  50.         fixTmStructure( &thisWeek);
  51.         #ifdef DEBUG
  52.             fprintf(stderr,"This week is: %s", 
  53.                 ascMyDate(&thisWeek));
  54.         #endif
  55.         priorityCutOff = [global lowPriority];
  56.         [self    setDelegate: self];
  57.     
  58.         mondayView = [mondayScroll docView];
  59.         tuesdayView = [tuesdayScroll docView];
  60.         wednesdayView = [wednesdayScroll docView];
  61.         thursdayView = [thursdayScroll docView];
  62.         fridayView = [fridayScroll docView];
  63.         saturdayView = [saturdayScroll docView];
  64.         sundayView = [sundayScroll docView];
  65.     
  66.         [mondayView setMonoFont: FALSE];
  67.         [tuesdayView setMonoFont: FALSE];
  68.         [wednesdayView setMonoFont: FALSE];
  69.         [thursdayView setMonoFont: FALSE];
  70.         [fridayView setMonoFont: FALSE];
  71.         [saturdayView setMonoFont: FALSE];
  72.         [sundayView setMonoFont: FALSE];
  73.  
  74.         [mondayView setEditable: FALSE];
  75.         [tuesdayView setEditable: FALSE];
  76.         [wednesdayView setEditable: FALSE];
  77.         [thursdayView setEditable: FALSE];
  78.         [fridayView setEditable: FALSE];
  79.         [saturdayView setEditable: FALSE];
  80.         [sundayView setEditable: FALSE];
  81.  
  82.         [self setMiniwindowIcon:     WEEKICON];
  83.         
  84.         viewPopUp =         [PopUpList new];
  85.         [viewPopUp    addItem:    "View All"];
  86.         [viewPopUp    addItem:    "View Mid~High"];
  87.         [viewPopUp    addItem:    "View Only High"];
  88.         [[viewPopUp itemList] selectCellAt:1:0];
  89.         NXAttachPopUpList(viewPopUpButton, viewPopUp);
  90.         }
  91.     [self    makeKeyAndOrderFront: self];
  92.     [self    update];    // this will cause an update
  93.     return self;
  94. }
  95.  
  96. - save
  97. {
  98.     // This window shouldn't save itself. 
  99.     return self;
  100. }
  101.  
  102. - close
  103. {
  104.     [global    saveThisWindowPosition:    DEFAULTWEEKFRAME : self];
  105.     [super close];
  106.     miniaturized = FALSE;
  107.     return self;
  108. }
  109.  
  110. - update
  111. {
  112.     struct tm tempDay;
  113.     char temp[80];
  114.     extern const char *shortMonths[12];
  115.     int tmp;
  116.     
  117.     fixTmStructure( &thisWeek );
  118.     tempDay = thisWeek;
  119.     
  120.     if( miniaturized || ![self isVisible])
  121.         return self;
  122.     
  123.     sprintf(temp, "Week of: %s %d, %d", 
  124.         shortMonths[tempDay.tm_mon], tempDay.tm_mday,
  125.         tempDay.tm_year + 1900);
  126.     
  127.     [weekTextField setStringValue:    temp];    
  128.     tmp = [self    updateView:  sundayView on: &tempDay fromEvent: 0];
  129.     tempDay.tm_mday ++;
  130.     tmp = [self    updateView:  mondayView on: &tempDay 
  131.         fromEvent: tmp];
  132.     tempDay.tm_mday ++;
  133.     tmp = [self    updateView:  tuesdayView on: &tempDay 
  134.         fromEvent: tmp];
  135.     tempDay.tm_mday ++;
  136.     tmp = [self    updateView:  wednesdayView on: &tempDay
  137.         fromEvent: tmp];
  138.     tempDay.tm_mday ++;
  139.     tmp = [self    updateView:  thursdayView on: &tempDay
  140.         fromEvent: tmp];
  141.     tempDay.tm_mday ++;
  142.     tmp = [self    updateView:  fridayView on: &tempDay
  143.         fromEvent: tmp];
  144.     tempDay.tm_mday ++;
  145.     [self    updateView:  saturdayView on: &tempDay fromEvent: tmp];    
  146.     return self;
  147. }
  148.  
  149. - (int) updateView: (id) dayView on: (struct tm *) thisDay fromEvent: (int) en
  150. {
  151.     NXStream *output; 
  152.     Event *ev;
  153.     int eventCount;
  154.     int today, temp;
  155.     extern const char *shortMonths[12], *shortWeekDays[7];
  156.  
  157.     fixTmStructure( thisDay );
  158.  
  159.     // We want to read the eventFile pointed to by global 
  160.     ev = [Event newAt:[global eventFile]];
  161.  
  162.     // We need a temporary memory stream to store the queue.
  163.     if( (output = NXOpenMemory( NULL, 0, NX_READWRITE)) == NULL)
  164.         {
  165.         fprintf(stderr,"%s: Couldn't open memory stream for "
  166.             "Week Window queue scrollview. FATAL ERRROR.\n",
  167.             PROGNAME);
  168.         exit(1) ;
  169.         }
  170.         
  171.     if( en != 0)        
  172.         [ev readEvent: en];
  173.     else
  174.         [ev firstEvent];
  175.                     
  176.     today = [ev mday];
  177.     
  178.     if( dayCompare( thisDay, (struct tm *) timeNow()) == -1)
  179.         today = -2;        
  180.     else
  181.         {        
  182.         while( [ev present] != 0 &&
  183.             dayCompare( [ev time], thisDay ) == -1 )
  184.             [ev readEvent : [ev next]];     
  185.  
  186.         if( ([ev mday] != thisDay->tm_mday) || 
  187.             ([ev mon]  != thisDay->tm_mon) || 
  188.             ([ev year] != thisDay->tm_year))
  189.                 today = -1;
  190.         else    
  191.             today = [ev mday];
  192.         }
  193.         
  194.     NXPrintf(output, "{\\rtf0\\ansi{\\fonttbl\\f0\\fmodern "
  195.         "%s;}\\fs%.0f\n",
  196.         [global fontName], [global fontSize] * 2);
  197.  
  198.     NXPrintf(output, "{\\f0\\b %s %s %2d\\par}\n", 
  199.         shortWeekDays[thisDay->tm_wday],
  200.         shortMonths[thisDay->tm_mon], thisDay->tm_mday);
  201.  
  202.     eventCount = 0;
  203.                 
  204.     /* Print all events for today regardless of priority */
  205.     while ( [ev present] != 0 && [ev mday] == today)
  206.         {
  207.         if( [ev priority] < priorityCutOff)
  208.             {
  209.             [ev readEvent : [ev next]];     
  210.             eventCount++;
  211.             continue;
  212.             }
  213.             
  214.         if( [ev priority] > [global highPriority])
  215.             NXPrintf(output, "{\\f0\\b ");
  216.         else
  217.             NXPrintf(output, "{\\f0 ");
  218.  
  219.         if( [global militaryTime])
  220.             NXPrintf(output, "\\par {\\f0\\b %2d:%02d}"
  221.                 "{\\par \\li360 %s\\par}}\n", 
  222.                 [ev hour], [ev min], [ev message]);
  223.         else
  224.             {
  225.             temp = [ev hour];
  226.             if( temp > 12 ) temp -= 12;
  227.             if( temp == 0 ) temp = 12;            
  228.             NXPrintf(output, "\\par{\\f0\\b %2d:%02d %s}"
  229.                 "{\\par \\li360 %s\\par}}\n", 
  230.                     temp,    [ev min], 
  231.                     ([ev hour]>11)?"pm":"am", 
  232.                     [ev message]);
  233.             }
  234.         [ev readEvent : [ev next]];     
  235.         }   
  236.  
  237.     if( [ev present] == 0 || today <  0)
  238.         {
  239.         if( today == -2)
  240.             NXPrintf(output,  "{\\f0\\par Ancient history.}\n");
  241.         else
  242.             NXPrintf(output, "{\\f0\\par No events.}\n");
  243.         }
  244.     
  245.     if( eventCount > 0)    // Number of invisible events
  246.         {
  247.         NXPrintf(output, "{\\par (%d hidden)\\par}\n", 
  248.                     eventCount);
  249.         }
  250.                     
  251.     NXPrintf(output, "}\n");    
  252.  
  253.     NXSeek(output, 0, NX_FROMSTART);
  254.     [dayView readRichText: output]; 
  255.     NXCloseMemory(output, NX_FREEBUFFER); 
  256.     
  257.     [dayView hideCaret];    // for some reason the Text
  258.     
  259.     temp = [ev present];
  260.     [ev free];            
  261.     return temp;
  262. }
  263.  
  264. - weekBefore:sender
  265. {        
  266.     if( ([NXApp currentEvent]->flags) & NX_ALTERNATEMASK)
  267.         thisWeek.tm_mday -= 1;
  268.     else        
  269.         thisWeek.tm_mday -= 7;        
  270.     [self update];
  271.     return self;
  272. }
  273.  
  274. - weekAfter:sender
  275. {
  276.     if( ([NXApp currentEvent]->flags) & NX_ALTERNATEMASK)
  277.         thisWeek.tm_mday += 1;
  278.     else
  279.         thisWeek.tm_mday += 7;
  280.     [self update];
  281.     return self;
  282. }
  283.  
  284. - weekNow:sender
  285. {
  286.     thisWeek = *timeNow();
  287.     thisWeek.tm_mday -= thisWeek.tm_wday;
  288.     thisWeek.tm_hour = thisWeek.tm_min = thisWeek.tm_sec = 0;
  289.     fixTmStructure( &thisWeek);
  290.     [self update];
  291.     return self;
  292. }
  293.  
  294.  
  295. - viewPopUpChanged:sender
  296. {    
  297.     switch( [[viewPopUp itemList] selectedRow])
  298.         {
  299.         case 2:
  300.             priorityCutOff = [global highPriority];
  301.             break;
  302.         case 1:
  303.             priorityCutOff = [global lowPriority];
  304.             break;
  305.         case 0:
  306.         default:
  307.             priorityCutOff = 0;
  308.             break;
  309.         }
  310.     #ifdef DEBUG
  311.         fprintf(stderr, "Priority changed to %d.... (tag = %d)\n",
  312.              priorityCutOff, [[viewPopUp itemList] selectedRow]);
  313.     #endif
  314.     [self update];
  315.     return self;
  316. }
  317.  
  318. - windowDidMiniaturize: sender
  319. {
  320.     #ifdef DEBUG
  321.         fprintf(stderr,"Today did miniaturize\n");
  322.     #endif
  323.  
  324.     miniaturized = TRUE;
  325.     minute = -1;        // Force a redisplay on reopening
  326.     return self;
  327. }
  328.  
  329. - windowDidDeminiaturize: sender
  330. {
  331.     #ifdef DEBUG
  332.         fprintf(stderr,"Today did deminiaturize\n");
  333.     #endif
  334.  
  335.     miniaturized = FALSE;
  336.     minute = -1;        // Force a redisplay
  337.     [self update];
  338.     return self;
  339. }
  340.  
  341. - defaultsDidChange: sender
  342. {
  343.     #ifdef DEBUG
  344.         fprintf(stderr,"Defaults changed.... Week win compensating\n");
  345.     #endif
  346.     
  347.     militaryTime     = [global militaryTime];
  348.     [self update];
  349.     return self;
  350. }
  351.  
  352.  
  353.  
  354. - windowWillResize: (id) sender toSize: (NXSize *) size
  355. {
  356.     #ifdef DEBUG
  357.         fprintf(stderr,"Window would have resized to %f x %f.\n", 
  358.             size->width, size->height);
  359.     #endif
  360.     
  361.     size->width = MIN_WIDTH;    // can't change the width, ever
  362.     if( size->height < MIN_HEIGHT)
  363.         size->height = MIN_HEIGHT;
  364.         
  365.     #ifdef DEBUG
  366.         fprintf(stderr,"Window will resize to %f x %f.\n", 
  367.             size->width, size->height);
  368.     #endif
  369.     return self;
  370. }
  371. @end
  372.