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.

236 lines
7.6KB

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