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.

185 lines
5.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. #include "jucer_JucerTreeViewBase.h"
  19. //==============================================================================
  20. JucerTreeViewBase::JucerTreeViewBase()
  21. : textX (0)
  22. {
  23. setLinesDrawnForSubItems (false);
  24. }
  25. Font JucerTreeViewBase::getFont() const
  26. {
  27. return Font (getItemHeight() * 0.6f);
  28. }
  29. void JucerTreeViewBase::paintItem (Graphics& g, int width, int height)
  30. {
  31. if (isSelected())
  32. g.fillAll (Colour (0x401111ee));
  33. }
  34. void JucerTreeViewBase::paintOpenCloseButton (Graphics& g, int width, int height, bool isMouseOver)
  35. {
  36. Path p;
  37. if (isOpen())
  38. p.addTriangle (width * 0.2f, height * 0.25f, width * 0.8f, height * 0.25f, width * 0.5f, height * 0.75f);
  39. else
  40. p.addTriangle (width * 0.25f, height * 0.25f, width * 0.8f, height * 0.5f, width * 0.25f, height * 0.75f);
  41. g.setColour (Colours::lightgrey);
  42. g.fillPath (p);
  43. }
  44. //==============================================================================
  45. class TreeItemComponent : public Component
  46. {
  47. public:
  48. TreeItemComponent (JucerTreeViewBase& item_)
  49. : item (item_)
  50. {
  51. setInterceptsMouseClicks (false, true);
  52. item.createLeftEdgeComponents (leftComps);
  53. for (int i = 0; i < leftComps.size(); ++i)
  54. addAndMakeVisible (leftComps.getUnchecked(i));
  55. addAndMakeVisible (rightHandComponent = item.createRightEdgeComponent());
  56. }
  57. void paint (Graphics& g)
  58. {
  59. g.setColour (Colours::black);
  60. const int height = getHeight();
  61. item.getIcon()->drawWithin (g, Rectangle<float> (0.0f, 2.0f, height + 6.0f, height - 4.0f),
  62. RectanglePlacement::centred | RectanglePlacement::onlyReduceInSize, 1.0f);
  63. g.setFont (item.getFont());
  64. g.setColour (item.isMissing() ? Colours::red : Colours::black);
  65. const int right = rightHandComponent != nullptr ? rightHandComponent->getX() - 2
  66. : getWidth();
  67. g.drawFittedText (item.getDisplayName(),
  68. item.textX, 0, right - item.textX, height, Justification::centredLeft, 1, 0.8f);
  69. }
  70. void resized()
  71. {
  72. const int edge = 1;
  73. const int itemSize = getHeight() - edge * 2;
  74. item.textX = (leftComps.size() + 1) * getHeight() + 8;
  75. for (int i = 0; i < leftComps.size(); ++i)
  76. leftComps.getUnchecked(i)->setBounds (5 + (i + 1) * getHeight(), edge, itemSize, itemSize);
  77. if (rightHandComponent != nullptr)
  78. rightHandComponent->setBounds (getWidth() - itemSize - edge, edge, itemSize, itemSize);
  79. }
  80. private:
  81. JucerTreeViewBase& item;
  82. OwnedArray<Component> leftComps;
  83. ScopedPointer<Component> rightHandComponent;
  84. };
  85. Component* JucerTreeViewBase::createItemComponent()
  86. {
  87. return new TreeItemComponent (*this);
  88. }
  89. //==============================================================================
  90. class RenameTreeItemCallback : public ModalComponentManager::Callback,
  91. public TextEditorListener
  92. {
  93. public:
  94. RenameTreeItemCallback (JucerTreeViewBase& item_, Component& parent, const Rectangle<int>& bounds)
  95. : item (item_)
  96. {
  97. ed.setMultiLine (false, false);
  98. ed.setPopupMenuEnabled (false);
  99. ed.setSelectAllWhenFocused (true);
  100. ed.setFont (item.getFont());
  101. ed.addListener (this);
  102. ed.setText (item.getRenamingName());
  103. ed.setBounds (bounds);
  104. parent.addAndMakeVisible (&ed);
  105. ed.enterModalState (true, this);
  106. }
  107. void modalStateFinished (int resultCode)
  108. {
  109. if (resultCode != 0)
  110. item.setName (ed.getText());
  111. }
  112. void textEditorTextChanged (TextEditor&) {}
  113. void textEditorReturnKeyPressed (TextEditor& editor) { editor.exitModalState (1); }
  114. void textEditorEscapeKeyPressed (TextEditor& editor) { editor.exitModalState (0); }
  115. void textEditorFocusLost (TextEditor& editor) { editor.exitModalState (0); }
  116. private:
  117. TextEditor ed;
  118. JucerTreeViewBase& item;
  119. JUCE_DECLARE_NON_COPYABLE (RenameTreeItemCallback);
  120. };
  121. void JucerTreeViewBase::showRenameBox()
  122. {
  123. Rectangle<int> r (getItemPosition (true));
  124. r.setLeft (r.getX() + textX);
  125. r.setHeight (getItemHeight());
  126. new RenameTreeItemCallback (*this, *getOwnerView(), r);
  127. }
  128. void JucerTreeViewBase::itemClicked (const MouseEvent& e)
  129. {
  130. if (e.mods.isPopupMenu())
  131. {
  132. if (getOwnerView()->getNumSelectedItems() > 1)
  133. showMultiSelectionPopupMenu();
  134. else
  135. showPopupMenu();
  136. }
  137. }
  138. void JucerTreeViewBase::showPopupMenu()
  139. {
  140. }
  141. void JucerTreeViewBase::showMultiSelectionPopupMenu()
  142. {
  143. }