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