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.

234 lines
7.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #pragma once
  18. class ProjectContentComponent;
  19. class Project;
  20. //==============================================================================
  21. class JucerTreeViewBase : public TreeViewItem
  22. {
  23. public:
  24. JucerTreeViewBase();
  25. ~JucerTreeViewBase();
  26. int getItemWidth() const override { return -1; }
  27. int getItemHeight() const override { return 25; }
  28. void paintOpenCloseButton (Graphics&, const Rectangle<float>& area, Colour backgroundColour, bool isMouseOver) override;
  29. void paintItem (Graphics& g, int width, int height) override;
  30. void itemClicked (const MouseEvent& e) override;
  31. void itemSelectionChanged (bool isNowSelected) override;
  32. void itemDoubleClicked (const MouseEvent&) override;
  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 Icon getIcon() const = 0;
  42. virtual bool isIconCrossedOut() const { return false; }
  43. virtual void paintIcon (Graphics& g, Rectangle<float> area);
  44. virtual void paintContent (Graphics& g, const Rectangle<int>& area);
  45. virtual int getRightHandButtonSpace() { return 0; }
  46. virtual Colour getContentColour (bool isIcon) const;
  47. virtual int getMillisecsAllowedForDragGesture() { return 120; }
  48. virtual File getDraggableFile() const { return {}; }
  49. virtual Component* createItemComponent() override;
  50. void refreshSubItems();
  51. virtual void deleteItem();
  52. virtual void deleteAllSelectedItems();
  53. virtual void showDocument();
  54. virtual void showMultiSelectionPopupMenu();
  55. virtual void showRenameBox();
  56. void launchPopupMenu (PopupMenu&); // runs asynchronously, and produces a callback to handlePopupMenuResult().
  57. virtual void showPopupMenu();
  58. virtual void showPlusMenu();
  59. virtual void handlePopupMenuResult (int resultCode);
  60. //==============================================================================
  61. // To handle situations where an item gets deleted before openness is
  62. // restored for it, this OpennessRestorer keeps only a pointer to the
  63. // topmost tree item.
  64. struct WholeTreeOpennessRestorer : public OpennessRestorer
  65. {
  66. WholeTreeOpennessRestorer (TreeViewItem& item) : OpennessRestorer (getTopLevelItem (item))
  67. {}
  68. private:
  69. static TreeViewItem& getTopLevelItem (TreeViewItem& item)
  70. {
  71. if (TreeViewItem* const p = item.getParentItem())
  72. return getTopLevelItem (*p);
  73. return item;
  74. }
  75. };
  76. int textX;
  77. protected:
  78. ProjectContentComponent* getProjectContentComponent() const;
  79. virtual void addSubItems() {}
  80. private:
  81. class ItemSelectionTimer;
  82. friend class ItemSelectionTimer;
  83. ScopedPointer<Timer> delayedSelectionTimer;
  84. WeakReference<JucerTreeViewBase>::Master masterReference;
  85. friend class WeakReference<JucerTreeViewBase>;
  86. void invokeShowDocument();
  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()
  104. {
  105. tree.setRootItem (nullptr);
  106. }
  107. void setRoot (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. tree.clearSelectedItems();
  147. }
  148. const Project* project;
  149. TreeView tree;
  150. ScopedPointer<JucerTreeViewBase> rootItem;
  151. private:
  152. String opennessStateKey, emptyTreeMessage;
  153. };
  154. //==============================================================================
  155. class TreeItemComponent : public Component
  156. {
  157. public:
  158. TreeItemComponent (JucerTreeViewBase& i) : item (i)
  159. {
  160. setInterceptsMouseClicks (false, true);
  161. }
  162. void paint (Graphics& g) override
  163. {
  164. auto bounds = getLocalBounds().toFloat();
  165. auto iconBounds = bounds.removeFromLeft (25).reduced (7, 5);
  166. bounds.removeFromRight (buttons.size() * bounds.getHeight());
  167. item.paintIcon (g, iconBounds);
  168. item.paintContent (g, bounds.toNearestInt());
  169. }
  170. void resized() override
  171. {
  172. item.textX = getHeight() + 4;
  173. Rectangle<int> r (getLocalBounds());
  174. for (int i = buttons.size(); --i >= 0;)
  175. buttons.getUnchecked(i)->setBounds (r.removeFromRight (r.getHeight()));
  176. }
  177. void addRightHandButton (Component* button)
  178. {
  179. buttons.add (button);
  180. addAndMakeVisible (button);
  181. }
  182. JucerTreeViewBase& item;
  183. OwnedArray<Component> buttons;
  184. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TreeItemComponent)
  185. };