Loading document parsing libraries...
# Selection
Class
## About
`Selection` holds the editor's selection: the selected nodes, the text selection while a node is being edited, and the commands that act on them. Each `Editor` creates one when it is constructed; read it from the editor's read-only `Selection` property.
```xojo
If Editor.Selection.CanCopy Then Editor.Selection.Copy
Editor.Selection.SelectAll
```
`Node` is the primary selected node and `Nodes` returns a copy of every selected node; `IsSelected`, `LastNodeIndex`, and `Description` report on them. `SelectNode`, `SelectAllNodes`, and `Clear` change the node selection, ending any text-editing session first. `DeleteNodes` and `Duplicate` act on the selected nodes even while one is being edited. `SelectText` selects a range in the content being edited. `MoveUp` and `MoveDown` move the primary selected node one place within its container, or within the document at the top level, as Option-Shift-Up and Option-Shift-Down (Alt-Shift elsewhere) do; `CanMoveUp` and `CanMoveDown` report whether there is a place to move to. A move is one undo step. Setting `Node` replaces the selection quietly: it does not end editing, raise `SelectionChanged`, or scroll, so use `SelectNode` for the interactive behavior.
The editing commands follow the editing context. While a node is being edited, commands act on its text: `SelectAll` selects all of it, `DeselectAll` collapses the selection to the caret and keeps editing, and `Copy`, `Cut`, and `Delete` act only on selected text, so an empty text selection does nothing. Otherwise, commands act on nodes. `Cut`, `Delete`, and `Paste` do nothing while the editor is `ReadOnly`.
Each command has a matching `Can` property that reports whether it would act, using the same rules. The editor's keyboard shortcuts and context menu use the same rules.
The editor does not handle menu-bar items itself. To connect an application's Edit menu, enable its items from these properties and call these methods from the menu handlers. Undo and redo are on `Editor.History`.
```xojo
Function EditCopy() As Boolean Handles EditCopy.MenuItemSelected
Editor.Selection.Copy
Return True
End Function
```
## Enumerations
| Name | Value | Description |
| --- | :---: | --- |
| Commands | Copy = 0 <br > Cut = 1 <br > Delete = 2 <br > DeselectAll = 3 <br > Paste = 4 <br > SelectAll = 5 <br > Clear = 6 <br > DeleteNodes = 7 <br > Duplicate = 8 <br > SelectAllNodes = 9 <br > MoveUp = 10 <br > MoveDown = 11 | (None) |
## Delegates
| Name | Parameters | Return Type | Description |
| --- | :---: | :---: | --- |
| CommandHandler | command As GraffitiProEditor . Selection . Commands <br> perform As Boolean) As Boolean | command As GraffitiProEditor . Selection . Commands, perform As Boolean) As Boolean | (None) |
| NodeHandler | value As GraffitiProEditor . Node <br> assign As Boolean) As GraffitiProEditor.Node | value As GraffitiProEditor . Node, assign As Boolean) As GraffitiProEditor.Node | (None) |
| NodesHandler | ) As GraffitiProEditor.Node() | ) As GraffitiProEditor.Node() | (None) |
| SelectNodeHandler | node As GraffitiProEditor . Node <br> addToSelection As Boolean) | (None) | (None) |
| SelectTextHandler | start As Integer <br> length As Integer) As Boolean | start As Integer, length As Integer) As Boolean | (None) |
## Methods
| Name | Parameters | Return Type | Description |
| --- | :---: | :---: | --- |
| Clear | (None) | (None) | Ends any text-editing session and clears the node selection. |
| Copy | (None) | (None) | Copies the selected text while editing, or the selected nodes otherwise, to the system clipboard. |
| Cut | (None) | (None) | Copies and then removes the selected text while editing, or the selected nodes otherwise. Does nothing while the editor is read-only. |
| Delete | (None) | (None) | Removes the selected text while editing, or the selected nodes otherwise. An empty text selection removes nothing. Does nothing while the editor is read-only. |
| DeleteNodes | (None) | (None) | Deletes the selected nodes from the document, including a node being edited. Locked nodes and nodes that cannot be deleted are skipped. Does nothing while the editor is read-only. |
| DeselectAll | (None) | (None) | Collapses the text selection to the caret while editing, or clears the node selection otherwise. |
| Duplicate | (None) | (None) | Duplicates every selected node that can be duplicated, inserts each copy after its source, and selects the copies. Does nothing while the editor is read-only. |
| IsSelected | node As GraffitiProEditor.Node | Boolean | Returns whether the supplied node is selected. |
| LastNodeIndex | (None) | Integer | Returns the zero-based index of the last selected node, or -1 when no node is selected. |
| MoveDown | (None) | (None) | Moves the primary selected node after the next node in its container, or in the document. Does nothing at the end, or while the editor is read-only. |
| MoveUp | (None) | (None) | Moves the primary selected node before the previous node in its container, or in the document. Does nothing at the start, or while the editor is read-only. |
| Nodes | (None) | GraffitiProEditor.Node | Returns a copy of the selected nodes. |
| Paste | (None) | (None) | Inserts the clipboard contents at the caret while editing, or as nodes otherwise. Does nothing while the editor is read-only. |
| SelectAll | (None) | (None) | Selects all content of the node being edited, or all selectable root-level nodes otherwise. |
| SelectAllNodes | (None) | (None) | Ends any text-editing session and selects all selectable root-level nodes. |
| SelectNode | node As GraffitiProEditor.Node <br> addToSelection As Boolean = False | (None) | Ends any text-editing session and selects the supplied node, scrolling it into view. When addToSelection is True, toggles the node in the existing selection instead. |
| SelectText | start As Integer <br> length As Integer | Boolean | Selects a grapheme-aligned range in the content being edited and returns whether it was accepted. Returns False when no node is being edited. |
## Properties
| Name | Type | Default Value | Description |
| --- | :---: | :---: | --- |
| CanCopy | Boolean | (None) | Returns True when Copy would copy something: selected text while editing, or selected nodes otherwise. |
| CanCut | Boolean | (None) | Returns True when Cut would remove something and the editor is not read-only. |
| CanDelete | Boolean | (None) | Returns True when Delete would remove something and the editor is not read-only. |
| CanDeselectAll | Boolean | (None) | Returns True when DeselectAll would change something: a text selection while editing, or selected nodes otherwise. |
| CanDuplicate | Boolean | (None) | Returns True when Duplicate would copy at least one selected node and the editor is not read-only. |
| CanMoveDown | Boolean | (None) | Returns True when MoveDown would move the primary selected node and the editor is not read-only. |
| CanMoveUp | Boolean | (None) | Returns True when MoveUp would move the primary selected node and the editor is not read-only. |
| CanPaste | Boolean | (None) | Returns True when the editor is not read-only and the clipboard holds content Paste can insert. |
| CanSelectAll | Boolean | (None) | Returns True when SelectAll would select something: the content being edited, or the document's nodes otherwise. |
| Description | String | (None) | Returns a short description of the node selection, such as "2 selected: Heading, Text", or the editor's TextNoSelectionCaption when nothing is selected. |
| Node | GraffitiProEditor.Node | (None) | Gets or sets the primary selected node. Setting it replaces the node selection with that node, or clears it when Nil, without ending a text-editing session, raising SelectionChanged, or scrolling; use SelectNode for the interactive behavior. A node that cannot be selected is ignored. |
| Owner | GraffitiProEditor.Editor | (None) | Returns the editor this selection belongs to. |