]> git.saurik.com Git - wxWidgets.git/blob - src/common/toplvcmn.cpp
fixed centering of a TLW on a non-TLW parent
[wxWidgets.git] / src / common / toplvcmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: common/toplvcmn.cpp
3 // Purpose: common (for all platforms) wxTopLevelWindow functions
4 // Author: Julian Smart, Vadim Zeitlin
5 // Created: 01/02/97
6 // Id: $Id$
7 // Copyright: (c) 1998 Robert Roebling and Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #ifndef WX_PRECOMP
27 #include "wx/toplevel.h"
28 #include "wx/dcclient.h"
29 #include "wx/app.h"
30 #endif // WX_PRECOMP
31
32 #include "wx/display.h"
33
34 // ----------------------------------------------------------------------------
35 // event table
36 // ----------------------------------------------------------------------------
37
38 BEGIN_EVENT_TABLE(wxTopLevelWindowBase, wxWindow)
39 EVT_CLOSE(wxTopLevelWindowBase::OnCloseWindow)
40 EVT_SIZE(wxTopLevelWindowBase::OnSize)
41 END_EVENT_TABLE()
42
43 // ============================================================================
44 // implementation
45 // ============================================================================
46
47 IMPLEMENT_ABSTRACT_CLASS(wxTopLevelWindow, wxWindow)
48
49 // ----------------------------------------------------------------------------
50 // construction/destruction
51 // ----------------------------------------------------------------------------
52
53 wxTopLevelWindowBase::wxTopLevelWindowBase()
54 {
55 // Unlike windows, top level windows are created hidden by default.
56 m_isShown = false;
57 }
58
59 wxTopLevelWindowBase::~wxTopLevelWindowBase()
60 {
61 // don't let wxTheApp keep any stale pointers to us
62 if ( wxTheApp && wxTheApp->GetTopWindow() == this )
63 wxTheApp->SetTopWindow(NULL);
64
65 bool shouldExit = IsLastBeforeExit();
66
67 wxTopLevelWindows.DeleteObject(this);
68
69 if ( shouldExit )
70 {
71 // then do it
72 wxTheApp->ExitMainLoop();
73 }
74 }
75
76 bool wxTopLevelWindowBase::Destroy()
77 {
78 // delayed destruction: the frame will be deleted during the next idle
79 // loop iteration
80 if ( !wxPendingDelete.Member(this) )
81 wxPendingDelete.Append(this);
82
83 if (wxTopLevelWindows.GetCount() > 1)
84 {
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.
91 Hide();
92 }
93
94 return true;
95 }
96
97 bool wxTopLevelWindowBase::IsLastBeforeExit() const
98 {
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();
104 }
105
106 // ----------------------------------------------------------------------------
107 // wxTopLevelWindow geometry
108 // ----------------------------------------------------------------------------
109
110 void wxTopLevelWindowBase::GetRectForTopLevelChildren(int *x, int *y, int *w, int *h)
111 {
112 GetPosition(x,y);
113 GetSize(w,h);
114 }
115
116 wxSize wxTopLevelWindowBase::GetMaxSize() const
117 {
118 wxSize size( GetMaxWidth(), GetMaxHeight() );
119 int w, h;
120
121 wxClientDisplayRect( 0, 0, &w, &h );
122
123 if( size.GetWidth() == wxDefaultCoord )
124 size.SetWidth( w );
125
126 if( size.GetHeight() == wxDefaultCoord )
127 size.SetHeight( h );
128
129 return size;
130 }
131
132 /* static */
133 wxSize wxTopLevelWindowBase::GetDefaultSize()
134 {
135 wxSize size = wxGetClientDisplayRect().GetSize();
136
137 // create proportionally bigger windows on small screens
138 if ( size.x >= 1024 )
139 size.x = 400;
140 else if ( size.x >= 800 )
141 size.x = 300;
142 else if ( size.x >= 320 )
143 size.x = 240;
144
145 if ( size.y >= 768 )
146 size.y = 250;
147 else if ( size.y > 200 )
148 {
149 size.y *= 2;
150 size.y /= 3;
151 }
152
153 return size;
154 }
155
156 void wxTopLevelWindowBase::DoCentre(int dir)
157 {
158 wxRect rectCentre;
159 if ( !(dir & wxCENTRE_ON_SCREEN) && GetParent() )
160 {
161 // centre on parent window: notice that we need screen coordinates for
162 // positioning this TLW
163 rectCentre = GetParent()->GetScreenRect();
164 }
165 else
166 {
167 // we were explicitely asked to centre this window on the entire screen
168 // or if we have no parent anyhow and so can't centre on it
169 #if wxUSE_DISPLAY
170 const int nDisplay = wxDisplay::GetFromWindow(this);
171 if ( nDisplay != wxNOT_FOUND )
172 {
173 rectCentre = wxDisplay(nDisplay).GetGeometry();
174 }
175 else
176 #endif // wxUSE_DISPLAY
177 {
178 wxDisplaySize(&rectCentre.width, &rectCentre.height);
179 }
180 }
181
182 // window may be at -1 if it's centered on a secondary display, for example
183 SetSize(GetRect().CentreIn(rectCentre, dir), wxSIZE_ALLOW_MINUS_ONE);
184 }
185
186 // ----------------------------------------------------------------------------
187 // wxTopLevelWindow size management: we exclude the areas taken by
188 // menu/status/toolbars from the client area, so the client area is what's
189 // really available for the frame contents
190 // ----------------------------------------------------------------------------
191
192 void wxTopLevelWindowBase::DoScreenToClient(int *x, int *y) const
193 {
194 wxWindow::DoScreenToClient(x, y);
195
196 // translate the wxWindow client coords to our client coords
197 wxPoint pt(GetClientAreaOrigin());
198 if ( x )
199 *x -= pt.x;
200 if ( y )
201 *y -= pt.y;
202 }
203
204 void wxTopLevelWindowBase::DoClientToScreen(int *x, int *y) const
205 {
206 // our client area origin (0, 0) may be really something like (0, 30) for
207 // wxWindow if we have a toolbar, account for it before translating
208 wxPoint pt(GetClientAreaOrigin());
209 if ( x )
210 *x += pt.x;
211 if ( y )
212 *y += pt.y;
213
214 wxWindow::DoClientToScreen(x, y);
215 }
216
217
218 // ----------------------------------------------------------------------------
219 // event handlers
220 // ----------------------------------------------------------------------------
221
222 // default resizing behaviour - if only ONE subwindow, resize to fill the
223 // whole client area
224 void wxTopLevelWindowBase::DoLayout()
225 {
226 // if we're using constraints or sizers - do use them
227 if ( GetAutoLayout() )
228 {
229 Layout();
230 }
231 else
232 {
233 // do we have _exactly_ one child?
234 wxWindow *child = (wxWindow *)NULL;
235 for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
236 node;
237 node = node->GetNext() )
238 {
239 wxWindow *win = node->GetData();
240
241 // exclude top level and managed windows (status bar isn't
242 // currently in the children list except under wxMac anyhow, but
243 // it makes no harm to test for it)
244 if ( !win->IsTopLevel() && !IsOneOfBars(win) )
245 {
246 if ( child )
247 {
248 return; // it's our second subwindow - nothing to do
249 }
250
251 child = win;
252 }
253 }
254
255 // do we have any children at all?
256 if ( child )
257 {
258 // exactly one child - set it's size to fill the whole frame
259 int clientW, clientH;
260 DoGetClientSize(&clientW, &clientH);
261
262 // for whatever reasons, wxGTK wants to have a small offset - it
263 // probably looks better with it?
264 #ifdef __WXGTK__
265 static const int ofs = 1;
266 #else
267 static const int ofs = 0;
268 #endif
269
270 child->SetSize(ofs, ofs, clientW - 2*ofs, clientH - 2*ofs);
271 }
272 }
273 }
274
275 // The default implementation for the close window event.
276 void wxTopLevelWindowBase::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
277 {
278 Destroy();
279 }
280
281 bool wxTopLevelWindowBase::SendIconizeEvent(bool iconized)
282 {
283 wxIconizeEvent event(GetId(), iconized);
284 event.SetEventObject(this);
285
286 return GetEventHandler()->ProcessEvent(event);
287 }
288
289 // do the window-specific processing after processing the update event
290 void wxTopLevelWindowBase::DoUpdateWindowUI(wxUpdateUIEvent& event)
291 {
292 // call inherited, but skip the wxControl's version, and call directly the
293 // wxWindow's one instead, because the only reason why we are overriding this
294 // function is that we want to use SetTitle() instead of wxControl::SetLabel()
295 wxWindowBase::DoUpdateWindowUI(event);
296
297 // update title
298 if ( event.GetSetText() )
299 {
300 if ( event.GetText() != GetTitle() )
301 SetTitle(event.GetText());
302 }
303 }
304
305 void wxTopLevelWindowBase::RequestUserAttention(int WXUNUSED(flags))
306 {
307 // it's probably better than do nothing, isn't it?
308 Raise();
309 }
310