]>
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 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) | |
21 | #pragma implementation "containr.h" | |
22 | #endif | |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
28 | #pragma hdrstop | |
29 | #endif | |
30 | ||
31 | #ifndef WX_PRECOMP | |
32 | #include "wx/log.h" | |
33 | #include "wx/event.h" | |
34 | #include "wx/window.h" | |
35 | #endif //WX_PRECOMP | |
36 | ||
37 | #include "wx/containr.h" | |
38 | ||
39 | #ifdef __WXMAC__ | |
40 | #include "wx/scrolbar.h" | |
41 | #endif | |
42 | ||
43 | #ifdef __WXMSW__ | |
44 | #include "wx/radiobut.h" | |
45 | #endif | |
46 | ||
47 | // ============================================================================ | |
48 | // implementation | |
49 | // ============================================================================ | |
50 | ||
51 | wxControlContainer::wxControlContainer(wxWindow *winParent) | |
52 | { | |
53 | m_winParent = winParent; | |
54 | ||
55 | m_winLastFocused = | |
56 | m_winTmpDefault = | |
57 | m_winDefault = NULL; | |
58 | m_inSetFocus = false; | |
59 | } | |
60 | ||
61 | bool wxControlContainer::AcceptsFocus() const | |
62 | { | |
63 | // if we're not shown or disabled, we can't accept focus | |
64 | if ( m_winParent->IsShown() && m_winParent->IsEnabled() ) | |
65 | { | |
66 | // otherwise we can accept focus either if we have no children at all | |
67 | // (in this case we're probably not used as a container) or only when | |
68 | // at least one child will accept focus | |
69 | wxWindowList::compatibility_iterator node = m_winParent->GetChildren().GetFirst(); | |
70 | if ( !node ) | |
71 | return true; | |
72 | ||
73 | #ifdef __WXMAC__ | |
74 | // wxMac has eventually the two scrollbars as children, they don't count | |
75 | // as real children in the algorithm mentioned above | |
76 | bool hasRealChildren = false ; | |
77 | #endif | |
78 | ||
79 | while ( node ) | |
80 | { | |
81 | wxWindow *child = node->GetData(); | |
82 | ||
83 | if ( child->AcceptsFocus() ) | |
84 | { | |
85 | return true; | |
86 | } | |
87 | ||
88 | #ifdef __WXMAC__ | |
89 | wxScrollBar *sb = wxDynamicCast( child , wxScrollBar ) ; | |
90 | if ( sb == NULL || !m_winParent->MacIsWindowScrollbar( sb ) ) | |
91 | hasRealChildren = true ; | |
92 | #endif | |
93 | node = node->GetNext(); | |
94 | } | |
95 | ||
96 | #ifdef __WXMAC__ | |
97 | if ( !hasRealChildren ) | |
98 | return true ; | |
99 | #endif | |
100 | } | |
101 | ||
102 | return false; | |
103 | } | |
104 | ||
105 | void wxControlContainer::SetLastFocus(wxWindow *win) | |
106 | { | |
107 | // the panel itself should never get the focus at all but if it does happen | |
108 | // temporarily (as it seems to do under wxGTK), at the very least don't | |
109 | // forget our previous m_winLastFocused | |
110 | if ( win != m_winParent ) | |
111 | { | |
112 | // if we're setting the focus | |
113 | if ( win ) | |
114 | { | |
115 | // find the last _immediate_ child which got focus | |
116 | wxWindow *winParent = win; | |
117 | while ( winParent != m_winParent ) | |
118 | { | |
119 | win = winParent; | |
120 | winParent = win->GetParent(); | |
121 | ||
122 | // Yes, this can happen, though in a totally pathological case. | |
123 | // like when detaching a menubar from a frame with a child | |
124 | // which has pushed itself as an event handler for the menubar. | |
125 | // (under wxGTK) | |
126 | ||
127 | wxASSERT_MSG( winParent, | |
128 | _T("Setting last focus for a window that is not our child?") ); | |
129 | } | |
130 | } | |
131 | ||
132 | m_winLastFocused = win; | |
133 | ||
134 | if ( win ) | |
135 | { | |
136 | wxLogTrace(_T("focus"), _T("Set last focus to %s(%s)"), | |
137 | win->GetClassInfo()->GetClassName(), | |
138 | win->GetLabel().c_str()); | |
139 | } | |
140 | else | |
141 | { | |
142 | wxLogTrace(_T("focus"), _T("No more last focus")); | |
143 | } | |
144 | } | |
145 | ||
146 | // propagate the last focus upwards so that our parent can set focus back | |
147 | // to us if it loses it now and regains later | |
148 | wxWindow *parent = m_winParent->GetParent(); | |
149 | if ( parent ) | |
150 | { | |
151 | wxChildFocusEvent eventFocus(m_winParent); | |
152 | parent->GetEventHandler()->ProcessEvent(eventFocus); | |
153 | } | |
154 | } | |
155 | ||
156 | // ---------------------------------------------------------------------------- | |
157 | // Keyboard handling - this is the place where the TAB traversal logic is | |
158 | // implemented. As this code is common to all ports, this ensures consistent | |
159 | // behaviour even if we don't specify how exactly the wxNavigationKeyEvent are | |
160 | // generated and this is done in platform specific code which also ensures that | |
161 | // we can follow the given platform standards. | |
162 | // ---------------------------------------------------------------------------- | |
163 | ||
164 | void wxControlContainer::HandleOnNavigationKey( wxNavigationKeyEvent& event ) | |
165 | { | |
166 | wxWindow *parent = m_winParent->GetParent(); | |
167 | ||
168 | // the event is propagated downwards if the event emitter was our parent | |
169 | bool goingDown = event.GetEventObject() == parent; | |
170 | ||
171 | const wxWindowList& children = m_winParent->GetChildren(); | |
172 | ||
173 | // there is not much to do if we don't have children and we're not | |
174 | // interested in "notebook page change" events here | |
175 | if ( !children.GetCount() || event.IsWindowChange() ) | |
176 | { | |
177 | // let the parent process it unless it already comes from our parent | |
178 | // of we don't have any | |
179 | if ( goingDown || | |
180 | !parent || !parent->GetEventHandler()->ProcessEvent(event) ) | |
181 | { | |
182 | event.Skip(); | |
183 | } | |
184 | ||
185 | return; | |
186 | } | |
187 | ||
188 | // where are we going? | |
189 | bool forward = event.GetDirection(); | |
190 | ||
191 | // the node of the children list from which we should start looking for the | |
192 | // next acceptable child | |
193 | wxWindowList::compatibility_iterator node, start_node; | |
194 | ||
195 | // we should start from the first/last control and not from the one which | |
196 | // had focus the last time if we're propagating the event downwards because | |
197 | // for our parent we look like a single control | |
198 | if ( goingDown ) | |
199 | { | |
200 | // just to be sure it's not used (normally this is not necessary, but | |
201 | // doesn't hurt neither) | |
202 | m_winLastFocused = (wxWindow *)NULL; | |
203 | ||
204 | // start from first or last depending on where we're going | |
205 | node = forward ? children.GetFirst() : children.GetLast(); | |
206 | ||
207 | // we want to cycle over all nodes | |
208 | start_node = wxWindowList::compatibility_iterator(); | |
209 | } | |
210 | else | |
211 | { | |
212 | // try to find the child which has the focus currently | |
213 | ||
214 | // the event emitter might have done this for us | |
215 | wxWindow *winFocus = event.GetCurrentFocus(); | |
216 | ||
217 | // but if not, we might know where the focus was ourselves | |
218 | if (!winFocus) | |
219 | winFocus = m_winLastFocused; | |
220 | ||
221 | // if still no luck, do it the hard way | |
222 | if (!winFocus) | |
223 | winFocus = wxWindow::FindFocus(); | |
224 | ||
225 | if ( winFocus ) | |
226 | { | |
227 | // ok, we found the focus - now is it our child? | |
228 | start_node = children.Find( winFocus ); | |
229 | } | |
230 | else | |
231 | { | |
232 | start_node = wxWindowList::compatibility_iterator(); | |
233 | } | |
234 | ||
235 | if ( !start_node && m_winLastFocused ) | |
236 | { | |
237 | // window which has focus isn't our child, fall back to the one | |
238 | // which had the focus the last time | |
239 | start_node = children.Find( m_winLastFocused ); | |
240 | } | |
241 | ||
242 | // if we still didn't find anything, we should start with the first one | |
243 | if ( !start_node ) | |
244 | { | |
245 | start_node = children.GetFirst(); | |
246 | } | |
247 | ||
248 | // and the first child which we can try setting focus to is the next or | |
249 | // the previous one | |
250 | node = forward ? start_node->GetNext() : start_node->GetPrevious(); | |
251 | } | |
252 | ||
253 | // we want to cycle over all elements passing by NULL | |
254 | while ( node != start_node ) | |
255 | { | |
256 | // Have we come to the last or first item on the panel? | |
257 | if ( !node ) | |
258 | { | |
259 | if ( !goingDown ) | |
260 | { | |
261 | // Check if our (may be grand) parent is another panel: if this | |
262 | // is the case, they will know what to do with this navigation | |
263 | // key and so give them the chance to process it instead of | |
264 | // looping inside this panel (normally, the focus will go to | |
265 | // the next/previous item after this panel in the parent | |
266 | // panel). | |
267 | wxWindow *focussed_child_of_parent = m_winParent; | |
268 | while ( parent ) | |
269 | { | |
270 | // we don't want to tab into a different dialog or frame | |
271 | if ( focussed_child_of_parent->IsTopLevel() ) | |
272 | break; | |
273 | ||
274 | event.SetCurrentFocus( focussed_child_of_parent ); | |
275 | if ( parent->GetEventHandler()->ProcessEvent( event ) ) | |
276 | return; | |
277 | ||
278 | focussed_child_of_parent = parent; | |
279 | ||
280 | parent = parent->GetParent(); | |
281 | } | |
282 | } | |
283 | //else: as the focus came from our parent, we definitely don't want | |
284 | // to send it back to it! | |
285 | ||
286 | // no, we are not inside another panel so process this ourself | |
287 | node = forward ? children.GetFirst() : children.GetLast(); | |
288 | ||
289 | continue; | |
290 | } | |
291 | ||
292 | wxWindow *child = node->GetData(); | |
293 | ||
294 | #ifdef __WXMSW__ | |
295 | bool is_not_msw_rb = ! wxIsKindOf(m_winLastFocused,wxRadioButton); | |
296 | #else | |
297 | bool is_not_msw_rb = true; | |
298 | ||
299 | #endif | |
300 | ||
301 | if ( child->AcceptsFocusFromKeyboard() && is_not_msw_rb) | |
302 | { | |
303 | // if we're setting the focus to a child panel we should prevent it | |
304 | // from giving it to the child which had the focus the last time | |
305 | // and instead give it to the first/last child depending from which | |
306 | // direction we're coming | |
307 | event.SetEventObject(m_winParent); | |
308 | ||
309 | #ifdef __WXMSW__ | |
310 | // we need to hop to the next activated | |
311 | // radio button, not just the next radio | |
312 | // button under MSW | |
313 | if (wxIsKindOf(child,wxRadioButton)) | |
314 | { | |
315 | wxRadioButton *rb = (wxRadioButton*) child; | |
316 | if (!rb->GetValue()) | |
317 | { | |
318 | for (;;) | |
319 | { | |
320 | wxWindowList::compatibility_iterator node = children.Find( child ); | |
321 | if (forward) | |
322 | node = node->GetNext(); | |
323 | else | |
324 | node = node->GetPrevious(); | |
325 | if (!node) return; // this would probably an error | |
326 | child = node->GetData(); | |
327 | if (!wxIsKindOf(child,wxRadioButton)) continue; | |
328 | rb = (wxRadioButton*) child; | |
329 | if (rb->GetValue()) break; | |
330 | } | |
331 | } | |
332 | } | |
333 | #endif | |
334 | // disable propagation for this call as otherwise the event might | |
335 | // bounce back to us. | |
336 | wxPropagationDisabler disableProp(event); | |
337 | if ( !child->GetEventHandler()->ProcessEvent(event) ) | |
338 | { | |
339 | // set it first in case SetFocusFromKbd() results in focus | |
340 | // change too | |
341 | m_winLastFocused = child; | |
342 | ||
343 | // everything is simple: just give focus to it | |
344 | child->SetFocusFromKbd(); | |
345 | } | |
346 | //else: the child manages its focus itself | |
347 | ||
348 | event.Skip( false ); | |
349 | ||
350 | return; | |
351 | } | |
352 | ||
353 | node = forward ? node->GetNext() : node->GetPrevious(); | |
354 | } | |
355 | ||
356 | // we cycled through all of our children and none of them wanted to accept | |
357 | // focus | |
358 | event.Skip(); | |
359 | } | |
360 | ||
361 | void wxControlContainer::HandleOnWindowDestroy(wxWindowBase *child) | |
362 | { | |
363 | if ( child == m_winLastFocused ) | |
364 | m_winLastFocused = NULL; | |
365 | ||
366 | if ( child == m_winDefault ) | |
367 | m_winDefault = NULL; | |
368 | ||
369 | if ( child == m_winTmpDefault ) | |
370 | m_winTmpDefault = NULL; | |
371 | } | |
372 | ||
373 | // ---------------------------------------------------------------------------- | |
374 | // focus handling | |
375 | // ---------------------------------------------------------------------------- | |
376 | ||
377 | bool wxControlContainer::DoSetFocus() | |
378 | { | |
379 | wxLogTrace(_T("focus"), _T("SetFocus on wxPanel 0x%08lx."), | |
380 | (unsigned long)m_winParent->GetHandle()); | |
381 | ||
382 | if (m_inSetFocus) | |
383 | return true; | |
384 | ||
385 | // when the panel gets the focus we move the focus to either the last | |
386 | // window that had the focus or the first one that can get it unless the | |
387 | // focus had been already set to some other child | |
388 | ||
389 | wxWindow *win = wxWindow::FindFocus(); | |
390 | while ( win ) | |
391 | { | |
392 | if ( win == m_winParent ) | |
393 | { | |
394 | // our child already has focus, don't take it away from it | |
395 | return true; | |
396 | } | |
397 | ||
398 | if ( win->IsTopLevel() ) | |
399 | { | |
400 | // don't look beyond the first top level parent - useless and | |
401 | // unnecessary | |
402 | break; | |
403 | } | |
404 | ||
405 | win = win->GetParent(); | |
406 | } | |
407 | ||
408 | // protect against infinite recursion: | |
409 | m_inSetFocus = true; | |
410 | ||
411 | bool ret = SetFocusToChild(); | |
412 | ||
413 | m_inSetFocus = false; | |
414 | ||
415 | return ret; | |
416 | } | |
417 | ||
418 | void wxControlContainer::HandleOnFocus(wxFocusEvent& event) | |
419 | { | |
420 | wxLogTrace(_T("focus"), _T("OnFocus on wxPanel 0x%08lx, name: %s"), | |
421 | (unsigned long)m_winParent->GetHandle(), | |
422 | m_winParent->GetName().c_str() ); | |
423 | ||
424 | DoSetFocus(); | |
425 | ||
426 | event.Skip(); | |
427 | } | |
428 | ||
429 | bool wxControlContainer::SetFocusToChild() | |
430 | { | |
431 | return wxSetFocusToChild(m_winParent, &m_winLastFocused); | |
432 | } | |
433 | ||
434 | // ---------------------------------------------------------------------------- | |
435 | // SetFocusToChild(): this function is used by wxPanel but also by wxFrame in | |
436 | // wxMSW, this is why it is outside of wxControlContainer class | |
437 | // ---------------------------------------------------------------------------- | |
438 | ||
439 | bool wxSetFocusToChild(wxWindow *win, wxWindow **childLastFocused) | |
440 | { | |
441 | wxCHECK_MSG( win, false, _T("wxSetFocusToChild(): invalid window") ); | |
442 | wxCHECK_MSG( childLastFocused, false, | |
443 | _T("wxSetFocusToChild(): NULL child poonter") ); | |
444 | ||
445 | if ( *childLastFocused ) | |
446 | { | |
447 | // It might happen that the window got reparented | |
448 | if ( (*childLastFocused)->GetParent() == win ) | |
449 | { | |
450 | wxLogTrace(_T("focus"), | |
451 | _T("SetFocusToChild() => last child (0x%08lx)."), | |
452 | (unsigned long)(*childLastFocused)->GetHandle()); | |
453 | ||
454 | // not SetFocusFromKbd(): we're restoring focus back to the old | |
455 | // window and not setting it as the result of a kbd action | |
456 | (*childLastFocused)->SetFocus(); | |
457 | return true; | |
458 | } | |
459 | else | |
460 | { | |
461 | // it doesn't count as such any more | |
462 | *childLastFocused = (wxWindow *)NULL; | |
463 | } | |
464 | } | |
465 | ||
466 | // set the focus to the first child who wants it | |
467 | wxWindowList::compatibility_iterator node = win->GetChildren().GetFirst(); | |
468 | while ( node ) | |
469 | { | |
470 | wxWindow *child = node->GetData(); | |
471 | ||
472 | if ( child->AcceptsFocusFromKeyboard() && !child->IsTopLevel() ) | |
473 | { | |
474 | wxLogTrace(_T("focus"), | |
475 | _T("SetFocusToChild() => first child (0x%08lx)."), | |
476 | (unsigned long)child->GetHandle()); | |
477 | ||
478 | *childLastFocused = child; | |
479 | child->SetFocusFromKbd(); | |
480 | return true; | |
481 | } | |
482 | ||
483 | node = node->GetNext(); | |
484 | } | |
485 | ||
486 | return false; | |
487 | } | |
488 |