unset the button as temporary default item when it's destroyed (closes bug 1354875)
[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 class WXDLLEXPORT wxFocusEvent;
17 class WXDLLEXPORT wxNavigationKeyEvent;
18 class WXDLLEXPORT wxWindow;
19 class WXDLLEXPORT wxWindowBase;
20
21 /*
22 Implementation note: wxControlContainer is not a real mix-in but rather
23 a class meant to be agregated with (and not inherited from). Although
24 logically it should be a mix-in, doing it like this has no advantage from
25 the point of view of the existing code but does have some problems (we'd
26 need to play tricks with event handlers which may be difficult to do
27 safely). The price we pay for this simplicity is the ugly macros below.
28 */
29
30 // ----------------------------------------------------------------------------
31 // wxControlContainer
32 // ----------------------------------------------------------------------------
33
34 class WXDLLEXPORT wxControlContainer
35 {
36 public:
37 // ctors and such
38 wxControlContainer(wxWindow *winParent = NULL);
39 void SetContainerWindow(wxWindow *winParent) { m_winParent = winParent; }
40
41 // default item access: we have a permanent default item which is the one
42 // set by the user code but we may also have a temporary default item which
43 // would be chosen if the user pressed "Enter" now but the default action
44 // reverts to the "permanent" default as soon as this temporary default
45 // item lsoes focus
46
47 // get the default item, temporary or permanent
48 wxWindow *GetDefaultItem() const
49 { return m_winTmpDefault ? m_winTmpDefault : m_winDefault; }
50
51 // set the permanent default item, return its old value
52 wxWindow *SetDefaultItem(wxWindow *win)
53 { wxWindow *winOld = m_winDefault; m_winDefault = win; return winOld; }
54
55 // set a temporary default item, SetTmpDefaultItem(NULL) should be called
56 // soon after a call to SetTmpDefaultItem(window)
57 void SetTmpDefaultItem(wxWindow *win) { m_winTmpDefault = win; }
58
59 // return the temporary default item, can be NULL
60 wxWindow *GetTmpDefaultItem() const { return m_winTmpDefault; }
61
62
63 // the methods to be called from the window event handlers
64 void HandleOnNavigationKey(wxNavigationKeyEvent& event);
65 void HandleOnFocus(wxFocusEvent& event);
66 void HandleOnWindowDestroy(wxWindowBase *child);
67
68 // should be called from SetFocus(), returns false if we did nothing with
69 // the focus and the default processing should take place
70 bool DoSetFocus();
71
72 // can our child get the focus?
73 bool AcceptsFocus() const;
74
75 // called from OnChildFocus() handler, i.e. when one of our (grand)
76 // children gets the focus
77 void SetLastFocus(wxWindow *win);
78
79 protected:
80 // set the focus to the child which had it the last time
81 bool SetFocusToChild();
82
83 // the parent window we manage the children for
84 wxWindow *m_winParent;
85
86 // the child which had the focus last time this panel was activated
87 wxWindow *m_winLastFocused;
88
89 // a default window (usually a button) or NULL
90 wxWindow *m_winDefault;
91
92 // a temporary override of m_winDefault, use the latter if NULL
93 wxWindow *m_winTmpDefault;
94
95 // a guard against infinite recursion
96 bool m_inSetFocus;
97
98 DECLARE_NO_COPY_CLASS(wxControlContainer)
99 };
100
101 // this function is for wxWidgets internal use only
102 extern bool wxSetFocusToChild(wxWindow *win, wxWindow **child);
103
104 // ----------------------------------------------------------------------------
105 // macros which may be used by the classes wishing to implement TAB navigation
106 // among their children
107 // ----------------------------------------------------------------------------
108
109 // declare the methods to be forwarded
110 #define WX_DECLARE_CONTROL_CONTAINER() \
111 public: \
112 void OnNavigationKey(wxNavigationKeyEvent& event); \
113 void OnFocus(wxFocusEvent& event); \
114 virtual void OnChildFocus(wxChildFocusEvent& event); \
115 virtual void SetFocus(); \
116 virtual void SetFocusIgnoringChildren(); \
117 virtual void RemoveChild(wxWindowBase *child); \
118 virtual wxWindow *GetDefaultItem() const; \
119 virtual wxWindow *SetDefaultItem(wxWindow *child); \
120 virtual void SetTmpDefaultItem(wxWindow *win); \
121 virtual wxWindow *GetTmpDefaultItem() const; \
122 virtual bool AcceptsFocus() const; \
123 \
124 protected: \
125 wxControlContainer m_container
126
127 // implement the event table entries for wxControlContainer
128 #define WX_EVENT_TABLE_CONTROL_CONTAINER(classname) \
129 EVT_SET_FOCUS(classname::OnFocus) \
130 EVT_CHILD_FOCUS(classname::OnChildFocus) \
131 EVT_NAVIGATION_KEY(classname::OnNavigationKey)
132
133 // implement the methods forwarding to the wxControlContainer
134 #define WX_DELEGATE_TO_CONTROL_CONTAINER(classname) \
135 wxWindow *classname::SetDefaultItem(wxWindow *child) \
136 { \
137 return m_container.SetDefaultItem(child); \
138 } \
139 \
140 void classname::SetTmpDefaultItem(wxWindow *child) \
141 { \
142 m_container.SetTmpDefaultItem(child); \
143 } \
144 \
145 wxWindow *classname::GetDefaultItem() const \
146 { \
147 return m_container.GetDefaultItem(); \
148 } \
149 \
150 wxWindow *classname::GetTmpDefaultItem() const \
151 { \
152 return m_container.GetTmpDefaultItem(); \
153 } \
154 \
155 void classname::OnNavigationKey( wxNavigationKeyEvent& event ) \
156 { \
157 m_container.HandleOnNavigationKey(event); \
158 } \
159 \
160 void classname::RemoveChild(wxWindowBase *child) \
161 { \
162 m_container.HandleOnWindowDestroy(child); \
163 \
164 wxWindow::RemoveChild(child); \
165 } \
166 \
167 void classname::SetFocus() \
168 { \
169 if ( !m_container.DoSetFocus() ) \
170 wxWindow::SetFocus(); \
171 } \
172 \
173 void classname::SetFocusIgnoringChildren() \
174 { \
175 wxWindow::SetFocus(); \
176 } \
177 \
178 void classname::OnChildFocus(wxChildFocusEvent& event) \
179 { \
180 m_container.SetLastFocus(event.GetWindow()); \
181 } \
182 \
183 void classname::OnFocus(wxFocusEvent& event) \
184 { \
185 m_container.HandleOnFocus(event); \
186 } \
187 bool classname::AcceptsFocus() const \
188 { \
189 return m_container.AcceptsFocus(); \
190 }
191
192
193 #endif // _WX_CONTAINR_H_