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::SetCanFocus(bool acceptsFocus
)
52 if ( acceptsFocus
== m_acceptsFocus
)
55 m_acceptsFocus
= acceptsFocus
;
57 m_winParent
->SetCanFocus(m_acceptsFocus
);
60 bool wxControlContainerBase::HasAnyFocusableChildren() const
62 const wxWindowList
& children
= m_winParent
->GetChildren();
63 for ( wxWindowList::const_iterator i
= children
.begin(),
68 const wxWindow
* const child
= *i
;
70 if ( !m_winParent
->IsClientAreaChild(child
) )
73 if ( child
->CanAcceptFocus() )
80 bool wxControlContainerBase::DoSetFocus()
82 wxLogTrace(TRACE_FOCUS
, wxT("SetFocus on wxPanel 0x%p."),
83 m_winParent
->GetHandle());
88 // when the panel gets the focus we move the focus to either the last
89 // window that had the focus or the first one that can get it unless the
90 // focus had been already set to some other child
92 wxWindow
*win
= wxWindow::FindFocus();
95 if ( win
== m_winParent
)
97 // our child already has focus, don't take it away from it
101 if ( win
->IsTopLevel() )
103 // don't look beyond the first top level parent - useless and
108 win
= win
->GetParent();
111 // protect against infinite recursion:
114 bool ret
= SetFocusToChild();
116 m_inSetFocus
= false;
121 bool wxControlContainerBase::SetFocusToChild()
123 return wxSetFocusToChild(m_winParent
, &m_winLastFocused
);
126 #ifndef wxHAS_NATIVE_TAB_TRAVERSAL
128 // ----------------------------------------------------------------------------
129 // generic wxControlContainer
130 // ----------------------------------------------------------------------------
132 wxControlContainer::wxControlContainer()
134 m_winLastFocused
= NULL
;
137 void wxControlContainer::SetLastFocus(wxWindow
*win
)
139 // the panel itself should never get the focus at all but if it does happen
140 // temporarily (as it seems to do under wxGTK), at the very least don't
141 // forget our previous m_winLastFocused
142 if ( win
!= m_winParent
)
144 // if we're setting the focus
147 // find the last _immediate_ child which got focus
148 wxWindow
*winParent
= win
;
149 while ( winParent
!= m_winParent
)
152 winParent
= win
->GetParent();
154 // Yes, this can happen, though in a totally pathological case.
155 // like when detaching a menubar from a frame with a child
156 // which has pushed itself as an event handler for the menubar.
159 wxASSERT_MSG( winParent
,
160 wxT("Setting last focus for a window that is not our child?") );
164 m_winLastFocused
= win
;
168 wxLogTrace(TRACE_FOCUS
, wxT("Set last focus to %s(%s)"),
169 win
->GetClassInfo()->GetClassName(),
170 win
->GetLabel().c_str());
174 wxLogTrace(TRACE_FOCUS
, wxT("No more last focus"));
178 // propagate the last focus upwards so that our parent can set focus back
179 // to us if it loses it now and regains later; do *not* do this if we are
180 // a toplevel window (e.g. wxDialog) that has another frame as its parent
181 if ( !m_winParent
->IsTopLevel() )
183 wxWindow
*parent
= m_winParent
->GetParent();
186 wxChildFocusEvent
eventFocus(m_winParent
);
187 parent
->GetEventHandler()->ProcessEvent(eventFocus
);
192 // --------------------------------------------------------------------
193 // The following four functions are used to find other radio buttons
194 // within the same group. Used by wxSetFocusToChild on wxMSW
195 // --------------------------------------------------------------------
197 #if defined(__WXMSW__) && wxUSE_RADIOBTN
199 wxRadioButton
* wxGetPreviousButtonInGroup(wxRadioButton
*btn
)
201 if ( btn
->HasFlag(wxRB_GROUP
) || btn
->HasFlag(wxRB_SINGLE
) )
204 const wxWindowList
& siblings
= btn
->GetParent()->GetChildren();
205 wxWindowList::compatibility_iterator nodeThis
= siblings
.Find(btn
);
206 wxCHECK_MSG( nodeThis
, NULL
, wxT("radio button not a child of its parent?") );
208 // Iterate over all previous siblings until we find the next radio button
209 wxWindowList::compatibility_iterator nodeBefore
= nodeThis
->GetPrevious();
210 wxRadioButton
*prevBtn
= 0;
213 prevBtn
= wxDynamicCast(nodeBefore
->GetData(), wxRadioButton
);
217 nodeBefore
= nodeBefore
->GetPrevious();
220 if (!prevBtn
|| prevBtn
->HasFlag(wxRB_SINGLE
))
222 // no more buttons in group
229 wxRadioButton
* wxGetNextButtonInGroup(wxRadioButton
*btn
)
231 if (btn
->HasFlag(wxRB_SINGLE
))
234 const wxWindowList
& siblings
= btn
->GetParent()->GetChildren();
235 wxWindowList::compatibility_iterator nodeThis
= siblings
.Find(btn
);
236 wxCHECK_MSG( nodeThis
, NULL
, wxT("radio button not a child of its parent?") );
238 // Iterate over all previous siblings until we find the next radio button
239 wxWindowList::compatibility_iterator nodeNext
= nodeThis
->GetNext();
240 wxRadioButton
*nextBtn
= 0;
243 nextBtn
= wxDynamicCast(nodeNext
->GetData(), wxRadioButton
);
247 nodeNext
= nodeNext
->GetNext();
250 if ( !nextBtn
|| nextBtn
->HasFlag(wxRB_GROUP
) || nextBtn
->HasFlag(wxRB_SINGLE
) )
252 // no more buttons or the first button of the next group
259 wxRadioButton
* wxGetFirstButtonInGroup(wxRadioButton
*btn
)
263 wxRadioButton
* prevBtn
= wxGetPreviousButtonInGroup(btn
);
271 wxRadioButton
* wxGetLastButtonInGroup(wxRadioButton
*btn
)
275 wxRadioButton
* nextBtn
= wxGetNextButtonInGroup(btn
);
283 wxRadioButton
* wxGetSelectedButtonInGroup(wxRadioButton
*btn
)
285 // Find currently selected button
289 if (btn
->HasFlag(wxRB_SINGLE
))
292 wxRadioButton
*selBtn
;
294 // First check all previous buttons
295 for (selBtn
= wxGetPreviousButtonInGroup(btn
); selBtn
; selBtn
= wxGetPreviousButtonInGroup(selBtn
))
296 if (selBtn
->GetValue())
299 // Now all following buttons
300 for (selBtn
= wxGetNextButtonInGroup(btn
); selBtn
; selBtn
= wxGetNextButtonInGroup(selBtn
))
301 if (selBtn
->GetValue())
309 // ----------------------------------------------------------------------------
310 // Keyboard handling - this is the place where the TAB traversal logic is
311 // implemented. As this code is common to all ports, this ensures consistent
312 // behaviour even if we don't specify how exactly the wxNavigationKeyEvent are
313 // generated and this is done in platform specific code which also ensures that
314 // we can follow the given platform standards.
315 // ----------------------------------------------------------------------------
317 void wxControlContainer::HandleOnNavigationKey( wxNavigationKeyEvent
& event
)
319 // for a TLW we shouldn't involve the parent window, it has nothing to do
320 // with keyboard navigation inside this TLW
321 wxWindow
*parent
= m_winParent
->IsTopLevel() ? NULL
322 : m_winParent
->GetParent();
324 // the event is propagated downwards if the event emitter was our parent
325 bool goingDown
= event
.GetEventObject() == parent
;
327 const wxWindowList
& children
= m_winParent
->GetChildren();
329 // if we have exactly one notebook-like child window (actually it could be
330 // any window that returns true from its HasMultiplePages()), then
331 // [Shift-]Ctrl-Tab and Ctrl-PageUp/Down keys should iterate over its pages
332 // even if the focus is outside of the control because this is how the
333 // standard MSW properties dialogs behave and we do it under other platforms
334 // as well because it seems like a good idea -- but we can always put this
335 // block inside "#ifdef __WXMSW__" if it's not suitable there
336 if ( event
.IsWindowChange() && !goingDown
)
338 // check if we have a unique notebook-like child
339 wxWindow
*bookctrl
= NULL
;
340 for ( wxWindowList::const_iterator i
= children
.begin(),
341 end
= children
.end();
345 wxWindow
* const window
= *i
;
346 if ( window
->HasMultiplePages() )
350 // this is the second book-like control already so don't do
351 // anything as we don't know which one should have its page
363 // make sure that we don't bubble up the event again from the book
364 // control resulting in infinite recursion
365 wxNavigationKeyEvent
eventCopy(event
);
366 eventCopy
.SetEventObject(m_winParent
);
367 if ( bookctrl
->GetEventHandler()->ProcessEvent(eventCopy
) )
372 // there is not much to do if we don't have children and we're not
373 // interested in "notebook page change" events here
374 if ( !children
.GetCount() || event
.IsWindowChange() )
376 // let the parent process it unless it already comes from our parent
377 // of we don't have any
379 !parent
|| !parent
->GetEventHandler()->ProcessEvent(event
) )
387 // where are we going?
388 const bool forward
= event
.GetDirection();
390 // the node of the children list from which we should start looking for the
391 // next acceptable child
392 wxWindowList::compatibility_iterator node
, start_node
;
394 // we should start from the first/last control and not from the one which
395 // had focus the last time if we're propagating the event downwards because
396 // for our parent we look like a single control
399 // just to be sure it's not used (normally this is not necessary, but
400 // doesn't hurt neither)
401 m_winLastFocused
= NULL
;
403 // start from first or last depending on where we're going
404 node
= forward
? children
.GetFirst() : children
.GetLast();
408 // try to find the child which has the focus currently
410 // the event emitter might have done this for us
411 wxWindow
*winFocus
= event
.GetCurrentFocus();
413 // but if not, we might know where the focus was ourselves
415 winFocus
= m_winLastFocused
;
417 // if still no luck, do it the hard way
419 winFocus
= wxWindow::FindFocus();
423 #if defined(__WXMSW__) && wxUSE_RADIOBTN
424 // If we are in a radio button group, start from the first item in the
426 if ( event
.IsFromTab() && wxIsKindOf(winFocus
, wxRadioButton
) )
427 winFocus
= wxGetFirstButtonInGroup((wxRadioButton
*)winFocus
);
429 // ok, we found the focus - now is it our child?
430 start_node
= children
.Find( winFocus
);
433 if ( !start_node
&& m_winLastFocused
)
435 // window which has focus isn't our child, fall back to the one
436 // which had the focus the last time
437 start_node
= children
.Find( m_winLastFocused
);
440 // if we still didn't find anything, we should start with the first one
443 start_node
= children
.GetFirst();
446 // and the first child which we can try setting focus to is the next or
448 node
= forward
? start_node
->GetNext() : start_node
->GetPrevious();
451 // we want to cycle over all elements passing by NULL
454 // don't go into infinite loop
455 if ( start_node
&& node
&& node
== start_node
)
458 // Have we come to the last or first item on the panel?
463 // exit now as otherwise we'd loop forever
469 // Check if our (maybe grand) parent is another panel: if this
470 // is the case, they will know what to do with this navigation
471 // key and so give them the chance to process it instead of
472 // looping inside this panel (normally, the focus will go to
473 // the next/previous item after this panel in the parent
475 wxWindow
*focussed_child_of_parent
= m_winParent
;
478 // we don't want to tab into a different dialog or frame
479 if ( focussed_child_of_parent
->IsTopLevel() )
482 event
.SetCurrentFocus( focussed_child_of_parent
);
483 if ( parent
->GetEventHandler()->ProcessEvent( event
) )
486 focussed_child_of_parent
= parent
;
488 parent
= parent
->GetParent();
491 //else: as the focus came from our parent, we definitely don't want
492 // to send it back to it!
494 // no, we are not inside another panel so process this ourself
495 node
= forward
? children
.GetFirst() : children
.GetLast();
500 wxWindow
*child
= node
->GetData();
502 // don't TAB to another TLW
503 if ( child
->IsTopLevel() )
505 node
= forward
? node
->GetNext() : node
->GetPrevious();
510 #if defined(__WXMSW__) && wxUSE_RADIOBTN
511 if ( event
.IsFromTab() )
513 if ( wxIsKindOf(child
, wxRadioButton
) )
515 // only radio buttons with either wxRB_GROUP or wxRB_SINGLE
517 if ( child
->HasFlag(wxRB_GROUP
) )
519 // need to tab into the active button within a group
520 wxRadioButton
*rb
= wxGetSelectedButtonInGroup((wxRadioButton
*)child
);
524 else if ( !child
->HasFlag(wxRB_SINGLE
) )
526 node
= forward
? node
->GetNext() : node
->GetPrevious();
531 else if ( m_winLastFocused
&&
532 wxIsKindOf(m_winLastFocused
, wxRadioButton
) &&
533 !m_winLastFocused
->HasFlag(wxRB_SINGLE
) )
535 wxRadioButton
* const
536 lastBtn
= static_cast<wxRadioButton
*>(m_winLastFocused
);
538 // cursor keys don't navigate out of a radio button group so
539 // find the correct radio button to focus
542 child
= wxGetNextButtonInGroup(lastBtn
);
545 // no next button in group, set it to the first button
546 child
= wxGetFirstButtonInGroup(lastBtn
);
551 child
= wxGetPreviousButtonInGroup(lastBtn
);
554 // no previous button in group, set it to the last button
555 child
= wxGetLastButtonInGroup(lastBtn
);
559 if ( child
== m_winLastFocused
)
561 // must be a group consisting of only one button therefore
562 // no need to send a navigation event
569 if ( child
->CanAcceptFocusFromKeyboard() )
571 // if we're setting the focus to a child panel we should prevent it
572 // from giving it to the child which had the focus the last time
573 // and instead give it to the first/last child depending from which
574 // direction we're coming
575 event
.SetEventObject(m_winParent
);
577 // disable propagation for this call as otherwise the event might
578 // bounce back to us.
579 wxPropagationDisabler
disableProp(event
);
580 if ( !child
->GetEventHandler()->ProcessEvent(event
) )
582 // set it first in case SetFocusFromKbd() results in focus
584 m_winLastFocused
= child
;
586 // everything is simple: just give focus to it
587 child
->SetFocusFromKbd();
589 //else: the child manages its focus itself
596 node
= forward
? node
->GetNext() : node
->GetPrevious();
599 // we cycled through all of our children and none of them wanted to accept
604 void wxControlContainer::HandleOnWindowDestroy(wxWindowBase
*child
)
606 if ( child
== m_winLastFocused
)
607 m_winLastFocused
= NULL
;
610 // ----------------------------------------------------------------------------
612 // ----------------------------------------------------------------------------
614 void wxControlContainer::HandleOnFocus(wxFocusEvent
& event
)
616 wxLogTrace(TRACE_FOCUS
, wxT("OnFocus on wxPanel 0x%p, name: %s"),
617 m_winParent
->GetHandle(),
618 m_winParent
->GetName().c_str() );
627 // wxHAS_NATIVE_TAB_TRAVERSAL
629 bool wxControlContainer::SetFocusToChild()
631 return wxSetFocusToChild(m_winParent
, NULL
);
635 #endif // !wxHAS_NATIVE_TAB_TRAVERSAL
637 // ----------------------------------------------------------------------------
638 // SetFocusToChild(): this function is used by wxPanel but also by wxFrame in
639 // wxMSW, this is why it is outside of wxControlContainer class
640 // ----------------------------------------------------------------------------
642 bool wxSetFocusToChild(wxWindow
*win
, wxWindow
**childLastFocused
)
644 wxCHECK_MSG( win
, false, wxT("wxSetFocusToChild(): invalid window") );
645 // wxCHECK_MSG( childLastFocused, false,
646 // wxT("wxSetFocusToChild(): NULL child poonter") );
648 if ( childLastFocused
&& *childLastFocused
)
650 // It might happen that the window got reparented
651 if ( (*childLastFocused
)->GetParent() == win
)
653 wxLogTrace(TRACE_FOCUS
,
654 wxT("SetFocusToChild() => last child (0x%p)."),
655 (*childLastFocused
)->GetHandle());
657 // not SetFocusFromKbd(): we're restoring focus back to the old
658 // window and not setting it as the result of a kbd action
659 (*childLastFocused
)->SetFocus();
664 // it doesn't count as such any more
665 *childLastFocused
= NULL
;
669 // set the focus to the first child who wants it
670 wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst();
673 wxWindow
*child
= node
->GetData();
674 node
= node
->GetNext();
676 // skip special windows:
677 if ( !win
->IsClientAreaChild(child
) )
680 if ( child
->CanAcceptFocusFromKeyboard() && !child
->IsTopLevel() )
682 #if defined(__WXMSW__) && wxUSE_RADIOBTN
683 // If a radiobutton is the first focusable child, search for the
684 // selected radiobutton in the same group
685 wxRadioButton
* btn
= wxDynamicCast(child
, wxRadioButton
);
688 wxRadioButton
* selected
= wxGetSelectedButtonInGroup(btn
);
694 wxLogTrace(TRACE_FOCUS
,
695 wxT("SetFocusToChild() => first child (0x%p)."),
698 if (childLastFocused
)
699 *childLastFocused
= child
;
700 child
->SetFocusFromKbd();