]> git.saurik.com Git - wxWidgets.git/blob - src/common/containr.cpp
fix printf() argument type in GetOsInfo()
[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
72 if ( child->AcceptsFocus() )
73 {
74 return true;
75 }
76
77 #ifdef __WXMAC__
78 wxScrollBar *sb = wxDynamicCast( child , wxScrollBar ) ;
79 if ( sb == NULL || !m_winParent->MacIsWindowScrollbar( sb ) )
80 hasRealChildren = true ;
81 #endif
82 node = node->GetNext();
83 }
84
85 #ifdef __WXMAC__
86 if ( !hasRealChildren )
87 return true ;
88 #endif
89 }
90
91 return false;
92 }
93
94 void wxControlContainer::SetLastFocus(wxWindow *win)
95 {
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
99 if ( win != m_winParent )
100 {
101 // if we're setting the focus
102 if ( win )
103 {
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();
110
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)
115
116 wxASSERT_MSG( winParent,
117 _T("Setting last focus for a window that is not our child?") );
118 }
119 }
120
121 m_winLastFocused = win;
122
123 if ( win )
124 {
125 wxLogTrace(TRACE_FOCUS, _T("Set last focus to %s(%s)"),
126 win->GetClassInfo()->GetClassName(),
127 win->GetLabel().c_str());
128 }
129 else
130 {
131 wxLogTrace(TRACE_FOCUS, _T("No more last focus"));
132 }
133 }
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 )
139 {
140 wxChildFocusEvent eventFocus(m_winParent);
141 parent->GetEventHandler()->ProcessEvent(eventFocus);
142 }
143 }
144
145 // --------------------------------------------------------------------
146 // The following four functions are used to find other radio buttons
147 // within the same group. Used by wxSetFocusToChild on wxMSW
148 // --------------------------------------------------------------------
149
150 #ifdef __WXMSW__
151
152 wxRadioButton* 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 }
172
173 if (!prevBtn || prevBtn->HasFlag(wxRB_SINGLE))
174 {
175 // no more buttons in group
176 return NULL;
177 }
178
179 return prevBtn;
180 }
181
182 wxRadioButton* 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 }
208
209 return nextBtn;
210 }
211
212 wxRadioButton* wxGetFirstButtonInGroup(wxRadioButton *btn)
213 {
214 while (true)
215 {
216 wxRadioButton* prevBtn = wxGetPreviousButtonInGroup(btn);
217 if (!prevBtn)
218 return btn;
219
220 btn = prevBtn;
221 }
222 }
223
224 wxRadioButton* 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
236 wxRadioButton* 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
260 #endif // __WXMSW__
261
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
270 void 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 // if we have exactly one notebook-like child window (actually it could be
280 // any window that returns true from its HasMultiplePages()), then
281 // [Shift-]Ctrl-Tab and Ctrl-PageUp/Down keys should iterate over its pages
282 // even if the focus is outside of the control because this is how the
283 // standard MSW properties dialogs behave and we do it under other platforms
284 // as well because it seems like a good idea -- but we can always put this
285 // block inside "#ifdef __WXMSW__" if it's not suitable there
286 if ( event.IsWindowChange() && !goingDown )
287 {
288 // check if we have a unique notebook-like child
289 wxWindow *bookctrl = NULL;
290 for ( wxWindowList::const_iterator i = children.begin(),
291 end = children.end();
292 i != end;
293 ++i )
294 {
295 wxWindow * const window = *i;
296 if ( window->HasMultiplePages() )
297 {
298 if ( bookctrl )
299 {
300 // this is the second book-like control already so don't do
301 // anything as we don't know which one should have its page
302 // changed
303 bookctrl = NULL;
304 break;
305 }
306
307 bookctrl = window;
308 }
309 }
310
311 if ( bookctrl && bookctrl->GetEventHandler()->ProcessEvent(event) )
312 return;
313 }
314
315 // there is not much to do if we don't have children and we're not
316 // interested in "notebook page change" events here
317 if ( !children.GetCount() || event.IsWindowChange() )
318 {
319 // let the parent process it unless it already comes from our parent
320 // of we don't have any
321 if ( goingDown ||
322 !parent || !parent->GetEventHandler()->ProcessEvent(event) )
323 {
324 event.Skip();
325 }
326
327 return;
328 }
329
330 // where are we going?
331 const bool forward = event.GetDirection();
332
333 // the node of the children list from which we should start looking for the
334 // next acceptable child
335 wxWindowList::compatibility_iterator node, start_node;
336
337 // we should start from the first/last control and not from the one which
338 // had focus the last time if we're propagating the event downwards because
339 // for our parent we look like a single control
340 if ( goingDown )
341 {
342 // just to be sure it's not used (normally this is not necessary, but
343 // doesn't hurt neither)
344 m_winLastFocused = (wxWindow *)NULL;
345
346 // start from first or last depending on where we're going
347 node = forward ? children.GetFirst() : children.GetLast();
348 }
349 else // going up
350 {
351 // try to find the child which has the focus currently
352
353 // the event emitter might have done this for us
354 wxWindow *winFocus = event.GetCurrentFocus();
355
356 // but if not, we might know where the focus was ourselves
357 if (!winFocus)
358 winFocus = m_winLastFocused;
359
360 // if still no luck, do it the hard way
361 if (!winFocus)
362 winFocus = wxWindow::FindFocus();
363
364 if ( winFocus )
365 {
366 #ifdef __WXMSW__
367 // If we are in a radio button group, start from the first item in the
368 // group
369 if ( event.IsFromTab() && wxIsKindOf(winFocus, wxRadioButton ) )
370 winFocus = wxGetFirstButtonInGroup((wxRadioButton*)winFocus);
371 #endif
372 // ok, we found the focus - now is it our child?
373 start_node = children.Find( winFocus );
374 }
375
376 if ( !start_node && m_winLastFocused )
377 {
378 // window which has focus isn't our child, fall back to the one
379 // which had the focus the last time
380 start_node = children.Find( m_winLastFocused );
381 }
382
383 // if we still didn't find anything, we should start with the first one
384 if ( !start_node )
385 {
386 start_node = children.GetFirst();
387 }
388
389 // and the first child which we can try setting focus to is the next or
390 // the previous one
391 node = forward ? start_node->GetNext() : start_node->GetPrevious();
392 }
393
394 // we want to cycle over all elements passing by NULL
395 for ( ;; )
396 {
397 // don't go into infinite loop
398 if ( start_node && node && node == start_node )
399 break;
400
401 // Have we come to the last or first item on the panel?
402 if ( !node )
403 {
404 if ( !start_node )
405 {
406 // exit now as otherwise we'd loop forever
407 break;
408 }
409
410 if ( !goingDown )
411 {
412 // Check if our (maybe grand) parent is another panel: if this
413 // is the case, they will know what to do with this navigation
414 // key and so give them the chance to process it instead of
415 // looping inside this panel (normally, the focus will go to
416 // the next/previous item after this panel in the parent
417 // panel).
418 wxWindow *focussed_child_of_parent = m_winParent;
419 while ( parent )
420 {
421 // we don't want to tab into a different dialog or frame
422 if ( focussed_child_of_parent->IsTopLevel() )
423 break;
424
425 event.SetCurrentFocus( focussed_child_of_parent );
426 if ( parent->GetEventHandler()->ProcessEvent( event ) )
427 return;
428
429 focussed_child_of_parent = parent;
430
431 parent = parent->GetParent();
432 }
433 }
434 //else: as the focus came from our parent, we definitely don't want
435 // to send it back to it!
436
437 // no, we are not inside another panel so process this ourself
438 node = forward ? children.GetFirst() : children.GetLast();
439
440 continue;
441 }
442
443 wxWindow *child = node->GetData();
444
445 #ifdef __WXMSW__
446 if ( event.IsFromTab() )
447 {
448 if ( wxIsKindOf(child, wxRadioButton) )
449 {
450 // only radio buttons with either wxRB_GROUP or wxRB_SINGLE
451 // can be tabbed to
452 if ( child->HasFlag(wxRB_GROUP) )
453 {
454 // need to tab into the active button within a group
455 wxRadioButton *rb = wxGetSelectedButtonInGroup((wxRadioButton*)child);
456 if ( rb )
457 child = rb;
458 }
459 else if ( !child->HasFlag(wxRB_SINGLE) )
460 {
461 node = forward ? node->GetNext() : node->GetPrevious();
462 continue;
463 }
464 }
465 }
466 else if ( m_winLastFocused &&
467 wxIsKindOf(m_winLastFocused, wxRadioButton) &&
468 !m_winLastFocused->HasFlag(wxRB_SINGLE) )
469 {
470 // cursor keys don't navigate out of a radio button group so
471 // find the correct radio button to focus
472 if ( forward )
473 {
474 child = wxGetNextButtonInGroup((wxRadioButton*)m_winLastFocused);
475 if ( !child )
476 {
477 // no next button in group, set it to the first button
478 child = wxGetFirstButtonInGroup((wxRadioButton*)m_winLastFocused);
479 }
480 }
481 else
482 {
483 child = wxGetPreviousButtonInGroup((wxRadioButton*)m_winLastFocused);
484 if ( !child )
485 {
486 // no previous button in group, set it to the last button
487 child = wxGetLastButtonInGroup((wxRadioButton*)m_winLastFocused);
488 }
489 }
490
491 if ( child == m_winLastFocused )
492 {
493 // must be a group consisting of only one button therefore
494 // no need to send a navigation event
495 event.Skip(false);
496 return;
497 }
498 }
499 #endif // __WXMSW__
500
501 if ( child->AcceptsFocusFromKeyboard() )
502 {
503 // if we're setting the focus to a child panel we should prevent it
504 // from giving it to the child which had the focus the last time
505 // and instead give it to the first/last child depending from which
506 // direction we're coming
507 event.SetEventObject(m_winParent);
508
509 // disable propagation for this call as otherwise the event might
510 // bounce back to us.
511 wxPropagationDisabler disableProp(event);
512 if ( !child->GetEventHandler()->ProcessEvent(event) )
513 {
514 // set it first in case SetFocusFromKbd() results in focus
515 // change too
516 m_winLastFocused = child;
517
518 // everything is simple: just give focus to it
519 child->SetFocusFromKbd();
520 }
521 //else: the child manages its focus itself
522
523 event.Skip( false );
524
525 return;
526 }
527
528 node = forward ? node->GetNext() : node->GetPrevious();
529 }
530
531 // we cycled through all of our children and none of them wanted to accept
532 // focus
533 event.Skip();
534 }
535
536 void wxControlContainer::HandleOnWindowDestroy(wxWindowBase *child)
537 {
538 if ( child == m_winLastFocused )
539 m_winLastFocused = NULL;
540 }
541
542 // ----------------------------------------------------------------------------
543 // focus handling
544 // ----------------------------------------------------------------------------
545
546 bool wxControlContainer::DoSetFocus()
547 {
548 wxLogTrace(TRACE_FOCUS, _T("SetFocus on wxPanel 0x%p."),
549 m_winParent->GetHandle());
550
551 if (m_inSetFocus)
552 return true;
553
554 // when the panel gets the focus we move the focus to either the last
555 // window that had the focus or the first one that can get it unless the
556 // focus had been already set to some other child
557
558 wxWindow *win = wxWindow::FindFocus();
559 while ( win )
560 {
561 if ( win == m_winParent )
562 {
563 // our child already has focus, don't take it away from it
564 return true;
565 }
566
567 if ( win->IsTopLevel() )
568 {
569 // don't look beyond the first top level parent - useless and
570 // unnecessary
571 break;
572 }
573
574 win = win->GetParent();
575 }
576
577 // protect against infinite recursion:
578 m_inSetFocus = true;
579
580 bool ret = SetFocusToChild();
581
582 m_inSetFocus = false;
583
584 return ret;
585 }
586
587 void wxControlContainer::HandleOnFocus(wxFocusEvent& event)
588 {
589 wxLogTrace(TRACE_FOCUS, _T("OnFocus on wxPanel 0x%p, name: %s"),
590 m_winParent->GetHandle(),
591 m_winParent->GetName().c_str() );
592
593 DoSetFocus();
594
595 event.Skip();
596 }
597
598 bool wxControlContainer::SetFocusToChild()
599 {
600 return wxSetFocusToChild(m_winParent, &m_winLastFocused);
601 }
602
603 // ----------------------------------------------------------------------------
604 // SetFocusToChild(): this function is used by wxPanel but also by wxFrame in
605 // wxMSW, this is why it is outside of wxControlContainer class
606 // ----------------------------------------------------------------------------
607
608 bool wxSetFocusToChild(wxWindow *win, wxWindow **childLastFocused)
609 {
610 wxCHECK_MSG( win, false, _T("wxSetFocusToChild(): invalid window") );
611 wxCHECK_MSG( childLastFocused, false,
612 _T("wxSetFocusToChild(): NULL child poonter") );
613
614 if ( *childLastFocused )
615 {
616 // It might happen that the window got reparented
617 if ( (*childLastFocused)->GetParent() == win )
618 {
619 wxLogTrace(TRACE_FOCUS,
620 _T("SetFocusToChild() => last child (0x%p)."),
621 (*childLastFocused)->GetHandle());
622
623 // not SetFocusFromKbd(): we're restoring focus back to the old
624 // window and not setting it as the result of a kbd action
625 (*childLastFocused)->SetFocus();
626 return true;
627 }
628 else
629 {
630 // it doesn't count as such any more
631 *childLastFocused = (wxWindow *)NULL;
632 }
633 }
634
635 // set the focus to the first child who wants it
636 wxWindowList::compatibility_iterator node = win->GetChildren().GetFirst();
637 while ( node )
638 {
639 wxWindow *child = node->GetData();
640
641 if ( child->AcceptsFocusFromKeyboard() && !child->IsTopLevel() )
642 {
643 #ifdef __WXMSW__
644 // If a radiobutton is the first focusable child, search for the
645 // selected radiobutton in the same group
646 wxRadioButton* btn = wxDynamicCast(child, wxRadioButton);
647 if (btn)
648 {
649 wxRadioButton* selected = wxGetSelectedButtonInGroup(btn);
650 if (selected)
651 child = selected;
652 }
653 #endif
654
655 wxLogTrace(TRACE_FOCUS,
656 _T("SetFocusToChild() => first child (0x%p)."),
657 child->GetHandle());
658
659 *childLastFocused = child;
660 child->SetFocusFromKbd();
661 return true;
662 }
663
664 node = node->GetNext();
665 }
666
667 return false;
668 }