treebook.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/treebook.h
  3. // Purpose: wxTreebook: wxNotebook-like control presenting pages in a tree
  4. // Author: Evgeniy Tarassov, Vadim Zeitlin
  5. // Modified by:
  6. // Created: 2005-09-15
  7. // Copyright: (c) 2005 Vadim Zeitlin <vadim@wxwidgets.org>
  8. // Licence: wxWindows licence
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_TREEBOOK_H_
  11. #define _WX_TREEBOOK_H_
  12. #include "wx/defs.h"
  13. #if wxUSE_TREEBOOK
  14. #include "wx/bookctrl.h"
  15. #include "wx/containr.h"
  16. #include "wx/treectrl.h" // for wxArrayTreeItemIds
  17. typedef wxWindow wxTreebookPage;
  18. class WXDLLIMPEXP_FWD_CORE wxTreeEvent;
  19. // ----------------------------------------------------------------------------
  20. // wxTreebook
  21. // ----------------------------------------------------------------------------
  22. class WXDLLIMPEXP_CORE wxTreebook : public wxNavigationEnabled<wxBookCtrlBase>
  23. {
  24. public:
  25. // Constructors and such
  26. // ---------------------
  27. // Default ctor doesn't create the control, use Create() afterwards
  28. wxTreebook()
  29. {
  30. Init();
  31. }
  32. // This ctor creates the tree book control
  33. wxTreebook(wxWindow *parent,
  34. wxWindowID id,
  35. const wxPoint& pos = wxDefaultPosition,
  36. const wxSize& size = wxDefaultSize,
  37. long style = wxBK_DEFAULT,
  38. const wxString& name = wxEmptyString)
  39. {
  40. Init();
  41. (void)Create(parent, id, pos, size, style, name);
  42. }
  43. // Really creates the control
  44. bool Create(wxWindow *parent,
  45. wxWindowID id,
  46. const wxPoint& pos = wxDefaultPosition,
  47. const wxSize& size = wxDefaultSize,
  48. long style = wxBK_DEFAULT,
  49. const wxString& name = wxEmptyString);
  50. // Page insertion operations
  51. // -------------------------
  52. // Notice that page pointer may be NULL in which case the next non NULL
  53. // page (usually the first child page of a node) is shown when this page is
  54. // selected
  55. // Inserts a new page just before the page indicated by page.
  56. // The new page is placed on the same level as page.
  57. virtual bool InsertPage(size_t pos,
  58. wxWindow *page,
  59. const wxString& text,
  60. bool bSelect = false,
  61. int imageId = NO_IMAGE);
  62. // Inserts a new sub-page to the end of children of the page at given pos.
  63. virtual bool InsertSubPage(size_t pos,
  64. wxWindow *page,
  65. const wxString& text,
  66. bool bSelect = false,
  67. int imageId = NO_IMAGE);
  68. // Adds a new page at top level after all other pages.
  69. virtual bool AddPage(wxWindow *page,
  70. const wxString& text,
  71. bool bSelect = false,
  72. int imageId = NO_IMAGE);
  73. // Adds a new child-page to the last top-level page inserted.
  74. // Useful when constructing 1 level tree structure.
  75. virtual bool AddSubPage(wxWindow *page,
  76. const wxString& text,
  77. bool bSelect = false,
  78. int imageId = NO_IMAGE);
  79. // Deletes the page and ALL its children. Could trigger page selection
  80. // change in a case when selected page is removed. In that case its parent
  81. // is selected (or the next page if no parent).
  82. virtual bool DeletePage(size_t pos);
  83. // Tree operations
  84. // ---------------
  85. // Gets the page node state -- node is expanded or collapsed
  86. virtual bool IsNodeExpanded(size_t pos) const;
  87. // Expands or collapses the page node. Returns the previous state.
  88. // May generate page changing events (if selected page
  89. // is under the collapsed branch, then parent is autoselected).
  90. virtual bool ExpandNode(size_t pos, bool expand = true);
  91. // shortcut for ExpandNode(pos, false)
  92. bool CollapseNode(size_t pos) { return ExpandNode(pos, false); }
  93. // get the parent page or wxNOT_FOUND if this is a top level page
  94. int GetPageParent(size_t pos) const;
  95. // the tree control we use for showing the pages index tree
  96. wxTreeCtrl* GetTreeCtrl() const { return (wxTreeCtrl*)m_bookctrl; }
  97. // Standard operations inherited from wxBookCtrlBase
  98. // -------------------------------------------------
  99. virtual bool SetPageText(size_t n, const wxString& strText);
  100. virtual wxString GetPageText(size_t n) const;
  101. virtual int GetPageImage(size_t n) const;
  102. virtual bool SetPageImage(size_t n, int imageId);
  103. virtual int SetSelection(size_t n) { return DoSetSelection(n, SetSelection_SendEvent); }
  104. virtual int ChangeSelection(size_t n) { return DoSetSelection(n); }
  105. virtual int HitTest(const wxPoint& pt, long *flags = NULL) const;
  106. virtual void SetImageList(wxImageList *imageList);
  107. virtual void AssignImageList(wxImageList *imageList);
  108. virtual bool DeleteAllPages();
  109. protected:
  110. // Implementation of a page removal. See DeletPage for comments.
  111. wxTreebookPage *DoRemovePage(size_t pos);
  112. // This subclass of wxBookCtrlBase accepts NULL page pointers (empty pages)
  113. virtual bool AllowNullPage() const { return true; }
  114. // event handlers
  115. void OnTreeSelectionChange(wxTreeEvent& event);
  116. void OnTreeNodeExpandedCollapsed(wxTreeEvent& event);
  117. // array of page ids and page windows
  118. wxArrayTreeItemIds m_treeIds;
  119. // in the situation when m_selection page is not wxNOT_FOUND but page is
  120. // NULL this is the first (sub)child that has a non-NULL page
  121. int m_actualSelection;
  122. private:
  123. // common part of all constructors
  124. void Init();
  125. // The real implementations of page insertion functions
  126. // ------------------------------------------------------
  127. // All DoInsert/Add(Sub)Page functions add the page into :
  128. // - the base class
  129. // - the tree control
  130. // - update the index/TreeItemId corespondance array
  131. bool DoInsertPage(size_t pos,
  132. wxWindow *page,
  133. const wxString& text,
  134. bool bSelect = false,
  135. int imageId = NO_IMAGE);
  136. bool DoInsertSubPage(size_t pos,
  137. wxWindow *page,
  138. const wxString& text,
  139. bool bSelect = false,
  140. int imageId = NO_IMAGE);
  141. bool DoAddSubPage(wxWindow *page,
  142. const wxString& text,
  143. bool bSelect = false,
  144. int imageId = NO_IMAGE);
  145. // Sets selection in the tree control and updates the page being shown.
  146. int DoSetSelection(size_t pos, int flags = 0);
  147. // Returns currently shown page. In a case when selected the node
  148. // has empty (NULL) page finds first (sub)child with not-empty page.
  149. wxTreebookPage *DoGetCurrentPage() const;
  150. // Does the selection update. Called from page insertion functions
  151. // to update selection if the selected page was pushed by the newly inserted
  152. void DoUpdateSelection(bool bSelect, int page);
  153. // Operations on the internal private members of the class
  154. // -------------------------------------------------------
  155. // Returns the page TreeItemId for the page.
  156. // Or, if the page index is incorrect, a fake one (fakePage.IsOk() == false)
  157. wxTreeItemId DoInternalGetPage(size_t pos) const;
  158. // Linear search for a page with the id specified. If no page
  159. // found wxNOT_FOUND is returned. The function is used when we catch an event
  160. // from m_tree (wxTreeCtrl) component.
  161. int DoInternalFindPageById(wxTreeItemId page) const;
  162. // Updates page and wxTreeItemId correspondance.
  163. void DoInternalAddPage(size_t newPos, wxWindow *page, wxTreeItemId pageId);
  164. // Removes the page from internal structure.
  165. void DoInternalRemovePage(size_t pos)
  166. { DoInternalRemovePageRange(pos, 0); }
  167. // Removes the page and all its children designated by subCount
  168. // from internal structures of the control.
  169. void DoInternalRemovePageRange(size_t pos, size_t subCount);
  170. // Returns internal number of pages which can be different from
  171. // GetPageCount() while performing a page insertion or removal.
  172. size_t DoInternalGetPageCount() const { return m_treeIds.GetCount(); }
  173. DECLARE_EVENT_TABLE()
  174. DECLARE_DYNAMIC_CLASS_NO_COPY(wxTreebook)
  175. };
  176. // ----------------------------------------------------------------------------
  177. // treebook event class and related stuff
  178. // ----------------------------------------------------------------------------
  179. // wxTreebookEvent is obsolete and defined for compatibility only
  180. #define wxTreebookEvent wxBookCtrlEvent
  181. typedef wxBookCtrlEventFunction wxTreebookEventFunction;
  182. #define wxTreebookEventHandler(func) wxBookCtrlEventHandler(func)
  183. wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_CORE, wxEVT_TREEBOOK_PAGE_CHANGED, wxBookCtrlEvent );
  184. wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_CORE, wxEVT_TREEBOOK_PAGE_CHANGING, wxBookCtrlEvent );
  185. wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_CORE, wxEVT_TREEBOOK_NODE_COLLAPSED, wxBookCtrlEvent );
  186. wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_CORE, wxEVT_TREEBOOK_NODE_EXPANDED, wxBookCtrlEvent );
  187. #define EVT_TREEBOOK_PAGE_CHANGED(winid, fn) \
  188. wx__DECLARE_EVT1(wxEVT_TREEBOOK_PAGE_CHANGED, winid, wxBookCtrlEventHandler(fn))
  189. #define EVT_TREEBOOK_PAGE_CHANGING(winid, fn) \
  190. wx__DECLARE_EVT1(wxEVT_TREEBOOK_PAGE_CHANGING, winid, wxBookCtrlEventHandler(fn))
  191. #define EVT_TREEBOOK_NODE_COLLAPSED(winid, fn) \
  192. wx__DECLARE_EVT1(wxEVT_TREEBOOK_NODE_COLLAPSED, winid, wxBookCtrlEventHandler(fn))
  193. #define EVT_TREEBOOK_NODE_EXPANDED(winid, fn) \
  194. wx__DECLARE_EVT1(wxEVT_TREEBOOK_NODE_EXPANDED, winid, wxBookCtrlEventHandler(fn))
  195. // old wxEVT_COMMAND_* constants
  196. #define wxEVT_COMMAND_TREEBOOK_PAGE_CHANGED wxEVT_TREEBOOK_PAGE_CHANGED
  197. #define wxEVT_COMMAND_TREEBOOK_PAGE_CHANGING wxEVT_TREEBOOK_PAGE_CHANGING
  198. #define wxEVT_COMMAND_TREEBOOK_NODE_COLLAPSED wxEVT_TREEBOOK_NODE_COLLAPSED
  199. #define wxEVT_COMMAND_TREEBOOK_NODE_EXPANDED wxEVT_TREEBOOK_NODE_EXPANDED
  200. #endif // wxUSE_TREEBOOK
  201. #endif // _WX_TREEBOOK_H_