]>
git.saurik.com Git - wxWidgets.git/blob - src/common/containr.cpp
f818d7ebb1d6321695b0556aca89cfbbc5880f2b
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/window.h"
37 #include "wx/containr.h"
39 // ============================================================================
41 // ============================================================================
43 wxControlContainer::wxControlContainer(wxWindow
*winParent
)
45 m_winParent
= winParent
;
51 void wxControlContainer::SetLastFocus(wxWindow
*win
)
53 // find the last _immediate_ child which got focus
56 wxWindow
*parent
= win
->GetParent();
57 if ( parent
== m_winParent
)
63 wxASSERT_MSG( win
, _T("attempt to set last focus to not a child?") );
65 m_winLastFocused
= win
;
68 // ----------------------------------------------------------------------------
69 // Keyboard handling - this is the place where the TAB traversal logic is
70 // implemented. As this code is common to all ports, this ensures consistent
71 // behaviour even if we don't specify how exactly the wxNavigationKeyEvent are
72 // generated and this is done in platform specific code which also ensures that
73 // we can follow the given platform standards.
74 // ----------------------------------------------------------------------------
76 void wxControlContainer::HandleOnNavigationKey( wxNavigationKeyEvent
& event
)
78 wxWindow
*parent
= m_winParent
->GetParent();
80 // the event is propagated downwards if the event emitter was our parent
81 bool goingDown
= event
.GetEventObject() == parent
;
83 const wxWindowList
& children
= m_winParent
->GetChildren();
85 // there is not much to do if we don't have children and we're not
86 // interested in "notebook page change" events here
87 if ( !children
.GetCount() || event
.IsWindowChange() )
89 // let the parent process it unless it already comes from our parent
90 // of we don't have any
92 !parent
|| !parent
->GetEventHandler()->ProcessEvent(event
) )
100 // where are we going?
101 bool forward
= event
.GetDirection();
103 // the node of the children list from which we should start looking for the
104 // next acceptable child
105 wxWindowList::Node
*node
, *start_node
;
107 // we should start from the first/last control and not from the one which
108 // had focus the last time if we're propagating the event downwards because
109 // for our parent we look like a single control
112 // just to be sure it's not used (normally this is not necessary, but
113 // doesn't hurt neither)
114 m_winLastFocused
= (wxWindow
*)NULL
;
116 // start from first or last depending on where we're going
117 node
= forward
? children
.GetFirst() : children
.GetLast();
119 // we want to cycle over all nodes
120 start_node
= (wxWindowList::Node
*)NULL
;
124 // try to find the child which has the focus currently
126 // the event emitter might have done this for us
127 wxWindow
*winFocus
= event
.GetCurrentFocus();
129 // but if not, we might know where the focus was ourselves
131 winFocus
= m_winLastFocused
;
133 // if still no luck, do it the hard way
135 winFocus
= wxWindow::FindFocus();
139 // ok, we found the focus - now is it our child?
140 start_node
= children
.Find( winFocus
);
144 start_node
= (wxWindowList::Node
*)NULL
;
147 if ( !start_node
&& m_winLastFocused
)
149 // window which has focus isn't our child, fall back to the one
150 // which had the focus the last time
151 start_node
= children
.Find( m_winLastFocused
);
154 // if we still didn't find anything, we should start with the first one
157 start_node
= children
.GetFirst();
160 // and the first child which we can try setting focus to is the next or
162 node
= forward
? start_node
->GetNext() : start_node
->GetPrevious();
165 // we want to cycle over all elements passing by NULL
166 while ( node
!= start_node
)
168 // Have we come to the last or first item on the panel?
173 // Check if our (may be grand) parent is another panel: if this
174 // is the case, they will know what to do with this navigation
175 // key and so give them the chance to process it instead of
176 // looping inside this panel (normally, the focus will go to
177 // the next/previous item after this panel in the parent
179 wxWindow
*focussed_child_of_parent
= m_winParent
;
182 // we don't want to tab into a different dialog or frame
183 if ( focussed_child_of_parent
->IsTopLevel() )
186 event
.SetCurrentFocus( focussed_child_of_parent
);
187 if ( parent
->GetEventHandler()->ProcessEvent( event
) )
190 focussed_child_of_parent
= parent
;
192 parent
= parent
->GetParent();
195 //else: as the focus came from our parent, we definitely don't want
196 // to send it back to it!
198 // no, we are not inside another panel so process this ourself
199 node
= forward
? children
.GetFirst() : children
.GetLast();
204 wxWindow
*child
= node
->GetData();
206 if ( child
->AcceptsFocusFromKeyboard() )
208 // if we're setting the focus to a child panel we should prevent it
209 // from giving it to the child which had the focus the last time
210 // and instead give it to the first/last child depending from which
211 // direction we're coming
212 event
.SetEventObject(m_winParent
);
213 if ( !child
->GetEventHandler()->ProcessEvent(event
) )
215 // everything is simple: just give focus to it
218 m_winLastFocused
= child
;
220 //else: the child manages its focus itself
227 node
= forward
? node
->GetNext() : node
->GetPrevious();
230 // we cycled through all of our children and none of them wanted to accept
235 void wxControlContainer::HandleOnWindowDestroy(wxWindowBase
*child
)
237 if ( child
== m_winLastFocused
)
238 m_winLastFocused
= NULL
;
240 if ( child
== m_winDefault
)
244 // ----------------------------------------------------------------------------
246 // ----------------------------------------------------------------------------
248 void wxControlContainer::DoSetFocus()
250 wxLogTrace(_T("focus"), _T("SetFocus on wxPanel 0x%08x."),
251 m_winParent
->GetHandle());
253 // If the panel gets the focus *by way of getting it set directly*
254 // we move the focus to the first window that can get it.
256 // VZ: no, we set the focus to the last window too. I don't understand why
257 // should we make this distinction: if an app wants to set focus to
258 // some precise control, it may always do it directly, but if we don't
259 // use m_winLastFocused here, the focus won't be set correctly after a
260 // notebook page change nor after frame activation under MSW (it calls
263 // RR: yes, when I the tab key to navigate in a panel with some controls and
264 // a notebook and the focus jumps to the notebook (typically coming from
265 // a button at the top) the notebook should focus the first child in the
266 // current notebook page, not the last one which would otherwise get the
267 // focus if you used the tab key to navigate from the current notebook
268 // page to button at the bottom. See every page in the controls sample.
270 // VZ: ok, but this still doesn't (at least I don't see how it can) take
271 // care of first/last child problem: i.e. if Shift-TAB is pressed in a
272 // situation like above, the focus should be given to the last child,
273 // not the first one (and not to the last focused one neither) - I
274 // think my addition to OnNavigationKey() above takes care of it.
275 // Keeping #ifdef __WXGTK__ for now, but please try removing it and see
278 // RR: Removed for now. Let's see what happens..
280 if ( !SetFocusToChild() )
282 m_winParent
->SetFocus();
286 void wxControlContainer::HandleOnFocus(wxFocusEvent
& event
)
288 wxLogTrace(_T("focus"), _T("OnFocus on wxPanel 0x%08x, name: %s"),
289 m_winParent
->GetHandle(),
290 m_winParent
->GetName().c_str() );
292 // If we panel got the focus *by way of getting clicked on*
293 // we move the focus to either the last window that had the
294 // focus or the first one that can get it.
295 (void)SetFocusToChild();
300 bool wxControlContainer::SetFocusToChild()
302 return wxSetFocusToChild(m_winParent
, &m_winLastFocused
);
305 // ----------------------------------------------------------------------------
306 // SetFocusToChild(): this function is used by wxPanel but also by wxFrame in
307 // wxMSW, this is why it is outside of wxControlContainer class
308 // ----------------------------------------------------------------------------
310 bool wxSetFocusToChild(wxWindow
*win
, wxWindow
**childLastFocused
)
312 wxCHECK_MSG( win
, FALSE
, _T("wxSetFocusToChild(): invalid window") );
314 if ( *childLastFocused
)
316 // It might happen that the window got reparented or no longer accepts
318 if ( (*childLastFocused
)->GetParent() == win
&&
319 (*childLastFocused
)->AcceptsFocusFromKeyboard() )
321 wxLogTrace(_T("focus"),
322 _T("SetFocusToChild() => last child (0x%08x)."),
323 (*childLastFocused
)->GetHandle());
325 (*childLastFocused
)->SetFocus();
330 // it doesn't count as such any more
331 *childLastFocused
= (wxWindow
*)NULL
;
335 // set the focus to the first child who wants it
336 wxWindowList::Node
*node
= win
->GetChildren().GetFirst();
339 wxWindow
*child
= node
->GetData();
341 if ( child
->AcceptsFocusFromKeyboard() && !child
->IsTopLevel() )
343 wxLogTrace(_T("focus"),
344 _T("SetFocusToChild() => first child (0x%08x)."),
347 *childLastFocused
= child
; // should be redundant, but it is not
352 node
= node
->GetNext();