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