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 if ( child
->CanAcceptFocus() )
91 bool wxControlContainerBase::DoSetFocus()
93 wxLogTrace(TRACE_FOCUS
, wxT("SetFocus on wxPanel 0x%p."),
94 m_winParent
->GetHandle());
99 // when the panel gets the focus we move the focus to either the last
100 // window that had the focus or the first one that can get it unless the
101 // focus had been already set to some other child
103 wxWindow
*win
= wxWindow::FindFocus();
106 if ( win
== m_winParent
)
108 // our child already has focus, don't take it away from it
112 if ( win
->IsTopLevel() )
114 // don't look beyond the first top level parent - useless and
119 win
= win
->GetParent();
122 // protect against infinite recursion:
125 bool ret
= SetFocusToChild();
127 m_inSetFocus
= false;
132 bool wxControlContainerBase::SetFocusToChild()
134 return wxSetFocusToChild(m_winParent
, &m_winLastFocused
);
137 #ifndef wxHAS_NATIVE_TAB_TRAVERSAL
139 // ----------------------------------------------------------------------------
140 // generic wxControlContainer
141 // ----------------------------------------------------------------------------
143 wxControlContainer::wxControlContainer()
145 m_winLastFocused
= NULL
;
148 void wxControlContainer::SetLastFocus(wxWindow
*win
)
150 // the panel itself should never get the focus at all but if it does happen
151 // temporarily (as it seems to do under wxGTK), at the very least don't
152 // forget our previous m_winLastFocused
153 if ( win
!= m_winParent
)
155 // if we're setting the focus
158 // find the last _immediate_ child which got focus
159 wxWindow
*winParent
= win
;
160 while ( winParent
!= m_winParent
)
163 winParent
= win
->GetParent();
165 // Yes, this can happen, though in a totally pathological case.
166 // like when detaching a menubar from a frame with a child
167 // which has pushed itself as an event handler for the menubar.
170 wxASSERT_MSG( winParent
,
171 wxT("Setting last focus for a window that is not our child?") );
175 m_winLastFocused
= win
;
179 wxLogTrace(TRACE_FOCUS
, wxT("Set last focus to %s(%s)"),
180 win
->GetClassInfo()->GetClassName(),
181 win
->GetLabel().c_str());
185 wxLogTrace(TRACE_FOCUS
, wxT("No more last focus"));
190 // --------------------------------------------------------------------
191 // The following four functions are used to find other radio buttons
192 // within the same group. Used by wxSetFocusToChild on wxMSW
193 // --------------------------------------------------------------------
197 wxRadioButton
* wxGetPreviousButtonInGroup(wxRadioButton
*btn
)
199 if ( btn
->HasFlag(wxRB_GROUP
) || btn
->HasFlag(wxRB_SINGLE
) )
202 const wxWindowList
& siblings
= btn
->GetParent()->GetChildren();
203 wxWindowList::compatibility_iterator nodeThis
= siblings
.Find(btn
);
204 wxCHECK_MSG( nodeThis
, NULL
, wxT("radio button not a child of its parent?") );
206 // Iterate over all previous siblings until we find the next radio button
207 wxWindowList::compatibility_iterator nodeBefore
= nodeThis
->GetPrevious();
208 wxRadioButton
*prevBtn
= 0;
211 prevBtn
= wxDynamicCast(nodeBefore
->GetData(), wxRadioButton
);
215 nodeBefore
= nodeBefore
->GetPrevious();
218 if (!prevBtn
|| prevBtn
->HasFlag(wxRB_SINGLE
))
220 // no more buttons in group
227 wxRadioButton
* wxGetNextButtonInGroup(wxRadioButton
*btn
)
229 if (btn
->HasFlag(wxRB_SINGLE
))
232 const wxWindowList
& siblings
= btn
->GetParent()->GetChildren();
233 wxWindowList::compatibility_iterator nodeThis
= siblings
.Find(btn
);
234 wxCHECK_MSG( nodeThis
, NULL
, wxT("radio button not a child of its parent?") );
236 // Iterate over all previous siblings until we find the next radio button
237 wxWindowList::compatibility_iterator nodeNext
= nodeThis
->GetNext();
238 wxRadioButton
*nextBtn
= 0;
241 nextBtn
= wxDynamicCast(nodeNext
->GetData(), wxRadioButton
);
245 nodeNext
= nodeNext
->GetNext();
248 if ( !nextBtn
|| nextBtn
->HasFlag(wxRB_GROUP
) || nextBtn
->HasFlag(wxRB_SINGLE
) )
250 // no more buttons or the first button of the next group
257 wxRadioButton
* wxGetFirstButtonInGroup(wxRadioButton
*btn
)
261 wxRadioButton
* prevBtn
= wxGetPreviousButtonInGroup(btn
);
269 wxRadioButton
* wxGetLastButtonInGroup(wxRadioButton
*btn
)
273 wxRadioButton
* nextBtn
= wxGetNextButtonInGroup(btn
);
281 wxRadioButton
* wxGetSelectedButtonInGroup(wxRadioButton
*btn
)
283 // Find currently selected button
287 if (btn
->HasFlag(wxRB_SINGLE
))
290 wxRadioButton
*selBtn
;
292 // First check all previous buttons
293 for (selBtn
= wxGetPreviousButtonInGroup(btn
); selBtn
; selBtn
= wxGetPreviousButtonInGroup(selBtn
))
294 if (selBtn
->GetValue())
297 // Now all following buttons
298 for (selBtn
= wxGetNextButtonInGroup(btn
); selBtn
; selBtn
= wxGetNextButtonInGroup(selBtn
))
299 if (selBtn
->GetValue())
307 // ----------------------------------------------------------------------------
308 // Keyboard handling - this is the place where the TAB traversal logic is
309 // implemented. As this code is common to all ports, this ensures consistent
310 // behaviour even if we don't specify how exactly the wxNavigationKeyEvent are
311 // generated and this is done in platform specific code which also ensures that
312 // we can follow the given platform standards.
313 // ----------------------------------------------------------------------------
315 void wxControlContainer::HandleOnNavigationKey( wxNavigationKeyEvent
& event
)
317 // for a TLW we shouldn't involve the parent window, it has nothing to do
318 // with keyboard navigation inside this TLW
319 wxWindow
*parent
= m_winParent
->IsTopLevel() ? NULL
320 : m_winParent
->GetParent();
322 // the event is propagated downwards if the event emitter was our parent
323 bool goingDown
= event
.GetEventObject() == parent
;
325 const wxWindowList
& children
= m_winParent
->GetChildren();
327 // if we have exactly one notebook-like child window (actually it could be
328 // any window that returns true from its HasMultiplePages()), then
329 // [Shift-]Ctrl-Tab and Ctrl-PageUp/Down keys should iterate over its pages
330 // even if the focus is outside of the control because this is how the
331 // standard MSW properties dialogs behave and we do it under other platforms
332 // as well because it seems like a good idea -- but we can always put this
333 // block inside "#ifdef __WXMSW__" if it's not suitable there
334 if ( event
.IsWindowChange() && !goingDown
)
336 // check if we have a unique notebook-like child
337 wxWindow
*bookctrl
= NULL
;
338 for ( wxWindowList::const_iterator i
= children
.begin(),
339 end
= children
.end();
343 wxWindow
* const window
= *i
;
344 if ( window
->HasMultiplePages() )
348 // this is the second book-like control already so don't do
349 // anything as we don't know which one should have its page
361 // make sure that we don't bubble up the event again from the book
362 // control resulting in infinite recursion
363 wxNavigationKeyEvent
eventCopy(event
);
364 eventCopy
.SetEventObject(m_winParent
);
365 if ( bookctrl
->GetEventHandler()->ProcessEvent(eventCopy
) )
370 // there is not much to do if we don't have children and we're not
371 // interested in "notebook page change" events here
372 if ( !children
.GetCount() || event
.IsWindowChange() )
374 // let the parent process it unless it already comes from our parent
375 // of we don't have any
377 !parent
|| !parent
->GetEventHandler()->ProcessEvent(event
) )
385 // where are we going?
386 const bool forward
= event
.GetDirection();
388 // the node of the children list from which we should start looking for the
389 // next acceptable child
390 wxWindowList::compatibility_iterator node
, start_node
;
392 // we should start from the first/last control and not from the one which
393 // had focus the last time if we're propagating the event downwards because
394 // for our parent we look like a single control
397 // just to be sure it's not used (normally this is not necessary, but
398 // doesn't hurt neither)
399 m_winLastFocused
= NULL
;
401 // start from first or last depending on where we're going
402 node
= forward
? children
.GetFirst() : children
.GetLast();
406 // try to find the child which has the focus currently
408 // the event emitter might have done this for us
409 wxWindow
*winFocus
= event
.GetCurrentFocus();
411 // but if not, we might know where the focus was ourselves
413 winFocus
= m_winLastFocused
;
415 // if still no luck, do it the hard way
417 winFocus
= wxWindow::FindFocus();
421 #if defined(__WXMSW__) && wxUSE_RADIOBTN
422 // If we are in a radio button group, start from the first item in the
424 if ( event
.IsFromTab() && wxIsKindOf(winFocus
, wxRadioButton
) )
425 winFocus
= wxGetFirstButtonInGroup((wxRadioButton
*)winFocus
);
427 // ok, we found the focus - now is it our child?
428 start_node
= children
.Find( winFocus
);
431 if ( !start_node
&& m_winLastFocused
)
433 // window which has focus isn't our child, fall back to the one
434 // which had the focus the last time
435 start_node
= children
.Find( m_winLastFocused
);
438 // if we still didn't find anything, we should start with the first one
441 start_node
= children
.GetFirst();
444 // and the first child which we can try setting focus to is the next or
446 node
= forward
? start_node
->GetNext() : start_node
->GetPrevious();
449 // we want to cycle over all elements passing by NULL
452 // don't go into infinite loop
453 if ( start_node
&& node
&& node
== start_node
)
456 // Have we come to the last or first item on the panel?
461 // exit now as otherwise we'd loop forever
467 // Check if our (maybe grand) parent is another panel: if this
468 // is the case, they will know what to do with this navigation
469 // key and so give them the chance to process it instead of
470 // looping inside this panel (normally, the focus will go to
471 // the next/previous item after this panel in the parent
473 wxWindow
*focusedParent
= m_winParent
;
476 // We don't want to tab into a different dialog or frame or
477 // even an MDI child frame, so test for this explicitly
478 // (and in particular don't just use IsTopLevel() which
479 // would return false in the latter case).
480 if ( focusedParent
->IsTopNavigationDomain() )
483 event
.SetCurrentFocus( focusedParent
);
484 if ( parent
->GetEventHandler()->ProcessEvent( event
) )
487 focusedParent
= parent
;
489 parent
= parent
->GetParent();
492 //else: as the focus came from our parent, we definitely don't want
493 // to send it back to it!
495 // no, we are not inside another panel so process this ourself
496 node
= forward
? children
.GetFirst() : children
.GetLast();
501 wxWindow
*child
= node
->GetData();
503 // don't TAB to another TLW
504 if ( child
->IsTopLevel() )
506 node
= forward
? node
->GetNext() : node
->GetPrevious();
511 #if defined(__WXMSW__) && wxUSE_RADIOBTN
512 if ( event
.IsFromTab() )
514 if ( wxIsKindOf(child
, wxRadioButton
) )
516 // only radio buttons with either wxRB_GROUP or wxRB_SINGLE
518 if ( child
->HasFlag(wxRB_GROUP
) )
520 // need to tab into the active button within a group
521 wxRadioButton
*rb
= wxGetSelectedButtonInGroup((wxRadioButton
*)child
);
525 else if ( !child
->HasFlag(wxRB_SINGLE
) )
527 node
= forward
? node
->GetNext() : node
->GetPrevious();
532 else if ( m_winLastFocused
&&
533 wxIsKindOf(m_winLastFocused
, wxRadioButton
) &&
534 !m_winLastFocused
->HasFlag(wxRB_SINGLE
) )
536 wxRadioButton
* const
537 lastBtn
= static_cast<wxRadioButton
*>(m_winLastFocused
);
539 // cursor keys don't navigate out of a radio button group so
540 // find the correct radio button to focus
543 child
= wxGetNextButtonInGroup(lastBtn
);
546 // no next button in group, set it to the first button
547 child
= wxGetFirstButtonInGroup(lastBtn
);
552 child
= wxGetPreviousButtonInGroup(lastBtn
);
555 // no previous button in group, set it to the last button
556 child
= wxGetLastButtonInGroup(lastBtn
);
560 if ( child
== m_winLastFocused
)
562 // must be a group consisting of only one button therefore
563 // no need to send a navigation event
570 if ( child
->CanAcceptFocusFromKeyboard() )
572 // if we're setting the focus to a child panel we should prevent it
573 // from giving it to the child which had the focus the last time
574 // and instead give it to the first/last child depending from which
575 // direction we're coming
576 event
.SetEventObject(m_winParent
);
578 // disable propagation for this call as otherwise the event might
579 // bounce back to us.
580 wxPropagationDisabler
disableProp(event
);
581 if ( !child
->GetEventHandler()->ProcessEvent(event
) )
583 // set it first in case SetFocusFromKbd() results in focus
585 m_winLastFocused
= child
;
587 // everything is simple: just give focus to it
588 child
->SetFocusFromKbd();
590 //else: the child manages its focus itself
597 node
= forward
? node
->GetNext() : node
->GetPrevious();
600 // we cycled through all of our children and none of them wanted to accept
605 void wxControlContainer::HandleOnWindowDestroy(wxWindowBase
*child
)
607 if ( child
== m_winLastFocused
)
608 m_winLastFocused
= NULL
;
611 // ----------------------------------------------------------------------------
613 // ----------------------------------------------------------------------------
615 void wxControlContainer::HandleOnFocus(wxFocusEvent
& event
)
617 wxLogTrace(TRACE_FOCUS
, wxT("OnFocus on wxPanel 0x%p, name: %s"),
618 m_winParent
->GetHandle(),
619 m_winParent
->GetName().c_str() );
628 // wxHAS_NATIVE_TAB_TRAVERSAL
630 bool wxControlContainer::SetFocusToChild()
632 return wxSetFocusToChild(m_winParent
, NULL
);
636 #endif // !wxHAS_NATIVE_TAB_TRAVERSAL
638 // ----------------------------------------------------------------------------
639 // SetFocusToChild(): this function is used by wxPanel but also by wxFrame in
640 // wxMSW, this is why it is outside of wxControlContainer class
641 // ----------------------------------------------------------------------------
643 bool wxSetFocusToChild(wxWindow
*win
, wxWindow
**childLastFocused
)
645 wxCHECK_MSG( win
, false, wxT("wxSetFocusToChild(): invalid window") );
646 // wxCHECK_MSG( childLastFocused, false,
647 // wxT("wxSetFocusToChild(): NULL child poonter") );
649 if ( childLastFocused
&& *childLastFocused
)
651 // It might happen that the window got reparented
652 if ( (*childLastFocused
)->GetParent() == win
)
654 // And it also could have become hidden in the meanwhile
655 // We want to focus on the deepest widget visible
656 wxWindow
*deepestVisibleWindow
= NULL
;
658 while ( *childLastFocused
)
660 if ( (*childLastFocused
)->IsShown() )
662 if ( !deepestVisibleWindow
)
663 deepestVisibleWindow
= *childLastFocused
;
666 deepestVisibleWindow
= NULL
;
668 *childLastFocused
= (*childLastFocused
)->GetParent();
671 if ( deepestVisibleWindow
)
673 *childLastFocused
= deepestVisibleWindow
;
675 wxLogTrace(TRACE_FOCUS
,
676 wxT("SetFocusToChild() => last child (0x%p)."),
677 (*childLastFocused
)->GetHandle());
679 // not SetFocusFromKbd(): we're restoring focus back to the old
680 // window and not setting it as the result of a kbd action
681 (*childLastFocused
)->SetFocus();
687 // it doesn't count as such any more
688 *childLastFocused
= NULL
;
692 // set the focus to the first child who wants it
693 wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst();
696 wxWindow
*child
= node
->GetData();
697 node
= node
->GetNext();
699 // skip special windows:
700 if ( !win
->IsClientAreaChild(child
) )
703 if ( child
->CanAcceptFocusFromKeyboard() && !child
->IsTopLevel() )
705 #if defined(__WXMSW__) && wxUSE_RADIOBTN
706 // If a radiobutton is the first focusable child, search for the
707 // selected radiobutton in the same group
708 wxRadioButton
* btn
= wxDynamicCast(child
, wxRadioButton
);
711 wxRadioButton
* selected
= wxGetSelectedButtonInGroup(btn
);
717 wxLogTrace(TRACE_FOCUS
,
718 wxT("SetFocusToChild() => first child (0x%p)."),
721 if (childLastFocused
)
722 *childLastFocused
= child
;
723 child
->SetFocusFromKbd();