Back to index

lightning-sunbird  0.9+nobinonly
nsHTMLObjectElement.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 "nsIDOMHTMLObjectElement.h"
00038 #include "nsGenericHTMLElement.h"
00039 #include "nsImageLoadingContent.h"
00040 #include "nsHTMLAtoms.h"
00041 #include "nsStyleConsts.h"
00042 #include "nsDOMError.h"
00043 
00044 #include "nsIDocument.h"
00045 #include "nsIPresShell.h"
00046 #include "nsIDOMDocument.h"
00047 #include "nsIWebNavigation.h"
00048 #include "nsIFormSubmission.h"
00049 #include "nsIObjectFrame.h"
00050 #include "nsIPluginInstance.h"
00051 #include "nsIPluginInstanceInternal.h"
00052 #include "nsIPluginElement.h"
00053 
00054 class nsHTMLObjectElement : public nsGenericHTMLFormElement,
00055                             public nsImageLoadingContent,
00056                             public nsIPluginElement,
00057                             public nsIDOMHTMLObjectElement
00058 {
00059 public:
00060   nsHTMLObjectElement(nsINodeInfo *aNodeInfo, PRBool aFromParser = PR_FALSE);
00061   virtual ~nsHTMLObjectElement();
00062 
00063   // nsISupports
00064   NS_DECL_ISUPPORTS_INHERITED
00065 
00066   // nsIPluginElement
00067   NS_DECL_NSIPLUGINELEMENT
00068 
00069   // nsIDOMNode
00070   NS_FORWARD_NSIDOMNODE_NO_CLONENODE(nsGenericHTMLFormElement::)
00071 
00072   // nsIDOMElement
00073   NS_FORWARD_NSIDOMELEMENT(nsGenericHTMLFormElement::)
00074 
00075   // nsIDOMHTMLElement
00076   NS_FORWARD_NSIDOMHTMLELEMENT(nsGenericHTMLFormElement::)
00077 
00078   // nsIDOMHTMLObjectElement
00079   NS_DECL_NSIDOMHTMLOBJECTELEMENT
00080 
00081   // nsIContent
00082   virtual void SetFocus(nsPresContext* aPresContext);
00083   virtual PRBool IsFocusable(PRInt32 *aTabIndex = nsnull);
00084 
00085   // Overriden nsIFormControl methods
00086   NS_IMETHOD_(PRInt32) GetType() const { return NS_FORM_OBJECT; }
00087   NS_IMETHOD Reset();
00088   NS_IMETHOD SubmitNamesValues(nsIFormSubmission* aFormSubmission,
00089                                nsIContent* aSubmitElement);
00090   NS_IMETHOD SaveState();
00091   virtual PRBool RestoreState(nsPresState* aState);
00092 
00093   virtual void DoneAddingChildren();
00094   virtual PRBool IsDoneAddingChildren();
00095 
00096   virtual PRBool ParseAttribute(nsIAtom* aAttribute,
00097                                 const nsAString& aValue,
00098                                 nsAttrValue& aResult);
00099   virtual nsMapRuleToAttributesFunc GetAttributeMappingFunction() const;
00100   NS_IMETHOD_(PRBool) IsAttributeMapped(const nsIAtom* aAttribute) const;
00101 
00102 protected:
00103   PRPackedBool mIsDoneAddingChildren;
00104   nsCString mActualType;
00105   PRPackedBool mInSetFocus; // Used in SetFocus(nsPresContext*)
00106 };
00107 
00108 
00109 NS_IMPL_NS_NEW_HTML_ELEMENT_CHECK_PARSER(Object)
00110 
00111 
00112 nsHTMLObjectElement::nsHTMLObjectElement(nsINodeInfo *aNodeInfo,
00113                                          PRBool aFromParser)
00114   : nsGenericHTMLFormElement(aNodeInfo),
00115     mIsDoneAddingChildren(!aFromParser),
00116     mInSetFocus(PR_FALSE)
00117 {
00118 }
00119 
00120 nsHTMLObjectElement::~nsHTMLObjectElement()
00121 {
00122   DestroyImageLoadingContent();
00123 }
00124 
00125 PRBool
00126 nsHTMLObjectElement::IsDoneAddingChildren()
00127 {
00128   return mIsDoneAddingChildren;
00129 }
00130 
00131 void
00132 nsHTMLObjectElement::DoneAddingChildren()
00133 {
00134   mIsDoneAddingChildren = PR_TRUE;
00135   RecreateFrames();
00136 }
00137 
00138 NS_IMPL_ADDREF_INHERITED(nsHTMLObjectElement, nsGenericElement) 
00139 NS_IMPL_RELEASE_INHERITED(nsHTMLObjectElement, nsGenericElement) 
00140 
00141 // QueryInterface implementation for nsHTMLObjectElement
00142 NS_HTML_CONTENT_INTERFACE_MAP_BEGIN(nsHTMLObjectElement,
00143                                     nsGenericHTMLFormElement)
00144   NS_INTERFACE_MAP_ENTRY(nsIDOMHTMLObjectElement)
00145   NS_INTERFACE_MAP_ENTRY(imgIDecoderObserver)
00146   NS_INTERFACE_MAP_ENTRY(imgIDecoderObserver_MOZILLA_1_8_BRANCH)
00147   NS_INTERFACE_MAP_ENTRY(nsIImageLoadingContent)
00148   NS_INTERFACE_MAP_ENTRY(nsIPluginElement)
00149   NS_INTERFACE_MAP_ENTRY_CONTENT_CLASSINFO(HTMLObjectElement)
00150 NS_HTML_CONTENT_INTERFACE_MAP_END
00151 
00152 
00153 NS_IMETHODIMP
00154 nsHTMLObjectElement::SetActualType(const nsACString& aActualType)
00155 {
00156   mActualType = aActualType;
00157 
00158   return NS_OK;
00159 }
00160 
00161 NS_IMETHODIMP
00162 nsHTMLObjectElement::GetActualType(nsACString& aActualType)
00163 {
00164   aActualType = mActualType;
00165 
00166   return NS_OK;
00167 }
00168 
00169 // nsIDOMHTMLObjectElement
00170 
00171 
00172 NS_IMPL_DOM_CLONENODE(nsHTMLObjectElement)
00173 
00174 
00175 NS_IMETHODIMP
00176 nsHTMLObjectElement::GetForm(nsIDOMHTMLFormElement** aForm)
00177 {
00178   return nsGenericHTMLFormElement::GetForm(aForm);
00179 }
00180 
00181 // nsIContent
00182 
00183 void
00184 nsHTMLObjectElement::SetFocus(nsPresContext* aPresContext)
00185 {
00186   if (mInSetFocus) {
00187     NS_WARNING("nsHTMLObjectElement::SetFocus() called recursively on same element");
00188     return;
00189   }
00190   mInSetFocus = PR_TRUE;
00191   nsGenericHTMLFormElement::SetFocus(aPresContext);
00192   mInSetFocus = PR_FALSE;
00193 }
00194 
00195 PRBool
00196 nsHTMLObjectElement::IsFocusable(PRInt32 *aTabIndex)
00197 {
00198   nsIFrame* frame = GetPrimaryFrame(PR_FALSE);
00199   if (frame) {
00200     nsIObjectFrame *objFrame = nsnull;
00201     CallQueryInterface(frame, &objFrame);
00202     if (objFrame) {
00203       nsCOMPtr<nsIPluginInstance> pluginInstance;
00204       objFrame->GetPluginInstance(*getter_AddRefs(pluginInstance));
00205       if (pluginInstance) {
00206         // Has a plugin: let the plugin decide what to do in terms of
00207         // internal focus from mouse clicks
00208         if (aTabIndex) {
00209           GetTabIndex(aTabIndex);
00210          }
00211   
00212         return PR_TRUE;
00213       }
00214     }
00215   }
00216 
00217   return nsGenericHTMLFormElement::IsFocusable(aTabIndex);
00218 }
00219 
00220 
00221 // nsIFormControl
00222 
00223 NS_IMETHODIMP
00224 nsHTMLObjectElement::Reset()
00225 {
00226   return NS_OK;
00227 }
00228 
00229 NS_IMETHODIMP
00230 nsHTMLObjectElement::SubmitNamesValues(nsIFormSubmission* aFormSubmission,
00231                                        nsIContent* aSubmitElement)
00232 {
00233   nsAutoString name;
00234   nsresult rv = GetAttr(kNameSpaceID_None, nsHTMLAtoms::name, name);
00235   if (NS_FAILED(rv)) {
00236     return rv;
00237   }
00238 
00239   if (rv == NS_CONTENT_ATTR_NOT_THERE) {
00240     // No name, don't submit.
00241 
00242     return NS_OK;
00243   }
00244 
00245   nsIFrame* frame = GetPrimaryFrame(PR_FALSE);
00246 
00247   nsIObjectFrame *objFrame = nsnull;
00248   if (frame) {
00249     CallQueryInterface(frame, &objFrame);
00250   }
00251 
00252   if (!objFrame) {
00253     // No frame, nothing to submit.
00254 
00255     return NS_OK;
00256   }
00257 
00258   nsCOMPtr<nsIPluginInstance> pi;
00259   objFrame->GetPluginInstance(*getter_AddRefs(pi));
00260 
00261   nsCOMPtr<nsIPluginInstanceInternal> pi_internal(do_QueryInterface(pi));
00262 
00263   if (!pi_internal) {
00264     // No plugin, nothing to submit.
00265 
00266     return NS_OK;
00267   }
00268 
00269   nsAutoString value;
00270   rv = pi_internal->GetFormValue(value);
00271   NS_ENSURE_SUCCESS(rv, rv);
00272 
00273   return aFormSubmission->AddNameValuePair(this, name, value);
00274 }
00275 
00276 NS_IMETHODIMP
00277 nsHTMLObjectElement::SaveState()
00278 {
00279   return NS_OK;
00280 }
00281 
00282 PRBool
00283 nsHTMLObjectElement::RestoreState(nsPresState* aState)
00284 {
00285   return PR_FALSE;
00286 }
00287 
00288 NS_IMPL_STRING_ATTR(nsHTMLObjectElement, Code, code)
00289 NS_IMPL_STRING_ATTR(nsHTMLObjectElement, Align, align)
00290 NS_IMPL_STRING_ATTR(nsHTMLObjectElement, Archive, archive)
00291 NS_IMPL_STRING_ATTR(nsHTMLObjectElement, Border, border)
00292 NS_IMPL_URI_ATTR(nsHTMLObjectElement, CodeBase, codebase)
00293 NS_IMPL_STRING_ATTR(nsHTMLObjectElement, CodeType, codetype)
00294 NS_IMPL_URI_ATTR(nsHTMLObjectElement, Data, data)
00295 NS_IMPL_BOOL_ATTR(nsHTMLObjectElement, Declare, declare)
00296 NS_IMPL_STRING_ATTR(nsHTMLObjectElement, Height, height)
00297 NS_IMPL_INT_ATTR(nsHTMLObjectElement, Hspace, hspace)
00298 NS_IMPL_STRING_ATTR(nsHTMLObjectElement, Name, name)
00299 NS_IMPL_STRING_ATTR(nsHTMLObjectElement, Standby, standby)
00300 NS_IMPL_INT_ATTR(nsHTMLObjectElement, TabIndex, tabindex)
00301 NS_IMPL_STRING_ATTR(nsHTMLObjectElement, Type, type)
00302 NS_IMPL_STRING_ATTR(nsHTMLObjectElement, UseMap, usemap)
00303 NS_IMPL_INT_ATTR(nsHTMLObjectElement, Vspace, vspace)
00304 NS_IMPL_STRING_ATTR(nsHTMLObjectElement, Width, width)
00305 
00306 NS_IMETHODIMP
00307 nsHTMLObjectElement::GetContentDocument(nsIDOMDocument** aContentDocument)
00308 {
00309   NS_ENSURE_ARG_POINTER(aContentDocument);
00310 
00311   *aContentDocument = nsnull;
00312 
00313   if (!IsInDoc()) {
00314     return NS_OK;
00315   }
00316 
00317   // XXXbz should this use GetCurrentDoc()?  sXBL/XBL2 issue!
00318   nsIDocument *sub_doc = GetOwnerDoc()->GetSubDocumentFor(this);
00319 
00320   if (!sub_doc) {
00321     return NS_OK;
00322   }
00323 
00324   return CallQueryInterface(sub_doc, aContentDocument);
00325 }
00326 
00327 PRBool
00328 nsHTMLObjectElement::ParseAttribute(nsIAtom* aAttribute,
00329                                     const nsAString& aValue,
00330                                     nsAttrValue& aResult)
00331 {
00332   if (aAttribute == nsHTMLAtoms::align) {
00333     return ParseAlignValue(aValue, aResult);
00334   }
00335   if (ParseImageAttribute(aAttribute, aValue, aResult)) {
00336     return PR_TRUE;
00337   }
00338 
00339   return nsGenericHTMLElement::ParseAttribute(aAttribute, aValue, aResult);
00340 }
00341 
00342 static void
00343 MapAttributesIntoRule(const nsMappedAttributes* aAttributes,
00344                       nsRuleData* aData)
00345 {
00346   nsGenericHTMLFormElement::MapImageAlignAttributeInto(aAttributes, aData);
00347   nsGenericHTMLFormElement::MapImageBorderAttributeInto(aAttributes, aData);
00348   nsGenericHTMLFormElement::MapImageMarginAttributeInto(aAttributes, aData);
00349   nsGenericHTMLFormElement::MapImageSizeAttributesInto(aAttributes, aData);
00350   nsGenericHTMLFormElement::MapCommonAttributesInto(aAttributes, aData);
00351 }
00352 
00353 NS_IMETHODIMP_(PRBool)
00354 nsHTMLObjectElement::IsAttributeMapped(const nsIAtom* aAttribute) const
00355 {
00356   static const MappedAttributeEntry* const map[] = {
00357     sCommonAttributeMap,
00358     sImageMarginSizeAttributeMap,
00359     sImageBorderAttributeMap,
00360     sImageAlignAttributeMap,
00361   };
00362 
00363   return FindAttributeDependence(aAttribute, map, NS_ARRAY_LENGTH(map));
00364 }
00365 
00366 
00367 nsMapRuleToAttributesFunc
00368 nsHTMLObjectElement::GetAttributeMappingFunction() const
00369 {
00370   return &MapAttributesIntoRule;
00371 }