Public Types | Public Member Functions

TreeView Class Reference

A tree-view component. More...

Inherits Component, SettableTooltipClient, FileDragAndDropTarget, DragAndDropTarget, and AsyncUpdater.

Inherited by FileTreeComponent.

List of all members.

Public Types

enum  ColourIds { backgroundColourId = 0x1000500, linesColourId = 0x1000501, dragAndDropIndicatorColourId = 0x1000502 }
 

A set of colour IDs to use to change the colour of various aspects of the treeview.

More...

Public Member Functions

 TreeView (const String &componentName=String::empty)
 Creates an empty treeview.
 ~TreeView ()
 Destructor.
void setRootItem (TreeViewItem *newRootItem)
 Sets the item that is displayed in the treeview.
TreeViewItemgetRootItem () const noexcept
 Returns the tree's root item.
void deleteRootItem ()
 This will remove and delete the current root item.
void setRootItemVisible (bool shouldBeVisible)
 Changes whether the tree's root item is shown or not.
bool isRootItemVisible () const noexcept
 Returns true if the root item is visible.
void setDefaultOpenness (bool isOpenByDefault)
 Sets whether items are open or closed by default.
bool areItemsOpenByDefault () const noexcept
 Returns true if the tree's items default to being open.
void setMultiSelectEnabled (bool canMultiSelect)
 This sets a flag to indicate that the tree can be used for multi-selection.
bool isMultiSelectEnabled () const noexcept
 Returns whether multi-select has been enabled for the tree.
void setOpenCloseButtonsVisible (bool shouldBeVisible)
 Sets a flag to indicate whether to hide the open/close buttons.
bool areOpenCloseButtonsVisible () const noexcept
 Returns whether open/close buttons are shown.
void clearSelectedItems ()
 Deselects any items that are currently selected.
int getNumSelectedItems (int maximumDepthToSearchTo=-1) const noexcept
 Returns the number of items that are currently selected.
TreeViewItemgetSelectedItem (int index) const noexcept
 Returns one of the selected items in the tree.
int getNumRowsInTree () const
 Returns the number of rows the tree is using.
TreeViewItemgetItemOnRow (int index) const
 Returns the item on a particular row of the tree.
TreeViewItemgetItemAt (int yPosition) const noexcept
 Returns the item that contains a given y position.
void scrollToKeepItemVisible (TreeViewItem *item)
 Tries to scroll the tree so that this item is on-screen somewhere.
ViewportgetViewport () const noexcept
 Returns the treeview's Viewport object.
int getIndentSize () const noexcept
 Returns the number of pixels by which each nested level of the tree is indented.
void setIndentSize (int newIndentSize)
 Changes the distance by which each nested level of the tree is indented.
TreeViewItemfindItemFromIdentifierString (const String &identifierString) const
 Searches the tree for an item with the specified identifier.
XmlElementgetOpennessState (bool alsoIncludeScrollPosition) const
 Saves the current state of open/closed nodes so it can be restored later.
void restoreOpennessState (const XmlElement &newState, bool restoreStoredSelection)
 Restores a previously saved arrangement of open/closed nodes.
void paint (Graphics &g)
 Components can override this method to draw their content.
void resized ()
 Called when this component's size has been changed.
bool keyPressed (const KeyPress &key)
 Called when a key is pressed.
void colourChanged ()
 This method is called when a colour is changed by the setColour() method.
void enablementChanged ()
 Callback to indicate that this component has been enabled or disabled.
bool isInterestedInFileDrag (const StringArray &files)
 Callback to check whether this target is interested in the set of files being offered.
void fileDragEnter (const StringArray &files, int x, int y)
 Callback to indicate that some files are being dragged over this component.
void fileDragMove (const StringArray &files, int x, int y)
 Callback to indicate that the user is dragging some files over this component.
void fileDragExit (const StringArray &files)
 Callback to indicate that the mouse has moved away from this component.
void filesDropped (const StringArray &files, int x, int y)
 Callback to indicate that the user has dropped the files onto this component.
bool isInterestedInDragSource (const SourceDetails &)
 Callback to check whether this target is interested in the type of object being dragged.
void itemDragEnter (const SourceDetails &)
 Callback to indicate that something is being dragged over this component.
void itemDragMove (const SourceDetails &)
 Callback to indicate that the user is dragging something over this component.
void itemDragExit (const SourceDetails &)
 Callback to indicate that something has been dragged off the edge of this component.
void itemDropped (const SourceDetails &)
 Callback to indicate that the user has dropped something onto this component.

Detailed Description

A tree-view component.

Use one of these to hold and display a structure of TreeViewItem objects.


