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