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.

243 lines
8.1KB

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