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