Back to index

lightning-sunbird  0.9+nobinonly
nsBaseDialog.cpp
Go to the documentation of this file.
00001 /* -*- Mode: c++; tab-width: 2; indent-tabs-mode: nil; -*- */
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.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "nsBaseDialog.h"
00039 #include "nsIDOMEvent.h"
00040 #include "nsIXPBaseWindow.h"
00041 #include "nsCOMPtr.h"
00042 #include "nsIDOMEventTarget.h"
00043 
00044 #include "nsIDOMHTMLInputElement.h"
00045 #include "nsIDOMHTMLDocument.h"
00046 static NS_DEFINE_IID(kIDOMHTMLInputElementIID, NS_IDOMHTMLINPUTELEMENT_IID);
00047 
00048 //-------------------------------------------------------------------------
00049 //
00050 // nsBaseDialog constructor
00051 //
00052 //-------------------------------------------------------------------------
00053 //-----------------------------------------------------------------
00054 nsBaseDialog::nsBaseDialog(nsBrowserWindow * aBrowserWindow) :
00055   mBrowserWindow(aBrowserWindow),
00056   mWindow(nsnull),
00057   mCancelBtn(nsnull)
00058 {
00059 }
00060 
00061 //-----------------------------------------------------------------
00062 nsBaseDialog::~nsBaseDialog()
00063 {
00064   NS_IF_RELEASE(mWindow);
00065 }
00066 
00067 //---------------------------------------------------------------
00068 void nsBaseDialog::Initialize(nsIXPBaseWindow * aWindow) 
00069 {
00070   mWindow = aWindow;
00071   NS_ADDREF(mWindow);
00072 
00073   nsIDOMHTMLDocument *doc = nsnull;
00074   mWindow->GetDocument(doc);
00075   if (nsnull != doc) {
00076     doc->GetElementById(NS_LITERAL_STRING("cancel"), &mCancelBtn);
00077     if (nsnull != mCancelBtn) {
00078       mWindow->AddEventListener(mCancelBtn);
00079     }
00080     NS_RELEASE(doc);
00081   }
00082 }
00083 
00084 
00085 //-----------------------------------------------------------------
00086 void nsBaseDialog::Destroy(nsIXPBaseWindow * aWindow)
00087 {
00088 
00089   // Unregister event listeners that were registered in the
00090   // Initialize here. 
00091   // XXX: Should change code in XPBaseWindow to automatically unregister
00092   // all event listening, That way this code will not be necessary.
00093   if (nsnull != mCancelBtn) {
00094     aWindow->RemoveEventListener(mCancelBtn);
00095   }
00096 }
00097 
00098 //-----------------------------------------------------------------
00099 void nsBaseDialog::MouseClick(nsIDOMEvent* aMouseEvent, nsIXPBaseWindow * aWindow, PRBool &aStatus)
00100 {
00101    // Event Dispatch. This method should not contain
00102    // anything but calls to methods. This idea is that this dispatch
00103    // mechanism may be replaced by JavaScript EventHandlers which call the idl'ed
00104    // interfaces to perform the same operation that is currently being handled by
00105    // this C++ code.
00106 
00107   aStatus = PR_FALSE;
00108 
00109   nsCOMPtr<nsIDOMEventTarget> target;
00110   aMouseEvent->GetTarget(getter_AddRefs(target));
00111   if (target) {
00112     nsCOMPtr<nsIDOMElement> node(do_QueryInterface(target));
00113     if (node.get() == mCancelBtn) {
00114       DoClose();
00115       aStatus = PR_TRUE;
00116     }
00117   }
00118 }
00119 
00120 //---------------------------------------------------------------
00121 void
00122 nsBaseDialog::DoClose()
00123 {
00124   mWindow->SetVisible(PR_FALSE);
00125 }
00126 
00127 
00128 //---------------------------------------------------------------
00129 PRBool 
00130 nsBaseDialog::IsChecked(nsIDOMElement * aNode)
00131 {
00132   nsIDOMHTMLInputElement * element;
00133   if (NS_OK == aNode->QueryInterface(kIDOMHTMLInputElementIID, (void**) &element)) {
00134     PRBool checked;
00135     element->GetChecked(&checked);
00136     NS_RELEASE(element);
00137     return checked;
00138   }
00139   return PR_FALSE;
00140 }
00141 
00142 //-----------------------------------------------------------------
00143 PRBool 
00144 nsBaseDialog::IsChecked(const nsAString & aName)
00145 {
00146   nsIDOMElement      * node;
00147   nsIDOMHTMLDocument * doc = nsnull;
00148   mWindow->GetDocument(doc);
00149   if (nsnull != doc) {
00150     if (NS_OK == doc->GetElementById(aName, &node)) {
00151       PRBool value = IsChecked(node);
00152       NS_RELEASE(node);
00153       NS_RELEASE(doc);
00154       return value;
00155     }
00156     NS_RELEASE(doc);
00157   }
00158   return PR_FALSE;
00159 }
00160 
00161 //---------------------------------------------------------------
00162 void  
00163 nsBaseDialog::SetChecked(nsIDOMElement * aNode, PRBool aValue)
00164 {
00165   nsIDOMHTMLInputElement * element;
00166   if (NS_OK == aNode->QueryInterface(kIDOMHTMLInputElementIID, (void**) &element)) {
00167     element->SetChecked(aValue);
00168     NS_RELEASE(element);
00169   }
00170 }
00171 
00172 //---------------------------------------------------------------
00173 void  
00174 nsBaseDialog::SetChecked(const nsAString & aName, PRBool aValue)
00175 {
00176   nsIDOMElement      * node;
00177   nsIDOMHTMLDocument * doc = nsnull;
00178   mWindow->GetDocument(doc);
00179   if (nsnull != doc) {
00180     if (NS_OK == doc->GetElementById(aName,   &node)) {
00181       SetChecked(node, aValue);
00182       NS_RELEASE(node);
00183     }
00184     NS_RELEASE(doc);
00185   }
00186 }
00187 
00188 //-----------------------------------------------------------------
00189 void nsBaseDialog::GetText(nsIDOMElement * aNode, nsAString & aStr)
00190 {
00191   nsIDOMHTMLInputElement * element;
00192   if (NS_OK == aNode->QueryInterface(kIDOMHTMLInputElementIID, (void**) &element)) {
00193     element->GetValue(aStr);
00194     NS_RELEASE(element);
00195   }
00196 }
00197 
00198 //-----------------------------------------------------------------
00199 void nsBaseDialog::GetText(const nsAString & aName, nsAString & aStr)
00200 {
00201   nsIDOMElement      * node;
00202   nsIDOMHTMLDocument * doc = nsnull;
00203   mWindow->GetDocument(doc);
00204   if (nsnull != doc) {
00205     if (NS_OK == doc->GetElementById(aName,   &node)) {
00206       GetText(node, aStr);
00207       NS_RELEASE(node);
00208     }
00209     NS_RELEASE(doc);
00210   }
00211 }
00212 
00213 //---------------------------------------------------------------
00214 void  
00215 nsBaseDialog::SetText(nsIDOMElement * aNode, const nsAString &aValue)
00216 {
00217   nsIDOMHTMLInputElement * element;
00218   if (NS_OK == aNode->QueryInterface(kIDOMHTMLInputElementIID, (void**) &element)) {
00219     element->SetValue(aValue);
00220     NS_RELEASE(element);
00221   }
00222 }
00223 
00224 
00225 //-----------------------------------------------------------------
00226 void nsBaseDialog::SetText(const nsAString & aName, const nsAString & aStr)
00227 {
00228   nsIDOMElement      * node;
00229   nsIDOMHTMLDocument * doc = nsnull;
00230   mWindow->GetDocument(doc);
00231   if (nsnull != doc) {
00232     if (NS_OK == doc->GetElementById(aName,   &node)) {
00233       SetText(node, aStr);
00234       NS_RELEASE(node);
00235     }
00236     NS_RELEASE(doc);
00237   }
00238 }
00239 
00240 //-----------------------------------------------------------------
00241 float nsBaseDialog::GetFloat(nsString & aStr)
00242 {
00243 
00244   return (float)0.0;
00245 }