Member Enumeration Documentation

A set of colour IDs to use to change the colour of various aspects of the treeview.

These constants can be used either via the Component::setColour(), or LookAndFeel::setColour() methods.

See also:
Component::setColour, Component::findColour, LookAndFeel::setColour, LookAndFeel::findColour
Enumerator:
backgroundColourId 

A background colour to fill the component with.

linesColourId 

The colour to draw the lines with.

dragAndDropIndicatorColourId 

The colour to use for the drag-and-drop target position indicator.


Constructor & Destructor Documentation

TreeView::TreeView ( const String componentName = String::empty )

Creates an empty treeview.

Once you've got a treeview component, you'll need to give it something to display, using the setRootItem() method.

TreeView::~TreeView (  )

Destructor.


Member Function Documentation

void TreeView::setRootItem ( TreeViewItem newRootItem )

Sets the item that is displayed in the treeview.

A tree has a single root item which contains as many sub-items as it needs. If you want the tree to contain a number of root items, you should still use a single root item above these, but hide it using setRootItemVisible().

You can pass in 0 to this method to clear the tree and remove its current root item.

The object passed in will not be deleted by the treeview, it's up to the caller to delete it when no longer needed. BUT make absolutely sure that you don't delete this item until you've removed it from the tree, either by calling setRootItem (nullptr), or by deleting the tree first. You can also use deleteRootItem() as a quick way to delete it.

TreeViewItem* TreeView::getRootItem (  ) const

Returns the tree's root item.

This will be the last object passed to setRootItem(), or 0 if none has been set.

void TreeView::deleteRootItem (  )

This will remove and delete the current root item.

It's a convenient way of deleting the item and calling setRootItem (nullptr).

void TreeView::setRootItemVisible ( bool  shouldBeVisible )

Changes whether the tree's root item is shown or not.

If the root item is hidden, only its sub-items will be shown in the treeview - this lets you make the tree look as if it's got many root items. If it's hidden, this call will also make sure the root item is open (otherwise the treeview would look empty).

bool TreeView::isRootItemVisible (  ) const

Returns true if the root item is visible.

See also:
setRootItemVisible
void TreeView::setDefaultOpenness ( bool  isOpenByDefault )

Sets whether items are open or closed by default.

Normally, items are closed until the user opens them, but you can use this to make them default to being open until explicitly closed.

See also:
areItemsOpenByDefault
bool TreeView::areItemsOpenByDefault (  ) const

Returns true if the tree's items default to being open.

See also:
setDefaultOpenness
void TreeView::setMultiSelectEnabled ( bool  canMultiSelect )

This sets a flag to indicate that the tree can be used for multi-selection.

You can always select multiple items internally by calling the TreeViewItem::setSelected() method, but this flag indicates whether the user is allowed to multi-select by clicking on the tree.

By default it is disabled.

See also:
isMultiSelectEnabled
bool TreeView::isMultiSelectEnabled (  ) const

Returns whether multi-select has been enabled for the tree.

See also:
setMultiSelectEnabled
void TreeView::setOpenCloseButtonsVisible ( bool  shouldBeVisible )

Sets a flag to indicate whether to hide the open/close buttons.

See also:
areOpenCloseButtonsVisible
bool TreeView::areOpenCloseButtonsVisible (  ) const

Returns whether open/close buttons are shown.

See also:
setOpenCloseButtonsVisible
void TreeView::clearSelectedItems (  )

Deselects any items that are currently selected.

int TreeView::getNumSelectedItems ( int  maximumDepthToSearchTo = -1 ) const

Returns the number of items that are currently selected.

If maximumDepthToSearchTo is >= 0, it lets you specify a maximum depth to which the tree will be recursed.

See also:
getSelectedItem, clearSelectedItems

Referenced by FileTreeComponent::getNumSelectedFiles().

TreeViewItem* TreeView::getSelectedItem ( int  index ) const

Returns one of the selected items in the tree.

Parameters:
indexthe index, 0 to (getNumSelectedItems() - 1)
int TreeView::getNumRowsInTree (  ) const

Returns the number of rows the tree is using.

This will depend on which items are open.

See also:
TreeViewItem::getRowNumberInTree()
TreeViewItem* TreeView::getItemOnRow ( int  index ) const

Returns the item on a particular row of the tree.

If the index is out of range, this will return 0.

See also:
getNumRowsInTree, TreeViewItem::getRowNumberInTree()
TreeViewItem* TreeView::getItemAt ( int  yPosition ) const

Returns the item that contains a given y position.

The y is relative to the top of the TreeView component.

