toplevel.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/persist/toplevel.h
  3. // Purpose: persistence support for wxTLW
  4. // Author: Vadim Zeitlin
  5. // Created: 2009-01-19
  6. // Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
  7. // Licence: wxWindows licence
  8. ///////////////////////////////////////////////////////////////////////////////
  9. #ifndef _WX_PERSIST_TOPLEVEL_H_
  10. #define _WX_PERSIST_TOPLEVEL_H_
  11. #include "wx/persist/window.h"
  12. #include "wx/toplevel.h"
  13. #include "wx/display.h"
  14. // ----------------------------------------------------------------------------
  15. // string constants used by wxPersistentTLW
  16. // ----------------------------------------------------------------------------
  17. // we use just "Window" to keep configuration files and such short, there
  18. // should be no confusion with wxWindow itself as we don't have persistent
  19. // windows, just persistent controls which have their own specific kind strings
  20. #define wxPERSIST_TLW_KIND "Window"
  21. // names for various persistent options
  22. #define wxPERSIST_TLW_X "x"
  23. #define wxPERSIST_TLW_Y "y"
  24. #define wxPERSIST_TLW_W "w"
  25. #define wxPERSIST_TLW_H "h"
  26. #define wxPERSIST_TLW_MAXIMIZED "Maximized"
  27. #define wxPERSIST_TLW_ICONIZED "Iconized"
  28. // ----------------------------------------------------------------------------
  29. // wxPersistentTLW: supports saving/restoring window position and size as well
  30. // as maximized/iconized/restore state
  31. // ----------------------------------------------------------------------------
  32. class wxPersistentTLW : public wxPersistentWindow<wxTopLevelWindow>
  33. {
  34. public:
  35. wxPersistentTLW(wxTopLevelWindow *tlw)
  36. : wxPersistentWindow<wxTopLevelWindow>(tlw)
  37. {
  38. }
  39. virtual void Save() const
  40. {
  41. const wxTopLevelWindow * const tlw = Get();
  42. const wxPoint pos = tlw->GetScreenPosition();
  43. SaveValue(wxPERSIST_TLW_X, pos.x);
  44. SaveValue(wxPERSIST_TLW_Y, pos.y);
  45. // notice that we use GetSize() here and not GetClientSize() because
  46. // the latter doesn't return correct results for the minimized windows
  47. // (at least not under Windows)
  48. //
  49. // of course, it shouldn't matter anyhow usually, the client size
  50. // should be preserved as well unless the size of the decorations
  51. // changed between the runs
  52. const wxSize size = tlw->GetSize();
  53. SaveValue(wxPERSIST_TLW_W, size.x);
  54. SaveValue(wxPERSIST_TLW_H, size.y);
  55. SaveValue(wxPERSIST_TLW_MAXIMIZED, tlw->IsMaximized());
  56. SaveValue(wxPERSIST_TLW_ICONIZED, tlw->IsIconized());
  57. }
  58. virtual bool Restore()
  59. {
  60. wxTopLevelWindow * const tlw = Get();
  61. long x wxDUMMY_INITIALIZE(-1),
  62. y wxDUMMY_INITIALIZE(-1),
  63. w wxDUMMY_INITIALIZE(-1),
  64. h wxDUMMY_INITIALIZE(-1);
  65. const bool hasPos = RestoreValue(wxPERSIST_TLW_X, &x) &&
  66. RestoreValue(wxPERSIST_TLW_Y, &y);
  67. const bool hasSize = RestoreValue(wxPERSIST_TLW_W, &w) &&
  68. RestoreValue(wxPERSIST_TLW_H, &h);
  69. if ( hasPos )
  70. {
  71. // to avoid making the window completely invisible if it had been
  72. // shown on a monitor which was disconnected since the last run
  73. // (this is pretty common for notebook with external displays)
  74. //
  75. // NB: we should allow window position to be (slightly) off screen,
  76. // it's not uncommon to position the window so that its upper
  77. // left corner has slightly negative coordinate
  78. if ( wxDisplay::GetFromPoint(wxPoint(x, y)) != wxNOT_FOUND ||
  79. (hasSize && wxDisplay::GetFromPoint(
  80. wxPoint(x + w, y + h)) != wxNOT_FOUND) )
  81. {
  82. tlw->Move(x, y, wxSIZE_ALLOW_MINUS_ONE);
  83. }
  84. //else: should we try to adjust position/size somehow?
  85. }
  86. if ( hasSize )
  87. tlw->SetSize(w, h);
  88. // note that the window can be both maximized and iconized
  89. bool maximized;
  90. if ( RestoreValue(wxPERSIST_TLW_MAXIMIZED, &maximized) && maximized )
  91. tlw->Maximize();
  92. bool iconized;
  93. if ( RestoreValue(wxPERSIST_TLW_ICONIZED, &iconized) && iconized )
  94. tlw->Iconize();
  95. // the most important property of the window that we restore is its
  96. // size, so disregard the value of hasPos here
  97. return hasSize;
  98. }
  99. virtual wxString GetKind() const { return wxPERSIST_TLW_KIND; }
  100. };
  101. inline wxPersistentObject *wxCreatePersistentObject(wxTopLevelWindow *tlw)
  102. {
  103. return new wxPersistentTLW(tlw);
  104. }
  105. #endif // _WX_PERSIST_TOPLEVEL_H_