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