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