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.

211 lines
6.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCER_JUCERTREEVIEWBASE_JUCEHEADER__
  19. #define __JUCER_JUCERTREEVIEWBASE_JUCEHEADER__
  20. #include "../jucer_Headers.h"
  21. class ProjectContentComponent;
  22. //==============================================================================
  23. class JucerTreeViewBase : public TreeViewItem
  24. {
  25. public:
  26. JucerTreeViewBase();
  27. int getItemWidth() const { return -1; }
  28. int getItemHeight() const { return 20; }
  29. void paintItem (Graphics& g, int width, int height);
  30. void paintOpenCloseButton (Graphics& g, int width, int height, bool isMouseOver);
  31. Component* createItemComponent();
  32. void itemClicked (const MouseEvent& e);
  33. void itemSelectionChanged (bool isNowSelected);
  34. void itemDoubleClicked (const MouseEvent&);
  35. void cancelDelayedSelectionTimer();
  36. //==============================================================================
  37. virtual Font getFont() const;
  38. virtual String getRenamingName() const = 0;
  39. virtual String getDisplayName() const = 0;
  40. virtual void setName (const String& newName) = 0;
  41. virtual bool isMissing() = 0;
  42. virtual Icon getIcon() const = 0;
  43. virtual float getIconSize() const;
  44. virtual void paintContent (Graphics& g, const Rectangle<int>& area);
  45. virtual int getMillisecsAllowedForDragGesture() { return 120; };
  46. void refreshSubItems();
  47. virtual void deleteItem();
  48. virtual void deleteAllSelectedItems();
  49. virtual void showDocument();
  50. virtual void showPopupMenu();
  51. virtual void showMultiSelectionPopupMenu();
  52. virtual void showRenameBox();
  53. void launchPopupMenu (PopupMenu&); // runs asynchronously, and produces a callback to handlePopupMenuResult().
  54. virtual void handlePopupMenuResult (int resultCode);
  55. //==============================================================================
  56. // To handle situations where an item gets deleted before openness is
  57. // restored for it, this OpennessRestorer keeps only a pointer to the
  58. // topmost tree item.
  59. struct WholeTreeOpennessRestorer : public OpennessRestorer
  60. {
  61. WholeTreeOpennessRestorer (TreeViewItem& item) : OpennessRestorer (getTopLevelItem (item))
  62. {}
  63. private:
  64. static TreeViewItem& getTopLevelItem (TreeViewItem& item)
  65. {
  66. TreeViewItem* const p = item.getParentItem();
  67. return p != nullptr ? getTopLevelItem (*p) : item;
  68. }
  69. };
  70. int textX;
  71. protected:
  72. ProjectContentComponent* getProjectContentComponent() const;
  73. virtual void addSubItems() {}
  74. Colour getBackgroundColour() const;
  75. Colour getContrastingColour (float contrast) const;
  76. Colour getContrastingColour (const Colour& targetColour, float minContrast) const;
  77. private:
  78. class ItemSelectionTimer;
  79. friend class ItemSelectionTimer;
  80. ScopedPointer<Timer> delayedSelectionTimer;
  81. void invokeShowDocument();
  82. };
  83. //==============================================================================
  84. class TreePanelBase : public Component
  85. {
  86. public:
  87. TreePanelBase (const String& opennessStateKey_)
  88. : opennessStateKey (opennessStateKey_)
  89. {
  90. addAndMakeVisible (&tree);
  91. tree.setRootItemVisible (true);
  92. tree.setDefaultOpenness (true);
  93. tree.setColour (TreeView::backgroundColourId, Colours::transparentBlack);
  94. tree.setIndentSize (14);
  95. tree.getViewport()->setScrollBarThickness (14);
  96. }
  97. ~TreePanelBase()
  98. {
  99. tree.setRootItem (nullptr);
  100. }
  101. void setRoot (JucerTreeViewBase* root)
  102. {
  103. rootItem = root;
  104. tree.setRootItem (root);
  105. tree.getRootItem()->setOpen (true);
  106. const ScopedPointer<XmlElement> treeOpenness (getAppProperties().getXmlValue (opennessStateKey));
  107. if (treeOpenness != nullptr)
  108. {
  109. tree.restoreOpennessState (*treeOpenness, true);
  110. for (int i = tree.getNumSelectedItems(); --i >= 0;)
  111. {
  112. JucerTreeViewBase* item = dynamic_cast<JucerTreeViewBase*> (tree.getSelectedItem (i));
  113. if (item != nullptr)
  114. item->cancelDelayedSelectionTimer();
  115. }
  116. }
  117. }
  118. void saveOpenness()
  119. {
  120. const ScopedPointer<XmlElement> opennessState (tree.getOpennessState (true));
  121. if (opennessState != nullptr)
  122. getAppProperties().setValue (opennessStateKey, opennessState);
  123. }
  124. void deleteSelectedItems()
  125. {
  126. if (rootItem != nullptr)
  127. rootItem->deleteAllSelectedItems();
  128. }
  129. void resized()
  130. {
  131. tree.setBounds (getAvailableBounds());
  132. }
  133. Rectangle<int> getAvailableBounds() const
  134. {
  135. return Rectangle<int> (0, 2, getWidth() - 2, getHeight() - 2);
  136. }
  137. TreeView tree;
  138. ScopedPointer<JucerTreeViewBase> rootItem;
  139. private:
  140. String opennessStateKey;
  141. };
  142. //==============================================================================
  143. class TreeItemComponent : public Component
  144. {
  145. public:
  146. TreeItemComponent (JucerTreeViewBase& item_) : item (item_)
  147. {
  148. setInterceptsMouseClicks (false, true);
  149. }
  150. void paint (Graphics& g)
  151. {
  152. g.setColour (Colours::black);
  153. paintIcon (g);
  154. item.paintContent (g, Rectangle<int> (item.textX, 0, getWidth() - item.textX, getHeight()));
  155. }
  156. void paintIcon (Graphics& g)
  157. {
  158. item.getIcon().draw (g, Rectangle<float> (4.0f, 2.0f, item.getIconSize(), getHeight() - 4.0f));
  159. }
  160. void resized()
  161. {
  162. item.textX = (int) item.getIconSize() + 8;
  163. }
  164. JucerTreeViewBase& item;
  165. };
  166. #endif