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
)
44 // ============================================================================
46 // ============================================================================
48 IMPLEMENT_ABSTRACT_CLASS(wxTopLevelWindow
, wxWindow
)
50 // ----------------------------------------------------------------------------
51 // construction/destruction
52 // ----------------------------------------------------------------------------
54 wxTopLevelWindowBase::wxTopLevelWindowBase()
56 // Unlike windows, top level windows are created hidden by default.
60 wxTopLevelWindowBase::~wxTopLevelWindowBase()
62 // don't let wxTheApp keep any stale pointers to us
63 if ( wxTheApp
&& wxTheApp
->GetTopWindow() == this )
64 wxTheApp
->SetTopWindow(NULL
);
66 wxTopLevelWindows
.DeleteObject(this);
68 // delete any our top level children which are still pending for deletion
69 // immediately: this could happen if a child (e.g. a temporary dialog
70 // created with this window as parent) was Destroy()'d) while this window
71 // was deleted directly (with delete, or maybe just because it was created
72 // on the stack) immediately afterwards and before the child TLW was really
73 // destroyed -- not destroying it now would leave it alive with a dangling
74 // parent pointer and result in a crash later
75 for ( wxObjectList::iterator i
= wxPendingDelete
.begin();
76 i
!= wxPendingDelete
.end();
79 wxWindow
* const win
= wxDynamicCast(*i
, wxWindow
);
80 if ( win
&& win
->GetParent() == this )
82 wxPendingDelete
.erase(i
);
86 // deleting it invalidated the list (and not only one node because
87 // it could have resulted in deletion of other objects to)
88 i
= wxPendingDelete
.begin();
96 if ( IsLastBeforeExit() )
98 // no other (important) windows left, quit the app
99 wxTheApp
->ExitMainLoop();
103 bool wxTopLevelWindowBase::Destroy()
105 // delayed destruction: the frame will be deleted during the next idle
107 if ( !wxPendingDelete
.Member(this) )
108 wxPendingDelete
.Append(this);
110 // normally we want to hide the window immediately so that it doesn't get
111 // stuck on the screen while it's being destroyed, however we shouldn't
112 // hide the last visible window as then we might not get any idle events
113 // any more as no events will be sent to the hidden window and without idle
114 // events we won't prune wxPendingDelete list and the application won't
116 for ( wxWindowList::const_iterator i
= wxTopLevelWindows
.begin(),
117 end
= wxTopLevelWindows
.end();
121 wxTopLevelWindow
* const win
= static_cast<wxTopLevelWindow
*>(*i
);
122 if ( win
!= this && win
->IsShown() )
124 // there remains at least one other visible TLW, we can hide this
135 bool wxTopLevelWindowBase::IsLastBeforeExit() const
137 // first of all, automatically exiting the app on last window close can be
138 // completely disabled at wxTheApp level
139 if ( !wxTheApp
|| !wxTheApp
->GetExitOnFrameDelete() )
142 wxWindowList::const_iterator i
;
143 const wxWindowList::const_iterator end
= wxTopLevelWindows
.end();
145 // then decide whether we should exit at all
146 for ( i
= wxTopLevelWindows
.begin(); i
!= end
; ++i
)
148 wxTopLevelWindow
* const win
= static_cast<wxTopLevelWindow
*>(*i
);
149 if ( win
->ShouldPreventAppExit() )
151 // there remains at least one important TLW, don't exit
156 // if yes, close all the other windows: this could still fail
157 for ( i
= wxTopLevelWindows
.begin(); i
!= end
; ++i
)
159 // don't close twice the windows which are already marked for deletion
160 wxTopLevelWindow
* const win
= static_cast<wxTopLevelWindow
*>(*i
);
161 if ( !wxPendingDelete
.Member(win
) && !win
->Close() )
163 // one of the windows refused to close, don't exit
165 // NB: of course, by now some other windows could have been already
166 // closed but there is really nothing we can do about it as we
167 // have no way just to ask the window if it can close without
168 // forcing it to do it
176 // ----------------------------------------------------------------------------
177 // wxTopLevelWindow geometry
178 // ----------------------------------------------------------------------------
180 void wxTopLevelWindowBase::SetMinSize(const wxSize
& minSize
)
182 SetSizeHints(minSize
, GetMaxSize());
185 void wxTopLevelWindowBase::SetMaxSize(const wxSize
& maxSize
)
187 SetSizeHints(GetMinSize(), maxSize
);
190 void wxTopLevelWindowBase::GetRectForTopLevelChildren(int *x
, int *y
, int *w
, int *h
)
197 wxSize
wxTopLevelWindowBase::GetDefaultSize()
199 wxSize size
= wxGetClientDisplayRect().GetSize();
200 #ifndef __WXOSX_IPHONE__
201 // create proportionally bigger windows on small screens
202 if ( size
.x
>= 1024 )
204 else if ( size
.x
>= 800 )
206 else if ( size
.x
>= 320 )
211 else if ( size
.y
> 200 )
220 void wxTopLevelWindowBase::DoCentre(int dir
)
222 // on some platforms centering top level windows is impossible
223 // because they are always maximized by guidelines or limitations
225 // and centering a maximized window doesn't make sense as its position
227 if ( IsAlwaysMaximized() || IsMaximized() )
230 // we need the display rect anyhow so store it first: notice that we should
231 // be centered on the same display as our parent window, the display of
232 // this window itself is not really defined yet
233 int nDisplay
= wxDisplay::GetFromWindow(GetParent() ? GetParent() : this);
234 wxDisplay
dpy(nDisplay
== wxNOT_FOUND
? 0 : nDisplay
);
235 const wxRect
rectDisplay(dpy
.GetClientArea());
237 // what should we centre this window on?
239 if ( !(dir
& wxCENTRE_ON_SCREEN
) && GetParent() )
241 // centre on parent window: notice that we need screen coordinates for
242 // positioning this TLW
243 rectParent
= GetParent()->GetScreenRect();
245 // if the parent is entirely off screen (happens at least with MDI
246 // parent frame under Mac but could happen elsewhere too if the frame
247 // was hidden/moved away for some reason), don't use it as otherwise
248 // this window wouldn't be visible at all
249 if ( !rectParent
.Intersects(rectDisplay
) )
251 // just centre on screen then
252 rectParent
= rectDisplay
;
257 // we were explicitly asked to centre this window on the entire screen
258 // or if we have no parent anyhow and so can't centre on it
259 rectParent
= rectDisplay
;
262 if ( !(dir
& wxBOTH
) )
263 dir
|= wxBOTH
; // if neither is specified, center in both directions
265 // the new window rect candidate
266 wxRect rect
= GetRect().CentreIn(rectParent
, dir
& ~wxCENTRE_ON_SCREEN
);
268 // we don't want to place the window off screen if Centre() is called as
269 // this is (almost?) never wanted and it would be very difficult to prevent
270 // it from happening from the user code if we didn't check for it here
271 if ( !rectDisplay
.Contains(rect
.GetTopLeft()) )
273 // move the window just enough to make the corner visible
274 int dx
= rectDisplay
.GetLeft() - rect
.GetLeft();
275 int dy
= rectDisplay
.GetTop() - rect
.GetTop();
276 rect
.Offset(dx
> 0 ? dx
: 0, dy
> 0 ? dy
: 0);
279 if ( !rectDisplay
.Contains(rect
.GetBottomRight()) )
281 // do the same for this corner too
282 int dx
= rectDisplay
.GetRight() - rect
.GetRight();
283 int dy
= rectDisplay
.GetBottom() - rect
.GetBottom();
284 rect
.Offset(dx
< 0 ? dx
: 0, dy
< 0 ? dy
: 0);
287 // the window top left and bottom right corner are both visible now and
288 // although the window might still be not entirely on screen (with 2
289 // staggered displays for example) we wouldn't be able to improve the
290 // layout much in such case, so we stop here
292 // -1 could be valid coordinate here if there are several displays
293 SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
296 // ----------------------------------------------------------------------------
297 // wxTopLevelWindow size management: we exclude the areas taken by
298 // menu/status/toolbars from the client area, so the client area is what's
299 // really available for the frame contents
300 // ----------------------------------------------------------------------------
302 void wxTopLevelWindowBase::DoScreenToClient(int *x
, int *y
) const
304 wxWindow::DoScreenToClient(x
, y
);
306 // translate the wxWindow client coords to our client coords
307 wxPoint
pt(GetClientAreaOrigin());
314 void wxTopLevelWindowBase::DoClientToScreen(int *x
, int *y
) const
316 // our client area origin (0, 0) may be really something like (0, 30) for
317 // wxWindow if we have a toolbar, account for it before translating
318 wxPoint
pt(GetClientAreaOrigin());
324 wxWindow::DoClientToScreen(x
, y
);
327 bool wxTopLevelWindowBase::IsAlwaysMaximized() const
329 #if defined(__SMARTPHONE__) || defined(__POCKETPC__)
336 // ----------------------------------------------------------------------------
338 // ----------------------------------------------------------------------------
340 wxIcon
wxTopLevelWindowBase::GetIcon() const
342 return m_icons
.IsEmpty() ? wxIcon() : m_icons
.GetIcon( -1 );
345 void wxTopLevelWindowBase::SetIcon(const wxIcon
& icon
)
347 // passing wxNullIcon to SetIcon() is possible (it means that we shouldn't
348 // have any icon), but adding an invalid icon to wxIconBundle is not
356 // ----------------------------------------------------------------------------
358 // ----------------------------------------------------------------------------
360 // default resizing behaviour - if only ONE subwindow, resize to fill the
362 void wxTopLevelWindowBase::DoLayout()
364 // if we're using constraints or sizers - do use them
365 if ( GetAutoLayout() )
371 // do we have _exactly_ one child?
372 wxWindow
*child
= NULL
;
373 for ( wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
375 node
= node
->GetNext() )
377 wxWindow
*win
= node
->GetData();
379 // exclude top level and managed windows (status bar isn't
380 // currently in the children list except under wxMac anyhow, but
381 // it makes no harm to test for it)
382 if ( !win
->IsTopLevel() && !IsOneOfBars(win
) )
386 return; // it's our second subwindow - nothing to do
393 // do we have any children at all?
394 if ( child
&& child
->IsShown() )
396 // exactly one child - set it's size to fill the whole frame
397 int clientW
, clientH
;
398 DoGetClientSize(&clientW
, &clientH
);
400 child
->SetSize(0, 0, clientW
, clientH
);
405 // The default implementation for the close window event.
406 void wxTopLevelWindowBase::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
411 bool wxTopLevelWindowBase::SendIconizeEvent(bool iconized
)
413 wxIconizeEvent
event(GetId(), iconized
);
414 event
.SetEventObject(this);
416 return GetEventHandler()->ProcessEvent(event
);
419 // do the window-specific processing after processing the update event
420 void wxTopLevelWindowBase::DoUpdateWindowUI(wxUpdateUIEvent
& event
)
422 // call inherited, but skip the wxControl's version, and call directly the
423 // wxWindow's one instead, because the only reason why we are overriding this
424 // function is that we want to use SetTitle() instead of wxControl::SetLabel()
425 wxWindowBase::DoUpdateWindowUI(event
);
428 if ( event
.GetSetText() )
430 if ( event
.GetText() != GetTitle() )
431 SetTitle(event
.GetText());
435 void wxTopLevelWindowBase::RequestUserAttention(int WXUNUSED(flags
))
437 // it's probably better than do nothing, isn't it?