Back to index

lightning-sunbird  0.9+nobinonly
nsXFormsTriggerElement.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 IBM Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 2004
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *  Darin Fisher <darin@meer.net>
00023  *  Allan Beaufour <allan@beaufour.dk>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * 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 "nsIDOM3Node.h"
00040 #include "nsIDOMElement.h"
00041 #include "nsIDOMUIEvent.h"
00042 #include "nsIDOMEventTarget.h"
00043 #include "nsIDOMDocumentEvent.h"
00044 #include "nsIDOMDocument.h"
00045 #include "nsIDOMHTMLElement.h"
00046 #include "nsIDOMHTMLButtonElement.h"
00047 #include "nsIDOMDocumentView.h"
00048 #include "nsIDOMAbstractView.h"
00049 #include "nsCOMPtr.h"
00050 #include "nsString.h"
00051 #include "nsXFormsAtoms.h"
00052 #include "nsXFormsUtils.h"
00053 #include "nsXFormsControlStub.h"
00054 #include "nsIXFormsSubmitElement.h"
00055 #include "nsIXFormsSubmissionElement.h"
00056 #include "nsXFormsDelegateStub.h"
00057 #include "nsIXFormsUIWidget.h"
00058 
00059 NS_HIDDEN_(nsresult)
00060 NS_NewXFormsTriggerElement(nsIXTFElement **aResult)
00061 {
00062   *aResult = new nsXFormsDelegateStub(NS_LITERAL_STRING("trigger"));
00063   if (!*aResult)
00064     return NS_ERROR_OUT_OF_MEMORY;
00065 
00066   NS_ADDREF(*aResult);
00067   return NS_OK;
00068 }
00069 
00070 //-----------------------------------------------------------------------------
00071 
00072 class nsXFormsSubmitElement : public nsXFormsDelegateStub,
00073                               public nsIXFormsSubmitElement
00074 {
00075 public:
00076   NS_DECL_ISUPPORTS_INHERITED
00077   NS_DECL_NSIXFORMSSUBMITELEMENT
00078 
00079   // nsIXTFElement overrides
00080   NS_IMETHOD HandleDefault(nsIDOMEvent *aEvent, PRBool *aHandled);
00081 
00082 #ifdef DEBUG_smaug
00083   virtual const char* Name() { return "submit"; }
00084 #endif
00085 };
00086 
00087 NS_IMPL_ISUPPORTS_INHERITED1(nsXFormsSubmitElement,
00088                              nsXFormsDelegateStub,
00089                              nsIXFormsSubmitElement)
00090 
00091 // nsIXTFElement
00092 
00093 NS_IMETHODIMP
00094 nsXFormsSubmitElement::HandleDefault(nsIDOMEvent *aEvent, PRBool *aHandled)
00095 {
00096   nsresult rv;
00097   
00098   rv = nsXFormsDelegateStub::HandleDefault(aEvent, aHandled);
00099   NS_ENSURE_SUCCESS(rv, rv);
00100   if (*aHandled) {
00101     return NS_OK;
00102   }
00103 
00104   if (!nsXFormsUtils::EventHandlingAllowed(aEvent, mElement))
00105     return NS_OK;
00106 
00107   nsAutoString type;
00108   aEvent->GetType(type);
00109   if (!(*aHandled = type.EqualsLiteral("DOMActivate")))
00110     return NS_OK;
00111 
00112   NS_NAMED_LITERAL_STRING(submission, "submission");
00113   nsAutoString submissionID;
00114   mElement->GetAttribute(submission, submissionID);
00115 
00116   nsCOMPtr<nsIDOMElement> submissionElement;
00117   nsXFormsUtils::GetElementByContextId(mElement, submissionID,
00118                                        getter_AddRefs(submissionElement));
00119 
00120   nsCOMPtr<nsIXFormsSubmissionElement> xfSubmission(do_QueryInterface(submissionElement));
00121 
00122   if (!xfSubmission) {
00123     const PRUnichar *strings[] = { submissionID.get(), submission.get() };
00124     nsXFormsUtils::ReportError(NS_LITERAL_STRING("idRefError"),
00125                                strings, 2, mElement, mElement);
00126     return nsXFormsUtils::DispatchEvent(mElement, eEvent_BindingException);
00127   }
00128 
00129   xfSubmission->SetActivator(this);
00130   nsXFormsUtils::DispatchEvent(submissionElement, eEvent_Submit);
00131 
00132   *aHandled = PR_TRUE;
00133   return NS_OK;
00134 }
00135 
00136 NS_IMETHODIMP
00137 nsXFormsSubmitElement::SetDisabled(PRBool aDisable)
00138 {
00139   nsCOMPtr<nsIXFormsUIWidget> widget(do_QueryInterface(mElement));
00140   if (widget) {
00141     widget->Disable(aDisable);
00142   }
00143 
00144   return NS_OK;
00145 }
00146 
00147 NS_HIDDEN_(nsresult)
00148 NS_NewXFormsSubmitElement(nsIXTFElement **aResult)
00149 {
00150   *aResult = new nsXFormsSubmitElement();
00151   if (!*aResult)
00152     return NS_ERROR_OUT_OF_MEMORY;
00153 
00154   NS_ADDREF(*aResult);
00155   return NS_OK;
00156 }