Back to index

lightning-sunbird  0.9+nobinonly
nsXFormsDispatchElement.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 XForms support.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Olli Pettay.
00019  * Portions created by the Initial Developer are Copyright (C) 2004
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Olli Pettay <Olli.Pettay@helsinki.fi> (original author)
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nsXFormsActionModuleBase.h"
00040 #include "nsIDOMDocument.h"
00041 #include "nsIDOMDocumentEvent.h"
00042 #include "nsIDOMEvent.h"
00043 #include "nsIDOMElement.h"
00044 #include "nsIDOMEventTarget.h"
00045 #include "nsString.h"
00046 #include "nsXFormsUtils.h"
00047 
00048 class nsXFormsDispatchElement : public nsXFormsActionModuleBase
00049 {
00050 public:
00051   nsXFormsDispatchElement();
00052   nsresult HandleSingleAction(nsIDOMEvent* aEvent,
00053                               nsIXFormsActionElement *aParentAction);
00054 };
00055 
00056 nsXFormsDispatchElement::nsXFormsDispatchElement()
00057 {
00058 }
00059 
00060 nsresult
00061 nsXFormsDispatchElement::HandleSingleAction(nsIDOMEvent* aEvent,
00062                                             nsIXFormsActionElement *aParentAction)
00063 {
00064   nsAutoString name;
00065   mElement->GetAttribute(NS_LITERAL_STRING("name"), name);
00066   if (name.IsEmpty())
00067     return NS_OK;
00068   
00069   nsAutoString target;
00070   mElement->GetAttribute(NS_LITERAL_STRING("target"), target);
00071   if (target.IsEmpty())
00072     return NS_OK;
00073 
00074   PRBool cancelable = PR_TRUE;
00075   PRBool bubbles = PR_TRUE;
00076   if (!nsXFormsUtils::IsXFormsEvent(name, cancelable, bubbles)) {
00077     nsAutoString cancelableStr;
00078     mElement->GetAttribute(NS_LITERAL_STRING("cancelable"), cancelableStr);
00079     cancelable = !(cancelableStr.EqualsLiteral("false") ||
00080                    cancelableStr.EqualsLiteral("0"));
00081     
00082     nsAutoString bubbleStr;
00083     mElement->GetAttribute(NS_LITERAL_STRING("bubbles"), bubbleStr);
00084     bubbles = !(bubbleStr.EqualsLiteral("false") ||
00085                 bubbleStr.EqualsLiteral("0"));
00086 
00087     PRBool tmp;
00088     if (cancelableStr.IsEmpty() && bubbleStr.IsEmpty())
00089       nsXFormsUtils::GetEventDefaults(name, cancelable, bubbles);
00090     else if (cancelableStr.IsEmpty())
00091       nsXFormsUtils::GetEventDefaults(name, cancelable, tmp);
00092     else if (bubbleStr.IsEmpty())
00093       nsXFormsUtils::GetEventDefaults(name, tmp, bubbles);
00094   }
00095 
00096   nsCOMPtr<nsIDOMElement> el;
00097   nsXFormsUtils::GetElementById(target, PR_FALSE, mElement, getter_AddRefs(el));
00098   if (!el)
00099     return NS_OK;
00100 
00101   nsCOMPtr<nsIDOMDocument> doc;
00102   mElement->GetOwnerDocument(getter_AddRefs(doc));
00103   nsCOMPtr<nsIDOMDocumentEvent> docEvent = do_QueryInterface(doc);
00104   if (!docEvent)
00105     return NS_OK;
00106 
00107   nsCOMPtr<nsIDOMEvent> event;
00108   docEvent->CreateEvent(NS_LITERAL_STRING("Events"), getter_AddRefs(event));
00109   event->InitEvent(name, bubbles, cancelable);
00110 
00111   nsCOMPtr<nsIDOMEventTarget> targetEl = do_QueryInterface(el);
00112   if (targetEl) {
00113     nsXFormsUtils::SetEventTrusted(event, el);
00114     PRBool defaultActionEnabled;
00115     targetEl->DispatchEvent(event, &defaultActionEnabled);
00116   }
00117   return NS_OK;
00118 }
00119 
00120 NS_HIDDEN_(nsresult)
00121 NS_NewXFormsDispatchElement(nsIXTFElement **aResult)
00122 {
00123   *aResult = new nsXFormsDispatchElement();
00124   if (!*aResult)
00125     return NS_ERROR_OUT_OF_MEMORY;
00126 
00127   NS_ADDREF(*aResult);
00128   return NS_OK;
00129 }
00130