]> git.saurik.com Git - wxWidgets.git/blame - src/common/containr.cpp
compilation warning fixes (patch 625810)
[wxWidgets.git] / src / common / containr.cpp
CommitLineData
456bc6d9
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: src/common/containr.cpp
3// Purpose: implementation of wxControlContainer
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 06.08.01
7// RCS-ID: $Id$
8// Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
55d99c7a 9// License: wxWindows licence
456bc6d9
VZ
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20#ifdef __GNUG__
21 #pragma implementation "containr.h"
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
28 #pragma hdrstop
29#endif
30
31#ifndef WX_PRECOMP
6285be72
VZ
32 #include "wx/log.h"
33 #include "wx/event.h"
34 #include "wx/window.h"
456bc6d9
VZ
35#endif //WX_PRECOMP
36
37#include "wx/containr.h"
38
76158d52
SC
39#ifdef __WXMAC__
40 #include "wx/scrolbar.h"
41#endif
42
456bc6d9
VZ
43// ============================================================================
44// implementation
45// ============================================================================
46
47wxControlContainer::wxControlContainer(wxWindow *winParent)
48{
49 m_winParent = winParent;
50
51 m_winLastFocused =
036da5e3 52 m_winTmpDefault =
456bc6d9
VZ
53 m_winDefault = NULL;
54}
55
3251b834
VZ
56bool wxControlContainer::AcceptsFocus() const
57{
a7407834 58 // if we're not shown or disabled, we can't accept focus
3251b834
VZ
59 if ( m_winParent->IsShown() && m_winParent->IsEnabled() )
60 {
a7407834
VZ
61 // otherwise we can accept focus either if we have no children at all
62 // (in this case we're probably not used as a container) or only when
63 // at least one child will accept focus
3251b834 64 wxWindowList::Node *node = m_winParent->GetChildren().GetFirst();
a7407834
VZ
65 if ( !node )
66 return TRUE;
67
2f64c3bb
SC
68#ifdef __WXMAC__
69 // wxMac has eventually the two scrollbars as children, they don't count
70 // as real children in the algorithm mentioned above
71 bool hasRealChildren = false ;
72#endif
73
3251b834
VZ
74 while ( node )
75 {
76 wxWindow *child = node->GetData();
77
78 if ( child->AcceptsFocus() )
79 {
80 return TRUE;
81 }
82
2f64c3bb
SC
83#ifdef __WXMAC__
84 wxScrollBar *sb = wxDynamicCast( child , wxScrollBar ) ;
85 if ( sb == NULL || !m_winParent->MacIsWindowScrollbar( sb ) )
86 hasRealChildren = true ;
87#endif
3251b834
VZ
88 node = node->GetNext();
89 }
2f64c3bb
SC
90
91#ifdef __WXMAC__
92 if ( !hasRealChildren )
93 return TRUE ;
94#endif
3251b834
VZ
95 }
96
97 return FALSE;
98}
99
456bc6d9
VZ
100void wxControlContainer::SetLastFocus(wxWindow *win)
101{
6aeb6f2a
VZ
102 // the panel itself should never get the focus at all but if it does happen
103 // temporarily (as it seems to do under wxGTK), at the very least don't
104 // forget our previous m_winLastFocused
c25b5d1f 105 if ( win != m_winParent )
456bc6d9 106 {
c25b5d1f
VZ
107 // if we're setting the focus
108 if ( win )
83c865f5 109 {
c25b5d1f
VZ
110 // find the last _immediate_ child which got focus
111 wxWindow *winParent = win;
112 while ( winParent != m_winParent )
113 {
114 win = winParent;
115 winParent = win->GetParent();
83c865f5 116
c25b5d1f
VZ
117 // Yes, this can happen, though in a totally pathological case.
118 // like when detaching a menubar from a frame with a child
119 // which has pushed itself as an event handler for the menubar.
120 // (under wxGTK)
6f8239de 121
c25b5d1f
VZ
122 wxASSERT_MSG( winParent,
123 _T("Setting last focus for a window that is not our child?") );
124 }
6f8239de 125 }
456bc6d9 126
c25b5d1f 127 m_winLastFocused = win;
6aeb6f2a 128
c25b5d1f
VZ
129 if ( win )
130 {
131 wxLogTrace(_T("focus"), _T("Set last focus to %s(%s)"),
132 win->GetClassInfo()->GetClassName(),
133 win->GetLabel().c_str());
134 }
135 else
136 {
137 wxLogTrace(_T("focus"), _T("No more last focus"));
138 }
6aeb6f2a 139 }
c25b5d1f
VZ
140
141 // propagate the last focus upwards so that our parent can set focus back
142 // to us if it loses it now and regains later
143 wxWindow *parent = m_winParent->GetParent();
144 if ( parent )
6aeb6f2a 145 {
c25b5d1f
VZ
146 wxChildFocusEvent eventFocus(m_winParent);
147 parent->GetEventHandler()->ProcessEvent(eventFocus);
6aeb6f2a 148 }
456bc6d9
VZ
149}
150
151// ----------------------------------------------------------------------------
152// Keyboard handling - this is the place where the TAB traversal logic is
153// implemented. As this code is common to all ports, this ensures consistent
154// behaviour even if we don't specify how exactly the wxNavigationKeyEvent are
155// generated and this is done in platform specific code which also ensures that
156// we can follow the given platform standards.
157// ----------------------------------------------------------------------------
158
159void wxControlContainer::HandleOnNavigationKey( wxNavigationKeyEvent& event )
160{
161 wxWindow *parent = m_winParent->GetParent();
162
163 // the event is propagated downwards if the event emitter was our parent
164 bool goingDown = event.GetEventObject() == parent;
165
166 const wxWindowList& children = m_winParent->GetChildren();
167
168 // there is not much to do if we don't have children and we're not
169 // interested in "notebook page change" events here
170 if ( !children.GetCount() || event.IsWindowChange() )
171 {
172 // let the parent process it unless it already comes from our parent
173 // of we don't have any
174 if ( goingDown ||
175 !parent || !parent->GetEventHandler()->ProcessEvent(event) )
176 {
177 event.Skip();
178 }
179
180 return;
181 }
182
183 // where are we going?
184 bool forward = event.GetDirection();
185
186 // the node of the children list from which we should start looking for the
187 // next acceptable child
188 wxWindowList::Node *node, *start_node;
189
190 // we should start from the first/last control and not from the one which
191 // had focus the last time if we're propagating the event downwards because
192 // for our parent we look like a single control
193 if ( goingDown )
194 {
195 // just to be sure it's not used (normally this is not necessary, but
196 // doesn't hurt neither)
197 m_winLastFocused = (wxWindow *)NULL;
198
199 // start from first or last depending on where we're going
200 node = forward ? children.GetFirst() : children.GetLast();
201
202 // we want to cycle over all nodes
203 start_node = (wxWindowList::Node *)NULL;
204 }
205 else
206 {
207 // try to find the child which has the focus currently
208
209 // the event emitter might have done this for us
210 wxWindow *winFocus = event.GetCurrentFocus();
211
212 // but if not, we might know where the focus was ourselves
213 if (!winFocus)
214 winFocus = m_winLastFocused;
215
216 // if still no luck, do it the hard way
217 if (!winFocus)
218 winFocus = wxWindow::FindFocus();
219
220 if ( winFocus )
221 {
222 // ok, we found the focus - now is it our child?
223 start_node = children.Find( winFocus );
224 }
225 else
226 {
227 start_node = (wxWindowList::Node *)NULL;
228 }
229
230 if ( !start_node && m_winLastFocused )
231 {
232 // window which has focus isn't our child, fall back to the one
233 // which had the focus the last time
234 start_node = children.Find( m_winLastFocused );
235 }
236
237 // if we still didn't find anything, we should start with the first one
238 if ( !start_node )
239 {
240 start_node = children.GetFirst();
241 }
242
243 // and the first child which we can try setting focus to is the next or
244 // the previous one
245 node = forward ? start_node->GetNext() : start_node->GetPrevious();
246 }
247
248 // we want to cycle over all elements passing by NULL
249 while ( node != start_node )
250 {
251 // Have we come to the last or first item on the panel?
252 if ( !node )
253 {
254 if ( !goingDown )
255 {
256 // Check if our (may be grand) parent is another panel: if this
257 // is the case, they will know what to do with this navigation
258 // key and so give them the chance to process it instead of
259 // looping inside this panel (normally, the focus will go to
260 // the next/previous item after this panel in the parent
261 // panel).
262 wxWindow *focussed_child_of_parent = m_winParent;
263 while ( parent )
264 {
265 // we don't want to tab into a different dialog or frame
266 if ( focussed_child_of_parent->IsTopLevel() )
267 break;
268
269 event.SetCurrentFocus( focussed_child_of_parent );
270 if ( parent->GetEventHandler()->ProcessEvent( event ) )
271 return;
272
273 focussed_child_of_parent = parent;
274
275 parent = parent->GetParent();
276 }
277 }
278 //else: as the focus came from our parent, we definitely don't want
279 // to send it back to it!
280
281 // no, we are not inside another panel so process this ourself
282 node = forward ? children.GetFirst() : children.GetLast();
283
284 continue;
285 }
286
287 wxWindow *child = node->GetData();
288
289 if ( child->AcceptsFocusFromKeyboard() )
290 {
291 // if we're setting the focus to a child panel we should prevent it
292 // from giving it to the child which had the focus the last time
293 // and instead give it to the first/last child depending from which
294 // direction we're coming
295 event.SetEventObject(m_winParent);
296 if ( !child->GetEventHandler()->ProcessEvent(event) )
297 {
2b5f62a0
VZ
298 // set it first in case SetFocusFromKbd() results in focus
299 // change too
300 m_winLastFocused = child;
301
456bc6d9 302 // everything is simple: just give focus to it
5463c0a4 303 child->SetFocusFromKbd();
456bc6d9
VZ
304 }
305 //else: the child manages its focus itself
306
307 event.Skip( FALSE );
308
309 return;
310 }
311
312 node = forward ? node->GetNext() : node->GetPrevious();
313 }
314
315 // we cycled through all of our children and none of them wanted to accept
316 // focus
317 event.Skip();
318}
319
320void wxControlContainer::HandleOnWindowDestroy(wxWindowBase *child)
321{
322 if ( child == m_winLastFocused )
323 m_winLastFocused = NULL;
324
325 if ( child == m_winDefault )
326 m_winDefault = NULL;
036da5e3
VZ
327
328 if ( child == m_winTmpDefault )
329 m_winTmpDefault = NULL;
456bc6d9
VZ
330}
331
332// ----------------------------------------------------------------------------
333// focus handling
334// ----------------------------------------------------------------------------
335
24a7a198 336bool wxControlContainer::DoSetFocus()
456bc6d9 337{
993eebf1
VZ
338 wxLogTrace(_T("focus"), _T("SetFocus on wxPanel 0x%08lx."),
339 (unsigned long)m_winParent->GetHandle());
456bc6d9 340
2b5f62a0
VZ
341 // when the panel gets the focus we move the focus to either the last
342 // window that had the focus or the first one that can get it unless the
343 // focus had been already set to some other child
344
0dcdeee9
VZ
345 wxWindow *win = wxWindow::FindFocus();
346 while ( win )
347 {
348 if ( win == m_winParent )
2b5f62a0
VZ
349 {
350 // our child already has focus, don't take it away from it
0dcdeee9 351 return TRUE;
2b5f62a0 352 }
0dcdeee9
VZ
353
354 if ( win->IsTopLevel() )
355 {
356 // don't look beyond the first top level parent - useless and
357 // unnecessary
358 break;
359 }
360
361 win = win->GetParent();
362 }
363
24a7a198 364 return SetFocusToChild();
456bc6d9
VZ
365}
366
367void wxControlContainer::HandleOnFocus(wxFocusEvent& event)
368{
993eebf1
VZ
369 wxLogTrace(_T("focus"), _T("OnFocus on wxPanel 0x%08lx, name: %s"),
370 (unsigned long)m_winParent->GetHandle(),
456bc6d9
VZ
371 m_winParent->GetName().c_str() );
372
2b5f62a0 373 DoSetFocus();
456bc6d9
VZ
374
375 event.Skip();
376}
377
378bool wxControlContainer::SetFocusToChild()
379{
380 return wxSetFocusToChild(m_winParent, &m_winLastFocused);
381}
382
383// ----------------------------------------------------------------------------
384// SetFocusToChild(): this function is used by wxPanel but also by wxFrame in
385// wxMSW, this is why it is outside of wxControlContainer class
386// ----------------------------------------------------------------------------
387
388bool wxSetFocusToChild(wxWindow *win, wxWindow **childLastFocused)
389{
390 wxCHECK_MSG( win, FALSE, _T("wxSetFocusToChild(): invalid window") );
c25b5d1f
VZ
391 wxCHECK_MSG( childLastFocused, FALSE,
392 _T("wxSetFocusToChild(): NULL child poonter") );
456bc6d9
VZ
393
394 if ( *childLastFocused )
395 {
c25b5d1f
VZ
396 // It might happen that the window got reparented
397 if ( (*childLastFocused)->GetParent() == win )
456bc6d9
VZ
398 {
399 wxLogTrace(_T("focus"),
993eebf1
VZ
400 _T("SetFocusToChild() => last child (0x%08lx)."),
401 (unsigned long)(*childLastFocused)->GetHandle());
456bc6d9 402
c25b5d1f
VZ
403 // not SetFocusFromKbd(): we're restoring focus back to the old
404 // window and not setting it as the result of a kbd action
405 (*childLastFocused)->SetFocus();
456bc6d9
VZ
406 return TRUE;
407 }
408 else
409 {
410 // it doesn't count as such any more
411 *childLastFocused = (wxWindow *)NULL;
412 }
413 }
414
415 // set the focus to the first child who wants it
416 wxWindowList::Node *node = win->GetChildren().GetFirst();
417 while ( node )
418 {
419 wxWindow *child = node->GetData();
420
421 if ( child->AcceptsFocusFromKeyboard() && !child->IsTopLevel() )
422 {
423 wxLogTrace(_T("focus"),
993eebf1
VZ
424 _T("SetFocusToChild() => first child (0x%08lx)."),
425 (unsigned long)child->GetHandle());
456bc6d9 426
a7407834 427 *childLastFocused = child;
5463c0a4 428 child->SetFocusFromKbd();
456bc6d9
VZ
429 return TRUE;
430 }
431
432 node = node->GetNext();
433 }
434
435 return FALSE;
436}
3251b834 437