1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/containr.cpp
3 // Purpose: implementation of wxControlContainer
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
27 #include "wx/containr.h"
33 #include "wx/window.h"
34 #include "wx/scrolbar.h"
35 #include "wx/radiobut.h"
38 // trace mask for focus messages
39 #define TRACE_FOCUS wxT("focus")
41 // ============================================================================
43 // ============================================================================
45 // ----------------------------------------------------------------------------
46 // wxControlContainerBase
47 // ----------------------------------------------------------------------------
49 void wxControlContainerBase::UpdateParentCanFocus()
51 // In the ports where it does something non trivial, the parent window
52 // should only be focusable if it doesn't have any focusable children
53 // (e.g. native focus handling in wxGTK totally breaks down otherwise).
54 m_winParent
->SetCanFocus(m_acceptsFocusSelf
&& !m_acceptsFocusChildren
);
57 bool wxControlContainerBase::UpdateCanFocusChildren()
59 const bool acceptsFocusChildren
= HasAnyFocusableChildren();
60 if ( acceptsFocusChildren
!= m_acceptsFocusChildren
)
62 m_acceptsFocusChildren
= acceptsFocusChildren
;
64 UpdateParentCanFocus();
67 return m_acceptsFocusChildren
;
70 bool wxControlContainerBase::HasAnyFocusableChildren() const
72 const wxWindowList
& children
= m_winParent
->GetChildren();
73 for ( wxWindowList::const_iterator i
= children
.begin(),
78 const wxWindow
* const child
= *i
;
80 if ( !m_winParent
->IsClientAreaChild(child
) )
83 // Here we check whether the child can accept the focus at all, as we
84 // want to try focusing it later even if it can't accept it right now.
85 if ( child
->AcceptsFocusRecursively() )
92 bool wxControlContainerBase::HasAnyChildrenAcceptingFocus() const
94 const wxWindowList
& children
= m_winParent
->GetChildren();
95 for ( wxWindowList::const_iterator i
= children
.begin(),
100 const wxWindow
* const child
= *i
;
102 if ( !m_winParent
->IsClientAreaChild(child
) )
105 // Here we check if the child accepts focus right now as we need to
106 // know if we can give the focus to it or not.
107 if ( child
->CanAcceptFocus() )
114 bool wxControlContainerBase::DoSetFocus()
116 wxLogTrace(TRACE_FOCUS
, wxT("SetFocus on wxPanel 0x%p."),
117 m_winParent
->GetHandle());
122 // when the panel gets the focus we move the focus to either the last
123 // window that had the focus or the first one that can get it unless the
124 // focus had been already set to some other child
126 wxWindow
*win
= wxWindow::FindFocus();
129 if ( win
== m_winParent
)
131 // our child already has focus, don't take it away from it
135 if ( win
->IsTopLevel() )
137 // don't look beyond the first top level parent - useless and
142 win
= win
->GetParent();
145 // protect against infinite recursion:
148 bool ret
= SetFocusToChild();
150 m_inSetFocus
= false;
155 bool wxControlContainerBase::AcceptsFocus() const
157 return m_acceptsFocusSelf
&& m_winParent
->CanBeFocused();
160 bool wxControlContainerBase::SetFocusToChild()
162 return wxSetFocusToChild(m_winParent
, &m_winLastFocused
);
165 #ifndef wxHAS_NATIVE_TAB_TRAVERSAL
167 // ----------------------------------------------------------------------------
168 // generic wxControlContainer
169 // ----------------------------------------------------------------------------
171 wxControlContainer::wxControlContainer()
173 m_winLastFocused
= NULL
;
176 void wxControlContainer::SetLastFocus(wxWindow
*win
)
178 // the panel itself should never get the focus at all but if it does happen
179 // temporarily (as it seems to do under wxGTK), at the very least don't
180 // forget our previous m_winLastFocused
181 if ( win
!= m_winParent
)
183 // if we're setting the focus
186 // find the last _immediate_ child which got focus
187 wxWindow
*winParent
= win
;
188 while ( winParent
!= m_winParent
)
191 winParent
= win
->GetParent();
193 // Yes, this can happen, though in a totally pathological case.
194 // like when detaching a menubar from a frame with a child
195 // which has pushed itself as an event handler for the menubar.
198 wxASSERT_MSG( winParent
,
199 wxT("Setting last focus for a window that is not our child?") );
203 m_winLastFocused
= win
;
207 wxLogTrace(TRACE_FOCUS
, wxT("Set last focus to %s(%s)"),
208 win
->GetClassInfo()->GetClassName(),
209 win
->GetLabel().c_str());
213 wxLogTrace(TRACE_FOCUS
, wxT("No more last focus"));
218 // --------------------------------------------------------------------
219 // The following four functions are used to find other radio buttons
220 // within the same group. Used by wxSetFocusToChild on wxMSW
221 // --------------------------------------------------------------------
225 wxRadioButton
* wxGetPreviousButtonInGroup(wxRadioButton
*btn
)
227 if ( btn
->HasFlag(wxRB_GROUP
) || btn
->HasFlag(wxRB_SINGLE
) )
230 const wxWindowList
& siblings
= btn
->GetParent()->GetChildren();
231 wxWindowList::compatibility_iterator nodeThis
= siblings
.Find(btn
);
232 wxCHECK_MSG( nodeThis
, NULL
, wxT("radio button not a child of its parent?") );
234 // Iterate over all previous siblings until we find the next radio button
235 wxWindowList::compatibility_iterator nodeBefore
= nodeThis
->GetPrevious();
236 wxRadioButton
*prevBtn
= 0;
239 prevBtn
= wxDynamicCast(nodeBefore
->GetData(), wxRadioButton
);
243 nodeBefore
= nodeBefore
->GetPrevious();
246 if (!prevBtn
|| prevBtn
->HasFlag(wxRB_SINGLE
))
248 // no more buttons in group
255 wxRadioButton
* wxGetNextButtonInGroup(wxRadioButton
*btn
)
257 if (btn
->HasFlag(wxRB_SINGLE
))
260 const wxWindowList
& siblings
= btn
->GetParent()->GetChildren();
261 wxWindowList::compatibility_iterator nodeThis
= siblings
.Find(btn
);
262 wxCHECK_MSG( nodeThis
, NULL
, wxT("radio button not a child of its parent?") );
264 // Iterate over all previous siblings until we find the next radio button
265 wxWindowList::compatibility_iterator nodeNext
= nodeThis
->GetNext();
266 wxRadioButton
*nextBtn
= 0;
269 nextBtn
= wxDynamicCast(nodeNext
->GetData(), wxRadioButton
);
273 nodeNext
= nodeNext
->GetNext();
276 if ( !nextBtn
|| nextBtn
->HasFlag(wxRB_GROUP
) || nextBtn
->HasFlag(wxRB_SINGLE
) )
278 // no more buttons or the first button of the next group
285 wxRadioButton
* wxGetFirstButtonInGroup(wxRadioButton
*btn
)
289 wxRadioButton
* prevBtn
= wxGetPreviousButtonInGroup(btn
);
297 wxRadioButton
* wxGetLastButtonInGroup(wxRadioButton
*btn
)
301 wxRadioButton
* nextBtn
= wxGetNextButtonInGroup(btn
);
309 wxRadioButton
* wxGetSelectedButtonInGroup(wxRadioButton
*btn
)
311 // Find currently selected button
315 if (btn
->HasFlag(wxRB_SINGLE
))
318 wxRadioButton
*selBtn
;
320 // First check all previous buttons
321 for (selBtn
= wxGetPreviousButtonInGroup(btn
); selBtn
; selBtn
= wxGetPreviousButtonInGroup(selBtn
))
322 if (selBtn
->GetValue())
325 // Now all following buttons
326 for (selBtn
= wxGetNextButtonInGroup(btn
); selBtn
; selBtn
= wxGetNextButtonInGroup(selBtn
))
327 if (selBtn
->GetValue())
335 // ----------------------------------------------------------------------------
336 // Keyboard handling - this is the place where the TAB traversal logic is
337 // implemented. As this code is common to all ports, this ensures consistent
338 // behaviour even if we don't specify how exactly the wxNavigationKeyEvent are
339 // generated and this is done in platform specific code which also ensures that
340 // we can follow the given platform standards.
341 // ----------------------------------------------------------------------------
343 void wxControlContainer::HandleOnNavigationKey( wxNavigationKeyEvent
& event
)
345 // for a TLW we shouldn't involve the parent window, it has nothing to do
346 // with keyboard navigation inside this TLW
347 wxWindow
*parent
= m_winParent
->IsTopLevel() ? NULL
348 : m_winParent
->GetParent();
350 // the event is propagated downwards if the event emitter was our parent
351 bool goingDown
= event
.GetEventObject() == parent
;
353 const wxWindowList
& children
= m_winParent
->GetChildren();
355 // if we have exactly one notebook-like child window (actually it could be
356 // any window that returns true from its HasMultiplePages()), then
357 // [Shift-]Ctrl-Tab and Ctrl-PageUp/Down keys should iterate over its pages
358 // even if the focus is outside of the control because this is how the
359 // standard MSW properties dialogs behave and we do it under other platforms
360 // as well because it seems like a good idea -- but we can always put this
361 // block inside "#ifdef __WXMSW__" if it's not suitable there
362 if ( event
.IsWindowChange() && !goingDown
)
364 // check if we have a unique notebook-like child
365 wxWindow
*bookctrl
= NULL
;
366 for ( wxWindowList::const_iterator i
= children
.begin(),
367 end
= children
.end();
371 wxWindow
* const window
= *i
;
372 if ( window
->HasMultiplePages() )
376 // this is the second book-like control already so don't do
377 // anything as we don't know which one should have its page
389 // make sure that we don't bubble up the event again from the book
390 // control resulting in infinite recursion
391 wxNavigationKeyEvent
eventCopy(event
);
392 eventCopy
.SetEventObject(m_winParent
);
393 if ( bookctrl
->GetEventHandler()->ProcessEvent(eventCopy
) )
398 // there is not much to do if we don't have children and we're not
399 // interested in "notebook page change" events here
400 if ( !children
.GetCount() || event
.IsWindowChange() )
402 // let the parent process it unless it already comes from our parent
403 // of we don't have any
405 !parent
|| !parent
->GetEventHandler()->ProcessEvent(event
) )
413 // where are we going?
414 const bool forward
= event
.GetDirection();
416 // the node of the children list from which we should start looking for the
417 // next acceptable child
418 wxWindowList::compatibility_iterator node
, start_node
;
420 // we should start from the first/last control and not from the one which
421 // had focus the last time if we're propagating the event downwards because
422 // for our parent we look like a single control
425 // just to be sure it's not used (normally this is not necessary, but
426 // doesn't hurt neither)
427 m_winLastFocused
= NULL
;
429 // start from first or last depending on where we're going
430 node
= forward
? children
.GetFirst() : children
.GetLast();
434 // try to find the child which has the focus currently
436 // the event emitter might have done this for us
437 wxWindow
*winFocus
= event
.GetCurrentFocus();
439 // but if not, we might know where the focus was ourselves
441 winFocus
= m_winLastFocused
;
443 // if still no luck, do it the hard way
445 winFocus
= wxWindow::FindFocus();
449 #if defined(__WXMSW__) && wxUSE_RADIOBTN
450 // If we are in a radio button group, start from the first item in the
452 if ( event
.IsFromTab() && wxIsKindOf(winFocus
, wxRadioButton
) )
453 winFocus
= wxGetFirstButtonInGroup((wxRadioButton
*)winFocus
);
455 // ok, we found the focus - now is it our child?
456 start_node
= children
.Find( winFocus
);
459 if ( !start_node
&& m_winLastFocused
)
461 // window which has focus isn't our child, fall back to the one
462 // which had the focus the last time
463 start_node
= children
.Find( m_winLastFocused
);
466 // if we still didn't find anything, we should start with the first one
469 start_node
= children
.GetFirst();
472 // and the first child which we can try setting focus to is the next or
474 node
= forward
? start_node
->GetNext() : start_node
->GetPrevious();
477 // we want to cycle over all elements passing by NULL
480 // don't go into infinite loop
481 if ( start_node
&& node
&& node
== start_node
)
484 // Have we come to the last or first item on the panel?
489 // exit now as otherwise we'd loop forever
495 // Check if our (maybe grand) parent is another panel: if this
496 // is the case, they will know what to do with this navigation
497 // key and so give them the chance to process it instead of
498 // looping inside this panel (normally, the focus will go to
499 // the next/previous item after this panel in the parent
501 wxWindow
*focusedParent
= m_winParent
;
504 // We don't want to tab into a different dialog or frame or
505 // even an MDI child frame, so test for this explicitly
506 // (and in particular don't just use IsTopLevel() which
507 // would return false in the latter case).
508 if ( focusedParent
->IsTopNavigationDomain() )
511 event
.SetCurrentFocus( focusedParent
);
512 if ( parent
->GetEventHandler()->ProcessEvent( event
) )
515 focusedParent
= parent
;
517 parent
= parent
->GetParent();
520 //else: as the focus came from our parent, we definitely don't want
521 // to send it back to it!
523 // no, we are not inside another panel so process this ourself
524 node
= forward
? children
.GetFirst() : children
.GetLast();
529 wxWindow
*child
= node
->GetData();
531 // don't TAB to another TLW
532 if ( child
->IsTopLevel() )
534 node
= forward
? node
->GetNext() : node
->GetPrevious();
539 #if defined(__WXMSW__) && wxUSE_RADIOBTN
540 if ( event
.IsFromTab() )
542 if ( wxIsKindOf(child
, wxRadioButton
) )
544 // only radio buttons with either wxRB_GROUP or wxRB_SINGLE
546 if ( child
->HasFlag(wxRB_GROUP
) )
548 // need to tab into the active button within a group
549 wxRadioButton
*rb
= wxGetSelectedButtonInGroup((wxRadioButton
*)child
);
553 else if ( !child
->HasFlag(wxRB_SINGLE
) )
555 node
= forward
? node
->GetNext() : node
->GetPrevious();
560 else if ( m_winLastFocused
&&
561 wxIsKindOf(m_winLastFocused
, wxRadioButton
) &&
562 !m_winLastFocused
->HasFlag(wxRB_SINGLE
) )
564 wxRadioButton
* const
565 lastBtn
= static_cast<wxRadioButton
*>(m_winLastFocused
);
567 // cursor keys don't navigate out of a radio button group so
568 // find the correct radio button to focus
571 child
= wxGetNextButtonInGroup(lastBtn
);
574 // no next button in group, set it to the first button
575 child
= wxGetFirstButtonInGroup(lastBtn
);
580 child
= wxGetPreviousButtonInGroup(lastBtn
);
583 // no previous button in group, set it to the last button
584 child
= wxGetLastButtonInGroup(lastBtn
);
588 if ( child
== m_winLastFocused
)
590 // must be a group consisting of only one button therefore
591 // no need to send a navigation event
598 if ( child
->CanAcceptFocusFromKeyboard() )
600 // if we're setting the focus to a child panel we should prevent it
601 // from giving it to the child which had the focus the last time
602 // and instead give it to the first/last child depending from which
603 // direction we're coming
604 event
.SetEventObject(m_winParent
);
606 // disable propagation for this call as otherwise the event might
607 // bounce back to us.
608 wxPropagationDisabler
disableProp(event
);
609 if ( !child
->GetEventHandler()->ProcessEvent(event
) )
611 // set it first in case SetFocusFromKbd() results in focus
613 m_winLastFocused
= child
;
615 // everything is simple: just give focus to it
616 child
->SetFocusFromKbd();
618 //else: the child manages its focus itself
625 node
= forward
? node
->GetNext() : node
->GetPrevious();
628 // we cycled through all of our children and none of them wanted to accept
633 void wxControlContainer::HandleOnWindowDestroy(wxWindowBase
*child
)
635 if ( child
== m_winLastFocused
)
636 m_winLastFocused
= NULL
;
639 // ----------------------------------------------------------------------------
641 // ----------------------------------------------------------------------------
643 void wxControlContainer::HandleOnFocus(wxFocusEvent
& event
)
645 wxLogTrace(TRACE_FOCUS
, wxT("OnFocus on wxPanel 0x%p, name: %s"),
646 m_winParent
->GetHandle(),
647 m_winParent
->GetName().c_str() );
656 // wxHAS_NATIVE_TAB_TRAVERSAL
658 bool wxControlContainer::SetFocusToChild()
660 return wxSetFocusToChild(m_winParent
, NULL
);
664 #endif // !wxHAS_NATIVE_TAB_TRAVERSAL
666 // ----------------------------------------------------------------------------
667 // SetFocusToChild(): this function is used by wxPanel but also by wxFrame in
668 // wxMSW, this is why it is outside of wxControlContainer class
669 // ----------------------------------------------------------------------------
671 bool wxSetFocusToChild(wxWindow
*win
, wxWindow
**childLastFocused
)
673 wxCHECK_MSG( win
, false, wxT("wxSetFocusToChild(): invalid window") );
674 // wxCHECK_MSG( childLastFocused, false,
675 // wxT("wxSetFocusToChild(): NULL child poonter") );
677 if ( childLastFocused
&& *childLastFocused
)
679 // It might happen that the window got reparented
680 if ( (*childLastFocused
)->GetParent() == win
)
682 // And it also could have become hidden in the meanwhile
683 // We want to focus on the deepest widget visible
684 wxWindow
*deepestVisibleWindow
= NULL
;
686 while ( *childLastFocused
)
688 if ( (*childLastFocused
)->IsShown() )
690 if ( !deepestVisibleWindow
)
691 deepestVisibleWindow
= *childLastFocused
;
694 deepestVisibleWindow
= NULL
;
696 *childLastFocused
= (*childLastFocused
)->GetParent();
699 if ( deepestVisibleWindow
)
701 *childLastFocused
= deepestVisibleWindow
;
703 wxLogTrace(TRACE_FOCUS
,
704 wxT("SetFocusToChild() => last child (0x%p)."),
705 (*childLastFocused
)->GetHandle());
707 // not SetFocusFromKbd(): we're restoring focus back to the old
708 // window and not setting it as the result of a kbd action
709 (*childLastFocused
)->SetFocus();
715 // it doesn't count as such any more
716 *childLastFocused
= NULL
;
720 // set the focus to the first child who wants it
721 wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst();
724 wxWindow
*child
= node
->GetData();
725 node
= node
->GetNext();
727 // skip special windows:
728 if ( !win
->IsClientAreaChild(child
) )
731 if ( child
->CanAcceptFocusFromKeyboard() && !child
->IsTopLevel() )
733 #if defined(__WXMSW__) && wxUSE_RADIOBTN
734 // If a radiobutton is the first focusable child, search for the
735 // selected radiobutton in the same group
736 wxRadioButton
* btn
= wxDynamicCast(child
, wxRadioButton
);
739 wxRadioButton
* selected
= wxGetSelectedButtonInGroup(btn
);
745 wxLogTrace(TRACE_FOCUS
,
746 wxT("SetFocusToChild() => first child (0x%p)."),
749 if (childLastFocused
)
750 *childLastFocused
= child
;
751 child
->SetFocusFromKbd();