]>
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> | |
65571936 | 9 | // License: wxWindows licence |
456bc6d9 VZ |
10 | /////////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
456bc6d9 VZ |
20 | // For compilers that support precompilation, includes "wx.h". |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
de160b06 VZ |
27 | #ifndef WX_PRECOMP |
28 | #include "wx/containr.h" | |
29 | #endif | |
30 | ||
31 | #ifndef wxHAS_NATIVE_TAB_TRAVERSAL | |
32 | ||
456bc6d9 | 33 | #ifndef WX_PRECOMP |
6285be72 VZ |
34 | #include "wx/log.h" |
35 | #include "wx/event.h" | |
36 | #include "wx/window.h" | |
851dee09 | 37 | #include "wx/scrolbar.h" |
b0b5881a | 38 | #include "wx/radiobut.h" |
456bc6d9 VZ |
39 | #endif //WX_PRECOMP |
40 | ||
9f542367 VZ |
41 | // trace mask for focus messages |
42 | #define TRACE_FOCUS _T("focus") | |
43 | ||
456bc6d9 VZ |
44 | // ============================================================================ |
45 | // implementation | |
46 | // ============================================================================ | |
47 | ||
48 | wxControlContainer::wxControlContainer(wxWindow *winParent) | |
49 | { | |
50 | m_winParent = winParent; | |
6c20e8f8 | 51 | m_winLastFocused = NULL; |
b33f7651 | 52 | m_inSetFocus = false; |
456bc6d9 VZ |
53 | } |
54 | ||
3251b834 VZ |
55 | bool wxControlContainer::AcceptsFocus() const |
56 | { | |
de160b06 VZ |
57 | // we can accept focus either if we have no children at all (in this case |
58 | // we're probably not used as a container) or only when at least one child | |
59 | // accepts focus | |
60 | wxWindowList::compatibility_iterator node = m_winParent->GetChildren().GetFirst(); | |
61 | if ( !node ) | |
62 | return true; | |
a7407834 | 63 | |
2f64c3bb | 64 | #ifdef __WXMAC__ |
de160b06 VZ |
65 | // wxMac has eventually the two scrollbars as children, they don't count |
66 | // as real children in the algorithm mentioned above | |
67 | bool hasRealChildren = false ; | |
2f64c3bb | 68 | #endif |
c9d59ee7 | 69 | |
de160b06 VZ |
70 | while ( node ) |
71 | { | |
72 | wxWindow *child = node->GetData(); | |
73 | node = node->GetNext(); | |
3251b834 | 74 | |
2f64c3bb | 75 | #ifdef __WXMAC__ |
de160b06 VZ |
76 | if ( m_winParent->MacIsWindowScrollbar( child ) ) |
77 | continue; | |
78 | hasRealChildren = true ; | |
2f64c3bb | 79 | #endif |
de160b06 VZ |
80 | if ( child->CanAcceptFocus() ) |
81 | { | |
82 | return true; | |
3251b834 | 83 | } |
de160b06 | 84 | } |
c9d59ee7 | 85 | |
2f64c3bb | 86 | #ifdef __WXMAC__ |
de160b06 VZ |
87 | if ( !hasRealChildren ) |
88 | return true ; | |
2f64c3bb | 89 | #endif |
3251b834 | 90 | |
c9d59ee7 | 91 | return false; |
3251b834 VZ |
92 | } |
93 | ||
456bc6d9 VZ |
94 | void wxControlContainer::SetLastFocus(wxWindow *win) |
95 | { | |
6aeb6f2a VZ |
96 | // the panel itself should never get the focus at all but if it does happen |
97 | // temporarily (as it seems to do under wxGTK), at the very least don't | |
98 | // forget our previous m_winLastFocused | |
c25b5d1f | 99 | if ( win != m_winParent ) |
456bc6d9 | 100 | { |
c25b5d1f VZ |
101 | // if we're setting the focus |
102 | if ( win ) | |
83c865f5 | 103 | { |
c25b5d1f VZ |
104 | // find the last _immediate_ child which got focus |
105 | wxWindow *winParent = win; | |
106 | while ( winParent != m_winParent ) | |
107 | { | |
108 | win = winParent; | |
109 | winParent = win->GetParent(); | |
83c865f5 | 110 | |
c25b5d1f VZ |
111 | // Yes, this can happen, though in a totally pathological case. |
112 | // like when detaching a menubar from a frame with a child | |
113 | // which has pushed itself as an event handler for the menubar. | |
114 | // (under wxGTK) | |
6f8239de | 115 | |
c25b5d1f VZ |
116 | wxASSERT_MSG( winParent, |
117 | _T("Setting last focus for a window that is not our child?") ); | |
118 | } | |
6f8239de | 119 | } |
456bc6d9 | 120 | |
c25b5d1f | 121 | m_winLastFocused = win; |
6aeb6f2a | 122 | |
c25b5d1f VZ |
123 | if ( win ) |
124 | { | |
9f542367 | 125 | wxLogTrace(TRACE_FOCUS, _T("Set last focus to %s(%s)"), |
c25b5d1f VZ |
126 | win->GetClassInfo()->GetClassName(), |
127 | win->GetLabel().c_str()); | |
128 | } | |
129 | else | |
130 | { | |
9f542367 | 131 | wxLogTrace(TRACE_FOCUS, _T("No more last focus")); |
c25b5d1f | 132 | } |
6aeb6f2a | 133 | } |
c25b5d1f VZ |
134 | |
135 | // propagate the last focus upwards so that our parent can set focus back | |
136 | // to us if it loses it now and regains later | |
137 | wxWindow *parent = m_winParent->GetParent(); | |
138 | if ( parent ) | |
6aeb6f2a | 139 | { |
c25b5d1f VZ |
140 | wxChildFocusEvent eventFocus(m_winParent); |
141 | parent->GetEventHandler()->ProcessEvent(eventFocus); | |
6aeb6f2a | 142 | } |
456bc6d9 VZ |
143 | } |
144 | ||
7ff1b620 | 145 | // -------------------------------------------------------------------- |
b49f58fe | 146 | // The following four functions are used to find other radio buttons |
7ff1b620 VZ |
147 | // within the same group. Used by wxSetFocusToChild on wxMSW |
148 | // -------------------------------------------------------------------- | |
149 | ||
150 | #ifdef __WXMSW__ | |
151 | ||
152 | wxRadioButton* wxGetPreviousButtonInGroup(wxRadioButton *btn) | |
153 | { | |
154 | if ( btn->HasFlag(wxRB_GROUP) || btn->HasFlag(wxRB_SINGLE) ) | |
155 | return NULL; | |
156 | ||
157 | const wxWindowList& siblings = btn->GetParent()->GetChildren(); | |
158 | wxWindowList::compatibility_iterator nodeThis = siblings.Find(btn); | |
159 | wxCHECK_MSG( nodeThis, NULL, _T("radio button not a child of its parent?") ); | |
160 | ||
161 | // Iterate over all previous siblings until we find the next radio button | |
162 | wxWindowList::compatibility_iterator nodeBefore = nodeThis->GetPrevious(); | |
163 | wxRadioButton *prevBtn = 0; | |
164 | while (nodeBefore) | |
165 | { | |
166 | prevBtn = wxDynamicCast(nodeBefore->GetData(), wxRadioButton); | |
167 | if (prevBtn) | |
168 | break; | |
169 | ||
170 | nodeBefore = nodeBefore->GetPrevious(); | |
171 | } | |
b49f58fe | 172 | |
7ff1b620 VZ |
173 | if (!prevBtn || prevBtn->HasFlag(wxRB_SINGLE)) |
174 | { | |
175 | // no more buttons in group | |
176 | return NULL; | |
177 | } | |
3d3afaec JS |
178 | |
179 | return prevBtn; | |
7ff1b620 VZ |
180 | } |
181 | ||
182 | wxRadioButton* wxGetNextButtonInGroup(wxRadioButton *btn) | |
183 | { | |
184 | if (btn->HasFlag(wxRB_SINGLE)) | |
185 | return NULL; | |
186 | ||
187 | const wxWindowList& siblings = btn->GetParent()->GetChildren(); | |
188 | wxWindowList::compatibility_iterator nodeThis = siblings.Find(btn); | |
189 | wxCHECK_MSG( nodeThis, NULL, _T("radio button not a child of its parent?") ); | |
190 | ||
191 | // Iterate over all previous siblings until we find the next radio button | |
192 | wxWindowList::compatibility_iterator nodeNext = nodeThis->GetNext(); | |
193 | wxRadioButton *nextBtn = 0; | |
194 | while (nodeNext) | |
195 | { | |
196 | nextBtn = wxDynamicCast(nodeNext->GetData(), wxRadioButton); | |
197 | if (nextBtn) | |
198 | break; | |
199 | ||
200 | nodeNext = nodeNext->GetNext(); | |
201 | } | |
202 | ||
203 | if ( !nextBtn || nextBtn->HasFlag(wxRB_GROUP) || nextBtn->HasFlag(wxRB_SINGLE) ) | |
204 | { | |
205 | // no more buttons or the first button of the next group | |
206 | return NULL; | |
207 | } | |
3d3afaec JS |
208 | |
209 | return nextBtn; | |
7ff1b620 VZ |
210 | } |
211 | ||
212 | wxRadioButton* wxGetFirstButtonInGroup(wxRadioButton *btn) | |
213 | { | |
214 | while (true) | |
215 | { | |
216 | wxRadioButton* prevBtn = wxGetPreviousButtonInGroup(btn); | |
217 | if (!prevBtn) | |
218 | return btn; | |
b49f58fe | 219 | |
7ff1b620 VZ |
220 | btn = prevBtn; |
221 | } | |
222 | } | |
223 | ||
3d3afaec JS |
224 | wxRadioButton* wxGetLastButtonInGroup(wxRadioButton *btn) |
225 | { | |
226 | while (true) | |
227 | { | |
228 | wxRadioButton* nextBtn = wxGetNextButtonInGroup(btn); | |
229 | if (!nextBtn) | |
230 | return btn; | |
231 | ||
232 | btn = nextBtn; | |
233 | } | |
234 | } | |
235 | ||
7ff1b620 VZ |
236 | wxRadioButton* wxGetSelectedButtonInGroup(wxRadioButton *btn) |
237 | { | |
238 | // Find currently selected button | |
239 | if (btn->GetValue()) | |
240 | return btn; | |
241 | ||
242 | if (btn->HasFlag(wxRB_SINGLE)) | |
243 | return NULL; | |
244 | ||
245 | wxRadioButton *selBtn; | |
246 | ||
247 | // First check all previous buttons | |
248 | for (selBtn = wxGetPreviousButtonInGroup(btn); selBtn; selBtn = wxGetPreviousButtonInGroup(selBtn)) | |
249 | if (selBtn->GetValue()) | |
250 | return selBtn; | |
251 | ||
252 | // Now all following buttons | |
253 | for (selBtn = wxGetNextButtonInGroup(btn); selBtn; selBtn = wxGetNextButtonInGroup(selBtn)) | |
254 | if (selBtn->GetValue()) | |
255 | return selBtn; | |
256 | ||
257 | return NULL; | |
258 | } | |
259 | ||
b49f58fe | 260 | #endif // __WXMSW__ |
7ff1b620 | 261 | |
456bc6d9 VZ |
262 | // ---------------------------------------------------------------------------- |
263 | // Keyboard handling - this is the place where the TAB traversal logic is | |
264 | // implemented. As this code is common to all ports, this ensures consistent | |
265 | // behaviour even if we don't specify how exactly the wxNavigationKeyEvent are | |
266 | // generated and this is done in platform specific code which also ensures that | |
267 | // we can follow the given platform standards. | |
268 | // ---------------------------------------------------------------------------- | |
269 | ||
270 | void wxControlContainer::HandleOnNavigationKey( wxNavigationKeyEvent& event ) | |
271 | { | |
272 | wxWindow *parent = m_winParent->GetParent(); | |
273 | ||
274 | // the event is propagated downwards if the event emitter was our parent | |
275 | bool goingDown = event.GetEventObject() == parent; | |
276 | ||
277 | const wxWindowList& children = m_winParent->GetChildren(); | |
278 | ||
f2426531 VZ |
279 | // if we have exactly one notebook-like child window (actually it could be |
280 | // any window that returns true from its HasMultiplePages()), then | |
281 | // [Shift-]Ctrl-Tab and Ctrl-PageUp/Down keys should iterate over its pages | |
282 | // even if the focus is outside of the control because this is how the | |
283 | // standard MSW properties dialogs behave and we do it under other platforms | |
284 | // as well because it seems like a good idea -- but we can always put this | |
285 | // block inside "#ifdef __WXMSW__" if it's not suitable there | |
286 | if ( event.IsWindowChange() && !goingDown ) | |
287 | { | |
288 | // check if we have a unique notebook-like child | |
289 | wxWindow *bookctrl = NULL; | |
290 | for ( wxWindowList::const_iterator i = children.begin(), | |
291 | end = children.end(); | |
292 | i != end; | |
293 | ++i ) | |
294 | { | |
295 | wxWindow * const window = *i; | |
296 | if ( window->HasMultiplePages() ) | |
297 | { | |
298 | if ( bookctrl ) | |
299 | { | |
300 | // this is the second book-like control already so don't do | |
301 | // anything as we don't know which one should have its page | |
302 | // changed | |
303 | bookctrl = NULL; | |
304 | break; | |
305 | } | |
306 | ||
307 | bookctrl = window; | |
308 | } | |
309 | } | |
310 | ||
5f28de16 VZ |
311 | if ( bookctrl ) |
312 | { | |
313 | // make sure that we don't bubble up the event again from the book | |
314 | // control resulting in infinite recursion | |
315 | wxNavigationKeyEvent eventCopy(event); | |
316 | eventCopy.SetEventObject(m_winParent); | |
317 | if ( bookctrl->GetEventHandler()->ProcessEvent(eventCopy) ) | |
318 | return; | |
319 | } | |
f2426531 VZ |
320 | } |
321 | ||
456bc6d9 VZ |
322 | // there is not much to do if we don't have children and we're not |
323 | // interested in "notebook page change" events here | |
324 | if ( !children.GetCount() || event.IsWindowChange() ) | |
325 | { | |
326 | // let the parent process it unless it already comes from our parent | |
327 | // of we don't have any | |
328 | if ( goingDown || | |
329 | !parent || !parent->GetEventHandler()->ProcessEvent(event) ) | |
330 | { | |
331 | event.Skip(); | |
332 | } | |
333 | ||
334 | return; | |
335 | } | |
336 | ||
337 | // where are we going? | |
edc0a395 | 338 | const bool forward = event.GetDirection(); |
456bc6d9 VZ |
339 | |
340 | // the node of the children list from which we should start looking for the | |
341 | // next acceptable child | |
222ed1d6 | 342 | wxWindowList::compatibility_iterator node, start_node; |
456bc6d9 VZ |
343 | |
344 | // we should start from the first/last control and not from the one which | |
345 | // had focus the last time if we're propagating the event downwards because | |
346 | // for our parent we look like a single control | |
347 | if ( goingDown ) | |
348 | { | |
349 | // just to be sure it's not used (normally this is not necessary, but | |
350 | // doesn't hurt neither) | |
351 | m_winLastFocused = (wxWindow *)NULL; | |
352 | ||
353 | // start from first or last depending on where we're going | |
354 | node = forward ? children.GetFirst() : children.GetLast(); | |
456bc6d9 | 355 | } |
edc0a395 | 356 | else // going up |
456bc6d9 VZ |
357 | { |
358 | // try to find the child which has the focus currently | |
359 | ||
360 | // the event emitter might have done this for us | |
361 | wxWindow *winFocus = event.GetCurrentFocus(); | |
362 | ||
363 | // but if not, we might know where the focus was ourselves | |
364 | if (!winFocus) | |
365 | winFocus = m_winLastFocused; | |
366 | ||
367 | // if still no luck, do it the hard way | |
368 | if (!winFocus) | |
369 | winFocus = wxWindow::FindFocus(); | |
370 | ||
371 | if ( winFocus ) | |
372 | { | |
7ff1b620 | 373 | #ifdef __WXMSW__ |
b49f58fe | 374 | // If we are in a radio button group, start from the first item in the |
7ff1b620 VZ |
375 | // group |
376 | if ( event.IsFromTab() && wxIsKindOf(winFocus, wxRadioButton ) ) | |
377 | winFocus = wxGetFirstButtonInGroup((wxRadioButton*)winFocus); | |
378 | #endif | |
456bc6d9 VZ |
379 | // ok, we found the focus - now is it our child? |
380 | start_node = children.Find( winFocus ); | |
381 | } | |
456bc6d9 VZ |
382 | |
383 | if ( !start_node && m_winLastFocused ) | |
384 | { | |
385 | // window which has focus isn't our child, fall back to the one | |
386 | // which had the focus the last time | |
387 | start_node = children.Find( m_winLastFocused ); | |
388 | } | |
389 | ||
390 | // if we still didn't find anything, we should start with the first one | |
391 | if ( !start_node ) | |
392 | { | |
393 | start_node = children.GetFirst(); | |
394 | } | |
395 | ||
396 | // and the first child which we can try setting focus to is the next or | |
397 | // the previous one | |
398 | node = forward ? start_node->GetNext() : start_node->GetPrevious(); | |
399 | } | |
400 | ||
401 | // we want to cycle over all elements passing by NULL | |
edc0a395 | 402 | for ( ;; ) |
456bc6d9 | 403 | { |
edc0a395 | 404 | // don't go into infinite loop |
aa78d22e | 405 | if ( start_node && node && node == start_node ) |
edc0a395 VZ |
406 | break; |
407 | ||
456bc6d9 VZ |
408 | // Have we come to the last or first item on the panel? |
409 | if ( !node ) | |
410 | { | |
e547f7a7 VZ |
411 | if ( !start_node ) |
412 | { | |
413 | // exit now as otherwise we'd loop forever | |
414 | break; | |
415 | } | |
416 | ||
456bc6d9 VZ |
417 | if ( !goingDown ) |
418 | { | |
edc0a395 | 419 | // Check if our (maybe grand) parent is another panel: if this |
456bc6d9 VZ |
420 | // is the case, they will know what to do with this navigation |
421 | // key and so give them the chance to process it instead of | |
422 | // looping inside this panel (normally, the focus will go to | |
423 | // the next/previous item after this panel in the parent | |
424 | // panel). | |
425 | wxWindow *focussed_child_of_parent = m_winParent; | |
426 | while ( parent ) | |
427 | { | |
428 | // we don't want to tab into a different dialog or frame | |
429 | if ( focussed_child_of_parent->IsTopLevel() ) | |
430 | break; | |
431 | ||
432 | event.SetCurrentFocus( focussed_child_of_parent ); | |
433 | if ( parent->GetEventHandler()->ProcessEvent( event ) ) | |
434 | return; | |
435 | ||
436 | focussed_child_of_parent = parent; | |
437 | ||
438 | parent = parent->GetParent(); | |
439 | } | |
440 | } | |
441 | //else: as the focus came from our parent, we definitely don't want | |
442 | // to send it back to it! | |
443 | ||
444 | // no, we are not inside another panel so process this ourself | |
445 | node = forward ? children.GetFirst() : children.GetLast(); | |
446 | ||
447 | continue; | |
448 | } | |
449 | ||
450 | wxWindow *child = node->GetData(); | |
451 | ||
7ff1b620 | 452 | #ifdef __WXMSW__ |
3d3afaec | 453 | if ( event.IsFromTab() ) |
7ff1b620 | 454 | { |
3d3afaec | 455 | if ( wxIsKindOf(child, wxRadioButton) ) |
7ff1b620 | 456 | { |
3d3afaec JS |
457 | // only radio buttons with either wxRB_GROUP or wxRB_SINGLE |
458 | // can be tabbed to | |
459 | if ( child->HasFlag(wxRB_GROUP) ) | |
7ff1b620 | 460 | { |
3d3afaec JS |
461 | // need to tab into the active button within a group |
462 | wxRadioButton *rb = wxGetSelectedButtonInGroup((wxRadioButton*)child); | |
463 | if ( rb ) | |
464 | child = rb; | |
465 | } | |
466 | else if ( !child->HasFlag(wxRB_SINGLE) ) | |
467 | { | |
468 | node = forward ? node->GetNext() : node->GetPrevious(); | |
469 | continue; | |
7ff1b620 VZ |
470 | } |
471 | } | |
472 | } | |
3d3afaec JS |
473 | else if ( m_winLastFocused && |
474 | wxIsKindOf(m_winLastFocused, wxRadioButton) && | |
475 | !m_winLastFocused->HasFlag(wxRB_SINGLE) ) | |
7ff1b620 | 476 | { |
3d3afaec JS |
477 | // cursor keys don't navigate out of a radio button group so |
478 | // find the correct radio button to focus | |
479 | if ( forward ) | |
7ff1b620 | 480 | { |
3d3afaec JS |
481 | child = wxGetNextButtonInGroup((wxRadioButton*)m_winLastFocused); |
482 | if ( !child ) | |
483 | { | |
484 | // no next button in group, set it to the first button | |
485 | child = wxGetFirstButtonInGroup((wxRadioButton*)m_winLastFocused); | |
486 | } | |
487 | } | |
488 | else | |
489 | { | |
490 | child = wxGetPreviousButtonInGroup((wxRadioButton*)m_winLastFocused); | |
491 | if ( !child ) | |
492 | { | |
493 | // no previous button in group, set it to the last button | |
494 | child = wxGetLastButtonInGroup((wxRadioButton*)m_winLastFocused); | |
495 | } | |
496 | } | |
497 | ||
498 | if ( child == m_winLastFocused ) | |
499 | { | |
500 | // must be a group consisting of only one button therefore | |
501 | // no need to send a navigation event | |
502 | event.Skip(false); | |
503 | return; | |
7ff1b620 VZ |
504 | } |
505 | } | |
3d3afaec | 506 | #endif // __WXMSW__ |
c932709d | 507 | |
de160b06 | 508 | if ( child->CanAcceptFocusFromKeyboard() ) |
456bc6d9 VZ |
509 | { |
510 | // if we're setting the focus to a child panel we should prevent it | |
511 | // from giving it to the child which had the focus the last time | |
512 | // and instead give it to the first/last child depending from which | |
513 | // direction we're coming | |
514 | event.SetEventObject(m_winParent); | |
944e8709 | 515 | |
aef35d0e VZ |
516 | // disable propagation for this call as otherwise the event might |
517 | // bounce back to us. | |
518 | wxPropagationDisabler disableProp(event); | |
456bc6d9 VZ |
519 | if ( !child->GetEventHandler()->ProcessEvent(event) ) |
520 | { | |
2b5f62a0 VZ |
521 | // set it first in case SetFocusFromKbd() results in focus |
522 | // change too | |
523 | m_winLastFocused = child; | |
524 | ||
456bc6d9 | 525 | // everything is simple: just give focus to it |
5463c0a4 | 526 | child->SetFocusFromKbd(); |
456bc6d9 VZ |
527 | } |
528 | //else: the child manages its focus itself | |
529 | ||
c9d59ee7 | 530 | event.Skip( false ); |
456bc6d9 VZ |
531 | |
532 | return; | |
533 | } | |
534 | ||
535 | node = forward ? node->GetNext() : node->GetPrevious(); | |
536 | } | |
537 | ||
538 | // we cycled through all of our children and none of them wanted to accept | |
539 | // focus | |
540 | event.Skip(); | |
541 | } | |
542 | ||
543 | void wxControlContainer::HandleOnWindowDestroy(wxWindowBase *child) | |
544 | { | |
545 | if ( child == m_winLastFocused ) | |
546 | m_winLastFocused = NULL; | |
456bc6d9 VZ |
547 | } |
548 | ||
549 | // ---------------------------------------------------------------------------- | |
550 | // focus handling | |
551 | // ---------------------------------------------------------------------------- | |
552 | ||
24a7a198 | 553 | bool wxControlContainer::DoSetFocus() |
456bc6d9 | 554 | { |
9f542367 VZ |
555 | wxLogTrace(TRACE_FOCUS, _T("SetFocus on wxPanel 0x%p."), |
556 | m_winParent->GetHandle()); | |
456bc6d9 | 557 | |
b33f7651 JS |
558 | if (m_inSetFocus) |
559 | return true; | |
c9d59ee7 | 560 | |
2b5f62a0 VZ |
561 | // when the panel gets the focus we move the focus to either the last |
562 | // window that had the focus or the first one that can get it unless the | |
563 | // focus had been already set to some other child | |
564 | ||
0dcdeee9 VZ |
565 | wxWindow *win = wxWindow::FindFocus(); |
566 | while ( win ) | |
567 | { | |
568 | if ( win == m_winParent ) | |
2b5f62a0 VZ |
569 | { |
570 | // our child already has focus, don't take it away from it | |
1c9e321b | 571 | return true; |
2b5f62a0 | 572 | } |
0dcdeee9 VZ |
573 | |
574 | if ( win->IsTopLevel() ) | |
575 | { | |
576 | // don't look beyond the first top level parent - useless and | |
577 | // unnecessary | |
578 | break; | |
579 | } | |
580 | ||
581 | win = win->GetParent(); | |
582 | } | |
c9d59ee7 | 583 | |
2e07bdda VS |
584 | // protect against infinite recursion: |
585 | m_inSetFocus = true; | |
0dcdeee9 | 586 | |
b33f7651 JS |
587 | bool ret = SetFocusToChild(); |
588 | ||
589 | m_inSetFocus = false; | |
590 | ||
591 | return ret; | |
456bc6d9 VZ |
592 | } |
593 | ||
594 | void wxControlContainer::HandleOnFocus(wxFocusEvent& event) | |
595 | { | |
9f542367 VZ |
596 | wxLogTrace(TRACE_FOCUS, _T("OnFocus on wxPanel 0x%p, name: %s"), |
597 | m_winParent->GetHandle(), | |
456bc6d9 VZ |
598 | m_winParent->GetName().c_str() ); |
599 | ||
2b5f62a0 | 600 | DoSetFocus(); |
456bc6d9 VZ |
601 | |
602 | event.Skip(); | |
603 | } | |
604 | ||
605 | bool wxControlContainer::SetFocusToChild() | |
606 | { | |
607 | return wxSetFocusToChild(m_winParent, &m_winLastFocused); | |
608 | } | |
609 | ||
610 | // ---------------------------------------------------------------------------- | |
611 | // SetFocusToChild(): this function is used by wxPanel but also by wxFrame in | |
612 | // wxMSW, this is why it is outside of wxControlContainer class | |
613 | // ---------------------------------------------------------------------------- | |
614 | ||
615 | bool wxSetFocusToChild(wxWindow *win, wxWindow **childLastFocused) | |
616 | { | |
c9d59ee7 WS |
617 | wxCHECK_MSG( win, false, _T("wxSetFocusToChild(): invalid window") ); |
618 | wxCHECK_MSG( childLastFocused, false, | |
c25b5d1f | 619 | _T("wxSetFocusToChild(): NULL child poonter") ); |
456bc6d9 VZ |
620 | |
621 | if ( *childLastFocused ) | |
622 | { | |
c25b5d1f VZ |
623 | // It might happen that the window got reparented |
624 | if ( (*childLastFocused)->GetParent() == win ) | |
456bc6d9 | 625 | { |
9f542367 VZ |
626 | wxLogTrace(TRACE_FOCUS, |
627 | _T("SetFocusToChild() => last child (0x%p)."), | |
628 | (*childLastFocused)->GetHandle()); | |
456bc6d9 | 629 | |
c25b5d1f VZ |
630 | // not SetFocusFromKbd(): we're restoring focus back to the old |
631 | // window and not setting it as the result of a kbd action | |
632 | (*childLastFocused)->SetFocus(); | |
c9d59ee7 | 633 | return true; |
456bc6d9 VZ |
634 | } |
635 | else | |
636 | { | |
637 | // it doesn't count as such any more | |
638 | *childLastFocused = (wxWindow *)NULL; | |
639 | } | |
640 | } | |
641 | ||
642 | // set the focus to the first child who wants it | |
222ed1d6 | 643 | wxWindowList::compatibility_iterator node = win->GetChildren().GetFirst(); |
456bc6d9 VZ |
644 | while ( node ) |
645 | { | |
646 | wxWindow *child = node->GetData(); | |
afd7bf26 | 647 | node = node->GetNext(); |
456bc6d9 | 648 | |
afd7bf26 | 649 | #ifdef __WXMAC__ |
31e1bd37 | 650 | if ( child->GetParent()->MacIsWindowScrollbar( child ) ) |
afd7bf26 RD |
651 | continue; |
652 | #endif | |
653 | ||
de160b06 | 654 | if ( child->CanAcceptFocusFromKeyboard() && !child->IsTopLevel() ) |
456bc6d9 | 655 | { |
7ff1b620 | 656 | #ifdef __WXMSW__ |
b49f58fe | 657 | // If a radiobutton is the first focusable child, search for the |
7ff1b620 VZ |
658 | // selected radiobutton in the same group |
659 | wxRadioButton* btn = wxDynamicCast(child, wxRadioButton); | |
660 | if (btn) | |
661 | { | |
662 | wxRadioButton* selected = wxGetSelectedButtonInGroup(btn); | |
663 | if (selected) | |
664 | child = selected; | |
665 | } | |
666 | #endif | |
667 | ||
9f542367 VZ |
668 | wxLogTrace(TRACE_FOCUS, |
669 | _T("SetFocusToChild() => first child (0x%p)."), | |
670 | child->GetHandle()); | |
456bc6d9 | 671 | |
a7407834 | 672 | *childLastFocused = child; |
5463c0a4 | 673 | child->SetFocusFromKbd(); |
c9d59ee7 | 674 | return true; |
456bc6d9 | 675 | } |
456bc6d9 VZ |
676 | } |
677 | ||
c9d59ee7 | 678 | return false; |
456bc6d9 | 679 | } |
de160b06 VZ |
680 | |
681 | #endif // !wxHAS_NATIVE_TAB_TRAVERSAL |