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
145 wxWindow
*parent
= m_winParent
->GetParent();
148 wxChildFocusEvent
eventFocus(m_winParent
);
149 parent
->GetEventHandler()->ProcessEvent(eventFocus
);
153 // --------------------------------------------------------------------
154 // The following four functions are used to find other radio buttons
155 // within the same group. Used by wxSetFocusToChild on wxMSW
156 // --------------------------------------------------------------------
160 wxRadioButton
* wxGetPreviousButtonInGroup(wxRadioButton
*btn
)
162 if ( btn
->HasFlag(wxRB_GROUP
) || btn
->HasFlag(wxRB_SINGLE
) )
165 const wxWindowList
& siblings
= btn
->GetParent()->GetChildren();
166 wxWindowList::compatibility_iterator nodeThis
= siblings
.Find(btn
);
167 wxCHECK_MSG( nodeThis
, NULL
, _T("radio button not a child of its parent?") );
169 // Iterate over all previous siblings until we find the next radio button
170 wxWindowList::compatibility_iterator nodeBefore
= nodeThis
->GetPrevious();
171 wxRadioButton
*prevBtn
= 0;
174 prevBtn
= wxDynamicCast(nodeBefore
->GetData(), wxRadioButton
);
178 nodeBefore
= nodeBefore
->GetPrevious();
181 if (!prevBtn
|| prevBtn
->HasFlag(wxRB_SINGLE
))
183 // no more buttons in group
190 wxRadioButton
* wxGetNextButtonInGroup(wxRadioButton
*btn
)
192 if (btn
->HasFlag(wxRB_SINGLE
))
195 const wxWindowList
& siblings
= btn
->GetParent()->GetChildren();
196 wxWindowList::compatibility_iterator nodeThis
= siblings
.Find(btn
);
197 wxCHECK_MSG( nodeThis
, NULL
, _T("radio button not a child of its parent?") );
199 // Iterate over all previous siblings until we find the next radio button
200 wxWindowList::compatibility_iterator nodeNext
= nodeThis
->GetNext();
201 wxRadioButton
*nextBtn
= 0;
204 nextBtn
= wxDynamicCast(nodeNext
->GetData(), wxRadioButton
);
208 nodeNext
= nodeNext
->GetNext();
211 if ( !nextBtn
|| nextBtn
->HasFlag(wxRB_GROUP
) || nextBtn
->HasFlag(wxRB_SINGLE
) )
213 // no more buttons or the first button of the next group
220 wxRadioButton
* wxGetFirstButtonInGroup(wxRadioButton
*btn
)
224 wxRadioButton
* prevBtn
= wxGetPreviousButtonInGroup(btn
);
232 wxRadioButton
* wxGetLastButtonInGroup(wxRadioButton
*btn
)
236 wxRadioButton
* nextBtn
= wxGetNextButtonInGroup(btn
);
244 wxRadioButton
* wxGetSelectedButtonInGroup(wxRadioButton
*btn
)
246 // Find currently selected button
250 if (btn
->HasFlag(wxRB_SINGLE
))
253 wxRadioButton
*selBtn
;
255 // First check all previous buttons
256 for (selBtn
= wxGetPreviousButtonInGroup(btn
); selBtn
; selBtn
= wxGetPreviousButtonInGroup(selBtn
))
257 if (selBtn
->GetValue())
260 // Now all following buttons
261 for (selBtn
= wxGetNextButtonInGroup(btn
); selBtn
; selBtn
= wxGetNextButtonInGroup(selBtn
))
262 if (selBtn
->GetValue())
270 // ----------------------------------------------------------------------------
271 // Keyboard handling - this is the place where the TAB traversal logic is
272 // implemented. As this code is common to all ports, this ensures consistent
273 // behaviour even if we don't specify how exactly the wxNavigationKeyEvent are
274 // generated and this is done in platform specific code which also ensures that
275 // we can follow the given platform standards.
276 // ----------------------------------------------------------------------------
278 void wxControlContainer::HandleOnNavigationKey( wxNavigationKeyEvent
& event
)
280 wxWindow
*parent
= m_winParent
->GetParent();
282 // the event is propagated downwards if the event emitter was our parent
283 bool goingDown
= event
.GetEventObject() == parent
;
285 const wxWindowList
& children
= m_winParent
->GetChildren();
287 // if we have exactly one notebook-like child window (actually it could be
288 // any window that returns true from its HasMultiplePages()), then
289 // [Shift-]Ctrl-Tab and Ctrl-PageUp/Down keys should iterate over its pages
290 // even if the focus is outside of the control because this is how the
291 // standard MSW properties dialogs behave and we do it under other platforms
292 // as well because it seems like a good idea -- but we can always put this
293 // block inside "#ifdef __WXMSW__" if it's not suitable there
294 if ( event
.IsWindowChange() && !goingDown
)
296 // check if we have a unique notebook-like child
297 wxWindow
*bookctrl
= NULL
;
298 for ( wxWindowList::const_iterator i
= children
.begin(),
299 end
= children
.end();
303 wxWindow
* const window
= *i
;
304 if ( window
->HasMultiplePages() )
308 // this is the second book-like control already so don't do
309 // anything as we don't know which one should have its page
321 // make sure that we don't bubble up the event again from the book
322 // control resulting in infinite recursion
323 wxNavigationKeyEvent
eventCopy(event
);
324 eventCopy
.SetEventObject(m_winParent
);
325 if ( bookctrl
->GetEventHandler()->ProcessEvent(eventCopy
) )
330 // there is not much to do if we don't have children and we're not
331 // interested in "notebook page change" events here
332 if ( !children
.GetCount() || event
.IsWindowChange() )
334 // let the parent process it unless it already comes from our parent
335 // of we don't have any
337 !parent
|| !parent
->GetEventHandler()->ProcessEvent(event
) )
345 // where are we going?
346 const bool forward
= event
.GetDirection();
348 // the node of the children list from which we should start looking for the
349 // next acceptable child
350 wxWindowList::compatibility_iterator node
, start_node
;
352 // we should start from the first/last control and not from the one which
353 // had focus the last time if we're propagating the event downwards because
354 // for our parent we look like a single control
357 // just to be sure it's not used (normally this is not necessary, but
358 // doesn't hurt neither)
359 m_winLastFocused
= (wxWindow
*)NULL
;
361 // start from first or last depending on where we're going
362 node
= forward
? children
.GetFirst() : children
.GetLast();
366 // try to find the child which has the focus currently
368 // the event emitter might have done this for us
369 wxWindow
*winFocus
= event
.GetCurrentFocus();
371 // but if not, we might know where the focus was ourselves
373 winFocus
= m_winLastFocused
;
375 // if still no luck, do it the hard way
377 winFocus
= wxWindow::FindFocus();
382 // If we are in a radio button group, start from the first item in the
384 if ( event
.IsFromTab() && wxIsKindOf(winFocus
, wxRadioButton
) )
385 winFocus
= wxGetFirstButtonInGroup((wxRadioButton
*)winFocus
);
387 // ok, we found the focus - now is it our child?
388 start_node
= children
.Find( winFocus
);
391 if ( !start_node
&& m_winLastFocused
)
393 // window which has focus isn't our child, fall back to the one
394 // which had the focus the last time
395 start_node
= children
.Find( m_winLastFocused
);
398 // if we still didn't find anything, we should start with the first one
401 start_node
= children
.GetFirst();
404 // and the first child which we can try setting focus to is the next or
406 node
= forward
? start_node
->GetNext() : start_node
->GetPrevious();
409 // we want to cycle over all elements passing by NULL
412 // don't go into infinite loop
413 if ( start_node
&& node
&& node
== start_node
)
416 // Have we come to the last or first item on the panel?
421 // exit now as otherwise we'd loop forever
427 // Check if our (maybe grand) parent is another panel: if this
428 // is the case, they will know what to do with this navigation
429 // key and so give them the chance to process it instead of
430 // looping inside this panel (normally, the focus will go to
431 // the next/previous item after this panel in the parent
433 wxWindow
*focussed_child_of_parent
= m_winParent
;
436 // we don't want to tab into a different dialog or frame
437 if ( focussed_child_of_parent
->IsTopLevel() )
440 event
.SetCurrentFocus( focussed_child_of_parent
);
441 if ( parent
->GetEventHandler()->ProcessEvent( event
) )
444 focussed_child_of_parent
= parent
;
446 parent
= parent
->GetParent();
449 //else: as the focus came from our parent, we definitely don't want
450 // to send it back to it!
452 // no, we are not inside another panel so process this ourself
453 node
= forward
? children
.GetFirst() : children
.GetLast();
458 wxWindow
*child
= node
->GetData();
461 if ( event
.IsFromTab() )
463 if ( wxIsKindOf(child
, wxRadioButton
) )
465 // only radio buttons with either wxRB_GROUP or wxRB_SINGLE
467 if ( child
->HasFlag(wxRB_GROUP
) )
469 // need to tab into the active button within a group
470 wxRadioButton
*rb
= wxGetSelectedButtonInGroup((wxRadioButton
*)child
);
474 else if ( !child
->HasFlag(wxRB_SINGLE
) )
476 node
= forward
? node
->GetNext() : node
->GetPrevious();
481 else if ( m_winLastFocused
&&
482 wxIsKindOf(m_winLastFocused
, wxRadioButton
) &&
483 !m_winLastFocused
->HasFlag(wxRB_SINGLE
) )
485 // cursor keys don't navigate out of a radio button group so
486 // find the correct radio button to focus
489 child
= wxGetNextButtonInGroup((wxRadioButton
*)m_winLastFocused
);
492 // no next button in group, set it to the first button
493 child
= wxGetFirstButtonInGroup((wxRadioButton
*)m_winLastFocused
);
498 child
= wxGetPreviousButtonInGroup((wxRadioButton
*)m_winLastFocused
);
501 // no previous button in group, set it to the last button
502 child
= wxGetLastButtonInGroup((wxRadioButton
*)m_winLastFocused
);
506 if ( child
== m_winLastFocused
)
508 // must be a group consisting of only one button therefore
509 // no need to send a navigation event
516 if ( child
->CanAcceptFocusFromKeyboard() )
518 // if we're setting the focus to a child panel we should prevent it
519 // from giving it to the child which had the focus the last time
520 // and instead give it to the first/last child depending from which
521 // direction we're coming
522 event
.SetEventObject(m_winParent
);
524 // disable propagation for this call as otherwise the event might
525 // bounce back to us.
526 wxPropagationDisabler
disableProp(event
);
527 if ( !child
->GetEventHandler()->ProcessEvent(event
) )
529 // set it first in case SetFocusFromKbd() results in focus
531 m_winLastFocused
= child
;
533 // everything is simple: just give focus to it
534 child
->SetFocusFromKbd();
536 //else: the child manages its focus itself
543 node
= forward
? node
->GetNext() : node
->GetPrevious();
546 // we cycled through all of our children and none of them wanted to accept
551 void wxControlContainer::HandleOnWindowDestroy(wxWindowBase
*child
)
553 if ( child
== m_winLastFocused
)
554 m_winLastFocused
= NULL
;
557 // ----------------------------------------------------------------------------
559 // ----------------------------------------------------------------------------
561 bool wxControlContainer::DoSetFocus()
563 wxLogTrace(TRACE_FOCUS
, _T("SetFocus on wxPanel 0x%p."),
564 m_winParent
->GetHandle());
569 // when the panel gets the focus we move the focus to either the last
570 // window that had the focus or the first one that can get it unless the
571 // focus had been already set to some other child
573 wxWindow
*win
= wxWindow::FindFocus();
576 if ( win
== m_winParent
)
578 // our child already has focus, don't take it away from it
582 if ( win
->IsTopLevel() )
584 // don't look beyond the first top level parent - useless and
589 win
= win
->GetParent();
592 // protect against infinite recursion:
595 bool ret
= SetFocusToChild();
597 m_inSetFocus
= false;
602 void wxControlContainer::HandleOnFocus(wxFocusEvent
& event
)
604 wxLogTrace(TRACE_FOCUS
, _T("OnFocus on wxPanel 0x%p, name: %s"),
605 m_winParent
->GetHandle(),
606 m_winParent
->GetName().c_str() );
613 bool wxControlContainer::SetFocusToChild()
615 return wxSetFocusToChild(m_winParent
, &m_winLastFocused
);
618 // ----------------------------------------------------------------------------
619 // SetFocusToChild(): this function is used by wxPanel but also by wxFrame in
620 // wxMSW, this is why it is outside of wxControlContainer class
621 // ----------------------------------------------------------------------------
623 bool wxSetFocusToChild(wxWindow
*win
, wxWindow
**childLastFocused
)
625 wxCHECK_MSG( win
, false, _T("wxSetFocusToChild(): invalid window") );
626 wxCHECK_MSG( childLastFocused
, false,
627 _T("wxSetFocusToChild(): NULL child poonter") );
629 if ( *childLastFocused
)
631 // It might happen that the window got reparented
632 if ( (*childLastFocused
)->GetParent() == win
)
634 wxLogTrace(TRACE_FOCUS
,
635 _T("SetFocusToChild() => last child (0x%p)."),
636 (*childLastFocused
)->GetHandle());
638 // not SetFocusFromKbd(): we're restoring focus back to the old
639 // window and not setting it as the result of a kbd action
640 (*childLastFocused
)->SetFocus();
645 // it doesn't count as such any more
646 *childLastFocused
= (wxWindow
*)NULL
;
650 // set the focus to the first child who wants it
651 wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst();
654 wxWindow
*child
= node
->GetData();
655 node
= node
->GetNext();
658 if ( child
->GetParent()->MacIsWindowScrollbar( child
) )
662 if ( child
->CanAcceptFocusFromKeyboard() && !child
->IsTopLevel() )
665 // If a radiobutton is the first focusable child, search for the
666 // selected radiobutton in the same group
667 wxRadioButton
* btn
= wxDynamicCast(child
, wxRadioButton
);
670 wxRadioButton
* selected
= wxGetSelectedButtonInGroup(btn
);
676 wxLogTrace(TRACE_FOCUS
,
677 _T("SetFocusToChild() => first child (0x%p)."),
680 *childLastFocused
= child
;
681 child
->SetFocusFromKbd();
689 #endif // !wxHAS_NATIVE_TAB_TRAVERSAL