Remove unused wxNavigationEnabled::AcceptFocus() method.
[wxWidgets.git] / include / wx / containr.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/containr.h
3 // Purpose: wxControlContainer and wxNavigationEnabled declarations
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 06.08.01
7 // RCS-ID: $Id$
8 // Copyright: (c) 2001, 2011 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_CONTAINR_H_
13 #define _WX_CONTAINR_H_
14
15 #include "wx/defs.h"
16
17 #ifndef wxHAS_NATIVE_TAB_TRAVERSAL
18 // We need wxEVT_XXX declarations in this case.
19 #include "wx/event.h"
20 #endif
21
22 class WXDLLIMPEXP_FWD_CORE wxWindow;
23 class WXDLLIMPEXP_FWD_CORE wxWindowBase;
24
25 /*
26 This header declares wxControlContainer class however it's not a real
27 container of controls but rather just a helper used to implement TAB
28 navigation among the window children. You should rarely need to use it
29 directly, derive from the documented public wxNavigationEnabled<> class to
30 implement TAB navigation in a custom composite window.
31 */
32
33 // ----------------------------------------------------------------------------
34 // wxControlContainerBase: common part used in both native and generic cases
35 // ----------------------------------------------------------------------------
36
37 class WXDLLIMPEXP_CORE wxControlContainerBase
38 {
39 public:
40 // default ctor, SetContainerWindow() must be called later
41 wxControlContainerBase()
42 {
43 m_winParent = NULL;
44
45 // do accept focus initially, we'll stop doing it if/when any children
46 // are added
47 m_acceptsFocus = true;
48 m_inSetFocus = false;
49 m_winLastFocused = NULL;
50 }
51 virtual ~wxControlContainerBase() {}
52
53 void SetContainerWindow(wxWindow *winParent)
54 {
55 wxASSERT_MSG( !m_winParent, wxT("shouldn't be called twice") );
56
57 m_winParent = winParent;
58 }
59
60 // should be called from SetFocus(), returns false if we did nothing with
61 // the focus and the default processing should take place
62 bool DoSetFocus();
63
64 // should be called when we decide that we should [stop] accepting focus
65 void SetCanFocus(bool acceptsFocus);
66
67 // returns whether we should accept focus ourselves or not
68 bool AcceptsFocus() const { return m_acceptsFocus; }
69
70 // returns whether we or one of our children accepts focus: we always do
71 // because if we don't have any focusable children it probably means that
72 // we're not being used as a container at all (think of wxGrid or generic
73 // wxListCtrl) and so should get focus for ourselves
74 bool AcceptsFocusRecursively() const { return true; }
75
76 // this is used to determine whether we can accept focus when Tab or
77 // another navigation key is pressed -- we alsways can, for the same reason
78 // as mentioned above for AcceptsFocusRecursively()
79 bool AcceptsFocusFromKeyboard() const { return true; }
80
81 // Call this when the number of children of the window changes.
82 // If we have any children, this panel (used just as container for
83 // them) shouldn't get focus for itself.
84 void UpdateCanFocus() { SetCanFocus(!HasAnyFocusableChildren()); }
85
86 protected:
87 // set the focus to the child which had it the last time
88 virtual bool SetFocusToChild();
89
90 // return true if we have any children accepting focus
91 bool HasAnyFocusableChildren() const;
92
93 // the parent window we manage the children for
94 wxWindow *m_winParent;
95
96 // the child which had the focus last time this panel was activated
97 wxWindow *m_winLastFocused;
98
99 private:
100 // value returned by AcceptsFocus(), should be changed using SetCanFocus()
101 // only
102 bool m_acceptsFocus;
103
104 // a guard against infinite recursion
105 bool m_inSetFocus;
106 };
107
108 #ifdef wxHAS_NATIVE_TAB_TRAVERSAL
109
110 // ----------------------------------------------------------------------------
111 // wxControlContainer for native TAB navigation
112 // ----------------------------------------------------------------------------
113
114 // this must be a real class as we forward-declare it elsewhere
115 class WXDLLIMPEXP_CORE wxControlContainer : public wxControlContainerBase
116 {
117 protected:
118 // set the focus to the child which had it the last time
119 virtual bool SetFocusToChild();
120 };
121
122 #else // !wxHAS_NATIVE_TAB_TRAVERSAL
123
124 // ----------------------------------------------------------------------------
125 // wxControlContainer for TAB navigation implemented in wx itself
126 // ----------------------------------------------------------------------------
127
128 class WXDLLIMPEXP_CORE wxControlContainer : public wxControlContainerBase
129 {
130 public:
131 // default ctor, SetContainerWindow() must be called later
132 wxControlContainer();
133
134 // the methods to be called from the window event handlers
135 void HandleOnNavigationKey(wxNavigationKeyEvent& event);
136 void HandleOnFocus(wxFocusEvent& event);
137 void HandleOnWindowDestroy(wxWindowBase *child);
138
139 // called from OnChildFocus() handler, i.e. when one of our (grand)
140 // children gets the focus
141 void SetLastFocus(wxWindow *win);
142
143 protected:
144
145 wxDECLARE_NO_COPY_CLASS(wxControlContainer);
146 };
147
148 #endif // wxHAS_NATIVE_TAB_TRAVERSAL/!wxHAS_NATIVE_TAB_TRAVERSAL
149
150 // this function is for wxWidgets internal use only
151 extern WXDLLIMPEXP_CORE bool wxSetFocusToChild(wxWindow *win, wxWindow **child);
152
153 // ----------------------------------------------------------------------------
154 // wxNavigationEnabled: Derive from this class to support keyboard navigation
155 // among window children in a wxWindow-derived class. The details of this class
156 // don't matter, you just need to derive from it to make navigation work.
157 // ----------------------------------------------------------------------------
158
159 // The template parameter W must be a wxWindow-derived class.
160 template <class W>
161 class wxNavigationEnabled : public W
162 {
163 public:
164 typedef W BaseWindowClass;
165
166 wxNavigationEnabled()
167 {
168 m_container.SetContainerWindow(this);
169
170 #ifndef wxHAS_NATIVE_TAB_TRAVERSAL
171 BaseWindowClass::Connect(wxEVT_NAVIGATION_KEY,
172 wxNavigationKeyEventHandler(wxNavigationEnabled::OnNavigationKey));
173
174 BaseWindowClass::Connect(wxEVT_SET_FOCUS,
175 wxFocusEventHandler(wxNavigationEnabled::OnFocus));
176
177 BaseWindowClass::Connect(wxEVT_CHILD_FOCUS,
178 wxChildFocusEventHandler(wxNavigationEnabled::OnChildFocus));
179 #endif // !wxHAS_NATIVE_TAB_TRAVERSAL
180 }
181
182 WXDLLIMPEXP_INLINE_CORE virtual bool AcceptsFocus() const
183 {
184 return m_container.AcceptsFocus();
185 }
186
187 WXDLLIMPEXP_INLINE_CORE virtual bool AcceptsFocusRecursively() const
188 {
189 return m_container.AcceptsFocusRecursively();
190 }
191
192 WXDLLIMPEXP_INLINE_CORE virtual bool AcceptsFocusFromKeyboard() const
193 {
194 return m_container.AcceptsFocusFromKeyboard();
195 }
196
197 WXDLLIMPEXP_INLINE_CORE virtual void AddChild(wxWindowBase *child)
198 {
199 BaseWindowClass::AddChild(child);
200
201 m_container.UpdateCanFocus();
202 }
203
204 WXDLLIMPEXP_INLINE_CORE virtual void RemoveChild(wxWindowBase *child)
205 {
206 #ifndef wxHAS_NATIVE_TAB_TRAVERSAL
207 m_container.HandleOnWindowDestroy(child);
208 #endif // !wxHAS_NATIVE_TAB_TRAVERSAL
209
210 BaseWindowClass::RemoveChild(child);
211
212 m_container.UpdateCanFocus();
213 }
214
215 WXDLLIMPEXP_INLINE_CORE virtual void SetFocus()
216 {
217 if ( !m_container.DoSetFocus() )
218 BaseWindowClass::SetFocus();
219 }
220
221 void SetFocusIgnoringChildren()
222 {
223 BaseWindowClass::SetFocus();
224 }
225
226 protected:
227 #ifndef wxHAS_NATIVE_TAB_TRAVERSAL
228 void OnNavigationKey(wxNavigationKeyEvent& event)
229 {
230 m_container.HandleOnNavigationKey(event);
231 }
232
233 void OnFocus(wxFocusEvent& event)
234 {
235 m_container.HandleOnFocus(event);
236 }
237
238 void OnChildFocus(wxChildFocusEvent& event)
239 {
240 m_container.SetLastFocus(event.GetWindow());
241 event.Skip();
242 }
243 #endif // !wxHAS_NATIVE_TAB_TRAVERSAL
244
245 wxControlContainer m_container;
246
247
248 wxDECLARE_NO_COPY_TEMPLATE_CLASS(wxNavigationEnabled, W);
249 };
250
251 // ----------------------------------------------------------------------------
252 // Compatibility macros from now on, do NOT use them and preferably do not even
253 // look at them.
254 // ----------------------------------------------------------------------------
255
256 #if WXWIN_COMPATIBILITY_2_8
257
258 // common part of WX_DECLARE_CONTROL_CONTAINER in the native and generic cases,
259 // it should be used in the wxWindow-derived class declaration
260 #define WX_DECLARE_CONTROL_CONTAINER_BASE() \
261 public: \
262 virtual bool AcceptsFocus() const; \
263 virtual bool AcceptsFocusRecursively() const; \
264 virtual bool AcceptsFocusFromKeyboard() const; \
265 virtual void AddChild(wxWindowBase *child); \
266 virtual void RemoveChild(wxWindowBase *child); \
267 virtual void SetFocus(); \
268 void SetFocusIgnoringChildren(); \
269 \
270 protected: \
271 wxControlContainer m_container
272
273 // this macro must be used in the derived class ctor
274 #define WX_INIT_CONTROL_CONTAINER() \
275 m_container.SetContainerWindow(this)
276
277 // common part of WX_DELEGATE_TO_CONTROL_CONTAINER in the native and generic
278 // cases, must be used in the wxWindow-derived class implementation
279 #define WX_DELEGATE_TO_CONTROL_CONTAINER_BASE(classname, basename) \
280 void classname::AddChild(wxWindowBase *child) \
281 { \
282 basename::AddChild(child); \
283 \
284 m_container.UpdateCanFocus(); \
285 } \
286 \
287 bool classname::AcceptsFocusRecursively() const \
288 { \
289 return m_container.AcceptsFocusRecursively(); \
290 } \
291 \
292 void classname::SetFocus() \
293 { \
294 if ( !m_container.DoSetFocus() ) \
295 basename::SetFocus(); \
296 } \
297 \
298 bool classname::AcceptsFocus() const \
299 { \
300 return m_container.AcceptsFocus(); \
301 } \
302 \
303 bool classname::AcceptsFocusFromKeyboard() const \
304 { \
305 return m_container.AcceptsFocusFromKeyboard(); \
306 }
307
308
309 #ifdef wxHAS_NATIVE_TAB_TRAVERSAL
310
311 #define WX_EVENT_TABLE_CONTROL_CONTAINER(classname)
312
313 #define WX_DECLARE_CONTROL_CONTAINER WX_DECLARE_CONTROL_CONTAINER_BASE
314
315 #define WX_DELEGATE_TO_CONTROL_CONTAINER(classname, basename) \
316 WX_DELEGATE_TO_CONTROL_CONTAINER_BASE(classname, basename) \
317 \
318 void classname::RemoveChild(wxWindowBase *child) \
319 { \
320 basename::RemoveChild(child); \
321 \
322 m_container.UpdateCanFocus(); \
323 } \
324 \
325 void classname::SetFocusIgnoringChildren() \
326 { \
327 basename::SetFocus(); \
328 }
329
330 #else // !wxHAS_NATIVE_TAB_TRAVERSAL
331
332 // declare the methods to be forwarded
333 #define WX_DECLARE_CONTROL_CONTAINER() \
334 WX_DECLARE_CONTROL_CONTAINER_BASE(); \
335 \
336 public: \
337 void OnNavigationKey(wxNavigationKeyEvent& event); \
338 void OnFocus(wxFocusEvent& event); \
339 virtual void OnChildFocus(wxChildFocusEvent& event)
340
341 // implement the event table entries for wxControlContainer
342 #define WX_EVENT_TABLE_CONTROL_CONTAINER(classname) \
343 EVT_SET_FOCUS(classname::OnFocus) \
344 EVT_CHILD_FOCUS(classname::OnChildFocus) \
345 EVT_NAVIGATION_KEY(classname::OnNavigationKey)
346
347 // implement the methods forwarding to the wxControlContainer
348 #define WX_DELEGATE_TO_CONTROL_CONTAINER(classname, basename) \
349 WX_DELEGATE_TO_CONTROL_CONTAINER_BASE(classname, basename) \
350 \
351 void classname::RemoveChild(wxWindowBase *child) \
352 { \
353 m_container.HandleOnWindowDestroy(child); \
354 \
355 basename::RemoveChild(child); \
356 \
357 m_container.UpdateCanFocus(); \
358 } \
359 \
360 void classname::OnNavigationKey( wxNavigationKeyEvent& event ) \
361 { \
362 m_container.HandleOnNavigationKey(event); \
363 } \
364 \
365 void classname::SetFocusIgnoringChildren() \
366 { \
367 basename::SetFocus(); \
368 } \
369 \
370 void classname::OnChildFocus(wxChildFocusEvent& event) \
371 { \
372 m_container.SetLastFocus(event.GetWindow()); \
373 event.Skip(); \
374 } \
375 \
376 void classname::OnFocus(wxFocusEvent& event) \
377 { \
378 m_container.HandleOnFocus(event); \
379 }
380
381 #endif // wxHAS_NATIVE_TAB_TRAVERSAL/!wxHAS_NATIVE_TAB_TRAVERSAL
382
383 #endif // WXWIN_COMPATIBILITY_2_8
384
385 #endif // _WX_CONTAINR_H_