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.

273 lines
10KB

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