]> git.saurik.com Git - wxWidgets.git/blob - src/generic/panelg.cpp
merged MapBitmap() from 2.2 branch
[wxWidgets.git] / src / generic / panelg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/panelg.cpp
3 // Purpose: wxPanel and the keyboard handling code
4 // Author: Julian Smart, Robert Roebling, Vadim Zeitlin
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "panelg.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/object.h"
33 #include "wx/font.h"
34 #include "wx/colour.h"
35 #include "wx/settings.h"
36 #include "wx/log.h"
37 #endif
38
39 #include "wx/generic/panelg.h"
40
41 // ----------------------------------------------------------------------------
42 // wxWin macros
43 // ----------------------------------------------------------------------------
44
45 IMPLEMENT_DYNAMIC_CLASS(wxPanel, wxWindow)
46
47 BEGIN_EVENT_TABLE(wxPanel, wxWindow)
48 EVT_SYS_COLOUR_CHANGED(wxPanel::OnSysColourChanged)
49 EVT_SET_FOCUS(wxPanel::OnFocus)
50 EVT_NAVIGATION_KEY(wxPanel::OnNavigationKey)
51 EVT_SIZE(wxPanel::OnSize)
52 END_EVENT_TABLE()
53
54 // ============================================================================
55 // implementation
56 // ============================================================================
57
58 // ----------------------------------------------------------------------------
59 // wxPanel creation
60 // ----------------------------------------------------------------------------
61
62 void wxPanel::Init()
63 {
64 m_winLastFocused = (wxWindow *)NULL;
65 #if wxUSE_BUTTON
66 m_btnDefault = (wxButton *)NULL;
67 #endif // wxUSE_BUTTON
68 }
69
70 bool wxPanel::Create(wxWindow *parent, wxWindowID id,
71 const wxPoint& pos,
72 const wxSize& size,
73 long style,
74 const wxString& name)
75 {
76 return wxWindow::Create(parent, id, pos, size, style, name);
77 }
78
79 // ----------------------------------------------------------------------------
80 // misc
81 // ----------------------------------------------------------------------------
82
83 void wxPanel::InitDialog()
84 {
85 wxInitDialogEvent event(GetId());
86 event.SetEventObject(this);
87 GetEventHandler()->ProcessEvent(event);
88 }
89
90 // Responds to colour changes, and passes event on to children.
91 void wxPanel::OnSysColourChanged(wxSysColourChangedEvent& event)
92 {
93 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
94 Refresh();
95
96 // Propagate the event to the non-top-level children
97 wxWindow::OnSysColourChanged(event);
98 }
99
100 void wxPanel::OnSize(wxSizeEvent& WXUNUSED(event))
101 {
102 #if wxUSE_CONSTRAINTS
103 if (GetAutoLayout())
104 Layout();
105 #endif
106 }
107
108 // ----------------------------------------------------------------------------
109 // Keyboard handling - this is the place where the TAB traversal logic is
110 // implemented. As this code is common to all ports, this ensures consistent
111 // behaviour even if we don't specify how exactly the wxNavigationKeyEvent are
112 // generated and this is done in platform specific code which also ensures that
113 // we can follow the given platform standards.
114 // ----------------------------------------------------------------------------
115
116 void wxPanel::OnNavigationKey( wxNavigationKeyEvent& event )
117 {
118 // the event is propagated downwards if the event emitter was our parent
119 bool goingDown = event.GetEventObject() == GetParent();
120
121 const wxWindowList& children = GetChildren();
122
123 // there is not much to do if we don't have children and we're not
124 // interested in "notebook page change" events here
125 if ( !children.GetCount() || event.IsWindowChange() )
126 {
127 // let the parent process it unless it already comes from our parent
128 // of we don't have any
129 wxWindow *parent = GetParent();
130 if ( goingDown ||
131 !parent || !parent->GetEventHandler()->ProcessEvent(event) )
132 {
133 event.Skip();
134 }
135
136 return;
137 }
138
139 // where are we going?
140 bool forward = event.GetDirection();
141
142 // the node of the children list from which we should start looking for the
143 // next acceptable child
144 wxWindowList::Node *node, *start_node;
145
146 // we should start from the first/last control and not from the one which
147 // had focus the last time if we're propagating the event downwards because
148 // for our parent we look like a single control
149 if ( goingDown )
150 {
151 // just to be sure it's not used (normally this is not necessary, but
152 // doesn't hurt neither)
153 m_winLastFocused = (wxWindow *)NULL;
154
155 // start from first or last depending on where we're going
156 node = forward ? children.GetFirst() : children.GetLast();
157
158 // we want to cycle over all nodes
159 start_node = (wxWindowList::Node *)NULL;
160 }
161 else
162 {
163 // try to find the child which has the focus currently
164
165 // the event emitter might have done this for us
166 wxWindow *winFocus = event.GetCurrentFocus();
167
168 // but if not, we might know where the focus was ourselves
169 if (!winFocus)
170 winFocus = m_winLastFocused;
171
172 // if still no luck, do it the hard way
173 if (!winFocus)
174 winFocus = wxWindow::FindFocus();
175
176 if ( winFocus )
177 {
178 // ok, we found the focus - now is it our child?
179 start_node = children.Find( winFocus );
180 }
181 else
182 {
183 start_node = (wxWindowList::Node *)NULL;
184 }
185
186 if ( !start_node && m_winLastFocused )
187 {
188 // window which has focus isn't our child, fall back to the one
189 // which had the focus the last time
190 start_node = children.Find( m_winLastFocused );
191 }
192
193 // if we still didn't find anything, we should start with the first one
194 if ( !start_node )
195 {
196 start_node = children.GetFirst();
197 }
198
199 // and the first child which we can try setting focus to is the next or
200 // the previous one
201 node = forward ? start_node->GetNext() : start_node->GetPrevious();
202 }
203
204 // we want to cycle over all elements passing by NULL
205 while ( node != start_node )
206 {
207 // Have we come to the last or first item on the panel?
208 if ( !node )
209 {
210 if ( !goingDown )
211 {
212 // Check if our (may be grand) parent is another panel: if this
213 // is the case, they will know what to do with this navigation
214 // key and so give them the chance to process it instead of
215 // looping inside this panel (normally, the focus will go to
216 // the next/previous item after this panel in the parent
217 // panel).
218 wxWindow *focussed_child_of_parent = this;
219 for ( wxWindow *parent = GetParent();
220 parent;
221 parent = parent->GetParent() )
222 {
223 // we don't want to tab into a different dialog or frame
224 if ( focussed_child_of_parent->IsTopLevel() )
225 break;
226
227 event.SetCurrentFocus( focussed_child_of_parent );
228 if (parent->GetEventHandler()->ProcessEvent( event ))
229 return;
230
231 focussed_child_of_parent = parent;
232 }
233 }
234 //else: as the focus came from our parent, we definitely don't want
235 // to send it back to it!
236
237 // no, we are not inside another panel so process this ourself
238 node = forward ? children.GetFirst() : children.GetLast();
239
240 continue;
241 }
242
243 wxWindow *child = node->GetData();
244
245 if ( child->AcceptsFocusFromKeyboard() )
246 {
247 m_winLastFocused = child; // should be redundant, but it is not
248
249 // if we're setting the focus to a child panel we should prevent it
250 // from giving it to the child which had the focus the last time
251 // and instead give it to the first/last child depending from which
252 // direction we're coming
253 wxPanel *subpanel = wxDynamicCast(child, wxPanel);
254 if ( subpanel )
255 {
256 // trick the panel into thinking that it got the navigation
257 // event - instead of duplicating all the code here
258 //
259 // make sure that we do trick it by setting all the parameters
260 // correctly (consistently with the code in this very function
261 // above) and that it starts from the very beginning/end by
262 // using SetLastFocus(NULL)
263 subpanel->SetLastFocus((wxWindow *)NULL);
264 }
265
266 event.SetEventObject(this);
267 if ( !child->GetEventHandler()->ProcessEvent(event) )
268 {
269 // everything is simple: just give focus to it
270 child->SetFocus();
271 }
272 //else: the child manages its focus itself
273
274 event.Skip( FALSE );
275 return;
276 }
277
278 node = forward ? node->GetNext() : node->GetPrevious();
279 }
280
281 // we cycled through all of our children and none of them wanted to accept
282 // focus
283 event.Skip();
284 }
285
286 void wxPanel::RemoveChild(wxWindowBase *child)
287 {
288 if ( child == m_winLastFocused )
289 m_winLastFocused = NULL;
290 wxWindow::RemoveChild(child);
291 }
292
293 void wxPanel::SetFocus()
294 {
295 wxLogTrace(_T("focus"), _T("SetFocus on wxPanel 0x%08x."), GetHandle());
296
297 // If the panel gets the focus *by way of getting it set directly*
298 // we move the focus to the first window that can get it.
299
300 // VZ: no, we set the focus to the last window too. I don't understand why
301 // should we make this distinction: if an app wants to set focus to
302 // some precise control, it may always do it directly, but if we don't
303 // use m_winLastFocused here, the focus won't be set correctly after a
304 // notebook page change nor after frame activation under MSW (it calls
305 // SetFocus too)
306 //
307 // RR: yes, when I the tab key to navigate in a panel with some controls and
308 // a notebook and the focus jumps to the notebook (typically coming from
309 // a button at the top) the notebook should focus the first child in the
310 // current notebook page, not the last one which would otherwise get the
311 // focus if you used the tab key to navigate from the current notebook
312 // page to button at the bottom. See every page in the controls sample.
313 //
314 // VZ: ok, but this still doesn't (at least I don't see how it can) take
315 // care of first/last child problem: i.e. if Shift-TAB is pressed in a
316 // situation like above, the focus should be given to the last child,
317 // not the first one (and not to the last focused one neither) - I
318 // think my addition to OnNavigationKey() above takes care of it.
319 // Keeping #ifdef __WXGTK__ for now, but please try removing it and see
320 // what happens.
321 //
322 // RR: Removed for now. Let's see what happens..
323
324 if ( !SetFocusToChild() )
325 {
326 wxWindow::SetFocus();
327 }
328 }
329
330 void wxPanel::OnFocus(wxFocusEvent& event)
331 {
332 wxLogTrace(_T("focus"), _T("OnFocus on wxPanel 0x%08x, name: %s"), GetHandle(), GetName().c_str() );
333
334 // If the panel gets the focus *by way of getting clicked on*
335 // we move the focus to either the last window that had the
336 // focus or the first one that can get it.
337 (void)SetFocusToChild();
338
339 event.Skip();
340 }
341
342 bool wxPanel::SetFocusToChild()
343 {
344 return wxSetFocusToChild(this, &m_winLastFocused);
345 }
346
347 // ----------------------------------------------------------------------------
348 // SetFocusToChild(): this function is used by wxPanel but also by wxFrame in
349 // wxMSW, this is why it is outside of wxPanel class
350 // ----------------------------------------------------------------------------
351
352 bool wxSetFocusToChild(wxWindow *win, wxWindow **childLastFocused)
353 {
354 wxCHECK_MSG( win, FALSE, _T("wxSetFocusToChild(): invalid window") );
355
356 if ( *childLastFocused )
357 {
358 // It might happen that the window got reparented or no longer accepts
359 // the focus.
360 if ( (*childLastFocused)->GetParent() == win &&
361 (*childLastFocused)->AcceptsFocusFromKeyboard() )
362 {
363 wxLogTrace(_T("focus"),
364 _T("SetFocusToChild() => last child (0x%08x)."),
365 (*childLastFocused)->GetHandle());
366
367 (*childLastFocused)->SetFocus();
368 return TRUE;
369 }
370 else
371 {
372 // it doesn't count as such any more
373 *childLastFocused = (wxWindow *)NULL;
374 }
375 }
376
377 // set the focus to the first child who wants it
378 wxWindowList::Node *node = win->GetChildren().GetFirst();
379 while ( node )
380 {
381 wxWindow *child = node->GetData();
382
383 if ( child->AcceptsFocusFromKeyboard() && !child->IsTopLevel() )
384 {
385 wxLogTrace(_T("focus"),
386 _T("SetFocusToChild() => first child (0x%08x)."),
387 child->GetHandle());
388
389 *childLastFocused = child; // should be redundant, but it is not
390 child->SetFocus();
391 return TRUE;
392 }
393
394 node = node->GetNext();
395 }
396
397 return FALSE;
398 }