treebook.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/persist/treebook.h
  3. // Purpose: persistence support for wxBookCtrl
  4. // Author: Vadim Zeitlin
  5. // Created: 2009-01-19
  6. // Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
  7. // Licence: wxWindows licence
  8. ///////////////////////////////////////////////////////////////////////////////
  9. #ifndef _WX_PERSIST_TREEBOOK_H_
  10. #define _WX_PERSIST_TREEBOOK_H_
  11. #include "wx/persist/bookctrl.h"
  12. #include "wx/arrstr.h"
  13. #include "wx/treebook.h"
  14. // ----------------------------------------------------------------------------
  15. // string constants used by wxPersistentTreeBookCtrl
  16. // ----------------------------------------------------------------------------
  17. #define wxPERSIST_TREEBOOK_KIND "TreeBook"
  18. // this key contains the indices of all expanded nodes in the tree book
  19. // separated by wxPERSIST_TREEBOOK_EXPANDED_SEP
  20. #define wxPERSIST_TREEBOOK_EXPANDED_BRANCHES "Expanded"
  21. #define wxPERSIST_TREEBOOK_EXPANDED_SEP ','
  22. // ----------------------------------------------------------------------------
  23. // wxPersistentTreeBookCtrl: supports saving/restoring open tree branches
  24. // ----------------------------------------------------------------------------
  25. class wxPersistentTreeBookCtrl : public wxPersistentBookCtrl
  26. {
  27. public:
  28. wxPersistentTreeBookCtrl(wxTreebook *book)
  29. : wxPersistentBookCtrl(book)
  30. {
  31. }
  32. virtual void Save() const
  33. {
  34. const wxTreebook * const book = GetTreeBook();
  35. wxString expanded;
  36. const size_t count = book->GetPageCount();
  37. for ( size_t n = 0; n < count; n++ )
  38. {
  39. if ( book->IsNodeExpanded(n) )
  40. {
  41. if ( !expanded.empty() )
  42. expanded += wxPERSIST_TREEBOOK_EXPANDED_SEP;
  43. expanded += wxString::Format("%u", static_cast<unsigned>(n));
  44. }
  45. }
  46. SaveValue(wxPERSIST_TREEBOOK_EXPANDED_BRANCHES, expanded);
  47. wxPersistentBookCtrl::Save();
  48. }
  49. virtual bool Restore()
  50. {
  51. wxTreebook * const book = GetTreeBook();
  52. wxString expanded;
  53. if ( RestoreValue(wxPERSIST_TREEBOOK_EXPANDED_BRANCHES, &expanded) )
  54. {
  55. const wxArrayString
  56. indices(wxSplit(expanded, wxPERSIST_TREEBOOK_EXPANDED_SEP));
  57. const size_t pageCount = book->GetPageCount();
  58. const size_t count = indices.size();
  59. for ( size_t n = 0; n < count; n++ )
  60. {
  61. unsigned long idx;
  62. if ( indices[n].ToULong(&idx) && idx < pageCount )
  63. book->ExpandNode(idx);
  64. }
  65. }
  66. return wxPersistentBookCtrl::Restore();
  67. }
  68. virtual wxString GetKind() const { return wxPERSIST_TREEBOOK_KIND; }
  69. wxTreebook *GetTreeBook() const { return static_cast<wxTreebook *>(Get()); }
  70. };
  71. inline wxPersistentObject *wxCreatePersistentObject(wxTreebook *book)
  72. {
  73. return new wxPersistentTreeBookCtrl(book);
  74. }
  75. #endif // _WX_PERSIST_TREEBOOK_H_