Back to index

lightning-sunbird  0.9+nobinonly
nsXFormsGroupElement.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  * Novell, Inc.
00019  * Portions created by the Initial Developer are Copyright (C) 2004
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *  Allan Beaufour <abeaufour@novell.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 #include "nsAutoPtr.h"
00039 #include "nsCOMPtr.h"
00040 #include "nsString.h"
00041 
00042 #include "nsIDOM3Node.h"
00043 #include "nsIDOMElement.h"
00044 #include "nsIDOMDocument.h"
00045 #include "nsIDOMNodeList.h"
00046 #include "nsIDOMEventTarget.h"
00047 #include "nsIDOMXPathResult.h"
00048 #include "nsIDOMHTMLDivElement.h"
00049 
00050 #include "nsXFormsControlStub.h"
00051 #include "nsIModelElementPrivate.h"
00052 #include "nsXFormsUtils.h"
00053 #include "nsXFormsDelegateStub.h"
00054 
00055 #ifdef DEBUG
00056 // #define DEBUG_XF_GROUP
00057 #endif
00058 
00071 class nsXFormsGroupElement : public nsXFormsDelegateStub
00072 {
00073 protected:
00075   PRBool TryFocusChildControl(nsIDOMNode *aParent);
00076 
00077 public:
00078   // nsIXFormsControl
00079   NS_IMETHOD TryFocus(PRBool *aOK);
00080 
00081 #ifdef DEBUG_smaug
00082   virtual const char* Name() { return "group"; }
00083 #endif
00084 
00085 };
00086 
00087 PRBool
00088 nsXFormsGroupElement::TryFocusChildControl(nsIDOMNode* aParent)
00089 {
00090   if (!aParent)
00091     return PR_FALSE;
00092 
00093   nsCOMPtr<nsIDOMNodeList> children;
00094   nsresult rv = aParent->GetChildNodes(getter_AddRefs(children));
00095   if (NS_FAILED(rv))
00096     return PR_FALSE;
00097 
00098   PRUint32 childCount = 0;
00099   children->GetLength(&childCount);
00100   nsCOMPtr<nsIDOMNode> child;
00101 
00102   for (PRUint32 i = 0; i < childCount; ++i) {
00103     children->Item(i, getter_AddRefs(child));
00104     nsCOMPtr<nsIXFormsControl> control(do_QueryInterface(child));
00105     PRBool focus = PR_FALSE;
00106     if (control) {
00107       control->TryFocus(&focus);
00108       if (focus)
00109         return PR_TRUE;
00110     }
00111     focus = TryFocusChildControl(child);
00112     if (focus)
00113       return PR_TRUE;
00114   }
00115 
00116   return PR_FALSE;
00117 }
00118 
00119 NS_IMETHODIMP
00120 nsXFormsGroupElement::TryFocus(PRBool* aOK)
00121 {
00122   *aOK = PR_FALSE;
00123   if (GetRelevantState()) {
00124     *aOK = TryFocusChildControl(mElement);
00125   }
00126   return NS_OK;
00127 }
00128 
00129 // Factory
00130 NS_HIDDEN_(nsresult)
00131 NS_NewXFormsGroupElement(nsIXTFElement **aResult)
00132 {
00133   *aResult = new nsXFormsGroupElement();
00134   if (!*aResult)
00135   return NS_ERROR_OUT_OF_MEMORY;
00136 
00137   NS_ADDREF(*aResult);
00138   return NS_OK;
00139 }