]> git.saurik.com Git - wxWidgets.git/blob - src/common/containr.cpp
c5133c803dd4003609508a69eb39518e369e6395
[wxWidgets.git] / src / common / containr.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/containr.cpp
3 // Purpose: implementation of wxControlContainer
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 06.08.01
7 // RCS-ID: $Id$
8 // Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // License: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "containr.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #endif //WX_PRECOMP
33
34 #include "wx/containr.h"
35
36 // ============================================================================
37 // implementation
38 // ============================================================================
39
40 wxControlContainer::wxControlContainer(wxWindow *winParent)
41 {
42 m_winParent = winParent;
43
44 m_winLastFocused =
45 m_winDefault = NULL;
46 }
47
48 void wxControlContainer::SetLastFocus(wxWindow *win)
49 {
50 // find the last _immediate_ child which got focus
51 while ( win )
52 {
53 wxWindow *parent = win->GetParent();
54 if ( parent == m_winParent )
55 break;
56
57 win = parent;
58 }
59
60 wxASSERT_MSG( win, _T("attempt to set last focus to not a child?") );
61
62 m_winLastFocused = win;
63 }
64
65 // ----------------------------------------------------------------------------
66 // Keyboard handling - this is the place where the TAB traversal logic is
67 // implemented. As this code is common to all ports, this ensures consistent
68 // behaviour even if we don't specify how exactly the wxNavigationKeyEvent are
69 // generated and this is done in platform specific code which also ensures that
70 // we can follow the given platform standards.
71 // ----------------------------------------------------------------------------
72
73 void wxControlContainer::HandleOnNavigationKey( wxNavigationKeyEvent& event )
74 {
75 wxWindow *parent = m_winParent->GetParent();
76
77 // the event is propagated downwards if the event emitter was our parent
78 bool goingDown = event.GetEventObject() == parent;
79
80 const wxWindowList& children = m_winParent->GetChildren();
81
82 // there is not much to do if we don't have children and we're not
83 // interested in "notebook page change" events here
84 if ( !children.GetCount() || event.IsWindowChange() )
85 {
86 // let the parent process it unless it already comes from our parent
87 // of we don't have any
88 if ( goingDown ||
89 !parent || !parent->GetEventHandler()->ProcessEvent(event) )
90 {
91 event.Skip();
92 }
93
94 return;
95 }
96
97 // where are we going?
98 bool forward = event.GetDirection();
99
100 // the node of the children list from which we should start looking for the
101 // next acceptable child
102 wxWindowList::Node *node, *start_node;
103
104 // we should start from the first/last control and not from the one which
105 // had focus the last time if we're propagating the event downwards because
106 // for our parent we look like a single control
107 if ( goingDown )
108 {
109 // just to be sure it's not used (normally this is not necessary, but
110 // doesn't hurt neither)
111 m_winLastFocused = (wxWindow *)NULL;
112
113 // start from first or last depending on where we're going
114 node = forward ? children.GetFirst() : children.GetLast();
115
116 // we want to cycle over all nodes
117 start_node = (wxWindowList::Node *)NULL;
118 }
119 else
120 {
121 // try to find the child which has the focus currently
122
123 // the event emitter might have done this for us
124 wxWindow *winFocus = event.GetCurrentFocus();
125
126 // but if not, we might know where the focus was ourselves
127 if (!winFocus)
128 winFocus = m_winLastFocused;
129
130 // if still no luck, do it the hard way
131 if (!winFocus)
132 winFocus = wxWindow::FindFocus();
133
134 if ( winFocus )
135 {
136 // ok, we found the focus - now is it our child?
137 start_node = children.Find( winFocus );
138 }
139 else
140 {
141 start_node = (wxWindowList::Node *)NULL;
142 }
143
144 if ( !start_node && m_winLastFocused )
145 {
146 // window which has focus isn't our child, fall back to the one
147 // which had the focus the last time
148 start_node = children.Find( m_winLastFocused );
149 }
150
151 // if we still didn't find anything, we should start with the first one
152 if ( !start_node )
153 {
154 start_node = children.GetFirst();
155 }
156
157 // and the first child which we can try setting focus to is the next or
158 // the previous one
159 node = forward ? start_node->GetNext() : start_node->GetPrevious();
160 }
161
162 // we want to cycle over all elements passing by NULL
163 while ( node != start_node )
164 {
165 // Have we come to the last or first item on the panel?
166 if ( !node )
167 {
168 if ( !goingDown )
169 {
170 // Check if our (may be grand) parent is another panel: if this
171 // is the case, they will know what to do with this navigation
172 // key and so give them the chance to process it instead of
173 // looping inside this panel (normally, the focus will go to
174 // the next/previous item after this panel in the parent
175 // panel).
176 wxWindow *focussed_child_of_parent = m_winParent;
177 while ( parent )
178 {
179 // we don't want to tab into a different dialog or frame
180 if ( focussed_child_of_parent->IsTopLevel() )
181 break;
182
183 event.SetCurrentFocus( focussed_child_of_parent );
184 if ( parent->GetEventHandler()->ProcessEvent( event ) )
185 return;
186
187 focussed_child_of_parent = parent;
188
189 parent = parent->GetParent();
190 }
191 }
192 //else: as the focus came from our parent, we definitely don't want
193 // to send it back to it!
194
195 // no, we are not inside another panel so process this ourself
196 node = forward ? children.GetFirst() : children.GetLast();
197
198 continue;
199 }
200
201 wxWindow *child = node->GetData();
202
203 if ( child->AcceptsFocusFromKeyboard() )
204 {
205 // if we're setting the focus to a child panel we should prevent it
206 // from giving it to the child which had the focus the last time
207 // and instead give it to the first/last child depending from which
208 // direction we're coming
209 event.SetEventObject(m_winParent);
210 if ( !child->GetEventHandler()->ProcessEvent(event) )
211 {
212 // everything is simple: just give focus to it
213 child->SetFocus();
214
215 m_winLastFocused = child;
216 }
217 //else: the child manages its focus itself
218
219 event.Skip( FALSE );
220
221 return;
222 }
223
224 node = forward ? node->GetNext() : node->GetPrevious();
225 }
226
227 // we cycled through all of our children and none of them wanted to accept
228 // focus
229 event.Skip();
230 }
231
232 void wxControlContainer::HandleOnWindowDestroy(wxWindowBase *child)
233 {
234 if ( child == m_winLastFocused )
235 m_winLastFocused = NULL;
236
237 if ( child == m_winDefault )
238 m_winDefault = NULL;
239 }
240
241 // ----------------------------------------------------------------------------
242 // focus handling
243 // ----------------------------------------------------------------------------
244
245 void wxControlContainer::DoSetFocus()
246 {
247 wxLogTrace(_T("focus"), _T("SetFocus on wxPanel 0x%08x."),
248 m_winParent->GetHandle());
249
250 // If the panel gets the focus *by way of getting it set directly*
251 // we move the focus to the first window that can get it.
252
253 // VZ: no, we set the focus to the last window too. I don't understand why
254 // should we make this distinction: if an app wants to set focus to
255 // some precise control, it may always do it directly, but if we don't
256 // use m_winLastFocused here, the focus won't be set correctly after a
257 // notebook page change nor after frame activation under MSW (it calls
258 // SetFocus too)
259 //
260 // RR: yes, when I the tab key to navigate in a panel with some controls and
261 // a notebook and the focus jumps to the notebook (typically coming from
262 // a button at the top) the notebook should focus the first child in the
263 // current notebook page, not the last one which would otherwise get the
264 // focus if you used the tab key to navigate from the current notebook
265 // page to button at the bottom. See every page in the controls sample.
266 //
267 // VZ: ok, but this still doesn't (at least I don't see how it can) take
268 // care of first/last child problem: i.e. if Shift-TAB is pressed in a
269 // situation like above, the focus should be given to the last child,
270 // not the first one (and not to the last focused one neither) - I
271 // think my addition to OnNavigationKey() above takes care of it.
272 // Keeping #ifdef __WXGTK__ for now, but please try removing it and see
273 // what happens.
274 //
275 // RR: Removed for now. Let's see what happens..
276
277 if ( !SetFocusToChild() )
278 {
279 m_winParent->SetFocus();
280 }
281 }
282
283 void wxControlContainer::HandleOnFocus(wxFocusEvent& event)
284 {
285 wxLogTrace(_T("focus"), _T("OnFocus on wxPanel 0x%08x, name: %s"),
286 m_winParent->GetHandle(),
287 m_winParent->GetName().c_str() );
288
289 // If we panel got the focus *by way of getting clicked on*
290 // we move the focus to either the last window that had the
291 // focus or the first one that can get it.
292 (void)SetFocusToChild();
293
294 event.Skip();
295 }
296
297 bool wxControlContainer::SetFocusToChild()
298 {
299 return wxSetFocusToChild(m_winParent, &m_winLastFocused);
300 }
301
302 // ----------------------------------------------------------------------------
303 // SetFocusToChild(): this function is used by wxPanel but also by wxFrame in
304 // wxMSW, this is why it is outside of wxControlContainer class
305 // ----------------------------------------------------------------------------
306
307 bool wxSetFocusToChild(wxWindow *win, wxWindow **childLastFocused)
308 {
309 wxCHECK_MSG( win, FALSE, _T("wxSetFocusToChild(): invalid window") );
310
311 if ( *childLastFocused )
312 {
313 // It might happen that the window got reparented or no longer accepts
314 // the focus.
315 if ( (*childLastFocused)->GetParent() == win &&
316 (*childLastFocused)->AcceptsFocusFromKeyboard() )
317 {
318 wxLogTrace(_T("focus"),
319 _T("SetFocusToChild() => last child (0x%08x)."),
320 (*childLastFocused)->GetHandle());
321
322 (*childLastFocused)->SetFocus();
323 return TRUE;
324 }
325 else
326 {
327 // it doesn't count as such any more
328 *childLastFocused = (wxWindow *)NULL;
329 }
330 }
331
332 // set the focus to the first child who wants it
333 wxWindowList::Node *node = win->GetChildren().GetFirst();
334 while ( node )
335 {
336 wxWindow *child = node->GetData();
337
338 if ( child->AcceptsFocusFromKeyboard() && !child->IsTopLevel() )
339 {
340 wxLogTrace(_T("focus"),
341 _T("SetFocusToChild() => first child (0x%08x)."),
342 child->GetHandle());
343
344 *childLastFocused = child; // should be redundant, but it is not
345 child->SetFocus();
346 return TRUE;
347 }
348
349 node = node->GetNext();
350 }
351
352 return FALSE;
353 }