window.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/persist/window.h
  3. // Purpose: wxPersistentWindow declaration
  4. // Author: Vadim Zeitlin
  5. // Created: 2009-01-23
  6. // Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
  7. // Licence: wxWindows licence
  8. ///////////////////////////////////////////////////////////////////////////////
  9. #ifndef _WX_PERSIST_WINDOW_H_
  10. #define _WX_PERSIST_WINDOW_H_
  11. #include "wx/persist.h"
  12. #include "wx/window.h"
  13. // ----------------------------------------------------------------------------
  14. // wxPersistentWindow: base class for persistent windows, uses the window name
  15. // as persistent name by default and automatically reacts
  16. // to the window destruction
  17. // ----------------------------------------------------------------------------
  18. // type-independent part of wxPersistentWindow
  19. class wxPersistentWindowBase :
  20. wxBIND_OR_CONNECT_HACK_BASE_CLASS
  21. public wxPersistentObject
  22. {
  23. public:
  24. wxPersistentWindowBase(wxWindow *win)
  25. : wxPersistentObject(win)
  26. {
  27. wxBIND_OR_CONNECT_HACK(win, wxEVT_DESTROY, wxWindowDestroyEventHandler,
  28. wxPersistentWindowBase::HandleDestroy, this);
  29. }
  30. virtual wxString GetName() const
  31. {
  32. const wxString name = GetWindow()->GetName();
  33. wxASSERT_MSG( !name.empty(), "persistent windows should be named!" );
  34. return name;
  35. }
  36. protected:
  37. wxWindow *GetWindow() const { return static_cast<wxWindow *>(GetObject()); }
  38. private:
  39. void HandleDestroy(wxWindowDestroyEvent& event)
  40. {
  41. event.Skip();
  42. // only react to the destruction of this object itself, not of any of
  43. // its children
  44. if ( event.GetEventObject() == GetObject() )
  45. {
  46. // this will delete this object itself
  47. wxPersistenceManager::Get().SaveAndUnregister(GetWindow());
  48. }
  49. }
  50. wxDECLARE_NO_COPY_CLASS(wxPersistentWindowBase);
  51. };
  52. template <class T>
  53. class wxPersistentWindow : public wxPersistentWindowBase
  54. {
  55. public:
  56. typedef T WindowType;
  57. wxPersistentWindow(WindowType *win)
  58. : wxPersistentWindowBase(win)
  59. {
  60. }
  61. WindowType *Get() const { return static_cast<WindowType *>(GetWindow()); }
  62. };
  63. #endif // _WX_PERSIST_WINDOW_H_