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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "containr.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
34 #include "wx/window.h"
37 #include "wx/containr.h"
40 #include "wx/scrolbar.h"
44 #include "wx/radiobut.h"
47 // ============================================================================
49 // ============================================================================
51 wxControlContainer::wxControlContainer(wxWindow
*winParent
)
53 m_winParent
= winParent
;
61 bool wxControlContainer::AcceptsFocus() const
63 // if we're not shown or disabled, we can't accept focus
64 if ( m_winParent
->IsShown() && m_winParent
->IsEnabled() )
66 // otherwise we can accept focus either if we have no children at all
67 // (in this case we're probably not used as a container) or only when
68 // at least one child will accept focus
69 wxWindowList::compatibility_iterator node
= m_winParent
->GetChildren().GetFirst();
74 // wxMac has eventually the two scrollbars as children, they don't count
75 // as real children in the algorithm mentioned above
76 bool hasRealChildren
= false ;
81 wxWindow
*child
= node
->GetData();
83 if ( child
->AcceptsFocus() )
89 wxScrollBar
*sb
= wxDynamicCast( child
, wxScrollBar
) ;
90 if ( sb
== NULL
|| !m_winParent
->MacIsWindowScrollbar( sb
) )
91 hasRealChildren
= true ;
93 node
= node
->GetNext();
97 if ( !hasRealChildren
)
105 void wxControlContainer::SetLastFocus(wxWindow
*win
)
107 // the panel itself should never get the focus at all but if it does happen
108 // temporarily (as it seems to do under wxGTK), at the very least don't
109 // forget our previous m_winLastFocused
110 if ( win
!= m_winParent
)
112 // if we're setting the focus
115 // find the last _immediate_ child which got focus
116 wxWindow
*winParent
= win
;
117 while ( winParent
!= m_winParent
)
120 winParent
= win
->GetParent();
122 // Yes, this can happen, though in a totally pathological case.
123 // like when detaching a menubar from a frame with a child
124 // which has pushed itself as an event handler for the menubar.
127 wxASSERT_MSG( winParent
,
128 _T("Setting last focus for a window that is not our child?") );
132 m_winLastFocused
= win
;
136 wxLogTrace(_T("focus"), _T("Set last focus to %s(%s)"),
137 win
->GetClassInfo()->GetClassName(),
138 win
->GetLabel().c_str());
142 wxLogTrace(_T("focus"), _T("No more last focus"));
146 // propagate the last focus upwards so that our parent can set focus back
147 // to us if it loses it now and regains later
148 wxWindow
*parent
= m_winParent
->GetParent();
151 wxChildFocusEvent
eventFocus(m_winParent
);
152 parent
->GetEventHandler()->ProcessEvent(eventFocus
);
156 // --------------------------------------------------------------------
157 // The following four functions are used to find other radio buttons
158 // within the same group. Used by wxSetFocusToChild on wxMSW
159 // --------------------------------------------------------------------
163 wxRadioButton
* wxGetPreviousButtonInGroup(wxRadioButton
*btn
)
165 if ( btn
->HasFlag(wxRB_GROUP
) || btn
->HasFlag(wxRB_SINGLE
) )
168 const wxWindowList
& siblings
= btn
->GetParent()->GetChildren();
169 wxWindowList::compatibility_iterator nodeThis
= siblings
.Find(btn
);
170 wxCHECK_MSG( nodeThis
, NULL
, _T("radio button not a child of its parent?") );
172 // Iterate over all previous siblings until we find the next radio button
173 wxWindowList::compatibility_iterator nodeBefore
= nodeThis
->GetPrevious();
174 wxRadioButton
*prevBtn
= 0;
177 prevBtn
= wxDynamicCast(nodeBefore
->GetData(), wxRadioButton
);
181 nodeBefore
= nodeBefore
->GetPrevious();
184 if (!prevBtn
|| prevBtn
->HasFlag(wxRB_SINGLE
))
186 // no more buttons in group
193 wxRadioButton
* wxGetNextButtonInGroup(wxRadioButton
*btn
)
195 if (btn
->HasFlag(wxRB_SINGLE
))
198 const wxWindowList
& siblings
= btn
->GetParent()->GetChildren();
199 wxWindowList::compatibility_iterator nodeThis
= siblings
.Find(btn
);
200 wxCHECK_MSG( nodeThis
, NULL
, _T("radio button not a child of its parent?") );
202 // Iterate over all previous siblings until we find the next radio button
203 wxWindowList::compatibility_iterator nodeNext
= nodeThis
->GetNext();
204 wxRadioButton
*nextBtn
= 0;
207 nextBtn
= wxDynamicCast(nodeNext
->GetData(), wxRadioButton
);
211 nodeNext
= nodeNext
->GetNext();
214 if ( !nextBtn
|| nextBtn
->HasFlag(wxRB_GROUP
) || nextBtn
->HasFlag(wxRB_SINGLE
) )
216 // no more buttons or the first button of the next group
223 wxRadioButton
* wxGetFirstButtonInGroup(wxRadioButton
*btn
)
227 wxRadioButton
* prevBtn
= wxGetPreviousButtonInGroup(btn
);
235 wxRadioButton
* wxGetSelectedButtonInGroup(wxRadioButton
*btn
)
237 // Find currently selected button
241 if (btn
->HasFlag(wxRB_SINGLE
))
244 wxRadioButton
*selBtn
;
246 // First check all previous buttons
247 for (selBtn
= wxGetPreviousButtonInGroup(btn
); selBtn
; selBtn
= wxGetPreviousButtonInGroup(selBtn
))
248 if (selBtn
->GetValue())
251 // Now all following buttons
252 for (selBtn
= wxGetNextButtonInGroup(btn
); selBtn
; selBtn
= wxGetNextButtonInGroup(selBtn
))
253 if (selBtn
->GetValue())
261 // ----------------------------------------------------------------------------
262 // Keyboard handling - this is the place where the TAB traversal logic is
263 // implemented. As this code is common to all ports, this ensures consistent
264 // behaviour even if we don't specify how exactly the wxNavigationKeyEvent are
265 // generated and this is done in platform specific code which also ensures that
266 // we can follow the given platform standards.
267 // ----------------------------------------------------------------------------
269 void wxControlContainer::HandleOnNavigationKey( wxNavigationKeyEvent
& event
)
271 wxWindow
*parent
= m_winParent
->GetParent();
273 // the event is propagated downwards if the event emitter was our parent
274 bool goingDown
= event
.GetEventObject() == parent
;
276 const wxWindowList
& children
= m_winParent
->GetChildren();
278 // there is not much to do if we don't have children and we're not
279 // interested in "notebook page change" events here
280 if ( !children
.GetCount() || event
.IsWindowChange() )
282 // let the parent process it unless it already comes from our parent
283 // of we don't have any
285 !parent
|| !parent
->GetEventHandler()->ProcessEvent(event
) )
293 // where are we going?
294 bool forward
= event
.GetDirection();
296 // the node of the children list from which we should start looking for the
297 // next acceptable child
298 wxWindowList::compatibility_iterator node
, start_node
;
300 // we should start from the first/last control and not from the one which
301 // had focus the last time if we're propagating the event downwards because
302 // for our parent we look like a single control
305 // just to be sure it's not used (normally this is not necessary, but
306 // doesn't hurt neither)
307 m_winLastFocused
= (wxWindow
*)NULL
;
309 // start from first or last depending on where we're going
310 node
= forward
? children
.GetFirst() : children
.GetLast();
312 // we want to cycle over all nodes
313 start_node
= wxWindowList::compatibility_iterator();
317 // try to find the child which has the focus currently
319 // the event emitter might have done this for us
320 wxWindow
*winFocus
= event
.GetCurrentFocus();
322 // but if not, we might know where the focus was ourselves
324 winFocus
= m_winLastFocused
;
326 // if still no luck, do it the hard way
328 winFocus
= wxWindow::FindFocus();
333 // If we are in a radio button group, start from the first item in the
335 if ( event
.IsFromTab() && wxIsKindOf(winFocus
, wxRadioButton
) )
336 winFocus
= wxGetFirstButtonInGroup((wxRadioButton
*)winFocus
);
338 // ok, we found the focus - now is it our child?
339 start_node
= children
.Find( winFocus
);
343 start_node
= wxWindowList::compatibility_iterator();
346 if ( !start_node
&& m_winLastFocused
)
348 // window which has focus isn't our child, fall back to the one
349 // which had the focus the last time
350 start_node
= children
.Find( m_winLastFocused
);
353 // if we still didn't find anything, we should start with the first one
356 start_node
= children
.GetFirst();
359 // and the first child which we can try setting focus to is the next or
361 node
= forward
? start_node
->GetNext() : start_node
->GetPrevious();
364 // we want to cycle over all elements passing by NULL
365 while ( node
!= start_node
)
367 // Have we come to the last or first item on the panel?
372 // Check if our (may be grand) parent is another panel: if this
373 // is the case, they will know what to do with this navigation
374 // key and so give them the chance to process it instead of
375 // looping inside this panel (normally, the focus will go to
376 // the next/previous item after this panel in the parent
378 wxWindow
*focussed_child_of_parent
= m_winParent
;
381 // we don't want to tab into a different dialog or frame
382 if ( focussed_child_of_parent
->IsTopLevel() )
385 event
.SetCurrentFocus( focussed_child_of_parent
);
386 if ( parent
->GetEventHandler()->ProcessEvent( event
) )
389 focussed_child_of_parent
= parent
;
391 parent
= parent
->GetParent();
394 //else: as the focus came from our parent, we definitely don't want
395 // to send it back to it!
397 // no, we are not inside another panel so process this ourself
398 node
= forward
? children
.GetFirst() : children
.GetLast();
403 wxWindow
*child
= node
->GetData();
406 bool canSelectRadioButton
= true;
407 if (!event
.IsFromTab())
409 // If navigating using cursor keys, make sure not to navigate out of a radio button group.
410 if (m_winLastFocused
&& wxIsKindOf(m_winLastFocused
, wxRadioButton
))
412 if (!wxIsKindOf(child
, wxRadioButton
))
415 wxGetNextButtonInGroup((wxRadioButton
*)m_winLastFocused
) :
416 wxGetPreviousButtonInGroup((wxRadioButton
*)m_winLastFocused
);
427 // If navigating using tabs, skip all but the first radio button in a group.
428 if (wxIsKindOf(child
, wxRadioButton
))
430 if (wxGetPreviousButtonInGroup((wxRadioButton
*)child
))
431 canSelectRadioButton
= false;
435 static bool canSelectRadioButton
= true;
438 if ( child
->AcceptsFocusFromKeyboard() && canSelectRadioButton
)
440 // if we're setting the focus to a child panel we should prevent it
441 // from giving it to the child which had the focus the last time
442 // and instead give it to the first/last child depending from which
443 // direction we're coming
444 event
.SetEventObject(m_winParent
);
446 #if defined(__WXMSW__)
447 // we need to hop to the next activated
448 // radio button, not just the next radio
450 if (wxIsKindOf(child
, wxRadioButton
) && event
.IsFromTab())
452 wxRadioButton
*rb
= wxGetSelectedButtonInGroup((wxRadioButton
*)child
);
458 // disable propagation for this call as otherwise the event might
459 // bounce back to us.
460 wxPropagationDisabler
disableProp(event
);
461 if ( !child
->GetEventHandler()->ProcessEvent(event
) )
463 // set it first in case SetFocusFromKbd() results in focus
465 m_winLastFocused
= child
;
467 // everything is simple: just give focus to it
468 child
->SetFocusFromKbd();
470 //else: the child manages its focus itself
477 node
= forward
? node
->GetNext() : node
->GetPrevious();
480 // we cycled through all of our children and none of them wanted to accept
485 void wxControlContainer::HandleOnWindowDestroy(wxWindowBase
*child
)
487 if ( child
== m_winLastFocused
)
488 m_winLastFocused
= NULL
;
490 if ( child
== m_winDefault
)
493 if ( child
== m_winTmpDefault
)
494 m_winTmpDefault
= NULL
;
497 // ----------------------------------------------------------------------------
499 // ----------------------------------------------------------------------------
501 bool wxControlContainer::DoSetFocus()
503 wxLogTrace(_T("focus"), _T("SetFocus on wxPanel 0x%08lx."),
504 (unsigned long)m_winParent
->GetHandle());
509 // when the panel gets the focus we move the focus to either the last
510 // window that had the focus or the first one that can get it unless the
511 // focus had been already set to some other child
513 wxWindow
*win
= wxWindow::FindFocus();
516 if ( win
== m_winParent
)
518 // our child already has focus, don't take it away from it
522 if ( win
->IsTopLevel() )
524 // don't look beyond the first top level parent - useless and
529 win
= win
->GetParent();
532 // protect against infinite recursion:
535 bool ret
= SetFocusToChild();
537 m_inSetFocus
= false;
542 void wxControlContainer::HandleOnFocus(wxFocusEvent
& event
)
544 wxLogTrace(_T("focus"), _T("OnFocus on wxPanel 0x%08lx, name: %s"),
545 (unsigned long)m_winParent
->GetHandle(),
546 m_winParent
->GetName().c_str() );
553 bool wxControlContainer::SetFocusToChild()
555 return wxSetFocusToChild(m_winParent
, &m_winLastFocused
);
558 // ----------------------------------------------------------------------------
559 // SetFocusToChild(): this function is used by wxPanel but also by wxFrame in
560 // wxMSW, this is why it is outside of wxControlContainer class
561 // ----------------------------------------------------------------------------
563 bool wxSetFocusToChild(wxWindow
*win
, wxWindow
**childLastFocused
)
565 wxCHECK_MSG( win
, false, _T("wxSetFocusToChild(): invalid window") );
566 wxCHECK_MSG( childLastFocused
, false,
567 _T("wxSetFocusToChild(): NULL child poonter") );
569 if ( *childLastFocused
)
571 // It might happen that the window got reparented
572 if ( (*childLastFocused
)->GetParent() == win
)
574 wxLogTrace(_T("focus"),
575 _T("SetFocusToChild() => last child (0x%08lx)."),
576 (unsigned long)(*childLastFocused
)->GetHandle());
578 // not SetFocusFromKbd(): we're restoring focus back to the old
579 // window and not setting it as the result of a kbd action
580 (*childLastFocused
)->SetFocus();
585 // it doesn't count as such any more
586 *childLastFocused
= (wxWindow
*)NULL
;
590 // set the focus to the first child who wants it
591 wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst();
594 wxWindow
*child
= node
->GetData();
596 if ( child
->AcceptsFocusFromKeyboard() && !child
->IsTopLevel() )
599 // If a radiobutton is the first focusable child, search for the
600 // selected radiobutton in the same group
601 wxRadioButton
* btn
= wxDynamicCast(child
, wxRadioButton
);
604 wxRadioButton
* selected
= wxGetSelectedButtonInGroup(btn
);
610 wxLogTrace(_T("focus"),
611 _T("SetFocusToChild() => first child (0x%08lx)."),
612 (unsigned long)child
->GetHandle());
614 *childLastFocused
= child
;
615 child
->SetFocusFromKbd();
619 node
= node
->GetNext();