home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.mdf / SourceCode / MiscKit1.2.6 / Palettes / MiscCalendarPalette / MiscCalendarView.subproj / MiscCalendarMatrix.m < prev    next >
Encoding:
Text File  |  1994-05-28  |  10.4 KB  |  523 lines

  1. #import <appkit/Application.h>
  2. #import <appkit/Font.h>
  3.  
  4. #import "misckit/MiscCalendarViewConstants.h"
  5. #import "misckit/DateDelegateProtocol.h"
  6. #import "MiscCalendarMatrix.h"
  7. #import "DateSelectionCell.h"
  8. #import "SimpleDate.h"
  9.  
  10. @implementation MiscCalendarMatrix
  11.  
  12. + initialize
  13. {
  14.     [MiscCalendarMatrix setVersion:1];
  15.  
  16.     return self;
  17. }
  18.  
  19. - initFrame:(const NXRect *)frameRect
  20. {
  21.     NXSize     spacing,
  22.         newCellSize;
  23.     id        aCell;
  24.  
  25.     [super initFrame:frameRect mode:NX_RADIOMODE 
  26.      cellClass:[DateSelectionCell class] numRows:NUM_MATRIX_ROWS
  27.      numCols:NUM_MATRIX_COLS];
  28.  
  29.     [self setEmptySelectionEnabled:YES];
  30.  
  31.     [self clearSelectedCell];
  32.  
  33.     [Font setUserFont:[Font newFont:"Helvetica" size:11 style:0
  34.                matrix:NX_FLIPPEDMATRIX]];
  35.  
  36.     [self setFont:[Font newFont:"Helvetica" size:11 style:0
  37.            matrix:NX_FLIPPEDMATRIX]];
  38.  
  39.     spacing.height = MATRIX_SPACING_HEIGHT;
  40.     spacing.width = MATRIX_SPACING_WIDTH;
  41.  
  42.     [self setIntercell:&spacing];
  43.  
  44.     newCellSize.height = MATRIX_CELL_HEIGHT;
  45.     newCellSize.width = MATRIX_CELL_WIDTH;
  46.  
  47.     [self setCellSize:&newCellSize];
  48.  
  49.     [self sizeToCells];
  50.  
  51.     [self sizeBy:1 :1];
  52.  
  53.     simpleDate = [[SimpleDate alloc] init];
  54.  
  55.     [self setDateDelegate:simpleDate];
  56.  
  57.     aCell = [[self cellList] objectAt:0];
  58.  
  59.     cellTextColor = [aCell textColor];
  60.     cellHighlightTextColor = [aCell highlightTextColor];
  61.     cellHighlightColor = [aCell highlightColor];
  62.     cellBackgroundColor = [aCell backgroundColor];
  63.  
  64.     overwriteCellColors = NO;
  65.  
  66.     [self displayDate:self];
  67.  
  68.     return self;
  69. }
  70.  
  71. - initFrame:(const NXRect *)frameRect mode:(int)aMode cellClass:cellId
  72.   numRows:(int)numRows numCols:(int)numCols
  73. {
  74.     return [self initFrame:frameRect];
  75. }
  76.  
  77. - initFrame:(const NXRect *)frameRect mode:(int)aMode prototype:aCell
  78.   numRows:(int)numRows numCols:(int)numCols
  79. {
  80.     return [self initFrame:frameRect];
  81. }
  82.  
  83. - free
  84. {
  85.     [simpleDate free];
  86.  
  87.     return [super free];
  88. }
  89.  
  90. - setDateDelegate:aDateObject
  91. {
  92.     if ([aDateObject conformsTo:@protocol(DateDelegate)])
  93.       dateDelegate = aDateObject;
  94.  
  95.     return self;
  96. }
  97.  
  98. - dateDelegate
  99. {
  100.     return dateDelegate;
  101. }
  102.  
  103. - overwriteCellColors:(BOOL)yn
  104. {
  105.     overwriteCellColors = yn;
  106.  
  107.     return self;
  108. }
  109.  
  110. - (BOOL)cellColorsOverwritten
  111. {
  112.     return overwriteCellColors;
  113. }
  114.  
  115. - setCellBackgroundColor:(NXColor)aColor
  116. {
  117.     List    *myCellList;
  118.     id        aCell;
  119.     int        i;
  120.  
  121.     myCellList = [self cellList];
  122.  
  123.     for (i = 0; i < [myCellList count]; i++)
  124.     {
  125.     aCell = [myCellList objectAt:i];
  126.  
  127.     if (overwriteCellColors || 
  128.         NXEqualColor(cellBackgroundColor, [aCell backgroundColor]))
  129.       [aCell setBackgroundColor:aColor];
  130.     }
  131.  
  132.     cellBackgroundColor = aColor;
  133.  
  134.     return self;
  135. }
  136.  
  137. - (NXColor)cellBackgroundColor
  138. {
  139.     return cellBackgroundColor;
  140. }
  141.  
  142. - setBackgroundColor:(NXColor)aColor forCellAt:(int)xPos :(int)yPos
  143. {
  144.     [[self cellAt:xPos :yPos] setBackgroundColor:aColor];
  145.  
  146.     return self;
  147. }
  148.  
  149. - (NXColor)backgroundColorForCellAt:(int)xPos :(int)yPos
  150. {
  151.     return [[self cellAt:xPos :yPos] backgroundColor];
  152. }
  153.  
  154. - setCellHighlightColor:(NXColor)aColor
  155. {
  156.     List    *myCellList;
  157.     id        aCell;
  158.     int        i;
  159.  
  160.     myCellList = [self cellList];
  161.  
  162.     for (i = 0; i < [myCellList count]; i++)
  163.     {
  164.     aCell = [myCellList objectAt:i];
  165.  
  166.     if (overwriteCellColors || 
  167.         NXEqualColor(cellHighlightColor, [aCell highlightColor]))
  168.       [aCell setHighlightColor:aColor];
  169.     }
  170.  
  171.     cellHighlightColor = aColor;
  172.  
  173.     return self;
  174. }
  175.  
  176. - (NXColor)cellHighlightColor
  177. {
  178.     return cellHighlightColor;
  179. }
  180.  
  181. - setHighlightColor:(NXColor)aColor forCellAt:(int)xPos :(int)yPos
  182. {
  183.     [[self cellAt:xPos :yPos] setHighlightColor:aColor];
  184.  
  185.     return self;
  186. }
  187.  
  188. - (NXColor)highlightColorForCellAt:(int)xPos :(int)yPos
  189. {
  190.     return [[self cellAt:xPos :yPos] highlightColor];
  191. }
  192.  
  193. - setCellTextColor:(NXColor)aColor
  194. {
  195.     List    *myCellList;
  196.     id        aCell;
  197.     int        i;
  198.  
  199.     myCellList = [self cellList];
  200.  
  201.     for (i = 0; i < [myCellList count]; i++)
  202.     {
  203.     aCell = [myCellList objectAt:i];
  204.     if (overwriteCellColors || 
  205.         NXEqualColor(cellTextColor, [aCell textColor]))
  206.     [aCell setTextColor:aColor];
  207.     }
  208.  
  209.     cellTextColor = aColor;
  210.  
  211.     return self;
  212. }
  213.  
  214. - (NXColor)cellTextColor
  215. {
  216.     return cellTextColor;
  217. }
  218.  
  219. - setTextColor:(NXColor)aColor forCellAt:(int)xPos :(int)yPos
  220. {
  221.     [[self cellAt:xPos :yPos] setTextColor:aColor];
  222.  
  223.     return self;
  224. }
  225.  
  226. - (NXColor)textColorForCellAt:(int)xPos :(int)yPos
  227. {
  228.     return [[self cellAt:xPos :yPos] textColor];
  229. }
  230.  
  231. - setCellHighlightTextColor:(NXColor)aColor
  232. {
  233.     List    *myCellList;
  234.     id        aCell;
  235.     int        i;
  236.  
  237.     myCellList = [self cellList];
  238.  
  239.     for (i = 0; i < [myCellList count]; i++)
  240.     {
  241.     aCell = [myCellList objectAt:i];
  242.  
  243.     if (overwriteCellColors || 
  244.         NXEqualColor(cellHighlightTextColor, [aCell highlightTextColor]))
  245.       [aCell setHighlightTextColor:aColor];
  246.     }
  247.  
  248.     cellHighlightTextColor = aColor;
  249.  
  250.     return self;
  251. }
  252.  
  253. - (NXColor)cellHighlightTextColor
  254. {
  255.     return cellHighlightTextColor;
  256. }
  257.  
  258. - setHighlightTextColor:(NXColor)aColor forCellAt:(int)xPos :(int)yPos
  259. {
  260.     [[self cellAt:xPos :yPos] setHighlightTextColor:aColor];
  261.  
  262.     return self;
  263. }
  264.  
  265. - (NXColor)highlightTextColorForCellAt:(int)xPos :(int)yPos
  266. {
  267.     return [[self cellAt:xPos :yPos] highlightTextColor];
  268. }
  269.  
  270. - mouseDown:(NXEvent *)theEvent
  271. {
  272.     id        theCell = nil,
  273.         previousCell = nil;
  274.     NXEvent    *nextEvent;
  275.     NXPoint    mouseLocation;
  276.     BOOL    looping = YES;
  277.     int        row = -1,
  278.         col = -1,
  279.             previousRow = -1,
  280.             previousCol = -1,
  281.         oldMask;
  282.  
  283.     nextEvent = theEvent;
  284.  
  285.     if ([self mode] == NX_HIGHLIGHTMODE)
  286.     {
  287.     /* Save our normal window event mask, and add the mouse dragged */
  288.     /* event mask */
  289.  
  290.     oldMask = [window addToEventMask:NX_MOUSEDRAGGEDMASK];
  291.  
  292.     while (looping)
  293.     {
  294.         /* Find the current coordinates of the mouse... */
  295.  
  296.         mouseLocation = nextEvent->location;
  297.  
  298.         /* ... and convert them into our view coordinate system */
  299.  
  300.         [self convertPoint:&mouseLocation fromView:nil];
  301.  
  302.         /* Find the row and column of the mouse */
  303.  
  304.         [self getRow:&row andCol:&col forPoint:&mouseLocation];
  305.  
  306.         if ( (row >= 0) && (col >= 0) && ((row != previousRow) ||
  307.                           (col != previousCol)) )
  308.         {
  309.         theCell = [self cellAt:row :col];
  310.  
  311.         if ([theCell isEnabled])
  312.         {
  313.             if (previousCell && (previousCell != theCell))
  314.               [previousCell incrementState];
  315.  
  316.             [theCell incrementState];
  317.  
  318.             [self display];
  319.  
  320.             previousCell = theCell;
  321.         }
  322.  
  323.         previousRow = row;
  324.         previousCol = col;
  325.         }
  326.         else if (((row < 0) || (col < 0)) && previousCell && 
  327.              [previousCell isEnabled])
  328.         {
  329.         [previousCell incrementState];
  330.         [self display];
  331.         previousCell = nil;
  332.         }
  333.         
  334.         nextEvent = [NXApp getNextEvent:NX_MOUSEUPMASK | 
  335.              NX_MOUSEDRAGGEDMASK];
  336.  
  337.         looping = (nextEvent->type == NX_MOUSEDRAGGED);
  338.     }
  339.  
  340.     [window setEventMask:oldMask];
  341.     }
  342.     else
  343.       return [super mouseDown:theEvent];
  344.  
  345.     return self;
  346. }
  347.  
  348. - displayDate:sender
  349. {
  350.     int    numberOfDays,
  351.     startCell,
  352.         currentDay = 0,
  353.     x,
  354.     y,
  355.         day;
  356.     id    dayCell,
  357.     aDate = nil;
  358.     
  359.     [[self window] disableFlushWindow];
  360.  
  361.  
  362.     if ((aDate = [self dateDelegate]))
  363.     {
  364.     currentDay = [aDate day];
  365.  
  366.     /* Get the number of days for the month that the date object */
  367.     /* is set to */
  368.  
  369.     numberOfDays = [aDate numberOfDaysInMonth];
  370.  
  371.     /* Get the starting day of the month */
  372.  
  373.     startCell = [aDate startDayOfMonth];
  374.  
  375.     }
  376.     else
  377.     {
  378.     currentDay = 1;
  379.     numberOfDays = 31;
  380.     startCell = 4;
  381.     }
  382.  
  383.     // Clear out the titles of the first row of dayCells
  384.     // from the start of the matrix to the x coordinate
  385.     // that corresponds to the day of the week that the
  386.     // month start on
  387.     
  388.     for (x=0, y=0; x < startCell; x++)
  389.     {
  390.     dayCell = [self cellAt:y :x];
  391.  
  392.     if (x == 0)
  393.       [dayCell setDrawEntireBottomEdge:NO];
  394.  
  395.     [dayCell setStringValue:""];
  396.     [dayCell setEnabled:NO];
  397.     }
  398.     
  399.     // Fill in the titles of all the dayCells. When x hits
  400.     // the seventh column of the matrix, we wrap around
  401.     // to the first column and continue filling in values
  402.     // on the next row
  403.     
  404.     for (day = 1; day <= numberOfDays; day++, x++)
  405.     {
  406.     if (x == 7)
  407.     {
  408.         x = 0;
  409.         y++;
  410.     }
  411.  
  412.     dayCell = [self cellAt:y :x];
  413.  
  414.     [dayCell setIntValue:day];
  415.     [dayCell setEnabled:YES];
  416.  
  417.     if (day == currentDay)
  418.       [self selectCellAt:y :x];
  419.  
  420.     if (x == 0)
  421.       [dayCell setDrawEntireBottomEdge:NO];
  422.  
  423.     if (x == 6)
  424.       [dayCell setDrawRightEdge:YES];
  425.     }
  426.     
  427.     // Clear out the titles of the rest of the dayCells
  428.     // from the last day of the month to the end of
  429.     // the matrix. When x hits the seventh column of 
  430.     // the matrix, we wrap around to the first column
  431.     // and continue blanking dayCells on the next row until
  432.     // y hits the seventh row. We are then done with 
  433.     // this matrix
  434.     
  435.     for (; y < 7; x++)
  436.     {
  437.     if (x == 7)
  438.     {
  439.         x = 0;
  440.         y++;
  441.     }
  442.     
  443.     dayCell = [self cellAt:y :x];
  444.  
  445.     if (x == 0)
  446.       [dayCell setDrawEntireBottomEdge:NO];
  447.  
  448.     if (x == 6)
  449.       [dayCell setDrawRightEdge:YES];
  450.  
  451.     [dayCell setStringValue:""];
  452.     [dayCell setEnabled:NO];
  453.  
  454.     }
  455.     
  456.     [self display];
  457.  
  458.     [[self window] reenableFlushWindow];
  459.     [[self window] flushWindowIfNeeded];
  460.  
  461.     return self;
  462. }
  463.  
  464. - read:(NXTypedStream *)aStream
  465. {
  466.     float    red,
  467.         green,
  468.         blue;
  469.     int     version;
  470.  
  471.     [super read:aStream];
  472.  
  473.     version = NXTypedStreamClassVersion(aStream, "MiscCalendarMatrix");
  474.  
  475.     NXReadTypes(aStream, "@@", &dateDelegate, &simpleDate);
  476.  
  477.     NXReadTypes(aStream, "fff", &red, &green, &blue);
  478.     cellBackgroundColor = NXConvertRGBToColor(red, green, blue);
  479.  
  480.     NXReadTypes(aStream, "fff", &red, &green, &blue);
  481.     cellHighlightColor = NXConvertRGBToColor(red, green, blue);
  482.  
  483.     NXReadTypes(aStream, "fff", &red, &green, &blue);
  484.     cellTextColor = NXConvertRGBToColor(red, green, blue);
  485.  
  486.     NXReadTypes(aStream, "fff", &red, &green, &blue);
  487.     cellHighlightTextColor = NXConvertRGBToColor(red, green, blue);
  488.  
  489.     if (version == 1)
  490.       NXReadTypes(aStream, "c", &overwriteCellColors);
  491.  
  492.     return self;
  493. }
  494.  
  495. - write:(NXTypedStream *)aStream
  496. {
  497.     float    red,
  498.         green,
  499.         blue;
  500.  
  501.     [super write:aStream];
  502.  
  503.     NXWriteTypes(aStream, "@@", &dateDelegate, &simpleDate);
  504.  
  505.     NXConvertColorToRGBA(cellBackgroundColor, &red, &green, &blue, 0);
  506.     NXWriteTypes(aStream, "fff", &red, &green, &blue);
  507.  
  508.     NXConvertColorToRGBA(cellHighlightColor, &red, &green, &blue, 0);
  509.     NXWriteTypes(aStream, "fff", &red, &green, &blue);
  510.  
  511.     NXConvertColorToRGBA(cellTextColor, &red, &green, &blue, 0);
  512.     NXWriteTypes(aStream, "fff", &red, &green, &blue);
  513.  
  514.     NXConvertColorToRGBA(cellHighlightTextColor, &red, &green, &blue, 0);
  515.     NXWriteTypes(aStream, "fff", &red, &green, &blue);
  516.  
  517.     NXWriteTypes(aStream, "c", &overwriteCellColors);
  518.  
  519.     return self;
  520. }
  521.  
  522. @end
  523.