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