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"
30 #include "wx/window.h"
31 #include "wx/scrolbar.h"
32 #include "wx/radiobut.h"
33 #include "wx/containr.h"
36 // trace mask for focus messages
37 #define TRACE_FOCUS _T("focus")
39 // ============================================================================
41 // ============================================================================
43 wxControlContainer::wxControlContainer(wxWindow
*winParent
)
45 m_winParent
= winParent
;
53 bool wxControlContainer::AcceptsFocus() const
55 // if we're not shown or disabled, we can't accept focus
56 if ( m_winParent
->IsShown() && m_winParent
->IsEnabled() )
58 // otherwise we can accept focus either if we have no children at all
59 // (in this case we're probably not used as a container) or only when
60 // at least one child will accept focus
61 wxWindowList::compatibility_iterator node
= m_winParent
->GetChildren().GetFirst();
66 // wxMac has eventually the two scrollbars as children, they don't count
67 // as real children in the algorithm mentioned above
68 bool hasRealChildren
= false ;
73 wxWindow
*child
= node
->GetData();
75 if ( child
->AcceptsFocus() )
81 wxScrollBar
*sb
= wxDynamicCast( child
, wxScrollBar
) ;
82 if ( sb
== NULL
|| !m_winParent
->MacIsWindowScrollbar( sb
) )
83 hasRealChildren
= true ;
85 node
= node
->GetNext();
89 if ( !hasRealChildren
)
97 void wxControlContainer::SetLastFocus(wxWindow
*win
)
99 // the panel itself should never get the focus at all but if it does happen
100 // temporarily (as it seems to do under wxGTK), at the very least don't
101 // forget our previous m_winLastFocused
102 if ( win
!= m_winParent
)
104 // if we're setting the focus
107 // find the last _immediate_ child which got focus
108 wxWindow
*winParent
= win
;
109 while ( winParent
!= m_winParent
)
112 winParent
= win
->GetParent();
114 // Yes, this can happen, though in a totally pathological case.
115 // like when detaching a menubar from a frame with a child
116 // which has pushed itself as an event handler for the menubar.
119 wxASSERT_MSG( winParent
,
120 _T("Setting last focus for a window that is not our child?") );
124 m_winLastFocused
= win
;
128 wxLogTrace(TRACE_FOCUS
, _T("Set last focus to %s(%s)"),
129 win
->GetClassInfo()->GetClassName(),
130 win
->GetLabel().c_str());
134 wxLogTrace(TRACE_FOCUS
, _T("No more last focus"));
138 // propagate the last focus upwards so that our parent can set focus back
139 // to us if it loses it now and regains later
140 wxWindow
*parent
= m_winParent
->GetParent();
143 wxChildFocusEvent
eventFocus(m_winParent
);
144 parent
->GetEventHandler()->ProcessEvent(eventFocus
);
148 // --------------------------------------------------------------------
149 // The following four functions are used to find other radio buttons
150 // within the same group. Used by wxSetFocusToChild on wxMSW
151 // --------------------------------------------------------------------
155 wxRadioButton
* wxGetPreviousButtonInGroup(wxRadioButton
*btn
)
157 if ( btn
->HasFlag(wxRB_GROUP
) || btn
->HasFlag(wxRB_SINGLE
) )
160 const wxWindowList
& siblings
= btn
->GetParent()->GetChildren();
161 wxWindowList::compatibility_iterator nodeThis
= siblings
.Find(btn
);
162 wxCHECK_MSG( nodeThis
, NULL
, _T("radio button not a child of its parent?") );
164 // Iterate over all previous siblings until we find the next radio button
165 wxWindowList::compatibility_iterator nodeBefore
= nodeThis
->GetPrevious();
166 wxRadioButton
*prevBtn
= 0;
169 prevBtn
= wxDynamicCast(nodeBefore
->GetData(), wxRadioButton
);
173 nodeBefore
= nodeBefore
->GetPrevious();
176 if (!prevBtn
|| prevBtn
->HasFlag(wxRB_SINGLE
))
178 // no more buttons in group
185 wxRadioButton
* wxGetNextButtonInGroup(wxRadioButton
*btn
)
187 if (btn
->HasFlag(wxRB_SINGLE
))
190 const wxWindowList
& siblings
= btn
->GetParent()->GetChildren();
191 wxWindowList::compatibility_iterator nodeThis
= siblings
.Find(btn
);
192 wxCHECK_MSG( nodeThis
, NULL
, _T("radio button not a child of its parent?") );
194 // Iterate over all previous siblings until we find the next radio button
195 wxWindowList::compatibility_iterator nodeNext
= nodeThis
->GetNext();
196 wxRadioButton
*nextBtn
= 0;
199 nextBtn
= wxDynamicCast(nodeNext
->GetData(), wxRadioButton
);
203 nodeNext
= nodeNext
->GetNext();
206 if ( !nextBtn
|| nextBtn
->HasFlag(wxRB_GROUP
) || nextBtn
->HasFlag(wxRB_SINGLE
) )
208 // no more buttons or the first button of the next group
215 wxRadioButton
* wxGetFirstButtonInGroup(wxRadioButton
*btn
)
219 wxRadioButton
* prevBtn
= wxGetPreviousButtonInGroup(btn
);
227 wxRadioButton
* wxGetLastButtonInGroup(wxRadioButton
*btn
)
231 wxRadioButton
* nextBtn
= wxGetNextButtonInGroup(btn
);
239 wxRadioButton
* wxGetSelectedButtonInGroup(wxRadioButton
*btn
)
241 // Find currently selected button
245 if (btn
->HasFlag(wxRB_SINGLE
))
248 wxRadioButton
*selBtn
;
250 // First check all previous buttons
251 for (selBtn
= wxGetPreviousButtonInGroup(btn
); selBtn
; selBtn
= wxGetPreviousButtonInGroup(selBtn
))
252 if (selBtn
->GetValue())
255 // Now all following buttons
256 for (selBtn
= wxGetNextButtonInGroup(btn
); selBtn
; selBtn
= wxGetNextButtonInGroup(selBtn
))
257 if (selBtn
->GetValue())
265 // ----------------------------------------------------------------------------
266 // Keyboard handling - this is the place where the TAB traversal logic is
267 // implemented. As this code is common to all ports, this ensures consistent
268 // behaviour even if we don't specify how exactly the wxNavigationKeyEvent are
269 // generated and this is done in platform specific code which also ensures that
270 // we can follow the given platform standards.
271 // ----------------------------------------------------------------------------
273 void wxControlContainer::HandleOnNavigationKey( wxNavigationKeyEvent
& event
)
275 wxWindow
*parent
= m_winParent
->GetParent();
277 // the event is propagated downwards if the event emitter was our parent
278 bool goingDown
= event
.GetEventObject() == parent
;
280 const wxWindowList
& children
= m_winParent
->GetChildren();
282 // there is not much to do if we don't have children and we're not
283 // interested in "notebook page change" events here
284 if ( !children
.GetCount() || event
.IsWindowChange() )
286 // let the parent process it unless it already comes from our parent
287 // of we don't have any
289 !parent
|| !parent
->GetEventHandler()->ProcessEvent(event
) )
297 // where are we going?
298 const bool forward
= event
.GetDirection();
300 // the node of the children list from which we should start looking for the
301 // next acceptable child
302 wxWindowList::compatibility_iterator node
, start_node
;
304 // we should start from the first/last control and not from the one which
305 // had focus the last time if we're propagating the event downwards because
306 // for our parent we look like a single control
309 // just to be sure it's not used (normally this is not necessary, but
310 // doesn't hurt neither)
311 m_winLastFocused
= (wxWindow
*)NULL
;
313 // start from first or last depending on where we're going
314 node
= forward
? children
.GetFirst() : children
.GetLast();
318 // try to find the child which has the focus currently
320 // the event emitter might have done this for us
321 wxWindow
*winFocus
= event
.GetCurrentFocus();
323 // but if not, we might know where the focus was ourselves
325 winFocus
= m_winLastFocused
;
327 // if still no luck, do it the hard way
329 winFocus
= wxWindow::FindFocus();
334 // If we are in a radio button group, start from the first item in the
336 if ( event
.IsFromTab() && wxIsKindOf(winFocus
, wxRadioButton
) )
337 winFocus
= wxGetFirstButtonInGroup((wxRadioButton
*)winFocus
);
339 // ok, we found the focus - now is it our child?
340 start_node
= children
.Find( winFocus
);
343 if ( !start_node
&& m_winLastFocused
)
345 // window which has focus isn't our child, fall back to the one
346 // which had the focus the last time
347 start_node
= children
.Find( m_winLastFocused
);
350 // if we still didn't find anything, we should start with the first one
353 start_node
= children
.GetFirst();
356 // and the first child which we can try setting focus to is the next or
358 node
= forward
? start_node
->GetNext() : start_node
->GetPrevious();
361 // we want to cycle over all elements passing by NULL
364 // don't go into infinite loop
365 if ( start_node
&& node
&& node
== start_node
)
368 // Have we come to the last or first item on the panel?
373 // exit now as otherwise we'd loop forever
379 // Check if our (maybe grand) parent is another panel: if this
380 // is the case, they will know what to do with this navigation
381 // key and so give them the chance to process it instead of
382 // looping inside this panel (normally, the focus will go to
383 // the next/previous item after this panel in the parent
385 wxWindow
*focussed_child_of_parent
= m_winParent
;
388 // we don't want to tab into a different dialog or frame
389 if ( focussed_child_of_parent
->IsTopLevel() )
392 event
.SetCurrentFocus( focussed_child_of_parent
);
393 if ( parent
->GetEventHandler()->ProcessEvent( event
) )
396 focussed_child_of_parent
= parent
;
398 parent
= parent
->GetParent();
401 //else: as the focus came from our parent, we definitely don't want
402 // to send it back to it!
404 // no, we are not inside another panel so process this ourself
405 node
= forward
? children
.GetFirst() : children
.GetLast();
410 wxWindow
*child
= node
->GetData();
413 if ( event
.IsFromTab() )
415 if ( wxIsKindOf(child
, wxRadioButton
) )
417 // only radio buttons with either wxRB_GROUP or wxRB_SINGLE
419 if ( child
->HasFlag(wxRB_GROUP
) )
421 // need to tab into the active button within a group
422 wxRadioButton
*rb
= wxGetSelectedButtonInGroup((wxRadioButton
*)child
);
426 else if ( !child
->HasFlag(wxRB_SINGLE
) )
428 node
= forward
? node
->GetNext() : node
->GetPrevious();
433 else if ( m_winLastFocused
&&
434 wxIsKindOf(m_winLastFocused
, wxRadioButton
) &&
435 !m_winLastFocused
->HasFlag(wxRB_SINGLE
) )
437 // cursor keys don't navigate out of a radio button group so
438 // find the correct radio button to focus
441 child
= wxGetNextButtonInGroup((wxRadioButton
*)m_winLastFocused
);
444 // no next button in group, set it to the first button
445 child
= wxGetFirstButtonInGroup((wxRadioButton
*)m_winLastFocused
);
450 child
= wxGetPreviousButtonInGroup((wxRadioButton
*)m_winLastFocused
);
453 // no previous button in group, set it to the last button
454 child
= wxGetLastButtonInGroup((wxRadioButton
*)m_winLastFocused
);
458 if ( child
== m_winLastFocused
)
460 // must be a group consisting of only one button therefore
461 // no need to send a navigation event
468 if ( child
->AcceptsFocusFromKeyboard() )
470 // if we're setting the focus to a child panel we should prevent it
471 // from giving it to the child which had the focus the last time
472 // and instead give it to the first/last child depending from which
473 // direction we're coming
474 event
.SetEventObject(m_winParent
);
476 // disable propagation for this call as otherwise the event might
477 // bounce back to us.
478 wxPropagationDisabler
disableProp(event
);
479 if ( !child
->GetEventHandler()->ProcessEvent(event
) )
481 // set it first in case SetFocusFromKbd() results in focus
483 m_winLastFocused
= child
;
485 // everything is simple: just give focus to it
486 child
->SetFocusFromKbd();
488 //else: the child manages its focus itself
495 node
= forward
? node
->GetNext() : node
->GetPrevious();
498 // we cycled through all of our children and none of them wanted to accept
503 void wxControlContainer::HandleOnWindowDestroy(wxWindowBase
*child
)
505 if ( child
== m_winLastFocused
)
506 m_winLastFocused
= NULL
;
508 if ( child
== m_winDefault
)
511 if ( child
== m_winTmpDefault
)
512 m_winTmpDefault
= NULL
;
515 // ----------------------------------------------------------------------------
517 // ----------------------------------------------------------------------------
519 bool wxControlContainer::DoSetFocus()
521 wxLogTrace(TRACE_FOCUS
, _T("SetFocus on wxPanel 0x%p."),
522 m_winParent
->GetHandle());
527 // when the panel gets the focus we move the focus to either the last
528 // window that had the focus or the first one that can get it unless the
529 // focus had been already set to some other child
531 wxWindow
*win
= wxWindow::FindFocus();
534 if ( win
== m_winParent
)
536 // our child already has focus, don't take it away from it
540 if ( win
->IsTopLevel() )
542 // don't look beyond the first top level parent - useless and
547 win
= win
->GetParent();
550 // protect against infinite recursion:
553 bool ret
= SetFocusToChild();
555 m_inSetFocus
= false;
560 void wxControlContainer::HandleOnFocus(wxFocusEvent
& event
)
562 wxLogTrace(TRACE_FOCUS
, _T("OnFocus on wxPanel 0x%p, name: %s"),
563 m_winParent
->GetHandle(),
564 m_winParent
->GetName().c_str() );
571 bool wxControlContainer::SetFocusToChild()
573 return wxSetFocusToChild(m_winParent
, &m_winLastFocused
);
576 // ----------------------------------------------------------------------------
577 // SetFocusToChild(): this function is used by wxPanel but also by wxFrame in
578 // wxMSW, this is why it is outside of wxControlContainer class
579 // ----------------------------------------------------------------------------
581 bool wxSetFocusToChild(wxWindow
*win
, wxWindow
**childLastFocused
)
583 wxCHECK_MSG( win
, false, _T("wxSetFocusToChild(): invalid window") );
584 wxCHECK_MSG( childLastFocused
, false,
585 _T("wxSetFocusToChild(): NULL child poonter") );
587 if ( *childLastFocused
)
589 // It might happen that the window got reparented
590 if ( (*childLastFocused
)->GetParent() == win
)
592 wxLogTrace(TRACE_FOCUS
,
593 _T("SetFocusToChild() => last child (0x%p)."),
594 (*childLastFocused
)->GetHandle());
596 // not SetFocusFromKbd(): we're restoring focus back to the old
597 // window and not setting it as the result of a kbd action
598 (*childLastFocused
)->SetFocus();
603 // it doesn't count as such any more
604 *childLastFocused
= (wxWindow
*)NULL
;
608 // set the focus to the first child who wants it
609 wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst();
612 wxWindow
*child
= node
->GetData();
614 if ( child
->AcceptsFocusFromKeyboard() && !child
->IsTopLevel() )
617 // If a radiobutton is the first focusable child, search for the
618 // selected radiobutton in the same group
619 wxRadioButton
* btn
= wxDynamicCast(child
, wxRadioButton
);
622 wxRadioButton
* selected
= wxGetSelectedButtonInGroup(btn
);
628 wxLogTrace(TRACE_FOCUS
,
629 _T("SetFocusToChild() => first child (0x%p)."),
632 *childLastFocused
= child
;
633 child
->SetFocusFromKbd();
637 node
= node
->GetNext();