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 : * Netscape Communications Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 1998
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Author: Aaron Leventhal (aaronl@netscape.com)
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 : #include "nsHTMLImageAccessible.h"
40 :
41 : #include "nsAccUtils.h"
42 : #include "Role.h"
43 : #include "States.h"
44 :
45 : #include "imgIContainer.h"
46 : #include "imgIRequest.h"
47 : #include "nsIDocument.h"
48 : #include "nsIImageLoadingContent.h"
49 : #include "nsILink.h"
50 : #include "nsIPresShell.h"
51 : #include "nsIServiceManager.h"
52 : #include "nsIDOMHTMLImageElement.h"
53 : #include "nsIDOMDocument.h"
54 : #include "nsPIDOMWindow.h"
55 :
56 : using namespace mozilla::a11y;
57 :
58 : ////////////////////////////////////////////////////////////////////////////////
59 : // nsHTMLImageAccessible
60 : ////////////////////////////////////////////////////////////////////////////////
61 :
62 0 : nsHTMLImageAccessible::
63 : nsHTMLImageAccessible(nsIContent* aContent, nsDocAccessible* aDoc) :
64 0 : nsLinkableAccessible(aContent, aDoc)
65 : {
66 0 : }
67 :
68 0 : NS_IMPL_ISUPPORTS_INHERITED1(nsHTMLImageAccessible, nsAccessible,
69 : nsIAccessibleImage)
70 :
71 : ////////////////////////////////////////////////////////////////////////////////
72 : // nsAccessible public
73 :
74 : PRUint64
75 0 : nsHTMLImageAccessible::NativeState()
76 : {
77 : // The state is a bitfield, get our inherited state, then logically OR it with
78 : // states::ANIMATED if this is an animated image.
79 :
80 0 : PRUint64 state = nsLinkableAccessible::NativeState();
81 :
82 0 : nsCOMPtr<nsIImageLoadingContent> content(do_QueryInterface(mContent));
83 0 : nsCOMPtr<imgIRequest> imageRequest;
84 :
85 0 : if (content)
86 0 : content->GetRequest(nsIImageLoadingContent::CURRENT_REQUEST,
87 0 : getter_AddRefs(imageRequest));
88 :
89 0 : nsCOMPtr<imgIContainer> imgContainer;
90 0 : if (imageRequest)
91 0 : imageRequest->GetImage(getter_AddRefs(imgContainer));
92 :
93 0 : if (imgContainer) {
94 : bool animated;
95 0 : imgContainer->GetAnimated(&animated);
96 0 : if (animated)
97 0 : state |= states::ANIMATED;
98 : }
99 :
100 0 : return state;
101 : }
102 :
103 : nsresult
104 0 : nsHTMLImageAccessible::GetNameInternal(nsAString& aName)
105 : {
106 : bool hasAltAttrib =
107 0 : mContent->GetAttr(kNameSpaceID_None, nsGkAtoms::alt, aName);
108 0 : if (!aName.IsEmpty())
109 0 : return NS_OK;
110 :
111 0 : nsresult rv = nsAccessible::GetNameInternal(aName);
112 0 : NS_ENSURE_SUCCESS(rv, rv);
113 :
114 0 : if (aName.IsEmpty() && hasAltAttrib) {
115 : // No accessible name but empty 'alt' attribute is present. If further name
116 : // computation algorithm doesn't provide non empty name then it means
117 : // an empty 'alt' attribute was used to indicate a decorative image (see
118 : // nsIAccessible::name attribute for details).
119 0 : return NS_OK_EMPTY_NAME;
120 : }
121 :
122 0 : return NS_OK;
123 : }
124 :
125 : role
126 0 : nsHTMLImageAccessible::NativeRole()
127 : {
128 0 : return roles::GRAPHIC;
129 : }
130 :
131 : ////////////////////////////////////////////////////////////////////////////////
132 : // nsIAccessible
133 :
134 : PRUint8
135 0 : nsHTMLImageAccessible::ActionCount()
136 : {
137 0 : PRUint8 actionCount = nsLinkableAccessible::ActionCount();
138 0 : return HasLongDesc() ? actionCount + 1 : actionCount;
139 : }
140 :
141 : NS_IMETHODIMP
142 0 : nsHTMLImageAccessible::GetActionName(PRUint8 aIndex, nsAString& aName)
143 : {
144 0 : aName.Truncate();
145 :
146 0 : if (IsDefunct())
147 0 : return NS_ERROR_FAILURE;
148 :
149 0 : if (IsValidLongDescIndex(aIndex)) {
150 0 : aName.AssignLiteral("showlongdesc");
151 0 : return NS_OK;
152 : }
153 0 : return nsLinkableAccessible::GetActionName(aIndex, aName);
154 : }
155 :
156 : NS_IMETHODIMP
157 0 : nsHTMLImageAccessible::DoAction(PRUint8 aIndex)
158 : {
159 0 : if (IsDefunct())
160 0 : return NS_ERROR_FAILURE;
161 :
162 0 : if (IsValidLongDescIndex(aIndex)) {
163 : //get the long description uri and open in a new window
164 0 : nsCOMPtr<nsIDOMHTMLImageElement> element(do_QueryInterface(mContent));
165 0 : NS_ENSURE_TRUE(element, NS_ERROR_FAILURE);
166 :
167 0 : nsAutoString longDesc;
168 0 : nsresult rv = element->GetLongDesc(longDesc);
169 0 : NS_ENSURE_SUCCESS(rv, rv);
170 :
171 0 : nsIDocument* document = mContent->OwnerDoc();
172 0 : nsCOMPtr<nsPIDOMWindow> piWindow = document->GetWindow();
173 0 : nsCOMPtr<nsIDOMWindow> win = do_QueryInterface(piWindow);
174 0 : NS_ENSURE_TRUE(win, NS_ERROR_FAILURE);
175 0 : nsCOMPtr<nsIDOMWindow> tmp;
176 0 : return win->Open(longDesc, EmptyString(), EmptyString(),
177 0 : getter_AddRefs(tmp));
178 : }
179 0 : return nsLinkableAccessible::DoAction(aIndex);
180 : }
181 :
182 : ////////////////////////////////////////////////////////////////////////////////
183 : // nsIAccessibleImage
184 :
185 : NS_IMETHODIMP
186 0 : nsHTMLImageAccessible::GetImagePosition(PRUint32 aCoordType,
187 : PRInt32 *aX, PRInt32 *aY)
188 : {
189 : PRInt32 width, height;
190 0 : nsresult rv = GetBounds(aX, aY, &width, &height);
191 0 : if (NS_FAILED(rv))
192 0 : return rv;
193 :
194 0 : return nsAccUtils::ConvertScreenCoordsTo(aX, aY, aCoordType, this);
195 : }
196 :
197 : NS_IMETHODIMP
198 0 : nsHTMLImageAccessible::GetImageSize(PRInt32 *aWidth, PRInt32 *aHeight)
199 : {
200 : PRInt32 x, y;
201 0 : return GetBounds(&x, &y, aWidth, aHeight);
202 : }
203 :
204 : // nsAccessible
205 : nsresult
206 0 : nsHTMLImageAccessible::GetAttributesInternal(nsIPersistentProperties *aAttributes)
207 : {
208 0 : if (IsDefunct())
209 0 : return NS_ERROR_FAILURE;
210 :
211 0 : nsresult rv = nsLinkableAccessible::GetAttributesInternal(aAttributes);
212 0 : NS_ENSURE_SUCCESS(rv, rv);
213 :
214 0 : nsAutoString src;
215 0 : mContent->GetAttr(kNameSpaceID_None, nsGkAtoms::src, src);
216 0 : if (!src.IsEmpty())
217 0 : nsAccUtils::SetAccAttr(aAttributes, nsGkAtoms::src, src);
218 :
219 0 : return NS_OK;
220 : }
221 :
222 : ////////////////////////////////////////////////////////////////////////////////
223 : // Private methods
224 :
225 : bool
226 0 : nsHTMLImageAccessible::HasLongDesc()
227 : {
228 0 : if (IsDefunct())
229 0 : return false;
230 :
231 0 : return mContent->HasAttr(kNameSpaceID_None, nsGkAtoms::longdesc);
232 : }
233 :
234 : bool
235 0 : nsHTMLImageAccessible::IsValidLongDescIndex(PRUint8 aIndex)
236 : {
237 0 : if (!HasLongDesc())
238 0 : return false;
239 :
240 0 : return aIndex == nsLinkableAccessible::ActionCount();
241 : }
|