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.

219 lines
7.2KB

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