2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-9 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #include "../jucedemo_headers.h"
29 //==============================================================================
30 class TreeViewDemoItem
: public TreeViewItem
33 TreeViewDemoItem (XmlElement
& xml_
)
42 int getItemWidth() const
44 return xml
.getIntAttribute ("width", -1);
47 const String
getUniqueName() const
49 return xml
.getTagName();
52 bool mightContainSubItems()
54 return xml
.getFirstChildElement() != 0;
57 void paintItem (Graphics
& g
, int width
, int height
)
59 // if this item is selected, fill it with a background colour..
61 g
.fillAll (Colours::blue
.withAlpha (0.3f
));
63 // use a "colour" attribute in the xml tag for this node to set the text colour..
64 g
.setColour (Colour (xml
.getStringAttribute ("colour", "ff000000").getHexValue32()));
66 g
.setFont (height
* 0.7f
);
68 // draw the xml element's tag name..
69 g
.drawText (xml
.getTagName(),
70 4, 0, width
- 4, height
,
71 Justification::centredLeft
, true);
74 void itemOpennessChanged (bool isNowOpen
)
78 // if we've not already done so, we'll now add the tree's sub-items. You could
79 // also choose to delete the existing ones and refresh them if that's more suitable
81 if (getNumSubItems() == 0)
83 // create and add sub-items to this node of the tree, corresponding to
84 // each sub-element in the XML..
86 forEachXmlChildElement (xml
, child
)
89 addSubItem (new TreeViewDemoItem (*child
));
95 // in this case, we'll leave any sub-items in the tree when the node gets closed,
96 // though you could choose to delete them if that's more appropriate for
101 const var
getDragSourceDescription()
103 return "TreeView Items";
110 //==============================================================================
111 class TreeViewDemo
: public Component
,
112 public DragAndDropContainer
,
113 public ButtonListener
116 //==============================================================================
119 thread ("Demo file tree thread"),
120 typeButton ("Type of treeview...")
122 setName ("Tree Views");
125 const String
treeXmlString (BinaryData::treedemo_xml
);
126 XmlDocument
parser (treeXmlString
);
127 treeXml
= parser
.getDocumentElement();
128 jassert (treeXml
!= 0);
131 rootItem
= new TreeViewDemoItem (*treeXml
);
132 rootItem
->setOpen (true);
134 // find the root of the user's home drive, and set that as our root..
135 File
folder (File::getSpecialLocation (File::userHomeDirectory
));
136 while (folder
.getParentDirectory() != folder
)
137 folder
= folder
.getParentDirectory();
139 directoryList
= new DirectoryContentsList (0, thread
);
140 directoryList
->setDirectory (folder
, true, true);
141 thread
.startThread (3);
143 addAndMakeVisible (&typeButton
);
144 typeButton
.addListener (this);
145 typeButton
.setAlwaysOnTop (true);
146 typeButton
.setTriggeredOnMouseDown (true);
148 showCustomTreeView();
154 directoryList
= 0; // (need to make sure this is deleted before the TimeSliceThread)
157 void paint (Graphics
& g
)
159 g
.setColour (Colours::grey
);
162 g
.drawRect (treeView
->getX(), treeView
->getY(),
163 treeView
->getWidth(), treeView
->getHeight());
165 if (fileTreeComp
!= 0)
166 g
.drawRect (fileTreeComp
->getX(), fileTreeComp
->getY(),
167 fileTreeComp
->getWidth(), fileTreeComp
->getHeight());
173 treeView
->setBoundsInset (BorderSize
<int> (40, 10, 10, 10));
174 else if (fileTreeComp
!= 0)
175 fileTreeComp
->setBoundsInset (BorderSize
<int> (40, 10, 10, 10));
177 typeButton
.changeWidthToFitText (22);
178 typeButton
.setTopLeftPosition (10, 10);
181 void showCustomTreeView()
186 addAndMakeVisible (treeView
= new TreeView());
187 treeView
->setRootItem (rootItem
);
188 treeView
->setMultiSelectEnabled (true);
193 void showFileTreeComp()
198 addAndMakeVisible (fileTreeComp
= new FileTreeComponent (*directoryList
));
202 void buttonClicked (Button
*)
205 m
.addItem (1, "Custom treeview showing an XML tree");
206 m
.addItem (2, "FileTreeComponent showing the file system");
208 m
.addItem (3, "Show root item", true,
209 treeView
!= 0 ? treeView
->isRootItemVisible()
210 : fileTreeComp
->isRootItemVisible());
211 m
.addItem (4, "Show open/close buttons", true,
212 treeView
!= 0 ? treeView
->areOpenCloseButtonsVisible()
213 : fileTreeComp
->areOpenCloseButtonsVisible());
215 m
.showMenuAsync (PopupMenu::Options().withTargetComponent (&typeButton
),
216 ModalCallbackFunction::forComponent (menuItemChosenCallback
, this));
219 static void menuItemChosenCallback (int result
, TreeViewDemo
* demoComponent
)
221 if (demoComponent
!= 0)
222 demoComponent
->menuItemChosenCallback (result
);
225 void menuItemChosenCallback (int result
)
229 showCustomTreeView();
231 else if (result
== 2)
235 else if (result
== 3)
238 treeView
->setRootItemVisible (! treeView
->isRootItemVisible());
240 fileTreeComp
->setRootItemVisible (! fileTreeComp
->isRootItemVisible());
242 else if (result
== 4)
245 treeView
->setOpenCloseButtonsVisible (! treeView
->areOpenCloseButtonsVisible());
247 fileTreeComp
->setOpenCloseButtonsVisible (! fileTreeComp
->areOpenCloseButtonsVisible());
252 ScopedPointer
<XmlElement
> treeXml
;
254 ScopedPointer
<TreeViewItem
> rootItem
;
255 ScopedPointer
<TreeView
> treeView
;
257 ScopedPointer
<FileTreeComponent
> fileTreeComp
;
258 ScopedPointer
<DirectoryContentsList
> directoryList
;
259 TimeSliceThread thread
;
261 TextButton typeButton
;
263 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TreeViewDemo
);
267 //==============================================================================
268 Component
* createTreeViewDemo()
270 return new TreeViewDemo();