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.

244 lines
7.9KB

  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;
  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. Component* createItemComponent() override;
  36. String getTooltip() override { return {}; }
  37. void cancelDelayedSelectionTimer();
  38. //==============================================================================
  39. virtual bool isRoot() const { return false; }
  40. virtual Font getFont() const;
  41. virtual String getRenamingName() const = 0;
  42. virtual String getDisplayName() const = 0;
  43. virtual void setName (const String& newName) = 0;
  44. virtual bool isMissing() const = 0;
  45. virtual bool hasWarnings() const { return false; }
  46. virtual Icon getIcon() const = 0;
  47. virtual bool isIconCrossedOut() const { return false; }
  48. virtual void paintIcon (Graphics& g, Rectangle<float> area);
  49. virtual void paintContent (Graphics& g, Rectangle<int> area);
  50. virtual int getRightHandButtonSpace() { return 0; }
  51. virtual Colour getContentColour (bool isIcon) const;
  52. virtual int getMillisecsAllowedForDragGesture() { return 120; }
  53. virtual File getDraggableFile() const { return {}; }
  54. void refreshSubItems();
  55. virtual void deleteItem();
  56. virtual void deleteAllSelectedItems();
  57. virtual void showDocument();
  58. virtual void showMultiSelectionPopupMenu();
  59. virtual void showRenameBox();
  60. void launchPopupMenu (PopupMenu&); // runs asynchronously, and produces a callback to handlePopupMenuResult().
  61. virtual void showPopupMenu();
  62. virtual void showAddMenu();
  63. virtual void handlePopupMenuResult (int resultCode);
  64. virtual void setSearchFilter (const String&) {}
  65. //==============================================================================
  66. // To handle situations where an item gets deleted before openness is
  67. // restored for it, this OpennessRestorer keeps only a pointer to the
  68. // topmost tree item.
  69. struct WholeTreeOpennessRestorer : public OpennessRestorer
  70. {
  71. WholeTreeOpennessRestorer (TreeViewItem& item) : OpennessRestorer (getTopLevelItem (item))
  72. {}
  73. private:
  74. static TreeViewItem& getTopLevelItem (TreeViewItem& item)
  75. {
  76. if (TreeViewItem* const p = item.getParentItem())
  77. return getTopLevelItem (*p);
  78. return item;
  79. }
  80. };
  81. int textX;
  82. protected:
  83. ProjectContentComponent* getProjectContentComponent() const;
  84. virtual void addSubItems() {}
  85. private:
  86. class ItemSelectionTimer;
  87. friend 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 (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();
  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 : public Component
  164. {
  165. public:
  166. TreeItemComponent (JucerTreeViewBase& i) : item (i)
  167. {
  168. setInterceptsMouseClicks (false, true);
  169. item.textX = iconWidth;
  170. }
  171. void paint (Graphics& g) override
  172. {
  173. auto bounds = getLocalBounds().toFloat();
  174. auto iconBounds = bounds.removeFromLeft ((float) iconWidth).reduced (7, 5);
  175. bounds.removeFromRight ((float) buttons.size() * bounds.getHeight());
  176. item.paintIcon (g, iconBounds);
  177. item.paintContent (g, bounds.toNearestInt());
  178. }
  179. void resized() override
  180. {
  181. auto r = getLocalBounds();
  182. for (int i = buttons.size(); --i >= 0;)
  183. buttons.getUnchecked(i)->setBounds (r.removeFromRight (r.getHeight()));
  184. }
  185. void addRightHandButton (Component* button)
  186. {
  187. buttons.add (button);
  188. addAndMakeVisible (button);
  189. }
  190. JucerTreeViewBase& item;
  191. OwnedArray<Component> buttons;
  192. const int iconWidth = 25;
  193. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TreeItemComponent)
  194. };