1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/persist/treebook.h
3 // Purpose: persistence support for wxBookCtrl
4 // Author: Vadim Zeitlin
6 // Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
7 // Licence: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
10 #ifndef _WX_PERSIST_TREEBOOK_H_
11 #define _WX_PERSIST_TREEBOOK_H_
13 #include "wx/persist/bookctrl.h"
15 #include "wx/arrstr.h"
16 #include "wx/treebook.h"
18 // ----------------------------------------------------------------------------
19 // string constants used by wxPersistentTreeBookCtrl
20 // ----------------------------------------------------------------------------
22 #define wxPERSIST_TREEBOOK_KIND "TreeBook"
24 // this key contains the indices of all expanded nodes in the tree book
25 // separated by wxPERSIST_TREEBOOK_EXPANDED_SEP
26 #define wxPERSIST_TREEBOOK_EXPANDED_BRANCHES "Expanded"
27 #define wxPERSIST_TREEBOOK_EXPANDED_SEP ','
29 // ----------------------------------------------------------------------------
30 // wxPersistentTreeBookCtrl: supports saving/restoring open tree branches
31 // ----------------------------------------------------------------------------
33 class wxPersistentTreeBookCtrl
: public wxPersistentBookCtrl
36 wxPersistentTreeBookCtrl(wxTreebook
*book
)
37 : wxPersistentBookCtrl(book
)
41 virtual void Save() const
43 const wxTreebook
* const book
= GetTreeBook();
46 const size_t count
= book
->GetPageCount();
47 for ( size_t n
= 0; n
< count
; n
++ )
49 if ( book
->IsNodeExpanded(n
) )
51 if ( !expanded
.empty() )
52 expanded
+= wxPERSIST_TREEBOOK_EXPANDED_SEP
;
54 expanded
+= wxString::Format("%u", static_cast<unsigned>(n
));
58 SaveValue(wxPERSIST_TREEBOOK_EXPANDED_BRANCHES
, expanded
);
60 wxPersistentBookCtrl::Save();
63 virtual bool Restore()
65 wxTreebook
* const book
= GetTreeBook();
68 if ( RestoreValue(wxPERSIST_TREEBOOK_EXPANDED_BRANCHES
, &expanded
) )
71 indices(wxSplit(expanded
, wxPERSIST_TREEBOOK_EXPANDED_SEP
));
73 const size_t pageCount
= book
->GetPageCount();
74 const size_t count
= indices
.size();
75 for ( size_t n
= 0; n
< count
; n
++ )
78 if ( indices
[n
].ToULong(&idx
) && idx
< pageCount
)
79 book
->ExpandNode(idx
);
83 return wxPersistentBookCtrl::Restore();
86 virtual wxString
GetKind() const { return wxPERSIST_TREEBOOK_KIND
; }
88 wxTreebook
*GetTreeBook() const { return static_cast<wxTreebook
*>(Get()); }
91 inline wxPersistentObject
*wxCreatePersistentObject(wxTreebook
*book
)
93 return new wxPersistentTreeBookCtrl(book
);
96 #endif // _WX_PERSIST_TREEBOOK_H_