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