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