<?xml version='1.0' encoding='UTF-8'?>

<?xml-stylesheet href="./_c74_tut.xsl" type="text/xsl"?>

<chapter name="Max Data Tutorial 4: Cellblock">

<setdocpatch name="04dCellblock" patch="04dCellblock.maxpat"/>
<previous name="datachapter03">Gesture Capture</previous>
<next name="datachapter05">List Processing</next>
<parent name="00_maxindex">Max Tutorials</parent>

<indexinfo category="Data" title="Cellblock">Working with a visual spreadsheet interface</indexinfo>

<h1>Data Tutorial 4: Cellblock</h1>

<h2>Introduction</h2>

<p>This tutorial focuses on the <o>jit.cellblock</o> object (commonly referred to as “cellblock”). The <o>jit.cellblock</o> object provides a visual interface to a 2-dimensional matrix of data, where each data point (or “cell”) can contain any of the data types used in Max. This data can be changed and edited, and selected either by the user or by the patch.</p>

<p>The <o>jit.cellblock</o> object fills a special need in Max – an space-efficient display of Max values, with the ability to select or edit them visually within the patch.  This object, like the <o>lcd</o> object, supports a very large number of messages and attribute settings; in this tutorial, we will cover some of the most useful ones, but there are many more options available for advanced use.</p>

<p>To open the tutorial patch, click on the <b>Open Tutorial</b> button in the upper right-hand corner of the documentation window.</p>

<h2>Structural messages and setup</h2>

<p>Take a look at the patcher for this tutorial.  You will see a grid-based display, along with three sets of <o>message</o> boxes.  The grid in the middle of the patcher (which resembles a spreadsheet) is the <o>jit.cellblock</o> object; it provides a two-dimensional display of data stored in an internal structure.</p>

<p>Many of the structural settings of the <o>jit.cellblock</o> are available for programmatic control.  For example, there are two <o>number</o> boxes connected to <m>rows</m> and <m>cols</m> messages.  These number boxes can be used to manually change the number of rows and columns stored in the <o>jit.cellblock</o>.  If you add more rows or columns than can be displayed in the available area, scrollbars will automatically appear.</p>

<p>Another type of structure can be implied by changing the color of the first column or row with the object.  When this is done, it makes it clear that this row is an “index” or “identifier” for the remainder of the row. Using the <m>rowhead</m> and <m>colhead</m> messages, you can change the perceived use of this first value.  It’s important to note that, while there is a visual change to the header area, there is no change in your use of the data, or your ability to add or change it.</p>

<p>There are other visual attributes that can be changed that have no effect on the underlying data.  An example is the <m>grid</m> attribute.  When the grid is on, we get a familiar spreadsheet-like display of the data.  When it is off, however, the display of the values is not delineated by the drawn grid; this is especially handy if you want a less distinct grid layout that uses either text or background colors to distinguish data subsets.</p>

<p>There are more structural settings available for manipulation, and opening the object inspector gives access to most of them.  If we open the object inspector for the <o>jit.cellblock</o> object in the tutorial patch and select the <b>Structure</b> section, we see attribute settings that match the patch-based messages we just used.  Additionally, there are settings for the default column and row sizes: the width and height of these elements if they are not specifically set. If you adjust these settings, you will see the cells in the <o>jit.cellblock</o> dynamically change to match.</p>

<p>Other visual settings are available under the appearance section of the inspector. The colors of the various <o>jit.cellblock</o> elements can be set using the color picker; click on the color that you want to alter, and a standard color selector will be displayed. Select the <m>Default Background Color</m>, and change the color to a bright red.  You will notice that the background of all of the cells will be changed, but the other elements (like the color of the grid, or the currently selected cell) remain unchanged.</p>

<p>Familiarize yourself with the different settings on the left side of the patcher (labeled <b>1.Structure and Appearance</b>), as well as some of the values in the Inspector for the <o>jit.cellblock</o> object.</p>

<h2>Storing and retrieving values</h2>

<p>Beyond setting up the structure of the <o>jit.cellblock</o> object, we will need to select cells, store data in them, and retrieve the data again. First, let’s look at choosing a cell.  The easiest way to select a cell is to click on it – much like you would with a spreadsheet.  You will notice that selecting a new cell outputs the coordinates (in column/row order) from the left outlet of the object (this is <o>print</o>ed into the Max Console for us).   Since you may want the program to control the current selection, the <o>jit.cellblock</o> object will also respond to a <m>select</m> message, with arguments that choose the column and row to select. You will find this in the section labeled <b>2.Contents</b>. Using the two number boxes attached to the select message, you will be able to control the current selection of the <o>jit.cellblock</o>.  Notice that both the row and column number is zero-based, meaning that the first row and first column is selected by sending the message <m>select 0 0</m>.</p>

