]> git.saurik.com Git - wxWidgets.git/blame - src/common/toplvcmn.cpp
create text control with wxTE_PROCESS_ENTER style if we have wxPROCESS_ENTER ourselve...
[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;
7d9f12f3
VS
58}
59
799ea011
GD
60wxTopLevelWindowBase::~wxTopLevelWindowBase()
61{
1cbee0b4
VZ
62 // don't let wxTheApp keep any stale pointers to us
63 if ( wxTheApp && wxTheApp->GetTopWindow() == this )
64 wxTheApp->SetTopWindow(NULL);
65
1cbee0b4 66 wxTopLevelWindows.DeleteObject(this);
cb719f2e 67
65afac3f 68 if ( IsLastBeforeExit() )
1cbee0b4 69 {
65afac3f 70 // no other (important) windows left, quit the app
1cbee0b4
VZ
71 wxTheApp->ExitMainLoop();
72 }
799ea011
GD
73}
74
7d9f12f3
VS
75bool wxTopLevelWindowBase::Destroy()
76{
77 // delayed destruction: the frame will be deleted during the next idle
78 // loop iteration
79 if ( !wxPendingDelete.Member(this) )
80 wxPendingDelete.Append(this);
81
b3bd912d
RR
82 if (wxTopLevelWindows.GetCount() > 1)
83 {
84 // Hide it immediately. This should
85 // not be done if this TLW is the
86 // only one left since we then would
87 // risk not to get any idle events
cb719f2e 88 // at all anymore during which we
b3bd912d
RR
89 // could delete any pending events.
90 Hide();
91 }
cafcf62a 92
cb719f2e 93 return true;
7d9f12f3
VS
94}
95
5c363878 96bool wxTopLevelWindowBase::IsLastBeforeExit() const
1cbee0b4 97{
65afac3f
VZ
98 // first of all, automatically exiting the app on last window close can be
99 // completely disabled at wxTheApp level
100 if ( !wxTheApp || !wxTheApp->GetExitOnFrameDelete() )
101 return false;
102
103 wxWindowList::const_iterator i;
104 const wxWindowList::const_iterator end = wxTopLevelWindows.end();
105
106 // then decide whether we should exit at all
107 for ( i = wxTopLevelWindows.begin(); i != end; ++i )
108 {
109 wxTopLevelWindow * const win = wx_static_cast(wxTopLevelWindow *, *i);
110 if ( win->ShouldPreventAppExit() )
111 {
112 // there remains at least one important TLW, don't exit
113 return false;
114 }
115 }
116
117 // if yes, close all the other windows: this could still fail
118 for ( i = wxTopLevelWindows.begin(); i != end; ++i )
119 {
120 // don't close twice the windows which are already marked for deletion
121 wxTopLevelWindow * const win = wx_static_cast(wxTopLevelWindow *, *i);
122 if ( !wxPendingDelete.Member(win) && !win->Close() )
123 {
124 // one of the windows refused to close, don't exit
125 //
126 // NB: of course, by now some other windows could have been already
127 // closed but there is really nothing we can do about it as we
128 // have no way just to ask the window if it can close without
129 // forcing it to do it
130 return false;
131 }
132 }
133
134 return true;
1cbee0b4
VZ
135}
136
137// ----------------------------------------------------------------------------
138// wxTopLevelWindow geometry
139// ----------------------------------------------------------------------------
140
0bba37f5
DE
141void wxTopLevelWindowBase::GetRectForTopLevelChildren(int *x, int *y, int *w, int *h)
142{
143 GetPosition(x,y);
144 GetSize(w,h);
145}
146
34c3ffca
RL
147wxSize wxTopLevelWindowBase::GetMaxSize() const
148{
149 wxSize size( GetMaxWidth(), GetMaxHeight() );
150 int w, h;
151
152 wxClientDisplayRect( 0, 0, &w, &h );
153
cb719f2e 154 if( size.GetWidth() == wxDefaultCoord )
34c3ffca
RL
155 size.SetWidth( w );
156
cb719f2e 157 if( size.GetHeight() == wxDefaultCoord )
34c3ffca
RL
158 size.SetHeight( h );
159
160 return size;
161}
162
0c089c08
VZ
163/* static */
164wxSize wxTopLevelWindowBase::GetDefaultSize()
165{
166 wxSize size = wxGetClientDisplayRect().GetSize();
167
168 // create proportionally bigger windows on small screens
169 if ( size.x >= 1024 )
170 size.x = 400;
171 else if ( size.x >= 800 )
172 size.x = 300;
173 else if ( size.x >= 320 )
174 size.x = 240;
175
176 if ( size.y >= 768 )
177 size.y = 250;
178 else if ( size.y > 200 )
179 {
180 size.y *= 2;
181 size.y /= 3;
182 }
183
184 return size;
185}
186
1f464296
VZ
187void wxTopLevelWindowBase::DoCentre(int dir)
188{
979a0320
WS
189 // on some platforms centering top level windows is impossible
190 // because they are always maximized by guidelines or limitations
191 if(IsAlwaysMaximized())
192 return;
193
262a1536
VZ
194 // we need the display rect anyhow so store it first
195 int nDisplay = wxDisplay::GetFromWindow(this);
196 wxDisplay dpy(nDisplay == wxNOT_FOUND ? 0 : nDisplay);
197 const wxRect rectDisplay(dpy.GetClientArea());
198
199 // what should we centre this window on?
200 wxRect rectParent;
1f464296
VZ
201 if ( !(dir & wxCENTRE_ON_SCREEN) && GetParent() )
202 {
5a63a0f1
VZ
203 // centre on parent window: notice that we need screen coordinates for
204 // positioning this TLW
262a1536
VZ
205 rectParent = GetParent()->GetScreenRect();
206
207 // if the parent is entirely off screen (happens at least with MDI
208 // parent frame under Mac but could happen elsewhere too if the frame
209 // was hidden/moved away for some reason), don't use it as otherwise
210 // this window wouldn't be visible at all
211 if ( !rectDisplay.Inside(rectParent.GetTopLeft()) &&
212 !rectParent.Inside(rectParent.GetBottomRight()) )
213 {
214 // this is enough to make IsEmpty() test below pass
215 rectParent.width = 0;
216 }
1f464296 217 }
262a1536
VZ
218
219 if ( rectParent.IsEmpty() )
1f464296
VZ
220 {
221 // we were explicitely asked to centre this window on the entire screen
222 // or if we have no parent anyhow and so can't centre on it
262a1536
VZ
223 rectParent = rectDisplay;
224 }
225
cca1624d
WS
226 // centering maximized window on screen is no-op
227 if((rectParent == rectDisplay) && IsMaximized())
228 return;
229
262a1536
VZ
230 // the new window rect candidate
231 wxRect rect = GetRect().CentreIn(rectParent, dir);
232
233 // we don't want to place the window off screen if Centre() is called as
234 // this is (almost?) never wanted and it would be very difficult to prevent
235 // it from happening from the user code if we didn't check for it here
236 if ( rectDisplay.Inside(rect.GetTopLeft()) )
237 {
238 if ( !rectDisplay.Inside(rect.GetBottomRight()) )
1f464296 239 {
262a1536
VZ
240 // check if we can move the window so that the bottom right corner
241 // is visible without hiding the top left one
242 int dx = rectDisplay.GetRight() - rect.GetRight();
243 int dy = rectDisplay.GetBottom() - rect.GetBottom();
244 rect.Offset(dx, dy);
1f464296 245 }
262a1536
VZ
246 //else: the window top left and bottom right corner are both visible,
247 // although the window might still be not entirely on screen (with
248 // 2 staggered displays for example) we wouldn't be able to
249 // improve the layout much in such case, so just leave it as is
250 }
251 else // make top left corner visible
252 {
253 if ( rect.x < rectDisplay.x )
254 rect.x = rectDisplay.x;
255
256 if ( rect.y < rectDisplay.y )
257 rect.y = rectDisplay.y;
1f464296
VZ
258 }
259
262a1536
VZ
260 // -1 could be valid coordinate here if there are several displays
261 SetSize(rect, wxSIZE_ALLOW_MINUS_ONE);
1f464296
VZ
262}
263
7d9f12f3 264// ----------------------------------------------------------------------------
82c9f85c
VZ
265// wxTopLevelWindow size management: we exclude the areas taken by
266// menu/status/toolbars from the client area, so the client area is what's
267// really available for the frame contents
7d9f12f3
VS
268// ----------------------------------------------------------------------------
269
270void wxTopLevelWindowBase::DoScreenToClient(int *x, int *y) const
271{
272 wxWindow::DoScreenToClient(x, y);
273
82c9f85c 274 // translate the wxWindow client coords to our client coords
7d9f12f3 275 wxPoint pt(GetClientAreaOrigin());
82c9f85c
VZ
276 if ( x )
277 *x -= pt.x;
278 if ( y )
279 *y -= pt.y;
7d9f12f3
VS
280}
281
282void wxTopLevelWindowBase::DoClientToScreen(int *x, int *y) const
283{
82c9f85c
VZ
284 // our client area origin (0, 0) may be really something like (0, 30) for
285 // wxWindow if we have a toolbar, account for it before translating
286 wxPoint pt(GetClientAreaOrigin());
287 if ( x )
288 *x += pt.x;
289 if ( y )
290 *y += pt.y;
7d9f12f3
VS
291
292 wxWindow::DoClientToScreen(x, y);
293}
294
979a0320
WS
295bool wxTopLevelWindowBase::IsAlwaysMaximized() const
296{
297#if defined(__SMARTPHONE__) || defined(__POCKETPC__)
298 return true;
299#else
300 return false;
301#endif
302}
7d9f12f3
VS
303
304// ----------------------------------------------------------------------------
305// event handlers
306// ----------------------------------------------------------------------------
307
308// default resizing behaviour - if only ONE subwindow, resize to fill the
309// whole client area
5e62d4a5 310void wxTopLevelWindowBase::DoLayout()
7d9f12f3 311{
bc55104d 312 // if we're using constraints or sizers - do use them
7d9f12f3
VS
313 if ( GetAutoLayout() )
314 {
315 Layout();
316 }
317 else
7d9f12f3
VS
318 {
319 // do we have _exactly_ one child?
320 wxWindow *child = (wxWindow *)NULL;
222ed1d6 321 for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
7d9f12f3
VS
322 node;
323 node = node->GetNext() )
324 {
325 wxWindow *win = node->GetData();
326
327 // exclude top level and managed windows (status bar isn't
328 // currently in the children list except under wxMac anyhow, but
329 // it makes no harm to test for it)
330 if ( !win->IsTopLevel() && !IsOneOfBars(win) )
331 {
332 if ( child )
333 {
334 return; // it's our second subwindow - nothing to do
335 }
336
337 child = win;
338 }
339 }
340
341 // do we have any children at all?
d3cd1c03 342 if ( child && child->IsShown() )
7d9f12f3
VS
343 {
344 // exactly one child - set it's size to fill the whole frame
345 int clientW, clientH;
346 DoGetClientSize(&clientW, &clientH);
347
348 // for whatever reasons, wxGTK wants to have a small offset - it
349 // probably looks better with it?
350#ifdef __WXGTK__
351 static const int ofs = 1;
352#else
353 static const int ofs = 0;
354#endif
355
45e0dc94 356 child->SetSize(ofs, ofs, clientW - 2*ofs, clientH - 2*ofs);
7d9f12f3
VS
357 }
358 }
359}
360
361// The default implementation for the close window event.
362void wxTopLevelWindowBase::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
363{
364 Destroy();
365}
366
367bool wxTopLevelWindowBase::SendIconizeEvent(bool iconized)
368{
369 wxIconizeEvent event(GetId(), iconized);
370 event.SetEventObject(this);
371
372 return GetEventHandler()->ProcessEvent(event);
373}
34c3ffca 374
e39af974
JS
375// do the window-specific processing after processing the update event
376void wxTopLevelWindowBase::DoUpdateWindowUI(wxUpdateUIEvent& event)
377{
a3a4105d
VZ
378 // call inherited, but skip the wxControl's version, and call directly the
379 // wxWindow's one instead, because the only reason why we are overriding this
380 // function is that we want to use SetTitle() instead of wxControl::SetLabel()
381 wxWindowBase::DoUpdateWindowUI(event);
cb719f2e 382
a3a4105d 383 // update title
e39af974
JS
384 if ( event.GetSetText() )
385 {
386 if ( event.GetText() != GetTitle() )
387 SetTitle(event.GetText());
388 }
389}
390
447fd332 391void wxTopLevelWindowBase::RequestUserAttention(int WXUNUSED(flags))
9b59c95b
VZ
392{
393 // it's probably better than do nothing, isn't it?
394 Raise();
395}