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