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 // License: 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 _T("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 // if the window has a focusable child, it shouldn't be focusable itself (think
61 // of wxPanel used for grouping different controls) but if it doesn't have any
62 // (focusable) children, then it should be possible to give it focus (think of
63 // wxGrid or generic wxListCtrl)
64 bool wxControlContainerBase::ShouldAcceptFocus() const
66 // we can accept focus either if we have no children at all (in this case
67 // we're probably not used as a container) or only when at least one child
69 wxWindowList::compatibility_iterator node
= m_winParent
->GetChildren().GetFirst();
75 wxWindow
*child
= node
->GetData();
76 node
= node
->GetNext();
79 if ( m_winParent
->MacIsWindowScrollbar( child
) )
83 if ( child
->CanAcceptFocus() )
90 #ifndef wxHAS_NATIVE_TAB_TRAVERSAL
92 // ----------------------------------------------------------------------------
93 // generic wxControlContainer
94 // ----------------------------------------------------------------------------
96 wxControlContainer::wxControlContainer()
98 m_winLastFocused
= NULL
;
102 void wxControlContainer::SetLastFocus(wxWindow
*win
)
104 // the panel itself should never get the focus at all but if it does happen
105 // temporarily (as it seems to do under wxGTK), at the very least don't
106 // forget our previous m_winLastFocused
107 if ( win
!= m_winParent
)
109 // if we're setting the focus
112 // find the last _immediate_ child which got focus
113 wxWindow
*winParent
= win
;
114 while ( winParent
!= m_winParent
)
117 winParent
= win
->GetParent();
119 // Yes, this can happen, though in a totally pathological case.
120 // like when detaching a menubar from a frame with a child
121 // which has pushed itself as an event handler for the menubar.
124 wxASSERT_MSG( winParent
,
125 _T("Setting last focus for a window that is not our child?") );
129 m_winLastFocused
= win
;
133 wxLogTrace(TRACE_FOCUS
, _T("Set last focus to %s(%s)"),
134 win
->GetClassInfo()->GetClassName(),
135 win
->GetLabel().c_str());
139 wxLogTrace(TRACE_FOCUS
, _T("No more last focus"));
143 // propagate the last focus upwards so that our parent can set focus back
144 // to us if it loses it now and regains later; do *not* do this if we are
145 // a toplevel window (e.g. wxDialog) that has another frame as its parent
146 if ( !m_winParent
->IsTopLevel() )
148 wxWindow
*parent
= m_winParent
->GetParent();
151 wxChildFocusEvent
eventFocus(m_winParent
);
152 parent
->GetEventHandler()->ProcessEvent(eventFocus
);
157 // --------------------------------------------------------------------
158 // The following four functions are used to find other radio buttons
159 // within the same group. Used by wxSetFocusToChild on wxMSW
160 // --------------------------------------------------------------------
164 wxRadioButton
* wxGetPreviousButtonInGroup(wxRadioButton
*btn
)
166 if ( btn
->HasFlag(wxRB_GROUP
) || btn
->HasFlag(wxRB_SINGLE
) )
169 const wxWindowList
& siblings
= btn
->GetParent()->GetChildren();
170 wxWindowList::compatibility_iterator nodeThis
= siblings
.Find(btn
);
171 wxCHECK_MSG( nodeThis
, NULL
, _T("radio button not a child of its parent?") );
173 // Iterate over all previous siblings until we find the next radio button
174 wxWindowList::compatibility_iterator nodeBefore
= nodeThis
->GetPrevious();
175 wxRadioButton
*prevBtn
= 0;
178 prevBtn
= wxDynamicCast(nodeBefore
->GetData(), wxRadioButton
);
182 nodeBefore
= nodeBefore
->GetPrevious();
185 if (!prevBtn
|| prevBtn
->HasFlag(wxRB_SINGLE
))
187 // no more buttons in group
194 wxRadioButton
* wxGetNextButtonInGroup(wxRadioButton
*btn
)
196 if (btn
->HasFlag(wxRB_SINGLE
))
199 const wxWindowList
& siblings
= btn
->GetParent()->GetChildren();
200 wxWindowList::compatibility_iterator nodeThis
= siblings
.Find(btn
);
201 wxCHECK_MSG( nodeThis
, NULL
, _T("radio button not a child of its parent?") );
203 // Iterate over all previous siblings until we find the next radio button
204 wxWindowList::compatibility_iterator nodeNext
= nodeThis
->GetNext();
205 wxRadioButton
*nextBtn
= 0;
208 nextBtn
= wxDynamicCast(nodeNext
->GetData(), wxRadioButton
);
212 nodeNext
= nodeNext
->GetNext();
215 if ( !nextBtn
|| nextBtn
->HasFlag(wxRB_GROUP
) || nextBtn
->HasFlag(wxRB_SINGLE
) )
217 // no more buttons or the first button of the next group
224 wxRadioButton
* wxGetFirstButtonInGroup(wxRadioButton
*btn
)
228 wxRadioButton
* prevBtn
= wxGetPreviousButtonInGroup(btn
);
236 wxRadioButton
* wxGetLastButtonInGroup(wxRadioButton
*btn
)
240 wxRadioButton
* nextBtn
= wxGetNextButtonInGroup(btn
);
248 wxRadioButton
* wxGetSelectedButtonInGroup(wxRadioButton
*btn
)
250 // Find currently selected button
254 if (btn
->HasFlag(wxRB_SINGLE
))
257 wxRadioButton
*selBtn
;
259 // First check all previous buttons
260 for (selBtn
= wxGetPreviousButtonInGroup(btn
); selBtn
; selBtn
= wxGetPreviousButtonInGroup(selBtn
))
261 if (selBtn
->GetValue())
264 // Now all following buttons
265 for (selBtn
= wxGetNextButtonInGroup(btn
); selBtn
; selBtn
= wxGetNextButtonInGroup(selBtn
))
266 if (selBtn
->GetValue())
274 // ----------------------------------------------------------------------------
275 // Keyboard handling - this is the place where the TAB traversal logic is
276 // implemented. As this code is common to all ports, this ensures consistent
277 // behaviour even if we don't specify how exactly the wxNavigationKeyEvent are
278 // generated and this is done in platform specific code which also ensures that
279 // we can follow the given platform standards.
280 // ----------------------------------------------------------------------------
282 void wxControlContainer::HandleOnNavigationKey( wxNavigationKeyEvent
& event
)
284 wxWindow
*parent
= m_winParent
->GetParent();
286 // the event is propagated downwards if the event emitter was our parent
287 bool goingDown
= event
.GetEventObject() == parent
;
289 const wxWindowList
& children
= m_winParent
->GetChildren();
291 // if we have exactly one notebook-like child window (actually it could be
292 // any window that returns true from its HasMultiplePages()), then
293 // [Shift-]Ctrl-Tab and Ctrl-PageUp/Down keys should iterate over its pages
294 // even if the focus is outside of the control because this is how the
295 // standard MSW properties dialogs behave and we do it under other platforms
296 // as well because it seems like a good idea -- but we can always put this
297 // block inside "#ifdef __WXMSW__" if it's not suitable there
298 if ( event
.IsWindowChange() && !goingDown
)
300 // check if we have a unique notebook-like child
301 wxWindow
*bookctrl
= NULL
;
302 for ( wxWindowList::const_iterator i
= children
.begin(),
303 end
= children
.end();
307 wxWindow
* const window
= *i
;
308 if ( window
->HasMultiplePages() )
312 // this is the second book-like control already so don't do
313 // anything as we don't know which one should have its page
325 // make sure that we don't bubble up the event again from the book
326 // control resulting in infinite recursion
327 wxNavigationKeyEvent
eventCopy(event
);
328 eventCopy
.SetEventObject(m_winParent
);
329 if ( bookctrl
->GetEventHandler()->ProcessEvent(eventCopy
) )
334 // there is not much to do if we don't have children and we're not
335 // interested in "notebook page change" events here
336 if ( !children
.GetCount() || event
.IsWindowChange() )
338 // let the parent process it unless it already comes from our parent
339 // of we don't have any
341 !parent
|| !parent
->GetEventHandler()->ProcessEvent(event
) )
349 // where are we going?
350 const bool forward
= event
.GetDirection();
352 // the node of the children list from which we should start looking for the
353 // next acceptable child
354 wxWindowList::compatibility_iterator node
, start_node
;
356 // we should start from the first/last control and not from the one which
357 // had focus the last time if we're propagating the event downwards because
358 // for our parent we look like a single control
361 // just to be sure it's not used (normally this is not necessary, but
362 // doesn't hurt neither)
363 m_winLastFocused
= (wxWindow
*)NULL
;
365 // start from first or last depending on where we're going
366 node
= forward
? children
.GetFirst() : children
.GetLast();
370 // try to find the child which has the focus currently
372 // the event emitter might have done this for us
373 wxWindow
*winFocus
= event
.GetCurrentFocus();
375 // but if not, we might know where the focus was ourselves
377 winFocus
= m_winLastFocused
;
379 // if still no luck, do it the hard way
381 winFocus
= wxWindow::FindFocus();
386 // If we are in a radio button group, start from the first item in the
388 if ( event
.IsFromTab() && wxIsKindOf(winFocus
, wxRadioButton
) )
389 winFocus
= wxGetFirstButtonInGroup((wxRadioButton
*)winFocus
);
391 // ok, we found the focus - now is it our child?
392 start_node
= children
.Find( winFocus
);
395 if ( !start_node
&& m_winLastFocused
)
397 // window which has focus isn't our child, fall back to the one
398 // which had the focus the last time
399 start_node
= children
.Find( m_winLastFocused
);
402 // if we still didn't find anything, we should start with the first one
405 start_node
= children
.GetFirst();
408 // and the first child which we can try setting focus to is the next or
410 node
= forward
? start_node
->GetNext() : start_node
->GetPrevious();
413 // we want to cycle over all elements passing by NULL
416 // don't go into infinite loop
417 if ( start_node
&& node
&& node
== start_node
)
420 // Have we come to the last or first item on the panel?
425 // exit now as otherwise we'd loop forever
431 // Check if our (maybe grand) parent is another panel: if this
432 // is the case, they will know what to do with this navigation
433 // key and so give them the chance to process it instead of
434 // looping inside this panel (normally, the focus will go to
435 // the next/previous item after this panel in the parent
437 wxWindow
*focussed_child_of_parent
= m_winParent
;
440 // we don't want to tab into a different dialog or frame
441 if ( focussed_child_of_parent
->IsTopLevel() )
444 event
.SetCurrentFocus( focussed_child_of_parent
);
445 if ( parent
->GetEventHandler()->ProcessEvent( event
) )
448 focussed_child_of_parent
= parent
;
450 parent
= parent
->GetParent();
453 //else: as the focus came from our parent, we definitely don't want
454 // to send it back to it!
456 // no, we are not inside another panel so process this ourself
457 node
= forward
? children
.GetFirst() : children
.GetLast();
462 wxWindow
*child
= node
->GetData();
465 if ( event
.IsFromTab() )
467 if ( wxIsKindOf(child
, wxRadioButton
) )
469 // only radio buttons with either wxRB_GROUP or wxRB_SINGLE
471 if ( child
->HasFlag(wxRB_GROUP
) )
473 // need to tab into the active button within a group
474 wxRadioButton
*rb
= wxGetSelectedButtonInGroup((wxRadioButton
*)child
);
478 else if ( !child
->HasFlag(wxRB_SINGLE
) )
480 node
= forward
? node
->GetNext() : node
->GetPrevious();
485 else if ( m_winLastFocused
&&
486 wxIsKindOf(m_winLastFocused
, wxRadioButton
) &&
487 !m_winLastFocused
->HasFlag(wxRB_SINGLE
) )
489 // cursor keys don't navigate out of a radio button group so
490 // find the correct radio button to focus
493 child
= wxGetNextButtonInGroup((wxRadioButton
*)m_winLastFocused
);
496 // no next button in group, set it to the first button
497 child
= wxGetFirstButtonInGroup((wxRadioButton
*)m_winLastFocused
);
502 child
= wxGetPreviousButtonInGroup((wxRadioButton
*)m_winLastFocused
);
505 // no previous button in group, set it to the last button
506 child
= wxGetLastButtonInGroup((wxRadioButton
*)m_winLastFocused
);
510 if ( child
== m_winLastFocused
)
512 // must be a group consisting of only one button therefore
513 // no need to send a navigation event
520 if ( child
->CanAcceptFocusFromKeyboard() )
522 // if we're setting the focus to a child panel we should prevent it
523 // from giving it to the child which had the focus the last time
524 // and instead give it to the first/last child depending from which
525 // direction we're coming
526 event
.SetEventObject(m_winParent
);
528 // disable propagation for this call as otherwise the event might
529 // bounce back to us.
530 wxPropagationDisabler
disableProp(event
);
531 if ( !child
->GetEventHandler()->ProcessEvent(event
) )
533 // set it first in case SetFocusFromKbd() results in focus
535 m_winLastFocused
= child
;
537 // everything is simple: just give focus to it
538 child
->SetFocusFromKbd();
540 //else: the child manages its focus itself
547 node
= forward
? node
->GetNext() : node
->GetPrevious();
550 // we cycled through all of our children and none of them wanted to accept
555 void wxControlContainer::HandleOnWindowDestroy(wxWindowBase
*child
)
557 if ( child
== m_winLastFocused
)
558 m_winLastFocused
= NULL
;
561 // ----------------------------------------------------------------------------
563 // ----------------------------------------------------------------------------
565 bool wxControlContainer::DoSetFocus()
567 wxLogTrace(TRACE_FOCUS
, _T("SetFocus on wxPanel 0x%p."),
568 m_winParent
->GetHandle());
573 // when the panel gets the focus we move the focus to either the last
574 // window that had the focus or the first one that can get it unless the
575 // focus had been already set to some other child
577 wxWindow
*win
= wxWindow::FindFocus();
580 if ( win
== m_winParent
)
582 // our child already has focus, don't take it away from it
586 if ( win
->IsTopLevel() )
588 // don't look beyond the first top level parent - useless and
593 win
= win
->GetParent();
596 // protect against infinite recursion:
599 bool ret
= SetFocusToChild();
601 m_inSetFocus
= false;
606 void wxControlContainer::HandleOnFocus(wxFocusEvent
& event
)
608 wxLogTrace(TRACE_FOCUS
, _T("OnFocus on wxPanel 0x%p, name: %s"),
609 m_winParent
->GetHandle(),
610 m_winParent
->GetName().c_str() );
617 bool wxControlContainer::SetFocusToChild()
619 return wxSetFocusToChild(m_winParent
, &m_winLastFocused
);
622 // ----------------------------------------------------------------------------
623 // SetFocusToChild(): this function is used by wxPanel but also by wxFrame in
624 // wxMSW, this is why it is outside of wxControlContainer class
625 // ----------------------------------------------------------------------------
627 bool wxSetFocusToChild(wxWindow
*win
, wxWindow
**childLastFocused
)
629 wxCHECK_MSG( win
, false, _T("wxSetFocusToChild(): invalid window") );
630 wxCHECK_MSG( childLastFocused
, false,
631 _T("wxSetFocusToChild(): NULL child poonter") );
633 if ( *childLastFocused
)
635 // It might happen that the window got reparented
636 if ( (*childLastFocused
)->GetParent() == win
)
638 wxLogTrace(TRACE_FOCUS
,
639 _T("SetFocusToChild() => last child (0x%p)."),
640 (*childLastFocused
)->GetHandle());
642 // not SetFocusFromKbd(): we're restoring focus back to the old
643 // window and not setting it as the result of a kbd action
644 (*childLastFocused
)->SetFocus();
649 // it doesn't count as such any more
650 *childLastFocused
= (wxWindow
*)NULL
;
654 // set the focus to the first child who wants it
655 wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst();
658 wxWindow
*child
= node
->GetData();
659 node
= node
->GetNext();
662 if ( child
->GetParent()->MacIsWindowScrollbar( child
) )
666 if ( child
->CanAcceptFocusFromKeyboard() && !child
->IsTopLevel() )
669 // If a radiobutton is the first focusable child, search for the
670 // selected radiobutton in the same group
671 wxRadioButton
* btn
= wxDynamicCast(child
, wxRadioButton
);
674 wxRadioButton
* selected
= wxGetSelectedButtonInGroup(btn
);
680 wxLogTrace(TRACE_FOCUS
,
681 _T("SetFocusToChild() => first child (0x%p)."),
684 *childLastFocused
= child
;
685 child
->SetFocusFromKbd();
693 #endif // !wxHAS_NATIVE_TAB_TRAVERSAL