| 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 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) 2005 Vadim Zeitlin <vadim@wxwidgets.org> |
| 9 | // Licence: wxWindows licence |
| 10 | /////////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | #ifndef _WX_TREEBOOK_H_ |
| 13 | #define _WX_TREEBOOK_H_ |
| 14 | |
| 15 | #include "wx/defs.h" |
| 16 | |
| 17 | #if wxUSE_TREEBOOK |
| 18 | |
| 19 | #include "wx/bookctrl.h" |
| 20 | #include "wx/treectrl.h" // for wxArrayTreeItemIds |
| 21 | |
| 22 | typedef wxWindow wxTreebookPage; |
| 23 | |
| 24 | class WXDLLEXPORT wxTreeEvent; |
| 25 | |
| 26 | // ---------------------------------------------------------------------------- |
| 27 | // wxTreebook |
| 28 | // ---------------------------------------------------------------------------- |
| 29 | |
| 30 | class WXDLLEXPORT wxTreebook : public wxBookCtrlBase |
| 31 | { |
| 32 | public: |
| 33 | // Constructors and such |
| 34 | // --------------------- |
| 35 | |
| 36 | // Default ctor doesn't create the control, use Create() afterwards |
| 37 | wxTreebook() |
| 38 | { |
| 39 | Init(); |
| 40 | } |
| 41 | |
| 42 | // This ctor creates the tree book control |
| 43 | wxTreebook(wxWindow *parent, |
| 44 | wxWindowID id, |
| 45 | const wxPoint& pos = wxDefaultPosition, |
| 46 | const wxSize& size = wxDefaultSize, |
| 47 | long style = wxBK_DEFAULT, |
| 48 | const wxString& name = wxEmptyString) |
| 49 | { |
| 50 | Init(); |
| 51 | |
| 52 | (void)Create(parent, id, pos, size, style, name); |
| 53 | } |
| 54 | |
| 55 | // Really creates the control |
| 56 | bool Create(wxWindow *parent, |
| 57 | wxWindowID id, |
| 58 | const wxPoint& pos = wxDefaultPosition, |
| 59 | const wxSize& size = wxDefaultSize, |
| 60 | long style = wxBK_DEFAULT, |
| 61 | const wxString& name = wxEmptyString); |
| 62 | |
| 63 | |
| 64 | // Page insertion operations |
| 65 | // ------------------------- |
| 66 | |
| 67 | // Notice that page pointer may be NULL in which case the next non NULL |
| 68 | // page (usually the first child page of a node) is shown when this page is |
| 69 | // selected |
| 70 | |
| 71 | // Inserts a new page just before the page indicated by page. |
| 72 | // The new page is placed on the same level as page. |
| 73 | virtual bool InsertPage(size_t pos, |
| 74 | wxWindow *page, |
| 75 | const wxString& text, |
| 76 | bool bSelect = false, |
| 77 | int imageId = wxNOT_FOUND); |
| 78 | |
| 79 | // Inserts a new sub-page to the end of children of the page at given pos. |
| 80 | virtual bool InsertSubPage(size_t pos, |
| 81 | wxWindow *page, |
| 82 | const wxString& text, |
| 83 | bool bSelect = false, |
| 84 | int imageId = wxNOT_FOUND); |
| 85 | |
| 86 | // Adds a new page at top level after all other pages. |
| 87 | virtual bool AddPage(wxWindow *page, |
| 88 | const wxString& text, |
| 89 | bool bSelect = false, |
| 90 | int imageId = wxNOT_FOUND); |
| 91 | |
| 92 | // Adds a new child-page to the last top-level page inserted. |
| 93 | // Useful when constructing 1 level tree structure. |
| 94 | virtual bool AddSubPage(wxWindow *page, |
| 95 | const wxString& text, |
| 96 | bool bSelect = false, |
| 97 | int imageId = wxNOT_FOUND); |
| 98 | |
| 99 | // Deletes the page and ALL its children. Could trigger page selection |
| 100 | // change in a case when selected page is removed. In that case its parent |
| 101 | // is selected (or the next page if no parent). |
| 102 | virtual bool DeletePage(size_t pos); |
| 103 | |
| 104 | |
| 105 | // Tree operations |
| 106 | // --------------- |
| 107 | |
| 108 | // Gets the page node state -- node is expanded or collapsed |
| 109 | virtual bool IsNodeExpanded(size_t pos) const; |
| 110 | |
| 111 | // Expands or collapses the page node. Returns the previous state. |
| 112 | // May generate page changing events (if selected page |
| 113 | // is under the collapsed branch, then parent is autoselected). |
| 114 | virtual bool ExpandNode(size_t pos, bool expand = true); |
| 115 | |
| 116 | // shortcut for ExpandNode(pos, false) |
| 117 | bool CollapseNode(size_t pos) { return ExpandNode(pos, false); } |
| 118 | |
| 119 | // get the parent page or wxNOT_FOUND if this is a top level page |
| 120 | int GetPageParent(size_t pos) const; |
| 121 | |
| 122 | // the tree control we use for showing the pages index tree |
| 123 | wxTreeCtrl* GetTreeCtrl() const { return (wxTreeCtrl*)m_bookctrl; } |
| 124 | |
| 125 | |
| 126 | // Standard operations inherited from wxBookCtrlBase |
| 127 | // ------------------------------------------------- |
| 128 | |
| 129 | virtual int GetSelection() const; |
| 130 | virtual bool SetPageText(size_t n, const wxString& strText); |
| 131 | virtual wxString GetPageText(size_t n) const; |
| 132 | virtual int GetPageImage(size_t n) const; |
| 133 | virtual bool SetPageImage(size_t n, int imageId); |
| 134 | virtual wxSize CalcSizeFromPage(const wxSize& sizePage) const; |
| 135 | virtual int SetSelection(size_t n); |
| 136 | virtual void SetImageList(wxImageList *imageList); |
| 137 | virtual void AssignImageList(wxImageList *imageList); |
| 138 | virtual bool DeleteAllPages(); |
| 139 | |
| 140 | protected: |
| 141 | // Implementation of a page removal. See DeletPage for comments. |
| 142 | wxTreebookPage *DoRemovePage(size_t pos); |
| 143 | |
| 144 | // This subclass of wxBookCtrlBase accepts NULL page pointers (empty pages) |
| 145 | virtual bool AllowNullPage() const { return true; } |
| 146 | |
| 147 | // event handlers |
| 148 | void OnTreeSelectionChange(wxTreeEvent& event); |
| 149 | void OnTreeNodeExpandedCollapsed(wxTreeEvent& event); |
| 150 | |
| 151 | // array of page ids and page windows |
| 152 | wxArrayTreeItemIds m_treeIds; |
| 153 | |
| 154 | // the currently selected page or wxNOT_FOUND if none |
| 155 | int m_selection; |
| 156 | |
| 157 | // in the situation when m_selection page is not wxNOT_FOUND but page is |
| 158 | // NULL this is the first (sub)child that has a non-NULL page |
| 159 | int m_actualSelection; |
| 160 | |
| 161 | private: |
| 162 | // common part of all constructors |
| 163 | void Init(); |
| 164 | |
| 165 | // The real implementations of page insertion functions |
| 166 | // ------------------------------------------------------ |
| 167 | // All DoInsert/Add(Sub)Page functions add the page into : |
| 168 | // - the base class |
| 169 | // - the tree control |
| 170 | // - update the index/TreeItemId corespondance array |
| 171 | bool DoInsertPage(size_t pos, |
| 172 | wxWindow *page, |
| 173 | const wxString& text, |
| 174 | bool bSelect = false, |
| 175 | int imageId = wxNOT_FOUND); |
| 176 | bool DoInsertSubPage(size_t pos, |
| 177 | wxWindow *page, |
| 178 | const wxString& text, |
| 179 | bool bSelect = false, |
| 180 | int imageId = wxNOT_FOUND); |
| 181 | bool DoAddSubPage(wxWindow *page, |
| 182 | const wxString& text, |
| 183 | bool bSelect = false, |
| 184 | int imageId = wxNOT_FOUND); |
| 185 | |
| 186 | // Sets selection in the tree control and updates the page being shown. |
| 187 | int DoSetSelection(size_t pos); |
| 188 | |
| 189 | // Returns currently shown page. In a case when selected the node |
| 190 | // has empty (NULL) page finds first (sub)child with not-empty page. |
| 191 | wxTreebookPage *DoGetCurrentPage() const; |
| 192 | |
| 193 | // Does the selection update. Called from page insertion functions |
| 194 | // to update selection if the selected page was pushed by the newly inserted |
| 195 | void DoUpdateSelection(bool bSelect, int page); |
| 196 | |
| 197 | |
| 198 | // Operations on the internal private members of the class |
| 199 | // ------------------------------------------------------- |
| 200 | // Returns the page TreeItemId for the page. |
| 201 | // Or, if the page index is incorrect, a fake one (fakePage.IsOk() == false) |
| 202 | wxTreeItemId DoInternalGetPage(size_t pos) const; |
| 203 | |
| 204 | // Linear search for a page with the id specified. If no page |
| 205 | // found wxNOT_FOUND is returned. The function is used when we catch an event |
| 206 | // from m_tree (wxTreeCtrl) component. |
| 207 | int DoInternalFindPageById(wxTreeItemId page) const; |
| 208 | |
| 209 | // Updates page and wxTreeItemId correspondance. |
| 210 | void DoInternalAddPage(size_t newPos, wxWindow *page, wxTreeItemId pageId); |
| 211 | |
| 212 | // Removes the page from internal structure. |
| 213 | void DoInternalRemovePage(size_t pos) |
| 214 | { DoInternalRemovePageRange(pos, 0); } |
| 215 | |
| 216 | // Removes the page and all its children designated by subCount |
| 217 | // from internal structures of the control. |
| 218 | void DoInternalRemovePageRange(size_t pos, size_t subCount); |
| 219 | |
| 220 | // Returns internal number of pages which can be different from |
| 221 | // GetPageCount() while performing a page insertion or removal. |
| 222 | size_t DoInternalGetPageCount() const { return m_treeIds.Count(); } |
| 223 | |
| 224 | |
| 225 | DECLARE_EVENT_TABLE() |
| 226 | DECLARE_DYNAMIC_CLASS_NO_COPY(wxTreebook) |
| 227 | }; |
| 228 | |
| 229 | |
| 230 | // ---------------------------------------------------------------------------- |
| 231 | // treebook event class and related stuff |
| 232 | // ---------------------------------------------------------------------------- |
| 233 | |
| 234 | class WXDLLEXPORT wxTreebookEvent : public wxBookCtrlBaseEvent |
| 235 | { |
| 236 | public: |
| 237 | wxTreebookEvent(wxEventType commandType = wxEVT_NULL, int id = 0, |
| 238 | int nSel = wxNOT_FOUND, int nOldSel = wxNOT_FOUND) |
| 239 | : wxBookCtrlBaseEvent(commandType, id, nSel, nOldSel) |
| 240 | { |
| 241 | } |
| 242 | |
| 243 | wxTreebookEvent(const wxTreebookEvent& event) |
| 244 | : wxBookCtrlBaseEvent(event) |
| 245 | { |
| 246 | } |
| 247 | |
| 248 | virtual wxEvent *Clone() const { return new wxTreebookEvent(*this); } |
| 249 | |
| 250 | private: |
| 251 | DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxTreebookEvent) |
| 252 | }; |
| 253 | |
| 254 | extern WXDLLIMPEXP_CORE const wxEventType wxEVT_COMMAND_TREEBOOK_PAGE_CHANGED; |
| 255 | extern WXDLLIMPEXP_CORE const wxEventType wxEVT_COMMAND_TREEBOOK_PAGE_CHANGING; |
| 256 | extern WXDLLIMPEXP_CORE const wxEventType wxEVT_COMMAND_TREEBOOK_NODE_COLLAPSED; |
| 257 | extern WXDLLIMPEXP_CORE const wxEventType wxEVT_COMMAND_TREEBOOK_NODE_EXPANDED; |
| 258 | |
| 259 | typedef void (wxEvtHandler::*wxTreebookEventFunction)(wxTreebookEvent&); |
| 260 | |
| 261 | #define wxTreebookEventHandler(func) \ |
| 262 | (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(wxTreebookEventFunction, &func) |
| 263 | |
| 264 | #define EVT_TREEBOOK_PAGE_CHANGED(winid, fn) \ |
| 265 | wx__DECLARE_EVT1(wxEVT_COMMAND_TREEBOOK_PAGE_CHANGED, winid, wxTreebookEventHandler(fn)) |
| 266 | |
| 267 | #define EVT_TREEBOOK_PAGE_CHANGING(winid, fn) \ |
| 268 | wx__DECLARE_EVT1(wxEVT_COMMAND_TREEBOOK_PAGE_CHANGING, winid, wxTreebookEventHandler(fn)) |
| 269 | |
| 270 | #define EVT_TREEBOOK_NODE_COLLAPSED(winid, fn) \ |
| 271 | wx__DECLARE_EVT1(wxEVT_COMMAND_TREEBOOK_NODE_COLLAPSED, winid, wxTreebookEventHandler(fn)) |
| 272 | |
| 273 | #define EVT_TREEBOOK_NODE_EXPANDED(winid, fn) \ |
| 274 | wx__DECLARE_EVT1(wxEVT_COMMAND_TREEBOOK_NODE_EXPANDED, winid, wxTreebookEventHandler(fn)) |
| 275 | |
| 276 | |
| 277 | #endif // wxUSE_TREEBOOK |
| 278 | |
| 279 | #endif // _WX_TREEBOOK_H_ |