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