1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/toplvcmn.cpp
3 // Purpose: common (for all platforms) wxTopLevelWindow functions
4 // Author: Julian Smart, Vadim Zeitlin
6 // Copyright: (c) 1998 Robert Roebling and Julian Smart
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // ============================================================================
12 // ============================================================================
14 // ----------------------------------------------------------------------------
16 // ----------------------------------------------------------------------------
18 // For compilers that support precompilation, includes "wx.h".
19 #include "wx/wxprec.h"
25 #include "wx/toplevel.h"
28 #include "wx/dcclient.h"
32 #include "wx/display.h"
34 // ----------------------------------------------------------------------------
36 // ----------------------------------------------------------------------------
38 BEGIN_EVENT_TABLE(wxTopLevelWindowBase
, wxWindow
)
39 EVT_CLOSE(wxTopLevelWindowBase::OnCloseWindow
)
40 EVT_SIZE(wxTopLevelWindowBase::OnSize
)
43 // ============================================================================
45 // ============================================================================
47 IMPLEMENT_ABSTRACT_CLASS(wxTopLevelWindow
, wxWindow
)
49 // ----------------------------------------------------------------------------
50 // construction/destruction
51 // ----------------------------------------------------------------------------
53 wxTopLevelWindowBase::wxTopLevelWindowBase()
55 // Unlike windows, top level windows are created hidden by default.
59 wxTopLevelWindowBase::~wxTopLevelWindowBase()
61 // don't let wxTheApp keep any stale pointers to us
62 if ( wxTheApp
&& wxTheApp
->GetTopWindow() == this )
63 wxTheApp
->SetTopWindow(NULL
);
65 wxTopLevelWindows
.DeleteObject(this);
67 // delete any our top level children which are still pending for deletion
68 // immediately: this could happen if a child (e.g. a temporary dialog
69 // created with this window as parent) was Destroy()'d) while this window
70 // was deleted directly (with delete, or maybe just because it was created
71 // on the stack) immediately afterwards and before the child TLW was really
72 // destroyed -- not destroying it now would leave it alive with a dangling
73 // parent pointer and result in a crash later
74 for ( wxObjectList::iterator i
= wxPendingDelete
.begin();
75 i
!= wxPendingDelete
.end();
78 wxWindow
* const win
= wxDynamicCast(*i
, wxWindow
);
79 if ( win
&& win
->GetParent() == this )
81 wxPendingDelete
.erase(i
);
85 // deleting it invalidated the list (and not only one node because
86 // it could have resulted in deletion of other objects to)
87 i
= wxPendingDelete
.begin();
95 if ( IsLastBeforeExit() )
97 // no other (important) windows left, quit the app
98 wxTheApp
->ExitMainLoop();
102 bool wxTopLevelWindowBase::Destroy()
104 // delayed destruction: the frame will be deleted during the next idle
106 if ( !wxPendingDelete
.Member(this) )
107 wxPendingDelete
.Append(this);
109 // normally we want to hide the window immediately so that it doesn't get
110 // stuck on the screen while it's being destroyed, however we shouldn't
111 // hide the last visible window as then we might not get any idle events
112 // any more as no events will be sent to the hidden window and without idle
113 // events we won't prune wxPendingDelete list and the application won't
115 for ( wxWindowList::const_iterator i
= wxTopLevelWindows
.begin(),
116 end
= wxTopLevelWindows
.end();
120 wxTopLevelWindow
* const win
= static_cast<wxTopLevelWindow
*>(*i
);
121 if ( win
!= this && win
->IsShown() )
123 // there remains at least one other visible TLW, we can hide this
134 bool wxTopLevelWindowBase::IsLastBeforeExit() const
136 // first of all, automatically exiting the app on last window close can be
137 // completely disabled at wxTheApp level
138 if ( !wxTheApp
|| !wxTheApp
->GetExitOnFrameDelete() )
141 wxWindowList::const_iterator i
;
142 const wxWindowList::const_iterator end
= wxTopLevelWindows
.end();
144 // then decide whether we should exit at all
145 for ( i
= wxTopLevelWindows
.begin(); i
!= end
; ++i
)
147 wxTopLevelWindow
* const win
= static_cast<wxTopLevelWindow
*>(*i
);
148 if ( win
->ShouldPreventAppExit() )
150 // there remains at least one important TLW, don't exit
155 // if yes, close all the other windows: this could still fail
156 for ( i
= wxTopLevelWindows
.begin(); i
!= end
; ++i
)
158 // don't close twice the windows which are already marked for deletion
159 wxTopLevelWindow
* const win
= static_cast<wxTopLevelWindow
*>(*i
);
160 if ( !wxPendingDelete
.Member(win
) && !win
->Close() )
162 // one of the windows refused to close, don't exit
164 // NB: of course, by now some other windows could have been already
165 // closed but there is really nothing we can do about it as we
166 // have no way just to ask the window if it can close without
167 // forcing it to do it
175 // ----------------------------------------------------------------------------
176 // wxTopLevelWindow geometry
177 // ----------------------------------------------------------------------------
179 void wxTopLevelWindowBase::SetMinSize(const wxSize
& minSize
)
181 SetSizeHints(minSize
, GetMaxSize());
184 void wxTopLevelWindowBase::SetMaxSize(const wxSize
& maxSize
)
186 SetSizeHints(GetMinSize(), maxSize
);
189 void wxTopLevelWindowBase::GetRectForTopLevelChildren(int *x
, int *y
, int *w
, int *h
)
196 wxSize
wxTopLevelWindowBase::GetDefaultSize()
198 wxSize size
= wxGetClientDisplayRect().GetSize();
199 #ifndef __WXOSX_IPHONE__
200 // create proportionally bigger windows on small screens
201 if ( size
.x
>= 1024 )
203 else if ( size
.x
>= 800 )
205 else if ( size
.x
>= 320 )
210 else if ( size
.y
> 200 )
219 void wxTopLevelWindowBase::DoCentre(int dir
)
221 // on some platforms centering top level windows is impossible
222 // because they are always maximized by guidelines or limitations
224 // and centering a maximized window doesn't make sense as its position
226 if ( IsAlwaysMaximized() || IsMaximized() )
229 // we need the display rect anyhow so store it first: notice that we should
230 // be centered on the same display as our parent window, the display of
231 // this window itself is not really defined yet
232 int nDisplay
= wxDisplay::GetFromWindow(GetParent() ? GetParent() : this);
233 wxDisplay
dpy(nDisplay
== wxNOT_FOUND
? 0 : nDisplay
);
234 const wxRect
rectDisplay(dpy
.GetClientArea());
236 // what should we centre this window on?
238 if ( !(dir
& wxCENTRE_ON_SCREEN
) && GetParent() )
240 // centre on parent window: notice that we need screen coordinates for
241 // positioning this TLW
242 rectParent
= GetParent()->GetScreenRect();
244 // if the parent is entirely off screen (happens at least with MDI
245 // parent frame under Mac but could happen elsewhere too if the frame
246 // was hidden/moved away for some reason), don't use it as otherwise
247 // this window wouldn't be visible at all
248 if ( !rectParent
.Intersects(rectDisplay
) )
250 // just centre on screen then
251 rectParent
= rectDisplay
;
256 // we were explicitly asked to centre this window on the entire screen
257 // or if we have no parent anyhow and so can't centre on it
258 rectParent
= rectDisplay
;
261 if ( !(dir
& wxBOTH
) )
262 dir
|= wxBOTH
; // if neither is specified, center in both directions
264 // the new window rect candidate
265 wxRect rect
= GetRect().CentreIn(rectParent
, dir
& ~wxCENTRE_ON_SCREEN
);
267 // we don't want to place the window off screen if Centre() is called as
268 // this is (almost?) never wanted and it would be very difficult to prevent
269 // it from happening from the user code if we didn't check for it here
270 if ( !rectDisplay
.Contains(rect
.GetTopLeft()) )
272 // move the window just enough to make the corner visible
273 int dx
= rectDisplay
.GetLeft() - rect
.GetLeft();
274 int dy
= rectDisplay
.GetTop() - rect
.GetTop();
275 rect
.Offset(dx
> 0 ? dx
: 0, dy
> 0 ? dy
: 0);
278 if ( !rectDisplay
.Contains(rect
.GetBottomRight()) )
280 // do the same for this corner too
281 int dx
= rectDisplay
.GetRight() - rect
.GetRight();
282 int dy
= rectDisplay
.GetBottom() - rect
.GetBottom();
283 rect
.Offset(dx
< 0 ? dx
: 0, dy
< 0 ? dy
: 0);
286 // the window top left and bottom right corner are both visible now and
287 // although the window might still be not entirely on screen (with 2
288 // staggered displays for example) we wouldn't be able to improve the
289 // layout much in such case, so we stop here
291 // -1 could be valid coordinate here if there are several displays
292 SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
295 // ----------------------------------------------------------------------------
296 // wxTopLevelWindow size management: we exclude the areas taken by
297 // menu/status/toolbars from the client area, so the client area is what's
298 // really available for the frame contents
299 // ----------------------------------------------------------------------------
301 void wxTopLevelWindowBase::DoScreenToClient(int *x
, int *y
) const
303 wxWindow::DoScreenToClient(x
, y
);
305 // translate the wxWindow client coords to our client coords
306 wxPoint
pt(GetClientAreaOrigin());
313 void wxTopLevelWindowBase::DoClientToScreen(int *x
, int *y
) const
315 // our client area origin (0, 0) may be really something like (0, 30) for
316 // wxWindow if we have a toolbar, account for it before translating
317 wxPoint
pt(GetClientAreaOrigin());
323 wxWindow::DoClientToScreen(x
, y
);
326 bool wxTopLevelWindowBase::IsAlwaysMaximized() const
328 #if defined(__SMARTPHONE__) || defined(__POCKETPC__)
335 // ----------------------------------------------------------------------------
337 // ----------------------------------------------------------------------------
339 wxIcon
wxTopLevelWindowBase::GetIcon() const
341 return m_icons
.IsEmpty() ? wxIcon() : m_icons
.GetIcon( -1 );
344 void wxTopLevelWindowBase::SetIcon(const wxIcon
& icon
)
346 // passing wxNullIcon to SetIcon() is possible (it means that we shouldn't
347 // have any icon), but adding an invalid icon to wxIconBundle is not
355 // ----------------------------------------------------------------------------
357 // ----------------------------------------------------------------------------
359 // default resizing behaviour - if only ONE subwindow, resize to fill the
361 void wxTopLevelWindowBase::DoLayout()
363 // We are called during the window destruction several times, e.g. as
364 // wxFrame tries to adjust to its tool/status bars disappearing. But
365 // actually doing the layout is pretty useless in this case as the window
366 // will disappear anyhow -- so just don't bother.
367 if ( IsBeingDeleted() )
371 // if we're using constraints or sizers - do use them
372 if ( GetAutoLayout() )
378 // do we have _exactly_ one child?
379 wxWindow
*child
= NULL
;
380 for ( wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
382 node
= node
->GetNext() )
384 wxWindow
*win
= node
->GetData();
386 // exclude top level and managed windows (status bar isn't
387 // currently in the children list except under wxMac anyhow, but
388 // it makes no harm to test for it)
389 if ( !win
->IsTopLevel() && !IsOneOfBars(win
) )
393 return; // it's our second subwindow - nothing to do
400 // do we have any children at all?
401 if ( child
&& child
->IsShown() )
403 // exactly one child - set it's size to fill the whole frame
404 int clientW
, clientH
;
405 DoGetClientSize(&clientW
, &clientH
);
407 child
->SetSize(0, 0, clientW
, clientH
);
412 // The default implementation for the close window event.
413 void wxTopLevelWindowBase::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
418 bool wxTopLevelWindowBase::SendIconizeEvent(bool iconized
)
420 wxIconizeEvent
event(GetId(), iconized
);
421 event
.SetEventObject(this);
423 return GetEventHandler()->ProcessEvent(event
);
426 // do the window-specific processing after processing the update event
427 void wxTopLevelWindowBase::DoUpdateWindowUI(wxUpdateUIEvent
& event
)
429 // call inherited, but skip the wxControl's version, and call directly the
430 // wxWindow's one instead, because the only reason why we are overriding this
431 // function is that we want to use SetTitle() instead of wxControl::SetLabel()
432 wxWindowBase::DoUpdateWindowUI(event
);
435 if ( event
.GetSetText() )
437 if ( event
.GetText() != GetTitle() )
438 SetTitle(event
.GetText());
442 void wxTopLevelWindowBase::RequestUserAttention(int WXUNUSED(flags
))
444 // it's probably better than do nothing, isn't it?