]> git.saurik.com Git - wxWidgets.git/blob - src/common/toplvcmn.cpp
fd659ac2b6dc471d135492165aaf4d30632538ce
[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, Julian Smart and Markus Holzem
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 #ifdef __GNUG__
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 #endif // WX_PRECOMP
34
35 // ----------------------------------------------------------------------------
36 // event table
37 // ----------------------------------------------------------------------------
38
39 BEGIN_EVENT_TABLE(wxTopLevelWindowBase, wxWindow)
40 EVT_CLOSE(wxTopLevelWindowBase::OnCloseWindow)
41 EVT_SIZE(wxTopLevelWindowBase::OnSize)
42 END_EVENT_TABLE()
43
44 // ============================================================================
45 // implementation
46 // ============================================================================
47
48 // FIXME: some platforms don't have wxTopLevelWindow yet
49 #ifdef wxTopLevelWindowNative
50 IMPLEMENT_DYNAMIC_CLASS(wxTopLevelWindow, wxWindow)
51 #endif
52
53 // ----------------------------------------------------------------------------
54 // construction/destruction
55 // ----------------------------------------------------------------------------
56
57 wxTopLevelWindowBase::wxTopLevelWindowBase()
58 {
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 return TRUE;
86 }
87
88 /* static */
89 bool wxTopLevelWindowBase::IsLastBeforeExit()
90 {
91 // we exit the application if there are no more top level windows left
92 // normally but wxApp can prevent this from happening
93 return (wxTopLevelWindows.GetCount() == 1) &&
94 wxTheApp && wxTheApp->GetExitOnFrameDelete();
95 }
96
97 // ----------------------------------------------------------------------------
98 // wxTopLevelWindow geometry
99 // ----------------------------------------------------------------------------
100
101 wxSize wxTopLevelWindowBase::GetMaxSize() const
102 {
103 wxSize size( GetMaxWidth(), GetMaxHeight() );
104 int w, h;
105
106 wxClientDisplayRect( 0, 0, &w, &h );
107
108 if( size.GetWidth() == -1 )
109 size.SetWidth( w );
110
111 if( size.GetHeight() == -1 )
112 size.SetHeight( h );
113
114 return size;
115 }
116
117 // ----------------------------------------------------------------------------
118 // wxTopLevelWindow size management: we exclude the areas taken by
119 // menu/status/toolbars from the client area, so the client area is what's
120 // really available for the frame contents
121 // ----------------------------------------------------------------------------
122
123 void wxTopLevelWindowBase::DoScreenToClient(int *x, int *y) const
124 {
125 wxWindow::DoScreenToClient(x, y);
126
127 // translate the wxWindow client coords to our client coords
128 wxPoint pt(GetClientAreaOrigin());
129 if ( x )
130 *x -= pt.x;
131 if ( y )
132 *y -= pt.y;
133 }
134
135 void wxTopLevelWindowBase::DoClientToScreen(int *x, int *y) const
136 {
137 // our client area origin (0, 0) may be really something like (0, 30) for
138 // wxWindow if we have a toolbar, account for it before translating
139 wxPoint pt(GetClientAreaOrigin());
140 if ( x )
141 *x += pt.x;
142 if ( y )
143 *y += pt.y;
144
145 wxWindow::DoClientToScreen(x, y);
146 }
147
148
149 // ----------------------------------------------------------------------------
150 // event handlers
151 // ----------------------------------------------------------------------------
152
153 // default resizing behaviour - if only ONE subwindow, resize to fill the
154 // whole client area
155 void wxTopLevelWindowBase::OnSize(wxSizeEvent& WXUNUSED(event))
156 {
157 // if we're using constraints or sizers - do use them
158 if ( GetAutoLayout() )
159 {
160 Layout();
161 }
162 else
163 {
164 // do we have _exactly_ one child?
165 wxWindow *child = (wxWindow *)NULL;
166 for ( wxWindowList::Node *node = GetChildren().GetFirst();
167 node;
168 node = node->GetNext() )
169 {
170 wxWindow *win = node->GetData();
171
172 // exclude top level and managed windows (status bar isn't
173 // currently in the children list except under wxMac anyhow, but
174 // it makes no harm to test for it)
175 if ( !win->IsTopLevel() && !IsOneOfBars(win) )
176 {
177 if ( child )
178 {
179 return; // it's our second subwindow - nothing to do
180 }
181
182 child = win;
183 }
184 }
185
186 // do we have any children at all?
187 if ( child )
188 {
189 // exactly one child - set it's size to fill the whole frame
190 int clientW, clientH;
191 DoGetClientSize(&clientW, &clientH);
192
193 // for whatever reasons, wxGTK wants to have a small offset - it
194 // probably looks better with it?
195 #ifdef __WXGTK__
196 static const int ofs = 1;
197 #else
198 static const int ofs = 0;
199 #endif
200
201 child->SetSize(ofs, ofs, clientW - 2*ofs, clientH - 2*ofs);
202 }
203 }
204 }
205
206 // The default implementation for the close window event.
207 void wxTopLevelWindowBase::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
208 {
209 Destroy();
210 }
211
212 bool wxTopLevelWindowBase::SendIconizeEvent(bool iconized)
213 {
214 wxIconizeEvent event(GetId(), iconized);
215 event.SetEventObject(this);
216
217 return GetEventHandler()->ProcessEvent(event);
218 }
219
220 // vi:sts=4:sw=4:et