Import 3.0 beta 3 tarball
[mozilla-extra.git] / extensions / xforms / nsXFormsSelectElement.cpp
blob097b21602f6f1def06f144ca83c29fafc7a6e807
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is Mozilla XForms support.
17 * The Initial Developer of the Original Code is
18 * IBM Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 2004
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * Brian Ryner <bryner@brianryner.com>
24 * Doron Rosenberg <doronr@us.ibm.com>
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
40 #include "nsXFormsUtils.h"
41 #include "nsXFormsControlStub.h"
42 #include "nsXFormsDelegateStub.h"
43 #include "nsXFormsAtoms.h"
44 #include "nsCOMPtr.h"
45 #include "nsIDOMElement.h"
46 #include "nsIDOMXPathResult.h"
47 #include "nsIDOM3Node.h"
48 #include "nsIDOMDocument.h"
49 #include "nsIDOMText.h"
50 #include "nsStringAPI.h"
51 #include "nsIXFormsUIWidget.h"
52 #include "nsIDocument.h"
53 #include "nsNetUtil.h"
54 #include "nsXFormsModelElement.h"
55 #include "nsXFormsRangeConditionAccessors.h"
56 #include "nsIEventStateManager.h"
58 class nsXFormsSelectElement : public nsXFormsDelegateStub
60 public:
61 NS_DECL_ISUPPORTS_INHERITED
63 NS_IMETHOD OnCreated(nsIXTFElementWrapper *aWrapper);
65 // nsIXTFElement overrides
66 NS_IMETHOD ChildInserted(nsIDOMNode *aChild, PRUint32 aIndex);
67 NS_IMETHOD ChildAppended(nsIDOMNode *aChild);
68 NS_IMETHOD ChildRemoved(PRUint32 aIndex);
70 // nsIXFormsControl
71 NS_IMETHOD Refresh();
72 NS_IMETHOD GetDefaultIntrinsicState(PRInt32 *aState);
73 NS_IMETHOD GetDisabledIntrinsicState(PRInt32 *aState);
75 // nsIXFormsDelegate overrides
76 NS_IMETHOD GetXFormsAccessors(nsIXFormsAccessors **aAccessor);
78 virtual PRBool IsContentAllowed();
80 #ifdef DEBUG_smaug
81 virtual const char* Name() { return "select"; }
82 #endif
85 NS_IMPL_ISUPPORTS_INHERITED0(nsXFormsSelectElement,
86 nsXFormsDelegateStub)
88 NS_IMETHODIMP
89 nsXFormsSelectElement::OnCreated(nsIXTFElementWrapper *aWrapper)
91 nsresult rv = nsXFormsDelegateStub::OnCreated(aWrapper);
92 NS_ENSURE_SUCCESS(rv, rv);
94 aWrapper->SetNotificationMask(kStandardNotificationMask |
95 nsIXTFElement::NOTIFY_CHILD_INSERTED |
96 nsIXTFElement::NOTIFY_CHILD_APPENDED |
97 nsIXTFElement::NOTIFY_CHILD_REMOVED |
98 nsIXTFElement::NOTIFY_PERFORM_ACCESSKEY);
99 return NS_OK;
102 // nsIXTFElement overrides
104 NS_IMETHODIMP
105 nsXFormsSelectElement::ChildInserted(nsIDOMNode *aChild, PRUint32 aIndex)
107 Refresh();
108 return NS_OK;
111 NS_IMETHODIMP
112 nsXFormsSelectElement::ChildAppended(nsIDOMNode *aChild)
114 Refresh();
115 return NS_OK;
118 NS_IMETHODIMP
119 nsXFormsSelectElement::ChildRemoved(PRUint32 aIndex)
121 Refresh();
122 return NS_OK;
125 // nsIXFormsControl
127 NS_IMETHODIMP
128 nsXFormsSelectElement::Refresh()
130 PRBool delayRefresh = nsXFormsModelElement::ContainerNeedsPostRefresh(this);
131 if (delayRefresh) {
132 return NS_OK;
135 return nsXFormsDelegateStub::Refresh();
138 NS_IMETHODIMP
139 nsXFormsSelectElement::GetDefaultIntrinsicState(PRInt32 *aState)
141 NS_ENSURE_ARG_POINTER(aState);
142 nsXFormsDelegateStub::GetDefaultIntrinsicState(aState);
143 *aState |= NS_EVENT_STATE_INRANGE;
144 return NS_OK;
147 NS_IMETHODIMP
148 nsXFormsSelectElement::GetDisabledIntrinsicState(PRInt32 *aState)
150 NS_ENSURE_ARG_POINTER(aState);
151 nsXFormsDelegateStub::GetDisabledIntrinsicState(aState);
152 *aState |= NS_EVENT_STATE_INRANGE;
153 return NS_OK;
156 // nsIXFormsDelegate
158 NS_IMETHODIMP
159 nsXFormsSelectElement::GetXFormsAccessors(nsIXFormsAccessors **aAccessor)
161 if (!mAccessor) {
162 mAccessor = new nsXFormsRangeConditionAccessors(this, mElement);
163 if (!mAccessor) {
164 return NS_ERROR_OUT_OF_MEMORY;
167 NS_ADDREF(*aAccessor = mAccessor);
168 return NS_OK;
171 PRBool
172 nsXFormsSelectElement::IsContentAllowed()
174 PRBool isAllowed = PR_TRUE;
176 // For select elements, non-simpleContent is only allowed if the select
177 // element contains an itemset.
178 PRBool isComplex = IsContentComplex();
179 if (isComplex && !nsXFormsUtils::NodeHasItemset(mElement)) {
180 isAllowed = PR_FALSE;
183 return isAllowed;
186 NS_HIDDEN_(nsresult)
187 NS_NewXFormsSelectElement(nsIXTFElement **aResult)
189 *aResult = new nsXFormsSelectElement();
190 if (!*aResult)
191 return NS_ERROR_OUT_OF_MEMORY;
193 NS_ADDREF(*aResult);
194 return NS_OK;