Persistent objects are simply the objects which automatically save their state when they are destroyed and restore it when they are recreated, even during another program invocation.
Most often, persistent objects are, in fact, persistent windows as it is especially convenient to automatically restore the UI state when the program is restarted but an object of any class can be made persistent. Moreover, persistence is implemented in a non-intrusive way so that the original object class doesn't need to be modified at all in order to add support for saving and restoring its properties.
The persistence framework involves
wxWidgets has built-in support for a (constantly growing) number of controls. Currently the following classes are supported:
To automatically save and restore the properties of the windows of classes listed above you need to
Example of using a notebook control which automatically remembers the last open page:
wxNotebook *book = new wxNotebook(parent, wxID_ANY); book->SetName("MyBook"); // do not use the default name book->AddPage(...); book->AddPage(...); book->AddPage(...); if ( !wxPersistenceManager::RegisterAndRestore(book) ) { // nothing was restored, so choose the default page ourselves book->SetSelection(0); }
User-defined classes can be easily integrated with wxPersistenceManager. To add support for your custom class MyWidget
you just need to:
MyPersistentWidget
class inheriting from wxPersistentWindow<MyWidget>.MyWidget
objects, typically something like "widget"
MyWidget
* and returning a new MyPersistentWidget
object.If you want to add persistence support for a class not deriving from wxWindow, you need to derive MyPersistentWidget
directly from wxPersistentObject and so implement its pure virtual wxPersistentObject::GetName() method too. Additionally, you must ensure that wxPersistenceManager::SaveAndUnregister() is called when your object is destroyed as this can be only done automatically for windows.