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.

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