void TreeView::scrollToKeepItemVisible ( TreeViewItem item )

Tries to scroll the tree so that this item is on-screen somewhere.

Viewport* TreeView::getViewport (  ) const

Returns the treeview's Viewport object.

int TreeView::getIndentSize (  ) const

Returns the number of pixels by which each nested level of the tree is indented.

See also:
setIndentSize
void TreeView::setIndentSize ( int  newIndentSize )

Changes the distance by which each nested level of the tree is indented.

See also:
getIndentSize
TreeViewItem* TreeView::findItemFromIdentifierString ( const String identifierString ) const

Searches the tree for an item with the specified identifier.

The identifer string must have been created by calling TreeViewItem::getItemIdentifierString(). If no such item exists, this will return false. If the item is found, all of its items will be automatically opened.

XmlElement* TreeView::getOpennessState ( bool  alsoIncludeScrollPosition ) const

Saves the current state of open/closed nodes so it can be restored later.

This takes a snapshot of which nodes have been explicitly opened or closed, and records it as XML. To identify node objects it uses the TreeViewItem::getUniqueName() method to create named paths. This means that the same state of open/closed nodes can be restored to a completely different instance of the tree, as long as it contains nodes whose unique names are the same.

The caller is responsible for deleting the object that is returned.

Parameters:
alsoIncludeScrollPositionif this is true, the state will also include information about where the tree has been scrolled to vertically, so this can also be restored
See also:
restoreOpennessState
void TreeView::restoreOpennessState ( const XmlElement newState,
bool  restoreStoredSelection 
)

Restores a previously saved arrangement of open/closed nodes.

This will try to restore a snapshot of the tree's state that was created by the getOpennessState() method. If any of the nodes named in the original XML aren't present in this tree, they will be ignored.

If restoreStoredSelection is true, it will also try to re-select any items that were selected in the stored state.

See also:
getOpennessState
void TreeView::paint ( Graphics g ) [virtual]

Components can override this method to draw their content.

The paint() method gets called when a region of a component needs redrawing, either because the component's repaint() method has been called, or because something has happened on the screen that means a section of a window needs to be redrawn.

Any child components will draw themselves over whatever this method draws. If you need to paint over the top of your child components, you can also implement the paintOverChildren() method to do this.

If you want to cause a component to redraw itself, this is done asynchronously - calling the repaint() method marks a region of the component as "dirty", and the paint() method will automatically be called sometime later, by the message thread, to paint any bits that need refreshing. In Juce (and almost all modern UI frameworks), you never redraw something synchronously.

You should never need to call this method directly - to take a snapshot of the component you could use createComponentSnapshot() or paintEntireComponent().

Parameters:
gthe graphics context that must be used to do the drawing operations.
See also:
repaint, paintOverChildren, Graphics

Reimplemented from Component.

void TreeView::resized (  ) [virtual]

Called when this component's size has been changed.

A component can implement this method to do things such as laying out its child components when its width or height changes.

The method is called synchronously as a result of the setBounds or setSize methods, so repeatedly changing a components size will repeatedly call its resized method (unlike things like repainting, where multiple calls to repaint are coalesced together).

If the component is a top-level window on the desktop, its size could also be changed by operating-system factors beyond the application's control.

See also:
moved, setSize

Reimplemented from Component.

bool TreeView::keyPressed ( const KeyPress key ) [virtual]

Called when a key is pressed.

When a key is pressed, the component that has the keyboard focus will have this method called. Remember that a component will only be given the focus if its setWantsKeyboardFocus() method has been used to enable this.

If your implementation returns true, the event will be consumed and not passed on to any other listeners. If it returns false, the key will be passed to any KeyListeners that have been registered with this component. As soon as one of these returns true, the process will stop, but if they all return false, the event will be passed upwards to this component's parent, and so on.

The default implementation of this method does nothing and returns false.

See also:
keyStateChanged, getCurrentlyFocusedComponent, addKeyListener

Reimplemented from Component.

void TreeView::colourChanged (  ) [virtual]

This method is called when a colour is changed by the setColour() method.

See also:
setColour, findColour

Reimplemented from Component.

void TreeView::enablementChanged (  ) [virtual]

Callback to indicate that this component has been enabled or disabled.

This can be triggered by one of the component's parent components being enabled or disabled, as well as changes to the component itself.

The default implementation of this method does nothing; your class may wish to repaint itself or something when this happens.

See also:
setEnabled, isEnabled

Reimplemented from Component.

bool TreeView::isInterestedInFileDrag ( const StringArray files ) [virtual]

Callback to check whether this target is interested in the set of files being offered.

