Back to index

lightning-sunbird  0.9+nobinonly
nsHTMLLegendElement.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla Communicator client code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 #include "nsIDOMHTMLLegendElement.h"
00038 #include "nsIDOMHTMLFormElement.h"
00039 #include "nsIDOMEventReceiver.h"
00040 #include "nsGenericHTMLElement.h"
00041 #include "nsHTMLAtoms.h"
00042 #include "nsStyleConsts.h"
00043 #include "nsPresContext.h"
00044 #include "nsIForm.h"
00045 #include "nsIFormControl.h"
00046 #include "nsIEventStateManager.h"
00047 #include "nsIFocusController.h"
00048 #include "nsIScriptGlobalObject.h"
00049 #include "nsIDocument.h"
00050 #include "nsPIDOMWindow.h"
00051 
00052 
00053 class nsHTMLLegendElement : public nsGenericHTMLFormElement,
00054                             public nsIDOMHTMLLegendElement
00055 {
00056 public:
00057   nsHTMLLegendElement(nsINodeInfo *aNodeInfo);
00058   virtual ~nsHTMLLegendElement();
00059 
00060   // nsISupports
00061   NS_DECL_ISUPPORTS_INHERITED
00062 
00063   // nsIDOMNode
00064   NS_FORWARD_NSIDOMNODE_NO_CLONENODE(nsGenericHTMLFormElement::)
00065 
00066   // nsIDOMElement
00067   NS_FORWARD_NSIDOMELEMENT(nsGenericHTMLFormElement::)
00068 
00069   // nsIDOMHTMLElement
00070   NS_FORWARD_NSIDOMHTMLELEMENT(nsGenericHTMLFormElement::)
00071 
00072   // nsIDOMHTMLLegendElement
00073   NS_DECL_NSIDOMHTMLLEGENDELEMENT
00074 
00075   // nsIFormControl
00076   NS_IMETHOD_(PRInt32) GetType() const { return NS_FORM_LEGEND; }
00077   NS_IMETHOD Reset();
00078   NS_IMETHOD SubmitNamesValues(nsIFormSubmission* aFormSubmission,
00079                                nsIContent* aSubmitElement);
00080 
00081   // nsIContent
00082   virtual nsresult BindToTree(nsIDocument* aDocument, nsIContent* aParent,
00083                               nsIContent* aBindingParent,
00084                               PRBool aCompileEventHandlers);
00085   virtual void UnbindFromTree(PRBool aDeep = PR_TRUE,
00086                               PRBool aNullParent = PR_TRUE);
00087   virtual void SetFocus(nsPresContext* aPresContext);
00088   virtual PRBool ParseAttribute(nsIAtom* aAttribute,
00089                                 const nsAString& aValue,
00090                                 nsAttrValue& aResult);
00091   virtual nsChangeHint GetAttributeChangeHint(const nsIAtom* aAttribute,
00092                                               PRInt32 aModType) const;
00093   nsresult SetAttr(PRInt32 aNameSpaceID, nsIAtom* aName,
00094                    const nsAString& aValue, PRBool aNotify)
00095   {
00096     return SetAttr(aNameSpaceID, aName, nsnull, aValue, aNotify);
00097   }
00098   virtual nsresult SetAttr(PRInt32 aNameSpaceID, nsIAtom* aName,
00099                            nsIAtom* aPrefix, const nsAString& aValue,
00100                            PRBool aNotify);
00101   virtual nsresult UnsetAttr(PRInt32 aNameSpaceID, nsIAtom* aAttribute,
00102                              PRBool aNotify);
00103 };
00104 
00105 
00106 NS_IMPL_NS_NEW_HTML_ELEMENT(Legend)
00107 
00108 
00109 nsHTMLLegendElement::nsHTMLLegendElement(nsINodeInfo *aNodeInfo)
00110   : nsGenericHTMLFormElement(aNodeInfo)
00111 {
00112 }
00113 
00114 nsHTMLLegendElement::~nsHTMLLegendElement()
00115 {
00116 }
00117 
00118 
00119 NS_IMPL_ADDREF_INHERITED(nsHTMLLegendElement, nsGenericElement) 
00120 NS_IMPL_RELEASE_INHERITED(nsHTMLLegendElement, nsGenericElement) 
00121 
00122 
00123 // QueryInterface implementation for nsHTMLLegendElement
00124 NS_HTML_CONTENT_INTERFACE_MAP_BEGIN(nsHTMLLegendElement,
00125                                     nsGenericHTMLFormElement)
00126   NS_INTERFACE_MAP_ENTRY(nsIDOMHTMLLegendElement)
00127   NS_INTERFACE_MAP_ENTRY_CONTENT_CLASSINFO(HTMLLegendElement)
00128 NS_HTML_CONTENT_INTERFACE_MAP_END
00129 
00130 
00131 // nsIDOMHTMLLegendElement
00132 
00133 
00134 NS_IMPL_DOM_CLONENODE(nsHTMLLegendElement)
00135 
00136 
00137 NS_IMETHODIMP
00138 nsHTMLLegendElement::GetForm(nsIDOMHTMLFormElement** aForm)
00139 {
00140   return nsGenericHTMLFormElement::GetForm(aForm);
00141 }
00142 
00143 
00144 NS_IMPL_STRING_ATTR(nsHTMLLegendElement, AccessKey, accesskey)
00145 NS_IMPL_STRING_ATTR(nsHTMLLegendElement, Align, align)
00146 
00147 // this contains center, because IE4 does
00148 static const nsAttrValue::EnumTable kAlignTable[] = {
00149   { "left", NS_STYLE_TEXT_ALIGN_LEFT },
00150   { "right", NS_STYLE_TEXT_ALIGN_RIGHT },
00151   { "center", NS_STYLE_TEXT_ALIGN_CENTER },
00152   { "bottom", NS_STYLE_VERTICAL_ALIGN_BOTTOM },
00153   { "top", NS_STYLE_VERTICAL_ALIGN_TOP },
00154   { 0 }
00155 };
00156 
00157 PRBool
00158 nsHTMLLegendElement::ParseAttribute(nsIAtom* aAttribute,
00159                                     const nsAString& aValue,
00160                                     nsAttrValue& aResult)
00161 {
00162   if (aAttribute == nsHTMLAtoms::align) {
00163     return aResult.ParseEnumValue(aValue, kAlignTable);
00164   }
00165 
00166   return nsGenericHTMLElement::ParseAttribute(aAttribute, aValue, aResult);
00167 }
00168 
00169 nsChangeHint
00170 nsHTMLLegendElement::GetAttributeChangeHint(const nsIAtom* aAttribute,
00171                                             PRInt32 aModType) const
00172 {
00173   nsChangeHint retval =
00174       nsGenericHTMLFormElement::GetAttributeChangeHint(aAttribute, aModType);
00175   if (aAttribute == nsHTMLAtoms::align) {
00176     NS_UpdateHint(retval, NS_STYLE_HINT_REFLOW);
00177   }
00178   return retval;
00179 }
00180 
00181 nsresult
00182 nsHTMLLegendElement::SetAttr(PRInt32 aNameSpaceID, nsIAtom* aAttribute,
00183                              nsIAtom* aPrefix, const nsAString& aValue,
00184                              PRBool aNotify)
00185 {
00186   PRBool accesskey = (aAttribute == nsHTMLAtoms::accesskey &&
00187                       aNameSpaceID == kNameSpaceID_None);
00188   if (accesskey) {
00189     RegUnRegAccessKey(PR_FALSE);
00190   }
00191 
00192   nsresult rv = nsGenericHTMLFormElement::SetAttr(aNameSpaceID, aAttribute,
00193                                                   aPrefix, aValue, aNotify);
00194 
00195   if (accesskey && !aValue.IsEmpty()) {
00196     RegUnRegAccessKey(PR_TRUE);
00197   }
00198 
00199   return rv;
00200 }
00201 
00202 nsresult
00203 nsHTMLLegendElement::UnsetAttr(PRInt32 aNameSpaceID, nsIAtom* aAttribute,
00204                                PRBool aNotify)
00205 {
00206   if (aAttribute == nsHTMLAtoms::accesskey &&
00207       aNameSpaceID == kNameSpaceID_None) {
00208     RegUnRegAccessKey(PR_FALSE);
00209   }
00210 
00211   return nsGenericHTMLFormElement::UnsetAttr(aNameSpaceID, aAttribute, aNotify);
00212 }
00213 
00214 nsresult
00215 nsHTMLLegendElement::Reset()
00216 {
00217   return NS_OK;
00218 }
00219 
00220 nsresult
00221 nsHTMLLegendElement::BindToTree(nsIDocument* aDocument, nsIContent* aParent,
00222                                 nsIContent* aBindingParent,
00223                                 PRBool aCompileEventHandlers)
00224 {
00225   nsresult rv = nsGenericHTMLFormElement::BindToTree(aDocument, aParent,
00226                                                      aBindingParent,
00227                                                      aCompileEventHandlers);
00228   NS_ENSURE_SUCCESS(rv, rv);
00229 
00230   if (aDocument) {
00231     RegUnRegAccessKey(PR_TRUE);
00232   }
00233 
00234   return rv;
00235 }
00236 
00237 void
00238 nsHTMLLegendElement::UnbindFromTree(PRBool aDeep, PRBool aNullParent)
00239 {
00240   if (IsInDoc()) {
00241     RegUnRegAccessKey(PR_FALSE);
00242   }
00243 
00244   nsGenericHTMLFormElement::UnbindFromTree(aDeep, aNullParent);
00245 }
00246 
00247 void
00248 nsHTMLLegendElement::SetFocus(nsPresContext* aPresContext)
00249 {
00250   nsIDocument *document = GetCurrentDoc();
00251   if (!aPresContext || !document) {
00252     return;
00253   }
00254 
00255   nsCOMPtr<nsIEventStateManager> esm = aPresContext->EventStateManager();
00256   if (IsFocusable()) {
00257     esm->SetContentState(this, NS_EVENT_STATE_FOCUS);
00258   } else {
00259     // If the legend isn't focusable (no tabindex) we focus whatever is
00260     // focusable following the legend instead, bug 81481.
00261     nsCOMPtr<nsPIDOMWindow> ourWindow = do_QueryInterface(document->GetScriptGlobalObject());
00262     if (ourWindow) {
00263       nsIFocusController* focusController = ourWindow->GetRootFocusController();
00264       nsIDOMElement* domElement = nsnull;
00265       CallQueryInterface(this, &domElement);
00266       if (focusController && domElement) {
00267         focusController->MoveFocus(PR_TRUE, domElement);
00268       }
00269     }
00270   }
00271 }
00272 
00273 NS_IMETHODIMP
00274 nsHTMLLegendElement::SubmitNamesValues(nsIFormSubmission* aFormSubmission,
00275                                        nsIContent* aSubmitElement)
00276 {
00277   return NS_OK;
00278 }