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.

210 lines
6.9KB

  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. class Project;
  23. //==============================================================================
  24. class JucerTreeViewBase : public TreeViewItem
  25. {
  26. public:
  27. JucerTreeViewBase();
  28. int getItemWidth() const { return -1; }
  29. int getItemHeight() const { return 20; }
  30. void paintItem (Graphics& g, int width, int height);
  31. void paintOpenCloseButton (Graphics& g, int width, int height, bool isMouseOver);
  32. Component* createItemComponent();
  33. void itemClicked (const MouseEvent& e);
  34. void itemSelectionChanged (bool isNowSelected);
  35. void itemDoubleClicked (const MouseEvent&);
  36. void cancelDelayedSelectionTimer();
  37. //==============================================================================
  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() = 0;
  43. virtual Icon getIcon() const = 0;
  44. virtual float getIconSize() const;
  45. virtual void paintContent (Graphics& g, const Rectangle<int>& area);
  46. virtual int getMillisecsAllowedForDragGesture() { return 120; };
  47. void refreshSubItems();
  48. virtual void deleteItem();
  49. virtual void deleteAllSelectedItems();
  50. virtual void showDocument();
  51. virtual void showMultiSelectionPopupMenu();
  52. virtual void showRenameBox();
  53. void launchPopupMenu (PopupMenu&); // runs asynchronously, and produces a callback to handlePopupMenuResult().
  54. virtual void showPopupMenu();
  55. virtual void handlePopupMenuResult (int resultCode);
  56. //==============================================================================
  57. // To handle situations where an item gets deleted before openness is
  58. // restored for it, this OpennessRestorer keeps only a pointer to the
  59. // topmost tree item.
  60. struct WholeTreeOpennessRestorer : public OpennessRestorer
  61. {
  62. WholeTreeOpennessRestorer (TreeViewItem& item) : OpennessRestorer (getTopLevelItem (item))
  63. {}
  64. private:
  65. static TreeViewItem& getTopLevelItem (TreeViewItem& item)
  66. {
  67. TreeViewItem* const p = item.getParentItem();
  68. return p != nullptr ? getTopLevelItem (*p) : item;
  69. }
  70. };
  71. int textX;
  72. protected:
  73. ProjectContentComponent* getProjectContentComponent() const;
  74. virtual void addSubItems() {}
  75. Colour getBackgroundColour() const;
  76. Colour getContrastingColour (float contrast) const;
  77. Colour getContrastingColour (const Colour& targetColour, float minContrast) const;
  78. private:
  79. class ItemSelectionTimer;
  80. friend class ItemSelectionTimer;
  81. ScopedPointer<Timer> delayedSelectionTimer;
  82. void invokeShowDocument();
  83. };
  84. //==============================================================================
  85. class TreePanelBase : public Component
  86. {
  87. public:
  88. TreePanelBase (const Project* p, const String& treeviewID)
  89. : project (p), opennessStateKey (treeviewID)
  90. {
  91. addAndMakeVisible (&tree);
  92. tree.setRootItemVisible (true);
  93. tree.setDefaultOpenness (true);
  94. tree.setColour (TreeView::backgroundColourId, Colours::transparentBlack);
  95. tree.setIndentSize (14);
  96. tree.getViewport()->setScrollBarThickness (14);
  97. }
  98. ~TreePanelBase()
  99. {
  100. tree.setRootItem (nullptr);
  101. }
  102. void setRoot (JucerTreeViewBase* root);
  103. void saveOpenness();
  104. void deleteSelectedItems()
  105. {
  106. if (rootItem != nullptr)
  107. rootItem->deleteAllSelectedItems();
  108. }
  109. void setEmptyTreeMessage (const String& newMessage)
  110. {
  111. if (emptyTreeMessage != newMessage)
  112. {
  113. emptyTreeMessage = newMessage;
  114. repaint();
  115. }
  116. }
  117. static void drawEmptyPanelMessage (Component& comp, Graphics& g, const String& message)
  118. {
  119. const int fontHeight = 13;
  120. const Rectangle<int> area (comp.getLocalBounds());
  121. g.setColour (comp.findColour (mainBackgroundColourId).contrasting (0.7f));
  122. g.setFont ((float) fontHeight);
  123. g.drawFittedText (message, area.reduced (4, 2), Justification::centred, area.getHeight() / fontHeight);
  124. }
  125. void paint (Graphics& g)
  126. {
  127. if (emptyTreeMessage.isNotEmpty() && (rootItem == nullptr || rootItem->getNumSubItems() == 0))
  128. drawEmptyPanelMessage (*this, g, emptyTreeMessage);
  129. }
  130. void resized()
  131. {
  132. tree.setBounds (getAvailableBounds());
  133. }
  134. Rectangle<int> getAvailableBounds() const
  135. {
  136. return Rectangle<int> (0, 2, getWidth() - 2, getHeight() - 2);
  137. }
  138. const Project* project;
  139. TreeView tree;
  140. ScopedPointer<JucerTreeViewBase> rootItem;
  141. private:
  142. String opennessStateKey, emptyTreeMessage;
  143. };
  144. //==============================================================================
  145. class TreeItemComponent : public Component
  146. {
  147. public:
  148. TreeItemComponent (JucerTreeViewBase& item_) : item (item_)
  149. {
  150. setInterceptsMouseClicks (false, true);
  151. }
  152. void paint (Graphics& g)
  153. {
  154. g.setColour (Colours::black);
  155. paintIcon (g);
  156. item.paintContent (g, Rectangle<int> (item.textX, 0, getWidth() - item.textX, getHeight()));
  157. }
  158. void paintIcon (Graphics& g)
  159. {
  160. item.getIcon().draw (g, Rectangle<float> (4.0f, 2.0f, item.getIconSize(), getHeight() - 4.0f));
  161. }
  162. void resized()
  163. {
  164. item.textX = (int) item.getIconSize() + 8;
  165. }
  166. JucerTreeViewBase& item;
  167. };
  168. #endif