Bug 1494333 - index crons just like artifacts r=Callek
[gecko.git] / dom / html / nsIFormControl.h
blob9905180232b19ceb4e0869b1582dab82ceef502c
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___
9 #include "mozilla/EventForwards.h"
10 #include "nsISupports.h"
12 namespace mozilla {
13 class PresState;
14 namespace dom {
15 class Element;
16 class HTMLFieldSetElement;
17 class HTMLFormSubmission;
18 class HTMLFormElement;
19 } // namespace dom
20 } // namespace mozilla
22 enum FormControlsTypes {
23 NS_FORM_FIELDSET = 1,
24 NS_FORM_OUTPUT,
25 NS_FORM_SELECT,
26 NS_FORM_TEXTAREA,
27 NS_FORM_OBJECT,
28 eFormControlsWithoutSubTypesMax,
29 // After this, all types will have sub-types which introduce new enum lists.
30 // eFormControlsWithoutSubTypesMax let us know if the previous types values
31 // are not overlapping with sub-types/masks.
33 // Elements with different types, the value is used as a mask.
34 // When changing the order, adding or removing elements, be sure to update
35 // the static_assert checks accordingly.
36 NS_FORM_BUTTON_ELEMENT = 0x40, // 0b01000000
37 NS_FORM_INPUT_ELEMENT = 0x80 // 0b10000000
40 enum ButtonElementTypes : uint8_t {
41 NS_FORM_BUTTON_BUTTON = NS_FORM_BUTTON_ELEMENT + 1,
42 NS_FORM_BUTTON_RESET,
43 NS_FORM_BUTTON_SUBMIT,
44 eButtonElementTypesMax
47 enum InputElementTypes : uint8_t {
48 NS_FORM_INPUT_BUTTON = NS_FORM_INPUT_ELEMENT + 1,
49 NS_FORM_INPUT_CHECKBOX,
50 NS_FORM_INPUT_COLOR,
51 NS_FORM_INPUT_DATE,
52 NS_FORM_INPUT_EMAIL,
53 NS_FORM_INPUT_FILE,
54 NS_FORM_INPUT_HIDDEN,
55 NS_FORM_INPUT_RESET,
56 NS_FORM_INPUT_IMAGE,
57 NS_FORM_INPUT_MONTH,
58 NS_FORM_INPUT_NUMBER,
59 NS_FORM_INPUT_PASSWORD,
60 NS_FORM_INPUT_RADIO,
61 NS_FORM_INPUT_SEARCH,
62 NS_FORM_INPUT_SUBMIT,
63 NS_FORM_INPUT_TEL,
64 NS_FORM_INPUT_TEXT,
65 NS_FORM_INPUT_TIME,
66 NS_FORM_INPUT_URL,
67 NS_FORM_INPUT_RANGE,
68 NS_FORM_INPUT_WEEK,
69 NS_FORM_INPUT_DATETIME_LOCAL,
70 eInputElementTypesMax
73 static_assert(static_cast<uint32_t>(eFormControlsWithoutSubTypesMax) <
74 static_cast<uint32_t>(NS_FORM_BUTTON_ELEMENT),
75 "Too many FormControlsTypes without sub-types");
76 static_assert(static_cast<uint32_t>(eButtonElementTypesMax) <
77 static_cast<uint32_t>(NS_FORM_INPUT_ELEMENT),
78 "Too many ButtonElementTypes");
79 static_assert(static_cast<uint32_t>(eInputElementTypesMax) < 1<<8,
80 "Too many form control types");
82 #define NS_IFORMCONTROL_IID \
83 { 0x4b89980c, 0x4dcd, 0x428f, \
84 { 0xb7, 0xad, 0x43, 0x5b, 0x93, 0x29, 0x79, 0xec } }
86 /**
87 * Interface which all form controls (e.g. buttons, checkboxes, text,
88 * radio buttons, select, etc) implement in addition to their dom specific
89 * interface.
91 class nsIFormControl : public nsISupports
93 public:
94 nsIFormControl(uint8_t aType)
95 : mType(aType)
99 NS_DECLARE_STATIC_IID_ACCESSOR(NS_IFORMCONTROL_IID)
102 * Get the fieldset for this form control.
103 * @return the fieldset
105 virtual mozilla::dom::HTMLFieldSetElement *GetFieldSet() = 0;
108 * Get the form for this form control.
109 * @return the form
111 virtual mozilla::dom::Element *GetFormElement() = 0;
114 * Set the form for this form control.
115 * @param aForm the form. This must not be null.
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.
122 virtual void SetForm(mozilla::dom::HTMLFormElement* aForm) = 0;
125 * Tell the control to forget about its form.
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.
132 virtual void ClearForm(bool aRemoveFromForm, bool aUnbindOrDelete) = 0;
135 * Get the type of this control as an int (see NS_FORM_* above)
136 * @return the type of this control
138 uint32_t ControlType() const { return mType; }
141 * Reset this form control (as it should be when the user clicks the Reset
142 * button)
144 NS_IMETHOD Reset() = 0;
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
152 NS_IMETHOD
153 SubmitNamesValues(mozilla::dom::HTMLFormSubmission* aFormSubmission) = 0;
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.
160 NS_IMETHOD SaveState() = 0;
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.
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.
171 virtual bool RestoreState(mozilla::PresState* aState) = 0;
173 virtual bool AllowDrop() = 0;
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.
180 inline bool IsSubmitControl() const;
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.
187 inline bool IsTextControl(bool aExcludePassword) const;
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.
194 inline bool IsTextOrNumberControl(bool aExcludePassword) const;
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.
201 inline bool IsSingleLineTextControl(bool aExcludePassword) const;
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.
208 inline bool IsSingleLineTextOrNumberControl(bool aExcludePassword) const;
211 * Returns whether this is a submittable form control.
212 * @return whether this is a submittable form control.
214 inline bool IsSubmittableControl() const;
217 * Returns whether this form control can have draggable children.
218 * @return whether this form control can have draggable children.
220 inline bool AllowDraggableChildren() const;
222 virtual bool IsDisabledForEvents(mozilla::EventMessage aMessage)
224 return false;
226 protected:
229 * Returns whether mType corresponds to a single line text control type.
230 * @param aExcludePassword to have NS_FORM_INPUT_PASSWORD ignored.
231 * @param aType the type to be tested.
232 * @return whether mType corresponds to a single line text control type.
234 inline static bool IsSingleLineTextControl(bool aExcludePassword, uint32_t aType);
237 * Returns whether this is a auto-focusable form control.
238 * @return whether this is a auto-focusable form control.
240 inline bool IsAutofocusable() const;
242 uint8_t mType;
245 bool
246 nsIFormControl::IsSubmitControl() const
248 uint32_t type = ControlType();
249 return type == NS_FORM_INPUT_SUBMIT ||
250 type == NS_FORM_INPUT_IMAGE ||
251 type == NS_FORM_BUTTON_SUBMIT;
254 bool
255 nsIFormControl::IsTextControl(bool aExcludePassword) const
257 uint32_t type = ControlType();
258 return type == NS_FORM_TEXTAREA ||
259 IsSingleLineTextControl(aExcludePassword, type);
262 bool
263 nsIFormControl::IsTextOrNumberControl(bool aExcludePassword) const
265 return IsTextControl(aExcludePassword) || ControlType() == NS_FORM_INPUT_NUMBER;
268 bool
269 nsIFormControl::IsSingleLineTextControl(bool aExcludePassword) const
271 return IsSingleLineTextControl(aExcludePassword, ControlType());
274 bool
275 nsIFormControl::IsSingleLineTextOrNumberControl(bool aExcludePassword) const
277 return IsSingleLineTextControl(aExcludePassword) ||
278 ControlType() == NS_FORM_INPUT_NUMBER;
281 /*static*/
282 bool
283 nsIFormControl::IsSingleLineTextControl(bool aExcludePassword, uint32_t aType)
285 return aType == NS_FORM_INPUT_TEXT ||
286 aType == NS_FORM_INPUT_EMAIL ||
287 aType == NS_FORM_INPUT_SEARCH ||
288 aType == NS_FORM_INPUT_TEL ||
289 aType == NS_FORM_INPUT_URL ||
290 // TODO: those are temporary until bug 773205 is fixed.
291 aType == NS_FORM_INPUT_MONTH ||
292 aType == NS_FORM_INPUT_WEEK ||
293 aType == NS_FORM_INPUT_DATETIME_LOCAL ||
294 (!aExcludePassword && aType == NS_FORM_INPUT_PASSWORD);
297 bool
298 nsIFormControl::IsSubmittableControl() const
300 // TODO: keygen should be in that list, see bug 101019.
301 uint32_t type = ControlType();
302 return type == NS_FORM_OBJECT ||
303 type == NS_FORM_TEXTAREA ||
304 type == NS_FORM_SELECT ||
305 // type == NS_FORM_KEYGEN ||
306 type & NS_FORM_BUTTON_ELEMENT ||
307 type & NS_FORM_INPUT_ELEMENT;
310 bool
311 nsIFormControl::AllowDraggableChildren() const
313 uint32_t type = ControlType();
314 return type == NS_FORM_OBJECT ||
315 type == NS_FORM_FIELDSET ||
316 type == NS_FORM_OUTPUT;
319 bool
320 nsIFormControl::IsAutofocusable() const
322 uint32_t type = ControlType();
323 return type & NS_FORM_INPUT_ELEMENT ||
324 type & NS_FORM_BUTTON_ELEMENT ||
325 type == NS_FORM_TEXTAREA ||
326 type == NS_FORM_SELECT;
329 NS_DEFINE_STATIC_IID_ACCESSOR(nsIFormControl, NS_IFORMCONTROL_IID)
331 #endif /* nsIFormControl_h___ */