fixed infinite recursion in SetFocus()
[wxWidgets.git] / include / wx / containr.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/containr.h
3 // Purpose: wxControlContainer class declration: a "mix-in" class which
4 // implements the TAB navigation between the controls
5 // Author: Vadim Zeitlin
6 // Modified by:
7 // Created: 06.08.01
8 // RCS-ID: $Id$
9 // Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
10 // Licence: wxWindows licence
11 ///////////////////////////////////////////////////////////////////////////////
12
13 #ifndef _WX_CONTAINR_H_
14 #define _WX_CONTAINR_H_
15
16 #ifdef __GNUG__
17 #pragma interface "containr.h"
18 #endif
19
20 class WXDLLEXPORT wxFocusEvent;
21 class WXDLLEXPORT wxNavigationKeyEvent;
22 class WXDLLEXPORT wxWindow;
23 class WXDLLEXPORT wxWindowBase;
24
25 /*
26 Implementation note: wxControlContainer is not a real mix-in but rather
27 a class meant to be agregated with (and not inherited from). Although
28 logically it should be a mix-in, doing it like this has no advantage from
29 the point of view of the existing code but does have some problems (we'd
30 need to play tricks with event handlers which may be difficult to do
31 safely). The price we pay for this simplicity is the ugly macros below.
32 */
33
34 // ----------------------------------------------------------------------------
35 // wxControlContainer
36 // ----------------------------------------------------------------------------
37
38 class WXDLLEXPORT wxControlContainer
39 {
40 public:
41 // ctors and such
42 wxControlContainer(wxWindow *winParent);
43
44 wxWindow *GetDefaultItem() const { return m_winDefault; }
45 wxWindow *SetDefaultItem(wxWindow *win)
46 { wxWindow *winOld = m_winDefault; m_winDefault = win; return winOld; }
47
48 void SetLastFocus(wxWindow *win);
49
50 // the methods to be called from the window event handlers
51 void HandleOnNavigationKey(wxNavigationKeyEvent& event);
52 void HandleOnFocus(wxFocusEvent& event);
53 void HandleOnWindowDestroy(wxWindowBase *child);
54
55 // should be called from SetFocus(), returns FALSE if we did nothing with
56 // the focus and the default processing should take place
57 bool DoSetFocus();
58
59 protected:
60 // set the focus to the child which had it the last time
61 bool SetFocusToChild();
62
63 // the parent window we manage the children for
64 wxWindow *m_winParent;
65
66 // the child which had the focus last time this panel was activated
67 wxWindow *m_winLastFocused;
68
69 // a default window (e.g. a button) or NULL
70 wxWindow *m_winDefault;
71 };
72
73 // this function is for wxWindows internal use only
74 extern bool wxSetFocusToChild(wxWindow *win, wxWindow **child);
75
76 // ----------------------------------------------------------------------------
77 // macros which may be used by the classes wishing to implement TAB navigation
78 // among their children
79 // ----------------------------------------------------------------------------
80
81 // declare the methods to be forwarded
82 #define WX_DECLARE_CONTROL_CONTAINER() \
83 void OnNavigationKey(wxNavigationKeyEvent& event); \
84 void OnFocus(wxFocusEvent& event); \
85 virtual void OnChildFocus(wxChildFocusEvent& event); \
86 virtual void SetFocus(); \
87 virtual void RemoveChild(wxWindowBase *child); \
88 virtual wxWindow *GetDefaultItem() const; \
89 virtual wxWindow *SetDefaultItem(wxWindow *child) \
90
91 // implement the event table entries for wxControlContainer
92 #define WX_EVENT_TABLE_CONTROL_CONTAINER(classname) \
93 EVT_SET_FOCUS(classname::OnFocus) \
94 EVT_CHILD_FOCUS(classname::OnChildFocus) \
95 EVT_NAVIGATION_KEY(classname::OnNavigationKey)
96
97 // implement the methods forwarding to the wxControlContainer
98 #define WX_DELEGATE_TO_CONTROL_CONTAINER(classname, container) \
99 wxWindow *classname::SetDefaultItem(wxWindow *child) \
100 { \
101 return container->SetDefaultItem(child); \
102 } \
103 \
104 wxWindow *classname::GetDefaultItem() const \
105 { \
106 return container->GetDefaultItem(); \
107 } \
108 \
109 void classname::OnNavigationKey( wxNavigationKeyEvent& event ) \
110 { \
111 container->HandleOnNavigationKey(event); \
112 } \
113 \
114 void classname::RemoveChild(wxWindowBase *child) \
115 { \
116 container->HandleOnWindowDestroy(child); \
117 \
118 wxWindow::RemoveChild(child); \
119 } \
120 \
121 void classname::SetFocus() \
122 { \
123 if ( !container->DoSetFocus() ) \
124 wxWindow::SetFocus(); \
125 } \
126 \
127 void classname::OnChildFocus(wxChildFocusEvent& event) \
128 { \
129 container->SetLastFocus(event.GetWindow()); \
130 } \
131 \
132 void classname::OnFocus(wxFocusEvent& event) \
133 { \
134 container->HandleOnFocus(event); \
135 }
136
137
138 #endif // _WX_CONTAINR_H_