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.

215 lines
7.1KB

  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. virtual File getDraggableFile() const { return File::nonexistent; }
  49. void refreshSubItems();
  50. virtual void deleteItem();
  51. virtual void deleteAllSelectedItems();
  52. virtual void showDocument();
  53. virtual void showMultiSelectionPopupMenu();
  54. virtual void showRenameBox();
  55. void launchPopupMenu (PopupMenu&); // runs asynchronously, and produces a callback to handlePopupMenuResult().
  56. virtual void showPopupMenu();
  57. virtual void handlePopupMenuResult (int resultCode);
  58. //==============================================================================
  59. // To handle situations where an item gets deleted before openness is
  60. // restored for it, this OpennessRestorer keeps only a pointer to the
  61. // topmost tree item.
  62. struct WholeTreeOpennessRestorer : public OpennessRestorer
  63. {
  64. WholeTreeOpennessRestorer (TreeViewItem& item) : OpennessRestorer (getTopLevelItem (item))
  65. {}
  66. private:
  67. static TreeViewItem& getTopLevelItem (TreeViewItem& item)
  68. {
  69. TreeViewItem* const p = item.getParentItem();
  70. return p != nullptr ? getTopLevelItem (*p) : item;
  71. }
  72. };
  73. int textX;
  74. protected:
  75. ProjectContentComponent* getProjectContentComponent() const;
  76. virtual void addSubItems() {}
  77. Colour getBackgroundColour() const;
  78. Colour getContrastingColour (float contrast) const;
  79. Colour getContrastingColour (const Colour& targetColour, float minContrast) const;
  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 (14);
  101. }
  102. ~TreePanelBase()
  103. {
  104. tree.setRootItem (nullptr);
  105. }
  106. void setRoot (JucerTreeViewBase* root);
  107. void saveOpenness();
  108. void deleteSelectedItems()
  109. {
  110. if (rootItem != nullptr)
  111. rootItem->deleteAllSelectedItems();
  112. }
  113. void setEmptyTreeMessage (const String& newMessage)
  114. {
  115. if (emptyTreeMessage != newMessage)
  116. {
  117. emptyTreeMessage = newMessage;
  118. repaint();
  119. }
  120. }
  121. static void drawEmptyPanelMessage (Component& comp, Graphics& g, const String& message)
  122. {
  123. const int fontHeight = 13;
  124. const Rectangle<int> area (comp.getLocalBounds());
  125. g.setColour (comp.findColour (mainBackgroundColourId).contrasting (0.7f));
  126. g.setFont ((float) fontHeight);
  127. g.drawFittedText (message, area.reduced (4, 2), Justification::centred, area.getHeight() / fontHeight);
  128. }
  129. void paint (Graphics& g)
  130. {
  131. if (emptyTreeMessage.isNotEmpty() && (rootItem == nullptr || rootItem->getNumSubItems() == 0))
  132. drawEmptyPanelMessage (*this, g, emptyTreeMessage);
  133. }
  134. void resized()
  135. {
  136. tree.setBounds (getAvailableBounds());
  137. }
  138. Rectangle<int> getAvailableBounds() const
  139. {
  140. return Rectangle<int> (0, 2, getWidth() - 2, getHeight() - 2);
  141. }
  142. const Project* project;
  143. TreeView tree;
  144. ScopedPointer<JucerTreeViewBase> rootItem;
  145. private:
  146. String opennessStateKey, emptyTreeMessage;
  147. };
  148. //==============================================================================
  149. class TreeItemComponent : public Component
  150. {
  151. public:
  152. TreeItemComponent (JucerTreeViewBase& i) : item (i)
  153. {
  154. setInterceptsMouseClicks (false, true);
  155. }
  156. void paint (Graphics& g)
  157. {
  158. g.setColour (Colours::black);
  159. paintIcon (g);
  160. item.paintContent (g, Rectangle<int> (item.textX, 0, getWidth() - item.textX, getHeight()));
  161. }
  162. void paintIcon (Graphics& g)
  163. {
  164. item.getIcon().draw (g, Rectangle<float> (4.0f, 2.0f, item.getIconSize(), getHeight() - 4.0f));
  165. }
  166. void resized()
  167. {
  168. item.textX = (int) item.getIconSize() + 8;
  169. }
  170. JucerTreeViewBase& item;
  171. };
  172. #endif