Import from 1.9a8 tarball
[mozilla-extra.git] / extensions / xforms / nsXFormsControlStub.h
blob9db377f19fd02501018ae5fa4889bb558748f98c
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 #ifndef nsXFormsControlStub_h_
41 #define nsXFormsControlStub_h_
43 #include "nsCOMPtr.h"
44 #include "nsString.h"
46 #include "nsIDOMElement.h"
47 #include "nsIDOMNode.h"
48 #include "nsIXTFElement.h"
49 #include "nsXFormsAtoms.h"
50 #include "nsIDOMEventListener.h"
52 #include "nsIModelElementPrivate.h"
53 #include "nsIXFormsControl.h"
54 #include "nsIXFormsRepeatElement.h"
55 #include "nsXFormsStubElement.h"
56 #include "nsXFormsUtils.h"
57 #include "nsIXTFElementWrapper.h"
58 #include "nsIXFormsControlBase.h"
60 class nsIDOMEvent;
61 class nsIDOMXPathResult;
63 /**
64 * Common stub for all XForms controls that inherit from nsIXFormsControl and
65 * is bound to an instance node.
67 class nsXFormsControlStub : public nsXFormsStubElement,
68 public nsIXFormsControl
70 public:
72 /** The standard notification flags set on nsIXTFElement */
73 const PRUint32 kStandardNotificationMask;
75 /**
76 * The element flags for the controls passed to
77 * nsXFormsUtils:EvaluateNodeBinding()
79 const PRUint32 kElementFlags;
81 NS_DECL_ISUPPORTS_INHERITED
83 // nsIXFormsControl
84 NS_IMETHOD GetBoundNode(nsIDOMNode **aBoundNode);
85 NS_IMETHOD BindToModel(PRBool aSetBoundNode = PR_FALSE);
86 NS_IMETHOD GetDependencies(nsCOMArray<nsIDOMNode> **aDependencies);
87 NS_IMETHOD GetElement(nsIDOMElement **aElement);
88 NS_IMETHOD ResetBoundNode(const nsString &aBindAttribute,
89 PRUint16 aResultType,
90 PRBool *aContextChanged);
91 NS_IMETHOD Bind(PRBool *aContextChanged);
92 NS_IMETHOD Refresh();
93 NS_IMETHOD GetOnDeferredBindList(PRBool *onList);
94 NS_IMETHOD SetOnDeferredBindList(PRBool putOnList);
95 NS_IMETHOD TryFocus(PRBool* aOK);
96 NS_IMETHOD IsEventTarget(PRBool *aOK);
97 NS_IMETHOD GetUsesModelBinding(PRBool *aRes);
98 NS_IMETHOD GetUsesSingleNodeBinding(PRBool *aRes);
99 NS_IMETHOD GetDefaultIntrinsicState(PRInt32 *aRes);
100 NS_IMETHOD GetDisabledIntrinsicState(PRInt32 *aRes);
101 NS_IMETHOD RebindAndRefresh();
102 NS_IMETHOD GetAbortedBindListContainer(nsIXFormsContextControl **aContainer);
103 NS_IMETHOD SetAbortedBindListContainer(nsIXFormsContextControl *aContainer);
105 // for nsIXTFElement
106 NS_IMETHOD OnCreated(nsIXTFElementWrapper *aWrapper);
107 NS_IMETHOD HandleDefault(nsIDOMEvent *aEvent,
108 PRBool *aHandled);
109 NS_IMETHOD OnDestroyed();
110 NS_IMETHOD WillChangeDocument(nsIDOMDocument *aNewDocument);
111 NS_IMETHOD DocumentChanged(nsIDOMDocument *aNewDocument);
112 NS_IMETHOD WillChangeParent(nsIDOMElement *aNewParent);
113 NS_IMETHOD ParentChanged(nsIDOMElement *aNewParent);
114 NS_IMETHOD WillSetAttribute(nsIAtom *aName, const nsAString &aValue);
115 NS_IMETHOD AttributeSet(nsIAtom *aName, const nsAString &aValue);
116 NS_IMETHOD WillRemoveAttribute(nsIAtom *aName);
117 NS_IMETHOD AttributeRemoved(nsIAtom *aName);
120 * This function manages the mBindAttrsCount value. mBindAttrsCount will
121 * be incremented if a single node binding attribute is specified that isn't
122 * currently on the control AND aValue isn't empty. Otherwise, if the control
123 * already contains this attribute with a non-empty value and aValue is empty,
124 * then mBindAttrsCount is decremented.
126 * aName - Atom of the single node binding attribute ('bind', 'ref', etc.).
127 * Using an atom to make it a tad harder to misuse by passing in
128 * any old string, for instance. Since different controls may have
129 * different binding attrs, we do NO validation on the attr. We
130 * assume that the caller is smart enough to only send us a
131 * binding attr atom.
132 * aValue - The string value that the SNB attribute is being set to.
135 void AddRemoveSNBAttr(nsIAtom *aName, const nsAString &aValue);
138 * This function is overridden by controls that are restricted in the
139 * type of content that may be bound to the control. For example,
140 * xf:input may only be bound to simpleContent.
143 virtual PRBool IsContentAllowed();
146 * This function determines if the content to which the control is being
147 * bound is complex; ie contains Element children.
150 virtual PRBool IsContentComplex();
152 // nsIXFormsContextControl
153 NS_DECL_NSIXFORMSCONTEXTCONTROL
155 #ifdef DEBUG_smaug
156 virtual const char* Name() { return "[undefined]"; }
157 #endif
160 /** Constructor */
161 nsXFormsControlStub() :
162 kStandardNotificationMask(nsIXTFElement::NOTIFY_WILL_SET_ATTRIBUTE |
163 nsIXTFElement::NOTIFY_ATTRIBUTE_SET |
164 nsIXTFElement::NOTIFY_WILL_REMOVE_ATTRIBUTE |
165 nsIXTFElement::NOTIFY_ATTRIBUTE_REMOVED |
166 nsIXTFElement::NOTIFY_WILL_CHANGE_DOCUMENT |
167 nsIXTFElement::NOTIFY_DOCUMENT_CHANGED |
168 nsIXTFElement::NOTIFY_WILL_CHANGE_PARENT |
169 nsIXTFElement::NOTIFY_PARENT_CHANGED |
170 nsIXTFElement::NOTIFY_HANDLE_DEFAULT),
171 kElementFlags(nsXFormsUtils::ELEMENT_WITH_MODEL_ATTR),
172 mPreventLoop(PR_FALSE),
173 mUsesModelBinding(PR_FALSE),
174 mAppearDisabled(PR_FALSE),
175 mOnDeferredBindList(PR_FALSE),
176 mBindAttrsCount(0)
179 protected:
180 /** The nsIXTFElementWrapper */
181 nsIDOMElement* mElement;
183 /** The node that the control is bound to. */
184 nsCOMPtr<nsIDOMNode> mBoundNode;
186 /** Array of nsIDOMNodes that the control depends on. */
187 nsCOMArray<nsIDOMNode> mDependencies;
189 /** The model for the control */
190 nsCOMPtr<nsIModelElementPrivate> mModel;
192 /** This event listener is used to create xforms-hint and xforms-help events. */
193 nsCOMPtr<nsIDOMEventListener> mEventListener;
195 /** State to prevent infinite loop when generating and handling xforms-next
196 * and xforms-previous events
198 PRPackedBool mPreventLoop;
201 * Does the control use a model bind? That is, does it have a @bind.
203 PRPackedBool mUsesModelBinding;
206 * Should the control appear disabled. This is f.x. used when a valid single
207 * node binding is not pointing to an instance data node.
209 PRPackedBool mAppearDisabled;
212 * Indicates whether this control is already on the deferred bind list
214 PRPackedBool mOnDeferredBindList;
216 /**
217 * Used to keep track of whether this control has any single node binding
218 * attributes.
220 PRInt8 mBindAttrsCount;
223 * List of repeats that the node binding depends on. This happens when using
224 * the index() function in the binding expression.
226 nsCOMArray<nsIXFormsRepeatElement> mIndexesUsed;
229 * List of XForms elements contained by this control who tried to bind
230 * and couldn't because this control wasn't ready to bind, yet. This means
231 * that these contained nodes couldn't bind because they could be potentially
232 * using this control's bound node as a context node. When this control
233 * is ready to bind and successfully binds, then this list will be processed.
234 * And in turn, if these controls contain controls on THEIR mAbortedBindList,
235 * then they will be similarly processed.
237 nsCOMArray<nsIXFormsControl> mAbortedBindList;
240 * The control that contains the aborted bind list that this control is on.
241 * A control should never be on more than one list at a time.
243 nsCOMPtr<nsIXFormsContextControl> mAbortedBindListContainer;
246 * Does control have a binding attribute?
248 PRBool HasBindingAttribute() const { return mBindAttrsCount != 0; };
250 /** Returns the relevant state of the control */
251 PRBool GetRelevantState();
254 * Processes the node binding of a control, get the current Model (mModel)
255 * and binds the control to it.
257 * @note Will return NS_OK_XFORMS_DEFERRED if the binding is being
258 * deferred.
260 * @param aBindingAttr The default binding attribute
261 * @param aResultType The XPath result type requested
262 * @param aResult The XPath result
263 * @param aModel The model
265 nsresult
266 ProcessNodeBinding(const nsString &aBindingAttr,
267 PRUint16 aResultType,
268 nsIDOMXPathResult **aResult,
269 nsIModelElementPrivate **aModel = nsnull);
272 * Reset (and reinitialize) the event listener, which is used to create
273 * xforms-hint and xforms-help events.
275 void ResetHelpAndHint(PRBool aInitialize);
278 * Checks whether an attribute is a binding attribute for the control. This
279 * should be overriden by controls that have "non-standard" binding
280 * attributes.
282 * @param aAttr The attribute to check.
284 virtual PRBool IsBindingAttribute(const nsIAtom *aAttr) const;
287 * Causes Bind() and Refresh() to be called if aName is the atom of a
288 * single node binding attribute for this control. Called by AttributeSet
289 * and AttributeRemoved.
291 virtual void AfterSetAttribute(nsIAtom *aName);
294 * Removes this control from its model's list of controls if a single node
295 * binding attribute is removed. Called by WillSetAttribute and
296 * WillRemoveAttribute.
298 * @param aName - atom of the attribute being changed
299 * @param aValue - value that the attribute is being changed to.
301 void BeforeSetAttribute(nsIAtom *aName, const nsAString &aValue);
303 /** Removes the index change event listeners */
304 void RemoveIndexListeners();
307 * Forces detaching from the model.
309 * @param aRebind Try rebinding to a new model?
311 nsresult ForceModelDetach(PRBool aRebind);
314 * Adds the form control to the model, if the model has changed.
316 * @param aOldModel The previous model the control was bound to
317 * @param aParent The parent XForms control
319 nsresult MaybeAddToModel(nsIModelElementPrivate *aOldModel,
320 nsIXFormsControl *aParent);
323 * Returns the nsISchemaBuiltinType of the node to which this element is
324 * bound.
326 nsresult GetBoundBuiltinType(PRUint16 *aBuiltinType);
330 #endif