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