treectrl.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/os2/treectrl.h
  3. // Purpose: wxTreeCtrl class
  4. // Author: David Webster
  5. // Modified by:
  6. // Created: 01/23/03
  7. // Copyright: (c) David Webster
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_TREECTRL_H_
  11. #define _WX_TREECTRL_H_
  12. // ----------------------------------------------------------------------------
  13. // headers
  14. // ----------------------------------------------------------------------------
  15. #if wxUSE_TREECTRL
  16. #include "wx/textctrl.h"
  17. #include "wx/dynarray.h"
  18. #include "wx/treebase.h"
  19. #include "wx/hashmap.h"
  20. // the type for "untyped" data
  21. typedef long wxDataType;
  22. // fwd decl
  23. class WXDLLIMPEXP_CORE wxImageList;
  24. class WXDLLIMPEXP_CORE wxDragImage;
  25. struct WXDLLIMPEXP_FWD_CORE wxTreeViewItem;
  26. // a callback function used for sorting tree items, it should return -1 if the
  27. // first item precedes the second, +1 if the second precedes the first or 0 if
  28. // they're equivalent
  29. class wxTreeItemData;
  30. #if WXWIN_COMPATIBILITY_2_6
  31. // flags for deprecated InsertItem() variant
  32. #define wxTREE_INSERT_FIRST 0xFFFF0001
  33. #define wxTREE_INSERT_LAST 0xFFFF0002
  34. #endif
  35. // hash storing attributes for our items
  36. WX_DECLARE_EXPORTED_VOIDPTR_HASH_MAP(wxTreeItemAttr *, wxMapTreeAttr);
  37. // ----------------------------------------------------------------------------
  38. // wxTreeCtrl
  39. // ----------------------------------------------------------------------------
  40. class WXDLLIMPEXP_CORE wxTreeCtrl : public wxControl
  41. {
  42. public:
  43. // creation
  44. // --------
  45. wxTreeCtrl() { Init(); }
  46. wxTreeCtrl( wxWindow* pParent
  47. ,wxWindowID vId = wxID_ANY
  48. ,const wxPoint& rPos = wxDefaultPosition
  49. ,const wxSize& rSize = wxDefaultSize
  50. ,long lStyle = wxTR_HAS_BUTTONS | wxTR_LINES_AT_ROOT
  51. ,const wxValidator& rValidator = wxDefaultValidator
  52. ,const wxString& rsName = wxTreeCtrlNameStr
  53. )
  54. {
  55. Create( pParent
  56. ,vId
  57. ,rPos
  58. ,rSize
  59. ,lStyle
  60. ,rValidator
  61. ,rsName
  62. );
  63. }
  64. virtual ~wxTreeCtrl();
  65. bool Create( wxWindow* pParent
  66. ,wxWindowID vId = wxID_ANY
  67. ,const wxPoint& rPos = wxDefaultPosition
  68. ,const wxSize& rSize = wxDefaultSize
  69. ,long lStyle = wxTR_HAS_BUTTONS | wxTR_LINES_AT_ROOT
  70. ,const wxValidator& rValidator = wxDefaultValidator
  71. ,const wxString& rsName = wxTreeCtrlNameStr
  72. );
  73. //
  74. // Accessors
  75. // ---------
  76. //
  77. //
  78. // Get the total number of items in the control
  79. //
  80. virtual unsigned int GetCount(void) const;
  81. //
  82. // Indent is the number of pixels the children are indented relative to
  83. // the parents position. SetIndent() also redraws the control
  84. // immediately.
  85. //
  86. unsigned int GetIndent(void) const;
  87. void SetIndent(unsigned int uIndent);
  88. //
  89. // Spacing is the number of pixels between the start and the Text
  90. //
  91. unsigned int GetSpacing(void) const { return 18; } // return wxGTK default
  92. void SetSpacing(unsigned int uSpacing) { }
  93. //
  94. // Image list: these functions allow to associate an image list with
  95. // the control and retrieve it. Note that the control does _not_ delete
  96. // the associated image list when it's deleted in order to allow image
  97. // lists to be shared between different controls.
  98. //
  99. // OS/2 doesn't really use imagelists as MSW does, but since the MSW
  100. // control is the basis for this one, until I decide how to get rid of
  101. // the need for them they are here for now.
  102. //
  103. wxImageList* GetImageList(void) const;
  104. wxImageList* GetStateImageList(void) const;
  105. void AssignImageList(wxImageList* pImageList);
  106. void AssignStateImageList(wxImageList* pImageList);
  107. void SetImageList(wxImageList* pImageList);
  108. void SetStateImageList(wxImageList* pImageList);
  109. //
  110. // Functions to work with tree ctrl items. Unfortunately, they can _not_ be
  111. // member functions of wxTreeItem because they must know the tree the item
  112. // belongs to for Windows implementation and storing the pointer to
  113. // wxTreeCtrl in each wxTreeItem is just too much waste.
  114. //
  115. // Item's label
  116. //
  117. wxString GetItemText(const wxTreeItemId& rItem) const;
  118. void SetItemText( const wxTreeItemId& rItem
  119. ,const wxString& rsText
  120. );
  121. //
  122. // One of the images associated with the item (normal by default)
  123. //
  124. int GetItemImage( const wxTreeItemId& rItem
  125. ,wxTreeItemIcon vWhich = wxTreeItemIcon_Normal
  126. ) const;
  127. void SetItemImage( const wxTreeItemId& rItem
  128. ,int nImage
  129. ,wxTreeItemIcon vWhich = wxTreeItemIcon_Normal
  130. );
  131. //
  132. // Data associated with the item
  133. //
  134. wxTreeItemData* GetItemData(const wxTreeItemId& rItem) const;
  135. void SetItemData( const wxTreeItemId& rItem
  136. ,wxTreeItemData* pData
  137. );
  138. //
  139. // Item's text colour
  140. //
  141. wxColour GetItemTextColour(const wxTreeItemId& rItem) const;
  142. void SetItemTextColour( const wxTreeItemId& rItem
  143. ,const wxColour& rColor
  144. );
  145. //
  146. // Item's background colour
  147. //
  148. wxColour GetItemBackgroundColour(const wxTreeItemId& rItem) const;
  149. void SetItemBackgroundColour( const wxTreeItemId& rItem
  150. ,const wxColour& rColour
  151. );
  152. //
  153. // Item's font
  154. //
  155. wxFont GetItemFont(const wxTreeItemId& rItem) const;
  156. void SetItemFont( const wxTreeItemId& rItem
  157. ,const wxFont& rFont
  158. );
  159. //
  160. // Force appearance of [+] button near the item. This is useful to
  161. // allow the user to expand the items which don't have any children now
  162. // - but instead add them only when needed, thus minimizing memory
  163. // usage and loading time.
  164. //
  165. void SetItemHasChildren( const wxTreeItemId& rItem
  166. ,bool bHas = true
  167. );
  168. //
  169. // The item will be shown in bold
  170. //
  171. void SetItemBold( const wxTreeItemId& rItem
  172. ,bool bBold = true
  173. );
  174. //
  175. // The item will be shown with a drop highlight
  176. //
  177. void SetItemDropHighlight( const wxTreeItemId& rItem
  178. ,bool bHighlight = true
  179. );
  180. //
  181. // Item status inquiries
  182. // ---------------------
  183. //
  184. //
  185. // Is the item visible (it might be outside the view or not expanded)?
  186. //
  187. bool IsVisible(const wxTreeItemId& rItem) const;
  188. //
  189. // Does the item has any children?
  190. //
  191. bool ItemHasChildren(const wxTreeItemId& rItem) const;
  192. //
  193. // Is the item expanded (only makes sense if HasChildren())?
  194. //
  195. bool IsExpanded(const wxTreeItemId& rItem) const;
  196. //
  197. // Is this item currently selected (the same as has focus)?
  198. //
  199. bool IsSelected(const wxTreeItemId& rItem) const;
  200. //
  201. // Is item text in bold font?
  202. //
  203. bool IsBold(const wxTreeItemId& rItem) const;
  204. //
  205. // Number of children
  206. // ------------------
  207. //
  208. //
  209. // If 'bRecursively' is false, only immediate children count, otherwise
  210. // the returned number is the number of all items in this branch
  211. //
  212. size_t GetChildrenCount( const wxTreeItemId& rItem
  213. ,bool bRecursively = true
  214. ) const;
  215. //
  216. // Navigation
  217. // ----------
  218. //
  219. //
  220. // Get the root tree item
  221. //
  222. wxTreeItemId GetRootItem(void) const;
  223. //
  224. // Get the item currently selected (may return NULL if no selection)
  225. //
  226. wxTreeItemId GetSelection(void) const;
  227. //
  228. // Get the items currently selected, return the number of such item
  229. //
  230. size_t GetSelections(wxArrayTreeItemIds& rSelections) const;
  231. //
  232. // Get the parent of this item (may return NULL if root)
  233. //
  234. wxTreeItemId GetItemParent(const wxTreeItemId& rItem) const;
  235. // for this enumeration function you must pass in a "cookie" parameter
  236. // which is opaque for the application but is necessary for the library
  237. // to make these functions reentrant (i.e. allow more than one
  238. // enumeration on one and the same object simultaneously). Of course,
  239. // the "cookie" passed to GetFirstChild() and GetNextChild() should be
  240. // the same!
  241. // get the first child of this item
  242. wxTreeItemId GetFirstChild(const wxTreeItemId& item,
  243. wxTreeItemIdValue& cookie) const;
  244. // get the next child
  245. wxTreeItemId GetNextChild(const wxTreeItemId& item,
  246. wxTreeItemIdValue& cookie) const;
  247. //
  248. // Get the last child of this item - this method doesn't use cookies
  249. //
  250. wxTreeItemId GetLastChild(const wxTreeItemId& rItem) const;
  251. //
  252. // Get the next sibling of this item
  253. //
  254. wxTreeItemId GetNextSibling(const wxTreeItemId& rItem) const;
  255. //
  256. // Get the previous sibling
  257. //
  258. wxTreeItemId GetPrevSibling(const wxTreeItemId& rItem) const;
  259. //
  260. // Get first visible item
  261. //
  262. wxTreeItemId GetFirstVisibleItem(void) const;
  263. //
  264. // Get the next visible item: item must be visible itself!
  265. // see IsVisible() and wxTreeCtrl::GetFirstVisibleItem()
  266. //
  267. wxTreeItemId GetNextVisible(const wxTreeItemId& rItem) const;
  268. //
  269. // Get the previous visible item: item must be visible itself!
  270. //
  271. wxTreeItemId GetPrevVisible(const wxTreeItemId& rItem) const;
  272. //
  273. // Operations
  274. // ----------
  275. //
  276. //
  277. // Add the root node to the tree
  278. //
  279. wxTreeItemId AddRoot( const wxString& rsText
  280. ,int nImage = -1
  281. ,int nSelectedImage = -1
  282. ,wxTreeItemData* pData = NULL
  283. );
  284. //
  285. // Insert a new item in as the first child of the parent
  286. //
  287. wxTreeItemId PrependItem( const wxTreeItemId& rParent
  288. ,const wxString& rsText
  289. ,int nImage = -1
  290. ,int nSelectedImage = -1
  291. ,wxTreeItemData* pData = NULL
  292. );
  293. //
  294. // Insert a new item after a given one
  295. //
  296. wxTreeItemId InsertItem( const wxTreeItemId& rParent
  297. ,const wxTreeItemId& rIdPrevious
  298. ,const wxString& rsText
  299. ,int nImage = -1
  300. ,int nSelectedImage = -1
  301. ,wxTreeItemData* pData = NULL
  302. );
  303. //
  304. // Insert a new item before the one with the given index
  305. //
  306. wxTreeItemId InsertItem( const wxTreeItemId& pParent
  307. ,size_t nIndex
  308. ,const wxString& rsText
  309. ,int nImage = -1
  310. ,int nSelectedImage = -1
  311. ,wxTreeItemData* pData = NULL
  312. );
  313. //
  314. // Insert a new item in as the last child of the parent
  315. //
  316. wxTreeItemId AppendItem( const wxTreeItemId& rParent
  317. ,const wxString& rsText
  318. ,int nImage = -1
  319. ,int nSelectedImage = -1
  320. ,wxTreeItemData* pData = NULL
  321. );
  322. //
  323. // Delete this item and associated data if any
  324. //
  325. void Delete(const wxTreeItemId& rItem);
  326. //
  327. // Delete all children (but don't delete the item itself)
  328. //
  329. void DeleteChildren(const wxTreeItemId& rItem);
  330. //
  331. // Delete all items from the tree
  332. //
  333. void DeleteAllItems(void);
  334. //
  335. // Expand this item
  336. //
  337. void Expand(const wxTreeItemId& rItem);
  338. //
  339. // Collapse the item without removing its children
  340. //
  341. void Collapse(const wxTreeItemId& rItem);
  342. //
  343. // Collapse the item and remove all children
  344. //
  345. void CollapseAndReset(const wxTreeItemId& rItem);
  346. //
  347. // Toggles the current state
  348. //
  349. void Toggle(const wxTreeItemId& rItem);
  350. //
  351. // Remove the selection from currently selected item (if any)
  352. //
  353. void Unselect(void);
  354. //
  355. // Unselect all items (only makes sense for multiple selection control)
  356. //
  357. void UnselectAll(void);
  358. //
  359. // Select this item
  360. //
  361. void SelectItem(const wxTreeItemId& rItem);
  362. //
  363. // Make sure this item is visible (expanding the parent item and/or
  364. // scrolling to this item if necessary)
  365. //
  366. void EnsureVisible(const wxTreeItemId& rItem);
  367. //
  368. // Scroll to this item (but don't expand its parent)
  369. //
  370. void ScrollTo(const wxTreeItemId& rItem);
  371. //
  372. // OS/2 does not use a separate edit field for editting text. Here for
  373. // interface compatibility, only.
  374. //
  375. wxTextCtrl* EditLabel( const wxTreeItemId& rItem
  376. ,wxClassInfo* pTextCtrlClass = wxCLASSINFO(wxTextCtrl)
  377. );
  378. //
  379. // returns NULL for OS/2 in ALL cases
  380. //
  381. wxTextCtrl* GetEditControl(void) const {return NULL;}
  382. //
  383. // End editing and accept or discard the changes to item label
  384. //
  385. void EndEditLabel( const wxTreeItemId& rItem
  386. ,bool bDiscardChanges = false
  387. );
  388. //
  389. // Sorting
  390. // -------
  391. //
  392. //
  393. // This function is called to compare 2 items and should return -1, 0
  394. // or +1 if the first item is less than, equal to or greater than the
  395. // second one. The base class version performs alphabetic comparaison
  396. // of item labels (GetText)
  397. //
  398. virtual int OnCompareItems( const wxTreeItemId& rItem1
  399. ,const wxTreeItemId& rItem2
  400. );
  401. //
  402. // Sort the children of this item using OnCompareItems
  403. //
  404. void SortChildren(const wxTreeItemId& rItem);
  405. //
  406. // Helpers
  407. // -------
  408. //
  409. //
  410. // Determine to which item (if any) belongs the given point (the
  411. // coordinates specified are relative to the client area of tree ctrl)
  412. // and fill the flags parameter with a bitmask of wxTREE_HITTEST_xxx
  413. // constants.
  414. //
  415. // The first function is more portable (because easier to implement
  416. // on other platforms), but the second one returns some extra info.
  417. //
  418. wxTreeItemId HitTest(const wxPoint& rPoint)
  419. { int nDummy = 0; return HitTest(rPoint, nDummy); }
  420. wxTreeItemId HitTest( const wxPoint& rPoint
  421. ,int& rFlags
  422. );
  423. //
  424. // Get the bounding rectangle of the item (or of its label only)
  425. //
  426. bool GetBoundingRect( const wxTreeItemId& rItem
  427. ,wxRect& rRect
  428. ,bool bTextOnly = false
  429. ) const;
  430. //
  431. // Implementation
  432. // --------------
  433. //
  434. virtual MRESULT OS2WindowProc( WXUINT uMsg
  435. ,WXWPARAM wParam
  436. ,WXLPARAM lParam
  437. );
  438. virtual bool OS2Command( WXUINT uParam
  439. ,WXWORD wId
  440. );
  441. // virtual bool OMSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result);
  442. //
  443. // Override some base class virtuals
  444. //
  445. virtual bool SetBackgroundColour(const wxColour& rColour);
  446. virtual bool SetForegroundColour(const wxColour& rColour);
  447. //
  448. // Get/set the check state for the item (only for wxTR_MULTIPLE)
  449. //
  450. bool IsItemChecked(const wxTreeItemId& rItem) const;
  451. void SetItemCheck( const wxTreeItemId& rItem
  452. ,bool bCheck = true
  453. );
  454. protected:
  455. //
  456. // SetImageList helper
  457. //
  458. void SetAnyImageList( wxImageList* pImageList
  459. ,int nWhich
  460. );
  461. //
  462. // Refresh a single item
  463. //
  464. void RefreshItem(const wxTreeItemId& rItem);
  465. wxImageList* m_pImageListNormal; // images for tree elements
  466. wxImageList* m_pImageListState; // special images for app defined states
  467. bool m_bOwnsImageListNormal;
  468. bool m_bOwnsImageListState;
  469. private:
  470. //
  471. // The common part of all ctors
  472. //
  473. void Init(void);
  474. //
  475. // Helper functions
  476. //
  477. inline bool DoGetItem(wxTreeViewItem* pTvItem) const;
  478. inline void DoSetItem(wxTreeViewItem* pTvItem);
  479. inline void DoExpand( const wxTreeItemId& rItem
  480. ,int nFlag
  481. );
  482. wxTreeItemId DoInsertItem( const wxTreeItemId& pParent
  483. ,wxTreeItemId hInsertAfter
  484. ,const wxString& rsText
  485. ,int nImage
  486. ,int nSelectedImage
  487. ,wxTreeItemData* pData
  488. );
  489. int DoGetItemImageFromData( const wxTreeItemId& rItem
  490. ,wxTreeItemIcon vWhich
  491. ) const;
  492. void DoSetItemImageFromData( const wxTreeItemId& rItem
  493. ,int nImage
  494. ,wxTreeItemIcon vWhich
  495. ) const;
  496. void DoSetItemImages( const wxTreeItemId& rItem
  497. ,int nImage
  498. ,int nImageSel
  499. );
  500. void DeleteTextCtrl() { }
  501. //
  502. // support for additional item images which we implement using
  503. // wxTreeItemIndirectData technique - see the comments in msw/treectrl.cpp
  504. //
  505. void SetIndirectItemData( const wxTreeItemId& rItem
  506. ,class wxTreeItemIndirectData* pData
  507. );
  508. bool HasIndirectData(const wxTreeItemId& rItem) const;
  509. bool IsDataIndirect(wxTreeItemData* pData) const
  510. { return pData && pData->GetId().m_pItem == 0; }
  511. //
  512. // The hash storing the items attributes (indexed by items ids)
  513. //
  514. wxMapTreeAttr m_vAttrs;
  515. //
  516. // true if the hash above is not empty
  517. //
  518. bool m_bHasAnyAttr;
  519. //
  520. // Used for dragging
  521. //
  522. wxDragImage* m_pDragImage;
  523. // Virtual root item, if wxTR_HIDE_ROOT is set.
  524. // void* m_pVirtualRoot;
  525. // the starting item for selection with Shift
  526. // WXHTREEITEM m_htSelStart;
  527. //
  528. friend class wxTreeItemIndirectData;
  529. friend class wxTreeSortHelper;
  530. DECLARE_DYNAMIC_CLASS(wxTreeCtrl)
  531. wxDECLARE_NO_COPY_CLASS(wxTreeCtrl);
  532. }; // end of CLASS wxTreeCtrl
  533. #endif // wxUSE_TREECTRL
  534. #endif
  535. // _WX_TREECTRL_H_