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