CGUITreeView.h 10 KB

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