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