]> git.saurik.com Git - wxWidgets.git/blob - src/common/toplvcmn.cpp
Source cleaning.
[wxWidgets.git] / src / common / toplvcmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/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 #include "wx/toplevel.h"
27
28 #ifndef WX_PRECOMP
29 #include "wx/dcclient.h"
30 #include "wx/app.h"
31 #endif // WX_PRECOMP
32
33 #include "wx/display.h"
34
35 // ----------------------------------------------------------------------------
36 // event table
37 // ----------------------------------------------------------------------------
38
39 BEGIN_EVENT_TABLE(wxTopLevelWindowBase, wxWindow)
40 EVT_CLOSE(wxTopLevelWindowBase::OnCloseWindow)
41 EVT_SIZE(wxTopLevelWindowBase::OnSize)
42 END_EVENT_TABLE()
43
44 // ============================================================================
45 // implementation
46 // ============================================================================
47
48 IMPLEMENT_ABSTRACT_CLASS(wxTopLevelWindow, wxWindow)
49
50 // ----------------------------------------------------------------------------
51 // construction/destruction
52 // ----------------------------------------------------------------------------
53
54 wxTopLevelWindowBase::wxTopLevelWindowBase()
55 {
56 // Unlike windows, top level windows are created hidden by default.
57 m_isShown = false;
58 }
59
60 wxTopLevelWindowBase::~wxTopLevelWindowBase()
61 {
62 // don't let wxTheApp keep any stale pointers to us
63 if ( wxTheApp && wxTheApp->GetTopWindow() == this )
64 wxTheApp->SetTopWindow(NULL);
65
66 wxTopLevelWindows.DeleteObject(this);
67
68 if ( IsLastBeforeExit() )
69 {
70 // no other (important) windows left, quit the app
71 wxTheApp->ExitMainLoop();
72 }
73 }
74
75 bool wxTopLevelWindowBase::Destroy()
76 {
77 // delayed destruction: the frame will be deleted during the next idle
78 // loop iteration
79 if ( !wxPendingDelete.Member(this) )
80 wxPendingDelete.Append(this);
81
82 if (wxTopLevelWindows.GetCount() > 1)
83 {
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.
90 Hide();
91 }
92
93 return true;
94 }
95
96 bool wxTopLevelWindowBase::IsLastBeforeExit() const
97 {
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() )
101 return false;
102
103 wxWindowList::const_iterator i;
104 const wxWindowList::const_iterator end = wxTopLevelWindows.end();
105
106 // then decide whether we should exit at all
107 for ( i = wxTopLevelWindows.begin(); i != end; ++i )
108 {
109 wxTopLevelWindow * const win = wx_static_cast(wxTopLevelWindow *, *i);
110 if ( win->ShouldPreventAppExit() )
111 {
112 // there remains at least one important TLW, don't exit
113 return false;
114 }
115 }
116
117 // if yes, close all the other windows: this could still fail
118 for ( i = wxTopLevelWindows.begin(); i != end; ++i )
119 {
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() )
123 {
124 // one of the windows refused to close, don't exit
125 //
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
130 return false;
131 }
132 }
133
134 return true;
135 }
136
137 // ----------------------------------------------------------------------------
138 // wxTopLevelWindow geometry
139 // ----------------------------------------------------------------------------
140
141 void wxTopLevelWindowBase::GetRectForTopLevelChildren(int *x, int *y, int *w, int *h)
142 {
143 GetPosition(x,y);
144 GetSize(w,h);
145 }
146
147 wxSize wxTopLevelWindowBase::GetMaxSize() const
148 {
149 wxSize size( GetMaxWidth(), GetMaxHeight() );
150 int w, h;
151
152 wxClientDisplayRect( 0, 0, &w, &h );
153
154 if( size.GetWidth() == wxDefaultCoord )
155 size.SetWidth( w );
156
157 if( size.GetHeight() == wxDefaultCoord )
158 size.SetHeight( h );
159
160 return size;
161 }
162
163 /* static */
164 wxSize wxTopLevelWindowBase::GetDefaultSize()
165 {
166 wxSize size = wxGetClientDisplayRect().GetSize();
167
168 // create proportionally bigger windows on small screens
169 if ( size.x >= 1024 )
170 size.x = 400;
171 else if ( size.x >= 800 )
172 size.x = 300;
173 else if ( size.x >= 320 )
174 size.x = 240;
175
176 if ( size.y >= 768 )
177 size.y = 250;
178 else if ( size.y > 200 )
179 {
180 size.y *= 2;
181 size.y /= 3;
182 }
183
184 return size;
185 }
186
187 void wxTopLevelWindowBase::DoCentre(int dir)
188 {
189 // on some platforms centering top level windows is impossible
190 // because they are always maximized by guidelines or limitations
191 if(IsAlwaysMaximized())
192 return;
193
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());
198
199 // what should we centre this window on?
200 wxRect rectParent;
201 if ( !(dir & wxCENTRE_ON_SCREEN) && GetParent() )
202 {
203 // centre on parent window: notice that we need screen coordinates for
204 // positioning this TLW
205 rectParent = GetParent()->GetScreenRect();
206
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()) )
213 {
214 // this is enough to make IsEmpty() test below pass
215 rectParent.width = 0;
216 }
217 }
218
219 if ( rectParent.IsEmpty() )
220 {
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;
224 }
225
226 // centering maximized window on screen is no-op
227 if((rectParent == rectDisplay) && IsMaximized())
228 return;
229
230 // the new window rect candidate
231 wxRect rect = GetRect().CentreIn(rectParent, dir);
232
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()) )
237 {
238 if ( !rectDisplay.Inside(rect.GetBottomRight()) )
239 {
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();
244 rect.Offset(dx, dy);
245 }
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
250 }
251 else // make top left corner visible
252 {
253 if ( rect.x < rectDisplay.x )
254 rect.x = rectDisplay.x;
255
256 if ( rect.y < rectDisplay.y )
257 rect.y = rectDisplay.y;
258 }
259
260 // -1 could be valid coordinate here if there are several displays
261 SetSize(rect, wxSIZE_ALLOW_MINUS_ONE);
262 }
263
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 // ----------------------------------------------------------------------------
269
270 void wxTopLevelWindowBase::DoScreenToClient(int *x, int *y) const
271 {
272 wxWindow::DoScreenToClient(x, y);
273
274 // translate the wxWindow client coords to our client coords
275 wxPoint pt(GetClientAreaOrigin());
276 if ( x )
277 *x -= pt.x;
278 if ( y )
279 *y -= pt.y;
280 }
281
282 void wxTopLevelWindowBase::DoClientToScreen(int *x, int *y) const
283 {
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());
287 if ( x )
288 *x += pt.x;
289 if ( y )
290 *y += pt.y;
291
292 wxWindow::DoClientToScreen(x, y);
293 }
294
295 bool wxTopLevelWindowBase::IsAlwaysMaximized() const
296 {
297 #if defined(__SMARTPHONE__) || defined(__POCKETPC__)
298 return true;
299 #else
300 return false;
301 #endif
302 }
303
304 // ----------------------------------------------------------------------------
305 // event handlers
306 // ----------------------------------------------------------------------------
307
308 // default resizing behaviour - if only ONE subwindow, resize to fill the
309 // whole client area
310 void wxTopLevelWindowBase::DoLayout()
311 {
312 // if we're using constraints or sizers - do use them
313 if ( GetAutoLayout() )
314 {
315 Layout();
316 }
317 else
318 {
319 // do we have _exactly_ one child?
320 wxWindow *child = (wxWindow *)NULL;
321 for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
322 node;
323 node = node->GetNext() )
324 {
325 wxWindow *win = node->GetData();
326
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) )
331 {
332 if ( child )
333 {
334 return; // it's our second subwindow - nothing to do
335 }
336
337 child = win;
338 }
339 }
340
341 // do we have any children at all?
342 if ( child && child->IsShown() )
343 {
344 // exactly one child - set it's size to fill the whole frame
345 int clientW, clientH;
346 DoGetClientSize(&clientW, &clientH);
347
348 // for whatever reasons, wxGTK wants to have a small offset - it
349 // probably looks better with it?
350 #ifdef __WXGTK__
351 static const int ofs = 1;
352 #else
353 static const int ofs = 0;
354 #endif
355
356 child->SetSize(ofs, ofs, clientW - 2*ofs, clientH - 2*ofs);
357 }
358 }
359 }
360
361 // The default implementation for the close window event.
362 void wxTopLevelWindowBase::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
363 {
364 Destroy();
365 }
366
367 bool wxTopLevelWindowBase::SendIconizeEvent(bool iconized)
368 {
369 wxIconizeEvent event(GetId(), iconized);
370 event.SetEventObject(this);
371
372 return GetEventHandler()->ProcessEvent(event);
373 }
374
375 // do the window-specific processing after processing the update event
376 void wxTopLevelWindowBase::DoUpdateWindowUI(wxUpdateUIEvent& event)
377 {
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);
382
383 // update title
384 if ( event.GetSetText() )
385 {
386 if ( event.GetText() != GetTitle() )
387 SetTitle(event.GetText());
388 }
389 }
390
391 void wxTopLevelWindowBase::RequestUserAttention(int WXUNUSED(flags))
392 {
393 // it's probably better than do nothing, isn't it?
394 Raise();
395 }