The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

824 lines
35KB

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