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.

225 lines
6.5KB

  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. #include "jucer_JucerTreeViewBase.h"
  19. #include "../Project/jucer_ProjectContentComponent.h"
  20. //==============================================================================
  21. JucerTreeViewBase::JucerTreeViewBase()
  22. : textX (0)
  23. {
  24. setLinesDrawnForSubItems (false);
  25. }
  26. void JucerTreeViewBase::refreshSubItems()
  27. {
  28. WholeTreeOpennessRestorer openness (*this);
  29. clearSubItems();
  30. addSubItems();
  31. }
  32. Font JucerTreeViewBase::getFont() const
  33. {
  34. return Font (getItemHeight() * 0.6f);
  35. }
  36. void JucerTreeViewBase::paintItem (Graphics& g, int width, int height)
  37. {
  38. if (isSelected())
  39. g.fillAll (Colour (0x401111ee));
  40. }
  41. float JucerTreeViewBase::getIconSize() const
  42. {
  43. return jmin (getItemHeight() - 4.0f, 18.0f);
  44. }
  45. void JucerTreeViewBase::paintOpenCloseButton (Graphics& g, int width, int height, bool isMouseOver)
  46. {
  47. Path p;
  48. if (isOpen())
  49. p.addTriangle (width * 0.2f, height * 0.25f, width * 0.8f, height * 0.25f, width * 0.5f, height * 0.75f);
  50. else
  51. p.addTriangle (width * 0.25f, height * 0.25f, width * 0.8f, height * 0.5f, width * 0.25f, height * 0.75f);
  52. g.setColour (Colours::lightgrey);
  53. g.fillPath (p);
  54. }
  55. void JucerTreeViewBase::paintContent (Graphics& g, const Rectangle<int>& area)
  56. {
  57. g.setFont (getFont());
  58. g.setColour (isMissing() ? Colours::red : Colours::black);
  59. g.drawFittedText (getDisplayName(),
  60. area.getX(), area.getY(), area.getWidth(), area.getHeight(),
  61. Justification::centredLeft, 1, 0.8f);
  62. }
  63. Component* JucerTreeViewBase::createItemComponent()
  64. {
  65. return new TreeItemComponent (*this);
  66. }
  67. //==============================================================================
  68. class RenameTreeItemCallback : public ModalComponentManager::Callback,
  69. public TextEditorListener
  70. {
  71. public:
  72. RenameTreeItemCallback (JucerTreeViewBase& item_, Component& parent, const Rectangle<int>& bounds)
  73. : item (item_)
  74. {
  75. ed.setMultiLine (false, false);
  76. ed.setPopupMenuEnabled (false);
  77. ed.setSelectAllWhenFocused (true);
  78. ed.setFont (item.getFont());
  79. ed.addListener (this);
  80. ed.setText (item.getRenamingName());
  81. ed.setBounds (bounds);
  82. parent.addAndMakeVisible (&ed);
  83. ed.enterModalState (true, this);
  84. }
  85. void modalStateFinished (int resultCode)
  86. {
  87. if (resultCode != 0)
  88. item.setName (ed.getText());
  89. }
  90. void textEditorTextChanged (TextEditor&) {}
  91. void textEditorReturnKeyPressed (TextEditor& editor) { editor.exitModalState (1); }
  92. void textEditorEscapeKeyPressed (TextEditor& editor) { editor.exitModalState (0); }
  93. void textEditorFocusLost (TextEditor& editor) { editor.exitModalState (0); }
  94. private:
  95. TextEditor ed;
  96. JucerTreeViewBase& item;
  97. JUCE_DECLARE_NON_COPYABLE (RenameTreeItemCallback);
  98. };
  99. void JucerTreeViewBase::showRenameBox()
  100. {
  101. Rectangle<int> r (getItemPosition (true));
  102. r.setLeft (r.getX() + textX);
  103. r.setHeight (getItemHeight());
  104. new RenameTreeItemCallback (*this, *getOwnerView(), r);
  105. }
  106. void JucerTreeViewBase::itemClicked (const MouseEvent& e)
  107. {
  108. if (e.mods.isPopupMenu())
  109. {
  110. if (getOwnerView()->getNumSelectedItems() > 1)
  111. showMultiSelectionPopupMenu();
  112. else
  113. showPopupMenu();
  114. }
  115. }
  116. void JucerTreeViewBase::deleteItem() {}
  117. void JucerTreeViewBase::deleteAllSelectedItems() {}
  118. void JucerTreeViewBase::showDocument() {}
  119. void JucerTreeViewBase::showPopupMenu() {}
  120. void JucerTreeViewBase::showMultiSelectionPopupMenu() {}
  121. static void treeViewMenuItemChosen (int resultCode, JucerTreeViewBase* item)
  122. {
  123. item->handlePopupMenuResult (resultCode);
  124. }
  125. void JucerTreeViewBase::launchPopupMenu (PopupMenu& m)
  126. {
  127. m.showMenuAsync (PopupMenu::Options(),
  128. ModalCallbackFunction::create (treeViewMenuItemChosen, this));
  129. }
  130. void JucerTreeViewBase::handlePopupMenuResult (int)
  131. {
  132. }
  133. ProjectContentComponent* JucerTreeViewBase::getProjectContentComponent() const
  134. {
  135. Component* c = getOwnerView();
  136. while (c != nullptr)
  137. {
  138. ProjectContentComponent* pcc = dynamic_cast <ProjectContentComponent*> (c);
  139. if (pcc != nullptr)
  140. return pcc;
  141. c = c->getParentComponent();
  142. }
  143. return 0;
  144. }
  145. //==============================================================================
  146. class JucerTreeViewBase::ItemSelectionTimer : public Timer
  147. {
  148. public:
  149. ItemSelectionTimer (JucerTreeViewBase& owner_) : owner (owner_) {}
  150. void timerCallback() { owner.invokeShowDocument(); }
  151. private:
  152. JucerTreeViewBase& owner;
  153. JUCE_DECLARE_NON_COPYABLE (ItemSelectionTimer);
  154. };
  155. void JucerTreeViewBase::itemSelectionChanged (bool isNowSelected)
  156. {
  157. if (isNowSelected)
  158. {
  159. delayedSelectionTimer = new ItemSelectionTimer (*this);
  160. delayedSelectionTimer->startTimer (getMillisecsAllowedForDragGesture());
  161. }
  162. else
  163. {
  164. cancelDelayedSelectionTimer();
  165. }
  166. }
  167. void JucerTreeViewBase::invokeShowDocument()
  168. {
  169. cancelDelayedSelectionTimer();
  170. showDocument();
  171. }
  172. void JucerTreeViewBase::itemDoubleClicked (const MouseEvent& e)
  173. {
  174. invokeShowDocument();
  175. }
  176. void JucerTreeViewBase::cancelDelayedSelectionTimer()
  177. {
  178. delayedSelectionTimer = nullptr;
  179. }