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.

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