]>
Commit | Line | Data |
---|---|---|
456bc6d9 VZ |
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> | |
55d99c7a | 9 | // License: wxWindows licence |
456bc6d9 VZ |
10 | /////////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | #ifdef __GNUG__ | |
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 | |
6285be72 VZ |
32 | #include "wx/log.h" |
33 | #include "wx/event.h" | |
34 | #include "wx/window.h" | |
456bc6d9 VZ |
35 | #endif //WX_PRECOMP |
36 | ||
37 | #include "wx/containr.h" | |
38 | ||
39 | // ============================================================================ | |
40 | // implementation | |
41 | // ============================================================================ | |
42 | ||
43 | wxControlContainer::wxControlContainer(wxWindow *winParent) | |
44 | { | |
45 | m_winParent = winParent; | |
46 | ||
47 | m_winLastFocused = | |
036da5e3 | 48 | m_winTmpDefault = |
456bc6d9 VZ |
49 | m_winDefault = NULL; |
50 | } | |
51 | ||
3251b834 VZ |
52 | bool wxControlContainer::AcceptsFocus() const |
53 | { | |
54 | // We can accept focus only when at last one child will accept focus | |
55 | if ( m_winParent->IsShown() && m_winParent->IsEnabled() ) | |
56 | { | |
57 | wxWindowList::Node *node = m_winParent->GetChildren().GetFirst(); | |
58 | while ( node ) | |
59 | { | |
60 | wxWindow *child = node->GetData(); | |
61 | ||
62 | if ( child->AcceptsFocus() ) | |
63 | { | |
64 | return TRUE; | |
65 | } | |
66 | ||
67 | node = node->GetNext(); | |
68 | } | |
69 | } | |
70 | ||
71 | return FALSE; | |
72 | } | |
73 | ||
456bc6d9 VZ |
74 | void wxControlContainer::SetLastFocus(wxWindow *win) |
75 | { | |
6aeb6f2a VZ |
76 | // the panel itself should never get the focus at all but if it does happen |
77 | // temporarily (as it seems to do under wxGTK), at the very least don't | |
78 | // forget our previous m_winLastFocused | |
c25b5d1f | 79 | if ( win != m_winParent ) |
456bc6d9 | 80 | { |
c25b5d1f VZ |
81 | // if we're setting the focus |
82 | if ( win ) | |
83c865f5 | 83 | { |
c25b5d1f VZ |
84 | // find the last _immediate_ child which got focus |
85 | wxWindow *winParent = win; | |
86 | while ( winParent != m_winParent ) | |
87 | { | |
88 | win = winParent; | |
89 | winParent = win->GetParent(); | |
83c865f5 | 90 | |
c25b5d1f VZ |
91 | // Yes, this can happen, though in a totally pathological case. |
92 | // like when detaching a menubar from a frame with a child | |
93 | // which has pushed itself as an event handler for the menubar. | |
94 | // (under wxGTK) | |
6f8239de | 95 | |
c25b5d1f VZ |
96 | wxASSERT_MSG( winParent, |
97 | _T("Setting last focus for a window that is not our child?") ); | |
98 | } | |
6f8239de | 99 | } |
456bc6d9 | 100 | |
c25b5d1f | 101 | m_winLastFocused = win; |
6aeb6f2a | 102 | |
c25b5d1f VZ |
103 | if ( win ) |
104 | { | |
105 | wxLogTrace(_T("focus"), _T("Set last focus to %s(%s)"), | |
106 | win->GetClassInfo()->GetClassName(), | |
107 | win->GetLabel().c_str()); | |
108 | } | |
109 | else | |
110 | { | |
111 | wxLogTrace(_T("focus"), _T("No more last focus")); | |
112 | } | |
6aeb6f2a | 113 | } |
c25b5d1f VZ |
114 | |
115 | // propagate the last focus upwards so that our parent can set focus back | |
116 | // to us if it loses it now and regains later | |
117 | wxWindow *parent = m_winParent->GetParent(); | |
118 | if ( parent ) | |
6aeb6f2a | 119 | { |
c25b5d1f VZ |
120 | wxChildFocusEvent eventFocus(m_winParent); |
121 | parent->GetEventHandler()->ProcessEvent(eventFocus); | |
6aeb6f2a | 122 | } |
456bc6d9 VZ |
123 | } |
124 | ||
125 | // ---------------------------------------------------------------------------- | |
126 | // Keyboard handling - this is the place where the TAB traversal logic is | |
127 | // implemented. As this code is common to all ports, this ensures consistent | |
128 | // behaviour even if we don't specify how exactly the wxNavigationKeyEvent are | |
129 | // generated and this is done in platform specific code which also ensures that | |
130 | // we can follow the given platform standards. | |
131 | // ---------------------------------------------------------------------------- | |
132 | ||
133 | void wxControlContainer::HandleOnNavigationKey( wxNavigationKeyEvent& event ) | |
134 | { | |
135 | wxWindow *parent = m_winParent->GetParent(); | |
136 | ||
137 | // the event is propagated downwards if the event emitter was our parent | |
138 | bool goingDown = event.GetEventObject() == parent; | |
139 | ||
140 | const wxWindowList& children = m_winParent->GetChildren(); | |
141 | ||
142 | // there is not much to do if we don't have children and we're not | |
143 | // interested in "notebook page change" events here | |
144 | if ( !children.GetCount() || event.IsWindowChange() ) | |
145 | { | |
146 | // let the parent process it unless it already comes from our parent | |
147 | // of we don't have any | |
148 | if ( goingDown || | |
149 | !parent || !parent->GetEventHandler()->ProcessEvent(event) ) | |
150 | { | |
151 | event.Skip(); | |
152 | } | |
153 | ||
154 | return; | |
155 | } | |
156 | ||
157 | // where are we going? | |
158 | bool forward = event.GetDirection(); | |
159 | ||
160 | // the node of the children list from which we should start looking for the | |
161 | // next acceptable child | |
162 | wxWindowList::Node *node, *start_node; | |
163 | ||
164 | // we should start from the first/last control and not from the one which | |
165 | // had focus the last time if we're propagating the event downwards because | |
166 | // for our parent we look like a single control | |
167 | if ( goingDown ) | |
168 | { | |
169 | // just to be sure it's not used (normally this is not necessary, but | |
170 | // doesn't hurt neither) | |
171 | m_winLastFocused = (wxWindow *)NULL; | |
172 | ||
173 | // start from first or last depending on where we're going | |
174 | node = forward ? children.GetFirst() : children.GetLast(); | |
175 | ||
176 | // we want to cycle over all nodes | |
177 | start_node = (wxWindowList::Node *)NULL; | |
178 | } | |
179 | else | |
180 | { | |
181 | // try to find the child which has the focus currently | |
182 | ||
183 | // the event emitter might have done this for us | |
184 | wxWindow *winFocus = event.GetCurrentFocus(); | |
185 | ||
186 | // but if not, we might know where the focus was ourselves | |
187 | if (!winFocus) | |
188 | winFocus = m_winLastFocused; | |
189 | ||
190 | // if still no luck, do it the hard way | |
191 | if (!winFocus) | |
192 | winFocus = wxWindow::FindFocus(); | |
193 | ||
194 | if ( winFocus ) | |
195 | { | |
196 | // ok, we found the focus - now is it our child? | |
197 | start_node = children.Find( winFocus ); | |
198 | } | |
199 | else | |
200 | { | |
201 | start_node = (wxWindowList::Node *)NULL; | |
202 | } | |
203 | ||
204 | if ( !start_node && m_winLastFocused ) | |
205 | { | |
206 | // window which has focus isn't our child, fall back to the one | |
207 | // which had the focus the last time | |
208 | start_node = children.Find( m_winLastFocused ); | |
209 | } | |
210 | ||
211 | // if we still didn't find anything, we should start with the first one | |
212 | if ( !start_node ) | |
213 | { | |
214 | start_node = children.GetFirst(); | |
215 | } | |
216 | ||
217 | // and the first child which we can try setting focus to is the next or | |
218 | // the previous one | |
219 | node = forward ? start_node->GetNext() : start_node->GetPrevious(); | |
220 | } | |
221 | ||
222 | // we want to cycle over all elements passing by NULL | |
223 | while ( node != start_node ) | |
224 | { | |
225 | // Have we come to the last or first item on the panel? | |
226 | if ( !node ) | |
227 | { | |
228 | if ( !goingDown ) | |
229 | { | |
230 | // Check if our (may be grand) parent is another panel: if this | |
231 | // is the case, they will know what to do with this navigation | |
232 | // key and so give them the chance to process it instead of | |
233 | // looping inside this panel (normally, the focus will go to | |
234 | // the next/previous item after this panel in the parent | |
235 | // panel). | |
236 | wxWindow *focussed_child_of_parent = m_winParent; | |
237 | while ( parent ) | |
238 | { | |
239 | // we don't want to tab into a different dialog or frame | |
240 | if ( focussed_child_of_parent->IsTopLevel() ) | |
241 | break; | |
242 | ||
243 | event.SetCurrentFocus( focussed_child_of_parent ); | |
244 | if ( parent->GetEventHandler()->ProcessEvent( event ) ) | |
245 | return; | |
246 | ||
247 | focussed_child_of_parent = parent; | |
248 | ||
249 | parent = parent->GetParent(); | |
250 | } | |
251 | } | |
252 | //else: as the focus came from our parent, we definitely don't want | |
253 | // to send it back to it! | |
254 | ||
255 | // no, we are not inside another panel so process this ourself | |
256 | node = forward ? children.GetFirst() : children.GetLast(); | |
257 | ||
258 | continue; | |
259 | } | |
260 | ||
261 | wxWindow *child = node->GetData(); | |
262 | ||
263 | if ( child->AcceptsFocusFromKeyboard() ) | |
264 | { | |
265 | // if we're setting the focus to a child panel we should prevent it | |
266 | // from giving it to the child which had the focus the last time | |
267 | // and instead give it to the first/last child depending from which | |
268 | // direction we're coming | |
269 | event.SetEventObject(m_winParent); | |
270 | if ( !child->GetEventHandler()->ProcessEvent(event) ) | |
271 | { | |
2b5f62a0 VZ |
272 | // set it first in case SetFocusFromKbd() results in focus |
273 | // change too | |
274 | m_winLastFocused = child; | |
275 | ||
456bc6d9 | 276 | // everything is simple: just give focus to it |
5463c0a4 | 277 | child->SetFocusFromKbd(); |
456bc6d9 VZ |
278 | } |
279 | //else: the child manages its focus itself | |
280 | ||
281 | event.Skip( FALSE ); | |
282 | ||
283 | return; | |
284 | } | |
285 | ||
286 | node = forward ? node->GetNext() : node->GetPrevious(); | |
287 | } | |
288 | ||
289 | // we cycled through all of our children and none of them wanted to accept | |
290 | // focus | |
291 | event.Skip(); | |
292 | } | |
293 | ||
294 | void wxControlContainer::HandleOnWindowDestroy(wxWindowBase *child) | |
295 | { | |
296 | if ( child == m_winLastFocused ) | |
297 | m_winLastFocused = NULL; | |
298 | ||
299 | if ( child == m_winDefault ) | |
300 | m_winDefault = NULL; | |
036da5e3 VZ |
301 | |
302 | if ( child == m_winTmpDefault ) | |
303 | m_winTmpDefault = NULL; | |
456bc6d9 VZ |
304 | } |
305 | ||
306 | // ---------------------------------------------------------------------------- | |
307 | // focus handling | |
308 | // ---------------------------------------------------------------------------- | |
309 | ||
24a7a198 | 310 | bool wxControlContainer::DoSetFocus() |
456bc6d9 | 311 | { |
993eebf1 VZ |
312 | wxLogTrace(_T("focus"), _T("SetFocus on wxPanel 0x%08lx."), |
313 | (unsigned long)m_winParent->GetHandle()); | |
456bc6d9 | 314 | |
2b5f62a0 VZ |
315 | // when the panel gets the focus we move the focus to either the last |
316 | // window that had the focus or the first one that can get it unless the | |
317 | // focus had been already set to some other child | |
318 | ||
0dcdeee9 VZ |
319 | wxWindow *win = wxWindow::FindFocus(); |
320 | while ( win ) | |
321 | { | |
322 | if ( win == m_winParent ) | |
2b5f62a0 VZ |
323 | { |
324 | // our child already has focus, don't take it away from it | |
0dcdeee9 | 325 | return TRUE; |
2b5f62a0 | 326 | } |
0dcdeee9 VZ |
327 | |
328 | if ( win->IsTopLevel() ) | |
329 | { | |
330 | // don't look beyond the first top level parent - useless and | |
331 | // unnecessary | |
332 | break; | |
333 | } | |
334 | ||
335 | win = win->GetParent(); | |
336 | } | |
337 | ||
24a7a198 | 338 | return SetFocusToChild(); |
456bc6d9 VZ |
339 | } |
340 | ||
341 | void wxControlContainer::HandleOnFocus(wxFocusEvent& event) | |
342 | { | |
993eebf1 VZ |
343 | wxLogTrace(_T("focus"), _T("OnFocus on wxPanel 0x%08lx, name: %s"), |
344 | (unsigned long)m_winParent->GetHandle(), | |
456bc6d9 VZ |
345 | m_winParent->GetName().c_str() ); |
346 | ||
2b5f62a0 | 347 | DoSetFocus(); |
456bc6d9 VZ |
348 | |
349 | event.Skip(); | |
350 | } | |
351 | ||
352 | bool wxControlContainer::SetFocusToChild() | |
353 | { | |
354 | return wxSetFocusToChild(m_winParent, &m_winLastFocused); | |
355 | } | |
356 | ||
357 | // ---------------------------------------------------------------------------- | |
358 | // SetFocusToChild(): this function is used by wxPanel but also by wxFrame in | |
359 | // wxMSW, this is why it is outside of wxControlContainer class | |
360 | // ---------------------------------------------------------------------------- | |
361 | ||
362 | bool wxSetFocusToChild(wxWindow *win, wxWindow **childLastFocused) | |
363 | { | |
364 | wxCHECK_MSG( win, FALSE, _T("wxSetFocusToChild(): invalid window") ); | |
c25b5d1f VZ |
365 | wxCHECK_MSG( childLastFocused, FALSE, |
366 | _T("wxSetFocusToChild(): NULL child poonter") ); | |
456bc6d9 VZ |
367 | |
368 | if ( *childLastFocused ) | |
369 | { | |
c25b5d1f VZ |
370 | // It might happen that the window got reparented |
371 | if ( (*childLastFocused)->GetParent() == win ) | |
456bc6d9 VZ |
372 | { |
373 | wxLogTrace(_T("focus"), | |
993eebf1 VZ |
374 | _T("SetFocusToChild() => last child (0x%08lx)."), |
375 | (unsigned long)(*childLastFocused)->GetHandle()); | |
456bc6d9 | 376 | |
c25b5d1f VZ |
377 | // not SetFocusFromKbd(): we're restoring focus back to the old |
378 | // window and not setting it as the result of a kbd action | |
379 | (*childLastFocused)->SetFocus(); | |
456bc6d9 VZ |
380 | return TRUE; |
381 | } | |
382 | else | |
383 | { | |
384 | // it doesn't count as such any more | |
385 | *childLastFocused = (wxWindow *)NULL; | |
386 | } | |
387 | } | |
388 | ||
389 | // set the focus to the first child who wants it | |
390 | wxWindowList::Node *node = win->GetChildren().GetFirst(); | |
391 | while ( node ) | |
392 | { | |
393 | wxWindow *child = node->GetData(); | |
394 | ||
395 | if ( child->AcceptsFocusFromKeyboard() && !child->IsTopLevel() ) | |
396 | { | |
397 | wxLogTrace(_T("focus"), | |
993eebf1 VZ |
398 | _T("SetFocusToChild() => first child (0x%08lx)."), |
399 | (unsigned long)child->GetHandle()); | |
456bc6d9 VZ |
400 | |
401 | *childLastFocused = child; // should be redundant, but it is not | |
5463c0a4 | 402 | child->SetFocusFromKbd(); |
456bc6d9 VZ |
403 | return TRUE; |
404 | } | |
405 | ||
406 | node = node->GetNext(); | |
407 | } | |
408 | ||
409 | return FALSE; | |
410 | } | |
3251b834 | 411 |