]> git.saurik.com Git - wxWidgets.git/blob - include/wx/persist/window.h
don't use template assign() with VC6, even though it can compile it it doesn't handle...
[wxWidgets.git] / include / wx / persist / window.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/persist/window.h
3 // Purpose: wxPersistentWindow declaration
4 // Author: Vadim Zeitlin
5 // Created: 2009-01-23
6 // RCS-ID: $Id$
7 // Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
10
11 #ifndef _WX_PERSIST_WINDOW_H_
12 #define _WX_PERSIST_WINDOW_H_
13
14 #include "wx/persist.h"
15
16 #include "wx/window.h"
17
18 // ----------------------------------------------------------------------------
19 // wxPersistentWindow: base class for persistent windows, uses the window name
20 // as persistent name by default and automatically reacts
21 // to the window destruction
22 // ----------------------------------------------------------------------------
23
24 // type-independent part of wxPersistentWindow
25 class wxPersistentWindowBase :
26 #if wxEVENTS_COMPATIBILITY_2_8
27 // in compatibility mode we need to derive from wxEvtHandler to be able to
28 // handle events
29 public wxEvtHandler ,
30 #endif
31 public wxPersistentObject
32 {
33 public:
34 wxPersistentWindowBase(wxWindow *win)
35 : wxPersistentObject(win)
36 {
37 #if wxEVENTS_COMPATIBILITY_2_8
38 win->Connect
39 (
40 wxEVT_DESTROY,
41 wxWindowDestroyEventHandler(
42 wxPersistentWindowBase::HandleDestroy),
43 NULL,
44 this
45 );
46 #else // !wxEVENTS_COMPATIBILITY_2_8
47 win->Bind(wxEVT_DESTROY, &wxPersistentWindowBase::HandleDestroy, this);
48 #endif // wxEVENTS_COMPATIBILITY_2_8/!wxEVENTS_COMPATIBILITY_2_8
49 }
50
51 virtual wxString GetName() const
52 {
53 const wxString name = GetWindow()->GetName();
54 wxASSERT_MSG( !name.empty(), "persistent windows should be named!" );
55
56 return name;
57 }
58
59 protected:
60 wxWindow *GetWindow() const { return static_cast<wxWindow *>(GetObject()); }
61
62 private:
63 void HandleDestroy(wxWindowDestroyEvent& event)
64 {
65 event.Skip();
66
67 // only react to the destruction of this object itself, not of any of
68 // its children
69 if ( event.GetEventObject() == GetObject() )
70 {
71 // this will delete this object itself
72 wxPersistenceManager::Get().SaveAndUnregister(GetWindow());
73 }
74 }
75
76 wxDECLARE_NO_COPY_CLASS(wxPersistentWindowBase);
77 };
78
79 template <class T>
80 class wxPersistentWindow : public wxPersistentWindowBase
81 {
82 public:
83 typedef T WindowType;
84
85 wxPersistentWindow(WindowType *win)
86 : wxPersistentWindowBase(win)
87 {
88 }
89
90 WindowType *Get() const { return static_cast<WindowType *>(GetWindow()); }
91 };
92
93 #endif // _WX_PERSIST_WINDOW_H_
94