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()
70 m_winDefault
= m_winTmpDefault
= NULL
;
72 // don't let wxTheApp keep any stale pointers to us
73 if ( wxTheApp
&& wxTheApp
->GetTopWindow() == this )
74 wxTheApp
->SetTopWindow(NULL
);
76 wxTopLevelWindows
.DeleteObject(this);
78 if ( IsLastBeforeExit() )
80 // no other (important) windows left, quit the app
81 wxTheApp
->ExitMainLoop();
85 bool wxTopLevelWindowBase::Destroy()
87 // delayed destruction: the frame will be deleted during the next idle
89 if ( !wxPendingDelete
.Member(this) )
90 wxPendingDelete
.Append(this);
92 if (wxTopLevelWindows
.GetCount() > 1)
94 // Hide it immediately. This should
95 // not be done if this TLW is the
96 // only one left since we then would
97 // risk not to get any idle events
98 // at all anymore during which we
99 // could delete any pending events.
106 bool wxTopLevelWindowBase::IsLastBeforeExit() const
108 // first of all, automatically exiting the app on last window close can be
109 // completely disabled at wxTheApp level
110 if ( !wxTheApp
|| !wxTheApp
->GetExitOnFrameDelete() )
113 wxWindowList::const_iterator i
;
114 const wxWindowList::const_iterator end
= wxTopLevelWindows
.end();
116 // then decide whether we should exit at all
117 for ( i
= wxTopLevelWindows
.begin(); i
!= end
; ++i
)
119 wxTopLevelWindow
* const win
= wx_static_cast(wxTopLevelWindow
*, *i
);
120 if ( win
->ShouldPreventAppExit() )
122 // there remains at least one important TLW, don't exit
127 // if yes, close all the other windows: this could still fail
128 for ( i
= wxTopLevelWindows
.begin(); i
!= end
; ++i
)
130 // don't close twice the windows which are already marked for deletion
131 wxTopLevelWindow
* const win
= wx_static_cast(wxTopLevelWindow
*, *i
);
132 if ( !wxPendingDelete
.Member(win
) && !win
->Close() )
134 // one of the windows refused to close, don't exit
136 // NB: of course, by now some other windows could have been already
137 // closed but there is really nothing we can do about it as we
138 // have no way just to ask the window if it can close without
139 // forcing it to do it
147 // ----------------------------------------------------------------------------
148 // wxTopLevelWindow geometry
149 // ----------------------------------------------------------------------------
151 void wxTopLevelWindowBase::SetMinSize(const wxSize
& minSize
)
153 SetSizeHints(minSize
, GetMaxSize());
156 void wxTopLevelWindowBase::SetMaxSize(const wxSize
& maxSize
)
158 SetSizeHints(GetMinSize(), maxSize
);
161 void wxTopLevelWindowBase::GetRectForTopLevelChildren(int *x
, int *y
, int *w
, int *h
)
168 wxSize
wxTopLevelWindowBase::GetDefaultSize()
170 wxSize size
= wxGetClientDisplayRect().GetSize();
172 // create proportionally bigger windows on small screens
173 if ( size
.x
>= 1024 )
175 else if ( size
.x
>= 800 )
177 else if ( size
.x
>= 320 )
182 else if ( size
.y
> 200 )
191 void wxTopLevelWindowBase::DoCentre(int dir
)
193 // on some platforms centering top level windows is impossible
194 // because they are always maximized by guidelines or limitations
195 if(IsAlwaysMaximized())
198 // we need the display rect anyhow so store it first: notice that we should
199 // be centered on the same display as our parent window, the display of
200 // this window itself is not really defined yet
201 int nDisplay
= wxDisplay::GetFromWindow(GetParent() ? GetParent() : this);
202 wxDisplay
dpy(nDisplay
== wxNOT_FOUND
? 0 : nDisplay
);
203 const wxRect
rectDisplay(dpy
.GetClientArea());
205 // what should we centre this window on?
207 if ( !(dir
& wxCENTRE_ON_SCREEN
) && GetParent() )
209 // centre on parent window: notice that we need screen coordinates for
210 // positioning this TLW
211 rectParent
= GetParent()->GetScreenRect();
213 // if the parent is entirely off screen (happens at least with MDI
214 // parent frame under Mac but could happen elsewhere too if the frame
215 // was hidden/moved away for some reason), don't use it as otherwise
216 // this window wouldn't be visible at all
217 if ( !rectDisplay
.Contains(rectParent
.GetTopLeft()) &&
218 !rectParent
.Contains(rectParent
.GetBottomRight()) )
220 // this is enough to make IsEmpty() test below pass
221 rectParent
.width
= 0;
225 if ( rectParent
.IsEmpty() )
227 // we were explicitly asked to centre this window on the entire screen
228 // or if we have no parent anyhow and so can't centre on it
229 rectParent
= rectDisplay
;
232 // centering maximized window on screen is no-op
233 if((rectParent
== rectDisplay
) && IsMaximized())
236 // the new window rect candidate
237 wxRect rect
= GetRect().CentreIn(rectParent
, dir
);
239 // we don't want to place the window off screen if Centre() is called as
240 // this is (almost?) never wanted and it would be very difficult to prevent
241 // it from happening from the user code if we didn't check for it here
242 if ( !rectDisplay
.Contains(rect
.GetTopLeft()) )
244 // move the window just enough to make the corner visible
245 int dx
= rectDisplay
.GetLeft() - rect
.GetLeft();
246 int dy
= rectDisplay
.GetTop() - rect
.GetTop();
247 rect
.Offset(dx
> 0 ? dx
: 0, dy
> 0 ? dy
: 0);
250 if ( !rectDisplay
.Contains(rect
.GetBottomRight()) )
252 // do the same for this corner too
253 int dx
= rectDisplay
.GetRight() - rect
.GetRight();
254 int dy
= rectDisplay
.GetBottom() - rect
.GetBottom();
255 rect
.Offset(dx
< 0 ? dx
: 0, dy
< 0 ? dy
: 0);
258 // the window top left and bottom right corner are both visible now and
259 // although the window might still be not entirely on screen (with 2
260 // staggered displays for example) we wouldn't be able to improve the
261 // layout much in such case, so we stop here
263 // -1 could be valid coordinate here if there are several displays
264 SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
267 // ----------------------------------------------------------------------------
268 // wxTopLevelWindow size management: we exclude the areas taken by
269 // menu/status/toolbars from the client area, so the client area is what's
270 // really available for the frame contents
271 // ----------------------------------------------------------------------------
273 void wxTopLevelWindowBase::DoScreenToClient(int *x
, int *y
) const
275 wxWindow::DoScreenToClient(x
, y
);
277 // translate the wxWindow client coords to our client coords
278 wxPoint
pt(GetClientAreaOrigin());
285 void wxTopLevelWindowBase::DoClientToScreen(int *x
, int *y
) const
287 // our client area origin (0, 0) may be really something like (0, 30) for
288 // wxWindow if we have a toolbar, account for it before translating
289 wxPoint
pt(GetClientAreaOrigin());
295 wxWindow::DoClientToScreen(x
, y
);
298 bool wxTopLevelWindowBase::IsAlwaysMaximized() const
300 #if defined(__SMARTPHONE__) || defined(__POCKETPC__)
307 // ----------------------------------------------------------------------------
309 // ----------------------------------------------------------------------------
311 wxIcon
wxTopLevelWindowBase::GetIcon() const
313 return m_icons
.IsEmpty() ? wxIcon() : m_icons
.GetIcon( -1 );
316 void wxTopLevelWindowBase::SetIcon(const wxIcon
& icon
)
318 // passing wxNullIcon to SetIcon() is possible (it means that we shouldn't
319 // have any icon), but adding an invalid icon to wxIconBundle is not
327 // ----------------------------------------------------------------------------
329 // ----------------------------------------------------------------------------
331 // default resizing behaviour - if only ONE subwindow, resize to fill the
333 void wxTopLevelWindowBase::DoLayout()
335 // if we're using constraints or sizers - do use them
336 if ( GetAutoLayout() )
342 // do we have _exactly_ one child?
343 wxWindow
*child
= (wxWindow
*)NULL
;
344 for ( wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
346 node
= node
->GetNext() )
348 wxWindow
*win
= node
->GetData();
350 // exclude top level and managed windows (status bar isn't
351 // currently in the children list except under wxMac anyhow, but
352 // it makes no harm to test for it)
353 if ( !win
->IsTopLevel() && !IsOneOfBars(win
) )
357 return; // it's our second subwindow - nothing to do
364 // do we have any children at all?
365 if ( child
&& child
->IsShown() )
367 // exactly one child - set it's size to fill the whole frame
368 int clientW
, clientH
;
369 DoGetClientSize(&clientW
, &clientH
);
371 child
->SetSize(0, 0, clientW
, clientH
);
376 // The default implementation for the close window event.
377 void wxTopLevelWindowBase::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
382 void wxTopLevelWindowBase::OnChildDestroy(wxWindowDestroyEvent
& event
)
386 wxWindow
* const win
= event
.GetWindow();
387 if ( win
== m_winDefault
)
389 if ( win
== m_winTmpDefault
)
390 m_winTmpDefault
= NULL
;
393 bool wxTopLevelWindowBase::SendIconizeEvent(bool iconized
)
395 wxIconizeEvent
event(GetId(), iconized
);
396 event
.SetEventObject(this);
398 return GetEventHandler()->ProcessEvent(event
);
401 // do the window-specific processing after processing the update event
402 void wxTopLevelWindowBase::DoUpdateWindowUI(wxUpdateUIEvent
& event
)
404 // call inherited, but skip the wxControl's version, and call directly the
405 // wxWindow's one instead, because the only reason why we are overriding this
406 // function is that we want to use SetTitle() instead of wxControl::SetLabel()
407 wxWindowBase::DoUpdateWindowUI(event
);
410 if ( event
.GetSetText() )
412 if ( event
.GetText() != GetTitle() )
413 SetTitle(event
.GetText());
417 void wxTopLevelWindowBase::RequestUserAttention(int WXUNUSED(flags
))
419 // it's probably better than do nothing, isn't it?