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.
59 m_winTmpDefault
= NULL
;
62 wxTopLevelWindowBase::~wxTopLevelWindowBase()
64 // don't let wxTheApp keep any stale pointers to us
65 if ( wxTheApp
&& wxTheApp
->GetTopWindow() == this )
66 wxTheApp
->SetTopWindow(NULL
);
68 wxTopLevelWindows
.DeleteObject(this);
70 if ( IsLastBeforeExit() )
72 // no other (important) windows left, quit the app
73 wxTheApp
->ExitMainLoop();
77 bool wxTopLevelWindowBase::Destroy()
79 // delayed destruction: the frame will be deleted during the next idle
81 if ( !wxPendingDelete
.Member(this) )
82 wxPendingDelete
.Append(this);
84 if (wxTopLevelWindows
.GetCount() > 1)
86 // Hide it immediately. This should
87 // not be done if this TLW is the
88 // only one left since we then would
89 // risk not to get any idle events
90 // at all anymore during which we
91 // could delete any pending events.
98 bool wxTopLevelWindowBase::IsLastBeforeExit() const
100 // first of all, automatically exiting the app on last window close can be
101 // completely disabled at wxTheApp level
102 if ( !wxTheApp
|| !wxTheApp
->GetExitOnFrameDelete() )
105 wxWindowList::const_iterator i
;
106 const wxWindowList::const_iterator end
= wxTopLevelWindows
.end();
108 // then decide whether we should exit at all
109 for ( i
= wxTopLevelWindows
.begin(); i
!= end
; ++i
)
111 wxTopLevelWindow
* const win
= wx_static_cast(wxTopLevelWindow
*, *i
);
112 if ( win
->ShouldPreventAppExit() )
114 // there remains at least one important TLW, don't exit
119 // if yes, close all the other windows: this could still fail
120 for ( i
= wxTopLevelWindows
.begin(); i
!= end
; ++i
)
122 // don't close twice the windows which are already marked for deletion
123 wxTopLevelWindow
* const win
= wx_static_cast(wxTopLevelWindow
*, *i
);
124 if ( !wxPendingDelete
.Member(win
) && !win
->Close() )
126 // one of the windows refused to close, don't exit
128 // NB: of course, by now some other windows could have been already
129 // closed but there is really nothing we can do about it as we
130 // have no way just to ask the window if it can close without
131 // forcing it to do it
139 // ----------------------------------------------------------------------------
140 // wxTopLevelWindow geometry
141 // ----------------------------------------------------------------------------
143 void wxTopLevelWindowBase::GetRectForTopLevelChildren(int *x
, int *y
, int *w
, int *h
)
149 wxSize
wxTopLevelWindowBase::GetMaxSize() const
151 wxSize
size( GetMaxWidth(), GetMaxHeight() );
154 wxClientDisplayRect( 0, 0, &w
, &h
);
156 if( size
.GetWidth() == wxDefaultCoord
)
159 if( size
.GetHeight() == wxDefaultCoord
)
166 wxSize
wxTopLevelWindowBase::GetDefaultSize()
168 wxSize size
= wxGetClientDisplayRect().GetSize();
170 // create proportionally bigger windows on small screens
171 if ( size
.x
>= 1024 )
173 else if ( size
.x
>= 800 )
175 else if ( size
.x
>= 320 )
180 else if ( size
.y
> 200 )
189 void wxTopLevelWindowBase::DoCentre(int dir
)
191 // on some platforms centering top level windows is impossible
192 // because they are always maximized by guidelines or limitations
193 if(IsAlwaysMaximized())
196 // we need the display rect anyhow so store it first
197 int nDisplay
= wxDisplay::GetFromWindow(this);
198 wxDisplay
dpy(nDisplay
== wxNOT_FOUND
? 0 : nDisplay
);
199 const wxRect
rectDisplay(dpy
.GetClientArea());
201 // what should we centre this window on?
203 if ( !(dir
& wxCENTRE_ON_SCREEN
) && GetParent() )
205 // centre on parent window: notice that we need screen coordinates for
206 // positioning this TLW
207 rectParent
= GetParent()->GetScreenRect();
209 // if the parent is entirely off screen (happens at least with MDI
210 // parent frame under Mac but could happen elsewhere too if the frame
211 // was hidden/moved away for some reason), don't use it as otherwise
212 // this window wouldn't be visible at all
213 if ( !rectDisplay
.Inside(rectParent
.GetTopLeft()) &&
214 !rectParent
.Inside(rectParent
.GetBottomRight()) )
216 // this is enough to make IsEmpty() test below pass
217 rectParent
.width
= 0;
221 if ( rectParent
.IsEmpty() )
223 // we were explicitely asked to centre this window on the entire screen
224 // or if we have no parent anyhow and so can't centre on it
225 rectParent
= rectDisplay
;
228 // centering maximized window on screen is no-op
229 if((rectParent
== rectDisplay
) && IsMaximized())
232 // the new window rect candidate
233 wxRect rect
= GetRect().CentreIn(rectParent
, dir
);
235 // we don't want to place the window off screen if Centre() is called as
236 // this is (almost?) never wanted and it would be very difficult to prevent
237 // it from happening from the user code if we didn't check for it here
238 if ( rectDisplay
.Inside(rect
.GetTopLeft()) )
240 if ( !rectDisplay
.Inside(rect
.GetBottomRight()) )
242 // check if we can move the window so that the bottom right corner
243 // is visible without hiding the top left one
244 int dx
= rectDisplay
.GetRight() - rect
.GetRight();
245 int dy
= rectDisplay
.GetBottom() - rect
.GetBottom();
248 //else: the window top left and bottom right corner are both visible,
249 // although the window might still be not entirely on screen (with
250 // 2 staggered displays for example) we wouldn't be able to
251 // improve the layout much in such case, so just leave it as is
253 else // make top left corner visible
255 if ( rect
.x
< rectDisplay
.x
)
256 rect
.x
= rectDisplay
.x
;
258 if ( rect
.y
< rectDisplay
.y
)
259 rect
.y
= rectDisplay
.y
;
262 // -1 could be valid coordinate here if there are several displays
263 SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
266 // ----------------------------------------------------------------------------
267 // wxTopLevelWindow size management: we exclude the areas taken by
268 // menu/status/toolbars from the client area, so the client area is what's
269 // really available for the frame contents
270 // ----------------------------------------------------------------------------
272 void wxTopLevelWindowBase::DoScreenToClient(int *x
, int *y
) const
274 wxWindow::DoScreenToClient(x
, y
);
276 // translate the wxWindow client coords to our client coords
277 wxPoint
pt(GetClientAreaOrigin());
284 void wxTopLevelWindowBase::DoClientToScreen(int *x
, int *y
) const
286 // our client area origin (0, 0) may be really something like (0, 30) for
287 // wxWindow if we have a toolbar, account for it before translating
288 wxPoint
pt(GetClientAreaOrigin());
294 wxWindow::DoClientToScreen(x
, y
);
297 bool wxTopLevelWindowBase::IsAlwaysMaximized() const
299 #if defined(__SMARTPHONE__) || defined(__POCKETPC__)
306 // ----------------------------------------------------------------------------
308 // ----------------------------------------------------------------------------
310 // default resizing behaviour - if only ONE subwindow, resize to fill the
312 void wxTopLevelWindowBase::DoLayout()
314 // if we're using constraints or sizers - do use them
315 if ( GetAutoLayout() )
321 // do we have _exactly_ one child?
322 wxWindow
*child
= (wxWindow
*)NULL
;
323 for ( wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
325 node
= node
->GetNext() )
327 wxWindow
*win
= node
->GetData();
329 // exclude top level and managed windows (status bar isn't
330 // currently in the children list except under wxMac anyhow, but
331 // it makes no harm to test for it)
332 if ( !win
->IsTopLevel() && !IsOneOfBars(win
) )
336 return; // it's our second subwindow - nothing to do
343 // do we have any children at all?
344 if ( child
&& child
->IsShown() )
346 // exactly one child - set it's size to fill the whole frame
347 int clientW
, clientH
;
348 DoGetClientSize(&clientW
, &clientH
);
350 // for whatever reasons, wxGTK wants to have a small offset - it
351 // probably looks better with it?
353 static const int ofs
= 1;
355 static const int ofs
= 0;
358 child
->SetSize(ofs
, ofs
, clientW
- 2*ofs
, clientH
- 2*ofs
);
363 // The default implementation for the close window event.
364 void wxTopLevelWindowBase::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
369 bool wxTopLevelWindowBase::SendIconizeEvent(bool iconized
)
371 wxIconizeEvent
event(GetId(), iconized
);
372 event
.SetEventObject(this);
374 return GetEventHandler()->ProcessEvent(event
);
377 // do the window-specific processing after processing the update event
378 void wxTopLevelWindowBase::DoUpdateWindowUI(wxUpdateUIEvent
& event
)
380 // call inherited, but skip the wxControl's version, and call directly the
381 // wxWindow's one instead, because the only reason why we are overriding this
382 // function is that we want to use SetTitle() instead of wxControl::SetLabel()
383 wxWindowBase::DoUpdateWindowUI(event
);
386 if ( event
.GetSetText() )
388 if ( event
.GetText() != GetTitle() )
389 SetTitle(event
.GetText());
393 void wxTopLevelWindowBase::RequestUserAttention(int WXUNUSED(flags
))
395 // it's probably better than do nothing, isn't it?
399 void wxTopLevelWindowBase::RemoveChild(wxWindowBase
*child
)
401 if ( child
== m_winDefault
)
404 if ( child
== m_winTmpDefault
)
405 m_winTmpDefault
= NULL
;
407 wxWindow::RemoveChild(child
);