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