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
;
56 bool wxControlContainer::AcceptsFocus() const
58 // if we're not shown or disabled, we can't accept focus
59 if ( m_winParent
->IsShown() && m_winParent
->IsEnabled() )
61 // otherwise we can accept focus either if we have no children at all
62 // (in this case we're probably not used as a container) or only when
63 // at least one child will accept focus
64 wxWindowList::compatibility_iterator node
= m_winParent
->GetChildren().GetFirst();
69 // wxMac has eventually the two scrollbars as children, they don't count
70 // as real children in the algorithm mentioned above
71 bool hasRealChildren
= false ;
76 wxWindow
*child
= node
->GetData();
78 if ( child
->AcceptsFocus() )
84 wxScrollBar
*sb
= wxDynamicCast( child
, wxScrollBar
) ;
85 if ( sb
== NULL
|| !m_winParent
->MacIsWindowScrollbar( sb
) )
86 hasRealChildren
= true ;
88 node
= node
->GetNext();
92 if ( !hasRealChildren
)
100 void wxControlContainer::SetLastFocus(wxWindow
*win
)
102 // the panel itself should never get the focus at all but if it does happen
103 // temporarily (as it seems to do under wxGTK), at the very least don't
104 // forget our previous m_winLastFocused
105 if ( win
!= m_winParent
)
107 // if we're setting the focus
110 // find the last _immediate_ child which got focus
111 wxWindow
*winParent
= win
;
112 while ( winParent
!= m_winParent
)
115 winParent
= win
->GetParent();
117 // Yes, this can happen, though in a totally pathological case.
118 // like when detaching a menubar from a frame with a child
119 // which has pushed itself as an event handler for the menubar.
122 wxASSERT_MSG( winParent
,
123 _T("Setting last focus for a window that is not our child?") );
127 m_winLastFocused
= win
;
131 wxLogTrace(_T("focus"), _T("Set last focus to %s(%s)"),
132 win
->GetClassInfo()->GetClassName(),
133 win
->GetLabel().c_str());
137 wxLogTrace(_T("focus"), _T("No more last focus"));
141 // propagate the last focus upwards so that our parent can set focus back
142 // to us if it loses it now and regains later
143 wxWindow
*parent
= m_winParent
->GetParent();
146 wxChildFocusEvent
eventFocus(m_winParent
);
147 parent
->GetEventHandler()->ProcessEvent(eventFocus
);
151 // ----------------------------------------------------------------------------
152 // Keyboard handling - this is the place where the TAB traversal logic is
153 // implemented. As this code is common to all ports, this ensures consistent
154 // behaviour even if we don't specify how exactly the wxNavigationKeyEvent are
155 // generated and this is done in platform specific code which also ensures that
156 // we can follow the given platform standards.
157 // ----------------------------------------------------------------------------
159 void wxControlContainer::HandleOnNavigationKey( wxNavigationKeyEvent
& event
)
161 wxWindow
*parent
= m_winParent
->GetParent();
163 // the event is propagated downwards if the event emitter was our parent
164 bool goingDown
= event
.GetEventObject() == parent
;
166 const wxWindowList
& children
= m_winParent
->GetChildren();
168 // there is not much to do if we don't have children and we're not
169 // interested in "notebook page change" events here
170 if ( !children
.GetCount() || event
.IsWindowChange() )
172 // let the parent process it unless it already comes from our parent
173 // of we don't have any
175 !parent
|| !parent
->GetEventHandler()->ProcessEvent(event
) )
183 // where are we going?
184 bool forward
= event
.GetDirection();
186 // the node of the children list from which we should start looking for the
187 // next acceptable child
188 wxWindowList::compatibility_iterator node
, start_node
;
190 // we should start from the first/last control and not from the one which
191 // had focus the last time if we're propagating the event downwards because
192 // for our parent we look like a single control
195 // just to be sure it's not used (normally this is not necessary, but
196 // doesn't hurt neither)
197 m_winLastFocused
= (wxWindow
*)NULL
;
199 // start from first or last depending on where we're going
200 node
= forward
? children
.GetFirst() : children
.GetLast();
202 // we want to cycle over all nodes
203 start_node
= wxWindowList::compatibility_iterator();
207 // try to find the child which has the focus currently
209 // the event emitter might have done this for us
210 wxWindow
*winFocus
= event
.GetCurrentFocus();
212 // but if not, we might know where the focus was ourselves
214 winFocus
= m_winLastFocused
;
216 // if still no luck, do it the hard way
218 winFocus
= wxWindow::FindFocus();
222 // ok, we found the focus - now is it our child?
223 start_node
= children
.Find( winFocus
);
227 start_node
= wxWindowList::compatibility_iterator();
230 if ( !start_node
&& m_winLastFocused
)
232 // window which has focus isn't our child, fall back to the one
233 // which had the focus the last time
234 start_node
= children
.Find( m_winLastFocused
);
237 // if we still didn't find anything, we should start with the first one
240 start_node
= children
.GetFirst();
243 // and the first child which we can try setting focus to is the next or
245 node
= forward
? start_node
->GetNext() : start_node
->GetPrevious();
248 // we want to cycle over all elements passing by NULL
249 while ( node
!= start_node
)
251 // Have we come to the last or first item on the panel?
256 // Check if our (may be grand) parent is another panel: if this
257 // is the case, they will know what to do with this navigation
258 // key and so give them the chance to process it instead of
259 // looping inside this panel (normally, the focus will go to
260 // the next/previous item after this panel in the parent
262 wxWindow
*focussed_child_of_parent
= m_winParent
;
265 // we don't want to tab into a different dialog or frame
266 if ( focussed_child_of_parent
->IsTopLevel() )
269 event
.SetCurrentFocus( focussed_child_of_parent
);
270 if ( parent
->GetEventHandler()->ProcessEvent( event
) )
273 focussed_child_of_parent
= parent
;
275 parent
= parent
->GetParent();
278 //else: as the focus came from our parent, we definitely don't want
279 // to send it back to it!
281 // no, we are not inside another panel so process this ourself
282 node
= forward
? children
.GetFirst() : children
.GetLast();
287 wxWindow
*child
= node
->GetData();
289 if ( child
->AcceptsFocusFromKeyboard() )
291 // if we're setting the focus to a child panel we should prevent it
292 // from giving it to the child which had the focus the last time
293 // and instead give it to the first/last child depending from which
294 // direction we're coming
295 event
.SetEventObject(m_winParent
);
296 // disable propagation for this call as otherwise the event might
297 // bounce back to us.
298 wxPropagationDisabler
disableProp(event
);
299 if ( !child
->GetEventHandler()->ProcessEvent(event
) )
301 // set it first in case SetFocusFromKbd() results in focus
303 m_winLastFocused
= child
;
305 // everything is simple: just give focus to it
306 child
->SetFocusFromKbd();
308 //else: the child manages its focus itself
315 node
= forward
? node
->GetNext() : node
->GetPrevious();
318 // we cycled through all of our children and none of them wanted to accept
323 void wxControlContainer::HandleOnWindowDestroy(wxWindowBase
*child
)
325 if ( child
== m_winLastFocused
)
326 m_winLastFocused
= NULL
;
328 if ( child
== m_winDefault
)
331 if ( child
== m_winTmpDefault
)
332 m_winTmpDefault
= NULL
;
335 // ----------------------------------------------------------------------------
337 // ----------------------------------------------------------------------------
339 bool wxControlContainer::DoSetFocus()
341 wxLogTrace(_T("focus"), _T("SetFocus on wxPanel 0x%08lx."),
342 (unsigned long)m_winParent
->GetHandle());
344 // when the panel gets the focus we move the focus to either the last
345 // window that had the focus or the first one that can get it unless the
346 // focus had been already set to some other child
348 wxWindow
*win
= wxWindow::FindFocus();
351 if ( win
== m_winParent
)
353 // our child already has focus, don't take it away from it
357 if ( win
->IsTopLevel() )
359 // don't look beyond the first top level parent - useless and
364 win
= win
->GetParent();
367 return SetFocusToChild();
370 void wxControlContainer::HandleOnFocus(wxFocusEvent
& event
)
372 wxLogTrace(_T("focus"), _T("OnFocus on wxPanel 0x%08lx, name: %s"),
373 (unsigned long)m_winParent
->GetHandle(),
374 m_winParent
->GetName().c_str() );
381 bool wxControlContainer::SetFocusToChild()
383 return wxSetFocusToChild(m_winParent
, &m_winLastFocused
);
386 // ----------------------------------------------------------------------------
387 // SetFocusToChild(): this function is used by wxPanel but also by wxFrame in
388 // wxMSW, this is why it is outside of wxControlContainer class
389 // ----------------------------------------------------------------------------
391 bool wxSetFocusToChild(wxWindow
*win
, wxWindow
**childLastFocused
)
393 wxCHECK_MSG( win
, FALSE
, _T("wxSetFocusToChild(): invalid window") );
394 wxCHECK_MSG( childLastFocused
, FALSE
,
395 _T("wxSetFocusToChild(): NULL child poonter") );
397 if ( *childLastFocused
)
399 // It might happen that the window got reparented
400 if ( (*childLastFocused
)->GetParent() == win
)
402 wxLogTrace(_T("focus"),
403 _T("SetFocusToChild() => last child (0x%08lx)."),
404 (unsigned long)(*childLastFocused
)->GetHandle());
406 // not SetFocusFromKbd(): we're restoring focus back to the old
407 // window and not setting it as the result of a kbd action
408 (*childLastFocused
)->SetFocus();
413 // it doesn't count as such any more
414 *childLastFocused
= (wxWindow
*)NULL
;
418 // set the focus to the first child who wants it
419 wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst();
422 wxWindow
*child
= node
->GetData();
424 if ( child
->AcceptsFocusFromKeyboard() && !child
->IsTopLevel() )
426 wxLogTrace(_T("focus"),
427 _T("SetFocusToChild() => first child (0x%08lx)."),
428 (unsigned long)child
->GetHandle());
430 *childLastFocused
= child
;
431 child
->SetFocusFromKbd();
435 node
= node
->GetNext();