]>
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 | ||
1832043f WS |
26 | #include "wx/toplevel.h" |
27 | ||
7d9f12f3 | 28 | #ifndef WX_PRECOMP |
7d9f12f3 | 29 | #include "wx/dcclient.h" |
5f1d3069 | 30 | #include "wx/app.h" |
7d9f12f3 VS |
31 | #endif // WX_PRECOMP |
32 | ||
1f464296 VZ |
33 | #include "wx/display.h" |
34 | ||
7d9f12f3 VS |
35 | // ---------------------------------------------------------------------------- |
36 | // event table | |
37 | // ---------------------------------------------------------------------------- | |
38 | ||
39 | BEGIN_EVENT_TABLE(wxTopLevelWindowBase, wxWindow) | |
40 | EVT_CLOSE(wxTopLevelWindowBase::OnCloseWindow) | |
41 | EVT_SIZE(wxTopLevelWindowBase::OnSize) | |
049908c5 | 42 | WX_EVENT_TABLE_CONTROL_CONTAINER(wxTopLevelWindowBase) |
7d9f12f3 VS |
43 | END_EVENT_TABLE() |
44 | ||
049908c5 VS |
45 | WX_DELEGATE_TO_CONTROL_CONTAINER(wxTopLevelWindowBase, wxWindow) |
46 | ||
7d9f12f3 VS |
47 | // ============================================================================ |
48 | // implementation | |
49 | // ============================================================================ | |
50 | ||
f58585c0 | 51 | IMPLEMENT_ABSTRACT_CLASS(wxTopLevelWindow, wxWindow) |
82c9f85c | 52 | |
7d9f12f3 VS |
53 | // ---------------------------------------------------------------------------- |
54 | // construction/destruction | |
55 | // ---------------------------------------------------------------------------- | |
56 | ||
57 | wxTopLevelWindowBase::wxTopLevelWindowBase() | |
58 | { | |
c7e61a5e DE |
59 | // Unlike windows, top level windows are created hidden by default. |
60 | m_isShown = false; | |
049908c5 VS |
61 | |
62 | WX_INIT_CONTROL_CONTAINER(); | |
7d9f12f3 VS |
63 | } |
64 | ||
799ea011 GD |
65 | wxTopLevelWindowBase::~wxTopLevelWindowBase() |
66 | { | |
1cbee0b4 VZ |
67 | // don't let wxTheApp keep any stale pointers to us |
68 | if ( wxTheApp && wxTheApp->GetTopWindow() == this ) | |
69 | wxTheApp->SetTopWindow(NULL); | |
70 | ||
1cbee0b4 | 71 | wxTopLevelWindows.DeleteObject(this); |
cb719f2e | 72 | |
ef37a431 VZ |
73 | // delete any our top level children which are still pending for deletion |
74 | // immediately: this could happen if a child (e.g. a temporary dialog | |
75 | // created with this window as parent) was Destroy()'d) while this window | |
76 | // was deleted directly (with delete, or maybe just because it was created | |
77 | // on the stack) immediately afterwards and before the child TLW was really | |
78 | // destroyed -- not destroying it now would leave it alive with a dangling | |
79 | // parent pointer and result in a crash later | |
80 | for ( wxObjectList::iterator i = wxPendingDelete.begin(); | |
81 | i != wxPendingDelete.end(); | |
82 | ) | |
83 | { | |
84 | wxWindow * const win = wxDynamicCast(*i, wxWindow); | |
85 | if ( win && win->GetParent() == this ) | |
86 | { | |
87 | wxPendingDelete.erase(i); | |
88 | ||
89 | delete win; | |
90 | ||
91 | // deleting it invalidated the list (and not only one node because | |
92 | // it could have resulted in deletion of other objects to) | |
93 | i = wxPendingDelete.begin(); | |
94 | } | |
95 | else | |
96 | { | |
97 | ++i; | |
98 | } | |
99 | } | |
100 | ||
65afac3f | 101 | if ( IsLastBeforeExit() ) |
1cbee0b4 | 102 | { |
65afac3f | 103 | // no other (important) windows left, quit the app |
1cbee0b4 VZ |
104 | wxTheApp->ExitMainLoop(); |
105 | } | |
799ea011 GD |
106 | } |
107 | ||
7d9f12f3 VS |
108 | bool wxTopLevelWindowBase::Destroy() |
109 | { | |
110 | // delayed destruction: the frame will be deleted during the next idle | |
111 | // loop iteration | |
112 | if ( !wxPendingDelete.Member(this) ) | |
113 | wxPendingDelete.Append(this); | |
114 | ||
18868d62 VZ |
115 | // normally we want to hide the window immediately so that it doesn't get |
116 | // stuck on the screen while it's being destroyed, however we shouldn't | |
117 | // hide the last visible window as then we might not get any idle events | |
118 | // any more as no events will be sent to the hidden window and without idle | |
119 | // events we won't prune wxPendingDelete list and the application won't | |
120 | // terminate | |
18868d62 VZ |
121 | for ( wxWindowList::const_iterator i = wxTopLevelWindows.begin(), |
122 | end = wxTopLevelWindows.end(); | |
123 | i != end; | |
124 | ++i ) | |
b3bd912d | 125 | { |
5c33522f | 126 | wxTopLevelWindow * const win = static_cast<wxTopLevelWindow *>(*i); |
18868d62 VZ |
127 | if ( win != this && win->IsShown() ) |
128 | { | |
129 | // there remains at least one other visible TLW, we can hide this | |
130 | // one | |
131 | Hide(); | |
132 | ||
133 | break; | |
134 | } | |
b3bd912d | 135 | } |
cafcf62a | 136 | |
cb719f2e | 137 | return true; |
7d9f12f3 VS |
138 | } |
139 | ||
5c363878 | 140 | bool wxTopLevelWindowBase::IsLastBeforeExit() const |
1cbee0b4 | 141 | { |
65afac3f VZ |
142 | // first of all, automatically exiting the app on last window close can be |
143 | // completely disabled at wxTheApp level | |
144 | if ( !wxTheApp || !wxTheApp->GetExitOnFrameDelete() ) | |
145 | return false; | |
146 | ||
147 | wxWindowList::const_iterator i; | |
148 | const wxWindowList::const_iterator end = wxTopLevelWindows.end(); | |
149 | ||
150 | // then decide whether we should exit at all | |
151 | for ( i = wxTopLevelWindows.begin(); i != end; ++i ) | |
152 | { | |
5c33522f | 153 | wxTopLevelWindow * const win = static_cast<wxTopLevelWindow *>(*i); |
65afac3f VZ |
154 | if ( win->ShouldPreventAppExit() ) |
155 | { | |
156 | // there remains at least one important TLW, don't exit | |
157 | return false; | |
158 | } | |
159 | } | |
160 | ||
161 | // if yes, close all the other windows: this could still fail | |
162 | for ( i = wxTopLevelWindows.begin(); i != end; ++i ) | |
163 | { | |
164 | // don't close twice the windows which are already marked for deletion | |
5c33522f | 165 | wxTopLevelWindow * const win = static_cast<wxTopLevelWindow *>(*i); |
65afac3f VZ |
166 | if ( !wxPendingDelete.Member(win) && !win->Close() ) |
167 | { | |
168 | // one of the windows refused to close, don't exit | |
169 | // | |
170 | // NB: of course, by now some other windows could have been already | |
171 | // closed but there is really nothing we can do about it as we | |
172 | // have no way just to ask the window if it can close without | |
173 | // forcing it to do it | |
174 | return false; | |
175 | } | |
176 | } | |
177 | ||
178 | return true; | |
1cbee0b4 VZ |
179 | } |
180 | ||
181 | // ---------------------------------------------------------------------------- | |
182 | // wxTopLevelWindow geometry | |
183 | // ---------------------------------------------------------------------------- | |
184 | ||
cda5834e RR |
185 | void wxTopLevelWindowBase::SetMinSize(const wxSize& minSize) |
186 | { | |
49c9d059 | 187 | SetSizeHints(minSize, GetMaxSize()); |
cda5834e RR |
188 | } |
189 | ||
190 | void wxTopLevelWindowBase::SetMaxSize(const wxSize& maxSize) | |
191 | { | |
49c9d059 | 192 | SetSizeHints(GetMinSize(), maxSize); |
9379c0d7 RR |
193 | } |
194 | ||
0bba37f5 DE |
195 | void wxTopLevelWindowBase::GetRectForTopLevelChildren(int *x, int *y, int *w, int *h) |
196 | { | |
197 | GetPosition(x,y); | |
198 | GetSize(w,h); | |
199 | } | |
200 | ||
0c089c08 VZ |
201 | /* static */ |
202 | wxSize wxTopLevelWindowBase::GetDefaultSize() | |
203 | { | |
204 | wxSize size = wxGetClientDisplayRect().GetSize(); | |
3ec9ba03 | 205 | #ifndef __WXOSX_IPHONE__ |
0c089c08 VZ |
206 | // create proportionally bigger windows on small screens |
207 | if ( size.x >= 1024 ) | |
208 | size.x = 400; | |
209 | else if ( size.x >= 800 ) | |
210 | size.x = 300; | |
211 | else if ( size.x >= 320 ) | |
212 | size.x = 240; | |
213 | ||
214 | if ( size.y >= 768 ) | |
215 | size.y = 250; | |
216 | else if ( size.y > 200 ) | |
217 | { | |
218 | size.y *= 2; | |
219 | size.y /= 3; | |
220 | } | |
3ec9ba03 | 221 | #endif |
0c089c08 VZ |
222 | return size; |
223 | } | |
224 | ||
1f464296 VZ |
225 | void wxTopLevelWindowBase::DoCentre(int dir) |
226 | { | |
979a0320 WS |
227 | // on some platforms centering top level windows is impossible |
228 | // because they are always maximized by guidelines or limitations | |
ca67f0d8 VZ |
229 | // |
230 | // and centering a maximized window doesn't make sense as its position | |
231 | // can't change | |
232 | if ( IsAlwaysMaximized() || IsMaximized() ) | |
979a0320 WS |
233 | return; |
234 | ||
4745f3c6 VZ |
235 | // we need the display rect anyhow so store it first: notice that we should |
236 | // be centered on the same display as our parent window, the display of | |
237 | // this window itself is not really defined yet | |
238 | int nDisplay = wxDisplay::GetFromWindow(GetParent() ? GetParent() : this); | |
262a1536 VZ |
239 | wxDisplay dpy(nDisplay == wxNOT_FOUND ? 0 : nDisplay); |
240 | const wxRect rectDisplay(dpy.GetClientArea()); | |
241 | ||
242 | // what should we centre this window on? | |
243 | wxRect rectParent; | |
1f464296 VZ |
244 | if ( !(dir & wxCENTRE_ON_SCREEN) && GetParent() ) |
245 | { | |
5a63a0f1 VZ |
246 | // centre on parent window: notice that we need screen coordinates for |
247 | // positioning this TLW | |
262a1536 VZ |
248 | rectParent = GetParent()->GetScreenRect(); |
249 | ||
250 | // if the parent is entirely off screen (happens at least with MDI | |
251 | // parent frame under Mac but could happen elsewhere too if the frame | |
252 | // was hidden/moved away for some reason), don't use it as otherwise | |
253 | // this window wouldn't be visible at all | |
ca67f0d8 | 254 | if ( !rectParent.Intersects(rectDisplay) ) |
262a1536 | 255 | { |
ca67f0d8 VZ |
256 | // just centre on screen then |
257 | rectParent = rectDisplay; | |
262a1536 | 258 | } |
1f464296 | 259 | } |
ca67f0d8 | 260 | else |
1f464296 | 261 | { |
ca08b543 | 262 | // we were explicitly asked to centre this window on the entire screen |
1f464296 | 263 | // or if we have no parent anyhow and so can't centre on it |
262a1536 VZ |
264 | rectParent = rectDisplay; |
265 | } | |
266 | ||
75454d24 VZ |
267 | if ( !(dir & wxBOTH) ) |
268 | dir |= wxBOTH; // if neither is specified, center in both directions | |
269 | ||
262a1536 | 270 | // the new window rect candidate |
75454d24 | 271 | wxRect rect = GetRect().CentreIn(rectParent, dir & ~wxCENTRE_ON_SCREEN); |
262a1536 VZ |
272 | |
273 | // we don't want to place the window off screen if Centre() is called as | |
274 | // this is (almost?) never wanted and it would be very difficult to prevent | |
275 | // it from happening from the user code if we didn't check for it here | |
4745f3c6 | 276 | if ( !rectDisplay.Contains(rect.GetTopLeft()) ) |
262a1536 | 277 | { |
4745f3c6 VZ |
278 | // move the window just enough to make the corner visible |
279 | int dx = rectDisplay.GetLeft() - rect.GetLeft(); | |
280 | int dy = rectDisplay.GetTop() - rect.GetTop(); | |
281 | rect.Offset(dx > 0 ? dx : 0, dy > 0 ? dy : 0); | |
262a1536 | 282 | } |
262a1536 | 283 | |
4745f3c6 VZ |
284 | if ( !rectDisplay.Contains(rect.GetBottomRight()) ) |
285 | { | |
286 | // do the same for this corner too | |
287 | int dx = rectDisplay.GetRight() - rect.GetRight(); | |
288 | int dy = rectDisplay.GetBottom() - rect.GetBottom(); | |
289 | rect.Offset(dx < 0 ? dx : 0, dy < 0 ? dy : 0); | |
1f464296 VZ |
290 | } |
291 | ||
4745f3c6 VZ |
292 | // the window top left and bottom right corner are both visible now and |
293 | // although the window might still be not entirely on screen (with 2 | |
294 | // staggered displays for example) we wouldn't be able to improve the | |
295 | // layout much in such case, so we stop here | |
296 | ||
262a1536 VZ |
297 | // -1 could be valid coordinate here if there are several displays |
298 | SetSize(rect, wxSIZE_ALLOW_MINUS_ONE); | |
1f464296 VZ |
299 | } |
300 | ||
7d9f12f3 | 301 | // ---------------------------------------------------------------------------- |
82c9f85c VZ |
302 | // wxTopLevelWindow size management: we exclude the areas taken by |
303 | // menu/status/toolbars from the client area, so the client area is what's | |
304 | // really available for the frame contents | |
7d9f12f3 VS |
305 | // ---------------------------------------------------------------------------- |
306 | ||
307 | void wxTopLevelWindowBase::DoScreenToClient(int *x, int *y) const | |
308 | { | |
309 | wxWindow::DoScreenToClient(x, y); | |
310 | ||
82c9f85c | 311 | // translate the wxWindow client coords to our client coords |
7d9f12f3 | 312 | wxPoint pt(GetClientAreaOrigin()); |
82c9f85c VZ |
313 | if ( x ) |
314 | *x -= pt.x; | |
315 | if ( y ) | |
316 | *y -= pt.y; | |
7d9f12f3 VS |
317 | } |
318 | ||
319 | void wxTopLevelWindowBase::DoClientToScreen(int *x, int *y) const | |
320 | { | |
82c9f85c VZ |
321 | // our client area origin (0, 0) may be really something like (0, 30) for |
322 | // wxWindow if we have a toolbar, account for it before translating | |
323 | wxPoint pt(GetClientAreaOrigin()); | |
324 | if ( x ) | |
325 | *x += pt.x; | |
326 | if ( y ) | |
327 | *y += pt.y; | |
7d9f12f3 VS |
328 | |
329 | wxWindow::DoClientToScreen(x, y); | |
330 | } | |
331 | ||
979a0320 WS |
332 | bool wxTopLevelWindowBase::IsAlwaysMaximized() const |
333 | { | |
334 | #if defined(__SMARTPHONE__) || defined(__POCKETPC__) | |
335 | return true; | |
336 | #else | |
337 | return false; | |
338 | #endif | |
339 | } | |
7d9f12f3 VS |
340 | |
341 | // ---------------------------------------------------------------------------- | |
ea098413 VZ |
342 | // icons |
343 | // ---------------------------------------------------------------------------- | |
344 | ||
345 | wxIcon wxTopLevelWindowBase::GetIcon() const | |
346 | { | |
347 | return m_icons.IsEmpty() ? wxIcon() : m_icons.GetIcon( -1 ); | |
348 | } | |
349 | ||
350 | void wxTopLevelWindowBase::SetIcon(const wxIcon& icon) | |
351 | { | |
352 | // passing wxNullIcon to SetIcon() is possible (it means that we shouldn't | |
353 | // have any icon), but adding an invalid icon to wxIconBundle is not | |
354 | wxIconBundle icons; | |
355 | if ( icon.Ok() ) | |
356 | icons.AddIcon(icon); | |
357 | ||
358 | SetIcons(icons); | |
359 | } | |
360 | ||
361 | // ---------------------------------------------------------------------------- | |
7d9f12f3 VS |
362 | // event handlers |
363 | // ---------------------------------------------------------------------------- | |
364 | ||
365 | // default resizing behaviour - if only ONE subwindow, resize to fill the | |
366 | // whole client area | |
5e62d4a5 | 367 | void wxTopLevelWindowBase::DoLayout() |
7d9f12f3 | 368 | { |
bc55104d | 369 | // if we're using constraints or sizers - do use them |
7d9f12f3 VS |
370 | if ( GetAutoLayout() ) |
371 | { | |
372 | Layout(); | |
373 | } | |
374 | else | |
7d9f12f3 VS |
375 | { |
376 | // do we have _exactly_ one child? | |
d3b9f782 | 377 | wxWindow *child = NULL; |
222ed1d6 | 378 | for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst(); |
7d9f12f3 VS |
379 | node; |
380 | node = node->GetNext() ) | |
381 | { | |
382 | wxWindow *win = node->GetData(); | |
383 | ||
384 | // exclude top level and managed windows (status bar isn't | |
385 | // currently in the children list except under wxMac anyhow, but | |
386 | // it makes no harm to test for it) | |
387 | if ( !win->IsTopLevel() && !IsOneOfBars(win) ) | |
388 | { | |
389 | if ( child ) | |
390 | { | |
391 | return; // it's our second subwindow - nothing to do | |
392 | } | |
393 | ||
394 | child = win; | |
395 | } | |
396 | } | |
397 | ||
398 | // do we have any children at all? | |
d3cd1c03 | 399 | if ( child && child->IsShown() ) |
7d9f12f3 VS |
400 | { |
401 | // exactly one child - set it's size to fill the whole frame | |
402 | int clientW, clientH; | |
403 | DoGetClientSize(&clientW, &clientH); | |
404 | ||
cd8667eb | 405 | child->SetSize(0, 0, clientW, clientH); |
7d9f12f3 VS |
406 | } |
407 | } | |
408 | } | |
409 | ||
410 | // The default implementation for the close window event. | |
411 | void wxTopLevelWindowBase::OnCloseWindow(wxCloseEvent& WXUNUSED(event)) | |
412 | { | |
413 | Destroy(); | |
414 | } | |
415 | ||
416 | bool wxTopLevelWindowBase::SendIconizeEvent(bool iconized) | |
417 | { | |
418 | wxIconizeEvent event(GetId(), iconized); | |
419 | event.SetEventObject(this); | |
420 | ||
421 | return GetEventHandler()->ProcessEvent(event); | |
422 | } | |
34c3ffca | 423 | |
e39af974 JS |
424 | // do the window-specific processing after processing the update event |
425 | void wxTopLevelWindowBase::DoUpdateWindowUI(wxUpdateUIEvent& event) | |
426 | { | |
a3a4105d VZ |
427 | // call inherited, but skip the wxControl's version, and call directly the |
428 | // wxWindow's one instead, because the only reason why we are overriding this | |
429 | // function is that we want to use SetTitle() instead of wxControl::SetLabel() | |
430 | wxWindowBase::DoUpdateWindowUI(event); | |
cb719f2e | 431 | |
a3a4105d | 432 | // update title |
e39af974 JS |
433 | if ( event.GetSetText() ) |
434 | { | |
435 | if ( event.GetText() != GetTitle() ) | |
436 | SetTitle(event.GetText()); | |
437 | } | |
438 | } | |
439 | ||
447fd332 | 440 | void wxTopLevelWindowBase::RequestUserAttention(int WXUNUSED(flags)) |
9b59c95b VZ |
441 | { |
442 | // it's probably better than do nothing, isn't it? | |
443 | Raise(); | |
444 | } |