]> git.saurik.com Git - wxWidgets.git/blame - src/common/toplvcmn.cpp
don't allocate m_refData unnecessarily
[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)
7d9f12f3
VS
43END_EVENT_TABLE()
44
45// ============================================================================
46// implementation
47// ============================================================================
48
f58585c0 49IMPLEMENT_ABSTRACT_CLASS(wxTopLevelWindow, wxWindow)
82c9f85c 50
7d9f12f3
VS
51// ----------------------------------------------------------------------------
52// construction/destruction
53// ----------------------------------------------------------------------------
54
55wxTopLevelWindowBase::wxTopLevelWindowBase()
56{
c7e61a5e
DE
57 // Unlike windows, top level windows are created hidden by default.
58 m_isShown = false;
6c20e8f8
VZ
59 m_winDefault = NULL;
60 m_winTmpDefault = NULL;
7d9f12f3
VS
61}
62
799ea011
GD
63wxTopLevelWindowBase::~wxTopLevelWindowBase()
64{
1cbee0b4
VZ
65 // don't let wxTheApp keep any stale pointers to us
66 if ( wxTheApp && wxTheApp->GetTopWindow() == this )
67 wxTheApp->SetTopWindow(NULL);
68
1cbee0b4 69 wxTopLevelWindows.DeleteObject(this);
cb719f2e 70
65afac3f 71 if ( IsLastBeforeExit() )
1cbee0b4 72 {
65afac3f 73 // no other (important) windows left, quit the app
1cbee0b4
VZ
74 wxTheApp->ExitMainLoop();
75 }
799ea011
GD
76}
77
7d9f12f3
VS
78bool wxTopLevelWindowBase::Destroy()
79{
80 // delayed destruction: the frame will be deleted during the next idle
81 // loop iteration
82 if ( !wxPendingDelete.Member(this) )
83 wxPendingDelete.Append(this);
84
b3bd912d
RR
85 if (wxTopLevelWindows.GetCount() > 1)
86 {
87 // Hide it immediately. This should
88 // not be done if this TLW is the
89 // only one left since we then would
90 // risk not to get any idle events
cb719f2e 91 // at all anymore during which we
b3bd912d
RR
92 // could delete any pending events.
93 Hide();
94 }
cafcf62a 95
cb719f2e 96 return true;
7d9f12f3
VS
97}
98
5c363878 99bool wxTopLevelWindowBase::IsLastBeforeExit() const
1cbee0b4 100{
65afac3f
VZ
101 // first of all, automatically exiting the app on last window close can be
102 // completely disabled at wxTheApp level
103 if ( !wxTheApp || !wxTheApp->GetExitOnFrameDelete() )
104 return false;
105
106 wxWindowList::const_iterator i;
107 const wxWindowList::const_iterator end = wxTopLevelWindows.end();
108
109 // then decide whether we should exit at all
110 for ( i = wxTopLevelWindows.begin(); i != end; ++i )
111 {
112 wxTopLevelWindow * const win = wx_static_cast(wxTopLevelWindow *, *i);
113 if ( win->ShouldPreventAppExit() )
114 {
115 // there remains at least one important TLW, don't exit
116 return false;
117 }
118 }
119
120 // if yes, close all the other windows: this could still fail
121 for ( i = wxTopLevelWindows.begin(); i != end; ++i )
122 {
123 // don't close twice the windows which are already marked for deletion
124 wxTopLevelWindow * const win = wx_static_cast(wxTopLevelWindow *, *i);
125 if ( !wxPendingDelete.Member(win) && !win->Close() )
126 {
127 // one of the windows refused to close, don't exit
128 //
129 // NB: of course, by now some other windows could have been already
130 // closed but there is really nothing we can do about it as we
131 // have no way just to ask the window if it can close without
132 // forcing it to do it
133 return false;
134 }
135 }
136
137 return true;
1cbee0b4
VZ
138}
139
140// ----------------------------------------------------------------------------
141// wxTopLevelWindow geometry
142// ----------------------------------------------------------------------------
143
cda5834e
RR
144void wxTopLevelWindowBase::SetMinSize(const wxSize& minSize)
145{
146 SetSizeHints( minSize.x, minSize.y, GetMaxWidth(), GetMaxHeight() );
147}
148
149void wxTopLevelWindowBase::SetMaxSize(const wxSize& maxSize)
150{
151 SetSizeHints( GetMinWidth(), GetMinHeight(), maxSize.x, maxSize.y );
152}
153
9379c0d7
RR
154// set the min/max size of the window
155void wxTopLevelWindowBase::DoSetSizeHints(int minW, int minH,
156 int maxW, int maxH,
157 int WXUNUSED(incW), int WXUNUSED(incH))
158{
159 // setting min width greater than max width leads to infinite loops under
160 // X11 and generally doesn't make any sense, so don't allow it
161 wxCHECK_RET( (minW == wxDefaultCoord || maxW == wxDefaultCoord || minW <= maxW) &&
162 (minH == wxDefaultCoord || maxH == wxDefaultCoord || minH <= maxH),
163 _T("min width/height must be less than max width/height!") );
164
165 m_minWidth = minW;
166 m_maxWidth = maxW;
167 m_minHeight = minH;
168 m_maxHeight = maxH;
169}
170
0bba37f5
DE
171void wxTopLevelWindowBase::GetRectForTopLevelChildren(int *x, int *y, int *w, int *h)
172{
173 GetPosition(x,y);
174 GetSize(w,h);
175}
176
0c089c08
VZ
177/* static */
178wxSize wxTopLevelWindowBase::GetDefaultSize()
179{
180 wxSize size = wxGetClientDisplayRect().GetSize();
181
182 // create proportionally bigger windows on small screens
183 if ( size.x >= 1024 )
184 size.x = 400;
185 else if ( size.x >= 800 )
186 size.x = 300;
187 else if ( size.x >= 320 )
188 size.x = 240;
189
190 if ( size.y >= 768 )
191 size.y = 250;
192 else if ( size.y > 200 )
193 {
194 size.y *= 2;
195 size.y /= 3;
196 }
197
198 return size;
199}
200
1f464296
VZ
201void wxTopLevelWindowBase::DoCentre(int dir)
202{
979a0320
WS
203 // on some platforms centering top level windows is impossible
204 // because they are always maximized by guidelines or limitations
205 if(IsAlwaysMaximized())
206 return;
207
262a1536
VZ
208 // we need the display rect anyhow so store it first
209 int nDisplay = wxDisplay::GetFromWindow(this);
210 wxDisplay dpy(nDisplay == wxNOT_FOUND ? 0 : nDisplay);
211 const wxRect rectDisplay(dpy.GetClientArea());
212
213 // what should we centre this window on?
214 wxRect rectParent;
1f464296
VZ
215 if ( !(dir & wxCENTRE_ON_SCREEN) && GetParent() )
216 {
5a63a0f1
VZ
217 // centre on parent window: notice that we need screen coordinates for
218 // positioning this TLW
262a1536
VZ
219 rectParent = GetParent()->GetScreenRect();
220
221 // if the parent is entirely off screen (happens at least with MDI
222 // parent frame under Mac but could happen elsewhere too if the frame
223 // was hidden/moved away for some reason), don't use it as otherwise
224 // this window wouldn't be visible at all
22a35096
VS
225 if ( !rectDisplay.Contains(rectParent.GetTopLeft()) &&
226 !rectParent.Contains(rectParent.GetBottomRight()) )
262a1536
VZ
227 {
228 // this is enough to make IsEmpty() test below pass
229 rectParent.width = 0;
230 }
1f464296 231 }
262a1536
VZ
232
233 if ( rectParent.IsEmpty() )
1f464296 234 {
ca08b543 235 // we were explicitly asked to centre this window on the entire screen
1f464296 236 // or if we have no parent anyhow and so can't centre on it
262a1536
VZ
237 rectParent = rectDisplay;
238 }
239
cca1624d
WS
240 // centering maximized window on screen is no-op
241 if((rectParent == rectDisplay) && IsMaximized())
242 return;
243
262a1536
VZ
244 // the new window rect candidate
245 wxRect rect = GetRect().CentreIn(rectParent, dir);
246
247 // we don't want to place the window off screen if Centre() is called as
248 // this is (almost?) never wanted and it would be very difficult to prevent
249 // it from happening from the user code if we didn't check for it here
22a35096 250 if ( rectDisplay.Contains(rect.GetTopLeft()) )
262a1536 251 {
22a35096 252 if ( !rectDisplay.Contains(rect.GetBottomRight()) )
1f464296 253 {
ca08b543
VZ
254 // move the window just enough for the bottom right corner to
255 // become visible
262a1536
VZ
256 int dx = rectDisplay.GetRight() - rect.GetRight();
257 int dy = rectDisplay.GetBottom() - rect.GetBottom();
ca08b543 258 rect.Offset(dx < 0 ? dx : 0, dy < 0 ? dy : 0);
1f464296 259 }
262a1536
VZ
260 //else: the window top left and bottom right corner are both visible,
261 // although the window might still be not entirely on screen (with
262 // 2 staggered displays for example) we wouldn't be able to
263 // improve the layout much in such case, so just leave it as is
264 }
265 else // make top left corner visible
266 {
267 if ( rect.x < rectDisplay.x )
268 rect.x = rectDisplay.x;
269
270 if ( rect.y < rectDisplay.y )
271 rect.y = rectDisplay.y;
1f464296
VZ
272 }
273
262a1536
VZ
274 // -1 could be valid coordinate here if there are several displays
275 SetSize(rect, wxSIZE_ALLOW_MINUS_ONE);
1f464296
VZ
276}
277
7d9f12f3 278// ----------------------------------------------------------------------------
82c9f85c
VZ
279// wxTopLevelWindow size management: we exclude the areas taken by
280// menu/status/toolbars from the client area, so the client area is what's
281// really available for the frame contents
7d9f12f3
VS
282// ----------------------------------------------------------------------------
283
284void wxTopLevelWindowBase::DoScreenToClient(int *x, int *y) const
285{
286 wxWindow::DoScreenToClient(x, y);
287
82c9f85c 288 // translate the wxWindow client coords to our client coords
7d9f12f3 289 wxPoint pt(GetClientAreaOrigin());
82c9f85c
VZ
290 if ( x )
291 *x -= pt.x;
292 if ( y )
293 *y -= pt.y;
7d9f12f3
VS
294}
295
296void wxTopLevelWindowBase::DoClientToScreen(int *x, int *y) const
297{
82c9f85c
VZ
298 // our client area origin (0, 0) may be really something like (0, 30) for
299 // wxWindow if we have a toolbar, account for it before translating
300 wxPoint pt(GetClientAreaOrigin());
301 if ( x )
302 *x += pt.x;
303 if ( y )
304 *y += pt.y;
7d9f12f3
VS
305
306 wxWindow::DoClientToScreen(x, y);
307}
308
979a0320
WS
309bool wxTopLevelWindowBase::IsAlwaysMaximized() const
310{
311#if defined(__SMARTPHONE__) || defined(__POCKETPC__)
312 return true;
313#else
314 return false;
315#endif
316}
7d9f12f3
VS
317
318// ----------------------------------------------------------------------------
319// event handlers
320// ----------------------------------------------------------------------------
321
322// default resizing behaviour - if only ONE subwindow, resize to fill the
323// whole client area
5e62d4a5 324void wxTopLevelWindowBase::DoLayout()
7d9f12f3 325{
bc55104d 326 // if we're using constraints or sizers - do use them
7d9f12f3
VS
327 if ( GetAutoLayout() )
328 {
329 Layout();
330 }
331 else
7d9f12f3
VS
332 {
333 // do we have _exactly_ one child?
334 wxWindow *child = (wxWindow *)NULL;
222ed1d6 335 for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
7d9f12f3
VS
336 node;
337 node = node->GetNext() )
338 {
339 wxWindow *win = node->GetData();
340
341 // exclude top level and managed windows (status bar isn't
342 // currently in the children list except under wxMac anyhow, but
343 // it makes no harm to test for it)
344 if ( !win->IsTopLevel() && !IsOneOfBars(win) )
345 {
346 if ( child )
347 {
348 return; // it's our second subwindow - nothing to do
349 }
350
351 child = win;
352 }
353 }
354
355 // do we have any children at all?
d3cd1c03 356 if ( child && child->IsShown() )
7d9f12f3
VS
357 {
358 // exactly one child - set it's size to fill the whole frame
359 int clientW, clientH;
360 DoGetClientSize(&clientW, &clientH);
361
cd8667eb 362 child->SetSize(0, 0, clientW, clientH);
7d9f12f3
VS
363 }
364 }
365}
366
367// The default implementation for the close window event.
368void wxTopLevelWindowBase::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
369{
370 Destroy();
371}
372
f8ab85ae
VZ
373void wxTopLevelWindowBase::OnChildDestroy(wxWindowDestroyEvent& event)
374{
375 event.Skip();
376
377 wxWindow * const win = event.GetWindow();
378 if ( win == m_winDefault )
379 m_winDefault = NULL;
380 if ( win == m_winTmpDefault )
381 m_winTmpDefault = NULL;
382}
383
7d9f12f3
VS
384bool wxTopLevelWindowBase::SendIconizeEvent(bool iconized)
385{
386 wxIconizeEvent event(GetId(), iconized);
387 event.SetEventObject(this);
388
389 return GetEventHandler()->ProcessEvent(event);
390}
34c3ffca 391
e39af974
JS
392// do the window-specific processing after processing the update event
393void wxTopLevelWindowBase::DoUpdateWindowUI(wxUpdateUIEvent& event)
394{
a3a4105d
VZ
395 // call inherited, but skip the wxControl's version, and call directly the
396 // wxWindow's one instead, because the only reason why we are overriding this
397 // function is that we want to use SetTitle() instead of wxControl::SetLabel()
398 wxWindowBase::DoUpdateWindowUI(event);
cb719f2e 399
a3a4105d 400 // update title
e39af974
JS
401 if ( event.GetSetText() )
402 {
403 if ( event.GetText() != GetTitle() )
404 SetTitle(event.GetText());
405 }
406}
407
447fd332 408void wxTopLevelWindowBase::RequestUserAttention(int WXUNUSED(flags))
9b59c95b
VZ
409{
410 // it's probably better than do nothing, isn't it?
411 Raise();
412}
6c20e8f8
VZ
413
414void wxTopLevelWindowBase::RemoveChild(wxWindowBase *child)
415{
416 if ( child == m_winDefault )
417 m_winDefault = NULL;
418
419 if ( child == m_winTmpDefault )
420 m_winTmpDefault = NULL;
421
422 wxWindow::RemoveChild(child);
423}