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
;
46 m_winLastFocused
= NULL
;
50 bool wxControlContainer::AcceptsFocus() const
52 // if we're not shown or disabled, we can't accept focus
53 if ( m_winParent
->IsShown() && m_winParent
->IsEnabled() )
55 // otherwise we can accept focus either if we have no children at all
56 // (in this case we're probably not used as a container) or only when
57 // at least one child will accept focus
58 wxWindowList::compatibility_iterator node
= m_winParent
->GetChildren().GetFirst();
63 // wxMac has eventually the two scrollbars as children, they don't count
64 // as real children in the algorithm mentioned above
65 bool hasRealChildren
= false ;
70 wxWindow
*child
= node
->GetData();
71 node
= node
->GetNext();
74 wxScrollBar
*sb
= wxDynamicCast( child
, wxScrollBar
) ;
75 if ( sb
== NULL
|| !m_winParent
->MacIsWindowScrollbar( sb
) )
76 hasRealChildren
= true ;
77 if ( sb
&& m_winParent
->MacIsWindowScrollbar( sb
) )
80 if ( child
->AcceptsFocus() )
87 if ( !hasRealChildren
)
95 void wxControlContainer::SetLastFocus(wxWindow
*win
)
97 // the panel itself should never get the focus at all but if it does happen
98 // temporarily (as it seems to do under wxGTK), at the very least don't
99 // forget our previous m_winLastFocused
100 if ( win
!= m_winParent
)
102 // if we're setting the focus
105 // find the last _immediate_ child which got focus
106 wxWindow
*winParent
= win
;
107 while ( winParent
!= m_winParent
)
110 winParent
= win
->GetParent();
112 // Yes, this can happen, though in a totally pathological case.
113 // like when detaching a menubar from a frame with a child
114 // which has pushed itself as an event handler for the menubar.
117 wxASSERT_MSG( winParent
,
118 _T("Setting last focus for a window that is not our child?") );
122 m_winLastFocused
= win
;
126 wxLogTrace(TRACE_FOCUS
, _T("Set last focus to %s(%s)"),
127 win
->GetClassInfo()->GetClassName(),
128 win
->GetLabel().c_str());
132 wxLogTrace(TRACE_FOCUS
, _T("No more last focus"));
136 // propagate the last focus upwards so that our parent can set focus back
137 // to us if it loses it now and regains later
138 wxWindow
*parent
= m_winParent
->GetParent();
141 wxChildFocusEvent
eventFocus(m_winParent
);
142 parent
->GetEventHandler()->ProcessEvent(eventFocus
);
146 // --------------------------------------------------------------------
147 // The following four functions are used to find other radio buttons
148 // within the same group. Used by wxSetFocusToChild on wxMSW
149 // --------------------------------------------------------------------
153 wxRadioButton
* wxGetPreviousButtonInGroup(wxRadioButton
*btn
)
155 if ( btn
->HasFlag(wxRB_GROUP
) || btn
->HasFlag(wxRB_SINGLE
) )
158 const wxWindowList
& siblings
= btn
->GetParent()->GetChildren();
159 wxWindowList::compatibility_iterator nodeThis
= siblings
.Find(btn
);
160 wxCHECK_MSG( nodeThis
, NULL
, _T("radio button not a child of its parent?") );
162 // Iterate over all previous siblings until we find the next radio button
163 wxWindowList::compatibility_iterator nodeBefore
= nodeThis
->GetPrevious();
164 wxRadioButton
*prevBtn
= 0;
167 prevBtn
= wxDynamicCast(nodeBefore
->GetData(), wxRadioButton
);
171 nodeBefore
= nodeBefore
->GetPrevious();
174 if (!prevBtn
|| prevBtn
->HasFlag(wxRB_SINGLE
))
176 // no more buttons in group
183 wxRadioButton
* wxGetNextButtonInGroup(wxRadioButton
*btn
)
185 if (btn
->HasFlag(wxRB_SINGLE
))
188 const wxWindowList
& siblings
= btn
->GetParent()->GetChildren();
189 wxWindowList::compatibility_iterator nodeThis
= siblings
.Find(btn
);
190 wxCHECK_MSG( nodeThis
, NULL
, _T("radio button not a child of its parent?") );
192 // Iterate over all previous siblings until we find the next radio button
193 wxWindowList::compatibility_iterator nodeNext
= nodeThis
->GetNext();
194 wxRadioButton
*nextBtn
= 0;
197 nextBtn
= wxDynamicCast(nodeNext
->GetData(), wxRadioButton
);
201 nodeNext
= nodeNext
->GetNext();
204 if ( !nextBtn
|| nextBtn
->HasFlag(wxRB_GROUP
) || nextBtn
->HasFlag(wxRB_SINGLE
) )
206 // no more buttons or the first button of the next group
213 wxRadioButton
* wxGetFirstButtonInGroup(wxRadioButton
*btn
)
217 wxRadioButton
* prevBtn
= wxGetPreviousButtonInGroup(btn
);
225 wxRadioButton
* wxGetLastButtonInGroup(wxRadioButton
*btn
)
229 wxRadioButton
* nextBtn
= wxGetNextButtonInGroup(btn
);
237 wxRadioButton
* wxGetSelectedButtonInGroup(wxRadioButton
*btn
)
239 // Find currently selected button
243 if (btn
->HasFlag(wxRB_SINGLE
))
246 wxRadioButton
*selBtn
;
248 // First check all previous buttons
249 for (selBtn
= wxGetPreviousButtonInGroup(btn
); selBtn
; selBtn
= wxGetPreviousButtonInGroup(selBtn
))
250 if (selBtn
->GetValue())
253 // Now all following buttons
254 for (selBtn
= wxGetNextButtonInGroup(btn
); selBtn
; selBtn
= wxGetNextButtonInGroup(selBtn
))
255 if (selBtn
->GetValue())
263 // ----------------------------------------------------------------------------
264 // Keyboard handling - this is the place where the TAB traversal logic is
265 // implemented. As this code is common to all ports, this ensures consistent
266 // behaviour even if we don't specify how exactly the wxNavigationKeyEvent are
267 // generated and this is done in platform specific code which also ensures that
268 // we can follow the given platform standards.
269 // ----------------------------------------------------------------------------
271 void wxControlContainer::HandleOnNavigationKey( wxNavigationKeyEvent
& event
)
273 wxWindow
*parent
= m_winParent
->GetParent();
275 // the event is propagated downwards if the event emitter was our parent
276 bool goingDown
= event
.GetEventObject() == parent
;
278 const wxWindowList
& children
= m_winParent
->GetChildren();
280 // if we have exactly one notebook-like child window (actually it could be
281 // any window that returns true from its HasMultiplePages()), then
282 // [Shift-]Ctrl-Tab and Ctrl-PageUp/Down keys should iterate over its pages
283 // even if the focus is outside of the control because this is how the
284 // standard MSW properties dialogs behave and we do it under other platforms
285 // as well because it seems like a good idea -- but we can always put this
286 // block inside "#ifdef __WXMSW__" if it's not suitable there
287 if ( event
.IsWindowChange() && !goingDown
)
289 // check if we have a unique notebook-like child
290 wxWindow
*bookctrl
= NULL
;
291 for ( wxWindowList::const_iterator i
= children
.begin(),
292 end
= children
.end();
296 wxWindow
* const window
= *i
;
297 if ( window
->HasMultiplePages() )
301 // this is the second book-like control already so don't do
302 // anything as we don't know which one should have its page
312 if ( bookctrl
&& bookctrl
->GetEventHandler()->ProcessEvent(event
) )
316 // there is not much to do if we don't have children and we're not
317 // interested in "notebook page change" events here
318 if ( !children
.GetCount() || event
.IsWindowChange() )
320 // let the parent process it unless it already comes from our parent
321 // of we don't have any
323 !parent
|| !parent
->GetEventHandler()->ProcessEvent(event
) )
331 // where are we going?
332 const bool forward
= event
.GetDirection();
334 // the node of the children list from which we should start looking for the
335 // next acceptable child
336 wxWindowList::compatibility_iterator node
, start_node
;
338 // we should start from the first/last control and not from the one which
339 // had focus the last time if we're propagating the event downwards because
340 // for our parent we look like a single control
343 // just to be sure it's not used (normally this is not necessary, but
344 // doesn't hurt neither)
345 m_winLastFocused
= (wxWindow
*)NULL
;
347 // start from first or last depending on where we're going
348 node
= forward
? children
.GetFirst() : children
.GetLast();
352 // try to find the child which has the focus currently
354 // the event emitter might have done this for us
355 wxWindow
*winFocus
= event
.GetCurrentFocus();
357 // but if not, we might know where the focus was ourselves
359 winFocus
= m_winLastFocused
;
361 // if still no luck, do it the hard way
363 winFocus
= wxWindow::FindFocus();
368 // If we are in a radio button group, start from the first item in the
370 if ( event
.IsFromTab() && wxIsKindOf(winFocus
, wxRadioButton
) )
371 winFocus
= wxGetFirstButtonInGroup((wxRadioButton
*)winFocus
);
373 // ok, we found the focus - now is it our child?
374 start_node
= children
.Find( winFocus
);
377 if ( !start_node
&& m_winLastFocused
)
379 // window which has focus isn't our child, fall back to the one
380 // which had the focus the last time
381 start_node
= children
.Find( m_winLastFocused
);
384 // if we still didn't find anything, we should start with the first one
387 start_node
= children
.GetFirst();
390 // and the first child which we can try setting focus to is the next or
392 node
= forward
? start_node
->GetNext() : start_node
->GetPrevious();
395 // we want to cycle over all elements passing by NULL
398 // don't go into infinite loop
399 if ( start_node
&& node
&& node
== start_node
)
402 // Have we come to the last or first item on the panel?
407 // exit now as otherwise we'd loop forever
413 // Check if our (maybe grand) parent is another panel: if this
414 // is the case, they will know what to do with this navigation
415 // key and so give them the chance to process it instead of
416 // looping inside this panel (normally, the focus will go to
417 // the next/previous item after this panel in the parent
419 wxWindow
*focussed_child_of_parent
= m_winParent
;
422 // we don't want to tab into a different dialog or frame
423 if ( focussed_child_of_parent
->IsTopLevel() )
426 event
.SetCurrentFocus( focussed_child_of_parent
);
427 if ( parent
->GetEventHandler()->ProcessEvent( event
) )
430 focussed_child_of_parent
= parent
;
432 parent
= parent
->GetParent();
435 //else: as the focus came from our parent, we definitely don't want
436 // to send it back to it!
438 // no, we are not inside another panel so process this ourself
439 node
= forward
? children
.GetFirst() : children
.GetLast();
444 wxWindow
*child
= node
->GetData();
447 if ( event
.IsFromTab() )
449 if ( wxIsKindOf(child
, wxRadioButton
) )
451 // only radio buttons with either wxRB_GROUP or wxRB_SINGLE
453 if ( child
->HasFlag(wxRB_GROUP
) )
455 // need to tab into the active button within a group
456 wxRadioButton
*rb
= wxGetSelectedButtonInGroup((wxRadioButton
*)child
);
460 else if ( !child
->HasFlag(wxRB_SINGLE
) )
462 node
= forward
? node
->GetNext() : node
->GetPrevious();
467 else if ( m_winLastFocused
&&
468 wxIsKindOf(m_winLastFocused
, wxRadioButton
) &&
469 !m_winLastFocused
->HasFlag(wxRB_SINGLE
) )
471 // cursor keys don't navigate out of a radio button group so
472 // find the correct radio button to focus
475 child
= wxGetNextButtonInGroup((wxRadioButton
*)m_winLastFocused
);
478 // no next button in group, set it to the first button
479 child
= wxGetFirstButtonInGroup((wxRadioButton
*)m_winLastFocused
);
484 child
= wxGetPreviousButtonInGroup((wxRadioButton
*)m_winLastFocused
);
487 // no previous button in group, set it to the last button
488 child
= wxGetLastButtonInGroup((wxRadioButton
*)m_winLastFocused
);
492 if ( child
== m_winLastFocused
)
494 // must be a group consisting of only one button therefore
495 // no need to send a navigation event
502 if ( child
->AcceptsFocusFromKeyboard() )
504 // if we're setting the focus to a child panel we should prevent it
505 // from giving it to the child which had the focus the last time
506 // and instead give it to the first/last child depending from which
507 // direction we're coming
508 event
.SetEventObject(m_winParent
);
510 // disable propagation for this call as otherwise the event might
511 // bounce back to us.
512 wxPropagationDisabler
disableProp(event
);
513 if ( !child
->GetEventHandler()->ProcessEvent(event
) )
515 // set it first in case SetFocusFromKbd() results in focus
517 m_winLastFocused
= child
;
519 // everything is simple: just give focus to it
520 child
->SetFocusFromKbd();
522 //else: the child manages its focus itself
529 node
= forward
? node
->GetNext() : node
->GetPrevious();
532 // we cycled through all of our children and none of them wanted to accept
537 void wxControlContainer::HandleOnWindowDestroy(wxWindowBase
*child
)
539 if ( child
== m_winLastFocused
)
540 m_winLastFocused
= NULL
;
543 // ----------------------------------------------------------------------------
545 // ----------------------------------------------------------------------------
547 bool wxControlContainer::DoSetFocus()
549 wxLogTrace(TRACE_FOCUS
, _T("SetFocus on wxPanel 0x%p."),
550 m_winParent
->GetHandle());
555 // when the panel gets the focus we move the focus to either the last
556 // window that had the focus or the first one that can get it unless the
557 // focus had been already set to some other child
559 wxWindow
*win
= wxWindow::FindFocus();
562 if ( win
== m_winParent
)
564 // our child already has focus, don't take it away from it
568 if ( win
->IsTopLevel() )
570 // don't look beyond the first top level parent - useless and
575 win
= win
->GetParent();
578 // protect against infinite recursion:
581 bool ret
= SetFocusToChild();
583 m_inSetFocus
= false;
588 void wxControlContainer::HandleOnFocus(wxFocusEvent
& event
)
590 wxLogTrace(TRACE_FOCUS
, _T("OnFocus on wxPanel 0x%p, name: %s"),
591 m_winParent
->GetHandle(),
592 m_winParent
->GetName().c_str() );
599 bool wxControlContainer::SetFocusToChild()
601 return wxSetFocusToChild(m_winParent
, &m_winLastFocused
);
604 // ----------------------------------------------------------------------------
605 // SetFocusToChild(): this function is used by wxPanel but also by wxFrame in
606 // wxMSW, this is why it is outside of wxControlContainer class
607 // ----------------------------------------------------------------------------
609 bool wxSetFocusToChild(wxWindow
*win
, wxWindow
**childLastFocused
)
611 wxCHECK_MSG( win
, false, _T("wxSetFocusToChild(): invalid window") );
612 wxCHECK_MSG( childLastFocused
, false,
613 _T("wxSetFocusToChild(): NULL child poonter") );
615 if ( *childLastFocused
)
617 // It might happen that the window got reparented
618 if ( (*childLastFocused
)->GetParent() == win
)
620 wxLogTrace(TRACE_FOCUS
,
621 _T("SetFocusToChild() => last child (0x%p)."),
622 (*childLastFocused
)->GetHandle());
624 // not SetFocusFromKbd(): we're restoring focus back to the old
625 // window and not setting it as the result of a kbd action
626 (*childLastFocused
)->SetFocus();
631 // it doesn't count as such any more
632 *childLastFocused
= (wxWindow
*)NULL
;
636 // set the focus to the first child who wants it
637 wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst();
640 wxWindow
*child
= node
->GetData();
641 node
= node
->GetNext();
644 wxScrollBar
*sb
= wxDynamicCast( child
, wxScrollBar
) ;
645 if ( sb
&& child
->GetParent()->MacIsWindowScrollbar( sb
) )
649 if ( child
->AcceptsFocusFromKeyboard() && !child
->IsTopLevel() )
652 // If a radiobutton is the first focusable child, search for the
653 // selected radiobutton in the same group
654 wxRadioButton
* btn
= wxDynamicCast(child
, wxRadioButton
);
657 wxRadioButton
* selected
= wxGetSelectedButtonInGroup(btn
);
663 wxLogTrace(TRACE_FOCUS
,
664 _T("SetFocusToChild() => first child (0x%p)."),
667 *childLastFocused
= child
;
668 child
->SetFocusFromKbd();