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
)
45 // ============================================================================
47 // ============================================================================
49 IMPLEMENT_ABSTRACT_CLASS(wxTopLevelWindow
, wxWindow
)
51 // ----------------------------------------------------------------------------
52 // construction/destruction
53 // ----------------------------------------------------------------------------
55 wxTopLevelWindowBase::wxTopLevelWindowBase()
57 // Unlike windows, top level windows are created hidden by default.
60 m_winTmpDefault
= NULL
;
63 wxTopLevelWindowBase::~wxTopLevelWindowBase()
65 // don't let wxTheApp keep any stale pointers to us
66 if ( wxTheApp
&& wxTheApp
->GetTopWindow() == this )
67 wxTheApp
->SetTopWindow(NULL
);
69 wxTopLevelWindows
.DeleteObject(this);
71 if ( IsLastBeforeExit() )
73 // no other (important) windows left, quit the app
74 wxTheApp
->ExitMainLoop();
78 bool wxTopLevelWindowBase::Destroy()
80 // delayed destruction: the frame will be deleted during the next idle
82 if ( !wxPendingDelete
.Member(this) )
83 wxPendingDelete
.Append(this);
85 if (wxTopLevelWindows
.GetCount() > 1)
87 // Hide it immediately. This should
88 // not be done if this TLW is the
89 // only one left since we then would
90 // risk not to get any idle events
91 // at all anymore during which we
92 // could delete any pending events.
99 bool wxTopLevelWindowBase::IsLastBeforeExit() const
101 // first of all, automatically exiting the app on last window close can be
102 // completely disabled at wxTheApp level
103 if ( !wxTheApp
|| !wxTheApp
->GetExitOnFrameDelete() )
106 wxWindowList::const_iterator i
;
107 const wxWindowList::const_iterator end
= wxTopLevelWindows
.end();
109 // then decide whether we should exit at all
110 for ( i
= wxTopLevelWindows
.begin(); i
!= end
; ++i
)
112 wxTopLevelWindow
* const win
= wx_static_cast(wxTopLevelWindow
*, *i
);
113 if ( win
->ShouldPreventAppExit() )
115 // there remains at least one important TLW, don't exit
120 // if yes, close all the other windows: this could still fail
121 for ( i
= wxTopLevelWindows
.begin(); i
!= end
; ++i
)
123 // don't close twice the windows which are already marked for deletion
124 wxTopLevelWindow
* const win
= wx_static_cast(wxTopLevelWindow
*, *i
);
125 if ( !wxPendingDelete
.Member(win
) && !win
->Close() )
127 // one of the windows refused to close, don't exit
129 // NB: of course, by now some other windows could have been already
130 // closed but there is really nothing we can do about it as we
131 // have no way just to ask the window if it can close without
132 // forcing it to do it
140 // ----------------------------------------------------------------------------
141 // wxTopLevelWindow geometry
142 // ----------------------------------------------------------------------------
144 void wxTopLevelWindowBase::SetMinSize(const wxSize
& minSize
)
146 SetSizeHints( minSize
.x
, minSize
.y
, GetMaxWidth(), GetMaxHeight() );
149 void wxTopLevelWindowBase::SetMaxSize(const wxSize
& maxSize
)
151 SetSizeHints( GetMinWidth(), GetMinHeight(), maxSize
.x
, maxSize
.y
);
154 // set the min/max size of the window
155 void wxTopLevelWindowBase::DoSetSizeHints(int minW
, int minH
,
157 int WXUNUSED(incW
), int WXUNUSED(incH
))
159 // setting min width greater than max width leads to infinite loops under
160 // X11 and generally doesn't make any sense, so don't allow it
161 wxCHECK_RET( (minW
== wxDefaultCoord
|| maxW
== wxDefaultCoord
|| minW
<= maxW
) &&
162 (minH
== wxDefaultCoord
|| maxH
== wxDefaultCoord
|| minH
<= maxH
),
163 _T("min width/height must be less than max width/height!") );
171 void wxTopLevelWindowBase::GetRectForTopLevelChildren(int *x
, int *y
, int *w
, int *h
)
178 wxSize
wxTopLevelWindowBase::GetDefaultSize()
180 wxSize size
= wxGetClientDisplayRect().GetSize();
182 // create proportionally bigger windows on small screens
183 if ( size
.x
>= 1024 )
185 else if ( size
.x
>= 800 )
187 else if ( size
.x
>= 320 )
192 else if ( size
.y
> 200 )
201 void wxTopLevelWindowBase::DoCentre(int dir
)
203 // on some platforms centering top level windows is impossible
204 // because they are always maximized by guidelines or limitations
205 if(IsAlwaysMaximized())
208 // we need the display rect anyhow so store it first
209 int nDisplay
= wxDisplay::GetFromWindow(this);
210 wxDisplay
dpy(nDisplay
== wxNOT_FOUND
? 0 : nDisplay
);
211 const wxRect
rectDisplay(dpy
.GetClientArea());
213 // what should we centre this window on?
215 if ( !(dir
& wxCENTRE_ON_SCREEN
) && GetParent() )
217 // centre on parent window: notice that we need screen coordinates for
218 // positioning this TLW
219 rectParent
= GetParent()->GetScreenRect();
221 // if the parent is entirely off screen (happens at least with MDI
222 // parent frame under Mac but could happen elsewhere too if the frame
223 // was hidden/moved away for some reason), don't use it as otherwise
224 // this window wouldn't be visible at all
225 if ( !rectDisplay
.Contains(rectParent
.GetTopLeft()) &&
226 !rectParent
.Contains(rectParent
.GetBottomRight()) )
228 // this is enough to make IsEmpty() test below pass
229 rectParent
.width
= 0;
233 if ( rectParent
.IsEmpty() )
235 // we were explicitly asked to centre this window on the entire screen
236 // or if we have no parent anyhow and so can't centre on it
237 rectParent
= rectDisplay
;
240 // centering maximized window on screen is no-op
241 if((rectParent
== rectDisplay
) && IsMaximized())
244 // the new window rect candidate
245 wxRect rect
= GetRect().CentreIn(rectParent
, dir
);
247 // we don't want to place the window off screen if Centre() is called as
248 // this is (almost?) never wanted and it would be very difficult to prevent
249 // it from happening from the user code if we didn't check for it here
250 if ( rectDisplay
.Contains(rect
.GetTopLeft()) )
252 if ( !rectDisplay
.Contains(rect
.GetBottomRight()) )
254 // move the window just enough for the bottom right corner to
256 int dx
= rectDisplay
.GetRight() - rect
.GetRight();
257 int dy
= rectDisplay
.GetBottom() - rect
.GetBottom();
258 rect
.Offset(dx
< 0 ? dx
: 0, dy
< 0 ? dy
: 0);
260 //else: the window top left and bottom right corner are both visible,
261 // although the window might still be not entirely on screen (with
262 // 2 staggered displays for example) we wouldn't be able to
263 // improve the layout much in such case, so just leave it as is
265 else // make top left corner visible
267 if ( rect
.x
< rectDisplay
.x
)
268 rect
.x
= rectDisplay
.x
;
270 if ( rect
.y
< rectDisplay
.y
)
271 rect
.y
= rectDisplay
.y
;
274 // -1 could be valid coordinate here if there are several displays
275 SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
278 // ----------------------------------------------------------------------------
279 // wxTopLevelWindow size management: we exclude the areas taken by
280 // menu/status/toolbars from the client area, so the client area is what's
281 // really available for the frame contents
282 // ----------------------------------------------------------------------------
284 void wxTopLevelWindowBase::DoScreenToClient(int *x
, int *y
) const
286 wxWindow::DoScreenToClient(x
, y
);
288 // translate the wxWindow client coords to our client coords
289 wxPoint
pt(GetClientAreaOrigin());
296 void wxTopLevelWindowBase::DoClientToScreen(int *x
, int *y
) const
298 // our client area origin (0, 0) may be really something like (0, 30) for
299 // wxWindow if we have a toolbar, account for it before translating
300 wxPoint
pt(GetClientAreaOrigin());
306 wxWindow::DoClientToScreen(x
, y
);
309 bool wxTopLevelWindowBase::IsAlwaysMaximized() const
311 #if defined(__SMARTPHONE__) || defined(__POCKETPC__)
318 // ----------------------------------------------------------------------------
320 // ----------------------------------------------------------------------------
322 wxIcon
wxTopLevelWindowBase::GetIcon() const
324 return m_icons
.IsEmpty() ? wxIcon() : m_icons
.GetIcon( -1 );
327 void wxTopLevelWindowBase::SetIcon(const wxIcon
& icon
)
329 // passing wxNullIcon to SetIcon() is possible (it means that we shouldn't
330 // have any icon), but adding an invalid icon to wxIconBundle is not
338 // ----------------------------------------------------------------------------
340 // ----------------------------------------------------------------------------
342 // default resizing behaviour - if only ONE subwindow, resize to fill the
344 void wxTopLevelWindowBase::DoLayout()
346 // if we're using constraints or sizers - do use them
347 if ( GetAutoLayout() )
353 // do we have _exactly_ one child?
354 wxWindow
*child
= (wxWindow
*)NULL
;
355 for ( wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
357 node
= node
->GetNext() )
359 wxWindow
*win
= node
->GetData();
361 // exclude top level and managed windows (status bar isn't
362 // currently in the children list except under wxMac anyhow, but
363 // it makes no harm to test for it)
364 if ( !win
->IsTopLevel() && !IsOneOfBars(win
) )
368 return; // it's our second subwindow - nothing to do
375 // do we have any children at all?
376 if ( child
&& child
->IsShown() )
378 // exactly one child - set it's size to fill the whole frame
379 int clientW
, clientH
;
380 DoGetClientSize(&clientW
, &clientH
);
382 child
->SetSize(0, 0, clientW
, clientH
);
387 // The default implementation for the close window event.
388 void wxTopLevelWindowBase::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
393 void wxTopLevelWindowBase::OnChildDestroy(wxWindowDestroyEvent
& event
)
397 wxWindow
* const win
= event
.GetWindow();
398 if ( win
== m_winDefault
)
400 if ( win
== m_winTmpDefault
)
401 m_winTmpDefault
= NULL
;
404 bool wxTopLevelWindowBase::SendIconizeEvent(bool iconized
)
406 wxIconizeEvent
event(GetId(), iconized
);
407 event
.SetEventObject(this);
409 return GetEventHandler()->ProcessEvent(event
);
412 // do the window-specific processing after processing the update event
413 void wxTopLevelWindowBase::DoUpdateWindowUI(wxUpdateUIEvent
& event
)
415 // call inherited, but skip the wxControl's version, and call directly the
416 // wxWindow's one instead, because the only reason why we are overriding this
417 // function is that we want to use SetTitle() instead of wxControl::SetLabel()
418 wxWindowBase::DoUpdateWindowUI(event
);
421 if ( event
.GetSetText() )
423 if ( event
.GetText() != GetTitle() )
424 SetTitle(event
.GetText());
428 void wxTopLevelWindowBase::RequestUserAttention(int WXUNUSED(flags
))
430 // it's probably better than do nothing, isn't it?
434 void wxTopLevelWindowBase::RemoveChild(wxWindowBase
*child
)
436 if ( child
== m_winDefault
)
439 if ( child
== m_winTmpDefault
)
440 m_winTmpDefault
= NULL
;
442 wxWindow::RemoveChild(child
);