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