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