]> git.saurik.com Git - wxWidgets.git/blob - src/common/containr.cpp
support for wxUSE_PALETTE = 0
[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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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::compatibility_iterator 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::compatibility_iterator 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::compatibility_iterator();
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::compatibility_iterator();
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 // disable propagation for this call as otherwise the event might
297 // bounce back to us.
298 wxPropagationDisabler disableProp(event);
299 if ( !child->GetEventHandler()->ProcessEvent(event) )
300 {
301 // set it first in case SetFocusFromKbd() results in focus
302 // change too
303 m_winLastFocused = child;
304
305 // everything is simple: just give focus to it
306 child->SetFocusFromKbd();
307 }
308 //else: the child manages its focus itself
309
310 event.Skip( FALSE );
311
312 return;
313 }
314
315 node = forward ? node->GetNext() : node->GetPrevious();
316 }
317
318 // we cycled through all of our children and none of them wanted to accept
319 // focus
320 event.Skip();
321 }
322
323 void wxControlContainer::HandleOnWindowDestroy(wxWindowBase *child)
324 {
325 if ( child == m_winLastFocused )
326 m_winLastFocused = NULL;
327
328 if ( child == m_winDefault )
329 m_winDefault = NULL;
330
331 if ( child == m_winTmpDefault )
332 m_winTmpDefault = NULL;
333 }
334
335 // ----------------------------------------------------------------------------
336 // focus handling
337 // ----------------------------------------------------------------------------
338
339 bool wxControlContainer::DoSetFocus()
340 {
341 wxLogTrace(_T("focus"), _T("SetFocus on wxPanel 0x%08lx."),
342 (unsigned long)m_winParent->GetHandle());
343
344 // when the panel gets the focus we move the focus to either the last
345 // window that had the focus or the first one that can get it unless the
346 // focus had been already set to some other child
347
348 wxWindow *win = wxWindow::FindFocus();
349 while ( win )
350 {
351 if ( win == m_winParent )
352 {
353 // our child already has focus, don't take it away from it
354 return TRUE;
355 }
356
357 if ( win->IsTopLevel() )
358 {
359 // don't look beyond the first top level parent - useless and
360 // unnecessary
361 break;
362 }
363
364 win = win->GetParent();
365 }
366
367 return SetFocusToChild();
368 }
369
370 void wxControlContainer::HandleOnFocus(wxFocusEvent& event)
371 {
372 wxLogTrace(_T("focus"), _T("OnFocus on wxPanel 0x%08lx, name: %s"),
373 (unsigned long)m_winParent->GetHandle(),
374 m_winParent->GetName().c_str() );
375
376 DoSetFocus();
377
378 event.Skip();
379 }
380
381 bool wxControlContainer::SetFocusToChild()
382 {
383 return wxSetFocusToChild(m_winParent, &m_winLastFocused);
384 }
385
386 // ----------------------------------------------------------------------------
387 // SetFocusToChild(): this function is used by wxPanel but also by wxFrame in
388 // wxMSW, this is why it is outside of wxControlContainer class
389 // ----------------------------------------------------------------------------
390
391 bool wxSetFocusToChild(wxWindow *win, wxWindow **childLastFocused)
392 {
393 wxCHECK_MSG( win, FALSE, _T("wxSetFocusToChild(): invalid window") );
394 wxCHECK_MSG( childLastFocused, FALSE,
395 _T("wxSetFocusToChild(): NULL child poonter") );
396
397 if ( *childLastFocused )
398 {
399 // It might happen that the window got reparented
400 if ( (*childLastFocused)->GetParent() == win )
401 {
402 wxLogTrace(_T("focus"),
403 _T("SetFocusToChild() => last child (0x%08lx)."),
404 (unsigned long)(*childLastFocused)->GetHandle());
405
406 // not SetFocusFromKbd(): we're restoring focus back to the old
407 // window and not setting it as the result of a kbd action
408 (*childLastFocused)->SetFocus();
409 return TRUE;
410 }
411 else
412 {
413 // it doesn't count as such any more
414 *childLastFocused = (wxWindow *)NULL;
415 }
416 }
417
418 // set the focus to the first child who wants it
419 wxWindowList::compatibility_iterator node = win->GetChildren().GetFirst();
420 while ( node )
421 {
422 wxWindow *child = node->GetData();
423
424 if ( child->AcceptsFocusFromKeyboard() && !child->IsTopLevel() )
425 {
426 wxLogTrace(_T("focus"),
427 _T("SetFocusToChild() => first child (0x%08lx)."),
428 (unsigned long)child->GetHandle());
429
430 *childLastFocused = child;
431 child->SetFocusFromKbd();
432 return TRUE;
433 }
434
435 node = node->GetNext();
436 }
437
438 return FALSE;
439 }
440