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 SVG Project code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Jonathan Watt.
19 : * Portions created by the Initial Developer are Copyright (C) 2004
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Jonathan Watt <jonathan.watt@strath.ac.uk> (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 MOZILLA_SVGANIMATEDPRESERVEASPECTRATIO_H__
40 : #define MOZILLA_SVGANIMATEDPRESERVEASPECTRATIO_H__
41 :
42 : #include "nsAutoPtr.h"
43 : #include "nsCycleCollectionParticipant.h"
44 : #include "nsDOMError.h"
45 : #include "nsError.h"
46 : #include "nsIDOMSVGAnimPresAspRatio.h"
47 : #include "nsIDOMSVGPresAspectRatio.h"
48 : #include "nsISMILAttr.h"
49 : #include "nsSVGElement.h"
50 :
51 : class nsISMILAnimationElement;
52 : class nsSMILValue;
53 :
54 : namespace mozilla {
55 :
56 : class SVGAnimatedPreserveAspectRatio;
57 :
58 : class SVGPreserveAspectRatio
59 : {
60 : friend class SVGAnimatedPreserveAspectRatio;
61 :
62 : public:
63 0 : SVGPreserveAspectRatio()
64 : : mAlign(0)
65 : , mMeetOrSlice(0)
66 0 : , mDefer(false)
67 0 : {};
68 :
69 0 : nsresult SetAlign(PRUint16 aAlign) {
70 0 : if (aAlign < nsIDOMSVGPreserveAspectRatio::SVG_PRESERVEASPECTRATIO_NONE ||
71 : aAlign > nsIDOMSVGPreserveAspectRatio::SVG_PRESERVEASPECTRATIO_XMAXYMAX)
72 0 : return NS_ERROR_FAILURE;
73 0 : mAlign = static_cast<PRUint8>(aAlign);
74 0 : return NS_OK;
75 : };
76 :
77 0 : PRUint16 GetAlign() const {
78 0 : return mAlign;
79 : };
80 :
81 0 : nsresult SetMeetOrSlice(PRUint16 aMeetOrSlice) {
82 0 : if (aMeetOrSlice < nsIDOMSVGPreserveAspectRatio::SVG_MEETORSLICE_MEET ||
83 : aMeetOrSlice > nsIDOMSVGPreserveAspectRatio::SVG_MEETORSLICE_SLICE)
84 0 : return NS_ERROR_FAILURE;
85 0 : mMeetOrSlice = static_cast<PRUint8>(aMeetOrSlice);
86 0 : return NS_OK;
87 : };
88 :
89 0 : PRUint16 GetMeetOrSlice() const {
90 0 : return mMeetOrSlice;
91 : };
92 :
93 0 : void SetDefer(bool aDefer) {
94 0 : mDefer = aDefer;
95 0 : };
96 :
97 0 : bool GetDefer() const {
98 0 : return mDefer;
99 : };
100 :
101 : private:
102 : PRUint8 mAlign;
103 : PRUint8 mMeetOrSlice;
104 : bool mDefer;
105 : };
106 :
107 : class SVGAnimatedPreserveAspectRatio
108 0 : {
109 : public:
110 0 : void Init() {
111 0 : mBaseVal.mAlign = nsIDOMSVGPreserveAspectRatio::SVG_PRESERVEASPECTRATIO_XMIDYMID;
112 0 : mBaseVal.mMeetOrSlice = nsIDOMSVGPreserveAspectRatio::SVG_MEETORSLICE_MEET;
113 0 : mBaseVal.mDefer = false;
114 0 : mAnimVal = mBaseVal;
115 0 : mIsAnimated = false;
116 0 : mIsBaseSet = false;
117 0 : }
118 :
119 : nsresult SetBaseValueString(const nsAString& aValue,
120 : nsSVGElement *aSVGElement);
121 : void GetBaseValueString(nsAString& aValue) const;
122 :
123 : nsresult SetBaseAlign(PRUint16 aAlign, nsSVGElement *aSVGElement);
124 : nsresult SetBaseMeetOrSlice(PRUint16 aMeetOrSlice, nsSVGElement *aSVGElement);
125 : void SetAnimValue(PRUint64 aPackedValue, nsSVGElement *aSVGElement);
126 :
127 0 : const SVGPreserveAspectRatio &GetBaseValue() const
128 0 : { return mBaseVal; }
129 0 : const SVGPreserveAspectRatio &GetAnimValue() const
130 0 : { return mAnimVal; }
131 0 : bool IsAnimated() const
132 0 : { return mIsAnimated; }
133 0 : bool IsExplicitlySet() const
134 0 : { return mIsAnimated || mIsBaseSet; }
135 :
136 : nsresult ToDOMAnimatedPreserveAspectRatio(
137 : nsIDOMSVGAnimatedPreserveAspectRatio **aResult,
138 : nsSVGElement* aSVGElement);
139 : // Returns a new nsISMILAttr object that the caller must delete
140 : nsISMILAttr* ToSMILAttr(nsSVGElement* aSVGElement);
141 :
142 : private:
143 :
144 : SVGPreserveAspectRatio mAnimVal;
145 : SVGPreserveAspectRatio mBaseVal;
146 : bool mIsAnimated;
147 : bool mIsBaseSet;
148 :
149 : nsresult ToDOMBaseVal(nsIDOMSVGPreserveAspectRatio **aResult,
150 : nsSVGElement* aSVGElement);
151 : nsresult ToDOMAnimVal(nsIDOMSVGPreserveAspectRatio **aResult,
152 : nsSVGElement* aSVGElement);
153 :
154 : public:
155 : struct DOMBaseVal : public nsIDOMSVGPreserveAspectRatio
156 0 : {
157 0 : NS_DECL_CYCLE_COLLECTING_ISUPPORTS
158 1464 : NS_DECL_CYCLE_COLLECTION_CLASS(DOMBaseVal)
159 :
160 0 : DOMBaseVal(SVGAnimatedPreserveAspectRatio* aVal, nsSVGElement *aSVGElement)
161 0 : : mVal(aVal), mSVGElement(aSVGElement) {}
162 :
163 : SVGAnimatedPreserveAspectRatio* mVal; // kept alive because it belongs to mSVGElement
164 : nsRefPtr<nsSVGElement> mSVGElement;
165 :
166 0 : NS_IMETHOD GetAlign(PRUint16* aAlign)
167 0 : { *aAlign = mVal->GetBaseValue().GetAlign(); return NS_OK; }
168 0 : NS_IMETHOD SetAlign(PRUint16 aAlign)
169 0 : { return mVal->SetBaseAlign(aAlign, mSVGElement); }
170 :
171 0 : NS_IMETHOD GetMeetOrSlice(PRUint16* aMeetOrSlice)
172 0 : { *aMeetOrSlice = mVal->GetBaseValue().GetMeetOrSlice(); return NS_OK; }
173 0 : NS_IMETHOD SetMeetOrSlice(PRUint16 aMeetOrSlice)
174 0 : { return mVal->SetBaseMeetOrSlice(aMeetOrSlice, mSVGElement); }
175 : };
176 :
177 : struct DOMAnimVal : public nsIDOMSVGPreserveAspectRatio
178 0 : {
179 0 : NS_DECL_CYCLE_COLLECTING_ISUPPORTS
180 1464 : NS_DECL_CYCLE_COLLECTION_CLASS(DOMAnimVal)
181 :
182 0 : DOMAnimVal(SVGAnimatedPreserveAspectRatio* aVal, nsSVGElement *aSVGElement)
183 0 : : mVal(aVal), mSVGElement(aSVGElement) {}
184 :
185 : SVGAnimatedPreserveAspectRatio* mVal; // kept alive because it belongs to mSVGElement
186 : nsRefPtr<nsSVGElement> mSVGElement;
187 :
188 : // Script may have modified animation parameters or timeline -- DOM getters
189 : // need to flush any resample requests to reflect these modifications.
190 0 : NS_IMETHOD GetAlign(PRUint16* aAlign)
191 : {
192 0 : mSVGElement->FlushAnimations();
193 0 : *aAlign = mVal->GetAnimValue().GetAlign();
194 0 : return NS_OK;
195 : }
196 0 : NS_IMETHOD SetAlign(PRUint16 aAlign)
197 0 : { return NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR; }
198 :
199 0 : NS_IMETHOD GetMeetOrSlice(PRUint16* aMeetOrSlice)
200 : {
201 0 : mSVGElement->FlushAnimations();
202 0 : *aMeetOrSlice = mVal->GetAnimValue().GetMeetOrSlice();
203 0 : return NS_OK;
204 : }
205 0 : NS_IMETHOD SetMeetOrSlice(PRUint16 aValue)
206 0 : { return NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR; }
207 : };
208 :
209 : struct DOMAnimPAspectRatio : public nsIDOMSVGAnimatedPreserveAspectRatio
210 0 : {
211 0 : NS_DECL_CYCLE_COLLECTING_ISUPPORTS
212 1464 : NS_DECL_CYCLE_COLLECTION_CLASS(DOMAnimPAspectRatio)
213 :
214 0 : DOMAnimPAspectRatio(SVGAnimatedPreserveAspectRatio* aVal,
215 : nsSVGElement *aSVGElement)
216 0 : : mVal(aVal), mSVGElement(aSVGElement) {}
217 :
218 : // kept alive because it belongs to content:
219 : SVGAnimatedPreserveAspectRatio* mVal;
220 :
221 : nsRefPtr<nsSVGElement> mSVGElement;
222 :
223 0 : NS_IMETHOD GetBaseVal(nsIDOMSVGPreserveAspectRatio **aBaseVal)
224 0 : { return mVal->ToDOMBaseVal(aBaseVal, mSVGElement); }
225 :
226 0 : NS_IMETHOD GetAnimVal(nsIDOMSVGPreserveAspectRatio **aAnimVal)
227 0 : { return mVal->ToDOMAnimVal(aAnimVal, mSVGElement); }
228 : };
229 :
230 : struct SMILPreserveAspectRatio : public nsISMILAttr
231 0 : {
232 : public:
233 0 : SMILPreserveAspectRatio(SVGAnimatedPreserveAspectRatio* aVal,
234 : nsSVGElement* aSVGElement)
235 0 : : mVal(aVal), mSVGElement(aSVGElement) {}
236 :
237 : // These will stay alive because a nsISMILAttr only lives as long
238 : // as the Compositing step, and DOM elements don't get a chance to
239 : // die during that.
240 : SVGAnimatedPreserveAspectRatio* mVal;
241 : nsSVGElement* mSVGElement;
242 :
243 : // nsISMILAttr methods
244 : virtual nsresult ValueFromString(const nsAString& aStr,
245 : const nsISMILAnimationElement* aSrcElement,
246 : nsSMILValue& aValue,
247 : bool& aPreventCachingOfSandwich) const;
248 : virtual nsSMILValue GetBaseValue() const;
249 : virtual void ClearAnimValue();
250 : virtual nsresult SetAnimValue(const nsSMILValue& aValue);
251 : };
252 : };
253 :
254 : } // namespace mozilla
255 :
256 : #endif // MOZILLA_SVGANIMATEDPRESERVEASPECTRATIO_H__
|