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