home *** CD-ROM | disk | FTP | other *** search
/ Nebula / nebula.bin / SourceCode / MiniExamples / CellScrollView / CellScrollView.m < prev    next >
Text File  |  1991-10-10  |  3KB  |  116 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.  
  8. #import "CellScrollView.h"
  9. #import "FooCell.h"
  10. #import <appkit/Matrix.h>
  11. #import <objc/List.h>
  12.  
  13. @implementation CellScrollView
  14.  
  15. - init
  16. {
  17.   return [self initFrame:NULL];
  18. }
  19.  
  20. - initFrame:(const NXRect *)frameRect
  21. {
  22.   NXSize interCellSpacing = {0.0, 0.0}, docSize;
  23.  
  24.   [super initFrame:frameRect];
  25.   cellMatrix = [[Matrix alloc] initFrame:frameRect
  26.         mode:NX_LISTMODE
  27.         cellClass:[FooCell class]
  28.         numRows:0
  29.         numCols:1];
  30.  
  31.   /*
  32.    * In the following lines,
  33.    * remember that "cellMatrix" is the matrix that will be installed
  34.    * in the scrollview, "self" is the scrollview.
  35.    */
  36.  
  37.   /* we don't want any space between the matrix's cells  */
  38.   [cellMatrix setIntercell:&interCellSpacing];
  39.   /* resize the matrix to contain the cells */
  40.   [cellMatrix sizeToCells];
  41.   [cellMatrix setAutosizeCells:YES];
  42.   /*
  43.    * when the user clicks in the matrix and then drags the mouse out of
  44.    * scrollView's contentView, we want the matrix to scroll
  45.    */
  46.   [cellMatrix setAutoscroll:YES];
  47.   /* let the matrix stretch horizontally */
  48.   [cellMatrix setAutosizing:NX_WIDTHSIZABLE];  
  49.   /* Install the matrix as the docview of the scrollview */
  50.   [[self setDocView:cellMatrix] free];
  51.   /* set up the visible attributes of the scrollview */
  52.   [self setVertScrollerRequired:YES];
  53.   [self setBorderType:NX_BEZEL];
  54.   /* tell the subviews to resize along with the scrollview */
  55.   [self setAutoresizeSubviews:YES];
  56.   /* This is the only way to get the clipview to resize too */
  57.   [[cellMatrix superview] setAutoresizeSubviews:YES];
  58.   /* Allow the scrollview to stretch both horizontally and vertically */
  59.   [self setAutosizing:NX_WIDTHSIZABLE|NX_HEIGHTSIZABLE];
  60.   /* Resize the matrix to fill the inside of the scrollview */
  61.   [self getContentSize:&docSize];
  62.   [cellMatrix sizeTo:docSize.width :docSize.height];
  63.  
  64.   return self;
  65. }
  66.  
  67. - free
  68. {
  69.   [cellMatrix free];
  70.   return [super free];
  71. }
  72.  
  73. - cellMatrix
  74. {
  75.   return cellMatrix;
  76. }
  77.  
  78. - loadCellsFrom:(List *)fooObjects
  79. /*
  80.  * Fill the matrix with FooCells, associate each FooCell with a FooObject.
  81.  *
  82.  * Since we recycle the cells (via renewRows:cols:), we also set the state
  83.  * of each cell to 0 and unhighlight it.  If we don't do that, the recycled
  84.  * cells will display their previous state.
  85.  */
  86. {
  87.   int i, cellCount;
  88.  
  89.   cellCount = [fooObjects count];
  90.  
  91.   /* tell the matrix there are 0 cells in it (but don't deallocate them) */
  92.   [cellMatrix renewRows:0 cols:1];
  93.   [cellMatrix lockFocus];        /* for highlightCellAt::lit: */
  94.   for (i=0;i<cellCount;i++) {
  95.     FooCell *cell;
  96.     /*
  97.      * add a row to the matrix.  (This doesn't necessarily allocate a new
  98.      * cell, thanks to renewRows:cols:).
  99.      */
  100.     [cellMatrix addRow];
  101.     cell = [cellMatrix cellAt:i:0];
  102.     /* make sure the cell is neither selected nor highlighted */
  103.     [cellMatrix highlightCellAt:i:0 lit:NO];
  104.     [cell setState:0];
  105.     /* install the fooObject in that cell */
  106.     [cell setFooObject:[fooObjects objectAt:i]];
  107.   }
  108.   [cellMatrix unlockFocus];
  109.   [cellMatrix sizeToCells];
  110.   [cellMatrix display];
  111.   
  112.   return self;
  113. }
  114.  
  115. @end
  116.