]> git.saurik.com Git - wxWidgets.git/blob - src/common/toplvcmn.cpp
Halfway reasonable implementation of wxFont for wxCocoa.
[wxWidgets.git] / src / common / toplvcmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/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 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #include "wx/toplevel.h"
27
28 #ifndef WX_PRECOMP
29 #include "wx/dcclient.h"
30 #include "wx/app.h"
31 #endif // WX_PRECOMP
32
33 #include "wx/display.h"
34
35 // ----------------------------------------------------------------------------
36 // event table
37 // ----------------------------------------------------------------------------
38
39 BEGIN_EVENT_TABLE(wxTopLevelWindowBase, wxWindow)
40 EVT_CLOSE(wxTopLevelWindowBase::OnCloseWindow)
41 EVT_SIZE(wxTopLevelWindowBase::OnSize)
42 EVT_WINDOW_DESTROY(wxTopLevelWindowBase::OnChildDestroy)
43 WX_EVENT_TABLE_CONTROL_CONTAINER(wxTopLevelWindowBase)
44 END_EVENT_TABLE()
45
46 WX_DELEGATE_TO_CONTROL_CONTAINER(wxTopLevelWindowBase, wxWindow)
47
48 // ============================================================================
49 // implementation
50 // ============================================================================
51
52 IMPLEMENT_ABSTRACT_CLASS(wxTopLevelWindow, wxWindow)
53
54 // ----------------------------------------------------------------------------
55 // construction/destruction
56 // ----------------------------------------------------------------------------
57
58 wxTopLevelWindowBase::wxTopLevelWindowBase()
59 {
60 // Unlike windows, top level windows are created hidden by default.
61 m_isShown = false;
62 m_winDefault =
63 m_winTmpDefault = NULL;
64
65 WX_INIT_CONTROL_CONTAINER();
66 }
67
68 wxTopLevelWindowBase::~wxTopLevelWindowBase()
69 {
70 m_winDefault =
71 m_winTmpDefault = NULL;
72
73 // don't let wxTheApp keep any stale pointers to us
74 if ( wxTheApp && wxTheApp->GetTopWindow() == this )
75 wxTheApp->SetTopWindow(NULL);
76
77 wxTopLevelWindows.DeleteObject(this);
78
79 // delete any our top level children which are still pending for deletion
80 // immediately: this could happen if a child (e.g. a temporary dialog
81 // created with this window as parent) was Destroy()'d) while this window
82 // was deleted directly (with delete, or maybe just because it was created
83 // on the stack) immediately afterwards and before the child TLW was really
84 // destroyed -- not destroying it now would leave it alive with a dangling
85 // parent pointer and result in a crash later
86 for ( wxObjectList::iterator i = wxPendingDelete.begin();
87 i != wxPendingDelete.end();
88 )
89 {
90 wxWindow * const win = wxDynamicCast(*i, wxWindow);
91 if ( win && win->GetParent() == this )
92 {
93 wxPendingDelete.erase(i);
94
95 delete win;
96
97 // deleting it invalidated the list (and not only one node because
98 // it could have resulted in deletion of other objects to)
99 i = wxPendingDelete.begin();
100 }
101 else
102 {
103 ++i;
104 }
105 }
106
107 if ( IsLastBeforeExit() )
108 {
109 // no other (important) windows left, quit the app
110 wxTheApp->ExitMainLoop();
111 }
112 }
113
114 bool wxTopLevelWindowBase::Destroy()
115 {
116 // delayed destruction: the frame will be deleted during the next idle
117 // loop iteration
118 if ( !wxPendingDelete.Member(this) )
119 wxPendingDelete.Append(this);
120
121 if (wxTopLevelWindows.GetCount() > 1)
122 {
123 // Hide it immediately. This should
124 // not be done if this TLW is the
125 // only one left since we then would
126 // risk not to get any idle events
127 // at all anymore during which we
128 // could delete any pending events.
129 Hide();
130 }
131
132 return true;
133 }
134
135 bool wxTopLevelWindowBase::IsLastBeforeExit() const
136 {
137 // first of all, automatically exiting the app on last window close can be
138 // completely disabled at wxTheApp level
139 if ( !wxTheApp || !wxTheApp->GetExitOnFrameDelete() )
140 return false;
141
142 wxWindowList::const_iterator i;
143 const wxWindowList::const_iterator end = wxTopLevelWindows.end();
144
145 // then decide whether we should exit at all
146 for ( i = wxTopLevelWindows.begin(); i != end; ++i )
147 {
148 wxTopLevelWindow * const win = wx_static_cast(wxTopLevelWindow *, *i);
149 if ( win->ShouldPreventAppExit() )
150 {
151 // there remains at least one important TLW, don't exit
152 return false;
153 }
154 }
155
156 // if yes, close all the other windows: this could still fail
157 for ( i = wxTopLevelWindows.begin(); i != end; ++i )
158 {
159 // don't close twice the windows which are already marked for deletion
160 wxTopLevelWindow * const win = wx_static_cast(wxTopLevelWindow *, *i);
161 if ( !wxPendingDelete.Member(win) && !win->Close() )
162 {
163 // one of the windows refused to close, don't exit
164 //
165 // NB: of course, by now some other windows could have been already
166 // closed but there is really nothing we can do about it as we
167 // have no way just to ask the window if it can close without
168 // forcing it to do it
169 return false;
170 }
171 }
172
173 return true;
174 }
175
176 // ----------------------------------------------------------------------------
177 // wxTopLevelWindow geometry
178 // ----------------------------------------------------------------------------
179
180 void wxTopLevelWindowBase::SetMinSize(const wxSize& minSize)
181 {
182 SetSizeHints(minSize, GetMaxSize());
183 }
184
185 void wxTopLevelWindowBase::SetMaxSize(const wxSize& maxSize)
186 {
187 SetSizeHints(GetMinSize(), maxSize);
188 }
189
190 void wxTopLevelWindowBase::GetRectForTopLevelChildren(int *x, int *y, int *w, int *h)
191 {
192 GetPosition(x,y);
193 GetSize(w,h);
194 }
195
196 /* static */
197 wxSize wxTopLevelWindowBase::GetDefaultSize()
198 {
199 wxSize size = wxGetClientDisplayRect().GetSize();
200
201 // create proportionally bigger windows on small screens
202 if ( size.x >= 1024 )
203 size.x = 400;
204 else if ( size.x >= 800 )
205 size.x = 300;
206 else if ( size.x >= 320 )
207 size.x = 240;
208
209 if ( size.y >= 768 )
210 size.y = 250;
211 else if ( size.y > 200 )
212 {
213 size.y *= 2;
214 size.y /= 3;
215 }
216
217 return size;
218 }
219
220 void wxTopLevelWindowBase::DoCentre(int dir)
221 {
222 // on some platforms centering top level windows is impossible
223 // because they are always maximized by guidelines or limitations
224 if(IsAlwaysMaximized())
225 return;
226
227 // we need the display rect anyhow so store it first: notice that we should
228 // be centered on the same display as our parent window, the display of
229 // this window itself is not really defined yet
230 int nDisplay = wxDisplay::GetFromWindow(GetParent() ? GetParent() : this);
231 wxDisplay dpy(nDisplay == wxNOT_FOUND ? 0 : nDisplay);
232 const wxRect rectDisplay(dpy.GetClientArea());
233
234 // what should we centre this window on?
235 wxRect rectParent;
236 if ( !(dir & wxCENTRE_ON_SCREEN) && GetParent() )
237 {
238 // centre on parent window: notice that we need screen coordinates for
239 // positioning this TLW
240 rectParent = GetParent()->GetScreenRect();
241
242 // if the parent is entirely off screen (happens at least with MDI
243 // parent frame under Mac but could happen elsewhere too if the frame
244 // was hidden/moved away for some reason), don't use it as otherwise
245 // this window wouldn't be visible at all
246 if ( !rectDisplay.Contains(rectParent.GetTopLeft()) &&
247 !rectParent.Contains(rectParent.GetBottomRight()) )
248 {
249 // this is enough to make IsEmpty() test below pass
250 rectParent.width = 0;
251 }
252 }
253
254 if ( rectParent.IsEmpty() )
255 {
256 // we were explicitly asked to centre this window on the entire screen
257 // or if we have no parent anyhow and so can't centre on it
258 rectParent = rectDisplay;
259 }
260
261 // centering maximized window on screen is no-op
262 if((rectParent == rectDisplay) && IsMaximized())
263 return;
264
265 // the new window rect candidate
266 wxRect rect = GetRect().CentreIn(rectParent, dir);
267
268 // we don't want to place the window off screen if Centre() is called as
269 // this is (almost?) never wanted and it would be very difficult to prevent
270 // it from happening from the user code if we didn't check for it here
271 if ( !rectDisplay.Contains(rect.GetTopLeft()) )
272 {
273 // move the window just enough to make the corner visible
274 int dx = rectDisplay.GetLeft() - rect.GetLeft();
275 int dy = rectDisplay.GetTop() - rect.GetTop();
276 rect.Offset(dx > 0 ? dx : 0, dy > 0 ? dy : 0);
277 }
278
279 if ( !rectDisplay.Contains(rect.GetBottomRight()) )
280 {
281 // do the same for this corner too
282 int dx = rectDisplay.GetRight() - rect.GetRight();
283 int dy = rectDisplay.GetBottom() - rect.GetBottom();
284 rect.Offset(dx < 0 ? dx : 0, dy < 0 ? dy : 0);
285 }
286
287 // the window top left and bottom right corner are both visible now and
288 // although the window might still be not entirely on screen (with 2
289 // staggered displays for example) we wouldn't be able to improve the
290 // layout much in such case, so we stop here
291
292 // -1 could be valid coordinate here if there are several displays
293 SetSize(rect, wxSIZE_ALLOW_MINUS_ONE);
294 }
295
296 // ----------------------------------------------------------------------------
297 // wxTopLevelWindow size management: we exclude the areas taken by
298 // menu/status/toolbars from the client area, so the client area is what's
299 // really available for the frame contents
300 // ----------------------------------------------------------------------------
301
302 void wxTopLevelWindowBase::DoScreenToClient(int *x, int *y) const
303 {
304 wxWindow::DoScreenToClient(x, y);
305
306 // translate the wxWindow client coords to our client coords
307 wxPoint pt(GetClientAreaOrigin());
308 if ( x )
309 *x -= pt.x;
310 if ( y )
311 *y -= pt.y;
312 }
313
314 void wxTopLevelWindowBase::DoClientToScreen(int *x, int *y) const
315 {
316 // our client area origin (0, 0) may be really something like (0, 30) for
317 // wxWindow if we have a toolbar, account for it before translating
318 wxPoint pt(GetClientAreaOrigin());
319 if ( x )
320 *x += pt.x;
321 if ( y )
322 *y += pt.y;
323
324 wxWindow::DoClientToScreen(x, y);
325 }
326
327 bool wxTopLevelWindowBase::IsAlwaysMaximized() const
328 {
329 #if defined(__SMARTPHONE__) || defined(__POCKETPC__)
330 return true;
331 #else
332 return false;
333 #endif
334 }
335
336 // ----------------------------------------------------------------------------
337 // icons
338 // ----------------------------------------------------------------------------
339
340 wxIcon wxTopLevelWindowBase::GetIcon() const
341 {
342 return m_icons.IsEmpty() ? wxIcon() : m_icons.GetIcon( -1 );
343 }
344
345 void wxTopLevelWindowBase::SetIcon(const wxIcon& icon)
346 {
347 // passing wxNullIcon to SetIcon() is possible (it means that we shouldn't
348 // have any icon), but adding an invalid icon to wxIconBundle is not
349 wxIconBundle icons;
350 if ( icon.Ok() )
351 icons.AddIcon(icon);
352
353 SetIcons(icons);
354 }
355
356 // ----------------------------------------------------------------------------
357 // event handlers
358 // ----------------------------------------------------------------------------
359
360 // default resizing behaviour - if only ONE subwindow, resize to fill the
361 // whole client area
362 void wxTopLevelWindowBase::DoLayout()
363 {
364 // if we're using constraints or sizers - do use them
365 if ( GetAutoLayout() )
366 {
367 Layout();
368 }
369 else
370 {
371 // do we have _exactly_ one child?
372 wxWindow *child = (wxWindow *)NULL;
373 for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
374 node;
375 node = node->GetNext() )
376 {
377 wxWindow *win = node->GetData();
378
379 // exclude top level and managed windows (status bar isn't
380 // currently in the children list except under wxMac anyhow, but
381 // it makes no harm to test for it)
382 if ( !win->IsTopLevel() && !IsOneOfBars(win) )
383 {
384 if ( child )
385 {
386 return; // it's our second subwindow - nothing to do
387 }
388
389 child = win;
390 }
391 }
392
393 // do we have any children at all?
394 if ( child && child->IsShown() )
395 {
396 // exactly one child - set it's size to fill the whole frame
397 int clientW, clientH;
398 DoGetClientSize(&clientW, &clientH);
399
400 child->SetSize(0, 0, clientW, clientH);
401 }
402 }
403 }
404
405 // The default implementation for the close window event.
406 void wxTopLevelWindowBase::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
407 {
408 Destroy();
409 }
410
411 void wxTopLevelWindowBase::OnChildDestroy(wxWindowDestroyEvent& event)
412 {
413 event.Skip();
414
415 wxWindow * const win = event.GetWindow();
416 if ( win == m_winDefault )
417 m_winDefault = NULL;
418 if ( win == m_winTmpDefault )
419 m_winTmpDefault = NULL;
420 }
421
422 bool wxTopLevelWindowBase::SendIconizeEvent(bool iconized)
423 {
424 wxIconizeEvent event(GetId(), iconized);
425 event.SetEventObject(this);
426
427 return GetEventHandler()->ProcessEvent(event);
428 }
429
430 // do the window-specific processing after processing the update event
431 void wxTopLevelWindowBase::DoUpdateWindowUI(wxUpdateUIEvent& event)
432 {
433 // call inherited, but skip the wxControl's version, and call directly the
434 // wxWindow's one instead, because the only reason why we are overriding this
435 // function is that we want to use SetTitle() instead of wxControl::SetLabel()
436 wxWindowBase::DoUpdateWindowUI(event);
437
438 // update title
439 if ( event.GetSetText() )
440 {
441 if ( event.GetText() != GetTitle() )
442 SetTitle(event.GetText());
443 }
444 }
445
446 void wxTopLevelWindowBase::RequestUserAttention(int WXUNUSED(flags))
447 {
448 // it's probably better than do nothing, isn't it?
449 Raise();
450 }