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