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