1 : /* ***** BEGIN LICENSE BLOCK *****
2 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 : *
4 : * The contents of this file are subject to the Mozilla Public License Version
5 : * 1.1 (the "License"); you may not use this file except in compliance with
6 : * the License. You may obtain a copy of the License at
7 : * http://www.mozilla.org/MPL/
8 : *
9 : * Software distributed under the License is distributed on an "AS IS" basis,
10 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 : * for the specific language governing rights and limitations under the
12 : * License.
13 : *
14 : * The Original Code is mozilla.org code.
15 : *
16 : * The Initial Developer of the Original Code is
17 : * Mozilla Foundation.
18 : * Portions created by the Initial Developer are Copyright (C) 2011
19 : * the Initial Developer. All Rights Reserved.
20 : *
21 : * Contributor(s):
22 : * Alexander Surkov <surkov.alexander@gmail.com> (original author)
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either the GNU General Public License Version 2 or later (the "GPL"), or
26 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #ifndef mozilla_a11y_FocusManager_h_
39 : #define mozilla_a11y_FocusManager_h_
40 :
41 : #include "nsAutoPtr.h"
42 : #include "mozilla/dom/Element.h"
43 :
44 : class AccEvent;
45 : class nsAccessible;
46 : class nsDocAccessible;
47 :
48 : namespace mozilla {
49 : namespace a11y {
50 :
51 : /**
52 : * Manage the accessible focus. Used to fire and process accessible events.
53 : */
54 : class FocusManager
55 : {
56 : public:
57 : virtual ~FocusManager();
58 :
59 : /**
60 : * Return a focused accessible.
61 : */
62 : nsAccessible* FocusedAccessible() const;
63 :
64 : /**
65 : * Return true if given accessible is focused.
66 : */
67 : bool IsFocused(const nsAccessible* aAccessible) const;
68 :
69 : /**
70 : * Return true if the given accessible is an active item, i.e. an item that
71 : * is current within the active widget.
72 : */
73 0 : inline bool IsActiveItem(const nsAccessible* aAccessible)
74 0 : { return aAccessible == mActiveItem; }
75 :
76 : /**
77 : * Return true if given DOM node has DOM focus.
78 : */
79 0 : inline bool HasDOMFocus(const nsINode* aNode) const
80 0 : { return aNode == FocusedDOMNode(); }
81 :
82 : /**
83 : * Return true if focused accessible is within the given container.
84 : */
85 : bool IsFocusWithin(const nsAccessible* aContainer) const;
86 :
87 : /**
88 : * Return whether the given accessible is focused or contains the focus or
89 : * contained by focused accessible.
90 : */
91 : enum FocusDisposition {
92 : eNone,
93 : eFocused,
94 : eContainsFocus,
95 : eContainedByFocus
96 : };
97 : FocusDisposition IsInOrContainsFocus(const nsAccessible* aAccessible) const;
98 :
99 : //////////////////////////////////////////////////////////////////////////////
100 : // Focus notifications and processing (don't use until you know what you do).
101 :
102 : /**
103 : * Called when DOM focus event is fired.
104 : */
105 : void NotifyOfDOMFocus(nsISupports* aTarget);
106 :
107 : /**
108 : * Called when DOM blur event is fired.
109 : */
110 : void NotifyOfDOMBlur(nsISupports* aTarget);
111 :
112 : /**
113 : * Called when active item is changed. Note: must be called when accessible
114 : * tree is up to date.
115 : */
116 : void ActiveItemChanged(nsAccessible* aItem, bool aCheckIfActive = true);
117 :
118 : /**
119 : * Dispatch delayed focus event for the current focus accessible.
120 : */
121 : void ForceFocusEvent();
122 :
123 : /**
124 : * Dispatch delayed focus event for the given target.
125 : */
126 : void DispatchFocusEvent(nsDocAccessible* aDocument, nsAccessible* aTarget);
127 :
128 : /**
129 : * Process DOM focus notification.
130 : */
131 : void ProcessDOMFocus(nsINode* aTarget);
132 :
133 : /**
134 : * Process the delayed accessible event.
135 : * do.
136 : */
137 : void ProcessFocusEvent(AccEvent* aEvent);
138 :
139 : protected:
140 : FocusManager();
141 :
142 : private:
143 : FocusManager(const FocusManager&);
144 : FocusManager& operator =(const FocusManager&);
145 :
146 : /**
147 : * Return DOM node having DOM focus.
148 : */
149 : nsINode* FocusedDOMNode() const;
150 :
151 : /**
152 : * Return DOM document having DOM focus.
153 : */
154 : nsIDocument* FocusedDOMDocument() const;
155 :
156 : private:
157 : nsRefPtr<nsAccessible> mActiveItem;
158 : nsRefPtr<nsAccessible> mActiveARIAMenubar;
159 : };
160 :
161 : } // namespace a11y
162 : } // namespace mozilla
163 :
164 :
165 : //#define A11YDEBUG_FOCUS
166 :
167 : #ifdef A11YDEBUG_FOCUS
168 :
169 : // Util macros (don't use them directly)
170 : #define A11YDEBUG_FOCUS_STARTBLOCK \
171 : printf(" {\n ");
172 :
173 : #define A11YDEBUG_FOCUS_ENDBLOCK \
174 : printf("\n }\n");
175 :
176 : #define A11YDEBUG_FOCUS_BLOCKOFFSET \
177 : printf(" ");
178 :
179 : #define A11YDEBUG_FOCUS_LOG_TIME \
180 : { \
181 : PRIntervalTime time = PR_IntervalNow(); \
182 : PRUint32 mins = (PR_IntervalToSeconds(time) / 60) % 60; \
183 : PRUint32 secs = PR_IntervalToSeconds(time) % 60; \
184 : PRUint32 msecs = PR_IntervalToMilliseconds(time) % 1000; \
185 : printf("Time: %2d:%2d.%3d\n", mins, secs, msecs); \
186 : }
187 :
188 : #define A11YDEBUG_FOCUS_LOG_DOMNODE(aNode) \
189 : if (aNode) { \
190 : if (aNode->IsElement()) { \
191 : dom::Element* targetElm = aNode->AsElement(); \
192 : nsCAutoString tag; \
193 : targetElm->Tag()->ToUTF8String(tag); \
194 : nsCAutoString id; \
195 : nsIAtom* atomid = targetElm->GetID(); \
196 : if (atomid) \
197 : atomid->ToUTF8String(id); \
198 : printf("element %s@id='%s': %p", tag.get(), id.get(), (void*)aNode); \
199 : } else if (aNode->IsNodeOfType(nsINode::eDOCUMENT)) { \
200 : nsCOMPtr<nsIDocument> document = do_QueryInterface(aNode); \
201 : nsIURI* uri = document->GetDocumentURI(); \
202 : nsCAutoString spec; \
203 : uri->GetSpec(spec); \
204 : printf("document: %p; uri: %s", (void*)aNode, spec.get()); \
205 : } \
206 : }
207 :
208 : #define A11YDEBUG_FOCUS_LOG_ACCESSIBLE(aAccessible) \
209 : printf("accessible: %p; ", (void*)aAccessible); \
210 : if (aAccessible) { \
211 : nsAutoString role; \
212 : GetAccService()->GetStringRole(aAccessible->Role(), role); \
213 : nsAutoString name; \
214 : aAccessible->GetName(name); \
215 : printf(" role: %s, name: %s; ", NS_ConvertUTF16toUTF8(role).get(), \
216 : NS_ConvertUTF16toUTF8(name).get()); \
217 : A11YDEBUG_FOCUS_LOG_DOMNODE(aAccessible->GetNode()) \
218 : }
219 :
220 : // Public macros
221 : #define A11YDEBUG_FOCUS_LOG_DOMTARGET(aMsg, aTarget) \
222 : A11YDEBUG_FOCUS_STARTBLOCK \
223 : printf(aMsg "\n"); \
224 : if (aTarget) { \
225 : A11YDEBUG_FOCUS_BLOCKOFFSET \
226 : A11YDEBUG_FOCUS_LOG_DOMNODE(aTarget) \
227 : } \
228 : A11YDEBUG_FOCUS_ENDBLOCK
229 :
230 : #define A11YDEBUG_FOCUS_LOG_ACCTARGET(aMsg, aTarget) \
231 : A11YDEBUG_FOCUS_STARTBLOCK \
232 : printf(aMsg "\n"); \
233 : A11YDEBUG_FOCUS_BLOCKOFFSET \
234 : A11YDEBUG_FOCUS_LOG_ACCESSIBLE(aTarget) \
235 : A11YDEBUG_FOCUS_ENDBLOCK
236 :
237 : #define A11YDEBUG_FOCUS_LOG_WIDGET(aMsg, aWidget) \
238 : A11YDEBUG_FOCUS_STARTBLOCK \
239 : printf(aMsg "\n"); \
240 : A11YDEBUG_FOCUS_BLOCKOFFSET \
241 : A11YDEBUG_FOCUS_LOG_ACCESSIBLE(aWidget) \
242 : printf("; widget is active: %s, has operable items: %s", \
243 : (aWidget && aWidget->IsActiveWidget() ? "true" : "false"), \
244 : (aWidget && aWidget->AreItemsOperable() ? "true" : "false")); \
245 : A11YDEBUG_FOCUS_ENDBLOCK
246 :
247 : #define A11YDEBUG_FOCUS_NOTIFICATION_SUPPORTSTARGET(aMsg, aTargetMsg, aTarget) \
248 : printf("\nA11Y FOCUS: " aMsg ". "); \
249 : A11YDEBUG_FOCUS_LOG_TIME \
250 : if (aTarget) { \
251 : A11YDEBUG_FOCUS_STARTBLOCK \
252 : printf(aTargetMsg "\n"); \
253 : A11YDEBUG_FOCUS_BLOCKOFFSET \
254 : nsCOMPtr<nsINode> targetNode(do_QueryInterface(aTarget)); \
255 : if (targetNode) { \
256 : A11YDEBUG_FOCUS_LOG_DOMNODE(targetNode) \
257 : } else { \
258 : printf("window: %p", (void*)aTarget); \
259 : } \
260 : A11YDEBUG_FOCUS_ENDBLOCK \
261 : }
262 :
263 : #define A11YDEBUG_FOCUS_NOTIFICATION_DOMTARGET(aMsg, aTargetMsg, aTarget) \
264 : printf("\nA11Y FOCUS: " aMsg ". "); \
265 : A11YDEBUG_FOCUS_LOG_TIME \
266 : A11YDEBUG_FOCUS_LOG_DOMTARGET(aTargetMsg, aTarget)
267 :
268 : #define A11YDEBUG_FOCUS_NOTIFICATION_ACCTARGET(aMsg, aTargetMsg, aTarget) \
269 : printf("\nA11Y FOCUS: " aMsg ". "); \
270 : A11YDEBUG_FOCUS_LOG_TIME \
271 : A11YDEBUG_FOCUS_LOG_ACCTARGET(aTargetMsg, aTarget)
272 :
273 : #define A11YDEBUG_FOCUS_ACTIVEITEMCHANGE_CAUSE(aMsg, aTarget) \
274 : A11YDEBUG_FOCUS_LOG_ACCTARGET("Caused by: " aMsg, aTarget)
275 :
276 : #else
277 : #define A11YDEBUG_FOCUS_LOG_DOMTARGET(aMsg, aTarget)
278 : #define A11YDEBUG_FOCUS_LOG_ACCTARGET(aMsg, aTarget)
279 : #define A11YDEBUG_FOCUS_LOG_WIDGET(aMsg, aWidget)
280 : #define A11YDEBUG_FOCUS_NOTIFICATION_SUPPORTSTARGET(aMsg, aTargetMsg, aTarget)
281 : #define A11YDEBUG_FOCUS_NOTIFICATION_DOMTARGET(aMsg, aTargetMsg, aTarget)
282 : #define A11YDEBUG_FOCUS_NOTIFICATION_ACCTARGET(aMsg, aTargetMsg, aTarget)
283 : #define A11YDEBUG_FOCUS_ACTIVEITEMCHANGE_CAUSE(aMsg, aTarget)
284 : #endif
285 :
286 : #endif
|