]>
git.saurik.com Git - wxWidgets.git/blob - interface/wx/persist.h
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: interface of wxPersistenceManager and related classes
4 // Author: Vadim Zeitlin
5 // Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
10 Provides support for automatically saving and restoring object properties
11 to persistent storage.
13 This class is the central element of wxWidgets persistence framework, see
14 @ref overview_persistence for its overview.
16 This is a singleton class and its unique instance can be retrieved using
23 class wxPersistenceManager
27 Set the global persistence manager to use.
29 Call this method to specify a non-default persistence manager to use.
30 It should usually be called very early (e.g. in wxApp-derived class
31 constructor or in the beginning of overridden wxApp::OnInit()) to
32 affect creation of all persistent controls and the object passed to it
33 must have a lifetime long enough to be still alive when the persistent
34 controls are destroyed and need it to save their state so typically
35 this would be a global or a wxApp member.
39 static void Set(wxPersistenceManager
& manager
);
42 Returns the unique persistence manager object.
44 If Set() hadn't been called before, a default persistence manager
45 implementation is returned.
47 static wxPersistenceManager
& Get();
50 Globally disable saving the persistence object properties.
52 By default, saving properties in Save() is enabled but the program may
53 wish to disable if, for example, it detects that it is running on a
54 system which shouldn't be modified in any way and so configuration
55 file (or Windows registry) shouldn't be written to.
57 @see DisableRestoring()
62 Globally disable restoring the persistence object properties.
64 By default, restoring properties in Restore() is enabled but this
65 function allows to disable it. This is mostly useful for testing.
69 void DisableRestoring();
73 Register an object with the manager automatically creating a
74 persistence adapter for it.
76 This is equivalent to calling Register(void *, wxPersistentObject *)
77 with wxCreatePersistentObject(obj) as the second argument.
80 The object to register. wxCreatePersistentObject() overload must be
81 defined for the objects of this class.
84 wxPersistentObject
*Register(T
*obj
);
87 Register an object with the manager.
89 Note that registering the object doesn't do anything except allowing to
90 call Restore() for it later. If you want to register the object and
91 restore its properties, use RegisterAndRestore().
93 The manager takes ownership of @a po and will delete it when it is
97 The object to register.
99 The wxPersistentObject to use for saving and restoring this object
102 wxPersistentObject
*Register(void *obj
, wxPersistentObject
*po
);
105 Check if the object is registered and return the associated
106 wxPersistentObject if it is or @NULL otherwise.
108 wxPersistentObject
*Find(void *obj
) const;
111 Unregister the object and delete the associated wxPersistentObject.
113 For the persistent windows this is done automatically (via
114 SaveAndUnregister()) when the window is destroyed so you only need to
115 call this function explicitly if you are using custom persistent
116 objects or if you want to prevent the object properties from being
120 An object previously registered with Register().
122 void Unregister(void *obj
);
126 Save the object properties to persistent storage.
128 This method does nothing if DisableSaving() had been called.
131 An object previously registered with Register().
133 @see SaveAndUnregister()
135 void Save(void *obj
);
138 Restore the object properties previously saved by Save().
140 This method does nothing if DisableRestoring() had been called.
143 An object previously registered with Register().
145 @true if the object properties were restored or @false if nothing
146 was found to restore or the saved settings were invalid.
148 @see RegisterAndRestore()
150 bool Restore(void *obj
);
152 /// Combines both Save() and Unregister() calls.
153 void SaveAndUnregister(void *obj
);
155 /// Combines both Register() and Restore() calls.
158 bool RegisterAndRestore(T
*obj
);
160 bool RegisterAndRestore(void *obj
, wxPersistentObject
*po
);
165 Protected default constructor.
167 This constructor is only provided for the derived classes, to use an
168 object of this class static Get() method should be called.
170 wxPersistenceManager();
173 Return the config object to use.
175 By default the global wxConfig, returned by wxConfigBase::Get(), is
176 used but a derived class could override this function to return a
177 different one if necessary.
181 virtual wxConfigBase
*GetConfig() const;
184 Return the path to use for saving the setting with the given name for
185 the specified object.
187 Notice that the @a name argument is the name of the setting, not the
188 name of the object itself which can be retrieved with its GetName()
191 This method can be overridden by a derived class to change where in
192 wxConfig the different options are stored. By default, all settings of
193 the persistent controls are stored under "Persistent_Options" group and
194 grouped by control type (e.g. "Window" for top level windows or
195 "Splitter") and name, so that the position of a splitter called "sep"
196 could be stored under "Persistent_Options/Splitter/sep/Position" key.
200 virtual wxString
GetKey(const wxPersistentObject
& who
,
201 const wxString
& name
) const;
205 Base class for persistent object adapters.
207 wxWidgets persistence framework is non-intrusive, i.e. can work with the
208 classes which have no relationship to nor knowledge of it. To allow this,
209 an intermediate persistence adapter is used: this is just a simple object
210 which provides the methods used by wxPersistenceManager to save and restore
211 the object properties and implements them using the concrete class methods.
213 You may derive your own classes from wxPersistentObject to implement
214 persistence support for your common classes, see @ref persistence_defining.
216 @see wxPersistentWindow<>
218 class wxPersistentObject
222 Constructor takes the object which we're associated with.
224 This object must have life-time greater than ours as we keep a pointer
227 wxPersistentObject(void *obj
);
229 /// Trivial but virtual destructor.
230 virtual ~wxPersistentObject();
234 @name Methods to be implemented in the derived classes.
236 Notice that these methods are only used by wxPersistenceManager
237 normally and shouldn't be called directly.
242 Save the object properties.
244 The implementation of this method should use SaveValue().
246 virtual void Save() const = 0;
249 Restore the object properties.
251 The implementation of this method should use RestoreValue().
253 virtual bool Restore() = 0;
257 Returns the string uniquely identifying the objects supported by this
260 This method is called from SaveValue() and RestoreValue() and normally
261 returns some short (but not too cryptic) strings, e.g. @c "Checkbox".
263 virtual wxString
GetKind() const = 0;
266 Returns the string uniquely identifying the object we're associated
267 with among all the other objects of the same type.
269 This method is used together with GetKind() to construct the unique
270 full name of the object in e.g. a configuration file.
272 virtual wxString
GetName() const = 0;
277 /// Return the associated object.
278 void *GetObject() const;
282 Save the specified value using the given name.
285 The name of the value in the configuration file.
287 The value to save, currently must be a type supported by wxConfig.
289 @true if the value was saved or @false if an error occurred.
291 template <typename T
>
292 bool SaveValue(const wxString
& name
, T value
) const;
295 Restore the value saved by Save().
298 The same name as was used by Save().
300 Non-@NULL pointer which will be filled with the value if it was
301 read successfully or not modified if it wasn't.
303 @true if the value was successfully read or @false if it was not
304 found or an error occurred.
306 template <typename T
>
307 bool RestoreValue(const wxString
& name
, T
*value
);
311 Function used to create the correct persistent adapter for the given type
314 To be precise, there is no such template function definition but there are
315 overloads of wxCreatePersistentObject() taking different object types for
316 all wxWidgets classes supporting persistence. And you may also define your
317 own overloads to integrate your custom classes with wxWidgets persistence
320 @see @ref persistence_defining
322 @header{wx/persist.h}
325 wxPersistentObject
*wxCreatePersistentObject(T
*obj
);
328 A shorter synonym for wxPersistenceManager::RegisterAndRestore().
330 This function simply calls wxPersistenceManager::RegisterAndRestore() but
331 using it results in slightly shorter code as it calls
332 wxPersistenceManager::Get() internally. As an additional convenience, this
333 function can also set the window name.
335 For the implementation reasons, this function @em must be used instead of
336 the template method when using Microsoft Visual C++ 6 compiler.
338 @param obj wxWindow-derived object to register with persistence manager and
339 to try to restore the settings for.
340 @param name If not empty, @a obj name is changed to the provided value
341 before registering it.
342 @return true if the settings were restored or false otherwise (this will
343 always be the case when the program runs for the first time, for
346 @since 2.9.0, @a name is new in 2.9.1.
348 @header{wx/persist.h}
351 bool wxPersistentRegisterAndRestore(T
*obj
, const wxString
& name
= wxString());