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