]> git.saurik.com Git - wxWidgets.git/blame - src/generic/panelg.cpp
fixed false alert in wxRegKey::DeleteSelf
[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
f6bcfd97
BP
39#include "wx/toolbar.h"
40#include "wx/statusbr.h"
41
c801d85f
KB
42#include "wx/generic/panelg.h"
43
d9506e77
VZ
44// ----------------------------------------------------------------------------
45// wxWin macros
46// ----------------------------------------------------------------------------
47
c801d85f
KB
48IMPLEMENT_DYNAMIC_CLASS(wxPanel, wxWindow)
49
50BEGIN_EVENT_TABLE(wxPanel, wxWindow)
51 EVT_SYS_COLOUR_CHANGED(wxPanel::OnSysColourChanged)
341c92a8 52 EVT_SET_FOCUS(wxPanel::OnFocus)
90c3bdac 53 EVT_NAVIGATION_KEY(wxPanel::OnNavigationKey)
27dc7e21 54 EVT_SIZE(wxPanel::OnSize)
c801d85f
KB
55END_EVENT_TABLE()
56
d9506e77
VZ
57// ============================================================================
58// implementation
59// ============================================================================
60
61// ----------------------------------------------------------------------------
62// wxPanel creation
63// ----------------------------------------------------------------------------
c801d85f 64
edccf428 65void wxPanel::Init()
c801d85f 66{
319fefa9 67 m_winLastFocused = (wxWindow *)NULL;
edccf428 68 m_btnDefault = (wxButton *)NULL;
c801d85f
KB
69}
70
debe6624 71bool wxPanel::Create(wxWindow *parent, wxWindowID id,
90c3bdac
VZ
72 const wxPoint& pos,
73 const wxSize& size,
74 long style,
75 const wxString& name)
c801d85f 76{
b292e2f5 77 bool ret = wxWindow::Create(parent, id, pos, size, style, name);
c801d85f 78
fb99aca7 79 if ( ret )
b292e2f5 80 {
b292e2f5
RR
81 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
82 SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT));
b292e2f5 83 }
c801d85f 84
b292e2f5 85 return ret;
c801d85f
KB
86}
87
d9506e77
VZ
88// ----------------------------------------------------------------------------
89// misc
90// ----------------------------------------------------------------------------
91
92void wxPanel::InitDialog()
c801d85f 93{
b292e2f5
RR
94 wxInitDialogEvent event(GetId());
95 event.SetEventObject(this);
96 GetEventHandler()->ProcessEvent(event);
90c3bdac
VZ
97}
98
c801d85f
KB
99// Responds to colour changes, and passes event on to children.
100void wxPanel::OnSysColourChanged(wxSysColourChangedEvent& event)
101{
102 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
c801d85f
KB
103 Refresh();
104
105 // Propagate the event to the non-top-level children
106 wxWindow::OnSysColourChanged(event);
107}
108
d9506e77
VZ
109void wxPanel::OnSize(wxSizeEvent& WXUNUSED(event))
110{
111#if wxUSE_CONSTRAINTS
112 if (GetAutoLayout())
113 Layout();
114#endif
115}
116
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// ----------------------------------------------------------------------------
124
b292e2f5 125void wxPanel::OnNavigationKey( wxNavigationKeyEvent& event )
90c3bdac 126{
624d1a4f
VZ
127 // the event is propagated downwards if the event emitter was our parent
128 bool goingDown = event.GetEventObject() == GetParent();
129
8614c467 130 const wxWindowList& children = GetChildren();
4ee1741f 131
8614c467
VZ
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() )
b292e2f5 135 {
624d1a4f
VZ
136 // let the parent process it unless it already comes from our parent
137 // of we don't have any
d9506e77 138 wxWindow *parent = GetParent();
624d1a4f
VZ
139 if ( goingDown ||
140 !parent || !parent->GetEventHandler()->ProcessEvent(event) )
d9506e77
VZ
141 {
142 event.Skip();
143 }
144
b292e2f5 145 return;
90c3bdac
VZ
146 }
147
d9506e77
VZ
148 // where are we going?
149 bool forward = event.GetDirection();
150
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;
154
d9506e77
VZ
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
158 if ( goingDown )
b292e2f5 159 {
d9506e77
VZ
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;
163
164 // start from first or last depending on where we're going
165 node = forward ? children.GetFirst() : children.GetLast();
166
167 // we want to cycle over all nodes
168 start_node = (wxWindowList::Node *)NULL;
90c3bdac 169 }
d9506e77
VZ
170 else
171 {
172 // try to find the child which has the focus currently
90c3bdac 173
d9506e77 174 // the event emitter might have done this for us
3da17724 175 wxWindow *winFocus = event.GetCurrentFocus();
87a1e308 176
d9506e77
VZ
177 // but if not, we might know where the focus was ourselves
178 if (!winFocus)
179 winFocus = m_winLastFocused;
87a1e308 180
d9506e77
VZ
181 // if still no luck, do it the hard way
182 if (!winFocus)
183 winFocus = wxWindow::FindFocus();
27dc7e21 184
d9506e77
VZ
185 if ( winFocus )
186 {
187 // ok, we found the focus - now is it our child?
188 start_node = children.Find( winFocus );
189 }
190 else
191 {
192 start_node = (wxWindowList::Node *)NULL;
193 }
fb99aca7 194
d9506e77
VZ
195 if ( !start_node && m_winLastFocused )
196 {
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 );
200 }
fb99aca7 201
d9506e77
VZ
202 // if we still didn't find anything, we should start with the first one
203 if ( !start_node )
204 {
205 start_node = children.GetFirst();
206 }
fb99aca7 207
d9506e77
VZ
208 // and the first child which we can try setting focus to is the next or
209 // the previous one
210 node = forward ? start_node->GetNext() : start_node->GetPrevious();
211 }
212
213 // we want to cycle over all elements passing by NULL
a1665b22 214 while ( node != start_node )
b292e2f5 215 {
3da17724 216 // Have we come to the last or first item on the panel?
a1665b22 217 if ( !node )
fb99aca7 218 {
d9506e77 219 if ( !goingDown )
a1665b22 220 {
d9506e77
VZ
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
226 // panel).
227 wxWindow *focussed_child_of_parent = this;
228 for ( wxWindow *parent = GetParent();
229 parent;
230 parent = parent->GetParent() )
a1665b22 231 {
d9506e77
VZ
232 // we don't want to tab into a different dialog or frame
233 if ( focussed_child_of_parent->IsTopLevel() )
234 break;
235
8253c7fd
RR
236 event.SetCurrentFocus( focussed_child_of_parent );
237 if (parent->GetEventHandler()->ProcessEvent( event ))
238 return;
d9506e77
VZ
239
240 focussed_child_of_parent = parent;
a1665b22
VZ
241 }
242 }
d9506e77
VZ
243 //else: as the focus came from our parent, we definitely don't want
244 // to send it back to it!
a1665b22
VZ
245
246 // no, we are not inside another panel so process this ourself
d9506e77 247 node = forward ? children.GetFirst() : children.GetLast();
341c92a8 248
e4ffaca4 249 continue;
fb99aca7
VZ
250 }
251
f03fc89f 252 wxWindow *child = node->GetData();
fb99aca7 253
a1665b22 254 if ( child->AcceptsFocus() )
fb99aca7 255 {
87a1e308 256 m_winLastFocused = child; // should be redundant, but it is not
d9506e77
VZ
257
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);
263 if ( subpanel )
264 {
265 // trick the panel into thinking that it got the navigation
266 // event - instead of duplicating all the code here
267 //
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);
273 }
274
275 event.SetEventObject(this);
276 if ( !child->GetEventHandler()->ProcessEvent(event) )
277 {
278 // everything is simple: just give focus to it
279 child->SetFocus();
280 }
281 //else: the child manages its focus itself
282
510fc784 283 event.Skip( FALSE );
fb99aca7
VZ
284 return;
285 }
286
d9506e77 287 node = forward ? node->GetNext() : node->GetPrevious();
b292e2f5 288 }
fb99aca7
VZ
289
290 // we cycled through all of our children and none of them wanted to accept
291 // focus
b292e2f5 292 event.Skip();
6e4739a0 293}
58614078 294
3da17724
RR
295void wxPanel::SetFocus()
296{
00c4e897
VZ
297 wxLogTrace(_T("focus"), _T("SetFocus on wxPanel 0x%08x."), GetHandle());
298
3da17724 299 // If the panel gets the focus *by way of getting it set directly*
69ffe1d2 300 // we move the focus to the first window that can get it.
3da17724 301
00c4e897
VZ
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
307 // SetFocus too)
308 //
88413fec
RR
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.
d9506e77
VZ
315 //
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
322 // what happens.
4ee1741f
RR
323 //
324 // RR: Removed for now. Let's see what happens..
00c4e897
VZ
325
326 if ( !SetFocusToChild() )
3da17724 327 {
00c4e897 328 wxWindow::SetFocus();
3da17724 329 }
3da17724
RR
330}
331
341c92a8
VZ
332void wxPanel::OnFocus(wxFocusEvent& event)
333{
8253c7fd 334 wxLogTrace(_T("focus"), _T("OnFocus on wxPanel 0x%08x, name: %s"), GetHandle(), GetName().c_str() );
00c4e897 335
3da17724 336 // If the panel gets the focus *by way of getting clicked on*
d9506e77 337 // we move the focus to either the last window that had the
69ffe1d2 338 // focus or the first one that can get it.
00c4e897
VZ
339 (void)SetFocusToChild();
340
341 event.Skip();
342}
3da17724 343
00c4e897
VZ
344bool wxPanel::SetFocusToChild()
345{
f6bcfd97
BP
346 return wxSetFocusToChild(this, &m_winLastFocused);
347}
348
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// ----------------------------------------------------------------------------
353
354bool wxSetFocusToChild(wxWindow *win, wxWindow **childLastFocused)
355{
356 if ( *childLastFocused )
0492c5a0 357 {
00c4e897
VZ
358 // It might happen that the window got reparented or no longer accepts
359 // the focus.
f6bcfd97
BP
360 if ( (*childLastFocused)->GetParent() == win &&
361 (*childLastFocused)->AcceptsFocus() )
87a1e308 362 {
00c4e897
VZ
363 wxLogTrace(_T("focus"),
364 _T("SetFocusToChild() => last child (0x%08x)."),
f6bcfd97 365 (*childLastFocused)->GetHandle());
00c4e897 366
f6bcfd97 367 (*childLastFocused)->SetFocus();
00c4e897
VZ
368 return TRUE;
369 }
370 else
371 {
372 // it doesn't count as such any more
f6bcfd97 373 *childLastFocused = (wxWindow *)NULL;
87a1e308 374 }
0492c5a0 375 }
87a1e308 376
00c4e897 377 // set the focus to the first child who wants it
f6bcfd97 378 wxWindowList::Node *node = win->GetChildren().GetFirst();
00c4e897 379 while ( node )
3da17724 380 {
00c4e897 381 wxWindow *child = node->GetData();
f6bcfd97
BP
382
383 if ( child->AcceptsFocus()
384 && !child->IsTopLevel()
385#if wxUSE_TOOLBAR
386 && !wxDynamicCast(child, wxToolBar)
387#endif // wxUSE_TOOLBAR
388#if wxUSE_STATUSBAR
389 && !wxDynamicCast(child, wxStatusBar)
390#endif // wxUSE_STATUSBAR
391 )
87a1e308 392 {
00c4e897
VZ
393 wxLogTrace(_T("focus"),
394 _T("SetFocusToChild() => first child (0x%08x)."),
395 child->GetHandle());
396
f6bcfd97 397 *childLastFocused = child; // should be redundant, but it is not
87a1e308 398 child->SetFocus();
00c4e897 399 return TRUE;
87a1e308 400 }
87a1e308 401
00c4e897
VZ
402 node = node->GetNext();
403 }
87a1e308 404
00c4e897 405 return FALSE;
341c92a8 406}