]> git.saurik.com Git - wxWidgets.git/blame - src/common/containr.cpp
fixing focus handling for generic controls in carbon-cfm
[wxWidgets.git] / src / common / containr.cpp
CommitLineData
456bc6d9
VZ
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>
65571936 9// License: wxWindows licence
456bc6d9
VZ
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
456bc6d9
VZ
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
6285be72
VZ
28 #include "wx/log.h"
29 #include "wx/event.h"
30 #include "wx/window.h"
456bc6d9
VZ
31#endif //WX_PRECOMP
32
33#include "wx/containr.h"
34
76158d52
SC
35#ifdef __WXMAC__
36 #include "wx/scrolbar.h"
37#endif
38
c932709d
RR
39#ifdef __WXMSW__
40 #include "wx/radiobut.h"
41#endif
42
9f542367
VZ
43// trace mask for focus messages
44#define TRACE_FOCUS _T("focus")
45
456bc6d9
VZ
46// ============================================================================
47// implementation
48// ============================================================================
49
50wxControlContainer::wxControlContainer(wxWindow *winParent)
51{
52 m_winParent = winParent;
53
54 m_winLastFocused =
036da5e3 55 m_winTmpDefault =
456bc6d9 56 m_winDefault = NULL;
b33f7651 57 m_inSetFocus = false;
456bc6d9
VZ
58}
59
3251b834
VZ
60bool wxControlContainer::AcceptsFocus() const
61{
a7407834 62 // if we're not shown or disabled, we can't accept focus
3251b834
VZ
63 if ( m_winParent->IsShown() && m_winParent->IsEnabled() )
64 {
a7407834
VZ
65 // otherwise we can accept focus either if we have no children at all
66 // (in this case we're probably not used as a container) or only when
67 // at least one child will accept focus
222ed1d6 68 wxWindowList::compatibility_iterator node = m_winParent->GetChildren().GetFirst();
a7407834 69 if ( !node )
c9d59ee7 70 return true;
a7407834 71
2f64c3bb
SC
72#ifdef __WXMAC__
73 // wxMac has eventually the two scrollbars as children, they don't count
74 // as real children in the algorithm mentioned above
75 bool hasRealChildren = false ;
76#endif
c9d59ee7 77
3251b834
VZ
78 while ( node )
79 {
80 wxWindow *child = node->GetData();
81
82 if ( child->AcceptsFocus() )
83 {
c9d59ee7 84 return true;
3251b834
VZ
85 }
86
2f64c3bb
SC
87#ifdef __WXMAC__
88 wxScrollBar *sb = wxDynamicCast( child , wxScrollBar ) ;
89 if ( sb == NULL || !m_winParent->MacIsWindowScrollbar( sb ) )
90 hasRealChildren = true ;
91#endif
3251b834
VZ
92 node = node->GetNext();
93 }
c9d59ee7 94
2f64c3bb
SC
95#ifdef __WXMAC__
96 if ( !hasRealChildren )
c9d59ee7 97 return true ;
2f64c3bb 98#endif
3251b834
VZ
99 }
100
c9d59ee7 101 return false;
3251b834
VZ
102}
103
456bc6d9
VZ
104void wxControlContainer::SetLastFocus(wxWindow *win)
105{
6aeb6f2a
VZ
106 // the panel itself should never get the focus at all but if it does happen
107 // temporarily (as it seems to do under wxGTK), at the very least don't
108 // forget our previous m_winLastFocused
c25b5d1f 109 if ( win != m_winParent )
456bc6d9 110 {
c25b5d1f
VZ
111 // if we're setting the focus
112 if ( win )
83c865f5 113 {
c25b5d1f
VZ
114 // find the last _immediate_ child which got focus
115 wxWindow *winParent = win;
116 while ( winParent != m_winParent )
117 {
118 win = winParent;
119 winParent = win->GetParent();
83c865f5 120
c25b5d1f
VZ
121 // Yes, this can happen, though in a totally pathological case.
122 // like when detaching a menubar from a frame with a child
123 // which has pushed itself as an event handler for the menubar.
124 // (under wxGTK)
6f8239de 125
c25b5d1f
VZ
126 wxASSERT_MSG( winParent,
127 _T("Setting last focus for a window that is not our child?") );
128 }
6f8239de 129 }
456bc6d9 130
c25b5d1f 131 m_winLastFocused = win;
6aeb6f2a 132
c25b5d1f
VZ
133 if ( win )
134 {
9f542367 135 wxLogTrace(TRACE_FOCUS, _T("Set last focus to %s(%s)"),
c25b5d1f
VZ
136 win->GetClassInfo()->GetClassName(),
137 win->GetLabel().c_str());
138 }
139 else
140 {
9f542367 141 wxLogTrace(TRACE_FOCUS, _T("No more last focus"));
c25b5d1f 142 }
6aeb6f2a 143 }
c25b5d1f
VZ
144
145 // propagate the last focus upwards so that our parent can set focus back
146 // to us if it loses it now and regains later
147 wxWindow *parent = m_winParent->GetParent();
148 if ( parent )
6aeb6f2a 149 {
c25b5d1f
VZ
150 wxChildFocusEvent eventFocus(m_winParent);
151 parent->GetEventHandler()->ProcessEvent(eventFocus);
6aeb6f2a 152 }
456bc6d9
VZ
153}
154
7ff1b620 155// --------------------------------------------------------------------
b49f58fe 156// The following four functions are used to find other radio buttons
7ff1b620
VZ
157// within the same group. Used by wxSetFocusToChild on wxMSW
158// --------------------------------------------------------------------
159
160#ifdef __WXMSW__
161
162wxRadioButton* wxGetPreviousButtonInGroup(wxRadioButton *btn)
163{
164 if ( btn->HasFlag(wxRB_GROUP) || btn->HasFlag(wxRB_SINGLE) )
165 return NULL;
166
167 const wxWindowList& siblings = btn->GetParent()->GetChildren();
168 wxWindowList::compatibility_iterator nodeThis = siblings.Find(btn);
169 wxCHECK_MSG( nodeThis, NULL, _T("radio button not a child of its parent?") );
170
171 // Iterate over all previous siblings until we find the next radio button
172 wxWindowList::compatibility_iterator nodeBefore = nodeThis->GetPrevious();
173 wxRadioButton *prevBtn = 0;
174 while (nodeBefore)
175 {
176 prevBtn = wxDynamicCast(nodeBefore->GetData(), wxRadioButton);
177 if (prevBtn)
178 break;
179
180 nodeBefore = nodeBefore->GetPrevious();
181 }
b49f58fe 182
7ff1b620
VZ
183 if (!prevBtn || prevBtn->HasFlag(wxRB_SINGLE))
184 {
185 // no more buttons in group
186 return NULL;
187 }
188 else
189 return prevBtn;
190}
191
192wxRadioButton* wxGetNextButtonInGroup(wxRadioButton *btn)
193{
194 if (btn->HasFlag(wxRB_SINGLE))
195 return NULL;
196
197 const wxWindowList& siblings = btn->GetParent()->GetChildren();
198 wxWindowList::compatibility_iterator nodeThis = siblings.Find(btn);
199 wxCHECK_MSG( nodeThis, NULL, _T("radio button not a child of its parent?") );
200
201 // Iterate over all previous siblings until we find the next radio button
202 wxWindowList::compatibility_iterator nodeNext = nodeThis->GetNext();
203 wxRadioButton *nextBtn = 0;
204 while (nodeNext)
205 {
206 nextBtn = wxDynamicCast(nodeNext->GetData(), wxRadioButton);
207 if (nextBtn)
208 break;
209
210 nodeNext = nodeNext->GetNext();
211 }
212
213 if ( !nextBtn || nextBtn->HasFlag(wxRB_GROUP) || nextBtn->HasFlag(wxRB_SINGLE) )
214 {
215 // no more buttons or the first button of the next group
216 return NULL;
217 }
218 else
219 return nextBtn;
220}
221
222wxRadioButton* wxGetFirstButtonInGroup(wxRadioButton *btn)
223{
224 while (true)
225 {
226 wxRadioButton* prevBtn = wxGetPreviousButtonInGroup(btn);
227 if (!prevBtn)
228 return btn;
b49f58fe 229
7ff1b620
VZ
230 btn = prevBtn;
231 }
232}
233
234wxRadioButton* wxGetSelectedButtonInGroup(wxRadioButton *btn)
235{
236 // Find currently selected button
237 if (btn->GetValue())
238 return btn;
239
240 if (btn->HasFlag(wxRB_SINGLE))
241 return NULL;
242
243 wxRadioButton *selBtn;
244
245 // First check all previous buttons
246 for (selBtn = wxGetPreviousButtonInGroup(btn); selBtn; selBtn = wxGetPreviousButtonInGroup(selBtn))
247 if (selBtn->GetValue())
248 return selBtn;
249
250 // Now all following buttons
251 for (selBtn = wxGetNextButtonInGroup(btn); selBtn; selBtn = wxGetNextButtonInGroup(selBtn))
252 if (selBtn->GetValue())
253 return selBtn;
254
255 return NULL;
256}
257
b49f58fe 258#endif // __WXMSW__
7ff1b620 259
456bc6d9
VZ
260// ----------------------------------------------------------------------------
261// Keyboard handling - this is the place where the TAB traversal logic is
262// implemented. As this code is common to all ports, this ensures consistent
263// behaviour even if we don't specify how exactly the wxNavigationKeyEvent are
264// generated and this is done in platform specific code which also ensures that
265// we can follow the given platform standards.
266// ----------------------------------------------------------------------------
267
268void wxControlContainer::HandleOnNavigationKey( wxNavigationKeyEvent& event )
269{
270 wxWindow *parent = m_winParent->GetParent();
271
272 // the event is propagated downwards if the event emitter was our parent
273 bool goingDown = event.GetEventObject() == parent;
274
275 const wxWindowList& children = m_winParent->GetChildren();
276
277 // there is not much to do if we don't have children and we're not
278 // interested in "notebook page change" events here
279 if ( !children.GetCount() || event.IsWindowChange() )
280 {
281 // let the parent process it unless it already comes from our parent
282 // of we don't have any
283 if ( goingDown ||
284 !parent || !parent->GetEventHandler()->ProcessEvent(event) )
285 {
286 event.Skip();
287 }
288
289 return;
290 }
291
292 // where are we going?
293 bool forward = event.GetDirection();
294
295 // the node of the children list from which we should start looking for the
296 // next acceptable child
222ed1d6 297 wxWindowList::compatibility_iterator node, start_node;
456bc6d9
VZ
298
299 // we should start from the first/last control and not from the one which
300 // had focus the last time if we're propagating the event downwards because
301 // for our parent we look like a single control
302 if ( goingDown )
303 {
304 // just to be sure it's not used (normally this is not necessary, but
305 // doesn't hurt neither)
306 m_winLastFocused = (wxWindow *)NULL;
307
308 // start from first or last depending on where we're going
309 node = forward ? children.GetFirst() : children.GetLast();
310
311 // we want to cycle over all nodes
222ed1d6 312 start_node = wxWindowList::compatibility_iterator();
456bc6d9
VZ
313 }
314 else
315 {
316 // try to find the child which has the focus currently
317
318 // the event emitter might have done this for us
319 wxWindow *winFocus = event.GetCurrentFocus();
320
321 // but if not, we might know where the focus was ourselves
322 if (!winFocus)
323 winFocus = m_winLastFocused;
324
325 // if still no luck, do it the hard way
326 if (!winFocus)
327 winFocus = wxWindow::FindFocus();
328
329 if ( winFocus )
330 {
7ff1b620 331#ifdef __WXMSW__
b49f58fe 332 // If we are in a radio button group, start from the first item in the
7ff1b620
VZ
333 // group
334 if ( event.IsFromTab() && wxIsKindOf(winFocus, wxRadioButton ) )
335 winFocus = wxGetFirstButtonInGroup((wxRadioButton*)winFocus);
336#endif
456bc6d9
VZ
337 // ok, we found the focus - now is it our child?
338 start_node = children.Find( winFocus );
339 }
340 else
341 {
222ed1d6 342 start_node = wxWindowList::compatibility_iterator();
456bc6d9
VZ
343 }
344
345 if ( !start_node && m_winLastFocused )
346 {
347 // window which has focus isn't our child, fall back to the one
348 // which had the focus the last time
349 start_node = children.Find( m_winLastFocused );
350 }
351
352 // if we still didn't find anything, we should start with the first one
353 if ( !start_node )
354 {
355 start_node = children.GetFirst();
356 }
357
358 // and the first child which we can try setting focus to is the next or
359 // the previous one
360 node = forward ? start_node->GetNext() : start_node->GetPrevious();
361 }
362
363 // we want to cycle over all elements passing by NULL
364 while ( node != start_node )
365 {
366 // Have we come to the last or first item on the panel?
367 if ( !node )
368 {
369 if ( !goingDown )
370 {
371 // Check if our (may be grand) parent is another panel: if this
372 // is the case, they will know what to do with this navigation
373 // key and so give them the chance to process it instead of
374 // looping inside this panel (normally, the focus will go to
375 // the next/previous item after this panel in the parent
376 // panel).
377 wxWindow *focussed_child_of_parent = m_winParent;
378 while ( parent )
379 {
380 // we don't want to tab into a different dialog or frame
381 if ( focussed_child_of_parent->IsTopLevel() )
382 break;
383
384 event.SetCurrentFocus( focussed_child_of_parent );
385 if ( parent->GetEventHandler()->ProcessEvent( event ) )
386 return;
387
388 focussed_child_of_parent = parent;
389
390 parent = parent->GetParent();
391 }
392 }
393 //else: as the focus came from our parent, we definitely don't want
394 // to send it back to it!
395
396 // no, we are not inside another panel so process this ourself
397 node = forward ? children.GetFirst() : children.GetLast();
398
399 continue;
400 }
401
402 wxWindow *child = node->GetData();
403
7ff1b620
VZ
404#ifdef __WXMSW__
405 bool canSelectRadioButton = true;
406 if (!event.IsFromTab())
407 {
408 // If navigating using cursor keys, make sure not to navigate out of a radio button group.
409 if (m_winLastFocused && wxIsKindOf(m_winLastFocused, wxRadioButton))
410 {
411 if (!wxIsKindOf(child, wxRadioButton))
412 {
b49f58fe
VZ
413 child = forward ?
414 wxGetNextButtonInGroup((wxRadioButton*)m_winLastFocused) :
7ff1b620
VZ
415 wxGetPreviousButtonInGroup((wxRadioButton*)m_winLastFocused);
416 if (!child)
417 {
418 event.Skip(false);
419 return;
420 }
421 }
422 }
423 }
424 else
425 {
426 // If navigating using tabs, skip all but the first radio button in a group.
427 if (wxIsKindOf(child, wxRadioButton))
428 {
429 if (wxGetPreviousButtonInGroup((wxRadioButton*)child))
430 canSelectRadioButton = false;
431 }
432 }
c932709d 433#else
7ff1b620 434 static bool canSelectRadioButton = true;
c932709d
RR
435#endif
436
7ff1b620 437 if ( child->AcceptsFocusFromKeyboard() && canSelectRadioButton )
456bc6d9
VZ
438 {
439 // if we're setting the focus to a child panel we should prevent it
440 // from giving it to the child which had the focus the last time
441 // and instead give it to the first/last child depending from which
442 // direction we're coming
443 event.SetEventObject(m_winParent);
944e8709 444
82ef81ed 445#if defined(__WXMSW__)
c932709d
RR
446 // we need to hop to the next activated
447 // radio button, not just the next radio
944e8709 448 // button under MSW
7ff1b620 449 if (wxIsKindOf(child, wxRadioButton) && event.IsFromTab())
c932709d 450 {
7ff1b620
VZ
451 wxRadioButton *rb = wxGetSelectedButtonInGroup((wxRadioButton*)child);
452 if (rb)
453 child = rb;
c932709d 454 }
944e8709
VZ
455#endif // __WXMSW__
456
aef35d0e
VZ
457 // disable propagation for this call as otherwise the event might
458 // bounce back to us.
459 wxPropagationDisabler disableProp(event);
456bc6d9
VZ
460 if ( !child->GetEventHandler()->ProcessEvent(event) )
461 {
2b5f62a0
VZ
462 // set it first in case SetFocusFromKbd() results in focus
463 // change too
464 m_winLastFocused = child;
465
456bc6d9 466 // everything is simple: just give focus to it
5463c0a4 467 child->SetFocusFromKbd();
456bc6d9
VZ
468 }
469 //else: the child manages its focus itself
470
c9d59ee7 471 event.Skip( false );
456bc6d9
VZ
472
473 return;
474 }
475
476 node = forward ? node->GetNext() : node->GetPrevious();
477 }
478
479 // we cycled through all of our children and none of them wanted to accept
480 // focus
481 event.Skip();
482}
483
484void wxControlContainer::HandleOnWindowDestroy(wxWindowBase *child)
485{
486 if ( child == m_winLastFocused )
487 m_winLastFocused = NULL;
488
489 if ( child == m_winDefault )
490 m_winDefault = NULL;
036da5e3
VZ
491
492 if ( child == m_winTmpDefault )
493 m_winTmpDefault = NULL;
456bc6d9
VZ
494}
495
496// ----------------------------------------------------------------------------
497// focus handling
498// ----------------------------------------------------------------------------
499
24a7a198 500bool wxControlContainer::DoSetFocus()
456bc6d9 501{
9f542367
VZ
502 wxLogTrace(TRACE_FOCUS, _T("SetFocus on wxPanel 0x%p."),
503 m_winParent->GetHandle());
456bc6d9 504
b33f7651
JS
505 if (m_inSetFocus)
506 return true;
c9d59ee7 507
2b5f62a0
VZ
508 // when the panel gets the focus we move the focus to either the last
509 // window that had the focus or the first one that can get it unless the
510 // focus had been already set to some other child
511
0dcdeee9
VZ
512 wxWindow *win = wxWindow::FindFocus();
513 while ( win )
514 {
515 if ( win == m_winParent )
2b5f62a0
VZ
516 {
517 // our child already has focus, don't take it away from it
1c9e321b 518 return true;
2b5f62a0 519 }
0dcdeee9
VZ
520
521 if ( win->IsTopLevel() )
522 {
523 // don't look beyond the first top level parent - useless and
524 // unnecessary
525 break;
526 }
527
528 win = win->GetParent();
529 }
c9d59ee7 530
2e07bdda
VS
531 // protect against infinite recursion:
532 m_inSetFocus = true;
0dcdeee9 533
b33f7651
JS
534 bool ret = SetFocusToChild();
535
536 m_inSetFocus = false;
537
538 return ret;
456bc6d9
VZ
539}
540
541void wxControlContainer::HandleOnFocus(wxFocusEvent& event)
542{
9f542367
VZ
543 wxLogTrace(TRACE_FOCUS, _T("OnFocus on wxPanel 0x%p, name: %s"),
544 m_winParent->GetHandle(),
456bc6d9
VZ
545 m_winParent->GetName().c_str() );
546
2b5f62a0 547 DoSetFocus();
456bc6d9
VZ
548
549 event.Skip();
550}
551
552bool wxControlContainer::SetFocusToChild()
553{
554 return wxSetFocusToChild(m_winParent, &m_winLastFocused);
555}
556
557// ----------------------------------------------------------------------------
558// SetFocusToChild(): this function is used by wxPanel but also by wxFrame in
559// wxMSW, this is why it is outside of wxControlContainer class
560// ----------------------------------------------------------------------------
561
562bool wxSetFocusToChild(wxWindow *win, wxWindow **childLastFocused)
563{
c9d59ee7
WS
564 wxCHECK_MSG( win, false, _T("wxSetFocusToChild(): invalid window") );
565 wxCHECK_MSG( childLastFocused, false,
c25b5d1f 566 _T("wxSetFocusToChild(): NULL child poonter") );
456bc6d9
VZ
567
568 if ( *childLastFocused )
569 {
c25b5d1f
VZ
570 // It might happen that the window got reparented
571 if ( (*childLastFocused)->GetParent() == win )
456bc6d9 572 {
9f542367
VZ
573 wxLogTrace(TRACE_FOCUS,
574 _T("SetFocusToChild() => last child (0x%p)."),
575 (*childLastFocused)->GetHandle());
456bc6d9 576
c25b5d1f
VZ
577 // not SetFocusFromKbd(): we're restoring focus back to the old
578 // window and not setting it as the result of a kbd action
579 (*childLastFocused)->SetFocus();
c9d59ee7 580 return true;
456bc6d9
VZ
581 }
582 else
583 {
584 // it doesn't count as such any more
585 *childLastFocused = (wxWindow *)NULL;
586 }
587 }
588
589 // set the focus to the first child who wants it
222ed1d6 590 wxWindowList::compatibility_iterator node = win->GetChildren().GetFirst();
456bc6d9
VZ
591 while ( node )
592 {
593 wxWindow *child = node->GetData();
594
595 if ( child->AcceptsFocusFromKeyboard() && !child->IsTopLevel() )
596 {
7ff1b620 597#ifdef __WXMSW__
b49f58fe 598 // If a radiobutton is the first focusable child, search for the
7ff1b620
VZ
599 // selected radiobutton in the same group
600 wxRadioButton* btn = wxDynamicCast(child, wxRadioButton);
601 if (btn)
602 {
603 wxRadioButton* selected = wxGetSelectedButtonInGroup(btn);
604 if (selected)
605 child = selected;
606 }
607#endif
608
9f542367
VZ
609 wxLogTrace(TRACE_FOCUS,
610 _T("SetFocusToChild() => first child (0x%p)."),
611 child->GetHandle());
456bc6d9 612
a7407834 613 *childLastFocused = child;
5463c0a4 614 child->SetFocusFromKbd();
c9d59ee7 615 return true;
456bc6d9
VZ
616 }
617
618 node = node->GetNext();
619 }
620
c9d59ee7 621 return false;
456bc6d9 622}
3251b834 623