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