]> git.saurik.com Git - wxWidgets.git/blame - src/common/toplvcmn.cpp
Misc validity fixes to samples/xrc/rc/*.xrc.
[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
55d99c7a 6// Copyright: (c) 1998 Robert Roebling and Julian Smart
65571936 7// Licence: wxWindows licence
7d9f12f3
VS
8/////////////////////////////////////////////////////////////////////////////
9
10// ============================================================================
11// declarations
12// ============================================================================
13
14// ----------------------------------------------------------------------------
15// headers
16// ----------------------------------------------------------------------------
17
7d9f12f3
VS
18// For compilers that support precompilation, includes "wx.h".
19#include "wx/wxprec.h"
20
21#ifdef __BORLANDC__
22 #pragma hdrstop
23#endif
24
1832043f
WS
25#include "wx/toplevel.h"
26
7d9f12f3 27#ifndef WX_PRECOMP
7d9f12f3 28 #include "wx/dcclient.h"
5f1d3069 29 #include "wx/app.h"
7d9f12f3
VS
30#endif // WX_PRECOMP
31
1f464296
VZ
32#include "wx/display.h"
33
7d9f12f3
VS
34// ----------------------------------------------------------------------------
35// event table
36// ----------------------------------------------------------------------------
37
38BEGIN_EVENT_TABLE(wxTopLevelWindowBase, wxWindow)
39 EVT_CLOSE(wxTopLevelWindowBase::OnCloseWindow)
40 EVT_SIZE(wxTopLevelWindowBase::OnSize)
41END_EVENT_TABLE()
42
43// ============================================================================
44// implementation
45// ============================================================================
46
f58585c0 47IMPLEMENT_ABSTRACT_CLASS(wxTopLevelWindow, wxWindow)
82c9f85c 48
7d9f12f3
VS
49// ----------------------------------------------------------------------------
50// construction/destruction
51// ----------------------------------------------------------------------------
52
53wxTopLevelWindowBase::wxTopLevelWindowBase()
54{
c7e61a5e
DE
55 // Unlike windows, top level windows are created hidden by default.
56 m_isShown = false;
7d9f12f3
VS
57}
58
799ea011
GD
59wxTopLevelWindowBase::~wxTopLevelWindowBase()
60{
1cbee0b4
VZ
61 // don't let wxTheApp keep any stale pointers to us
62 if ( wxTheApp && wxTheApp->GetTopWindow() == this )
63 wxTheApp->SetTopWindow(NULL);
64
1cbee0b4 65 wxTopLevelWindows.DeleteObject(this);
cb719f2e 66
ef37a431
VZ
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
65afac3f 95 if ( IsLastBeforeExit() )
1cbee0b4 96 {
65afac3f 97 // no other (important) windows left, quit the app
1cbee0b4
VZ
98 wxTheApp->ExitMainLoop();
99 }
799ea011
GD
100}
101
7d9f12f3
VS
102bool 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
18868d62
VZ
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
18868d62
VZ
115 for ( wxWindowList::const_iterator i = wxTopLevelWindows.begin(),
116 end = wxTopLevelWindows.end();
117 i != end;
118 ++i )
b3bd912d 119 {
5c33522f 120 wxTopLevelWindow * const win = static_cast<wxTopLevelWindow *>(*i);
18868d62
VZ
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 }
b3bd912d 129 }
cafcf62a 130
cb719f2e 131 return true;
7d9f12f3
VS
132}
133
5c363878 134bool wxTopLevelWindowBase::IsLastBeforeExit() const
1cbee0b4 135{
65afac3f
VZ
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 {
5c33522f 147 wxTopLevelWindow * const win = static_cast<wxTopLevelWindow *>(*i);
65afac3f
VZ
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
5c33522f 159 wxTopLevelWindow * const win = static_cast<wxTopLevelWindow *>(*i);
65afac3f
VZ
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;
1cbee0b4
VZ
173}
174
175// ----------------------------------------------------------------------------
176// wxTopLevelWindow geometry
177// ----------------------------------------------------------------------------
178
cda5834e
RR
179void wxTopLevelWindowBase::SetMinSize(const wxSize& minSize)
180{
49c9d059 181 SetSizeHints(minSize, GetMaxSize());
cda5834e
RR
182}
183
184void wxTopLevelWindowBase::SetMaxSize(const wxSize& maxSize)
185{
49c9d059 186 SetSizeHints(GetMinSize(), maxSize);
9379c0d7
RR
187}
188
0bba37f5
DE
189void wxTopLevelWindowBase::GetRectForTopLevelChildren(int *x, int *y, int *w, int *h)
190{
191 GetPosition(x,y);
192 GetSize(w,h);
193}
194
0c089c08
VZ
195/* static */
196wxSize wxTopLevelWindowBase::GetDefaultSize()
197{
198 wxSize size = wxGetClientDisplayRect().GetSize();
3ec9ba03 199#ifndef __WXOSX_IPHONE__
0c089c08
VZ
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 }
3ec9ba03 215#endif
0c089c08
VZ
216 return size;
217}
218
1f464296
VZ
219void wxTopLevelWindowBase::DoCentre(int dir)
220{
979a0320
WS
221 // on some platforms centering top level windows is impossible
222 // because they are always maximized by guidelines or limitations
ca67f0d8
VZ
223 //
224 // and centering a maximized window doesn't make sense as its position
225 // can't change
226 if ( IsAlwaysMaximized() || IsMaximized() )
979a0320
WS
227 return;
228
4745f3c6
VZ
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);
262a1536
VZ
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;
1f464296
VZ
238 if ( !(dir & wxCENTRE_ON_SCREEN) && GetParent() )
239 {
5a63a0f1
VZ
240 // centre on parent window: notice that we need screen coordinates for
241 // positioning this TLW
262a1536
VZ
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
ca67f0d8 248 if ( !rectParent.Intersects(rectDisplay) )
262a1536 249 {
ca67f0d8
VZ
250 // just centre on screen then
251 rectParent = rectDisplay;
262a1536 252 }
1f464296 253 }
ca67f0d8 254 else
1f464296 255 {
ca08b543 256 // we were explicitly asked to centre this window on the entire screen
1f464296 257 // or if we have no parent anyhow and so can't centre on it
262a1536
VZ
258 rectParent = rectDisplay;
259 }
260
75454d24
VZ
261 if ( !(dir & wxBOTH) )
262 dir |= wxBOTH; // if neither is specified, center in both directions
263
262a1536 264 // the new window rect candidate
75454d24 265 wxRect rect = GetRect().CentreIn(rectParent, dir & ~wxCENTRE_ON_SCREEN);
262a1536
VZ
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
4745f3c6 270 if ( !rectDisplay.Contains(rect.GetTopLeft()) )
262a1536 271 {
4745f3c6
VZ
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);
262a1536 276 }
262a1536 277
4745f3c6
VZ
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);
1f464296
VZ
284 }
285
4745f3c6
VZ
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
262a1536
VZ
291 // -1 could be valid coordinate here if there are several displays
292 SetSize(rect, wxSIZE_ALLOW_MINUS_ONE);
1f464296
VZ
293}
294
7d9f12f3 295// ----------------------------------------------------------------------------
82c9f85c
VZ
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
7d9f12f3
VS
299// ----------------------------------------------------------------------------
300
301void wxTopLevelWindowBase::DoScreenToClient(int *x, int *y) const
302{
303 wxWindow::DoScreenToClient(x, y);
304
82c9f85c 305 // translate the wxWindow client coords to our client coords
7d9f12f3 306 wxPoint pt(GetClientAreaOrigin());
82c9f85c
VZ
307 if ( x )
308 *x -= pt.x;
309 if ( y )
310 *y -= pt.y;
7d9f12f3
VS
311}
312
313void wxTopLevelWindowBase::DoClientToScreen(int *x, int *y) const
314{
82c9f85c
VZ
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;
7d9f12f3
VS
322
323 wxWindow::DoClientToScreen(x, y);
324}
325
979a0320
WS
326bool wxTopLevelWindowBase::IsAlwaysMaximized() const
327{
328#if defined(__SMARTPHONE__) || defined(__POCKETPC__)
329 return true;
330#else
331 return false;
332#endif
333}
7d9f12f3
VS
334
335// ----------------------------------------------------------------------------
ea098413
VZ
336// icons
337// ----------------------------------------------------------------------------
338
339wxIcon wxTopLevelWindowBase::GetIcon() const
340{
341 return m_icons.IsEmpty() ? wxIcon() : m_icons.GetIcon( -1 );
342}
343
344void 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;
a1b806b9 349 if ( icon.IsOk() )
ea098413
VZ
350 icons.AddIcon(icon);
351
352 SetIcons(icons);
353}
354
355// ----------------------------------------------------------------------------
7d9f12f3
VS
356// event handlers
357// ----------------------------------------------------------------------------
358
359// default resizing behaviour - if only ONE subwindow, resize to fill the
360// whole client area
5e62d4a5 361void wxTopLevelWindowBase::DoLayout()
7d9f12f3 362{
5f9f6b47
VZ
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
bc55104d 371 // if we're using constraints or sizers - do use them
7d9f12f3
VS
372 if ( GetAutoLayout() )
373 {
374 Layout();
375 }
376 else
7d9f12f3
VS
377 {
378 // do we have _exactly_ one child?
d3b9f782 379 wxWindow *child = NULL;
222ed1d6 380 for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
7d9f12f3
VS
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?
d3cd1c03 401 if ( child && child->IsShown() )
7d9f12f3
VS
402 {
403 // exactly one child - set it's size to fill the whole frame
404 int clientW, clientH;
405 DoGetClientSize(&clientW, &clientH);
406
cd8667eb 407 child->SetSize(0, 0, clientW, clientH);
7d9f12f3
VS
408 }
409 }
410}
411
412// The default implementation for the close window event.
413void wxTopLevelWindowBase::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
414{
415 Destroy();
416}
417
418bool wxTopLevelWindowBase::SendIconizeEvent(bool iconized)
419{
420 wxIconizeEvent event(GetId(), iconized);
421 event.SetEventObject(this);
422
423 return GetEventHandler()->ProcessEvent(event);
424}
34c3ffca 425
e39af974
JS
426// do the window-specific processing after processing the update event
427void wxTopLevelWindowBase::DoUpdateWindowUI(wxUpdateUIEvent& event)
428{
a3a4105d
VZ
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);
cb719f2e 433
a3a4105d 434 // update title
e39af974
JS
435 if ( event.GetSetText() )
436 {
437 if ( event.GetText() != GetTitle() )
438 SetTitle(event.GetText());
439 }
440}
441
447fd332 442void wxTopLevelWindowBase::RequestUserAttention(int WXUNUSED(flags))
9b59c95b
VZ
443{
444 // it's probably better than do nothing, isn't it?
445 Raise();
446}