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 Mozilla Foundation
18 : * Portions created by the Initial Developer are Copyright (C) 2010
19 : * the Initial Developer. All Rights Reserved.
20 : *
21 : * Contributor(s):
22 : * Mounir Lamouri <mounir.lamouri@mozilla.com> (original author)
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either of the GNU General Public License Version 2 or later (the "GPL"),
26 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #include "nsIDOMHTMLProgressElement.h"
39 : #include "nsGenericHTMLElement.h"
40 : #include "nsAttrValue.h"
41 : #include "nsEventStateManager.h"
42 :
43 :
44 : class nsHTMLProgressElement : public nsGenericHTMLFormElement,
45 : public nsIDOMHTMLProgressElement
46 : {
47 : public:
48 : nsHTMLProgressElement(already_AddRefed<nsINodeInfo> aNodeInfo);
49 : virtual ~nsHTMLProgressElement();
50 :
51 : // nsISupports
52 : NS_DECL_ISUPPORTS_INHERITED
53 :
54 : // nsIDOMNode
55 0 : NS_FORWARD_NSIDOMNODE(nsGenericHTMLFormElement::)
56 :
57 : // nsIDOMElement
58 0 : NS_FORWARD_NSIDOMELEMENT(nsGenericHTMLFormElement::)
59 :
60 : // nsIDOMHTMLElement
61 0 : NS_FORWARD_NSIDOMHTMLELEMENT(nsGenericHTMLFormElement::)
62 :
63 : // nsIDOMHTMLProgressElement
64 : NS_DECL_NSIDOMHTMLPROGRESSELEMENT
65 :
66 : // nsIFormControl
67 0 : NS_IMETHOD_(PRUint32) GetType() const { return NS_FORM_PROGRESS; }
68 : NS_IMETHOD Reset();
69 : NS_IMETHOD SubmitNamesValues(nsFormSubmission* aFormSubmission);
70 :
71 : nsEventStates IntrinsicState() const;
72 :
73 : nsresult Clone(nsINodeInfo* aNodeInfo, nsINode** aResult) const;
74 :
75 : bool ParseAttribute(PRInt32 aNamespaceID, nsIAtom* aAttribute,
76 : const nsAString& aValue, nsAttrValue& aResult);
77 :
78 : virtual nsXPCClassInfo* GetClassInfo();
79 :
80 : protected:
81 : /**
82 : * Returns whethem the progress element is in the indeterminate state.
83 : * A progress element is in the indeterminate state if its value is ommited
84 : * or is not a floating point number..
85 : *
86 : * @return whether the progress element is in the indeterminate state.
87 : */
88 : bool IsIndeterminate() const;
89 :
90 : static const double kIndeterminatePosition;
91 : static const double kDefaultValue;
92 : static const double kDefaultMax;
93 : };
94 :
95 : const double nsHTMLProgressElement::kIndeterminatePosition = -1.0;
96 : const double nsHTMLProgressElement::kDefaultValue = 0.0;
97 : const double nsHTMLProgressElement::kDefaultMax = 1.0;
98 :
99 0 : NS_IMPL_NS_NEW_HTML_ELEMENT(Progress)
100 :
101 :
102 0 : nsHTMLProgressElement::nsHTMLProgressElement(already_AddRefed<nsINodeInfo> aNodeInfo)
103 0 : : nsGenericHTMLFormElement(aNodeInfo)
104 : {
105 : // We start out indeterminate
106 0 : AddStatesSilently(NS_EVENT_STATE_INDETERMINATE);
107 0 : }
108 :
109 0 : nsHTMLProgressElement::~nsHTMLProgressElement()
110 : {
111 0 : }
112 :
113 0 : NS_IMPL_ADDREF_INHERITED(nsHTMLProgressElement, nsGenericElement)
114 0 : NS_IMPL_RELEASE_INHERITED(nsHTMLProgressElement, nsGenericElement)
115 :
116 0 : DOMCI_NODE_DATA(HTMLProgressElement, nsHTMLProgressElement)
117 :
118 0 : NS_INTERFACE_TABLE_HEAD(nsHTMLProgressElement)
119 0 : NS_HTML_CONTENT_INTERFACE_TABLE1(nsHTMLProgressElement,
120 : nsIDOMHTMLProgressElement)
121 0 : NS_HTML_CONTENT_INTERFACE_TABLE_TO_MAP_SEGUE(nsHTMLProgressElement,
122 : nsGenericHTMLFormElement)
123 0 : NS_HTML_CONTENT_INTERFACE_TABLE_TAIL_CLASSINFO(HTMLProgressElement)
124 :
125 0 : NS_IMPL_ELEMENT_CLONE(nsHTMLProgressElement)
126 :
127 :
128 : NS_IMETHODIMP
129 0 : nsHTMLProgressElement::Reset()
130 : {
131 : // The progress element is not resettable.
132 0 : return NS_OK;
133 : }
134 :
135 : NS_IMETHODIMP
136 0 : nsHTMLProgressElement::SubmitNamesValues(nsFormSubmission* aFormSubmission)
137 : {
138 : // The progress element is not submittable.
139 0 : return NS_OK;
140 : }
141 :
142 : nsEventStates
143 0 : nsHTMLProgressElement::IntrinsicState() const
144 : {
145 0 : nsEventStates state = nsGenericHTMLFormElement::IntrinsicState();
146 :
147 0 : if (IsIndeterminate()) {
148 0 : state |= NS_EVENT_STATE_INDETERMINATE;
149 : }
150 :
151 : return state;
152 : }
153 :
154 : bool
155 0 : nsHTMLProgressElement::ParseAttribute(PRInt32 aNamespaceID, nsIAtom* aAttribute,
156 : const nsAString& aValue, nsAttrValue& aResult)
157 : {
158 0 : if (aNamespaceID == kNameSpaceID_None) {
159 0 : if (aAttribute == nsGkAtoms::value || aAttribute == nsGkAtoms::max) {
160 0 : return aResult.ParseDoubleValue(aValue);
161 : }
162 : }
163 :
164 : return nsGenericHTMLFormElement::ParseAttribute(aNamespaceID, aAttribute,
165 0 : aValue, aResult);
166 : }
167 :
168 : NS_IMETHODIMP
169 0 : nsHTMLProgressElement::GetForm(nsIDOMHTMLFormElement** aForm)
170 : {
171 0 : return nsGenericHTMLFormElement::GetForm(aForm);
172 : }
173 :
174 : NS_IMETHODIMP
175 0 : nsHTMLProgressElement::GetValue(double* aValue)
176 : {
177 0 : const nsAttrValue* attrValue = mAttrsAndChildren.GetAttr(nsGkAtoms::value);
178 0 : if (!attrValue || attrValue->Type() != nsAttrValue::eDoubleValue ||
179 0 : attrValue->GetDoubleValue() < 0.0) {
180 0 : *aValue = kDefaultValue;
181 0 : return NS_OK;
182 : }
183 :
184 0 : *aValue = attrValue->GetDoubleValue();
185 :
186 : double max;
187 0 : GetMax(&max);
188 :
189 0 : *aValue = NS_MIN(*aValue, max);
190 :
191 0 : return NS_OK;
192 : }
193 :
194 : NS_IMETHODIMP
195 0 : nsHTMLProgressElement::SetValue(double aValue)
196 : {
197 0 : return SetDoubleAttr(nsGkAtoms::value, aValue);
198 : }
199 :
200 : NS_IMETHODIMP
201 0 : nsHTMLProgressElement::GetMax(double* aValue)
202 : {
203 0 : const nsAttrValue* attrMax = mAttrsAndChildren.GetAttr(nsGkAtoms::max);
204 0 : if (attrMax && attrMax->Type() == nsAttrValue::eDoubleValue &&
205 0 : attrMax->GetDoubleValue() > 0.0) {
206 0 : *aValue = attrMax->GetDoubleValue();
207 : } else {
208 0 : *aValue = kDefaultMax;
209 : }
210 :
211 0 : return NS_OK;
212 : }
213 :
214 : NS_IMETHODIMP
215 0 : nsHTMLProgressElement::SetMax(double aValue)
216 : {
217 0 : return SetDoubleAttr(nsGkAtoms::max, aValue);
218 : }
219 :
220 : NS_IMETHODIMP
221 0 : nsHTMLProgressElement::GetPosition(double* aPosition)
222 : {
223 0 : if (IsIndeterminate()) {
224 0 : *aPosition = kIndeterminatePosition;
225 0 : return NS_OK;
226 : }
227 :
228 : double value;
229 : double max;
230 0 : GetValue(&value);
231 0 : GetMax(&max);
232 :
233 0 : *aPosition = value / max;
234 :
235 0 : return NS_OK;
236 : }
237 :
238 : bool
239 0 : nsHTMLProgressElement::IsIndeterminate() const
240 : {
241 0 : const nsAttrValue* attrValue = mAttrsAndChildren.GetAttr(nsGkAtoms::value);
242 0 : return !attrValue || attrValue->Type() != nsAttrValue::eDoubleValue;
243 : }
244 :
|