home *** CD-ROM | disk | FTP | other *** search
/ Nebula / nebula.bin / SourceCode / DynamicApp / MatrixScrollView.m < prev    next >
Text File  |  1993-01-19  |  6KB  |  214 lines

  1. /*---------------------------------------------------------------------------
  2. MatrixScrollView.m -- single column selection matrix within a scroll view
  3.  
  4. Copyright (c) 1990 Doug Brenner
  5.  
  6.    This program is free software; you can redistribute it and/or modify
  7.    it under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 1, or (at your option)
  9.    any later version.
  10.  
  11.    This program is distributed in the hope that it will be useful,
  12.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.    GNU General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU General Public License
  17.    along with this program; if not, write to the Free Software
  18.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA, or send
  19.    electronic mail to the the author.
  20.  
  21. Implementation for the MatrixScrollView class.  MatrixScrollView is similar
  22. to the ScrollView available in Interface Builder (IB), except it contains a
  23. matrix of selection cells, not a text object.
  24.  
  25. The matrix is specifically designed to hold one row and to resize itself so
  26. the matrix always fills the clip view.  (See AutoWidthMatrix for more details
  27. about the matrix.)
  28.  
  29. MatrixScrollView is rather single-minded in its behavior because I use it as
  30. a Custom View in Interface Builder.  (Until custom views are supported in
  31. Interface Builder there seems little other choice.)
  32.  
  33. Doug Brenner <dbrenner@umaxc.weeg.uiowa.edu>
  34.  
  35. $Header$
  36. -----------------------------------------------------------------------------
  37. $Log$
  38. ---------------------------------------------------------------------------*/
  39.  
  40. #import "MatrixScrollView.h"
  41. #import "AutoWidthMatrix.h"
  42.  
  43. #import <appkit/SelectionCell.h>
  44.  
  45. #include <string.h>
  46.  
  47. @implementation MatrixScrollView
  48.  
  49. + newFrame:(const NXRect *) frameRect
  50. {
  51.    /* ScrollView instance variables used:
  52.       id contentView */
  53.  
  54.    NXRect matrixFrame = {0.0, 0.0, 0.0, 0.0};
  55.    id matrix;
  56.    
  57.  
  58.    /* ---------- create the scroll view */
  59.  
  60.    self = [super newFrame:frameRect];
  61.    [self setBorderType:NX_BEZEL];
  62.    [self setVertScrollerRequired:YES];
  63.  
  64.    /* The matrix may not be as large as the ClipView; a light gray background
  65.       makes the transition from Matrix to ScrollView less apparent. */
  66.  
  67.    [self setBackgroundGray:NX_LTGRAY];
  68.  
  69.    /* Make sure resize messages (superviewSizeChanged:) are passed down the
  70.       view chain so things will update correctly. */
  71.  
  72.    [self setAutoresizeSubviews:YES];
  73.    [contentView setAutoresizeSubviews:YES];
  74.    
  75.  
  76.    /* ---------- create the matrix managed by the scroll and clip views */
  77.  
  78.    [self getContentSize:&(matrixFrame.size)];
  79.  
  80.    matrix = [AutoWidthMatrix newFrame:&matrixFrame mode:NX_RADIOMODE
  81.              cellClass:[SelectionCell class] numRows:0 numCols:0];
  82.    [matrix setAutoscroll:YES];
  83.  
  84.    /* Setting the matrix background to light gray hides the intercell gaps. */
  85.  
  86.    [matrix setBackgroundGray:NX_LTGRAY];
  87.    
  88.    /* Attach the matrix to the scroll and clip views and size to superview. */
  89.  
  90.    [[self setDocView:matrix] free];
  91.    [matrix sizeToSuperviewWidth];
  92.  
  93.    return self;
  94. }
  95.  
  96. - setTarget:theTarget action:(SEL) theAction
  97. {
  98.    id matrix = [self docView];
  99.    
  100.    if ([matrix isKindOf:[Matrix class]] == NO)
  101.       return self;
  102.    
  103.    [[matrix setTarget:theTarget] setAction:theAction];
  104.  
  105.    return self;
  106. }
  107.  
  108. - removeSelectedName
  109. {
  110.    id matrix = [self docView];
  111.  
  112.    if (! ([matrix isKindOf:[Matrix class]] == YES && [matrix selectedCell]))
  113.        return self;
  114.  
  115.    [matrix removeRowAt:[matrix selectedRow] andFree:YES];
  116.    [matrix sizeToSuperviewWidth];
  117.    [matrix display];
  118.    
  119.    return self;
  120. }
  121.  
  122. - (const char *) selectedName
  123. {
  124.    id matrix = [self docView];
  125.  
  126.    if ([matrix isKindOf:[Matrix class]] == NO) return NULL;
  127.  
  128.    return [[matrix selectedCell] stringValue];
  129. }
  130.  
  131. - insertName:(const char *) name alphaOrder:(BOOL) aFlag select:(BOOL) sFlag
  132. {
  133.    int matrixRows, matrixCols;
  134.    id matrix = [self docView];
  135.    
  136.    if (! (name && [matrix isKindOf:[Matrix class]] == YES))
  137.       return self;
  138.    
  139.    [matrix addRow];
  140.    [matrix getNumRows:&matrixRows numCols:&matrixCols];
  141.    [[matrix cellAt:matrixRows-1:matrixCols-1] setStringValue:name];
  142.  
  143.    if (sFlag == YES)
  144.       [matrix selectCellAt:matrixRows-1 :matrixCols-1];
  145.  
  146.    [matrix sizeToSuperviewWidth];
  147.    [matrix display];
  148.  
  149.    return self;
  150. }
  151.  
  152. - replaceSelectedNameWith:(const char *) newName
  153. {
  154.    id matrix = [self docView];
  155.  
  156.    if (! (newName && [matrix isKindOf:[Matrix class]] == YES
  157.       && [matrix selectedCell]))
  158.       return self;
  159.  
  160.    [[matrix selectedCell] setStringValue:newName];
  161.    [[matrix selectedCell] setLeaf:NO];
  162.    
  163.    [matrix display];
  164.    
  165.    return self;
  166. }
  167.  
  168. - selectName:(const char *) name
  169. {
  170.    int matrixRow, matrixCol;
  171.    const char *cellName;
  172.    id matrix = [self docView];
  173.    
  174.    if (! (name && [matrix isKindOf:[Matrix class]] == YES))
  175.       return self;
  176.  
  177.    [matrix getNumRows:&matrixRow numCols:&matrixCol];
  178.    
  179.    for (matrixRow--; matrixRow >= 0; matrixRow--) {
  180.       cellName = [[matrix cellAt:matrixRow :0] stringValue];
  181.       if (cellName && strcmp (cellName, name) == 0) {
  182.      [matrix selectCellAt:matrixRow :0];
  183.      break;
  184.       }
  185.    }
  186.    
  187.    return self;
  188. }
  189.  
  190. - selectRow:(int) row
  191. {
  192.    if ([[self docView] isKindOf:[Matrix class]] == NO)
  193.       return self;
  194.    
  195.    return [[self docView] selectCellAt:row :0];
  196. }
  197.  
  198. - (int) selectedRow
  199. {
  200.    if ([[self docView] isKindOf:[Matrix class]] == NO)
  201.       return -1;
  202.  
  203.    return [[self docView] selectedRow];
  204. }
  205.  
  206. - free
  207. {
  208.    [[self docView] free];
  209.    [super free];
  210.    return self;
  211. }
  212.  
  213. @end
  214.