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