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