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 EVT_WINDOW_DESTROY(wxTopLevelWindowBase::OnChildDestroy
)
43 WX_EVENT_TABLE_CONTROL_CONTAINER(wxTopLevelWindowBase
)
46 WX_DELEGATE_TO_CONTROL_CONTAINER(wxTopLevelWindowBase
, wxWindow
)
48 // ============================================================================
50 // ============================================================================
52 IMPLEMENT_ABSTRACT_CLASS(wxTopLevelWindow
, wxWindow
)
54 // ----------------------------------------------------------------------------
55 // construction/destruction
56 // ----------------------------------------------------------------------------
58 wxTopLevelWindowBase::wxTopLevelWindowBase()
60 // Unlike windows, top level windows are created hidden by default.
63 m_winTmpDefault
= NULL
;
65 WX_INIT_CONTROL_CONTAINER();
68 wxTopLevelWindowBase::~wxTopLevelWindowBase()
71 m_winTmpDefault
= NULL
;
73 // don't let wxTheApp keep any stale pointers to us
74 if ( wxTheApp
&& wxTheApp
->GetTopWindow() == this )
75 wxTheApp
->SetTopWindow(NULL
);
77 wxTopLevelWindows
.DeleteObject(this);
79 // delete any our top level children which are still pending for deletion
80 // immediately: this could happen if a child (e.g. a temporary dialog
81 // created with this window as parent) was Destroy()'d) while this window
82 // was deleted directly (with delete, or maybe just because it was created
83 // on the stack) immediately afterwards and before the child TLW was really
84 // destroyed -- not destroying it now would leave it alive with a dangling
85 // parent pointer and result in a crash later
86 for ( wxObjectList::iterator i
= wxPendingDelete
.begin();
87 i
!= wxPendingDelete
.end();
90 wxWindow
* const win
= wxDynamicCast(*i
, wxWindow
);
91 if ( win
&& win
->GetParent() == this )
93 wxPendingDelete
.erase(i
);
97 // deleting it invalidated the list (and not only one node because
98 // it could have resulted in deletion of other objects to)
99 i
= wxPendingDelete
.begin();
107 if ( IsLastBeforeExit() )
109 // no other (important) windows left, quit the app
110 wxTheApp
->ExitMainLoop();
114 bool wxTopLevelWindowBase::Destroy()
116 // delayed destruction: the frame will be deleted during the next idle
118 if ( !wxPendingDelete
.Member(this) )
119 wxPendingDelete
.Append(this);
121 // normally we want to hide the window immediately so that it doesn't get
122 // stuck on the screen while it's being destroyed, however we shouldn't
123 // hide the last visible window as then we might not get any idle events
124 // any more as no events will be sent to the hidden window and without idle
125 // events we won't prune wxPendingDelete list and the application won't
127 for ( wxWindowList::const_iterator i
= wxTopLevelWindows
.begin(),
128 end
= wxTopLevelWindows
.end();
132 wxTopLevelWindow
* const win
= wx_static_cast(wxTopLevelWindow
*, *i
);
133 if ( win
!= this && win
->IsShown() )
135 // there remains at least one other visible TLW, we can hide this
146 bool wxTopLevelWindowBase::IsLastBeforeExit() const
148 // first of all, automatically exiting the app on last window close can be
149 // completely disabled at wxTheApp level
150 if ( !wxTheApp
|| !wxTheApp
->GetExitOnFrameDelete() )
153 wxWindowList::const_iterator i
;
154 const wxWindowList::const_iterator end
= wxTopLevelWindows
.end();
156 // then decide whether we should exit at all
157 for ( i
= wxTopLevelWindows
.begin(); i
!= end
; ++i
)
159 wxTopLevelWindow
* const win
= wx_static_cast(wxTopLevelWindow
*, *i
);
160 if ( win
->ShouldPreventAppExit() )
162 // there remains at least one important TLW, don't exit
167 // if yes, close all the other windows: this could still fail
168 for ( i
= wxTopLevelWindows
.begin(); i
!= end
; ++i
)
170 // don't close twice the windows which are already marked for deletion
171 wxTopLevelWindow
* const win
= wx_static_cast(wxTopLevelWindow
*, *i
);
172 if ( !wxPendingDelete
.Member(win
) && !win
->Close() )
174 // one of the windows refused to close, don't exit
176 // NB: of course, by now some other windows could have been already
177 // closed but there is really nothing we can do about it as we
178 // have no way just to ask the window if it can close without
179 // forcing it to do it
187 // ----------------------------------------------------------------------------
188 // wxTopLevelWindow geometry
189 // ----------------------------------------------------------------------------
191 void wxTopLevelWindowBase::SetMinSize(const wxSize
& minSize
)
193 SetSizeHints(minSize
, GetMaxSize());
196 void wxTopLevelWindowBase::SetMaxSize(const wxSize
& maxSize
)
198 SetSizeHints(GetMinSize(), maxSize
);
201 void wxTopLevelWindowBase::GetRectForTopLevelChildren(int *x
, int *y
, int *w
, int *h
)
208 wxSize
wxTopLevelWindowBase::GetDefaultSize()
210 wxSize size
= wxGetClientDisplayRect().GetSize();
212 // create proportionally bigger windows on small screens
213 if ( size
.x
>= 1024 )
215 else if ( size
.x
>= 800 )
217 else if ( size
.x
>= 320 )
222 else if ( size
.y
> 200 )
231 void wxTopLevelWindowBase::DoCentre(int dir
)
233 // on some platforms centering top level windows is impossible
234 // because they are always maximized by guidelines or limitations
235 if(IsAlwaysMaximized())
238 // we need the display rect anyhow so store it first: notice that we should
239 // be centered on the same display as our parent window, the display of
240 // this window itself is not really defined yet
241 int nDisplay
= wxDisplay::GetFromWindow(GetParent() ? GetParent() : this);
242 wxDisplay
dpy(nDisplay
== wxNOT_FOUND
? 0 : nDisplay
);
243 const wxRect
rectDisplay(dpy
.GetClientArea());
245 // what should we centre this window on?
247 if ( !(dir
& wxCENTRE_ON_SCREEN
) && GetParent() )
249 // centre on parent window: notice that we need screen coordinates for
250 // positioning this TLW
251 rectParent
= GetParent()->GetScreenRect();
253 // if the parent is entirely off screen (happens at least with MDI
254 // parent frame under Mac but could happen elsewhere too if the frame
255 // was hidden/moved away for some reason), don't use it as otherwise
256 // this window wouldn't be visible at all
257 if ( !rectDisplay
.Contains(rectParent
.GetTopLeft()) &&
258 !rectParent
.Contains(rectParent
.GetBottomRight()) )
260 // this is enough to make IsEmpty() test below pass
261 rectParent
.width
= 0;
265 if ( rectParent
.IsEmpty() )
267 // we were explicitly asked to centre this window on the entire screen
268 // or if we have no parent anyhow and so can't centre on it
269 rectParent
= rectDisplay
;
272 // centering maximized window on screen is no-op
273 if((rectParent
== rectDisplay
) && IsMaximized())
276 if ( !(dir
& wxBOTH
) )
277 dir
|= wxBOTH
; // if neither is specified, center in both directions
279 // the new window rect candidate
280 wxRect rect
= GetRect().CentreIn(rectParent
, dir
& ~wxCENTRE_ON_SCREEN
);
282 // we don't want to place the window off screen if Centre() is called as
283 // this is (almost?) never wanted and it would be very difficult to prevent
284 // it from happening from the user code if we didn't check for it here
285 if ( !rectDisplay
.Contains(rect
.GetTopLeft()) )
287 // move the window just enough to make the corner visible
288 int dx
= rectDisplay
.GetLeft() - rect
.GetLeft();
289 int dy
= rectDisplay
.GetTop() - rect
.GetTop();
290 rect
.Offset(dx
> 0 ? dx
: 0, dy
> 0 ? dy
: 0);
293 if ( !rectDisplay
.Contains(rect
.GetBottomRight()) )
295 // do the same for this corner too
296 int dx
= rectDisplay
.GetRight() - rect
.GetRight();
297 int dy
= rectDisplay
.GetBottom() - rect
.GetBottom();
298 rect
.Offset(dx
< 0 ? dx
: 0, dy
< 0 ? dy
: 0);
301 // the window top left and bottom right corner are both visible now and
302 // although the window might still be not entirely on screen (with 2
303 // staggered displays for example) we wouldn't be able to improve the
304 // layout much in such case, so we stop here
306 // -1 could be valid coordinate here if there are several displays
307 SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
310 // ----------------------------------------------------------------------------
311 // wxTopLevelWindow size management: we exclude the areas taken by
312 // menu/status/toolbars from the client area, so the client area is what's
313 // really available for the frame contents
314 // ----------------------------------------------------------------------------
316 void wxTopLevelWindowBase::DoScreenToClient(int *x
, int *y
) const
318 wxWindow::DoScreenToClient(x
, y
);
320 // translate the wxWindow client coords to our client coords
321 wxPoint
pt(GetClientAreaOrigin());
328 void wxTopLevelWindowBase::DoClientToScreen(int *x
, int *y
) const
330 // our client area origin (0, 0) may be really something like (0, 30) for
331 // wxWindow if we have a toolbar, account for it before translating
332 wxPoint
pt(GetClientAreaOrigin());
338 wxWindow::DoClientToScreen(x
, y
);
341 bool wxTopLevelWindowBase::IsAlwaysMaximized() const
343 #if defined(__SMARTPHONE__) || defined(__POCKETPC__)
350 // ----------------------------------------------------------------------------
352 // ----------------------------------------------------------------------------
354 wxIcon
wxTopLevelWindowBase::GetIcon() const
356 return m_icons
.IsEmpty() ? wxIcon() : m_icons
.GetIcon( -1 );
359 void wxTopLevelWindowBase::SetIcon(const wxIcon
& icon
)
361 // passing wxNullIcon to SetIcon() is possible (it means that we shouldn't
362 // have any icon), but adding an invalid icon to wxIconBundle is not
370 // ----------------------------------------------------------------------------
372 // ----------------------------------------------------------------------------
374 // default resizing behaviour - if only ONE subwindow, resize to fill the
376 void wxTopLevelWindowBase::DoLayout()
378 // if we're using constraints or sizers - do use them
379 if ( GetAutoLayout() )
385 // do we have _exactly_ one child?
386 wxWindow
*child
= (wxWindow
*)NULL
;
387 for ( wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
389 node
= node
->GetNext() )
391 wxWindow
*win
= node
->GetData();
393 // exclude top level and managed windows (status bar isn't
394 // currently in the children list except under wxMac anyhow, but
395 // it makes no harm to test for it)
396 if ( !win
->IsTopLevel() && !IsOneOfBars(win
) )
400 return; // it's our second subwindow - nothing to do
407 // do we have any children at all?
408 if ( child
&& child
->IsShown() )
410 // exactly one child - set it's size to fill the whole frame
411 int clientW
, clientH
;
412 DoGetClientSize(&clientW
, &clientH
);
414 child
->SetSize(0, 0, clientW
, clientH
);
419 // The default implementation for the close window event.
420 void wxTopLevelWindowBase::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
425 void wxTopLevelWindowBase::OnChildDestroy(wxWindowDestroyEvent
& event
)
429 wxWindow
* const win
= event
.GetWindow();
430 if ( win
== m_winDefault
)
432 if ( win
== m_winTmpDefault
)
433 m_winTmpDefault
= NULL
;
436 bool wxTopLevelWindowBase::SendIconizeEvent(bool iconized
)
438 wxIconizeEvent
event(GetId(), iconized
);
439 event
.SetEventObject(this);
441 return GetEventHandler()->ProcessEvent(event
);
444 // do the window-specific processing after processing the update event
445 void wxTopLevelWindowBase::DoUpdateWindowUI(wxUpdateUIEvent
& event
)
447 // call inherited, but skip the wxControl's version, and call directly the
448 // wxWindow's one instead, because the only reason why we are overriding this
449 // function is that we want to use SetTitle() instead of wxControl::SetLabel()
450 wxWindowBase::DoUpdateWindowUI(event
);
453 if ( event
.GetSetText() )
455 if ( event
.GetText() != GetTitle() )
456 SetTitle(event
.GetText());
460 void wxTopLevelWindowBase::RequestUserAttention(int WXUNUSED(flags
))
462 // it's probably better than do nothing, isn't it?