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