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