<!-- Generated by tools/SharpCell.Grid.Docs from the XML documentation comments. Do not edit; run the tool with 'generate'. -->

# GridPainter

```csharp
public sealed class GridPainter
```

Namespace `SharpCell.Grid` · package `SharpCell.Grid`

Paints a worksheet the way Excel shows it: column and row headers, gridlines, each cell's `Cell.Text` in its style (font, fill, borders, alignment, wrapping, indent), merged areas as one cell, and the active cell. Only the cells inside the viewport are read, so a sheet's size does not matter. Also answers the geometry questions of a grid on screen: where a cell is, what is under a point, where to scroll. With [GridPainter.MeasureText](SharpCell.Grid.GridPainter.md#measuretext) set, text too wide for its cell runs on over empty neighbours, numbers too wide for their column are shortened or shown as `###`, and row headers widen for long row numbers, as in Excel.

## Constructors

<a id="ctor"></a>
### GridPainter()

```csharp
public GridPainter()
```

A painter with Excel's default row heights and column widths.

<a id="ctor-sheetlayout"></a>
### GridPainter(SheetLayout)

```csharp
public GridPainter(SheetLayout layout)
```

A painter over the given sizes of rows and columns.

## Properties

<a id="columnheaderheight"></a>
### ColumnHeaderHeight

```csharp
public double ColumnHeaderHeight { get; set; }
```

The height of the column headers at the top at 100% zoom; [GridPainter.Zoom](SharpCell.Grid.GridPainter.md#zoom) scales it.

<a id="copiedarea"></a>
### CopiedArea

```csharp
public CellArea? CopiedArea { get; set; }
```

The cells copied or cut, painted with a dashed border as Excel shows them until they are pasted; null for none.

<a id="fillarea"></a>
### FillArea

```csharp
public CellArea? FillArea { get; set; }
```

The cells a fill handle is being dragged over, painted with a dashed grey border; null for none.

<a id="frozencolumns"></a>
### FrozenColumns

```csharp
public int FrozenColumns { get; set; }
```

How many columns on the left stay in place while the rest scrolls; a sheet's own is `Worksheet.FrozenColumns`.

**Exceptions**

- `ArgumentOutOfRangeException`: The count is negative or leaves no column to scroll.

<a id="frozenrows"></a>
### FrozenRows

```csharp
public int FrozenRows { get; set; }
```

How many rows at the top stay in place while the rest scrolls, as Excel's Freeze Panes; a sheet's own is `Worksheet.FrozenRows`.

**Exceptions**

- `ArgumentOutOfRangeException`: The count is negative or leaves no row to scroll.

<a id="layout"></a>
### Layout

```csharp
public SheetLayout Layout { get; set; }
```

The sizes of rows and columns; [SheetLayout.FromWorksheet](SharpCell.Grid.SheetLayout.md#fromworksheet-worksheet-double) gives a sheet's own.

<a id="measuretext"></a>
### MeasureText

```csharp
public Func<string, GridFont, double>? MeasureText { get; set; }
```

The width in pixels of a line of text in a font. Without it, text is never fitted to its cell, row headers keep [GridPainter.RowHeaderWidth](SharpCell.Grid.GridPainter.md#rowheaderwidth), and Calibri 11's digit width is assumed.

<a id="referencearea"></a>
### ReferenceArea

```csharp
public CellArea? ReferenceArea { get; set; }
```

The cells picked with the mouse while a formula is typed, painted with a dashed blue border; null for none.

<a id="rowheaderwidth"></a>
### RowHeaderWidth

```csharp
public double RowHeaderWidth { get; set; }
```

The width of the row headers on the left at 100% zoom; with [GridPainter.MeasureText](SharpCell.Grid.GridPainter.md#measuretext), the least width, widened to fit the number of the last visible row. [GridPainter.Zoom](SharpCell.Grid.GridPainter.md#zoom) scales it.

<a id="showsfillhandle"></a>
### ShowsFillHandle

```csharp
public bool ShowsFillHandle { get; set; }
```

Whether the selection's bottom-right corner has Excel's fill handle, the square dragged to fill cells. False by default; a view that changes cells turns it on.

<a id="theme"></a>
### Theme

```csharp
public GridTheme Theme { get; set; }
```

The colours of everything but the cells' own styles.

<a id="zoom"></a>
### Zoom

```csharp
public double Zoom { get; set; }
```

How large cells and headers are painted, 1 for Excel's 100%: fonts, headers and the space around text grow with it, lines stay one pixel. [GridPainter.Layout](SharpCell.Grid.GridPainter.md#layout) is not scaled: give it the sheet's sizes at the zoom, as [SheetLayout.FromWorksheet](SharpCell.Grid.SheetLayout.md#fromworksheet-worksheet-double-double) with [GridPainter.MaxDigitWidth](SharpCell.Grid.GridPainter.md#maxdigitwidth-worksheet) does.

**Exceptions**

- `ArgumentOutOfRangeException`: The zoom is outside Excel's 10% to 400%.

## Methods

<a id="cellbounds-gridviewport-cellposition"></a>
### CellBounds(GridViewport, CellPosition)

```csharp
public GridRect CellBounds(GridViewport viewport, CellPosition cell)
```

Where a cell is on screen; it may lie outside the viewport.

<a id="cellbounds-gridviewport-cellposition-cellposition"></a>
### CellBounds(GridViewport, CellPosition, CellPosition)

```csharp
public GridRect CellBounds(GridViewport viewport, CellPosition first, CellPosition last)
```

Where a rectangle of cells is on screen, from its top-left to its bottom-right cell.

<a id="cellnearest-gridviewport-double-double"></a>
### CellNearest(GridViewport, double, double)

```csharp
public CellPosition CellNearest(GridViewport viewport, double x, double y)
```

The cell nearest to a point, for dragging a selection: the cell under it, or past an edge of the cells the next one out of view that way, so that scrolling to it follows the mouse.

<a id="clamp-gridviewport"></a>
### Clamp(GridViewport)

```csharp
public GridViewport Clamp(GridViewport viewport)
```

The viewport with its offsets kept between the start and the end of the sheet.

<a id="columnedgeat-gridviewport-double-double"></a>
### ColumnEdgeAt(GridViewport, double, double)

```csharp
public int? ColumnEdgeAt(GridViewport viewport, double x, double y)
```

The column whose right edge is under a point of the column headers, as Excel lets it be dragged to resize the column; null elsewhere. Hidden columns are never found.

<a id="fitcolumnwidth-worksheet-int"></a>
### FitColumnWidth(Worksheet, int)

```csharp
public double? FitColumnWidth(Worksheet sheet, int column)
```

The width in pixels at [GridPainter.Zoom](SharpCell.Grid.GridPainter.md#zoom) that fits the widest text of a column, as Excel's AutoFit Column Width does; merged cells are left out. Null when the column holds nothing to fit or without [GridPainter.MeasureText](SharpCell.Grid.GridPainter.md#measuretext).

<a id="fitrowheight-worksheet-int"></a>
### FitRowHeight(Worksheet, int)

```csharp
public double? FitRowHeight(Worksheet sheet, int row)
```

The height in pixels at [GridPainter.Zoom](SharpCell.Grid.GridPainter.md#zoom) that fits the tallest text of a row, as Excel's AutoFit Row Height does: a line of each cell's font, and as many lines as wrapped text or line breaks take. Null when the row holds nothing, for the sheet's default height.

<a id="fontof-cellfont"></a>
### FontOf(CellFont)

```csharp
public static GridFont FontOf(CellFont font)
```

The font of a SharpCell font at 96 DPI.

<a id="hit-gridviewport-double-double-worksheet"></a>
### Hit(GridViewport, double, double, Worksheet)

```csharp
public GridHit? Hit(GridViewport viewport, double x, double y, Worksheet? sheet = null)
```

What is under a point of the grid: a cell, a column or row header, or the corner above the row headers; null outside the grid. [GridSelection.At](SharpCell.Grid.GridSelection.md#at-gridhit-worksheet) turns it into what a click selects.

<a id="hittest-gridviewport-double-double-worksheet"></a>
### HitTest(GridViewport, double, double, Worksheet)

```csharp
public CellPosition? HitTest(GridViewport viewport, double x, double y, Worksheet? sheet = null)
```

The cell under a point of the grid; null on the headers or outside the grid. Inside a merged area of the sheet, its top-left cell.

<a id="isonfillhandle-gridviewport-double-double-gridselection-worksheet"></a>
### IsOnFillHandle(GridViewport, double, double, GridSelection, Worksheet)

```csharp
public bool IsOnFillHandle(GridViewport viewport, double x, double y, GridSelection? selection, Worksheet? sheet)
```

Whether a point is on the fill handle of the selection, when [GridPainter.ShowsFillHandle](SharpCell.Grid.GridPainter.md#showsfillhandle) shows one.

<a id="maxdigitwidth-worksheet"></a>
### MaxDigitWidth(Worksheet)

```csharp
public double MaxDigitWidth(Worksheet? sheet)
```

The width in pixels of the widest digit of a sheet's default font at [GridPainter.Zoom](SharpCell.Grid.GridPainter.md#zoom), which Excel bases column widths on; 7, Calibri 11's at 100%, without [GridPainter.MeasureText](SharpCell.Grid.GridPainter.md#measuretext).

<a id="paint-igridcanvas-worksheet-gridviewport-cellposition"></a>
### Paint(IGridCanvas, Worksheet, GridViewport, CellPosition?)

```csharp
public void Paint(IGridCanvas canvas, Worksheet? sheet, GridViewport viewport, CellPosition? activeCell = default)
```

Paints the part of the sheet in the viewport with the active cell alone selected; without a sheet, an empty grid.

<a id="paint-igridcanvas-worksheet-gridviewport-gridselection"></a>
### Paint(IGridCanvas, Worksheet, GridViewport, GridSelection)

```csharp
public void Paint(IGridCanvas canvas, Worksheet? sheet, GridViewport viewport, GridSelection? selection)
```

Paints the part of the sheet in the viewport and the selection over it, as Excel does: the selected cells but the active one shaded, a border around them, their headers highlighted. Without a sheet, an empty grid; without a selection, none.

<a id="rowedgeat-gridviewport-double-double"></a>
### RowEdgeAt(GridViewport, double, double)

```csharp
public int? RowEdgeAt(GridViewport viewport, double x, double y)
```

The row whose bottom edge is under a point of the row headers; null elsewhere. See [GridPainter.ColumnEdgeAt](SharpCell.Grid.GridPainter.md#columnedgeat-gridviewport-double-double).

<a id="scrollintoview-gridviewport-cellposition"></a>
### ScrollIntoView(GridViewport, CellPosition)

```csharp
public GridViewport ScrollIntoView(GridViewport viewport, CellPosition cell)
```

The viewport scrolled just enough to show the whole cell, or its top-left part when it is larger than the viewport; unchanged when the cell is already in view.

<a id="visiblearea-gridviewport"></a>
### VisibleArea(GridViewport)

```csharp
public CellArea VisibleArea(GridViewport viewport)
```

The cells the viewport shows, partly or wholly, of the part that scrolls: frozen rows and columns aside.
