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