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