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 bool wxControlContainerBase::UpdateCanFocusChildren()
52 const bool acceptsFocusChildren
= HasAnyFocusableChildren();
53 if ( acceptsFocusChildren
!= m_acceptsFocusChildren
)
55 m_acceptsFocusChildren
= acceptsFocusChildren
;
57 // In the ports where it does something non trivial, the parent window
58 // should only be focusable if it doesn't have any focusable children
59 // (e.g. native focus handling in wxGTK totally breaks down otherwise).
60 m_winParent
->SetCanFocus(m_acceptsFocusSelf
&& !m_acceptsFocusChildren
);
63 return m_acceptsFocusChildren
;
66 bool wxControlContainerBase::HasAnyFocusableChildren() const
68 const wxWindowList
& children
= m_winParent
->GetChildren();
69 for ( wxWindowList::const_iterator i
= children
.begin(),
74 const wxWindow
* const child
= *i
;
76 if ( !m_winParent
->IsClientAreaChild(child
) )
79 if ( child
->CanAcceptFocus() )
86 bool wxControlContainerBase::DoSetFocus()
88 wxLogTrace(TRACE_FOCUS
, wxT("SetFocus on wxPanel 0x%p."),
89 m_winParent
->GetHandle());
94 // when the panel gets the focus we move the focus to either the last
95 // window that had the focus or the first one that can get it unless the
96 // focus had been already set to some other child
98 wxWindow
*win
= wxWindow::FindFocus();
101 if ( win
== m_winParent
)
103 // our child already has focus, don't take it away from it
107 if ( win
->IsTopLevel() )
109 // don't look beyond the first top level parent - useless and
114 win
= win
->GetParent();
117 // protect against infinite recursion:
120 bool ret
= SetFocusToChild();
122 m_inSetFocus
= false;
127 bool wxControlContainerBase::SetFocusToChild()
129 return wxSetFocusToChild(m_winParent
, &m_winLastFocused
);
132 #ifndef wxHAS_NATIVE_TAB_TRAVERSAL
134 // ----------------------------------------------------------------------------
135 // generic wxControlContainer
136 // ----------------------------------------------------------------------------
138 wxControlContainer::wxControlContainer()
140 m_winLastFocused
= NULL
;
143 void wxControlContainer::SetLastFocus(wxWindow
*win
)
145 // the panel itself should never get the focus at all but if it does happen
146 // temporarily (as it seems to do under wxGTK), at the very least don't
147 // forget our previous m_winLastFocused
148 if ( win
!= m_winParent
)
150 // if we're setting the focus
153 // find the last _immediate_ child which got focus
154 wxWindow
*winParent
= win
;
155 while ( winParent
!= m_winParent
)
158 winParent
= win
->GetParent();
160 // Yes, this can happen, though in a totally pathological case.
161 // like when detaching a menubar from a frame with a child
162 // which has pushed itself as an event handler for the menubar.
165 wxASSERT_MSG( winParent
,
166 wxT("Setting last focus for a window that is not our child?") );
170 m_winLastFocused
= win
;
174 wxLogTrace(TRACE_FOCUS
, wxT("Set last focus to %s(%s)"),
175 win
->GetClassInfo()->GetClassName(),
176 win
->GetLabel().c_str());
180 wxLogTrace(TRACE_FOCUS
, wxT("No more last focus"));
185 // --------------------------------------------------------------------
186 // The following four functions are used to find other radio buttons
187 // within the same group. Used by wxSetFocusToChild on wxMSW
188 // --------------------------------------------------------------------
190 #if defined(__WXMSW__) && wxUSE_RADIOBTN
192 wxRadioButton
* wxGetPreviousButtonInGroup(wxRadioButton
*btn
)
194 if ( btn
->HasFlag(wxRB_GROUP
) || btn
->HasFlag(wxRB_SINGLE
) )
197 const wxWindowList
& siblings
= btn
->GetParent()->GetChildren();
198 wxWindowList::compatibility_iterator nodeThis
= siblings
.Find(btn
);
199 wxCHECK_MSG( nodeThis
, NULL
, wxT("radio button not a child of its parent?") );
201 // Iterate over all previous siblings until we find the next radio button
202 wxWindowList::compatibility_iterator nodeBefore
= nodeThis
->GetPrevious();
203 wxRadioButton
*prevBtn
= 0;
206 prevBtn
= wxDynamicCast(nodeBefore
->GetData(), wxRadioButton
);
210 nodeBefore
= nodeBefore
->GetPrevious();
213 if (!prevBtn
|| prevBtn
->HasFlag(wxRB_SINGLE
))
215 // no more buttons in group
222 wxRadioButton
* wxGetNextButtonInGroup(wxRadioButton
*btn
)
224 if (btn
->HasFlag(wxRB_SINGLE
))
227 const wxWindowList
& siblings
= btn
->GetParent()->GetChildren();
228 wxWindowList::compatibility_iterator nodeThis
= siblings
.Find(btn
);
229 wxCHECK_MSG( nodeThis
, NULL
, wxT("radio button not a child of its parent?") );
231 // Iterate over all previous siblings until we find the next radio button
232 wxWindowList::compatibility_iterator nodeNext
= nodeThis
->GetNext();
233 wxRadioButton
*nextBtn
= 0;
236 nextBtn
= wxDynamicCast(nodeNext
->GetData(), wxRadioButton
);
240 nodeNext
= nodeNext
->GetNext();
243 if ( !nextBtn
|| nextBtn
->HasFlag(wxRB_GROUP
) || nextBtn
->HasFlag(wxRB_SINGLE
) )
245 // no more buttons or the first button of the next group
252 wxRadioButton
* wxGetFirstButtonInGroup(wxRadioButton
*btn
)
256 wxRadioButton
* prevBtn
= wxGetPreviousButtonInGroup(btn
);
264 wxRadioButton
* wxGetLastButtonInGroup(wxRadioButton
*btn
)
268 wxRadioButton
* nextBtn
= wxGetNextButtonInGroup(btn
);
276 wxRadioButton
* wxGetSelectedButtonInGroup(wxRadioButton
*btn
)
278 // Find currently selected button
282 if (btn
->HasFlag(wxRB_SINGLE
))
285 wxRadioButton
*selBtn
;
287 // First check all previous buttons
288 for (selBtn
= wxGetPreviousButtonInGroup(btn
); selBtn
; selBtn
= wxGetPreviousButtonInGroup(selBtn
))
289 if (selBtn
->GetValue())
292 // Now all following buttons
293 for (selBtn
= wxGetNextButtonInGroup(btn
); selBtn
; selBtn
= wxGetNextButtonInGroup(selBtn
))
294 if (selBtn
->GetValue())
302 // ----------------------------------------------------------------------------
303 // Keyboard handling - this is the place where the TAB traversal logic is
304 // implemented. As this code is common to all ports, this ensures consistent
305 // behaviour even if we don't specify how exactly the wxNavigationKeyEvent are
306 // generated and this is done in platform specific code which also ensures that
307 // we can follow the given platform standards.
308 // ----------------------------------------------------------------------------
310 void wxControlContainer::HandleOnNavigationKey( wxNavigationKeyEvent
& event
)
312 // for a TLW we shouldn't involve the parent window, it has nothing to do
313 // with keyboard navigation inside this TLW
314 wxWindow
*parent
= m_winParent
->IsTopLevel() ? NULL
315 : m_winParent
->GetParent();
317 // the event is propagated downwards if the event emitter was our parent
318 bool goingDown
= event
.GetEventObject() == parent
;
320 const wxWindowList
& children
= m_winParent
->GetChildren();
322 // if we have exactly one notebook-like child window (actually it could be
323 // any window that returns true from its HasMultiplePages()), then
324 // [Shift-]Ctrl-Tab and Ctrl-PageUp/Down keys should iterate over its pages
325 // even if the focus is outside of the control because this is how the
326 // standard MSW properties dialogs behave and we do it under other platforms
327 // as well because it seems like a good idea -- but we can always put this
328 // block inside "#ifdef __WXMSW__" if it's not suitable there
329 if ( event
.IsWindowChange() && !goingDown
)
331 // check if we have a unique notebook-like child
332 wxWindow
*bookctrl
= NULL
;
333 for ( wxWindowList::const_iterator i
= children
.begin(),
334 end
= children
.end();
338 wxWindow
* const window
= *i
;
339 if ( window
->HasMultiplePages() )
343 // this is the second book-like control already so don't do
344 // anything as we don't know which one should have its page
356 // make sure that we don't bubble up the event again from the book
357 // control resulting in infinite recursion
358 wxNavigationKeyEvent
eventCopy(event
);
359 eventCopy
.SetEventObject(m_winParent
);
360 if ( bookctrl
->GetEventHandler()->ProcessEvent(eventCopy
) )
365 // there is not much to do if we don't have children and we're not
366 // interested in "notebook page change" events here
367 if ( !children
.GetCount() || event
.IsWindowChange() )
369 // let the parent process it unless it already comes from our parent
370 // of we don't have any
372 !parent
|| !parent
->GetEventHandler()->ProcessEvent(event
) )
380 // where are we going?
381 const bool forward
= event
.GetDirection();
383 // the node of the children list from which we should start looking for the
384 // next acceptable child
385 wxWindowList::compatibility_iterator node
, start_node
;
387 // we should start from the first/last control and not from the one which
388 // had focus the last time if we're propagating the event downwards because
389 // for our parent we look like a single control
392 // just to be sure it's not used (normally this is not necessary, but
393 // doesn't hurt neither)
394 m_winLastFocused
= NULL
;
396 // start from first or last depending on where we're going
397 node
= forward
? children
.GetFirst() : children
.GetLast();
401 // try to find the child which has the focus currently
403 // the event emitter might have done this for us
404 wxWindow
*winFocus
= event
.GetCurrentFocus();
406 // but if not, we might know where the focus was ourselves
408 winFocus
= m_winLastFocused
;
410 // if still no luck, do it the hard way
412 winFocus
= wxWindow::FindFocus();
416 #if defined(__WXMSW__) && wxUSE_RADIOBTN
417 // If we are in a radio button group, start from the first item in the
419 if ( event
.IsFromTab() && wxIsKindOf(winFocus
, wxRadioButton
) )
420 winFocus
= wxGetFirstButtonInGroup((wxRadioButton
*)winFocus
);
422 // ok, we found the focus - now is it our child?
423 start_node
= children
.Find( winFocus
);
426 if ( !start_node
&& m_winLastFocused
)
428 // window which has focus isn't our child, fall back to the one
429 // which had the focus the last time
430 start_node
= children
.Find( m_winLastFocused
);
433 // if we still didn't find anything, we should start with the first one
436 start_node
= children
.GetFirst();
439 // and the first child which we can try setting focus to is the next or
441 node
= forward
? start_node
->GetNext() : start_node
->GetPrevious();
444 // we want to cycle over all elements passing by NULL
447 // don't go into infinite loop
448 if ( start_node
&& node
&& node
== start_node
)
451 // Have we come to the last or first item on the panel?
456 // exit now as otherwise we'd loop forever
462 // Check if our (maybe grand) parent is another panel: if this
463 // is the case, they will know what to do with this navigation
464 // key and so give them the chance to process it instead of
465 // looping inside this panel (normally, the focus will go to
466 // the next/previous item after this panel in the parent
468 wxWindow
*focusedParent
= m_winParent
;
471 // We don't want to tab into a different dialog or frame or
472 // even an MDI child frame, so test for this explicitly
473 // (and in particular don't just use IsTopLevel() which
474 // would return false in the latter case).
475 if ( focusedParent
->IsTopNavigationDomain() )
478 event
.SetCurrentFocus( focusedParent
);
479 if ( parent
->GetEventHandler()->ProcessEvent( event
) )
482 focusedParent
= parent
;
484 parent
= parent
->GetParent();
487 //else: as the focus came from our parent, we definitely don't want
488 // to send it back to it!
490 // no, we are not inside another panel so process this ourself
491 node
= forward
? children
.GetFirst() : children
.GetLast();
496 wxWindow
*child
= node
->GetData();
498 // don't TAB to another TLW
499 if ( child
->IsTopLevel() )
501 node
= forward
? node
->GetNext() : node
->GetPrevious();
506 #if defined(__WXMSW__) && wxUSE_RADIOBTN
507 if ( event
.IsFromTab() )
509 if ( wxIsKindOf(child
, wxRadioButton
) )
511 // only radio buttons with either wxRB_GROUP or wxRB_SINGLE
513 if ( child
->HasFlag(wxRB_GROUP
) )
515 // need to tab into the active button within a group
516 wxRadioButton
*rb
= wxGetSelectedButtonInGroup((wxRadioButton
*)child
);
520 else if ( !child
->HasFlag(wxRB_SINGLE
) )
522 node
= forward
? node
->GetNext() : node
->GetPrevious();
527 else if ( m_winLastFocused
&&
528 wxIsKindOf(m_winLastFocused
, wxRadioButton
) &&
529 !m_winLastFocused
->HasFlag(wxRB_SINGLE
) )
531 wxRadioButton
* const
532 lastBtn
= static_cast<wxRadioButton
*>(m_winLastFocused
);
534 // cursor keys don't navigate out of a radio button group so
535 // find the correct radio button to focus
538 child
= wxGetNextButtonInGroup(lastBtn
);
541 // no next button in group, set it to the first button
542 child
= wxGetFirstButtonInGroup(lastBtn
);
547 child
= wxGetPreviousButtonInGroup(lastBtn
);
550 // no previous button in group, set it to the last button
551 child
= wxGetLastButtonInGroup(lastBtn
);
555 if ( child
== m_winLastFocused
)
557 // must be a group consisting of only one button therefore
558 // no need to send a navigation event
565 if ( child
->CanAcceptFocusFromKeyboard() )
567 // if we're setting the focus to a child panel we should prevent it
568 // from giving it to the child which had the focus the last time
569 // and instead give it to the first/last child depending from which
570 // direction we're coming
571 event
.SetEventObject(m_winParent
);
573 // disable propagation for this call as otherwise the event might
574 // bounce back to us.
575 wxPropagationDisabler
disableProp(event
);
576 if ( !child
->GetEventHandler()->ProcessEvent(event
) )
578 // set it first in case SetFocusFromKbd() results in focus
580 m_winLastFocused
= child
;
582 // everything is simple: just give focus to it
583 child
->SetFocusFromKbd();
585 //else: the child manages its focus itself
592 node
= forward
? node
->GetNext() : node
->GetPrevious();
595 // we cycled through all of our children and none of them wanted to accept
600 void wxControlContainer::HandleOnWindowDestroy(wxWindowBase
*child
)
602 if ( child
== m_winLastFocused
)
603 m_winLastFocused
= NULL
;
606 // ----------------------------------------------------------------------------
608 // ----------------------------------------------------------------------------
610 void wxControlContainer::HandleOnFocus(wxFocusEvent
& event
)
612 wxLogTrace(TRACE_FOCUS
, wxT("OnFocus on wxPanel 0x%p, name: %s"),
613 m_winParent
->GetHandle(),
614 m_winParent
->GetName().c_str() );
623 // wxHAS_NATIVE_TAB_TRAVERSAL
625 bool wxControlContainer::SetFocusToChild()
627 return wxSetFocusToChild(m_winParent
, NULL
);
631 #endif // !wxHAS_NATIVE_TAB_TRAVERSAL
633 // ----------------------------------------------------------------------------
634 // SetFocusToChild(): this function is used by wxPanel but also by wxFrame in
635 // wxMSW, this is why it is outside of wxControlContainer class
636 // ----------------------------------------------------------------------------
638 bool wxSetFocusToChild(wxWindow
*win
, wxWindow
**childLastFocused
)
640 wxCHECK_MSG( win
, false, wxT("wxSetFocusToChild(): invalid window") );
641 // wxCHECK_MSG( childLastFocused, false,
642 // wxT("wxSetFocusToChild(): NULL child poonter") );
644 if ( childLastFocused
&& *childLastFocused
)
646 // It might happen that the window got reparented
647 if ( (*childLastFocused
)->GetParent() == win
)
649 // And it also could have become hidden in the meanwhile
650 // We want to focus on the deepest widget visible
651 wxWindow
*deepestVisibleWindow
= NULL
;
653 while ( *childLastFocused
)
655 if ( (*childLastFocused
)->IsShown() )
657 if ( !deepestVisibleWindow
)
658 deepestVisibleWindow
= *childLastFocused
;
661 deepestVisibleWindow
= NULL
;
663 *childLastFocused
= (*childLastFocused
)->GetParent();
666 if ( deepestVisibleWindow
)
668 *childLastFocused
= deepestVisibleWindow
;
670 wxLogTrace(TRACE_FOCUS
,
671 wxT("SetFocusToChild() => last child (0x%p)."),
672 (*childLastFocused
)->GetHandle());
674 // not SetFocusFromKbd(): we're restoring focus back to the old
675 // window and not setting it as the result of a kbd action
676 (*childLastFocused
)->SetFocus();
682 // it doesn't count as such any more
683 *childLastFocused
= NULL
;
687 // set the focus to the first child who wants it
688 wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst();
691 wxWindow
*child
= node
->GetData();
692 node
= node
->GetNext();
694 // skip special windows:
695 if ( !win
->IsClientAreaChild(child
) )
698 if ( child
->CanAcceptFocusFromKeyboard() && !child
->IsTopLevel() )
700 #if defined(__WXMSW__) && wxUSE_RADIOBTN
701 // If a radiobutton is the first focusable child, search for the
702 // selected radiobutton in the same group
703 wxRadioButton
* btn
= wxDynamicCast(child
, wxRadioButton
);
706 wxRadioButton
* selected
= wxGetSelectedButtonInGroup(btn
);
712 wxLogTrace(TRACE_FOCUS
,
713 wxT("SetFocusToChild() => first child (0x%p)."),
716 if (childLastFocused
)
717 *childLastFocused
= child
;
718 child
->SetFocusFromKbd();