1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Mozilla Foundation.
19 : * Portions created by the Initial Developer are Copyright (C) 2007
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Alexander Surkov <surkov.alexander@gmail.com> (original author)
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either of the GNU General Public License Version 2 or later (the "GPL"),
27 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #ifndef nsCoreUtils_h_
40 : #define nsCoreUtils_h_
41 :
42 :
43 : #include "nsIDOMNode.h"
44 : #include "nsIContent.h"
45 : #include "nsIBoxObject.h"
46 : #include "nsITreeBoxObject.h"
47 : #include "nsITreeColumns.h"
48 :
49 : #include "nsIFrame.h"
50 : #include "nsIDocShellTreeItem.h"
51 : #include "nsIDOMCSSStyleDeclaration.h"
52 : #include "nsIDOMDOMStringList.h"
53 : #include "nsIMutableArray.h"
54 : #include "nsPoint.h"
55 : #include "nsTArray.h"
56 :
57 : /**
58 : * Core utils.
59 : */
60 : class nsCoreUtils
61 : {
62 : public:
63 : /**
64 : * Return true if the given node has registered click, mousedown or mouseup
65 : * event listeners.
66 : */
67 : static bool HasClickListener(nsIContent *aContent);
68 :
69 : /**
70 : * Dispatch click event to XUL tree cell.
71 : *
72 : * @param aTreeBoxObj [in] tree box object
73 : * @param aRowIndex [in] row index
74 : * @param aColumn [in] column object
75 : * @param aPseudoElm [in] pseudo elemenet inside the cell, see
76 : * nsITreeBoxObject for available values
77 : */
78 : static void DispatchClickEvent(nsITreeBoxObject *aTreeBoxObj,
79 : PRInt32 aRowIndex, nsITreeColumn *aColumn,
80 0 : const nsCString& aPseudoElt = EmptyCString());
81 :
82 : /**
83 : * Send mouse event to the given element.
84 : *
85 : * @param aEventType [in] an event type (see nsGUIEvent.h for constants)
86 : * @param aPresShell [in] the presshell for the given element
87 : * @param aContent [in] the element
88 : */
89 : static bool DispatchMouseEvent(PRUint32 aEventType,
90 : nsIPresShell *aPresShell,
91 : nsIContent *aContent);
92 :
93 : /**
94 : * Send mouse event to the given element.
95 : *
96 : * @param aEventType [in] an event type (see nsGUIEvent.h for constants)
97 : * @param aX [in] x coordinate in dev pixels
98 : * @param aY [in] y coordinate in dev pixels
99 : * @param aContent [in] the element
100 : * @param aFrame [in] frame of the element
101 : * @param aPresShell [in] the presshell for the element
102 : * @param aRootWidget [in] the root widget of the element
103 : */
104 : static void DispatchMouseEvent(PRUint32 aEventType, PRInt32 aX, PRInt32 aY,
105 : nsIContent *aContent, nsIFrame *aFrame,
106 : nsIPresShell *aPresShell,
107 : nsIWidget *aRootWidget);
108 :
109 : /**
110 : * Return an accesskey registered on the given element by
111 : * nsEventStateManager or 0 if there is no registered accesskey.
112 : *
113 : * @param aContent - the given element.
114 : */
115 : static PRUint32 GetAccessKeyFor(nsIContent *aContent);
116 :
117 : /**
118 : * Return DOM element related with the given node, i.e.
119 : * a) itself if it is DOM element
120 : * b) parent element if it is text node
121 : * c) otherwise nsnull
122 : *
123 : * @param aNode [in] the given DOM node
124 : */
125 : static nsIContent* GetDOMElementFor(nsIContent *aContent);
126 :
127 : /**
128 : * Return DOM node for the given DOM point.
129 : */
130 : static nsINode *GetDOMNodeFromDOMPoint(nsINode *aNode, PRUint32 aOffset);
131 :
132 : /**
133 : * Return the nsIContent* to check for ARIA attributes on -- this may not
134 : * always be the DOM node for the accessible. Specifically, for doc
135 : * accessibles, it is not the document node, but either the root element or
136 : * <body> in HTML.
137 : *
138 : * @param aNode [in] DOM node for the accessible that may be affected by ARIA
139 : * @return the nsIContent which may have ARIA markup
140 : */
141 : static nsIContent* GetRoleContent(nsINode *aNode);
142 :
143 : /**
144 : * Is the first passed in node an ancestor of the second?
145 : * Note: A node is not considered to be the ancestor of itself.
146 : *
147 : * @param aPossibleAncestorNode [in] node to test for ancestor-ness of
148 : * aPossibleDescendantNode
149 : * @param aPossibleDescendantNode [in] node to test for descendant-ness of
150 : * aPossibleAncestorNode
151 : * @param aRootNode [in, optional] the root node that search
152 : * search should be performed within
153 : * @return true if aPossibleAncestorNode is an ancestor of
154 : * aPossibleDescendantNode
155 : */
156 : static bool IsAncestorOf(nsINode *aPossibleAncestorNode,
157 : nsINode *aPossibleDescendantNode,
158 : nsINode *aRootNode = nsnull);
159 :
160 : /**
161 : * Helper method to scroll range into view, used for implementation of
162 : * nsIAccessibleText::scrollSubstringTo().
163 : *
164 : * @param aFrame the frame for accessible the range belongs to.
165 : * @param aStartNode start node of a range
166 : * @param aStartOffset an offset inside the start node
167 : * @param aEndNode end node of a range
168 : * @param aEndOffset an offset inside the end node
169 : * @param aScrollType the place a range should be scrolled to
170 : */
171 : static nsresult ScrollSubstringTo(nsIFrame *aFrame,
172 : nsIDOMNode *aStartNode, PRInt32 aStartIndex,
173 : nsIDOMNode *aEndNode, PRInt32 aEndIndex,
174 : PRUint32 aScrollType);
175 :
176 : /** Helper method to scroll range into view, used for implementation of
177 : * nsIAccessibleText::scrollSubstringTo[Point]().
178 : *
179 : * @param aFrame the frame for accessible the range belongs to.
180 : * @param aStartNode start node of a range
181 : * @param aStartOffset an offset inside the start node
182 : * @param aEndNode end node of a range
183 : * @param aEndOffset an offset inside the end node
184 : * @param aVPercent how to align vertically, specified in percents
185 : * @param aHPercent how to align horizontally, specified in percents
186 : */
187 : static nsresult ScrollSubstringTo(nsIFrame *aFrame,
188 : nsIDOMNode *aStartNode, PRInt32 aStartIndex,
189 : nsIDOMNode *aEndNode, PRInt32 aEndIndex,
190 : PRInt16 aVPercent, PRInt16 aHPercent);
191 :
192 : /**
193 : * Scrolls the given frame to the point, used for implememntation of
194 : * nsIAccessible::scrollToPoint and nsIAccessibleText::scrollSubstringToPoint.
195 : *
196 : * @param aScrollableFrame the scrollable frame
197 : * @param aFrame the frame to scroll
198 : * @param aPoint the point scroll to
199 : */
200 : static void ScrollFrameToPoint(nsIFrame *aScrollableFrame,
201 : nsIFrame *aFrame, const nsIntPoint& aPoint);
202 :
203 : /**
204 : * Converts scroll type constant defined in nsIAccessibleScrollType to
205 : * vertical and horizontal percents.
206 : */
207 : static void ConvertScrollTypeToPercents(PRUint32 aScrollType,
208 : PRInt16 *aVPercent,
209 : PRInt16 *aHPercent);
210 :
211 : /**
212 : * Returns coordinates relative screen for the top level window.
213 : *
214 : * @param aNode the DOM node hosted in the window.
215 : */
216 : static nsIntPoint GetScreenCoordsForWindow(nsINode *aNode);
217 :
218 : /**
219 : * Return document shell tree item for the given DOM node.
220 : */
221 : static already_AddRefed<nsIDocShellTreeItem>
222 : GetDocShellTreeItemFor(nsINode *aNode);
223 :
224 : /**
225 : * Return true if the given document is root document.
226 : */
227 : static bool IsRootDocument(nsIDocument *aDocument);
228 :
229 : /**
230 : * Return true if the given document is content document (not chrome).
231 : */
232 : static bool IsContentDocument(nsIDocument *aDocument);
233 :
234 : /**
235 : * Return true if the given document node is for tab document accessible.
236 : */
237 : static bool IsTabDocument(nsIDocument* aDocumentNode);
238 :
239 : /**
240 : * Return true if the given document is an error page.
241 : */
242 : static bool IsErrorPage(nsIDocument *aDocument);
243 :
244 : /**
245 : * Return presShell for the document containing the given DOM node.
246 : */
247 0 : static nsIPresShell *GetPresShellFor(nsINode *aNode)
248 : {
249 0 : return aNode->OwnerDoc()->GetShell();
250 : }
251 :
252 : /**
253 : * Return document node for the given document shell tree item.
254 : */
255 : static already_AddRefed<nsIDOMNode>
256 : GetDOMNodeForContainer(nsIDocShellTreeItem *aContainer);
257 :
258 : /**
259 : * Get the ID for an element, in some types of XML this may not be the ID attribute
260 : * @param aContent Node to get the ID for
261 : * @param aID Where to put ID string
262 : * @return true if there is an ID set for this node
263 : */
264 : static bool GetID(nsIContent *aContent, nsAString& aID);
265 :
266 : /**
267 : * Convert attribute value of the given node to positive integer. If no
268 : * attribute or wrong value then false is returned.
269 : */
270 : static bool GetUIntAttr(nsIContent *aContent, nsIAtom *aAttr,
271 : PRInt32 *aUInt);
272 :
273 : /**
274 : * Check if the given element is XLink.
275 : *
276 : * @param aContent the given element
277 : * @return true if the given element is XLink
278 : */
279 : static bool IsXLink(nsIContent *aContent);
280 :
281 : /**
282 : * Returns language for the given node.
283 : *
284 : * @param aContent [in] the given node
285 : * @param aRootContent [in] container of the given node
286 : * @param aLanguage [out] language
287 : */
288 : static void GetLanguageFor(nsIContent *aContent, nsIContent *aRootContent,
289 : nsAString& aLanguage);
290 :
291 : /**
292 : * Return box object for XUL treechildren element by tree box object.
293 : */
294 : static already_AddRefed<nsIBoxObject>
295 : GetTreeBodyBoxObject(nsITreeBoxObject *aTreeBoxObj);
296 :
297 : /**
298 : * Return tree box object from any levels DOMNode under the XUL tree.
299 : */
300 : static already_AddRefed<nsITreeBoxObject>
301 : GetTreeBoxObject(nsIContent* aContent);
302 :
303 : /**
304 : * Return first sensible column for the given tree box object.
305 : */
306 : static already_AddRefed<nsITreeColumn>
307 : GetFirstSensibleColumn(nsITreeBoxObject *aTree);
308 :
309 : /**
310 : * Return sensible columns count for the given tree box object.
311 : */
312 : static PRUint32 GetSensibleColumnCount(nsITreeBoxObject *aTree);
313 :
314 : /**
315 : * Return sensible column at the given index for the given tree box object.
316 : */
317 : static already_AddRefed<nsITreeColumn>
318 : GetSensibleColumnAt(nsITreeBoxObject *aTree, PRUint32 aIndex);
319 :
320 : /**
321 : * Return next sensible column for the given column.
322 : */
323 : static already_AddRefed<nsITreeColumn>
324 : GetNextSensibleColumn(nsITreeColumn *aColumn);
325 :
326 : /**
327 : * Return previous sensible column for the given column.
328 : */
329 : static already_AddRefed<nsITreeColumn>
330 : GetPreviousSensibleColumn(nsITreeColumn *aColumn);
331 :
332 : /**
333 : * Return true if the given column is hidden (i.e. not sensible).
334 : */
335 : static bool IsColumnHidden(nsITreeColumn *aColumn);
336 :
337 : /**
338 : * Return true if the given node is table header element.
339 : */
340 0 : static bool IsHTMLTableHeader(nsIContent *aContent)
341 : {
342 0 : return aContent->NodeInfo()->Equals(nsGkAtoms::th) ||
343 0 : aContent->HasAttr(kNameSpaceID_None, nsGkAtoms::scope);
344 : }
345 :
346 : };
347 :
348 :
349 : /**
350 : * nsIDOMDOMStringList implementation.
351 : */
352 : class nsAccessibleDOMStringList : public nsIDOMDOMStringList
353 : {
354 : public:
355 0 : nsAccessibleDOMStringList() {};
356 0 : virtual ~nsAccessibleDOMStringList() {};
357 :
358 : NS_DECL_ISUPPORTS
359 : NS_DECL_NSIDOMDOMSTRINGLIST
360 :
361 0 : bool Add(const nsAString& aName) {
362 0 : return mNames.AppendElement(aName) != nsnull;
363 : }
364 :
365 : private:
366 : nsTArray<nsString> mNames;
367 : };
368 :
369 : #endif
370 :
|