]> git.saurik.com Git - wxWidgets.git/blame - src/generic/panelg.cpp
Added RTLD_GLOBAL to dlopen() flags which is needed if libraries depend
[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{
624d1a4f
VZ
124 // the event is propagated downwards if the event emitter was our parent
125 bool goingDown = event.GetEventObject() == GetParent();
126
8614c467 127 const wxWindowList& children = GetChildren();
4ee1741f 128
8614c467
VZ
129 // there is not much to do if we don't have children and we're not
130 // interested in "notebook page change" events here
131 if ( !children.GetCount() || event.IsWindowChange() )
b292e2f5 132 {
624d1a4f
VZ
133 // let the parent process it unless it already comes from our parent
134 // of we don't have any
d9506e77 135 wxWindow *parent = GetParent();
624d1a4f
VZ
136 if ( goingDown ||
137 !parent || !parent->GetEventHandler()->ProcessEvent(event) )
d9506e77
VZ
138 {
139 event.Skip();
140 }
141
b292e2f5 142 return;
90c3bdac
VZ
143 }
144
d9506e77
VZ
145 // where are we going?
146 bool forward = event.GetDirection();
147
148 // the node of the children list from which we should start looking for the
149 // next acceptable child
150 wxWindowList::Node *node, *start_node;
151
d9506e77
VZ
152 // we should start from the first/last control and not from the one which
153 // had focus the last time if we're propagating the event downwards because
154 // for our parent we look like a single control
155 if ( goingDown )
b292e2f5 156 {
d9506e77
VZ
157 // just to be sure it's not used (normally this is not necessary, but
158 // doesn't hurt neither)
159 m_winLastFocused = (wxWindow *)NULL;
160
161 // start from first or last depending on where we're going
162 node = forward ? children.GetFirst() : children.GetLast();
163
164 // we want to cycle over all nodes
165 start_node = (wxWindowList::Node *)NULL;
90c3bdac 166 }
d9506e77
VZ
167 else
168 {
169 // try to find the child which has the focus currently
90c3bdac 170
d9506e77 171 // the event emitter might have done this for us
3da17724 172 wxWindow *winFocus = event.GetCurrentFocus();
87a1e308 173
d9506e77
VZ
174 // but if not, we might know where the focus was ourselves
175 if (!winFocus)
176 winFocus = m_winLastFocused;
87a1e308 177
d9506e77
VZ
178 // if still no luck, do it the hard way
179 if (!winFocus)
180 winFocus = wxWindow::FindFocus();
27dc7e21 181
d9506e77
VZ
182 if ( winFocus )
183 {
184 // ok, we found the focus - now is it our child?
185 start_node = children.Find( winFocus );
186 }
187 else
188 {
189 start_node = (wxWindowList::Node *)NULL;
190 }
fb99aca7 191
d9506e77
VZ
192 if ( !start_node && m_winLastFocused )
193 {
194 // window which has focus isn't our child, fall back to the one
195 // which had the focus the last time
196 start_node = children.Find( m_winLastFocused );
197 }
fb99aca7 198
d9506e77
VZ
199 // if we still didn't find anything, we should start with the first one
200 if ( !start_node )
201 {
202 start_node = children.GetFirst();
203 }
fb99aca7 204
d9506e77
VZ
205 // and the first child which we can try setting focus to is the next or
206 // the previous one
207 node = forward ? start_node->GetNext() : start_node->GetPrevious();
208 }
209
210 // we want to cycle over all elements passing by NULL
a1665b22 211 while ( node != start_node )
b292e2f5 212 {
3da17724 213 // Have we come to the last or first item on the panel?
a1665b22 214 if ( !node )
fb99aca7 215 {
d9506e77 216 if ( !goingDown )
a1665b22 217 {
d9506e77
VZ
218 // Check if our (may be grand) parent is another panel: if this
219 // is the case, they will know what to do with this navigation
220 // key and so give them the chance to process it instead of
221 // looping inside this panel (normally, the focus will go to
222 // the next/previous item after this panel in the parent
223 // panel).
224 wxWindow *focussed_child_of_parent = this;
225 for ( wxWindow *parent = GetParent();
226 parent;
227 parent = parent->GetParent() )
a1665b22 228 {
d9506e77
VZ
229 // we don't want to tab into a different dialog or frame
230 if ( focussed_child_of_parent->IsTopLevel() )
231 break;
232
8253c7fd
RR
233 event.SetCurrentFocus( focussed_child_of_parent );
234 if (parent->GetEventHandler()->ProcessEvent( event ))
235 return;
d9506e77
VZ
236
237 focussed_child_of_parent = parent;
a1665b22
VZ
238 }
239 }
d9506e77
VZ
240 //else: as the focus came from our parent, we definitely don't want
241 // to send it back to it!
a1665b22
VZ
242
243 // no, we are not inside another panel so process this ourself
d9506e77 244 node = forward ? children.GetFirst() : children.GetLast();
341c92a8 245
e4ffaca4 246 continue;
fb99aca7
VZ
247 }
248
f03fc89f 249 wxWindow *child = node->GetData();
fb99aca7 250
a1665b22 251 if ( child->AcceptsFocus() )
fb99aca7 252 {
87a1e308 253 m_winLastFocused = child; // should be redundant, but it is not
d9506e77
VZ
254
255 // if we're setting the focus to a child panel we should prevent it
256 // from giving it to the child which had the focus the last time
257 // and instead give it to the first/last child depending from which
258 // direction we're coming
259 wxPanel *subpanel = wxDynamicCast(child, wxPanel);
260 if ( subpanel )
261 {
262 // trick the panel into thinking that it got the navigation
263 // event - instead of duplicating all the code here
264 //
265 // make sure that we do trick it by setting all the parameters
266 // correctly (consistently with the code in this very function
267 // above) and that it starts from the very beginning/end by
268 // using SetLastFocus(NULL)
269 subpanel->SetLastFocus((wxWindow *)NULL);
270 }
271
272 event.SetEventObject(this);
273 if ( !child->GetEventHandler()->ProcessEvent(event) )
274 {
275 // everything is simple: just give focus to it
276 child->SetFocus();
277 }
278 //else: the child manages its focus itself
279
510fc784 280 event.Skip( FALSE );
fb99aca7
VZ
281 return;
282 }
283
d9506e77 284 node = forward ? node->GetNext() : node->GetPrevious();
b292e2f5 285 }
fb99aca7
VZ
286
287 // we cycled through all of our children and none of them wanted to accept
288 // focus
b292e2f5 289 event.Skip();
6e4739a0 290}
58614078 291
3da17724
RR
292void wxPanel::SetFocus()
293{
00c4e897
VZ
294 wxLogTrace(_T("focus"), _T("SetFocus on wxPanel 0x%08x."), GetHandle());
295
3da17724 296 // If the panel gets the focus *by way of getting it set directly*
69ffe1d2 297 // we move the focus to the first window that can get it.
3da17724 298
00c4e897
VZ
299 // VZ: no, we set the focus to the last window too. I don't understand why
300 // should we make this distinction: if an app wants to set focus to
301 // some precise control, it may always do it directly, but if we don't
302 // use m_winLastFocused here, the focus won't be set correctly after a
303 // notebook page change nor after frame activation under MSW (it calls
304 // SetFocus too)
305 //
88413fec
RR
306 // RR: yes, when I the tab key to navigate in a panel with some controls and
307 // a notebook and the focus jumps to the notebook (typically coming from
308 // a button at the top) the notebook should focus the first child in the
309 // current notebook page, not the last one which would otherwise get the
310 // focus if you used the tab key to navigate from the current notebook
311 // page to button at the bottom. See every page in the controls sample.
d9506e77
VZ
312 //
313 // VZ: ok, but this still doesn't (at least I don't see how it can) take
314 // care of first/last child problem: i.e. if Shift-TAB is pressed in a
315 // situation like above, the focus should be given to the last child,
316 // not the first one (and not to the last focused one neither) - I
317 // think my addition to OnNavigationKey() above takes care of it.
318 // Keeping #ifdef __WXGTK__ for now, but please try removing it and see
319 // what happens.
4ee1741f
RR
320 //
321 // RR: Removed for now. Let's see what happens..
00c4e897
VZ
322
323 if ( !SetFocusToChild() )
3da17724 324 {
00c4e897 325 wxWindow::SetFocus();
3da17724 326 }
3da17724
RR
327}
328
341c92a8
VZ
329void wxPanel::OnFocus(wxFocusEvent& event)
330{
8253c7fd 331 wxLogTrace(_T("focus"), _T("OnFocus on wxPanel 0x%08x, name: %s"), GetHandle(), GetName().c_str() );
00c4e897 332
3da17724 333 // If the panel gets the focus *by way of getting clicked on*
d9506e77 334 // we move the focus to either the last window that had the
69ffe1d2 335 // focus or the first one that can get it.
00c4e897
VZ
336 (void)SetFocusToChild();
337
338 event.Skip();
339}
3da17724 340
00c4e897
VZ
341bool wxPanel::SetFocusToChild()
342{
343 if ( m_winLastFocused )
0492c5a0 344 {
00c4e897
VZ
345 // It might happen that the window got reparented or no longer accepts
346 // the focus.
347 if ( (m_winLastFocused->GetParent() == this) &&
348 m_winLastFocused->AcceptsFocus() )
87a1e308 349 {
00c4e897
VZ
350 wxLogTrace(_T("focus"),
351 _T("SetFocusToChild() => last child (0x%08x)."),
352 m_winLastFocused->GetHandle());
353
319fefa9 354 m_winLastFocused->SetFocus();
00c4e897
VZ
355 return TRUE;
356 }
357 else
358 {
359 // it doesn't count as such any more
360 m_winLastFocused = (wxWindow *)NULL;
87a1e308 361 }
0492c5a0 362 }
87a1e308 363
00c4e897
VZ
364 // set the focus to the first child who wants it
365 wxWindowList::Node *node = GetChildren().GetFirst();
366 while ( node )
3da17724 367 {
00c4e897
VZ
368 wxWindow *child = node->GetData();
369 if ( child->AcceptsFocus() )
87a1e308 370 {
00c4e897
VZ
371 wxLogTrace(_T("focus"),
372 _T("SetFocusToChild() => first child (0x%08x)."),
373 child->GetHandle());
374
87a1e308
VZ
375 m_winLastFocused = child; // should be redundant, but it is not
376 child->SetFocus();
00c4e897 377 return TRUE;
87a1e308 378 }
87a1e308 379
00c4e897
VZ
380 node = node->GetNext();
381 }
87a1e308 382
00c4e897 383 return FALSE;
341c92a8 384}