JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
juce_TreeView.h
Go to the documentation of this file.
1 /*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11 Agreement and JUCE Privacy Policy.
12
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
15
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
18
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21 DISCLAIMED.
22
23 ==============================================================================
24*/
25
26namespace juce
27{
28
29class TreeView;
30
31//==============================================================================
47class JUCE_API TreeViewItem
48{
49public:
50 //==============================================================================
53
55 virtual ~TreeViewItem();
56
57 //==============================================================================
63 int getNumSubItems() const noexcept;
64
71 TreeViewItem* getSubItem (int index) const noexcept;
72
74 void clearSubItems();
75
84 void addSubItem (TreeViewItem* newItem, int insertPosition = -1);
85
94 template <class ElementComparator>
95 void addSubItemSorted (ElementComparator& comparator, TreeViewItem* newItem)
96 {
97 addSubItem (newItem, findInsertIndexInSortedArray (comparator, subItems.begin(), newItem, 0, subItems.size()));
98 }
99
105 void removeSubItem (int index, bool deleteItem = true);
106
122 template <class ElementComparator>
123 void sortSubItems (ElementComparator& comparator)
124 {
125 subItems.sort (comparator);
126 }
127
128 //==============================================================================
130 TreeView* getOwnerView() const noexcept { return ownerView; }
131
133 TreeViewItem* getParentItem() const noexcept { return parentItem; }
134
135 //==============================================================================
140 bool isOpen() const noexcept;
141
155 void setOpen (bool shouldBeOpen);
156
158 enum class Openness
159 {
160 opennessDefault,
161 opennessClosed,
162 opennessOpen
163 };
164
169 Openness getOpenness() const noexcept;
170
179 void setOpenness (Openness newOpenness);
180
185 bool isSelected() const noexcept;
186
192 void setSelected (bool shouldBeSelected,
193 bool deselectOtherItemsFirst,
194 NotificationType shouldNotify = sendNotification);
195
203 Rectangle<int> getItemPosition (bool relativeToTreeViewTopLeft) const noexcept;
204
209 void treeHasChanged() const noexcept;
210
216 void repaintItem() const;
217
224 int getRowNumberInTree() const noexcept;
225
230 bool areAllParentsOpen() const noexcept;
231
236 void setLinesDrawnForSubItems (bool shouldDrawLines) noexcept;
237
238 //==============================================================================
245 virtual bool mightContainSubItems() = 0;
246
257 virtual String getUniqueName() const;
258
279 virtual void itemOpennessChanged (bool isNowOpen);
280
294 virtual int getItemWidth() const { return -1; }
295
302 virtual int getItemHeight() const { return 20; }
303
307 virtual bool canBeSelected() const { return true; }
308
330
331 //==============================================================================
347 virtual void paintItem (Graphics& g, int width, int height);
348
357 virtual void paintOpenCloseButton (Graphics&, const Rectangle<float>& area,
358 Colour backgroundColour, bool isMouseOver);
359
361 virtual void paintHorizontalConnectingLine (Graphics&, const Line<float>& line);
362
364 virtual void paintVerticalConnectingLine (Graphics&, const Line<float>& line);
365
371 virtual bool customComponentUsesTreeViewMouseHandler() const { return false; }
372
384 virtual void itemClicked (const MouseEvent&);
385
400 virtual void itemDoubleClicked (const MouseEvent&);
401
407 virtual void itemSelectionChanged (bool isNowSelected);
408
410 virtual void ownerViewChanged (TreeView* newOwner);
411
416 virtual String getTooltip();
417
427 virtual String getAccessibilityName();
428
429 //==============================================================================
445 virtual var getDragSourceDescription();
446
460 virtual bool isInterestedInFileDrag (const StringArray& files);
461
471 virtual void filesDropped (const StringArray& files, int insertIndex);
472
481 virtual bool isInterestedInDragSource (const DragAndDropTarget::SourceDetails& dragSourceDetails);
482
492 virtual void itemDropped (const DragAndDropTarget::SourceDetails& dragSourceDetails, int insertIndex);
493
494 //==============================================================================
508 void setDrawsInLeftMargin (bool canDrawInLeftMargin) noexcept;
509
521 void setDrawsInRightMargin (bool canDrawInRightMargin) noexcept;
522
523 //==============================================================================
542 std::unique_ptr<XmlElement> getOpennessState() const;
543
554 void restoreOpennessState (const XmlElement& xml);
555
556 //==============================================================================
558 int getIndexInParent() const noexcept;
559
561 bool isLastOfSiblings() const noexcept;
562
571 String getItemIdentifierString() const;
572
573 //==============================================================================
595 class JUCE_API OpennessRestorer
596 {
597 public:
600
601 private:
602 TreeViewItem& treeViewItem;
603 std::unique_ptr<XmlElement> oldOpenness;
604
606 };
607
608private:
609 //==============================================================================
610 friend class TreeView;
611
612 void updatePositions (int);
613 int getIndentX() const noexcept;
614 void setOwnerView (TreeView*) noexcept;
615 TreeViewItem* getTopLevelItem() noexcept;
616 const TreeViewItem* getDeepestOpenParentItem() const noexcept;
617 int getNumRows() const noexcept;
618 TreeViewItem* getItemOnRow (int) noexcept;
619 void deselectAllRecursively (TreeViewItem*);
620 int countSelectedItemsRecursively (int) const noexcept;
621 TreeViewItem* getSelectedItemWithIndex (int) noexcept;
622 TreeViewItem* findItemFromIdentifierString (const String&);
623 void restoreToDefaultOpenness();
624 bool isFullyOpen() const noexcept;
625 std::unique_ptr<XmlElement> getOpennessState (bool) const;
626 bool removeSubItemFromList (int, bool);
627 void removeAllSubItemsFromList();
628 bool areLinesDrawn() const;
629 void draw (Graphics&, int, bool);
630
631 //==============================================================================
632 TreeView* ownerView = nullptr;
633 TreeViewItem* parentItem = nullptr;
634 OwnedArray<TreeViewItem> subItems;
635
636 Openness openness = Openness::opennessDefault;
637 int y = 0, itemHeight = 0, totalHeight = 0, itemWidth = 0, totalWidth = 0, uid = 0;
638 bool selected = false, redrawNeeded = true, drawLinesInside = false, drawLinesSet = false,
639 drawsInLeftMargin = false, drawsInRightMargin = false;
640
641 //==============================================================================
643};
644
645//==============================================================================
653class JUCE_API TreeView : public Component,
656 public DragAndDropTarget
657{
658public:
659 //==============================================================================
665 TreeView (const String& componentName = {});
666
668 ~TreeView() override;
669
670 //==============================================================================
685 void setRootItem (TreeViewItem* newRootItem);
686
691 TreeViewItem* getRootItem() const noexcept { return rootItem; }
692
697 void deleteRootItem();
698
705 void setRootItemVisible (bool shouldBeVisible);
706
711 bool isRootItemVisible() const noexcept { return rootItemVisible; }
712
720 void setDefaultOpenness (bool isOpenByDefault);
721
726 bool areItemsOpenByDefault() const noexcept { return defaultOpenness; }
727
738 void setMultiSelectEnabled (bool canMultiSelect);
739
744 bool isMultiSelectEnabled() const noexcept { return multiSelectEnabled; }
745
750 void setOpenCloseButtonsVisible (bool shouldBeVisible);
751
756 bool areOpenCloseButtonsVisible() const noexcept { return openCloseButtonsVisible; }
757
758 //==============================================================================
760 void clearSelectedItems();
761
769 int getNumSelectedItems (int maximumDepthToSearchTo = -1) const noexcept;
770
775 TreeViewItem* getSelectedItem (int index) const noexcept;
776
778 void moveSelectedRow (int deltaRows);
779
780 //==============================================================================
785 int getNumRowsInTree() const;
786
793 TreeViewItem* getItemOnRow (int index) const;
794
798 TreeViewItem* getItemAt (int yPosition) const noexcept;
799
801 void scrollToKeepItemVisible (const TreeViewItem* item);
802
804 Viewport* getViewport() const noexcept;
805
810 int getIndentSize() noexcept;
811
816 void setIndentSize (int newIndentSize);
817
824 TreeViewItem* findItemFromIdentifierString (const String& identifierString) const;
825
827 Component* getItemComponent (const TreeViewItem* item) const;
828
829 //==============================================================================
845 std::unique_ptr<XmlElement> getOpennessState (bool alsoIncludeScrollPosition) const;
846
858 void restoreOpennessState (const XmlElement& newState, bool restoreStoredSelection);
859
860 //==============================================================================
869 {
870 backgroundColourId = 0x1000500,
871 linesColourId = 0x1000501,
872 dragAndDropIndicatorColourId = 0x1000502,
873 selectedItemBackgroundColourId = 0x1000503,
874 oddItemsColourId = 0x1000504,
875 evenItemsColourId = 0x1000505
876 };
877
878 //==============================================================================
882 struct JUCE_API LookAndFeelMethods
883 {
884 virtual ~LookAndFeelMethods() = default;
885
886 virtual void drawTreeviewPlusMinusBox (Graphics&, const Rectangle<float>& area,
887 Colour backgroundColour, bool isItemOpen, bool isMouseOver) = 0;
888
889 virtual bool areLinesDrawnForTreeView (TreeView&) = 0;
890 virtual int getTreeViewIndentSize (TreeView&) = 0;
891 };
892
893 //==============================================================================
895 void paint (Graphics&) override;
897 void resized() override;
899 bool keyPressed (const KeyPress&) override;
901 void colourChanged() override;
903 void enablementChanged() override;
905 bool isInterestedInFileDrag (const StringArray&) override;
907 void fileDragEnter (const StringArray&, int, int) override;
909 void fileDragMove (const StringArray&, int, int) override;
911 void fileDragExit (const StringArray&) override;
913 void filesDropped (const StringArray&, int, int) override;
915 bool isInterestedInDragSource (const SourceDetails&) override;
917 void itemDragEnter (const SourceDetails&) override;
919 void itemDragMove (const SourceDetails&) override;
921 void itemDragExit (const SourceDetails&) override;
923 void itemDropped (const SourceDetails&) override;
925 std::unique_ptr<AccessibilityHandler> createAccessibilityHandler() override;
926
927private:
928 friend class TreeViewItem;
929
930 class ItemComponent;
931 class ContentComponent;
932 class TreeViewport;
935 class TreeAccessibilityHandler;
936 struct InsertPoint;
937
938 void itemsChanged() noexcept;
939 void updateVisibleItems (std::optional<Point<int>> viewportPosition = {});
940 void updateButtonUnderMouse (const MouseEvent&);
941 void showDragHighlight (const InsertPoint&) noexcept;
942 void hideDragHighlight() noexcept;
943 void handleDrag (const StringArray&, const SourceDetails&);
944 void handleDrop (const StringArray&, const SourceDetails&);
945 bool toggleOpenSelectedItem();
946 void moveOutOfSelectedItem();
947 void moveIntoSelectedItem();
948 void moveByPages (int);
949
950 std::unique_ptr<TreeViewport> viewport;
951 TreeViewItem* rootItem = nullptr;
952 std::unique_ptr<InsertPointHighlight> dragInsertPointHighlight;
953 std::unique_ptr<TargetGroupHighlight> dragTargetGroupHighlight;
954 int indentSize = -1;
955 bool defaultOpenness = false, rootItemVisible = true, multiSelectEnabled = false, openCloseButtonsVisible = true;
956
958};
959
960} // namespace juce
Represents a colour, also including a transparency value.
Definition juce_Colour.h:38
The base class for all JUCE user-interface objects.
Contains details about the source of a drag-and-drop operation.
Components derived from this class can have things dropped onto them by a DragAndDropContainer.
Components derived from this class can have files dropped onto them by an external application.
A graphics context, used for drawing a component or image.
Represents a key press, including any modifier keys that are needed.
Represents a line.
Definition juce_Line.h:47
Contains position and status information about a mouse event.
An array designed for holding objects.
A pair of (x, y) coordinates.
Definition juce_Point.h:42
Manages a rectangle and allows geometric operations to be performed on it.
An implementation of TooltipClient that stores the tooltip string and a method for changing it.
A special array for holding a list of strings.
The JUCE String class!
Definition juce_String.h:53
This handy class takes a copy of a TreeViewItem's openness when you create it, and restores that open...
An item in a TreeView.
virtual bool canBeSelected() const
You can override this method to return false if you don't want to allow the user to select this item.
TreeView * getOwnerView() const noexcept
Returns the TreeView to which this item belongs.
void sortSubItems(ElementComparator &comparator)
Sorts the list of sub-items using a standard array comparator.
void addSubItemSorted(ElementComparator &comparator, TreeViewItem *newItem)
Adds a sub-item with a sort-comparator, assuming that the existing items are already sorted.
virtual std::unique_ptr< Component > createItemComponent()
Creates a component that will be used to represent this item.
virtual int getItemHeight() const
Must return the height required by this item.
virtual bool customComponentUsesTreeViewMouseHandler() const
This should return true if you want to use a custom component, and also use the TreeView's built-in m...
TreeViewItem * getParentItem() const noexcept
Returns the item within which this item is contained.
Openness
An enum of states to describe the explicit or implicit openness of an item.
A tree-view component.
ColourIds
A set of colour IDs to use to change the colour of various aspects of the TreeView.
TreeViewItem * getRootItem() const noexcept
Returns the tree's root item.
bool areOpenCloseButtonsVisible() const noexcept
Returns whether open/close buttons are shown.
bool isMultiSelectEnabled() const noexcept
Returns whether multi-select has been enabled for the tree.
bool areItemsOpenByDefault() const noexcept
Returns true if the tree's items default to being open.
bool isRootItemVisible() const noexcept
Returns true if the root item is visible.
A Viewport is used to contain a larger child component, and allows the child to be automatically scro...
Used to build a tree of elements representing an XML document.
A variant class, that can be used to hold a range of primitive values.
#define JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(className)
This is a shorthand way of writing both a JUCE_DECLARE_NON_COPYABLE and JUCE_LEAK_DETECTOR macro for ...
JUCE Namespace.
NotificationType
These enums are used in various classes to indicate whether a notification event should be sent out.
This abstract base class is implemented by LookAndFeel classes to provide TreeView drawing functional...