<p>The currently selected cell(s) can be referred to as “current” in certain messages.  So, the next <o>number</o> box is routed through a <m>set current</m> message; this will take the incoming value and change the current cell’s value to this integer. Test this out by selecting several different cells and changing the value.  Now click on any cell with a value: you will see that the output of the object has changed, with the Max Console displaying not only the column and row numbers, but also the <i>data</i> that is in the cell.</p>

<p>You don’t have to select a cell to change the data it contains. The next <m>message</m> box uses the <m>set x y value...</m> syntax for setting contents in the object.  The two integers following the <m>set</m> message represent the column and row that will be changed.  All data following these coordinates will be stored. If the data is an integer or floating-point number, that single entry will be stored. Symbols and lists can also be stored as values for a cell.  Clicking on the third <o>message</o> box will force cell <m>2 2</m> to contain the text <m>Here I am!</m>. Of course, we also have to be able to remove data, which is the function of the <m>clear</m> message.  The next <o>message</o> box generates the <m>clear 2 2</m> message, which will erase the text that we just set.</p>

<p>The <o>jit.cellblock</o> object is useful partly because it provides a large set of user options for selecting and manipulating data in a cell.  Of course, if we’ve gone through the trouble of setting the values of the cells, we wouldn’t want the user (or the program, for that matter) to change these values. Setting the <m>readonly</m> attribute to <m>1</m> will prevent the values from being changed.  When this is set, you can use the <o>jit.cellblock</o> without worrying about the values being changed.</p>

<h2>Selection and output options</h2>

<p>In order to be as flexible as possible, <o>jit.cellblock</o> has several selection and output modes.  The selection mode (set with the <m>selmode</m> attribute) defaults to selecting a single cell (known as “one cell select” mode).  If we look at the left <o>umenu</o> of section <b>3. Selection and Output</b>, we will see that there are six different options for cell selection.</p>

<p>The first option (option <m>0</m>, “no selection") prevents any cell selection. This can be useful if you want the <o>jit.cellblock</o> object to act like a large set of labels. Option <m>1</m>, “single cell select”, is the default selection handler, where only one cell can be selected at a time.  The next two options (<m>2</m> and <m>3</m>) all you to select a large number of cells with a single click.  Option <m>2</m> (“column select”) will select all cells in any column that you select.  Option <m>3</m> (“row select”) does the same for row values.</p>

<p>The fifth option (option <m>4</m>, “header select”) is more involved.  If you have column or row headers turned on, and if you click on a header cell, the entire column or row will be selected.  Otherwise, only a single cell will be selected.  This is very similar to the way a spreadsheet works, where the headers have special selection rules that are different from standard cells.</p>

<p>The last option (option <m>5</m>, “in-place edit”) provides an easy way to enter data into the <o>jit.cellblock</o>. Double-clicking on any cell will allow you to edit or add information into the selected cell.  This may be more convenient than using <m>select</m> and <m>set</m> messages, although it cannot be controlled with your patch.</p>

<p>Since several of these modes will select multiple cells, it begs the question “What format will the data be in?”  The <o>jit.cellblock</o> object provides three output modes as an answer - these are set with the <m>outmode</m> attribute. Mode <m>0</m>, or “individual output” mode, will send each selected cell in its own message, and will output a message for cells that may not have any data value.  Mode <m>1</m>, or “output as one list”, will combine all non-blank cell values into a single list, and output it in a single message.  Mode <m>2</m>, or “output as one symbol”, will combine all of the non-blank cell values into a list, then turn the list into a single text <i>symbol</i>.  This is most useful if you have filled your <o>jit.cellblock</o> with textual values, and want to combine them into larger phrases.</p>

<p>To see the output mode in action, click on the <o>button</o> attached to the <b>loader</b> subpatcher.  This will format and load the <o>jit.cellblock</o> object with numbers from <m>0</m> to <m>99</m>.  The <o>jit.cellblock</o> is also flipped into “row select” mode, so we will be selecting many cells with each click.  Click on a cell to select a row; you will notice that 10 messages appear in the Max Console sequentially, one for each cell that is selected. Now, change the output mode to “output as one list”, and click on the sixth row.  The Max Console now shows a single line result, meaning that only one message was produced.  The first two numbers are the column (irrelevant, in this case) and row (5, since <o>jit.cellblock</o> is zero-based), then a list of all the values in that row.</p>

<p>If we change the output mode to “output as one symbol”, the results in the Max Console seem no different.  However, if you look at the output of the <o>zl</o> <m>len</m> object (covered in depth in another tutorial) connected to the <o>jit.cellblock</o> object's outlet, you will see that our message only has <m>3</m> elements, rather than the <m>12</m> elements we saw in the previous mode.</p>

<h2>Summary</h2>

<p>The <o>jit.cellblock</o> object is a robust tool for dealing with 2D data of any type, and for the presentation of data in a grid-like format. We have only touched on a few of the most useful commands; for more information, review the help files and reference documentation on this object.</p>

<seealsolist>
<seealso name="jit.cellblock">Two-dimensional storage and viewing</seealso>
</seealsolist>

</chapter>
