Files
Mozilla/mozilla/layout/html/content/src/nsHTMLLegendElement.cpp
2000-09-09 05:46:14 +00:00

333 lines
9.0 KiB
C++

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is Mozilla Communicator client code.
*
* The Initial Developer of the Original Code is Netscape Communications
* Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
*/
#include "nsIDOMHTMLLegendElement.h"
#include "nsIDOMHTMLFormElement.h"
#include "nsIScriptObjectOwner.h"
#include "nsIDOMEventReceiver.h"
#include "nsIHTMLContent.h"
#include "nsGenericHTMLElement.h"
#include "nsHTMLAtoms.h"
#include "nsHTMLIIDs.h"
#include "nsIStyleContext.h"
#include "nsIMutableStyleContext.h"
#include "nsStyleConsts.h"
#include "nsIPresContext.h"
#include "nsIForm.h"
#include "nsIFormControl.h"
#include "nsISizeOfHandler.h"
class nsHTMLLegendElement : public nsIDOMHTMLLegendElement,
public nsIJSScriptObject,
public nsIHTMLContent,
public nsIFormControl
{
public:
nsHTMLLegendElement(nsINodeInfo *aNodeInfo);
virtual ~nsHTMLLegendElement();
// nsISupports
NS_DECL_ISUPPORTS
// nsIDOMNode
NS_IMPL_IDOMNODE_USING_GENERIC(mInner)
// nsIDOMElement
NS_IMPL_IDOMELEMENT_USING_GENERIC(mInner)
// nsIDOMHTMLElement
NS_IMPL_IDOMHTMLELEMENT_USING_GENERIC(mInner)
// nsIDOMHTMLLegendElement
NS_DECL_IDOMHTMLLEGENDELEMENT
// nsIJSScriptObject
NS_IMPL_IJSSCRIPTOBJECT_USING_GENERIC(mInner)
// nsIContent
NS_IMPL_ICONTENT_NO_SETPARENT_NO_SETDOCUMENT_USING_GENERIC(mInner)
// nsIHTMLContent
NS_IMPL_IHTMLCONTENT_USING_GENERIC(mInner)
// nsIFormControl
NS_IMETHOD SetForm(nsIDOMHTMLFormElement* aForm);
NS_IMETHOD GetType(PRInt32* aType);
NS_IMETHOD Init() { return NS_OK; }
protected:
nsGenericHTMLContainerFormElement mInner;
nsIForm* mForm;
};
nsresult
NS_NewHTMLLegendElement(nsIHTMLContent** aInstancePtrResult,
nsINodeInfo *aNodeInfo)
{
NS_ENSURE_ARG_POINTER(aInstancePtrResult);
NS_ENSURE_ARG_POINTER(aNodeInfo);
nsIHTMLContent* it = new nsHTMLLegendElement(aNodeInfo);
if (nsnull == it) {
return NS_ERROR_OUT_OF_MEMORY;
}
return it->QueryInterface(NS_GET_IID(nsIHTMLContent), (void**) aInstancePtrResult);
}
nsHTMLLegendElement::nsHTMLLegendElement(nsINodeInfo *aNodeInfo)
{
NS_INIT_REFCNT();
mInner.Init(this, aNodeInfo);
mForm = nsnull;
}
nsHTMLLegendElement::~nsHTMLLegendElement()
{
// Null out form's pointer to us - no ref counting here!
SetForm(nsnull);
}
NS_IMPL_ADDREF(nsHTMLLegendElement)
NS_IMPL_RELEASE(nsHTMLLegendElement)
nsresult
nsHTMLLegendElement::QueryInterface(REFNSIID aIID, void** aInstancePtr)
{
NS_IMPL_HTML_CONTENT_QUERY_INTERFACE(aIID, aInstancePtr, this)
if (aIID.Equals(NS_GET_IID(nsIDOMHTMLLegendElement))) {
nsIDOMHTMLLegendElement* tmp = this;
*aInstancePtr = (void*) tmp;
NS_ADDREF_THIS();
return NS_OK;
}
else if (aIID.Equals(NS_GET_IID(nsIFormControl))) {
*aInstancePtr = (void*)(nsIFormControl*)this;
NS_ADDREF_THIS();
return NS_OK;
}
return NS_NOINTERFACE;
}
nsresult
nsHTMLLegendElement::CloneNode(PRBool aDeep, nsIDOMNode** aReturn)
{
nsHTMLLegendElement* it = new nsHTMLLegendElement(mInner.mNodeInfo);
if (nsnull == it) {
return NS_ERROR_OUT_OF_MEMORY;
}
nsCOMPtr<nsIDOMNode> kungFuDeathGrip(it);
mInner.CopyInnerTo(this, &it->mInner, aDeep);
return it->QueryInterface(NS_GET_IID(nsIDOMNode), (void**) aReturn);
}
// nsIContent
NS_IMETHODIMP
nsHTMLLegendElement::SetParent(nsIContent* aParent)
{
return mInner.SetParentForFormControls(aParent, this, mForm);
}
NS_IMETHODIMP
nsHTMLLegendElement::SetDocument(nsIDocument* aDocument, PRBool aDeep, PRBool aCompileEventHandlers)
{
return mInner.SetDocumentForFormControls(aDocument, aDeep, aCompileEventHandlers, this, mForm);
}
NS_IMETHODIMP
nsHTMLLegendElement::GetForm(nsIDOMHTMLFormElement** aForm)
{
nsresult result = NS_OK;
*aForm = nsnull;
if (nsnull != mForm) {
nsIDOMHTMLFormElement* formElem = nsnull;
result = mForm->QueryInterface(NS_GET_IID(nsIDOMHTMLFormElement), (void**)&formElem);
if (NS_OK == result) {
*aForm = formElem;
}
}
return result;
}
NS_IMETHODIMP
nsHTMLLegendElement::SetForm(nsIDOMHTMLFormElement* aForm)
{
nsCOMPtr<nsIFormControl> formControl;
nsresult result = QueryInterface(NS_GET_IID(nsIFormControl), getter_AddRefs(formControl));
if (NS_FAILED(result)) formControl = nsnull;
nsAutoString nameVal, idVal;
mInner.GetAttribute(kNameSpaceID_None, nsHTMLAtoms::name, nameVal);
mInner.GetAttribute(kNameSpaceID_None, nsHTMLAtoms::id, idVal);
if (mForm && formControl) {
mForm->RemoveElement(formControl);
if (nameVal.Length())
mForm->RemoveElementFromTable(this, nameVal);
if (idVal.Length())
mForm->RemoveElementFromTable(this, idVal);
}
if (aForm) {
nsCOMPtr<nsIForm> theForm = do_QueryInterface(aForm, &result);
mForm = theForm; // Even if we fail, update mForm (nsnull in failure)
if ((NS_OK == result) && theForm) {
if (formControl) {
theForm->AddElement(formControl);
if (nameVal.Length())
theForm->AddElementToTable(this, nameVal);
if (idVal.Length())
theForm->AddElementToTable(this, idVal);
}
}
} else {
mForm = nsnull;
}
mInner.SetForm(mForm);
return result;
}
// nsIFormControl
NS_IMETHODIMP
nsHTMLLegendElement::GetType(PRInt32* aType)
{
if (aType) {
*aType = NS_FORM_LEGEND;
return NS_OK;
} else {
return NS_FORM_NOTOK;
}
}
NS_IMPL_STRING_ATTR(nsHTMLLegendElement, AccessKey, accesskey)
NS_IMPL_STRING_ATTR(nsHTMLLegendElement, Align, align)
// this contains center, because IE4 does
static nsGenericHTMLElement::EnumTable kAlignTable[] = {
{ "left", NS_STYLE_TEXT_ALIGN_LEFT },
{ "right", NS_STYLE_TEXT_ALIGN_RIGHT },
{ "center", NS_STYLE_TEXT_ALIGN_CENTER },
{ "bottom", NS_STYLE_VERTICAL_ALIGN_BOTTOM },
{ "top", NS_STYLE_VERTICAL_ALIGN_TOP },
{ 0 }
};
NS_IMETHODIMP
nsHTMLLegendElement::StringToAttribute(nsIAtom* aAttribute,
const nsAReadableString& aValue,
nsHTMLValue& aResult)
{
if (aAttribute == nsHTMLAtoms::align) {
if (nsGenericHTMLElement::ParseEnumValue(aValue, kAlignTable, aResult)) {
return NS_CONTENT_ATTR_HAS_VALUE;
}
}
return NS_CONTENT_ATTR_NOT_THERE;
}
NS_IMETHODIMP
nsHTMLLegendElement::AttributeToString(nsIAtom* aAttribute,
const nsHTMLValue& aValue,
nsAWritableString& aResult) const
{
if (aAttribute == nsHTMLAtoms::align) {
if (eHTMLUnit_Enumerated == aValue.GetUnit()) {
nsGenericHTMLElement::EnumValueToString(aValue, kAlignTable, aResult);
return NS_CONTENT_ATTR_HAS_VALUE;
}
}
return mInner.AttributeToString(aAttribute, aValue, aResult);
}
static void
MapAttributesInto(const nsIHTMLMappedAttributes* aAttributes,
nsIMutableStyleContext* aContext,
nsIPresContext* aPresContext)
{
nsGenericHTMLElement::MapCommonAttributesInto(aAttributes, aContext, aPresContext);
}
NS_IMETHODIMP
nsHTMLLegendElement::GetMappedAttributeImpact(const nsIAtom* aAttribute,
PRInt32& aHint) const
{
if (! nsGenericHTMLElement::GetCommonMappedAttributesImpact(aAttribute, aHint)) {
aHint = NS_STYLE_HINT_CONTENT;
}
return NS_OK;
}
NS_IMETHODIMP
nsHTMLLegendElement::GetAttributeMappingFunctions(nsMapAttributesFunc& aFontMapFunc,
nsMapAttributesFunc& aMapFunc) const
{
aFontMapFunc = nsnull;
aMapFunc = &MapAttributesInto;
return NS_OK;
}
NS_IMETHODIMP
nsHTMLLegendElement::HandleDOMEvent(nsIPresContext* aPresContext,
nsEvent* aEvent,
nsIDOMEvent** aDOMEvent,
PRUint32 aFlags,
nsEventStatus* aEventStatus)
{
return mInner.HandleDOMEvent(aPresContext, aEvent, aDOMEvent,
aFlags, aEventStatus);
}
NS_IMETHODIMP
nsHTMLLegendElement::SizeOf(nsISizeOfHandler* aSizer, PRUint32* aResult) const
{
if (!aResult) return NS_ERROR_NULL_POINTER;
#ifdef DEBUG
mInner.SizeOf(aSizer, aResult, sizeof(*this));
if (mForm) {
PRBool recorded;
aSizer->RecordObject(mForm, &recorded);
if (!recorded) {
PRUint32 formSize;
mForm->SizeOf(aSizer, &formSize);
aSizer->AddSize(nsHTMLAtoms::iform, formSize);
}
}
#endif
return NS_OK;
}