matrix (Using the GNUstep AppKit 0.1)

## 11 Matrix Controls ¶

Matrix controls are groups of cells, arranged in a table like format, with each cell indexed by row and column. It is similar to a table view, but differs in that it doesn’t have a predefined cell class. Instead, it uses objects of classes derived from NSCell to be implemented. A matrix is implemented by the NSMatrix class.14

It takes it’s implementation from NSControl, and hence what applies to controls also applies to matrixes. This matrix control is used to implement browsers as well. Note that it does not use a delegate or a data source to display or obtain it’s data or size. Instead, this is done using accessor methods, making the NSMatrix class more passive in it’s representation of data.

### 11.1 Creating Matrix Controls ¶

A matrix control can be creating by new `NSMatrix` instance and then calling `-initWithFrame:mode:prototype:numberOfRows:numberOfColumns:` or `-initWithFrame:mode:cellClass:numberOfRows:numberOfColumns:`. The former method uses an instance of a cell to instantiate cells for the rows and columns, while the latter uses a cell class to create the cells.

Both these methods require a matrix mode, those of which are specified in `NSMatrixMode` and control how the matrix "tracks" the mouse:

`NSRadioModeMatrix`

  Only permits one cell in the matrix to be selected at a time.

`NSHighlightModeMatrix`

  Performs trackng (as described below) as well as highlighting the cell before tracking commences.

`NSListModeMatrix`

  Cells objects are highlighted without the opportunity to track the mouse.

`NSTrackModeMatrix`

  The cell is able to track the mouse while the cursor is within it’s bounds.

For more information about cell tracking, See Basic Controls.

### 11.2 Using Matrix controls ¶

After having placed one on a window using Gorm, we can change what appears in the matrix by using it’s methods.

The cell class is what class is to be used (by default) to create cells. We can use instances of many different cells classes, for example, you may choose to populate your matrix with `NSTextCell` instances as well as `NSButtonCell` instances if you were creating an interactive form. You set the default cell class by calling either `+setCellClass:` on the `NSMatrix` class to set the cell class over all new `NSMatrix` instances, or you can call `-setCellClass:` to set the class used to create new cells on a instance-by-instance basis for each of your matrix instances.

We can retrieve information about the cells in a matrix through a variety of methods. To retrieve the cell at a certain location, use the `-cellAtRow:column:` method. The size of cells is retrieved using the `-cellSize` method. To access specific cells, use `-cellAtRow:column:`, or to access all the cells, simple call `-cells` to get an array.

We can begin adding rows or columns to the end our matrix using the `-addColumn` and `-addRow` methods. To specify the specific cells, use the `-addColumnWithCells:` and `-addRowWithCells:` methods, passing an array of the cells for that column/row. Alternatively, rows and columns can be inserted at arbitrary locations using the `-insertRow:` and `-insertColumn:` methods, specifying a row or column number. `-insertRow:withCells:` and `-insertColumn:withCells:` lets you pass in the cells to be inserted.

Rows and columns can also be removed or replaced. You can remove a column or a row by number using the `-removeColumn:` or `-removeRow:` methods respectively. To replace a particular cell, use the `-putCell:atRow:column:` method.

The cell selection and selection behaviour can be modified. A specific cell can be selected with the `-selectCellAtRow:column:` by specifying it’s location, `-selectCellWithTag:` by specifying it’s tag, or `-selectCell:` with the cell object. You can also select all the cells with the `-selectAll:` method.

The selected cell is returned from `-selectedCell:`, or `-selectedCells` if more than one cell is selected. `-selectedRow` and `-selectedColumn` can be used if an entire row/column is selected.

#### Footnotes

##### (14)

Matrices, as referred to here, are not to be confused with affine transforms, the latter of which is commonly referred to as a matrix, due to it’s internal implementation of a mathematical matrix.