]>
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/toolbar.h"
40 #include "wx/statusbr.h"
42 #include "wx/generic/panelg.h"
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 IMPLEMENT_DYNAMIC_CLASS(wxPanel
, wxWindow
)
50 BEGIN_EVENT_TABLE(wxPanel
, wxWindow
)
51 EVT_SYS_COLOUR_CHANGED(wxPanel::OnSysColourChanged
)
52 EVT_SET_FOCUS(wxPanel::OnFocus
)
53 EVT_NAVIGATION_KEY(wxPanel::OnNavigationKey
)
54 EVT_SIZE(wxPanel::OnSize
)
57 // ============================================================================
59 // ============================================================================
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
67 m_winLastFocused
= (wxWindow
*)NULL
;
68 m_btnDefault
= (wxButton
*)NULL
;
71 bool wxPanel::Create(wxWindow
*parent
, wxWindowID id
,
77 bool ret
= wxWindow::Create(parent
, id
, pos
, size
, style
, name
);
81 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
82 SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
88 // ----------------------------------------------------------------------------
90 // ----------------------------------------------------------------------------
92 void wxPanel::InitDialog()
94 wxInitDialogEvent
event(GetId());
95 event
.SetEventObject(this);
96 GetEventHandler()->ProcessEvent(event
);
99 // Responds to colour changes, and passes event on to children.
100 void wxPanel::OnSysColourChanged(wxSysColourChangedEvent
& event
)
102 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
105 // Propagate the event to the non-top-level children
106 wxWindow::OnSysColourChanged(event
);
109 void wxPanel::OnSize(wxSizeEvent
& WXUNUSED(event
))
111 #if wxUSE_CONSTRAINTS
117 // ----------------------------------------------------------------------------
118 // Keyboard handling - this is the place where the TAB traversal logic is
119 // implemented. As this code is common to all ports, this ensures consistent
120 // behaviour even if we don't specify how exactly the wxNavigationKeyEvent are
121 // generated and this is done in platform specific code which also ensures that
122 // we can follow the given platform standards.
123 // ----------------------------------------------------------------------------
125 void wxPanel::OnNavigationKey( wxNavigationKeyEvent
& event
)
127 // the event is propagated downwards if the event emitter was our parent
128 bool goingDown
= event
.GetEventObject() == GetParent();
130 const wxWindowList
& children
= GetChildren();
132 // there is not much to do if we don't have children and we're not
133 // interested in "notebook page change" events here
134 if ( !children
.GetCount() || event
.IsWindowChange() )
136 // let the parent process it unless it already comes from our parent
137 // of we don't have any
138 wxWindow
*parent
= GetParent();
140 !parent
|| !parent
->GetEventHandler()->ProcessEvent(event
) )
148 // where are we going?
149 bool forward
= event
.GetDirection();
151 // the node of the children list from which we should start looking for the
152 // next acceptable child
153 wxWindowList::Node
*node
, *start_node
;
155 // we should start from the first/last control and not from the one which
156 // had focus the last time if we're propagating the event downwards because
157 // for our parent we look like a single control
160 // just to be sure it's not used (normally this is not necessary, but
161 // doesn't hurt neither)
162 m_winLastFocused
= (wxWindow
*)NULL
;
164 // start from first or last depending on where we're going
165 node
= forward
? children
.GetFirst() : children
.GetLast();
167 // we want to cycle over all nodes
168 start_node
= (wxWindowList::Node
*)NULL
;
172 // try to find the child which has the focus currently
174 // the event emitter might have done this for us
175 wxWindow
*winFocus
= event
.GetCurrentFocus();
177 // but if not, we might know where the focus was ourselves
179 winFocus
= m_winLastFocused
;
181 // if still no luck, do it the hard way
183 winFocus
= wxWindow::FindFocus();
187 // ok, we found the focus - now is it our child?
188 start_node
= children
.Find( winFocus
);
192 start_node
= (wxWindowList::Node
*)NULL
;
195 if ( !start_node
&& m_winLastFocused
)
197 // window which has focus isn't our child, fall back to the one
198 // which had the focus the last time
199 start_node
= children
.Find( m_winLastFocused
);
202 // if we still didn't find anything, we should start with the first one
205 start_node
= children
.GetFirst();
208 // and the first child which we can try setting focus to is the next or
210 node
= forward
? start_node
->GetNext() : start_node
->GetPrevious();
213 // we want to cycle over all elements passing by NULL
214 while ( node
!= start_node
)
216 // Have we come to the last or first item on the panel?
221 // Check if our (may be grand) parent is another panel: if this
222 // is the case, they will know what to do with this navigation
223 // key and so give them the chance to process it instead of
224 // looping inside this panel (normally, the focus will go to
225 // the next/previous item after this panel in the parent
227 wxWindow
*focussed_child_of_parent
= this;
228 for ( wxWindow
*parent
= GetParent();
230 parent
= parent
->GetParent() )
232 // we don't want to tab into a different dialog or frame
233 if ( focussed_child_of_parent
->IsTopLevel() )
236 event
.SetCurrentFocus( focussed_child_of_parent
);
237 if (parent
->GetEventHandler()->ProcessEvent( event
))
240 focussed_child_of_parent
= parent
;
243 //else: as the focus came from our parent, we definitely don't want
244 // to send it back to it!
246 // no, we are not inside another panel so process this ourself
247 node
= forward
? children
.GetFirst() : children
.GetLast();
252 wxWindow
*child
= node
->GetData();
254 if ( child
->AcceptsFocus() )
256 m_winLastFocused
= child
; // should be redundant, but it is not
258 // if we're setting the focus to a child panel we should prevent it
259 // from giving it to the child which had the focus the last time
260 // and instead give it to the first/last child depending from which
261 // direction we're coming
262 wxPanel
*subpanel
= wxDynamicCast(child
, wxPanel
);
265 // trick the panel into thinking that it got the navigation
266 // event - instead of duplicating all the code here
268 // make sure that we do trick it by setting all the parameters
269 // correctly (consistently with the code in this very function
270 // above) and that it starts from the very beginning/end by
271 // using SetLastFocus(NULL)
272 subpanel
->SetLastFocus((wxWindow
*)NULL
);
275 event
.SetEventObject(this);
276 if ( !child
->GetEventHandler()->ProcessEvent(event
) )
278 // everything is simple: just give focus to it
281 //else: the child manages its focus itself
287 node
= forward
? node
->GetNext() : node
->GetPrevious();
290 // we cycled through all of our children and none of them wanted to accept
295 void wxPanel::SetFocus()
297 wxLogTrace(_T("focus"), _T("SetFocus on wxPanel 0x%08x."), GetHandle());
299 // If the panel gets the focus *by way of getting it set directly*
300 // we move the focus to the first window that can get it.
302 // VZ: no, we set the focus to the last window too. I don't understand why
303 // should we make this distinction: if an app wants to set focus to
304 // some precise control, it may always do it directly, but if we don't
305 // use m_winLastFocused here, the focus won't be set correctly after a
306 // notebook page change nor after frame activation under MSW (it calls
309 // RR: yes, when I the tab key to navigate in a panel with some controls and
310 // a notebook and the focus jumps to the notebook (typically coming from
311 // a button at the top) the notebook should focus the first child in the
312 // current notebook page, not the last one which would otherwise get the
313 // focus if you used the tab key to navigate from the current notebook
314 // page to button at the bottom. See every page in the controls sample.
316 // VZ: ok, but this still doesn't (at least I don't see how it can) take
317 // care of first/last child problem: i.e. if Shift-TAB is pressed in a
318 // situation like above, the focus should be given to the last child,
319 // not the first one (and not to the last focused one neither) - I
320 // think my addition to OnNavigationKey() above takes care of it.
321 // Keeping #ifdef __WXGTK__ for now, but please try removing it and see
324 // RR: Removed for now. Let's see what happens..
326 if ( !SetFocusToChild() )
328 wxWindow::SetFocus();
332 void wxPanel::OnFocus(wxFocusEvent
& event
)
334 wxLogTrace(_T("focus"), _T("OnFocus on wxPanel 0x%08x, name: %s"), GetHandle(), GetName().c_str() );
336 // If the panel gets the focus *by way of getting clicked on*
337 // we move the focus to either the last window that had the
338 // focus or the first one that can get it.
339 (void)SetFocusToChild();
344 bool wxPanel::SetFocusToChild()
346 return wxSetFocusToChild(this, &m_winLastFocused
);
349 // ----------------------------------------------------------------------------
350 // SetFocusToChild(): this function is used by wxPanel but also by wxFrame in
351 // wxMSW, this is why it is outside of wxPanel class
352 // ----------------------------------------------------------------------------
354 bool wxSetFocusToChild(wxWindow
*win
, wxWindow
**childLastFocused
)
356 if ( *childLastFocused
)
358 // It might happen that the window got reparented or no longer accepts
360 if ( (*childLastFocused
)->GetParent() == win
&&
361 (*childLastFocused
)->AcceptsFocus() )
363 wxLogTrace(_T("focus"),
364 _T("SetFocusToChild() => last child (0x%08x)."),
365 (*childLastFocused
)->GetHandle());
367 (*childLastFocused
)->SetFocus();
372 // it doesn't count as such any more
373 *childLastFocused
= (wxWindow
*)NULL
;
377 // set the focus to the first child who wants it
378 wxWindowList::Node
*node
= win
->GetChildren().GetFirst();
381 wxWindow
*child
= node
->GetData();
383 if ( child
->AcceptsFocus()
384 && !child
->IsTopLevel()
386 && !wxDynamicCast(child
, wxToolBar
)
387 #endif // wxUSE_TOOLBAR
389 && !wxDynamicCast(child
, wxStatusBar
)
390 #endif // wxUSE_STATUSBAR
393 wxLogTrace(_T("focus"),
394 _T("SetFocusToChild() => first child (0x%08x)."),
397 *childLastFocused
= child
; // should be redundant, but it is not
402 node
= node
->GetNext();