]> git.saurik.com Git - wxWidgets.git/blob - src/generic/panelg.cpp
0218693d28f7cb55dc27c8cd3900e3d028ba0145
[wxWidgets.git] / src / generic / panelg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: panelg.cpp
3 // Purpose: wxPanel
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "panelg.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include "wx/object.h"
25 #include "wx/font.h"
26 #include "wx/colour.h"
27 #include "wx/settings.h"
28 #include "wx/log.h"
29 #endif
30
31 #include "wx/generic/panelg.h"
32
33 IMPLEMENT_DYNAMIC_CLASS(wxPanel, wxWindow)
34
35 BEGIN_EVENT_TABLE(wxPanel, wxWindow)
36 EVT_SYS_COLOUR_CHANGED(wxPanel::OnSysColourChanged)
37 EVT_SET_FOCUS(wxPanel::OnFocus)
38 EVT_NAVIGATION_KEY(wxPanel::OnNavigationKey)
39 EVT_SIZE(wxPanel::OnSize)
40 END_EVENT_TABLE()
41
42
43 void wxPanel::Init()
44 {
45 m_winLastFocused = (wxWindow *)NULL;
46 m_btnDefault = (wxButton *)NULL;
47 }
48
49 bool wxPanel::Create(wxWindow *parent, wxWindowID id,
50 const wxPoint& pos,
51 const wxSize& size,
52 long style,
53 const wxString& name)
54 {
55 bool ret = wxWindow::Create(parent, id, pos, size, style, name);
56
57 if ( ret )
58 {
59 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
60 SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT));
61 }
62
63 return ret;
64 }
65
66 void wxPanel::InitDialog(void)
67 {
68 wxInitDialogEvent event(GetId());
69 event.SetEventObject(this);
70 GetEventHandler()->ProcessEvent(event);
71 }
72
73 // Responds to colour changes, and passes event on to children.
74 void wxPanel::OnSysColourChanged(wxSysColourChangedEvent& event)
75 {
76 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
77 Refresh();
78
79 // Propagate the event to the non-top-level children
80 wxWindow::OnSysColourChanged(event);
81 }
82
83 void wxPanel::OnNavigationKey( wxNavigationKeyEvent& event )
84 {
85 // there is not much to do if we have only one child (or not at all)
86 if (GetChildren().GetCount() < 2)
87 {
88 event.Skip();
89 return;
90 }
91
92 // don't process these ones here
93 if (event.IsWindowChange())
94 {
95 event.Skip();
96 return;
97 }
98
99 // Did the event emitter tell us where the last focus was?
100 // wxGTK does this in wxWindow, but wxMSW does not. It is
101 // also done in wxPanel if the event is propagated up.
102 wxWindow *winFocus = event.GetCurrentFocus();
103
104 // Do we know where the focus was ourselves, then?
105 if (!winFocus)
106 winFocus = m_winLastFocused;
107
108 if (!winFocus)
109 winFocus = wxWindow::FindFocus();
110
111 if (!winFocus)
112 {
113 event.Skip();
114 return;
115 }
116
117 wxWindowList::Node *start_node = GetChildren().Find( winFocus );
118 if ( !start_node )
119 start_node = GetChildren().Find( m_winLastFocused );
120 if ( !start_node )
121 start_node = GetChildren().GetFirst();
122
123 wxWindowList::Node *node = event.GetDirection() ? start_node->GetNext()
124 : start_node->GetPrevious();
125
126 while ( node != start_node )
127 {
128 // Have we come to the last or first item on the panel?
129 if ( !node )
130 {
131 // Check if our (may be grand) parent is another panel: if this is
132 // the case, they will know what to do with this navigation key and
133 // so give them the chance to process it instead of looping inside
134 // this panel (normally, the focus will go to the next/previous
135 // item after this panel in the parent panel).
136 wxWindow *focussed_child_of_parent = this;
137 for ( wxWindow *parent = GetParent(); parent; parent = parent->GetParent() )
138 {
139 // we don't want to tab into a different dialog or frame
140 if ( focussed_child_of_parent->IsTopLevel() )
141 break;
142
143 // is the parent a panel?
144 wxPanel *panel = wxDynamicCast(parent, wxPanel);
145 if (panel)
146 {
147 event.SetCurrentFocus( focussed_child_of_parent );
148 if (parent->GetEventHandler()->ProcessEvent( event ))
149 return;
150 }
151
152 focussed_child_of_parent = parent;
153 }
154
155 // no, we are not inside another panel so process this ourself
156 node = event.GetDirection() ? GetChildren().GetFirst()
157 : GetChildren().GetLast();
158
159 continue;
160 }
161
162 wxWindow *child = node->GetData();
163
164 if ( child->AcceptsFocus() )
165 {
166 m_winLastFocused = child; // should be redundant, but it is not
167 child->SetFocus();
168 return;
169 }
170
171 node = event.GetDirection() ? node->GetNext() : node->GetPrevious();
172 }
173
174 // we cycled through all of our children and none of them wanted to accept
175 // focus
176 event.Skip();
177 }
178
179
180 void wxPanel::OnSize(wxSizeEvent& WXUNUSED(event))
181 {
182 #if wxUSE_CONSTRAINTS
183 if (GetAutoLayout())
184 Layout();
185 #endif
186 }
187
188 void wxPanel::SetFocus()
189 {
190 wxLogTrace(_T("focus"), _T("SetFocus on wxPanel 0x%08x."), GetHandle());
191
192 // If the panel gets the focus *by way of getting it set directly*
193 // we move the focus to the first window that can get it.
194
195 // VZ: no, we set the focus to the last window too. I don't understand why
196 // should we make this distinction: if an app wants to set focus to
197 // some precise control, it may always do it directly, but if we don't
198 // use m_winLastFocused here, the focus won't be set correctly after a
199 // notebook page change nor after frame activation under MSW (it calls
200 // SetFocus too)
201 //
202 // If you still want to have old behaviour for wxGTK, edit the
203 // following line
204 #if 0 // def __WXGTK__
205 m_winLastFocused = (wxWindow *)NULL;
206 #endif // 0
207
208 if ( !SetFocusToChild() )
209 {
210 wxWindow::SetFocus();
211 }
212 }
213
214 void wxPanel::OnFocus(wxFocusEvent& event)
215 {
216 wxLogTrace(_T("focus"), _T("OnFocus on wxPanel 0x%08x."), GetHandle());
217
218 // If the panel gets the focus *by way of getting clicked on*
219 // we move the focus to either the last window that had the
220 // focus or the first one that can get it.
221 (void)SetFocusToChild();
222
223 event.Skip();
224 }
225
226 bool wxPanel::SetFocusToChild()
227 {
228 if ( m_winLastFocused )
229 {
230 // It might happen that the window got reparented or no longer accepts
231 // the focus.
232 if ( (m_winLastFocused->GetParent() == this) &&
233 m_winLastFocused->AcceptsFocus() )
234 {
235 wxLogTrace(_T("focus"),
236 _T("SetFocusToChild() => last child (0x%08x)."),
237 m_winLastFocused->GetHandle());
238
239 m_winLastFocused->SetFocus();
240 return TRUE;
241 }
242 else
243 {
244 // it doesn't count as such any more
245 m_winLastFocused = (wxWindow *)NULL;
246 }
247 }
248
249 // set the focus to the first child who wants it
250 wxWindowList::Node *node = GetChildren().GetFirst();
251 while ( node )
252 {
253 wxWindow *child = node->GetData();
254 if ( child->AcceptsFocus() )
255 {
256 wxLogTrace(_T("focus"),
257 _T("SetFocusToChild() => first child (0x%08x)."),
258 child->GetHandle());
259
260 m_winLastFocused = child; // should be redundant, but it is not
261 child->SetFocus();
262 return TRUE;
263 }
264
265 node = node->GetNext();
266 }
267
268 return FALSE;
269 }