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.

271 lines
10KB

  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() const override { return false; }
  69. Icon getIcon() const override { return Icon (getIcons().graph, getContentColour (true)); }
  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() const override { return false; }
  120. Icon getIcon() const override { return Icon (getIcons().box, getContentColour (true)); }
  121. bool canBeSelected() const override { return true; }
  122. bool mightContainSubItems() override { return false; }
  123. String getUniqueName() const override { return comp.getName(); }
  124. int getRightHandButtonSpace() override { return canBeLaunched() ? 60 : 40; }
  125. Component* createItemComponent() override
  126. {
  127. auto* content = new TreeItemComponent (*this);
  128. content->addRightHandButton (new ClassItemButton (*this, true));
  129. if (canBeLaunched())
  130. content->addRightHandButton (new ClassItemButton (*this, false));
  131. return content;
  132. }
  133. Colour getContentColour (bool isIcon) const override
  134. {
  135. auto alpha = comp.getInstantiationFlags().canBeInstantiated() ? 1.0f : 0.4f;
  136. auto& lf = ProjucerApplication::getApp().lookAndFeel;
  137. if (isSelected())
  138. return lf.findColour (defaultHighlightedTextColourId).withMultipliedAlpha (alpha);
  139. return lf.findColour (isIcon ? treeIconColourId : defaultTextColourId).withMultipliedAlpha (alpha);
  140. }
  141. bool canBeLaunched() const
  142. {
  143. return comp.getInstantiationFlags().canBeInstantiated();
  144. }
  145. void showClassDeclaration() const
  146. {
  147. if (ComponentListComp* clc = getOwnerView()->findParentComponentOfClass<ComponentListComp>())
  148. clc->showClassDeclaration (comp);
  149. }
  150. void launchEditor() const
  151. {
  152. if (ComponentListComp* clc = getOwnerView()->findParentComponentOfClass<ComponentListComp>())
  153. clc->openPreview (comp);
  154. }
  155. void itemClicked (const MouseEvent&) override
  156. {
  157. if (! canBeLaunched())
  158. if (ProjectContentComponent* const pcc = getOwnerView()->findParentComponentOfClass<ProjectContentComponent>())
  159. pcc->showBubbleMessage (pcc->getLocalArea (getOwnerView(), getItemPosition (true)),
  160. "Cannot create a live view:\n" + comp.getInstantiationFlags().getReasonForUnavailability());
  161. }
  162. void itemDoubleClicked (const MouseEvent&) override
  163. {
  164. if (canBeLaunched())
  165. launchEditor();
  166. else
  167. showClassDeclaration();
  168. }
  169. struct ClassItemButton : public Button
  170. {
  171. ClassItemButton (const ClassItem& c, bool isShowCodeButton)
  172. : Button (String()), classItem (c), isShowCode (isShowCodeButton)
  173. {
  174. setMouseCursor (MouseCursor::PointingHandCursor);
  175. }
  176. void paintButton (Graphics& g, bool isMouseOverButton, bool isButtonDown) override
  177. {
  178. const Path& path = isShowCode ? getIcons().code
  179. : getIcons().play;
  180. auto colour = classItem.getContentColour (true).withAlpha (isButtonDown ? 1.0f
  181. : (isMouseOverButton ? 0.8f
  182. : 0.5f));
  183. Icon (path, colour).draw (g, getLocalBounds().reduced (getHeight() / 5).toFloat(), false);
  184. }
  185. void clicked() override
  186. {
  187. if (isShowCode)
  188. classItem.showClassDeclaration();
  189. else
  190. classItem.launchEditor();
  191. }
  192. const ClassItem& classItem;
  193. bool isShowCode;
  194. };
  195. struct ClassComponent : public Component
  196. {
  197. ClassComponent (ClassItem& item, bool canBeLaunched)
  198. {
  199. addAndMakeVisible (buttons.add (new ClassItemButton (item, true)));
  200. if (canBeLaunched)
  201. addAndMakeVisible (buttons.add (new ClassItemButton (item, false)));
  202. setInterceptsMouseClicks (false, true);
  203. }
  204. void resized() override
  205. {
  206. auto bounds = getLocalBounds();
  207. for (auto b : buttons)
  208. b->setBounds (bounds.removeFromRight (25).reduced (2));
  209. }
  210. OwnedArray<ClassItemButton> buttons;
  211. };
  212. const ClassDatabase::Class comp;
  213. String displayName;
  214. };
  215. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ComponentListComp)
  216. };