1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/persist/treebook.h
3 // Purpose: persistence support for wxBookCtrl
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_PERSIST_TREEBOOK_H_
12 #define _WX_PERSIST_TREEBOOK_H_
14 #include "wx/persist/bookctrl.h"
16 #include "wx/arrstr.h"
17 #include "wx/treebook.h"
19 // ----------------------------------------------------------------------------
20 // string constants used by wxPersistentTreeBookCtrl
21 // ----------------------------------------------------------------------------
23 #define wxPERSIST_TREEBOOK_KIND "TreeBook"
25 // this key contains the indices of all expanded nodes in the tree book
26 // separated by wxPERSIST_TREEBOOK_EXPANDED_SEP
27 #define wxPERSIST_TREEBOOK_EXPANDED_BRANCHES "Expanded"
28 #define wxPERSIST_TREEBOOK_EXPANDED_SEP ','
30 // ----------------------------------------------------------------------------
31 // wxPersistentTreeBookCtrl: supports saving/restoring open tree branches
32 // ----------------------------------------------------------------------------
34 class wxPersistentTreeBookCtrl
: public wxPersistentBookCtrl
37 wxPersistentTreeBookCtrl(wxTreebook
*book
)
38 : wxPersistentBookCtrl(book
)
42 virtual void Save() const
44 const wxTreebook
* const book
= GetTreeBook();
47 const size_t count
= book
->GetPageCount();
48 for ( size_t n
= 0; n
< count
; n
++ )
50 if ( book
->IsNodeExpanded(n
) )
52 if ( !expanded
.empty() )
53 expanded
+= wxPERSIST_TREEBOOK_EXPANDED_SEP
;
55 expanded
+= wxString::Format("%u", static_cast<unsigned>(n
));
59 SaveValue(wxPERSIST_TREEBOOK_EXPANDED_BRANCHES
, expanded
);
61 wxPersistentBookCtrl::Save();
64 virtual bool Restore()
66 wxTreebook
* const book
= GetTreeBook();
69 if ( RestoreValue(wxPERSIST_TREEBOOK_EXPANDED_BRANCHES
, &expanded
) )
72 indices(wxSplit(expanded
, wxPERSIST_TREEBOOK_EXPANDED_SEP
));
74 const size_t pageCount
= book
->GetPageCount();
75 const size_t count
= indices
.size();
76 for ( size_t n
= 0; n
< count
; n
++ )
79 if ( indices
[n
].ToULong(&idx
) && idx
< pageCount
)
80 book
->ExpandNode(idx
);
84 return wxPersistentBookCtrl::Restore();
87 virtual wxString
GetKind() const { return wxPERSIST_TREEBOOK_KIND
; }
89 wxTreebook
*GetTreeBook() const { return static_cast<wxTreebook
*>(Get()); }
92 inline wxPersistentObject
*wxCreatePersistentObject(wxTreebook
*book
)
94 return new wxPersistentTreeBookCtrl(book
);
97 #endif // _WX_PERSIST_TREEBOOK_H_