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 // ============================================================================
45 // ============================================================================
47 wxControlContainer::wxControlContainer(wxWindow
*winParent
)
49 m_winParent
= winParent
;
57 bool wxControlContainer::AcceptsFocus() const
59 // if we're not shown or disabled, we can't accept focus
60 if ( m_winParent
->IsShown() && m_winParent
->IsEnabled() )
62 // otherwise we can accept focus either if we have no children at all
63 // (in this case we're probably not used as a container) or only when
64 // at least one child will accept focus
65 wxWindowList::compatibility_iterator node
= m_winParent
->GetChildren().GetFirst();
70 // wxMac has eventually the two scrollbars as children, they don't count
71 // as real children in the algorithm mentioned above
72 bool hasRealChildren
= false ;
77 wxWindow
*child
= node
->GetData();
79 if ( child
->AcceptsFocus() )
85 wxScrollBar
*sb
= wxDynamicCast( child
, wxScrollBar
) ;
86 if ( sb
== NULL
|| !m_winParent
->MacIsWindowScrollbar( sb
) )
87 hasRealChildren
= true ;
89 node
= node
->GetNext();
93 if ( !hasRealChildren
)
101 void wxControlContainer::SetLastFocus(wxWindow
*win
)
103 // the panel itself should never get the focus at all but if it does happen
104 // temporarily (as it seems to do under wxGTK), at the very least don't
105 // forget our previous m_winLastFocused
106 if ( win
!= m_winParent
)
108 // if we're setting the focus
111 // find the last _immediate_ child which got focus
112 wxWindow
*winParent
= win
;
113 while ( winParent
!= m_winParent
)
116 winParent
= win
->GetParent();
118 // Yes, this can happen, though in a totally pathological case.
119 // like when detaching a menubar from a frame with a child
120 // which has pushed itself as an event handler for the menubar.
123 wxASSERT_MSG( winParent
,
124 _T("Setting last focus for a window that is not our child?") );
128 m_winLastFocused
= win
;
132 wxLogTrace(_T("focus"), _T("Set last focus to %s(%s)"),
133 win
->GetClassInfo()->GetClassName(),
134 win
->GetLabel().c_str());
138 wxLogTrace(_T("focus"), _T("No more last focus"));
142 // propagate the last focus upwards so that our parent can set focus back
143 // to us if it loses it now and regains later
144 wxWindow
*parent
= m_winParent
->GetParent();
147 wxChildFocusEvent
eventFocus(m_winParent
);
148 parent
->GetEventHandler()->ProcessEvent(eventFocus
);
152 // --------------------------------------------------------------------
153 // The following four functions are used to find other radio buttons
154 // within the same group. Used by wxSetFocusToChild on wxMSW
155 // --------------------------------------------------------------------
159 wxRadioButton
* wxGetPreviousButtonInGroup(wxRadioButton
*btn
)
161 if ( btn
->HasFlag(wxRB_GROUP
) || btn
->HasFlag(wxRB_SINGLE
) )
164 const wxWindowList
& siblings
= btn
->GetParent()->GetChildren();
165 wxWindowList::compatibility_iterator nodeThis
= siblings
.Find(btn
);
166 wxCHECK_MSG( nodeThis
, NULL
, _T("radio button not a child of its parent?") );
168 // Iterate over all previous siblings until we find the next radio button
169 wxWindowList::compatibility_iterator nodeBefore
= nodeThis
->GetPrevious();
170 wxRadioButton
*prevBtn
= 0;
173 prevBtn
= wxDynamicCast(nodeBefore
->GetData(), wxRadioButton
);
177 nodeBefore
= nodeBefore
->GetPrevious();
180 if (!prevBtn
|| prevBtn
->HasFlag(wxRB_SINGLE
))
182 // no more buttons in group
189 wxRadioButton
* wxGetNextButtonInGroup(wxRadioButton
*btn
)
191 if (btn
->HasFlag(wxRB_SINGLE
))
194 const wxWindowList
& siblings
= btn
->GetParent()->GetChildren();
195 wxWindowList::compatibility_iterator nodeThis
= siblings
.Find(btn
);
196 wxCHECK_MSG( nodeThis
, NULL
, _T("radio button not a child of its parent?") );
198 // Iterate over all previous siblings until we find the next radio button
199 wxWindowList::compatibility_iterator nodeNext
= nodeThis
->GetNext();
200 wxRadioButton
*nextBtn
= 0;
203 nextBtn
= wxDynamicCast(nodeNext
->GetData(), wxRadioButton
);
207 nodeNext
= nodeNext
->GetNext();
210 if ( !nextBtn
|| nextBtn
->HasFlag(wxRB_GROUP
) || nextBtn
->HasFlag(wxRB_SINGLE
) )
212 // no more buttons or the first button of the next group
219 wxRadioButton
* wxGetFirstButtonInGroup(wxRadioButton
*btn
)
223 wxRadioButton
* prevBtn
= wxGetPreviousButtonInGroup(btn
);
231 wxRadioButton
* wxGetSelectedButtonInGroup(wxRadioButton
*btn
)
233 // Find currently selected button
237 if (btn
->HasFlag(wxRB_SINGLE
))
240 wxRadioButton
*selBtn
;
242 // First check all previous buttons
243 for (selBtn
= wxGetPreviousButtonInGroup(btn
); selBtn
; selBtn
= wxGetPreviousButtonInGroup(selBtn
))
244 if (selBtn
->GetValue())
247 // Now all following buttons
248 for (selBtn
= wxGetNextButtonInGroup(btn
); selBtn
; selBtn
= wxGetNextButtonInGroup(selBtn
))
249 if (selBtn
->GetValue())
257 // ----------------------------------------------------------------------------
258 // Keyboard handling - this is the place where the TAB traversal logic is
259 // implemented. As this code is common to all ports, this ensures consistent
260 // behaviour even if we don't specify how exactly the wxNavigationKeyEvent are
261 // generated and this is done in platform specific code which also ensures that
262 // we can follow the given platform standards.
263 // ----------------------------------------------------------------------------
265 void wxControlContainer::HandleOnNavigationKey( wxNavigationKeyEvent
& event
)
267 wxWindow
*parent
= m_winParent
->GetParent();
269 // the event is propagated downwards if the event emitter was our parent
270 bool goingDown
= event
.GetEventObject() == parent
;
272 const wxWindowList
& children
= m_winParent
->GetChildren();
274 // there is not much to do if we don't have children and we're not
275 // interested in "notebook page change" events here
276 if ( !children
.GetCount() || event
.IsWindowChange() )
278 // let the parent process it unless it already comes from our parent
279 // of we don't have any
281 !parent
|| !parent
->GetEventHandler()->ProcessEvent(event
) )
289 // where are we going?
290 bool forward
= event
.GetDirection();
292 // the node of the children list from which we should start looking for the
293 // next acceptable child
294 wxWindowList::compatibility_iterator node
, start_node
;
296 // we should start from the first/last control and not from the one which
297 // had focus the last time if we're propagating the event downwards because
298 // for our parent we look like a single control
301 // just to be sure it's not used (normally this is not necessary, but
302 // doesn't hurt neither)
303 m_winLastFocused
= (wxWindow
*)NULL
;
305 // start from first or last depending on where we're going
306 node
= forward
? children
.GetFirst() : children
.GetLast();
308 // we want to cycle over all nodes
309 start_node
= wxWindowList::compatibility_iterator();
313 // try to find the child which has the focus currently
315 // the event emitter might have done this for us
316 wxWindow
*winFocus
= event
.GetCurrentFocus();
318 // but if not, we might know where the focus was ourselves
320 winFocus
= m_winLastFocused
;
322 // if still no luck, do it the hard way
324 winFocus
= wxWindow::FindFocus();
329 // If we are in a radio button group, start from the first item in the
331 if ( event
.IsFromTab() && wxIsKindOf(winFocus
, wxRadioButton
) )
332 winFocus
= wxGetFirstButtonInGroup((wxRadioButton
*)winFocus
);
334 // ok, we found the focus - now is it our child?
335 start_node
= children
.Find( winFocus
);
339 start_node
= wxWindowList::compatibility_iterator();
342 if ( !start_node
&& m_winLastFocused
)
344 // window which has focus isn't our child, fall back to the one
345 // which had the focus the last time
346 start_node
= children
.Find( m_winLastFocused
);
349 // if we still didn't find anything, we should start with the first one
352 start_node
= children
.GetFirst();
355 // and the first child which we can try setting focus to is the next or
357 node
= forward
? start_node
->GetNext() : start_node
->GetPrevious();
360 // we want to cycle over all elements passing by NULL
361 while ( node
!= start_node
)
363 // Have we come to the last or first item on the panel?
368 // Check if our (may be grand) parent is another panel: if this
369 // is the case, they will know what to do with this navigation
370 // key and so give them the chance to process it instead of
371 // looping inside this panel (normally, the focus will go to
372 // the next/previous item after this panel in the parent
374 wxWindow
*focussed_child_of_parent
= m_winParent
;
377 // we don't want to tab into a different dialog or frame
378 if ( focussed_child_of_parent
->IsTopLevel() )
381 event
.SetCurrentFocus( focussed_child_of_parent
);
382 if ( parent
->GetEventHandler()->ProcessEvent( event
) )
385 focussed_child_of_parent
= parent
;
387 parent
= parent
->GetParent();
390 //else: as the focus came from our parent, we definitely don't want
391 // to send it back to it!
393 // no, we are not inside another panel so process this ourself
394 node
= forward
? children
.GetFirst() : children
.GetLast();
399 wxWindow
*child
= node
->GetData();
402 bool canSelectRadioButton
= true;
403 if (!event
.IsFromTab())
405 // If navigating using cursor keys, make sure not to navigate out of a radio button group.
406 if (m_winLastFocused
&& wxIsKindOf(m_winLastFocused
, wxRadioButton
))
408 if (!wxIsKindOf(child
, wxRadioButton
))
411 wxGetNextButtonInGroup((wxRadioButton
*)m_winLastFocused
) :
412 wxGetPreviousButtonInGroup((wxRadioButton
*)m_winLastFocused
);
423 // If navigating using tabs, skip all but the first radio button in a group.
424 if (wxIsKindOf(child
, wxRadioButton
))
426 if (wxGetPreviousButtonInGroup((wxRadioButton
*)child
))
427 canSelectRadioButton
= false;
431 static bool canSelectRadioButton
= true;
434 if ( child
->AcceptsFocusFromKeyboard() && canSelectRadioButton
)
436 // if we're setting the focus to a child panel we should prevent it
437 // from giving it to the child which had the focus the last time
438 // and instead give it to the first/last child depending from which
439 // direction we're coming
440 event
.SetEventObject(m_winParent
);
442 #if defined(__WXMSW__)
443 // we need to hop to the next activated
444 // radio button, not just the next radio
446 if (wxIsKindOf(child
, wxRadioButton
) && event
.IsFromTab())
448 wxRadioButton
*rb
= wxGetSelectedButtonInGroup((wxRadioButton
*)child
);
454 // disable propagation for this call as otherwise the event might
455 // bounce back to us.
456 wxPropagationDisabler
disableProp(event
);
457 if ( !child
->GetEventHandler()->ProcessEvent(event
) )
459 // set it first in case SetFocusFromKbd() results in focus
461 m_winLastFocused
= child
;
463 // everything is simple: just give focus to it
464 child
->SetFocusFromKbd();
466 //else: the child manages its focus itself
473 node
= forward
? node
->GetNext() : node
->GetPrevious();
476 // we cycled through all of our children and none of them wanted to accept
481 void wxControlContainer::HandleOnWindowDestroy(wxWindowBase
*child
)
483 if ( child
== m_winLastFocused
)
484 m_winLastFocused
= NULL
;
486 if ( child
== m_winDefault
)
489 if ( child
== m_winTmpDefault
)
490 m_winTmpDefault
= NULL
;
493 // ----------------------------------------------------------------------------
495 // ----------------------------------------------------------------------------
497 bool wxControlContainer::DoSetFocus()
499 wxLogTrace(_T("focus"), _T("SetFocus on wxPanel 0x%08lx."),
500 (unsigned long)m_winParent
->GetHandle());
505 // when the panel gets the focus we move the focus to either the last
506 // window that had the focus or the first one that can get it unless the
507 // focus had been already set to some other child
509 wxWindow
*win
= wxWindow::FindFocus();
512 if ( win
== m_winParent
)
514 // our child already has focus, don't take it away from it
518 if ( win
->IsTopLevel() )
520 // don't look beyond the first top level parent - useless and
525 win
= win
->GetParent();
528 // protect against infinite recursion:
531 bool ret
= SetFocusToChild();
533 m_inSetFocus
= false;
538 void wxControlContainer::HandleOnFocus(wxFocusEvent
& event
)
540 wxLogTrace(_T("focus"), _T("OnFocus on wxPanel 0x%08lx, name: %s"),
541 (unsigned long)m_winParent
->GetHandle(),
542 m_winParent
->GetName().c_str() );
549 bool wxControlContainer::SetFocusToChild()
551 return wxSetFocusToChild(m_winParent
, &m_winLastFocused
);
554 // ----------------------------------------------------------------------------
555 // SetFocusToChild(): this function is used by wxPanel but also by wxFrame in
556 // wxMSW, this is why it is outside of wxControlContainer class
557 // ----------------------------------------------------------------------------
559 bool wxSetFocusToChild(wxWindow
*win
, wxWindow
**childLastFocused
)
561 wxCHECK_MSG( win
, false, _T("wxSetFocusToChild(): invalid window") );
562 wxCHECK_MSG( childLastFocused
, false,
563 _T("wxSetFocusToChild(): NULL child poonter") );
565 if ( *childLastFocused
)
567 // It might happen that the window got reparented
568 if ( (*childLastFocused
)->GetParent() == win
)
570 wxLogTrace(_T("focus"),
571 _T("SetFocusToChild() => last child (0x%08lx)."),
572 (unsigned long)(*childLastFocused
)->GetHandle());
574 // not SetFocusFromKbd(): we're restoring focus back to the old
575 // window and not setting it as the result of a kbd action
576 (*childLastFocused
)->SetFocus();
581 // it doesn't count as such any more
582 *childLastFocused
= (wxWindow
*)NULL
;
586 // set the focus to the first child who wants it
587 wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst();
590 wxWindow
*child
= node
->GetData();
592 if ( child
->AcceptsFocusFromKeyboard() && !child
->IsTopLevel() )
595 // If a radiobutton is the first focusable child, search for the
596 // selected radiobutton in the same group
597 wxRadioButton
* btn
= wxDynamicCast(child
, wxRadioButton
);
600 wxRadioButton
* selected
= wxGetSelectedButtonInGroup(btn
);
606 wxLogTrace(_T("focus"),
607 _T("SetFocusToChild() => first child (0x%08lx)."),
608 (unsigned long)child
->GetHandle());
610 *childLastFocused
= child
;
611 child
->SetFocusFromKbd();
615 node
= node
->GetNext();