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