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"
28 #include "wx/containr.h"
34 #include "wx/window.h"
35 #include "wx/scrolbar.h"
36 #include "wx/radiobut.h"
39 // trace mask for focus messages
40 #define TRACE_FOCUS _T("focus")
42 // ============================================================================
44 // ============================================================================
46 // ----------------------------------------------------------------------------
47 // wxControlContainerBase
48 // ----------------------------------------------------------------------------
50 void wxControlContainerBase::SetCanFocus(bool acceptsFocus
)
52 if ( acceptsFocus
== m_acceptsFocus
)
55 m_acceptsFocus
= acceptsFocus
;
57 m_winParent
->SetCanFocus(m_acceptsFocus
);
60 bool wxControlContainerBase::HasAnyFocusableChildren() const
62 const wxWindowList
& children
= m_winParent
->GetChildren();
63 for ( wxWindowList::const_iterator i
= children
.begin(),
68 const wxWindow
* const child
= *i
;
70 if ( !m_winParent
->IsClientAreaChild(child
) )
73 if ( child
->CanAcceptFocus() )
80 #ifndef wxHAS_NATIVE_TAB_TRAVERSAL
82 // ----------------------------------------------------------------------------
83 // generic wxControlContainer
84 // ----------------------------------------------------------------------------
86 wxControlContainer::wxControlContainer()
88 m_winLastFocused
= NULL
;
92 void wxControlContainer::SetLastFocus(wxWindow
*win
)
94 // the panel itself should never get the focus at all but if it does happen
95 // temporarily (as it seems to do under wxGTK), at the very least don't
96 // forget our previous m_winLastFocused
97 if ( win
!= m_winParent
)
99 // if we're setting the focus
102 // find the last _immediate_ child which got focus
103 wxWindow
*winParent
= win
;
104 while ( winParent
!= m_winParent
)
107 winParent
= win
->GetParent();
109 // Yes, this can happen, though in a totally pathological case.
110 // like when detaching a menubar from a frame with a child
111 // which has pushed itself as an event handler for the menubar.
114 wxASSERT_MSG( winParent
,
115 _T("Setting last focus for a window that is not our child?") );
119 m_winLastFocused
= win
;
123 wxLogTrace(TRACE_FOCUS
, _T("Set last focus to %s(%s)"),
124 win
->GetClassInfo()->GetClassName(),
125 win
->GetLabel().c_str());
129 wxLogTrace(TRACE_FOCUS
, _T("No more last focus"));
133 // propagate the last focus upwards so that our parent can set focus back
134 // to us if it loses it now and regains later; do *not* do this if we are
135 // a toplevel window (e.g. wxDialog) that has another frame as its parent
136 if ( !m_winParent
->IsTopLevel() )
138 wxWindow
*parent
= m_winParent
->GetParent();
141 wxChildFocusEvent
eventFocus(m_winParent
);
142 parent
->GetEventHandler()->ProcessEvent(eventFocus
);
147 // --------------------------------------------------------------------
148 // The following four functions are used to find other radio buttons
149 // within the same group. Used by wxSetFocusToChild on wxMSW
150 // --------------------------------------------------------------------
152 #if defined(__WXMSW__) && wxUSE_RADIOBTN
154 wxRadioButton
* wxGetPreviousButtonInGroup(wxRadioButton
*btn
)
156 if ( btn
->HasFlag(wxRB_GROUP
) || btn
->HasFlag(wxRB_SINGLE
) )
159 const wxWindowList
& siblings
= btn
->GetParent()->GetChildren();
160 wxWindowList::compatibility_iterator nodeThis
= siblings
.Find(btn
);
161 wxCHECK_MSG( nodeThis
, NULL
, _T("radio button not a child of its parent?") );
163 // Iterate over all previous siblings until we find the next radio button
164 wxWindowList::compatibility_iterator nodeBefore
= nodeThis
->GetPrevious();
165 wxRadioButton
*prevBtn
= 0;
168 prevBtn
= wxDynamicCast(nodeBefore
->GetData(), wxRadioButton
);
172 nodeBefore
= nodeBefore
->GetPrevious();
175 if (!prevBtn
|| prevBtn
->HasFlag(wxRB_SINGLE
))
177 // no more buttons in group
184 wxRadioButton
* wxGetNextButtonInGroup(wxRadioButton
*btn
)
186 if (btn
->HasFlag(wxRB_SINGLE
))
189 const wxWindowList
& siblings
= btn
->GetParent()->GetChildren();
190 wxWindowList::compatibility_iterator nodeThis
= siblings
.Find(btn
);
191 wxCHECK_MSG( nodeThis
, NULL
, _T("radio button not a child of its parent?") );
193 // Iterate over all previous siblings until we find the next radio button
194 wxWindowList::compatibility_iterator nodeNext
= nodeThis
->GetNext();
195 wxRadioButton
*nextBtn
= 0;
198 nextBtn
= wxDynamicCast(nodeNext
->GetData(), wxRadioButton
);
202 nodeNext
= nodeNext
->GetNext();
205 if ( !nextBtn
|| nextBtn
->HasFlag(wxRB_GROUP
) || nextBtn
->HasFlag(wxRB_SINGLE
) )
207 // no more buttons or the first button of the next group
214 wxRadioButton
* wxGetFirstButtonInGroup(wxRadioButton
*btn
)
218 wxRadioButton
* prevBtn
= wxGetPreviousButtonInGroup(btn
);
226 wxRadioButton
* wxGetLastButtonInGroup(wxRadioButton
*btn
)
230 wxRadioButton
* nextBtn
= wxGetNextButtonInGroup(btn
);
238 wxRadioButton
* wxGetSelectedButtonInGroup(wxRadioButton
*btn
)
240 // Find currently selected button
244 if (btn
->HasFlag(wxRB_SINGLE
))
247 wxRadioButton
*selBtn
;
249 // First check all previous buttons
250 for (selBtn
= wxGetPreviousButtonInGroup(btn
); selBtn
; selBtn
= wxGetPreviousButtonInGroup(selBtn
))
251 if (selBtn
->GetValue())
254 // Now all following buttons
255 for (selBtn
= wxGetNextButtonInGroup(btn
); selBtn
; selBtn
= wxGetNextButtonInGroup(selBtn
))
256 if (selBtn
->GetValue())
264 // ----------------------------------------------------------------------------
265 // Keyboard handling - this is the place where the TAB traversal logic is
266 // implemented. As this code is common to all ports, this ensures consistent
267 // behaviour even if we don't specify how exactly the wxNavigationKeyEvent are
268 // generated and this is done in platform specific code which also ensures that
269 // we can follow the given platform standards.
270 // ----------------------------------------------------------------------------
272 void wxControlContainer::HandleOnNavigationKey( wxNavigationKeyEvent
& event
)
274 wxWindow
*parent
= m_winParent
->GetParent();
276 // the event is propagated downwards if the event emitter was our parent
277 bool goingDown
= event
.GetEventObject() == parent
;
279 const wxWindowList
& children
= m_winParent
->GetChildren();
281 // if we have exactly one notebook-like child window (actually it could be
282 // any window that returns true from its HasMultiplePages()), then
283 // [Shift-]Ctrl-Tab and Ctrl-PageUp/Down keys should iterate over its pages
284 // even if the focus is outside of the control because this is how the
285 // standard MSW properties dialogs behave and we do it under other platforms
286 // as well because it seems like a good idea -- but we can always put this
287 // block inside "#ifdef __WXMSW__" if it's not suitable there
288 if ( event
.IsWindowChange() && !goingDown
)
290 // check if we have a unique notebook-like child
291 wxWindow
*bookctrl
= NULL
;
292 for ( wxWindowList::const_iterator i
= children
.begin(),
293 end
= children
.end();
297 wxWindow
* const window
= *i
;
298 if ( window
->HasMultiplePages() )
302 // this is the second book-like control already so don't do
303 // anything as we don't know which one should have its page
315 // make sure that we don't bubble up the event again from the book
316 // control resulting in infinite recursion
317 wxNavigationKeyEvent
eventCopy(event
);
318 eventCopy
.SetEventObject(m_winParent
);
319 if ( bookctrl
->GetEventHandler()->ProcessEvent(eventCopy
) )
324 // there is not much to do if we don't have children and we're not
325 // interested in "notebook page change" events here
326 if ( !children
.GetCount() || event
.IsWindowChange() )
328 // let the parent process it unless it already comes from our parent
329 // of we don't have any
331 !parent
|| !parent
->GetEventHandler()->ProcessEvent(event
) )
339 // where are we going?
340 const bool forward
= event
.GetDirection();
342 // the node of the children list from which we should start looking for the
343 // next acceptable child
344 wxWindowList::compatibility_iterator node
, start_node
;
346 // we should start from the first/last control and not from the one which
347 // had focus the last time if we're propagating the event downwards because
348 // for our parent we look like a single control
351 // just to be sure it's not used (normally this is not necessary, but
352 // doesn't hurt neither)
353 m_winLastFocused
= (wxWindow
*)NULL
;
355 // start from first or last depending on where we're going
356 node
= forward
? children
.GetFirst() : children
.GetLast();
360 // try to find the child which has the focus currently
362 // the event emitter might have done this for us
363 wxWindow
*winFocus
= event
.GetCurrentFocus();
365 // but if not, we might know where the focus was ourselves
367 winFocus
= m_winLastFocused
;
369 // if still no luck, do it the hard way
371 winFocus
= wxWindow::FindFocus();
375 #if defined(__WXMSW__) && wxUSE_RADIOBTN
376 // If we are in a radio button group, start from the first item in the
378 if ( event
.IsFromTab() && wxIsKindOf(winFocus
, wxRadioButton
) )
379 winFocus
= wxGetFirstButtonInGroup((wxRadioButton
*)winFocus
);
381 // ok, we found the focus - now is it our child?
382 start_node
= children
.Find( winFocus
);
385 if ( !start_node
&& m_winLastFocused
)
387 // window which has focus isn't our child, fall back to the one
388 // which had the focus the last time
389 start_node
= children
.Find( m_winLastFocused
);
392 // if we still didn't find anything, we should start with the first one
395 start_node
= children
.GetFirst();
398 // and the first child which we can try setting focus to is the next or
400 node
= forward
? start_node
->GetNext() : start_node
->GetPrevious();
403 // we want to cycle over all elements passing by NULL
406 // don't go into infinite loop
407 if ( start_node
&& node
&& node
== start_node
)
410 // Have we come to the last or first item on the panel?
415 // exit now as otherwise we'd loop forever
421 // Check if our (maybe grand) parent is another panel: if this
422 // is the case, they will know what to do with this navigation
423 // key and so give them the chance to process it instead of
424 // looping inside this panel (normally, the focus will go to
425 // the next/previous item after this panel in the parent
427 wxWindow
*focussed_child_of_parent
= m_winParent
;
430 // we don't want to tab into a different dialog or frame
431 if ( focussed_child_of_parent
->IsTopLevel() )
434 event
.SetCurrentFocus( focussed_child_of_parent
);
435 if ( parent
->GetEventHandler()->ProcessEvent( event
) )
438 focussed_child_of_parent
= parent
;
440 parent
= parent
->GetParent();
443 //else: as the focus came from our parent, we definitely don't want
444 // to send it back to it!
446 // no, we are not inside another panel so process this ourself
447 node
= forward
? children
.GetFirst() : children
.GetLast();
452 wxWindow
*child
= node
->GetData();
454 #if defined(__WXMSW__) && wxUSE_RADIOBTN
455 if ( event
.IsFromTab() )
457 if ( wxIsKindOf(child
, wxRadioButton
) )
459 // only radio buttons with either wxRB_GROUP or wxRB_SINGLE
461 if ( child
->HasFlag(wxRB_GROUP
) )
463 // need to tab into the active button within a group
464 wxRadioButton
*rb
= wxGetSelectedButtonInGroup((wxRadioButton
*)child
);
468 else if ( !child
->HasFlag(wxRB_SINGLE
) )
470 node
= forward
? node
->GetNext() : node
->GetPrevious();
475 else if ( m_winLastFocused
&&
476 wxIsKindOf(m_winLastFocused
, wxRadioButton
) &&
477 !m_winLastFocused
->HasFlag(wxRB_SINGLE
) )
479 // cursor keys don't navigate out of a radio button group so
480 // find the correct radio button to focus
483 child
= wxGetNextButtonInGroup((wxRadioButton
*)m_winLastFocused
);
486 // no next button in group, set it to the first button
487 child
= wxGetFirstButtonInGroup((wxRadioButton
*)m_winLastFocused
);
492 child
= wxGetPreviousButtonInGroup((wxRadioButton
*)m_winLastFocused
);
495 // no previous button in group, set it to the last button
496 child
= wxGetLastButtonInGroup((wxRadioButton
*)m_winLastFocused
);
500 if ( child
== m_winLastFocused
)
502 // must be a group consisting of only one button therefore
503 // no need to send a navigation event
510 if ( child
->CanAcceptFocusFromKeyboard() )
512 // if we're setting the focus to a child panel we should prevent it
513 // from giving it to the child which had the focus the last time
514 // and instead give it to the first/last child depending from which
515 // direction we're coming
516 event
.SetEventObject(m_winParent
);
518 // disable propagation for this call as otherwise the event might
519 // bounce back to us.
520 wxPropagationDisabler
disableProp(event
);
521 if ( !child
->GetEventHandler()->ProcessEvent(event
) )
523 // set it first in case SetFocusFromKbd() results in focus
525 m_winLastFocused
= child
;
527 // everything is simple: just give focus to it
528 child
->SetFocusFromKbd();
530 //else: the child manages its focus itself
537 node
= forward
? node
->GetNext() : node
->GetPrevious();
540 // we cycled through all of our children and none of them wanted to accept
545 void wxControlContainer::HandleOnWindowDestroy(wxWindowBase
*child
)
547 if ( child
== m_winLastFocused
)
548 m_winLastFocused
= NULL
;
551 // ----------------------------------------------------------------------------
553 // ----------------------------------------------------------------------------
555 bool wxControlContainer::DoSetFocus()
557 wxLogTrace(TRACE_FOCUS
, _T("SetFocus on wxPanel 0x%p."),
558 m_winParent
->GetHandle());
563 // when the panel gets the focus we move the focus to either the last
564 // window that had the focus or the first one that can get it unless the
565 // focus had been already set to some other child
567 wxWindow
*win
= wxWindow::FindFocus();
570 if ( win
== m_winParent
)
572 // our child already has focus, don't take it away from it
576 if ( win
->IsTopLevel() )
578 // don't look beyond the first top level parent - useless and
583 win
= win
->GetParent();
586 // protect against infinite recursion:
589 bool ret
= SetFocusToChild();
591 m_inSetFocus
= false;
596 void wxControlContainer::HandleOnFocus(wxFocusEvent
& event
)
598 wxLogTrace(TRACE_FOCUS
, _T("OnFocus on wxPanel 0x%p, name: %s"),
599 m_winParent
->GetHandle(),
600 m_winParent
->GetName().c_str() );
607 bool wxControlContainer::SetFocusToChild()
609 return wxSetFocusToChild(m_winParent
, &m_winLastFocused
);
612 // ----------------------------------------------------------------------------
613 // SetFocusToChild(): this function is used by wxPanel but also by wxFrame in
614 // wxMSW, this is why it is outside of wxControlContainer class
615 // ----------------------------------------------------------------------------
617 bool wxSetFocusToChild(wxWindow
*win
, wxWindow
**childLastFocused
)
619 wxCHECK_MSG( win
, false, _T("wxSetFocusToChild(): invalid window") );
620 wxCHECK_MSG( childLastFocused
, false,
621 _T("wxSetFocusToChild(): NULL child poonter") );
623 if ( *childLastFocused
)
625 // It might happen that the window got reparented
626 if ( (*childLastFocused
)->GetParent() == win
)
628 wxLogTrace(TRACE_FOCUS
,
629 _T("SetFocusToChild() => last child (0x%p)."),
630 (*childLastFocused
)->GetHandle());
632 // not SetFocusFromKbd(): we're restoring focus back to the old
633 // window and not setting it as the result of a kbd action
634 (*childLastFocused
)->SetFocus();
639 // it doesn't count as such any more
640 *childLastFocused
= (wxWindow
*)NULL
;
644 // set the focus to the first child who wants it
645 wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst();
648 wxWindow
*child
= node
->GetData();
649 node
= node
->GetNext();
651 // skip special windows:
652 if ( !win
->IsClientAreaChild(child
) )
655 if ( child
->CanAcceptFocusFromKeyboard() && !child
->IsTopLevel() )
657 #if defined(__WXMSW__) && wxUSE_RADIOBTN
658 // If a radiobutton is the first focusable child, search for the
659 // selected radiobutton in the same group
660 wxRadioButton
* btn
= wxDynamicCast(child
, wxRadioButton
);
663 wxRadioButton
* selected
= wxGetSelectedButtonInGroup(btn
);
669 wxLogTrace(TRACE_FOCUS
,
670 _T("SetFocusToChild() => first child (0x%p)."),
673 *childLastFocused
= child
;
674 child
->SetFocusFromKbd();
682 #endif // !wxHAS_NATIVE_TAB_TRAVERSAL