]> git.saurik.com Git - wxWidgets.git/blame - src/common/containr.cpp
Rich text lib separation.
[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"
851dee09 31 #include "wx/scrolbar.h"
b0b5881a 32 #include "wx/radiobut.h"
841f23e1 33 #include "wx/containr.h"
456bc6d9
VZ
34#endif //WX_PRECOMP
35
9f542367
VZ
36// trace mask for focus messages
37#define TRACE_FOCUS _T("focus")
38
456bc6d9
VZ
39// ============================================================================
40// implementation
41// ============================================================================
42
43wxControlContainer::wxControlContainer(wxWindow *winParent)
44{
45 m_winParent = winParent;
46
47 m_winLastFocused =
036da5e3 48 m_winTmpDefault =
456bc6d9 49 m_winDefault = NULL;
b33f7651 50 m_inSetFocus = false;
456bc6d9
VZ
51}
52
3251b834
VZ
53bool wxControlContainer::AcceptsFocus() const
54{
a7407834 55 // if we're not shown or disabled, we can't accept focus
3251b834
VZ
56 if ( m_winParent->IsShown() && m_winParent->IsEnabled() )
57 {
a7407834
VZ
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
222ed1d6 61 wxWindowList::compatibility_iterator node = m_winParent->GetChildren().GetFirst();
a7407834 62 if ( !node )
c9d59ee7 63 return true;
a7407834 64
2f64c3bb
SC
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
c9d59ee7 70
3251b834
VZ
71 while ( node )
72 {
73 wxWindow *child = node->GetData();
74
75 if ( child->AcceptsFocus() )
76 {
c9d59ee7 77 return true;
3251b834
VZ
78 }
79
2f64c3bb
SC
80#ifdef __WXMAC__
81 wxScrollBar *sb = wxDynamicCast( child , wxScrollBar ) ;
82 if ( sb == NULL || !m_winParent->MacIsWindowScrollbar( sb ) )
83 hasRealChildren = true ;
84#endif
3251b834
VZ
85 node = node->GetNext();
86 }
c9d59ee7 87
2f64c3bb
SC
88#ifdef __WXMAC__
89 if ( !hasRealChildren )
c9d59ee7 90 return true ;
2f64c3bb 91#endif
3251b834
VZ
92 }
93
c9d59ee7 94 return false;
3251b834
VZ
95}
96
456bc6d9
VZ
97void wxControlContainer::SetLastFocus(wxWindow *win)
98{
6aeb6f2a
VZ
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
c25b5d1f 102 if ( win != m_winParent )
456bc6d9 103 {
c25b5d1f
VZ
104 // if we're setting the focus
105 if ( win )
83c865f5 106 {
c25b5d1f
VZ
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();
83c865f5 113
c25b5d1f
VZ
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)
6f8239de 118
c25b5d1f
VZ
119 wxASSERT_MSG( winParent,
120 _T("Setting last focus for a window that is not our child?") );
121 }
6f8239de 122 }
456bc6d9 123
c25b5d1f 124 m_winLastFocused = win;
6aeb6f2a 125
c25b5d1f
VZ
126 if ( win )
127 {
9f542367 128 wxLogTrace(TRACE_FOCUS, _T("Set last focus to %s(%s)"),
c25b5d1f
VZ
129 win->GetClassInfo()->GetClassName(),
130 win->GetLabel().c_str());
131 }
132 else
133 {
9f542367 134 wxLogTrace(TRACE_FOCUS, _T("No more last focus"));
c25b5d1f 135 }
6aeb6f2a 136 }
c25b5d1f
VZ
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 )
6aeb6f2a 142 {
c25b5d1f
VZ
143 wxChildFocusEvent eventFocus(m_winParent);
144 parent->GetEventHandler()->ProcessEvent(eventFocus);
6aeb6f2a 145 }
456bc6d9
VZ
146}
147
7ff1b620 148// --------------------------------------------------------------------
b49f58fe 149// The following four functions are used to find other radio buttons
7ff1b620
VZ
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 }
b49f58fe 175
7ff1b620
VZ
176 if (!prevBtn || prevBtn->HasFlag(wxRB_SINGLE))
177 {
178 // no more buttons in group
179 return NULL;
180 }
3d3afaec
JS
181
182 return prevBtn;
7ff1b620
VZ
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 }
3d3afaec
JS
211
212 return nextBtn;
7ff1b620
VZ
213}
214
215wxRadioButton* wxGetFirstButtonInGroup(wxRadioButton *btn)
216{
217 while (true)
218 {
219 wxRadioButton* prevBtn = wxGetPreviousButtonInGroup(btn);
220 if (!prevBtn)
221 return btn;
b49f58fe 222
7ff1b620
VZ
223 btn = prevBtn;
224 }
225}
226
3d3afaec
JS
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
7ff1b620
VZ
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
b49f58fe 263#endif // __WXMSW__
7ff1b620 264
456bc6d9
VZ
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?
edc0a395 298 const bool forward = event.GetDirection();
456bc6d9
VZ
299
300 // the node of the children list from which we should start looking for the
301 // next acceptable child
222ed1d6 302 wxWindowList::compatibility_iterator node, start_node;
456bc6d9
VZ
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();
456bc6d9 315 }
edc0a395 316 else // going up
456bc6d9
VZ
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 {
7ff1b620 333#ifdef __WXMSW__
b49f58fe 334 // If we are in a radio button group, start from the first item in the
7ff1b620
VZ
335 // group
336 if ( event.IsFromTab() && wxIsKindOf(winFocus, wxRadioButton ) )
337 winFocus = wxGetFirstButtonInGroup((wxRadioButton*)winFocus);
338#endif
456bc6d9
VZ
339 // ok, we found the focus - now is it our child?
340 start_node = children.Find( winFocus );
341 }
456bc6d9
VZ
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
edc0a395 362 for ( ;; )
456bc6d9 363 {
edc0a395 364 // don't go into infinite loop
aa78d22e 365 if ( start_node && node && node == start_node )
edc0a395
VZ
366 break;
367
456bc6d9
VZ
368 // Have we come to the last or first item on the panel?
369 if ( !node )
370 {
e547f7a7
VZ
371 if ( !start_node )
372 {
373 // exit now as otherwise we'd loop forever
374 break;
375 }
376
456bc6d9
VZ
377 if ( !goingDown )
378 {
edc0a395 379 // Check if our (maybe grand) parent is another panel: if this
456bc6d9
VZ
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
7ff1b620 412#ifdef __WXMSW__
3d3afaec 413 if ( event.IsFromTab() )
7ff1b620 414 {
3d3afaec 415 if ( wxIsKindOf(child, wxRadioButton) )
7ff1b620 416 {
3d3afaec
JS
417 // only radio buttons with either wxRB_GROUP or wxRB_SINGLE
418 // can be tabbed to
419 if ( child->HasFlag(wxRB_GROUP) )
7ff1b620 420 {
3d3afaec
JS
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;
7ff1b620
VZ
430 }
431 }
432 }
3d3afaec
JS
433 else if ( m_winLastFocused &&
434 wxIsKindOf(m_winLastFocused, wxRadioButton) &&
435 !m_winLastFocused->HasFlag(wxRB_SINGLE) )
7ff1b620 436 {
3d3afaec
JS
437 // cursor keys don't navigate out of a radio button group so
438 // find the correct radio button to focus
439 if ( forward )
7ff1b620 440 {
3d3afaec
JS
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;
7ff1b620
VZ
464 }
465 }
3d3afaec 466#endif // __WXMSW__
c932709d 467
3d3afaec 468 if ( child->AcceptsFocusFromKeyboard() )
456bc6d9
VZ
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);
944e8709 475
aef35d0e
VZ
476 // disable propagation for this call as otherwise the event might
477 // bounce back to us.
478 wxPropagationDisabler disableProp(event);
456bc6d9
VZ
479 if ( !child->GetEventHandler()->ProcessEvent(event) )
480 {
2b5f62a0
VZ
481 // set it first in case SetFocusFromKbd() results in focus
482 // change too
483 m_winLastFocused = child;
484
456bc6d9 485 // everything is simple: just give focus to it
5463c0a4 486 child->SetFocusFromKbd();
456bc6d9
VZ
487 }
488 //else: the child manages its focus itself
489
c9d59ee7 490 event.Skip( false );
456bc6d9
VZ
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;
036da5e3
VZ
510
511 if ( child == m_winTmpDefault )
512 m_winTmpDefault = NULL;
456bc6d9
VZ
513}
514
515// ----------------------------------------------------------------------------
516// focus handling
517// ----------------------------------------------------------------------------
518
24a7a198 519bool wxControlContainer::DoSetFocus()
456bc6d9 520{
9f542367
VZ
521 wxLogTrace(TRACE_FOCUS, _T("SetFocus on wxPanel 0x%p."),
522 m_winParent->GetHandle());
456bc6d9 523
b33f7651
JS
524 if (m_inSetFocus)
525 return true;
c9d59ee7 526
2b5f62a0
VZ
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
0dcdeee9
VZ
531 wxWindow *win = wxWindow::FindFocus();
532 while ( win )
533 {
534 if ( win == m_winParent )
2b5f62a0
VZ
535 {
536 // our child already has focus, don't take it away from it
1c9e321b 537 return true;
2b5f62a0 538 }
0dcdeee9
VZ
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 }
c9d59ee7 549
2e07bdda
VS
550 // protect against infinite recursion:
551 m_inSetFocus = true;
0dcdeee9 552
b33f7651
JS
553 bool ret = SetFocusToChild();
554
555 m_inSetFocus = false;
556
557 return ret;
456bc6d9
VZ
558}
559
560void wxControlContainer::HandleOnFocus(wxFocusEvent& event)
561{
9f542367
VZ
562 wxLogTrace(TRACE_FOCUS, _T("OnFocus on wxPanel 0x%p, name: %s"),
563 m_winParent->GetHandle(),
456bc6d9
VZ
564 m_winParent->GetName().c_str() );
565
2b5f62a0 566 DoSetFocus();
456bc6d9
VZ
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{
c9d59ee7
WS
583 wxCHECK_MSG( win, false, _T("wxSetFocusToChild(): invalid window") );
584 wxCHECK_MSG( childLastFocused, false,
c25b5d1f 585 _T("wxSetFocusToChild(): NULL child poonter") );
456bc6d9
VZ
586
587 if ( *childLastFocused )
588 {
c25b5d1f
VZ
589 // It might happen that the window got reparented
590 if ( (*childLastFocused)->GetParent() == win )
456bc6d9 591 {
9f542367
VZ
592 wxLogTrace(TRACE_FOCUS,
593 _T("SetFocusToChild() => last child (0x%p)."),
594 (*childLastFocused)->GetHandle());
456bc6d9 595
c25b5d1f
VZ
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();
c9d59ee7 599 return true;
456bc6d9
VZ
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
222ed1d6 609 wxWindowList::compatibility_iterator node = win->GetChildren().GetFirst();
456bc6d9
VZ
610 while ( node )
611 {
612 wxWindow *child = node->GetData();
613
614 if ( child->AcceptsFocusFromKeyboard() && !child->IsTopLevel() )
615 {
7ff1b620 616#ifdef __WXMSW__
b49f58fe 617 // If a radiobutton is the first focusable child, search for the
7ff1b620
VZ
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
9f542367
VZ
628 wxLogTrace(TRACE_FOCUS,
629 _T("SetFocusToChild() => first child (0x%p)."),
630 child->GetHandle());
456bc6d9 631
a7407834 632 *childLastFocused = child;
5463c0a4 633 child->SetFocusFromKbd();
c9d59ee7 634 return true;
456bc6d9
VZ
635 }
636
637 node = node->GetNext();
638 }
639
c9d59ee7 640 return false;
456bc6d9 641}