/
TransientWindow.cpp
149 lines (124 loc) · 2.96 KB
/
TransientWindow.cpp
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
#include "TransientWindow.h"
#include "imainframe.h"
#include "iregistry.h"
#include "wxutil/Bitmap.h"
namespace wxutil
{
TransientWindow::TransientWindow(const std::string& title, wxWindow* parent,
bool hideOnDelete)
: wxFrame(parent, wxID_ANY, title, wxDefaultPosition, wxDefaultSize,
wxSYSTEM_MENU | wxRESIZE_BORDER | wxCLOSE_BOX | wxCAPTION
#if defined(_WIN32)
// Avoids minimisation problems on Windows, but results in child
// window appearing unfocused on GTK, which is annoying.
| wxFRAME_TOOL_WINDOW
#endif
| wxCLIP_CHILDREN | wxFRAME_FLOAT_ON_PARENT | wxFRAME_NO_TASKBAR),
_hideOnDelete(hideOnDelete)
{
Connect(wxEVT_CLOSE_WINDOW, wxCloseEventHandler(TransientWindow::_onDelete), NULL, this);
Connect(wxEVT_SHOW, wxShowEventHandler(TransientWindow::_onShowHide), NULL, this);
CenterOnParent();
// Set the window icon
wxIcon appIcon;
appIcon.CopyFromBitmap(wxutil::GetLocalBitmap("darkradiant_icon_64x64.png"));
SetIcon(appIcon);
}
bool TransientWindow::Show(bool show)
{
if (show)
{
// Restore the position
_windowPosition.applyPosition();
_preShow();
}
else
{
SaveWindowState();
_preHide();
}
// Pass the call to base
return wxFrame::Show(show);
}
void TransientWindow::_onShowHide(wxShowEvent& ev)
{
ev.Skip();
if (ev.IsShown())
{
_postShow();
}
else
{
// Bring the mainframe to foreground after closing this Window (#3965)
// If we don't do this, some completely different application like Windows Explorer
// might get the focus instead.
if (GlobalMainFrame().getWxTopLevelWindow() != NULL)
{
GlobalMainFrame().getWxTopLevelWindow()->SetFocus();
}
_postHide();
}
}
bool TransientWindow::_onDeleteEvent()
{
if (_hideOnDelete)
{
Hide();
return true; // veto event
}
_preDestroy();
Destroy();
_postDestroy();
return false;
}
void TransientWindow::SaveWindowState()
{
// Save the window position, to make sure
_windowPosition.readPosition();
// Tell the position tracker to save the information
if (!_windowStateKey.empty())
{
_windowPosition.saveToPath(_windowStateKey);
}
}
void TransientWindow::ToggleVisibility()
{
if (!IsShownOnScreen())
{
Show();
}
else
{
Hide();
}
}
void TransientWindow::InitialiseWindowPosition(int defaultWidth, int defaultHeight,
const std::string& windowStateKey)
{
SetSize(defaultWidth, defaultHeight);
Fit();
_windowStateKey = windowStateKey;
if (GlobalRegistry().keyExists(_windowStateKey))
{
// Connect the window position tracker
_windowPosition.loadFromPath(_windowStateKey);
}
_windowPosition.connect(this);
}
const std::string& TransientWindow::GetWindowStateKey() const
{
return _windowStateKey;
}
void TransientWindow::_onDelete(wxCloseEvent& ev)
{
if (_onDeleteEvent())
{
ev.Veto();
}
}
void TransientWindow::_onFocus(wxFocusEvent& ev)
{
_onSetFocus();
ev.Skip();
}
} // namespace