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