]>
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 // set it first in case SetFocusFromKbd() results in focus
252 m_winLastFocused
= child
;
254 // everything is simple: just give focus to it
255 child
->SetFocusFromKbd();
257 //else: the child manages its focus itself
264 node
= forward
? node
->GetNext() : node
->GetPrevious();
267 // we cycled through all of our children and none of them wanted to accept
272 void wxControlContainer::HandleOnWindowDestroy(wxWindowBase
*child
)
274 if ( child
== m_winLastFocused
)
275 m_winLastFocused
= NULL
;
277 if ( child
== m_winDefault
)
280 if ( child
== m_winTmpDefault
)
281 m_winTmpDefault
= NULL
;
284 // ----------------------------------------------------------------------------
286 // ----------------------------------------------------------------------------
288 bool wxControlContainer::DoSetFocus()
290 wxLogTrace(_T("focus"), _T("SetFocus on wxPanel 0x%08lx."),
291 (unsigned long)m_winParent
->GetHandle());
293 // when the panel gets the focus we move the focus to either the last
294 // window that had the focus or the first one that can get it unless the
295 // focus had been already set to some other child
297 wxWindow
*win
= wxWindow::FindFocus();
300 if ( win
== m_winParent
)
302 // our child already has focus, don't take it away from it
306 if ( win
->IsTopLevel() )
308 // don't look beyond the first top level parent - useless and
313 win
= win
->GetParent();
316 return SetFocusToChild();
319 void wxControlContainer::HandleOnFocus(wxFocusEvent
& event
)
321 wxLogTrace(_T("focus"), _T("OnFocus on wxPanel 0x%08lx, name: %s"),
322 (unsigned long)m_winParent
->GetHandle(),
323 m_winParent
->GetName().c_str() );
330 bool wxControlContainer::SetFocusToChild()
332 return wxSetFocusToChild(m_winParent
, &m_winLastFocused
);
335 // ----------------------------------------------------------------------------
336 // SetFocusToChild(): this function is used by wxPanel but also by wxFrame in
337 // wxMSW, this is why it is outside of wxControlContainer class
338 // ----------------------------------------------------------------------------
340 bool wxSetFocusToChild(wxWindow
*win
, wxWindow
**childLastFocused
)
342 wxCHECK_MSG( win
, FALSE
, _T("wxSetFocusToChild(): invalid window") );
343 wxCHECK_MSG( childLastFocused
, FALSE
,
344 _T("wxSetFocusToChild(): NULL child poonter") );
346 if ( *childLastFocused
)
348 // It might happen that the window got reparented
349 if ( (*childLastFocused
)->GetParent() == win
)
351 wxLogTrace(_T("focus"),
352 _T("SetFocusToChild() => last child (0x%08lx)."),
353 (unsigned long)(*childLastFocused
)->GetHandle());
355 // not SetFocusFromKbd(): we're restoring focus back to the old
356 // window and not setting it as the result of a kbd action
357 (*childLastFocused
)->SetFocus();
362 // it doesn't count as such any more
363 *childLastFocused
= (wxWindow
*)NULL
;
367 // set the focus to the first child who wants it
368 wxWindowList::Node
*node
= win
->GetChildren().GetFirst();
371 wxWindow
*child
= node
->GetData();
373 if ( child
->AcceptsFocusFromKeyboard() && !child
->IsTopLevel() )
375 wxLogTrace(_T("focus"),
376 _T("SetFocusToChild() => first child (0x%08lx)."),
377 (unsigned long)child
->GetHandle());
379 *childLastFocused
= child
; // should be redundant, but it is not
380 child
->SetFocusFromKbd();
384 node
= node
->GetNext();