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