Back to index

lightning-sunbird  0.9+nobinonly
nsXFormsSelect1Element.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) 2005
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 
00043 #include "nsXFormsUtils.h"
00044 #include "nsXFormsControlStub.h"
00045 #include "nsXFormsDelegateStub.h"
00046 #include "nsXFormsAtoms.h"
00047 #include "nsCOMPtr.h"
00048 #include "nsIDOMElement.h"
00049 #include "nsIDOMXPathResult.h"
00050 #include "nsIDOM3Node.h"
00051 #include "nsIDOMDocument.h"
00052 #include "nsIDOMText.h"
00053 #include "nsString.h"
00054 #include "nsIXFormsUIWidget.h"
00055 #include "nsIDocument.h"
00056 #include "nsNetUtil.h"
00057 #include "nsXFormsModelElement.h"
00058 #include "nsXFormsRangeConditionAccessors.h"
00059 #include "nsIEventStateManager.h"
00060 
00061 class nsXFormsSelect1Element : public nsXFormsDelegateStub
00062 {
00063 public:
00064   NS_DECL_ISUPPORTS_INHERITED
00065 
00066   NS_IMETHOD OnCreated(nsIXTFBindableElementWrapper *aWrapper);
00067 
00068   // nsIXTFElement overrides
00069   NS_IMETHOD ChildInserted(nsIDOMNode *aChild, PRUint32 aIndex);
00070   NS_IMETHOD ChildAppended(nsIDOMNode *aChild);
00071   NS_IMETHOD ChildRemoved(PRUint32 aIndex);
00072   NS_IMETHOD BeginAddingChildren();
00073   NS_IMETHOD DoneAddingChildren();
00074 
00075   // nsIXFormsControl
00076   NS_IMETHOD Refresh();
00077   NS_IMETHOD GetDefaultIntrinsicState(PRInt32 *aState);
00078   NS_IMETHOD GetDisabledIntrinsicState(PRInt32 *aState);
00079 
00080   // nsIXFormsDelegate overrides
00081   NS_IMETHOD GetXFormsAccessors(nsIXFormsAccessors **aAccessor);
00082 
00083   virtual PRBool IsContentAllowed();
00084 
00085   nsXFormsSelect1Element(const nsAString& aType)
00086     : nsXFormsDelegateStub(aType)
00087     {}
00088 protected:
00089   PRBool mAddingChildren;
00090 };
00091 
00092 NS_IMPL_ISUPPORTS_INHERITED0(nsXFormsSelect1Element,
00093                              nsXFormsDelegateStub)
00094 
00095 NS_IMETHODIMP
00096 nsXFormsSelect1Element::OnCreated(nsIXTFBindableElementWrapper *aWrapper)
00097 {
00098   mAddingChildren = PR_FALSE;
00099   nsresult rv = nsXFormsDelegateStub::OnCreated(aWrapper);
00100   NS_ENSURE_SUCCESS(rv, rv);
00101 
00102   aWrapper->SetNotificationMask(kStandardNotificationMask |
00103                                 nsIXTFElement::NOTIFY_CHILD_INSERTED |
00104                                 nsIXTFElement::NOTIFY_CHILD_APPENDED |
00105                                 nsIXTFElement::NOTIFY_CHILD_REMOVED |
00106                                 nsIXTFElement::NOTIFY_BEGIN_ADDING_CHILDREN |
00107                                 nsIXTFElement::NOTIFY_DONE_ADDING_CHILDREN);
00108   return NS_OK;
00109 }
00110 
00111 NS_IMETHODIMP
00112 nsXFormsSelect1Element::BeginAddingChildren()
00113 {
00114   mAddingChildren = PR_TRUE;
00115   return NS_OK;
00116 }
00117 
00118 NS_IMETHODIMP
00119 nsXFormsSelect1Element::DoneAddingChildren()
00120 {
00121   mAddingChildren = PR_FALSE;
00122   return NS_OK;
00123 }
00124 
00125 NS_IMETHODIMP
00126 nsXFormsSelect1Element::ChildInserted(nsIDOMNode *aChild, PRUint32 aIndex)
00127 {
00128   if (!mAddingChildren) {
00129     Refresh();
00130   }
00131   return NS_OK;
00132 }
00133 
00134 NS_IMETHODIMP
00135 nsXFormsSelect1Element::ChildAppended(nsIDOMNode *aChild)
00136 {
00137   if (!mAddingChildren) {
00138     Refresh();
00139   }
00140   return NS_OK;
00141 }
00142 
00143 NS_IMETHODIMP
00144 nsXFormsSelect1Element::ChildRemoved(PRUint32 aIndex)
00145 {
00146   if (!mAddingChildren) {
00147     Refresh();
00148   }
00149   return NS_OK;
00150 }
00151 
00152 // nsIXFormsControl
00153 
00154 NS_IMETHODIMP
00155 nsXFormsSelect1Element::Refresh()
00156 {
00157   PRBool delayRefresh = nsXFormsModelElement::ContainerNeedsPostRefresh(this);
00158   if (delayRefresh) {
00159     return NS_OK;
00160   }
00161 
00162   return nsXFormsDelegateStub::Refresh();
00163 }
00164 
00165 NS_IMETHODIMP
00166 nsXFormsSelect1Element::GetDefaultIntrinsicState(PRInt32 *aState)
00167 {
00168   NS_ENSURE_ARG_POINTER(aState);
00169   nsXFormsDelegateStub::GetDefaultIntrinsicState(aState);
00170   *aState |= NS_EVENT_STATE_INRANGE;
00171   return NS_OK;
00172 }
00173 
00174 NS_IMETHODIMP
00175 nsXFormsSelect1Element::GetDisabledIntrinsicState(PRInt32 *aState)
00176 {
00177   NS_ENSURE_ARG_POINTER(aState);
00178   nsXFormsDelegateStub::GetDisabledIntrinsicState(aState);
00179   *aState |= NS_EVENT_STATE_INRANGE;
00180   return NS_OK;
00181 }
00182 
00183 // nsIXFormsDelegate
00184 
00185 NS_IMETHODIMP
00186 nsXFormsSelect1Element::GetXFormsAccessors(nsIXFormsAccessors **aAccessor)
00187 {
00188   if (!mAccessor) {
00189     mAccessor = new nsXFormsRangeConditionAccessors(this, mElement);
00190     if (!mAccessor) {
00191       return NS_ERROR_OUT_OF_MEMORY;
00192     }
00193   }
00194   NS_ADDREF(*aAccessor = mAccessor);
00195   return NS_OK;
00196 }
00197 
00198 PRBool
00199 nsXFormsSelect1Element::IsContentAllowed()
00200 {
00201   PRBool isAllowed = PR_TRUE;
00202 
00203   // For select1 elements, non-simpleContent is only allowed if the select
00204   // element contains an itemset.
00205   PRBool isComplex = IsContentComplex();
00206   if (isComplex && !nsXFormsUtils::NodeHasItemset(mElement)) {
00207     isAllowed = PR_FALSE;
00208   }
00209 
00210   return isAllowed;
00211 }
00212 
00213 NS_HIDDEN_(nsresult)
00214 NS_NewXFormsSelect1Element(nsIXTFElement **aResult)
00215 {
00216   *aResult = new nsXFormsSelect1Element(NS_LITERAL_STRING("select1"));
00217   if (!*aResult)
00218     return NS_ERROR_OUT_OF_MEMORY;
00219 
00220   NS_ADDREF(*aResult);
00221   return NS_OK;
00222 }