The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

245 lines
7.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 7 technical preview.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For the technical preview this file cannot be licensed commercially.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. #include "../../Application/jucer_Headers.h"
  14. #include "jucer_JucerTreeViewBase.h"
  15. #include "../../Project/UI/jucer_ProjectContentComponent.h"
  16. //==============================================================================
  17. void TreePanelBase::setRoot (std::unique_ptr<JucerTreeViewBase> root)
  18. {
  19. rootItem = std::move (root);
  20. tree.setRootItem (rootItem.get());
  21. tree.getRootItem()->setOpen (true);
  22. if (project != nullptr)
  23. {
  24. if (auto treeOpenness = project->getStoredProperties().getXmlValue (opennessStateKey))
  25. {
  26. tree.restoreOpennessState (*treeOpenness, true);
  27. for (int i = tree.getNumSelectedItems(); --i >= 0;)
  28. if (auto item = dynamic_cast<JucerTreeViewBase*> (tree.getSelectedItem (i)))
  29. item->cancelDelayedSelectionTimer();
  30. }
  31. }
  32. }
  33. void TreePanelBase::saveOpenness()
  34. {
  35. if (project != nullptr)
  36. {
  37. std::unique_ptr<XmlElement> opennessState (tree.getOpennessState (true));
  38. if (opennessState != nullptr)
  39. project->getStoredProperties().setValue (opennessStateKey, opennessState.get());
  40. else
  41. project->getStoredProperties().removeValue (opennessStateKey);
  42. }
  43. }
  44. //==============================================================================
  45. JucerTreeViewBase::JucerTreeViewBase()
  46. {
  47. setLinesDrawnForSubItems (false);
  48. setDrawsInLeftMargin (true);
  49. }
  50. void JucerTreeViewBase::refreshSubItems()
  51. {
  52. WholeTreeOpennessRestorer wtor (*this);
  53. clearSubItems();
  54. addSubItems();
  55. }
  56. Font JucerTreeViewBase::getFont() const
  57. {
  58. return Font ((float) getItemHeight() * 0.6f);
  59. }
  60. void JucerTreeViewBase::paintOpenCloseButton (Graphics& g, const Rectangle<float>& area, Colour /*backgroundColour*/, bool isMouseOver)
  61. {
  62. g.setColour (getOwnerView()->findColour (isSelected() ? defaultHighlightedTextColourId : treeIconColourId));
  63. TreeViewItem::paintOpenCloseButton (g, area, getOwnerView()->findColour (defaultIconColourId), isMouseOver);
  64. }
  65. void JucerTreeViewBase::paintIcon (Graphics& g, Rectangle<float> area)
  66. {
  67. g.setColour (getContentColour (true));
  68. getIcon().draw (g, area, isIconCrossedOut());
  69. textX = roundToInt (area.getRight()) + 7;
  70. }
  71. void JucerTreeViewBase::paintItem (Graphics& g, int width, int height)
  72. {
  73. ignoreUnused (width, height);
  74. auto bounds = g.getClipBounds().withY (0).withHeight (height).toFloat();
  75. g.setColour (getOwnerView()->findColour (treeIconColourId).withMultipliedAlpha (0.4f));
  76. g.fillRect (bounds.removeFromBottom (0.5f).reduced (5, 0));
  77. }
  78. Colour JucerTreeViewBase::getContentColour (bool isIcon) const
  79. {
  80. if (isMissing()) return Colours::red;
  81. if (isSelected()) return getOwnerView()->findColour (defaultHighlightedTextColourId);
  82. if (hasWarnings()) return getOwnerView()->findColour (defaultHighlightColourId);
  83. return getOwnerView()->findColour (isIcon ? treeIconColourId : defaultTextColourId);
  84. }
  85. void JucerTreeViewBase::paintContent (Graphics& g, Rectangle<int> area)
  86. {
  87. g.setFont (getFont());
  88. g.setColour (getContentColour (false));
  89. g.drawFittedText (getDisplayName(), area, Justification::centredLeft, 1, 1.0f);
  90. }
  91. std::unique_ptr<Component> JucerTreeViewBase::createItemComponent()
  92. {
  93. return std::make_unique<TreeItemComponent> (*this);
  94. }
  95. //==============================================================================
  96. class RenameTreeItemCallback : public ModalComponentManager::Callback
  97. {
  98. public:
  99. RenameTreeItemCallback (JucerTreeViewBase& ti, Component& parent, const Rectangle<int>& bounds)
  100. : item (ti)
  101. {
  102. ed.setMultiLine (false, false);
  103. ed.setPopupMenuEnabled (false);
  104. ed.setSelectAllWhenFocused (true);
  105. ed.setFont (item.getFont());
  106. ed.onReturnKey = [this] { ed.exitModalState (1); };
  107. ed.onEscapeKey = [this] { ed.exitModalState (0); };
  108. ed.onFocusLost = [this] { ed.exitModalState (0); };
  109. ed.setText (item.getRenamingName());
  110. ed.setBounds (bounds);
  111. parent.addAndMakeVisible (ed);
  112. ed.enterModalState (true, this);
  113. }
  114. void modalStateFinished (int resultCode) override
  115. {
  116. if (resultCode != 0)
  117. item.setName (ed.getText());
  118. }
  119. private:
  120. struct RenameEditor : public TextEditor
  121. {
  122. void inputAttemptWhenModal() override { exitModalState (0); }
  123. };
  124. RenameEditor ed;
  125. JucerTreeViewBase& item;
  126. JUCE_DECLARE_NON_COPYABLE (RenameTreeItemCallback)
  127. };
  128. void JucerTreeViewBase::showRenameBox()
  129. {
  130. Rectangle<int> r (getItemPosition (true));
  131. r.setLeft (r.getX() + textX);
  132. r.setHeight (getItemHeight());
  133. new RenameTreeItemCallback (*this, *getOwnerView(), r);
  134. }
  135. void JucerTreeViewBase::itemClicked (const MouseEvent& e)
  136. {
  137. if (e.mods.isPopupMenu())
  138. {
  139. if (getOwnerView()->getNumSelectedItems() > 1)
  140. showMultiSelectionPopupMenu (e.getMouseDownScreenPosition());
  141. else
  142. showPopupMenu (e.getMouseDownScreenPosition());
  143. }
  144. else if (isSelected())
  145. {
  146. itemSelectionChanged (true);
  147. }
  148. }
  149. static void treeViewMenuItemChosen (int resultCode, WeakReference<JucerTreeViewBase> item)
  150. {
  151. if (item != nullptr)
  152. item->handlePopupMenuResult (resultCode);
  153. }
  154. void JucerTreeViewBase::launchPopupMenu (PopupMenu& m, Point<int> p)
  155. {
  156. m.showMenuAsync (PopupMenu::Options().withTargetScreenArea ({ p.x, p.y, 1, 1 }),
  157. ModalCallbackFunction::create (treeViewMenuItemChosen, WeakReference<JucerTreeViewBase> (this)));
  158. }
  159. ProjectContentComponent* JucerTreeViewBase::getProjectContentComponent() const
  160. {
  161. for (Component* c = getOwnerView(); c != nullptr; c = c->getParentComponent())
  162. if (ProjectContentComponent* pcc = dynamic_cast<ProjectContentComponent*> (c))
  163. return pcc;
  164. return nullptr;
  165. }
  166. //==============================================================================
  167. class JucerTreeViewBase::ItemSelectionTimer : public Timer
  168. {
  169. public:
  170. explicit ItemSelectionTimer (JucerTreeViewBase& tvb) : owner (tvb) {}
  171. void timerCallback() override { owner.invokeShowDocument(); }
  172. private:
  173. JucerTreeViewBase& owner;
  174. JUCE_DECLARE_NON_COPYABLE (ItemSelectionTimer)
  175. };
  176. void JucerTreeViewBase::itemSelectionChanged (bool isNowSelected)
  177. {
  178. if (isNowSelected)
  179. {
  180. delayedSelectionTimer.reset (new ItemSelectionTimer (*this));
  181. delayedSelectionTimer->startTimer (getMillisecsAllowedForDragGesture());
  182. }
  183. else
  184. {
  185. cancelDelayedSelectionTimer();
  186. }
  187. }
  188. void JucerTreeViewBase::invokeShowDocument()
  189. {
  190. cancelDelayedSelectionTimer();
  191. showDocument();
  192. }
  193. void JucerTreeViewBase::itemDoubleClicked (const MouseEvent&)
  194. {
  195. invokeShowDocument();
  196. }
  197. void JucerTreeViewBase::cancelDelayedSelectionTimer()
  198. {
  199. delayedSelectionTimer.reset();
  200. }