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