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"
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 // Keyboard handling - this is the place where the TAB traversal logic is
154 // implemented. As this code is common to all ports, this ensures consistent
155 // behaviour even if we don't specify how exactly the wxNavigationKeyEvent are
156 // generated and this is done in platform specific code which also ensures that
157 // we can follow the given platform standards.
158 // ----------------------------------------------------------------------------
160 void wxControlContainer::HandleOnNavigationKey( wxNavigationKeyEvent
& event
)
162 wxWindow
*parent
= m_winParent
->GetParent();
164 // the event is propagated downwards if the event emitter was our parent
165 bool goingDown
= event
.GetEventObject() == parent
;
167 const wxWindowList
& children
= m_winParent
->GetChildren();
169 // there is not much to do if we don't have children and we're not
170 // interested in "notebook page change" events here
171 if ( !children
.GetCount() || event
.IsWindowChange() )
173 // let the parent process it unless it already comes from our parent
174 // of we don't have any
176 !parent
|| !parent
->GetEventHandler()->ProcessEvent(event
) )
184 // where are we going?
185 bool forward
= event
.GetDirection();
187 // the node of the children list from which we should start looking for the
188 // next acceptable child
189 wxWindowList::compatibility_iterator node
, start_node
;
191 // we should start from the first/last control and not from the one which
192 // had focus the last time if we're propagating the event downwards because
193 // for our parent we look like a single control
196 // just to be sure it's not used (normally this is not necessary, but
197 // doesn't hurt neither)
198 m_winLastFocused
= (wxWindow
*)NULL
;
200 // start from first or last depending on where we're going
201 node
= forward
? children
.GetFirst() : children
.GetLast();
203 // we want to cycle over all nodes
204 start_node
= wxWindowList::compatibility_iterator();
208 // try to find the child which has the focus currently
210 // the event emitter might have done this for us
211 wxWindow
*winFocus
= event
.GetCurrentFocus();
213 // but if not, we might know where the focus was ourselves
215 winFocus
= m_winLastFocused
;
217 // if still no luck, do it the hard way
219 winFocus
= wxWindow::FindFocus();
223 // ok, we found the focus - now is it our child?
224 start_node
= children
.Find( winFocus
);
228 start_node
= wxWindowList::compatibility_iterator();
231 if ( !start_node
&& m_winLastFocused
)
233 // window which has focus isn't our child, fall back to the one
234 // which had the focus the last time
235 start_node
= children
.Find( m_winLastFocused
);
238 // if we still didn't find anything, we should start with the first one
241 start_node
= children
.GetFirst();
244 // and the first child which we can try setting focus to is the next or
246 node
= forward
? start_node
->GetNext() : start_node
->GetPrevious();
249 // we want to cycle over all elements passing by NULL
250 while ( node
!= start_node
)
252 // Have we come to the last or first item on the panel?
257 // Check if our (may be grand) parent is another panel: if this
258 // is the case, they will know what to do with this navigation
259 // key and so give them the chance to process it instead of
260 // looping inside this panel (normally, the focus will go to
261 // the next/previous item after this panel in the parent
263 wxWindow
*focussed_child_of_parent
= m_winParent
;
266 // we don't want to tab into a different dialog or frame
267 if ( focussed_child_of_parent
->IsTopLevel() )
270 event
.SetCurrentFocus( focussed_child_of_parent
);
271 if ( parent
->GetEventHandler()->ProcessEvent( event
) )
274 focussed_child_of_parent
= parent
;
276 parent
= parent
->GetParent();
279 //else: as the focus came from our parent, we definitely don't want
280 // to send it back to it!
282 // no, we are not inside another panel so process this ourself
283 node
= forward
? children
.GetFirst() : children
.GetLast();
288 wxWindow
*child
= node
->GetData();
290 if ( child
->AcceptsFocusFromKeyboard() )
292 // if we're setting the focus to a child panel we should prevent it
293 // from giving it to the child which had the focus the last time
294 // and instead give it to the first/last child depending from which
295 // direction we're coming
296 event
.SetEventObject(m_winParent
);
297 // disable propagation for this call as otherwise the event might
298 // bounce back to us.
299 wxPropagationDisabler
disableProp(event
);
300 if ( !child
->GetEventHandler()->ProcessEvent(event
) )
302 // set it first in case SetFocusFromKbd() results in focus
304 m_winLastFocused
= child
;
306 // everything is simple: just give focus to it
307 child
->SetFocusFromKbd();
309 //else: the child manages its focus itself
316 node
= forward
? node
->GetNext() : node
->GetPrevious();
319 // we cycled through all of our children and none of them wanted to accept
324 void wxControlContainer::HandleOnWindowDestroy(wxWindowBase
*child
)
326 if ( child
== m_winLastFocused
)
327 m_winLastFocused
= NULL
;
329 if ( child
== m_winDefault
)
332 if ( child
== m_winTmpDefault
)
333 m_winTmpDefault
= NULL
;
336 // ----------------------------------------------------------------------------
338 // ----------------------------------------------------------------------------
340 bool wxControlContainer::DoSetFocus()
342 wxLogTrace(_T("focus"), _T("SetFocus on wxPanel 0x%08lx."),
343 (unsigned long)m_winParent
->GetHandle());
348 // when the panel gets the focus we move the focus to either the last
349 // window that had the focus or the first one that can get it unless the
350 // focus had been already set to some other child
352 wxWindow
*win
= wxWindow::FindFocus();
355 if ( win
== m_winParent
)
357 // our child already has focus, don't take it away from it
361 if ( win
->IsTopLevel() )
363 // don't look beyond the first top level parent - useless and
368 win
= win
->GetParent();
371 // protect against infinite recursion:
374 bool ret
= SetFocusToChild();
376 m_inSetFocus
= false;
381 void wxControlContainer::HandleOnFocus(wxFocusEvent
& event
)
383 wxLogTrace(_T("focus"), _T("OnFocus on wxPanel 0x%08lx, name: %s"),
384 (unsigned long)m_winParent
->GetHandle(),
385 m_winParent
->GetName().c_str() );
392 bool wxControlContainer::SetFocusToChild()
394 return wxSetFocusToChild(m_winParent
, &m_winLastFocused
);
397 // ----------------------------------------------------------------------------
398 // SetFocusToChild(): this function is used by wxPanel but also by wxFrame in
399 // wxMSW, this is why it is outside of wxControlContainer class
400 // ----------------------------------------------------------------------------
402 bool wxSetFocusToChild(wxWindow
*win
, wxWindow
**childLastFocused
)
404 wxCHECK_MSG( win
, false, _T("wxSetFocusToChild(): invalid window") );
405 wxCHECK_MSG( childLastFocused
, false,
406 _T("wxSetFocusToChild(): NULL child poonter") );
408 if ( *childLastFocused
)
410 // It might happen that the window got reparented
411 if ( (*childLastFocused
)->GetParent() == win
)
413 wxLogTrace(_T("focus"),
414 _T("SetFocusToChild() => last child (0x%08lx)."),
415 (unsigned long)(*childLastFocused
)->GetHandle());
417 // not SetFocusFromKbd(): we're restoring focus back to the old
418 // window and not setting it as the result of a kbd action
419 (*childLastFocused
)->SetFocus();
424 // it doesn't count as such any more
425 *childLastFocused
= (wxWindow
*)NULL
;
429 // set the focus to the first child who wants it
430 wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst();
433 wxWindow
*child
= node
->GetData();
435 if ( child
->AcceptsFocusFromKeyboard() && !child
->IsTopLevel() )
437 wxLogTrace(_T("focus"),
438 _T("SetFocusToChild() => first child (0x%08lx)."),
439 (unsigned long)child
->GetHandle());
441 *childLastFocused
= child
;
442 child
->SetFocusFromKbd();
446 node
= node
->GetNext();