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