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.

279 lines
10KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. #pragma once
  19. //==============================================================================
  20. class ComponentListComp : public TreePanelBase,
  21. private ActivityList::Listener
  22. {
  23. public:
  24. ComponentListComp (CompileEngineChildProcess& c)
  25. : TreePanelBase (&c.project, "compClassTreeState"),
  26. owner (c)
  27. {
  28. setName ("Components");
  29. tree.setRootItemVisible (false);
  30. tree.setMultiSelectEnabled (false);
  31. tree.setDefaultOpenness (true);
  32. setRoot (new NamespaceItem (&owner.getComponentList().globalNamespace));
  33. classListChanged (owner.getComponentList());
  34. owner.activityList.addListener (this);
  35. }
  36. ~ComponentListComp() override
  37. {
  38. saveOpenness();
  39. owner.activityList.removeListener (this);
  40. }
  41. void classListChanged (const ClassDatabase::ClassList& newClasses) override
  42. {
  43. rootItem->clearSubItems();
  44. static_cast<NamespaceItem*> (rootItem.get())->setNamespace (&newClasses.globalNamespace);
  45. }
  46. void openPreview (const ClassDatabase::Class& comp)
  47. {
  48. owner.openPreview (comp);
  49. }
  50. void showClassDeclaration (const ClassDatabase::Class& comp)
  51. {
  52. owner.handleHighlightCode (comp.getClassDeclarationRange());
  53. }
  54. private:
  55. CompileEngineChildProcess& owner;
  56. struct ClassItem;
  57. struct NamespaceItem : public JucerTreeViewBase
  58. {
  59. NamespaceItem (const ClassDatabase::Namespace* n)
  60. {
  61. setNamespace (n);
  62. }
  63. void setNamespace (const ClassDatabase::Namespace* newNamespace)
  64. {
  65. namespaceToShow = newNamespace;
  66. uniqueID = namespaceToShow != nullptr ? "ns_" + namespaceToShow->fullName : "null";
  67. refreshSubItems();
  68. }
  69. String getRenamingName() const override { return getDisplayName(); }
  70. String getDisplayName() const override { return (namespaceToShow != nullptr ? namespaceToShow->name : String()) + "::"; }
  71. void setName (const String&) override {}
  72. bool isMissing() const override { return false; }
  73. Icon getIcon() const override { return Icon (getIcons().graph, getContentColour (true)); }
  74. bool canBeSelected() const override { return true; }
  75. bool mightContainSubItems() override { return namespaceToShow != nullptr && ! namespaceToShow->isEmpty(); }
  76. String getUniqueName() const override { return uniqueID; }
  77. void addSubItems() override
  78. {
  79. if (namespaceToShow != nullptr)
  80. {
  81. Array<ClassItem*> newComps;
  82. Array<NamespaceItem*> newNamespaces;
  83. for (const auto& c : namespaceToShow->components)
  84. newComps.addSorted (*this, new ClassItem (c, *namespaceToShow));
  85. for(const auto& n : namespaceToShow->namespaces)
  86. {
  87. if (n.getTotalClassesAndNamespaces() < 10)
  88. createFlatItemList (n, newComps);
  89. else
  90. newNamespaces.add (new NamespaceItem (&n));
  91. }
  92. for (auto c : newComps)
  93. addSubItem (c);
  94. for (auto n : newNamespaces)
  95. addSubItem (n);
  96. }
  97. }
  98. void createFlatItemList (const ClassDatabase::Namespace& ns, Array<ClassItem*>& newComps)
  99. {
  100. for (const auto& c : ns.components)
  101. newComps.addSorted (*this, new ClassItem (c, *namespaceToShow));
  102. for (const auto& n : ns.namespaces)
  103. createFlatItemList (n, newComps);
  104. }
  105. static int compareElements (ClassItem* c1, ClassItem* c2)
  106. {
  107. return c1->comp.getName().compareIgnoreCase (c2->comp.getName());
  108. }
  109. private:
  110. const ClassDatabase::Namespace* namespaceToShow = nullptr;
  111. String uniqueID; // must be stored rather than calculated, in case the namespace obj is dangling
  112. };
  113. struct ClassItem : public JucerTreeViewBase
  114. {
  115. ClassItem (const ClassDatabase::Class& c, const ClassDatabase::Namespace& parentNS)
  116. : comp (c),
  117. displayName (comp.getName().substring (parentNS.fullName.length()))
  118. {
  119. }
  120. String getRenamingName() const override { return getDisplayName(); }
  121. String getDisplayName() const override { return displayName; }
  122. void setName (const String&) override {}
  123. bool isMissing() const override { return false; }
  124. Icon getIcon() const override { return Icon (getIcons().box, getContentColour (true)); }
  125. bool canBeSelected() const override { return true; }
  126. bool mightContainSubItems() override { return false; }
  127. String getUniqueName() const override { return comp.getName(); }
  128. int getRightHandButtonSpace() override { return canBeLaunched() ? 60 : 40; }
  129. Component* createItemComponent() override
  130. {
  131. auto* content = new TreeItemComponent (*this);
  132. content->addRightHandButton (new ClassItemButton (*this, true));
  133. if (canBeLaunched())
  134. content->addRightHandButton (new ClassItemButton (*this, false));
  135. return content;
  136. }
  137. Colour getContentColour (bool isIcon) const override
  138. {
  139. auto alpha = comp.getInstantiationFlags().canBeInstantiated() ? 1.0f : 0.4f;
  140. auto& lf = ProjucerApplication::getApp().lookAndFeel;
  141. if (isSelected())
  142. return lf.findColour (defaultHighlightedTextColourId).withMultipliedAlpha (alpha);
  143. return lf.findColour (isIcon ? treeIconColourId : defaultTextColourId).withMultipliedAlpha (alpha);
  144. }
  145. bool canBeLaunched() const
  146. {
  147. return comp.getInstantiationFlags().canBeInstantiated();
  148. }
  149. void showClassDeclaration() const
  150. {
  151. if (ComponentListComp* clc = getOwnerView()->findParentComponentOfClass<ComponentListComp>())
  152. clc->showClassDeclaration (comp);
  153. }
  154. void launchEditor() const
  155. {
  156. if (ComponentListComp* clc = getOwnerView()->findParentComponentOfClass<ComponentListComp>())
  157. clc->openPreview (comp);
  158. }
  159. void itemClicked (const MouseEvent&) override
  160. {
  161. if (! canBeLaunched())
  162. if (ProjectContentComponent* const pcc = getOwnerView()->findParentComponentOfClass<ProjectContentComponent>())
  163. pcc->showBubbleMessage (pcc->getLocalArea (getOwnerView(), getItemPosition (true)),
  164. "Cannot create a live view:\n" + comp.getInstantiationFlags().getReasonForUnavailability());
  165. }
  166. void itemDoubleClicked (const MouseEvent&) override
  167. {
  168. if (canBeLaunched())
  169. launchEditor();
  170. else
  171. showClassDeclaration();
  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. auto colour = classItem.getContentColour (true).withAlpha (isButtonDown ? 1.0f
  185. : (isMouseOverButton ? 0.8f
  186. : 0.5f));
  187. Icon (path, colour).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. using Button::clicked;
  197. const ClassItem& classItem;
  198. bool isShowCode;
  199. };
  200. struct ClassComponent : public Component
  201. {
  202. ClassComponent (ClassItem& item, bool canBeLaunched)
  203. {
  204. addAndMakeVisible (buttons.add (new ClassItemButton (item, true)));
  205. if (canBeLaunched)
  206. addAndMakeVisible (buttons.add (new ClassItemButton (item, false)));
  207. setInterceptsMouseClicks (false, true);
  208. }
  209. void resized() override
  210. {
  211. auto bounds = getLocalBounds();
  212. for (auto b : buttons)
  213. b->setBounds (bounds.removeFromRight (25).reduced (2));
  214. }
  215. OwnedArray<ClassItemButton> buttons;
  216. };
  217. const ClassDatabase::Class comp;
  218. String displayName;
  219. };
  220. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ComponentListComp)
  221. };