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