File: tlwgeom.h

package info (click to toggle)
wxpython4.0 4.2.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 221,752 kB
  • sloc: cpp: 962,555; python: 230,573; ansic: 170,731; makefile: 51,756; sh: 9,342; perl: 1,564; javascript: 584; php: 326; xml: 200
file content (184 lines) | stat: -rw-r--r-- 6,152 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
///////////////////////////////////////////////////////////////////////////////
// Name:        wx/private/tlwgeom.h
// Purpose:     Declaration of platform-specific and private wxTLWGeometry.
// Author:      Vadim Zeitlin
// Created:     2018-04-29
// Copyright:   (c) 2018 Vadim Zeitlin <vadim@wxwidgets.org>
// Licence:     wxWindows licence
///////////////////////////////////////////////////////////////////////////////

#ifndef _WX_PRIVATE_TLWGEOM_H_
#define _WX_PRIVATE_TLWGEOM_H_

#include "wx/display.h"
#include "wx/toplevel.h"

// ----------------------------------------------------------------------------
// wxTLWGeometryBase: abstract base class for platform-specific classes
// ----------------------------------------------------------------------------

// wxTLWGeometry contains full information about the window geometry, which may
// include things other than the obvious ones like its current position and
// size (e.g. under MSW it also stores the position of the maximized window,
// under GTK the size of non-client decorations etc). It is private to wx and
// is only used indirectly, via wxTopLevelWindow::SaveGeometry() and
// RestoreToGeometry() methods, in the public API.

class wxTLWGeometryBase
{
public:
    typedef wxTopLevelWindow::GeometrySerializer Serializer;

    wxTLWGeometryBase() {}
    virtual ~wxTLWGeometryBase() {}

    // Initialize from the given window.
    virtual bool GetFrom(const wxTopLevelWindow* tlw) = 0;

    // Resize the window to use this geometry.
    virtual bool ApplyTo(wxTopLevelWindow* tlw) = 0;

    // Serialize or deserialize the object by using the provided object for
    // writing/reading the values of the different fields of this object.
    virtual bool Save(const Serializer& ser) const = 0;
    virtual bool Restore(Serializer& ser) = 0;
};

// ----------------------------------------------------------------------------
// wxTLWGeometryGeneric: simplest possible generic implementation
// ----------------------------------------------------------------------------

// names for various persistent options
#define wxPERSIST_TLW_X "x"
#define wxPERSIST_TLW_Y "y"
#define wxPERSIST_TLW_W "w"
#define wxPERSIST_TLW_H "h"

#define wxPERSIST_TLW_MAXIMIZED "Maximized"
#define wxPERSIST_TLW_ICONIZED "Iconized"

// MSW has its own native implementation and doesn't use this class.
#ifndef __WXMSW__

class wxTLWGeometryGeneric : public wxTLWGeometryBase
{
public:
    wxTLWGeometryGeneric()
    {
        m_hasPos =
        m_hasSize =
        m_iconized =
        m_maximized = false;
    }

    virtual bool Save(const Serializer& ser) const wxOVERRIDE
    {
        if ( !ser.SaveField(wxPERSIST_TLW_X, m_rectScreen.x) ||
             !ser.SaveField(wxPERSIST_TLW_Y, m_rectScreen.y) )
            return false;

        if ( !ser.SaveField(wxPERSIST_TLW_W, m_rectScreen.width) ||
             !ser.SaveField(wxPERSIST_TLW_H, m_rectScreen.height) )
            return false;

        if ( !ser.SaveField(wxPERSIST_TLW_MAXIMIZED, m_maximized) )
            return false;

        if ( !ser.SaveField(wxPERSIST_TLW_ICONIZED, m_iconized) )
            return false;

        return true;
    }

    virtual bool Restore(Serializer& ser) wxOVERRIDE
    {
        m_hasPos = ser.RestoreField(wxPERSIST_TLW_X, &m_rectScreen.x) &&
                   ser.RestoreField(wxPERSIST_TLW_Y, &m_rectScreen.y);

        m_hasSize = ser.RestoreField(wxPERSIST_TLW_W, &m_rectScreen.width) &&
                    ser.RestoreField(wxPERSIST_TLW_H, &m_rectScreen.height);

        int tmp;
        if ( ser.RestoreField(wxPERSIST_TLW_MAXIMIZED, &tmp) )
            m_maximized = tmp != 0;

        if ( ser.RestoreField(wxPERSIST_TLW_ICONIZED, &tmp) )
            m_iconized = tmp != 0;

        // If we restored at least something, return true.
        return m_hasPos || m_hasSize || m_maximized || m_iconized;
    }

    virtual bool GetFrom(const wxTopLevelWindow* tlw) wxOVERRIDE
    {
        m_rectScreen = tlw->GetScreenRect();
        m_hasPos =
        m_hasSize = true;
        m_iconized = tlw->IsIconized();
        m_maximized = tlw->IsMaximized();

        return true;
    }

    virtual bool ApplyTo(wxTopLevelWindow* tlw) wxOVERRIDE
    {
        if ( m_hasPos )
        {
            // to avoid making the window completely invisible if it had been
            // shown on a monitor which was disconnected since the last run
            // (this is pretty common for notebook with external displays)
            //
            // NB: we should allow window position to be (slightly) off screen,
            //     it's not uncommon to position the window so that its upper
            //     left corner has slightly negative coordinate
            if ( wxDisplay::GetFromPoint(m_rectScreen.GetTopLeft()) != wxNOT_FOUND ||
                 (m_hasSize &&
                  wxDisplay::GetFromPoint(m_rectScreen.GetBottomRight()) != wxNOT_FOUND) )
            {
                tlw->Move(m_rectScreen.GetTopLeft(), wxSIZE_ALLOW_MINUS_ONE);
            }
            //else: should we try to adjust position/size somehow?
        }

        if ( m_hasSize )
        {
            // a previous version of the program could have saved the window
            // size which used to be big enough, but which is not big enough
            // any more for the new version, so check that the size we restore
            // doesn't cut off parts of the window
            wxSize size = m_rectScreen.GetSize();
            size.IncTo(tlw->GetBestSize());
            tlw->SetSize(size);
        }

        // note that the window can be both maximized and iconized
        if ( m_maximized )
            tlw->Maximize();

        if ( m_iconized )
            tlw->Iconize();

        return true;
    }

private:
    wxRect m_rectScreen;
    bool m_hasPos;
    bool m_hasSize;
    bool m_iconized;
    bool m_maximized;
};

#endif // !__WXMSW__

#ifdef __WXGTK20__
    #include "wx/gtk/private/tlwgeom.h"
#elif defined(__WXMSW__)
    #include "wx/msw/private/tlwgeom.h"
#else
    class wxTLWGeometry : public wxTLWGeometryGeneric
    {
    };
#endif

#endif // _WX_PRIVATE_TLWGEOM_H_