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