File: WindowPosition.cpp

package info (click to toggle)
darkradiant 3.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 41,080 kB
  • sloc: cpp: 264,743; ansic: 10,659; python: 1,852; xml: 1,650; sh: 92; makefile: 21
file content (207 lines) | stat: -rw-r--r-- 5,574 bytes parent folder | download | duplicates (3)
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include "WindowPosition.h"

#include "iregistry.h"
#include "string/convert.h"
#include <wx/frame.h>
#include <wx/display.h>

namespace
{
	constexpr int DEFAULT_POSITION_X = 50;
	constexpr int DEFAULT_POSITION_Y = 25;
	constexpr int DEFAULT_SIZE_X = 400;
	constexpr int DEFAULT_SIZE_Y = 300;
}

namespace wxutil
{

WindowPosition::WindowPosition() :
    _position({ DEFAULT_POSITION_X, DEFAULT_POSITION_Y }),
    _size({ DEFAULT_SIZE_X, DEFAULT_SIZE_Y }),
	_window(nullptr)
{}

void WindowPosition::initialise(wxTopLevelWindow* window, const std::string& windowStateKey)
{
    initialise(window, windowStateKey, 0.6f, 0.8f);
}

void WindowPosition::initialise(wxTopLevelWindow* window, 
                                const std::string& windowStateKey,
                                float defaultXFraction, 
                                float defaultYFraction)
{
    // Set up events and such
    connect(window);

    // Load from registry if possible
    if (GlobalRegistry().keyExists(windowStateKey))
    {
        loadFromPath(windowStateKey);
    }
    else
    {
        fitToScreen(defaultXFraction, defaultYFraction);
    }

    applyPosition();
}

// Connect the passed window to this object
void WindowPosition::connect(wxTopLevelWindow* window)
{
	if (_window != nullptr)
	{
		disconnect(_window);
	}

	_window = window;
	applyPosition();

	window->Bind(wxEVT_SIZE, &WindowPosition::onResize, this);
	window->Bind(wxEVT_MOVE, &WindowPosition::onMove, this);
}

void WindowPosition::disconnect(wxTopLevelWindow* window)
{
	_window = nullptr;

	window->Unbind(wxEVT_SIZE, &WindowPosition::onResize, this);
	window->Unbind(wxEVT_MOVE, &WindowPosition::onMove, this);
}

const std::array<int, 2>& WindowPosition::getPosition() const
{
	return _position;
}

const std::array<int, 2>& WindowPosition::getSize() const
{
	return _size;
}

void WindowPosition::setPosition(int x, int y)
{
	_position.at(0) = x;
	_position.at(1) = y;
}

void WindowPosition::setSize(int width, int height)
{
	_size.at(0) = width;
	_size.at(1) = height;
}

void WindowPosition::saveToPath(const std::string& path)
{
    if (path.empty()) return;

	GlobalRegistry().setAttribute(path, "xPosition", string::to_string(_position.at(0)));
	GlobalRegistry().setAttribute(path, "yPosition", string::to_string(_position.at(1)));
	GlobalRegistry().setAttribute(path, "width", string::to_string(_size.at(0)));
	GlobalRegistry().setAttribute(path, "height", string::to_string(_size.at(1)));
}

void WindowPosition::loadFromPath(const std::string& path)
{
    if (path.empty()) return;

	_position.at(0) = string::convert<int>(GlobalRegistry().getAttribute(path, "xPosition"));
	_position.at(1) = string::convert<int>(GlobalRegistry().getAttribute(path, "yPosition"));

	_size.at(0) = string::convert<int>(GlobalRegistry().getAttribute(path, "width"));
	_size.at(1) = string::convert<int>(GlobalRegistry().getAttribute(path, "height"));

    if (_size.at(0) == 0 || _size.at(1) == 0)
    {
        auto defaultXFraction = string::convert<float>(GlobalRegistry().getAttribute(path, "defaultWidthFraction"));
        auto defaultYFraction = string::convert<float>(GlobalRegistry().getAttribute(path, "defaultHeightFraction"));

        fitToScreen(defaultXFraction, defaultYFraction);
    }

    applyPosition();
}

void WindowPosition::applyPosition()
{
	if (_window == nullptr) return;

    if (_size.at(0) == 0 || _size.at(1) == 0)
    {
        // Don't apply empty sizes
        return;
    }

	// On multi-monitor setups, wxWidgets offers a virtual big screen with
	// coordinates going from 0,0 to whatever lower-rightmost point there is

	// Sanity check the window position
    wxRect targetPos(_position.at(0), _position.at(1), _size.at(0), _size.at(1));
	
	constexpr int TOL = 8;

	// Employ a few pixels tolerance to allow for placement very near the borders
	if (wxDisplay::GetFromPoint(targetPos.GetTopLeft() + wxPoint(TOL, TOL)) == wxNOT_FOUND)
	{
		// Window probably ends up invisible, refuse these coords
		_window->CenterOnParent();
	}
	else
	{
		_window->SetPosition(wxPoint(_position.at(0), _position.at(1)));
	}

	_window->SetSize(_size.at(0), _size.at(1));
}

// Reads the position from the window
void WindowPosition::readPosition()
{
    if (_window != nullptr)
    {
        _window->GetScreenPosition(&_position.at(0), &_position.at(1));
        _window->GetSize(&_size.at(0), &_size.at(1));
    }
}

void WindowPosition::fitToScreen(float xfraction, float yfraction)
{
	if (_window == nullptr) return;

	wxDisplay display(wxDisplay::GetFromWindow(_window));

	// Pass the call
	fitToScreen(display.GetGeometry(), xfraction, yfraction);
}

void WindowPosition::fitToScreen(const wxRect& screen, float xfraction, float yfraction)
{
	_size.at(0) = static_cast<int>(screen.GetWidth() * xfraction) - 12;
	_size.at(1) = static_cast<int>(screen.GetHeight() * yfraction) - 48;

    _position.at(0) = screen.GetX() + (screen.GetWidth() - _size.at(0) - 12) / 2;
    _position.at(1) = screen.GetY() + (screen.GetHeight() - _size.at(1) - 48) / 2;
}

void WindowPosition::onResize(wxSizeEvent& ev)
{
	setSize(ev.GetSize().GetWidth(), ev.GetSize().GetHeight());
	ev.Skip();
}

void WindowPosition::onMove(wxMoveEvent& ev)
{
    if (_window == nullptr) return;

    // The position passed in the wxMoveEvent seems (on my Win10 system) 
    // to be off by about x=8,y=51
    // Call GetScreenPosition to get the real coordinates
	//setPosition(ev.GetPosition().x, ev.GetPosition().y);
    _window->GetScreenPosition(&_position.at(0), &_position.at(1));

	ev.Skip();
}

} // namespace