1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/toplvcmn.cpp
3 // Purpose: common (for all platforms) wxTopLevelWindow functions
4 // Author: Julian Smart, Vadim Zeitlin
7 // Copyright: (c) 1998 Robert Roebling and Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
26 #include "wx/toplevel.h"
29 #include "wx/dcclient.h"
33 #include "wx/display.h"
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
39 BEGIN_EVENT_TABLE(wxTopLevelWindowBase
, wxWindow
)
40 EVT_CLOSE(wxTopLevelWindowBase::OnCloseWindow
)
41 EVT_SIZE(wxTopLevelWindowBase::OnSize
)
42 WX_EVENT_TABLE_CONTROL_CONTAINER(wxTopLevelWindowBase
)
45 WX_DELEGATE_TO_CONTROL_CONTAINER(wxTopLevelWindowBase
, wxWindow
)
47 // ============================================================================
49 // ============================================================================
51 IMPLEMENT_ABSTRACT_CLASS(wxTopLevelWindow
, wxWindow
)
53 // ----------------------------------------------------------------------------
54 // construction/destruction
55 // ----------------------------------------------------------------------------
57 wxTopLevelWindowBase::wxTopLevelWindowBase()
59 // Unlike windows, top level windows are created hidden by default.
62 WX_INIT_CONTROL_CONTAINER();
65 wxTopLevelWindowBase::~wxTopLevelWindowBase()
67 // don't let wxTheApp keep any stale pointers to us
68 if ( wxTheApp
&& wxTheApp
->GetTopWindow() == this )
69 wxTheApp
->SetTopWindow(NULL
);
71 wxTopLevelWindows
.DeleteObject(this);
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();
84 wxWindow
* const win
= wxDynamicCast(*i
, wxWindow
);
85 if ( win
&& win
->GetParent() == this )
87 wxPendingDelete
.erase(i
);
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();
101 if ( IsLastBeforeExit() )
103 // no other (important) windows left, quit the app
104 wxTheApp
->ExitMainLoop();
108 bool wxTopLevelWindowBase::Destroy()
110 // delayed destruction: the frame will be deleted during the next idle
112 if ( !wxPendingDelete
.Member(this) )
113 wxPendingDelete
.Append(this);
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
121 for ( wxWindowList::const_iterator i
= wxTopLevelWindows
.begin(),
122 end
= wxTopLevelWindows
.end();
126 wxTopLevelWindow
* const win
= static_cast<wxTopLevelWindow
*>(*i
);
127 if ( win
!= this && win
->IsShown() )
129 // there remains at least one other visible TLW, we can hide this
140 bool wxTopLevelWindowBase::IsLastBeforeExit() const
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() )
147 wxWindowList::const_iterator i
;
148 const wxWindowList::const_iterator end
= wxTopLevelWindows
.end();
150 // then decide whether we should exit at all
151 for ( i
= wxTopLevelWindows
.begin(); i
!= end
; ++i
)
153 wxTopLevelWindow
* const win
= static_cast<wxTopLevelWindow
*>(*i
);
154 if ( win
->ShouldPreventAppExit() )
156 // there remains at least one important TLW, don't exit
161 // if yes, close all the other windows: this could still fail
162 for ( i
= wxTopLevelWindows
.begin(); i
!= end
; ++i
)
164 // don't close twice the windows which are already marked for deletion
165 wxTopLevelWindow
* const win
= static_cast<wxTopLevelWindow
*>(*i
);
166 if ( !wxPendingDelete
.Member(win
) && !win
->Close() )
168 // one of the windows refused to close, don't exit
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
181 // ----------------------------------------------------------------------------
182 // wxTopLevelWindow geometry
183 // ----------------------------------------------------------------------------
185 void wxTopLevelWindowBase::SetMinSize(const wxSize
& minSize
)
187 SetSizeHints(minSize
, GetMaxSize());
190 void wxTopLevelWindowBase::SetMaxSize(const wxSize
& maxSize
)
192 SetSizeHints(GetMinSize(), maxSize
);
195 void wxTopLevelWindowBase::GetRectForTopLevelChildren(int *x
, int *y
, int *w
, int *h
)
202 wxSize
wxTopLevelWindowBase::GetDefaultSize()
204 wxSize size
= wxGetClientDisplayRect().GetSize();
205 #ifndef __WXOSX_IPHONE__
206 // create proportionally bigger windows on small screens
207 if ( size
.x
>= 1024 )
209 else if ( size
.x
>= 800 )
211 else if ( size
.x
>= 320 )
216 else if ( size
.y
> 200 )
225 void wxTopLevelWindowBase::DoCentre(int dir
)
227 // on some platforms centering top level windows is impossible
228 // because they are always maximized by guidelines or limitations
229 if(IsAlwaysMaximized())
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);
236 wxDisplay
dpy(nDisplay
== wxNOT_FOUND
? 0 : nDisplay
);
237 const wxRect
rectDisplay(dpy
.GetClientArea());
239 // what should we centre this window on?
241 if ( !(dir
& wxCENTRE_ON_SCREEN
) && GetParent() )
243 // centre on parent window: notice that we need screen coordinates for
244 // positioning this TLW
245 rectParent
= GetParent()->GetScreenRect();
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
251 if ( !rectDisplay
.Contains(rectParent
.GetTopLeft()) &&
252 !rectParent
.Contains(rectParent
.GetBottomRight()) )
254 // this is enough to make IsEmpty() test below pass
255 rectParent
.width
= 0;
259 if ( rectParent
.IsEmpty() )
261 // we were explicitly asked to centre this window on the entire screen
262 // or if we have no parent anyhow and so can't centre on it
263 rectParent
= rectDisplay
;
266 // centering maximized window on screen is no-op
267 if((rectParent
== rectDisplay
) && IsMaximized())
270 if ( !(dir
& wxBOTH
) )
271 dir
|= wxBOTH
; // if neither is specified, center in both directions
273 // the new window rect candidate
274 wxRect rect
= GetRect().CentreIn(rectParent
, dir
& ~wxCENTRE_ON_SCREEN
);
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
279 if ( !rectDisplay
.Contains(rect
.GetTopLeft()) )
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);
287 if ( !rectDisplay
.Contains(rect
.GetBottomRight()) )
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);
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
300 // -1 could be valid coordinate here if there are several displays
301 SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
304 // ----------------------------------------------------------------------------
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
308 // ----------------------------------------------------------------------------
310 void wxTopLevelWindowBase::DoScreenToClient(int *x
, int *y
) const
312 wxWindow::DoScreenToClient(x
, y
);
314 // translate the wxWindow client coords to our client coords
315 wxPoint
pt(GetClientAreaOrigin());
322 void wxTopLevelWindowBase::DoClientToScreen(int *x
, int *y
) const
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());
332 wxWindow::DoClientToScreen(x
, y
);
335 bool wxTopLevelWindowBase::IsAlwaysMaximized() const
337 #if defined(__SMARTPHONE__) || defined(__POCKETPC__)
344 // ----------------------------------------------------------------------------
346 // ----------------------------------------------------------------------------
348 wxIcon
wxTopLevelWindowBase::GetIcon() const
350 return m_icons
.IsEmpty() ? wxIcon() : m_icons
.GetIcon( -1 );
353 void wxTopLevelWindowBase::SetIcon(const wxIcon
& icon
)
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
364 // ----------------------------------------------------------------------------
366 // ----------------------------------------------------------------------------
368 // default resizing behaviour - if only ONE subwindow, resize to fill the
370 void wxTopLevelWindowBase::DoLayout()
372 // if we're using constraints or sizers - do use them
373 if ( GetAutoLayout() )
379 // do we have _exactly_ one child?
380 wxWindow
*child
= NULL
;
381 for ( wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
383 node
= node
->GetNext() )
385 wxWindow
*win
= node
->GetData();
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
) )
394 return; // it's our second subwindow - nothing to do
401 // do we have any children at all?
402 if ( child
&& child
->IsShown() )
404 // exactly one child - set it's size to fill the whole frame
405 int clientW
, clientH
;
406 DoGetClientSize(&clientW
, &clientH
);
408 child
->SetSize(0, 0, clientW
, clientH
);
413 // The default implementation for the close window event.
414 void wxTopLevelWindowBase::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
419 bool wxTopLevelWindowBase::SendIconizeEvent(bool iconized
)
421 wxIconizeEvent
event(GetId(), iconized
);
422 event
.SetEventObject(this);
424 return GetEventHandler()->ProcessEvent(event
);
427 // do the window-specific processing after processing the update event
428 void wxTopLevelWindowBase::DoUpdateWindowUI(wxUpdateUIEvent
& event
)
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
);
436 if ( event
.GetSetText() )
438 if ( event
.GetText() != GetTitle() )
439 SetTitle(event
.GetText());
443 void wxTopLevelWindowBase::RequestUserAttention(int WXUNUSED(flags
))
445 // it's probably better than do nothing, isn't it?