]>
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 license | |
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 | |
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 | // ============================================================================ | |
40 | // implementation | |
41 | // ============================================================================ | |
42 | ||
43 | wxControlContainer::wxControlContainer(wxWindow *winParent) | |
44 | { | |
45 | m_winParent = winParent; | |
46 | ||
47 | m_winLastFocused = | |
48 | m_winDefault = NULL; | |
49 | } | |
50 | ||
51 | void wxControlContainer::SetLastFocus(wxWindow *win) | |
52 | { | |
53 | // find the last _immediate_ child which got focus | |
54 | while ( win && win != m_winParent ) | |
55 | { | |
56 | win = win->GetParent(); | |
57 | } | |
58 | ||
59 | wxASSERT_MSG( win, _T("attempt to set last focus to not a child?") ); | |
60 | ||
61 | m_winLastFocused = win; | |
62 | } | |
63 | ||
64 | // ---------------------------------------------------------------------------- | |
65 | // Keyboard handling - this is the place where the TAB traversal logic is | |
66 | // implemented. As this code is common to all ports, this ensures consistent | |
67 | // behaviour even if we don't specify how exactly the wxNavigationKeyEvent are | |
68 | // generated and this is done in platform specific code which also ensures that | |
69 | // we can follow the given platform standards. | |
70 | // ---------------------------------------------------------------------------- | |
71 | ||
72 | void wxControlContainer::HandleOnNavigationKey( wxNavigationKeyEvent& event ) | |
73 | { | |
74 | wxWindow *parent = m_winParent->GetParent(); | |
75 | ||
76 | // the event is propagated downwards if the event emitter was our parent | |
77 | bool goingDown = event.GetEventObject() == parent; | |
78 | ||
79 | const wxWindowList& children = m_winParent->GetChildren(); | |
80 | ||
81 | // there is not much to do if we don't have children and we're not | |
82 | // interested in "notebook page change" events here | |
83 | if ( !children.GetCount() || event.IsWindowChange() ) | |
84 | { | |
85 | // let the parent process it unless it already comes from our parent | |
86 | // of we don't have any | |
87 | if ( goingDown || | |
88 | !parent || !parent->GetEventHandler()->ProcessEvent(event) ) | |
89 | { | |
90 | event.Skip(); | |
91 | } | |
92 | ||
93 | return; | |
94 | } | |
95 | ||
96 | // where are we going? | |
97 | bool forward = event.GetDirection(); | |
98 | ||
99 | // the node of the children list from which we should start looking for the | |
100 | // next acceptable child | |
101 | wxWindowList::Node *node, *start_node; | |
102 | ||
103 | // we should start from the first/last control and not from the one which | |
104 | // had focus the last time if we're propagating the event downwards because | |
105 | // for our parent we look like a single control | |
106 | if ( goingDown ) | |
107 | { | |
108 | // just to be sure it's not used (normally this is not necessary, but | |
109 | // doesn't hurt neither) | |
110 | m_winLastFocused = (wxWindow *)NULL; | |
111 | ||
112 | // start from first or last depending on where we're going | |
113 | node = forward ? children.GetFirst() : children.GetLast(); | |
114 | ||
115 | // we want to cycle over all nodes | |
116 | start_node = (wxWindowList::Node *)NULL; | |
117 | } | |
118 | else | |
119 | { | |
120 | // try to find the child which has the focus currently | |
121 | ||
122 | // the event emitter might have done this for us | |
123 | wxWindow *winFocus = event.GetCurrentFocus(); | |
124 | ||
125 | // but if not, we might know where the focus was ourselves | |
126 | if (!winFocus) | |
127 | winFocus = m_winLastFocused; | |
128 | ||
129 | // if still no luck, do it the hard way | |
130 | if (!winFocus) | |
131 | winFocus = wxWindow::FindFocus(); | |
132 | ||
133 | if ( winFocus ) | |
134 | { | |
135 | // ok, we found the focus - now is it our child? | |
136 | start_node = children.Find( winFocus ); | |
137 | } | |
138 | else | |
139 | { | |
140 | start_node = (wxWindowList::Node *)NULL; | |
141 | } | |
142 | ||
143 | if ( !start_node && m_winLastFocused ) | |
144 | { | |
145 | // window which has focus isn't our child, fall back to the one | |
146 | // which had the focus the last time | |
147 | start_node = children.Find( m_winLastFocused ); | |
148 | } | |
149 | ||
150 | // if we still didn't find anything, we should start with the first one | |
151 | if ( !start_node ) | |
152 | { | |
153 | start_node = children.GetFirst(); | |
154 | } | |
155 | ||
156 | // and the first child which we can try setting focus to is the next or | |
157 | // the previous one | |
158 | node = forward ? start_node->GetNext() : start_node->GetPrevious(); | |
159 | } | |
160 | ||
161 | // we want to cycle over all elements passing by NULL | |
162 | while ( node != start_node ) | |
163 | { | |
164 | // Have we come to the last or first item on the panel? | |
165 | if ( !node ) | |
166 | { | |
167 | if ( !goingDown ) | |
168 | { | |
169 | // Check if our (may be grand) parent is another panel: if this | |
170 | // is the case, they will know what to do with this navigation | |
171 | // key and so give them the chance to process it instead of | |
172 | // looping inside this panel (normally, the focus will go to | |
173 | // the next/previous item after this panel in the parent | |
174 | // panel). | |
175 | wxWindow *focussed_child_of_parent = m_winParent; | |
176 | while ( parent ) | |
177 | { | |
178 | // we don't want to tab into a different dialog or frame | |
179 | if ( focussed_child_of_parent->IsTopLevel() ) | |
180 | break; | |
181 | ||
182 | event.SetCurrentFocus( focussed_child_of_parent ); | |
183 | if ( parent->GetEventHandler()->ProcessEvent( event ) ) | |
184 | return; | |
185 | ||
186 | focussed_child_of_parent = parent; | |
187 | ||
188 | parent = parent->GetParent(); | |
189 | } | |
190 | } | |
191 | //else: as the focus came from our parent, we definitely don't want | |
192 | // to send it back to it! | |
193 | ||
194 | // no, we are not inside another panel so process this ourself | |
195 | node = forward ? children.GetFirst() : children.GetLast(); | |
196 | ||
197 | continue; | |
198 | } | |
199 | ||
200 | wxWindow *child = node->GetData(); | |
201 | ||
202 | if ( child->AcceptsFocusFromKeyboard() ) | |
203 | { | |
204 | // if we're setting the focus to a child panel we should prevent it | |
205 | // from giving it to the child which had the focus the last time | |
206 | // and instead give it to the first/last child depending from which | |
207 | // direction we're coming | |
208 | event.SetEventObject(m_winParent); | |
209 | if ( !child->GetEventHandler()->ProcessEvent(event) ) | |
210 | { | |
211 | // everything is simple: just give focus to it | |
212 | child->SetFocus(); | |
213 | ||
214 | m_winLastFocused = child; | |
215 | } | |
216 | //else: the child manages its focus itself | |
217 | ||
218 | event.Skip( FALSE ); | |
219 | ||
220 | return; | |
221 | } | |
222 | ||
223 | node = forward ? node->GetNext() : node->GetPrevious(); | |
224 | } | |
225 | ||
226 | // we cycled through all of our children and none of them wanted to accept | |
227 | // focus | |
228 | event.Skip(); | |
229 | } | |
230 | ||
231 | void wxControlContainer::HandleOnWindowDestroy(wxWindowBase *child) | |
232 | { | |
233 | if ( child == m_winLastFocused ) | |
234 | m_winLastFocused = NULL; | |
235 | ||
236 | if ( child == m_winDefault ) | |
237 | m_winDefault = NULL; | |
238 | } | |
239 | ||
240 | // ---------------------------------------------------------------------------- | |
241 | // focus handling | |
242 | // ---------------------------------------------------------------------------- | |
243 | ||
244 | bool wxControlContainer::DoSetFocus() | |
245 | { | |
246 | wxLogTrace(_T("focus"), _T("SetFocus on wxPanel 0x%08x."), | |
247 | m_winParent->GetHandle()); | |
248 | ||
249 | // If the panel gets the focus *by way of getting it set directly* | |
250 | // we move the focus to the first window that can get it. | |
251 | ||
252 | // VZ: no, we set the focus to the last window too. I don't understand why | |
253 | // should we make this distinction: if an app wants to set focus to | |
254 | // some precise control, it may always do it directly, but if we don't | |
255 | // use m_winLastFocused here, the focus won't be set correctly after a | |
256 | // notebook page change nor after frame activation under MSW (it calls | |
257 | // SetFocus too) | |
258 | // | |
259 | // RR: yes, when I the tab key to navigate in a panel with some controls and | |
260 | // a notebook and the focus jumps to the notebook (typically coming from | |
261 | // a button at the top) the notebook should focus the first child in the | |
262 | // current notebook page, not the last one which would otherwise get the | |
263 | // focus if you used the tab key to navigate from the current notebook | |
264 | // page to button at the bottom. See every page in the controls sample. | |
265 | // | |
266 | // VZ: ok, but this still doesn't (at least I don't see how it can) take | |
267 | // care of first/last child problem: i.e. if Shift-TAB is pressed in a | |
268 | // situation like above, the focus should be given to the last child, | |
269 | // not the first one (and not to the last focused one neither) - I | |
270 | // think my addition to OnNavigationKey() above takes care of it. | |
271 | // Keeping #ifdef __WXGTK__ for now, but please try removing it and see | |
272 | // what happens. | |
273 | // | |
274 | // RR: Removed for now. Let's see what happens.. | |
275 | ||
276 | return SetFocusToChild(); | |
277 | } | |
278 | ||
279 | void wxControlContainer::HandleOnFocus(wxFocusEvent& event) | |
280 | { | |
281 | wxLogTrace(_T("focus"), _T("OnFocus on wxPanel 0x%08x, name: %s"), | |
282 | m_winParent->GetHandle(), | |
283 | m_winParent->GetName().c_str() ); | |
284 | ||
285 | // If we panel got the focus *by way of getting clicked on* | |
286 | // we move the focus to either the last window that had the | |
287 | // focus or the first one that can get it. | |
288 | (void)SetFocusToChild(); | |
289 | ||
290 | event.Skip(); | |
291 | } | |
292 | ||
293 | bool wxControlContainer::SetFocusToChild() | |
294 | { | |
295 | return wxSetFocusToChild(m_winParent, &m_winLastFocused); | |
296 | } | |
297 | ||
298 | // ---------------------------------------------------------------------------- | |
299 | // SetFocusToChild(): this function is used by wxPanel but also by wxFrame in | |
300 | // wxMSW, this is why it is outside of wxControlContainer class | |
301 | // ---------------------------------------------------------------------------- | |
302 | ||
303 | bool wxSetFocusToChild(wxWindow *win, wxWindow **childLastFocused) | |
304 | { | |
305 | wxCHECK_MSG( win, FALSE, _T("wxSetFocusToChild(): invalid window") ); | |
306 | ||
307 | if ( *childLastFocused ) | |
308 | { | |
309 | // It might happen that the window got reparented or no longer accepts | |
310 | // the focus. | |
311 | if ( (*childLastFocused)->GetParent() == win && | |
312 | (*childLastFocused)->AcceptsFocusFromKeyboard() ) | |
313 | { | |
314 | wxLogTrace(_T("focus"), | |
315 | _T("SetFocusToChild() => last child (0x%08x)."), | |
316 | (*childLastFocused)->GetHandle()); | |
317 | ||
318 | (*childLastFocused)->SetFocus(); | |
319 | return TRUE; | |
320 | } | |
321 | else | |
322 | { | |
323 | // it doesn't count as such any more | |
324 | *childLastFocused = (wxWindow *)NULL; | |
325 | } | |
326 | } | |
327 | ||
328 | // set the focus to the first child who wants it | |
329 | wxWindowList::Node *node = win->GetChildren().GetFirst(); | |
330 | while ( node ) | |
331 | { | |
332 | wxWindow *child = node->GetData(); | |
333 | ||
334 | if ( child->AcceptsFocusFromKeyboard() && !child->IsTopLevel() ) | |
335 | { | |
336 | wxLogTrace(_T("focus"), | |
337 | _T("SetFocusToChild() => first child (0x%08x)."), | |
338 | child->GetHandle()); | |
339 | ||
340 | *childLastFocused = child; // should be redundant, but it is not | |
341 | child->SetFocus(); | |
342 | return TRUE; | |
343 | } | |
344 | ||
345 | node = node->GetNext(); | |
346 | } | |
347 | ||
348 | return FALSE; | |
349 | } |