]>
git.saurik.com Git - wxWidgets.git/blob - 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
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "panelg.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
32 #include "wx/object.h"
34 #include "wx/colour.h"
35 #include "wx/settings.h"
39 #include "wx/generic/panelg.h"
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
45 IMPLEMENT_DYNAMIC_CLASS(wxPanel
, wxWindow
)
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
)
54 // ============================================================================
56 // ============================================================================
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
64 m_winLastFocused
= (wxWindow
*)NULL
;
65 m_btnDefault
= (wxButton
*)NULL
;
68 bool wxPanel::Create(wxWindow
*parent
, wxWindowID id
,
74 bool ret
= wxWindow::Create(parent
, id
, pos
, size
, style
, name
);
78 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
79 SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
85 // ----------------------------------------------------------------------------
87 // ----------------------------------------------------------------------------
89 void wxPanel::InitDialog()
91 wxInitDialogEvent
event(GetId());
92 event
.SetEventObject(this);
93 GetEventHandler()->ProcessEvent(event
);
96 // Responds to colour changes, and passes event on to children.
97 void wxPanel::OnSysColourChanged(wxSysColourChangedEvent
& event
)
99 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
102 // Propagate the event to the non-top-level children
103 wxWindow::OnSysColourChanged(event
);
106 void wxPanel::OnSize(wxSizeEvent
& WXUNUSED(event
))
108 #if wxUSE_CONSTRAINTS
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 // ----------------------------------------------------------------------------
122 void wxPanel::OnNavigationKey( wxNavigationKeyEvent
& event
)
124 const wxWindowList
& children
= GetChildren();
126 // there is not much to do if we don't have children and we're not
127 // interested in "notebook page change" events here
128 if ( !children
.GetCount() || event
.IsWindowChange() )
130 wxWindow
*parent
= GetParent();
131 if ( !parent
|| !parent
->GetEventHandler()->ProcessEvent(event
) )
139 // where are we going?
140 bool forward
= event
.GetDirection();
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
;
146 // the event is propagated downwards if the event emitter was our parent
147 bool goingDown
= event
.GetEventObject() == GetParent();
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
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
;
158 // start from first or last depending on where we're going
159 node
= forward
? children
.GetFirst() : children
.GetLast();
161 // we want to cycle over all nodes
162 start_node
= (wxWindowList::Node
*)NULL
;
166 // try to find the child which has the focus currently
168 // the event emitter might have done this for us
169 wxWindow
*winFocus
= event
.GetCurrentFocus();
171 // but if not, we might know where the focus was ourselves
173 winFocus
= m_winLastFocused
;
175 // if still no luck, do it the hard way
177 winFocus
= wxWindow::FindFocus();
181 // ok, we found the focus - now is it our child?
182 start_node
= children
.Find( winFocus
);
186 start_node
= (wxWindowList::Node
*)NULL
;
189 if ( !start_node
&& m_winLastFocused
)
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
);
196 // if we still didn't find anything, we should start with the first one
199 start_node
= children
.GetFirst();
202 // and the first child which we can try setting focus to is the next or
204 node
= forward
? start_node
->GetNext() : start_node
->GetPrevious();
207 // we want to cycle over all elements passing by NULL
208 while ( node
!= start_node
)
210 // Have we come to the last or first item on the panel?
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
221 wxWindow
*focussed_child_of_parent
= this;
222 for ( wxWindow
*parent
= GetParent();
224 parent
= parent
->GetParent() )
226 // we don't want to tab into a different dialog or frame
227 if ( focussed_child_of_parent
->IsTopLevel() )
230 event
.SetCurrentFocus( focussed_child_of_parent
);
231 if (parent
->GetEventHandler()->ProcessEvent( event
))
234 focussed_child_of_parent
= parent
;
237 //else: as the focus came from our parent, we definitely don't want
238 // to send it back to it!
240 // no, we are not inside another panel so process this ourself
241 node
= forward
? children
.GetFirst() : children
.GetLast();
246 wxWindow
*child
= node
->GetData();
248 if ( child
->AcceptsFocus() )
250 m_winLastFocused
= child
; // should be redundant, but it is not
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
);
259 // trick the panel into thinking that it got the navigation
260 // event - instead of duplicating all the code here
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
);
269 event
.SetEventObject(this);
270 if ( !child
->GetEventHandler()->ProcessEvent(event
) )
272 // everything is simple: just give focus to it
275 //else: the child manages its focus itself
281 node
= forward
? node
->GetNext() : node
->GetPrevious();
284 // we cycled through all of our children and none of them wanted to accept
289 void wxPanel::SetFocus()
291 wxLogTrace(_T("focus"), _T("SetFocus on wxPanel 0x%08x."), GetHandle());
293 // If the panel gets the focus *by way of getting it set directly*
294 // we move the focus to the first window that can get it.
296 // VZ: no, we set the focus to the last window too. I don't understand why
297 // should we make this distinction: if an app wants to set focus to
298 // some precise control, it may always do it directly, but if we don't
299 // use m_winLastFocused here, the focus won't be set correctly after a
300 // notebook page change nor after frame activation under MSW (it calls
303 // RR: yes, when I the tab key to navigate in a panel with some controls and
304 // a notebook and the focus jumps to the notebook (typically coming from
305 // a button at the top) the notebook should focus the first child in the
306 // current notebook page, not the last one which would otherwise get the
307 // focus if you used the tab key to navigate from the current notebook
308 // page to button at the bottom. See every page in the controls sample.
310 // VZ: ok, but this still doesn't (at least I don't see how it can) take
311 // care of first/last child problem: i.e. if Shift-TAB is pressed in a
312 // situation like above, the focus should be given to the last child,
313 // not the first one (and not to the last focused one neither) - I
314 // think my addition to OnNavigationKey() above takes care of it.
315 // Keeping #ifdef __WXGTK__ for now, but please try removing it and see
318 // RR: Removed for now. Let's see what happens..
320 if ( !SetFocusToChild() )
322 wxWindow::SetFocus();
326 void wxPanel::OnFocus(wxFocusEvent
& event
)
328 wxLogTrace(_T("focus"), _T("OnFocus on wxPanel 0x%08x, name: %s"), GetHandle(), GetName().c_str() );
330 // If the panel gets the focus *by way of getting clicked on*
331 // we move the focus to either the last window that had the
332 // focus or the first one that can get it.
333 (void)SetFocusToChild();
338 bool wxPanel::SetFocusToChild()
340 if ( m_winLastFocused
)
342 // It might happen that the window got reparented or no longer accepts
344 if ( (m_winLastFocused
->GetParent() == this) &&
345 m_winLastFocused
->AcceptsFocus() )
347 wxLogTrace(_T("focus"),
348 _T("SetFocusToChild() => last child (0x%08x)."),
349 m_winLastFocused
->GetHandle());
351 m_winLastFocused
->SetFocus();
356 // it doesn't count as such any more
357 m_winLastFocused
= (wxWindow
*)NULL
;
361 // set the focus to the first child who wants it
362 wxWindowList::Node
*node
= GetChildren().GetFirst();
365 wxWindow
*child
= node
->GetData();
366 if ( child
->AcceptsFocus() )
368 wxLogTrace(_T("focus"),
369 _T("SetFocusToChild() => first child (0x%08x)."),
372 m_winLastFocused
= child
; // should be redundant, but it is not
377 node
= node
->GetNext();