]> git.saurik.com Git - wxWidgets.git/blame - src/common/containr.cpp
fixing overrelease and out-of-bounds write, fixes #13725
[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>
526954c5 9// Licence: wxWindows licence
456bc6d9
VZ
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
456bc6d9
VZ
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
24 #pragma hdrstop
25#endif
26
de160b06
VZ
27#ifndef WX_PRECOMP
28 #include "wx/containr.h"
29#endif
30
456bc6d9 31#ifndef WX_PRECOMP
6285be72
VZ
32 #include "wx/log.h"
33 #include "wx/event.h"
34 #include "wx/window.h"
851dee09 35 #include "wx/scrolbar.h"
b0b5881a 36 #include "wx/radiobut.h"
456bc6d9
VZ
37#endif //WX_PRECOMP
38
9f542367 39// trace mask for focus messages
9a83f860 40#define TRACE_FOCUS wxT("focus")
9f542367 41
456bc6d9
VZ
42// ============================================================================
43// implementation
44// ============================================================================
45
80332672
VZ
46// ----------------------------------------------------------------------------
47// wxControlContainerBase
48// ----------------------------------------------------------------------------
49
e68b7b36 50bool wxControlContainerBase::UpdateCanFocusChildren()
456bc6d9 51{
e68b7b36
VZ
52 const bool acceptsFocusChildren = HasAnyFocusableChildren();
53 if ( acceptsFocusChildren != m_acceptsFocusChildren )
54 {
55 m_acceptsFocusChildren = acceptsFocusChildren;
80332672 56
e68b7b36
VZ
57 m_winParent->SetCanFocus(AcceptsFocusRecursively());
58 }
80332672 59
e68b7b36 60 return m_acceptsFocusChildren;
456bc6d9
VZ
61}
62
edc09871 63bool wxControlContainerBase::HasAnyFocusableChildren() const
3251b834 64{
edc09871
VZ
65 const wxWindowList& children = m_winParent->GetChildren();
66 for ( wxWindowList::const_iterator i = children.begin(),
67 end = children.end();
68 i != end;
69 ++i )
de160b06 70 {
edc09871 71 const wxWindow * const child = *i;
3251b834 72
049908c5 73 if ( !m_winParent->IsClientAreaChild(child) )
de160b06 74 continue;
80332672 75
de160b06 76 if ( child->CanAcceptFocus() )
edc09871 77 return true;
de160b06 78 }
c9d59ee7 79
edc09871 80 return false;
80332672
VZ
81}
82
c7bfb76a
JS
83bool wxControlContainerBase::DoSetFocus()
84{
9a83f860 85 wxLogTrace(TRACE_FOCUS, wxT("SetFocus on wxPanel 0x%p."),
c7bfb76a
JS
86 m_winParent->GetHandle());
87
88 if (m_inSetFocus)
89 return true;
90
91 // when the panel gets the focus we move the focus to either the last
92 // window that had the focus or the first one that can get it unless the
93 // focus had been already set to some other child
94
95 wxWindow *win = wxWindow::FindFocus();
96 while ( win )
97 {
98 if ( win == m_winParent )
99 {
100 // our child already has focus, don't take it away from it
101 return true;
102 }
103
104 if ( win->IsTopLevel() )
105 {
106 // don't look beyond the first top level parent - useless and
107 // unnecessary
108 break;
109 }
110
111 win = win->GetParent();
112 }
113
114 // protect against infinite recursion:
115 m_inSetFocus = true;
116
117 bool ret = SetFocusToChild();
118
119 m_inSetFocus = false;
120
121 return ret;
122}
123
124bool wxControlContainerBase::SetFocusToChild()
125{
126 return wxSetFocusToChild(m_winParent, &m_winLastFocused);
127}
128
80332672 129#ifndef wxHAS_NATIVE_TAB_TRAVERSAL
3251b834 130
80332672
VZ
131// ----------------------------------------------------------------------------
132// generic wxControlContainer
133// ----------------------------------------------------------------------------
134
135wxControlContainer::wxControlContainer()
136{
137 m_winLastFocused = NULL;
3251b834
VZ
138}
139
456bc6d9
VZ
140void wxControlContainer::SetLastFocus(wxWindow *win)
141{
6aeb6f2a
VZ
142 // the panel itself should never get the focus at all but if it does happen
143 // temporarily (as it seems to do under wxGTK), at the very least don't
144 // forget our previous m_winLastFocused
c25b5d1f 145 if ( win != m_winParent )
456bc6d9 146 {
c25b5d1f
VZ
147 // if we're setting the focus
148 if ( win )
83c865f5 149 {
c25b5d1f
VZ
150 // find the last _immediate_ child which got focus
151 wxWindow *winParent = win;
152 while ( winParent != m_winParent )
153 {
154 win = winParent;
155 winParent = win->GetParent();
83c865f5 156
c25b5d1f
VZ
157 // Yes, this can happen, though in a totally pathological case.
158 // like when detaching a menubar from a frame with a child
159 // which has pushed itself as an event handler for the menubar.
160 // (under wxGTK)
6f8239de 161
c25b5d1f 162 wxASSERT_MSG( winParent,
9a83f860 163 wxT("Setting last focus for a window that is not our child?") );
c25b5d1f 164 }
6f8239de 165 }
456bc6d9 166
c25b5d1f 167 m_winLastFocused = win;
6aeb6f2a 168
c25b5d1f
VZ
169 if ( win )
170 {
9a83f860 171 wxLogTrace(TRACE_FOCUS, wxT("Set last focus to %s(%s)"),
c25b5d1f
VZ
172 win->GetClassInfo()->GetClassName(),
173 win->GetLabel().c_str());
174 }
175 else
176 {
9a83f860 177 wxLogTrace(TRACE_FOCUS, wxT("No more last focus"));
c25b5d1f 178 }
6aeb6f2a 179 }
456bc6d9
VZ
180}
181
7ff1b620 182// --------------------------------------------------------------------
b49f58fe 183// The following four functions are used to find other radio buttons
7ff1b620
VZ
184// within the same group. Used by wxSetFocusToChild on wxMSW
185// --------------------------------------------------------------------
186
a8ff046b 187#if defined(__WXMSW__) && wxUSE_RADIOBTN
7ff1b620
VZ
188
189wxRadioButton* wxGetPreviousButtonInGroup(wxRadioButton *btn)
190{
191 if ( btn->HasFlag(wxRB_GROUP) || btn->HasFlag(wxRB_SINGLE) )
192 return NULL;
193
194 const wxWindowList& siblings = btn->GetParent()->GetChildren();
195 wxWindowList::compatibility_iterator nodeThis = siblings.Find(btn);
9a83f860 196 wxCHECK_MSG( nodeThis, NULL, wxT("radio button not a child of its parent?") );
7ff1b620
VZ
197
198 // Iterate over all previous siblings until we find the next radio button
199 wxWindowList::compatibility_iterator nodeBefore = nodeThis->GetPrevious();
200 wxRadioButton *prevBtn = 0;
201 while (nodeBefore)
202 {
203 prevBtn = wxDynamicCast(nodeBefore->GetData(), wxRadioButton);
204 if (prevBtn)
205 break;
206
207 nodeBefore = nodeBefore->GetPrevious();
208 }
b49f58fe 209
7ff1b620
VZ
210 if (!prevBtn || prevBtn->HasFlag(wxRB_SINGLE))
211 {
212 // no more buttons in group
213 return NULL;
214 }
3d3afaec
JS
215
216 return prevBtn;
7ff1b620
VZ
217}
218
219wxRadioButton* wxGetNextButtonInGroup(wxRadioButton *btn)
220{
221 if (btn->HasFlag(wxRB_SINGLE))
222 return NULL;
223
224 const wxWindowList& siblings = btn->GetParent()->GetChildren();
225 wxWindowList::compatibility_iterator nodeThis = siblings.Find(btn);
9a83f860 226 wxCHECK_MSG( nodeThis, NULL, wxT("radio button not a child of its parent?") );
7ff1b620
VZ
227
228 // Iterate over all previous siblings until we find the next radio button
229 wxWindowList::compatibility_iterator nodeNext = nodeThis->GetNext();
230 wxRadioButton *nextBtn = 0;
231 while (nodeNext)
232 {
233 nextBtn = wxDynamicCast(nodeNext->GetData(), wxRadioButton);
234 if (nextBtn)
235 break;
236
237 nodeNext = nodeNext->GetNext();
238 }
239
240 if ( !nextBtn || nextBtn->HasFlag(wxRB_GROUP) || nextBtn->HasFlag(wxRB_SINGLE) )
241 {
242 // no more buttons or the first button of the next group
243 return NULL;
244 }
3d3afaec
JS
245
246 return nextBtn;
7ff1b620
VZ
247}
248
249wxRadioButton* wxGetFirstButtonInGroup(wxRadioButton *btn)
250{
251 while (true)
252 {
253 wxRadioButton* prevBtn = wxGetPreviousButtonInGroup(btn);
254 if (!prevBtn)
255 return btn;
b49f58fe 256
7ff1b620
VZ
257 btn = prevBtn;
258 }
259}
260
3d3afaec
JS
261wxRadioButton* wxGetLastButtonInGroup(wxRadioButton *btn)
262{
263 while (true)
264 {
265 wxRadioButton* nextBtn = wxGetNextButtonInGroup(btn);
266 if (!nextBtn)
267 return btn;
268
269 btn = nextBtn;
270 }
271}
272
7ff1b620
VZ
273wxRadioButton* wxGetSelectedButtonInGroup(wxRadioButton *btn)
274{
275 // Find currently selected button
276 if (btn->GetValue())
277 return btn;
278
279 if (btn->HasFlag(wxRB_SINGLE))
280 return NULL;
281
282 wxRadioButton *selBtn;
283
284 // First check all previous buttons
285 for (selBtn = wxGetPreviousButtonInGroup(btn); selBtn; selBtn = wxGetPreviousButtonInGroup(selBtn))
286 if (selBtn->GetValue())
287 return selBtn;
288
289 // Now all following buttons
290 for (selBtn = wxGetNextButtonInGroup(btn); selBtn; selBtn = wxGetNextButtonInGroup(selBtn))
291 if (selBtn->GetValue())
292 return selBtn;
293
294 return NULL;
295}
296
b49f58fe 297#endif // __WXMSW__
7ff1b620 298
456bc6d9
VZ
299// ----------------------------------------------------------------------------
300// Keyboard handling - this is the place where the TAB traversal logic is
301// implemented. As this code is common to all ports, this ensures consistent
302// behaviour even if we don't specify how exactly the wxNavigationKeyEvent are
303// generated and this is done in platform specific code which also ensures that
304// we can follow the given platform standards.
305// ----------------------------------------------------------------------------
306
307void wxControlContainer::HandleOnNavigationKey( wxNavigationKeyEvent& event )
308{
391b1695
VZ
309 // for a TLW we shouldn't involve the parent window, it has nothing to do
310 // with keyboard navigation inside this TLW
311 wxWindow *parent = m_winParent->IsTopLevel() ? NULL
312 : m_winParent->GetParent();
456bc6d9
VZ
313
314 // the event is propagated downwards if the event emitter was our parent
315 bool goingDown = event.GetEventObject() == parent;
316
317 const wxWindowList& children = m_winParent->GetChildren();
318
f2426531
VZ
319 // if we have exactly one notebook-like child window (actually it could be
320 // any window that returns true from its HasMultiplePages()), then
321 // [Shift-]Ctrl-Tab and Ctrl-PageUp/Down keys should iterate over its pages
322 // even if the focus is outside of the control because this is how the
323 // standard MSW properties dialogs behave and we do it under other platforms
324 // as well because it seems like a good idea -- but we can always put this
325 // block inside "#ifdef __WXMSW__" if it's not suitable there
326 if ( event.IsWindowChange() && !goingDown )
327 {
328 // check if we have a unique notebook-like child
329 wxWindow *bookctrl = NULL;
330 for ( wxWindowList::const_iterator i = children.begin(),
331 end = children.end();
332 i != end;
333 ++i )
334 {
335 wxWindow * const window = *i;
336 if ( window->HasMultiplePages() )
337 {
338 if ( bookctrl )
339 {
340 // this is the second book-like control already so don't do
341 // anything as we don't know which one should have its page
342 // changed
343 bookctrl = NULL;
344 break;
345 }
346
347 bookctrl = window;
348 }
349 }
350
5f28de16
VZ
351 if ( bookctrl )
352 {
353 // make sure that we don't bubble up the event again from the book
354 // control resulting in infinite recursion
355 wxNavigationKeyEvent eventCopy(event);
356 eventCopy.SetEventObject(m_winParent);
357 if ( bookctrl->GetEventHandler()->ProcessEvent(eventCopy) )
358 return;
359 }
f2426531
VZ
360 }
361
456bc6d9
VZ
362 // there is not much to do if we don't have children and we're not
363 // interested in "notebook page change" events here
364 if ( !children.GetCount() || event.IsWindowChange() )
365 {
366 // let the parent process it unless it already comes from our parent
367 // of we don't have any
368 if ( goingDown ||
369 !parent || !parent->GetEventHandler()->ProcessEvent(event) )
370 {
371 event.Skip();
372 }
373
374 return;
375 }
376
377 // where are we going?
edc0a395 378 const bool forward = event.GetDirection();
456bc6d9
VZ
379
380 // the node of the children list from which we should start looking for the
381 // next acceptable child
222ed1d6 382 wxWindowList::compatibility_iterator node, start_node;
456bc6d9
VZ
383
384 // we should start from the first/last control and not from the one which
385 // had focus the last time if we're propagating the event downwards because
386 // for our parent we look like a single control
387 if ( goingDown )
388 {
389 // just to be sure it's not used (normally this is not necessary, but
390 // doesn't hurt neither)
d3b9f782 391 m_winLastFocused = NULL;
456bc6d9
VZ
392
393 // start from first or last depending on where we're going
394 node = forward ? children.GetFirst() : children.GetLast();
456bc6d9 395 }
edc0a395 396 else // going up
456bc6d9
VZ
397 {
398 // try to find the child which has the focus currently
399
400 // the event emitter might have done this for us
401 wxWindow *winFocus = event.GetCurrentFocus();
402
403 // but if not, we might know where the focus was ourselves
404 if (!winFocus)
405 winFocus = m_winLastFocused;
406
407 // if still no luck, do it the hard way
408 if (!winFocus)
409 winFocus = wxWindow::FindFocus();
410
411 if ( winFocus )
412 {
a8ff046b 413#if defined(__WXMSW__) && wxUSE_RADIOBTN
b49f58fe 414 // If we are in a radio button group, start from the first item in the
7ff1b620
VZ
415 // group
416 if ( event.IsFromTab() && wxIsKindOf(winFocus, wxRadioButton ) )
417 winFocus = wxGetFirstButtonInGroup((wxRadioButton*)winFocus);
a8ff046b 418#endif // __WXMSW__
456bc6d9
VZ
419 // ok, we found the focus - now is it our child?
420 start_node = children.Find( winFocus );
421 }
456bc6d9
VZ
422
423 if ( !start_node && m_winLastFocused )
424 {
425 // window which has focus isn't our child, fall back to the one
426 // which had the focus the last time
427 start_node = children.Find( m_winLastFocused );
428 }
429
430 // if we still didn't find anything, we should start with the first one
431 if ( !start_node )
432 {
433 start_node = children.GetFirst();
434 }
435
436 // and the first child which we can try setting focus to is the next or
437 // the previous one
438 node = forward ? start_node->GetNext() : start_node->GetPrevious();
439 }
440
441 // we want to cycle over all elements passing by NULL
edc0a395 442 for ( ;; )
456bc6d9 443 {
edc0a395 444 // don't go into infinite loop
aa78d22e 445 if ( start_node && node && node == start_node )
edc0a395
VZ
446 break;
447
456bc6d9
VZ
448 // Have we come to the last or first item on the panel?
449 if ( !node )
450 {
e547f7a7
VZ
451 if ( !start_node )
452 {
453 // exit now as otherwise we'd loop forever
454 break;
455 }
456
456bc6d9
VZ
457 if ( !goingDown )
458 {
edc0a395 459 // Check if our (maybe grand) parent is another panel: if this
456bc6d9
VZ
460 // is the case, they will know what to do with this navigation
461 // key and so give them the chance to process it instead of
462 // looping inside this panel (normally, the focus will go to
463 // the next/previous item after this panel in the parent
464 // panel).
2a0777a8 465 wxWindow *focusedParent = m_winParent;
456bc6d9
VZ
466 while ( parent )
467 {
6e92c299
VZ
468 // We don't want to tab into a different dialog or frame or
469 // even an MDI child frame, so test for this explicitly
470 // (and in particular don't just use IsTopLevel() which
471 // would return false in the latter case).
472 if ( focusedParent->IsTopNavigationDomain() )
456bc6d9
VZ
473 break;
474
2a0777a8 475 event.SetCurrentFocus( focusedParent );
456bc6d9
VZ
476 if ( parent->GetEventHandler()->ProcessEvent( event ) )
477 return;
478
2a0777a8 479 focusedParent = parent;
456bc6d9
VZ
480
481 parent = parent->GetParent();
482 }
483 }
484 //else: as the focus came from our parent, we definitely don't want
485 // to send it back to it!
486
487 // no, we are not inside another panel so process this ourself
488 node = forward ? children.GetFirst() : children.GetLast();
489
490 continue;
491 }
492
493 wxWindow *child = node->GetData();
494
e49d331c
VZ
495 // don't TAB to another TLW
496 if ( child->IsTopLevel() )
497 {
498 node = forward ? node->GetNext() : node->GetPrevious();
499
500 continue;
501 }
502
a8ff046b 503#if defined(__WXMSW__) && wxUSE_RADIOBTN
3d3afaec 504 if ( event.IsFromTab() )
7ff1b620 505 {
3d3afaec 506 if ( wxIsKindOf(child, wxRadioButton) )
7ff1b620 507 {
3d3afaec
JS
508 // only radio buttons with either wxRB_GROUP or wxRB_SINGLE
509 // can be tabbed to
510 if ( child->HasFlag(wxRB_GROUP) )
7ff1b620 511 {
3d3afaec
JS
512 // need to tab into the active button within a group
513 wxRadioButton *rb = wxGetSelectedButtonInGroup((wxRadioButton*)child);
514 if ( rb )
515 child = rb;
516 }
517 else if ( !child->HasFlag(wxRB_SINGLE) )
518 {
519 node = forward ? node->GetNext() : node->GetPrevious();
520 continue;
7ff1b620
VZ
521 }
522 }
523 }
3d3afaec
JS
524 else if ( m_winLastFocused &&
525 wxIsKindOf(m_winLastFocused, wxRadioButton) &&
526 !m_winLastFocused->HasFlag(wxRB_SINGLE) )
7ff1b620 527 {
506f9e92 528 wxRadioButton * const
5c33522f 529 lastBtn = static_cast<wxRadioButton *>(m_winLastFocused);
506f9e92 530
3d3afaec
JS
531 // cursor keys don't navigate out of a radio button group so
532 // find the correct radio button to focus
533 if ( forward )
7ff1b620 534 {
506f9e92 535 child = wxGetNextButtonInGroup(lastBtn);
3d3afaec
JS
536 if ( !child )
537 {
538 // no next button in group, set it to the first button
506f9e92 539 child = wxGetFirstButtonInGroup(lastBtn);
3d3afaec
JS
540 }
541 }
542 else
543 {
506f9e92 544 child = wxGetPreviousButtonInGroup(lastBtn);
3d3afaec
JS
545 if ( !child )
546 {
547 // no previous button in group, set it to the last button
506f9e92 548 child = wxGetLastButtonInGroup(lastBtn);
3d3afaec
JS
549 }
550 }
551
552 if ( child == m_winLastFocused )
553 {
554 // must be a group consisting of only one button therefore
555 // no need to send a navigation event
556 event.Skip(false);
557 return;
7ff1b620
VZ
558 }
559 }
3d3afaec 560#endif // __WXMSW__
c932709d 561
21bf81db 562 if ( child->CanAcceptFocusFromKeyboard() )
456bc6d9
VZ
563 {
564 // if we're setting the focus to a child panel we should prevent it
565 // from giving it to the child which had the focus the last time
566 // and instead give it to the first/last child depending from which
567 // direction we're coming
568 event.SetEventObject(m_winParent);
944e8709 569
aef35d0e
VZ
570 // disable propagation for this call as otherwise the event might
571 // bounce back to us.
572 wxPropagationDisabler disableProp(event);
456bc6d9
VZ
573 if ( !child->GetEventHandler()->ProcessEvent(event) )
574 {
2b5f62a0
VZ
575 // set it first in case SetFocusFromKbd() results in focus
576 // change too
577 m_winLastFocused = child;
578
456bc6d9 579 // everything is simple: just give focus to it
5463c0a4 580 child->SetFocusFromKbd();
456bc6d9
VZ
581 }
582 //else: the child manages its focus itself
583
c9d59ee7 584 event.Skip( false );
456bc6d9
VZ
585
586 return;
587 }
588
589 node = forward ? node->GetNext() : node->GetPrevious();
590 }
591
592 // we cycled through all of our children and none of them wanted to accept
593 // focus
594 event.Skip();
595}
596
597void wxControlContainer::HandleOnWindowDestroy(wxWindowBase *child)
598{
599 if ( child == m_winLastFocused )
600 m_winLastFocused = NULL;
456bc6d9
VZ
601}
602
603// ----------------------------------------------------------------------------
604// focus handling
605// ----------------------------------------------------------------------------
606
456bc6d9
VZ
607void wxControlContainer::HandleOnFocus(wxFocusEvent& event)
608{
9a83f860 609 wxLogTrace(TRACE_FOCUS, wxT("OnFocus on wxPanel 0x%p, name: %s"),
9f542367 610 m_winParent->GetHandle(),
456bc6d9
VZ
611 m_winParent->GetName().c_str() );
612
2b5f62a0 613 DoSetFocus();
456bc6d9
VZ
614
615 event.Skip();
616}
617
c7bfb76a
JS
618
619#else
620 // wxHAS_NATIVE_TAB_TRAVERSAL
621
456bc6d9
VZ
622bool wxControlContainer::SetFocusToChild()
623{
c7bfb76a 624 return wxSetFocusToChild(m_winParent, NULL);
456bc6d9
VZ
625}
626
c7bfb76a
JS
627
628#endif // !wxHAS_NATIVE_TAB_TRAVERSAL
629
456bc6d9
VZ
630// ----------------------------------------------------------------------------
631// SetFocusToChild(): this function is used by wxPanel but also by wxFrame in
632// wxMSW, this is why it is outside of wxControlContainer class
633// ----------------------------------------------------------------------------
634
635bool wxSetFocusToChild(wxWindow *win, wxWindow **childLastFocused)
636{
9a83f860 637 wxCHECK_MSG( win, false, wxT("wxSetFocusToChild(): invalid window") );
c7bfb76a 638 // wxCHECK_MSG( childLastFocused, false,
9a83f860 639 // wxT("wxSetFocusToChild(): NULL child poonter") );
456bc6d9 640
c7bfb76a 641 if ( childLastFocused && *childLastFocused )
456bc6d9 642 {
c25b5d1f
VZ
643 // It might happen that the window got reparented
644 if ( (*childLastFocused)->GetParent() == win )
456bc6d9 645 {
6ca243fc
VZ
646 // And it also could have become hidden in the meanwhile
647 // We want to focus on the deepest widget visible
648 wxWindow *deepestVisibleWindow = NULL;
649
650 while ( *childLastFocused )
26647ae4 651 {
6ca243fc
VZ
652 if ( (*childLastFocused)->IsShown() )
653 {
654 if ( !deepestVisibleWindow )
655 deepestVisibleWindow = *childLastFocused;
656 }
657 else
658 deepestVisibleWindow = NULL;
659
26647ae4 660 *childLastFocused = (*childLastFocused)->GetParent();
26647ae4 661 }
456bc6d9 662
6ca243fc 663 if ( deepestVisibleWindow )
26647ae4 664 {
6ca243fc
VZ
665 *childLastFocused = deepestVisibleWindow;
666
26647ae4
VZ
667 wxLogTrace(TRACE_FOCUS,
668 wxT("SetFocusToChild() => last child (0x%p)."),
669 (*childLastFocused)->GetHandle());
670
671 // not SetFocusFromKbd(): we're restoring focus back to the old
672 // window and not setting it as the result of a kbd action
673 (*childLastFocused)->SetFocus();
674 return true;
675 }
456bc6d9
VZ
676 }
677 else
678 {
679 // it doesn't count as such any more
d3b9f782 680 *childLastFocused = NULL;
456bc6d9
VZ
681 }
682 }
683
684 // set the focus to the first child who wants it
222ed1d6 685 wxWindowList::compatibility_iterator node = win->GetChildren().GetFirst();
456bc6d9
VZ
686 while ( node )
687 {
688 wxWindow *child = node->GetData();
afd7bf26 689 node = node->GetNext();
456bc6d9 690
049908c5
VS
691 // skip special windows:
692 if ( !win->IsClientAreaChild(child) )
afd7bf26 693 continue;
049908c5 694
de160b06 695 if ( child->CanAcceptFocusFromKeyboard() && !child->IsTopLevel() )
456bc6d9 696 {
a8ff046b 697#if defined(__WXMSW__) && wxUSE_RADIOBTN
b49f58fe 698 // If a radiobutton is the first focusable child, search for the
7ff1b620
VZ
699 // selected radiobutton in the same group
700 wxRadioButton* btn = wxDynamicCast(child, wxRadioButton);
701 if (btn)
702 {
703 wxRadioButton* selected = wxGetSelectedButtonInGroup(btn);
704 if (selected)
705 child = selected;
706 }
a8ff046b 707#endif // __WXMSW__
7ff1b620 708
9f542367 709 wxLogTrace(TRACE_FOCUS,
9a83f860 710 wxT("SetFocusToChild() => first child (0x%p)."),
9f542367 711 child->GetHandle());
456bc6d9 712
c7bfb76a
JS
713 if (childLastFocused)
714 *childLastFocused = child;
5463c0a4 715 child->SetFocusFromKbd();
c9d59ee7 716 return true;
456bc6d9 717 }
456bc6d9
VZ
718 }
719
c9d59ee7 720 return false;
456bc6d9 721}
de160b06 722