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