Import 3.0 beta 3 tarball
[mozilla-extra.git] / extensions / xforms / nsXFormsOutputElement.cpp
blobb44a915803674ed2860617ea6c709d2bb47b46ce
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 * Novell, Inc.
19 * Portions created by the Initial Developer are Copyright (C) 2004
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * Allan Beaufour <abeaufour@novell.com>
24 * Olli Pettay <Olli.Pettay@helsinki.fi>
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 #ifdef DEBUG
41 //#define DEBUG_XF_OUTPUT
42 #endif
44 #include "nsAutoPtr.h"
45 #include "nsCOMPtr.h"
46 #include "nsMemory.h"
47 #include "nsStringAPI.h"
48 #include "nsIDOM3Node.h"
49 #include "nsIDOMDocument.h"
50 #include "nsIDOMElement.h"
51 #include "nsIDOMEventTarget.h"
52 #include "nsIDOMXPathExpression.h"
53 #include "nsIDOMXPathResult.h"
54 #include "nsISchema.h"
56 #include "nsIModelElementPrivate.h"
57 #include "nsXFormsDelegateStub.h"
58 #include "nsXFormsAtoms.h"
59 #include "nsXFormsUtils.h"
60 #include "nsIXFormsUIWidget.h"
61 #include "nsXFormsModelElement.h"
64 /**
65 * Implementation of the XForms \<output\> element.
67 * \<output\> is not really that different from \<input\>
68 * (ie. nsXFormsDelegateStub), except that it has an "value" attribute that
69 * must be handled separately because it is evaluated to a string result.
71 * @see http://www.w3.org/TR/xforms/slice8.html#ui-output
73 class nsXFormsOutputElement : public nsXFormsDelegateStub
75 public:
76 // nsIXFormsControl
77 NS_IMETHOD Bind(PRBool* aContextChanged);
78 NS_IMETHOD Refresh();
79 NS_IMETHOD GetBoundNode(nsIDOMNode **aBoundNode);
81 // nsIXFormsDelegate
82 NS_IMETHOD GetValue(nsAString& aValue);
83 NS_IMETHOD SetValue(const nsAString& aValue);
84 NS_IMETHOD GetHasBoundNode(PRBool *aHasBoundNode);
86 virtual PRBool IsContentAllowed();
88 nsXFormsOutputElement();
90 private:
91 // The value of the "value" attribute (if any). Updated by Bind()
92 nsString mValue;
94 // Use the "value" attribute
95 PRBool mUseValueAttribute;
98 nsXFormsOutputElement::nsXFormsOutputElement()
99 : nsXFormsDelegateStub(NS_LITERAL_STRING("output")),
100 mUseValueAttribute(PR_FALSE)
104 // nsIXFormsControl
106 nsresult
107 nsXFormsOutputElement::Bind(PRBool *aContextChanged)
109 mValue.SetIsVoid(PR_TRUE);
110 mUseValueAttribute = PR_FALSE;
112 nsresult rv = nsXFormsDelegateStub::Bind(aContextChanged);
113 NS_ENSURE_SUCCESS(rv, rv);
115 if (!mHasParent || !mElement || rv == NS_OK_XFORMS_DEFERRED)
116 return NS_OK;
118 // Besides the standard single node binding (SNB) attributes, \<output\>
119 // also has a "value" attribute, which is used when there are not other SNB
120 // attributes.
121 if (!HasBindingAttribute()) {
122 mUseValueAttribute = PR_TRUE;
123 } else {
124 PRBool hasAttr;
125 rv = mElement->HasAttribute(NS_LITERAL_STRING("ref"), &hasAttr);
126 NS_ENSURE_SUCCESS(rv, rv);
127 if (!hasAttr) {
128 rv = mElement->HasAttribute(NS_LITERAL_STRING("bind"), &hasAttr);
129 NS_ENSURE_SUCCESS(rv, rv);
130 mUseValueAttribute = !hasAttr;
134 if (!mUseValueAttribute)
135 return NS_OK;
137 // Bind to model and set mBoundNode . The bound node is used for setting
138 // context for our children (our parent's context in the case of @value),
139 // and the call to ProcessNodeBinding() will not set it.
140 rv = BindToModel(PR_TRUE);
141 NS_ENSURE_SUCCESS(rv, rv);
143 // Process node binding
144 nsCOMPtr<nsIDOMXPathResult> result;
145 rv = ProcessNodeBinding(NS_LITERAL_STRING("value"),
146 nsIDOMXPathResult::STRING_TYPE,
147 getter_AddRefs(result));
148 NS_ENSURE_SUCCESS(rv, rv);
150 if (rv == NS_OK_XFORMS_DEFERRED) {
151 // Can't leave the output in the model list. Since the xforms processor
152 // is still deferring binds any contextcontrol helping to set the context
153 // for the output's value expression probably isn't in the control list
154 // yet. And if that's the case, we can't have the output in the list
155 // before the contextcontrol or the output won't bind right once the
156 // deferred binds finallyhappen. If we got mBoundNode in addition to
157 // mModel during BindToModel, it isn't likely good, either, so junk it, too.
158 mBoundNode = nsnull;
159 if (mModel) {
160 mModel->RemoveFormControl(this);
161 mModel = nsnull;
164 return NS_OK;
167 if (result) {
168 rv = result->GetStringValue(mValue);
169 NS_ENSURE_SUCCESS(rv, rv);
172 return NS_OK;
175 NS_IMETHODIMP
176 nsXFormsOutputElement::Refresh()
178 return nsXFormsDelegateStub::Refresh();
181 NS_IMETHODIMP
182 nsXFormsOutputElement::GetBoundNode(nsIDOMNode **aBoundNode)
184 return !mUseValueAttribute ? nsXFormsDelegateStub::GetBoundNode(aBoundNode)
185 : NS_OK;
188 NS_IMETHODIMP
189 nsXFormsOutputElement::GetHasBoundNode(PRBool *aHasBoundNode)
191 NS_ENSURE_ARG_POINTER(aHasBoundNode);
192 *aHasBoundNode = (mBoundNode && !mUseValueAttribute) ? PR_TRUE : PR_FALSE;
193 return NS_OK;
196 NS_IMETHODIMP
197 nsXFormsOutputElement::GetValue(nsAString& aValue)
199 if (mUseValueAttribute) {
200 aValue = mValue;
201 return NS_OK;
204 return nsXFormsDelegateStub::GetValue(aValue);
207 NS_IMETHODIMP
208 nsXFormsOutputElement::SetValue(const nsAString& aValue)
210 // Setting the value on an output controls seems wrong semantically.
211 return NS_ERROR_NOT_AVAILABLE;
214 PRBool
215 nsXFormsOutputElement::IsContentAllowed()
217 PRBool isAllowed = PR_TRUE;
219 // Output may not be bound to complexContent.
220 PRBool isComplex = IsContentComplex();
221 if (isComplex) {
222 isAllowed = PR_FALSE;
224 return isAllowed;
227 NS_HIDDEN_(nsresult)
228 NS_NewXFormsOutputElement(nsIXTFElement **aResult)
230 *aResult = new nsXFormsOutputElement();
231 if (!*aResult)
232 return NS_ERROR_OUT_OF_MEMORY;
234 NS_ADDREF(*aResult);
235 return NS_OK;