nsIFormControl.h 9.0 KB

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