]> git.saurik.com Git - wxWidgets.git/blame - src/common/toplvcmn.cpp
added possibility to throw different objects and crash directly from the main frame...
[wxWidgets.git] / src / common / toplvcmn.cpp
CommitLineData
7d9f12f3
VS
1/////////////////////////////////////////////////////////////////////////////
2// Name: 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$
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
14f355c2 19#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
7d9f12f3
VS
20 #pragma implementation "toplevelbase.h"
21#endif
22
23// For compilers that support precompilation, includes "wx.h".
24#include "wx/wxprec.h"
25
26#ifdef __BORLANDC__
27 #pragma hdrstop
28#endif
29
30#ifndef WX_PRECOMP
31 #include "wx/toplevel.h"
32 #include "wx/dcclient.h"
5f1d3069 33 #include "wx/app.h"
7d9f12f3
VS
34#endif // WX_PRECOMP
35
36// ----------------------------------------------------------------------------
37// event table
38// ----------------------------------------------------------------------------
39
40BEGIN_EVENT_TABLE(wxTopLevelWindowBase, wxWindow)
41 EVT_CLOSE(wxTopLevelWindowBase::OnCloseWindow)
42 EVT_SIZE(wxTopLevelWindowBase::OnSize)
43END_EVENT_TABLE()
44
45// ============================================================================
46// implementation
47// ============================================================================
48
ddbfcced 49IMPLEMENT_DYNAMIC_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;
7d9f12f3
VS
59}
60
799ea011
GD
61wxTopLevelWindowBase::~wxTopLevelWindowBase()
62{
1cbee0b4
VZ
63 // don't let wxTheApp keep any stale pointers to us
64 if ( wxTheApp && wxTheApp->GetTopWindow() == this )
65 wxTheApp->SetTopWindow(NULL);
66
67 bool shouldExit = IsLastBeforeExit();
68
69 wxTopLevelWindows.DeleteObject(this);
cb719f2e 70
1cbee0b4
VZ
71 if ( shouldExit )
72 {
73 // then do it
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
VZ
100{
101 // we exit the application if there are no more top level windows left
102 // normally but wxApp can prevent this from happening
5c363878
VZ
103 return wxTopLevelWindows.GetCount() == 1 &&
104 wxTopLevelWindows.GetFirst()->GetData() == (wxWindow *)this &&
1cbee0b4
VZ
105 wxTheApp && wxTheApp->GetExitOnFrameDelete();
106}
107
108// ----------------------------------------------------------------------------
109// wxTopLevelWindow geometry
110// ----------------------------------------------------------------------------
111
0bba37f5
DE
112void wxTopLevelWindowBase::GetRectForTopLevelChildren(int *x, int *y, int *w, int *h)
113{
114 GetPosition(x,y);
115 GetSize(w,h);
116}
117
34c3ffca
RL
118wxSize wxTopLevelWindowBase::GetMaxSize() const
119{
120 wxSize size( GetMaxWidth(), GetMaxHeight() );
121 int w, h;
122
123 wxClientDisplayRect( 0, 0, &w, &h );
124
cb719f2e 125 if( size.GetWidth() == wxDefaultCoord )
34c3ffca
RL
126 size.SetWidth( w );
127
cb719f2e 128 if( size.GetHeight() == wxDefaultCoord )
34c3ffca
RL
129 size.SetHeight( h );
130
131 return size;
132}
133
0c089c08
VZ
134/* static */
135wxSize wxTopLevelWindowBase::GetDefaultSize()
136{
137 wxSize size = wxGetClientDisplayRect().GetSize();
138
139 // create proportionally bigger windows on small screens
140 if ( size.x >= 1024 )
141 size.x = 400;
142 else if ( size.x >= 800 )
143 size.x = 300;
144 else if ( size.x >= 320 )
145 size.x = 240;
146
147 if ( size.y >= 768 )
148 size.y = 250;
149 else if ( size.y > 200 )
150 {
151 size.y *= 2;
152 size.y /= 3;
153 }
154
155 return size;
156}
157
7d9f12f3 158// ----------------------------------------------------------------------------
82c9f85c
VZ
159// wxTopLevelWindow size management: we exclude the areas taken by
160// menu/status/toolbars from the client area, so the client area is what's
161// really available for the frame contents
7d9f12f3
VS
162// ----------------------------------------------------------------------------
163
164void wxTopLevelWindowBase::DoScreenToClient(int *x, int *y) const
165{
166 wxWindow::DoScreenToClient(x, y);
167
82c9f85c 168 // translate the wxWindow client coords to our client coords
7d9f12f3 169 wxPoint pt(GetClientAreaOrigin());
82c9f85c
VZ
170 if ( x )
171 *x -= pt.x;
172 if ( y )
173 *y -= pt.y;
7d9f12f3
VS
174}
175
176void wxTopLevelWindowBase::DoClientToScreen(int *x, int *y) const
177{
82c9f85c
VZ
178 // our client area origin (0, 0) may be really something like (0, 30) for
179 // wxWindow if we have a toolbar, account for it before translating
180 wxPoint pt(GetClientAreaOrigin());
181 if ( x )
182 *x += pt.x;
183 if ( y )
184 *y += pt.y;
7d9f12f3
VS
185
186 wxWindow::DoClientToScreen(x, y);
187}
188
189
190// ----------------------------------------------------------------------------
191// event handlers
192// ----------------------------------------------------------------------------
193
194// default resizing behaviour - if only ONE subwindow, resize to fill the
195// whole client area
5e62d4a5 196void wxTopLevelWindowBase::DoLayout()
7d9f12f3 197{
bc55104d 198 // if we're using constraints or sizers - do use them
7d9f12f3
VS
199 if ( GetAutoLayout() )
200 {
201 Layout();
202 }
203 else
7d9f12f3
VS
204 {
205 // do we have _exactly_ one child?
206 wxWindow *child = (wxWindow *)NULL;
222ed1d6 207 for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
7d9f12f3
VS
208 node;
209 node = node->GetNext() )
210 {
211 wxWindow *win = node->GetData();
212
213 // exclude top level and managed windows (status bar isn't
214 // currently in the children list except under wxMac anyhow, but
215 // it makes no harm to test for it)
216 if ( !win->IsTopLevel() && !IsOneOfBars(win) )
217 {
218 if ( child )
219 {
220 return; // it's our second subwindow - nothing to do
221 }
222
223 child = win;
224 }
225 }
226
227 // do we have any children at all?
228 if ( child )
229 {
230 // exactly one child - set it's size to fill the whole frame
231 int clientW, clientH;
232 DoGetClientSize(&clientW, &clientH);
233
234 // for whatever reasons, wxGTK wants to have a small offset - it
235 // probably looks better with it?
236#ifdef __WXGTK__
237 static const int ofs = 1;
238#else
239 static const int ofs = 0;
240#endif
241
45e0dc94 242 child->SetSize(ofs, ofs, clientW - 2*ofs, clientH - 2*ofs);
7d9f12f3
VS
243 }
244 }
245}
246
247// The default implementation for the close window event.
248void wxTopLevelWindowBase::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
249{
250 Destroy();
251}
252
253bool wxTopLevelWindowBase::SendIconizeEvent(bool iconized)
254{
255 wxIconizeEvent event(GetId(), iconized);
256 event.SetEventObject(this);
257
258 return GetEventHandler()->ProcessEvent(event);
259}
34c3ffca 260
e39af974
JS
261// do the window-specific processing after processing the update event
262void wxTopLevelWindowBase::DoUpdateWindowUI(wxUpdateUIEvent& event)
263{
264 if ( event.GetSetEnabled() )
265 Enable(event.GetEnabled());
cb719f2e 266
e39af974
JS
267 if ( event.GetSetText() )
268 {
269 if ( event.GetText() != GetTitle() )
270 SetTitle(event.GetText());
271 }
272}
273
447fd332 274void wxTopLevelWindowBase::RequestUserAttention(int WXUNUSED(flags))
9b59c95b
VZ
275{
276 // it's probably better than do nothing, isn't it?
277 Raise();
278}
279