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.

250 lines
8.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - 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 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-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 : 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. friend class ItemSelectionTimer;
  89. std::unique_ptr<Timer> delayedSelectionTimer;
  90. void invokeShowDocument();
  91. JUCE_DECLARE_WEAK_REFERENCEABLE (JucerTreeViewBase)
  92. };
  93. //==============================================================================
  94. class TreePanelBase : public Component
  95. {
  96. public:
  97. TreePanelBase (const Project* p, const String& treeviewID)
  98. : project (p), opennessStateKey (treeviewID)
  99. {
  100. addAndMakeVisible (tree);
  101. tree.setRootItemVisible (true);
  102. tree.setDefaultOpenness (true);
  103. tree.setColour (TreeView::backgroundColourId, Colours::transparentBlack);
  104. tree.setIndentSize (14);
  105. tree.getViewport()->setScrollBarThickness (6);
  106. tree.addMouseListener (this, true);
  107. }
  108. ~TreePanelBase() override
  109. {
  110. tree.setRootItem (nullptr);
  111. }
  112. void setRoot (std::unique_ptr<JucerTreeViewBase>);
  113. void saveOpenness();
  114. virtual void deleteSelectedItems()
  115. {
  116. if (rootItem != nullptr)
  117. rootItem->deleteAllSelectedItems();
  118. }
  119. void setEmptyTreeMessage (const String& newMessage)
  120. {
  121. if (emptyTreeMessage != newMessage)
  122. {
  123. emptyTreeMessage = newMessage;
  124. repaint();
  125. }
  126. }
  127. static void drawEmptyPanelMessage (Component& comp, Graphics& g, const String& message)
  128. {
  129. const int fontHeight = 13;
  130. const Rectangle<int> area (comp.getLocalBounds());
  131. g.setColour (comp.findColour (defaultTextColourId));
  132. g.setFont ((float) fontHeight);
  133. g.drawFittedText (message, area.reduced (4, 2), Justification::centred, area.getHeight() / fontHeight);
  134. }
  135. void paint (Graphics& g) override
  136. {
  137. if (emptyTreeMessage.isNotEmpty() && (rootItem == nullptr || rootItem->getNumSubItems() == 0))
  138. drawEmptyPanelMessage (*this, g, emptyTreeMessage);
  139. }
  140. void resized() override
  141. {
  142. tree.setBounds (getAvailableBounds());
  143. }
  144. Rectangle<int> getAvailableBounds() const
  145. {
  146. return Rectangle<int> (0, 2, getWidth() - 2, getHeight() - 2);
  147. }
  148. void mouseDown (const MouseEvent& e) override
  149. {
  150. if (e.eventComponent == &tree)
  151. {
  152. tree.clearSelectedItems();
  153. if (e.mods.isRightButtonDown())
  154. rootItem->showPopupMenu (e.getMouseDownScreenPosition());
  155. }
  156. }
  157. const Project* project;
  158. TreeView tree;
  159. std::unique_ptr<JucerTreeViewBase> rootItem;
  160. private:
  161. String opennessStateKey, emptyTreeMessage;
  162. };
  163. //==============================================================================
  164. class TreeItemComponent : public Component
  165. {
  166. public:
  167. TreeItemComponent (JucerTreeViewBase& i) : item (&i)
  168. {
  169. setAccessible (false);
  170. setInterceptsMouseClicks (false, true);
  171. item->textX = iconWidth;
  172. }
  173. void paint (Graphics& g) override
  174. {
  175. if (item == nullptr)
  176. return;
  177. auto bounds = getLocalBounds().toFloat();
  178. auto iconBounds = bounds.removeFromLeft ((float) iconWidth).reduced (7, 5);
  179. bounds.removeFromRight ((float) buttons.size() * bounds.getHeight());
  180. item->paintIcon (g, iconBounds);
  181. item->paintContent (g, bounds.toNearestInt());
  182. }
  183. void resized() override
  184. {
  185. auto r = getLocalBounds();
  186. for (int i = buttons.size(); --i >= 0;)
  187. buttons.getUnchecked (i)->setBounds (r.removeFromRight (r.getHeight()));
  188. }
  189. void addRightHandButton (Component* button)
  190. {
  191. buttons.add (button);
  192. addAndMakeVisible (button);
  193. }
  194. WeakReference<JucerTreeViewBase> item;
  195. OwnedArray<Component> buttons;
  196. const int iconWidth = 25;
  197. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TreeItemComponent)
  198. };