LCOV - code coverage report
Current view: directory - objdir/dist/include - nsIFormControl.h (source / functions) Found Hit Coverage
Test: app.info Lines: 20 11 55.0 %
Date: 2012-06-02 Functions: 8 5 62.5 %

       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
      18                 :  * Netscape Communications Corporation.
      19                 :  * Portions created by the Initial Developer are Copyright (C) 1998
      20                 :  * the Initial Developer. All Rights Reserved.
      21                 :  *
      22                 :  * Contributor(s):
      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                 : #ifndef nsIFormControl_h___
      38                 : #define nsIFormControl_h___
      39                 : 
      40                 : #include "nsISupports.h"
      41                 : class nsIDOMHTMLFormElement;
      42                 : class nsPresState;
      43                 : class nsIContent;
      44                 : class nsString;
      45                 : class nsIFormProcessor;
      46                 : class nsFormSubmission;
      47                 : 
      48                 : namespace mozilla {
      49                 : namespace dom {
      50                 : class Element;
      51                 : } // namespace dom
      52                 : } // namespace mozilla
      53                 : 
      54                 : enum FormControlsTypes {
      55                 :   NS_FORM_FIELDSET = 1,
      56                 :   NS_FORM_LABEL,
      57                 :   NS_FORM_OUTPUT,
      58                 :   NS_FORM_SELECT,
      59                 :   NS_FORM_TEXTAREA,
      60                 :   NS_FORM_OBJECT,
      61                 :   NS_FORM_PROGRESS,
      62                 :   eFormControlsWithoutSubTypesMax,
      63                 :   // After this, all types will have sub-types which introduce new enum lists.
      64                 :   // eFormControlsWithoutSubTypesMax let us know if the previous types values
      65                 :   // are not overlapping with sub-types/masks.
      66                 : 
      67                 :   // Elements with different types, the value is used as a mask.
      68                 :   // Adding '_ELEMENT' because NS_FORM_INPUT is used for 'oninput' event.
      69                 :   // When changing the order, adding or removing elements, be sure to update
      70                 :   // the PR_STATIC_ASSERT checks accordingly.
      71                 :   NS_FORM_BUTTON_ELEMENT = 0x40, // 0b01000000
      72                 :   NS_FORM_INPUT_ELEMENT  = 0x80  // 0b10000000
      73                 : };
      74                 : 
      75                 : enum ButtonElementTypes {
      76                 :   NS_FORM_BUTTON_BUTTON = NS_FORM_BUTTON_ELEMENT + 1,
      77                 :   NS_FORM_BUTTON_RESET,
      78                 :   NS_FORM_BUTTON_SUBMIT,
      79                 :   eButtonElementTypesMax
      80                 : };
      81                 : 
      82                 : enum InputElementTypes {
      83                 :   NS_FORM_INPUT_BUTTON = NS_FORM_INPUT_ELEMENT + 1,
      84                 :   NS_FORM_INPUT_CHECKBOX,
      85                 :   NS_FORM_INPUT_EMAIL,
      86                 :   NS_FORM_INPUT_FILE,
      87                 :   NS_FORM_INPUT_HIDDEN,
      88                 :   NS_FORM_INPUT_RESET,
      89                 :   NS_FORM_INPUT_IMAGE,
      90                 :   NS_FORM_INPUT_PASSWORD,
      91                 :   NS_FORM_INPUT_RADIO,
      92                 :   NS_FORM_INPUT_SEARCH,
      93                 :   NS_FORM_INPUT_SUBMIT,
      94                 :   NS_FORM_INPUT_TEL,
      95                 :   NS_FORM_INPUT_TEXT,
      96                 :   NS_FORM_INPUT_URL,
      97                 :   eInputElementTypesMax
      98                 : };
      99                 : 
     100                 : PR_STATIC_ASSERT((PRUint32)eFormControlsWithoutSubTypesMax < (PRUint32)NS_FORM_BUTTON_ELEMENT);
     101                 : PR_STATIC_ASSERT((PRUint32)eButtonElementTypesMax < (PRUint32)NS_FORM_INPUT_ELEMENT);
     102                 : PR_STATIC_ASSERT((PRUint32)eInputElementTypesMax  < 1<<8);
     103                 : 
     104                 : #define NS_IFORMCONTROL_IID   \
     105                 : { 0x671ef379, 0x7ac0, 0x414c, \
     106                 :  { 0xa2, 0x2b, 0xc1, 0x9e, 0x0b, 0x61, 0x4e, 0x83 } }
     107                 : 
     108                 : /**
     109                 :  * Interface which all form controls (e.g. buttons, checkboxes, text,
     110                 :  * radio buttons, select, etc) implement in addition to their dom specific
     111                 :  * interface.
     112                 :  */
     113                 : class nsIFormControl : public nsISupports
     114               1 : {
     115                 : public:
     116                 : 
     117                 :   NS_DECLARE_STATIC_IID_ACCESSOR(NS_IFORMCONTROL_IID)
     118                 : 
     119                 :   /**
     120                 :    * Get the form for this form control.
     121                 :    * @return the form
     122                 :    */
     123                 :   virtual mozilla::dom::Element *GetFormElement() = 0;
     124                 : 
     125                 :   /**
     126                 :    * Set the form for this form control.
     127                 :    * @param aForm the form.  This must not be null.
     128                 :    *
     129                 :    * @note that when setting the form the control is not added to the
     130                 :    * form.  It adds itself when it gets bound to the tree thereafter,
     131                 :    * so that it can be properly sorted with the other controls in the
     132                 :    * form.
     133                 :    */
     134                 :   virtual void SetForm(nsIDOMHTMLFormElement* aForm) = 0;
     135                 : 
     136                 :   /**
     137                 :    * Tell the control to forget about its form.
     138                 :    *
     139                 :    * @param aRemoveFromForm set false if you do not want this element removed
     140                 :    *        from the form.  (Used by nsFormControlList::Clear())
     141                 :    */
     142                 :   virtual void ClearForm(bool aRemoveFromForm) = 0;
     143                 : 
     144                 :   /**
     145                 :    * Get the type of this control as an int (see NS_FORM_* above)
     146                 :    * @return the type of this control
     147                 :    */
     148                 :   NS_IMETHOD_(PRUint32) GetType() const = 0 ;
     149                 : 
     150                 :   /**
     151                 :    * Reset this form control (as it should be when the user clicks the Reset
     152                 :    * button)
     153                 :    */
     154                 :   NS_IMETHOD Reset() = 0;
     155                 : 
     156                 :   /**
     157                 :    * Tells the form control to submit its names and values to the form
     158                 :    * submission object
     159                 :    * @param aFormSubmission the form submission to notify of names/values/files
     160                 :    *                       to submit
     161                 :    */
     162                 :   NS_IMETHOD SubmitNamesValues(nsFormSubmission* aFormSubmission) = 0;
     163                 : 
     164                 :   /**
     165                 :    * Save to presentation state.  The form control will determine whether it
     166                 :    * has anything to save and if so, create an entry in the layout history for
     167                 :    * its pres context.
     168                 :    */
     169                 :   NS_IMETHOD SaveState() = 0;
     170                 : 
     171                 :   /**
     172                 :    * Restore from presentation state.  You pass in the presentation state for
     173                 :    * this form control (generated with GenerateStateKey() + "-C") and the form
     174                 :    * control will grab its state from there.
     175                 :    *
     176                 :    * @param aState the pres state to use to restore the control
     177                 :    * @return true if the form control was a checkbox and its
     178                 :    *         checked state was restored, false otherwise.
     179                 :    */
     180                 :   virtual bool RestoreState(nsPresState* aState) = 0;
     181                 : 
     182                 :   virtual bool AllowDrop() = 0;
     183                 : 
     184                 :   /**
     185                 :    * Returns whether this is a control which submits the form when activated by
     186                 :    * the user.
     187                 :    * @return whether this is a submit control.
     188                 :    */
     189                 :   inline bool IsSubmitControl() const;
     190                 : 
     191                 :   /**
     192                 :    * Returns whether this is a text control.
     193                 :    * @param  aExcludePassword  to have NS_FORM_INPUT_PASSWORD returning false.
     194                 :    * @return whether this is a text control.
     195                 :    */
     196                 :   inline bool IsTextControl(bool aExcludePassword) const ;
     197                 : 
     198                 :   /**
     199                 :    * Returns whether this is a single line text control.
     200                 :    * @param  aExcludePassword  to have NS_FORM_INPUT_PASSWORD returning false.
     201                 :    * @return whether this is a single line text control.
     202                 :    */
     203                 :   inline bool IsSingleLineTextControl(bool aExcludePassword) const;
     204                 : 
     205                 :   /**
     206                 :    * Returns whether this is a labelable form control.
     207                 :    * @return whether this is a labelable form control.
     208                 :    */
     209                 :   inline bool IsLabelableControl() const;
     210                 : 
     211                 :   /**
     212                 :    * Returns whether this is a submittable form control.
     213                 :    * @return whether this is a submittable form control.
     214                 :    */
     215                 :   inline bool IsSubmittableControl() const;
     216                 : 
     217                 :   /**
     218                 :    * Returns whether this form control can have draggable children.
     219                 :    * @return whether this form control can have draggable children.
     220                 :    */
     221                 :   inline bool AllowDraggableChildren() const;
     222                 : 
     223                 : protected:
     224                 : 
     225                 :   /**
     226                 :    * Returns whether mType corresponds to a single line text control type.
     227                 :    * @param aExcludePassword to have NS_FORM_INPUT_PASSWORD ignored.
     228                 :    * @param aType the type to be tested.
     229                 :    * @return whether mType corresponds to a single line text control type.
     230                 :    */
     231                 :   inline static bool IsSingleLineTextControl(bool aExcludePassword, PRUint32 aType);
     232                 : 
     233                 :   /**
     234                 :    * Returns whether this is a auto-focusable form control.
     235                 :    * @return whether this is a auto-focusable form control.
     236                 :    */
     237                 :   inline bool IsAutofocusable() const;
     238                 : };
     239                 : 
     240                 : bool
     241               0 : nsIFormControl::IsSubmitControl() const
     242                 : {
     243               0 :   PRUint32 type = GetType();
     244                 :   return type == NS_FORM_INPUT_SUBMIT ||
     245                 :          type == NS_FORM_INPUT_IMAGE ||
     246               0 :          type == NS_FORM_BUTTON_SUBMIT;
     247                 : }
     248                 : 
     249                 : bool
     250               1 : nsIFormControl::IsTextControl(bool aExcludePassword) const
     251                 : {
     252               1 :   PRUint32 type = GetType();
     253                 :   return type == NS_FORM_TEXTAREA ||
     254               1 :          IsSingleLineTextControl(aExcludePassword, type);
     255                 : }
     256                 : 
     257                 : bool
     258               5 : nsIFormControl::IsSingleLineTextControl(bool aExcludePassword) const
     259                 : {
     260               5 :   return IsSingleLineTextControl(aExcludePassword, GetType());
     261                 : }
     262                 : 
     263                 : /*static*/
     264                 : bool
     265              13 : nsIFormControl::IsSingleLineTextControl(bool aExcludePassword, PRUint32 aType)
     266                 : {
     267                 :   return aType == NS_FORM_INPUT_TEXT ||
     268                 :          aType == NS_FORM_INPUT_EMAIL ||
     269                 :          aType == NS_FORM_INPUT_SEARCH ||
     270                 :          aType == NS_FORM_INPUT_TEL ||
     271                 :          aType == NS_FORM_INPUT_URL ||
     272              13 :          (!aExcludePassword && aType == NS_FORM_INPUT_PASSWORD);
     273                 : }
     274                 : 
     275                 : bool
     276               0 : nsIFormControl::IsLabelableControl() const
     277                 : {
     278                 :   // TODO: keygen should be in that list, see bug 101019.
     279                 :   // TODO: meter should be added, see bug 555985.
     280                 :   // TODO: NS_FORM_INPUT_HIDDEN should be removed, see bug 597650.
     281               0 :   PRUint32 type = GetType();
     282                 :   return type & NS_FORM_INPUT_ELEMENT ||
     283                 :          type & NS_FORM_BUTTON_ELEMENT ||
     284                 :          // type == NS_FORM_KEYGEN ||
     285                 :          // type == NS_FORM_METER ||
     286                 :          type == NS_FORM_OUTPUT ||
     287                 :          type == NS_FORM_PROGRESS ||
     288                 :          type == NS_FORM_SELECT ||
     289               0 :          type == NS_FORM_TEXTAREA;
     290                 : }
     291                 : 
     292                 : bool
     293                 : nsIFormControl::IsSubmittableControl() const
     294                 : {
     295                 :   // TODO: keygen should be in that list, see bug 101019.
     296                 :   PRUint32 type = GetType();
     297                 :   return type == NS_FORM_OBJECT ||
     298                 :          type == NS_FORM_TEXTAREA ||
     299                 :          type == NS_FORM_SELECT ||
     300                 :          // type == NS_FORM_KEYGEN ||
     301                 :          type & NS_FORM_BUTTON_ELEMENT ||
     302                 :          type & NS_FORM_INPUT_ELEMENT;
     303                 : }
     304                 : 
     305                 : bool
     306               0 : nsIFormControl::AllowDraggableChildren() const
     307                 : {
     308               0 :   PRUint32 type = GetType();
     309                 :   return type == NS_FORM_OBJECT ||
     310                 :          type == NS_FORM_LABEL ||
     311                 :          type == NS_FORM_FIELDSET ||
     312               0 :          type == NS_FORM_OUTPUT;
     313                 : }
     314                 : 
     315                 : bool
     316               1 : nsIFormControl::IsAutofocusable() const
     317                 : {
     318               1 :   PRUint32 type = GetType();
     319                 :   return type & NS_FORM_INPUT_ELEMENT ||
     320                 :          type & NS_FORM_BUTTON_ELEMENT ||
     321                 :          type == NS_FORM_TEXTAREA ||
     322               1 :          type == NS_FORM_SELECT;
     323                 : }
     324                 : 
     325                 : NS_DEFINE_STATIC_IID_ACCESSOR(nsIFormControl, NS_IFORMCONTROL_IID)
     326                 : 
     327                 : #endif /* nsIFormControl_h___ */

Generated by: LCOV version 1.7