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::SetFocusToChild()
158 return wxSetFocusToChild(m_winParent
, &m_winLastFocused
);
161 #ifndef wxHAS_NATIVE_TAB_TRAVERSAL
163 // ----------------------------------------------------------------------------
164 // generic wxControlContainer
165 // ----------------------------------------------------------------------------
167 wxControlContainer::wxControlContainer()
169 m_winLastFocused
= NULL
;
172 void wxControlContainer::SetLastFocus(wxWindow
*win
)
174 // the panel itself should never get the focus at all but if it does happen
175 // temporarily (as it seems to do under wxGTK), at the very least don't
176 // forget our previous m_winLastFocused
177 if ( win
!= m_winParent
)
179 // if we're setting the focus
182 // find the last _immediate_ child which got focus
183 wxWindow
*winParent
= win
;
184 while ( winParent
!= m_winParent
)
187 winParent
= win
->GetParent();
189 // Yes, this can happen, though in a totally pathological case.
190 // like when detaching a menubar from a frame with a child
191 // which has pushed itself as an event handler for the menubar.
194 wxASSERT_MSG( winParent
,
195 wxT("Setting last focus for a window that is not our child?") );
199 m_winLastFocused
= win
;
203 wxLogTrace(TRACE_FOCUS
, wxT("Set last focus to %s(%s)"),
204 win
->GetClassInfo()->GetClassName(),
205 win
->GetLabel().c_str());
209 wxLogTrace(TRACE_FOCUS
, wxT("No more last focus"));
214 // --------------------------------------------------------------------
215 // The following four functions are used to find other radio buttons
216 // within the same group. Used by wxSetFocusToChild on wxMSW
217 // --------------------------------------------------------------------
221 wxRadioButton
* wxGetPreviousButtonInGroup(wxRadioButton
*btn
)
223 if ( btn
->HasFlag(wxRB_GROUP
) || btn
->HasFlag(wxRB_SINGLE
) )
226 const wxWindowList
& siblings
= btn
->GetParent()->GetChildren();
227 wxWindowList::compatibility_iterator nodeThis
= siblings
.Find(btn
);
228 wxCHECK_MSG( nodeThis
, NULL
, wxT("radio button not a child of its parent?") );
230 // Iterate over all previous siblings until we find the next radio button
231 wxWindowList::compatibility_iterator nodeBefore
= nodeThis
->GetPrevious();
232 wxRadioButton
*prevBtn
= 0;
235 prevBtn
= wxDynamicCast(nodeBefore
->GetData(), wxRadioButton
);
239 nodeBefore
= nodeBefore
->GetPrevious();
242 if (!prevBtn
|| prevBtn
->HasFlag(wxRB_SINGLE
))
244 // no more buttons in group
251 wxRadioButton
* wxGetNextButtonInGroup(wxRadioButton
*btn
)
253 if (btn
->HasFlag(wxRB_SINGLE
))
256 const wxWindowList
& siblings
= btn
->GetParent()->GetChildren();
257 wxWindowList::compatibility_iterator nodeThis
= siblings
.Find(btn
);
258 wxCHECK_MSG( nodeThis
, NULL
, wxT("radio button not a child of its parent?") );
260 // Iterate over all previous siblings until we find the next radio button
261 wxWindowList::compatibility_iterator nodeNext
= nodeThis
->GetNext();
262 wxRadioButton
*nextBtn
= 0;
265 nextBtn
= wxDynamicCast(nodeNext
->GetData(), wxRadioButton
);
269 nodeNext
= nodeNext
->GetNext();
272 if ( !nextBtn
|| nextBtn
->HasFlag(wxRB_GROUP
) || nextBtn
->HasFlag(wxRB_SINGLE
) )
274 // no more buttons or the first button of the next group
281 wxRadioButton
* wxGetFirstButtonInGroup(wxRadioButton
*btn
)
285 wxRadioButton
* prevBtn
= wxGetPreviousButtonInGroup(btn
);
293 wxRadioButton
* wxGetLastButtonInGroup(wxRadioButton
*btn
)
297 wxRadioButton
* nextBtn
= wxGetNextButtonInGroup(btn
);
305 wxRadioButton
* wxGetSelectedButtonInGroup(wxRadioButton
*btn
)
307 // Find currently selected button
311 if (btn
->HasFlag(wxRB_SINGLE
))
314 wxRadioButton
*selBtn
;
316 // First check all previous buttons
317 for (selBtn
= wxGetPreviousButtonInGroup(btn
); selBtn
; selBtn
= wxGetPreviousButtonInGroup(selBtn
))
318 if (selBtn
->GetValue())
321 // Now all following buttons
322 for (selBtn
= wxGetNextButtonInGroup(btn
); selBtn
; selBtn
= wxGetNextButtonInGroup(selBtn
))
323 if (selBtn
->GetValue())
331 // ----------------------------------------------------------------------------
332 // Keyboard handling - this is the place where the TAB traversal logic is
333 // implemented. As this code is common to all ports, this ensures consistent
334 // behaviour even if we don't specify how exactly the wxNavigationKeyEvent are
335 // generated and this is done in platform specific code which also ensures that
336 // we can follow the given platform standards.
337 // ----------------------------------------------------------------------------
339 void wxControlContainer::HandleOnNavigationKey( wxNavigationKeyEvent
& event
)
341 // for a TLW we shouldn't involve the parent window, it has nothing to do
342 // with keyboard navigation inside this TLW
343 wxWindow
*parent
= m_winParent
->IsTopLevel() ? NULL
344 : m_winParent
->GetParent();
346 // the event is propagated downwards if the event emitter was our parent
347 bool goingDown
= event
.GetEventObject() == parent
;
349 const wxWindowList
& children
= m_winParent
->GetChildren();
351 // if we have exactly one notebook-like child window (actually it could be
352 // any window that returns true from its HasMultiplePages()), then
353 // [Shift-]Ctrl-Tab and Ctrl-PageUp/Down keys should iterate over its pages
354 // even if the focus is outside of the control because this is how the
355 // standard MSW properties dialogs behave and we do it under other platforms
356 // as well because it seems like a good idea -- but we can always put this
357 // block inside "#ifdef __WXMSW__" if it's not suitable there
358 if ( event
.IsWindowChange() && !goingDown
)
360 // check if we have a unique notebook-like child
361 wxWindow
*bookctrl
= NULL
;
362 for ( wxWindowList::const_iterator i
= children
.begin(),
363 end
= children
.end();
367 wxWindow
* const window
= *i
;
368 if ( window
->HasMultiplePages() )
372 // this is the second book-like control already so don't do
373 // anything as we don't know which one should have its page
385 // make sure that we don't bubble up the event again from the book
386 // control resulting in infinite recursion
387 wxNavigationKeyEvent
eventCopy(event
);
388 eventCopy
.SetEventObject(m_winParent
);
389 if ( bookctrl
->GetEventHandler()->ProcessEvent(eventCopy
) )
394 // there is not much to do if we don't have children and we're not
395 // interested in "notebook page change" events here
396 if ( !children
.GetCount() || event
.IsWindowChange() )
398 // let the parent process it unless it already comes from our parent
399 // of we don't have any
401 !parent
|| !parent
->GetEventHandler()->ProcessEvent(event
) )
409 // where are we going?
410 const bool forward
= event
.GetDirection();
412 // the node of the children list from which we should start looking for the
413 // next acceptable child
414 wxWindowList::compatibility_iterator node
, start_node
;
416 // we should start from the first/last control and not from the one which
417 // had focus the last time if we're propagating the event downwards because
418 // for our parent we look like a single control
421 // just to be sure it's not used (normally this is not necessary, but
422 // doesn't hurt neither)
423 m_winLastFocused
= NULL
;
425 // start from first or last depending on where we're going
426 node
= forward
? children
.GetFirst() : children
.GetLast();
430 // try to find the child which has the focus currently
432 // the event emitter might have done this for us
433 wxWindow
*winFocus
= event
.GetCurrentFocus();
435 // but if not, we might know where the focus was ourselves
437 winFocus
= m_winLastFocused
;
439 // if still no luck, do it the hard way
441 winFocus
= wxWindow::FindFocus();
445 #if defined(__WXMSW__) && wxUSE_RADIOBTN
446 // If we are in a radio button group, start from the first item in the
448 if ( event
.IsFromTab() && wxIsKindOf(winFocus
, wxRadioButton
) )
449 winFocus
= wxGetFirstButtonInGroup((wxRadioButton
*)winFocus
);
451 // ok, we found the focus - now is it our child?
452 start_node
= children
.Find( winFocus
);
455 if ( !start_node
&& m_winLastFocused
)
457 // window which has focus isn't our child, fall back to the one
458 // which had the focus the last time
459 start_node
= children
.Find( m_winLastFocused
);
462 // if we still didn't find anything, we should start with the first one
465 start_node
= children
.GetFirst();
468 // and the first child which we can try setting focus to is the next or
470 node
= forward
? start_node
->GetNext() : start_node
->GetPrevious();
473 // we want to cycle over all elements passing by NULL
476 // don't go into infinite loop
477 if ( start_node
&& node
&& node
== start_node
)
480 // Have we come to the last or first item on the panel?
485 // exit now as otherwise we'd loop forever
491 // Check if our (maybe grand) parent is another panel: if this
492 // is the case, they will know what to do with this navigation
493 // key and so give them the chance to process it instead of
494 // looping inside this panel (normally, the focus will go to
495 // the next/previous item after this panel in the parent
497 wxWindow
*focusedParent
= m_winParent
;
500 // We don't want to tab into a different dialog or frame or
501 // even an MDI child frame, so test for this explicitly
502 // (and in particular don't just use IsTopLevel() which
503 // would return false in the latter case).
504 if ( focusedParent
->IsTopNavigationDomain() )
507 event
.SetCurrentFocus( focusedParent
);
508 if ( parent
->GetEventHandler()->ProcessEvent( event
) )
511 focusedParent
= parent
;
513 parent
= parent
->GetParent();
516 //else: as the focus came from our parent, we definitely don't want
517 // to send it back to it!
519 // no, we are not inside another panel so process this ourself
520 node
= forward
? children
.GetFirst() : children
.GetLast();
525 wxWindow
*child
= node
->GetData();
527 // don't TAB to another TLW
528 if ( child
->IsTopLevel() )
530 node
= forward
? node
->GetNext() : node
->GetPrevious();
535 #if defined(__WXMSW__) && wxUSE_RADIOBTN
536 if ( event
.IsFromTab() )
538 if ( wxIsKindOf(child
, wxRadioButton
) )
540 // only radio buttons with either wxRB_GROUP or wxRB_SINGLE
542 if ( child
->HasFlag(wxRB_GROUP
) )
544 // need to tab into the active button within a group
545 wxRadioButton
*rb
= wxGetSelectedButtonInGroup((wxRadioButton
*)child
);
549 else if ( !child
->HasFlag(wxRB_SINGLE
) )
551 node
= forward
? node
->GetNext() : node
->GetPrevious();
556 else if ( m_winLastFocused
&&
557 wxIsKindOf(m_winLastFocused
, wxRadioButton
) &&
558 !m_winLastFocused
->HasFlag(wxRB_SINGLE
) )
560 wxRadioButton
* const
561 lastBtn
= static_cast<wxRadioButton
*>(m_winLastFocused
);
563 // cursor keys don't navigate out of a radio button group so
564 // find the correct radio button to focus
567 child
= wxGetNextButtonInGroup(lastBtn
);
570 // no next button in group, set it to the first button
571 child
= wxGetFirstButtonInGroup(lastBtn
);
576 child
= wxGetPreviousButtonInGroup(lastBtn
);
579 // no previous button in group, set it to the last button
580 child
= wxGetLastButtonInGroup(lastBtn
);
584 if ( child
== m_winLastFocused
)
586 // must be a group consisting of only one button therefore
587 // no need to send a navigation event
594 if ( child
->CanAcceptFocusFromKeyboard() )
596 // if we're setting the focus to a child panel we should prevent it
597 // from giving it to the child which had the focus the last time
598 // and instead give it to the first/last child depending from which
599 // direction we're coming
600 event
.SetEventObject(m_winParent
);
602 // disable propagation for this call as otherwise the event might
603 // bounce back to us.
604 wxPropagationDisabler
disableProp(event
);
605 if ( !child
->GetEventHandler()->ProcessEvent(event
) )
607 // set it first in case SetFocusFromKbd() results in focus
609 m_winLastFocused
= child
;
611 // everything is simple: just give focus to it
612 child
->SetFocusFromKbd();
614 //else: the child manages its focus itself
621 node
= forward
? node
->GetNext() : node
->GetPrevious();
624 // we cycled through all of our children and none of them wanted to accept
629 void wxControlContainer::HandleOnWindowDestroy(wxWindowBase
*child
)
631 if ( child
== m_winLastFocused
)
632 m_winLastFocused
= NULL
;
635 // ----------------------------------------------------------------------------
637 // ----------------------------------------------------------------------------
639 void wxControlContainer::HandleOnFocus(wxFocusEvent
& event
)
641 wxLogTrace(TRACE_FOCUS
, wxT("OnFocus on wxPanel 0x%p, name: %s"),
642 m_winParent
->GetHandle(),
643 m_winParent
->GetName().c_str() );
652 // wxHAS_NATIVE_TAB_TRAVERSAL
654 bool wxControlContainer::SetFocusToChild()
656 return wxSetFocusToChild(m_winParent
, NULL
);
660 #endif // !wxHAS_NATIVE_TAB_TRAVERSAL
662 // ----------------------------------------------------------------------------
663 // SetFocusToChild(): this function is used by wxPanel but also by wxFrame in
664 // wxMSW, this is why it is outside of wxControlContainer class
665 // ----------------------------------------------------------------------------
667 bool wxSetFocusToChild(wxWindow
*win
, wxWindow
**childLastFocused
)
669 wxCHECK_MSG( win
, false, wxT("wxSetFocusToChild(): invalid window") );
670 // wxCHECK_MSG( childLastFocused, false,
671 // wxT("wxSetFocusToChild(): NULL child poonter") );
673 if ( childLastFocused
&& *childLastFocused
)
675 // It might happen that the window got reparented
676 if ( (*childLastFocused
)->GetParent() == win
)
678 // And it also could have become hidden in the meanwhile
679 // We want to focus on the deepest widget visible
680 wxWindow
*deepestVisibleWindow
= NULL
;
682 while ( *childLastFocused
)
684 if ( (*childLastFocused
)->IsShown() )
686 if ( !deepestVisibleWindow
)
687 deepestVisibleWindow
= *childLastFocused
;
690 deepestVisibleWindow
= NULL
;
692 *childLastFocused
= (*childLastFocused
)->GetParent();
695 if ( deepestVisibleWindow
)
697 *childLastFocused
= deepestVisibleWindow
;
699 wxLogTrace(TRACE_FOCUS
,
700 wxT("SetFocusToChild() => last child (0x%p)."),
701 (*childLastFocused
)->GetHandle());
703 // not SetFocusFromKbd(): we're restoring focus back to the old
704 // window and not setting it as the result of a kbd action
705 (*childLastFocused
)->SetFocus();
711 // it doesn't count as such any more
712 *childLastFocused
= NULL
;
716 // set the focus to the first child who wants it
717 wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst();
720 wxWindow
*child
= node
->GetData();
721 node
= node
->GetNext();
723 // skip special windows:
724 if ( !win
->IsClientAreaChild(child
) )
727 if ( child
->CanAcceptFocusFromKeyboard() && !child
->IsTopLevel() )
729 #if defined(__WXMSW__) && wxUSE_RADIOBTN
730 // If a radiobutton is the first focusable child, search for the
731 // selected radiobutton in the same group
732 wxRadioButton
* btn
= wxDynamicCast(child
, wxRadioButton
);
735 wxRadioButton
* selected
= wxGetSelectedButtonInGroup(btn
);
741 wxLogTrace(TRACE_FOCUS
,
742 wxT("SetFocusToChild() => first child (0x%p)."),
745 if (childLastFocused
)
746 *childLastFocused
= child
;
747 child
->SetFocusFromKbd();