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