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