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 m_winParent
->SetCanFocus(AcceptsFocusRecursively());
60 return m_acceptsFocusChildren
;
63 bool wxControlContainerBase::HasAnyFocusableChildren() const
65 const wxWindowList
& children
= m_winParent
->GetChildren();
66 for ( wxWindowList::const_iterator i
= children
.begin(),
71 const wxWindow
* const child
= *i
;
73 if ( !m_winParent
->IsClientAreaChild(child
) )
76 if ( child
->CanAcceptFocus() )
83 bool wxControlContainerBase::DoSetFocus()
85 wxLogTrace(TRACE_FOCUS
, wxT("SetFocus on wxPanel 0x%p."),
86 m_winParent
->GetHandle());
91 // when the panel gets the focus we move the focus to either the last
92 // window that had the focus or the first one that can get it unless the
93 // focus had been already set to some other child
95 wxWindow
*win
= wxWindow::FindFocus();
98 if ( win
== m_winParent
)
100 // our child already has focus, don't take it away from it
104 if ( win
->IsTopLevel() )
106 // don't look beyond the first top level parent - useless and
111 win
= win
->GetParent();
114 // protect against infinite recursion:
117 bool ret
= SetFocusToChild();
119 m_inSetFocus
= false;
124 bool wxControlContainerBase::SetFocusToChild()
126 return wxSetFocusToChild(m_winParent
, &m_winLastFocused
);
129 #ifndef wxHAS_NATIVE_TAB_TRAVERSAL
131 // ----------------------------------------------------------------------------
132 // generic wxControlContainer
133 // ----------------------------------------------------------------------------
135 wxControlContainer::wxControlContainer()
137 m_winLastFocused
= NULL
;
140 void wxControlContainer::SetLastFocus(wxWindow
*win
)
142 // the panel itself should never get the focus at all but if it does happen
143 // temporarily (as it seems to do under wxGTK), at the very least don't
144 // forget our previous m_winLastFocused
145 if ( win
!= m_winParent
)
147 // if we're setting the focus
150 // find the last _immediate_ child which got focus
151 wxWindow
*winParent
= win
;
152 while ( winParent
!= m_winParent
)
155 winParent
= win
->GetParent();
157 // Yes, this can happen, though in a totally pathological case.
158 // like when detaching a menubar from a frame with a child
159 // which has pushed itself as an event handler for the menubar.
162 wxASSERT_MSG( winParent
,
163 wxT("Setting last focus for a window that is not our child?") );
167 m_winLastFocused
= win
;
171 wxLogTrace(TRACE_FOCUS
, wxT("Set last focus to %s(%s)"),
172 win
->GetClassInfo()->GetClassName(),
173 win
->GetLabel().c_str());
177 wxLogTrace(TRACE_FOCUS
, wxT("No more last focus"));
182 // --------------------------------------------------------------------
183 // The following four functions are used to find other radio buttons
184 // within the same group. Used by wxSetFocusToChild on wxMSW
185 // --------------------------------------------------------------------
187 #if defined(__WXMSW__) && wxUSE_RADIOBTN
189 wxRadioButton
* wxGetPreviousButtonInGroup(wxRadioButton
*btn
)
191 if ( btn
->HasFlag(wxRB_GROUP
) || btn
->HasFlag(wxRB_SINGLE
) )
194 const wxWindowList
& siblings
= btn
->GetParent()->GetChildren();
195 wxWindowList::compatibility_iterator nodeThis
= siblings
.Find(btn
);
196 wxCHECK_MSG( nodeThis
, NULL
, wxT("radio button not a child of its parent?") );
198 // Iterate over all previous siblings until we find the next radio button
199 wxWindowList::compatibility_iterator nodeBefore
= nodeThis
->GetPrevious();
200 wxRadioButton
*prevBtn
= 0;
203 prevBtn
= wxDynamicCast(nodeBefore
->GetData(), wxRadioButton
);
207 nodeBefore
= nodeBefore
->GetPrevious();
210 if (!prevBtn
|| prevBtn
->HasFlag(wxRB_SINGLE
))
212 // no more buttons in group
219 wxRadioButton
* wxGetNextButtonInGroup(wxRadioButton
*btn
)
221 if (btn
->HasFlag(wxRB_SINGLE
))
224 const wxWindowList
& siblings
= btn
->GetParent()->GetChildren();
225 wxWindowList::compatibility_iterator nodeThis
= siblings
.Find(btn
);
226 wxCHECK_MSG( nodeThis
, NULL
, wxT("radio button not a child of its parent?") );
228 // Iterate over all previous siblings until we find the next radio button
229 wxWindowList::compatibility_iterator nodeNext
= nodeThis
->GetNext();
230 wxRadioButton
*nextBtn
= 0;
233 nextBtn
= wxDynamicCast(nodeNext
->GetData(), wxRadioButton
);
237 nodeNext
= nodeNext
->GetNext();
240 if ( !nextBtn
|| nextBtn
->HasFlag(wxRB_GROUP
) || nextBtn
->HasFlag(wxRB_SINGLE
) )
242 // no more buttons or the first button of the next group
249 wxRadioButton
* wxGetFirstButtonInGroup(wxRadioButton
*btn
)
253 wxRadioButton
* prevBtn
= wxGetPreviousButtonInGroup(btn
);
261 wxRadioButton
* wxGetLastButtonInGroup(wxRadioButton
*btn
)
265 wxRadioButton
* nextBtn
= wxGetNextButtonInGroup(btn
);
273 wxRadioButton
* wxGetSelectedButtonInGroup(wxRadioButton
*btn
)
275 // Find currently selected button
279 if (btn
->HasFlag(wxRB_SINGLE
))
282 wxRadioButton
*selBtn
;
284 // First check all previous buttons
285 for (selBtn
= wxGetPreviousButtonInGroup(btn
); selBtn
; selBtn
= wxGetPreviousButtonInGroup(selBtn
))
286 if (selBtn
->GetValue())
289 // Now all following buttons
290 for (selBtn
= wxGetNextButtonInGroup(btn
); selBtn
; selBtn
= wxGetNextButtonInGroup(selBtn
))
291 if (selBtn
->GetValue())
299 // ----------------------------------------------------------------------------
300 // Keyboard handling - this is the place where the TAB traversal logic is
301 // implemented. As this code is common to all ports, this ensures consistent
302 // behaviour even if we don't specify how exactly the wxNavigationKeyEvent are
303 // generated and this is done in platform specific code which also ensures that
304 // we can follow the given platform standards.
305 // ----------------------------------------------------------------------------
307 void wxControlContainer::HandleOnNavigationKey( wxNavigationKeyEvent
& event
)
309 // for a TLW we shouldn't involve the parent window, it has nothing to do
310 // with keyboard navigation inside this TLW
311 wxWindow
*parent
= m_winParent
->IsTopLevel() ? NULL
312 : m_winParent
->GetParent();
314 // the event is propagated downwards if the event emitter was our parent
315 bool goingDown
= event
.GetEventObject() == parent
;
317 const wxWindowList
& children
= m_winParent
->GetChildren();
319 // if we have exactly one notebook-like child window (actually it could be
320 // any window that returns true from its HasMultiplePages()), then
321 // [Shift-]Ctrl-Tab and Ctrl-PageUp/Down keys should iterate over its pages
322 // even if the focus is outside of the control because this is how the
323 // standard MSW properties dialogs behave and we do it under other platforms
324 // as well because it seems like a good idea -- but we can always put this
325 // block inside "#ifdef __WXMSW__" if it's not suitable there
326 if ( event
.IsWindowChange() && !goingDown
)
328 // check if we have a unique notebook-like child
329 wxWindow
*bookctrl
= NULL
;
330 for ( wxWindowList::const_iterator i
= children
.begin(),
331 end
= children
.end();
335 wxWindow
* const window
= *i
;
336 if ( window
->HasMultiplePages() )
340 // this is the second book-like control already so don't do
341 // anything as we don't know which one should have its page
353 // make sure that we don't bubble up the event again from the book
354 // control resulting in infinite recursion
355 wxNavigationKeyEvent
eventCopy(event
);
356 eventCopy
.SetEventObject(m_winParent
);
357 if ( bookctrl
->GetEventHandler()->ProcessEvent(eventCopy
) )
362 // there is not much to do if we don't have children and we're not
363 // interested in "notebook page change" events here
364 if ( !children
.GetCount() || event
.IsWindowChange() )
366 // let the parent process it unless it already comes from our parent
367 // of we don't have any
369 !parent
|| !parent
->GetEventHandler()->ProcessEvent(event
) )
377 // where are we going?
378 const bool forward
= event
.GetDirection();
380 // the node of the children list from which we should start looking for the
381 // next acceptable child
382 wxWindowList::compatibility_iterator node
, start_node
;
384 // we should start from the first/last control and not from the one which
385 // had focus the last time if we're propagating the event downwards because
386 // for our parent we look like a single control
389 // just to be sure it's not used (normally this is not necessary, but
390 // doesn't hurt neither)
391 m_winLastFocused
= NULL
;
393 // start from first or last depending on where we're going
394 node
= forward
? children
.GetFirst() : children
.GetLast();
398 // try to find the child which has the focus currently
400 // the event emitter might have done this for us
401 wxWindow
*winFocus
= event
.GetCurrentFocus();
403 // but if not, we might know where the focus was ourselves
405 winFocus
= m_winLastFocused
;
407 // if still no luck, do it the hard way
409 winFocus
= wxWindow::FindFocus();
413 #if defined(__WXMSW__) && wxUSE_RADIOBTN
414 // If we are in a radio button group, start from the first item in the
416 if ( event
.IsFromTab() && wxIsKindOf(winFocus
, wxRadioButton
) )
417 winFocus
= wxGetFirstButtonInGroup((wxRadioButton
*)winFocus
);
419 // ok, we found the focus - now is it our child?
420 start_node
= children
.Find( winFocus
);
423 if ( !start_node
&& m_winLastFocused
)
425 // window which has focus isn't our child, fall back to the one
426 // which had the focus the last time
427 start_node
= children
.Find( m_winLastFocused
);
430 // if we still didn't find anything, we should start with the first one
433 start_node
= children
.GetFirst();
436 // and the first child which we can try setting focus to is the next or
438 node
= forward
? start_node
->GetNext() : start_node
->GetPrevious();
441 // we want to cycle over all elements passing by NULL
444 // don't go into infinite loop
445 if ( start_node
&& node
&& node
== start_node
)
448 // Have we come to the last or first item on the panel?
453 // exit now as otherwise we'd loop forever
459 // Check if our (maybe grand) parent is another panel: if this
460 // is the case, they will know what to do with this navigation
461 // key and so give them the chance to process it instead of
462 // looping inside this panel (normally, the focus will go to
463 // the next/previous item after this panel in the parent
465 wxWindow
*focusedParent
= m_winParent
;
468 // We don't want to tab into a different dialog or frame or
469 // even an MDI child frame, so test for this explicitly
470 // (and in particular don't just use IsTopLevel() which
471 // would return false in the latter case).
472 if ( focusedParent
->IsTopNavigationDomain() )
475 event
.SetCurrentFocus( focusedParent
);
476 if ( parent
->GetEventHandler()->ProcessEvent( event
) )
479 focusedParent
= parent
;
481 parent
= parent
->GetParent();
484 //else: as the focus came from our parent, we definitely don't want
485 // to send it back to it!
487 // no, we are not inside another panel so process this ourself
488 node
= forward
? children
.GetFirst() : children
.GetLast();
493 wxWindow
*child
= node
->GetData();
495 // don't TAB to another TLW
496 if ( child
->IsTopLevel() )
498 node
= forward
? node
->GetNext() : node
->GetPrevious();
503 #if defined(__WXMSW__) && wxUSE_RADIOBTN
504 if ( event
.IsFromTab() )
506 if ( wxIsKindOf(child
, wxRadioButton
) )
508 // only radio buttons with either wxRB_GROUP or wxRB_SINGLE
510 if ( child
->HasFlag(wxRB_GROUP
) )
512 // need to tab into the active button within a group
513 wxRadioButton
*rb
= wxGetSelectedButtonInGroup((wxRadioButton
*)child
);
517 else if ( !child
->HasFlag(wxRB_SINGLE
) )
519 node
= forward
? node
->GetNext() : node
->GetPrevious();
524 else if ( m_winLastFocused
&&
525 wxIsKindOf(m_winLastFocused
, wxRadioButton
) &&
526 !m_winLastFocused
->HasFlag(wxRB_SINGLE
) )
528 wxRadioButton
* const
529 lastBtn
= static_cast<wxRadioButton
*>(m_winLastFocused
);
531 // cursor keys don't navigate out of a radio button group so
532 // find the correct radio button to focus
535 child
= wxGetNextButtonInGroup(lastBtn
);
538 // no next button in group, set it to the first button
539 child
= wxGetFirstButtonInGroup(lastBtn
);
544 child
= wxGetPreviousButtonInGroup(lastBtn
);
547 // no previous button in group, set it to the last button
548 child
= wxGetLastButtonInGroup(lastBtn
);
552 if ( child
== m_winLastFocused
)
554 // must be a group consisting of only one button therefore
555 // no need to send a navigation event
562 if ( child
->CanAcceptFocusFromKeyboard() )
564 // if we're setting the focus to a child panel we should prevent it
565 // from giving it to the child which had the focus the last time
566 // and instead give it to the first/last child depending from which
567 // direction we're coming
568 event
.SetEventObject(m_winParent
);
570 // disable propagation for this call as otherwise the event might
571 // bounce back to us.
572 wxPropagationDisabler
disableProp(event
);
573 if ( !child
->GetEventHandler()->ProcessEvent(event
) )
575 // set it first in case SetFocusFromKbd() results in focus
577 m_winLastFocused
= child
;
579 // everything is simple: just give focus to it
580 child
->SetFocusFromKbd();
582 //else: the child manages its focus itself
589 node
= forward
? node
->GetNext() : node
->GetPrevious();
592 // we cycled through all of our children and none of them wanted to accept
597 void wxControlContainer::HandleOnWindowDestroy(wxWindowBase
*child
)
599 if ( child
== m_winLastFocused
)
600 m_winLastFocused
= NULL
;
603 // ----------------------------------------------------------------------------
605 // ----------------------------------------------------------------------------
607 void wxControlContainer::HandleOnFocus(wxFocusEvent
& event
)
609 wxLogTrace(TRACE_FOCUS
, wxT("OnFocus on wxPanel 0x%p, name: %s"),
610 m_winParent
->GetHandle(),
611 m_winParent
->GetName().c_str() );
620 // wxHAS_NATIVE_TAB_TRAVERSAL
622 bool wxControlContainer::SetFocusToChild()
624 return wxSetFocusToChild(m_winParent
, NULL
);
628 #endif // !wxHAS_NATIVE_TAB_TRAVERSAL
630 // ----------------------------------------------------------------------------
631 // SetFocusToChild(): this function is used by wxPanel but also by wxFrame in
632 // wxMSW, this is why it is outside of wxControlContainer class
633 // ----------------------------------------------------------------------------
635 bool wxSetFocusToChild(wxWindow
*win
, wxWindow
**childLastFocused
)
637 wxCHECK_MSG( win
, false, wxT("wxSetFocusToChild(): invalid window") );
638 // wxCHECK_MSG( childLastFocused, false,
639 // wxT("wxSetFocusToChild(): NULL child poonter") );
641 if ( childLastFocused
&& *childLastFocused
)
643 // It might happen that the window got reparented
644 if ( (*childLastFocused
)->GetParent() == win
)
646 // And it also could have become hidden in the meanwhile
647 // We want to focus on the deepest widget visible
648 wxWindow
*deepestVisibleWindow
= NULL
;
650 while ( *childLastFocused
)
652 if ( (*childLastFocused
)->IsShown() )
654 if ( !deepestVisibleWindow
)
655 deepestVisibleWindow
= *childLastFocused
;
658 deepestVisibleWindow
= NULL
;
660 *childLastFocused
= (*childLastFocused
)->GetParent();
663 if ( deepestVisibleWindow
)
665 *childLastFocused
= deepestVisibleWindow
;
667 wxLogTrace(TRACE_FOCUS
,
668 wxT("SetFocusToChild() => last child (0x%p)."),
669 (*childLastFocused
)->GetHandle());
671 // not SetFocusFromKbd(): we're restoring focus back to the old
672 // window and not setting it as the result of a kbd action
673 (*childLastFocused
)->SetFocus();
679 // it doesn't count as such any more
680 *childLastFocused
= NULL
;
684 // set the focus to the first child who wants it
685 wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst();
688 wxWindow
*child
= node
->GetData();
689 node
= node
->GetNext();
691 // skip special windows:
692 if ( !win
->IsClientAreaChild(child
) )
695 if ( child
->CanAcceptFocusFromKeyboard() && !child
->IsTopLevel() )
697 #if defined(__WXMSW__) && wxUSE_RADIOBTN
698 // If a radiobutton is the first focusable child, search for the
699 // selected radiobutton in the same group
700 wxRadioButton
* btn
= wxDynamicCast(child
, wxRadioButton
);
703 wxRadioButton
* selected
= wxGetSelectedButtonInGroup(btn
);
709 wxLogTrace(TRACE_FOCUS
,
710 wxT("SetFocusToChild() => first child (0x%p)."),
713 if (childLastFocused
)
714 *childLastFocused
= child
;
715 child
->SetFocusFromKbd();