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"
33 #include "wx/containr.h"
36 #include "wx/scrolbar.h"
40 #include "wx/radiobut.h"
43 // trace mask for focus messages
44 #define TRACE_FOCUS _T("focus")
46 // ============================================================================
48 // ============================================================================
50 wxControlContainer::wxControlContainer(wxWindow
*winParent
)
52 m_winParent
= winParent
;
60 bool wxControlContainer::AcceptsFocus() const
62 // if we're not shown or disabled, we can't accept focus
63 if ( m_winParent
->IsShown() && m_winParent
->IsEnabled() )
65 // otherwise we can accept focus either if we have no children at all
66 // (in this case we're probably not used as a container) or only when
67 // at least one child will accept focus
68 wxWindowList::compatibility_iterator node
= m_winParent
->GetChildren().GetFirst();
73 // wxMac has eventually the two scrollbars as children, they don't count
74 // as real children in the algorithm mentioned above
75 bool hasRealChildren
= false ;
80 wxWindow
*child
= node
->GetData();
82 if ( child
->AcceptsFocus() )
88 wxScrollBar
*sb
= wxDynamicCast( child
, wxScrollBar
) ;
89 if ( sb
== NULL
|| !m_winParent
->MacIsWindowScrollbar( sb
) )
90 hasRealChildren
= true ;
92 node
= node
->GetNext();
96 if ( !hasRealChildren
)
104 void wxControlContainer::SetLastFocus(wxWindow
*win
)
106 // the panel itself should never get the focus at all but if it does happen
107 // temporarily (as it seems to do under wxGTK), at the very least don't
108 // forget our previous m_winLastFocused
109 if ( win
!= m_winParent
)
111 // if we're setting the focus
114 // find the last _immediate_ child which got focus
115 wxWindow
*winParent
= win
;
116 while ( winParent
!= m_winParent
)
119 winParent
= win
->GetParent();
121 // Yes, this can happen, though in a totally pathological case.
122 // like when detaching a menubar from a frame with a child
123 // which has pushed itself as an event handler for the menubar.
126 wxASSERT_MSG( winParent
,
127 _T("Setting last focus for a window that is not our child?") );
131 m_winLastFocused
= win
;
135 wxLogTrace(TRACE_FOCUS
, _T("Set last focus to %s(%s)"),
136 win
->GetClassInfo()->GetClassName(),
137 win
->GetLabel().c_str());
141 wxLogTrace(TRACE_FOCUS
, _T("No more last focus"));
145 // propagate the last focus upwards so that our parent can set focus back
146 // to us if it loses it now and regains later
147 wxWindow
*parent
= m_winParent
->GetParent();
150 wxChildFocusEvent
eventFocus(m_winParent
);
151 parent
->GetEventHandler()->ProcessEvent(eventFocus
);
155 // --------------------------------------------------------------------
156 // The following four functions are used to find other radio buttons
157 // within the same group. Used by wxSetFocusToChild on wxMSW
158 // --------------------------------------------------------------------
162 wxRadioButton
* wxGetPreviousButtonInGroup(wxRadioButton
*btn
)
164 if ( btn
->HasFlag(wxRB_GROUP
) || btn
->HasFlag(wxRB_SINGLE
) )
167 const wxWindowList
& siblings
= btn
->GetParent()->GetChildren();
168 wxWindowList::compatibility_iterator nodeThis
= siblings
.Find(btn
);
169 wxCHECK_MSG( nodeThis
, NULL
, _T("radio button not a child of its parent?") );
171 // Iterate over all previous siblings until we find the next radio button
172 wxWindowList::compatibility_iterator nodeBefore
= nodeThis
->GetPrevious();
173 wxRadioButton
*prevBtn
= 0;
176 prevBtn
= wxDynamicCast(nodeBefore
->GetData(), wxRadioButton
);
180 nodeBefore
= nodeBefore
->GetPrevious();
183 if (!prevBtn
|| prevBtn
->HasFlag(wxRB_SINGLE
))
185 // no more buttons in group
192 wxRadioButton
* wxGetNextButtonInGroup(wxRadioButton
*btn
)
194 if (btn
->HasFlag(wxRB_SINGLE
))
197 const wxWindowList
& siblings
= btn
->GetParent()->GetChildren();
198 wxWindowList::compatibility_iterator nodeThis
= siblings
.Find(btn
);
199 wxCHECK_MSG( nodeThis
, NULL
, _T("radio button not a child of its parent?") );
201 // Iterate over all previous siblings until we find the next radio button
202 wxWindowList::compatibility_iterator nodeNext
= nodeThis
->GetNext();
203 wxRadioButton
*nextBtn
= 0;
206 nextBtn
= wxDynamicCast(nodeNext
->GetData(), wxRadioButton
);
210 nodeNext
= nodeNext
->GetNext();
213 if ( !nextBtn
|| nextBtn
->HasFlag(wxRB_GROUP
) || nextBtn
->HasFlag(wxRB_SINGLE
) )
215 // no more buttons or the first button of the next group
222 wxRadioButton
* wxGetFirstButtonInGroup(wxRadioButton
*btn
)
226 wxRadioButton
* prevBtn
= wxGetPreviousButtonInGroup(btn
);
234 wxRadioButton
* wxGetLastButtonInGroup(wxRadioButton
*btn
)
238 wxRadioButton
* nextBtn
= wxGetNextButtonInGroup(btn
);
246 wxRadioButton
* wxGetSelectedButtonInGroup(wxRadioButton
*btn
)
248 // Find currently selected button
252 if (btn
->HasFlag(wxRB_SINGLE
))
255 wxRadioButton
*selBtn
;
257 // First check all previous buttons
258 for (selBtn
= wxGetPreviousButtonInGroup(btn
); selBtn
; selBtn
= wxGetPreviousButtonInGroup(selBtn
))
259 if (selBtn
->GetValue())
262 // Now all following buttons
263 for (selBtn
= wxGetNextButtonInGroup(btn
); selBtn
; selBtn
= wxGetNextButtonInGroup(selBtn
))
264 if (selBtn
->GetValue())
272 // ----------------------------------------------------------------------------
273 // Keyboard handling - this is the place where the TAB traversal logic is
274 // implemented. As this code is common to all ports, this ensures consistent
275 // behaviour even if we don't specify how exactly the wxNavigationKeyEvent are
276 // generated and this is done in platform specific code which also ensures that
277 // we can follow the given platform standards.
278 // ----------------------------------------------------------------------------
280 void wxControlContainer::HandleOnNavigationKey( wxNavigationKeyEvent
& event
)
282 wxWindow
*parent
= m_winParent
->GetParent();
284 // the event is propagated downwards if the event emitter was our parent
285 bool goingDown
= event
.GetEventObject() == parent
;
287 const wxWindowList
& children
= m_winParent
->GetChildren();
289 // there is not much to do if we don't have children and we're not
290 // interested in "notebook page change" events here
291 if ( !children
.GetCount() || event
.IsWindowChange() )
293 // let the parent process it unless it already comes from our parent
294 // of we don't have any
296 !parent
|| !parent
->GetEventHandler()->ProcessEvent(event
) )
304 // where are we going?
305 const bool forward
= event
.GetDirection();
307 // the node of the children list from which we should start looking for the
308 // next acceptable child
309 wxWindowList::compatibility_iterator node
, start_node
;
311 // we should start from the first/last control and not from the one which
312 // had focus the last time if we're propagating the event downwards because
313 // for our parent we look like a single control
316 // just to be sure it's not used (normally this is not necessary, but
317 // doesn't hurt neither)
318 m_winLastFocused
= (wxWindow
*)NULL
;
320 // start from first or last depending on where we're going
321 node
= forward
? children
.GetFirst() : children
.GetLast();
325 // try to find the child which has the focus currently
327 // the event emitter might have done this for us
328 wxWindow
*winFocus
= event
.GetCurrentFocus();
330 // but if not, we might know where the focus was ourselves
332 winFocus
= m_winLastFocused
;
334 // if still no luck, do it the hard way
336 winFocus
= wxWindow::FindFocus();
341 // If we are in a radio button group, start from the first item in the
343 if ( event
.IsFromTab() && wxIsKindOf(winFocus
, wxRadioButton
) )
344 winFocus
= wxGetFirstButtonInGroup((wxRadioButton
*)winFocus
);
346 // ok, we found the focus - now is it our child?
347 start_node
= children
.Find( winFocus
);
350 if ( !start_node
&& m_winLastFocused
)
352 // window which has focus isn't our child, fall back to the one
353 // which had the focus the last time
354 start_node
= children
.Find( m_winLastFocused
);
357 // if we still didn't find anything, we should start with the first one
360 start_node
= children
.GetFirst();
363 // and the first child which we can try setting focus to is the next or
365 node
= forward
? start_node
->GetNext() : start_node
->GetPrevious();
368 // we want to cycle over all elements passing by NULL
371 // don't go into infinite loop
372 if ( start_node
&& node
&& node
== start_node
)
375 // Have we come to the last or first item on the panel?
380 // exit now as otherwise we'd loop forever
386 // Check if our (maybe grand) parent is another panel: if this
387 // is the case, they will know what to do with this navigation
388 // key and so give them the chance to process it instead of
389 // looping inside this panel (normally, the focus will go to
390 // the next/previous item after this panel in the parent
392 wxWindow
*focussed_child_of_parent
= m_winParent
;
395 // we don't want to tab into a different dialog or frame
396 if ( focussed_child_of_parent
->IsTopLevel() )
399 event
.SetCurrentFocus( focussed_child_of_parent
);
400 if ( parent
->GetEventHandler()->ProcessEvent( event
) )
403 focussed_child_of_parent
= parent
;
405 parent
= parent
->GetParent();
408 //else: as the focus came from our parent, we definitely don't want
409 // to send it back to it!
411 // no, we are not inside another panel so process this ourself
412 node
= forward
? children
.GetFirst() : children
.GetLast();
417 wxWindow
*child
= node
->GetData();
420 if ( event
.IsFromTab() )
422 if ( wxIsKindOf(child
, wxRadioButton
) )
424 // only radio buttons with either wxRB_GROUP or wxRB_SINGLE
426 if ( child
->HasFlag(wxRB_GROUP
) )
428 // need to tab into the active button within a group
429 wxRadioButton
*rb
= wxGetSelectedButtonInGroup((wxRadioButton
*)child
);
433 else if ( !child
->HasFlag(wxRB_SINGLE
) )
435 node
= forward
? node
->GetNext() : node
->GetPrevious();
440 else if ( m_winLastFocused
&&
441 wxIsKindOf(m_winLastFocused
, wxRadioButton
) &&
442 !m_winLastFocused
->HasFlag(wxRB_SINGLE
) )
444 // cursor keys don't navigate out of a radio button group so
445 // find the correct radio button to focus
448 child
= wxGetNextButtonInGroup((wxRadioButton
*)m_winLastFocused
);
451 // no next button in group, set it to the first button
452 child
= wxGetFirstButtonInGroup((wxRadioButton
*)m_winLastFocused
);
457 child
= wxGetPreviousButtonInGroup((wxRadioButton
*)m_winLastFocused
);
460 // no previous button in group, set it to the last button
461 child
= wxGetLastButtonInGroup((wxRadioButton
*)m_winLastFocused
);
465 if ( child
== m_winLastFocused
)
467 // must be a group consisting of only one button therefore
468 // no need to send a navigation event
475 if ( child
->AcceptsFocusFromKeyboard() )
477 // if we're setting the focus to a child panel we should prevent it
478 // from giving it to the child which had the focus the last time
479 // and instead give it to the first/last child depending from which
480 // direction we're coming
481 event
.SetEventObject(m_winParent
);
483 // disable propagation for this call as otherwise the event might
484 // bounce back to us.
485 wxPropagationDisabler
disableProp(event
);
486 if ( !child
->GetEventHandler()->ProcessEvent(event
) )
488 // set it first in case SetFocusFromKbd() results in focus
490 m_winLastFocused
= child
;
492 // everything is simple: just give focus to it
493 child
->SetFocusFromKbd();
495 //else: the child manages its focus itself
502 node
= forward
? node
->GetNext() : node
->GetPrevious();
505 // we cycled through all of our children and none of them wanted to accept
510 void wxControlContainer::HandleOnWindowDestroy(wxWindowBase
*child
)
512 if ( child
== m_winLastFocused
)
513 m_winLastFocused
= NULL
;
515 if ( child
== m_winDefault
)
518 if ( child
== m_winTmpDefault
)
519 m_winTmpDefault
= NULL
;
522 // ----------------------------------------------------------------------------
524 // ----------------------------------------------------------------------------
526 bool wxControlContainer::DoSetFocus()
528 wxLogTrace(TRACE_FOCUS
, _T("SetFocus on wxPanel 0x%p."),
529 m_winParent
->GetHandle());
534 // when the panel gets the focus we move the focus to either the last
535 // window that had the focus or the first one that can get it unless the
536 // focus had been already set to some other child
538 wxWindow
*win
= wxWindow::FindFocus();
541 if ( win
== m_winParent
)
543 // our child already has focus, don't take it away from it
547 if ( win
->IsTopLevel() )
549 // don't look beyond the first top level parent - useless and
554 win
= win
->GetParent();
557 // protect against infinite recursion:
560 bool ret
= SetFocusToChild();
562 m_inSetFocus
= false;
567 void wxControlContainer::HandleOnFocus(wxFocusEvent
& event
)
569 wxLogTrace(TRACE_FOCUS
, _T("OnFocus on wxPanel 0x%p, name: %s"),
570 m_winParent
->GetHandle(),
571 m_winParent
->GetName().c_str() );
578 bool wxControlContainer::SetFocusToChild()
580 return wxSetFocusToChild(m_winParent
, &m_winLastFocused
);
583 // ----------------------------------------------------------------------------
584 // SetFocusToChild(): this function is used by wxPanel but also by wxFrame in
585 // wxMSW, this is why it is outside of wxControlContainer class
586 // ----------------------------------------------------------------------------
588 bool wxSetFocusToChild(wxWindow
*win
, wxWindow
**childLastFocused
)
590 wxCHECK_MSG( win
, false, _T("wxSetFocusToChild(): invalid window") );
591 wxCHECK_MSG( childLastFocused
, false,
592 _T("wxSetFocusToChild(): NULL child poonter") );
594 if ( *childLastFocused
)
596 // It might happen that the window got reparented
597 if ( (*childLastFocused
)->GetParent() == win
)
599 wxLogTrace(TRACE_FOCUS
,
600 _T("SetFocusToChild() => last child (0x%p)."),
601 (*childLastFocused
)->GetHandle());
603 // not SetFocusFromKbd(): we're restoring focus back to the old
604 // window and not setting it as the result of a kbd action
605 (*childLastFocused
)->SetFocus();
610 // it doesn't count as such any more
611 *childLastFocused
= (wxWindow
*)NULL
;
615 // set the focus to the first child who wants it
616 wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst();
619 wxWindow
*child
= node
->GetData();
621 if ( child
->AcceptsFocusFromKeyboard() && !child
->IsTopLevel() )
624 // If a radiobutton is the first focusable child, search for the
625 // selected radiobutton in the same group
626 wxRadioButton
* btn
= wxDynamicCast(child
, wxRadioButton
);
629 wxRadioButton
* selected
= wxGetSelectedButtonInGroup(btn
);
635 wxLogTrace(TRACE_FOCUS
,
636 _T("SetFocusToChild() => first child (0x%p)."),
639 *childLastFocused
= child
;
640 child
->SetFocusFromKbd();
644 node
= node
->GetNext();