treectrl.h 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: treectrl.h
  3. // Purpose: interface of wxTreeItemData
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. @class wxTreeCtrl
  9. A tree control presents information as a hierarchy, with items that may be
  10. expanded to show further items. Items in a tree control are referenced by
  11. wxTreeItemId handles, which may be tested for validity by calling
  12. wxTreeItemId::IsOk().
  13. A similar control with a fully native implementation for GTK+ and OS X
  14. as well is wxDataViewTreeCtrl.
  15. To intercept events from a tree control, use the event table macros
  16. described in wxTreeEvent.
  17. @beginStyleTable
  18. @style{wxTR_EDIT_LABELS}
  19. Use this style if you wish the user to be able to edit labels in the
  20. tree control.
  21. @style{wxTR_NO_BUTTONS}
  22. For convenience to document that no buttons are to be drawn.
  23. @style{wxTR_HAS_BUTTONS}
  24. Use this style to show + and - buttons to the left of parent items.
  25. @style{wxTR_TWIST_BUTTONS}
  26. Selects alternative style of @c +/@c - buttons and shows rotating
  27. ("twisting") arrows instead. Currently this style is only implemented
  28. under Microsoft Windows Vista and later Windows versions and is ignored
  29. under the other platforms. Notice that under Vista this style results
  30. in the same appearance as used by the tree control in Explorer and
  31. other built-in programs and so using it may be preferable to the
  32. default style.
  33. @style{wxTR_NO_LINES}
  34. Use this style to hide vertical level connectors.
  35. @style{wxTR_FULL_ROW_HIGHLIGHT}
  36. Use this style to have the background colour and the selection highlight
  37. extend over the entire horizontal row of the tree control window. (This
  38. flag is ignored under Windows unless you specify @c wxTR_NO_LINES as
  39. well.)
  40. @style{wxTR_LINES_AT_ROOT}
  41. Use this style to show lines between root nodes. Only applicable if @c
  42. wxTR_HIDE_ROOT is set and @c wxTR_NO_LINES is not set.
  43. @style{wxTR_HIDE_ROOT}
  44. Use this style to suppress the display of the root node, effectively
  45. causing the first-level nodes to appear as a series of root nodes.
  46. @style{wxTR_ROW_LINES}
  47. Use this style to draw a contrasting border between displayed rows.
  48. @style{wxTR_HAS_VARIABLE_ROW_HEIGHT}
  49. Use this style to cause row heights to be just big enough to fit the
  50. content. If not set, all rows use the largest row height. The default is
  51. that this flag is unset. Generic only.
  52. @style{wxTR_SINGLE}
  53. For convenience to document that only one item may be selected at a
  54. time. Selecting another item causes the current selection, if any, to be
  55. deselected. This is the default.
  56. @style{wxTR_MULTIPLE}
  57. Use this style to allow a range of items to be selected. If a second
  58. range is selected, the current range, if any, is deselected.
  59. @style{wxTR_DEFAULT_STYLE}
  60. The set of flags that are closest to the defaults for the native control
  61. for a particular toolkit.
  62. @endStyleTable
  63. @beginEventEmissionTable{wxTreeEvent}
  64. @event{EVT_TREE_BEGIN_DRAG(id, func)}
  65. Begin dragging with the left mouse button.
  66. If you want to enable left-dragging you need to intercept this event
  67. and explicitly call wxTreeEvent::Allow(), as it's vetoed by default.
  68. Processes a @c wxEVT_TREE_BEGIN_DRAG event type.
  69. @event{EVT_TREE_BEGIN_RDRAG(id, func)}
  70. Begin dragging with the right mouse button.
  71. If you want to enable right-dragging you need to intercept this event
  72. and explicitly call wxTreeEvent::Allow(), as it's vetoed by default.
  73. Processes a @c wxEVT_TREE_BEGIN_RDRAG event type.
  74. @event{EVT_TREE_END_DRAG(id, func)}
  75. End dragging with the left or right mouse button.
  76. Processes a @c wxEVT_TREE_END_DRAG event type.
  77. @event{EVT_TREE_BEGIN_LABEL_EDIT(id, func)}
  78. Begin editing a label. This can be prevented by calling Veto().
  79. Processes a @c wxEVT_TREE_BEGIN_LABEL_EDIT event type.
  80. @event{EVT_TREE_END_LABEL_EDIT(id, func)}
  81. Finish editing a label. This can be prevented by calling Veto().
  82. Processes a @c wxEVT_TREE_END_LABEL_EDIT event type.
  83. @event{EVT_TREE_DELETE_ITEM(id, func)}
  84. An item was deleted.
  85. Processes a @c wxEVT_TREE_DELETE_ITEM event type.
  86. @event{EVT_TREE_GET_INFO(id, func)}
  87. Request information from the application.
  88. Processes a @c wxEVT_TREE_GET_INFO event type.
  89. @event{EVT_TREE_SET_INFO(id, func)}
  90. Information is being supplied.
  91. Processes a @c wxEVT_TREE_SET_INFO event type.
  92. @event{EVT_TREE_ITEM_ACTIVATED(id, func)}
  93. The item has been activated, i.e. chosen by double clicking it with
  94. mouse or from keyboard.
  95. Processes a @c wxEVT_TREE_ITEM_ACTIVATED event type.
  96. @event{EVT_TREE_ITEM_COLLAPSED(id, func)}
  97. The item has been collapsed.
  98. Processes a @c wxEVT_TREE_ITEM_COLLAPSED event type.
  99. @event{EVT_TREE_ITEM_COLLAPSING(id, func)}
  100. The item is being collapsed. This can be prevented by calling Veto().
  101. Processes a @c wxEVT_TREE_ITEM_COLLAPSING event type.
  102. @event{EVT_TREE_ITEM_EXPANDED(id, func)}
  103. The item has been expanded.
  104. Processes a @c wxEVT_TREE_ITEM_EXPANDED event type.
  105. @event{EVT_TREE_ITEM_EXPANDING(id, func)}
  106. The item is being expanded. This can be prevented by calling Veto().
  107. Processes a @c wxEVT_TREE_ITEM_EXPANDING event type.
  108. @event{EVT_TREE_ITEM_RIGHT_CLICK(id, func)}
  109. The user has clicked the item with the right mouse button.
  110. Processes a @c wxEVT_TREE_ITEM_RIGHT_CLICK event type.
  111. @event{EVT_TREE_ITEM_MIDDLE_CLICK(id, func)}
  112. The user has clicked the item with the middle mouse button. This is
  113. only supported by the generic control.
  114. Processes a @c wxEVT_TREE_ITEM_MIDDLE_CLICK event type.
  115. @event{EVT_TREE_SEL_CHANGED(id, func)}
  116. Selection has changed.
  117. Processes a @c wxEVT_TREE_SEL_CHANGED event type.
  118. @event{EVT_TREE_SEL_CHANGING(id, func)}
  119. Selection is changing. This can be prevented by calling Veto().
  120. Processes a @c wxEVT_TREE_SEL_CHANGING event type.
  121. @event{EVT_TREE_KEY_DOWN(id, func)}
  122. A key has been pressed.
  123. Processes a @c wxEVT_TREE_KEY_DOWN event type.
  124. @event{EVT_TREE_ITEM_GETTOOLTIP(id, func)}
  125. The opportunity to set the item tooltip is being given to the application
  126. (call wxTreeEvent::SetToolTip). Windows only.
  127. Processes a @c wxEVT_TREE_ITEM_GETTOOLTIP event type.
  128. @event{EVT_TREE_ITEM_MENU(id, func)}
  129. The context menu for the selected item has been requested, either by a
  130. right click or by using the menu key.
  131. Processes a @c wxEVT_TREE_ITEM_MENU event type.
  132. @event{EVT_TREE_STATE_IMAGE_CLICK(id, func)}
  133. The state image has been clicked.
  134. Processes a @c wxEVT_TREE_STATE_IMAGE_CLICK event type.
  135. @endEventTable
  136. See also @ref overview_windowstyles.
  137. @b Win32 @b notes:
  138. wxTreeCtrl class uses the standard common treeview control under Win32
  139. implemented in the system library comctl32.dll. Some versions of this
  140. library are known to have bugs with handling the tree control colours: the
  141. usual symptom is that the expanded items leave black (or otherwise
  142. incorrectly coloured) background behind them, especially for the controls
  143. using non-default background colour. The recommended solution is to upgrade
  144. the comctl32.dll to a newer version: see
  145. http://www.microsoft.com/downloads/details.aspx?familyid=cb2cf3a2-8025-4e8f-8511-9b476a8d35d2
  146. @library{wxcore}
  147. @category{ctrl}
  148. @appearance{treectrl}
  149. @see wxDataViewTreeCtrl, wxTreeEvent, wxTreeItemData, @ref overview_treectrl,
  150. wxListBox, wxListCtrl, wxImageList
  151. */
  152. class wxTreeCtrl : public wxControl
  153. {
  154. public:
  155. /**
  156. Default Constructor.
  157. */
  158. wxTreeCtrl();
  159. /**
  160. Constructor, creating and showing a tree control.
  161. @param parent
  162. Parent window. Must not be @NULL.
  163. @param id
  164. Window identifier. The value @c wxID_ANY indicates a default value.
  165. @param pos
  166. Window position.
  167. If ::wxDefaultPosition is specified then a default position is chosen.
  168. @param size
  169. Window size.
  170. If ::wxDefaultSize is specified then the window is sized appropriately.
  171. @param style
  172. Window style. See wxTreeCtrl.
  173. @param validator
  174. Window validator.
  175. @param name
  176. Window name.
  177. @see Create(), wxValidator
  178. */
  179. wxTreeCtrl(wxWindow* parent, wxWindowID id = wxID_ANY,
  180. const wxPoint& pos = wxDefaultPosition,
  181. const wxSize& size = wxDefaultSize,
  182. long style = wxTR_DEFAULT_STYLE,
  183. const wxValidator& validator = wxDefaultValidator,
  184. const wxString& name = wxTreeCtrlNameStr);
  185. /**
  186. Destructor, destroying the tree control.
  187. */
  188. virtual ~wxTreeCtrl();
  189. /**
  190. Adds the root node to the tree, returning the new item.
  191. The @a image and @a selImage parameters are an index within the normal
  192. image list specifying the image to use for unselected and selected
  193. items, respectively. If @a image -1 and @a selImage is -1, the same
  194. image is used for both selected and unselected items.
  195. */
  196. virtual wxTreeItemId AddRoot(const wxString& text, int image = -1,
  197. int selImage = -1,
  198. wxTreeItemData* data = NULL);
  199. /**
  200. Appends an item to the end of the branch identified by @a parent, return
  201. a new item id.
  202. The @a image and @a selImage parameters are an index within the normal
  203. image list specifying the image to use for unselected and selected
  204. items, respectively. If @a image > -1 and @a selImage is -1, the same
  205. image is used for both selected and unselected items.
  206. */
  207. wxTreeItemId AppendItem(const wxTreeItemId& parent,
  208. const wxString& text,
  209. int image = -1,
  210. int selImage = -1,
  211. wxTreeItemData* data = NULL);
  212. /**
  213. Sets the buttons image list. The button images assigned with this method
  214. will be automatically deleted by wxTreeCtrl as appropriate (i.e. it
  215. takes ownership of the list).
  216. Setting or assigning the button image list enables the display of image
  217. buttons. Once enabled, the only way to disable the display of button
  218. images is to set the button image list to @NULL.
  219. This function is only available in the generic version.
  220. @see SetButtonsImageList().
  221. */
  222. void AssignButtonsImageList(wxImageList* imageList);
  223. /**
  224. Sets the normal image list. The image list assigned with this method
  225. will be automatically deleted by wxTreeCtrl as appropriate (i.e. it
  226. takes ownership of the list).
  227. @see SetImageList().
  228. */
  229. void AssignImageList(wxImageList* imageList);
  230. /**
  231. Sets the state image list. Image list assigned with this method will be
  232. automatically deleted by wxTreeCtrl as appropriate (i.e. it takes
  233. ownership of the list).
  234. @see SetStateImageList().
  235. */
  236. void AssignStateImageList(wxImageList* imageList);
  237. /**
  238. Collapses the given item.
  239. */
  240. virtual void Collapse(const wxTreeItemId& item);
  241. /**
  242. Collapses the root item.
  243. @see ExpandAll()
  244. */
  245. void CollapseAll();
  246. /**
  247. Collapses this item and all of its children, recursively.
  248. @see ExpandAllChildren()
  249. */
  250. void CollapseAllChildren(const wxTreeItemId& item);
  251. /**
  252. Collapses the given item and removes all children.
  253. */
  254. virtual void CollapseAndReset(const wxTreeItemId& item);
  255. /**
  256. Creates the tree control.
  257. See wxTreeCtrl::wxTreeCtrl() for further details.
  258. */
  259. bool Create(wxWindow* parent, wxWindowID id = wxID_ANY,
  260. const wxPoint& pos = wxDefaultPosition,
  261. const wxSize& size = wxDefaultSize,
  262. long style = wxTR_DEFAULT_STYLE,
  263. const wxValidator& validator = wxDefaultValidator,
  264. const wxString& name = wxTreeCtrlNameStr);
  265. /**
  266. Deletes the specified item. A @c EVT_TREE_DELETE_ITEM event will be
  267. generated.
  268. This function may cause a subsequent call to GetNextChild() to fail.
  269. */
  270. virtual void Delete(const wxTreeItemId& item);
  271. /**
  272. Deletes all items in the control. Note that this may not generate
  273. @c EVT_TREE_DELETE_ITEM events under some Windows versions although
  274. normally such event is generated for each removed item.
  275. */
  276. virtual void DeleteAllItems();
  277. /**
  278. Deletes all children of the given item (but not the item itself). Note
  279. that this will @b not generate any events unlike Delete() method.
  280. If you have called SetItemHasChildren(), you may need to call it again
  281. since DeleteChildren() does not automatically clear the setting.
  282. */
  283. virtual void DeleteChildren(const wxTreeItemId& item);
  284. /**
  285. Starts editing the label of the given @a item. This function generates a
  286. @c EVT_TREE_BEGIN_LABEL_EDIT event which can be vetoed so that no text
  287. control will appear for in-place editing.
  288. If the user changed the label (i.e. s/he does not press ESC or leave the
  289. text control without changes, a @c EVT_TREE_END_LABEL_EDIT event will be
  290. sent which can be vetoed as well.
  291. @see EndEditLabel(), wxTreeEvent
  292. */
  293. virtual wxTextCtrl *EditLabel(const wxTreeItemId& item,
  294. wxClassInfo* textCtrlClass = wxCLASSINFO(wxTextCtrl));
  295. /**
  296. Enable or disable a beep if there is no match for the currently
  297. entered text when searching for the item from keyboard.
  298. The default is to not beep in this case except in wxMSW where the
  299. beep is always generated by the native control and cannot be disabled,
  300. i.e. calls to this function do nothing there.
  301. @since 2.9.5
  302. */
  303. void EnableBellOnNoMatch(bool on = true);
  304. /**
  305. Ends label editing. If @a cancelEdit is @true, the edit will be
  306. cancelled.
  307. @note
  308. This function is currently supported under Windows only.
  309. @see EditLabel()
  310. */
  311. virtual void EndEditLabel(const wxTreeItemId& item, bool discardChanges = false);
  312. /**
  313. Scrolls and/or expands items to ensure that the given item is visible.
  314. */
  315. virtual void EnsureVisible(const wxTreeItemId& item);
  316. /**
  317. Expands the given item.
  318. */
  319. virtual void Expand(const wxTreeItemId& item);
  320. /**
  321. Expands all items in the tree.
  322. */
  323. void ExpandAll();
  324. /**
  325. Expands the given item and all its children recursively.
  326. */
  327. void ExpandAllChildren(const wxTreeItemId& item);
  328. /**
  329. Retrieves the rectangle bounding the @a item. If @a textOnly is @true,
  330. only the rectangle around the item's label will be returned, otherwise
  331. the item's image is also taken into account.
  332. The return value is @true if the rectangle was successfully retrieved or
  333. @c @false if it was not (in this case @a rect is not changed) -- for
  334. example, if the item is currently invisible.
  335. Notice that the rectangle coordinates are logical, not physical ones.
  336. So, for example, the x coordinate may be negative if the tree has a
  337. horizontal scrollbar and its position is not 0.
  338. @beginWxPerlOnly
  339. In wxPerl this method only takes the @a item and
  340. @a textOnly parameters and returns a @c Wx::Rect (or @c undef).
  341. @endWxPerlOnly
  342. */
  343. virtual bool GetBoundingRect(const wxTreeItemId& item, wxRect& rect,
  344. bool textOnly = false) const;
  345. /**
  346. Returns the buttons image list (from which application-defined button
  347. images are taken).
  348. This function is only available in the generic version.
  349. */
  350. wxImageList* GetButtonsImageList() const;
  351. /**
  352. Returns the number of items in the branch. If @a recursively is @true,
  353. returns the total number of descendants, otherwise only one level of
  354. children is counted.
  355. */
  356. virtual size_t GetChildrenCount(const wxTreeItemId& item,
  357. bool recursively = true) const;
  358. /**
  359. Returns the number of items in the control.
  360. */
  361. virtual unsigned int GetCount() const;
  362. /**
  363. Returns the edit control being currently used to edit a label. Returns
  364. @NULL if no label is being edited.
  365. @note This is currently only implemented for wxMSW.
  366. */
  367. virtual wxTextCtrl* GetEditControl() const;
  368. /**
  369. Returns the first child; call GetNextChild() for the next child.
  370. For this enumeration function you must pass in a 'cookie' parameter
  371. which is opaque for the application but is necessary for the library to
  372. make these functions reentrant (i.e. allow more than one enumeration on
  373. one and the same object simultaneously). The cookie passed to
  374. GetFirstChild() and GetNextChild() should be the same variable.
  375. Returns an invalid tree item (i.e. wxTreeItemId::IsOk() returns @false)
  376. if there are no further children.
  377. @beginWxPerlOnly
  378. In wxPerl this method only takes the @a item parameter, and
  379. returns a 2-element list (item, cookie).
  380. @endWxPerlOnly
  381. @see GetNextChild(), GetNextSibling()
  382. */
  383. virtual wxTreeItemId GetFirstChild(const wxTreeItemId& item,
  384. wxTreeItemIdValue& cookie) const;
  385. /**
  386. Returns the first visible item.
  387. */
  388. virtual wxTreeItemId GetFirstVisibleItem() const;
  389. /**
  390. Returns the item last clicked or otherwise selected.
  391. Unlike GetSelection(), it can be used whether or not
  392. the control has the @c wxTR_MULTIPLE style.
  393. @since 2.9.1
  394. */
  395. virtual wxTreeItemId GetFocusedItem() const;
  396. /**
  397. Clears the currently focused item
  398. @since 2.9.1
  399. */
  400. virtual void ClearFocusedItem();
  401. /**
  402. Sets the currently focused item.
  403. @param item
  404. The item to make the current one. It must be valid.
  405. @since 2.9.1
  406. */
  407. virtual void SetFocusedItem(const wxTreeItemId& item);
  408. /**
  409. Returns the normal image list.
  410. */
  411. wxImageList* GetImageList() const;
  412. /**
  413. Returns the current tree control indentation.
  414. */
  415. virtual unsigned int GetIndent() const;
  416. /**
  417. Returns the background colour of the item.
  418. */
  419. virtual wxColour GetItemBackgroundColour(const wxTreeItemId& item) const;
  420. /**
  421. Returns the tree item data associated with the item.
  422. @see wxTreeItemData
  423. @beginWxPerlOnly
  424. wxPerl provides the following shortcut method:
  425. - GetPlData(item): returns the Perl data
  426. associated with the Wx::TreeItemData. It is just the same as
  427. tree->GetItemData(item)->GetData().
  428. @endWxPerlOnly
  429. */
  430. virtual wxTreeItemData* GetItemData(const wxTreeItemId& item) const;
  431. /**
  432. Returns the font of the item label.
  433. If the font hadn't been explicitly set for the specified @a item with
  434. SetItemFont(), returns an invalid ::wxNullFont font. GetFont() can be
  435. used to retrieve the global tree control font used for the items
  436. without any specific font.
  437. */
  438. virtual wxFont GetItemFont(const wxTreeItemId& item) const;
  439. /**
  440. Gets the specified item image. The value of @a which may be:
  441. - ::wxTreeItemIcon_Normal: to get the normal item image.
  442. - ::wxTreeItemIcon_Selected: to get the selected item image (i.e. the
  443. image which is shown when the item is currently selected).
  444. - ::wxTreeItemIcon_Expanded: to get the expanded image (this only makes
  445. sense for items which have children - then this image is shown when
  446. the item is expanded and the normal image is shown when it is
  447. collapsed).
  448. - ::wxTreeItemIcon_SelectedExpanded: to get the selected expanded image
  449. (which is shown when an expanded item is currently selected).
  450. */
  451. virtual int GetItemImage(const wxTreeItemId& item,
  452. wxTreeItemIcon which = wxTreeItemIcon_Normal) const;
  453. /**
  454. Returns the item's parent.
  455. */
  456. virtual wxTreeItemId GetItemParent(const wxTreeItemId& item) const;
  457. /**
  458. Gets the specified item state.
  459. */
  460. int GetItemState(const wxTreeItemId& item) const;
  461. /**
  462. Returns the item label.
  463. */
  464. virtual wxString GetItemText(const wxTreeItemId& item) const;
  465. /**
  466. Returns the colour of the item label.
  467. */
  468. virtual wxColour GetItemTextColour(const wxTreeItemId& item) const;
  469. /**
  470. Returns the last child of the item (or an invalid tree item if this item
  471. has no children).
  472. @see GetFirstChild(), GetNextSibling(), GetLastChild()
  473. */
  474. virtual wxTreeItemId GetLastChild(const wxTreeItemId& item) const;
  475. /**
  476. Returns the next child; call GetFirstChild() for the first child. For
  477. this enumeration function you must pass in a 'cookie' parameter which is
  478. opaque for the application but is necessary for the library to make
  479. these functions reentrant (i.e. allow more than one enumeration on one
  480. and the same object simultaneously). The cookie passed to
  481. GetFirstChild() and GetNextChild() should be the same.
  482. Returns an invalid tree item if there are no further children.
  483. @beginWxPerlOnly
  484. In wxPerl this method returns a 2-element list
  485. (item, cookie) instead of modifying its parameters.
  486. @endWxPerlOnly
  487. @see GetFirstChild()
  488. */
  489. virtual wxTreeItemId GetNextChild(const wxTreeItemId& item,
  490. wxTreeItemIdValue& cookie) const;
  491. /**
  492. Returns the next sibling of the specified item; call GetPrevSibling()
  493. for the previous sibling.
  494. Returns an invalid tree item if there are no further siblings.
  495. @see GetPrevSibling()
  496. */
  497. virtual wxTreeItemId GetNextSibling(const wxTreeItemId& item) const;
  498. /**
  499. Returns the next visible item or an invalid item if this item is the
  500. last visible one.
  501. @note The @a item itself must be visible.
  502. */
  503. virtual wxTreeItemId GetNextVisible(const wxTreeItemId& item) const;
  504. /**
  505. Returns the previous sibling of the specified item; call
  506. GetNextSibling() for the next sibling.
  507. Returns an invalid tree item if there are no further children.
  508. @see GetNextSibling()
  509. */
  510. virtual wxTreeItemId GetPrevSibling(const wxTreeItemId& item) const;
  511. /**
  512. Returns the previous visible item or an invalid item if this item is the
  513. first visible one.
  514. @note The @a item itself must be visible.
  515. */
  516. virtual wxTreeItemId GetPrevVisible(const wxTreeItemId& item) const;
  517. /**
  518. Returns @true if the control will use a quick calculation for the best
  519. size, looking only at the first and last items. The default is @false.
  520. @see SetQuickBestSize()
  521. */
  522. bool GetQuickBestSize() const;
  523. /**
  524. Returns the root item for the tree control.
  525. */
  526. virtual wxTreeItemId GetRootItem() const;
  527. /**
  528. Returns the selection, or an invalid item if there is no selection. This
  529. function only works with the controls without @c wxTR_MULTIPLE style,
  530. use GetSelections() for the controls which do have this style
  531. or, if a single item is wanted, use GetFocusedItem().
  532. */
  533. virtual wxTreeItemId GetSelection() const;
  534. /**
  535. Fills the array of tree items passed in with the currently selected
  536. items. This function can be called only if the control has the @c
  537. wxTR_MULTIPLE style.
  538. Returns the number of selected items.
  539. @beginWxPerlOnly
  540. In wxPerl this method takes no parameters and returns a list of
  541. @c Wx::TreeItemId.
  542. @endWxPerlOnly
  543. */
  544. virtual size_t GetSelections(wxArrayTreeItemIds& selection) const;
  545. /**
  546. Returns the state image list (from which application-defined state
  547. images are taken).
  548. */
  549. wxImageList* GetStateImageList() const;
  550. /**
  551. Calculates which (if any) item is under the given @a point, returning
  552. the tree item id at this point plus extra information @a flags. @a flags
  553. is a bitlist of the following:
  554. - @c wxTREE_HITTEST_ABOVE: Above the client area.
  555. - @c wxTREE_HITTEST_BELOW: Below the client area.
  556. - @c wxTREE_HITTEST_NOWHERE: In the client area but below the last item.
  557. - @c wxTREE_HITTEST_ONITEMBUTTON: On the button associated with an item.
  558. - @c wxTREE_HITTEST_ONITEMICON: On the bitmap associated with an item.
  559. - @c wxTREE_HITTEST_ONITEMINDENT: In the indentation associated with an item.
  560. - @c wxTREE_HITTEST_ONITEMLABEL: On the label (string) associated with an item.
  561. - @c wxTREE_HITTEST_ONITEMRIGHT: In the area to the right of an item.
  562. - @c wxTREE_HITTEST_ONITEMSTATEICON: On the state icon for a tree view
  563. item that is in a user-defined state.
  564. - @c wxTREE_HITTEST_TOLEFT: To the right of the client area.
  565. - @c wxTREE_HITTEST_TORIGHT: To the left of the client area.
  566. @beginWxPerlOnly
  567. In wxPerl this method only takes the @a point parameter
  568. and returns a 2-element list (item, flags).
  569. @endWxPerlOnly
  570. */
  571. wxTreeItemId HitTest(const wxPoint& point, int& flags) const;
  572. /**
  573. Inserts an item after a given one (@a previous).
  574. The @a image and @a selImage parameters are an index within the normal
  575. image list specifying the image to use for unselected and selected
  576. items, respectively. If @a image -1 and @a selImage is -1, the same
  577. image is used for both selected and unselected items.
  578. */
  579. wxTreeItemId InsertItem(const wxTreeItemId& parent,
  580. const wxTreeItemId& previous,
  581. const wxString& text,
  582. int image = -1,
  583. int selImage = -1,
  584. wxTreeItemData* data = NULL);
  585. /**
  586. Inserts an item before one identified
  587. by its position (@a pos). @a pos must be less than or equal to
  588. the number of children.
  589. The @a image and @a selImage parameters are an index within the normal
  590. image list specifying the image to use for unselected and selected
  591. items, respectively. If @a image -1 and @a selImage is -1, the same
  592. image is used for both selected and unselected items.
  593. */
  594. wxTreeItemId InsertItem(const wxTreeItemId& parent,
  595. size_t pos,
  596. const wxString& text,
  597. int image = -1,
  598. int selImage = -1,
  599. wxTreeItemData* data = NULL);
  600. /**
  601. Returns @true if the given item is in bold state.
  602. @see SetItemBold()
  603. */
  604. virtual bool IsBold(const wxTreeItemId& item) const;
  605. /**
  606. Returns @true if the control is empty (i.e.\ has no items, even no root
  607. one).
  608. */
  609. bool IsEmpty() const;
  610. /**
  611. Returns @true if the item is expanded (only makes sense if it has
  612. children).
  613. */
  614. virtual bool IsExpanded(const wxTreeItemId& item) const;
  615. /**
  616. Returns @true if the item is selected.
  617. */
  618. virtual bool IsSelected(const wxTreeItemId& item) const;
  619. /**
  620. Returns @true if the item is visible on the screen.
  621. */
  622. virtual bool IsVisible(const wxTreeItemId& item) const;
  623. /**
  624. Returns @true if the item has children.
  625. */
  626. virtual bool ItemHasChildren(const wxTreeItemId& item) const;
  627. /**
  628. Override this function in the derived class to change the sort order of
  629. the items in the tree control. The function should return a negative,
  630. zero or positive value if the first item is less than, equal to or
  631. greater than the second one.
  632. Please note that you @b must use wxRTTI macros wxDECLARE_DYNAMIC_CLASS()
  633. and wxIMPLEMENT_DYNAMIC_CLASS() if you override this function because
  634. otherwise the base class considers that it is not overridden and uses
  635. the default comparison, i.e. sorts the items alphabetically, which
  636. allows it optimize away the calls to the virtual function completely.
  637. @see SortChildren()
  638. */
  639. virtual int OnCompareItems(const wxTreeItemId& item1,
  640. const wxTreeItemId& item2);
  641. /**
  642. Appends an item as the first child of @a parent, return a new item id.
  643. The @a image and @a selImage parameters are an index within the normal
  644. image list specifying the image to use for unselected and selected
  645. items, respectively. If @a image -1 and @a selImage is -1, the same
  646. image is used for both selected and unselected items.
  647. */
  648. wxTreeItemId PrependItem(const wxTreeItemId& parent,
  649. const wxString& text,
  650. int image = -1,
  651. int selImage = -1,
  652. wxTreeItemData* data = NULL);
  653. /**
  654. Scrolls the specified item into view.
  655. */
  656. virtual void ScrollTo(const wxTreeItemId& item);
  657. /**
  658. Selects the given item.
  659. In multiple selection controls, can be also used to deselect a
  660. currently selected item if the value of @a select is @false.
  661. Notice that calling this method will generate
  662. @c wxEVT_TREE_SEL_CHANGING and @c wxEVT_TREE_SEL_CHANGED
  663. events and that the change could be vetoed by the former event handler.
  664. */
  665. virtual void SelectItem(const wxTreeItemId& item, bool select = true);
  666. /**
  667. Sets the buttons image list (from which application-defined button
  668. images are taken).
  669. The button images assigned with this method will @b not be deleted by
  670. @ref wxTreeCtrl "wxTreeCtrl"'s destructor, you must delete it yourself.
  671. Setting or assigning the button image list enables the display of image
  672. buttons. Once enabled, the only way to disable the display of button
  673. images is to set the button image list to @NULL.
  674. @note This function is only available in the generic version.
  675. @see AssignButtonsImageList().
  676. */
  677. void SetButtonsImageList(wxImageList* imageList);
  678. /**
  679. Sets the normal image list. The image list assigned with this method
  680. will @b not be deleted by @ref wxTreeCtrl "wxTreeCtrl"'s destructor, you
  681. must delete it yourself.
  682. @see AssignImageList().
  683. */
  684. virtual void SetImageList(wxImageList* imageList);
  685. /**
  686. Sets the indentation for the tree control.
  687. */
  688. virtual void SetIndent(unsigned int indent);
  689. /**
  690. Sets the colour of the item's background.
  691. */
  692. virtual void SetItemBackgroundColour(const wxTreeItemId& item,
  693. const wxColour& col);
  694. /**
  695. Makes item appear in bold font if @a bold parameter is @true or resets
  696. it to the normal state.
  697. @see IsBold()
  698. */
  699. virtual void SetItemBold(const wxTreeItemId& item, bool bold = true);
  700. /**
  701. Sets the item client data.
  702. Notice that the client data previously associated with the @a item (if
  703. any) is @em not freed by this function and so calling this function
  704. multiple times for the same item will result in memory leaks unless you
  705. delete the old item data pointer yourself.
  706. @beginWxPerlOnly
  707. wxPerl provides the following shortcut method:
  708. - SetPlData(item, data): sets the Perl data
  709. associated with the @c Wx::TreeItemData. It is just the same as
  710. tree->GetItemData(item)->SetData(data).
  711. @endWxPerlOnly
  712. */
  713. virtual void SetItemData(const wxTreeItemId& item, wxTreeItemData* data);
  714. /**
  715. Gives the item the visual feedback for Drag'n'Drop actions, which is
  716. useful if something is dragged from the outside onto the tree control
  717. (as opposed to a DnD operation within the tree control, which already
  718. is implemented internally).
  719. */
  720. virtual void SetItemDropHighlight(const wxTreeItemId& item,
  721. bool highlight = true);
  722. /**
  723. Sets the item's font. All items in the tree should have the same height
  724. to avoid text clipping, so the fonts height should be the same for all
  725. of them, although font attributes may vary.
  726. @see SetItemBold()
  727. */
  728. virtual void SetItemFont(const wxTreeItemId& item, const wxFont& font);
  729. /**
  730. Force appearance of the button next to the item. This is useful to
  731. allow the user to expand the items which don't have any children now,
  732. but instead adding them only when needed, thus minimizing memory
  733. usage and loading time.
  734. */
  735. virtual void SetItemHasChildren(const wxTreeItemId& item,
  736. bool hasChildren = true);
  737. /**
  738. Sets the specified item's image. See GetItemImage() for the description
  739. of the @a which parameter.
  740. */
  741. virtual void SetItemImage(const wxTreeItemId& item, int image,
  742. wxTreeItemIcon which = wxTreeItemIcon_Normal);
  743. /**
  744. Sets the specified item state. The value of @a state may be:
  745. - @c wxTREE_ITEMSTATE_NONE: to disable the item state (the state image will
  746. be not displayed).
  747. - @c wxTREE_ITEMSTATE_NEXT: to set the next item state.
  748. - @c wxTREE_ITEMSTATE_PREV: to set the previous item state.
  749. */
  750. void SetItemState(const wxTreeItemId& item, int state);
  751. /**
  752. Sets the item label.
  753. */
  754. virtual void SetItemText(const wxTreeItemId& item, const wxString& text);
  755. /**
  756. Sets the colour of the item's text.
  757. */
  758. virtual void SetItemTextColour(const wxTreeItemId& item,
  759. const wxColour& col);
  760. /**
  761. If @true is passed, specifies that the control will use a quick
  762. calculation for the best size, looking only at the first and last items.
  763. Otherwise, it will look at all items. The default is @false.
  764. @see GetQuickBestSize()
  765. */
  766. void SetQuickBestSize(bool quickBestSize);
  767. /**
  768. Sets the state image list (from which application-defined state images
  769. are taken). Image list assigned with this method will @b not be deleted
  770. by @ref wxTreeCtrl "wxTreeCtrl"'s destructor, you must delete it
  771. yourself.
  772. @see AssignStateImageList().
  773. */
  774. virtual void SetStateImageList(wxImageList* imageList);
  775. /**
  776. Sets the mode flags associated with the display of the tree control. The
  777. new mode takes effect immediately.
  778. @note Generic only; MSW ignores changes.
  779. */
  780. void SetWindowStyle(long styles);
  781. /**
  782. Sorts the children of the given item using OnCompareItems().
  783. You should override that method to change the sort order (the default is
  784. ascending case-sensitive alphabetical order).
  785. @see wxTreeItemData, OnCompareItems()
  786. */
  787. virtual void SortChildren(const wxTreeItemId& item);
  788. /**
  789. Toggles the given item between collapsed and expanded states.
  790. */
  791. virtual void Toggle(const wxTreeItemId& item);
  792. /**
  793. Toggles the given item between selected and unselected states. For
  794. multiselection controls only.
  795. */
  796. void ToggleItemSelection(const wxTreeItemId& item);
  797. /**
  798. Removes the selection from the currently selected item (if any).
  799. */
  800. virtual void Unselect();
  801. /**
  802. This function either behaves the same as Unselect() if the control
  803. doesn't have @c wxTR_MULTIPLE style, or removes the selection from all
  804. items if it does have this style.
  805. */
  806. virtual void UnselectAll();
  807. /**
  808. Unselects the given item. This works in multiselection controls only.
  809. */
  810. void UnselectItem(const wxTreeItemId& item);
  811. /**
  812. Select all the immediate children of the given parent.
  813. This function can be used with multiselection controls only.
  814. @since 2.9.1
  815. */
  816. virtual void SelectChildren(const wxTreeItemId& parent);
  817. };
  818. /**
  819. @class wxTreeEvent
  820. A tree event holds information about events associated with wxTreeCtrl
  821. objects.
  822. To process input from a tree control, use these event handler macros to
  823. direct input to member functions that take a wxTreeEvent argument.
  824. @beginEventTable{wxTreeEvent}
  825. @event{EVT_TREE_BEGIN_DRAG(id, func)}
  826. Begin dragging with the left mouse button. If you want to enable
  827. left-dragging you need to intercept this event and explicitly call
  828. wxTreeEvent::Allow(), as it's vetoed by default. Also notice that the
  829. control must have an associated image list (see SetImageList()) to
  830. drag its items under MSW.
  831. @event{EVT_TREE_BEGIN_RDRAG(id, func)}
  832. Begin dragging with the right mouse button. If you want to enable
  833. right-dragging you need to intercept this event and explicitly call
  834. wxTreeEvent::Allow(), as it's vetoed by default.
  835. @event{EVT_TREE_END_DRAG(id, func)}
  836. End dragging with the left or right mouse button.
  837. @event{EVT_TREE_BEGIN_LABEL_EDIT(id, func)}
  838. Begin editing a label. This can be prevented by calling Veto().
  839. @event{EVT_TREE_END_LABEL_EDIT(id, func)}
  840. Finish editing a label. This can be prevented by calling Veto().
  841. @event{EVT_TREE_DELETE_ITEM(id, func)}
  842. Delete an item.
  843. @event{EVT_TREE_GET_INFO(id, func)}
  844. Request information from the application.
  845. @event{EVT_TREE_SET_INFO(id, func)}
  846. Information is being supplied.
  847. @event{EVT_TREE_ITEM_ACTIVATED(id, func)}
  848. The item has been activated, i.e. chosen by double clicking it with
  849. mouse or from keyboard.
  850. @event{EVT_TREE_ITEM_COLLAPSED(id, func)}
  851. The item has been collapsed.
  852. @event{EVT_TREE_ITEM_COLLAPSING(id, func)}
  853. The item is being collapsed. This can be prevented by calling Veto().
  854. @event{EVT_TREE_ITEM_EXPANDED(id, func)}
  855. The item has been expanded.
  856. @event{EVT_TREE_ITEM_EXPANDING(id, func)}
  857. The item is being expanded. This can be prevented by calling Veto().
  858. @event{EVT_TREE_ITEM_RIGHT_CLICK(id, func)}
  859. The user has clicked the item with the right mouse button.
  860. @event{EVT_TREE_ITEM_MIDDLE_CLICK(id, func)}
  861. The user has clicked the item with the middle mouse button.
  862. @event{EVT_TREE_SEL_CHANGED(id, func)}
  863. Selection has changed.
  864. @event{EVT_TREE_SEL_CHANGING(id, func)}
  865. Selection is changing. This can be prevented by calling Veto().
  866. @event{EVT_TREE_KEY_DOWN(id, func)}
  867. A key has been pressed.
  868. @event{EVT_TREE_ITEM_GETTOOLTIP(id, func)}
  869. The opportunity to set the item tooltip is being given to the
  870. application (call SetToolTip()). Windows only.
  871. @event{EVT_TREE_ITEM_MENU(id, func)}
  872. The context menu for the selected item has been requested, either by a
  873. right click or by using the menu key.
  874. @event{EVT_TREE_STATE_IMAGE_CLICK(id, func)}
  875. The state image has been clicked.
  876. @endEventTable
  877. @library{wxcore}
  878. @category{events}
  879. @see wxTreeCtrl
  880. */
  881. class wxTreeEvent : public wxNotifyEvent
  882. {
  883. public:
  884. /**
  885. Constructor, used by wxWidgets itself only.
  886. */
  887. wxTreeEvent(wxEventType commandType, wxTreeCtrl* tree,
  888. const wxTreeItemId& item = wxTreeItemId());
  889. /**
  890. Returns the item (valid for all events).
  891. */
  892. wxTreeItemId GetItem() const;
  893. /**
  894. Returns the key code if the event is a key event. Use GetKeyEvent() to
  895. get the values of the modifier keys for this event (i.e. Shift or Ctrl).
  896. */
  897. int GetKeyCode() const;
  898. /**
  899. Returns the key event for @c EVT_TREE_KEY_DOWN events.
  900. */
  901. const wxKeyEvent& GetKeyEvent() const;
  902. /**
  903. Returns the label if the event is a begin or end edit label event.
  904. */
  905. const wxString& GetLabel() const;
  906. /**
  907. Returns the old item index (valid for @c EVT_TREE_ITEM_CHANGING and
  908. @c EVT_TREE_ITEM_CHANGED events).
  909. */
  910. wxTreeItemId GetOldItem() const;
  911. /**
  912. Returns the position of the mouse pointer if the event is a drag or
  913. menu-context event.
  914. In both cases the position is in client coordinates - i.e. relative to
  915. the wxTreeCtrl window (so that you can pass it directly to e.g.
  916. wxWindow::PopupMenu()).
  917. */
  918. wxPoint GetPoint() const;
  919. /**
  920. Returns @true if the label edit was cancelled. This should be called
  921. from within an @c EVT_TREE_END_LABEL_EDIT handler.
  922. */
  923. bool IsEditCancelled() const;
  924. /**
  925. Set the tooltip for the item (valid for @c EVT_TREE_ITEM_GETTOOLTIP
  926. events). Windows only.
  927. */
  928. void SetToolTip(const wxString& tooltip);
  929. };
  930. wxEventType wxEVT_TREE_BEGIN_DRAG;
  931. wxEventType wxEVT_TREE_BEGIN_RDRAG;
  932. wxEventType wxEVT_TREE_BEGIN_LABEL_EDIT;
  933. wxEventType wxEVT_TREE_END_LABEL_EDIT;
  934. wxEventType wxEVT_TREE_DELETE_ITEM;
  935. wxEventType wxEVT_TREE_GET_INFO;
  936. wxEventType wxEVT_TREE_SET_INFO;
  937. wxEventType wxEVT_TREE_ITEM_EXPANDED;
  938. wxEventType wxEVT_TREE_ITEM_EXPANDING;
  939. wxEventType wxEVT_TREE_ITEM_COLLAPSED;
  940. wxEventType wxEVT_TREE_ITEM_COLLAPSING;
  941. wxEventType wxEVT_TREE_SEL_CHANGED;
  942. wxEventType wxEVT_TREE_SEL_CHANGING;
  943. wxEventType wxEVT_TREE_KEY_DOWN;
  944. wxEventType wxEVT_TREE_ITEM_ACTIVATED;
  945. wxEventType wxEVT_TREE_ITEM_RIGHT_CLICK;
  946. wxEventType wxEVT_TREE_ITEM_MIDDLE_CLICK;
  947. wxEventType wxEVT_TREE_END_DRAG;
  948. wxEventType wxEVT_TREE_STATE_IMAGE_CLICK;
  949. wxEventType wxEVT_TREE_ITEM_GETTOOLTIP;
  950. wxEventType wxEVT_TREE_ITEM_MENU;