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