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