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