Back to index

lightning-sunbird  0.9+nobinonly
nsXFormsChoicesElement.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  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * 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 
00039 #include "nsIXFormsSelectChild.h"
00040 #include "nsXFormsStubElement.h"
00041 #include "nsIDOMHTMLOptGroupElement.h"
00042 #include "nsString.h"
00043 #include "nsCOMPtr.h"
00044 #include "nsIXTFBindableElementWrapper.h"
00045 #include "nsIDOMDocument.h"
00046 #include "nsIDOMNodeList.h"
00047 #include "nsIXFormsControl.h"
00048 #include "nsXFormsUtils.h"
00049 
00050 class nsXFormsChoicesElement : public nsXFormsBindableStub,
00051                                public nsIXFormsSelectChild
00052 {
00053 public:
00054   nsXFormsChoicesElement() : mElement(nsnull), mDoneAddingChildren(PR_FALSE) {}
00055 
00056   NS_DECL_ISUPPORTS_INHERITED
00057 
00058   NS_IMETHOD OnCreated(nsIXTFBindableElementWrapper *aWrapper);
00059 
00060   // nsIXTFElement overrides
00061   NS_IMETHOD ParentChanged(nsIDOMElement *aNewParent);
00062   NS_IMETHOD ChildInserted(nsIDOMNode *aChild, PRUint32 aIndex);
00063   NS_IMETHOD ChildAppended(nsIDOMNode *aChild);
00064   NS_IMETHOD ChildRemoved(PRUint32 aIndex);
00065   NS_IMETHOD BeginAddingChildren();
00066   NS_IMETHOD DoneAddingChildren();
00067 
00068   // nsIXFormsSelectChild
00069   NS_DECL_NSIXFORMSSELECTCHILD
00070 
00071 private:
00072   nsIDOMElement* mElement;
00073   PRBool         mDoneAddingChildren;
00074 
00075   void Refresh();
00076 
00077 };
00078 
00079 NS_IMPL_ISUPPORTS_INHERITED1(nsXFormsChoicesElement,
00080                              nsXFormsBindableStub,
00081                              nsIXFormsSelectChild)
00082 
00083 NS_IMETHODIMP
00084 nsXFormsChoicesElement::OnCreated(nsIXTFBindableElementWrapper *aWrapper)
00085 {
00086   nsresult rv = nsXFormsBindableStub::OnCreated(aWrapper);
00087   NS_ENSURE_SUCCESS(rv, rv);
00088 
00089   aWrapper->SetNotificationMask(nsIXTFElement::NOTIFY_PARENT_CHANGED |
00090                                 nsIXTFElement::NOTIFY_CHILD_INSERTED |
00091                                 nsIXTFElement::NOTIFY_CHILD_APPENDED |
00092                                 nsIXTFElement::NOTIFY_CHILD_REMOVED |
00093                                 nsIXTFElement::NOTIFY_BEGIN_ADDING_CHILDREN);
00094 
00095   nsCOMPtr<nsIDOMElement> node;
00096   aWrapper->GetElementNode(getter_AddRefs(node));
00097 
00098   // It's ok to keep a pointer to mElement.  mElement will have an
00099   // owning reference to this object, so as long as we null out mElement in
00100   // OnDestroyed, it will always be valid.
00101 
00102   mElement = node;
00103   NS_ASSERTION(mElement, "Wrapper is not an nsIDOMElement, we'll crash soon");
00104 
00105   return NS_OK;
00106 }
00107 
00108 NS_IMETHODIMP
00109 nsXFormsChoicesElement::ParentChanged(nsIDOMElement *aNewParent)
00110 {
00111   if (aNewParent) {
00112     Refresh();
00113   }
00114   return NS_OK;
00115 }
00116 
00117 NS_IMETHODIMP
00118 nsXFormsChoicesElement::ChildInserted(nsIDOMNode *aChild, PRUint32 aIndex)
00119 {
00120   nsCOMPtr<nsIXFormsSelectChild> item = do_QueryInterface(aChild);
00121   if (item) {
00122     // XXX get anonymous nodes and insert them at the correct place
00123   } else if (nsXFormsUtils::IsLabelElement(aChild)) {
00124     // If a label child was inserted, we need to clone it into our
00125     // anonymous content.
00126     Refresh();
00127   }
00128     
00129   return NS_OK;
00130 }
00131 
00132 NS_IMETHODIMP
00133 nsXFormsChoicesElement::ChildAppended(nsIDOMNode *aChild)
00134 {
00135   nsCOMPtr<nsIXFormsSelectChild> item = do_QueryInterface(aChild);
00136   if (item) {
00137     // XXX get anonymous nodes and insert them at the correct place
00138   } else if (nsXFormsUtils::IsLabelElement(aChild)) {
00139     // If a label child was inserted, we need to clone it into our
00140     // anonymous content.
00141     Refresh();
00142   }
00143 
00144   return NS_OK;
00145 }
00146 
00147 NS_IMETHODIMP
00148 nsXFormsChoicesElement::ChildRemoved(PRUint32 aIndex)
00149 {
00150   // XXX: only need to Refresh() when a label child is removed, but it's
00151   // not possible to get ahold of the actual removed child at this point.
00152 
00153   Refresh();
00154   return NS_OK;
00155 }
00156 
00157 NS_IMETHODIMP
00158 nsXFormsChoicesElement::BeginAddingChildren()
00159 {
00160   // Suppress child notifications until we're done getting children.
00161   nsCOMPtr<nsIXTFElementWrapper> wrapper = do_QueryInterface(mElement);
00162   NS_ASSERTION(wrapper, "huh? our element must be an xtf wrapper");
00163 
00164   wrapper->SetNotificationMask(nsIXTFElement::NOTIFY_PARENT_CHANGED |
00165                                nsIXTFElement::NOTIFY_DONE_ADDING_CHILDREN);
00166 
00167   return NS_OK;
00168 }
00169 
00170 NS_IMETHODIMP
00171 nsXFormsChoicesElement::DoneAddingChildren()
00172 {
00173   mDoneAddingChildren = PR_TRUE;
00174 
00175   // Unsuppress notifications
00176   nsCOMPtr<nsIXTFElementWrapper> wrapper = do_QueryInterface(mElement);
00177   NS_ASSERTION(wrapper, "huh? our element must be an xtf wrapper");
00178 
00179   wrapper->SetNotificationMask(nsIXTFElement::NOTIFY_PARENT_CHANGED |
00180                                nsIXTFElement::NOTIFY_CHILD_INSERTED |
00181                                nsIXTFElement::NOTIFY_CHILD_APPENDED |
00182                                nsIXTFElement::NOTIFY_CHILD_REMOVED);
00183 
00184   // Assume that we've got something worth refreshing now.
00185   Refresh();
00186   return NS_OK;
00187 }
00188 
00189 // nsIXFormsSelectChild
00190 
00191 NS_IMETHODIMP
00192 nsXFormsChoicesElement::SelectItemByValue(const nsAString &aValue, nsIDOMNode **aSelected)
00193 {
00194   NS_ENSURE_ARG_POINTER(aSelected);
00195   NS_ENSURE_STATE(mElement);
00196   *aSelected = nsnull;
00197   nsCOMPtr<nsIDOMNodeList> children;
00198   nsresult rv = mElement->GetChildNodes(getter_AddRefs(children));
00199   NS_ENSURE_SUCCESS(rv, rv);
00200 
00201   PRUint32 childCount = 0;
00202   children->GetLength(&childCount);
00203 
00204   nsCOMPtr<nsIDOMNode> childNode;
00205   nsCOMPtr<nsIXFormsSelectChild> childItem;
00206 
00207   for (PRUint32 i = 0; i < childCount; ++i) {
00208     children->Item(i, getter_AddRefs(childNode));
00209     childItem = do_QueryInterface(childNode);
00210     if (childItem) {
00211       childItem->SelectItemByValue(aValue, aSelected);
00212       if (*aSelected)
00213         return NS_OK;
00214     }
00215   }
00216   
00217   return NS_OK;
00218 }
00219 
00220 NS_IMETHODIMP
00221 nsXFormsChoicesElement::SelectItemByNode(nsIDOMNode *aNode,
00222                                          nsIDOMNode **aSelected)
00223 {
00224   NS_ENSURE_ARG_POINTER(aSelected);
00225   NS_ENSURE_STATE(mElement);
00226   *aSelected = nsnull;
00227   nsCOMPtr<nsIDOMNodeList> children;
00228   nsresult rv = mElement->GetChildNodes(getter_AddRefs(children));
00229   NS_ENSURE_SUCCESS(rv, rv);
00230 
00231   PRUint32 childCount = 0;
00232   children->GetLength(&childCount);
00233 
00234   nsCOMPtr<nsIDOMNode> childNode;
00235   nsCOMPtr<nsIXFormsSelectChild> childItem;
00236 
00237   for (PRUint32 i = 0; i < childCount; ++i) {
00238     children->Item(i, getter_AddRefs(childNode));
00239     childItem = do_QueryInterface(childNode);
00240     if (childItem) {
00241       childItem->SelectItemByNode(aNode, aSelected);
00242       if (*aSelected)
00243         return NS_OK;
00244     }
00245   }
00246   
00247   return NS_OK;
00248 }
00249 
00250 // internal methods
00251 
00252 void
00253 nsXFormsChoicesElement::Refresh()
00254 {
00255   if (mDoneAddingChildren) {
00256     nsCOMPtr<nsIDOMNode> parent, current;
00257     current = mElement;
00258     do {
00259       current->GetParentNode(getter_AddRefs(parent));
00260       if (nsXFormsUtils::IsXFormsElement(parent, NS_LITERAL_STRING("select1")) ||
00261           nsXFormsUtils::IsXFormsElement(parent, NS_LITERAL_STRING("select"))) {
00262         nsCOMPtr<nsIXFormsControl> select(do_QueryInterface(parent));
00263         if (select) {
00264           select->Refresh();
00265         }
00266         return;
00267       }
00268       current = parent;
00269     } while(current);
00270   }
00271 }
00272 
00273 NS_HIDDEN_(nsresult)
00274 NS_NewXFormsChoicesElement(nsIXTFElement **aResult)
00275 {
00276   *aResult = new nsXFormsChoicesElement();
00277   if (!*aResult)
00278     return NS_ERROR_OUT_OF_MEMORY;
00279 
00280   NS_ADDREF(*aResult);
00281   return NS_OK;
00282 }
00283