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