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