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.

240 lines
7.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. #pragma once
  20. class ProjectContentComponent;
  21. class Project;
  22. //==============================================================================
  23. class JucerTreeViewBase : public TreeViewItem,
  24. public TooltipClient
  25. {
  26. public:
  27. JucerTreeViewBase();
  28. ~JucerTreeViewBase();
  29. int getItemWidth() const override { return -1; }
  30. int getItemHeight() const override { return 25; }
  31. void paintOpenCloseButton (Graphics&, const Rectangle<float>& area, Colour backgroundColour, bool isMouseOver) override;
  32. void paintItem (Graphics& g, int width, int height) override;
  33. void itemClicked (const MouseEvent& e) override;
  34. void itemSelectionChanged (bool isNowSelected) override;
  35. void itemDoubleClicked (const MouseEvent&) override;
  36. void cancelDelayedSelectionTimer();
  37. //==============================================================================
  38. virtual bool isRoot() const { return false; }
  39. virtual Font getFont() const;
  40. virtual String getRenamingName() const = 0;
  41. virtual String getDisplayName() const = 0;
  42. virtual void setName (const String& newName) = 0;
  43. virtual bool isMissing() const = 0;
  44. virtual bool hasWarnings() const { return false; }
  45. virtual Icon getIcon() const = 0;
  46. virtual bool isIconCrossedOut() const { return false; }
  47. virtual void paintIcon (Graphics& g, Rectangle<float> area);
  48. virtual void paintContent (Graphics& g, const Rectangle<int>& area);
  49. virtual int getRightHandButtonSpace() { return 0; }
  50. virtual Colour getContentColour (bool isIcon) const;
  51. virtual int getMillisecsAllowedForDragGesture() { return 120; }
  52. virtual File getDraggableFile() const { return {}; }
  53. virtual Component* createItemComponent() override;
  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 showPlusMenu();
  63. virtual void handlePopupMenuResult (int resultCode);
  64. String getTooltip() override { return {}; }
  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. ScopedPointer<Timer> delayedSelectionTimer;
  89. WeakReference<JucerTreeViewBase>::Master masterReference;
  90. friend class WeakReference<JucerTreeViewBase>;
  91. void invokeShowDocument();
  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()
  109. {
  110. tree.setRootItem (nullptr);
  111. }
  112. void setRoot (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. tree.clearSelectedItems();
  152. }
  153. const Project* project;
  154. TreeView tree;
  155. ScopedPointer<JucerTreeViewBase> rootItem;
  156. private:
  157. String opennessStateKey, emptyTreeMessage;
  158. };
  159. //==============================================================================
  160. class TreeItemComponent : public Component
  161. {
  162. public:
  163. TreeItemComponent (JucerTreeViewBase& i) : item (i)
  164. {
  165. setInterceptsMouseClicks (false, true);
  166. }
  167. void paint (Graphics& g) override
  168. {
  169. auto bounds = getLocalBounds().toFloat();
  170. auto iconBounds = bounds.removeFromLeft (25).reduced (7, 5);
  171. bounds.removeFromRight (buttons.size() * bounds.getHeight());
  172. item.paintIcon (g, iconBounds);
  173. item.paintContent (g, bounds.toNearestInt());
  174. }
  175. void resized() override
  176. {
  177. item.textX = getHeight() + 4;
  178. Rectangle<int> r (getLocalBounds());
  179. for (int i = buttons.size(); --i >= 0;)
  180. buttons.getUnchecked(i)->setBounds (r.removeFromRight (r.getHeight()));
  181. }
  182. void addRightHandButton (Component* button)
  183. {
  184. buttons.add (button);
  185. addAndMakeVisible (button);
  186. }
  187. JucerTreeViewBase& item;
  188. OwnedArray<Component> buttons;
  189. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TreeItemComponent)
  190. };