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.

214 lines
7.0KB

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