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