]>
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
)
59 // if we're setting the focus
62 // find the last _immediate_ child which got focus
63 wxWindow
*winParent
= win
;
64 while ( winParent
!= m_winParent
)
67 winParent
= win
->GetParent();
69 // Yes, this can happen, though in a totally pathological case.
70 // like when detaching a menubar from a frame with a child
71 // which has pushed itself as an event handler for the menubar.
74 wxASSERT_MSG( winParent
,
75 _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 // propagate the last focus upwards so that our parent can set focus back
94 // to us if it loses it now and regains later
95 wxWindow
*parent
= m_winParent
->GetParent();
98 wxChildFocusEvent
eventFocus(m_winParent
);
99 parent
->GetEventHandler()->ProcessEvent(eventFocus
);
103 // ----------------------------------------------------------------------------
104 // Keyboard handling - this is the place where the TAB traversal logic is
105 // implemented. As this code is common to all ports, this ensures consistent
106 // behaviour even if we don't specify how exactly the wxNavigationKeyEvent are
107 // generated and this is done in platform specific code which also ensures that
108 // we can follow the given platform standards.
109 // ----------------------------------------------------------------------------
111 void wxControlContainer::HandleOnNavigationKey( wxNavigationKeyEvent
& event
)
113 wxWindow
*parent
= m_winParent
->GetParent();
115 // the event is propagated downwards if the event emitter was our parent
116 bool goingDown
= event
.GetEventObject() == parent
;
118 const wxWindowList
& children
= m_winParent
->GetChildren();
120 // there is not much to do if we don't have children and we're not
121 // interested in "notebook page change" events here
122 if ( !children
.GetCount() || event
.IsWindowChange() )
124 // let the parent process it unless it already comes from our parent
125 // of we don't have any
127 !parent
|| !parent
->GetEventHandler()->ProcessEvent(event
) )
135 // where are we going?
136 bool forward
= event
.GetDirection();
138 // the node of the children list from which we should start looking for the
139 // next acceptable child
140 wxWindowList::Node
*node
, *start_node
;
142 // we should start from the first/last control and not from the one which
143 // had focus the last time if we're propagating the event downwards because
144 // for our parent we look like a single control
147 // just to be sure it's not used (normally this is not necessary, but
148 // doesn't hurt neither)
149 m_winLastFocused
= (wxWindow
*)NULL
;
151 // start from first or last depending on where we're going
152 node
= forward
? children
.GetFirst() : children
.GetLast();
154 // we want to cycle over all nodes
155 start_node
= (wxWindowList::Node
*)NULL
;
159 // try to find the child which has the focus currently
161 // the event emitter might have done this for us
162 wxWindow
*winFocus
= event
.GetCurrentFocus();
164 // but if not, we might know where the focus was ourselves
166 winFocus
= m_winLastFocused
;
168 // if still no luck, do it the hard way
170 winFocus
= wxWindow::FindFocus();
174 // ok, we found the focus - now is it our child?
175 start_node
= children
.Find( winFocus
);
179 start_node
= (wxWindowList::Node
*)NULL
;
182 if ( !start_node
&& m_winLastFocused
)
184 // window which has focus isn't our child, fall back to the one
185 // which had the focus the last time
186 start_node
= children
.Find( m_winLastFocused
);
189 // if we still didn't find anything, we should start with the first one
192 start_node
= children
.GetFirst();
195 // and the first child which we can try setting focus to is the next or
197 node
= forward
? start_node
->GetNext() : start_node
->GetPrevious();
200 // we want to cycle over all elements passing by NULL
201 while ( node
!= start_node
)
203 // Have we come to the last or first item on the panel?
208 // Check if our (may be grand) parent is another panel: if this
209 // is the case, they will know what to do with this navigation
210 // key and so give them the chance to process it instead of
211 // looping inside this panel (normally, the focus will go to
212 // the next/previous item after this panel in the parent
214 wxWindow
*focussed_child_of_parent
= m_winParent
;
217 // we don't want to tab into a different dialog or frame
218 if ( focussed_child_of_parent
->IsTopLevel() )
221 event
.SetCurrentFocus( focussed_child_of_parent
);
222 if ( parent
->GetEventHandler()->ProcessEvent( event
) )
225 focussed_child_of_parent
= parent
;
227 parent
= parent
->GetParent();
230 //else: as the focus came from our parent, we definitely don't want
231 // to send it back to it!
233 // no, we are not inside another panel so process this ourself
234 node
= forward
? children
.GetFirst() : children
.GetLast();
239 wxWindow
*child
= node
->GetData();
241 if ( child
->AcceptsFocusFromKeyboard() )
243 // if we're setting the focus to a child panel we should prevent it
244 // from giving it to the child which had the focus the last time
245 // and instead give it to the first/last child depending from which
246 // direction we're coming
247 event
.SetEventObject(m_winParent
);
248 if ( !child
->GetEventHandler()->ProcessEvent(event
) )
250 // everything is simple: just give focus to it
251 child
->SetFocusFromKbd();
253 m_winLastFocused
= child
;
255 //else: the child manages its focus itself
262 node
= forward
? node
->GetNext() : node
->GetPrevious();
265 // we cycled through all of our children and none of them wanted to accept
270 void wxControlContainer::HandleOnWindowDestroy(wxWindowBase
*child
)
272 if ( child
== m_winLastFocused
)
273 m_winLastFocused
= NULL
;
275 if ( child
== m_winDefault
)
278 if ( child
== m_winTmpDefault
)
279 m_winTmpDefault
= NULL
;
282 // ----------------------------------------------------------------------------
284 // ----------------------------------------------------------------------------
286 bool wxControlContainer::DoSetFocus()
288 wxLogTrace(_T("focus"), _T("SetFocus on wxPanel 0x%08x."),
289 m_winParent
->GetHandle());
291 // If the panel gets the focus *by way of getting it set directly*
292 // we move the focus to the first window that can get it.
294 // VZ: no, we set the focus to the last window too. I don't understand why
295 // should we make this distinction: if an app wants to set focus to
296 // some precise control, it may always do it directly, but if we don't
297 // use m_winLastFocused here, the focus won't be set correctly after a
298 // notebook page change nor after frame activation under MSW (it calls
301 // RR: yes, when I the tab key to navigate in a panel with some controls and
302 // a notebook and the focus jumps to the notebook (typically coming from
303 // a button at the top) the notebook should focus the first child in the
304 // current notebook page, not the last one which would otherwise get the
305 // focus if you used the tab key to navigate from the current notebook
306 // page to button at the bottom. See every page in the controls sample.
308 // VZ: ok, but this still doesn't (at least I don't see how it can) take
309 // care of first/last child problem: i.e. if Shift-TAB is pressed in a
310 // situation like above, the focus should be given to the last child,
311 // not the first one (and not to the last focused one neither) - I
312 // think my addition to OnNavigationKey() above takes care of it.
313 // Keeping #ifdef __WXGTK__ for now, but please try removing it and see
316 // RR: Removed for now. Let's see what happens..
318 // if our child already has focus, don't take it away from it
319 wxWindow
*win
= wxWindow::FindFocus();
322 if ( win
== m_winParent
)
325 if ( win
->IsTopLevel() )
327 // don't look beyond the first top level parent - useless and
332 win
= win
->GetParent();
335 return SetFocusToChild();
338 void wxControlContainer::HandleOnFocus(wxFocusEvent
& event
)
340 wxLogTrace(_T("focus"), _T("OnFocus on wxPanel 0x%08x, name: %s"),
341 m_winParent
->GetHandle(),
342 m_winParent
->GetName().c_str() );
344 // If we panel got the focus *by way of getting clicked on*
345 // we move the focus to either the last window that had the
346 // focus or the first one that can get it.
347 (void)SetFocusToChild();
352 bool wxControlContainer::SetFocusToChild()
354 return wxSetFocusToChild(m_winParent
, &m_winLastFocused
);
357 // ----------------------------------------------------------------------------
358 // SetFocusToChild(): this function is used by wxPanel but also by wxFrame in
359 // wxMSW, this is why it is outside of wxControlContainer class
360 // ----------------------------------------------------------------------------
362 bool wxSetFocusToChild(wxWindow
*win
, wxWindow
**childLastFocused
)
364 wxCHECK_MSG( win
, FALSE
, _T("wxSetFocusToChild(): invalid window") );
365 wxCHECK_MSG( childLastFocused
, FALSE
,
366 _T("wxSetFocusToChild(): NULL child poonter") );
368 if ( *childLastFocused
)
370 // It might happen that the window got reparented
371 if ( (*childLastFocused
)->GetParent() == win
)
373 wxLogTrace(_T("focus"),
374 _T("SetFocusToChild() => last child (0x%08x)."),
375 (*childLastFocused
)->GetHandle());
377 // not SetFocusFromKbd(): we're restoring focus back to the old
378 // window and not setting it as the result of a kbd action
379 (*childLastFocused
)->SetFocus();
384 // it doesn't count as such any more
385 *childLastFocused
= (wxWindow
*)NULL
;
389 // set the focus to the first child who wants it
390 wxWindowList::Node
*node
= win
->GetChildren().GetFirst();
393 wxWindow
*child
= node
->GetData();
395 if ( child
->AcceptsFocusFromKeyboard() && !child
->IsTopLevel() )
397 wxLogTrace(_T("focus"),
398 _T("SetFocusToChild() => first child (0x%08x)."),
401 *childLastFocused
= child
; // should be redundant, but it is not
402 child
->SetFocusFromKbd();
406 node
= node
->GetNext();