]> git.saurik.com Git - wxWidgets.git/blob - src/common/toplvcmn.cpp
clean up modules after destroying the app, not before it
[wxWidgets.git] / src / common / toplvcmn.cpp
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$
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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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"
33 #include "wx/app.h"
34 #endif // WX_PRECOMP
35
36 // ----------------------------------------------------------------------------
37 // event table
38 // ----------------------------------------------------------------------------
39
40 BEGIN_EVENT_TABLE(wxTopLevelWindowBase, wxWindow)
41 EVT_CLOSE(wxTopLevelWindowBase::OnCloseWindow)
42 EVT_SIZE(wxTopLevelWindowBase::OnSize)
43 END_EVENT_TABLE()
44
45 // ============================================================================
46 // implementation
47 // ============================================================================
48
49 IMPLEMENT_DYNAMIC_CLASS(wxTopLevelWindow, wxWindow)
50
51 // ----------------------------------------------------------------------------
52 // construction/destruction
53 // ----------------------------------------------------------------------------
54
55 wxTopLevelWindowBase::wxTopLevelWindowBase()
56 {
57 // Unlike windows, top level windows are created hidden by default.
58 m_isShown = false;
59 }
60
61 wxTopLevelWindowBase::~wxTopLevelWindowBase()
62 {
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);
70
71 if ( shouldExit )
72 {
73 // then do it
74 wxTheApp->ExitMainLoop();
75 }
76 }
77
78 bool 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
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
91 // at all anymore during which we
92 // could delete any pending events.
93 Hide();
94 }
95
96 return TRUE;
97 }
98
99 bool wxTopLevelWindowBase::IsLastBeforeExit() const
100 {
101 // we exit the application if there are no more top level windows left
102 // normally but wxApp can prevent this from happening
103 return wxTopLevelWindows.GetCount() == 1 &&
104 wxTopLevelWindows.GetFirst()->GetData() == (wxWindow *)this &&
105 wxTheApp && wxTheApp->GetExitOnFrameDelete();
106 }
107
108 // ----------------------------------------------------------------------------
109 // wxTopLevelWindow geometry
110 // ----------------------------------------------------------------------------
111
112 wxSize wxTopLevelWindowBase::GetMaxSize() const
113 {
114 wxSize size( GetMaxWidth(), GetMaxHeight() );
115 int w, h;
116
117 wxClientDisplayRect( 0, 0, &w, &h );
118
119 if( size.GetWidth() == -1 )
120 size.SetWidth( w );
121
122 if( size.GetHeight() == -1 )
123 size.SetHeight( h );
124
125 return size;
126 }
127
128 /* static */
129 wxSize wxTopLevelWindowBase::GetDefaultSize()
130 {
131 wxSize size = wxGetClientDisplayRect().GetSize();
132
133 // create proportionally bigger windows on small screens
134 if ( size.x >= 1024 )
135 size.x = 400;
136 else if ( size.x >= 800 )
137 size.x = 300;
138 else if ( size.x >= 320 )
139 size.x = 240;
140
141 if ( size.y >= 768 )
142 size.y = 250;
143 else if ( size.y > 200 )
144 {
145 size.y *= 2;
146 size.y /= 3;
147 }
148
149 return size;
150 }
151
152 // ----------------------------------------------------------------------------
153 // wxTopLevelWindow size management: we exclude the areas taken by
154 // menu/status/toolbars from the client area, so the client area is what's
155 // really available for the frame contents
156 // ----------------------------------------------------------------------------
157
158 void wxTopLevelWindowBase::DoScreenToClient(int *x, int *y) const
159 {
160 wxWindow::DoScreenToClient(x, y);
161
162 // translate the wxWindow client coords to our client coords
163 wxPoint pt(GetClientAreaOrigin());
164 if ( x )
165 *x -= pt.x;
166 if ( y )
167 *y -= pt.y;
168 }
169
170 void wxTopLevelWindowBase::DoClientToScreen(int *x, int *y) const
171 {
172 // our client area origin (0, 0) may be really something like (0, 30) for
173 // wxWindow if we have a toolbar, account for it before translating
174 wxPoint pt(GetClientAreaOrigin());
175 if ( x )
176 *x += pt.x;
177 if ( y )
178 *y += pt.y;
179
180 wxWindow::DoClientToScreen(x, y);
181 }
182
183
184 // ----------------------------------------------------------------------------
185 // event handlers
186 // ----------------------------------------------------------------------------
187
188 // default resizing behaviour - if only ONE subwindow, resize to fill the
189 // whole client area
190 void wxTopLevelWindowBase::OnSize(wxSizeEvent& WXUNUSED(event))
191 {
192 // if we're using constraints or sizers - do use them
193 if ( GetAutoLayout() )
194 {
195 Layout();
196 }
197 else
198 {
199 // do we have _exactly_ one child?
200 wxWindow *child = (wxWindow *)NULL;
201 for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
202 node;
203 node = node->GetNext() )
204 {
205 wxWindow *win = node->GetData();
206
207 // exclude top level and managed windows (status bar isn't
208 // currently in the children list except under wxMac anyhow, but
209 // it makes no harm to test for it)
210 if ( !win->IsTopLevel() && !IsOneOfBars(win) )
211 {
212 if ( child )
213 {
214 return; // it's our second subwindow - nothing to do
215 }
216
217 child = win;
218 }
219 }
220
221 // do we have any children at all?
222 if ( child )
223 {
224 // exactly one child - set it's size to fill the whole frame
225 int clientW, clientH;
226 DoGetClientSize(&clientW, &clientH);
227
228 // for whatever reasons, wxGTK wants to have a small offset - it
229 // probably looks better with it?
230 #ifdef __WXGTK__
231 static const int ofs = 1;
232 #else
233 static const int ofs = 0;
234 #endif
235
236 child->SetSize(ofs, ofs, clientW - 2*ofs, clientH - 2*ofs);
237 }
238 }
239 }
240
241 // The default implementation for the close window event.
242 void wxTopLevelWindowBase::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
243 {
244 Destroy();
245 }
246
247 bool wxTopLevelWindowBase::SendIconizeEvent(bool iconized)
248 {
249 wxIconizeEvent event(GetId(), iconized);
250 event.SetEventObject(this);
251
252 return GetEventHandler()->ProcessEvent(event);
253 }
254
255 // do the window-specific processing after processing the update event
256 void wxTopLevelWindowBase::DoUpdateWindowUI(wxUpdateUIEvent& event)
257 {
258 if ( event.GetSetEnabled() )
259 Enable(event.GetEnabled());
260
261 if ( event.GetSetText() )
262 {
263 if ( event.GetText() != GetTitle() )
264 SetTitle(event.GetText());
265 }
266 }
267
268 // vi:sts=4:sw=4:et