]>
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
;
51 void wxControlContainer::SetLastFocus(wxWindow
*win
)
53 // the panel itself should never get the focus at all but if it does happen
54 // temporarily (as it seems to do under wxGTK), at the very least don't
55 // forget our previous m_winLastFocused
56 if ( win
== m_winParent
)
59 // if we're setting the focus
62 // find the last _immediate_ child which got focus but be prepared to
63 // handle the case when win == m_winParent as well
64 wxWindow
*winParent
= win
;
65 while ( winParent
!= m_winParent
)
68 winParent
= win
->GetParent();
70 // Yes, this can happen, though in a totally pathological case.
71 // like when detaching a menubar from a frame with a child which
72 // has pushed itself as an event handler for the menubar. (wxGtk)
74 wxASSERT_MSG( winParent
, _T("Setting last-focus for a window that is not our child?") );
78 m_winLastFocused
= win
;
82 wxLogTrace(_T("focus"), _T("Set last focus to %s(%s)"),
83 win
->GetClassInfo()->GetClassName(),
84 win
->GetLabel().c_str());
88 wxLogTrace(_T("focus"), _T("No more last focus"));
92 // ----------------------------------------------------------------------------
93 // Keyboard handling - this is the place where the TAB traversal logic is
94 // implemented. As this code is common to all ports, this ensures consistent
95 // behaviour even if we don't specify how exactly the wxNavigationKeyEvent are
96 // generated and this is done in platform specific code which also ensures that
97 // we can follow the given platform standards.
98 // ----------------------------------------------------------------------------
100 void wxControlContainer::HandleOnNavigationKey( wxNavigationKeyEvent
& event
)
102 wxWindow
*parent
= m_winParent
->GetParent();
104 // the event is propagated downwards if the event emitter was our parent
105 bool goingDown
= event
.GetEventObject() == parent
;
107 const wxWindowList
& children
= m_winParent
->GetChildren();
109 // there is not much to do if we don't have children and we're not
110 // interested in "notebook page change" events here
111 if ( !children
.GetCount() || event
.IsWindowChange() )
113 // let the parent process it unless it already comes from our parent
114 // of we don't have any
116 !parent
|| !parent
->GetEventHandler()->ProcessEvent(event
) )
124 // where are we going?
125 bool forward
= event
.GetDirection();
127 // the node of the children list from which we should start looking for the
128 // next acceptable child
129 wxWindowList::Node
*node
, *start_node
;
131 // we should start from the first/last control and not from the one which
132 // had focus the last time if we're propagating the event downwards because
133 // for our parent we look like a single control
136 // just to be sure it's not used (normally this is not necessary, but
137 // doesn't hurt neither)
138 m_winLastFocused
= (wxWindow
*)NULL
;
140 // start from first or last depending on where we're going
141 node
= forward
? children
.GetFirst() : children
.GetLast();
143 // we want to cycle over all nodes
144 start_node
= (wxWindowList::Node
*)NULL
;
148 // try to find the child which has the focus currently
150 // the event emitter might have done this for us
151 wxWindow
*winFocus
= event
.GetCurrentFocus();
153 // but if not, we might know where the focus was ourselves
155 winFocus
= m_winLastFocused
;
157 // if still no luck, do it the hard way
159 winFocus
= wxWindow::FindFocus();
163 // ok, we found the focus - now is it our child?
164 start_node
= children
.Find( winFocus
);
168 start_node
= (wxWindowList::Node
*)NULL
;
171 if ( !start_node
&& m_winLastFocused
)
173 // window which has focus isn't our child, fall back to the one
174 // which had the focus the last time
175 start_node
= children
.Find( m_winLastFocused
);
178 // if we still didn't find anything, we should start with the first one
181 start_node
= children
.GetFirst();
184 // and the first child which we can try setting focus to is the next or
186 node
= forward
? start_node
->GetNext() : start_node
->GetPrevious();
189 // we want to cycle over all elements passing by NULL
190 while ( node
!= start_node
)
192 // Have we come to the last or first item on the panel?
197 // Check if our (may be grand) parent is another panel: if this
198 // is the case, they will know what to do with this navigation
199 // key and so give them the chance to process it instead of
200 // looping inside this panel (normally, the focus will go to
201 // the next/previous item after this panel in the parent
203 wxWindow
*focussed_child_of_parent
= m_winParent
;
206 // we don't want to tab into a different dialog or frame
207 if ( focussed_child_of_parent
->IsTopLevel() )
210 event
.SetCurrentFocus( focussed_child_of_parent
);
211 if ( parent
->GetEventHandler()->ProcessEvent( event
) )
214 focussed_child_of_parent
= parent
;
216 parent
= parent
->GetParent();
219 //else: as the focus came from our parent, we definitely don't want
220 // to send it back to it!
222 // no, we are not inside another panel so process this ourself
223 node
= forward
? children
.GetFirst() : children
.GetLast();
228 wxWindow
*child
= node
->GetData();
230 if ( child
->AcceptsFocusFromKeyboard() )
232 // if we're setting the focus to a child panel we should prevent it
233 // from giving it to the child which had the focus the last time
234 // and instead give it to the first/last child depending from which
235 // direction we're coming
236 event
.SetEventObject(m_winParent
);
237 if ( !child
->GetEventHandler()->ProcessEvent(event
) )
239 // everything is simple: just give focus to it
240 child
->SetFocusFromKbd();
242 m_winLastFocused
= child
;
244 //else: the child manages its focus itself
251 node
= forward
? node
->GetNext() : node
->GetPrevious();
254 // we cycled through all of our children and none of them wanted to accept
259 void wxControlContainer::HandleOnWindowDestroy(wxWindowBase
*child
)
261 if ( child
== m_winLastFocused
)
262 m_winLastFocused
= NULL
;
264 if ( child
== m_winDefault
)
268 // ----------------------------------------------------------------------------
270 // ----------------------------------------------------------------------------
272 bool wxControlContainer::DoSetFocus()
274 wxLogTrace(_T("focus"), _T("SetFocus on wxPanel 0x%08x."),
275 m_winParent
->GetHandle());
277 // If the panel gets the focus *by way of getting it set directly*
278 // we move the focus to the first window that can get it.
280 // VZ: no, we set the focus to the last window too. I don't understand why
281 // should we make this distinction: if an app wants to set focus to
282 // some precise control, it may always do it directly, but if we don't
283 // use m_winLastFocused here, the focus won't be set correctly after a
284 // notebook page change nor after frame activation under MSW (it calls
287 // RR: yes, when I the tab key to navigate in a panel with some controls and
288 // a notebook and the focus jumps to the notebook (typically coming from
289 // a button at the top) the notebook should focus the first child in the
290 // current notebook page, not the last one which would otherwise get the
291 // focus if you used the tab key to navigate from the current notebook
292 // page to button at the bottom. See every page in the controls sample.
294 // VZ: ok, but this still doesn't (at least I don't see how it can) take
295 // care of first/last child problem: i.e. if Shift-TAB is pressed in a
296 // situation like above, the focus should be given to the last child,
297 // not the first one (and not to the last focused one neither) - I
298 // think my addition to OnNavigationKey() above takes care of it.
299 // Keeping #ifdef __WXGTK__ for now, but please try removing it and see
302 // RR: Removed for now. Let's see what happens..
304 // if our child already has focus, don't take it away from it
305 wxWindow
*win
= wxWindow::FindFocus();
308 if ( win
== m_winParent
)
311 if ( win
->IsTopLevel() )
313 // don't look beyond the first top level parent - useless and
318 win
= win
->GetParent();
321 return SetFocusToChild();
324 void wxControlContainer::HandleOnFocus(wxFocusEvent
& event
)
326 wxLogTrace(_T("focus"), _T("OnFocus on wxPanel 0x%08x, name: %s"),
327 m_winParent
->GetHandle(),
328 m_winParent
->GetName().c_str() );
330 // If we panel got the focus *by way of getting clicked on*
331 // we move the focus to either the last window that had the
332 // focus or the first one that can get it.
333 (void)SetFocusToChild();
338 bool wxControlContainer::SetFocusToChild()
340 return wxSetFocusToChild(m_winParent
, &m_winLastFocused
);
343 // ----------------------------------------------------------------------------
344 // SetFocusToChild(): this function is used by wxPanel but also by wxFrame in
345 // wxMSW, this is why it is outside of wxControlContainer class
346 // ----------------------------------------------------------------------------
348 bool wxSetFocusToChild(wxWindow
*win
, wxWindow
**childLastFocused
)
350 wxCHECK_MSG( win
, FALSE
, _T("wxSetFocusToChild(): invalid window") );
352 if ( *childLastFocused
)
354 // It might happen that the window got reparented or no longer accepts
356 if ( (*childLastFocused
)->GetParent() == win
&&
357 (*childLastFocused
)->AcceptsFocusFromKeyboard() )
359 wxLogTrace(_T("focus"),
360 _T("SetFocusToChild() => last child (0x%08x)."),
361 (*childLastFocused
)->GetHandle());
363 (*childLastFocused
)->SetFocusFromKbd();
368 // it doesn't count as such any more
369 *childLastFocused
= (wxWindow
*)NULL
;
373 // set the focus to the first child who wants it
374 wxWindowList::Node
*node
= win
->GetChildren().GetFirst();
377 wxWindow
*child
= node
->GetData();
379 if ( child
->AcceptsFocusFromKeyboard() && !child
->IsTopLevel() )
381 wxLogTrace(_T("focus"),
382 _T("SetFocusToChild() => first child (0x%08x)."),
385 *childLastFocused
= child
; // should be redundant, but it is not
386 child
->SetFocusFromKbd();
390 node
= node
->GetNext();