Add TAL-Reverb-II plugin to test
[juce-lv2.git] / juce / source / extras / JuceDemo / Source / demos / TreeViewDemo.cpp
blob2f7a4065e65243c16ca94fe7e278292075dfb0b9
1 /*
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
32 public:
33 TreeViewDemoItem (XmlElement& xml_)
34 : xml (xml_)
38 ~TreeViewDemoItem()
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..
60 if (isSelected())
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)
76 if (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
80 // in your app.
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)
88 jassert (child != 0);
89 addSubItem (new TreeViewDemoItem (*child));
93 else
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
97 // your application.
101 const var getDragSourceDescription()
103 return "TreeView Items";
106 private:
107 XmlElement& xml;
110 //==============================================================================
111 class TreeViewDemo : public Component,
112 public DragAndDropContainer,
113 public ButtonListener
115 public:
116 //==============================================================================
117 TreeViewDemo()
118 : treeView (0),
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();
151 ~TreeViewDemo()
153 fileTreeComp = 0;
154 directoryList = 0; // (need to make sure this is deleted before the TimeSliceThread)
157 void paint (Graphics& g)
159 g.setColour (Colours::grey);
161 if (treeView != 0)
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());
170 void resized()
172 if (treeView != 0)
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()
183 treeView = 0;
184 fileTreeComp = 0;
186 addAndMakeVisible (treeView = new TreeView());
187 treeView->setRootItem (rootItem);
188 treeView->setMultiSelectEnabled (true);
190 resized();
193 void showFileTreeComp()
195 treeView = 0;
196 fileTreeComp = 0;
198 addAndMakeVisible (fileTreeComp = new FileTreeComponent (*directoryList));
199 resized();
202 void buttonClicked (Button*)
204 PopupMenu m;
205 m.addItem (1, "Custom treeview showing an XML tree");
206 m.addItem (2, "FileTreeComponent showing the file system");
207 m.addSeparator();
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)
227 if (result == 1)
229 showCustomTreeView();
231 else if (result == 2)
233 showFileTreeComp();
235 else if (result == 3)
237 if (treeView != 0)
238 treeView->setRootItemVisible (! treeView->isRootItemVisible());
239 else
240 fileTreeComp->setRootItemVisible (! fileTreeComp->isRootItemVisible());
242 else if (result == 4)
244 if (treeView != 0)
245 treeView->setOpenCloseButtonsVisible (! treeView->areOpenCloseButtonsVisible());
246 else
247 fileTreeComp->setOpenCloseButtonsVisible (! fileTreeComp->areOpenCloseButtonsVisible());
251 private:
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();