]> git.saurik.com Git - wxWidgets.git/blob - src/common/toplvcmn.cpp
Upgrade bundled zlib to 1.2.8.
[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 // Copyright: (c) 1998 Robert Roebling and Julian Smart
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // ============================================================================
11 // declarations
12 // ============================================================================
13
14 // ----------------------------------------------------------------------------
15 // headers
16 // ----------------------------------------------------------------------------
17
18 // For compilers that support precompilation, includes "wx.h".
19 #include "wx/wxprec.h"
20
21 #ifdef __BORLANDC__
22 #pragma hdrstop
23 #endif
24
25 #include "wx/toplevel.h"
26
27 #ifndef WX_PRECOMP
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 wxTopLevelWindows.DeleteObject(this);
66
67 // delete any our top level children which are still pending for deletion
68 // immediately: this could happen if a child (e.g. a temporary dialog
69 // created with this window as parent) was Destroy()'d) while this window
70 // was deleted directly (with delete, or maybe just because it was created
71 // on the stack) immediately afterwards and before the child TLW was really
72 // destroyed -- not destroying it now would leave it alive with a dangling
73 // parent pointer and result in a crash later
74 for ( wxObjectList::iterator i = wxPendingDelete.begin();
75 i != wxPendingDelete.end();
76 )
77 {
78 wxWindow * const win = wxDynamicCast(*i, wxWindow);
79 if ( win && win->GetParent() == this )
80 {
81 wxPendingDelete.erase(i);
82
83 delete win;
84
85 // deleting it invalidated the list (and not only one node because
86 // it could have resulted in deletion of other objects to)
87 i = wxPendingDelete.begin();
88 }
89 else
90 {
91 ++i;
92 }
93 }
94
95 if ( IsLastBeforeExit() )
96 {
97 // no other (important) windows left, quit the app
98 wxTheApp->ExitMainLoop();
99 }
100 }
101
102 bool wxTopLevelWindowBase::Destroy()
103 {
104 // delayed destruction: the frame will be deleted during the next idle
105 // loop iteration
106 if ( !wxPendingDelete.Member(this) )
107 wxPendingDelete.Append(this);
108
109 // normally we want to hide the window immediately so that it doesn't get
110 // stuck on the screen while it's being destroyed, however we shouldn't
111 // hide the last visible window as then we might not get any idle events
112 // any more as no events will be sent to the hidden window and without idle
113 // events we won't prune wxPendingDelete list and the application won't
114 // terminate
115 for ( wxWindowList::const_iterator i = wxTopLevelWindows.begin(),
116 end = wxTopLevelWindows.end();
117 i != end;
118 ++i )
119 {
120 wxTopLevelWindow * const win = static_cast<wxTopLevelWindow *>(*i);
121 if ( win != this && win->IsShown() )
122 {
123 // there remains at least one other visible TLW, we can hide this
124 // one
125 Hide();
126
127 break;
128 }
129 }
130
131 return true;
132 }
133
134 bool wxTopLevelWindowBase::IsLastBeforeExit() const
135 {
136 // first of all, automatically exiting the app on last window close can be
137 // completely disabled at wxTheApp level
138 if ( !wxTheApp || !wxTheApp->GetExitOnFrameDelete() )
139 return false;
140
141 wxWindowList::const_iterator i;
142 const wxWindowList::const_iterator end = wxTopLevelWindows.end();
143
144 // then decide whether we should exit at all
145 for ( i = wxTopLevelWindows.begin(); i != end; ++i )
146 {
147 wxTopLevelWindow * const win = static_cast<wxTopLevelWindow *>(*i);
148 if ( win->ShouldPreventAppExit() )
149 {
150 // there remains at least one important TLW, don't exit
151 return false;
152 }
153 }
154
155 // if yes, close all the other windows: this could still fail
156 for ( i = wxTopLevelWindows.begin(); i != end; ++i )
157 {
158 // don't close twice the windows which are already marked for deletion
159 wxTopLevelWindow * const win = static_cast<wxTopLevelWindow *>(*i);
160 if ( !wxPendingDelete.Member(win) && !win->Close() )
161 {
162 // one of the windows refused to close, don't exit
163 //
164 // NB: of course, by now some other windows could have been already
165 // closed but there is really nothing we can do about it as we
166 // have no way just to ask the window if it can close without
167 // forcing it to do it
168 return false;
169 }
170 }
171
172 return true;
173 }
174
175 // ----------------------------------------------------------------------------
176 // wxTopLevelWindow geometry
177 // ----------------------------------------------------------------------------
178
179 void wxTopLevelWindowBase::SetMinSize(const wxSize& minSize)
180 {
181 SetSizeHints(minSize, GetMaxSize());
182 }
183
184 void wxTopLevelWindowBase::SetMaxSize(const wxSize& maxSize)
185 {
186 SetSizeHints(GetMinSize(), maxSize);
187 }
188
189 void wxTopLevelWindowBase::GetRectForTopLevelChildren(int *x, int *y, int *w, int *h)
190 {
191 GetPosition(x,y);
192 GetSize(w,h);
193 }
194
195 /* static */
196 wxSize wxTopLevelWindowBase::GetDefaultSize()
197 {
198 wxSize size = wxGetClientDisplayRect().GetSize();
199 #ifndef __WXOSX_IPHONE__
200 // create proportionally bigger windows on small screens
201 if ( size.x >= 1024 )
202 size.x = 400;
203 else if ( size.x >= 800 )
204 size.x = 300;
205 else if ( size.x >= 320 )
206 size.x = 240;
207
208 if ( size.y >= 768 )
209 size.y = 250;
210 else if ( size.y > 200 )
211 {
212 size.y *= 2;
213 size.y /= 3;
214 }
215 #endif
216 return size;
217 }
218
219 void wxTopLevelWindowBase::DoCentre(int dir)
220 {
221 // on some platforms centering top level windows is impossible
222 // because they are always maximized by guidelines or limitations
223 //
224 // and centering a maximized window doesn't make sense as its position
225 // can't change
226 if ( IsAlwaysMaximized() || IsMaximized() )
227 return;
228
229 // we need the display rect anyhow so store it first: notice that we should
230 // be centered on the same display as our parent window, the display of
231 // this window itself is not really defined yet
232 int nDisplay = wxDisplay::GetFromWindow(GetParent() ? GetParent() : this);
233 wxDisplay dpy(nDisplay == wxNOT_FOUND ? 0 : nDisplay);
234 const wxRect rectDisplay(dpy.GetClientArea());
235
236 // what should we centre this window on?
237 wxRect rectParent;
238 if ( !(dir & wxCENTRE_ON_SCREEN) && GetParent() )
239 {
240 // centre on parent window: notice that we need screen coordinates for
241 // positioning this TLW
242 rectParent = GetParent()->GetScreenRect();
243
244 // if the parent is entirely off screen (happens at least with MDI
245 // parent frame under Mac but could happen elsewhere too if the frame
246 // was hidden/moved away for some reason), don't use it as otherwise
247 // this window wouldn't be visible at all
248 if ( !rectParent.Intersects(rectDisplay) )
249 {
250 // just centre on screen then
251 rectParent = rectDisplay;
252 }
253 }
254 else
255 {
256 // we were explicitly asked to centre this window on the entire screen
257 // or if we have no parent anyhow and so can't centre on it
258 rectParent = rectDisplay;
259 }
260
261 if ( !(dir & wxBOTH) )
262 dir |= wxBOTH; // if neither is specified, center in both directions
263
264 // the new window rect candidate
265 wxRect rect = GetRect().CentreIn(rectParent, dir & ~wxCENTRE_ON_SCREEN);
266
267 // we don't want to place the window off screen if Centre() is called as
268 // this is (almost?) never wanted and it would be very difficult to prevent
269 // it from happening from the user code if we didn't check for it here
270 if ( !rectDisplay.Contains(rect.GetTopLeft()) )
271 {
272 // move the window just enough to make the corner visible
273 int dx = rectDisplay.GetLeft() - rect.GetLeft();
274 int dy = rectDisplay.GetTop() - rect.GetTop();
275 rect.Offset(dx > 0 ? dx : 0, dy > 0 ? dy : 0);
276 }
277
278 if ( !rectDisplay.Contains(rect.GetBottomRight()) )
279 {
280 // do the same for this corner too
281 int dx = rectDisplay.GetRight() - rect.GetRight();
282 int dy = rectDisplay.GetBottom() - rect.GetBottom();
283 rect.Offset(dx < 0 ? dx : 0, dy < 0 ? dy : 0);
284 }
285
286 // the window top left and bottom right corner are both visible now and
287 // although the window might still be not entirely on screen (with 2
288 // staggered displays for example) we wouldn't be able to improve the
289 // layout much in such case, so we stop here
290
291 // -1 could be valid coordinate here if there are several displays
292 SetSize(rect, wxSIZE_ALLOW_MINUS_ONE);
293 }
294
295 // ----------------------------------------------------------------------------
296 // wxTopLevelWindow size management: we exclude the areas taken by
297 // menu/status/toolbars from the client area, so the client area is what's
298 // really available for the frame contents
299 // ----------------------------------------------------------------------------
300
301 void wxTopLevelWindowBase::DoScreenToClient(int *x, int *y) const
302 {
303 wxWindow::DoScreenToClient(x, y);
304
305 // translate the wxWindow client coords to our client coords
306 wxPoint pt(GetClientAreaOrigin());
307 if ( x )
308 *x -= pt.x;
309 if ( y )
310 *y -= pt.y;
311 }
312
313 void wxTopLevelWindowBase::DoClientToScreen(int *x, int *y) const
314 {
315 // our client area origin (0, 0) may be really something like (0, 30) for
316 // wxWindow if we have a toolbar, account for it before translating
317 wxPoint pt(GetClientAreaOrigin());
318 if ( x )
319 *x += pt.x;
320 if ( y )
321 *y += pt.y;
322
323 wxWindow::DoClientToScreen(x, y);
324 }
325
326 bool wxTopLevelWindowBase::IsAlwaysMaximized() const
327 {
328 #if defined(__SMARTPHONE__) || defined(__POCKETPC__)
329 return true;
330 #else
331 return false;
332 #endif
333 }
334
335 // ----------------------------------------------------------------------------
336 // icons
337 // ----------------------------------------------------------------------------
338
339 wxIcon wxTopLevelWindowBase::GetIcon() const
340 {
341 return m_icons.IsEmpty() ? wxIcon() : m_icons.GetIcon( -1 );
342 }
343
344 void wxTopLevelWindowBase::SetIcon(const wxIcon& icon)
345 {
346 // passing wxNullIcon to SetIcon() is possible (it means that we shouldn't
347 // have any icon), but adding an invalid icon to wxIconBundle is not
348 wxIconBundle icons;
349 if ( icon.IsOk() )
350 icons.AddIcon(icon);
351
352 SetIcons(icons);
353 }
354
355 // ----------------------------------------------------------------------------
356 // event handlers
357 // ----------------------------------------------------------------------------
358
359 // default resizing behaviour - if only ONE subwindow, resize to fill the
360 // whole client area
361 void wxTopLevelWindowBase::DoLayout()
362 {
363 // We are called during the window destruction several times, e.g. as
364 // wxFrame tries to adjust to its tool/status bars disappearing. But
365 // actually doing the layout is pretty useless in this case as the window
366 // will disappear anyhow -- so just don't bother.
367 if ( IsBeingDeleted() )
368 return;
369
370
371 // if we're using constraints or sizers - do use them
372 if ( GetAutoLayout() )
373 {
374 Layout();
375 }
376 else
377 {
378 // do we have _exactly_ one child?
379 wxWindow *child = NULL;
380 for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
381 node;
382 node = node->GetNext() )
383 {
384 wxWindow *win = node->GetData();
385
386 // exclude top level and managed windows (status bar isn't
387 // currently in the children list except under wxMac anyhow, but
388 // it makes no harm to test for it)
389 if ( !win->IsTopLevel() && !IsOneOfBars(win) )
390 {
391 if ( child )
392 {
393 return; // it's our second subwindow - nothing to do
394 }
395
396 child = win;
397 }
398 }
399
400 // do we have any children at all?
401 if ( child && child->IsShown() )
402 {
403 // exactly one child - set it's size to fill the whole frame
404 int clientW, clientH;
405 DoGetClientSize(&clientW, &clientH);
406
407 child->SetSize(0, 0, clientW, clientH);
408 }
409 }
410 }
411
412 // The default implementation for the close window event.
413 void wxTopLevelWindowBase::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
414 {
415 Destroy();
416 }
417
418 bool wxTopLevelWindowBase::SendIconizeEvent(bool iconized)
419 {
420 wxIconizeEvent event(GetId(), iconized);
421 event.SetEventObject(this);
422
423 return GetEventHandler()->ProcessEvent(event);
424 }
425
426 // do the window-specific processing after processing the update event
427 void wxTopLevelWindowBase::DoUpdateWindowUI(wxUpdateUIEvent& event)
428 {
429 // call inherited, but skip the wxControl's version, and call directly the
430 // wxWindow's one instead, because the only reason why we are overriding this
431 // function is that we want to use SetTitle() instead of wxControl::SetLabel()
432 wxWindowBase::DoUpdateWindowUI(event);
433
434 // update title
435 if ( event.GetSetText() )
436 {
437 if ( event.GetText() != GetTitle() )
438 SetTitle(event.GetText());
439 }
440 }
441
442 void wxTopLevelWindowBase::RequestUserAttention(int WXUNUSED(flags))
443 {
444 // it's probably better than do nothing, isn't it?
445 Raise();
446 }