]>
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(); | |
205 | ||
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 | } | |
221 | ||
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 | |
229 | if(IsAlwaysMaximized()) | |
230 | return; | |
231 | ||
4745f3c6 VZ |
232 | // we need the display rect anyhow so store it first: notice that we should |
233 | // be centered on the same display as our parent window, the display of | |
234 | // this window itself is not really defined yet | |
235 | int nDisplay = wxDisplay::GetFromWindow(GetParent() ? GetParent() : this); | |
262a1536 VZ |
236 | wxDisplay dpy(nDisplay == wxNOT_FOUND ? 0 : nDisplay); |
237 | const wxRect rectDisplay(dpy.GetClientArea()); | |
238 | ||
239 | // what should we centre this window on? | |
240 | wxRect rectParent; | |
1f464296 VZ |
241 | if ( !(dir & wxCENTRE_ON_SCREEN) && GetParent() ) |
242 | { | |
5a63a0f1 VZ |
243 | // centre on parent window: notice that we need screen coordinates for |
244 | // positioning this TLW | |
262a1536 VZ |
245 | rectParent = GetParent()->GetScreenRect(); |
246 | ||
247 | // if the parent is entirely off screen (happens at least with MDI | |
248 | // parent frame under Mac but could happen elsewhere too if the frame | |
249 | // was hidden/moved away for some reason), don't use it as otherwise | |
250 | // this window wouldn't be visible at all | |
22a35096 VS |
251 | if ( !rectDisplay.Contains(rectParent.GetTopLeft()) && |
252 | !rectParent.Contains(rectParent.GetBottomRight()) ) | |
262a1536 VZ |
253 | { |
254 | // this is enough to make IsEmpty() test below pass | |
255 | rectParent.width = 0; | |
256 | } | |
1f464296 | 257 | } |
262a1536 VZ |
258 | |
259 | if ( rectParent.IsEmpty() ) | |
1f464296 | 260 | { |
ca08b543 | 261 | // we were explicitly asked to centre this window on the entire screen |
1f464296 | 262 | // or if we have no parent anyhow and so can't centre on it |
262a1536 VZ |
263 | rectParent = rectDisplay; |
264 | } | |
265 | ||
cca1624d WS |
266 | // centering maximized window on screen is no-op |
267 | if((rectParent == rectDisplay) && IsMaximized()) | |
268 | return; | |
269 | ||
75454d24 VZ |
270 | if ( !(dir & wxBOTH) ) |
271 | dir |= wxBOTH; // if neither is specified, center in both directions | |
272 | ||
262a1536 | 273 | // the new window rect candidate |
75454d24 | 274 | wxRect rect = GetRect().CentreIn(rectParent, dir & ~wxCENTRE_ON_SCREEN); |
262a1536 VZ |
275 | |
276 | // we don't want to place the window off screen if Centre() is called as | |
277 | // this is (almost?) never wanted and it would be very difficult to prevent | |
278 | // it from happening from the user code if we didn't check for it here | |
4745f3c6 | 279 | if ( !rectDisplay.Contains(rect.GetTopLeft()) ) |
262a1536 | 280 | { |
4745f3c6 VZ |
281 | // move the window just enough to make the corner visible |
282 | int dx = rectDisplay.GetLeft() - rect.GetLeft(); | |
283 | int dy = rectDisplay.GetTop() - rect.GetTop(); | |
284 | rect.Offset(dx > 0 ? dx : 0, dy > 0 ? dy : 0); | |
262a1536 | 285 | } |
262a1536 | 286 | |
4745f3c6 VZ |
287 | if ( !rectDisplay.Contains(rect.GetBottomRight()) ) |
288 | { | |
289 | // do the same for this corner too | |
290 | int dx = rectDisplay.GetRight() - rect.GetRight(); | |
291 | int dy = rectDisplay.GetBottom() - rect.GetBottom(); | |
292 | rect.Offset(dx < 0 ? dx : 0, dy < 0 ? dy : 0); | |
1f464296 VZ |
293 | } |
294 | ||
4745f3c6 VZ |
295 | // the window top left and bottom right corner are both visible now and |
296 | // although the window might still be not entirely on screen (with 2 | |
297 | // staggered displays for example) we wouldn't be able to improve the | |
298 | // layout much in such case, so we stop here | |
299 | ||
262a1536 VZ |
300 | // -1 could be valid coordinate here if there are several displays |
301 | SetSize(rect, wxSIZE_ALLOW_MINUS_ONE); | |
1f464296 VZ |
302 | } |
303 | ||
7d9f12f3 | 304 | // ---------------------------------------------------------------------------- |
82c9f85c VZ |
305 | // wxTopLevelWindow size management: we exclude the areas taken by |
306 | // menu/status/toolbars from the client area, so the client area is what's | |
307 | // really available for the frame contents | |
7d9f12f3 VS |
308 | // ---------------------------------------------------------------------------- |
309 | ||
310 | void wxTopLevelWindowBase::DoScreenToClient(int *x, int *y) const | |
311 | { | |
312 | wxWindow::DoScreenToClient(x, y); | |
313 | ||
82c9f85c | 314 | // translate the wxWindow client coords to our client coords |
7d9f12f3 | 315 | wxPoint pt(GetClientAreaOrigin()); |
82c9f85c VZ |
316 | if ( x ) |
317 | *x -= pt.x; | |
318 | if ( y ) | |
319 | *y -= pt.y; | |
7d9f12f3 VS |
320 | } |
321 | ||
322 | void wxTopLevelWindowBase::DoClientToScreen(int *x, int *y) const | |
323 | { | |
82c9f85c VZ |
324 | // our client area origin (0, 0) may be really something like (0, 30) for |
325 | // wxWindow if we have a toolbar, account for it before translating | |
326 | wxPoint pt(GetClientAreaOrigin()); | |
327 | if ( x ) | |
328 | *x += pt.x; | |
329 | if ( y ) | |
330 | *y += pt.y; | |
7d9f12f3 VS |
331 | |
332 | wxWindow::DoClientToScreen(x, y); | |
333 | } | |
334 | ||
979a0320 WS |
335 | bool wxTopLevelWindowBase::IsAlwaysMaximized() const |
336 | { | |
337 | #if defined(__SMARTPHONE__) || defined(__POCKETPC__) | |
338 | return true; | |
339 | #else | |
340 | return false; | |
341 | #endif | |
342 | } | |
7d9f12f3 VS |
343 | |
344 | // ---------------------------------------------------------------------------- | |
ea098413 VZ |
345 | // icons |
346 | // ---------------------------------------------------------------------------- | |
347 | ||
348 | wxIcon wxTopLevelWindowBase::GetIcon() const | |
349 | { | |
350 | return m_icons.IsEmpty() ? wxIcon() : m_icons.GetIcon( -1 ); | |
351 | } | |
352 | ||
353 | void wxTopLevelWindowBase::SetIcon(const wxIcon& icon) | |
354 | { | |
355 | // passing wxNullIcon to SetIcon() is possible (it means that we shouldn't | |
356 | // have any icon), but adding an invalid icon to wxIconBundle is not | |
357 | wxIconBundle icons; | |
358 | if ( icon.Ok() ) | |
359 | icons.AddIcon(icon); | |
360 | ||
361 | SetIcons(icons); | |
362 | } | |
363 | ||
364 | // ---------------------------------------------------------------------------- | |
7d9f12f3 VS |
365 | // event handlers |
366 | // ---------------------------------------------------------------------------- | |
367 | ||
368 | // default resizing behaviour - if only ONE subwindow, resize to fill the | |
369 | // whole client area | |
5e62d4a5 | 370 | void wxTopLevelWindowBase::DoLayout() |
7d9f12f3 | 371 | { |
bc55104d | 372 | // if we're using constraints or sizers - do use them |
7d9f12f3 VS |
373 | if ( GetAutoLayout() ) |
374 | { | |
375 | Layout(); | |
376 | } | |
377 | else | |
7d9f12f3 VS |
378 | { |
379 | // do we have _exactly_ one child? | |
d3b9f782 | 380 | wxWindow *child = NULL; |
222ed1d6 | 381 | for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst(); |
7d9f12f3 VS |
382 | node; |
383 | node = node->GetNext() ) | |
384 | { | |
385 | wxWindow *win = node->GetData(); | |
386 | ||
387 | // exclude top level and managed windows (status bar isn't | |
388 | // currently in the children list except under wxMac anyhow, but | |
389 | // it makes no harm to test for it) | |
390 | if ( !win->IsTopLevel() && !IsOneOfBars(win) ) | |
391 | { | |
392 | if ( child ) | |
393 | { | |
394 | return; // it's our second subwindow - nothing to do | |
395 | } | |
396 | ||
397 | child = win; | |
398 | } | |
399 | } | |
400 | ||
401 | // do we have any children at all? | |
d3cd1c03 | 402 | if ( child && child->IsShown() ) |
7d9f12f3 VS |
403 | { |
404 | // exactly one child - set it's size to fill the whole frame | |
405 | int clientW, clientH; | |
406 | DoGetClientSize(&clientW, &clientH); | |
407 | ||
cd8667eb | 408 | child->SetSize(0, 0, clientW, clientH); |
7d9f12f3 VS |
409 | } |
410 | } | |
411 | } | |
412 | ||
413 | // The default implementation for the close window event. | |
414 | void wxTopLevelWindowBase::OnCloseWindow(wxCloseEvent& WXUNUSED(event)) | |
415 | { | |
416 | Destroy(); | |
417 | } | |
418 | ||
419 | bool wxTopLevelWindowBase::SendIconizeEvent(bool iconized) | |
420 | { | |
421 | wxIconizeEvent event(GetId(), iconized); | |
422 | event.SetEventObject(this); | |
423 | ||
424 | return GetEventHandler()->ProcessEvent(event); | |
425 | } | |
34c3ffca | 426 | |
e39af974 JS |
427 | // do the window-specific processing after processing the update event |
428 | void wxTopLevelWindowBase::DoUpdateWindowUI(wxUpdateUIEvent& event) | |
429 | { | |
a3a4105d VZ |
430 | // call inherited, but skip the wxControl's version, and call directly the |
431 | // wxWindow's one instead, because the only reason why we are overriding this | |
432 | // function is that we want to use SetTitle() instead of wxControl::SetLabel() | |
433 | wxWindowBase::DoUpdateWindowUI(event); | |
cb719f2e | 434 | |
a3a4105d | 435 | // update title |
e39af974 JS |
436 | if ( event.GetSetText() ) |
437 | { | |
438 | if ( event.GetText() != GetTitle() ) | |
439 | SetTitle(event.GetText()); | |
440 | } | |
441 | } | |
442 | ||
447fd332 | 443 | void wxTopLevelWindowBase::RequestUserAttention(int WXUNUSED(flags)) |
9b59c95b VZ |
444 | { |
445 | // it's probably better than do nothing, isn't it? | |
446 | Raise(); | |
447 | } |