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