1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/containr.cpp
3 // Purpose: implementation of wxControlContainer
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
28 #include "wx/containr.h"
34 #include "wx/window.h"
35 #include "wx/scrolbar.h"
36 #include "wx/radiobut.h"
39 // trace mask for focus messages
40 #define TRACE_FOCUS wxT("focus")
42 // ============================================================================
44 // ============================================================================
46 // ----------------------------------------------------------------------------
47 // wxControlContainerBase
48 // ----------------------------------------------------------------------------
50 void wxControlContainerBase::UpdateParentCanFocus()
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
);
58 bool wxControlContainerBase::UpdateCanFocusChildren()
60 const bool acceptsFocusChildren
= HasAnyFocusableChildren();
61 if ( acceptsFocusChildren
!= m_acceptsFocusChildren
)
63 m_acceptsFocusChildren
= acceptsFocusChildren
;
65 UpdateParentCanFocus();
68 return m_acceptsFocusChildren
;
71 bool wxControlContainerBase::HasAnyFocusableChildren() const
73 const wxWindowList
& children
= m_winParent
->GetChildren();
74 for ( wxWindowList::const_iterator i
= children
.begin(),
79 const wxWindow
* const child
= *i
;
81 if ( !m_winParent
->IsClientAreaChild(child
) )
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() )
93 bool wxControlContainerBase::HasAnyChildrenAcceptingFocus() const
95 const wxWindowList
& children
= m_winParent
->GetChildren();
96 for ( wxWindowList::const_iterator i
= children
.begin(),
101 const wxWindow
* const child
= *i
;
103 if ( !m_winParent
->IsClientAreaChild(child
) )
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.
108 if ( child
->CanAcceptFocus() )
115 bool wxControlContainerBase::DoSetFocus()
117 wxLogTrace(TRACE_FOCUS
, wxT("SetFocus on wxPanel 0x%p."),
118 m_winParent
->GetHandle());
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
127 wxWindow
*win
= wxWindow::FindFocus();
130 if ( win
== m_winParent
)
132 // our child already has focus, don't take it away from it
136 if ( win
->IsTopLevel() )
138 // don't look beyond the first top level parent - useless and
143 win
= win
->GetParent();
146 // protect against infinite recursion:
149 bool ret
= SetFocusToChild();
151 m_inSetFocus
= false;
156 bool wxControlContainerBase::AcceptsFocus() const
158 return m_acceptsFocusSelf
&& m_winParent
->CanBeFocused();
161 bool wxControlContainerBase::SetFocusToChild()
163 return wxSetFocusToChild(m_winParent
, &m_winLastFocused
);
166 #ifndef wxHAS_NATIVE_TAB_TRAVERSAL
168 // ----------------------------------------------------------------------------
169 // generic wxControlContainer
170 // ----------------------------------------------------------------------------
172 wxControlContainer::wxControlContainer()
174 m_winLastFocused
= NULL
;
177 void wxControlContainer::SetLastFocus(wxWindow
*win
)
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
182 if ( win
!= m_winParent
)
184 // if we're setting the focus
187 // find the last _immediate_ child which got focus
188 wxWindow
*winParent
= win
;
189 while ( winParent
!= m_winParent
)
192 winParent
= win
->GetParent();
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.
199 wxASSERT_MSG( winParent
,
200 wxT("Setting last focus for a window that is not our child?") );
204 m_winLastFocused
= win
;
208 wxLogTrace(TRACE_FOCUS
, wxT("Set last focus to %s(%s)"),
209 win
->GetClassInfo()->GetClassName(),
210 win
->GetLabel().c_str());
214 wxLogTrace(TRACE_FOCUS
, wxT("No more last focus"));
219 // --------------------------------------------------------------------
220 // The following four functions are used to find other radio buttons
221 // within the same group. Used by wxSetFocusToChild on wxMSW
222 // --------------------------------------------------------------------
226 wxRadioButton
* wxGetPreviousButtonInGroup(wxRadioButton
*btn
)
228 if ( btn
->HasFlag(wxRB_GROUP
) || btn
->HasFlag(wxRB_SINGLE
) )
231 const wxWindowList
& siblings
= btn
->GetParent()->GetChildren();
232 wxWindowList::compatibility_iterator nodeThis
= siblings
.Find(btn
);
233 wxCHECK_MSG( nodeThis
, NULL
, wxT("radio button not a child of its parent?") );
235 // Iterate over all previous siblings until we find the next radio button
236 wxWindowList::compatibility_iterator nodeBefore
= nodeThis
->GetPrevious();
237 wxRadioButton
*prevBtn
= 0;
240 prevBtn
= wxDynamicCast(nodeBefore
->GetData(), wxRadioButton
);
244 nodeBefore
= nodeBefore
->GetPrevious();
247 if (!prevBtn
|| prevBtn
->HasFlag(wxRB_SINGLE
))
249 // no more buttons in group
256 wxRadioButton
* wxGetNextButtonInGroup(wxRadioButton
*btn
)
258 if (btn
->HasFlag(wxRB_SINGLE
))
261 const wxWindowList
& siblings
= btn
->GetParent()->GetChildren();
262 wxWindowList::compatibility_iterator nodeThis
= siblings
.Find(btn
);
263 wxCHECK_MSG( nodeThis
, NULL
, wxT("radio button not a child of its parent?") );
265 // Iterate over all previous siblings until we find the next radio button
266 wxWindowList::compatibility_iterator nodeNext
= nodeThis
->GetNext();
267 wxRadioButton
*nextBtn
= 0;
270 nextBtn
= wxDynamicCast(nodeNext
->GetData(), wxRadioButton
);
274 nodeNext
= nodeNext
->GetNext();
277 if ( !nextBtn
|| nextBtn
->HasFlag(wxRB_GROUP
) || nextBtn
->HasFlag(wxRB_SINGLE
) )
279 // no more buttons or the first button of the next group
286 wxRadioButton
* wxGetFirstButtonInGroup(wxRadioButton
*btn
)
290 wxRadioButton
* prevBtn
= wxGetPreviousButtonInGroup(btn
);
298 wxRadioButton
* wxGetLastButtonInGroup(wxRadioButton
*btn
)
302 wxRadioButton
* nextBtn
= wxGetNextButtonInGroup(btn
);
310 wxRadioButton
* wxGetSelectedButtonInGroup(wxRadioButton
*btn
)
312 // Find currently selected button
316 if (btn
->HasFlag(wxRB_SINGLE
))
319 wxRadioButton
*selBtn
;
321 // First check all previous buttons
322 for (selBtn
= wxGetPreviousButtonInGroup(btn
); selBtn
; selBtn
= wxGetPreviousButtonInGroup(selBtn
))
323 if (selBtn
->GetValue())
326 // Now all following buttons
327 for (selBtn
= wxGetNextButtonInGroup(btn
); selBtn
; selBtn
= wxGetNextButtonInGroup(selBtn
))
328 if (selBtn
->GetValue())
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 // ----------------------------------------------------------------------------
344 void wxControlContainer::HandleOnNavigationKey( wxNavigationKeyEvent
& event
)
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();
351 // the event is propagated downwards if the event emitter was our parent
352 bool goingDown
= event
.GetEventObject() == parent
;
354 const wxWindowList
& children
= m_winParent
->GetChildren();
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
)
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();
372 wxWindow
* const window
= *i
;
373 if ( window
->HasMultiplePages() )
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
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
) )
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() )
403 // let the parent process it unless it already comes from our parent
404 // of we don't have any
406 !parent
|| !parent
->GetEventHandler()->ProcessEvent(event
) )
414 // where are we going?
415 const bool forward
= event
.GetDirection();
417 // the node of the children list from which we should start looking for the
418 // next acceptable child
419 wxWindowList::compatibility_iterator node
, start_node
;
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
426 // just to be sure it's not used (normally this is not necessary, but
427 // doesn't hurt neither)
428 m_winLastFocused
= NULL
;
430 // start from first or last depending on where we're going
431 node
= forward
? children
.GetFirst() : children
.GetLast();
435 // try to find the child which has the focus currently
437 // the event emitter might have done this for us
438 wxWindow
*winFocus
= event
.GetCurrentFocus();
440 // but if not, we might know where the focus was ourselves
442 winFocus
= m_winLastFocused
;
444 // if still no luck, do it the hard way
446 winFocus
= wxWindow::FindFocus();
450 #if defined(__WXMSW__) && wxUSE_RADIOBTN
451 // If we are in a radio button group, start from the first item in the
453 if ( event
.IsFromTab() && wxIsKindOf(winFocus
, wxRadioButton
) )
454 winFocus
= wxGetFirstButtonInGroup((wxRadioButton
*)winFocus
);
456 // ok, we found the focus - now is it our child?
457 start_node
= children
.Find( winFocus
);
460 if ( !start_node
&& m_winLastFocused
)
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
);
467 // if we still didn't find anything, we should start with the first one
470 start_node
= children
.GetFirst();
473 // and the first child which we can try setting focus to is the next or
475 node
= forward
? start_node
->GetNext() : start_node
->GetPrevious();
478 // we want to cycle over all elements passing by NULL
481 // don't go into infinite loop
482 if ( start_node
&& node
&& node
== start_node
)
485 // Have we come to the last or first item on the panel?
490 // exit now as otherwise we'd loop forever
496 // Check if our (maybe grand) parent is another panel: if this
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
502 wxWindow
*focusedParent
= m_winParent
;
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() )
512 event
.SetCurrentFocus( focusedParent
);
513 if ( parent
->GetEventHandler()->ProcessEvent( event
) )
516 focusedParent
= parent
;
518 parent
= parent
->GetParent();
521 //else: as the focus came from our parent, we definitely don't want
522 // to send it back to it!
524 // no, we are not inside another panel so process this ourself
525 node
= forward
? children
.GetFirst() : children
.GetLast();
530 wxWindow
*child
= node
->GetData();
532 // don't TAB to another TLW
533 if ( child
->IsTopLevel() )
535 node
= forward
? node
->GetNext() : node
->GetPrevious();
540 #if defined(__WXMSW__) && wxUSE_RADIOBTN
541 if ( event
.IsFromTab() )
543 if ( wxIsKindOf(child
, wxRadioButton
) )
545 // only radio buttons with either wxRB_GROUP or wxRB_SINGLE
547 if ( child
->HasFlag(wxRB_GROUP
) )
549 // need to tab into the active button within a group
550 wxRadioButton
*rb
= wxGetSelectedButtonInGroup((wxRadioButton
*)child
);
554 else if ( !child
->HasFlag(wxRB_SINGLE
) )
556 node
= forward
? node
->GetNext() : node
->GetPrevious();
561 else if ( m_winLastFocused
&&
562 wxIsKindOf(m_winLastFocused
, wxRadioButton
) &&
563 !m_winLastFocused
->HasFlag(wxRB_SINGLE
) )
565 wxRadioButton
* const
566 lastBtn
= static_cast<wxRadioButton
*>(m_winLastFocused
);
568 // cursor keys don't navigate out of a radio button group so
569 // find the correct radio button to focus
572 child
= wxGetNextButtonInGroup(lastBtn
);
575 // no next button in group, set it to the first button
576 child
= wxGetFirstButtonInGroup(lastBtn
);
581 child
= wxGetPreviousButtonInGroup(lastBtn
);
584 // no previous button in group, set it to the last button
585 child
= wxGetLastButtonInGroup(lastBtn
);
589 if ( child
== m_winLastFocused
)
591 // must be a group consisting of only one button therefore
592 // no need to send a navigation event
599 if ( child
->CanAcceptFocusFromKeyboard() )
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
);
607 // disable propagation for this call as otherwise the event might
608 // bounce back to us.
609 wxPropagationDisabler
disableProp(event
);
610 if ( !child
->GetEventHandler()->ProcessEvent(event
) )
612 // set it first in case SetFocusFromKbd() results in focus
614 m_winLastFocused
= child
;
616 // everything is simple: just give focus to it
617 child
->SetFocusFromKbd();
619 //else: the child manages its focus itself
626 node
= forward
? node
->GetNext() : node
->GetPrevious();
629 // we cycled through all of our children and none of them wanted to accept
634 void wxControlContainer::HandleOnWindowDestroy(wxWindowBase
*child
)
636 if ( child
== m_winLastFocused
)
637 m_winLastFocused
= NULL
;
640 // ----------------------------------------------------------------------------
642 // ----------------------------------------------------------------------------
644 void wxControlContainer::HandleOnFocus(wxFocusEvent
& event
)
646 wxLogTrace(TRACE_FOCUS
, wxT("OnFocus on wxPanel 0x%p, name: %s"),
647 m_winParent
->GetHandle(),
648 m_winParent
->GetName().c_str() );
657 // wxHAS_NATIVE_TAB_TRAVERSAL
659 bool wxControlContainer::SetFocusToChild()
661 return wxSetFocusToChild(m_winParent
, NULL
);
665 #endif // !wxHAS_NATIVE_TAB_TRAVERSAL
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 // ----------------------------------------------------------------------------
672 bool wxSetFocusToChild(wxWindow
*win
, wxWindow
**childLastFocused
)
674 wxCHECK_MSG( win
, false, wxT("wxSetFocusToChild(): invalid window") );
675 // wxCHECK_MSG( childLastFocused, false,
676 // wxT("wxSetFocusToChild(): NULL child poonter") );
678 if ( childLastFocused
&& *childLastFocused
)
680 // It might happen that the window got reparented
681 if ( (*childLastFocused
)->GetParent() == win
)
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
;
687 while ( *childLastFocused
)
689 if ( (*childLastFocused
)->IsShown() )
691 if ( !deepestVisibleWindow
)
692 deepestVisibleWindow
= *childLastFocused
;
695 deepestVisibleWindow
= NULL
;
697 *childLastFocused
= (*childLastFocused
)->GetParent();
700 if ( deepestVisibleWindow
)
702 *childLastFocused
= deepestVisibleWindow
;
704 wxLogTrace(TRACE_FOCUS
,
705 wxT("SetFocusToChild() => last child (0x%p)."),
706 (*childLastFocused
)->GetHandle());
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();
716 // it doesn't count as such any more
717 *childLastFocused
= NULL
;
721 // set the focus to the first child who wants it
722 wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst();
725 wxWindow
*child
= node
->GetData();
726 node
= node
->GetNext();
728 // skip special windows:
729 if ( !win
->IsClientAreaChild(child
) )
732 if ( child
->CanAcceptFocusFromKeyboard() && !child
->IsTopLevel() )
734 #if defined(__WXMSW__) && wxUSE_RADIOBTN
735 // If a radiobutton is the first focusable child, search for the
736 // selected radiobutton in the same group
737 wxRadioButton
* btn
= wxDynamicCast(child
, wxRadioButton
);
740 wxRadioButton
* selected
= wxGetSelectedButtonInGroup(btn
);
746 wxLogTrace(TRACE_FOCUS
,
747 wxT("SetFocusToChild() => first child (0x%p)."),
750 if (childLastFocused
)
751 *childLastFocused
= child
;
752 child
->SetFocusFromKbd();