]>
git.saurik.com Git - wxWidgets.git/blob - src/common/containr.cpp
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 // ----------------------------------------------------------------------------
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::Node
*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::Node
*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::Node
*)NULL
;
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::Node
*)NULL
;
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 if ( !child
->GetEventHandler()->ProcessEvent(event
) )
298 // set it first in case SetFocusFromKbd() results in focus
300 m_winLastFocused
= child
;
302 // everything is simple: just give focus to it
303 child
->SetFocusFromKbd();
305 //else: the child manages its focus itself
312 node
= forward
? node
->GetNext() : node
->GetPrevious();
315 // we cycled through all of our children and none of them wanted to accept
320 void wxControlContainer::HandleOnWindowDestroy(wxWindowBase
*child
)
322 if ( child
== m_winLastFocused
)
323 m_winLastFocused
= NULL
;
325 if ( child
== m_winDefault
)
328 if ( child
== m_winTmpDefault
)
329 m_winTmpDefault
= NULL
;
332 // ----------------------------------------------------------------------------
334 // ----------------------------------------------------------------------------
336 bool wxControlContainer::DoSetFocus()
338 wxLogTrace(_T("focus"), _T("SetFocus on wxPanel 0x%08lx."),
339 (unsigned long)m_winParent
->GetHandle());
341 // when the panel gets the focus we move the focus to either the last
342 // window that had the focus or the first one that can get it unless the
343 // focus had been already set to some other child
345 wxWindow
*win
= wxWindow::FindFocus();
348 if ( win
== m_winParent
)
350 // our child already has focus, don't take it away from it
354 if ( win
->IsTopLevel() )
356 // don't look beyond the first top level parent - useless and
361 win
= win
->GetParent();
364 return SetFocusToChild();
367 void wxControlContainer::HandleOnFocus(wxFocusEvent
& event
)
369 wxLogTrace(_T("focus"), _T("OnFocus on wxPanel 0x%08lx, name: %s"),
370 (unsigned long)m_winParent
->GetHandle(),
371 m_winParent
->GetName().c_str() );
378 bool wxControlContainer::SetFocusToChild()
380 return wxSetFocusToChild(m_winParent
, &m_winLastFocused
);
383 // ----------------------------------------------------------------------------
384 // SetFocusToChild(): this function is used by wxPanel but also by wxFrame in
385 // wxMSW, this is why it is outside of wxControlContainer class
386 // ----------------------------------------------------------------------------
388 bool wxSetFocusToChild(wxWindow
*win
, wxWindow
**childLastFocused
)
390 wxCHECK_MSG( win
, FALSE
, _T("wxSetFocusToChild(): invalid window") );
391 wxCHECK_MSG( childLastFocused
, FALSE
,
392 _T("wxSetFocusToChild(): NULL child poonter") );
394 if ( *childLastFocused
)
396 // It might happen that the window got reparented
397 if ( (*childLastFocused
)->GetParent() == win
)
399 wxLogTrace(_T("focus"),
400 _T("SetFocusToChild() => last child (0x%08lx)."),
401 (unsigned long)(*childLastFocused
)->GetHandle());
403 // not SetFocusFromKbd(): we're restoring focus back to the old
404 // window and not setting it as the result of a kbd action
405 (*childLastFocused
)->SetFocus();
410 // it doesn't count as such any more
411 *childLastFocused
= (wxWindow
*)NULL
;
415 // set the focus to the first child who wants it
416 wxWindowList::Node
*node
= win
->GetChildren().GetFirst();
419 wxWindow
*child
= node
->GetData();
421 if ( child
->AcceptsFocusFromKeyboard() && !child
->IsTopLevel() )
423 wxLogTrace(_T("focus"),
424 _T("SetFocusToChild() => first child (0x%08lx)."),
425 (unsigned long)child
->GetHandle());
427 *childLastFocused
= child
;
428 child
->SetFocusFromKbd();
432 node
= node
->GetNext();