]>
Commit | Line | Data |
---|---|---|
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 | // ============================================================================ | |
44 | // implementation | |
45 | // ============================================================================ | |
46 | ||
47 | wxControlContainer::wxControlContainer(wxWindow *winParent) | |
48 | { | |
49 | m_winParent = winParent; | |
50 | ||
51 | m_winLastFocused = | |
52 | m_winTmpDefault = | |
53 | m_winDefault = NULL; | |
54 | m_inSetFocus = false; | |
55 | } | |
56 | ||
57 | bool wxControlContainer::AcceptsFocus() const | |
58 | { | |
59 | // if we're not shown or disabled, we can't accept focus | |
60 | if ( m_winParent->IsShown() && m_winParent->IsEnabled() ) | |
61 | { | |
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 | |
65 | wxWindowList::compatibility_iterator node = m_winParent->GetChildren().GetFirst(); | |
66 | if ( !node ) | |
67 | return true; | |
68 | ||
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 | |
74 | ||
75 | while ( node ) | |
76 | { | |
77 | wxWindow *child = node->GetData(); | |
78 | ||
79 | if ( child->AcceptsFocus() ) | |
80 | { | |
81 | return true; | |
82 | } | |
83 | ||
84 | #ifdef __WXMAC__ | |
85 | wxScrollBar *sb = wxDynamicCast( child , wxScrollBar ) ; | |
86 | if ( sb == NULL || !m_winParent->MacIsWindowScrollbar( sb ) ) | |
87 | hasRealChildren = true ; | |
88 | #endif | |
89 | node = node->GetNext(); | |
90 | } | |
91 | ||
92 | #ifdef __WXMAC__ | |
93 | if ( !hasRealChildren ) | |
94 | return true ; | |
95 | #endif | |
96 | } | |
97 | ||
98 | return false; | |
99 | } | |
100 | ||
101 | void wxControlContainer::SetLastFocus(wxWindow *win) | |
102 | { | |
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 | |
106 | if ( win != m_winParent ) | |
107 | { | |
108 | // if we're setting the focus | |
109 | if ( win ) | |
110 | { | |
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(); | |
117 | ||
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) | |
122 | ||
123 | wxASSERT_MSG( winParent, | |
124 | _T("Setting last focus for a window that is not our child?") ); | |
125 | } | |
126 | } | |
127 | ||
128 | m_winLastFocused = win; | |
129 | ||
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 | } | |
140 | } | |
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 ) | |
146 | { | |
147 | wxChildFocusEvent eventFocus(m_winParent); | |
148 | parent->GetEventHandler()->ProcessEvent(eventFocus); | |
149 | } | |
150 | } | |
151 | ||
152 | // -------------------------------------------------------------------- | |
153 | // The following four functions are used to find other radio buttons | |
154 | // within the same group. Used by wxSetFocusToChild on wxMSW | |
155 | // -------------------------------------------------------------------- | |
156 | ||
157 | #ifdef __WXMSW__ | |
158 | ||
159 | wxRadioButton* 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 | } | |
179 | ||
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 | ||
189 | wxRadioButton* 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 | ||
219 | wxRadioButton* wxGetFirstButtonInGroup(wxRadioButton *btn) | |
220 | { | |
221 | while (true) | |
222 | { | |
223 | wxRadioButton* prevBtn = wxGetPreviousButtonInGroup(btn); | |
224 | if (!prevBtn) | |
225 | return btn; | |
226 | ||
227 | btn = prevBtn; | |
228 | } | |
229 | } | |
230 | ||
231 | wxRadioButton* 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 | ||
255 | #endif // __WXMSW__ | |
256 | ||
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 | ||
265 | void 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 | |
294 | wxWindowList::compatibility_iterator node, start_node; | |
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 | |
309 | start_node = wxWindowList::compatibility_iterator(); | |
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 | { | |
328 | #ifdef __WXMSW__ | |
329 | // If we are in a radio button group, start from the first item in the | |
330 | // group | |
331 | if ( event.IsFromTab() && wxIsKindOf(winFocus, wxRadioButton ) ) | |
332 | winFocus = wxGetFirstButtonInGroup((wxRadioButton*)winFocus); | |
333 | #endif | |
334 | // ok, we found the focus - now is it our child? | |
335 | start_node = children.Find( winFocus ); | |
336 | } | |
337 | else | |
338 | { | |
339 | start_node = wxWindowList::compatibility_iterator(); | |
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 | ||
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 | { | |
410 | child = forward ? | |
411 | wxGetNextButtonInGroup((wxRadioButton*)m_winLastFocused) : | |
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 | } | |
430 | #else | |
431 | static bool canSelectRadioButton = true; | |
432 | #endif | |
433 | ||
434 | if ( child->AcceptsFocusFromKeyboard() && canSelectRadioButton ) | |
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); | |
441 | ||
442 | #if defined(__WXMSW__) | |
443 | // we need to hop to the next activated | |
444 | // radio button, not just the next radio | |
445 | // button under MSW | |
446 | if (wxIsKindOf(child, wxRadioButton) && event.IsFromTab()) | |
447 | { | |
448 | wxRadioButton *rb = wxGetSelectedButtonInGroup((wxRadioButton*)child); | |
449 | if (rb) | |
450 | child = rb; | |
451 | } | |
452 | #endif // __WXMSW__ | |
453 | ||
454 | // disable propagation for this call as otherwise the event might | |
455 | // bounce back to us. | |
456 | wxPropagationDisabler disableProp(event); | |
457 | if ( !child->GetEventHandler()->ProcessEvent(event) ) | |
458 | { | |
459 | // set it first in case SetFocusFromKbd() results in focus | |
460 | // change too | |
461 | m_winLastFocused = child; | |
462 | ||
463 | // everything is simple: just give focus to it | |
464 | child->SetFocusFromKbd(); | |
465 | } | |
466 | //else: the child manages its focus itself | |
467 | ||
468 | event.Skip( false ); | |
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 | ||
481 | void wxControlContainer::HandleOnWindowDestroy(wxWindowBase *child) | |
482 | { | |
483 | if ( child == m_winLastFocused ) | |
484 | m_winLastFocused = NULL; | |
485 | ||
486 | if ( child == m_winDefault ) | |
487 | m_winDefault = NULL; | |
488 | ||
489 | if ( child == m_winTmpDefault ) | |
490 | m_winTmpDefault = NULL; | |
491 | } | |
492 | ||
493 | // ---------------------------------------------------------------------------- | |
494 | // focus handling | |
495 | // ---------------------------------------------------------------------------- | |
496 | ||
497 | bool wxControlContainer::DoSetFocus() | |
498 | { | |
499 | wxLogTrace(_T("focus"), _T("SetFocus on wxPanel 0x%08lx."), | |
500 | (unsigned long)m_winParent->GetHandle()); | |
501 | ||
502 | if (m_inSetFocus) | |
503 | return true; | |
504 | ||
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 | ||
509 | wxWindow *win = wxWindow::FindFocus(); | |
510 | while ( win ) | |
511 | { | |
512 | if ( win == m_winParent ) | |
513 | { | |
514 | // our child already has focus, don't take it away from it | |
515 | return true; | |
516 | } | |
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 | } | |
527 | ||
528 | // protect against infinite recursion: | |
529 | m_inSetFocus = true; | |
530 | ||
531 | bool ret = SetFocusToChild(); | |
532 | ||
533 | m_inSetFocus = false; | |
534 | ||
535 | return ret; | |
536 | } | |
537 | ||
538 | void wxControlContainer::HandleOnFocus(wxFocusEvent& event) | |
539 | { | |
540 | wxLogTrace(_T("focus"), _T("OnFocus on wxPanel 0x%08lx, name: %s"), | |
541 | (unsigned long)m_winParent->GetHandle(), | |
542 | m_winParent->GetName().c_str() ); | |
543 | ||
544 | DoSetFocus(); | |
545 | ||
546 | event.Skip(); | |
547 | } | |
548 | ||
549 | bool 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 | ||
559 | bool wxSetFocusToChild(wxWindow *win, wxWindow **childLastFocused) | |
560 | { | |
561 | wxCHECK_MSG( win, false, _T("wxSetFocusToChild(): invalid window") ); | |
562 | wxCHECK_MSG( childLastFocused, false, | |
563 | _T("wxSetFocusToChild(): NULL child poonter") ); | |
564 | ||
565 | if ( *childLastFocused ) | |
566 | { | |
567 | // It might happen that the window got reparented | |
568 | if ( (*childLastFocused)->GetParent() == win ) | |
569 | { | |
570 | wxLogTrace(_T("focus"), | |
571 | _T("SetFocusToChild() => last child (0x%08lx)."), | |
572 | (unsigned long)(*childLastFocused)->GetHandle()); | |
573 | ||
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(); | |
577 | return true; | |
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 | |
587 | wxWindowList::compatibility_iterator node = win->GetChildren().GetFirst(); | |
588 | while ( node ) | |
589 | { | |
590 | wxWindow *child = node->GetData(); | |
591 | ||
592 | if ( child->AcceptsFocusFromKeyboard() && !child->IsTopLevel() ) | |
593 | { | |
594 | #ifdef __WXMSW__ | |
595 | // If a radiobutton is the first focusable child, search for the | |
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 | ||
606 | wxLogTrace(_T("focus"), | |
607 | _T("SetFocusToChild() => first child (0x%08lx)."), | |
608 | (unsigned long)child->GetHandle()); | |
609 | ||
610 | *childLastFocused = child; | |
611 | child->SetFocusFromKbd(); | |
612 | return true; | |
613 | } | |
614 | ||
615 | node = node->GetNext(); | |
616 | } | |
617 | ||
618 | return false; | |
619 | } | |
620 |