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 if ( IsLastBeforeExit() )
70 // no other (important) windows left, quit the app
71 wxTheApp
->ExitMainLoop();
75 bool wxTopLevelWindowBase::Destroy()
77 // delayed destruction: the frame will be deleted during the next idle
79 if ( !wxPendingDelete
.Member(this) )
80 wxPendingDelete
.Append(this);
82 if (wxTopLevelWindows
.GetCount() > 1)
84 // Hide it immediately. This should
85 // not be done if this TLW is the
86 // only one left since we then would
87 // risk not to get any idle events
88 // at all anymore during which we
89 // could delete any pending events.
96 bool wxTopLevelWindowBase::IsLastBeforeExit() const
98 // first of all, automatically exiting the app on last window close can be
99 // completely disabled at wxTheApp level
100 if ( !wxTheApp
|| !wxTheApp
->GetExitOnFrameDelete() )
103 wxWindowList::const_iterator i
;
104 const wxWindowList::const_iterator end
= wxTopLevelWindows
.end();
106 // then decide whether we should exit at all
107 for ( i
= wxTopLevelWindows
.begin(); i
!= end
; ++i
)
109 wxTopLevelWindow
* const win
= wx_static_cast(wxTopLevelWindow
*, *i
);
110 if ( win
->ShouldPreventAppExit() )
112 // there remains at least one important TLW, don't exit
117 // if yes, close all the other windows: this could still fail
118 for ( i
= wxTopLevelWindows
.begin(); i
!= end
; ++i
)
120 // don't close twice the windows which are already marked for deletion
121 wxTopLevelWindow
* const win
= wx_static_cast(wxTopLevelWindow
*, *i
);
122 if ( !wxPendingDelete
.Member(win
) && !win
->Close() )
124 // one of the windows refused to close, don't exit
126 // NB: of course, by now some other windows could have been already
127 // closed but there is really nothing we can do about it as we
128 // have no way just to ask the window if it can close without
129 // forcing it to do it
137 // ----------------------------------------------------------------------------
138 // wxTopLevelWindow geometry
139 // ----------------------------------------------------------------------------
141 void wxTopLevelWindowBase::GetRectForTopLevelChildren(int *x
, int *y
, int *w
, int *h
)
147 wxSize
wxTopLevelWindowBase::GetMaxSize() const
149 wxSize
size( GetMaxWidth(), GetMaxHeight() );
152 wxClientDisplayRect( 0, 0, &w
, &h
);
154 if( size
.GetWidth() == wxDefaultCoord
)
157 if( size
.GetHeight() == wxDefaultCoord
)
164 wxSize
wxTopLevelWindowBase::GetDefaultSize()
166 wxSize size
= wxGetClientDisplayRect().GetSize();
168 // create proportionally bigger windows on small screens
169 if ( size
.x
>= 1024 )
171 else if ( size
.x
>= 800 )
173 else if ( size
.x
>= 320 )
178 else if ( size
.y
> 200 )
187 void wxTopLevelWindowBase::DoCentre(int dir
)
189 // on some platforms centering top level windows is impossible
190 // because they are always maximized by guidelines or limitations
191 if(IsAlwaysMaximized())
194 // we need the display rect anyhow so store it first
195 int nDisplay
= wxDisplay::GetFromWindow(this);
196 wxDisplay
dpy(nDisplay
== wxNOT_FOUND
? 0 : nDisplay
);
197 const wxRect
rectDisplay(dpy
.GetClientArea());
199 // what should we centre this window on?
201 if ( !(dir
& wxCENTRE_ON_SCREEN
) && GetParent() )
203 // centre on parent window: notice that we need screen coordinates for
204 // positioning this TLW
205 rectParent
= GetParent()->GetScreenRect();
207 // if the parent is entirely off screen (happens at least with MDI
208 // parent frame under Mac but could happen elsewhere too if the frame
209 // was hidden/moved away for some reason), don't use it as otherwise
210 // this window wouldn't be visible at all
211 if ( !rectDisplay
.Inside(rectParent
.GetTopLeft()) &&
212 !rectParent
.Inside(rectParent
.GetBottomRight()) )
214 // this is enough to make IsEmpty() test below pass
215 rectParent
.width
= 0;
219 if ( rectParent
.IsEmpty() )
221 // we were explicitely asked to centre this window on the entire screen
222 // or if we have no parent anyhow and so can't centre on it
223 rectParent
= rectDisplay
;
226 // centering maximized window on screen is no-op
227 if((rectParent
== rectDisplay
) && IsMaximized())
230 // the new window rect candidate
231 wxRect rect
= GetRect().CentreIn(rectParent
, dir
);
233 // we don't want to place the window off screen if Centre() is called as
234 // this is (almost?) never wanted and it would be very difficult to prevent
235 // it from happening from the user code if we didn't check for it here
236 if ( rectDisplay
.Inside(rect
.GetTopLeft()) )
238 if ( !rectDisplay
.Inside(rect
.GetBottomRight()) )
240 // check if we can move the window so that the bottom right corner
241 // is visible without hiding the top left one
242 int dx
= rectDisplay
.GetRight() - rect
.GetRight();
243 int dy
= rectDisplay
.GetBottom() - rect
.GetBottom();
246 //else: the window top left and bottom right corner are both visible,
247 // although the window might still be not entirely on screen (with
248 // 2 staggered displays for example) we wouldn't be able to
249 // improve the layout much in such case, so just leave it as is
251 else // make top left corner visible
253 if ( rect
.x
< rectDisplay
.x
)
254 rect
.x
= rectDisplay
.x
;
256 if ( rect
.y
< rectDisplay
.y
)
257 rect
.y
= rectDisplay
.y
;
260 // -1 could be valid coordinate here if there are several displays
261 SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
264 // ----------------------------------------------------------------------------
265 // wxTopLevelWindow size management: we exclude the areas taken by
266 // menu/status/toolbars from the client area, so the client area is what's
267 // really available for the frame contents
268 // ----------------------------------------------------------------------------
270 void wxTopLevelWindowBase::DoScreenToClient(int *x
, int *y
) const
272 wxWindow::DoScreenToClient(x
, y
);
274 // translate the wxWindow client coords to our client coords
275 wxPoint
pt(GetClientAreaOrigin());
282 void wxTopLevelWindowBase::DoClientToScreen(int *x
, int *y
) const
284 // our client area origin (0, 0) may be really something like (0, 30) for
285 // wxWindow if we have a toolbar, account for it before translating
286 wxPoint
pt(GetClientAreaOrigin());
292 wxWindow::DoClientToScreen(x
, y
);
295 bool wxTopLevelWindowBase::IsAlwaysMaximized() const
297 #if defined(__SMARTPHONE__) || defined(__POCKETPC__)
304 // ----------------------------------------------------------------------------
306 // ----------------------------------------------------------------------------
308 // default resizing behaviour - if only ONE subwindow, resize to fill the
310 void wxTopLevelWindowBase::DoLayout()
312 // if we're using constraints or sizers - do use them
313 if ( GetAutoLayout() )
319 // do we have _exactly_ one child?
320 wxWindow
*child
= (wxWindow
*)NULL
;
321 for ( wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
323 node
= node
->GetNext() )
325 wxWindow
*win
= node
->GetData();
327 // exclude top level and managed windows (status bar isn't
328 // currently in the children list except under wxMac anyhow, but
329 // it makes no harm to test for it)
330 if ( !win
->IsTopLevel() && !IsOneOfBars(win
) )
334 return; // it's our second subwindow - nothing to do
341 // do we have any children at all?
342 if ( child
&& child
->IsShown() )
344 // exactly one child - set it's size to fill the whole frame
345 int clientW
, clientH
;
346 DoGetClientSize(&clientW
, &clientH
);
348 // for whatever reasons, wxGTK wants to have a small offset - it
349 // probably looks better with it?
351 static const int ofs
= 1;
353 static const int ofs
= 0;
356 child
->SetSize(ofs
, ofs
, clientW
- 2*ofs
, clientH
- 2*ofs
);
361 // The default implementation for the close window event.
362 void wxTopLevelWindowBase::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
367 bool wxTopLevelWindowBase::SendIconizeEvent(bool iconized
)
369 wxIconizeEvent
event(GetId(), iconized
);
370 event
.SetEventObject(this);
372 return GetEventHandler()->ProcessEvent(event
);
375 // do the window-specific processing after processing the update event
376 void wxTopLevelWindowBase::DoUpdateWindowUI(wxUpdateUIEvent
& event
)
378 // call inherited, but skip the wxControl's version, and call directly the
379 // wxWindow's one instead, because the only reason why we are overriding this
380 // function is that we want to use SetTitle() instead of wxControl::SetLabel()
381 wxWindowBase::DoUpdateWindowUI(event
);
384 if ( event
.GetSetText() )
386 if ( event
.GetText() != GetTitle() )
387 SetTitle(event
.GetText());
391 void wxTopLevelWindowBase::RequestUserAttention(int WXUNUSED(flags
))
393 // it's probably better than do nothing, isn't it?