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