]>
Commit | Line | Data |
---|---|---|
7d9f12f3 | 1 | ///////////////////////////////////////////////////////////////////////////// |
cca1624d | 2 | // Name: src/common/toplvcmn.cpp |
7d9f12f3 VS |
3 | // Purpose: common (for all platforms) wxTopLevelWindow functions |
4 | // Author: Julian Smart, Vadim Zeitlin | |
5 | // Created: 01/02/97 | |
6 | // Id: $Id$ | |
55d99c7a | 7 | // Copyright: (c) 1998 Robert Roebling and Julian Smart |
65571936 | 8 | // Licence: wxWindows licence |
7d9f12f3 VS |
9 | ///////////////////////////////////////////////////////////////////////////// |
10 | ||
11 | // ============================================================================ | |
12 | // declarations | |
13 | // ============================================================================ | |
14 | ||
15 | // ---------------------------------------------------------------------------- | |
16 | // headers | |
17 | // ---------------------------------------------------------------------------- | |
18 | ||
7d9f12f3 VS |
19 | // For compilers that support precompilation, includes "wx.h". |
20 | #include "wx/wxprec.h" | |
21 | ||
22 | #ifdef __BORLANDC__ | |
23 | #pragma hdrstop | |
24 | #endif | |
25 | ||
26 | #ifndef WX_PRECOMP | |
27 | #include "wx/toplevel.h" | |
28 | #include "wx/dcclient.h" | |
5f1d3069 | 29 | #include "wx/app.h" |
7d9f12f3 VS |
30 | #endif // WX_PRECOMP |
31 | ||
1f464296 VZ |
32 | #include "wx/display.h" |
33 | ||
7d9f12f3 VS |
34 | // ---------------------------------------------------------------------------- |
35 | // event table | |
36 | // ---------------------------------------------------------------------------- | |
37 | ||
38 | BEGIN_EVENT_TABLE(wxTopLevelWindowBase, wxWindow) | |
39 | EVT_CLOSE(wxTopLevelWindowBase::OnCloseWindow) | |
40 | EVT_SIZE(wxTopLevelWindowBase::OnSize) | |
41 | END_EVENT_TABLE() | |
42 | ||
43 | // ============================================================================ | |
44 | // implementation | |
45 | // ============================================================================ | |
46 | ||
f58585c0 | 47 | IMPLEMENT_ABSTRACT_CLASS(wxTopLevelWindow, wxWindow) |
82c9f85c | 48 | |
7d9f12f3 VS |
49 | // ---------------------------------------------------------------------------- |
50 | // construction/destruction | |
51 | // ---------------------------------------------------------------------------- | |
52 | ||
53 | wxTopLevelWindowBase::wxTopLevelWindowBase() | |
54 | { | |
c7e61a5e DE |
55 | // Unlike windows, top level windows are created hidden by default. |
56 | m_isShown = false; | |
7d9f12f3 VS |
57 | } |
58 | ||
799ea011 GD |
59 | wxTopLevelWindowBase::~wxTopLevelWindowBase() |
60 | { | |
1cbee0b4 VZ |
61 | // don't let wxTheApp keep any stale pointers to us |
62 | if ( wxTheApp && wxTheApp->GetTopWindow() == this ) | |
63 | wxTheApp->SetTopWindow(NULL); | |
64 | ||
65 | bool shouldExit = IsLastBeforeExit(); | |
66 | ||
67 | wxTopLevelWindows.DeleteObject(this); | |
cb719f2e | 68 | |
1cbee0b4 VZ |
69 | if ( shouldExit ) |
70 | { | |
71 | // then do it | |
72 | wxTheApp->ExitMainLoop(); | |
73 | } | |
799ea011 GD |
74 | } |
75 | ||
7d9f12f3 VS |
76 | bool wxTopLevelWindowBase::Destroy() |
77 | { | |
78 | // delayed destruction: the frame will be deleted during the next idle | |
79 | // loop iteration | |
80 | if ( !wxPendingDelete.Member(this) ) | |
81 | wxPendingDelete.Append(this); | |
82 | ||
b3bd912d RR |
83 | if (wxTopLevelWindows.GetCount() > 1) |
84 | { | |
85 | // Hide it immediately. This should | |
86 | // not be done if this TLW is the | |
87 | // only one left since we then would | |
88 | // risk not to get any idle events | |
cb719f2e | 89 | // at all anymore during which we |
b3bd912d RR |
90 | // could delete any pending events. |
91 | Hide(); | |
92 | } | |
cafcf62a | 93 | |
cb719f2e | 94 | return true; |
7d9f12f3 VS |
95 | } |
96 | ||
5c363878 | 97 | bool wxTopLevelWindowBase::IsLastBeforeExit() const |
1cbee0b4 VZ |
98 | { |
99 | // we exit the application if there are no more top level windows left | |
100 | // normally but wxApp can prevent this from happening | |
5c363878 VZ |
101 | return wxTopLevelWindows.GetCount() == 1 && |
102 | wxTopLevelWindows.GetFirst()->GetData() == (wxWindow *)this && | |
1cbee0b4 VZ |
103 | wxTheApp && wxTheApp->GetExitOnFrameDelete(); |
104 | } | |
105 | ||
106 | // ---------------------------------------------------------------------------- | |
107 | // wxTopLevelWindow geometry | |
108 | // ---------------------------------------------------------------------------- | |
109 | ||
0bba37f5 DE |
110 | void wxTopLevelWindowBase::GetRectForTopLevelChildren(int *x, int *y, int *w, int *h) |
111 | { | |
112 | GetPosition(x,y); | |
113 | GetSize(w,h); | |
114 | } | |
115 | ||
34c3ffca RL |
116 | wxSize wxTopLevelWindowBase::GetMaxSize() const |
117 | { | |
118 | wxSize size( GetMaxWidth(), GetMaxHeight() ); | |
119 | int w, h; | |
120 | ||
121 | wxClientDisplayRect( 0, 0, &w, &h ); | |
122 | ||
cb719f2e | 123 | if( size.GetWidth() == wxDefaultCoord ) |
34c3ffca RL |
124 | size.SetWidth( w ); |
125 | ||
cb719f2e | 126 | if( size.GetHeight() == wxDefaultCoord ) |
34c3ffca RL |
127 | size.SetHeight( h ); |
128 | ||
129 | return size; | |
130 | } | |
131 | ||
0c089c08 VZ |
132 | /* static */ |
133 | wxSize wxTopLevelWindowBase::GetDefaultSize() | |
134 | { | |
135 | wxSize size = wxGetClientDisplayRect().GetSize(); | |
136 | ||
137 | // create proportionally bigger windows on small screens | |
138 | if ( size.x >= 1024 ) | |
139 | size.x = 400; | |
140 | else if ( size.x >= 800 ) | |
141 | size.x = 300; | |
142 | else if ( size.x >= 320 ) | |
143 | size.x = 240; | |
144 | ||
145 | if ( size.y >= 768 ) | |
146 | size.y = 250; | |
147 | else if ( size.y > 200 ) | |
148 | { | |
149 | size.y *= 2; | |
150 | size.y /= 3; | |
151 | } | |
152 | ||
153 | return size; | |
154 | } | |
155 | ||
1f464296 VZ |
156 | void wxTopLevelWindowBase::DoCentre(int dir) |
157 | { | |
262a1536 VZ |
158 | // we need the display rect anyhow so store it first |
159 | int nDisplay = wxDisplay::GetFromWindow(this); | |
160 | wxDisplay dpy(nDisplay == wxNOT_FOUND ? 0 : nDisplay); | |
161 | const wxRect rectDisplay(dpy.GetClientArea()); | |
162 | ||
163 | // what should we centre this window on? | |
164 | wxRect rectParent; | |
1f464296 VZ |
165 | if ( !(dir & wxCENTRE_ON_SCREEN) && GetParent() ) |
166 | { | |
5a63a0f1 VZ |
167 | // centre on parent window: notice that we need screen coordinates for |
168 | // positioning this TLW | |
262a1536 VZ |
169 | rectParent = GetParent()->GetScreenRect(); |
170 | ||
171 | // if the parent is entirely off screen (happens at least with MDI | |
172 | // parent frame under Mac but could happen elsewhere too if the frame | |
173 | // was hidden/moved away for some reason), don't use it as otherwise | |
174 | // this window wouldn't be visible at all | |
175 | if ( !rectDisplay.Inside(rectParent.GetTopLeft()) && | |
176 | !rectParent.Inside(rectParent.GetBottomRight()) ) | |
177 | { | |
178 | // this is enough to make IsEmpty() test below pass | |
179 | rectParent.width = 0; | |
180 | } | |
1f464296 | 181 | } |
262a1536 VZ |
182 | |
183 | if ( rectParent.IsEmpty() ) | |
1f464296 VZ |
184 | { |
185 | // we were explicitely asked to centre this window on the entire screen | |
186 | // or if we have no parent anyhow and so can't centre on it | |
262a1536 VZ |
187 | rectParent = rectDisplay; |
188 | } | |
189 | ||
cca1624d WS |
190 | // centering maximized window on screen is no-op |
191 | if((rectParent == rectDisplay) && IsMaximized()) | |
192 | return; | |
193 | ||
262a1536 VZ |
194 | // the new window rect candidate |
195 | wxRect rect = GetRect().CentreIn(rectParent, dir); | |
196 | ||
197 | // we don't want to place the window off screen if Centre() is called as | |
198 | // this is (almost?) never wanted and it would be very difficult to prevent | |
199 | // it from happening from the user code if we didn't check for it here | |
200 | if ( rectDisplay.Inside(rect.GetTopLeft()) ) | |
201 | { | |
202 | if ( !rectDisplay.Inside(rect.GetBottomRight()) ) | |
1f464296 | 203 | { |
262a1536 VZ |
204 | // check if we can move the window so that the bottom right corner |
205 | // is visible without hiding the top left one | |
206 | int dx = rectDisplay.GetRight() - rect.GetRight(); | |
207 | int dy = rectDisplay.GetBottom() - rect.GetBottom(); | |
208 | rect.Offset(dx, dy); | |
1f464296 | 209 | } |
262a1536 VZ |
210 | //else: the window top left and bottom right corner are both visible, |
211 | // although the window might still be not entirely on screen (with | |
212 | // 2 staggered displays for example) we wouldn't be able to | |
213 | // improve the layout much in such case, so just leave it as is | |
214 | } | |
215 | else // make top left corner visible | |
216 | { | |
217 | if ( rect.x < rectDisplay.x ) | |
218 | rect.x = rectDisplay.x; | |
219 | ||
220 | if ( rect.y < rectDisplay.y ) | |
221 | rect.y = rectDisplay.y; | |
1f464296 VZ |
222 | } |
223 | ||
262a1536 VZ |
224 | // -1 could be valid coordinate here if there are several displays |
225 | SetSize(rect, wxSIZE_ALLOW_MINUS_ONE); | |
1f464296 VZ |
226 | } |
227 | ||
7d9f12f3 | 228 | // ---------------------------------------------------------------------------- |
82c9f85c VZ |
229 | // wxTopLevelWindow size management: we exclude the areas taken by |
230 | // menu/status/toolbars from the client area, so the client area is what's | |
231 | // really available for the frame contents | |
7d9f12f3 VS |
232 | // ---------------------------------------------------------------------------- |
233 | ||
234 | void wxTopLevelWindowBase::DoScreenToClient(int *x, int *y) const | |
235 | { | |
236 | wxWindow::DoScreenToClient(x, y); | |
237 | ||
82c9f85c | 238 | // translate the wxWindow client coords to our client coords |
7d9f12f3 | 239 | wxPoint pt(GetClientAreaOrigin()); |
82c9f85c VZ |
240 | if ( x ) |
241 | *x -= pt.x; | |
242 | if ( y ) | |
243 | *y -= pt.y; | |
7d9f12f3 VS |
244 | } |
245 | ||
246 | void wxTopLevelWindowBase::DoClientToScreen(int *x, int *y) const | |
247 | { | |
82c9f85c VZ |
248 | // our client area origin (0, 0) may be really something like (0, 30) for |
249 | // wxWindow if we have a toolbar, account for it before translating | |
250 | wxPoint pt(GetClientAreaOrigin()); | |
251 | if ( x ) | |
252 | *x += pt.x; | |
253 | if ( y ) | |
254 | *y += pt.y; | |
7d9f12f3 VS |
255 | |
256 | wxWindow::DoClientToScreen(x, y); | |
257 | } | |
258 | ||
259 | ||
260 | // ---------------------------------------------------------------------------- | |
261 | // event handlers | |
262 | // ---------------------------------------------------------------------------- | |
263 | ||
264 | // default resizing behaviour - if only ONE subwindow, resize to fill the | |
265 | // whole client area | |
5e62d4a5 | 266 | void wxTopLevelWindowBase::DoLayout() |
7d9f12f3 | 267 | { |
bc55104d | 268 | // if we're using constraints or sizers - do use them |
7d9f12f3 VS |
269 | if ( GetAutoLayout() ) |
270 | { | |
271 | Layout(); | |
272 | } | |
273 | else | |
7d9f12f3 VS |
274 | { |
275 | // do we have _exactly_ one child? | |
276 | wxWindow *child = (wxWindow *)NULL; | |
222ed1d6 | 277 | for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst(); |
7d9f12f3 VS |
278 | node; |
279 | node = node->GetNext() ) | |
280 | { | |
281 | wxWindow *win = node->GetData(); | |
282 | ||
283 | // exclude top level and managed windows (status bar isn't | |
284 | // currently in the children list except under wxMac anyhow, but | |
285 | // it makes no harm to test for it) | |
286 | if ( !win->IsTopLevel() && !IsOneOfBars(win) ) | |
287 | { | |
288 | if ( child ) | |
289 | { | |
290 | return; // it's our second subwindow - nothing to do | |
291 | } | |
292 | ||
293 | child = win; | |
294 | } | |
295 | } | |
296 | ||
297 | // do we have any children at all? | |
298 | if ( child ) | |
299 | { | |
300 | // exactly one child - set it's size to fill the whole frame | |
301 | int clientW, clientH; | |
302 | DoGetClientSize(&clientW, &clientH); | |
303 | ||
304 | // for whatever reasons, wxGTK wants to have a small offset - it | |
305 | // probably looks better with it? | |
306 | #ifdef __WXGTK__ | |
307 | static const int ofs = 1; | |
308 | #else | |
309 | static const int ofs = 0; | |
310 | #endif | |
311 | ||
45e0dc94 | 312 | child->SetSize(ofs, ofs, clientW - 2*ofs, clientH - 2*ofs); |
7d9f12f3 VS |
313 | } |
314 | } | |
315 | } | |
316 | ||
317 | // The default implementation for the close window event. | |
318 | void wxTopLevelWindowBase::OnCloseWindow(wxCloseEvent& WXUNUSED(event)) | |
319 | { | |
320 | Destroy(); | |
321 | } | |
322 | ||
323 | bool wxTopLevelWindowBase::SendIconizeEvent(bool iconized) | |
324 | { | |
325 | wxIconizeEvent event(GetId(), iconized); | |
326 | event.SetEventObject(this); | |
327 | ||
328 | return GetEventHandler()->ProcessEvent(event); | |
329 | } | |
34c3ffca | 330 | |
e39af974 JS |
331 | // do the window-specific processing after processing the update event |
332 | void wxTopLevelWindowBase::DoUpdateWindowUI(wxUpdateUIEvent& event) | |
333 | { | |
a3a4105d VZ |
334 | // call inherited, but skip the wxControl's version, and call directly the |
335 | // wxWindow's one instead, because the only reason why we are overriding this | |
336 | // function is that we want to use SetTitle() instead of wxControl::SetLabel() | |
337 | wxWindowBase::DoUpdateWindowUI(event); | |
cb719f2e | 338 | |
a3a4105d | 339 | // update title |
e39af974 JS |
340 | if ( event.GetSetText() ) |
341 | { | |
342 | if ( event.GetText() != GetTitle() ) | |
343 | SetTitle(event.GetText()); | |
344 | } | |
345 | } | |
346 | ||
447fd332 | 347 | void wxTopLevelWindowBase::RequestUserAttention(int WXUNUSED(flags)) |
9b59c95b VZ |
348 | { |
349 | // it's probably better than do nothing, isn't it? | |
350 | Raise(); | |
351 | } |