Note that this will be called repeatedly when the user is dragging the mouse around over your component, so don't do anything time-consuming in here, like opening the files to have a look inside them!

Parameters:
filesthe set of (absolute) pathnames of the files that the user is dragging
Returns:
true if this component wants to receive the other callbacks regarging this type of object; if it returns false, no other callbacks will be made.

Implements FileDragAndDropTarget.

void TreeView::fileDragEnter ( const StringArray files,
int  x,
int  y 
) [virtual]

Callback to indicate that some files are being dragged over this component.

This gets called when the user moves the mouse into this component while dragging.

Use this callback as a trigger to make your component repaint itself to give the user feedback about whether the files can be dropped here or not.

Parameters:
filesthe set of (absolute) pathnames of the files that the user is dragging
xthe mouse x position, relative to this component
ythe mouse y position, relative to this component

Reimplemented from FileDragAndDropTarget.

void TreeView::fileDragMove ( const StringArray files,
int  x,
int  y 
) [virtual]

Callback to indicate that the user is dragging some files over this component.

This gets called when the user moves the mouse over this component while dragging. Normally overriding itemDragEnter() and itemDragExit() are enough, but this lets you know what happens in-between.

Parameters:
filesthe set of (absolute) pathnames of the files that the user is dragging
xthe mouse x position, relative to this component
ythe mouse y position, relative to this component

Reimplemented from FileDragAndDropTarget.

void TreeView::fileDragExit ( const StringArray files ) [virtual]

Callback to indicate that the mouse has moved away from this component.

This gets called when the user moves the mouse out of this component while dragging the files.

If you've used fileDragEnter() to repaint your component and give feedback, use this as a signal to repaint it in its normal state.

Parameters:
filesthe set of (absolute) pathnames of the files that the user is dragging

Reimplemented from FileDragAndDropTarget.

void TreeView::filesDropped ( const StringArray files,
int  x,
int  y 
) [virtual]

Callback to indicate that the user has dropped the files onto this component.

When the user drops the files, this get called, and you can use the files in whatever way is appropriate.

Note that after this is called, the fileDragExit method may not be called, so you should clean up in here if there's anything you need to do when the drag finishes.

Parameters:
filesthe set of (absolute) pathnames of the files that the user is dragging
xthe mouse x position, relative to this component
ythe mouse y position, relative to this component

Implements FileDragAndDropTarget.

bool TreeView::isInterestedInDragSource ( const SourceDetails dragSourceDetails ) [virtual]

Callback to check whether this target is interested in the type of object being dragged.

Parameters:
dragSourceDetailscontains information about the source of the drag operation.
Returns:
true if this component wants to receive the other callbacks regarging this type of object; if it returns false, no other callbacks will be made.

Implements DragAndDropTarget.

void TreeView::itemDragEnter ( const SourceDetails dragSourceDetails ) [virtual]

Callback to indicate that something is being dragged over this component.

This gets called when the user moves the mouse into this component while dragging something.

Use this callback as a trigger to make your component repaint itself to give the user feedback about whether the item can be dropped here or not.

Parameters:
dragSourceDetailscontains information about the source of the drag operation.
See also:
itemDragExit

Reimplemented from DragAndDropTarget.

void TreeView::itemDragMove ( const SourceDetails dragSourceDetails ) [virtual]

Callback to indicate that the user is dragging something over this component.

This gets called when the user moves the mouse over this component while dragging something. Normally overriding itemDragEnter() and itemDragExit() are enough, but this lets you know what happens in-between.

Parameters:
dragSourceDetailscontains information about the source of the drag operation.

Reimplemented from DragAndDropTarget.

void TreeView::itemDragExit ( const SourceDetails dragSourceDetails ) [virtual]

Callback to indicate that something has been dragged off the edge of this component.

This gets called when the user moves the mouse out of this component while dragging something.

If you've used itemDragEnter() to repaint your component and give feedback, use this as a signal to repaint it in its normal state.

Parameters:
dragSourceDetailscontains information about the source of the drag operation.
See also:
itemDragEnter

Reimplemented from DragAndDropTarget.

void TreeView::itemDropped ( const SourceDetails dragSourceDetails ) [virtual]

Callback to indicate that the user has dropped something onto this component.

When the user drops an item this get called, and you can use the description to work out whether your object wants to deal with it or not.

Note that after this is called, the itemDragExit method may not be called, so you should clean up in here if there's anything you need to do when the drag finishes.

Parameters:
dragSourceDetailscontains information about the source of the drag operation.

Implements DragAndDropTarget.


The documentation for this class was generated from the following file:
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines