]>
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 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
;
52 void wxControlContainer::SetLastFocus(wxWindow
*win
)
54 // the panel itself should never get the focus at all but if it does happen
55 // temporarily (as it seems to do under wxGTK), at the very least don't
56 // forget our previous m_winLastFocused
57 if ( win
== m_winParent
)
60 // if we're setting the focus
63 // find the last _immediate_ child which got focus but be prepared to
64 // handle the case when win == m_winParent as well
65 wxWindow
*winParent
= win
;
66 while ( winParent
!= m_winParent
)
69 winParent
= win
->GetParent();
71 // Yes, this can happen, though in a totally pathological case.
72 // like when detaching a menubar from a frame with a child which
73 // has pushed itself as an event handler for the menubar. (wxGtk)
75 wxASSERT_MSG( winParent
, _T("Setting last-focus for a window that is not our child?") );
79 m_winLastFocused
= win
;
83 wxLogTrace(_T("focus"), _T("Set last focus to %s(%s)"),
84 win
->GetClassInfo()->GetClassName(),
85 win
->GetLabel().c_str());
89 wxLogTrace(_T("focus"), _T("No more last focus"));
93 // ----------------------------------------------------------------------------
94 // Keyboard handling - this is the place where the TAB traversal logic is
95 // implemented. As this code is common to all ports, this ensures consistent
96 // behaviour even if we don't specify how exactly the wxNavigationKeyEvent are
97 // generated and this is done in platform specific code which also ensures that
98 // we can follow the given platform standards.
99 // ----------------------------------------------------------------------------
101 void wxControlContainer::HandleOnNavigationKey( wxNavigationKeyEvent
& event
)
103 wxWindow
*parent
= m_winParent
->GetParent();
105 // the event is propagated downwards if the event emitter was our parent
106 bool goingDown
= event
.GetEventObject() == parent
;
108 const wxWindowList
& children
= m_winParent
->GetChildren();
110 // there is not much to do if we don't have children and we're not
111 // interested in "notebook page change" events here
112 if ( !children
.GetCount() || event
.IsWindowChange() )
114 // let the parent process it unless it already comes from our parent
115 // of we don't have any
117 !parent
|| !parent
->GetEventHandler()->ProcessEvent(event
) )
125 // where are we going?
126 bool forward
= event
.GetDirection();
128 // the node of the children list from which we should start looking for the
129 // next acceptable child
130 wxWindowList::Node
*node
, *start_node
;
132 // we should start from the first/last control and not from the one which
133 // had focus the last time if we're propagating the event downwards because
134 // for our parent we look like a single control
137 // just to be sure it's not used (normally this is not necessary, but
138 // doesn't hurt neither)
139 m_winLastFocused
= (wxWindow
*)NULL
;
141 // start from first or last depending on where we're going
142 node
= forward
? children
.GetFirst() : children
.GetLast();
144 // we want to cycle over all nodes
145 start_node
= (wxWindowList::Node
*)NULL
;
149 // try to find the child which has the focus currently
151 // the event emitter might have done this for us
152 wxWindow
*winFocus
= event
.GetCurrentFocus();
154 // but if not, we might know where the focus was ourselves
156 winFocus
= m_winLastFocused
;
158 // if still no luck, do it the hard way
160 winFocus
= wxWindow::FindFocus();
164 // ok, we found the focus - now is it our child?
165 start_node
= children
.Find( winFocus
);
169 start_node
= (wxWindowList::Node
*)NULL
;
172 if ( !start_node
&& m_winLastFocused
)
174 // window which has focus isn't our child, fall back to the one
175 // which had the focus the last time
176 start_node
= children
.Find( m_winLastFocused
);
179 // if we still didn't find anything, we should start with the first one
182 start_node
= children
.GetFirst();
185 // and the first child which we can try setting focus to is the next or
187 node
= forward
? start_node
->GetNext() : start_node
->GetPrevious();
190 // we want to cycle over all elements passing by NULL
191 while ( node
!= start_node
)
193 // Have we come to the last or first item on the panel?
198 // Check if our (may be grand) parent is another panel: if this
199 // is the case, they will know what to do with this navigation
200 // key and so give them the chance to process it instead of
201 // looping inside this panel (normally, the focus will go to
202 // the next/previous item after this panel in the parent
204 wxWindow
*focussed_child_of_parent
= m_winParent
;
207 // we don't want to tab into a different dialog or frame
208 if ( focussed_child_of_parent
->IsTopLevel() )
211 event
.SetCurrentFocus( focussed_child_of_parent
);
212 if ( parent
->GetEventHandler()->ProcessEvent( event
) )
215 focussed_child_of_parent
= parent
;
217 parent
= parent
->GetParent();
220 //else: as the focus came from our parent, we definitely don't want
221 // to send it back to it!
223 // no, we are not inside another panel so process this ourself
224 node
= forward
? children
.GetFirst() : children
.GetLast();
229 wxWindow
*child
= node
->GetData();
231 if ( child
->AcceptsFocusFromKeyboard() )
233 // if we're setting the focus to a child panel we should prevent it
234 // from giving it to the child which had the focus the last time
235 // and instead give it to the first/last child depending from which
236 // direction we're coming
237 event
.SetEventObject(m_winParent
);
238 if ( !child
->GetEventHandler()->ProcessEvent(event
) )
240 // everything is simple: just give focus to it
241 child
->SetFocusFromKbd();
243 m_winLastFocused
= child
;
245 //else: the child manages its focus itself
252 node
= forward
? node
->GetNext() : node
->GetPrevious();
255 // we cycled through all of our children and none of them wanted to accept
260 void wxControlContainer::HandleOnWindowDestroy(wxWindowBase
*child
)
262 if ( child
== m_winLastFocused
)
263 m_winLastFocused
= NULL
;
265 if ( child
== m_winDefault
)
268 if ( child
== m_winTmpDefault
)
269 m_winTmpDefault
= NULL
;
272 // ----------------------------------------------------------------------------
274 // ----------------------------------------------------------------------------
276 bool wxControlContainer::DoSetFocus()
278 wxLogTrace(_T("focus"), _T("SetFocus on wxPanel 0x%08x."),
279 m_winParent
->GetHandle());
281 // If the panel gets the focus *by way of getting it set directly*
282 // we move the focus to the first window that can get it.
284 // VZ: no, we set the focus to the last window too. I don't understand why
285 // should we make this distinction: if an app wants to set focus to
286 // some precise control, it may always do it directly, but if we don't
287 // use m_winLastFocused here, the focus won't be set correctly after a
288 // notebook page change nor after frame activation under MSW (it calls
291 // RR: yes, when I the tab key to navigate in a panel with some controls and
292 // a notebook and the focus jumps to the notebook (typically coming from
293 // a button at the top) the notebook should focus the first child in the
294 // current notebook page, not the last one which would otherwise get the
295 // focus if you used the tab key to navigate from the current notebook
296 // page to button at the bottom. See every page in the controls sample.
298 // VZ: ok, but this still doesn't (at least I don't see how it can) take
299 // care of first/last child problem: i.e. if Shift-TAB is pressed in a
300 // situation like above, the focus should be given to the last child,
301 // not the first one (and not to the last focused one neither) - I
302 // think my addition to OnNavigationKey() above takes care of it.
303 // Keeping #ifdef __WXGTK__ for now, but please try removing it and see
306 // RR: Removed for now. Let's see what happens..
308 // if our child already has focus, don't take it away from it
309 wxWindow
*win
= wxWindow::FindFocus();
312 if ( win
== m_winParent
)
315 if ( win
->IsTopLevel() )
317 // don't look beyond the first top level parent - useless and
322 win
= win
->GetParent();
325 return SetFocusToChild();
328 void wxControlContainer::HandleOnFocus(wxFocusEvent
& event
)
330 wxLogTrace(_T("focus"), _T("OnFocus on wxPanel 0x%08x, name: %s"),
331 m_winParent
->GetHandle(),
332 m_winParent
->GetName().c_str() );
334 // If we panel got the focus *by way of getting clicked on*
335 // we move the focus to either the last window that had the
336 // focus or the first one that can get it.
337 (void)SetFocusToChild();
342 bool wxControlContainer::SetFocusToChild()
344 return wxSetFocusToChild(m_winParent
, &m_winLastFocused
);
347 // ----------------------------------------------------------------------------
348 // SetFocusToChild(): this function is used by wxPanel but also by wxFrame in
349 // wxMSW, this is why it is outside of wxControlContainer class
350 // ----------------------------------------------------------------------------
352 bool wxSetFocusToChild(wxWindow
*win
, wxWindow
**childLastFocused
)
354 wxCHECK_MSG( win
, FALSE
, _T("wxSetFocusToChild(): invalid window") );
356 if ( *childLastFocused
)
358 // It might happen that the window got reparented or no longer accepts
360 if ( (*childLastFocused
)->GetParent() == win
&&
361 (*childLastFocused
)->AcceptsFocusFromKeyboard() )
363 wxLogTrace(_T("focus"),
364 _T("SetFocusToChild() => last child (0x%08x)."),
365 (*childLastFocused
)->GetHandle());
367 (*childLastFocused
)->SetFocusFromKbd();
372 // it doesn't count as such any more
373 *childLastFocused
= (wxWindow
*)NULL
;
377 // set the focus to the first child who wants it
378 wxWindowList::Node
*node
= win
->GetChildren().GetFirst();
381 wxWindow
*child
= node
->GetData();
383 if ( child
->AcceptsFocusFromKeyboard() && !child
->IsTopLevel() )
385 wxLogTrace(_T("focus"),
386 _T("SetFocusToChild() => first child (0x%08x)."),
389 *childLastFocused
= child
; // should be redundant, but it is not
390 child
->SetFocusFromKbd();
394 node
= node
->GetNext();