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