1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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"
27 #include "wx/toplevel.h"
28 #include "wx/dcclient.h"
32 #include "wx/display.h"
34 // ----------------------------------------------------------------------------
36 // ----------------------------------------------------------------------------
38 BEGIN_EVENT_TABLE(wxTopLevelWindowBase
, wxWindow
)
39 EVT_CLOSE(wxTopLevelWindowBase::OnCloseWindow
)
40 EVT_SIZE(wxTopLevelWindowBase::OnSize
)
43 // ============================================================================
45 // ============================================================================
47 IMPLEMENT_ABSTRACT_CLASS(wxTopLevelWindow
, wxWindow
)
49 // ----------------------------------------------------------------------------
50 // construction/destruction
51 // ----------------------------------------------------------------------------
53 wxTopLevelWindowBase::wxTopLevelWindowBase()
55 // Unlike windows, top level windows are created hidden by default.
59 wxTopLevelWindowBase::~wxTopLevelWindowBase()
61 // don't let wxTheApp keep any stale pointers to us
62 if ( wxTheApp
&& wxTheApp
->GetTopWindow() == this )
63 wxTheApp
->SetTopWindow(NULL
);
65 bool shouldExit
= IsLastBeforeExit();
67 wxTopLevelWindows
.DeleteObject(this);
72 wxTheApp
->ExitMainLoop();
76 bool wxTopLevelWindowBase::Destroy()
78 // delayed destruction: the frame will be deleted during the next idle
80 if ( !wxPendingDelete
.Member(this) )
81 wxPendingDelete
.Append(this);
83 if (wxTopLevelWindows
.GetCount() > 1)
85 // Hide it immediately. This should
86 // not be done if this TLW is the
87 // only one left since we then would
88 // risk not to get any idle events
89 // at all anymore during which we
90 // could delete any pending events.
97 bool wxTopLevelWindowBase::IsLastBeforeExit() const
99 // we exit the application if there are no more top level windows left
100 // normally but wxApp can prevent this from happening
101 return wxTopLevelWindows
.GetCount() == 1 &&
102 wxTopLevelWindows
.GetFirst()->GetData() == (wxWindow
*)this &&
103 wxTheApp
&& wxTheApp
->GetExitOnFrameDelete();
106 // ----------------------------------------------------------------------------
107 // wxTopLevelWindow geometry
108 // ----------------------------------------------------------------------------
110 void wxTopLevelWindowBase::GetRectForTopLevelChildren(int *x
, int *y
, int *w
, int *h
)
116 wxSize
wxTopLevelWindowBase::GetMaxSize() const
118 wxSize
size( GetMaxWidth(), GetMaxHeight() );
121 wxClientDisplayRect( 0, 0, &w
, &h
);
123 if( size
.GetWidth() == wxDefaultCoord
)
126 if( size
.GetHeight() == wxDefaultCoord
)
133 wxSize
wxTopLevelWindowBase::GetDefaultSize()
135 wxSize size
= wxGetClientDisplayRect().GetSize();
137 // create proportionally bigger windows on small screens
138 if ( size
.x
>= 1024 )
140 else if ( size
.x
>= 800 )
142 else if ( size
.x
>= 320 )
147 else if ( size
.y
> 200 )
156 void wxTopLevelWindowBase::DoCentre(int dir
)
158 // we need the display rect anyhow so store it first
159 int nDisplay
= wxDisplay::GetFromWindow(this);
160 wxDisplay
dpy(nDisplay
== wxNOT_FOUND
? 0 : nDisplay
);
161 const wxRect
rectDisplay(dpy
.GetClientArea());
163 // what should we centre this window on?
165 if ( !(dir
& wxCENTRE_ON_SCREEN
) && GetParent() )
167 // centre on parent window: notice that we need screen coordinates for
168 // positioning this TLW
169 rectParent
= GetParent()->GetScreenRect();
171 // if the parent is entirely off screen (happens at least with MDI
172 // parent frame under Mac but could happen elsewhere too if the frame
173 // was hidden/moved away for some reason), don't use it as otherwise
174 // this window wouldn't be visible at all
175 if ( !rectDisplay
.Inside(rectParent
.GetTopLeft()) &&
176 !rectParent
.Inside(rectParent
.GetBottomRight()) )
178 // this is enough to make IsEmpty() test below pass
179 rectParent
.width
= 0;
183 if ( rectParent
.IsEmpty() )
185 // we were explicitely asked to centre this window on the entire screen
186 // or if we have no parent anyhow and so can't centre on it
187 rectParent
= rectDisplay
;
190 // the new window rect candidate
191 wxRect rect
= GetRect().CentreIn(rectParent
, dir
);
193 // we don't want to place the window off screen if Centre() is called as
194 // this is (almost?) never wanted and it would be very difficult to prevent
195 // it from happening from the user code if we didn't check for it here
196 if ( rectDisplay
.Inside(rect
.GetTopLeft()) )
198 if ( !rectDisplay
.Inside(rect
.GetBottomRight()) )
200 // check if we can move the window so that the bottom right corner
201 // is visible without hiding the top left one
202 int dx
= rectDisplay
.GetRight() - rect
.GetRight();
203 int dy
= rectDisplay
.GetBottom() - rect
.GetBottom();
206 //else: the window top left and bottom right corner are both visible,
207 // although the window might still be not entirely on screen (with
208 // 2 staggered displays for example) we wouldn't be able to
209 // improve the layout much in such case, so just leave it as is
211 else // make top left corner visible
213 if ( rect
.x
< rectDisplay
.x
)
214 rect
.x
= rectDisplay
.x
;
216 if ( rect
.y
< rectDisplay
.y
)
217 rect
.y
= rectDisplay
.y
;
220 // -1 could be valid coordinate here if there are several displays
221 SetSize(rect
, wxSIZE_ALLOW_MINUS_ONE
);
224 // ----------------------------------------------------------------------------
225 // wxTopLevelWindow size management: we exclude the areas taken by
226 // menu/status/toolbars from the client area, so the client area is what's
227 // really available for the frame contents
228 // ----------------------------------------------------------------------------
230 void wxTopLevelWindowBase::DoScreenToClient(int *x
, int *y
) const
232 wxWindow::DoScreenToClient(x
, y
);
234 // translate the wxWindow client coords to our client coords
235 wxPoint
pt(GetClientAreaOrigin());
242 void wxTopLevelWindowBase::DoClientToScreen(int *x
, int *y
) const
244 // our client area origin (0, 0) may be really something like (0, 30) for
245 // wxWindow if we have a toolbar, account for it before translating
246 wxPoint
pt(GetClientAreaOrigin());
252 wxWindow::DoClientToScreen(x
, y
);
256 // ----------------------------------------------------------------------------
258 // ----------------------------------------------------------------------------
260 // default resizing behaviour - if only ONE subwindow, resize to fill the
262 void wxTopLevelWindowBase::DoLayout()
264 // if we're using constraints or sizers - do use them
265 if ( GetAutoLayout() )
271 // do we have _exactly_ one child?
272 wxWindow
*child
= (wxWindow
*)NULL
;
273 for ( wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
275 node
= node
->GetNext() )
277 wxWindow
*win
= node
->GetData();
279 // exclude top level and managed windows (status bar isn't
280 // currently in the children list except under wxMac anyhow, but
281 // it makes no harm to test for it)
282 if ( !win
->IsTopLevel() && !IsOneOfBars(win
) )
286 return; // it's our second subwindow - nothing to do
293 // do we have any children at all?
296 // exactly one child - set it's size to fill the whole frame
297 int clientW
, clientH
;
298 DoGetClientSize(&clientW
, &clientH
);
300 // for whatever reasons, wxGTK wants to have a small offset - it
301 // probably looks better with it?
303 static const int ofs
= 1;
305 static const int ofs
= 0;
308 child
->SetSize(ofs
, ofs
, clientW
- 2*ofs
, clientH
- 2*ofs
);
313 // The default implementation for the close window event.
314 void wxTopLevelWindowBase::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
319 bool wxTopLevelWindowBase::SendIconizeEvent(bool iconized
)
321 wxIconizeEvent
event(GetId(), iconized
);
322 event
.SetEventObject(this);
324 return GetEventHandler()->ProcessEvent(event
);
327 // do the window-specific processing after processing the update event
328 void wxTopLevelWindowBase::DoUpdateWindowUI(wxUpdateUIEvent
& event
)
330 // call inherited, but skip the wxControl's version, and call directly the
331 // wxWindow's one instead, because the only reason why we are overriding this
332 // function is that we want to use SetTitle() instead of wxControl::SetLabel()
333 wxWindowBase::DoUpdateWindowUI(event
);
336 if ( event
.GetSetText() )
338 if ( event
.GetText() != GetTitle() )
339 SetTitle(event
.GetText());
343 void wxTopLevelWindowBase::RequestUserAttention(int WXUNUSED(flags
))
345 // it's probably better than do nothing, isn't it?