|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* vim: set ts=2 et sw=2 tw=80: */ |
|
3 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 #include "sdnTextAccessible.h" |
|
8 |
|
9 #include "ISimpleDOMText_i.c" |
|
10 |
|
11 #include "nsCoreUtils.h" |
|
12 #include "DocAccessible.h" |
|
13 |
|
14 #include "nsIFrame.h" |
|
15 #include "nsFontMetrics.h" |
|
16 #include "nsPresContext.h" |
|
17 #include "nsLayoutUtils.h" |
|
18 #include "gfxFont.h" |
|
19 #include "nsIAccessibleTypes.h" |
|
20 #include "mozilla/gfx/2D.h" |
|
21 |
|
22 using namespace mozilla::a11y; |
|
23 |
|
24 //////////////////////////////////////////////////////////////////////////////// |
|
25 // sdnTextAccessible |
|
26 //////////////////////////////////////////////////////////////////////////////// |
|
27 |
|
28 IMPL_IUNKNOWN_QUERY_HEAD(sdnTextAccessible) |
|
29 IMPL_IUNKNOWN_QUERY_IFACE(ISimpleDOMText) |
|
30 IMPL_IUNKNOWN_QUERY_TAIL_AGGREGATED(mAccessible) |
|
31 |
|
32 STDMETHODIMP |
|
33 sdnTextAccessible::get_domText(BSTR __RPC_FAR* aText) |
|
34 { |
|
35 A11Y_TRYBLOCK_BEGIN |
|
36 |
|
37 if (!aText) |
|
38 return E_INVALIDARG; |
|
39 *aText = nullptr; |
|
40 |
|
41 if (mAccessible->IsDefunct()) |
|
42 return CO_E_OBJNOTCONNECTED; |
|
43 |
|
44 nsAutoString nodeValue; |
|
45 |
|
46 nsCOMPtr<nsIDOMNode> DOMNode(do_QueryInterface(mAccessible->GetContent())); |
|
47 DOMNode->GetNodeValue(nodeValue); |
|
48 if (nodeValue.IsEmpty()) |
|
49 return S_FALSE; |
|
50 |
|
51 *aText = ::SysAllocStringLen(nodeValue.get(), nodeValue.Length()); |
|
52 return *aText ? S_OK : E_OUTOFMEMORY; |
|
53 |
|
54 A11Y_TRYBLOCK_END |
|
55 } |
|
56 |
|
57 STDMETHODIMP |
|
58 sdnTextAccessible::get_clippedSubstringBounds(unsigned int aStartIndex, |
|
59 unsigned int aEndIndex, |
|
60 int __RPC_FAR* aX, |
|
61 int __RPC_FAR* aY, |
|
62 int __RPC_FAR* aWidth, |
|
63 int __RPC_FAR* aHeight) |
|
64 { |
|
65 A11Y_TRYBLOCK_BEGIN |
|
66 |
|
67 nscoord x = 0, y = 0, width = 0, height = 0; |
|
68 HRESULT rv = get_unclippedSubstringBounds(aStartIndex, aEndIndex, |
|
69 &x, &y, &width, &height); |
|
70 if (FAILED(rv)) |
|
71 return rv; |
|
72 |
|
73 DocAccessible* document = mAccessible->Document(); |
|
74 NS_ASSERTION(document, |
|
75 "There must always be a doc accessible, but there isn't. Crash!"); |
|
76 |
|
77 nscoord docX = 0, docY = 0, docWidth = 0, docHeight = 0; |
|
78 document->GetBounds(&docX, &docY, &docWidth, &docHeight); |
|
79 |
|
80 nsIntRect unclippedRect(x, y, width, height); |
|
81 nsIntRect docRect(docX, docY, docWidth, docHeight); |
|
82 |
|
83 nsIntRect clippedRect; |
|
84 clippedRect.IntersectRect(unclippedRect, docRect); |
|
85 |
|
86 *aX = clippedRect.x; |
|
87 *aY = clippedRect.y; |
|
88 *aWidth = clippedRect.width; |
|
89 *aHeight = clippedRect.height; |
|
90 return S_OK; |
|
91 |
|
92 A11Y_TRYBLOCK_END |
|
93 } |
|
94 |
|
95 STDMETHODIMP |
|
96 sdnTextAccessible::get_unclippedSubstringBounds(unsigned int aStartIndex, |
|
97 unsigned int aEndIndex, |
|
98 int __RPC_FAR* aX, |
|
99 int __RPC_FAR* aY, |
|
100 int __RPC_FAR* aWidth, |
|
101 int __RPC_FAR* aHeight) |
|
102 { |
|
103 A11Y_TRYBLOCK_BEGIN |
|
104 |
|
105 if (!aX || !aY || !aWidth || !aHeight) |
|
106 return E_INVALIDARG; |
|
107 *aX = *aY = *aWidth = *aHeight = 0; |
|
108 |
|
109 if (mAccessible->IsDefunct()) |
|
110 return CO_E_OBJNOTCONNECTED; |
|
111 |
|
112 nsIFrame *frame = mAccessible->GetFrame(); |
|
113 NS_ENSURE_TRUE(frame, E_FAIL); |
|
114 |
|
115 nsPoint startPoint, endPoint; |
|
116 nsIFrame* startFrame = GetPointFromOffset(frame, aStartIndex, true, startPoint); |
|
117 nsIFrame* endFrame = GetPointFromOffset(frame, aEndIndex, false, endPoint); |
|
118 if (!startFrame || !endFrame) |
|
119 return E_FAIL; |
|
120 |
|
121 nsRect sum; |
|
122 nsIFrame* iter = startFrame; |
|
123 nsIFrame* stopLoopFrame = endFrame->GetNextContinuation(); |
|
124 for (; iter != stopLoopFrame; iter = iter->GetNextContinuation()) { |
|
125 nsRect rect = iter->GetScreenRectInAppUnits(); |
|
126 nscoord start = (iter == startFrame) ? startPoint.x : 0; |
|
127 nscoord end = (iter == endFrame) ? endPoint.x : rect.width; |
|
128 rect.x += start; |
|
129 rect.width = end - start; |
|
130 sum.UnionRect(sum, rect); |
|
131 } |
|
132 |
|
133 nsPresContext* presContext = mAccessible->Document()->PresContext(); |
|
134 *aX = presContext->AppUnitsToDevPixels(sum.x); |
|
135 *aY = presContext->AppUnitsToDevPixels(sum.y); |
|
136 *aWidth = presContext->AppUnitsToDevPixels(sum.width); |
|
137 *aHeight = presContext->AppUnitsToDevPixels(sum.height); |
|
138 |
|
139 return S_OK; |
|
140 |
|
141 A11Y_TRYBLOCK_END |
|
142 } |
|
143 |
|
144 STDMETHODIMP |
|
145 sdnTextAccessible::scrollToSubstring(unsigned int aStartIndex, |
|
146 unsigned int aEndIndex) |
|
147 { |
|
148 A11Y_TRYBLOCK_BEGIN |
|
149 |
|
150 if (mAccessible->IsDefunct()) |
|
151 return CO_E_OBJNOTCONNECTED; |
|
152 |
|
153 nsRefPtr<nsRange> range = new nsRange(mAccessible->GetContent()); |
|
154 if (NS_FAILED(range->SetStart(mAccessible->GetContent(), aStartIndex))) |
|
155 return E_FAIL; |
|
156 |
|
157 if (NS_FAILED(range->SetEnd(mAccessible->GetContent(), aEndIndex))) |
|
158 return E_FAIL; |
|
159 |
|
160 nsresult rv = |
|
161 nsCoreUtils::ScrollSubstringTo(mAccessible->GetFrame(), range, |
|
162 nsIAccessibleScrollType::SCROLL_TYPE_ANYWHERE); |
|
163 return GetHRESULT(rv); |
|
164 |
|
165 A11Y_TRYBLOCK_END |
|
166 } |
|
167 |
|
168 STDMETHODIMP |
|
169 sdnTextAccessible::get_fontFamily(BSTR __RPC_FAR* aFontFamily) |
|
170 { |
|
171 A11Y_TRYBLOCK_BEGIN |
|
172 |
|
173 if (!aFontFamily) |
|
174 return E_INVALIDARG; |
|
175 *aFontFamily = nullptr; |
|
176 |
|
177 if (mAccessible->IsDefunct()) |
|
178 return CO_E_OBJNOTCONNECTED; |
|
179 |
|
180 nsIFrame* frame = mAccessible->GetFrame(); |
|
181 if (!frame) |
|
182 return E_FAIL; |
|
183 |
|
184 nsRefPtr<nsFontMetrics> fm; |
|
185 nsLayoutUtils::GetFontMetricsForFrame(frame, getter_AddRefs(fm)); |
|
186 |
|
187 const nsString& name = fm->GetThebesFontGroup()->GetFontAt(0)->GetName(); |
|
188 if (name.IsEmpty()) |
|
189 return S_FALSE; |
|
190 |
|
191 *aFontFamily = ::SysAllocStringLen(name.get(), name.Length()); |
|
192 return *aFontFamily ? S_OK : E_OUTOFMEMORY; |
|
193 |
|
194 A11Y_TRYBLOCK_END |
|
195 } |
|
196 |
|
197 nsIFrame* |
|
198 sdnTextAccessible::GetPointFromOffset(nsIFrame* aContainingFrame, |
|
199 int32_t aOffset, |
|
200 bool aPreferNext, |
|
201 nsPoint& aOutPoint) |
|
202 { |
|
203 nsIFrame* textFrame = nullptr; |
|
204 int32_t outOffset; |
|
205 aContainingFrame->GetChildFrameContainingOffset(aOffset, aPreferNext, |
|
206 &outOffset, &textFrame); |
|
207 if (textFrame) |
|
208 textFrame->GetPointFromOffset(aOffset, &aOutPoint); |
|
209 |
|
210 return textFrame; |
|
211 } |