]> git.saurik.com Git - wxWidgets.git/blame - src/common/toplvcmn.cpp
Minor header cleaning.
[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
9379c0d7
RR
143// set the min/max size of the window
144void wxTopLevelWindowBase::DoSetSizeHints(int minW, int minH,
145 int maxW, int maxH,
146 int WXUNUSED(incW), int WXUNUSED(incH))
147{
148 // setting min width greater than max width leads to infinite loops under
149 // X11 and generally doesn't make any sense, so don't allow it
150 wxCHECK_RET( (minW == wxDefaultCoord || maxW == wxDefaultCoord || minW <= maxW) &&
151 (minH == wxDefaultCoord || maxH == wxDefaultCoord || minH <= maxH),
152 _T("min width/height must be less than max width/height!") );
153
154 m_minWidth = minW;
155 m_maxWidth = maxW;
156 m_minHeight = minH;
157 m_maxHeight = maxH;
158}
159
0bba37f5
DE
160void wxTopLevelWindowBase::GetRectForTopLevelChildren(int *x, int *y, int *w, int *h)
161{
162 GetPosition(x,y);
163 GetSize(w,h);
164}
165
34c3ffca
RL
166wxSize wxTopLevelWindowBase::GetMaxSize() const
167{
20aeaa20 168 wxSize size = wxWindow::GetMaxSize();
34c3ffca 169
20aeaa20
VZ
170 int w, h;
171 wxClientDisplayRect(NULL, NULL, &w, &h );
34c3ffca 172
20aeaa20
VZ
173 if ( size.GetWidth() == wxDefaultCoord )
174 size.SetWidth(w);
34c3ffca 175
20aeaa20
VZ
176 if ( size.GetHeight() == wxDefaultCoord )
177 size.SetHeight(h);
34c3ffca
RL
178
179 return size;
180}
181
0c089c08
VZ
182/* static */
183wxSize wxTopLevelWindowBase::GetDefaultSize()
184{
185 wxSize size = wxGetClientDisplayRect().GetSize();
186
187 // create proportionally bigger windows on small screens
188 if ( size.x >= 1024 )
189 size.x = 400;
190 else if ( size.x >= 800 )
191 size.x = 300;
192 else if ( size.x >= 320 )
193 size.x = 240;
194
195 if ( size.y >= 768 )
196 size.y = 250;
197 else if ( size.y > 200 )
198 {
199 size.y *= 2;
200 size.y /= 3;
201 }
202
203 return size;
204}
205
1f464296
VZ
206void wxTopLevelWindowBase::DoCentre(int dir)
207{
979a0320
WS
208 // on some platforms centering top level windows is impossible
209 // because they are always maximized by guidelines or limitations
210 if(IsAlwaysMaximized())
211 return;
212
262a1536
VZ
213 // we need the display rect anyhow so store it first
214 int nDisplay = wxDisplay::GetFromWindow(this);
215 wxDisplay dpy(nDisplay == wxNOT_FOUND ? 0 : nDisplay);
216 const wxRect rectDisplay(dpy.GetClientArea());
217
218 // what should we centre this window on?
219 wxRect rectParent;
1f464296
VZ
220 if ( !(dir & wxCENTRE_ON_SCREEN) && GetParent() )
221 {
5a63a0f1
VZ
222 // centre on parent window: notice that we need screen coordinates for
223 // positioning this TLW
262a1536
VZ
224 rectParent = GetParent()->GetScreenRect();
225
226 // if the parent is entirely off screen (happens at least with MDI
227 // parent frame under Mac but could happen elsewhere too if the frame
228 // was hidden/moved away for some reason), don't use it as otherwise
229 // this window wouldn't be visible at all
22a35096
VS
230 if ( !rectDisplay.Contains(rectParent.GetTopLeft()) &&
231 !rectParent.Contains(rectParent.GetBottomRight()) )
262a1536
VZ
232 {
233 // this is enough to make IsEmpty() test below pass
234 rectParent.width = 0;
235 }
1f464296 236 }
262a1536
VZ
237
238 if ( rectParent.IsEmpty() )
1f464296
VZ
239 {
240 // we were explicitely asked to centre this window on the entire screen
241 // or if we have no parent anyhow and so can't centre on it
262a1536
VZ
242 rectParent = rectDisplay;
243 }
244
cca1624d
WS
245 // centering maximized window on screen is no-op
246 if((rectParent == rectDisplay) && IsMaximized())
247 return;
248
262a1536
VZ
249 // the new window rect candidate
250 wxRect rect = GetRect().CentreIn(rectParent, dir);
251
252 // we don't want to place the window off screen if Centre() is called as
253 // this is (almost?) never wanted and it would be very difficult to prevent
254 // it from happening from the user code if we didn't check for it here
22a35096 255 if ( rectDisplay.Contains(rect.GetTopLeft()) )
262a1536 256 {
22a35096 257 if ( !rectDisplay.Contains(rect.GetBottomRight()) )
1f464296 258 {
262a1536
VZ
259 // check if we can move the window so that the bottom right corner
260 // is visible without hiding the top left one
261 int dx = rectDisplay.GetRight() - rect.GetRight();
262 int dy = rectDisplay.GetBottom() - rect.GetBottom();
263 rect.Offset(dx, dy);
1f464296 264 }
262a1536
VZ
265 //else: the window top left and bottom right corner are both visible,
266 // although the window might still be not entirely on screen (with
267 // 2 staggered displays for example) we wouldn't be able to
268 // improve the layout much in such case, so just leave it as is
269 }
270 else // make top left corner visible
271 {
272 if ( rect.x < rectDisplay.x )
273 rect.x = rectDisplay.x;
274
275 if ( rect.y < rectDisplay.y )
276 rect.y = rectDisplay.y;
1f464296
VZ
277 }
278
262a1536
VZ
279 // -1 could be valid coordinate here if there are several displays
280 SetSize(rect, wxSIZE_ALLOW_MINUS_ONE);
1f464296
VZ
281}
282
7d9f12f3 283// ----------------------------------------------------------------------------
82c9f85c
VZ
284// wxTopLevelWindow size management: we exclude the areas taken by
285// menu/status/toolbars from the client area, so the client area is what's
286// really available for the frame contents
7d9f12f3
VS
287// ----------------------------------------------------------------------------
288
289void wxTopLevelWindowBase::DoScreenToClient(int *x, int *y) const
290{
291 wxWindow::DoScreenToClient(x, y);
292
82c9f85c 293 // translate the wxWindow client coords to our client coords
7d9f12f3 294 wxPoint pt(GetClientAreaOrigin());
82c9f85c
VZ
295 if ( x )
296 *x -= pt.x;
297 if ( y )
298 *y -= pt.y;
7d9f12f3
VS
299}
300
301void wxTopLevelWindowBase::DoClientToScreen(int *x, int *y) const
302{
82c9f85c
VZ
303 // our client area origin (0, 0) may be really something like (0, 30) for
304 // wxWindow if we have a toolbar, account for it before translating
305 wxPoint pt(GetClientAreaOrigin());
306 if ( x )
307 *x += pt.x;
308 if ( y )
309 *y += pt.y;
7d9f12f3
VS
310
311 wxWindow::DoClientToScreen(x, y);
312}
313
979a0320
WS
314bool wxTopLevelWindowBase::IsAlwaysMaximized() const
315{
316#if defined(__SMARTPHONE__) || defined(__POCKETPC__)
317 return true;
318#else
319 return false;
320#endif
321}
7d9f12f3
VS
322
323// ----------------------------------------------------------------------------
324// event handlers
325// ----------------------------------------------------------------------------
326
327// default resizing behaviour - if only ONE subwindow, resize to fill the
328// whole client area
5e62d4a5 329void wxTopLevelWindowBase::DoLayout()
7d9f12f3 330{
bc55104d 331 // if we're using constraints or sizers - do use them
7d9f12f3
VS
332 if ( GetAutoLayout() )
333 {
334 Layout();
335 }
336 else
7d9f12f3
VS
337 {
338 // do we have _exactly_ one child?
339 wxWindow *child = (wxWindow *)NULL;
222ed1d6 340 for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
7d9f12f3
VS
341 node;
342 node = node->GetNext() )
343 {
344 wxWindow *win = node->GetData();
345
346 // exclude top level and managed windows (status bar isn't
347 // currently in the children list except under wxMac anyhow, but
348 // it makes no harm to test for it)
349 if ( !win->IsTopLevel() && !IsOneOfBars(win) )
350 {
351 if ( child )
352 {
353 return; // it's our second subwindow - nothing to do
354 }
355
356 child = win;
357 }
358 }
359
360 // do we have any children at all?
d3cd1c03 361 if ( child && child->IsShown() )
7d9f12f3
VS
362 {
363 // exactly one child - set it's size to fill the whole frame
364 int clientW, clientH;
365 DoGetClientSize(&clientW, &clientH);
366
367 // for whatever reasons, wxGTK wants to have a small offset - it
368 // probably looks better with it?
369#ifdef __WXGTK__
69346023
PC
370 const int ofs = 1;
371 clientW -= 2 * ofs;
372 clientH -= 2 * ofs;
373 if (clientW < 0)
374 clientW = 0;
375 if (clientH < 0)
376 clientH = 0;
7d9f12f3 377#else
69346023 378 const int ofs = 0;
7d9f12f3
VS
379#endif
380
69346023 381 child->SetSize(ofs, ofs, clientW, clientH);
7d9f12f3
VS
382 }
383 }
384}
385
386// The default implementation for the close window event.
387void wxTopLevelWindowBase::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
388{
389 Destroy();
390}
391
392bool wxTopLevelWindowBase::SendIconizeEvent(bool iconized)
393{
394 wxIconizeEvent event(GetId(), iconized);
395 event.SetEventObject(this);
396
397 return GetEventHandler()->ProcessEvent(event);
398}
34c3ffca 399
e39af974
JS
400// do the window-specific processing after processing the update event
401void wxTopLevelWindowBase::DoUpdateWindowUI(wxUpdateUIEvent& event)
402{
a3a4105d
VZ
403 // call inherited, but skip the wxControl's version, and call directly the
404 // wxWindow's one instead, because the only reason why we are overriding this
405 // function is that we want to use SetTitle() instead of wxControl::SetLabel()
406 wxWindowBase::DoUpdateWindowUI(event);
cb719f2e 407
a3a4105d 408 // update title
e39af974
JS
409 if ( event.GetSetText() )
410 {
411 if ( event.GetText() != GetTitle() )
412 SetTitle(event.GetText());
413 }
414}
415
447fd332 416void wxTopLevelWindowBase::RequestUserAttention(int WXUNUSED(flags))
9b59c95b
VZ
417{
418 // it's probably better than do nothing, isn't it?
419 Raise();
420}
6c20e8f8
VZ
421
422void wxTopLevelWindowBase::RemoveChild(wxWindowBase *child)
423{
424 if ( child == m_winDefault )
425 m_winDefault = NULL;
426
427 if ( child == m_winTmpDefault )
428 m_winTmpDefault = NULL;
429
430 wxWindow::RemoveChild(child);
431}