persistence.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: docs/doxygen/overviews/persistence.h
  3. // Purpose: overview of persistent objects
  4. // Author: Vadim Zeitlin
  5. // Created: 2009-01-23
  6. // Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
  7. // Licence: wxWindows licence
  8. /////////////////////////////////////////////////////////////////////////////
  9. /**
  10. @page overview_persistence Persistent Objects Overview
  11. @tableofcontents
  12. Persistent objects are simply the objects which automatically save their state
  13. when they are destroyed and restore it when they are recreated, even during
  14. another program invocation.
  15. Most often, persistent objects are, in fact, persistent windows as it is
  16. especially convenient to automatically restore the UI state when the program is
  17. restarted but an object of any class can be made persistent. Moreover,
  18. persistence is implemented in a non-intrusive way so that the original object
  19. class doesn't need to be modified at all in order to add support for saving and
  20. restoring its properties.
  21. The persistence framework includes the following components:
  22. - wxPersistenceManager which all persistent objects register themselves with.
  23. This class handles actual saving and restoring of persistent data as well as
  24. various global aspects of persistence, e.g. it can be used to disable
  25. restoring the saved data.
  26. - wxPersistentObject is the base class for all persistent objects or, rather,
  27. adaptors for the persistent objects as this class main purpose is to provide
  28. the bridge between the original class -- which has no special persistence
  29. support -- and wxPersistenceManager,
  30. - wxPersistentWindow<> which derives from wxPersistentObject and implements some
  31. of its methods using wxWindow-specific functionality. Notably,
  32. wxPersistenceManager handles the destruction of persistent windows
  33. automatically implicitly while it has to be done explicitly for the
  34. arbitrary persistent objects.
  35. - wxCreatePersistentObject() function which is used to create the
  36. appropriate persistence adapter for the object.
  37. @section persistence_using Using Persistent Windows
  38. wxWidgets has built-in support for a (constantly growing) number of controls.
  39. Currently the following classes are supported:
  40. - wxTopLevelWindow (and hence wxFrame and wxDialog)
  41. - wxBookCtrlBase (i.e. wxNotebook, wxListbook, wxToolbook and wxChoicebook)
  42. - wxTreebook
  43. To automatically save and restore the properties of the windows of classes
  44. listed above you need to:
  45. -# Set a unique name for the window using wxWindow::SetName(): this step is
  46. important as the name is used in the configuration file and so must be
  47. unique among all windows of the same class.
  48. -# Call wxPersistenceManager::Register() at any moment after creating the
  49. window and then wxPersistenceManager::Restore() when the settings may be
  50. restored (which can't be always done immediately, e.g. often the window
  51. needs to be populated first). If settings can be restored immediately after
  52. the window creation, as is often the case for wxTopLevelWindow, for
  53. example, then wxPersistenceManager::RegisterAndRestore() can be used to do
  54. both at once.
  55. -# If you do not want the settings for the window to be saved (for example
  56. the changes to the dialog size are usually not saved if the dialog was
  57. cancelled), you need to call wxPersistenceManager::Unregister() manually.
  58. Otherwise the settings will be automatically saved when the control itself
  59. is destroyed.
  60. Example of using a notebook control which automatically remembers the last open
  61. page:
  62. @code
  63. wxNotebook *book = new wxNotebook(parent, wxID_ANY);
  64. book->SetName("MyBook"); // do not use the default name
  65. book->AddPage(...);
  66. book->AddPage(...);
  67. book->AddPage(...);
  68. if ( !wxPersistenceManager::RegisterAndRestore(book) )
  69. {
  70. // nothing was restored, so choose the default page ourselves
  71. book->SetSelection(0);
  72. }
  73. @endcode
  74. @section persistence_defining Defining Custom Persistent Windows
  75. User-defined classes can be easily integrated with wxPersistenceManager. To add
  76. support for your custom class @c MyWidget you just need to:
  77. -# Define a new @c MyPersistentWidget class inheriting from
  78. wxPersistentWindow<MyWidget>.
  79. -# Implement its pure virtual GetKind() method returning a unique string
  80. identifying all @c MyWidget objects, typically something like @c "widget"
  81. -# Implement its pure virtual Save() and Restore() methods to actually save
  82. and restore the widget settings using wxPersistentObject::SaveValue() and
  83. wxPersistentObject::RestoreValue() methods.
  84. -# Define wxCreatePersistentObject() overload taking @c MyWidget * and
  85. returning a new @c MyPersistentWidget object.
  86. If you want to add persistence support for a class not deriving from wxWindow,
  87. you need to derive @c MyPersistentWidget directly from wxPersistentObject and
  88. so implement its pure virtual wxPersistentObject::GetName() method too.
  89. Additionally, you must ensure that wxPersistenceManager::SaveAndUnregister() is
  90. called when your object is destroyed as this can be only done automatically for
  91. windows.
  92. */