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

juce_TreeView.cpp 65KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. static int getItemDepth (const TreeViewItem* item)
  21. {
  22. if (item == nullptr || item->getOwnerView() == nullptr)
  23. return 0;
  24. auto depth = item->getOwnerView()->isRootItemVisible() ? 0 : -1;
  25. for (auto* parent = item->getParentItem(); parent != nullptr; parent = parent->getParentItem())
  26. ++depth;
  27. return depth;
  28. }
  29. //==============================================================================
  30. class TreeView::ItemComponent : public Component,
  31. public TooltipClient
  32. {
  33. public:
  34. explicit ItemComponent (TreeViewItem& itemToRepresent)
  35. : item (itemToRepresent),
  36. customComponent (item.createItemComponent())
  37. {
  38. if (hasCustomComponent())
  39. addAndMakeVisible (*customComponent);
  40. }
  41. void paint (Graphics& g) override
  42. {
  43. item.draw (g, getWidth(), mouseIsOverButton);
  44. }
  45. void resized() override
  46. {
  47. if (hasCustomComponent())
  48. {
  49. auto itemPosition = item.getItemPosition (false);
  50. customComponent->setBounds (getLocalBounds().withX (itemPosition.getX())
  51. .withWidth (itemPosition.getWidth()));
  52. }
  53. }
  54. void setMouseIsOverButton (bool isOver)
  55. {
  56. mouseIsOverButton = isOver;
  57. repaint();
  58. }
  59. TreeViewItem& getRepresentedItem() const noexcept
  60. {
  61. return item;
  62. }
  63. String getTooltip() override
  64. {
  65. return item.getTooltip();
  66. }
  67. private:
  68. //==============================================================================
  69. class ItemAccessibilityHandler : public AccessibilityHandler
  70. {
  71. public:
  72. explicit ItemAccessibilityHandler (ItemComponent& comp)
  73. : AccessibilityHandler (comp,
  74. AccessibilityRole::treeItem,
  75. getAccessibilityActions (comp),
  76. { std::make_unique<ItemCellInterface> (comp) }),
  77. itemComponent (comp)
  78. {
  79. }
  80. String getTitle() const override
  81. {
  82. return itemComponent.getRepresentedItem().getAccessibilityName();
  83. }
  84. String getHelp() const override
  85. {
  86. return itemComponent.getRepresentedItem().getTooltip();
  87. }
  88. AccessibleState getCurrentState() const override
  89. {
  90. auto& treeItem = itemComponent.getRepresentedItem();
  91. auto state = AccessibilityHandler::getCurrentState().withAccessibleOffscreen();
  92. if (auto* tree = treeItem.getOwnerView())
  93. {
  94. if (tree->isMultiSelectEnabled())
  95. state = state.withMultiSelectable();
  96. else
  97. state = state.withSelectable();
  98. }
  99. if (treeItem.mightContainSubItems())
  100. {
  101. state = state.withExpandable();
  102. if (treeItem.isOpen())
  103. state = state.withExpanded();
  104. else
  105. state = state.withCollapsed();
  106. }
  107. if (treeItem.isSelected())
  108. state = state.withSelected();
  109. return state;
  110. }
  111. class ItemCellInterface : public AccessibilityCellInterface
  112. {
  113. public:
  114. explicit ItemCellInterface (ItemComponent& c) : itemComponent (c) {}
  115. int getColumnIndex() const override { return 0; }
  116. int getColumnSpan() const override { return 1; }
  117. int getRowIndex() const override
  118. {
  119. return itemComponent.getRepresentedItem().getRowNumberInTree();
  120. }
  121. int getRowSpan() const override
  122. {
  123. return 1;
  124. }
  125. int getDisclosureLevel() const override
  126. {
  127. return getItemDepth (&itemComponent.getRepresentedItem());
  128. }
  129. const AccessibilityHandler* getTableHandler() const override
  130. {
  131. if (auto* tree = itemComponent.getRepresentedItem().getOwnerView())
  132. return tree->getAccessibilityHandler();
  133. return nullptr;
  134. }
  135. private:
  136. ItemComponent& itemComponent;
  137. };
  138. private:
  139. static AccessibilityActions getAccessibilityActions (ItemComponent& itemComponent)
  140. {
  141. auto onFocus = [&itemComponent]
  142. {
  143. auto& treeItem = itemComponent.getRepresentedItem();
  144. if (auto* tree = treeItem.getOwnerView())
  145. tree->scrollToKeepItemVisible (&treeItem);
  146. };
  147. auto onPress = [&itemComponent]
  148. {
  149. itemComponent.getRepresentedItem().itemClicked (generateMouseEvent (itemComponent, { ModifierKeys::leftButtonModifier }));
  150. };
  151. auto onShowMenu = [&itemComponent]
  152. {
  153. itemComponent.getRepresentedItem().itemClicked (generateMouseEvent (itemComponent, { ModifierKeys::popupMenuClickModifier }));
  154. };
  155. auto onToggle = [&itemComponent, onFocus]
  156. {
  157. if (auto* handler = itemComponent.getAccessibilityHandler())
  158. {
  159. auto isSelected = handler->getCurrentState().isSelected();
  160. if (! isSelected)
  161. onFocus();
  162. itemComponent.getRepresentedItem().setSelected (! isSelected, true);
  163. }
  164. };
  165. auto actions = AccessibilityActions().addAction (AccessibilityActionType::focus, std::move (onFocus))
  166. .addAction (AccessibilityActionType::press, std::move (onPress))
  167. .addAction (AccessibilityActionType::showMenu, std::move (onShowMenu))
  168. .addAction (AccessibilityActionType::toggle, std::move (onToggle));
  169. return actions;
  170. }
  171. ItemComponent& itemComponent;
  172. static MouseEvent generateMouseEvent (ItemComponent& itemComp, ModifierKeys mods)
  173. {
  174. auto topLeft = itemComp.getRepresentedItem().getItemPosition (false).toFloat().getTopLeft();
  175. return { Desktop::getInstance().getMainMouseSource(), topLeft, mods,
  176. MouseInputSource::defaultPressure, MouseInputSource::defaultOrientation, MouseInputSource::defaultRotation,
  177. MouseInputSource::defaultTiltX, MouseInputSource::defaultTiltY,
  178. &itemComp, &itemComp, Time::getCurrentTime(), topLeft, Time::getCurrentTime(), 0, false };
  179. }
  180. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ItemAccessibilityHandler)
  181. };
  182. std::unique_ptr<AccessibilityHandler> createAccessibilityHandler() override
  183. {
  184. if (hasCustomComponent() && customComponent->getAccessibilityHandler() != nullptr)
  185. return nullptr;
  186. return std::make_unique<ItemAccessibilityHandler> (*this);
  187. }
  188. bool hasCustomComponent() const noexcept { return customComponent.get() != nullptr; }
  189. TreeViewItem& item;
  190. std::unique_ptr<Component> customComponent;
  191. bool mouseIsOverButton = false;
  192. //==============================================================================
  193. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ItemComponent)
  194. };
  195. //==============================================================================
  196. class TreeView::ContentComponent : public Component,
  197. public TooltipClient,
  198. public AsyncUpdater
  199. {
  200. public:
  201. ContentComponent (TreeView& tree) : owner (tree)
  202. {
  203. }
  204. //==============================================================================
  205. void resized() override
  206. {
  207. triggerAsyncUpdate();
  208. }
  209. String getTooltip() override
  210. {
  211. if (auto* itemComponent = getItemComponentAt (getMouseXYRelative()))
  212. return itemComponent->getRepresentedItem().getTooltip();
  213. return owner.getTooltip();
  214. }
  215. void mouseDown (const MouseEvent& e) override { mouseDownInternal (e.getEventRelativeTo (this)); }
  216. void mouseUp (const MouseEvent& e) override { mouseUpInternal (e.getEventRelativeTo (this)); }
  217. void mouseDoubleClick (const MouseEvent& e) override { mouseDoubleClickInternal (e.getEventRelativeTo (this));}
  218. void mouseDrag (const MouseEvent& e) override { mouseDragInternal (e.getEventRelativeTo (this));}
  219. void mouseMove (const MouseEvent& e) override { mouseMoveInternal (e.getEventRelativeTo (this)); }
  220. void mouseExit (const MouseEvent& e) override { mouseExitInternal (e.getEventRelativeTo (this)); }
  221. //==============================================================================
  222. ItemComponent* getItemComponentAt (Point<int> p)
  223. {
  224. auto iter = std::find_if (itemComponents.cbegin(), itemComponents.cend(),
  225. [p] (const std::unique_ptr<ItemComponent>& c)
  226. {
  227. return c->getBounds().contains (p);
  228. });
  229. if (iter != itemComponents.cend())
  230. return iter->get();
  231. return nullptr;
  232. }
  233. ItemComponent* getComponentForItem (const TreeViewItem* item) const
  234. {
  235. const auto iter = std::find_if (itemComponents.begin(), itemComponents.end(),
  236. [item] (const std::unique_ptr<ItemComponent>& c)
  237. {
  238. return &c->getRepresentedItem() == item;
  239. });
  240. if (iter != itemComponents.end())
  241. return iter->get();
  242. return nullptr;
  243. }
  244. void itemBeingDeleted (const TreeViewItem* item)
  245. {
  246. const auto iter = std::find_if (itemComponents.begin(), itemComponents.end(),
  247. [item] (const std::unique_ptr<ItemComponent>& c)
  248. {
  249. return &c->getRepresentedItem() == item;
  250. });
  251. if (iter != itemComponents.end())
  252. {
  253. if (itemUnderMouse == iter->get())
  254. itemUnderMouse = nullptr;
  255. if (isMouseDraggingInChildComp (*(iter->get())))
  256. owner.hideDragHighlight();
  257. itemComponents.erase (iter);
  258. }
  259. }
  260. void updateComponents()
  261. {
  262. std::set<ItemComponent*> componentsToKeep;
  263. for (auto* treeItem : getAllVisibleItems())
  264. {
  265. if (auto* itemComp = getComponentForItem (treeItem))
  266. {
  267. componentsToKeep.insert (itemComp);
  268. }
  269. else
  270. {
  271. auto newComp = std::make_unique<ItemComponent> (*treeItem);
  272. addAndMakeVisible (*newComp);
  273. newComp->addMouseListener (this, treeItem->customComponentUsesTreeViewMouseHandler());
  274. componentsToKeep.insert (newComp.get());
  275. itemComponents.push_back (std::move (newComp));
  276. }
  277. }
  278. auto removePredicate = [&] (auto& item)
  279. {
  280. if (item == nullptr)
  281. return true;
  282. return componentsToKeep.find (item.get()) == componentsToKeep.end()
  283. && ! isMouseDraggingInChildComp (*item);
  284. };
  285. const auto iter = std::remove_if (itemComponents.begin(), itemComponents.end(), std::move (removePredicate));
  286. itemComponents.erase (iter, itemComponents.end());
  287. for (auto& comp : itemComponents)
  288. {
  289. auto& treeItem = comp->getRepresentedItem();
  290. comp->setBounds ({ 0, treeItem.y, getWidth(), treeItem.itemHeight });
  291. }
  292. }
  293. private:
  294. //==============================================================================
  295. struct ScopedDisableViewportScroll
  296. {
  297. explicit ScopedDisableViewportScroll (ItemComponent& c)
  298. : item (&c)
  299. {
  300. item->setViewportIgnoreDragFlag (true);
  301. }
  302. ~ScopedDisableViewportScroll()
  303. {
  304. if (item != nullptr)
  305. item->setViewportIgnoreDragFlag (false);
  306. }
  307. SafePointer<ItemComponent> item;
  308. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ScopedDisableViewportScroll)
  309. };
  310. //==============================================================================
  311. std::unique_ptr<AccessibilityHandler> createAccessibilityHandler() override
  312. {
  313. return createIgnoredAccessibilityHandler (*this);
  314. }
  315. void mouseDownInternal (const MouseEvent& e)
  316. {
  317. updateItemUnderMouse (e);
  318. isDragging = false;
  319. scopedScrollDisabler = nullptr;
  320. needSelectionOnMouseUp = false;
  321. if (! isEnabled())
  322. return;
  323. if (auto* itemComponent = getItemComponentAt (e.getPosition()))
  324. {
  325. auto& item = itemComponent->getRepresentedItem();
  326. auto pos = item.getItemPosition (false);
  327. // (if the open/close buttons are hidden, we'll treat clicks to the left of the item
  328. // as selection clicks)
  329. if (e.x < pos.getX() && owner.openCloseButtonsVisible)
  330. {
  331. // (clicks to the left of an open/close button are ignored)
  332. if (e.x >= pos.getX() - owner.getIndentSize())
  333. item.setOpen (! item.isOpen());
  334. }
  335. else
  336. {
  337. // mouse-down inside the body of the item..
  338. if (! owner.isMultiSelectEnabled())
  339. item.setSelected (true, true);
  340. else if (item.isSelected())
  341. needSelectionOnMouseUp = ! e.mods.isPopupMenu();
  342. else
  343. selectBasedOnModifiers (item, e.mods);
  344. if (e.x >= pos.getX())
  345. item.itemClicked (e.withNewPosition (e.position - pos.getPosition().toFloat()));
  346. }
  347. }
  348. }
  349. void mouseUpInternal (const MouseEvent& e)
  350. {
  351. updateItemUnderMouse (e);
  352. if (isEnabled() && needSelectionOnMouseUp && e.mouseWasClicked())
  353. if (auto* itemComponent = getItemComponentAt (e.getPosition()))
  354. selectBasedOnModifiers (itemComponent->getRepresentedItem(), e.mods);
  355. }
  356. void mouseDoubleClickInternal (const MouseEvent& e)
  357. {
  358. if (isEnabled() && e.getNumberOfClicks() != 3) // ignore triple clicks
  359. {
  360. if (auto* itemComponent = getItemComponentAt (e.getPosition()))
  361. {
  362. auto& item = itemComponent->getRepresentedItem();
  363. auto pos = item.getItemPosition (false);
  364. if (e.x >= pos.getX() || ! owner.openCloseButtonsVisible)
  365. item.itemDoubleClicked (e.withNewPosition (e.position - pos.getPosition().toFloat()));
  366. }
  367. }
  368. }
  369. void mouseDragInternal (const MouseEvent& e)
  370. {
  371. if (isEnabled()
  372. && ! (isDragging || e.mouseWasClicked()
  373. || e.getDistanceFromDragStart() < 5
  374. || e.mods.isPopupMenu()))
  375. {
  376. isDragging = true;
  377. if (auto* itemComponent = getItemComponentAt (e.getMouseDownPosition()))
  378. {
  379. auto& item = itemComponent->getRepresentedItem();
  380. auto pos = item.getItemPosition (false);
  381. if (e.getMouseDownX() >= pos.getX())
  382. {
  383. auto dragDescription = item.getDragSourceDescription();
  384. if (! (dragDescription.isVoid() || (dragDescription.isString() && dragDescription.toString().isEmpty())))
  385. {
  386. if (auto* dragContainer = DragAndDropContainer::findParentDragContainerFor (this))
  387. {
  388. pos.setSize (pos.getWidth(), item.itemHeight);
  389. const auto additionalScale = 2.0f;
  390. auto dragImage = Component::createComponentSnapshot (pos,
  391. true,
  392. Component::getApproximateScaleFactorForComponent (itemComponent) * additionalScale);
  393. dragImage.multiplyAllAlphas (0.6f);
  394. auto imageOffset = pos.getPosition() - e.getPosition();
  395. dragContainer->startDragging (dragDescription, &owner, { dragImage, additionalScale }, true, &imageOffset, &e.source);
  396. scopedScrollDisabler = std::make_unique<ScopedDisableViewportScroll> (*itemComponent);
  397. }
  398. else
  399. {
  400. // to be able to do a drag-and-drop operation, the treeview needs to
  401. // be inside a component which is also a DragAndDropContainer.
  402. jassertfalse;
  403. }
  404. }
  405. }
  406. }
  407. }
  408. }
  409. void mouseMoveInternal (const MouseEvent& e) { updateItemUnderMouse (e); }
  410. void mouseExitInternal (const MouseEvent& e) { updateItemUnderMouse (e); }
  411. static bool isMouseDraggingInChildComp (const Component& comp)
  412. {
  413. for (auto& ms : Desktop::getInstance().getMouseSources())
  414. if (ms.isDragging())
  415. if (auto* underMouse = ms.getComponentUnderMouse())
  416. return (&comp == underMouse || comp.isParentOf (underMouse));
  417. return false;
  418. }
  419. void updateItemUnderMouse (const MouseEvent& e)
  420. {
  421. if (! owner.openCloseButtonsVisible)
  422. return;
  423. auto* newItem = [this, &e]() -> ItemComponent*
  424. {
  425. if (auto* itemComponent = getItemComponentAt (e.getPosition()))
  426. {
  427. auto& item = itemComponent->getRepresentedItem();
  428. if (item.mightContainSubItems())
  429. {
  430. const auto xPos = item.getItemPosition (false).getX();
  431. if (xPos - owner.getIndentSize() <= e.x && e.x < xPos)
  432. return itemComponent;
  433. }
  434. }
  435. return nullptr;
  436. }();
  437. if (itemUnderMouse != newItem)
  438. {
  439. if (itemUnderMouse != nullptr)
  440. itemUnderMouse->setMouseIsOverButton (false);
  441. if (newItem != nullptr)
  442. newItem->setMouseIsOverButton (true);
  443. itemUnderMouse = newItem;
  444. }
  445. }
  446. void handleAsyncUpdate() override
  447. {
  448. owner.updateVisibleItems();
  449. }
  450. //==============================================================================
  451. void selectBasedOnModifiers (TreeViewItem& item, const ModifierKeys modifiers)
  452. {
  453. TreeViewItem* firstSelected = nullptr;
  454. if (modifiers.isShiftDown() && ((firstSelected = owner.getSelectedItem (0)) != nullptr))
  455. {
  456. auto* lastSelected = owner.getSelectedItem (owner.getNumSelectedItems() - 1);
  457. if (lastSelected == nullptr)
  458. {
  459. jassertfalse;
  460. return;
  461. }
  462. auto rowStart = firstSelected->getRowNumberInTree();
  463. auto rowEnd = lastSelected->getRowNumberInTree();
  464. if (rowStart > rowEnd)
  465. std::swap (rowStart, rowEnd);
  466. auto ourRow = item.getRowNumberInTree();
  467. auto otherEnd = ourRow < rowEnd ? rowStart : rowEnd;
  468. if (ourRow > otherEnd)
  469. std::swap (ourRow, otherEnd);
  470. for (int i = ourRow; i <= otherEnd; ++i)
  471. owner.getItemOnRow (i)->setSelected (true, false);
  472. }
  473. else
  474. {
  475. const auto cmd = modifiers.isCommandDown();
  476. item.setSelected ((! cmd) || ! item.isSelected(), ! cmd);
  477. }
  478. }
  479. static TreeViewItem* getNextVisibleItem (TreeViewItem* item, bool forwards)
  480. {
  481. if (item == nullptr || item->ownerView == nullptr)
  482. return nullptr;
  483. auto* nextItem = item->ownerView->getItemOnRow (item->getRowNumberInTree() + (forwards ? 1 : -1));
  484. return nextItem == item->ownerView->rootItem && ! item->ownerView->rootItemVisible ? nullptr
  485. : nextItem;
  486. }
  487. std::vector<TreeViewItem*> getAllVisibleItems() const
  488. {
  489. if (owner.rootItem == nullptr)
  490. return {};
  491. const auto visibleTop = -getY();
  492. const auto visibleBottom = visibleTop + getParentHeight();
  493. std::vector<TreeViewItem*> visibleItems;
  494. auto* item = [&]
  495. {
  496. auto* i = owner.rootItemVisible ? owner.rootItem
  497. : owner.rootItem->subItems.getFirst();
  498. while (i != nullptr && i->y < visibleTop)
  499. i = getNextVisibleItem (i, true);
  500. return i;
  501. }();
  502. auto addOffscreenItemBuffer = [&visibleItems] (TreeViewItem* i, int num, bool forwards)
  503. {
  504. while (--num >= 0)
  505. {
  506. i = getNextVisibleItem (i, forwards);
  507. if (i == nullptr)
  508. return;
  509. visibleItems.push_back (i);
  510. }
  511. };
  512. addOffscreenItemBuffer (item, 2, false);
  513. while (item != nullptr && item->y < visibleBottom)
  514. {
  515. visibleItems.push_back (item);
  516. item = getNextVisibleItem (item, true);
  517. }
  518. if (item != nullptr)
  519. visibleItems.push_back (item);
  520. addOffscreenItemBuffer (item, 2, true);
  521. return visibleItems;
  522. }
  523. //==============================================================================
  524. TreeView& owner;
  525. std::vector<std::unique_ptr<ItemComponent>> itemComponents;
  526. ItemComponent* itemUnderMouse = nullptr;
  527. std::unique_ptr<ScopedDisableViewportScroll> scopedScrollDisabler;
  528. bool isDragging = false, needSelectionOnMouseUp = false;
  529. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ContentComponent)
  530. };
  531. //==============================================================================
  532. class TreeView::TreeViewport : public Viewport,
  533. private AsyncUpdater
  534. {
  535. public:
  536. explicit TreeViewport (TreeView& treeView) : owner (treeView) {}
  537. void visibleAreaChanged (const Rectangle<int>& newVisibleArea) override
  538. {
  539. const auto hasScrolledSideways = (newVisibleArea.getX() != lastX);
  540. lastX = newVisibleArea.getX();
  541. updateComponents (hasScrolledSideways);
  542. structureChanged = true;
  543. triggerAsyncUpdate();
  544. }
  545. bool keyPressed (const KeyPress& key) override
  546. {
  547. if (auto* tree = getParentComponent())
  548. if (tree->keyPressed (key))
  549. return true;
  550. return Viewport::keyPressed (key);
  551. }
  552. ContentComponent* getContentComp() const noexcept
  553. {
  554. return static_cast<ContentComponent*> (getViewedComponent());
  555. }
  556. enum class Async { yes, no };
  557. void recalculatePositions (Async useAsyncUpdate)
  558. {
  559. needsRecalculating = true;
  560. if (useAsyncUpdate == Async::yes)
  561. triggerAsyncUpdate();
  562. else
  563. handleAsyncUpdate();
  564. }
  565. private:
  566. std::unique_ptr<AccessibilityHandler> createAccessibilityHandler() override
  567. {
  568. return createIgnoredAccessibilityHandler (*this);
  569. }
  570. void handleAsyncUpdate() override
  571. {
  572. if (structureChanged)
  573. {
  574. if (auto* handler = owner.getAccessibilityHandler())
  575. handler->notifyAccessibilityEvent (AccessibilityEvent::structureChanged);
  576. structureChanged = false;
  577. }
  578. if (needsRecalculating)
  579. {
  580. if (auto* root = owner.rootItem)
  581. {
  582. const auto startY = owner.rootItemVisible ? 0 : -root->itemHeight;
  583. root->updatePositions (startY);
  584. getViewedComponent()->setSize (jmax (getMaximumVisibleWidth(), root->totalWidth + 50),
  585. root->totalHeight + startY);
  586. }
  587. else
  588. {
  589. getViewedComponent()->setSize (0, 0);
  590. }
  591. updateComponents (false);
  592. needsRecalculating = false;
  593. }
  594. }
  595. void updateComponents (bool triggerResize)
  596. {
  597. if (auto* content = getContentComp())
  598. {
  599. if (triggerResize)
  600. content->resized();
  601. else
  602. content->updateComponents();
  603. }
  604. repaint();
  605. }
  606. TreeView& owner;
  607. int lastX = -1;
  608. bool structureChanged = false, needsRecalculating = false;
  609. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TreeViewport)
  610. };
  611. //==============================================================================
  612. TreeView::TreeView (const String& name) : Component (name)
  613. {
  614. viewport = std::make_unique<TreeViewport> (*this);
  615. addAndMakeVisible (viewport.get());
  616. viewport->setViewedComponent (new ContentComponent (*this));
  617. setWantsKeyboardFocus (true);
  618. setFocusContainerType (FocusContainerType::focusContainer);
  619. }
  620. TreeView::~TreeView()
  621. {
  622. if (rootItem != nullptr)
  623. rootItem->setOwnerView (nullptr);
  624. }
  625. void TreeView::setRootItem (TreeViewItem* const newRootItem)
  626. {
  627. if (rootItem != newRootItem)
  628. {
  629. if (newRootItem != nullptr)
  630. {
  631. // can't use a tree item in more than one tree at once..
  632. jassert (newRootItem->ownerView == nullptr);
  633. if (newRootItem->ownerView != nullptr)
  634. newRootItem->ownerView->setRootItem (nullptr);
  635. }
  636. if (rootItem != nullptr)
  637. rootItem->setOwnerView (nullptr);
  638. rootItem = newRootItem;
  639. if (newRootItem != nullptr)
  640. newRootItem->setOwnerView (this);
  641. if (rootItem != nullptr && (defaultOpenness || ! rootItemVisible))
  642. {
  643. rootItem->setOpen (false); // force a re-open
  644. rootItem->setOpen (true);
  645. }
  646. viewport->recalculatePositions (TreeViewport::Async::no);
  647. }
  648. }
  649. void TreeView::deleteRootItem()
  650. {
  651. const std::unique_ptr<TreeViewItem> deleter (rootItem);
  652. setRootItem (nullptr);
  653. }
  654. void TreeView::setRootItemVisible (const bool shouldBeVisible)
  655. {
  656. rootItemVisible = shouldBeVisible;
  657. if (rootItem != nullptr && (defaultOpenness || ! rootItemVisible))
  658. {
  659. rootItem->setOpen (false); // force a re-open
  660. rootItem->setOpen (true);
  661. }
  662. updateVisibleItems();
  663. }
  664. void TreeView::colourChanged()
  665. {
  666. setOpaque (findColour (backgroundColourId).isOpaque());
  667. repaint();
  668. }
  669. void TreeView::setIndentSize (const int newIndentSize)
  670. {
  671. if (indentSize != newIndentSize)
  672. {
  673. indentSize = newIndentSize;
  674. resized();
  675. }
  676. }
  677. int TreeView::getIndentSize() noexcept
  678. {
  679. return indentSize >= 0 ? indentSize
  680. : getLookAndFeel().getTreeViewIndentSize (*this);
  681. }
  682. void TreeView::setDefaultOpenness (const bool isOpenByDefault)
  683. {
  684. if (defaultOpenness != isOpenByDefault)
  685. {
  686. defaultOpenness = isOpenByDefault;
  687. updateVisibleItems();
  688. }
  689. }
  690. void TreeView::setMultiSelectEnabled (const bool canMultiSelect)
  691. {
  692. multiSelectEnabled = canMultiSelect;
  693. }
  694. void TreeView::setOpenCloseButtonsVisible (const bool shouldBeVisible)
  695. {
  696. if (openCloseButtonsVisible != shouldBeVisible)
  697. {
  698. openCloseButtonsVisible = shouldBeVisible;
  699. updateVisibleItems();
  700. }
  701. }
  702. Viewport* TreeView::getViewport() const noexcept
  703. {
  704. return viewport.get();
  705. }
  706. //==============================================================================
  707. void TreeView::clearSelectedItems()
  708. {
  709. if (rootItem != nullptr)
  710. rootItem->deselectAllRecursively (nullptr);
  711. }
  712. int TreeView::getNumSelectedItems (int maximumDepthToSearchTo) const noexcept
  713. {
  714. return rootItem != nullptr ? rootItem->countSelectedItemsRecursively (maximumDepthToSearchTo) : 0;
  715. }
  716. TreeViewItem* TreeView::getSelectedItem (const int index) const noexcept
  717. {
  718. return rootItem != nullptr ? rootItem->getSelectedItemWithIndex (index) : nullptr;
  719. }
  720. int TreeView::getNumRowsInTree() const
  721. {
  722. return rootItem != nullptr ? (rootItem->getNumRows() - (rootItemVisible ? 0 : 1)) : 0;
  723. }
  724. TreeViewItem* TreeView::getItemOnRow (int index) const
  725. {
  726. if (! rootItemVisible)
  727. ++index;
  728. if (rootItem != nullptr && index >= 0)
  729. return rootItem->getItemOnRow (index);
  730. return nullptr;
  731. }
  732. TreeViewItem* TreeView::getItemAt (int y) const noexcept
  733. {
  734. if (auto* contentComp = viewport->getContentComp())
  735. if (auto* itemComponent = contentComp->getItemComponentAt (contentComp->getLocalPoint (this, Point<int> (0, y))))
  736. return &itemComponent->getRepresentedItem();
  737. return nullptr;
  738. }
  739. TreeViewItem* TreeView::findItemFromIdentifierString (const String& identifierString) const
  740. {
  741. if (rootItem == nullptr)
  742. return nullptr;
  743. return rootItem->findItemFromIdentifierString (identifierString);
  744. }
  745. Component* TreeView::getItemComponent (const TreeViewItem* item) const
  746. {
  747. return viewport->getContentComp()->getComponentForItem (item);
  748. }
  749. //==============================================================================
  750. static void addAllSelectedItemIds (TreeViewItem* item, XmlElement& parent)
  751. {
  752. if (item->isSelected())
  753. parent.createNewChildElement ("SELECTED")->setAttribute ("id", item->getItemIdentifierString());
  754. auto numSubItems = item->getNumSubItems();
  755. for (int i = 0; i < numSubItems; ++i)
  756. addAllSelectedItemIds (item->getSubItem (i), parent);
  757. }
  758. std::unique_ptr<XmlElement> TreeView::getOpennessState (bool alsoIncludeScrollPosition) const
  759. {
  760. if (rootItem != nullptr)
  761. {
  762. if (auto rootOpenness = rootItem->getOpennessState (false))
  763. {
  764. if (alsoIncludeScrollPosition)
  765. rootOpenness->setAttribute ("scrollPos", viewport->getViewPositionY());
  766. addAllSelectedItemIds (rootItem, *rootOpenness);
  767. return rootOpenness;
  768. }
  769. }
  770. return {};
  771. }
  772. void TreeView::restoreOpennessState (const XmlElement& newState, bool restoreStoredSelection)
  773. {
  774. if (rootItem != nullptr)
  775. {
  776. rootItem->restoreOpennessState (newState);
  777. if (newState.hasAttribute ("scrollPos"))
  778. viewport->setViewPosition (viewport->getViewPositionX(),
  779. newState.getIntAttribute ("scrollPos"));
  780. if (restoreStoredSelection)
  781. {
  782. clearSelectedItems();
  783. for (auto* e : newState.getChildWithTagNameIterator ("SELECTED"))
  784. if (auto* item = rootItem->findItemFromIdentifierString (e->getStringAttribute ("id")))
  785. item->setSelected (true, false);
  786. }
  787. updateVisibleItems();
  788. }
  789. }
  790. //==============================================================================
  791. void TreeView::paint (Graphics& g)
  792. {
  793. g.fillAll (findColour (backgroundColourId));
  794. }
  795. void TreeView::resized()
  796. {
  797. viewport->setBounds (getLocalBounds());
  798. updateVisibleItems();
  799. }
  800. void TreeView::enablementChanged()
  801. {
  802. repaint();
  803. }
  804. void TreeView::moveSelectedRow (int delta)
  805. {
  806. auto numRowsInTree = getNumRowsInTree();
  807. if (numRowsInTree > 0)
  808. {
  809. int rowSelected = 0;
  810. if (auto* firstSelected = getSelectedItem (0))
  811. rowSelected = firstSelected->getRowNumberInTree();
  812. rowSelected = jlimit (0, numRowsInTree - 1, rowSelected + delta);
  813. for (;;)
  814. {
  815. if (auto* item = getItemOnRow (rowSelected))
  816. {
  817. if (! item->canBeSelected())
  818. {
  819. // if the row we want to highlight doesn't allow it, try skipping
  820. // to the next item..
  821. auto nextRowToTry = jlimit (0, numRowsInTree - 1, rowSelected + (delta < 0 ? -1 : 1));
  822. if (rowSelected != nextRowToTry)
  823. {
  824. rowSelected = nextRowToTry;
  825. continue;
  826. }
  827. break;
  828. }
  829. item->setSelected (true, true);
  830. scrollToKeepItemVisible (item);
  831. }
  832. break;
  833. }
  834. }
  835. }
  836. void TreeView::scrollToKeepItemVisible (TreeViewItem* item)
  837. {
  838. if (item != nullptr && item->ownerView == this)
  839. {
  840. updateVisibleItems();
  841. item = item->getDeepestOpenParentItem();
  842. auto y = item->y;
  843. auto viewTop = viewport->getViewPositionY();
  844. if (y < viewTop)
  845. {
  846. viewport->setViewPosition (viewport->getViewPositionX(), y);
  847. }
  848. else if (y + item->itemHeight > viewTop + viewport->getViewHeight())
  849. {
  850. viewport->setViewPosition (viewport->getViewPositionX(),
  851. (y + item->itemHeight) - viewport->getViewHeight());
  852. }
  853. }
  854. }
  855. bool TreeView::toggleOpenSelectedItem()
  856. {
  857. if (auto* firstSelected = getSelectedItem (0))
  858. {
  859. if (firstSelected->mightContainSubItems())
  860. {
  861. firstSelected->setOpen (! firstSelected->isOpen());
  862. return true;
  863. }
  864. }
  865. return false;
  866. }
  867. void TreeView::moveOutOfSelectedItem()
  868. {
  869. if (auto* firstSelected = getSelectedItem (0))
  870. {
  871. if (firstSelected->isOpen())
  872. {
  873. firstSelected->setOpen (false);
  874. }
  875. else
  876. {
  877. auto* parent = firstSelected->parentItem;
  878. if ((! rootItemVisible) && parent == rootItem)
  879. parent = nullptr;
  880. if (parent != nullptr)
  881. {
  882. parent->setSelected (true, true);
  883. scrollToKeepItemVisible (parent);
  884. }
  885. }
  886. }
  887. }
  888. void TreeView::moveIntoSelectedItem()
  889. {
  890. if (auto* firstSelected = getSelectedItem (0))
  891. {
  892. if (firstSelected->isOpen() || ! firstSelected->mightContainSubItems())
  893. moveSelectedRow (1);
  894. else
  895. firstSelected->setOpen (true);
  896. }
  897. }
  898. void TreeView::moveByPages (int numPages)
  899. {
  900. if (auto* currentItem = getSelectedItem (0))
  901. {
  902. auto pos = currentItem->getItemPosition (false);
  903. auto targetY = pos.getY() + numPages * (getHeight() - pos.getHeight());
  904. auto currentRow = currentItem->getRowNumberInTree();
  905. for (;;)
  906. {
  907. moveSelectedRow (numPages);
  908. currentItem = getSelectedItem (0);
  909. if (currentItem == nullptr)
  910. break;
  911. auto y = currentItem->getItemPosition (false).getY();
  912. if ((numPages < 0 && y <= targetY) || (numPages > 0 && y >= targetY))
  913. break;
  914. auto newRow = currentItem->getRowNumberInTree();
  915. if (newRow == currentRow)
  916. break;
  917. currentRow = newRow;
  918. }
  919. }
  920. }
  921. bool TreeView::keyPressed (const KeyPress& key)
  922. {
  923. if (rootItem != nullptr)
  924. {
  925. if (key == KeyPress::upKey) { moveSelectedRow (-1); return true; }
  926. if (key == KeyPress::downKey) { moveSelectedRow (1); return true; }
  927. if (key == KeyPress::homeKey) { moveSelectedRow (-0x3fffffff); return true; }
  928. if (key == KeyPress::endKey) { moveSelectedRow (0x3fffffff); return true; }
  929. if (key == KeyPress::pageUpKey) { moveByPages (-1); return true; }
  930. if (key == KeyPress::pageDownKey) { moveByPages (1); return true; }
  931. if (key == KeyPress::returnKey) { return toggleOpenSelectedItem(); }
  932. if (key == KeyPress::leftKey) { moveOutOfSelectedItem(); return true; }
  933. if (key == KeyPress::rightKey) { moveIntoSelectedItem(); return true; }
  934. }
  935. return false;
  936. }
  937. void TreeView::updateVisibleItems()
  938. {
  939. viewport->recalculatePositions (TreeViewport::Async::yes);
  940. }
  941. //==============================================================================
  942. struct TreeView::InsertPoint
  943. {
  944. InsertPoint (TreeView& view, const StringArray& files,
  945. const DragAndDropTarget::SourceDetails& dragSourceDetails)
  946. : pos (dragSourceDetails.localPosition),
  947. item (view.getItemAt (dragSourceDetails.localPosition.y))
  948. {
  949. if (item != nullptr)
  950. {
  951. auto itemPos = item->getItemPosition (true);
  952. insertIndex = item->getIndexInParent();
  953. auto oldY = pos.y;
  954. pos.y = itemPos.getY();
  955. if (item->getNumSubItems() == 0 || ! item->isOpen())
  956. {
  957. if (files.size() > 0 ? item->isInterestedInFileDrag (files)
  958. : item->isInterestedInDragSource (dragSourceDetails))
  959. {
  960. // Check if we're trying to drag into an empty group item..
  961. if (oldY > itemPos.getY() + itemPos.getHeight() / 4
  962. && oldY < itemPos.getBottom() - itemPos.getHeight() / 4)
  963. {
  964. insertIndex = 0;
  965. pos.x = itemPos.getX() + view.getIndentSize();
  966. pos.y = itemPos.getBottom();
  967. return;
  968. }
  969. }
  970. }
  971. if (oldY > itemPos.getCentreY())
  972. {
  973. pos.y += item->getItemHeight();
  974. while (item->isLastOfSiblings() && item->getParentItem() != nullptr
  975. && item->getParentItem()->getParentItem() != nullptr)
  976. {
  977. if (pos.x > itemPos.getX())
  978. break;
  979. item = item->getParentItem();
  980. itemPos = item->getItemPosition (true);
  981. insertIndex = item->getIndexInParent();
  982. }
  983. ++insertIndex;
  984. }
  985. pos.x = itemPos.getX();
  986. item = item->getParentItem();
  987. }
  988. else if (auto* root = view.getRootItem())
  989. {
  990. // If they're dragging beyond the bottom of the list, then insert at the end of the root item..
  991. item = root;
  992. insertIndex = root->getNumSubItems();
  993. pos = root->getItemPosition (true).getBottomLeft();
  994. pos.x += view.getIndentSize();
  995. }
  996. }
  997. Point<int> pos;
  998. TreeViewItem* item;
  999. int insertIndex = 0;
  1000. };
  1001. //==============================================================================
  1002. class TreeView::InsertPointHighlight : public Component
  1003. {
  1004. public:
  1005. InsertPointHighlight()
  1006. {
  1007. setSize (100, 12);
  1008. setAlwaysOnTop (true);
  1009. setInterceptsMouseClicks (false, false);
  1010. }
  1011. void setTargetPosition (const InsertPoint& insertPos, const int width) noexcept
  1012. {
  1013. lastItem = insertPos.item;
  1014. lastIndex = insertPos.insertIndex;
  1015. auto offset = getHeight() / 2;
  1016. setBounds (insertPos.pos.x - offset, insertPos.pos.y - offset,
  1017. width - (insertPos.pos.x - offset), getHeight());
  1018. }
  1019. void paint (Graphics& g) override
  1020. {
  1021. Path p;
  1022. auto h = (float) getHeight();
  1023. p.addEllipse (2.0f, 2.0f, h - 4.0f, h - 4.0f);
  1024. p.startNewSubPath (h - 2.0f, h / 2.0f);
  1025. p.lineTo ((float) getWidth(), h / 2.0f);
  1026. g.setColour (findColour (TreeView::dragAndDropIndicatorColourId, true));
  1027. g.strokePath (p, PathStrokeType (2.0f));
  1028. }
  1029. TreeViewItem* lastItem = nullptr;
  1030. int lastIndex = 0;
  1031. private:
  1032. JUCE_DECLARE_NON_COPYABLE (InsertPointHighlight)
  1033. };
  1034. //==============================================================================
  1035. class TreeView::TargetGroupHighlight : public Component
  1036. {
  1037. public:
  1038. TargetGroupHighlight()
  1039. {
  1040. setAlwaysOnTop (true);
  1041. setInterceptsMouseClicks (false, false);
  1042. }
  1043. void setTargetPosition (TreeViewItem* const item) noexcept
  1044. {
  1045. setBounds (item->getItemPosition (true)
  1046. .withHeight (item->getItemHeight()));
  1047. }
  1048. void paint (Graphics& g) override
  1049. {
  1050. g.setColour (findColour (TreeView::dragAndDropIndicatorColourId, true));
  1051. g.drawRoundedRectangle (1.0f, 1.0f, (float) getWidth() - 2.0f, (float) getHeight() - 2.0f, 3.0f, 2.0f);
  1052. }
  1053. private:
  1054. JUCE_DECLARE_NON_COPYABLE (TargetGroupHighlight)
  1055. };
  1056. //==============================================================================
  1057. void TreeView::showDragHighlight (const InsertPoint& insertPos) noexcept
  1058. {
  1059. beginDragAutoRepeat (100);
  1060. if (dragInsertPointHighlight == nullptr)
  1061. {
  1062. dragInsertPointHighlight = std::make_unique<InsertPointHighlight>();
  1063. dragTargetGroupHighlight = std::make_unique<TargetGroupHighlight>();
  1064. addAndMakeVisible (dragInsertPointHighlight.get());
  1065. addAndMakeVisible (dragTargetGroupHighlight.get());
  1066. }
  1067. dragInsertPointHighlight->setTargetPosition (insertPos, viewport->getViewWidth());
  1068. dragTargetGroupHighlight->setTargetPosition (insertPos.item);
  1069. }
  1070. void TreeView::hideDragHighlight() noexcept
  1071. {
  1072. dragInsertPointHighlight = nullptr;
  1073. dragTargetGroupHighlight = nullptr;
  1074. }
  1075. void TreeView::handleDrag (const StringArray& files, const SourceDetails& dragSourceDetails)
  1076. {
  1077. const auto scrolled = viewport->autoScroll (dragSourceDetails.localPosition.x,
  1078. dragSourceDetails.localPosition.y, 20, 10);
  1079. InsertPoint insertPos (*this, files, dragSourceDetails);
  1080. if (insertPos.item != nullptr)
  1081. {
  1082. if (scrolled || dragInsertPointHighlight == nullptr
  1083. || dragInsertPointHighlight->lastItem != insertPos.item
  1084. || dragInsertPointHighlight->lastIndex != insertPos.insertIndex)
  1085. {
  1086. if (files.size() > 0 ? insertPos.item->isInterestedInFileDrag (files)
  1087. : insertPos.item->isInterestedInDragSource (dragSourceDetails))
  1088. showDragHighlight (insertPos);
  1089. else
  1090. hideDragHighlight();
  1091. }
  1092. }
  1093. else
  1094. {
  1095. hideDragHighlight();
  1096. }
  1097. }
  1098. void TreeView::handleDrop (const StringArray& files, const SourceDetails& dragSourceDetails)
  1099. {
  1100. hideDragHighlight();
  1101. InsertPoint insertPos (*this, files, dragSourceDetails);
  1102. if (insertPos.item == nullptr)
  1103. insertPos.item = rootItem;
  1104. if (insertPos.item != nullptr)
  1105. {
  1106. if (files.size() > 0)
  1107. {
  1108. if (insertPos.item->isInterestedInFileDrag (files))
  1109. insertPos.item->filesDropped (files, insertPos.insertIndex);
  1110. }
  1111. else
  1112. {
  1113. if (insertPos.item->isInterestedInDragSource (dragSourceDetails))
  1114. insertPos.item->itemDropped (dragSourceDetails, insertPos.insertIndex);
  1115. }
  1116. }
  1117. }
  1118. //==============================================================================
  1119. bool TreeView::isInterestedInFileDrag (const StringArray&)
  1120. {
  1121. return true;
  1122. }
  1123. void TreeView::fileDragEnter (const StringArray& files, int x, int y)
  1124. {
  1125. fileDragMove (files, x, y);
  1126. }
  1127. void TreeView::fileDragMove (const StringArray& files, int x, int y)
  1128. {
  1129. handleDrag (files, SourceDetails (var(), this, { x, y }));
  1130. }
  1131. void TreeView::fileDragExit (const StringArray&)
  1132. {
  1133. hideDragHighlight();
  1134. }
  1135. void TreeView::filesDropped (const StringArray& files, int x, int y)
  1136. {
  1137. handleDrop (files, SourceDetails (var(), this, { x, y }));
  1138. }
  1139. bool TreeView::isInterestedInDragSource (const SourceDetails& /*dragSourceDetails*/)
  1140. {
  1141. return true;
  1142. }
  1143. void TreeView::itemDragEnter (const SourceDetails& dragSourceDetails)
  1144. {
  1145. itemDragMove (dragSourceDetails);
  1146. }
  1147. void TreeView::itemDragMove (const SourceDetails& dragSourceDetails)
  1148. {
  1149. handleDrag (StringArray(), dragSourceDetails);
  1150. }
  1151. void TreeView::itemDragExit (const SourceDetails& /*dragSourceDetails*/)
  1152. {
  1153. hideDragHighlight();
  1154. }
  1155. void TreeView::itemDropped (const SourceDetails& dragSourceDetails)
  1156. {
  1157. handleDrop (StringArray(), dragSourceDetails);
  1158. }
  1159. //==============================================================================
  1160. std::unique_ptr<AccessibilityHandler> TreeView::createAccessibilityHandler()
  1161. {
  1162. class TableInterface : public AccessibilityTableInterface
  1163. {
  1164. public:
  1165. explicit TableInterface (TreeView& treeViewToWrap) : treeView (treeViewToWrap) {}
  1166. int getNumRows() const override { return treeView.getNumRowsInTree(); }
  1167. int getNumColumns() const override { return 1; }
  1168. const AccessibilityHandler* getCellHandler (int row, int) const override
  1169. {
  1170. if (auto* itemComp = treeView.getItemComponent (treeView.getItemOnRow (row)))
  1171. return itemComp->getAccessibilityHandler();
  1172. return nullptr;
  1173. }
  1174. private:
  1175. TreeView& treeView;
  1176. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TableInterface)
  1177. };
  1178. return std::make_unique<AccessibilityHandler> (*this,
  1179. AccessibilityRole::tree,
  1180. AccessibilityActions{},
  1181. AccessibilityHandler::Interfaces { std::make_unique<TableInterface> (*this) });
  1182. }
  1183. //==============================================================================
  1184. TreeViewItem::TreeViewItem()
  1185. {
  1186. static int nextUID = 0;
  1187. uid = nextUID++;
  1188. }
  1189. TreeViewItem::~TreeViewItem()
  1190. {
  1191. if (ownerView != nullptr)
  1192. ownerView->viewport->getContentComp()->itemBeingDeleted (this);
  1193. }
  1194. String TreeViewItem::getUniqueName() const
  1195. {
  1196. return {};
  1197. }
  1198. void TreeViewItem::itemOpennessChanged (bool)
  1199. {
  1200. }
  1201. int TreeViewItem::getNumSubItems() const noexcept
  1202. {
  1203. return subItems.size();
  1204. }
  1205. TreeViewItem* TreeViewItem::getSubItem (const int index) const noexcept
  1206. {
  1207. return subItems[index];
  1208. }
  1209. void TreeViewItem::clearSubItems()
  1210. {
  1211. if (ownerView != nullptr)
  1212. {
  1213. if (! subItems.isEmpty())
  1214. {
  1215. removeAllSubItemsFromList();
  1216. treeHasChanged();
  1217. }
  1218. }
  1219. else
  1220. {
  1221. removeAllSubItemsFromList();
  1222. }
  1223. }
  1224. void TreeViewItem::removeAllSubItemsFromList()
  1225. {
  1226. for (int i = subItems.size(); --i >= 0;)
  1227. removeSubItemFromList (i, true);
  1228. }
  1229. void TreeViewItem::addSubItem (TreeViewItem* const newItem, const int insertPosition)
  1230. {
  1231. if (newItem != nullptr)
  1232. {
  1233. newItem->parentItem = nullptr;
  1234. newItem->setOwnerView (ownerView);
  1235. newItem->y = 0;
  1236. newItem->itemHeight = newItem->getItemHeight();
  1237. newItem->totalHeight = 0;
  1238. newItem->itemWidth = newItem->getItemWidth();
  1239. newItem->totalWidth = 0;
  1240. newItem->parentItem = this;
  1241. if (ownerView != nullptr)
  1242. {
  1243. subItems.insert (insertPosition, newItem);
  1244. treeHasChanged();
  1245. if (newItem->isOpen())
  1246. newItem->itemOpennessChanged (true);
  1247. }
  1248. else
  1249. {
  1250. subItems.insert (insertPosition, newItem);
  1251. if (newItem->isOpen())
  1252. newItem->itemOpennessChanged (true);
  1253. }
  1254. }
  1255. }
  1256. void TreeViewItem::removeSubItem (int index, bool deleteItem)
  1257. {
  1258. if (ownerView != nullptr)
  1259. {
  1260. if (removeSubItemFromList (index, deleteItem))
  1261. treeHasChanged();
  1262. }
  1263. else
  1264. {
  1265. removeSubItemFromList (index, deleteItem);
  1266. }
  1267. }
  1268. bool TreeViewItem::removeSubItemFromList (int index, bool deleteItem)
  1269. {
  1270. if (auto* child = subItems[index])
  1271. {
  1272. child->parentItem = nullptr;
  1273. subItems.remove (index, deleteItem);
  1274. return true;
  1275. }
  1276. return false;
  1277. }
  1278. TreeViewItem::Openness TreeViewItem::getOpenness() const noexcept
  1279. {
  1280. return openness;
  1281. }
  1282. void TreeViewItem::setOpenness (Openness newOpenness)
  1283. {
  1284. auto wasOpen = isOpen();
  1285. openness = newOpenness;
  1286. auto isNowOpen = isOpen();
  1287. if (isNowOpen != wasOpen)
  1288. {
  1289. treeHasChanged();
  1290. itemOpennessChanged (isNowOpen);
  1291. }
  1292. }
  1293. bool TreeViewItem::isOpen() const noexcept
  1294. {
  1295. if (openness == Openness::opennessDefault)
  1296. return ownerView != nullptr && ownerView->defaultOpenness;
  1297. return openness == Openness::opennessOpen;
  1298. }
  1299. void TreeViewItem::setOpen (const bool shouldBeOpen)
  1300. {
  1301. if (isOpen() != shouldBeOpen)
  1302. setOpenness (shouldBeOpen ? Openness::opennessOpen
  1303. : Openness::opennessClosed);
  1304. }
  1305. bool TreeViewItem::isFullyOpen() const noexcept
  1306. {
  1307. if (! isOpen())
  1308. return false;
  1309. for (auto* i : subItems)
  1310. if (! i->isFullyOpen())
  1311. return false;
  1312. return true;
  1313. }
  1314. void TreeViewItem::restoreToDefaultOpenness()
  1315. {
  1316. setOpenness (Openness::opennessDefault);
  1317. }
  1318. bool TreeViewItem::isSelected() const noexcept
  1319. {
  1320. return selected;
  1321. }
  1322. void TreeViewItem::deselectAllRecursively (TreeViewItem* itemToIgnore)
  1323. {
  1324. if (this != itemToIgnore)
  1325. setSelected (false, false);
  1326. for (auto* i : subItems)
  1327. i->deselectAllRecursively (itemToIgnore);
  1328. }
  1329. void TreeViewItem::setSelected (const bool shouldBeSelected,
  1330. const bool deselectOtherItemsFirst,
  1331. const NotificationType notify)
  1332. {
  1333. if (shouldBeSelected && ! canBeSelected())
  1334. return;
  1335. if (deselectOtherItemsFirst)
  1336. getTopLevelItem()->deselectAllRecursively (this);
  1337. if (shouldBeSelected != selected)
  1338. {
  1339. selected = shouldBeSelected;
  1340. if (ownerView != nullptr)
  1341. {
  1342. ownerView->repaint();
  1343. if (selected)
  1344. {
  1345. if (auto* itemComponent = ownerView->getItemComponent (this))
  1346. if (auto* itemHandler = itemComponent->getAccessibilityHandler())
  1347. itemHandler->grabFocus();
  1348. }
  1349. if (auto* handler = ownerView->getAccessibilityHandler())
  1350. handler->notifyAccessibilityEvent (AccessibilityEvent::rowSelectionChanged);
  1351. }
  1352. if (notify != dontSendNotification)
  1353. itemSelectionChanged (shouldBeSelected);
  1354. }
  1355. }
  1356. void TreeViewItem::paintItem (Graphics&, int, int)
  1357. {
  1358. }
  1359. void TreeViewItem::paintOpenCloseButton (Graphics& g, const Rectangle<float>& area, Colour backgroundColour, bool isMouseOver)
  1360. {
  1361. getOwnerView()->getLookAndFeel()
  1362. .drawTreeviewPlusMinusBox (g, area, backgroundColour, isOpen(), isMouseOver);
  1363. }
  1364. void TreeViewItem::paintHorizontalConnectingLine (Graphics& g, const Line<float>& line)
  1365. {
  1366. g.setColour (ownerView->findColour (TreeView::linesColourId));
  1367. g.drawLine (line);
  1368. }
  1369. void TreeViewItem::paintVerticalConnectingLine (Graphics& g, const Line<float>& line)
  1370. {
  1371. g.setColour (ownerView->findColour (TreeView::linesColourId));
  1372. g.drawLine (line);
  1373. }
  1374. void TreeViewItem::itemClicked (const MouseEvent&)
  1375. {
  1376. }
  1377. void TreeViewItem::itemDoubleClicked (const MouseEvent&)
  1378. {
  1379. if (mightContainSubItems())
  1380. setOpen (! isOpen());
  1381. }
  1382. void TreeViewItem::itemSelectionChanged (bool)
  1383. {
  1384. }
  1385. String TreeViewItem::getTooltip()
  1386. {
  1387. return {};
  1388. }
  1389. String TreeViewItem::getAccessibilityName()
  1390. {
  1391. auto tooltipString = getTooltip();
  1392. return tooltipString.isNotEmpty()
  1393. ? tooltipString
  1394. : "Level " + String (getItemDepth (this)) + " row " + String (getIndexInParent());
  1395. }
  1396. void TreeViewItem::ownerViewChanged (TreeView*)
  1397. {
  1398. }
  1399. var TreeViewItem::getDragSourceDescription()
  1400. {
  1401. return {};
  1402. }
  1403. bool TreeViewItem::isInterestedInFileDrag (const StringArray&)
  1404. {
  1405. return false;
  1406. }
  1407. void TreeViewItem::filesDropped (const StringArray& /*files*/, int /*insertIndex*/)
  1408. {
  1409. }
  1410. bool TreeViewItem::isInterestedInDragSource (const DragAndDropTarget::SourceDetails& /*dragSourceDetails*/)
  1411. {
  1412. return false;
  1413. }
  1414. void TreeViewItem::itemDropped (const DragAndDropTarget::SourceDetails& /*dragSourceDetails*/, int /*insertIndex*/)
  1415. {
  1416. }
  1417. Rectangle<int> TreeViewItem::getItemPosition (const bool relativeToTreeViewTopLeft) const noexcept
  1418. {
  1419. auto indentX = getIndentX();
  1420. auto width = itemWidth;
  1421. if (ownerView != nullptr && width < 0)
  1422. width = ownerView->viewport->getViewWidth() - indentX;
  1423. Rectangle<int> r (indentX, y, jmax (0, width), totalHeight);
  1424. if (relativeToTreeViewTopLeft && ownerView != nullptr)
  1425. r -= ownerView->viewport->getViewPosition();
  1426. return r;
  1427. }
  1428. void TreeViewItem::treeHasChanged() const noexcept
  1429. {
  1430. if (ownerView != nullptr)
  1431. ownerView->updateVisibleItems();
  1432. }
  1433. void TreeViewItem::repaintItem() const
  1434. {
  1435. if (ownerView != nullptr && areAllParentsOpen())
  1436. if (auto* component = ownerView->getItemComponent (this))
  1437. component->repaint();
  1438. }
  1439. bool TreeViewItem::areAllParentsOpen() const noexcept
  1440. {
  1441. return parentItem == nullptr
  1442. || (parentItem->isOpen() && parentItem->areAllParentsOpen());
  1443. }
  1444. void TreeViewItem::updatePositions (int newY)
  1445. {
  1446. y = newY;
  1447. itemHeight = getItemHeight();
  1448. totalHeight = itemHeight;
  1449. itemWidth = getItemWidth();
  1450. totalWidth = jmax (itemWidth, 0) + getIndentX();
  1451. if (isOpen())
  1452. {
  1453. newY += totalHeight;
  1454. for (auto* i : subItems)
  1455. {
  1456. i->updatePositions (newY);
  1457. newY += i->totalHeight;
  1458. totalHeight += i->totalHeight;
  1459. totalWidth = jmax (totalWidth, i->totalWidth);
  1460. }
  1461. }
  1462. }
  1463. TreeViewItem* TreeViewItem::getDeepestOpenParentItem() noexcept
  1464. {
  1465. auto* result = this;
  1466. auto* item = this;
  1467. while (item->parentItem != nullptr)
  1468. {
  1469. item = item->parentItem;
  1470. if (! item->isOpen())
  1471. result = item;
  1472. }
  1473. return result;
  1474. }
  1475. void TreeViewItem::setOwnerView (TreeView* const newOwner) noexcept
  1476. {
  1477. ownerView = newOwner;
  1478. for (auto* i : subItems)
  1479. {
  1480. i->setOwnerView (newOwner);
  1481. i->ownerViewChanged (newOwner);
  1482. }
  1483. }
  1484. int TreeViewItem::getIndentX() const noexcept
  1485. {
  1486. if (ownerView == nullptr)
  1487. return 0;
  1488. int x = ownerView->rootItemVisible ? 1 : 0;
  1489. if (! ownerView->openCloseButtonsVisible)
  1490. --x;
  1491. for (auto* p = parentItem; p != nullptr; p = p->parentItem)
  1492. ++x;
  1493. return x * ownerView->getIndentSize();
  1494. }
  1495. void TreeViewItem::setDrawsInLeftMargin (bool canDrawInLeftMargin) noexcept
  1496. {
  1497. drawsInLeftMargin = canDrawInLeftMargin;
  1498. }
  1499. void TreeViewItem::setDrawsInRightMargin (bool canDrawInRightMargin) noexcept
  1500. {
  1501. drawsInRightMargin = canDrawInRightMargin;
  1502. }
  1503. bool TreeViewItem::areLinesDrawn() const
  1504. {
  1505. return drawLinesSet ? drawLinesInside
  1506. : (ownerView != nullptr && ownerView->getLookAndFeel().areLinesDrawnForTreeView (*ownerView));
  1507. }
  1508. bool TreeViewItem::isLastOfSiblings() const noexcept
  1509. {
  1510. return parentItem == nullptr
  1511. || parentItem->subItems.getLast() == this;
  1512. }
  1513. int TreeViewItem::getIndexInParent() const noexcept
  1514. {
  1515. return parentItem == nullptr ? 0
  1516. : parentItem->subItems.indexOf (this);
  1517. }
  1518. TreeViewItem* TreeViewItem::getTopLevelItem() noexcept
  1519. {
  1520. return parentItem == nullptr ? this
  1521. : parentItem->getTopLevelItem();
  1522. }
  1523. int TreeViewItem::getNumRows() const noexcept
  1524. {
  1525. int num = 1;
  1526. if (isOpen())
  1527. for (auto* i : subItems)
  1528. num += i->getNumRows();
  1529. return num;
  1530. }
  1531. TreeViewItem* TreeViewItem::getItemOnRow (int index) noexcept
  1532. {
  1533. if (index == 0)
  1534. return this;
  1535. if (index > 0 && isOpen())
  1536. {
  1537. --index;
  1538. for (auto* i : subItems)
  1539. {
  1540. if (index == 0)
  1541. return i;
  1542. auto numRows = i->getNumRows();
  1543. if (numRows > index)
  1544. return i->getItemOnRow (index);
  1545. index -= numRows;
  1546. }
  1547. }
  1548. return nullptr;
  1549. }
  1550. int TreeViewItem::countSelectedItemsRecursively (int depth) const noexcept
  1551. {
  1552. int total = isSelected() ? 1 : 0;
  1553. if (depth != 0)
  1554. for (auto* i : subItems)
  1555. total += i->countSelectedItemsRecursively (depth - 1);
  1556. return total;
  1557. }
  1558. TreeViewItem* TreeViewItem::getSelectedItemWithIndex (int index) noexcept
  1559. {
  1560. if (isSelected())
  1561. {
  1562. if (index == 0)
  1563. return this;
  1564. --index;
  1565. }
  1566. if (index >= 0)
  1567. {
  1568. for (auto* i : subItems)
  1569. {
  1570. if (auto* found = i->getSelectedItemWithIndex (index))
  1571. return found;
  1572. index -= i->countSelectedItemsRecursively (-1);
  1573. }
  1574. }
  1575. return nullptr;
  1576. }
  1577. int TreeViewItem::getRowNumberInTree() const noexcept
  1578. {
  1579. if (parentItem != nullptr && ownerView != nullptr)
  1580. {
  1581. if (! parentItem->isOpen())
  1582. return parentItem->getRowNumberInTree();
  1583. auto n = 1 + parentItem->getRowNumberInTree();
  1584. auto ourIndex = parentItem->subItems.indexOf (this);
  1585. jassert (ourIndex >= 0);
  1586. while (--ourIndex >= 0)
  1587. n += parentItem->subItems [ourIndex]->getNumRows();
  1588. if (parentItem->parentItem == nullptr
  1589. && ! ownerView->rootItemVisible)
  1590. --n;
  1591. return n;
  1592. }
  1593. return 0;
  1594. }
  1595. void TreeViewItem::setLinesDrawnForSubItems (bool drawLines) noexcept
  1596. {
  1597. drawLinesInside = drawLines;
  1598. drawLinesSet = true;
  1599. }
  1600. static String escapeSlashesInTreeViewItemName (const String& s)
  1601. {
  1602. return s.replaceCharacter ('/', '\\');
  1603. }
  1604. String TreeViewItem::getItemIdentifierString() const
  1605. {
  1606. String s;
  1607. if (parentItem != nullptr)
  1608. s = parentItem->getItemIdentifierString();
  1609. return s + "/" + escapeSlashesInTreeViewItemName (getUniqueName());
  1610. }
  1611. TreeViewItem* TreeViewItem::findItemFromIdentifierString (const String& identifierString)
  1612. {
  1613. auto thisId = "/" + escapeSlashesInTreeViewItemName (getUniqueName());
  1614. if (thisId == identifierString)
  1615. return this;
  1616. if (identifierString.startsWith (thisId + "/"))
  1617. {
  1618. auto remainingPath = identifierString.substring (thisId.length());
  1619. const auto wasOpen = isOpen();
  1620. setOpen (true);
  1621. for (auto* i : subItems)
  1622. if (auto* item = i->findItemFromIdentifierString (remainingPath))
  1623. return item;
  1624. setOpen (wasOpen);
  1625. }
  1626. return nullptr;
  1627. }
  1628. void TreeViewItem::restoreOpennessState (const XmlElement& e)
  1629. {
  1630. if (e.hasTagName ("CLOSED"))
  1631. {
  1632. setOpen (false);
  1633. }
  1634. else if (e.hasTagName ("OPEN"))
  1635. {
  1636. setOpen (true);
  1637. Array<TreeViewItem*> items;
  1638. items.addArray (subItems);
  1639. for (auto* n : e.getChildIterator())
  1640. {
  1641. auto id = n->getStringAttribute ("id");
  1642. for (int i = 0; i < items.size(); ++i)
  1643. {
  1644. auto* ti = items.getUnchecked (i);
  1645. if (ti->getUniqueName() == id)
  1646. {
  1647. ti->restoreOpennessState (*n);
  1648. items.remove (i);
  1649. break;
  1650. }
  1651. }
  1652. }
  1653. // for any items that weren't mentioned in the XML, reset them to default:
  1654. for (auto* i : items)
  1655. i->restoreToDefaultOpenness();
  1656. }
  1657. }
  1658. std::unique_ptr<XmlElement> TreeViewItem::getOpennessState() const
  1659. {
  1660. return getOpennessState (true);
  1661. }
  1662. std::unique_ptr<XmlElement> TreeViewItem::getOpennessState (bool canReturnNull) const
  1663. {
  1664. auto name = getUniqueName();
  1665. if (name.isNotEmpty())
  1666. {
  1667. std::unique_ptr<XmlElement> e;
  1668. if (isOpen())
  1669. {
  1670. if (canReturnNull && ownerView != nullptr && ownerView->defaultOpenness && isFullyOpen())
  1671. return nullptr;
  1672. e = std::make_unique<XmlElement> ("OPEN");
  1673. for (int i = subItems.size(); --i >= 0;)
  1674. e->prependChildElement (subItems.getUnchecked (i)->getOpennessState (true).release());
  1675. }
  1676. else
  1677. {
  1678. if (canReturnNull && ownerView != nullptr && ! ownerView->defaultOpenness)
  1679. return nullptr;
  1680. e = std::make_unique<XmlElement> ("CLOSED");
  1681. }
  1682. e->setAttribute ("id", name);
  1683. return e;
  1684. }
  1685. // trying to save the openness for an element that has no name - this won't
  1686. // work because it needs the names to identify what to open.
  1687. jassertfalse;
  1688. return {};
  1689. }
  1690. //==============================================================================
  1691. TreeViewItem::OpennessRestorer::OpennessRestorer (TreeViewItem& item)
  1692. : treeViewItem (item),
  1693. oldOpenness (item.getOpennessState())
  1694. {
  1695. }
  1696. TreeViewItem::OpennessRestorer::~OpennessRestorer()
  1697. {
  1698. if (oldOpenness != nullptr)
  1699. treeViewItem.restoreOpennessState (*oldOpenness);
  1700. }
  1701. void TreeViewItem::draw (Graphics& g, int width, bool isMouseOverButton)
  1702. {
  1703. if (ownerView == nullptr)
  1704. return;
  1705. const auto indent = getIndentX();
  1706. const auto itemW = (itemWidth < 0 || drawsInRightMargin) ? width - indent : itemWidth;
  1707. {
  1708. Graphics::ScopedSaveState ss (g);
  1709. g.setOrigin (indent, 0);
  1710. if (g.reduceClipRegion (drawsInLeftMargin ? -indent : 0, 0,
  1711. drawsInLeftMargin ? itemW + indent : itemW, itemHeight))
  1712. {
  1713. if (isSelected())
  1714. g.fillAll (ownerView->findColour (TreeView::selectedItemBackgroundColourId));
  1715. else
  1716. g.fillAll ((getRowNumberInTree() % 2 == 0) ? ownerView->findColour (TreeView::oddItemsColourId)
  1717. : ownerView->findColour (TreeView::evenItemsColourId));
  1718. paintItem (g, itemWidth < 0 ? width - indent : itemWidth, itemHeight);
  1719. }
  1720. }
  1721. const auto halfH = (float) itemHeight * 0.5f;
  1722. const auto indentWidth = ownerView->getIndentSize();
  1723. const auto depth = getItemDepth (this);
  1724. if (depth >= 0 && ownerView->openCloseButtonsVisible)
  1725. {
  1726. auto x = ((float) depth + 0.5f) * (float) indentWidth;
  1727. const auto parentLinesDrawn = parentItem != nullptr && parentItem->areLinesDrawn();
  1728. if (parentLinesDrawn)
  1729. paintVerticalConnectingLine (g, Line<float> (x, 0, x, isLastOfSiblings() ? halfH : (float) itemHeight));
  1730. if (parentLinesDrawn || (parentItem == nullptr && areLinesDrawn()))
  1731. paintHorizontalConnectingLine (g, Line<float> (x, halfH, x + (float) indentWidth * 0.5f, halfH));
  1732. {
  1733. auto* p = parentItem;
  1734. auto d = depth;
  1735. while (p != nullptr && --d >= 0)
  1736. {
  1737. x -= (float) indentWidth;
  1738. if ((p->parentItem == nullptr || p->parentItem->areLinesDrawn()) && ! p->isLastOfSiblings())
  1739. p->paintVerticalConnectingLine (g, Line<float> (x, 0, x, (float) itemHeight));
  1740. p = p->parentItem;
  1741. }
  1742. }
  1743. if (mightContainSubItems())
  1744. {
  1745. auto backgroundColour = ownerView->findColour (TreeView::backgroundColourId);
  1746. paintOpenCloseButton (g, Rectangle<float> ((float) (depth * indentWidth), 0, (float) indentWidth, (float) itemHeight),
  1747. backgroundColour.isTransparent() ? Colours::white : backgroundColour,
  1748. isMouseOverButton);
  1749. }
  1750. }
  1751. }
  1752. } // namespace juce