|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189 |
- /*
- ==============================================================================
-
- This file is part of the JUCE library.
- Copyright (c) 2022 - Raw Material Software Limited
-
- JUCE is an open source library subject to commercial or open-source
- licensing.
-
- By using JUCE, you agree to the terms of both the JUCE 7 End-User License
- Agreement and JUCE Privacy Policy.
-
- End User License Agreement: www.juce.com/juce-7-licence
- Privacy Policy: www.juce.com/juce-privacy-policy
-
- Or: You may also use this code under the terms of the GPL v3 (see
- www.gnu.org/licenses).
-
- JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
- EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
- DISCLAIMED.
-
- ==============================================================================
- */
-
- namespace juce
- {
-
- static int getItemDepth (const TreeViewItem* item)
- {
- if (item == nullptr || item->getOwnerView() == nullptr)
- return 0;
-
- auto depth = item->getOwnerView()->isRootItemVisible() ? 0 : -1;
-
- for (auto* parent = item->getParentItem(); parent != nullptr; parent = parent->getParentItem())
- ++depth;
-
- return depth;
- }
-
- //==============================================================================
- class TreeView::ItemComponent : public Component,
- public TooltipClient
- {
- public:
- explicit ItemComponent (TreeViewItem& itemToRepresent)
- : item (itemToRepresent),
- customComponent (item.createItemComponent())
- {
- if (hasCustomComponent())
- addAndMakeVisible (*customComponent);
- }
-
- void paint (Graphics& g) override
- {
- item.draw (g, getWidth(), mouseIsOverButton);
- }
-
- void resized() override
- {
- if (hasCustomComponent())
- {
- auto itemPosition = item.getItemPosition (false);
-
- customComponent->setBounds (getLocalBounds().withX (itemPosition.getX())
- .withWidth (itemPosition.getWidth()));
- }
- }
-
- void setMouseIsOverButton (bool isOver)
- {
- mouseIsOverButton = isOver;
- repaint();
- }
-
- TreeViewItem& getRepresentedItem() const noexcept
- {
- return item;
- }
-
- String getTooltip() override
- {
- return item.getTooltip();
- }
-
- private:
- //==============================================================================
- class ItemAccessibilityHandler : public AccessibilityHandler
- {
- public:
- explicit ItemAccessibilityHandler (ItemComponent& comp)
- : AccessibilityHandler (comp,
- AccessibilityRole::treeItem,
- getAccessibilityActions (comp),
- { std::make_unique<ItemCellInterface> (comp) }),
- itemComponent (comp)
- {
- }
-
- String getTitle() const override
- {
- return itemComponent.getRepresentedItem().getAccessibilityName();
- }
-
- String getHelp() const override
- {
- return itemComponent.getRepresentedItem().getTooltip();
- }
-
- AccessibleState getCurrentState() const override
- {
- auto& treeItem = itemComponent.getRepresentedItem();
-
- auto state = AccessibilityHandler::getCurrentState().withAccessibleOffscreen();
-
- if (auto* tree = treeItem.getOwnerView())
- {
- if (tree->isMultiSelectEnabled())
- state = state.withMultiSelectable();
- else
- state = state.withSelectable();
- }
-
- if (treeItem.mightContainSubItems())
- {
- state = state.withExpandable();
-
- if (treeItem.isOpen())
- state = state.withExpanded();
- else
- state = state.withCollapsed();
- }
-
- if (treeItem.isSelected())
- state = state.withSelected();
-
- return state;
- }
-
- class ItemCellInterface : public AccessibilityCellInterface
- {
- public:
- explicit ItemCellInterface (ItemComponent& c) : itemComponent (c) {}
-
- int getColumnIndex() const override { return 0; }
- int getColumnSpan() const override { return 1; }
-
- int getRowIndex() const override
- {
- return itemComponent.getRepresentedItem().getRowNumberInTree();
- }
-
- int getRowSpan() const override
- {
- return 1;
- }
-
- int getDisclosureLevel() const override
- {
- return getItemDepth (&itemComponent.getRepresentedItem());
- }
-
- const AccessibilityHandler* getTableHandler() const override
- {
- if (auto* tree = itemComponent.getRepresentedItem().getOwnerView())
- return tree->getAccessibilityHandler();
-
- return nullptr;
- }
-
- private:
- ItemComponent& itemComponent;
- };
-
- private:
- static AccessibilityActions getAccessibilityActions (ItemComponent& itemComponent)
- {
- auto onFocus = [&itemComponent]
- {
- auto& treeItem = itemComponent.getRepresentedItem();
-
- if (auto* tree = treeItem.getOwnerView())
- tree->scrollToKeepItemVisible (&treeItem);
- };
-
- auto onPress = [&itemComponent]
- {
- itemComponent.getRepresentedItem().itemClicked (generateMouseEvent (itemComponent, { ModifierKeys::leftButtonModifier }));
- };
-
- auto onShowMenu = [&itemComponent]
- {
- itemComponent.getRepresentedItem().itemClicked (generateMouseEvent (itemComponent, { ModifierKeys::popupMenuClickModifier }));
- };
-
- auto onToggle = [&itemComponent, onFocus]
- {
- if (auto* handler = itemComponent.getAccessibilityHandler())
- {
- auto isSelected = handler->getCurrentState().isSelected();
-
- if (! isSelected)
- onFocus();
-
- itemComponent.getRepresentedItem().setSelected (! isSelected, true);
- }
- };
-
- auto actions = AccessibilityActions().addAction (AccessibilityActionType::focus, std::move (onFocus))
- .addAction (AccessibilityActionType::press, std::move (onPress))
- .addAction (AccessibilityActionType::showMenu, std::move (onShowMenu))
- .addAction (AccessibilityActionType::toggle, std::move (onToggle));
-
- return actions;
- }
-
- ItemComponent& itemComponent;
-
- static MouseEvent generateMouseEvent (ItemComponent& itemComp, ModifierKeys mods)
- {
- auto topLeft = itemComp.getRepresentedItem().getItemPosition (false).toFloat().getTopLeft();
-
- return { Desktop::getInstance().getMainMouseSource(), topLeft, mods,
- MouseInputSource::defaultPressure, MouseInputSource::defaultOrientation, MouseInputSource::defaultRotation,
- MouseInputSource::defaultTiltX, MouseInputSource::defaultTiltY,
- &itemComp, &itemComp, Time::getCurrentTime(), topLeft, Time::getCurrentTime(), 0, false };
- }
-
- JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ItemAccessibilityHandler)
- };
-
- std::unique_ptr<AccessibilityHandler> createAccessibilityHandler() override
- {
- if (hasCustomComponent() && customComponent->getAccessibilityHandler() != nullptr)
- return createIgnoredAccessibilityHandler (*this);
-
- return std::make_unique<ItemAccessibilityHandler> (*this);
- }
-
- bool hasCustomComponent() const noexcept { return customComponent.get() != nullptr; }
-
- TreeViewItem& item;
- std::unique_ptr<Component> customComponent;
-
- bool mouseIsOverButton = false;
-
- //==============================================================================
- JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ItemComponent)
- };
-
- //==============================================================================
- class TreeView::ContentComponent : public Component,
- public TooltipClient,
- public AsyncUpdater
- {
- public:
- ContentComponent (TreeView& tree) : owner (tree)
- {
- }
-
- //==============================================================================
- void resized() override
- {
- triggerAsyncUpdate();
- }
-
- String getTooltip() override
- {
- if (auto* itemComponent = getItemComponentAt (getMouseXYRelative()))
- return itemComponent->getRepresentedItem().getTooltip();
-
- return owner.getTooltip();
- }
-
- void mouseDown (const MouseEvent& e) override { mouseDownInternal (e.getEventRelativeTo (this)); }
- void mouseUp (const MouseEvent& e) override { mouseUpInternal (e.getEventRelativeTo (this)); }
- void mouseDoubleClick (const MouseEvent& e) override { mouseDoubleClickInternal (e.getEventRelativeTo (this));}
- void mouseDrag (const MouseEvent& e) override { mouseDragInternal (e.getEventRelativeTo (this));}
- void mouseMove (const MouseEvent& e) override { mouseMoveInternal (e.getEventRelativeTo (this)); }
- void mouseExit (const MouseEvent& e) override { mouseExitInternal (e.getEventRelativeTo (this)); }
-
- //==============================================================================
- ItemComponent* getItemComponentAt (Point<int> p)
- {
- auto iter = std::find_if (itemComponents.cbegin(), itemComponents.cend(),
- [p] (const std::unique_ptr<ItemComponent>& c)
- {
- return c->getBounds().contains (p);
- });
-
- if (iter != itemComponents.cend())
- return iter->get();
-
- return nullptr;
- }
-
- ItemComponent* getComponentForItem (const TreeViewItem* item) const
- {
- const auto iter = std::find_if (itemComponents.begin(), itemComponents.end(),
- [item] (const std::unique_ptr<ItemComponent>& c)
- {
- return &c->getRepresentedItem() == item;
- });
-
- if (iter != itemComponents.end())
- return iter->get();
-
- return nullptr;
- }
-
- void itemBeingDeleted (const TreeViewItem* item)
- {
- const auto iter = std::find_if (itemComponents.begin(), itemComponents.end(),
- [item] (const std::unique_ptr<ItemComponent>& c)
- {
- return &c->getRepresentedItem() == item;
- });
-
- if (iter != itemComponents.end())
- {
- if (itemUnderMouse == iter->get())
- itemUnderMouse = nullptr;
-
- if (isMouseDraggingInChildComp (*(iter->get())))
- owner.hideDragHighlight();
-
- itemComponents.erase (iter);
- }
- }
-
- void updateComponents()
- {
- std::set<ItemComponent*> componentsToKeep;
-
- for (auto* treeItem : getAllVisibleItems())
- {
- if (auto* itemComp = getComponentForItem (treeItem))
- {
- componentsToKeep.insert (itemComp);
- }
- else
- {
- auto newComp = std::make_unique<ItemComponent> (*treeItem);
-
- addAndMakeVisible (*newComp);
- newComp->addMouseListener (this, treeItem->customComponentUsesTreeViewMouseHandler());
- componentsToKeep.insert (newComp.get());
-
- itemComponents.push_back (std::move (newComp));
- }
- }
-
- auto removePredicate = [&] (auto& item)
- {
- if (item == nullptr)
- return true;
-
- return componentsToKeep.find (item.get()) == componentsToKeep.end()
- && ! isMouseDraggingInChildComp (*item);
- };
-
- const auto iter = std::remove_if (itemComponents.begin(), itemComponents.end(), std::move (removePredicate));
- itemComponents.erase (iter, itemComponents.end());
-
- for (auto& comp : itemComponents)
- {
- auto& treeItem = comp->getRepresentedItem();
- comp->setBounds ({ 0, treeItem.y, getWidth(), treeItem.itemHeight });
- }
- }
-
- private:
- //==============================================================================
- struct ScopedDisableViewportScroll
- {
- explicit ScopedDisableViewportScroll (ItemComponent& c)
- : item (&c)
- {
- item->setViewportIgnoreDragFlag (true);
- }
-
- ~ScopedDisableViewportScroll()
- {
- if (item != nullptr)
- item->setViewportIgnoreDragFlag (false);
- }
-
- SafePointer<ItemComponent> item;
-
- JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ScopedDisableViewportScroll)
- };
-
- //==============================================================================
- std::unique_ptr<AccessibilityHandler> createAccessibilityHandler() override
- {
- return createIgnoredAccessibilityHandler (*this);
- }
-
- void mouseDownInternal (const MouseEvent& e)
- {
- updateItemUnderMouse (e);
-
- isDragging = false;
- scopedScrollDisabler = nullptr;
- needSelectionOnMouseUp = false;
-
- if (! isEnabled())
- return;
-
- if (auto* itemComponent = getItemComponentAt (e.getPosition()))
- {
- auto& item = itemComponent->getRepresentedItem();
- auto pos = item.getItemPosition (false);
-
- // (if the open/close buttons are hidden, we'll treat clicks to the left of the item
- // as selection clicks)
- if (e.x < pos.getX() && owner.openCloseButtonsVisible)
- {
- // (clicks to the left of an open/close button are ignored)
- if (e.x >= pos.getX() - owner.getIndentSize())
- item.setOpen (! item.isOpen());
- }
- else
- {
- // mouse-down inside the body of the item..
- if (! owner.isMultiSelectEnabled())
- item.setSelected (true, true);
- else if (item.isSelected())
- needSelectionOnMouseUp = ! e.mods.isPopupMenu();
- else
- selectBasedOnModifiers (item, e.mods);
-
- if (e.x >= pos.getX())
- item.itemClicked (e.withNewPosition (e.position - pos.getPosition().toFloat()));
- }
- }
- }
-
- void mouseUpInternal (const MouseEvent& e)
- {
- updateItemUnderMouse (e);
-
- if (isEnabled() && needSelectionOnMouseUp && e.mouseWasClicked())
- if (auto* itemComponent = getItemComponentAt (e.getPosition()))
- selectBasedOnModifiers (itemComponent->getRepresentedItem(), e.mods);
- }
-
- void mouseDoubleClickInternal (const MouseEvent& e)
- {
- if (isEnabled() && e.getNumberOfClicks() != 3) // ignore triple clicks
- {
- if (auto* itemComponent = getItemComponentAt (e.getPosition()))
- {
- auto& item = itemComponent->getRepresentedItem();
- auto pos = item.getItemPosition (false);
-
- if (e.x >= pos.getX() || ! owner.openCloseButtonsVisible)
- item.itemDoubleClicked (e.withNewPosition (e.position - pos.getPosition().toFloat()));
- }
- }
- }
-
- void mouseDragInternal (const MouseEvent& e)
- {
- if (isEnabled()
- && ! (isDragging || e.mouseWasClicked()
- || e.getDistanceFromDragStart() < 5
- || e.mods.isPopupMenu()))
- {
- isDragging = true;
-
- if (auto* itemComponent = getItemComponentAt (e.getMouseDownPosition()))
- {
- auto& item = itemComponent->getRepresentedItem();
- auto pos = item.getItemPosition (false);
-
- if (e.getMouseDownX() >= pos.getX())
- {
- auto dragDescription = item.getDragSourceDescription();
-
- if (! (dragDescription.isVoid() || (dragDescription.isString() && dragDescription.toString().isEmpty())))
- {
- if (auto* dragContainer = DragAndDropContainer::findParentDragContainerFor (this))
- {
- pos.setSize (pos.getWidth(), item.itemHeight);
-
- const auto additionalScale = 2.0f;
- auto dragImage = Component::createComponentSnapshot (pos,
- true,
- Component::getApproximateScaleFactorForComponent (itemComponent) * additionalScale);
-
- dragImage.multiplyAllAlphas (0.6f);
-
- auto imageOffset = pos.getPosition() - e.getPosition();
- dragContainer->startDragging (dragDescription, &owner, { dragImage, additionalScale }, true, &imageOffset, &e.source);
-
- scopedScrollDisabler = std::make_unique<ScopedDisableViewportScroll> (*itemComponent);
- }
- else
- {
- // to be able to do a drag-and-drop operation, the treeview needs to
- // be inside a component which is also a DragAndDropContainer.
- jassertfalse;
- }
- }
- }
- }
- }
- }
-
- void mouseMoveInternal (const MouseEvent& e) { updateItemUnderMouse (e); }
- void mouseExitInternal (const MouseEvent& e) { updateItemUnderMouse (e); }
-
- static bool isMouseDraggingInChildComp (const Component& comp)
- {
- for (auto& ms : Desktop::getInstance().getMouseSources())
- if (ms.isDragging())
- if (auto* underMouse = ms.getComponentUnderMouse())
- return (&comp == underMouse || comp.isParentOf (underMouse));
-
- return false;
- }
-
- void updateItemUnderMouse (const MouseEvent& e)
- {
- if (! owner.openCloseButtonsVisible)
- return;
-
- auto* newItem = [this, &e]() -> ItemComponent*
- {
- if (auto* itemComponent = getItemComponentAt (e.getPosition()))
- {
- auto& item = itemComponent->getRepresentedItem();
-
- if (item.mightContainSubItems())
- {
- const auto xPos = item.getItemPosition (false).getX();
-
- if (xPos - owner.getIndentSize() <= e.x && e.x < xPos)
- return itemComponent;
- }
- }
-
- return nullptr;
- }();
-
- if (itemUnderMouse != newItem)
- {
- if (itemUnderMouse != nullptr)
- itemUnderMouse->setMouseIsOverButton (false);
-
- if (newItem != nullptr)
- newItem->setMouseIsOverButton (true);
-
- itemUnderMouse = newItem;
- }
- }
-
- void handleAsyncUpdate() override
- {
- owner.updateVisibleItems();
- }
-
- //==============================================================================
- void selectBasedOnModifiers (TreeViewItem& item, const ModifierKeys modifiers)
- {
- TreeViewItem* firstSelected = nullptr;
-
- if (modifiers.isShiftDown() && ((firstSelected = owner.getSelectedItem (0)) != nullptr))
- {
- auto* lastSelected = owner.getSelectedItem (owner.getNumSelectedItems() - 1);
-
- if (lastSelected == nullptr)
- {
- jassertfalse;
- return;
- }
-
- auto rowStart = firstSelected->getRowNumberInTree();
- auto rowEnd = lastSelected->getRowNumberInTree();
-
- if (rowStart > rowEnd)
- std::swap (rowStart, rowEnd);
-
- auto ourRow = item.getRowNumberInTree();
- auto otherEnd = ourRow < rowEnd ? rowStart : rowEnd;
-
- if (ourRow > otherEnd)
- std::swap (ourRow, otherEnd);
-
- for (int i = ourRow; i <= otherEnd; ++i)
- owner.getItemOnRow (i)->setSelected (true, false);
- }
- else
- {
- const auto cmd = modifiers.isCommandDown();
- item.setSelected ((! cmd) || ! item.isSelected(), ! cmd);
- }
- }
-
- static TreeViewItem* getNextVisibleItem (TreeViewItem* item, bool forwards)
- {
- if (item == nullptr || item->ownerView == nullptr)
- return nullptr;
-
- auto* nextItem = item->ownerView->getItemOnRow (item->getRowNumberInTree() + (forwards ? 1 : -1));
-
- return nextItem == item->ownerView->rootItem && ! item->ownerView->rootItemVisible ? nullptr
- : nextItem;
- }
-
- std::vector<TreeViewItem*> getAllVisibleItems() const
- {
- if (owner.rootItem == nullptr)
- return {};
-
- const auto visibleTop = -getY();
- const auto visibleBottom = visibleTop + getParentHeight();
-
- std::vector<TreeViewItem*> visibleItems;
-
- auto* item = [&]
- {
- auto* i = owner.rootItemVisible ? owner.rootItem
- : owner.rootItem->subItems.getFirst();
-
- while (i != nullptr && i->y < visibleTop)
- i = getNextVisibleItem (i, true);
-
- return i;
- }();
-
- auto addOffscreenItemBuffer = [&visibleItems] (TreeViewItem* i, int num, bool forwards)
- {
- while (--num >= 0)
- {
- i = getNextVisibleItem (i, forwards);
-
- if (i == nullptr)
- return;
-
- visibleItems.push_back (i);
- }
- };
-
- addOffscreenItemBuffer (item, 2, false);
-
- while (item != nullptr && item->y < visibleBottom)
- {
- visibleItems.push_back (item);
- item = getNextVisibleItem (item, true);
- }
-
- if (item != nullptr)
- visibleItems.push_back (item);
-
- addOffscreenItemBuffer (item, 2, true);
-
- return visibleItems;
- }
-
- //==============================================================================
- TreeView& owner;
-
- std::vector<std::unique_ptr<ItemComponent>> itemComponents;
- ItemComponent* itemUnderMouse = nullptr;
- std::unique_ptr<ScopedDisableViewportScroll> scopedScrollDisabler;
- bool isDragging = false, needSelectionOnMouseUp = false;
-
- JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ContentComponent)
- };
-
- //==============================================================================
- class TreeView::TreeViewport : public Viewport,
- private AsyncUpdater
- {
- public:
- explicit TreeViewport (TreeView& treeView) : owner (treeView) {}
-
- void visibleAreaChanged (const Rectangle<int>& newVisibleArea) override
- {
- const auto hasScrolledSideways = (newVisibleArea.getX() != lastX);
-
- lastX = newVisibleArea.getX();
- updateComponents (hasScrolledSideways);
-
- structureChanged = true;
- triggerAsyncUpdate();
- }
-
- bool keyPressed (const KeyPress& key) override
- {
- if (auto* tree = getParentComponent())
- if (tree->keyPressed (key))
- return true;
-
- return Viewport::keyPressed (key);
- }
-
- ContentComponent* getContentComp() const noexcept
- {
- return static_cast<ContentComponent*> (getViewedComponent());
- }
-
- enum class Async { yes, no };
-
- void recalculatePositions (Async useAsyncUpdate)
- {
- needsRecalculating = true;
-
- if (useAsyncUpdate == Async::yes)
- triggerAsyncUpdate();
- else
- handleAsyncUpdate();
- }
-
- private:
- std::unique_ptr<AccessibilityHandler> createAccessibilityHandler() override
- {
- return createIgnoredAccessibilityHandler (*this);
- }
-
- void handleAsyncUpdate() override
- {
- if (structureChanged)
- {
- if (auto* handler = owner.getAccessibilityHandler())
- handler->notifyAccessibilityEvent (AccessibilityEvent::structureChanged);
-
- structureChanged = false;
- }
-
- if (needsRecalculating)
- {
- if (auto* root = owner.rootItem)
- {
- const auto startY = owner.rootItemVisible ? 0 : -root->itemHeight;
-
- root->updatePositions (startY);
- getViewedComponent()->setSize (jmax (getMaximumVisibleWidth(), root->totalWidth + 50),
- root->totalHeight + startY);
- }
- else
- {
- getViewedComponent()->setSize (0, 0);
- }
-
- updateComponents (false);
-
- needsRecalculating = false;
- }
- }
-
- void updateComponents (bool triggerResize)
- {
- if (auto* content = getContentComp())
- {
- if (triggerResize)
- content->resized();
- else
- content->updateComponents();
- }
-
- repaint();
- }
-
- TreeView& owner;
- int lastX = -1;
- bool structureChanged = false, needsRecalculating = false;
-
- JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TreeViewport)
- };
-
- //==============================================================================
- TreeView::TreeView (const String& name) : Component (name)
- {
- viewport = std::make_unique<TreeViewport> (*this);
- addAndMakeVisible (viewport.get());
- viewport->setViewedComponent (new ContentComponent (*this));
-
- setWantsKeyboardFocus (true);
- setFocusContainerType (FocusContainerType::focusContainer);
- }
-
- TreeView::~TreeView()
- {
- if (rootItem != nullptr)
- rootItem->setOwnerView (nullptr);
- }
-
- void TreeView::setRootItem (TreeViewItem* const newRootItem)
- {
- if (rootItem != newRootItem)
- {
- if (newRootItem != nullptr)
- {
- // can't use a tree item in more than one tree at once..
- jassert (newRootItem->ownerView == nullptr);
-
- if (newRootItem->ownerView != nullptr)
- newRootItem->ownerView->setRootItem (nullptr);
- }
-
- if (rootItem != nullptr)
- rootItem->setOwnerView (nullptr);
-
- rootItem = newRootItem;
-
- if (newRootItem != nullptr)
- newRootItem->setOwnerView (this);
-
- if (rootItem != nullptr && (defaultOpenness || ! rootItemVisible))
- {
- rootItem->setOpen (false); // force a re-open
- rootItem->setOpen (true);
- }
-
- viewport->recalculatePositions (TreeViewport::Async::no);
- }
- }
-
- void TreeView::deleteRootItem()
- {
- const std::unique_ptr<TreeViewItem> deleter (rootItem);
- setRootItem (nullptr);
- }
-
- void TreeView::setRootItemVisible (const bool shouldBeVisible)
- {
- rootItemVisible = shouldBeVisible;
-
- if (rootItem != nullptr && (defaultOpenness || ! rootItemVisible))
- {
- rootItem->setOpen (false); // force a re-open
- rootItem->setOpen (true);
- }
-
- updateVisibleItems();
- }
-
- void TreeView::colourChanged()
- {
- setOpaque (findColour (backgroundColourId).isOpaque());
- repaint();
- }
-
- void TreeView::setIndentSize (const int newIndentSize)
- {
- if (indentSize != newIndentSize)
- {
- indentSize = newIndentSize;
- resized();
- }
- }
-
- int TreeView::getIndentSize() noexcept
- {
- return indentSize >= 0 ? indentSize
- : getLookAndFeel().getTreeViewIndentSize (*this);
- }
-
- void TreeView::setDefaultOpenness (const bool isOpenByDefault)
- {
- if (defaultOpenness != isOpenByDefault)
- {
- defaultOpenness = isOpenByDefault;
- updateVisibleItems();
- }
- }
-
- void TreeView::setMultiSelectEnabled (const bool canMultiSelect)
- {
- multiSelectEnabled = canMultiSelect;
- }
-
- void TreeView::setOpenCloseButtonsVisible (const bool shouldBeVisible)
- {
- if (openCloseButtonsVisible != shouldBeVisible)
- {
- openCloseButtonsVisible = shouldBeVisible;
- updateVisibleItems();
- }
- }
-
- Viewport* TreeView::getViewport() const noexcept
- {
- return viewport.get();
- }
-
- //==============================================================================
- void TreeView::clearSelectedItems()
- {
- if (rootItem != nullptr)
- rootItem->deselectAllRecursively (nullptr);
- }
-
- int TreeView::getNumSelectedItems (int maximumDepthToSearchTo) const noexcept
- {
- return rootItem != nullptr ? rootItem->countSelectedItemsRecursively (maximumDepthToSearchTo) : 0;
- }
-
- TreeViewItem* TreeView::getSelectedItem (const int index) const noexcept
- {
- return rootItem != nullptr ? rootItem->getSelectedItemWithIndex (index) : nullptr;
- }
-
- int TreeView::getNumRowsInTree() const
- {
- return rootItem != nullptr ? (rootItem->getNumRows() - (rootItemVisible ? 0 : 1)) : 0;
- }
-
- TreeViewItem* TreeView::getItemOnRow (int index) const
- {
- if (! rootItemVisible)
- ++index;
-
- if (rootItem != nullptr && index >= 0)
- return rootItem->getItemOnRow (index);
-
- return nullptr;
- }
-
- TreeViewItem* TreeView::getItemAt (int y) const noexcept
- {
- if (auto* contentComp = viewport->getContentComp())
- if (auto* itemComponent = contentComp->getItemComponentAt (contentComp->getLocalPoint (this, Point<int> (0, y))))
- return &itemComponent->getRepresentedItem();
-
- return nullptr;
- }
-
- TreeViewItem* TreeView::findItemFromIdentifierString (const String& identifierString) const
- {
- if (rootItem == nullptr)
- return nullptr;
-
- return rootItem->findItemFromIdentifierString (identifierString);
- }
-
- Component* TreeView::getItemComponent (const TreeViewItem* item) const
- {
- return viewport->getContentComp()->getComponentForItem (item);
- }
-
- //==============================================================================
- static void addAllSelectedItemIds (TreeViewItem* item, XmlElement& parent)
- {
- if (item->isSelected())
- parent.createNewChildElement ("SELECTED")->setAttribute ("id", item->getItemIdentifierString());
-
- auto numSubItems = item->getNumSubItems();
-
- for (int i = 0; i < numSubItems; ++i)
- addAllSelectedItemIds (item->getSubItem (i), parent);
- }
-
- std::unique_ptr<XmlElement> TreeView::getOpennessState (bool alsoIncludeScrollPosition) const
- {
- if (rootItem != nullptr)
- {
- if (auto rootOpenness = rootItem->getOpennessState (false))
- {
- if (alsoIncludeScrollPosition)
- rootOpenness->setAttribute ("scrollPos", viewport->getViewPositionY());
-
- addAllSelectedItemIds (rootItem, *rootOpenness);
- return rootOpenness;
- }
- }
-
- return {};
- }
-
- void TreeView::restoreOpennessState (const XmlElement& newState, bool restoreStoredSelection)
- {
- if (rootItem != nullptr)
- {
- rootItem->restoreOpennessState (newState);
-
- if (newState.hasAttribute ("scrollPos"))
- viewport->setViewPosition (viewport->getViewPositionX(),
- newState.getIntAttribute ("scrollPos"));
-
- if (restoreStoredSelection)
- {
- clearSelectedItems();
-
- for (auto* e : newState.getChildWithTagNameIterator ("SELECTED"))
- if (auto* item = rootItem->findItemFromIdentifierString (e->getStringAttribute ("id")))
- item->setSelected (true, false);
- }
-
- updateVisibleItems();
- }
- }
-
- //==============================================================================
- void TreeView::paint (Graphics& g)
- {
- g.fillAll (findColour (backgroundColourId));
- }
-
- void TreeView::resized()
- {
- viewport->setBounds (getLocalBounds());
- updateVisibleItems();
- }
-
- void TreeView::enablementChanged()
- {
- repaint();
- }
-
- void TreeView::moveSelectedRow (int delta)
- {
- auto numRowsInTree = getNumRowsInTree();
-
- if (numRowsInTree > 0)
- {
- int rowSelected = 0;
-
- if (auto* firstSelected = getSelectedItem (0))
- rowSelected = firstSelected->getRowNumberInTree();
-
- rowSelected = jlimit (0, numRowsInTree - 1, rowSelected + delta);
-
- for (;;)
- {
- if (auto* item = getItemOnRow (rowSelected))
- {
- if (! item->canBeSelected())
- {
- // if the row we want to highlight doesn't allow it, try skipping
- // to the next item..
- auto nextRowToTry = jlimit (0, numRowsInTree - 1, rowSelected + (delta < 0 ? -1 : 1));
-
- if (rowSelected != nextRowToTry)
- {
- rowSelected = nextRowToTry;
- continue;
- }
-
- break;
- }
-
- item->setSelected (true, true);
- scrollToKeepItemVisible (item);
- }
-
- break;
- }
- }
- }
-
- void TreeView::scrollToKeepItemVisible (TreeViewItem* item)
- {
- if (item != nullptr && item->ownerView == this)
- {
- updateVisibleItems();
-
- item = item->getDeepestOpenParentItem();
-
- auto y = item->y;
- auto viewTop = viewport->getViewPositionY();
-
- if (y < viewTop)
- {
- viewport->setViewPosition (viewport->getViewPositionX(), y);
- }
- else if (y + item->itemHeight > viewTop + viewport->getViewHeight())
- {
- viewport->setViewPosition (viewport->getViewPositionX(),
- (y + item->itemHeight) - viewport->getViewHeight());
- }
- }
- }
-
- bool TreeView::toggleOpenSelectedItem()
- {
- if (auto* firstSelected = getSelectedItem (0))
- {
- if (firstSelected->mightContainSubItems())
- {
- firstSelected->setOpen (! firstSelected->isOpen());
- return true;
- }
- }
-
- return false;
- }
-
- void TreeView::moveOutOfSelectedItem()
- {
- if (auto* firstSelected = getSelectedItem (0))
- {
- if (firstSelected->isOpen())
- {
- firstSelected->setOpen (false);
- }
- else
- {
- auto* parent = firstSelected->parentItem;
-
- if ((! rootItemVisible) && parent == rootItem)
- parent = nullptr;
-
- if (parent != nullptr)
- {
- parent->setSelected (true, true);
- scrollToKeepItemVisible (parent);
- }
- }
- }
- }
-
- void TreeView::moveIntoSelectedItem()
- {
- if (auto* firstSelected = getSelectedItem (0))
- {
- if (firstSelected->isOpen() || ! firstSelected->mightContainSubItems())
- moveSelectedRow (1);
- else
- firstSelected->setOpen (true);
- }
- }
-
- void TreeView::moveByPages (int numPages)
- {
- if (auto* currentItem = getSelectedItem (0))
- {
- auto pos = currentItem->getItemPosition (false);
- auto targetY = pos.getY() + numPages * (getHeight() - pos.getHeight());
- auto currentRow = currentItem->getRowNumberInTree();
-
- for (;;)
- {
- moveSelectedRow (numPages);
- currentItem = getSelectedItem (0);
-
- if (currentItem == nullptr)
- break;
-
- auto y = currentItem->getItemPosition (false).getY();
-
- if ((numPages < 0 && y <= targetY) || (numPages > 0 && y >= targetY))
- break;
-
- auto newRow = currentItem->getRowNumberInTree();
-
- if (newRow == currentRow)
- break;
-
- currentRow = newRow;
- }
- }
- }
-
- bool TreeView::keyPressed (const KeyPress& key)
- {
- if (rootItem != nullptr)
- {
- if (key == KeyPress::upKey) { moveSelectedRow (-1); return true; }
- if (key == KeyPress::downKey) { moveSelectedRow (1); return true; }
- if (key == KeyPress::homeKey) { moveSelectedRow (-0x3fffffff); return true; }
- if (key == KeyPress::endKey) { moveSelectedRow (0x3fffffff); return true; }
- if (key == KeyPress::pageUpKey) { moveByPages (-1); return true; }
- if (key == KeyPress::pageDownKey) { moveByPages (1); return true; }
- if (key == KeyPress::returnKey) { return toggleOpenSelectedItem(); }
- if (key == KeyPress::leftKey) { moveOutOfSelectedItem(); return true; }
- if (key == KeyPress::rightKey) { moveIntoSelectedItem(); return true; }
- }
-
- return false;
- }
-
- void TreeView::updateVisibleItems()
- {
- viewport->recalculatePositions (TreeViewport::Async::yes);
- }
-
- //==============================================================================
- struct TreeView::InsertPoint
- {
- InsertPoint (TreeView& view, const StringArray& files,
- const DragAndDropTarget::SourceDetails& dragSourceDetails)
- : pos (dragSourceDetails.localPosition),
- item (view.getItemAt (dragSourceDetails.localPosition.y))
- {
- if (item != nullptr)
- {
- auto itemPos = item->getItemPosition (true);
- insertIndex = item->getIndexInParent();
- auto oldY = pos.y;
- pos.y = itemPos.getY();
-
- if (item->getNumSubItems() == 0 || ! item->isOpen())
- {
- if (files.size() > 0 ? item->isInterestedInFileDrag (files)
- : item->isInterestedInDragSource (dragSourceDetails))
- {
- // Check if we're trying to drag into an empty group item..
- if (oldY > itemPos.getY() + itemPos.getHeight() / 4
- && oldY < itemPos.getBottom() - itemPos.getHeight() / 4)
- {
- insertIndex = 0;
- pos.x = itemPos.getX() + view.getIndentSize();
- pos.y = itemPos.getBottom();
- return;
- }
- }
- }
-
- if (oldY > itemPos.getCentreY())
- {
- pos.y += item->getItemHeight();
-
- while (item->isLastOfSiblings() && item->getParentItem() != nullptr
- && item->getParentItem()->getParentItem() != nullptr)
- {
- if (pos.x > itemPos.getX())
- break;
-
- item = item->getParentItem();
- itemPos = item->getItemPosition (true);
- insertIndex = item->getIndexInParent();
- }
-
- ++insertIndex;
- }
-
- pos.x = itemPos.getX();
- item = item->getParentItem();
- }
- else if (auto* root = view.getRootItem())
- {
- // If they're dragging beyond the bottom of the list, then insert at the end of the root item..
- item = root;
- insertIndex = root->getNumSubItems();
- pos = root->getItemPosition (true).getBottomLeft();
- pos.x += view.getIndentSize();
- }
- }
-
- Point<int> pos;
- TreeViewItem* item;
- int insertIndex = 0;
- };
-
- //==============================================================================
- class TreeView::InsertPointHighlight : public Component
- {
- public:
- InsertPointHighlight()
- {
- setSize (100, 12);
- setAlwaysOnTop (true);
- setInterceptsMouseClicks (false, false);
- }
-
- void setTargetPosition (const InsertPoint& insertPos, const int width) noexcept
- {
- lastItem = insertPos.item;
- lastIndex = insertPos.insertIndex;
- auto offset = getHeight() / 2;
- setBounds (insertPos.pos.x - offset, insertPos.pos.y - offset,
- width - (insertPos.pos.x - offset), getHeight());
- }
-
- void paint (Graphics& g) override
- {
- Path p;
- auto h = (float) getHeight();
- p.addEllipse (2.0f, 2.0f, h - 4.0f, h - 4.0f);
- p.startNewSubPath (h - 2.0f, h / 2.0f);
- p.lineTo ((float) getWidth(), h / 2.0f);
-
- g.setColour (findColour (TreeView::dragAndDropIndicatorColourId, true));
- g.strokePath (p, PathStrokeType (2.0f));
- }
-
- TreeViewItem* lastItem = nullptr;
- int lastIndex = 0;
-
- private:
- JUCE_DECLARE_NON_COPYABLE (InsertPointHighlight)
- };
-
- //==============================================================================
- class TreeView::TargetGroupHighlight : public Component
- {
- public:
- TargetGroupHighlight()
- {
- setAlwaysOnTop (true);
- setInterceptsMouseClicks (false, false);
- }
-
- void setTargetPosition (TreeViewItem* const item) noexcept
- {
- setBounds (item->getItemPosition (true)
- .withHeight (item->getItemHeight()));
- }
-
- void paint (Graphics& g) override
- {
- g.setColour (findColour (TreeView::dragAndDropIndicatorColourId, true));
- g.drawRoundedRectangle (1.0f, 1.0f, (float) getWidth() - 2.0f, (float) getHeight() - 2.0f, 3.0f, 2.0f);
- }
-
- private:
- JUCE_DECLARE_NON_COPYABLE (TargetGroupHighlight)
- };
-
- //==============================================================================
- void TreeView::showDragHighlight (const InsertPoint& insertPos) noexcept
- {
- beginDragAutoRepeat (100);
-
- if (dragInsertPointHighlight == nullptr)
- {
- dragInsertPointHighlight = std::make_unique<InsertPointHighlight>();
- dragTargetGroupHighlight = std::make_unique<TargetGroupHighlight>();
-
- addAndMakeVisible (dragInsertPointHighlight.get());
- addAndMakeVisible (dragTargetGroupHighlight.get());
- }
-
- dragInsertPointHighlight->setTargetPosition (insertPos, viewport->getViewWidth());
- dragTargetGroupHighlight->setTargetPosition (insertPos.item);
- }
-
- void TreeView::hideDragHighlight() noexcept
- {
- dragInsertPointHighlight = nullptr;
- dragTargetGroupHighlight = nullptr;
- }
-
- void TreeView::handleDrag (const StringArray& files, const SourceDetails& dragSourceDetails)
- {
- const auto scrolled = viewport->autoScroll (dragSourceDetails.localPosition.x,
- dragSourceDetails.localPosition.y, 20, 10);
-
- InsertPoint insertPos (*this, files, dragSourceDetails);
-
- if (insertPos.item != nullptr)
- {
- if (scrolled || dragInsertPointHighlight == nullptr
- || dragInsertPointHighlight->lastItem != insertPos.item
- || dragInsertPointHighlight->lastIndex != insertPos.insertIndex)
- {
- if (files.size() > 0 ? insertPos.item->isInterestedInFileDrag (files)
- : insertPos.item->isInterestedInDragSource (dragSourceDetails))
- showDragHighlight (insertPos);
- else
- hideDragHighlight();
- }
- }
- else
- {
- hideDragHighlight();
- }
- }
-
- void TreeView::handleDrop (const StringArray& files, const SourceDetails& dragSourceDetails)
- {
- hideDragHighlight();
-
- InsertPoint insertPos (*this, files, dragSourceDetails);
-
- if (insertPos.item == nullptr)
- insertPos.item = rootItem;
-
- if (insertPos.item != nullptr)
- {
- if (files.size() > 0)
- {
- if (insertPos.item->isInterestedInFileDrag (files))
- insertPos.item->filesDropped (files, insertPos.insertIndex);
- }
- else
- {
- if (insertPos.item->isInterestedInDragSource (dragSourceDetails))
- insertPos.item->itemDropped (dragSourceDetails, insertPos.insertIndex);
- }
- }
- }
-
- //==============================================================================
- bool TreeView::isInterestedInFileDrag (const StringArray&)
- {
- return true;
- }
-
- void TreeView::fileDragEnter (const StringArray& files, int x, int y)
- {
- fileDragMove (files, x, y);
- }
-
- void TreeView::fileDragMove (const StringArray& files, int x, int y)
- {
- handleDrag (files, SourceDetails (var(), this, { x, y }));
- }
-
- void TreeView::fileDragExit (const StringArray&)
- {
- hideDragHighlight();
- }
-
- void TreeView::filesDropped (const StringArray& files, int x, int y)
- {
- handleDrop (files, SourceDetails (var(), this, { x, y }));
- }
-
- bool TreeView::isInterestedInDragSource (const SourceDetails& /*dragSourceDetails*/)
- {
- return true;
- }
-
- void TreeView::itemDragEnter (const SourceDetails& dragSourceDetails)
- {
- itemDragMove (dragSourceDetails);
- }
-
- void TreeView::itemDragMove (const SourceDetails& dragSourceDetails)
- {
- handleDrag (StringArray(), dragSourceDetails);
- }
-
- void TreeView::itemDragExit (const SourceDetails& /*dragSourceDetails*/)
- {
- hideDragHighlight();
- }
-
- void TreeView::itemDropped (const SourceDetails& dragSourceDetails)
- {
- handleDrop (StringArray(), dragSourceDetails);
- }
-
- //==============================================================================
- std::unique_ptr<AccessibilityHandler> TreeView::createAccessibilityHandler()
- {
- class TableInterface : public AccessibilityTableInterface
- {
- public:
- explicit TableInterface (TreeView& treeViewToWrap) : treeView (treeViewToWrap) {}
-
- int getNumRows() const override { return treeView.getNumRowsInTree(); }
- int getNumColumns() const override { return 1; }
-
- const AccessibilityHandler* getCellHandler (int row, int) const override
- {
- if (auto* itemComp = treeView.getItemComponent (treeView.getItemOnRow (row)))
- return itemComp->getAccessibilityHandler();
-
- return nullptr;
- }
-
- private:
- TreeView& treeView;
-
- JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TableInterface)
- };
-
- return std::make_unique<AccessibilityHandler> (*this,
- AccessibilityRole::tree,
- AccessibilityActions{},
- AccessibilityHandler::Interfaces { std::make_unique<TableInterface> (*this) });
- }
-
- //==============================================================================
- TreeViewItem::TreeViewItem()
- {
- static int nextUID = 0;
- uid = nextUID++;
- }
-
- TreeViewItem::~TreeViewItem()
- {
- if (ownerView != nullptr)
- ownerView->viewport->getContentComp()->itemBeingDeleted (this);
- }
-
- String TreeViewItem::getUniqueName() const
- {
- return {};
- }
-
- void TreeViewItem::itemOpennessChanged (bool)
- {
- }
-
- int TreeViewItem::getNumSubItems() const noexcept
- {
- return subItems.size();
- }
-
- TreeViewItem* TreeViewItem::getSubItem (const int index) const noexcept
- {
- return subItems[index];
- }
-
- void TreeViewItem::clearSubItems()
- {
- if (ownerView != nullptr)
- {
- if (! subItems.isEmpty())
- {
- removeAllSubItemsFromList();
- treeHasChanged();
- }
- }
- else
- {
- removeAllSubItemsFromList();
- }
- }
-
- void TreeViewItem::removeAllSubItemsFromList()
- {
- for (int i = subItems.size(); --i >= 0;)
- removeSubItemFromList (i, true);
- }
-
- void TreeViewItem::addSubItem (TreeViewItem* const newItem, const int insertPosition)
- {
- if (newItem != nullptr)
- {
- newItem->parentItem = nullptr;
- newItem->setOwnerView (ownerView);
- newItem->y = 0;
- newItem->itemHeight = newItem->getItemHeight();
- newItem->totalHeight = 0;
- newItem->itemWidth = newItem->getItemWidth();
- newItem->totalWidth = 0;
- newItem->parentItem = this;
-
- if (ownerView != nullptr)
- {
- subItems.insert (insertPosition, newItem);
- treeHasChanged();
-
- if (newItem->isOpen())
- newItem->itemOpennessChanged (true);
- }
- else
- {
- subItems.insert (insertPosition, newItem);
-
- if (newItem->isOpen())
- newItem->itemOpennessChanged (true);
- }
- }
- }
-
- void TreeViewItem::removeSubItem (int index, bool deleteItem)
- {
- if (ownerView != nullptr)
- {
- if (removeSubItemFromList (index, deleteItem))
- treeHasChanged();
- }
- else
- {
- removeSubItemFromList (index, deleteItem);
- }
- }
-
- bool TreeViewItem::removeSubItemFromList (int index, bool deleteItem)
- {
- if (auto* child = subItems[index])
- {
- child->parentItem = nullptr;
- subItems.remove (index, deleteItem);
-
- return true;
- }
-
- return false;
- }
-
- TreeViewItem::Openness TreeViewItem::getOpenness() const noexcept
- {
- return openness;
- }
-
- void TreeViewItem::setOpenness (Openness newOpenness)
- {
- auto wasOpen = isOpen();
- openness = newOpenness;
- auto isNowOpen = isOpen();
-
- if (isNowOpen != wasOpen)
- {
- treeHasChanged();
- itemOpennessChanged (isNowOpen);
- }
- }
-
- bool TreeViewItem::isOpen() const noexcept
- {
- if (openness == Openness::opennessDefault)
- return ownerView != nullptr && ownerView->defaultOpenness;
-
- return openness == Openness::opennessOpen;
- }
-
- void TreeViewItem::setOpen (const bool shouldBeOpen)
- {
- if (isOpen() != shouldBeOpen)
- setOpenness (shouldBeOpen ? Openness::opennessOpen
- : Openness::opennessClosed);
- }
-
- bool TreeViewItem::isFullyOpen() const noexcept
- {
- if (! isOpen())
- return false;
-
- for (auto* i : subItems)
- if (! i->isFullyOpen())
- return false;
-
- return true;
- }
-
- void TreeViewItem::restoreToDefaultOpenness()
- {
- setOpenness (Openness::opennessDefault);
- }
-
- bool TreeViewItem::isSelected() const noexcept
- {
- return selected;
- }
-
- void TreeViewItem::deselectAllRecursively (TreeViewItem* itemToIgnore)
- {
- if (this != itemToIgnore)
- setSelected (false, false);
-
- for (auto* i : subItems)
- i->deselectAllRecursively (itemToIgnore);
- }
-
- void TreeViewItem::setSelected (const bool shouldBeSelected,
- const bool deselectOtherItemsFirst,
- const NotificationType notify)
- {
- if (shouldBeSelected && ! canBeSelected())
- return;
-
- if (deselectOtherItemsFirst)
- getTopLevelItem()->deselectAllRecursively (this);
-
- if (shouldBeSelected != selected)
- {
- selected = shouldBeSelected;
-
- if (ownerView != nullptr)
- {
- ownerView->repaint();
-
- if (selected)
- {
- if (auto* itemComponent = ownerView->getItemComponent (this))
- if (auto* itemHandler = itemComponent->getAccessibilityHandler())
- itemHandler->grabFocus();
- }
-
- if (auto* handler = ownerView->getAccessibilityHandler())
- handler->notifyAccessibilityEvent (AccessibilityEvent::rowSelectionChanged);
- }
-
- if (notify != dontSendNotification)
- itemSelectionChanged (shouldBeSelected);
- }
- }
-
- void TreeViewItem::paintItem (Graphics&, int, int)
- {
- }
-
- void TreeViewItem::paintOpenCloseButton (Graphics& g, const Rectangle<float>& area, Colour backgroundColour, bool isMouseOver)
- {
- getOwnerView()->getLookAndFeel()
- .drawTreeviewPlusMinusBox (g, area, backgroundColour, isOpen(), isMouseOver);
- }
-
- void TreeViewItem::paintHorizontalConnectingLine (Graphics& g, const Line<float>& line)
- {
- g.setColour (ownerView->findColour (TreeView::linesColourId));
- g.drawLine (line);
- }
-
- void TreeViewItem::paintVerticalConnectingLine (Graphics& g, const Line<float>& line)
- {
- g.setColour (ownerView->findColour (TreeView::linesColourId));
- g.drawLine (line);
- }
-
- void TreeViewItem::itemClicked (const MouseEvent&)
- {
- }
-
- void TreeViewItem::itemDoubleClicked (const MouseEvent&)
- {
- if (mightContainSubItems())
- setOpen (! isOpen());
- }
-
- void TreeViewItem::itemSelectionChanged (bool)
- {
- }
-
- String TreeViewItem::getTooltip()
- {
- return {};
- }
-
- String TreeViewItem::getAccessibilityName()
- {
- auto tooltipString = getTooltip();
-
- return tooltipString.isNotEmpty()
- ? tooltipString
- : "Level " + String (getItemDepth (this)) + " row " + String (getIndexInParent());
- }
-
- void TreeViewItem::ownerViewChanged (TreeView*)
- {
- }
-
- var TreeViewItem::getDragSourceDescription()
- {
- return {};
- }
-
- bool TreeViewItem::isInterestedInFileDrag (const StringArray&)
- {
- return false;
- }
-
- void TreeViewItem::filesDropped (const StringArray& /*files*/, int /*insertIndex*/)
- {
- }
-
- bool TreeViewItem::isInterestedInDragSource (const DragAndDropTarget::SourceDetails& /*dragSourceDetails*/)
- {
- return false;
- }
-
- void TreeViewItem::itemDropped (const DragAndDropTarget::SourceDetails& /*dragSourceDetails*/, int /*insertIndex*/)
- {
- }
-
- Rectangle<int> TreeViewItem::getItemPosition (const bool relativeToTreeViewTopLeft) const noexcept
- {
- auto indentX = getIndentX();
- auto width = itemWidth;
-
- if (ownerView != nullptr && width < 0)
- width = ownerView->viewport->getViewWidth() - indentX;
-
- Rectangle<int> r (indentX, y, jmax (0, width), totalHeight);
-
- if (relativeToTreeViewTopLeft && ownerView != nullptr)
- r -= ownerView->viewport->getViewPosition();
-
- return r;
- }
-
- void TreeViewItem::treeHasChanged() const noexcept
- {
- if (ownerView != nullptr)
- ownerView->updateVisibleItems();
- }
-
- void TreeViewItem::repaintItem() const
- {
- if (ownerView != nullptr && areAllParentsOpen())
- if (auto* component = ownerView->getItemComponent (this))
- component->repaint();
- }
-
- bool TreeViewItem::areAllParentsOpen() const noexcept
- {
- return parentItem == nullptr
- || (parentItem->isOpen() && parentItem->areAllParentsOpen());
- }
-
- void TreeViewItem::updatePositions (int newY)
- {
- y = newY;
- itemHeight = getItemHeight();
- totalHeight = itemHeight;
- itemWidth = getItemWidth();
- totalWidth = jmax (itemWidth, 0) + getIndentX();
-
- if (isOpen())
- {
- newY += totalHeight;
-
- for (auto* i : subItems)
- {
- i->updatePositions (newY);
- newY += i->totalHeight;
- totalHeight += i->totalHeight;
- totalWidth = jmax (totalWidth, i->totalWidth);
- }
- }
- }
-
- TreeViewItem* TreeViewItem::getDeepestOpenParentItem() noexcept
- {
- auto* result = this;
- auto* item = this;
-
- while (item->parentItem != nullptr)
- {
- item = item->parentItem;
-
- if (! item->isOpen())
- result = item;
- }
-
- return result;
- }
-
- void TreeViewItem::setOwnerView (TreeView* const newOwner) noexcept
- {
- ownerView = newOwner;
-
- for (auto* i : subItems)
- {
- i->setOwnerView (newOwner);
- i->ownerViewChanged (newOwner);
- }
- }
-
- int TreeViewItem::getIndentX() const noexcept
- {
- if (ownerView == nullptr)
- return 0;
-
- int x = ownerView->rootItemVisible ? 1 : 0;
-
- if (! ownerView->openCloseButtonsVisible)
- --x;
-
- for (auto* p = parentItem; p != nullptr; p = p->parentItem)
- ++x;
-
- return x * ownerView->getIndentSize();
- }
-
- void TreeViewItem::setDrawsInLeftMargin (bool canDrawInLeftMargin) noexcept
- {
- drawsInLeftMargin = canDrawInLeftMargin;
- }
-
- void TreeViewItem::setDrawsInRightMargin (bool canDrawInRightMargin) noexcept
- {
- drawsInRightMargin = canDrawInRightMargin;
- }
-
- bool TreeViewItem::areLinesDrawn() const
- {
- return drawLinesSet ? drawLinesInside
- : (ownerView != nullptr && ownerView->getLookAndFeel().areLinesDrawnForTreeView (*ownerView));
- }
-
- bool TreeViewItem::isLastOfSiblings() const noexcept
- {
- return parentItem == nullptr
- || parentItem->subItems.getLast() == this;
- }
-
- int TreeViewItem::getIndexInParent() const noexcept
- {
- return parentItem == nullptr ? 0
- : parentItem->subItems.indexOf (this);
- }
-
- TreeViewItem* TreeViewItem::getTopLevelItem() noexcept
- {
- return parentItem == nullptr ? this
- : parentItem->getTopLevelItem();
- }
-
- int TreeViewItem::getNumRows() const noexcept
- {
- int num = 1;
-
- if (isOpen())
- for (auto* i : subItems)
- num += i->getNumRows();
-
- return num;
- }
-
- TreeViewItem* TreeViewItem::getItemOnRow (int index) noexcept
- {
- if (index == 0)
- return this;
-
- if (index > 0 && isOpen())
- {
- --index;
-
- for (auto* i : subItems)
- {
- if (index == 0)
- return i;
-
- auto numRows = i->getNumRows();
-
- if (numRows > index)
- return i->getItemOnRow (index);
-
- index -= numRows;
- }
- }
-
- return nullptr;
- }
-
- int TreeViewItem::countSelectedItemsRecursively (int depth) const noexcept
- {
- int total = isSelected() ? 1 : 0;
-
- if (depth != 0)
- for (auto* i : subItems)
- total += i->countSelectedItemsRecursively (depth - 1);
-
- return total;
- }
-
- TreeViewItem* TreeViewItem::getSelectedItemWithIndex (int index) noexcept
- {
- if (isSelected())
- {
- if (index == 0)
- return this;
-
- --index;
- }
-
- if (index >= 0)
- {
- for (auto* i : subItems)
- {
- if (auto* found = i->getSelectedItemWithIndex (index))
- return found;
-
- index -= i->countSelectedItemsRecursively (-1);
- }
- }
-
- return nullptr;
- }
-
- int TreeViewItem::getRowNumberInTree() const noexcept
- {
- if (parentItem != nullptr && ownerView != nullptr)
- {
- if (! parentItem->isOpen())
- return parentItem->getRowNumberInTree();
-
- auto n = 1 + parentItem->getRowNumberInTree();
-
- auto ourIndex = parentItem->subItems.indexOf (this);
- jassert (ourIndex >= 0);
-
- while (--ourIndex >= 0)
- n += parentItem->subItems [ourIndex]->getNumRows();
-
- if (parentItem->parentItem == nullptr
- && ! ownerView->rootItemVisible)
- --n;
-
- return n;
- }
-
- return 0;
- }
-
- void TreeViewItem::setLinesDrawnForSubItems (bool drawLines) noexcept
- {
- drawLinesInside = drawLines;
- drawLinesSet = true;
- }
-
- static String escapeSlashesInTreeViewItemName (const String& s)
- {
- return s.replaceCharacter ('/', '\\');
- }
-
- String TreeViewItem::getItemIdentifierString() const
- {
- String s;
-
- if (parentItem != nullptr)
- s = parentItem->getItemIdentifierString();
-
- return s + "/" + escapeSlashesInTreeViewItemName (getUniqueName());
- }
-
- TreeViewItem* TreeViewItem::findItemFromIdentifierString (const String& identifierString)
- {
- auto thisId = "/" + escapeSlashesInTreeViewItemName (getUniqueName());
-
- if (thisId == identifierString)
- return this;
-
- if (identifierString.startsWith (thisId + "/"))
- {
- auto remainingPath = identifierString.substring (thisId.length());
-
- const auto wasOpen = isOpen();
- setOpen (true);
-
- for (auto* i : subItems)
- if (auto* item = i->findItemFromIdentifierString (remainingPath))
- return item;
-
- setOpen (wasOpen);
- }
-
- return nullptr;
- }
-
- void TreeViewItem::restoreOpennessState (const XmlElement& e)
- {
- if (e.hasTagName ("CLOSED"))
- {
- setOpen (false);
- }
- else if (e.hasTagName ("OPEN"))
- {
- setOpen (true);
-
- Array<TreeViewItem*> items;
- items.addArray (subItems);
-
- for (auto* n : e.getChildIterator())
- {
- auto id = n->getStringAttribute ("id");
-
- for (int i = 0; i < items.size(); ++i)
- {
- auto* ti = items.getUnchecked (i);
-
- if (ti->getUniqueName() == id)
- {
- ti->restoreOpennessState (*n);
- items.remove (i);
- break;
- }
- }
- }
-
- // for any items that weren't mentioned in the XML, reset them to default:
- for (auto* i : items)
- i->restoreToDefaultOpenness();
- }
- }
-
- std::unique_ptr<XmlElement> TreeViewItem::getOpennessState() const
- {
- return getOpennessState (true);
- }
-
- std::unique_ptr<XmlElement> TreeViewItem::getOpennessState (bool canReturnNull) const
- {
- auto name = getUniqueName();
-
- if (name.isNotEmpty())
- {
- std::unique_ptr<XmlElement> e;
-
- if (isOpen())
- {
- if (canReturnNull && ownerView != nullptr && ownerView->defaultOpenness && isFullyOpen())
- return nullptr;
-
- e = std::make_unique<XmlElement> ("OPEN");
-
- for (int i = subItems.size(); --i >= 0;)
- e->prependChildElement (subItems.getUnchecked (i)->getOpennessState (true).release());
- }
- else
- {
- if (canReturnNull && ownerView != nullptr && ! ownerView->defaultOpenness)
- return nullptr;
-
- e = std::make_unique<XmlElement> ("CLOSED");
- }
-
- e->setAttribute ("id", name);
- return e;
- }
-
- // trying to save the openness for an element that has no name - this won't
- // work because it needs the names to identify what to open.
- jassertfalse;
- return {};
- }
-
- //==============================================================================
- TreeViewItem::OpennessRestorer::OpennessRestorer (TreeViewItem& item)
- : treeViewItem (item),
- oldOpenness (item.getOpennessState())
- {
- }
-
- TreeViewItem::OpennessRestorer::~OpennessRestorer()
- {
- if (oldOpenness != nullptr)
- treeViewItem.restoreOpennessState (*oldOpenness);
- }
-
- void TreeViewItem::draw (Graphics& g, int width, bool isMouseOverButton)
- {
- if (ownerView == nullptr)
- return;
-
- const auto indent = getIndentX();
- const auto itemW = (itemWidth < 0 || drawsInRightMargin) ? width - indent : itemWidth;
-
- {
- Graphics::ScopedSaveState ss (g);
- g.setOrigin (indent, 0);
-
- if (g.reduceClipRegion (drawsInLeftMargin ? -indent : 0, 0,
- drawsInLeftMargin ? itemW + indent : itemW, itemHeight))
- {
- if (isSelected())
- g.fillAll (ownerView->findColour (TreeView::selectedItemBackgroundColourId));
- else
- g.fillAll ((getRowNumberInTree() % 2 == 0) ? ownerView->findColour (TreeView::oddItemsColourId)
- : ownerView->findColour (TreeView::evenItemsColourId));
-
- paintItem (g, itemWidth < 0 ? width - indent : itemWidth, itemHeight);
- }
- }
-
- const auto halfH = (float) itemHeight * 0.5f;
- const auto indentWidth = ownerView->getIndentSize();
- const auto depth = getItemDepth (this);
-
- if (depth >= 0 && ownerView->openCloseButtonsVisible)
- {
- auto x = ((float) depth + 0.5f) * (float) indentWidth;
- const auto parentLinesDrawn = parentItem != nullptr && parentItem->areLinesDrawn();
-
- if (parentLinesDrawn)
- paintVerticalConnectingLine (g, Line<float> (x, 0, x, isLastOfSiblings() ? halfH : (float) itemHeight));
-
- if (parentLinesDrawn || (parentItem == nullptr && areLinesDrawn()))
- paintHorizontalConnectingLine (g, Line<float> (x, halfH, x + (float) indentWidth * 0.5f, halfH));
-
- {
- auto* p = parentItem;
- auto d = depth;
-
- while (p != nullptr && --d >= 0)
- {
- x -= (float) indentWidth;
-
- if ((p->parentItem == nullptr || p->parentItem->areLinesDrawn()) && ! p->isLastOfSiblings())
- p->paintVerticalConnectingLine (g, Line<float> (x, 0, x, (float) itemHeight));
-
- p = p->parentItem;
- }
- }
-
- if (mightContainSubItems())
- {
- auto backgroundColour = ownerView->findColour (TreeView::backgroundColourId);
-
- paintOpenCloseButton (g, Rectangle<float> ((float) (depth * indentWidth), 0, (float) indentWidth, (float) itemHeight),
- backgroundColour.isTransparent() ? Colours::white : backgroundColour,
- isMouseOverButton);
- }
- }
- }
-
- } // namespace juce
|