Back to index

lightning-sunbird  0.9+nobinonly
nsXFormsSelectElement.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  * IBM Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 2004
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *  Brian Ryner <bryner@brianryner.com>
00024  *  Doron Rosenberg <doronr@us.ibm.com>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either the GNU General Public License Version 2 or later (the "GPL"), or
00028  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #include "nsXFormsUtils.h"
00041 #include "nsXFormsControlStub.h"
00042 #include "nsXFormsDelegateStub.h"
00043 #include "nsXFormsAtoms.h"
00044 #include "nsCOMPtr.h"
00045 #include "nsIDOMElement.h"
00046 #include "nsIDOMXPathResult.h"
00047 #include "nsIDOM3Node.h"
00048 #include "nsIDOMDocument.h"
00049 #include "nsIDOMText.h"
00050 #include "nsString.h"
00051 #include "nsIXFormsUIWidget.h"
00052 #include "nsIDocument.h"
00053 #include "nsNetUtil.h"
00054 #include "nsXFormsModelElement.h"
00055 #include "nsXFormsRangeConditionAccessors.h"
00056 #include "nsIEventStateManager.h"
00057 
00058 class nsXFormsSelectElement : public nsXFormsDelegateStub
00059 {
00060 public:
00061   NS_DECL_ISUPPORTS_INHERITED
00062 
00063   NS_IMETHOD OnCreated(nsIXTFBindableElementWrapper *aWrapper);
00064 
00065   // nsIXTFElement overrides
00066   NS_IMETHOD ChildInserted(nsIDOMNode *aChild, PRUint32 aIndex);
00067   NS_IMETHOD ChildAppended(nsIDOMNode *aChild);
00068   NS_IMETHOD ChildRemoved(PRUint32 aIndex);
00069 
00070   // nsIXFormsControl
00071   NS_IMETHOD Refresh();
00072   NS_IMETHOD GetDefaultIntrinsicState(PRInt32 *aState);
00073   NS_IMETHOD GetDisabledIntrinsicState(PRInt32 *aState);
00074 
00075   // nsIXFormsDelegate overrides
00076   NS_IMETHOD GetXFormsAccessors(nsIXFormsAccessors **aAccessor);
00077 
00078   virtual PRBool IsContentAllowed();
00079 
00080 #ifdef DEBUG_smaug
00081   virtual const char* Name() { return "select"; }
00082 #endif
00083 };
00084 
00085 NS_IMPL_ISUPPORTS_INHERITED0(nsXFormsSelectElement,
00086                              nsXFormsDelegateStub)
00087 
00088 NS_IMETHODIMP
00089 nsXFormsSelectElement::OnCreated(nsIXTFBindableElementWrapper *aWrapper)
00090 {
00091   nsresult rv = nsXFormsDelegateStub::OnCreated(aWrapper);
00092   NS_ENSURE_SUCCESS(rv, rv);
00093 
00094   aWrapper->SetNotificationMask(kStandardNotificationMask |
00095                                 nsIXTFElement::NOTIFY_CHILD_INSERTED |
00096                                 nsIXTFElement::NOTIFY_CHILD_APPENDED |
00097                                 nsIXTFElement::NOTIFY_CHILD_REMOVED);
00098   return NS_OK;
00099 }
00100 
00101 // nsIXTFElement overrides
00102 
00103 NS_IMETHODIMP
00104 nsXFormsSelectElement::ChildInserted(nsIDOMNode *aChild, PRUint32 aIndex)
00105 {
00106   Refresh();
00107   return NS_OK;
00108 }
00109 
00110 NS_IMETHODIMP
00111 nsXFormsSelectElement::ChildAppended(nsIDOMNode *aChild)
00112 {
00113   Refresh();
00114   return NS_OK;
00115 }
00116 
00117 NS_IMETHODIMP
00118 nsXFormsSelectElement::ChildRemoved(PRUint32 aIndex)
00119 {
00120   Refresh();
00121   return NS_OK;
00122 }
00123 
00124 // nsIXFormsControl
00125 
00126 NS_IMETHODIMP
00127 nsXFormsSelectElement::Refresh()
00128 {
00129   PRBool delayRefresh = nsXFormsModelElement::ContainerNeedsPostRefresh(this);
00130   if (delayRefresh) {
00131     return NS_OK;
00132   }
00133 
00134   return nsXFormsDelegateStub::Refresh();
00135 }
00136 
00137 NS_IMETHODIMP
00138 nsXFormsSelectElement::GetDefaultIntrinsicState(PRInt32 *aState)
00139 {
00140   NS_ENSURE_ARG_POINTER(aState);
00141   nsXFormsDelegateStub::GetDefaultIntrinsicState(aState);
00142   *aState |= NS_EVENT_STATE_INRANGE;
00143   return NS_OK;
00144 }
00145 
00146 NS_IMETHODIMP
00147 nsXFormsSelectElement::GetDisabledIntrinsicState(PRInt32 *aState)
00148 {
00149   NS_ENSURE_ARG_POINTER(aState);
00150   nsXFormsDelegateStub::GetDisabledIntrinsicState(aState);
00151   *aState |= NS_EVENT_STATE_INRANGE;
00152   return NS_OK;
00153 }
00154 
00155 // nsIXFormsDelegate
00156 
00157 NS_IMETHODIMP
00158 nsXFormsSelectElement::GetXFormsAccessors(nsIXFormsAccessors **aAccessor)
00159 {
00160   if (!mAccessor) {
00161     mAccessor = new nsXFormsRangeConditionAccessors(this, mElement);
00162     if (!mAccessor) {
00163       return NS_ERROR_OUT_OF_MEMORY;
00164     }
00165   }
00166   NS_ADDREF(*aAccessor = mAccessor);
00167   return NS_OK;
00168 }
00169 
00170 PRBool
00171 nsXFormsSelectElement::IsContentAllowed()
00172 {
00173   PRBool isAllowed = PR_TRUE;
00174 
00175   // For select elements, non-simpleContent is only allowed if the select
00176   // element contains an itemset.
00177   PRBool isComplex = IsContentComplex();
00178   if (isComplex && !nsXFormsUtils::NodeHasItemset(mElement)) {
00179     isAllowed = PR_FALSE;
00180   }
00181 
00182   return isAllowed;
00183 }
00184 
00185 NS_HIDDEN_(nsresult)
00186 NS_NewXFormsSelectElement(nsIXTFElement **aResult)
00187 {
00188   *aResult = new nsXFormsSelectElement();
00189   if (!*aResult)
00190     return NS_ERROR_OUT_OF_MEMORY;
00191 
00192   NS_ADDREF(*aResult);
00193   return NS_OK;
00194 }
00195