1 // This file is part of the "Irrlicht Engine".
2 // written by Reinhard Ostermeier, reinhard@nospam.r-ostermeier.de
3 
4 #ifndef __C_GUI_TREE_VIEW_H_INCLUDED__
5 #define __C_GUI_TREE_VIEW_H_INCLUDED__
6 
7 #include "IGUITreeView.h"
8 #include "irrList.h"
9 
10 
11 namespace irr
12 {
13 namespace gui
14 {
15 	// forward declarations
16 	class IGUIFont;
17 	class IGUIScrollBar;
18 	class CGUITreeView;
19 
20 	//! Node for gui tree view
21 	class CGUITreeViewNode : public IGUITreeViewNode
22 	{
23 	friend class CGUITreeView;
24 
25 	public:
26 		//! constructor
27 		CGUITreeViewNode( CGUITreeView* owner, CGUITreeViewNode* parent );
28 
29 		//! destructor
30 		~CGUITreeViewNode();
31 
32 		//! returns the owner (tree view) of this node
33 		virtual IGUITreeView* getOwner() const;
34 
35 		//! Returns the parent node of this node.
36 		virtual IGUITreeViewNode* getParent() const;
37 
38 		//! returns the text of the node
getText()39 		virtual const wchar_t* getText() const
40 		{ return Text.c_str(); }
41 
42 		//! sets the text of the node
43 		virtual void setText( const wchar_t* text );
44 
45 		//! returns the icon text of the node
getIcon()46 		virtual const wchar_t* getIcon() const
47 		{ return Icon.c_str(); }
48 
49 		//! sets the icon text of the node
50 		virtual void setIcon( const wchar_t* icon );
51 
52 		//! returns the image index of the node
getImageIndex()53 		virtual u32 getImageIndex() const
54 		{ return ImageIndex; }
55 
56 		//! sets the image index of the node
setImageIndex(u32 imageIndex)57 		virtual void setImageIndex( u32 imageIndex )
58 		{ ImageIndex = imageIndex; }
59 
60 		//! returns the image index of the node
getSelectedImageIndex()61 		virtual u32 getSelectedImageIndex() const
62 		{ return SelectedImageIndex; }
63 
64 		//! sets the image index of the node
setSelectedImageIndex(u32 imageIndex)65 		virtual void setSelectedImageIndex( u32 imageIndex )
66 		{ SelectedImageIndex = imageIndex; }
67 
68 		//! returns the user data (void*) of this node
getData()69 		virtual void* getData() const
70 		{ return Data; }
71 
72 		//! sets the user data (void*) of this node
setData(void * data)73 		virtual void setData( void* data )
74 		{ Data = data; }
75 
76 		//! returns the user data2 (IReferenceCounted) of this node
getData2()77 		virtual IReferenceCounted* getData2() const
78 		{ return Data2; }
79 
80 		//! sets the user data2 (IReferenceCounted) of this node
setData2(IReferenceCounted * data)81 		virtual void setData2( IReferenceCounted* data )
82 		{
83 			if( Data2 )
84 			{
85 				Data2->drop();
86 			}
87 			Data2 = data;
88 			if( Data2 )
89 			{
90 				Data2->grab();
91 			}
92 		}
93 
94 		//! returns the child item count
getChildCount()95 		virtual u32 getChildCount() const
96 		{ return Children.getSize(); }
97 
98 		//! removes all children (recursive) from this node
99 		virtual void clearChildren();
100 
101 		//! returns true if this node has child nodes
hasChildren()102 		virtual bool hasChildren() const
103 		{ return !Children.empty(); }
104 
105 		//! Adds a new node behind the last child node.
106 		//! \param text text of the new node
107 		//! \param icon icon text of the new node
108 		//! \param imageIndex index of the image for the new node (-1 = none)
109 		//! \param selectedImageIndex index of the selected image for the new node (-1 = same as imageIndex)
110 		//! \param data user data (void*) of the new node
111 		//! \param data2 user data2 (IReferenceCounted*) of the new node
112 		//! \return
113 		//! returns the new node
114 		virtual IGUITreeViewNode* addChildBack(
115 				const wchar_t* text,
116 				const wchar_t* icon = 0,
117 				s32 imageIndex = -1,
118 				s32 selectedImageIndex = -1,
119 				void* data = 0,
120 				IReferenceCounted* data2 = 0);
121 
122 		//! Adds a new node before the first child node.
123 		//! \param text text of the new node
124 		//! \param icon icon text of the new node
125 		//! \param imageIndex index of the image for the new node (-1 = none)
126 		//! \param selectedImageIndex index of the selected image for the new node (-1 = same as imageIndex)
127 		//! \param data user data (void*) of the new node
128 		//! \param data2 user data2 (IReferenceCounted*) of the new node
129 		//! \return
130 		//! returns the new node
131 		virtual IGUITreeViewNode* addChildFront(
132 				const wchar_t*		text,
133 				const wchar_t*		icon = 0,
134 				s32					imageIndex = -1,
135 				s32					selectedImageIndex = -1,
136 				void*					data = 0,
137 				IReferenceCounted*			data2 = 0 );
138 
139 		//! Adds a new node behind the other node.
140 		//! The other node has also te be a child node from this node.
141 		//! \param text text of the new node
142 		//! \param icon icon text of the new node
143 		//! \param imageIndex index of the image for the new node (-1 = none)
144 		//! \param selectedImageIndex index of the selected image for the new node (-1 = same as imageIndex)
145 		//! \param data user data (void*) of the new node
146 		//! \param data2 user data2 (IReferenceCounted*) of the new node
147 		//! \return
148 		//! returns the new node or 0 if other is no child node from this
149 		virtual IGUITreeViewNode* insertChildAfter(
150 				IGUITreeViewNode*	other,
151 				const wchar_t*		text,
152 				const wchar_t*		icon = 0,
153 				s32					imageIndex = -1,
154 				s32					selectedImageIndex = -1,
155 				void*					data = 0,
156 				IReferenceCounted*			data2 = 0 );
157 
158 		//! Adds a new node before the other node.
159 		//! The other node has also te be a child node from this node.
160 		//! \param text text of the new node
161 		//! \param icon icon text of the new node
162 		//! \param imageIndex index of the image for the new node (-1 = none)
163 		//! \param selectedImageIndex index of the selected image for the new node (-1 = same as imageIndex)
164 		//! \param data user data (void*) of the new node
165 		//! \param data2 user data2 (IReferenceCounted*) of the new node
166 		//! \return
167 		//! returns the new node or 0 if other is no child node from this
168 		virtual IGUITreeViewNode* insertChildBefore(
169 				IGUITreeViewNode*	other,
170 				const wchar_t*		text,
171 				const wchar_t*		icon = 0,
172 				s32					imageIndex = -1,
173 				s32					selectedImageIndex = -1,
174 				void*					data = 0,
175 				IReferenceCounted*			data2 = 0 );
176 
177 		//! Return the first child note from this node.
178 		virtual IGUITreeViewNode* getFirstChild() const;
179 
180 		//! Return the last child note from this node.
181 		virtual IGUITreeViewNode* getLastChild() const;
182 
183 		//! Returns the preverse sibling node from this node.
184 		virtual IGUITreeViewNode* getPrevSibling() const;
185 
186 		//! Returns the next sibling node from this node.
187 		virtual IGUITreeViewNode* getNextSibling() const;
188 
189 		//! Returns the next visible (expanded, may be out of scrolling) node from this node.
190 		virtual IGUITreeViewNode* getNextVisible() const;
191 
192 		//! Deletes a child node.
193 		virtual bool deleteChild( IGUITreeViewNode* child );
194 
195 		//! Moves a child node one position up.
196 		virtual bool moveChildUp( IGUITreeViewNode* child );
197 
198 		//! Moves a child node one position down.
199 		virtual bool moveChildDown( IGUITreeViewNode* child );
200 
201 		//! Returns true if the node is expanded (children are visible).
getExpanded()202 		virtual bool getExpanded() const
203 		{ return Expanded; }
204 
205 		//! Sets if the node is expanded.
206 		virtual void setExpanded( bool expanded );
207 
208 		//! Returns true if the node is currently selected.
209 		virtual bool getSelected() const;
210 
211 		//! Sets this node as selected.
212 		virtual void setSelected( bool selected );
213 
214 		//! Returns true if this node is the root node.
215 		virtual bool isRoot() const;
216 
217 		//! Returns the level of this node.
218 		virtual s32 getLevel() const;
219 
220 		//! Returns true if this node is visible (all parents are expanded).
221 		virtual bool isVisible() const;
222 
223 	private:
224 
225 		CGUITreeView*			Owner;
226 		CGUITreeViewNode*		Parent;
227 		core::stringw			Text;
228 		core::stringw			Icon;
229 		s32				ImageIndex;
230 		s32				SelectedImageIndex;
231 		void*				Data;
232 		IReferenceCounted*		Data2;
233 		bool				Expanded;
234 		core::list<CGUITreeViewNode*>	Children;
235 	};
236 
237 
238 	//! Default tree view GUI element.
239 	class CGUITreeView : public IGUITreeView
240 	{
241 	friend class CGUITreeViewNode;
242 
243 	public:
244 		//! constructor
245 		CGUITreeView( IGUIEnvironment* environment, IGUIElement* parent,
246 			s32 id, core::rect<s32> rectangle, bool clip = true,
247 			bool drawBack = false, bool scrollBarVertical = true, bool scrollBarHorizontal = true );
248 
249 		//! destructor
250 		virtual ~CGUITreeView();
251 
252 		//! returns the root node (not visible) from the tree.
getRoot()253 		virtual IGUITreeViewNode* getRoot() const
254 		{ return Root; }
255 
256 		//! returns the selected node of the tree or 0 if none is selected
getSelected()257 		virtual IGUITreeViewNode* getSelected() const
258 		{ return Selected; }
259 
260 		//! returns true if the tree lines are visible
getLinesVisible()261 		virtual bool getLinesVisible() const
262 		{ return LinesVisible; }
263 
264 		//! sets if the tree lines are visible
setLinesVisible(bool visible)265 		virtual void setLinesVisible( bool visible )
266 		{ LinesVisible = visible; }
267 
268 		//! called if an event happened.
269 		virtual bool OnEvent( const SEvent &event );
270 
271 		//! draws the element and its children
272 		virtual void draw();
273 
274 		//! Sets the font which should be used as icon font. This font is set to the Irrlicht engine
275 		//! built-in-font by default. Icons can be displayed in front of every list item.
276 		//! An icon is a string, displayed with the icon font. When using the build-in-font of the
277 		//! Irrlicht engine as icon font, the icon strings defined in GUIIcons.h can be used.
278 		virtual void setIconFont( IGUIFont* font );
279 
280 		//! Sets the image list which should be used for the image and selected image of every node.
281 		//! The default is 0 (no images).
282 		virtual void setImageList( IGUIImageList* imageList );
283 
284 		//! Returns the image list which is used for the nodes.
getImageList()285 		virtual IGUIImageList* getImageList() const
286 		{ return ImageList; }
287 
288 		//! Sets if the image is left of the icon. Default is true.
setImageLeftOfIcon(bool bLeftOf)289 		virtual void setImageLeftOfIcon( bool bLeftOf )
290 		{ ImageLeftOfIcon = bLeftOf; }
291 
292 		//! Returns if the Image is left of the icon. Default is true.
getImageLeftOfIcon()293 		virtual bool getImageLeftOfIcon() const
294 		{ return ImageLeftOfIcon; }
295 
296 		//! Returns the node which is associated to the last event.
getLastEventNode()297 		virtual IGUITreeViewNode* getLastEventNode() const
298 		{ return LastEventNode; }
299 
300 	private:
301 		//! calculates the heigth of an node and of all visible nodes.
302 		void recalculateItemHeight();
303 
304 		//! executes an mouse action (like selectNew of CGUIListBox)
305 		void mouseAction( s32 xpos, s32 ypos, bool onlyHover = false );
306 
307 		CGUITreeViewNode*	Root;
308 		IGUITreeViewNode*	Selected;
309 		s32			ItemHeight;
310 		s32			IndentWidth;
311 		s32			TotalItemHeight;
312 		s32			TotalItemWidth;
313 		IGUIFont*		Font;
314 		IGUIFont*		IconFont;
315 		IGUIScrollBar*		ScrollBarH;
316 		IGUIScrollBar*		ScrollBarV;
317 		IGUIImageList*		ImageList;
318 		IGUITreeViewNode*	LastEventNode;
319 		bool			LinesVisible;
320 		bool			Selecting;
321 		bool			Clip;
322 		bool			DrawBack;
323 		bool			ImageLeftOfIcon;
324 	};
325 
326 
327 } // end namespace gui
328 } // end namespace irr
329 
330 #endif
331 
332