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 // Licence: 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 wxT("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 bool wxControlContainerBase::DoSetFocus()
82 wxLogTrace(TRACE_FOCUS
, wxT("SetFocus on wxPanel 0x%p."),
83 m_winParent
->GetHandle());
88 // when the panel gets the focus we move the focus to either the last
89 // window that had the focus or the first one that can get it unless the
90 // focus had been already set to some other child
92 wxWindow
*win
= wxWindow::FindFocus();
95 if ( win
== m_winParent
)
97 // our child already has focus, don't take it away from it
101 if ( win
->IsTopLevel() )
103 // don't look beyond the first top level parent - useless and
108 win
= win
->GetParent();
111 // protect against infinite recursion:
114 bool ret
= SetFocusToChild();
116 m_inSetFocus
= false;
121 bool wxControlContainerBase::SetFocusToChild()
123 return wxSetFocusToChild(m_winParent
, &m_winLastFocused
);
126 #ifndef wxHAS_NATIVE_TAB_TRAVERSAL
128 // ----------------------------------------------------------------------------
129 // generic wxControlContainer
130 // ----------------------------------------------------------------------------
132 wxControlContainer::wxControlContainer()
134 m_winLastFocused
= NULL
;
137 void wxControlContainer::SetLastFocus(wxWindow
*win
)
139 // the panel itself should never get the focus at all but if it does happen
140 // temporarily (as it seems to do under wxGTK), at the very least don't
141 // forget our previous m_winLastFocused
142 if ( win
!= m_winParent
)
144 // if we're setting the focus
147 // find the last _immediate_ child which got focus
148 wxWindow
*winParent
= win
;
149 while ( winParent
!= m_winParent
)
152 winParent
= win
->GetParent();
154 // Yes, this can happen, though in a totally pathological case.
155 // like when detaching a menubar from a frame with a child
156 // which has pushed itself as an event handler for the menubar.
159 wxASSERT_MSG( winParent
,
160 wxT("Setting last focus for a window that is not our child?") );
164 m_winLastFocused
= win
;
168 wxLogTrace(TRACE_FOCUS
, wxT("Set last focus to %s(%s)"),
169 win
->GetClassInfo()->GetClassName(),
170 win
->GetLabel().c_str());
174 wxLogTrace(TRACE_FOCUS
, wxT("No more last focus"));
179 // --------------------------------------------------------------------
180 // The following four functions are used to find other radio buttons
181 // within the same group. Used by wxSetFocusToChild on wxMSW
182 // --------------------------------------------------------------------
184 #if defined(__WXMSW__) && wxUSE_RADIOBTN
186 wxRadioButton
* wxGetPreviousButtonInGroup(wxRadioButton
*btn
)
188 if ( btn
->HasFlag(wxRB_GROUP
) || btn
->HasFlag(wxRB_SINGLE
) )
191 const wxWindowList
& siblings
= btn
->GetParent()->GetChildren();
192 wxWindowList::compatibility_iterator nodeThis
= siblings
.Find(btn
);
193 wxCHECK_MSG( nodeThis
, NULL
, wxT("radio button not a child of its parent?") );
195 // Iterate over all previous siblings until we find the next radio button
196 wxWindowList::compatibility_iterator nodeBefore
= nodeThis
->GetPrevious();
197 wxRadioButton
*prevBtn
= 0;
200 prevBtn
= wxDynamicCast(nodeBefore
->GetData(), wxRadioButton
);
204 nodeBefore
= nodeBefore
->GetPrevious();
207 if (!prevBtn
|| prevBtn
->HasFlag(wxRB_SINGLE
))
209 // no more buttons in group
216 wxRadioButton
* wxGetNextButtonInGroup(wxRadioButton
*btn
)
218 if (btn
->HasFlag(wxRB_SINGLE
))
221 const wxWindowList
& siblings
= btn
->GetParent()->GetChildren();
222 wxWindowList::compatibility_iterator nodeThis
= siblings
.Find(btn
);
223 wxCHECK_MSG( nodeThis
, NULL
, wxT("radio button not a child of its parent?") );
225 // Iterate over all previous siblings until we find the next radio button
226 wxWindowList::compatibility_iterator nodeNext
= nodeThis
->GetNext();
227 wxRadioButton
*nextBtn
= 0;
230 nextBtn
= wxDynamicCast(nodeNext
->GetData(), wxRadioButton
);
234 nodeNext
= nodeNext
->GetNext();
237 if ( !nextBtn
|| nextBtn
->HasFlag(wxRB_GROUP
) || nextBtn
->HasFlag(wxRB_SINGLE
) )
239 // no more buttons or the first button of the next group
246 wxRadioButton
* wxGetFirstButtonInGroup(wxRadioButton
*btn
)
250 wxRadioButton
* prevBtn
= wxGetPreviousButtonInGroup(btn
);
258 wxRadioButton
* wxGetLastButtonInGroup(wxRadioButton
*btn
)
262 wxRadioButton
* nextBtn
= wxGetNextButtonInGroup(btn
);
270 wxRadioButton
* wxGetSelectedButtonInGroup(wxRadioButton
*btn
)
272 // Find currently selected button
276 if (btn
->HasFlag(wxRB_SINGLE
))
279 wxRadioButton
*selBtn
;
281 // First check all previous buttons
282 for (selBtn
= wxGetPreviousButtonInGroup(btn
); selBtn
; selBtn
= wxGetPreviousButtonInGroup(selBtn
))
283 if (selBtn
->GetValue())
286 // Now all following buttons
287 for (selBtn
= wxGetNextButtonInGroup(btn
); selBtn
; selBtn
= wxGetNextButtonInGroup(selBtn
))
288 if (selBtn
->GetValue())
296 // ----------------------------------------------------------------------------
297 // Keyboard handling - this is the place where the TAB traversal logic is
298 // implemented. As this code is common to all ports, this ensures consistent
299 // behaviour even if we don't specify how exactly the wxNavigationKeyEvent are
300 // generated and this is done in platform specific code which also ensures that
301 // we can follow the given platform standards.
302 // ----------------------------------------------------------------------------
304 void wxControlContainer::HandleOnNavigationKey( wxNavigationKeyEvent
& event
)
306 // for a TLW we shouldn't involve the parent window, it has nothing to do
307 // with keyboard navigation inside this TLW
308 wxWindow
*parent
= m_winParent
->IsTopLevel() ? NULL
309 : m_winParent
->GetParent();
311 // the event is propagated downwards if the event emitter was our parent
312 bool goingDown
= event
.GetEventObject() == parent
;
314 const wxWindowList
& children
= m_winParent
->GetChildren();
316 // if we have exactly one notebook-like child window (actually it could be
317 // any window that returns true from its HasMultiplePages()), then
318 // [Shift-]Ctrl-Tab and Ctrl-PageUp/Down keys should iterate over its pages
319 // even if the focus is outside of the control because this is how the
320 // standard MSW properties dialogs behave and we do it under other platforms
321 // as well because it seems like a good idea -- but we can always put this
322 // block inside "#ifdef __WXMSW__" if it's not suitable there
323 if ( event
.IsWindowChange() && !goingDown
)
325 // check if we have a unique notebook-like child
326 wxWindow
*bookctrl
= NULL
;
327 for ( wxWindowList::const_iterator i
= children
.begin(),
328 end
= children
.end();
332 wxWindow
* const window
= *i
;
333 if ( window
->HasMultiplePages() )
337 // this is the second book-like control already so don't do
338 // anything as we don't know which one should have its page
350 // make sure that we don't bubble up the event again from the book
351 // control resulting in infinite recursion
352 wxNavigationKeyEvent
eventCopy(event
);
353 eventCopy
.SetEventObject(m_winParent
);
354 if ( bookctrl
->GetEventHandler()->ProcessEvent(eventCopy
) )
359 // there is not much to do if we don't have children and we're not
360 // interested in "notebook page change" events here
361 if ( !children
.GetCount() || event
.IsWindowChange() )
363 // let the parent process it unless it already comes from our parent
364 // of we don't have any
366 !parent
|| !parent
->GetEventHandler()->ProcessEvent(event
) )
374 // where are we going?
375 const bool forward
= event
.GetDirection();
377 // the node of the children list from which we should start looking for the
378 // next acceptable child
379 wxWindowList::compatibility_iterator node
, start_node
;
381 // we should start from the first/last control and not from the one which
382 // had focus the last time if we're propagating the event downwards because
383 // for our parent we look like a single control
386 // just to be sure it's not used (normally this is not necessary, but
387 // doesn't hurt neither)
388 m_winLastFocused
= NULL
;
390 // start from first or last depending on where we're going
391 node
= forward
? children
.GetFirst() : children
.GetLast();
395 // try to find the child which has the focus currently
397 // the event emitter might have done this for us
398 wxWindow
*winFocus
= event
.GetCurrentFocus();
400 // but if not, we might know where the focus was ourselves
402 winFocus
= m_winLastFocused
;
404 // if still no luck, do it the hard way
406 winFocus
= wxWindow::FindFocus();
410 #if defined(__WXMSW__) && wxUSE_RADIOBTN
411 // If we are in a radio button group, start from the first item in the
413 if ( event
.IsFromTab() && wxIsKindOf(winFocus
, wxRadioButton
) )
414 winFocus
= wxGetFirstButtonInGroup((wxRadioButton
*)winFocus
);
416 // ok, we found the focus - now is it our child?
417 start_node
= children
.Find( winFocus
);
420 if ( !start_node
&& m_winLastFocused
)
422 // window which has focus isn't our child, fall back to the one
423 // which had the focus the last time
424 start_node
= children
.Find( m_winLastFocused
);
427 // if we still didn't find anything, we should start with the first one
430 start_node
= children
.GetFirst();
433 // and the first child which we can try setting focus to is the next or
435 node
= forward
? start_node
->GetNext() : start_node
->GetPrevious();
438 // we want to cycle over all elements passing by NULL
441 // don't go into infinite loop
442 if ( start_node
&& node
&& node
== start_node
)
445 // Have we come to the last or first item on the panel?
450 // exit now as otherwise we'd loop forever
456 // Check if our (maybe grand) parent is another panel: if this
457 // is the case, they will know what to do with this navigation
458 // key and so give them the chance to process it instead of
459 // looping inside this panel (normally, the focus will go to
460 // the next/previous item after this panel in the parent
462 wxWindow
*focusedParent
= m_winParent
;
465 // We don't want to tab into a different dialog or frame or
466 // even an MDI child frame, so test for this explicitly
467 // (and in particular don't just use IsTopLevel() which
468 // would return false in the latter case).
469 if ( focusedParent
->IsTopNavigationDomain() )
472 event
.SetCurrentFocus( focusedParent
);
473 if ( parent
->GetEventHandler()->ProcessEvent( event
) )
476 focusedParent
= parent
;
478 parent
= parent
->GetParent();
481 //else: as the focus came from our parent, we definitely don't want
482 // to send it back to it!
484 // no, we are not inside another panel so process this ourself
485 node
= forward
? children
.GetFirst() : children
.GetLast();
490 wxWindow
*child
= node
->GetData();
492 // don't TAB to another TLW
493 if ( child
->IsTopLevel() )
495 node
= forward
? node
->GetNext() : node
->GetPrevious();
500 #if defined(__WXMSW__) && wxUSE_RADIOBTN
501 if ( event
.IsFromTab() )
503 if ( wxIsKindOf(child
, wxRadioButton
) )
505 // only radio buttons with either wxRB_GROUP or wxRB_SINGLE
507 if ( child
->HasFlag(wxRB_GROUP
) )
509 // need to tab into the active button within a group
510 wxRadioButton
*rb
= wxGetSelectedButtonInGroup((wxRadioButton
*)child
);
514 else if ( !child
->HasFlag(wxRB_SINGLE
) )
516 node
= forward
? node
->GetNext() : node
->GetPrevious();
521 else if ( m_winLastFocused
&&
522 wxIsKindOf(m_winLastFocused
, wxRadioButton
) &&
523 !m_winLastFocused
->HasFlag(wxRB_SINGLE
) )
525 wxRadioButton
* const
526 lastBtn
= static_cast<wxRadioButton
*>(m_winLastFocused
);
528 // cursor keys don't navigate out of a radio button group so
529 // find the correct radio button to focus
532 child
= wxGetNextButtonInGroup(lastBtn
);
535 // no next button in group, set it to the first button
536 child
= wxGetFirstButtonInGroup(lastBtn
);
541 child
= wxGetPreviousButtonInGroup(lastBtn
);
544 // no previous button in group, set it to the last button
545 child
= wxGetLastButtonInGroup(lastBtn
);
549 if ( child
== m_winLastFocused
)
551 // must be a group consisting of only one button therefore
552 // no need to send a navigation event
559 if ( child
->CanAcceptFocusFromKeyboard() )
561 // if we're setting the focus to a child panel we should prevent it
562 // from giving it to the child which had the focus the last time
563 // and instead give it to the first/last child depending from which
564 // direction we're coming
565 event
.SetEventObject(m_winParent
);
567 // disable propagation for this call as otherwise the event might
568 // bounce back to us.
569 wxPropagationDisabler
disableProp(event
);
570 if ( !child
->GetEventHandler()->ProcessEvent(event
) )
572 // set it first in case SetFocusFromKbd() results in focus
574 m_winLastFocused
= child
;
576 // everything is simple: just give focus to it
577 child
->SetFocusFromKbd();
579 //else: the child manages its focus itself
586 node
= forward
? node
->GetNext() : node
->GetPrevious();
589 // we cycled through all of our children and none of them wanted to accept
594 void wxControlContainer::HandleOnWindowDestroy(wxWindowBase
*child
)
596 if ( child
== m_winLastFocused
)
597 m_winLastFocused
= NULL
;
600 // ----------------------------------------------------------------------------
602 // ----------------------------------------------------------------------------
604 void wxControlContainer::HandleOnFocus(wxFocusEvent
& event
)
606 wxLogTrace(TRACE_FOCUS
, wxT("OnFocus on wxPanel 0x%p, name: %s"),
607 m_winParent
->GetHandle(),
608 m_winParent
->GetName().c_str() );
617 // wxHAS_NATIVE_TAB_TRAVERSAL
619 bool wxControlContainer::SetFocusToChild()
621 return wxSetFocusToChild(m_winParent
, NULL
);
625 #endif // !wxHAS_NATIVE_TAB_TRAVERSAL
627 // ----------------------------------------------------------------------------
628 // SetFocusToChild(): this function is used by wxPanel but also by wxFrame in
629 // wxMSW, this is why it is outside of wxControlContainer class
630 // ----------------------------------------------------------------------------
632 bool wxSetFocusToChild(wxWindow
*win
, wxWindow
**childLastFocused
)
634 wxCHECK_MSG( win
, false, wxT("wxSetFocusToChild(): invalid window") );
635 // wxCHECK_MSG( childLastFocused, false,
636 // wxT("wxSetFocusToChild(): NULL child poonter") );
638 if ( childLastFocused
&& *childLastFocused
)
640 // It might happen that the window got reparented
641 if ( (*childLastFocused
)->GetParent() == win
)
643 // And it also could have become hidden in the meanwhile
644 // We want to focus on the deepest widget visible
645 wxWindow
*deepestVisibleWindow
= NULL
;
647 while ( *childLastFocused
)
649 if ( (*childLastFocused
)->IsShown() )
651 if ( !deepestVisibleWindow
)
652 deepestVisibleWindow
= *childLastFocused
;
655 deepestVisibleWindow
= NULL
;
657 *childLastFocused
= (*childLastFocused
)->GetParent();
660 if ( deepestVisibleWindow
)
662 *childLastFocused
= deepestVisibleWindow
;
664 wxLogTrace(TRACE_FOCUS
,
665 wxT("SetFocusToChild() => last child (0x%p)."),
666 (*childLastFocused
)->GetHandle());
668 // not SetFocusFromKbd(): we're restoring focus back to the old
669 // window and not setting it as the result of a kbd action
670 (*childLastFocused
)->SetFocus();
676 // it doesn't count as such any more
677 *childLastFocused
= NULL
;
681 // set the focus to the first child who wants it
682 wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst();
685 wxWindow
*child
= node
->GetData();
686 node
= node
->GetNext();
688 // skip special windows:
689 if ( !win
->IsClientAreaChild(child
) )
692 if ( child
->CanAcceptFocusFromKeyboard() && !child
->IsTopLevel() )
694 #if defined(__WXMSW__) && wxUSE_RADIOBTN
695 // If a radiobutton is the first focusable child, search for the
696 // selected radiobutton in the same group
697 wxRadioButton
* btn
= wxDynamicCast(child
, wxRadioButton
);
700 wxRadioButton
* selected
= wxGetSelectedButtonInGroup(btn
);
706 wxLogTrace(TRACE_FOCUS
,
707 wxT("SetFocusToChild() => first child (0x%p)."),
710 if (childLastFocused
)
711 *childLastFocused
= child
;
712 child
->SetFocusFromKbd();