]>
git.saurik.com Git - wxWidgets.git/blob - src/common/containr.cpp
c5133c803dd4003609508a69eb39518e369e6395
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 license
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/containr.h"
36 // ============================================================================
38 // ============================================================================
40 wxControlContainer::wxControlContainer(wxWindow
*winParent
)
42 m_winParent
= winParent
;
48 void wxControlContainer::SetLastFocus(wxWindow
*win
)
50 // find the last _immediate_ child which got focus
53 wxWindow
*parent
= win
->GetParent();
54 if ( parent
== m_winParent
)
60 wxASSERT_MSG( win
, _T("attempt to set last focus to not a child?") );
62 m_winLastFocused
= win
;
65 // ----------------------------------------------------------------------------
66 // Keyboard handling - this is the place where the TAB traversal logic is
67 // implemented. As this code is common to all ports, this ensures consistent
68 // behaviour even if we don't specify how exactly the wxNavigationKeyEvent are
69 // generated and this is done in platform specific code which also ensures that
70 // we can follow the given platform standards.
71 // ----------------------------------------------------------------------------
73 void wxControlContainer::HandleOnNavigationKey( wxNavigationKeyEvent
& event
)
75 wxWindow
*parent
= m_winParent
->GetParent();
77 // the event is propagated downwards if the event emitter was our parent
78 bool goingDown
= event
.GetEventObject() == parent
;
80 const wxWindowList
& children
= m_winParent
->GetChildren();
82 // there is not much to do if we don't have children and we're not
83 // interested in "notebook page change" events here
84 if ( !children
.GetCount() || event
.IsWindowChange() )
86 // let the parent process it unless it already comes from our parent
87 // of we don't have any
89 !parent
|| !parent
->GetEventHandler()->ProcessEvent(event
) )
97 // where are we going?
98 bool forward
= event
.GetDirection();
100 // the node of the children list from which we should start looking for the
101 // next acceptable child
102 wxWindowList::Node
*node
, *start_node
;
104 // we should start from the first/last control and not from the one which
105 // had focus the last time if we're propagating the event downwards because
106 // for our parent we look like a single control
109 // just to be sure it's not used (normally this is not necessary, but
110 // doesn't hurt neither)
111 m_winLastFocused
= (wxWindow
*)NULL
;
113 // start from first or last depending on where we're going
114 node
= forward
? children
.GetFirst() : children
.GetLast();
116 // we want to cycle over all nodes
117 start_node
= (wxWindowList::Node
*)NULL
;
121 // try to find the child which has the focus currently
123 // the event emitter might have done this for us
124 wxWindow
*winFocus
= event
.GetCurrentFocus();
126 // but if not, we might know where the focus was ourselves
128 winFocus
= m_winLastFocused
;
130 // if still no luck, do it the hard way
132 winFocus
= wxWindow::FindFocus();
136 // ok, we found the focus - now is it our child?
137 start_node
= children
.Find( winFocus
);
141 start_node
= (wxWindowList::Node
*)NULL
;
144 if ( !start_node
&& m_winLastFocused
)
146 // window which has focus isn't our child, fall back to the one
147 // which had the focus the last time
148 start_node
= children
.Find( m_winLastFocused
);
151 // if we still didn't find anything, we should start with the first one
154 start_node
= children
.GetFirst();
157 // and the first child which we can try setting focus to is the next or
159 node
= forward
? start_node
->GetNext() : start_node
->GetPrevious();
162 // we want to cycle over all elements passing by NULL
163 while ( node
!= start_node
)
165 // Have we come to the last or first item on the panel?
170 // Check if our (may be grand) parent is another panel: if this
171 // is the case, they will know what to do with this navigation
172 // key and so give them the chance to process it instead of
173 // looping inside this panel (normally, the focus will go to
174 // the next/previous item after this panel in the parent
176 wxWindow
*focussed_child_of_parent
= m_winParent
;
179 // we don't want to tab into a different dialog or frame
180 if ( focussed_child_of_parent
->IsTopLevel() )
183 event
.SetCurrentFocus( focussed_child_of_parent
);
184 if ( parent
->GetEventHandler()->ProcessEvent( event
) )
187 focussed_child_of_parent
= parent
;
189 parent
= parent
->GetParent();
192 //else: as the focus came from our parent, we definitely don't want
193 // to send it back to it!
195 // no, we are not inside another panel so process this ourself
196 node
= forward
? children
.GetFirst() : children
.GetLast();
201 wxWindow
*child
= node
->GetData();
203 if ( child
->AcceptsFocusFromKeyboard() )
205 // if we're setting the focus to a child panel we should prevent it
206 // from giving it to the child which had the focus the last time
207 // and instead give it to the first/last child depending from which
208 // direction we're coming
209 event
.SetEventObject(m_winParent
);
210 if ( !child
->GetEventHandler()->ProcessEvent(event
) )
212 // everything is simple: just give focus to it
215 m_winLastFocused
= child
;
217 //else: the child manages its focus itself
224 node
= forward
? node
->GetNext() : node
->GetPrevious();
227 // we cycled through all of our children and none of them wanted to accept
232 void wxControlContainer::HandleOnWindowDestroy(wxWindowBase
*child
)
234 if ( child
== m_winLastFocused
)
235 m_winLastFocused
= NULL
;
237 if ( child
== m_winDefault
)
241 // ----------------------------------------------------------------------------
243 // ----------------------------------------------------------------------------
245 void wxControlContainer::DoSetFocus()
247 wxLogTrace(_T("focus"), _T("SetFocus on wxPanel 0x%08x."),
248 m_winParent
->GetHandle());
250 // If the panel gets the focus *by way of getting it set directly*
251 // we move the focus to the first window that can get it.
253 // VZ: no, we set the focus to the last window too. I don't understand why
254 // should we make this distinction: if an app wants to set focus to
255 // some precise control, it may always do it directly, but if we don't
256 // use m_winLastFocused here, the focus won't be set correctly after a
257 // notebook page change nor after frame activation under MSW (it calls
260 // RR: yes, when I the tab key to navigate in a panel with some controls and
261 // a notebook and the focus jumps to the notebook (typically coming from
262 // a button at the top) the notebook should focus the first child in the
263 // current notebook page, not the last one which would otherwise get the
264 // focus if you used the tab key to navigate from the current notebook
265 // page to button at the bottom. See every page in the controls sample.
267 // VZ: ok, but this still doesn't (at least I don't see how it can) take
268 // care of first/last child problem: i.e. if Shift-TAB is pressed in a
269 // situation like above, the focus should be given to the last child,
270 // not the first one (and not to the last focused one neither) - I
271 // think my addition to OnNavigationKey() above takes care of it.
272 // Keeping #ifdef __WXGTK__ for now, but please try removing it and see
275 // RR: Removed for now. Let's see what happens..
277 if ( !SetFocusToChild() )
279 m_winParent
->SetFocus();
283 void wxControlContainer::HandleOnFocus(wxFocusEvent
& event
)
285 wxLogTrace(_T("focus"), _T("OnFocus on wxPanel 0x%08x, name: %s"),
286 m_winParent
->GetHandle(),
287 m_winParent
->GetName().c_str() );
289 // If we panel got the focus *by way of getting clicked on*
290 // we move the focus to either the last window that had the
291 // focus or the first one that can get it.
292 (void)SetFocusToChild();
297 bool wxControlContainer::SetFocusToChild()
299 return wxSetFocusToChild(m_winParent
, &m_winLastFocused
);
302 // ----------------------------------------------------------------------------
303 // SetFocusToChild(): this function is used by wxPanel but also by wxFrame in
304 // wxMSW, this is why it is outside of wxControlContainer class
305 // ----------------------------------------------------------------------------
307 bool wxSetFocusToChild(wxWindow
*win
, wxWindow
**childLastFocused
)
309 wxCHECK_MSG( win
, FALSE
, _T("wxSetFocusToChild(): invalid window") );
311 if ( *childLastFocused
)
313 // It might happen that the window got reparented or no longer accepts
315 if ( (*childLastFocused
)->GetParent() == win
&&
316 (*childLastFocused
)->AcceptsFocusFromKeyboard() )
318 wxLogTrace(_T("focus"),
319 _T("SetFocusToChild() => last child (0x%08x)."),
320 (*childLastFocused
)->GetHandle());
322 (*childLastFocused
)->SetFocus();
327 // it doesn't count as such any more
328 *childLastFocused
= (wxWindow
*)NULL
;
332 // set the focus to the first child who wants it
333 wxWindowList::Node
*node
= win
->GetChildren().GetFirst();
336 wxWindow
*child
= node
->GetData();
338 if ( child
->AcceptsFocusFromKeyboard() && !child
->IsTopLevel() )
340 wxLogTrace(_T("focus"),
341 _T("SetFocusToChild() => first child (0x%08x)."),
344 *childLastFocused
= child
; // should be redundant, but it is not
349 node
= node
->GetNext();