Bug 453883, ensure true/false marcos are available, r=joshmoz, sr=jst
[wine-gecko.git] / accessible / src / msaa / CAccessibleComponent.cpp
blobe76811de5785acd69c32067ec2b7610377155253
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:expandtab:shiftwidth=2:tabstop=2:
3 */
4 /* ***** BEGIN LICENSE BLOCK *****
5 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
7 * The contents of this file are subject to the Mozilla Public License Version
8 * 1.1 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://www.mozilla.org/MPL/
12 * Software distributed under the License is distributed on an "AS IS" basis,
13 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14 * for the specific language governing rights and limitations under the
15 * License.
17 * The Original Code is mozilla.org code.
19 * The Initial Developer of the Original Code is
20 * Mozilla Foundation.
21 * Portions created by the Initial Developer are Copyright (C) 2007
22 * the Initial Developer. All Rights Reserved.
24 * Contributor(s):
25 * Alexander Surkov <surkov.alexander@gmail.com> (original author)
27 * Alternatively, the contents of this file may be used under the terms of
28 * either the GNU General Public License Version 2 or later (the "GPL"), or
29 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
39 * ***** END LICENSE BLOCK ***** */
41 #include "CAccessibleComponent.h"
43 #include "AccessibleComponent_i.c"
45 #include "nsIAccessNode.h"
46 #include "nsIAccessible.h"
47 #include "nsIAccessibleStates.h"
48 #include "nsAccessNodeWrap.h"
50 #include "nsCOMPtr.h"
51 #include "nsString.h"
53 #include "nsIDOMCSSPrimitiveValue.h"
54 #include "nsIDOMNSRGBAColor.h"
56 enum {
57 IA2AlphaShift = 24,
58 IA2RedShift = 16,
59 IA2GreenShift = 8,
60 IA2BlueShift = 0
63 // IUnknown
65 STDMETHODIMP
66 CAccessibleComponent::QueryInterface(REFIID iid, void** ppv)
68 *ppv = NULL;
70 if (IID_IAccessibleComponent == iid) {
71 *ppv = static_cast<IAccessibleComponent*>(this);
72 (reinterpret_cast<IUnknown*>(*ppv))->AddRef();
73 return S_OK;
76 return E_NOINTERFACE;
79 // IAccessibleComponent
81 STDMETHODIMP
82 CAccessibleComponent::get_locationInParent(long *aX, long *aY)
84 __try {
85 *aX = 0;
86 *aY = 0;
88 nsCOMPtr<nsIAccessible> acc(do_QueryInterface(this));
89 if (!acc)
90 return E_FAIL;
92 // If the object is not on any screen the returned position is (0,0).
93 PRUint32 states = 0;
94 nsresult rv = acc->GetFinalState(&states, nsnull);
95 if (NS_FAILED(rv))
96 return GetHRESULT(rv);
98 if (states & nsIAccessibleStates::STATE_INVISIBLE)
99 return S_OK;
101 PRInt32 x = 0, y = 0, width = 0, height = 0;
102 rv = acc->GetBounds(&x, &y, &width, &height);
103 if (NS_FAILED(rv))
104 return GetHRESULT(rv);
106 nsCOMPtr<nsIAccessible> parentAcc;
107 rv = acc->GetParent(getter_AddRefs(parentAcc));
108 if (NS_FAILED(rv))
109 return GetHRESULT(rv);
111 // The coordinates of the returned position are relative to this object's
112 // parent or relative to the screen on which this object is rendered if it
113 // has no parent.
114 if (!parentAcc) {
115 *aX = x;
116 *aY = y;
117 return S_OK;
120 // The coordinates of the bounding box are given relative to the parent's
121 // coordinate system.
122 PRInt32 parentx = 0, parenty = 0;
123 rv = acc->GetBounds(&parentx, &parenty, &width, &height);
124 if (NS_FAILED(rv))
125 return GetHRESULT(rv);
127 *aX = x - parentx;
128 *aY = y - parenty;
129 return S_OK;
131 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
132 return E_FAIL;
135 STDMETHODIMP
136 CAccessibleComponent::get_foreground(IA2Color *aForeground)
138 __try {
139 return GetARGBValueFromCSSProperty(NS_LITERAL_STRING("color"), aForeground);
140 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
142 return E_FAIL;
145 STDMETHODIMP
146 CAccessibleComponent::get_background(IA2Color *aBackground)
148 __try {
149 return GetARGBValueFromCSSProperty(NS_LITERAL_STRING("background-color"),
150 aBackground);
151 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
153 return E_FAIL;
156 HRESULT
157 CAccessibleComponent::GetARGBValueFromCSSProperty(const nsAString& aPropName,
158 IA2Color *aColorValue)
160 __try {
161 *aColorValue = 0;
163 nsCOMPtr<nsIAccessNode> acc(do_QueryInterface(this));
164 if (!acc)
165 return E_FAIL;
167 nsCOMPtr<nsIDOMCSSPrimitiveValue> cssValue;
168 nsresult rv = acc->GetComputedStyleCSSValue(EmptyString(), aPropName,
169 getter_AddRefs(cssValue));
170 if (NS_FAILED(rv) || !cssValue)
171 return GetHRESULT(rv);
173 nsCOMPtr<nsIDOMRGBColor> rgbColor;
174 rv = cssValue->GetRGBColorValue(getter_AddRefs(rgbColor));
175 if (NS_FAILED(rv) || !rgbColor)
176 return GetHRESULT(rv);
178 nsCOMPtr<nsIDOMNSRGBAColor> rgbaColor(do_QueryInterface(rgbColor));
179 if (!rgbaColor)
180 return GetHRESULT(rv);
182 // get alpha
183 nsCOMPtr<nsIDOMCSSPrimitiveValue> alphaValue;
184 rv = rgbaColor->GetAlpha(getter_AddRefs(alphaValue));
185 if (NS_FAILED(rv) || !alphaValue)
186 return GetHRESULT(rv);
188 float alpha = 0.0;
189 rv = alphaValue->GetFloatValue(nsIDOMCSSPrimitiveValue::CSS_NUMBER, &alpha);
190 if (NS_FAILED(rv))
191 return GetHRESULT(rv);
193 // get red
194 nsCOMPtr<nsIDOMCSSPrimitiveValue> redValue;
195 rv = rgbaColor->GetRed(getter_AddRefs(redValue));
196 if (NS_FAILED(rv) || !redValue)
197 return GetHRESULT(rv);
199 float red = 0.0;
200 rv = redValue->GetFloatValue(nsIDOMCSSPrimitiveValue::CSS_NUMBER, &red);
201 if (NS_FAILED(rv))
202 return GetHRESULT(rv);
204 // get green
205 nsCOMPtr<nsIDOMCSSPrimitiveValue> greenValue;
206 rv = rgbaColor->GetGreen(getter_AddRefs(greenValue));
207 if (NS_FAILED(rv) || !greenValue)
208 return GetHRESULT(rv);
210 float green = 0.0;
211 rv = greenValue->GetFloatValue(nsIDOMCSSPrimitiveValue::CSS_NUMBER, &green);
212 if (NS_FAILED(rv))
213 return GetHRESULT(rv);
215 // get blue
216 nsCOMPtr<nsIDOMCSSPrimitiveValue> blueValue;
217 rv = rgbaColor->GetBlue(getter_AddRefs(blueValue));
218 if (NS_FAILED(rv) || !blueValue)
219 return GetHRESULT(rv);
221 float blue = 0.0;
222 rv = blueValue->GetFloatValue(nsIDOMCSSPrimitiveValue::CSS_NUMBER, &blue);
223 if (NS_FAILED(rv))
224 return GetHRESULT(rv);
226 // compose ARGB value
227 *aColorValue = (((IA2Color) blue) << IA2BlueShift) |
228 (((IA2Color) green) << IA2GreenShift) |
229 (((IA2Color) red) << IA2RedShift) |
230 (((IA2Color) (alpha * 0xff)) << IA2AlphaShift);
231 return S_OK;
233 } __except(nsAccessNodeWrap::FilterA11yExceptions(::GetExceptionCode(), GetExceptionInformation())) { }
234 return E_FAIL;