]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
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 | |
87a1e308 | 9 | // Licence: wxWindows license |
c801d85f KB |
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 | |
2432b92d JS |
24 | #include "wx/object.h" |
25 | #include "wx/font.h" | |
26 | #include "wx/colour.h" | |
c801d85f | 27 | #include "wx/settings.h" |
ea451729 | 28 | #include "wx/log.h" |
c801d85f KB |
29 | #endif |
30 | ||
31 | #include "wx/generic/panelg.h" | |
32 | ||
c801d85f KB |
33 | IMPLEMENT_DYNAMIC_CLASS(wxPanel, wxWindow) |
34 | ||
35 | BEGIN_EVENT_TABLE(wxPanel, wxWindow) | |
36 | EVT_SYS_COLOUR_CHANGED(wxPanel::OnSysColourChanged) | |
341c92a8 | 37 | EVT_SET_FOCUS(wxPanel::OnFocus) |
90c3bdac | 38 | EVT_NAVIGATION_KEY(wxPanel::OnNavigationKey) |
27dc7e21 | 39 | EVT_SIZE(wxPanel::OnSize) |
c801d85f KB |
40 | END_EVENT_TABLE() |
41 | ||
c801d85f | 42 | |
edccf428 | 43 | void wxPanel::Init() |
c801d85f | 44 | { |
319fefa9 | 45 | m_winLastFocused = (wxWindow *)NULL; |
edccf428 | 46 | m_btnDefault = (wxButton *)NULL; |
c801d85f KB |
47 | } |
48 | ||
debe6624 | 49 | bool wxPanel::Create(wxWindow *parent, wxWindowID id, |
90c3bdac VZ |
50 | const wxPoint& pos, |
51 | const wxSize& size, | |
52 | long style, | |
53 | const wxString& name) | |
c801d85f | 54 | { |
b292e2f5 | 55 | bool ret = wxWindow::Create(parent, id, pos, size, style, name); |
c801d85f | 56 | |
fb99aca7 | 57 | if ( ret ) |
b292e2f5 | 58 | { |
b292e2f5 RR |
59 | SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE)); |
60 | SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT)); | |
b292e2f5 | 61 | } |
c801d85f | 62 | |
b292e2f5 | 63 | return ret; |
c801d85f KB |
64 | } |
65 | ||
66 | void wxPanel::InitDialog(void) | |
67 | { | |
b292e2f5 RR |
68 | wxInitDialogEvent event(GetId()); |
69 | event.SetEventObject(this); | |
70 | GetEventHandler()->ProcessEvent(event); | |
90c3bdac VZ |
71 | } |
72 | ||
c801d85f KB |
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)); | |
c801d85f KB |
77 | Refresh(); |
78 | ||
79 | // Propagate the event to the non-top-level children | |
80 | wxWindow::OnSysColourChanged(event); | |
81 | } | |
82 | ||
b292e2f5 | 83 | void wxPanel::OnNavigationKey( wxNavigationKeyEvent& event ) |
90c3bdac | 84 | { |
fb99aca7 | 85 | // there is not much to do if we have only one child (or not at all) |
399aa5e3 | 86 | if (GetChildren().GetCount() < 2) |
b292e2f5 RR |
87 | { |
88 | event.Skip(); | |
89 | return; | |
90c3bdac VZ |
90 | } |
91 | ||
b292e2f5 | 92 | // don't process these ones here |
fb99aca7 | 93 | if (event.IsWindowChange()) |
b292e2f5 RR |
94 | { |
95 | event.Skip(); | |
96 | return; | |
90c3bdac VZ |
97 | } |
98 | ||
3da17724 RR |
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(); | |
87a1e308 | 103 | |
3da17724 RR |
104 | // Do we know where the focus was ourselves, then? |
105 | if (!winFocus) | |
106 | winFocus = m_winLastFocused; | |
87a1e308 | 107 | |
fb99aca7 VZ |
108 | if (!winFocus) |
109 | winFocus = wxWindow::FindFocus(); | |
27dc7e21 | 110 | |
b292e2f5 RR |
111 | if (!winFocus) |
112 | { | |
113 | event.Skip(); | |
114 | return; | |
115 | } | |
fb99aca7 | 116 | |
f03fc89f | 117 | wxWindowList::Node *start_node = GetChildren().Find( winFocus ); |
a1665b22 VZ |
118 | if ( !start_node ) |
119 | start_node = GetChildren().Find( m_winLastFocused ); | |
120 | if ( !start_node ) | |
f03fc89f | 121 | start_node = GetChildren().GetFirst(); |
fb99aca7 | 122 | |
f03fc89f VZ |
123 | wxWindowList::Node *node = event.GetDirection() ? start_node->GetNext() |
124 | : start_node->GetPrevious(); | |
fb99aca7 | 125 | |
a1665b22 | 126 | while ( node != start_node ) |
b292e2f5 | 127 | { |
3da17724 | 128 | // Have we come to the last or first item on the panel? |
a1665b22 | 129 | if ( !node ) |
fb99aca7 | 130 | { |
3da17724 | 131 | // Check if our (may be grand) parent is another panel: if this is |
a1665b22 VZ |
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 | |
3da17724 | 135 | // item after this panel in the parent panel). |
87a1e308 | 136 | wxWindow *focussed_child_of_parent = this; |
3da17724 | 137 | for ( wxWindow *parent = GetParent(); parent; parent = parent->GetParent() ) |
a1665b22 | 138 | { |
87a1e308 VZ |
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); | |
3da17724 | 145 | if (panel) |
a1665b22 | 146 | { |
87a1e308 VZ |
147 | event.SetCurrentFocus( focussed_child_of_parent ); |
148 | if (parent->GetEventHandler()->ProcessEvent( event )) | |
ed58dbea | 149 | return; |
a1665b22 | 150 | } |
87a1e308 VZ |
151 | |
152 | focussed_child_of_parent = parent; | |
a1665b22 VZ |
153 | } |
154 | ||
155 | // no, we are not inside another panel so process this ourself | |
f03fc89f VZ |
156 | node = event.GetDirection() ? GetChildren().GetFirst() |
157 | : GetChildren().GetLast(); | |
341c92a8 | 158 | |
e4ffaca4 | 159 | continue; |
fb99aca7 VZ |
160 | } |
161 | ||
f03fc89f | 162 | wxWindow *child = node->GetData(); |
fb99aca7 | 163 | |
a1665b22 | 164 | if ( child->AcceptsFocus() ) |
fb99aca7 | 165 | { |
87a1e308 | 166 | m_winLastFocused = child; // should be redundant, but it is not |
fb99aca7 VZ |
167 | child->SetFocus(); |
168 | return; | |
169 | } | |
170 | ||
f03fc89f | 171 | node = event.GetDirection() ? node->GetNext() : node->GetPrevious(); |
b292e2f5 | 172 | } |
fb99aca7 VZ |
173 | |
174 | // we cycled through all of our children and none of them wanted to accept | |
175 | // focus | |
b292e2f5 | 176 | event.Skip(); |
6e4739a0 | 177 | } |
58614078 | 178 | |
27dc7e21 RD |
179 | |
180 | void wxPanel::OnSize(wxSizeEvent& WXUNUSED(event)) | |
181 | { | |
182 | #if wxUSE_CONSTRAINTS | |
d9317fd4 VZ |
183 | if (GetAutoLayout()) |
184 | Layout(); | |
27dc7e21 RD |
185 | #endif |
186 | } | |
187 | ||
3da17724 RR |
188 | void wxPanel::SetFocus() |
189 | { | |
00c4e897 VZ |
190 | wxLogTrace(_T("focus"), _T("SetFocus on wxPanel 0x%08x."), GetHandle()); |
191 | ||
3da17724 | 192 | // If the panel gets the focus *by way of getting it set directly* |
69ffe1d2 | 193 | // we move the focus to the first window that can get it. |
3da17724 | 194 | |
00c4e897 VZ |
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 | // | |
88413fec RR |
202 | // RR: yes, when I the tab key to navigate in a panel with some controls and |
203 | // a notebook and the focus jumps to the notebook (typically coming from | |
204 | // a button at the top) the notebook should focus the first child in the | |
205 | // current notebook page, not the last one which would otherwise get the | |
206 | // focus if you used the tab key to navigate from the current notebook | |
207 | // page to button at the bottom. See every page in the controls sample. | |
208 | ||
209 | #ifdef __WXGTK__ | |
00c4e897 VZ |
210 | m_winLastFocused = (wxWindow *)NULL; |
211 | #endif // 0 | |
212 | ||
213 | if ( !SetFocusToChild() ) | |
3da17724 | 214 | { |
00c4e897 | 215 | wxWindow::SetFocus(); |
3da17724 | 216 | } |
3da17724 RR |
217 | } |
218 | ||
341c92a8 VZ |
219 | void wxPanel::OnFocus(wxFocusEvent& event) |
220 | { | |
00c4e897 VZ |
221 | wxLogTrace(_T("focus"), _T("OnFocus on wxPanel 0x%08x."), GetHandle()); |
222 | ||
3da17724 | 223 | // If the panel gets the focus *by way of getting clicked on* |
69ffe1d2 RR |
224 | // we move the focus to either the last window that had the |
225 | // focus or the first one that can get it. | |
00c4e897 VZ |
226 | (void)SetFocusToChild(); |
227 | ||
228 | event.Skip(); | |
229 | } | |
3da17724 | 230 | |
00c4e897 VZ |
231 | bool wxPanel::SetFocusToChild() |
232 | { | |
233 | if ( m_winLastFocused ) | |
0492c5a0 | 234 | { |
00c4e897 VZ |
235 | // It might happen that the window got reparented or no longer accepts |
236 | // the focus. | |
237 | if ( (m_winLastFocused->GetParent() == this) && | |
238 | m_winLastFocused->AcceptsFocus() ) | |
87a1e308 | 239 | { |
00c4e897 VZ |
240 | wxLogTrace(_T("focus"), |
241 | _T("SetFocusToChild() => last child (0x%08x)."), | |
242 | m_winLastFocused->GetHandle()); | |
243 | ||
319fefa9 | 244 | m_winLastFocused->SetFocus(); |
00c4e897 VZ |
245 | return TRUE; |
246 | } | |
247 | else | |
248 | { | |
249 | // it doesn't count as such any more | |
250 | m_winLastFocused = (wxWindow *)NULL; | |
87a1e308 | 251 | } |
0492c5a0 | 252 | } |
87a1e308 | 253 | |
00c4e897 VZ |
254 | // set the focus to the first child who wants it |
255 | wxWindowList::Node *node = GetChildren().GetFirst(); | |
256 | while ( node ) | |
3da17724 | 257 | { |
00c4e897 VZ |
258 | wxWindow *child = node->GetData(); |
259 | if ( child->AcceptsFocus() ) | |
87a1e308 | 260 | { |
00c4e897 VZ |
261 | wxLogTrace(_T("focus"), |
262 | _T("SetFocusToChild() => first child (0x%08x)."), | |
263 | child->GetHandle()); | |
264 | ||
87a1e308 VZ |
265 | m_winLastFocused = child; // should be redundant, but it is not |
266 | child->SetFocus(); | |
00c4e897 | 267 | return TRUE; |
87a1e308 | 268 | } |
87a1e308 | 269 | |
00c4e897 VZ |
270 | node = node->GetNext(); |
271 | } | |
87a1e308 | 272 | |
00c4e897 | 273 | return FALSE; |
341c92a8 | 274 | } |