Include wx/list.h according to precompiled headers of wx/wx.h (with other minor clean...
[wxWidgets.git] / src / common / toplvcmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/toplvcmn.cpp
3 // Purpose: common (for all platforms) wxTopLevelWindow functions
4 // Author: Julian Smart, Vadim Zeitlin
5 // Created: 01/02/97
6 // Id: $Id$
7 // Copyright: (c) 1998 Robert Roebling and Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
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"
29 #include "wx/app.h"
30 #endif // WX_PRECOMP
31
32 #include "wx/display.h"
33
34 // ----------------------------------------------------------------------------
35 // event table
36 // ----------------------------------------------------------------------------
37
38 BEGIN_EVENT_TABLE(wxTopLevelWindowBase, wxWindow)
39 EVT_CLOSE(wxTopLevelWindowBase::OnCloseWindow)
40 EVT_SIZE(wxTopLevelWindowBase::OnSize)
41 END_EVENT_TABLE()
42
43 // ============================================================================
44 // implementation
45 // ============================================================================
46
47 IMPLEMENT_ABSTRACT_CLASS(wxTopLevelWindow, wxWindow)
48
49 // ----------------------------------------------------------------------------
50 // construction/destruction
51 // ----------------------------------------------------------------------------
52
53 wxTopLevelWindowBase::wxTopLevelWindowBase()
54 {
55 // Unlike windows, top level windows are created hidden by default.
56 m_isShown = false;
57 }
58
59 wxTopLevelWindowBase::~wxTopLevelWindowBase()
60 {
61 // don't let wxTheApp keep any stale pointers to us
62 if ( wxTheApp && wxTheApp->GetTopWindow() == this )
63 wxTheApp->SetTopWindow(NULL);
64
65 wxTopLevelWindows.DeleteObject(this);
66
67 if ( IsLastBeforeExit() )
68 {
69 // no other (important) windows left, quit the app
70 wxTheApp->ExitMainLoop();
71 }
72 }
73
74 bool 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
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
87 // at all anymore during which we
88 // could delete any pending events.
89 Hide();
90 }
91
92 return true;
93 }
94
95 bool wxTopLevelWindowBase::IsLastBeforeExit() const
96 {
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;
134 }
135
136 // ----------------------------------------------------------------------------
137 // wxTopLevelWindow geometry
138 // ----------------------------------------------------------------------------
139
140 void wxTopLevelWindowBase::GetRectForTopLevelChildren(int *x, int *y, int *w, int *h)
141 {
142 GetPosition(x,y);
143 GetSize(w,h);
144 }
145
146 wxSize wxTopLevelWindowBase::GetMaxSize() const
147 {
148 wxSize size( GetMaxWidth(), GetMaxHeight() );
149 int w, h;
150
151 wxClientDisplayRect( 0, 0, &w, &h );
152
153 if( size.GetWidth() == wxDefaultCoord )
154 size.SetWidth( w );
155
156 if( size.GetHeight() == wxDefaultCoord )
157 size.SetHeight( h );
158
159 return size;
160 }
161
162 /* static */
163 wxSize 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
186 void wxTopLevelWindowBase::DoCentre(int dir)
187 {
188 // we need the display rect anyhow so store it first
189 int nDisplay = wxDisplay::GetFromWindow(this);
190 wxDisplay dpy(nDisplay == wxNOT_FOUND ? 0 : nDisplay);
191 const wxRect rectDisplay(dpy.GetClientArea());
192
193 // what should we centre this window on?
194 wxRect rectParent;
195 if ( !(dir & wxCENTRE_ON_SCREEN) && GetParent() )
196 {
197 // centre on parent window: notice that we need screen coordinates for
198 // positioning this TLW
199 rectParent = GetParent()->GetScreenRect();
200
201 // if the parent is entirely off screen (happens at least with MDI
202 // parent frame under Mac but could happen elsewhere too if the frame
203 // was hidden/moved away for some reason), don't use it as otherwise
204 // this window wouldn't be visible at all
205 if ( !rectDisplay.Inside(rectParent.GetTopLeft()) &&
206 !rectParent.Inside(rectParent.GetBottomRight()) )
207 {
208 // this is enough to make IsEmpty() test below pass
209 rectParent.width = 0;
210 }
211 }
212
213 if ( rectParent.IsEmpty() )
214 {
215 // we were explicitely asked to centre this window on the entire screen
216 // or if we have no parent anyhow and so can't centre on it
217 rectParent = rectDisplay;
218 }
219
220 // centering maximized window on screen is no-op
221 if((rectParent == rectDisplay) && IsMaximized())
222 return;
223
224 // the new window rect candidate
225 wxRect rect = GetRect().CentreIn(rectParent, dir);
226
227 // we don't want to place the window off screen if Centre() is called as
228 // this is (almost?) never wanted and it would be very difficult to prevent
229 // it from happening from the user code if we didn't check for it here
230 if ( rectDisplay.Inside(rect.GetTopLeft()) )
231 {
232 if ( !rectDisplay.Inside(rect.GetBottomRight()) )
233 {
234 // check if we can move the window so that the bottom right corner
235 // is visible without hiding the top left one
236 int dx = rectDisplay.GetRight() - rect.GetRight();
237 int dy = rectDisplay.GetBottom() - rect.GetBottom();
238 rect.Offset(dx, dy);
239 }
240 //else: the window top left and bottom right corner are both visible,
241 // although the window might still be not entirely on screen (with
242 // 2 staggered displays for example) we wouldn't be able to
243 // improve the layout much in such case, so just leave it as is
244 }
245 else // make top left corner visible
246 {
247 if ( rect.x < rectDisplay.x )
248 rect.x = rectDisplay.x;
249
250 if ( rect.y < rectDisplay.y )
251 rect.y = rectDisplay.y;
252 }
253
254 // -1 could be valid coordinate here if there are several displays
255 SetSize(rect, wxSIZE_ALLOW_MINUS_ONE);
256 }
257
258 // ----------------------------------------------------------------------------
259 // wxTopLevelWindow size management: we exclude the areas taken by
260 // menu/status/toolbars from the client area, so the client area is what's
261 // really available for the frame contents
262 // ----------------------------------------------------------------------------
263
264 void wxTopLevelWindowBase::DoScreenToClient(int *x, int *y) const
265 {
266 wxWindow::DoScreenToClient(x, y);
267
268 // translate the wxWindow client coords to our client coords
269 wxPoint pt(GetClientAreaOrigin());
270 if ( x )
271 *x -= pt.x;
272 if ( y )
273 *y -= pt.y;
274 }
275
276 void wxTopLevelWindowBase::DoClientToScreen(int *x, int *y) const
277 {
278 // our client area origin (0, 0) may be really something like (0, 30) for
279 // wxWindow if we have a toolbar, account for it before translating
280 wxPoint pt(GetClientAreaOrigin());
281 if ( x )
282 *x += pt.x;
283 if ( y )
284 *y += pt.y;
285
286 wxWindow::DoClientToScreen(x, y);
287 }
288
289
290 // ----------------------------------------------------------------------------
291 // event handlers
292 // ----------------------------------------------------------------------------
293
294 // default resizing behaviour - if only ONE subwindow, resize to fill the
295 // whole client area
296 void wxTopLevelWindowBase::DoLayout()
297 {
298 // if we're using constraints or sizers - do use them
299 if ( GetAutoLayout() )
300 {
301 Layout();
302 }
303 else
304 {
305 // do we have _exactly_ one child?
306 wxWindow *child = (wxWindow *)NULL;
307 for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
308 node;
309 node = node->GetNext() )
310 {
311 wxWindow *win = node->GetData();
312
313 // exclude top level and managed windows (status bar isn't
314 // currently in the children list except under wxMac anyhow, but
315 // it makes no harm to test for it)
316 if ( !win->IsTopLevel() && !IsOneOfBars(win) )
317 {
318 if ( child )
319 {
320 return; // it's our second subwindow - nothing to do
321 }
322
323 child = win;
324 }
325 }
326
327 // do we have any children at all?
328 if ( child )
329 {
330 // exactly one child - set it's size to fill the whole frame
331 int clientW, clientH;
332 DoGetClientSize(&clientW, &clientH);
333
334 // for whatever reasons, wxGTK wants to have a small offset - it
335 // probably looks better with it?
336 #ifdef __WXGTK__
337 static const int ofs = 1;
338 #else
339 static const int ofs = 0;
340 #endif
341
342 child->SetSize(ofs, ofs, clientW - 2*ofs, clientH - 2*ofs);
343 }
344 }
345 }
346
347 // The default implementation for the close window event.
348 void wxTopLevelWindowBase::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
349 {
350 Destroy();
351 }
352
353 bool wxTopLevelWindowBase::SendIconizeEvent(bool iconized)
354 {
355 wxIconizeEvent event(GetId(), iconized);
356 event.SetEventObject(this);
357
358 return GetEventHandler()->ProcessEvent(event);
359 }
360
361 // do the window-specific processing after processing the update event
362 void wxTopLevelWindowBase::DoUpdateWindowUI(wxUpdateUIEvent& event)
363 {
364 // call inherited, but skip the wxControl's version, and call directly the
365 // wxWindow's one instead, because the only reason why we are overriding this
366 // function is that we want to use SetTitle() instead of wxControl::SetLabel()
367 wxWindowBase::DoUpdateWindowUI(event);
368
369 // update title
370 if ( event.GetSetText() )
371 {
372 if ( event.GetText() != GetTitle() )
373 SetTitle(event.GetText());
374 }
375 }
376
377 void wxTopLevelWindowBase::RequestUserAttention(int WXUNUSED(flags))
378 {
379 // it's probably better than do nothing, isn't it?
380 Raise();
381 }