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