Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

892 lines
39KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #ifndef JUCE_TREEVIEW_H_INCLUDED
  18. #define JUCE_TREEVIEW_H_INCLUDED
  19. class TreeView;
  20. //==============================================================================
  21. /**
  22. An item in a treeview.
  23. A TreeViewItem can either be a leaf-node in the tree, or it can contain its
  24. own sub-items.
  25. To implement an item that contains sub-items, override the itemOpennessChanged()
  26. method so that when it is opened, it adds the new sub-items to itself using the
  27. addSubItem method. Depending on the nature of the item it might choose to only
  28. do this the first time it's opened, or it might want to refresh itself each time.
  29. It also has the option of deleting its sub-items when it is closed, or leaving them
  30. in place.
  31. */
  32. class JUCE_API TreeViewItem
  33. {
  34. public:
  35. //==============================================================================
  36. /** Constructor. */
  37. TreeViewItem();
  38. /** Destructor. */
  39. virtual ~TreeViewItem();
  40. //==============================================================================
  41. /** Returns the number of sub-items that have been added to this item.
  42. Note that this doesn't mean much if the node isn't open.
  43. @see getSubItem, mightContainSubItems, addSubItem
  44. */
  45. int getNumSubItems() const noexcept;
  46. /** Returns one of the item's sub-items.
  47. Remember that the object returned might get deleted at any time when its parent
  48. item is closed or refreshed, depending on the nature of the items you're using.
  49. @see getNumSubItems
  50. */
  51. TreeViewItem* getSubItem (int index) const noexcept;
  52. /** Removes any sub-items. */
  53. void clearSubItems();
  54. /** Adds a sub-item.
  55. @param newItem the object to add to the item's sub-item list. Once added, these can be
  56. found using getSubItem(). When the items are later removed with
  57. removeSubItem() (or when this item is deleted), they will be deleted.
  58. @param insertPosition the index which the new item should have when it's added. If this
  59. value is less than 0, the item will be added to the end of the list.
  60. */
  61. void addSubItem (TreeViewItem* newItem, int insertPosition = -1);
  62. /** Adds a sub-item with a sort-comparator, assuming that the existing items are already sorted.
  63. @param comparator the comparator object for sorting - see sortSubItems() for details about
  64. the methods this class must provide.
  65. @param newItem the object to add to the item's sub-item list. Once added, these can be
  66. found using getSubItem(). When the items are later removed with
  67. removeSubItem() (or when this item is deleted), they will be deleted.
  68. */
  69. template <class ElementComparator>
  70. void addSubItemSorted (ElementComparator& comparator, TreeViewItem* newItem)
  71. {
  72. addSubItem (newItem, findInsertIndexInSortedArray (comparator, subItems.begin(), newItem, 0, subItems.size()));
  73. }
  74. /** Removes one of the sub-items.
  75. @param index the item to remove
  76. @param deleteItem if true, the item that is removed will also be deleted.
  77. */
  78. void removeSubItem (int index, bool deleteItem = true);
  79. /** Sorts the list of sub-items using a standard array comparator.
  80. This will use a comparator object to sort the elements into order. The comparator
  81. object must have a method of the form:
  82. @code
  83. int compareElements (TreeViewItem* first, TreeViewItem* second);
  84. @endcode
  85. ..and this method must return:
  86. - a value of < 0 if the first comes before the second
  87. - a value of 0 if the two objects are equivalent
  88. - a value of > 0 if the second comes before the first
  89. To improve performance, the compareElements() method can be declared as static or const.
  90. */
  91. template <class ElementComparator>
  92. void sortSubItems (ElementComparator& comparator)
  93. {
  94. subItems.sort (comparator);
  95. }
  96. //==============================================================================
  97. /** Returns the TreeView to which this item belongs. */
  98. TreeView* getOwnerView() const noexcept { return ownerView; }
  99. /** Returns the item within which this item is contained. */
  100. TreeViewItem* getParentItem() const noexcept { return parentItem; }
  101. //==============================================================================
  102. /** True if this item is currently open in the treeview. */
  103. bool isOpen() const noexcept;
  104. /** Opens or closes the item.
  105. When opened or closed, the item's itemOpennessChanged() method will be called,
  106. and a subclass should use this callback to create and add any sub-items that
  107. it needs to.
  108. @see itemOpennessChanged, mightContainSubItems
  109. */
  110. void setOpen (bool shouldBeOpen);
  111. /** True if this item is currently selected.
  112. Use this when painting the node, to decide whether to draw it as selected or not.
  113. */
  114. bool isSelected() const noexcept;
  115. /** Selects or deselects the item.
  116. If shouldNotify == sendNotification, then a callback will be made
  117. to itemSelectionChanged()
  118. */
  119. void setSelected (bool shouldBeSelected,
  120. bool deselectOtherItemsFirst,
  121. NotificationType shouldNotify = sendNotification);
  122. /** Returns the rectangle that this item occupies.
  123. If relativeToTreeViewTopLeft is true, the coordinates are relative to the
  124. top-left of the TreeView comp, so this will depend on the scroll-position of
  125. the tree. If false, it is relative to the top-left of the topmost item in the
  126. tree (so this would be unaffected by scrolling the view).
  127. */
  128. Rectangle<int> getItemPosition (bool relativeToTreeViewTopLeft) const noexcept;
  129. /** Sends a signal to the treeview to make it refresh itself.
  130. Call this if your items have changed and you want the tree to update to reflect this.
  131. */
  132. void treeHasChanged() const noexcept;
  133. /** Sends a repaint message to redraw just this item.
  134. Note that you should only call this if you want to repaint a superficial change. If
  135. you're altering the tree's nodes, you should instead call treeHasChanged().
  136. */
  137. void repaintItem() const;
  138. /** Returns the row number of this item in the tree.
  139. The row number of an item will change according to which items are open.
  140. @see TreeView::getNumRowsInTree(), TreeView::getItemOnRow()
  141. */
  142. int getRowNumberInTree() const noexcept;
  143. /** Returns true if all the item's parent nodes are open.
  144. This is useful to check whether the item might actually be visible or not.
  145. */
  146. bool areAllParentsOpen() const noexcept;
  147. /** Changes whether lines are drawn to connect any sub-items to this item.
  148. By default, line-drawing is turned on according to LookAndFeel::areLinesDrawnForTreeView().
  149. */
  150. void setLinesDrawnForSubItems (bool shouldDrawLines) noexcept;
  151. //==============================================================================
  152. /** Tells the tree whether this item can potentially be opened.
  153. If your item could contain sub-items, this should return true; if it returns
  154. false then the tree will not try to open the item. This determines whether or
  155. not the item will be drawn with a 'plus' button next to it.
  156. */
  157. virtual bool mightContainSubItems() = 0;
  158. /** Returns a string to uniquely identify this item.
  159. If you're planning on using the TreeView::getOpennessState() method, then
  160. these strings will be used to identify which nodes are open. The string
  161. should be unique amongst the item's sibling items, but it's ok for there
  162. to be duplicates at other levels of the tree.
  163. If you're not going to store the state, then it's ok not to bother implementing
  164. this method.
  165. */
  166. virtual String getUniqueName() const;
  167. /** Called when an item is opened or closed.
  168. When setOpen() is called and the item has specified that it might
  169. have sub-items with the mightContainSubItems() method, this method
  170. is called to let the item create or manage its sub-items.
  171. So when this is called with isNowOpen set to true (i.e. when the item is being
  172. opened), a subclass might choose to use clearSubItems() and addSubItem() to
  173. refresh its sub-item list.
  174. When this is called with isNowOpen set to false, the subclass might want
  175. to use clearSubItems() to save on space, or it might choose to leave them,
  176. depending on the nature of the tree.
  177. You could also use this callback as a trigger to start a background process
  178. which asynchronously creates sub-items and adds them, if that's more
  179. appropriate for the task in hand.
  180. @see mightContainSubItems
  181. */
  182. virtual void itemOpennessChanged (bool isNowOpen);
  183. /** Must return the width required by this item.
  184. If your item needs to have a particular width in pixels, return that value; if
  185. you'd rather have it just fill whatever space is available in the treeview,
  186. return -1.
  187. If all your items return -1, no horizontal scrollbar will be shown, but if any
  188. items have fixed widths and extend beyond the width of the treeview, a
  189. scrollbar will appear.
  190. Each item can be a different width, but if they change width, you should call
  191. treeHasChanged() to update the tree.
  192. */
  193. virtual int getItemWidth() const { return -1; }
  194. /** Must return the height required by this item.
  195. This is the height in pixels that the item will take up. Items in the tree
  196. can be different heights, but if they change height, you should call
  197. treeHasChanged() to update the tree.
  198. */
  199. virtual int getItemHeight() const { return 20; }
  200. /** You can override this method to return false if you don't want to allow the
  201. user to select this item.
  202. */
  203. virtual bool canBeSelected() const { return true; }
  204. /** Creates a component that will be used to represent this item.
  205. You don't have to implement this method - if it returns nullptr then no component
  206. will be used for the item, and you can just draw it using the paintItem()
  207. callback. But if you do return a component, it will be positioned in the
  208. treeview so that it can be used to represent this item.
  209. The component returned will be managed by the treeview, so always return
  210. a new component, and don't keep a reference to it, as the treeview will
  211. delete it later when it goes off the screen or is no longer needed. Also
  212. bear in mind that if the component keeps a reference to the item that
  213. created it, that item could be deleted before the component. Its position
  214. and size will be completely managed by the tree, so don't attempt to move it
  215. around.
  216. Something you may want to do with your component is to give it a pointer to
  217. the TreeView that created it. This is perfectly safe, and there's no danger
  218. of it becoming a dangling pointer because the TreeView will always delete
  219. the component before it is itself deleted.
  220. As long as you stick to these rules you can return whatever kind of
  221. component you like. It's most useful if you're doing things like drag-and-drop
  222. of items, or want to use a Label component to edit item names, etc.
  223. */
  224. virtual Component* createItemComponent() { return nullptr; }
  225. //==============================================================================
  226. /** Draws the item's contents.
  227. You can choose to either implement this method and draw each item, or you
  228. can use createItemComponent() to create a component that will represent the
  229. item.
  230. If all you need in your tree is to be able to draw the items and detect when
  231. the user selects or double-clicks one of them, it's probably enough to
  232. use paintItem(), itemClicked() and itemDoubleClicked(). If you need more
  233. complicated interactions, you may need to use createItemComponent() instead.
  234. @param g the graphics context to draw into
  235. @param width the width of the area available for drawing
  236. @param height the height of the area available for drawing
  237. */
  238. virtual void paintItem (Graphics& g, int width, int height);
  239. /** Draws the item's open/close button.
  240. If you don't implement this method, the default behaviour is to call
  241. LookAndFeel::drawTreeviewPlusMinusBox(), but you can override it for custom
  242. effects. You may want to override it and call the base-class implementation
  243. with a different backgroundColour parameter, if your implementation has a
  244. background colour other than the default (white).
  245. */
  246. virtual void paintOpenCloseButton (Graphics&, const Rectangle<float>& area,
  247. Colour backgroundColour, bool isMouseOver);
  248. /** Draws the line that connects this item to the vertical line extending below its parent. */
  249. virtual void paintHorizontalConnectingLine (Graphics&, const Line<float>& line);
  250. /** Draws the line that extends vertically up towards one of its parents, or down to one of its children. */
  251. virtual void paintVerticalConnectingLine (Graphics&, const Line<float>& line);
  252. /** Called when the user clicks on this item.
  253. If you're using createItemComponent() to create a custom component for the
  254. item, the mouse-clicks might not make it through to the treeview, but this
  255. is how you find out about clicks when just drawing each item individually.
  256. The associated mouse-event details are passed in, so you can find out about
  257. which button, where it was, etc.
  258. @see itemDoubleClicked
  259. */
  260. virtual void itemClicked (const MouseEvent& e);
  261. /** Called when the user double-clicks on this item.
  262. If you're using createItemComponent() to create a custom component for the
  263. item, the mouse-clicks might not make it through to the treeview, but this
  264. is how you find out about clicks when just drawing each item individually.
  265. The associated mouse-event details are passed in, so you can find out about
  266. which button, where it was, etc.
  267. If not overridden, the base class method here will open or close the item as
  268. if the 'plus' button had been clicked.
  269. @see itemClicked
  270. */
  271. virtual void itemDoubleClicked (const MouseEvent& e);
  272. /** Called when the item is selected or deselected.
  273. Use this if you want to do something special when the item's selectedness
  274. changes. By default it'll get repainted when this happens.
  275. */
  276. virtual void itemSelectionChanged (bool isNowSelected);
  277. /** The item can return a tool tip string here if it wants to.
  278. @see TooltipClient
  279. */
  280. virtual String getTooltip();
  281. //==============================================================================
  282. /** To allow items from your treeview to be dragged-and-dropped, implement this method.
  283. If this returns a non-null variant then when the user drags an item, the treeview will
  284. try to find a DragAndDropContainer in its parent hierarchy, and will use it to trigger
  285. a drag-and-drop operation, using this string as the source description, with the treeview
  286. itself as the source component.
  287. If you need more complex drag-and-drop behaviour, you can use custom components for
  288. the items, and use those to trigger the drag.
  289. To accept drag-and-drop in your tree, see isInterestedInDragSource(),
  290. isInterestedInFileDrag(), etc.
  291. @see DragAndDropContainer::startDragging
  292. */
  293. virtual var getDragSourceDescription();
  294. /** If you want your item to be able to have files drag-and-dropped onto it, implement this
  295. method and return true.
  296. If you return true and allow some files to be dropped, you'll also need to implement the
  297. filesDropped() method to do something with them.
  298. Note that this will be called often, so make your implementation very quick! There's
  299. certainly no time to try opening the files and having a think about what's inside them!
  300. For responding to internal drag-and-drop of other types of object, see isInterestedInDragSource().
  301. @see FileDragAndDropTarget::isInterestedInFileDrag, isInterestedInDragSource
  302. */
  303. virtual bool isInterestedInFileDrag (const StringArray& files);
  304. /** When files are dropped into this item, this callback is invoked.
  305. For this to work, you'll need to have also implemented isInterestedInFileDrag().
  306. The insertIndex value indicates where in the list of sub-items the files were dropped.
  307. If files are dropped onto an area of the tree where there are no visible items, this
  308. method is called on the root item of the tree, with an insert index of 0.
  309. @see FileDragAndDropTarget::filesDropped, isInterestedInFileDrag
  310. */
  311. virtual void filesDropped (const StringArray& files, int insertIndex);
  312. /** If you want your item to act as a DragAndDropTarget, implement this method and return true.
  313. If you implement this method, you'll also need to implement itemDropped() in order to handle
  314. the items when they are dropped.
  315. To respond to drag-and-drop of files from external applications, see isInterestedInFileDrag().
  316. @see DragAndDropTarget::isInterestedInDragSource, itemDropped
  317. */
  318. virtual bool isInterestedInDragSource (const DragAndDropTarget::SourceDetails& dragSourceDetails);
  319. /** When a things are dropped into this item, this callback is invoked.
  320. For this to work, you need to have also implemented isInterestedInDragSource().
  321. The insertIndex value indicates where in the list of sub-items the new items should be placed.
  322. If files are dropped onto an area of the tree where there are no visible items, this
  323. method is called on the root item of the tree, with an insert index of 0.
  324. @see isInterestedInDragSource, DragAndDropTarget::itemDropped
  325. */
  326. virtual void itemDropped (const DragAndDropTarget::SourceDetails& dragSourceDetails, int insertIndex);
  327. //==============================================================================
  328. /** Sets a flag to indicate that the item wants to be allowed
  329. to draw all the way across to the left edge of the treeview.
  330. By default this is false, which means that when the paintItem()
  331. method is called, its graphics context is clipped to only allow
  332. drawing within the item's rectangle. If this flag is set to true,
  333. then the graphics context isn't clipped on its left side, so it
  334. can draw all the way across to the left margin. Note that the
  335. context will still have its origin in the same place though, so
  336. the coordinates of anything to its left will be negative. It's
  337. mostly useful if you want to draw a wider bar behind the
  338. highlighted item.
  339. */
  340. void setDrawsInLeftMargin (bool canDrawInLeftMargin) noexcept;
  341. //==============================================================================
  342. /** Saves the current state of open/closed nodes so it can be restored later.
  343. This takes a snapshot of which sub-nodes have been explicitly opened or closed,
  344. and records it as XML. To identify node objects it uses the
  345. TreeViewItem::getUniqueName() method to create named paths. This
  346. means that the same state of open/closed nodes can be restored to a
  347. completely different instance of the tree, as long as it contains nodes
  348. whose unique names are the same.
  349. You'd normally want to use TreeView::getOpennessState() rather than call it
  350. for a specific item, but this can be handy if you need to briefly save the state
  351. for a section of the tree.
  352. The caller is responsible for deleting the object that is returned.
  353. Note that if all nodes of the tree are in their default state, then this may
  354. return a nullptr.
  355. @see TreeView::getOpennessState, restoreOpennessState
  356. */
  357. XmlElement* getOpennessState() const;
  358. /** Restores the openness of this item and all its sub-items from a saved state.
  359. See TreeView::restoreOpennessState for more details.
  360. You'd normally want to use TreeView::restoreOpennessState() rather than call it
  361. for a specific item, but this can be handy if you need to briefly save the state
  362. for a section of the tree.
  363. @see TreeView::restoreOpennessState, getOpennessState
  364. */
  365. void restoreOpennessState (const XmlElement& xml);
  366. //==============================================================================
  367. /** Returns the index of this item in its parent's sub-items. */
  368. int getIndexInParent() const noexcept;
  369. /** Returns true if this item is the last of its parent's sub-itens. */
  370. bool isLastOfSiblings() const noexcept;
  371. /** Creates a string that can be used to uniquely retrieve this item in the tree.
  372. The string that is returned can be passed to TreeView::findItemFromIdentifierString().
  373. The string takes the form of a path, constructed from the getUniqueName() of this
  374. item and all its parents, so these must all be correctly implemented for it to work.
  375. @see TreeView::findItemFromIdentifierString, getUniqueName
  376. */
  377. String getItemIdentifierString() const;
  378. //==============================================================================
  379. /**
  380. This handy class takes a copy of a TreeViewItem's openness when you create it,
  381. and restores that openness state when its destructor is called.
  382. This can very handy when you're refreshing sub-items - e.g.
  383. @code
  384. void MyTreeViewItem::updateChildItems()
  385. {
  386. OpennessRestorer openness (*this); // saves the openness state here..
  387. clearSubItems();
  388. // add a bunch of sub-items here which may or may not be the same as the ones that
  389. // were previously there
  390. addSubItem (...
  391. // ..and at this point, the old openness is restored, so any items that haven't
  392. // changed will have their old openness retained.
  393. }
  394. @endcode
  395. */
  396. class OpennessRestorer
  397. {
  398. public:
  399. OpennessRestorer (TreeViewItem& treeViewItem);
  400. ~OpennessRestorer();
  401. private:
  402. TreeViewItem& treeViewItem;
  403. ScopedPointer <XmlElement> oldOpenness;
  404. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpennessRestorer)
  405. };
  406. private:
  407. //==============================================================================
  408. TreeView* ownerView;
  409. TreeViewItem* parentItem;
  410. OwnedArray<TreeViewItem> subItems;
  411. int y, itemHeight, totalHeight, itemWidth, totalWidth;
  412. int uid;
  413. bool selected : 1;
  414. bool redrawNeeded : 1;
  415. bool drawLinesInside : 1;
  416. bool drawLinesSet : 1;
  417. bool drawsInLeftMargin : 1;
  418. unsigned int openness : 2;
  419. friend class TreeView;
  420. void updatePositions (int newY);
  421. int getIndentX() const noexcept;
  422. void setOwnerView (TreeView*) noexcept;
  423. void paintRecursively (Graphics&, int width);
  424. TreeViewItem* getTopLevelItem() noexcept;
  425. TreeViewItem* findItemRecursively (int y) noexcept;
  426. TreeViewItem* getDeepestOpenParentItem() noexcept;
  427. int getNumRows() const noexcept;
  428. TreeViewItem* getItemOnRow (int index) noexcept;
  429. void deselectAllRecursively (TreeViewItem* itemToIgnore);
  430. int countSelectedItemsRecursively (int depth) const noexcept;
  431. TreeViewItem* getSelectedItemWithIndex (int index) noexcept;
  432. TreeViewItem* getNextVisibleItem (bool recurse) const noexcept;
  433. TreeViewItem* findItemFromIdentifierString (const String&);
  434. void restoreToDefaultOpenness();
  435. bool isFullyOpen() const noexcept;
  436. XmlElement* getOpennessState (bool canReturnNull) const;
  437. bool removeSubItemFromList (int index, bool deleteItem);
  438. void removeAllSubItemsFromList();
  439. bool areLinesDrawn() const;
  440. #if JUCE_CATCH_DEPRECATED_CODE_MISUSE
  441. // The parameters for these methods have changed - please update your code!
  442. virtual void isInterestedInDragSource (const String&, Component*) {}
  443. virtual int itemDropped (const String&, Component*, int) { return 0; }
  444. #endif
  445. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TreeViewItem)
  446. };
  447. //==============================================================================
  448. /**
  449. A tree-view component.
  450. Use one of these to hold and display a structure of TreeViewItem objects.
  451. */
  452. class JUCE_API TreeView : public Component,
  453. public SettableTooltipClient,
  454. public FileDragAndDropTarget,
  455. public DragAndDropTarget
  456. {
  457. public:
  458. //==============================================================================
  459. /** Creates an empty treeview.
  460. Once you've got a treeview component, you'll need to give it something to
  461. display, using the setRootItem() method.
  462. */
  463. TreeView (const String& componentName = String::empty);
  464. /** Destructor. */
  465. ~TreeView();
  466. //==============================================================================
  467. /** Sets the item that is displayed in the treeview.
  468. A tree has a single root item which contains as many sub-items as it needs. If
  469. you want the tree to contain a number of root items, you should still use a single
  470. root item above these, but hide it using setRootItemVisible().
  471. You can pass nullptr to this method to clear the tree and remove its current root item.
  472. The object passed in will not be deleted by the treeview, it's up to the caller
  473. to delete it when no longer needed. BUT make absolutely sure that you don't delete
  474. this item until you've removed it from the tree, either by calling setRootItem (nullptr),
  475. or by deleting the tree first. You can also use deleteRootItem() as a quick way
  476. to delete it.
  477. */
  478. void setRootItem (TreeViewItem* newRootItem);
  479. /** Returns the tree's root item.
  480. This will be the last object passed to setRootItem(), or nullptr if none has been set.
  481. */
  482. TreeViewItem* getRootItem() const noexcept { return rootItem; }
  483. /** This will remove and delete the current root item.
  484. It's a convenient way of deleting the item and calling setRootItem (nullptr).
  485. */
  486. void deleteRootItem();
  487. /** Changes whether the tree's root item is shown or not.
  488. If the root item is hidden, only its sub-items will be shown in the treeview - this
  489. lets you make the tree look as if it's got many root items. If it's hidden, this call
  490. will also make sure the root item is open (otherwise the treeview would look empty).
  491. */
  492. void setRootItemVisible (bool shouldBeVisible);
  493. /** Returns true if the root item is visible.
  494. @see setRootItemVisible
  495. */
  496. bool isRootItemVisible() const noexcept { return rootItemVisible; }
  497. /** Sets whether items are open or closed by default.
  498. Normally, items are closed until the user opens them, but you can use this
  499. to make them default to being open until explicitly closed.
  500. @see areItemsOpenByDefault
  501. */
  502. void setDefaultOpenness (bool isOpenByDefault);
  503. /** Returns true if the tree's items default to being open.
  504. @see setDefaultOpenness
  505. */
  506. bool areItemsOpenByDefault() const noexcept { return defaultOpenness; }
  507. /** This sets a flag to indicate that the tree can be used for multi-selection.
  508. You can always select multiple items internally by calling the
  509. TreeViewItem::setSelected() method, but this flag indicates whether the user
  510. is allowed to multi-select by clicking on the tree.
  511. By default it is disabled.
  512. @see isMultiSelectEnabled
  513. */
  514. void setMultiSelectEnabled (bool canMultiSelect);
  515. /** Returns whether multi-select has been enabled for the tree.
  516. @see setMultiSelectEnabled
  517. */
  518. bool isMultiSelectEnabled() const noexcept { return multiSelectEnabled; }
  519. /** Sets a flag to indicate whether to hide the open/close buttons.
  520. @see areOpenCloseButtonsVisible
  521. */
  522. void setOpenCloseButtonsVisible (bool shouldBeVisible);
  523. /** Returns whether open/close buttons are shown.
  524. @see setOpenCloseButtonsVisible
  525. */
  526. bool areOpenCloseButtonsVisible() const noexcept { return openCloseButtonsVisible; }
  527. //==============================================================================
  528. /** Deselects any items that are currently selected. */
  529. void clearSelectedItems();
  530. /** Returns the number of items that are currently selected.
  531. If maximumDepthToSearchTo is >= 0, it lets you specify a maximum depth to which the
  532. tree will be recursed.
  533. @see getSelectedItem, clearSelectedItems
  534. */
  535. int getNumSelectedItems (int maximumDepthToSearchTo = -1) const noexcept;
  536. /** Returns one of the selected items in the tree.
  537. @param index the index, 0 to (getNumSelectedItems() - 1)
  538. */
  539. TreeViewItem* getSelectedItem (int index) const noexcept;
  540. /** Moves the selected row up or down by the specified number of rows. */
  541. void moveSelectedRow (int deltaRows);
  542. //==============================================================================
  543. /** Returns the number of rows the tree is using.
  544. This will depend on which items are open.
  545. @see TreeViewItem::getRowNumberInTree()
  546. */
  547. int getNumRowsInTree() const;
  548. /** Returns the item on a particular row of the tree.
  549. If the index is out of range, this will return 0.
  550. @see getNumRowsInTree, TreeViewItem::getRowNumberInTree()
  551. */
  552. TreeViewItem* getItemOnRow (int index) const;
  553. /** Returns the item that contains a given y position.
  554. The y is relative to the top of the TreeView component.
  555. */
  556. TreeViewItem* getItemAt (int yPosition) const noexcept;
  557. /** Tries to scroll the tree so that this item is on-screen somewhere. */
  558. void scrollToKeepItemVisible (TreeViewItem* item);
  559. /** Returns the treeview's Viewport object. */
  560. Viewport* getViewport() const noexcept;
  561. /** Returns the number of pixels by which each nested level of the tree is indented.
  562. @see setIndentSize
  563. */
  564. int getIndentSize() noexcept;
  565. /** Changes the distance by which each nested level of the tree is indented.
  566. @see getIndentSize
  567. */
  568. void setIndentSize (int newIndentSize);
  569. /** Searches the tree for an item with the specified identifier.
  570. The identifier string must have been created by calling TreeViewItem::getItemIdentifierString().
  571. If no such item exists, this will return false. If the item is found, all of its items
  572. will be automatically opened.
  573. */
  574. TreeViewItem* findItemFromIdentifierString (const String& identifierString) const;
  575. //==============================================================================
  576. /** Saves the current state of open/closed nodes so it can be restored later.
  577. This takes a snapshot of which nodes have been explicitly opened or closed,
  578. and records it as XML. To identify node objects it uses the
  579. TreeViewItem::getUniqueName() method to create named paths. This
  580. means that the same state of open/closed nodes can be restored to a
  581. completely different instance of the tree, as long as it contains nodes
  582. whose unique names are the same.
  583. The caller is responsible for deleting the object that is returned.
  584. @param alsoIncludeScrollPosition if this is true, the state will also
  585. include information about where the
  586. tree has been scrolled to vertically,
  587. so this can also be restored
  588. @see restoreOpennessState
  589. */
  590. XmlElement* getOpennessState (bool alsoIncludeScrollPosition) const;
  591. /** Restores a previously saved arrangement of open/closed nodes.
  592. This will try to restore a snapshot of the tree's state that was created by
  593. the getOpennessState() method. If any of the nodes named in the original
  594. XML aren't present in this tree, they will be ignored.
  595. If restoreStoredSelection is true, it will also try to re-select any items that
  596. were selected in the stored state.
  597. @see getOpennessState
  598. */
  599. void restoreOpennessState (const XmlElement& newState,
  600. bool restoreStoredSelection);
  601. //==============================================================================
  602. /** A set of colour IDs to use to change the colour of various aspects of the treeview.
  603. These constants can be used either via the Component::setColour(), or LookAndFeel::setColour()
  604. methods.
  605. @see Component::setColour, Component::findColour, LookAndFeel::setColour, LookAndFeel::findColour
  606. */
  607. enum ColourIds
  608. {
  609. backgroundColourId = 0x1000500, /**< A background colour to fill the component with. */
  610. linesColourId = 0x1000501, /**< The colour to draw the lines with.*/
  611. dragAndDropIndicatorColourId = 0x1000502, /**< The colour to use for the drag-and-drop target position indicator. */
  612. selectedItemBackgroundColourId = 0x1000503 /**< The colour to use to fill the background of any selected items. */
  613. };
  614. //==============================================================================
  615. /** This abstract base class is implemented by LookAndFeel classes to provide
  616. treeview drawing functionality.
  617. */
  618. struct JUCE_API LookAndFeelMethods
  619. {
  620. virtual ~LookAndFeelMethods() {}
  621. virtual void drawTreeviewPlusMinusBox (Graphics&, const Rectangle<float>& area,
  622. Colour backgroundColour, bool isItemOpen, bool isMouseOver) = 0;
  623. virtual bool areLinesDrawnForTreeView (TreeView&) = 0;
  624. virtual int getTreeViewIndentSize (TreeView&) = 0;
  625. };
  626. //==============================================================================
  627. /** @internal */
  628. void paint (Graphics&) override;
  629. /** @internal */
  630. void resized() override;
  631. /** @internal */
  632. bool keyPressed (const KeyPress&) override;
  633. /** @internal */
  634. void colourChanged() override;
  635. /** @internal */
  636. void enablementChanged() override;
  637. /** @internal */
  638. bool isInterestedInFileDrag (const StringArray& files) override;
  639. /** @internal */
  640. void fileDragEnter (const StringArray& files, int x, int y) override;
  641. /** @internal */
  642. void fileDragMove (const StringArray& files, int x, int y) override;
  643. /** @internal */
  644. void fileDragExit (const StringArray& files) override;
  645. /** @internal */
  646. void filesDropped (const StringArray& files, int x, int y) override;
  647. /** @internal */
  648. bool isInterestedInDragSource (const SourceDetails&) override;
  649. /** @internal */
  650. void itemDragEnter (const SourceDetails&) override;
  651. /** @internal */
  652. void itemDragMove (const SourceDetails&) override;
  653. /** @internal */
  654. void itemDragExit (const SourceDetails&) override;
  655. /** @internal */
  656. void itemDropped (const SourceDetails&) override;
  657. private:
  658. class ContentComponent;
  659. class TreeViewport;
  660. class InsertPointHighlight;
  661. class TargetGroupHighlight;
  662. friend class TreeViewItem;
  663. friend class ContentComponent;
  664. friend struct ContainerDeletePolicy<TreeViewport>;
  665. friend struct ContainerDeletePolicy<InsertPointHighlight>;
  666. friend struct ContainerDeletePolicy<TargetGroupHighlight>;
  667. ScopedPointer<TreeViewport> viewport;
  668. CriticalSection nodeAlterationLock;
  669. TreeViewItem* rootItem;
  670. ScopedPointer<InsertPointHighlight> dragInsertPointHighlight;
  671. ScopedPointer<TargetGroupHighlight> dragTargetGroupHighlight;
  672. int indentSize;
  673. bool defaultOpenness, needsRecalculating, rootItemVisible;
  674. bool multiSelectEnabled, openCloseButtonsVisible;
  675. void itemsChanged() noexcept;
  676. void recalculateIfNeeded();
  677. void updateButtonUnderMouse (const MouseEvent&);
  678. struct InsertPoint;
  679. void showDragHighlight (const InsertPoint&) noexcept;
  680. void hideDragHighlight() noexcept;
  681. void handleDrag (const StringArray&, const SourceDetails&);
  682. void handleDrop (const StringArray&, const SourceDetails&);
  683. bool toggleOpenSelectedItem();
  684. void moveOutOfSelectedItem();
  685. void moveIntoSelectedItem();
  686. void moveByPages (int numPages);
  687. #if JUCE_CATCH_DEPRECATED_CODE_MISUSE
  688. // this method has been deprecated - see the new version..
  689. virtual int paintOpenCloseButton (Graphics&, int, int, bool) { return 0; }
  690. #endif
  691. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TreeView)
  692. };
  693. #endif // JUCE_TREEVIEW_H_INCLUDED