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.

254 lines
9.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. class ComponentListComp : public TreePanelBase,
  18. private ActivityList::Listener
  19. {
  20. public:
  21. ComponentListComp (CompileEngineChildProcess& c)
  22. : TreePanelBase (&c.project, "compClassTreeState"),
  23. owner (c)
  24. {
  25. setName ("Components");
  26. tree.setRootItemVisible (false);
  27. tree.setMultiSelectEnabled (false);
  28. tree.setDefaultOpenness (true);
  29. setRoot (new NamespaceItem (&owner.getComponentList().globalNamespace));
  30. classListChanged (owner.getComponentList());
  31. owner.activityList.addListener (this);
  32. }
  33. ~ComponentListComp()
  34. {
  35. saveOpenness();
  36. owner.activityList.removeListener (this);
  37. }
  38. void classListChanged (const ClassDatabase::ClassList& newClasses) override
  39. {
  40. static_cast<NamespaceItem*> (rootItem.get())->setNamespace (&newClasses.globalNamespace);
  41. }
  42. void openPreview (const ClassDatabase::Class& comp)
  43. {
  44. owner.openPreview (comp);
  45. }
  46. void showClassDeclaration (const ClassDatabase::Class& comp)
  47. {
  48. owner.handleHighlightCode (comp.getClassDeclarationRange());
  49. }
  50. private:
  51. CompileEngineChildProcess& owner;
  52. struct ClassItem;
  53. struct NamespaceItem : public JucerTreeViewBase
  54. {
  55. NamespaceItem (const ClassDatabase::Namespace* n)
  56. {
  57. setNamespace (n);
  58. }
  59. void setNamespace (const ClassDatabase::Namespace* newNamespace)
  60. {
  61. namespaceToShow = newNamespace;
  62. uniqueID = namespaceToShow != nullptr ? "ns_" + namespaceToShow->fullName : "null";
  63. refreshSubItems();
  64. }
  65. String getRenamingName() const override { return getDisplayName(); }
  66. String getDisplayName() const override { return (namespaceToShow != nullptr ? namespaceToShow->name : String()) + "::"; }
  67. void setName (const String&) override {}
  68. bool isMissing() override { return false; }
  69. Icon getIcon() const override { return Icon (getIcons().graph, getContrastingColour (Colours::darkred, 0.5f)); }
  70. bool canBeSelected() const override { return true; }
  71. bool mightContainSubItems() override { return namespaceToShow != nullptr && ! namespaceToShow->isEmpty(); }
  72. String getUniqueName() const override { return uniqueID; }
  73. void addSubItems() override
  74. {
  75. if (namespaceToShow != nullptr)
  76. {
  77. Array<ClassItem*> newComps;
  78. Array<NamespaceItem*> newNamespaces;
  79. for (const auto& c : namespaceToShow->components)
  80. newComps.addSorted (*this, new ClassItem (c, *namespaceToShow));
  81. for(const auto& n : namespaceToShow->namespaces)
  82. {
  83. if (n.getTotalClassesAndNamespaces() < 10)
  84. createFlatItemList (n, newComps);
  85. else
  86. newNamespaces.add (new NamespaceItem (&n));
  87. }
  88. for (auto c : newComps)
  89. addSubItem (c);
  90. for (auto n : newNamespaces)
  91. addSubItem (n);
  92. }
  93. }
  94. void createFlatItemList (const ClassDatabase::Namespace& ns, Array<ClassItem*>& newComps)
  95. {
  96. for (const auto& c : ns.components)
  97. newComps.addSorted (*this, new ClassItem (c, *namespaceToShow));
  98. for (const auto& n : ns.namespaces)
  99. createFlatItemList (n, newComps);
  100. }
  101. static int compareElements (ClassItem* c1, ClassItem* c2)
  102. {
  103. return c1->comp.getName().compareIgnoreCase (c2->comp.getName());
  104. }
  105. private:
  106. const ClassDatabase::Namespace* namespaceToShow = nullptr;
  107. String uniqueID; // must be stored rather than calculated, in case the namespace obj is dangling
  108. };
  109. struct ClassItem : public JucerTreeViewBase
  110. {
  111. ClassItem (const ClassDatabase::Class& c, const ClassDatabase::Namespace& parentNS)
  112. : comp (c),
  113. displayName (comp.getName().substring (parentNS.fullName.length()))
  114. {
  115. }
  116. String getRenamingName() const override { return getDisplayName(); }
  117. String getDisplayName() const override { return displayName; }
  118. void setName (const String&) override {}
  119. bool isMissing() override { return false; }
  120. Icon getIcon() const override { return Icon (getIcons().box, getTextColour()); }
  121. bool canBeSelected() const override { return true; }
  122. bool mightContainSubItems() override { return false; }
  123. String getUniqueName() const override { return comp.getName(); }
  124. bool canBeLaunched() const
  125. {
  126. return comp.getInstantiationFlags().canBeInstantiated();
  127. }
  128. void showClassDeclaration() const
  129. {
  130. if (ComponentListComp* clc = getOwnerView()->findParentComponentOfClass<ComponentListComp>())
  131. clc->showClassDeclaration (comp);
  132. }
  133. void launchEditor() const
  134. {
  135. if (ComponentListComp* clc = getOwnerView()->findParentComponentOfClass<ComponentListComp>())
  136. clc->openPreview (comp);
  137. }
  138. void itemClicked (const MouseEvent&) override
  139. {
  140. if (! canBeLaunched())
  141. if (ProjectContentComponent* const pcc = getOwnerView()->findParentComponentOfClass<ProjectContentComponent>())
  142. pcc->showBubbleMessage (pcc->getLocalArea (getOwnerView(), getItemPosition (true)),
  143. "Cannot create a live view:\n" + comp.getInstantiationFlags().getReasonForUnavailability());
  144. }
  145. void itemDoubleClicked (const MouseEvent&) override
  146. {
  147. if (canBeLaunched())
  148. launchEditor();
  149. else
  150. showClassDeclaration();
  151. }
  152. void paintContent (Graphics& g, const Rectangle<int>& area) override
  153. {
  154. g.setFont (getFont());
  155. g.setColour (getTextColour());
  156. g.drawFittedText (getDisplayName(),
  157. area.withWidth (area.getWidth() - 40), // to account for buttons
  158. Justification::centredLeft, 1, 0.8f);
  159. }
  160. Colour getTextColour() const
  161. {
  162. return getContrastingColour (comp.getInstantiationFlags().canBeInstantiated() ? 0.8f : 0.3f);
  163. }
  164. Component* createItemComponent() override
  165. {
  166. Component* c = JucerTreeViewBase::createItemComponent();
  167. jassert (dynamic_cast<TreeItemComponent*> (c) != nullptr);
  168. if (canBeLaunched())
  169. static_cast<TreeItemComponent*> (c)->addRightHandButton (new ClassItemButton (*this, false));
  170. static_cast<TreeItemComponent*> (c)->addRightHandButton (new ClassItemButton (*this, true));
  171. return c;
  172. }
  173. struct ClassItemButton : public Button
  174. {
  175. ClassItemButton (const ClassItem& c, bool isShowCodeButton)
  176. : Button (String()), classItem (c), isShowCode (isShowCodeButton)
  177. {
  178. setMouseCursor (MouseCursor::PointingHandCursor);
  179. }
  180. void paintButton (Graphics& g, bool isMouseOverButton, bool isButtonDown) override
  181. {
  182. const Path& path = isShowCode ? getIcons().code
  183. : getIcons().play;
  184. Colour col (classItem.getBackgroundColour().contrasting (isShowCode ? Colours::white
  185. : Colours::lightgreen, 0.6f));
  186. Icon (path, col.withAlpha (isButtonDown ? 1.0f : (isMouseOverButton ? 0.8f : 0.5f)))
  187. .draw (g, getLocalBounds().reduced (getHeight() / 5).toFloat(), false);
  188. }
  189. void clicked() override
  190. {
  191. if (isShowCode)
  192. classItem.showClassDeclaration();
  193. else
  194. classItem.launchEditor();
  195. }
  196. const ClassItem& classItem;
  197. bool isShowCode;
  198. };
  199. const ClassDatabase::Class comp;
  200. String displayName;
  201. };
  202. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ComponentListComp)
  203. };