Back to index

lightning-sunbird  0.9+nobinonly
nsFindDialog.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 "nsCOMPtr.h"
00039 #include "nsFindDialog.h"
00040 #include "nsIDOMEvent.h"
00041 #include "nsIXPBaseWindow.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 // nsFindDialog constructor
00051 //
00052 //-------------------------------------------------------------------------
00053 //-----------------------------------------------------------------
00054 nsFindDialog::nsFindDialog(nsBrowserWindow * aBrowserWindow) :
00055   nsBaseDialog(aBrowserWindow),
00056   mFindBtn(nsnull),
00057   mSearchDown(PR_TRUE)
00058 {
00059 }
00060 
00061 //-----------------------------------------------------------------
00062 nsFindDialog::~nsFindDialog()
00063 {
00064 }
00065 
00066 //---------------------------------------------------------------
00067 void nsFindDialog::Initialize(nsIXPBaseWindow * aWindow) 
00068 {
00069   nsBaseDialog::Initialize(aWindow);
00070 
00071   nsIDOMHTMLDocument *doc = nsnull;
00072   mWindow->GetDocument(doc);
00073   if (nsnull != doc) {
00074     doc->GetElementById(NS_LITERAL_STRING("find"),       &mFindBtn);
00075     doc->GetElementById(NS_LITERAL_STRING("searchup"),   &mUpRB);
00076     doc->GetElementById(NS_LITERAL_STRING("searchdown"), &mDwnRB);
00077     doc->GetElementById(NS_LITERAL_STRING("matchcase"),  &mMatchCaseCB);
00078 
00079     // XXX: Register event listening on each dom element. We should change this so
00080     // all DOM events are automatically passed through.
00081     aWindow->AddEventListener(mFindBtn);
00082     aWindow->AddEventListener(mUpRB);
00083     aWindow->AddEventListener(mDwnRB);
00084 
00085     SetChecked(mMatchCaseCB, PR_FALSE);
00086     NS_RELEASE(doc);
00087   }
00088 }
00089 
00090 //-----------------------------------------------------------------
00091 void nsFindDialog::MouseClick(nsIDOMEvent* aMouseEvent, nsIXPBaseWindow * aWindow, PRBool &aStatus)
00092 {
00093    // Event Dispatch. This method should not contain
00094    // anything but calls to methods. This idea is that this dispatch
00095    // mechanism may be replaced by JavaScript EventHandlers which call the idl'ed
00096    // interfaces to perform the same operation that is currently being handled by
00097    // this C++ code.
00098 
00099   nsBaseDialog::MouseClick(aMouseEvent, aWindow, aStatus);
00100   if (!aStatus) { // is not consumed
00101     nsCOMPtr<nsIDOMEventTarget> target;
00102     aMouseEvent->GetTarget(getter_AddRefs(target));
00103     if (target) {
00104       nsCOMPtr<nsIDOMElement> node(do_QueryInterface(target));
00105       if (node.get() == mFindBtn) {
00106         DoFind();
00107       }
00108     }
00109   }
00110 }
00111 
00112 //-----------------------------------------------------------------
00113 void nsFindDialog::Destroy(nsIXPBaseWindow * aWindow)
00114 {
00115   // Unregister event listeners that were registered in the
00116   // Initialize here. 
00117   // XXX: Should change code in XPBaseWindow to automatically unregister
00118   // all event listening, That way this code will not be necessary.
00119   aWindow->RemoveEventListener(mFindBtn);
00120 }
00121 
00122 //---------------------------------------------------------------
00123 void
00124 nsFindDialog::DoFind()
00125 {
00126   // Now we have the content tree, lets find the 
00127   // widgets holding the info.
00128 
00129   nsIDOMElement * textNode = nsnull;
00130   nsIDOMHTMLDocument *doc = nsnull;
00131   mWindow->GetDocument(doc);
00132   if (nsnull != doc) {
00133     if (NS_OK == doc->GetElementById(NS_LITERAL_STRING("query"), &textNode)) {
00134       nsIDOMHTMLInputElement * element;
00135       if (NS_OK == textNode->QueryInterface(kIDOMHTMLInputElementIID, (void**) &element)) {
00136         nsString str;
00137         PRBool foundIt = PR_FALSE;
00138         element->GetValue(str);
00139         PRBool searchDown = IsChecked(mDwnRB);
00140         PRBool matchcase  = IsChecked(mMatchCaseCB);
00141 
00142         mBrowserWindow->FindNext(str, matchcase, searchDown, foundIt);
00143         if (foundIt) {
00144           mBrowserWindow->ForceRefresh();
00145         }
00146 
00147         NS_RELEASE(element);
00148       }
00149       NS_RELEASE(textNode);
00150     }
00151     NS_RELEASE(doc);
00152   }
00153 }
00154 
00155 //---------------------------------------------------------------
00156 void
00157 nsFindDialog::DoClose()
00158 {
00159   mWindow->SetVisible(PR_FALSE);
00160 }
00161 
00162