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