1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=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 #ifndef nsIFormControl_h___
7 #define nsIFormControl_h___
8
9 #include "mozilla/EventForwards.h"
10 #include "nsISupports.h"
11
12 class nsIDOMHTMLFormElement;
13 class nsPresState;
14
15 namespace mozilla {
16 namespace dom {
17 class Element;
18 class HTMLFieldSetElement;
19 class HTMLFormSubmission;
20 } // namespace dom
21 } // namespace mozilla
22
23 enum FormControlsTypes {
24 NS_FORM_FIELDSET = 1,
25 NS_FORM_OUTPUT,
26 NS_FORM_SELECT,
27 NS_FORM_TEXTAREA,
28 NS_FORM_OBJECT,
29 eFormControlsWithoutSubTypesMax,
30 // After this, all types will have sub-types which introduce new enum lists.
31 // eFormControlsWithoutSubTypesMax let us know if the previous types values
32 // are not overlapping with sub-types/masks.
33
34 // Elements with different types, the value is used as a mask.
35 // When changing the order, adding or removing elements, be sure to update
36 // the static_assert checks accordingly.
37 NS_FORM_BUTTON_ELEMENT = 0x40, // 0b01000000
38 NS_FORM_INPUT_ELEMENT = 0x80 // 0b10000000
39 };
40
41 enum ButtonElementTypes : uint8_t {
42 NS_FORM_BUTTON_BUTTON = NS_FORM_BUTTON_ELEMENT + 1,
43 NS_FORM_BUTTON_RESET,
44 NS_FORM_BUTTON_SUBMIT,
45 eButtonElementTypesMax
46 };
47
48 enum InputElementTypes : uint8_t {
49 NS_FORM_INPUT_BUTTON = NS_FORM_INPUT_ELEMENT + 1,
50 NS_FORM_INPUT_CHECKBOX,
51 NS_FORM_INPUT_COLOR,
52 NS_FORM_INPUT_DATE,
53 NS_FORM_INPUT_EMAIL,
54 NS_FORM_INPUT_FILE,
55 NS_FORM_INPUT_HIDDEN,
56 NS_FORM_INPUT_RESET,
57 NS_FORM_INPUT_IMAGE,
58 NS_FORM_INPUT_MONTH,
59 NS_FORM_INPUT_NUMBER,
60 NS_FORM_INPUT_PASSWORD,
61 NS_FORM_INPUT_RADIO,
62 NS_FORM_INPUT_SEARCH,
63 NS_FORM_INPUT_SUBMIT,
64 NS_FORM_INPUT_TEL,
65 NS_FORM_INPUT_TEXT,
66 NS_FORM_INPUT_TIME,
67 NS_FORM_INPUT_URL,
68 NS_FORM_INPUT_RANGE,
69 NS_FORM_INPUT_WEEK,
70 NS_FORM_INPUT_DATETIME_LOCAL,
71 eInputElementTypesMax
72 };
73
74 static_assert(static_cast<uint32_t>(eFormControlsWithoutSubTypesMax) <
75 static_cast<uint32_t>(NS_FORM_BUTTON_ELEMENT),
76 "Too many FormControlsTypes without sub-types");
77 static_assert(static_cast<uint32_t>(eButtonElementTypesMax) <
78 static_cast<uint32_t>(NS_FORM_INPUT_ELEMENT),
79 "Too many ButtonElementTypes");
80 static_assert(static_cast<uint32_t>(eInputElementTypesMax) < 1 << 8,
81 "Too many form control types");
82
83 #define NS_IFORMCONTROL_IID \
84 { \
85 0x4b89980c, 0x4dcd, 0x428f, { \
86 0xb7, 0xad, 0x43, 0x5b, 0x93, 0x29, 0x79, 0xec \
87 } \
88 }
89
90 /**
91 * Interface which all form controls (e.g. buttons, checkboxes, text,
92 * radio buttons, select, etc) implement in addition to their dom specific
93 * interface.
94 */
95 class nsIFormControl : public nsISupports {
96 public:
nsIFormControl(uint8_t aType)97 nsIFormControl(uint8_t aType) : mType(aType) {}
98
99 NS_DECLARE_STATIC_IID_ACCESSOR(NS_IFORMCONTROL_IID)
100
101 /**
102 * Get the fieldset for this form control.
103 * @return the fieldset
104 */
105 virtual mozilla::dom::HTMLFieldSetElement* GetFieldSet() = 0;
106
107 /**
108 * Get the form for this form control.
109 * @return the form
110 */
111 virtual mozilla::dom::Element* GetFormElement() = 0;
112
113 /**
114 * Set the form for this form control.
115 * @param aForm the form. This must not be null.
116 *
117 * @note that when setting the form the control is not added to the
118 * form. It adds itself when it gets bound to the tree thereafter,
119 * so that it can be properly sorted with the other controls in the
120 * form.
121 */
122 virtual void SetForm(nsIDOMHTMLFormElement* aForm) = 0;
123
124 /**
125 * Tell the control to forget about its form.
126 *
127 * @param aRemoveFromForm set false if you do not want this element removed
128 * from the form. (Used by nsFormControlList::Clear())
129 * @param aUnbindOrDelete set true if the element is being deleted or unbound
130 * from tree.
131 */
132 virtual void ClearForm(bool aRemoveFromForm, bool aUnbindOrDelete) = 0;
133
134 /**
135 * Get the type of this control as an int (see NS_FORM_* above)
136 * @return the type of this control
137 */
ControlType()138 uint32_t ControlType() const { return mType; }
139
140 /**
141 * Reset this form control (as it should be when the user clicks the Reset
142 * button)
143 */
144 NS_IMETHOD Reset() = 0;
145
146 /**
147 * Tells the form control to submit its names and values to the form
148 * submission object
149 * @param aFormSubmission the form submission to notify of names/values/files
150 * to submit
151 */
152 NS_IMETHOD
153 SubmitNamesValues(mozilla::dom::HTMLFormSubmission* aFormSubmission) = 0;
154
155 /**
156 * Save to presentation state. The form control will determine whether it
157 * has anything to save and if so, create an entry in the layout history for
158 * its pres context.
159 */
160 NS_IMETHOD SaveState() = 0;
161
162 /**
163 * Restore from presentation state. You pass in the presentation state for
164 * this form control (generated with GenerateStateKey() + "-C") and the form
165 * control will grab its state from there.
166 *
167 * @param aState the pres state to use to restore the control
168 * @return true if the form control was a checkbox and its
169 * checked state was restored, false otherwise.
170 */
171 virtual bool RestoreState(nsPresState* aState) = 0;
172
173 virtual bool AllowDrop() = 0;
174
175 /**
176 * Returns whether this is a control which submits the form when activated by
177 * the user.
178 * @return whether this is a submit control.
179 */
180 inline bool IsSubmitControl() const;
181
182 /**
183 * Returns whether this is a text control.
184 * @param aExcludePassword to have NS_FORM_INPUT_PASSWORD returning false.
185 * @return whether this is a text control.
186 */
187 inline bool IsTextControl(bool aExcludePassword) const;
188
189 /**
190 * Returns true if this is a text control or a number control.
191 * @param aExcludePassword to have NS_FORM_INPUT_PASSWORD returning false.
192 * @return true if this is a text control or a number control.
193 */
194 inline bool IsTextOrNumberControl(bool aExcludePassword) const;
195
196 /**
197 * Returns whether this is a single line text control.
198 * @param aExcludePassword to have NS_FORM_INPUT_PASSWORD returning false.
199 * @return whether this is a single line text control.
200 */
201 inline bool IsSingleLineTextControl(bool aExcludePassword) const;
202
203 /**
204 * Returns true if this is a single line text control or a number control.
205 * @param aExcludePassword to have NS_FORM_INPUT_PASSWORD returning false.
206 * @return true if this is a single line text control or a number control.
207 */
208 inline bool IsSingleLineTextOrNumberControl(bool aExcludePassword) const;
209
210 /**
211 * Returns whether this is a submittable form control.
212 * @return whether this is a submittable form control.
213 */
214 inline bool IsSubmittableControl() const;
215
216 /**
217 * Returns whether this form control can have draggable children.
218 * @return whether this form control can have draggable children.
219 */
220 inline bool AllowDraggableChildren() const;
221
IsDisabledForEvents(mozilla::EventMessage aMessage)222 virtual bool IsDisabledForEvents(mozilla::EventMessage aMessage) {
223 return false;
224 }
225
226 protected:
227 /**
228 * Returns whether mType corresponds to a single line text control type.
229 * @param aExcludePassword to have NS_FORM_INPUT_PASSWORD ignored.
230 * @param aType the type to be tested.
231 * @return whether mType corresponds to a single line text control type.
232 */
233 inline static bool IsSingleLineTextControl(bool aExcludePassword,
234 uint32_t aType);
235
236 /**
237 * Returns whether this is a auto-focusable form control.
238 * @return whether this is a auto-focusable form control.
239 */
240 inline bool IsAutofocusable() const;
241
242 uint8_t mType;
243 };
244
IsSubmitControl()245 bool nsIFormControl::IsSubmitControl() const {
246 uint32_t type = ControlType();
247 return type == NS_FORM_INPUT_SUBMIT || type == NS_FORM_INPUT_IMAGE ||
248 type == NS_FORM_BUTTON_SUBMIT;
249 }
250
IsTextControl(bool aExcludePassword)251 bool nsIFormControl::IsTextControl(bool aExcludePassword) const {
252 uint32_t type = ControlType();
253 return type == NS_FORM_TEXTAREA ||
254 IsSingleLineTextControl(aExcludePassword, type);
255 }
256
IsTextOrNumberControl(bool aExcludePassword)257 bool nsIFormControl::IsTextOrNumberControl(bool aExcludePassword) const {
258 return IsTextControl(aExcludePassword) ||
259 ControlType() == NS_FORM_INPUT_NUMBER;
260 }
261
IsSingleLineTextControl(bool aExcludePassword)262 bool nsIFormControl::IsSingleLineTextControl(bool aExcludePassword) const {
263 return IsSingleLineTextControl(aExcludePassword, ControlType());
264 }
265
IsSingleLineTextOrNumberControl(bool aExcludePassword)266 bool nsIFormControl::IsSingleLineTextOrNumberControl(
267 bool aExcludePassword) const {
268 return IsSingleLineTextControl(aExcludePassword) ||
269 ControlType() == NS_FORM_INPUT_NUMBER;
270 }
271
272 /*static*/
IsSingleLineTextControl(bool aExcludePassword,uint32_t aType)273 bool nsIFormControl::IsSingleLineTextControl(bool aExcludePassword,
274 uint32_t aType) {
275 return aType == NS_FORM_INPUT_TEXT || aType == NS_FORM_INPUT_EMAIL ||
276 aType == NS_FORM_INPUT_SEARCH || aType == NS_FORM_INPUT_TEL ||
277 aType == NS_FORM_INPUT_URL ||
278 // TODO: those are temporary until bug 773205 is fixed.
279 aType == NS_FORM_INPUT_MONTH || aType == NS_FORM_INPUT_WEEK ||
280 aType == NS_FORM_INPUT_DATETIME_LOCAL ||
281 (!aExcludePassword && aType == NS_FORM_INPUT_PASSWORD);
282 }
283
IsSubmittableControl()284 bool nsIFormControl::IsSubmittableControl() const {
285 // TODO: keygen should be in that list, see bug 101019.
286 uint32_t type = ControlType();
287 return type == NS_FORM_OBJECT || type == NS_FORM_TEXTAREA ||
288 type == NS_FORM_SELECT ||
289 // type == NS_FORM_KEYGEN ||
290 type & NS_FORM_BUTTON_ELEMENT || type & NS_FORM_INPUT_ELEMENT;
291 }
292
AllowDraggableChildren()293 bool nsIFormControl::AllowDraggableChildren() const {
294 uint32_t type = ControlType();
295 return type == NS_FORM_OBJECT || type == NS_FORM_FIELDSET ||
296 type == NS_FORM_OUTPUT;
297 }
298
IsAutofocusable()299 bool nsIFormControl::IsAutofocusable() const {
300 uint32_t type = ControlType();
301 return type & NS_FORM_INPUT_ELEMENT || type & NS_FORM_BUTTON_ELEMENT ||
302 type == NS_FORM_TEXTAREA || type == NS_FORM_SELECT;
303 }
304
305 NS_DEFINE_STATIC_IID_ACCESSOR(nsIFormControl, NS_IFORMCONTROL_IID)
306
307 #endif /* nsIFormControl_h___ */
308