Line data Source code
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 : { 0x4b89980c, 0x4dcd, 0x428f, \
85 : { 0xb7, 0xad, 0x43, 0x5b, 0x93, 0x29, 0x79, 0xec } }
86 :
87 : /**
88 : * Interface which all form controls (e.g. buttons, checkboxes, text,
89 : * radio buttons, select, etc) implement in addition to their dom specific
90 : * interface.
91 : */
92 : class nsIFormControl : public nsISupports
93 : {
94 : public:
95 9 : nsIFormControl(uint8_t aType)
96 9 : : mType(aType)
97 : {
98 9 : }
99 :
100 : NS_DECLARE_STATIC_IID_ACCESSOR(NS_IFORMCONTROL_IID)
101 :
102 : /**
103 : * Get the fieldset for this form control.
104 : * @return the fieldset
105 : */
106 : virtual mozilla::dom::HTMLFieldSetElement *GetFieldSet() = 0;
107 :
108 : /**
109 : * Get the form for this form control.
110 : * @return the form
111 : */
112 : virtual mozilla::dom::Element *GetFormElement() = 0;
113 :
114 : /**
115 : * Set the form for this form control.
116 : * @param aForm the form. This must not be null.
117 : *
118 : * @note that when setting the form the control is not added to the
119 : * form. It adds itself when it gets bound to the tree thereafter,
120 : * so that it can be properly sorted with the other controls in the
121 : * form.
122 : */
123 : virtual void SetForm(nsIDOMHTMLFormElement* aForm) = 0;
124 :
125 : /**
126 : * Tell the control to forget about its form.
127 : *
128 : * @param aRemoveFromForm set false if you do not want this element removed
129 : * from the form. (Used by nsFormControlList::Clear())
130 : * @param aUnbindOrDelete set true if the element is being deleted or unbound
131 : * from tree.
132 : */
133 : virtual void ClearForm(bool aRemoveFromForm, bool aUnbindOrDelete) = 0;
134 :
135 : /**
136 : * Get the type of this control as an int (see NS_FORM_* above)
137 : * @return the type of this control
138 : */
139 700 : uint32_t ControlType() const { return mType; }
140 :
141 : /**
142 : * Reset this form control (as it should be when the user clicks the Reset
143 : * button)
144 : */
145 : NS_IMETHOD Reset() = 0;
146 :
147 : /**
148 : * Tells the form control to submit its names and values to the form
149 : * submission object
150 : * @param aFormSubmission the form submission to notify of names/values/files
151 : * to submit
152 : */
153 : NS_IMETHOD
154 : SubmitNamesValues(mozilla::dom::HTMLFormSubmission* aFormSubmission) = 0;
155 :
156 : /**
157 : * Save to presentation state. The form control will determine whether it
158 : * has anything to save and if so, create an entry in the layout history for
159 : * its pres context.
160 : */
161 : NS_IMETHOD SaveState() = 0;
162 :
163 : /**
164 : * Restore from presentation state. You pass in the presentation state for
165 : * this form control (generated with GenerateStateKey() + "-C") and the form
166 : * control will grab its state from there.
167 : *
168 : * @param aState the pres state to use to restore the control
169 : * @return true if the form control was a checkbox and its
170 : * checked state was restored, false otherwise.
171 : */
172 : virtual bool RestoreState(nsPresState* aState) = 0;
173 :
174 : virtual bool AllowDrop() = 0;
175 :
176 : /**
177 : * Returns whether this is a control which submits the form when activated by
178 : * the user.
179 : * @return whether this is a submit control.
180 : */
181 : inline bool IsSubmitControl() const;
182 :
183 : /**
184 : * Returns whether this is a text control.
185 : * @param aExcludePassword to have NS_FORM_INPUT_PASSWORD returning false.
186 : * @return whether this is a text control.
187 : */
188 : inline bool IsTextControl(bool aExcludePassword) const;
189 :
190 : /**
191 : * Returns true if this is a text control or a number control.
192 : * @param aExcludePassword to have NS_FORM_INPUT_PASSWORD returning false.
193 : * @return true if this is a text control or a number control.
194 : */
195 : inline bool IsTextOrNumberControl(bool aExcludePassword) const;
196 :
197 : /**
198 : * Returns whether this is a single line text control.
199 : * @param aExcludePassword to have NS_FORM_INPUT_PASSWORD returning false.
200 : * @return whether this is a single line text control.
201 : */
202 : inline bool IsSingleLineTextControl(bool aExcludePassword) const;
203 :
204 : /**
205 : * Returns whether this is a submittable form control.
206 : * @return whether this is a submittable form control.
207 : */
208 : inline bool IsSubmittableControl() const;
209 :
210 : /**
211 : * Returns whether this form control can have draggable children.
212 : * @return whether this form control can have draggable children.
213 : */
214 : inline bool AllowDraggableChildren() const;
215 :
216 0 : virtual bool IsDisabledForEvents(mozilla::EventMessage aMessage)
217 : {
218 0 : return false;
219 : }
220 : protected:
221 :
222 : /**
223 : * Returns whether mType corresponds to a single line text control type.
224 : * @param aExcludePassword to have NS_FORM_INPUT_PASSWORD ignored.
225 : * @param aType the type to be tested.
226 : * @return whether mType corresponds to a single line text control type.
227 : */
228 : inline static bool IsSingleLineTextControl(bool aExcludePassword, uint32_t aType);
229 :
230 : /**
231 : * Returns whether this is a auto-focusable form control.
232 : * @return whether this is a auto-focusable form control.
233 : */
234 : inline bool IsAutofocusable() const;
235 :
236 : uint8_t mType;
237 : };
238 :
239 : bool
240 0 : nsIFormControl::IsSubmitControl() const
241 : {
242 0 : uint32_t type = ControlType();
243 0 : return type == NS_FORM_INPUT_SUBMIT ||
244 0 : type == NS_FORM_INPUT_IMAGE ||
245 0 : type == NS_FORM_BUTTON_SUBMIT;
246 : }
247 :
248 : bool
249 69 : nsIFormControl::IsTextControl(bool aExcludePassword) const
250 : {
251 69 : uint32_t type = ControlType();
252 132 : return type == NS_FORM_TEXTAREA ||
253 132 : IsSingleLineTextControl(aExcludePassword, type);
254 : }
255 :
256 : bool
257 69 : nsIFormControl::IsTextOrNumberControl(bool aExcludePassword) const
258 : {
259 69 : return IsTextControl(aExcludePassword) || ControlType() == NS_FORM_INPUT_NUMBER;
260 : }
261 :
262 : bool
263 530 : nsIFormControl::IsSingleLineTextControl(bool aExcludePassword) const
264 : {
265 530 : return IsSingleLineTextControl(aExcludePassword, ControlType());
266 : }
267 :
268 : /*static*/
269 : bool
270 597 : nsIFormControl::IsSingleLineTextControl(bool aExcludePassword, uint32_t aType)
271 : {
272 206 : return aType == NS_FORM_INPUT_TEXT ||
273 206 : aType == NS_FORM_INPUT_EMAIL ||
274 3 : aType == NS_FORM_INPUT_SEARCH ||
275 3 : aType == NS_FORM_INPUT_TEL ||
276 3 : aType == NS_FORM_INPUT_URL ||
277 : // TODO: those are temporary until bug 773205 is fixed.
278 : #if defined(MOZ_WIDGET_ANDROID) || defined(MOZ_WIDGET_GONK)
279 : // On Android/B2G, date/time input appears as a normal text box.
280 : aType == NS_FORM_INPUT_TIME ||
281 : aType == NS_FORM_INPUT_DATE ||
282 : #endif
283 3 : aType == NS_FORM_INPUT_MONTH ||
284 3 : aType == NS_FORM_INPUT_WEEK ||
285 1194 : aType == NS_FORM_INPUT_DATETIME_LOCAL ||
286 603 : (!aExcludePassword && aType == NS_FORM_INPUT_PASSWORD);
287 : }
288 :
289 : bool
290 : nsIFormControl::IsSubmittableControl() const
291 : {
292 : // TODO: keygen should be in that list, see bug 101019.
293 : uint32_t type = ControlType();
294 : return type == NS_FORM_OBJECT ||
295 : type == NS_FORM_TEXTAREA ||
296 : type == NS_FORM_SELECT ||
297 : // type == NS_FORM_KEYGEN ||
298 : type & NS_FORM_BUTTON_ELEMENT ||
299 : type & NS_FORM_INPUT_ELEMENT;
300 : }
301 :
302 : bool
303 0 : nsIFormControl::AllowDraggableChildren() const
304 : {
305 0 : uint32_t type = ControlType();
306 0 : return type == NS_FORM_OBJECT ||
307 0 : type == NS_FORM_FIELDSET ||
308 0 : type == NS_FORM_OUTPUT;
309 : }
310 :
311 : bool
312 25 : nsIFormControl::IsAutofocusable() const
313 : {
314 25 : uint32_t type = ControlType();
315 29 : return type & NS_FORM_INPUT_ELEMENT ||
316 7 : type & NS_FORM_BUTTON_ELEMENT ||
317 25 : type == NS_FORM_TEXTAREA ||
318 25 : type == NS_FORM_SELECT;
319 : }
320 :
321 : NS_DEFINE_STATIC_IID_ACCESSOR(nsIFormControl, NS_IFORMCONTROL_IID)
322 :
323 : #endif /* nsIFormControl_h___ */
|