2009-01-30 21:38:29 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: wx/persist.h
|
|
|
|
// Purpose: interface of wxPersistenceManager and related classes
|
|
|
|
// Author: Vadim Zeitlin
|
|
|
|
// RCS-ID: $Id$
|
|
|
|
// Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
|
2010-07-13 13:29:13 +00:00
|
|
|
// Licence: wxWindows licence
|
2009-01-30 21:38:29 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/**
|
|
|
|
Provides support for automatically saving and restoring object properties
|
|
|
|
to persistent storage.
|
|
|
|
|
|
|
|
This class is the central element of wxWidgets persistence framework, see
|
|
|
|
@ref overview_persistence for its overview.
|
|
|
|
|
|
|
|
This is a singleton class and its unique instance can be retrieved using
|
|
|
|
Get() method.
|
|
|
|
|
2010-07-11 10:44:23 +00:00
|
|
|
@since 2.9.0
|
|
|
|
|
2009-01-30 21:38:29 +00:00
|
|
|
@library{wxcore}
|
|
|
|
*/
|
|
|
|
class wxPersistenceManager
|
|
|
|
{
|
|
|
|
public:
|
2011-10-30 10:08:18 +00:00
|
|
|
/**
|
|
|
|
Set the global persistence manager to use.
|
|
|
|
|
|
|
|
Call this method to specify a non-default persistence manager to use.
|
|
|
|
It should usually be called very early (e.g. in wxApp-derived class
|
|
|
|
constructor or in the beginning of overridden wxApp::OnInit()) to
|
|
|
|
affect creation of all persistent controls and the object passed to it
|
|
|
|
must have a lifetime long enough to be still alive when the persistent
|
|
|
|
controls are destroyed and need it to save their state so typically
|
|
|
|
this would be a global or a wxApp member.
|
|
|
|
|
|
|
|
@since 2.9.3
|
|
|
|
*/
|
|
|
|
static void Set(wxPersistenceManager& manager);
|
|
|
|
|
2009-01-30 21:38:29 +00:00
|
|
|
/**
|
|
|
|
Returns the unique persistence manager object.
|
2011-10-30 10:08:18 +00:00
|
|
|
|
|
|
|
If Set() hadn't been called before, a default persistence manager
|
|
|
|
implementation is returned.
|
2009-01-30 21:38:29 +00:00
|
|
|
*/
|
|
|
|
static wxPersistenceManager& Get();
|
|
|
|
|
|
|
|
/**
|
|
|
|
Globally disable saving the persistence object properties.
|
|
|
|
|
|
|
|
By default, saving properties in Save() is enabled but the program may
|
|
|
|
wish to disable if, for example, it detects that it is running on a
|
|
|
|
system which shouldn't be modified in any way and so configuration
|
|
|
|
file (or Windows registry) shouldn't be written to.
|
|
|
|
|
|
|
|
@see DisableRestoring()
|
|
|
|
*/
|
2012-04-02 16:07:28 +00:00
|
|
|
void DisableSaving();
|
2009-01-30 21:38:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
Globally disable restoring the persistence object properties.
|
|
|
|
|
|
|
|
By default, restoring properties in Restore() is enabled but this
|
|
|
|
function allows to disable it. This is mostly useful for testing.
|
|
|
|
|
|
|
|
@see DisableSaving()
|
|
|
|
*/
|
2012-04-02 16:07:28 +00:00
|
|
|
void DisableRestoring();
|
2009-01-30 21:38:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Register an object with the manager automatically creating a
|
|
|
|
persistence adapter for it.
|
|
|
|
|
|
|
|
This is equivalent to calling Register(void *, wxPersistentObject *)
|
|
|
|
with wxCreatePersistentObject(obj) as the second argument.
|
|
|
|
|
|
|
|
@param obj
|
|
|
|
The object to register. wxCreatePersistentObject() overload must be
|
|
|
|
defined for the objects of this class.
|
|
|
|
*/
|
|
|
|
template <class T>
|
2009-02-01 19:56:06 +00:00
|
|
|
wxPersistentObject *Register(T *obj);
|
2009-01-30 21:38:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
Register an object with the manager.
|
|
|
|
|
|
|
|
Note that registering the object doesn't do anything except allowing to
|
|
|
|
call Restore() for it later. If you want to register the object and
|
|
|
|
restore its properties, use RegisterAndRestore().
|
|
|
|
|
|
|
|
The manager takes ownership of @a po and will delete it when it is
|
|
|
|
unregistered.
|
|
|
|
|
|
|
|
@param obj
|
|
|
|
The object to register.
|
|
|
|
@param po
|
|
|
|
The wxPersistentObject to use for saving and restoring this object
|
|
|
|
properties.
|
|
|
|
*/
|
|
|
|
wxPersistentObject *Register(void *obj, wxPersistentObject *po);
|
|
|
|
|
|
|
|
/**
|
|
|
|
Check if the object is registered and return the associated
|
|
|
|
wxPersistentObject if it is or @NULL otherwise.
|
|
|
|
*/
|
|
|
|
wxPersistentObject *Find(void *obj) const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
Unregister the object and delete the associated wxPersistentObject.
|
|
|
|
|
|
|
|
For the persistent windows this is done automatically (via
|
|
|
|
SaveAndUnregister()) when the window is destroyed so you only need to
|
|
|
|
call this function explicitly if you are using custom persistent
|
|
|
|
objects or if you want to prevent the object properties from being
|
|
|
|
saved.
|
|
|
|
|
|
|
|
@param obj
|
|
|
|
An object previously registered with Register().
|
|
|
|
*/
|
|
|
|
void Unregister(void *obj);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Save the object properties to persistent storage.
|
|
|
|
|
|
|
|
This method does nothing if DisableSaving() had been called.
|
|
|
|
|
|
|
|
@param obj
|
|
|
|
An object previously registered with Register().
|
|
|
|
|
|
|
|
@see SaveAndUnregister()
|
|
|
|
*/
|
|
|
|
void Save(void *obj);
|
2009-02-01 19:56:06 +00:00
|
|
|
|
2009-01-30 21:38:29 +00:00
|
|
|
/**
|
|
|
|
Restore the object properties previously saved by Save().
|
|
|
|
|
|
|
|
This method does nothing if DisableRestoring() had been called.
|
|
|
|
|
|
|
|
@param obj
|
|
|
|
An object previously registered with Register().
|
|
|
|
@return
|
|
|
|
@true if the object properties were restored or @false if nothing
|
|
|
|
was found to restore or the saved settings were invalid.
|
|
|
|
|
|
|
|
@see RegisterAndRestore()
|
|
|
|
*/
|
|
|
|
bool Restore(void *obj);
|
|
|
|
|
|
|
|
/// Combines both Save() and Unregister() calls.
|
|
|
|
void SaveAndUnregister(void *obj);
|
|
|
|
|
|
|
|
/// Combines both Register() and Restore() calls.
|
|
|
|
//@{
|
|
|
|
template <class T>
|
|
|
|
bool RegisterAndRestore(T *obj);
|
|
|
|
|
|
|
|
bool RegisterAndRestore(void *obj, wxPersistentObject *po);
|
|
|
|
//@}
|
2011-10-30 10:08:18 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
/**
|
|
|
|
Protected default constructor.
|
|
|
|
|
|
|
|
This constructor is only provided for the derived classes, to use an
|
|
|
|
object of this class static Get() method should be called.
|
|
|
|
*/
|
|
|
|
wxPersistenceManager();
|
|
|
|
|
|
|
|
/**
|
|
|
|
Return the config object to use.
|
|
|
|
|
|
|
|
By default the global wxConfig, returned by wxConfigBase::Get(), is
|
|
|
|
used but a derived class could override this function to return a
|
|
|
|
different one if necessary.
|
|
|
|
|
|
|
|
@since 2.9.3
|
|
|
|
*/
|
|
|
|
virtual wxConfigBase *GetConfig() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
Return the path to use for saving the setting with the given name for
|
|
|
|
the specified object.
|
|
|
|
|
|
|
|
Notice that the @a name argument is the name of the setting, not the
|
|
|
|
name of the object itself which can be retrieved with its GetName()
|
|
|
|
method.
|
|
|
|
|
|
|
|
This method can be overridden by a derived class to change where in
|
|
|
|
wxConfig the different options are stored. By default, all settings of
|
|
|
|
the persistent controls are stored under "Persistent_Options" group and
|
|
|
|
grouped by control type (e.g. "Window" for top level windows or
|
|
|
|
"Splitter") and name, so that the position of a splitter called "sep"
|
|
|
|
could be stored under "Persistent_Options/Splitter/sep/Position" key.
|
|
|
|
|
|
|
|
@since 2.9.3
|
|
|
|
*/
|
|
|
|
virtual wxString GetKey(const wxPersistentObject& who,
|
|
|
|
const wxString& name) const;
|
2009-01-30 21:38:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
Base class for persistent object adapters.
|
|
|
|
|
|
|
|
wxWidgets persistence framework is non-intrusive, i.e. can work with the
|
|
|
|
classes which have no relationship to nor knowledge of it. To allow this,
|
|
|
|
an intermediate persistence adapter is used: this is just a simple object
|
|
|
|
which provides the methods used by wxPersistenceManager to save and restore
|
|
|
|
the object properties and implements them using the concrete class methods.
|
|
|
|
|
|
|
|
You may derive your own classes from wxPersistentObject to implement
|
|
|
|
persistence support for your common classes, see @ref persistence_defining.
|
|
|
|
|
|
|
|
@see wxPersistentWindow<>
|
|
|
|
*/
|
|
|
|
class wxPersistentObject
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
Constructor takes the object which we're associated with.
|
|
|
|
|
|
|
|
This object must have life-time greater than ours as we keep a pointer
|
|
|
|
to it.
|
|
|
|
*/
|
|
|
|
wxPersistentObject(void *obj);
|
|
|
|
|
|
|
|
/// Trivial but virtual destructor.
|
|
|
|
virtual ~wxPersistentObject();
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@name Methods to be implemented in the derived classes.
|
|
|
|
|
|
|
|
Notice that these methods are only used by wxPersistenceManager
|
|
|
|
normally and shouldn't be called directly.
|
|
|
|
*/
|
|
|
|
//@{
|
|
|
|
|
|
|
|
/**
|
|
|
|
Save the object properties.
|
|
|
|
|
|
|
|
The implementation of this method should use SaveValue().
|
|
|
|
*/
|
|
|
|
virtual void Save() const = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
Restore the object properties.
|
|
|
|
|
|
|
|
The implementation of this method should use RestoreValue().
|
|
|
|
*/
|
|
|
|
virtual bool Restore() = 0;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Returns the string uniquely identifying the objects supported by this
|
|
|
|
adapter.
|
|
|
|
|
|
|
|
This method is called from SaveValue() and RestoreValue() and normally
|
|
|
|
returns some short (but not too cryptic) strings, e.g. @c "Checkbox".
|
|
|
|
*/
|
|
|
|
virtual wxString GetKind() const = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
Returns the string uniquely identifying the object we're associated
|
|
|
|
with among all the other objects of the same type.
|
|
|
|
|
|
|
|
This method is used together with GetKind() to construct the unique
|
|
|
|
full name of the object in e.g. a configuration file.
|
|
|
|
*/
|
|
|
|
virtual wxString GetName() const = 0;
|
|
|
|
|
|
|
|
//@}
|
|
|
|
|
|
|
|
|
|
|
|
/// Return the associated object.
|
|
|
|
void *GetObject() const;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
/**
|
|
|
|
Save the specified value using the given name.
|
|
|
|
|
|
|
|
@param name
|
|
|
|
The name of the value in the configuration file.
|
|
|
|
@param value
|
|
|
|
The value to save, currently must be a type supported by wxConfig.
|
|
|
|
@return
|
|
|
|
@true if the value was saved or @false if an error occurred.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
2009-02-01 19:56:06 +00:00
|
|
|
bool SaveValue(const wxString& name, T value) const;
|
2009-01-30 21:38:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
Restore the value saved by Save().
|
|
|
|
|
|
|
|
@param name
|
|
|
|
The same name as was used by Save().
|
|
|
|
@param value
|
|
|
|
Non-@NULL pointer which will be filled with the value if it was
|
|
|
|
read successfully or not modified if it wasn't.
|
|
|
|
@return
|
|
|
|
@true if the value was successfully read or @false if it was not
|
|
|
|
found or an error occurred.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
2009-02-01 19:56:06 +00:00
|
|
|
bool RestoreValue(const wxString& name, T *value);
|
2009-01-30 21:38:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
Function used to create the correct persistent adapter for the given type
|
|
|
|
of objects.
|
|
|
|
|
|
|
|
To be precise, there is no such template function definition but there are
|
|
|
|
overloads of wxCreatePersistentObject() taking different object types for
|
|
|
|
all wxWidgets classes supporting persistence. And you may also define your
|
|
|
|
own overloads to integrate your custom classes with wxWidgets persistence
|
|
|
|
framework.
|
|
|
|
|
|
|
|
@see @ref persistence_defining
|
2009-02-03 12:02:20 +00:00
|
|
|
|
|
|
|
@header{wx/persist.h}
|
2009-01-30 21:38:29 +00:00
|
|
|
*/
|
|
|
|
template <class T>
|
|
|
|
wxPersistentObject *wxCreatePersistentObject(T *obj);
|
2009-02-03 12:02:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
A shorter synonym for wxPersistenceManager::RegisterAndRestore().
|
|
|
|
|
|
|
|
This function simply calls wxPersistenceManager::RegisterAndRestore() but
|
|
|
|
using it results in slightly shorter code as it calls
|
2010-07-11 10:44:23 +00:00
|
|
|
wxPersistenceManager::Get() internally. As an additional convenience, this
|
|
|
|
function can also set the window name.
|
2009-02-03 12:02:20 +00:00
|
|
|
|
2010-07-11 10:44:23 +00:00
|
|
|
For the implementation reasons, this function @em must be used instead of
|
2009-02-03 12:02:20 +00:00
|
|
|
the template method when using Microsoft Visual C++ 6 compiler.
|
|
|
|
|
2010-07-11 10:44:23 +00:00
|
|
|
@param obj wxWindow-derived object to register with persistence manager and
|
|
|
|
to try to restore the settings for.
|
|
|
|
@param name If not empty, @a obj name is changed to the provided value
|
|
|
|
before registering it.
|
|
|
|
@return true if the settings were restored or false otherwise (this will
|
|
|
|
always be the case when the program runs for the first time, for
|
|
|
|
example).
|
|
|
|
|
|
|
|
@since 2.9.0, @a name is new in 2.9.1.
|
|
|
|
|
2009-02-03 12:02:20 +00:00
|
|
|
@header{wx/persist.h}
|
|
|
|
*/
|
|
|
|
template <class T>
|
2010-07-11 10:44:23 +00:00
|
|
|
bool wxPersistentRegisterAndRestore(T *obj, const wxString& name = wxString());
|