]> git.saurik.com Git - wxWidgets.git/blob - src/common/toplvcmn.cpp
add description for GetFrameParameters() function; some other minor fixes (closes...
[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
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
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 if(IsAlwaysMaximized())
230 return;
231
232 // we need the display rect anyhow so store it first: notice that we should
233 // be centered on the same display as our parent window, the display of
234 // this window itself is not really defined yet
235 int nDisplay = wxDisplay::GetFromWindow(GetParent() ? GetParent() : this);
236 wxDisplay dpy(nDisplay == wxNOT_FOUND ? 0 : nDisplay);
237 const wxRect rectDisplay(dpy.GetClientArea());
238
239 // what should we centre this window on?
240 wxRect rectParent;
241 if ( !(dir & wxCENTRE_ON_SCREEN) && GetParent() )
242 {
243 // centre on parent window: notice that we need screen coordinates for
244 // positioning this TLW
245 rectParent = GetParent()->GetScreenRect();
246
247 // if the parent is entirely off screen (happens at least with MDI
248 // parent frame under Mac but could happen elsewhere too if the frame
249 // was hidden/moved away for some reason), don't use it as otherwise
250 // this window wouldn't be visible at all
251 if ( !rectDisplay.Contains(rectParent.GetTopLeft()) &&
252 !rectParent.Contains(rectParent.GetBottomRight()) )
253 {
254 // this is enough to make IsEmpty() test below pass
255 rectParent.width = 0;
256 }
257 }
258
259 if ( rectParent.IsEmpty() )
260 {
261 // we were explicitly asked to centre this window on the entire screen
262 // or if we have no parent anyhow and so can't centre on it
263 rectParent = rectDisplay;
264 }
265
266 // centering maximized window on screen is no-op
267 if((rectParent == rectDisplay) && IsMaximized())
268 return;
269
270 if ( !(dir & wxBOTH) )
271 dir |= wxBOTH; // if neither is specified, center in both directions
272
273 // the new window rect candidate
274 wxRect rect = GetRect().CentreIn(rectParent, dir & ~wxCENTRE_ON_SCREEN);
275
276 // we don't want to place the window off screen if Centre() is called as
277 // this is (almost?) never wanted and it would be very difficult to prevent
278 // it from happening from the user code if we didn't check for it here
279 if ( !rectDisplay.Contains(rect.GetTopLeft()) )
280 {
281 // move the window just enough to make the corner visible
282 int dx = rectDisplay.GetLeft() - rect.GetLeft();
283 int dy = rectDisplay.GetTop() - rect.GetTop();
284 rect.Offset(dx > 0 ? dx : 0, dy > 0 ? dy : 0);
285 }
286
287 if ( !rectDisplay.Contains(rect.GetBottomRight()) )
288 {
289 // do the same for this corner too
290 int dx = rectDisplay.GetRight() - rect.GetRight();
291 int dy = rectDisplay.GetBottom() - rect.GetBottom();
292 rect.Offset(dx < 0 ? dx : 0, dy < 0 ? dy : 0);
293 }
294
295 // the window top left and bottom right corner are both visible now and
296 // although the window might still be not entirely on screen (with 2
297 // staggered displays for example) we wouldn't be able to improve the
298 // layout much in such case, so we stop here
299
300 // -1 could be valid coordinate here if there are several displays
301 SetSize(rect, wxSIZE_ALLOW_MINUS_ONE);
302 }
303
304 // ----------------------------------------------------------------------------
305 // wxTopLevelWindow size management: we exclude the areas taken by
306 // menu/status/toolbars from the client area, so the client area is what's
307 // really available for the frame contents
308 // ----------------------------------------------------------------------------
309
310 void wxTopLevelWindowBase::DoScreenToClient(int *x, int *y) const
311 {
312 wxWindow::DoScreenToClient(x, y);
313
314 // translate the wxWindow client coords to our client coords
315 wxPoint pt(GetClientAreaOrigin());
316 if ( x )
317 *x -= pt.x;
318 if ( y )
319 *y -= pt.y;
320 }
321
322 void wxTopLevelWindowBase::DoClientToScreen(int *x, int *y) const
323 {
324 // our client area origin (0, 0) may be really something like (0, 30) for
325 // wxWindow if we have a toolbar, account for it before translating
326 wxPoint pt(GetClientAreaOrigin());
327 if ( x )
328 *x += pt.x;
329 if ( y )
330 *y += pt.y;
331
332 wxWindow::DoClientToScreen(x, y);
333 }
334
335 bool wxTopLevelWindowBase::IsAlwaysMaximized() const
336 {
337 #if defined(__SMARTPHONE__) || defined(__POCKETPC__)
338 return true;
339 #else
340 return false;
341 #endif
342 }
343
344 // ----------------------------------------------------------------------------
345 // icons
346 // ----------------------------------------------------------------------------
347
348 wxIcon wxTopLevelWindowBase::GetIcon() const
349 {
350 return m_icons.IsEmpty() ? wxIcon() : m_icons.GetIcon( -1 );
351 }
352
353 void wxTopLevelWindowBase::SetIcon(const wxIcon& icon)
354 {
355 // passing wxNullIcon to SetIcon() is possible (it means that we shouldn't
356 // have any icon), but adding an invalid icon to wxIconBundle is not
357 wxIconBundle icons;
358 if ( icon.Ok() )
359 icons.AddIcon(icon);
360
361 SetIcons(icons);
362 }
363
364 // ----------------------------------------------------------------------------
365 // event handlers
366 // ----------------------------------------------------------------------------
367
368 // default resizing behaviour - if only ONE subwindow, resize to fill the
369 // whole client area
370 void wxTopLevelWindowBase::DoLayout()
371 {
372 // if we're using constraints or sizers - do use them
373 if ( GetAutoLayout() )
374 {
375 Layout();
376 }
377 else
378 {
379 // do we have _exactly_ one child?
380 wxWindow *child = (wxWindow *)NULL;
381 for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
382 node;
383 node = node->GetNext() )
384 {
385 wxWindow *win = node->GetData();
386
387 // exclude top level and managed windows (status bar isn't
388 // currently in the children list except under wxMac anyhow, but
389 // it makes no harm to test for it)
390 if ( !win->IsTopLevel() && !IsOneOfBars(win) )
391 {
392 if ( child )
393 {
394 return; // it's our second subwindow - nothing to do
395 }
396
397 child = win;
398 }
399 }
400
401 // do we have any children at all?
402 if ( child && child->IsShown() )
403 {
404 // exactly one child - set it's size to fill the whole frame
405 int clientW, clientH;
406 DoGetClientSize(&clientW, &clientH);
407
408 child->SetSize(0, 0, clientW, clientH);
409 }
410 }
411 }
412
413 // The default implementation for the close window event.
414 void wxTopLevelWindowBase::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
415 {
416 Destroy();
417 }
418
419 bool wxTopLevelWindowBase::SendIconizeEvent(bool iconized)
420 {
421 wxIconizeEvent event(GetId(), iconized);
422 event.SetEventObject(this);
423
424 return GetEventHandler()->ProcessEvent(event);
425 }
426
427 // do the window-specific processing after processing the update event
428 void wxTopLevelWindowBase::DoUpdateWindowUI(wxUpdateUIEvent& event)
429 {
430 // call inherited, but skip the wxControl's version, and call directly the
431 // wxWindow's one instead, because the only reason why we are overriding this
432 // function is that we want to use SetTitle() instead of wxControl::SetLabel()
433 wxWindowBase::DoUpdateWindowUI(event);
434
435 // update title
436 if ( event.GetSetText() )
437 {
438 if ( event.GetText() != GetTitle() )
439 SetTitle(event.GetText());
440 }
441 }
442
443 void wxTopLevelWindowBase::RequestUserAttention(int WXUNUSED(flags))
444 {
445 // it's probably better than do nothing, isn't it?
446 Raise();
447 }