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.

249 lines
8.3KB

  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. #pragma once
  19. class ProjectContentComponent;
  20. class Project;
  21. //==============================================================================
  22. class JucerTreeViewBase : public TreeViewItem,
  23. public TooltipClient
  24. {
  25. public:
  26. JucerTreeViewBase();
  27. ~JucerTreeViewBase() override = default;
  28. int getItemWidth() const override { return -1; }
  29. int getItemHeight() const override { return 25; }
  30. void paintOpenCloseButton (Graphics&, const Rectangle<float>& area, Colour backgroundColour, bool isMouseOver) override;
  31. void paintItem (Graphics& g, int width, int height) override;
  32. void itemClicked (const MouseEvent& e) override;
  33. void itemSelectionChanged (bool isNowSelected) override;
  34. void itemDoubleClicked (const MouseEvent&) override;
  35. std::unique_ptr<Component> createItemComponent() override;
  36. String getTooltip() override { return {}; }
  37. String getAccessibilityName() override { return getDisplayName(); }
  38. void cancelDelayedSelectionTimer();
  39. //==============================================================================
  40. virtual bool isRoot() const { return false; }
  41. virtual Font getFont() const;
  42. virtual String getRenamingName() const = 0;
  43. virtual String getDisplayName() const = 0;
  44. virtual void setName (const String& newName) = 0;
  45. virtual bool isMissing() const = 0;
  46. virtual bool hasWarnings() const { return false; }
  47. virtual Icon getIcon() const = 0;
  48. virtual bool isIconCrossedOut() const { return false; }
  49. virtual void paintIcon (Graphics& g, Rectangle<float> area);
  50. virtual void paintContent (Graphics& g, Rectangle<int> area);
  51. virtual int getRightHandButtonSpace() { return 0; }
  52. virtual Colour getContentColour (bool isIcon) const;
  53. virtual int getMillisecsAllowedForDragGesture() { return 120; }
  54. virtual File getDraggableFile() const { return {}; }
  55. void refreshSubItems();
  56. void showRenameBox();
  57. virtual void deleteItem() {}
  58. virtual void deleteAllSelectedItems() {}
  59. virtual void showDocument() {}
  60. virtual void showMultiSelectionPopupMenu (Point<int>) {}
  61. virtual void showPopupMenu (Point<int>) {}
  62. virtual void showAddMenu (Point<int>) {}
  63. virtual void handlePopupMenuResult (int) {}
  64. virtual void setSearchFilter (const String&) {}
  65. void launchPopupMenu (PopupMenu&, Point<int>); // runs asynchronously, and produces a callback to handlePopupMenuResult().
  66. //==============================================================================
  67. // To handle situations where an item gets deleted before openness is
  68. // restored for it, this OpennessRestorer keeps only a pointer to the
  69. // topmost tree item.
  70. struct WholeTreeOpennessRestorer final : public OpennessRestorer
  71. {
  72. WholeTreeOpennessRestorer (TreeViewItem& item) : OpennessRestorer (getTopLevelItem (item))
  73. {}
  74. private:
  75. static TreeViewItem& getTopLevelItem (TreeViewItem& item)
  76. {
  77. if (TreeViewItem* const p = item.getParentItem())
  78. return getTopLevelItem (*p);
  79. return item;
  80. }
  81. };
  82. int textX = 0;
  83. protected:
  84. ProjectContentComponent* getProjectContentComponent() const;
  85. virtual void addSubItems() {}
  86. private:
  87. class ItemSelectionTimer;
  88. std::unique_ptr<Timer> delayedSelectionTimer;
  89. void invokeShowDocument();
  90. JUCE_DECLARE_WEAK_REFERENCEABLE (JucerTreeViewBase)
  91. };
  92. //==============================================================================
  93. class TreePanelBase : public Component
  94. {
  95. public:
  96. TreePanelBase (const Project* p, const String& treeviewID)
  97. : project (p), opennessStateKey (treeviewID)
  98. {
  99. addAndMakeVisible (tree);
  100. tree.setRootItemVisible (true);
  101. tree.setDefaultOpenness (true);
  102. tree.setColour (TreeView::backgroundColourId, Colours::transparentBlack);
  103. tree.setIndentSize (14);
  104. tree.getViewport()->setScrollBarThickness (6);
  105. tree.addMouseListener (this, true);
  106. }
  107. ~TreePanelBase() override
  108. {
  109. tree.setRootItem (nullptr);
  110. }
  111. void setRoot (std::unique_ptr<JucerTreeViewBase>);
  112. void saveOpenness();
  113. virtual void deleteSelectedItems()
  114. {
  115. if (rootItem != nullptr)
  116. rootItem->deleteAllSelectedItems();
  117. }
  118. void setEmptyTreeMessage (const String& newMessage)
  119. {
  120. if (emptyTreeMessage != newMessage)
  121. {
  122. emptyTreeMessage = newMessage;
  123. repaint();
  124. }
  125. }
  126. static void drawEmptyPanelMessage (Component& comp, Graphics& g, const String& message)
  127. {
  128. const int fontHeight = 13;
  129. const Rectangle<int> area (comp.getLocalBounds());
  130. g.setColour (comp.findColour (defaultTextColourId));
  131. g.setFont ((float) fontHeight);
  132. g.drawFittedText (message, area.reduced (4, 2), Justification::centred, area.getHeight() / fontHeight);
  133. }
  134. void paint (Graphics& g) override
  135. {
  136. if (emptyTreeMessage.isNotEmpty() && (rootItem == nullptr || rootItem->getNumSubItems() == 0))
  137. drawEmptyPanelMessage (*this, g, emptyTreeMessage);
  138. }
  139. void resized() override
  140. {
  141. tree.setBounds (getAvailableBounds());
  142. }
  143. Rectangle<int> getAvailableBounds() const
  144. {
  145. return Rectangle<int> (0, 2, getWidth() - 2, getHeight() - 2);
  146. }
  147. void mouseDown (const MouseEvent& e) override
  148. {
  149. if (e.eventComponent == &tree)
  150. {
  151. tree.clearSelectedItems();
  152. if (e.mods.isRightButtonDown())
  153. rootItem->showPopupMenu (e.getMouseDownScreenPosition());
  154. }
  155. }
  156. const Project* project;
  157. TreeView tree;
  158. std::unique_ptr<JucerTreeViewBase> rootItem;
  159. private:
  160. String opennessStateKey, emptyTreeMessage;
  161. };
  162. //==============================================================================
  163. class TreeItemComponent final : public Component
  164. {
  165. public:
  166. TreeItemComponent (JucerTreeViewBase& i) : item (&i)
  167. {
  168. setAccessible (false);
  169. setInterceptsMouseClicks (false, true);
  170. item->textX = iconWidth;
  171. }
  172. void paint (Graphics& g) override
  173. {
  174. if (item == nullptr)
  175. return;
  176. auto bounds = getLocalBounds().toFloat();
  177. auto iconBounds = bounds.removeFromLeft ((float) iconWidth).reduced (7, 5);
  178. bounds.removeFromRight ((float) buttons.size() * bounds.getHeight());
  179. item->paintIcon (g, iconBounds);
  180. item->paintContent (g, bounds.toNearestInt());
  181. }
  182. void resized() override
  183. {
  184. auto r = getLocalBounds();
  185. for (int i = buttons.size(); --i >= 0;)
  186. buttons.getUnchecked (i)->setBounds (r.removeFromRight (r.getHeight()));
  187. }
  188. void addRightHandButton (Component* button)
  189. {
  190. buttons.add (button);
  191. addAndMakeVisible (button);
  192. }
  193. WeakReference<JucerTreeViewBase> item;
  194. OwnedArray<Component> buttons;
  195. const int iconWidth = 25;
  196. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TreeItemComponent)
  197. };