Back to index

lightning-sunbird  0.9+nobinonly
nsHTMLEditorMouseListener.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; 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.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  *   Charles Manske (cmanske@netscape.com)
00024  *   Daniel Glazman (glazman@netscape.com)
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 #include "nsHTMLEditorMouseListener.h"
00040 #include "nsString.h"
00041 
00042 #include "nsIDOMEvent.h"
00043 #include "nsIDOMNSEvent.h"
00044 #include "nsIDOMElement.h"
00045 #include "nsIDOMMouseEvent.h"
00046 #include "nsISelection.h"
00047 #include "nsIDOMRange.h"
00048 #include "nsIDOMNSRange.h"
00049 #include "nsIDOMEventTarget.h"
00050 #include "nsIDOMNSUIEvent.h"
00051 #include "nsIDOMHTMLTableElement.h"
00052 #include "nsIDOMHTMLTableCellElement.h"
00053 #include "nsIContent.h"
00054 
00055 #include "nsIEditor.h"
00056 #include "nsIHTMLEditor.h"
00057 #include "nsIHTMLObjectResizer.h"
00058 #include "nsEditProperty.h"
00059 #include "nsTextEditUtils.h"
00060 #include "nsHTMLEditUtils.h"
00061 #include "nsIHTMLInlineTableEditor.h"
00062 
00063 /*
00064  * nsHTMLEditorMouseListener implementation
00065  *
00066  * The only reason we need this is so a context mouse-click
00067  *  moves the caret or selects an element as it does for normal click
00068  */
00069 
00070 nsHTMLEditorMouseListener::nsHTMLEditorMouseListener(nsHTMLEditor *aHTMLEditor)
00071   : mHTMLEditor(aHTMLEditor)
00072 {
00073   SetEditor(mHTMLEditor); // Tell the base class about the editor.
00074 }
00075 
00076 nsHTMLEditorMouseListener::~nsHTMLEditorMouseListener() 
00077 {
00078 }
00079 
00080 NS_IMPL_ISUPPORTS_INHERITED1(nsHTMLEditorMouseListener, nsTextEditorMouseListener, nsIDOMMouseListener)
00081 
00082 nsresult
00083 nsHTMLEditorMouseListener::MouseUp(nsIDOMEvent* aMouseEvent)
00084 {
00085   nsCOMPtr<nsIDOMMouseEvent> mouseEvent ( do_QueryInterface(aMouseEvent) );
00086   if (!mouseEvent) {
00087     //non-ui event passed in.  bad things.
00088     return NS_OK;
00089   }
00090 
00091   // Don't do anything special if not an HTML editor
00092   nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(mEditor);
00093   if (htmlEditor)
00094   {
00095     nsCOMPtr<nsIDOMEventTarget> target;
00096     nsresult res = aMouseEvent->GetTarget(getter_AddRefs(target));
00097     if (NS_FAILED(res)) return res;
00098     if (!target) return NS_ERROR_NULL_POINTER;
00099     nsCOMPtr<nsIDOMElement> element = do_QueryInterface(target);
00100 
00101     nsCOMPtr<nsIHTMLObjectResizer> objectResizer = do_QueryInterface(htmlEditor);
00102     PRInt32 clientX, clientY;
00103     mouseEvent->GetClientX(&clientX);
00104     mouseEvent->GetClientY(&clientY);
00105     objectResizer->MouseUp(clientX, clientY, element);
00106   }
00107 
00108   return nsTextEditorMouseListener::MouseUp(aMouseEvent);
00109 }
00110 
00111 nsresult
00112 nsHTMLEditorMouseListener::MouseDown(nsIDOMEvent* aMouseEvent)
00113 {
00114   nsCOMPtr<nsIDOMMouseEvent> mouseEvent ( do_QueryInterface(aMouseEvent) );
00115   if (!mouseEvent) {
00116     //non-ui event passed in.  bad things.
00117     return NS_OK;
00118   }
00119 
00120   // Don't do anything special if not an HTML editor
00121   nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(mEditor);
00122   if (htmlEditor)
00123   {
00124     // Detect only "context menu" click
00125     //XXX This should be easier to do!
00126     // But eDOMEvents_contextmenu and NS_CONTEXTMENU is not exposed in any event interface :-(
00127     PRUint16 buttonNumber;
00128     nsresult res = mouseEvent->GetButton(&buttonNumber);
00129     if (NS_FAILED(res)) return res;
00130 
00131     PRBool isContextClick;
00132 
00133 #if defined(XP_MAC) || defined(XP_MACOSX)
00134     // Ctrl+Click for context menu
00135     res = mouseEvent->GetCtrlKey(&isContextClick);
00136     if (NS_FAILED(res)) return res;
00137 #else
00138     // Right mouse button for Windows, UNIX
00139     isContextClick = buttonNumber == 2;
00140 #endif
00141     
00142     PRInt32 clickCount;
00143     res = mouseEvent->GetDetail(&clickCount);
00144     if (NS_FAILED(res)) return res;
00145 
00146     nsCOMPtr<nsIDOMEventTarget> target;
00147     nsCOMPtr<nsIDOMNSEvent> internalEvent = do_QueryInterface(aMouseEvent);
00148     res = internalEvent->GetExplicitOriginalTarget(getter_AddRefs(target));
00149     if (NS_FAILED(res)) return res;
00150     if (!target) return NS_ERROR_NULL_POINTER;
00151     nsCOMPtr<nsIDOMElement> element = do_QueryInterface(target);
00152 
00153     if (isContextClick || (buttonNumber == 0 && clickCount == 2))
00154     {
00155       nsCOMPtr<nsISelection> selection;
00156       mEditor->GetSelection(getter_AddRefs(selection));
00157       if (!selection) return NS_OK;
00158 
00159       // Get location of mouse within target node
00160       nsCOMPtr<nsIDOMNSUIEvent> uiEvent = do_QueryInterface(aMouseEvent);
00161       if (!uiEvent) return NS_ERROR_FAILURE;
00162 
00163       nsCOMPtr<nsIDOMNode> parent;
00164       PRInt32 offset = 0;
00165 
00166       res = uiEvent->GetRangeParent(getter_AddRefs(parent));
00167       if (NS_FAILED(res)) return res;
00168       if (!parent) return NS_ERROR_FAILURE;
00169 
00170       res = uiEvent->GetRangeOffset(&offset);
00171       if (NS_FAILED(res)) return res;
00172 
00173       // Detect if mouse point is within current selection for context click
00174       PRBool nodeIsInSelection = PR_FALSE;
00175       if (isContextClick)
00176       {
00177         PRBool isCollapsed;
00178         selection->GetIsCollapsed(&isCollapsed);
00179         if (!isCollapsed)
00180         {
00181           PRInt32 rangeCount;
00182           res = selection->GetRangeCount(&rangeCount);
00183           if (NS_FAILED(res)) return res;
00184 
00185           for (PRInt32 i = 0; i < rangeCount; i++)
00186           {
00187             nsCOMPtr<nsIDOMRange> range;
00188 
00189             res = selection->GetRangeAt(i, getter_AddRefs(range));
00190             if (NS_FAILED(res) || !range) 
00191               continue;//dont bail yet, iterate through them all
00192 
00193             nsCOMPtr<nsIDOMNSRange> nsrange(do_QueryInterface(range));
00194             if (NS_FAILED(res) || !nsrange) 
00195               continue;//dont bail yet, iterate through them all
00196 
00197             res = nsrange->IsPointInRange(parent, offset, &nodeIsInSelection);
00198 
00199             // Done when we find a range that we are in
00200             if (nodeIsInSelection)
00201               break;
00202           }
00203         }
00204       }
00205       nsCOMPtr<nsIDOMNode> node = do_QueryInterface(target);
00206       if (node && !nodeIsInSelection)
00207       {
00208         if (!element)
00209         {
00210           if (isContextClick)
00211           {
00212             // Set the selection to the point under the mouse cursor:
00213             selection->Collapse(parent, offset);
00214           }
00215           else
00216           {
00217             // Get enclosing link if in text so we can select the link
00218             nsCOMPtr<nsIDOMElement> linkElement;
00219             res = htmlEditor->GetElementOrParentByTagName(NS_LITERAL_STRING("href"), node, getter_AddRefs(linkElement));
00220             if (NS_FAILED(res)) return res;
00221             if (linkElement)
00222               element = linkElement;
00223           }
00224         }
00225         // Select entire element clicked on if NOT within an existing selection
00226         //   and not the entire body, or table-related elements
00227         if (element)
00228         {
00229           nsCOMPtr<nsIDOMNode> selectAllNode = mHTMLEditor->FindUserSelectAllNode(element);
00230 
00231           if (selectAllNode)
00232           {
00233             nsCOMPtr<nsIDOMElement> newElement = do_QueryInterface(selectAllNode);
00234             if (newElement)
00235             {
00236               node = selectAllNode;
00237               element = newElement;
00238             }
00239           }
00240 
00241 // XXX: should we call nsHTMLEditUtils::IsTableElement here?
00242 // that also checks for thead, tbody, tfoot
00243           if (nsTextEditUtils::IsBody(node) ||
00244               nsHTMLEditUtils::IsTableCellOrCaption(node) ||
00245               nsHTMLEditUtils::IsTableRow(node) ||
00246               nsHTMLEditUtils::IsTable(node))
00247           {
00248             // This will place caret just inside table cell or at start of body
00249             selection->Collapse(parent, offset);
00250           }
00251           else
00252           {
00253             htmlEditor->SelectElement(element);
00254           }
00255         }
00256       }
00257       // HACK !!! Context click places the caret but the context menu consumes
00258       // the event; so we need to check resizing state ourselves
00259       htmlEditor->CheckSelectionStateForAnonymousButtons(selection);
00260 
00261       // Prevent bubbling if we changed selection or 
00262       //   for all context clicks
00263       if (element || isContextClick)
00264       {
00265       #ifndef XP_OS2
00266         mouseEvent->PreventDefault();
00267       #endif
00268         return NS_OK;
00269       }
00270     }
00271     else if (!isContextClick && buttonNumber == 0 && clickCount == 1)
00272     {
00273       // if the target element is an image, we have to display resizers
00274       nsCOMPtr<nsIHTMLObjectResizer> objectResizer = do_QueryInterface(htmlEditor);
00275       PRInt32 clientX, clientY;
00276       mouseEvent->GetClientX(&clientX);
00277       mouseEvent->GetClientY(&clientY);
00278       objectResizer->MouseDown(clientX, clientY, element);
00279     }
00280   }
00281 
00282   return nsTextEditorMouseListener::MouseDown(aMouseEvent);
00283 }
00284 
00285 nsresult
00286 nsHTMLEditorMouseListener::MouseClick(nsIDOMEvent* aMouseEvent)
00287 {
00288   nsCOMPtr<nsIDOMMouseEvent> mouseEvent ( do_QueryInterface(aMouseEvent) );
00289   if (!mouseEvent) {
00290     //non-ui event passed in.  bad things.
00291     return NS_OK;
00292   }
00293 
00294   // Don't do anything special if not an HTML inline table editor
00295   nsCOMPtr<nsIHTMLInlineTableEditor> inlineTableEditing = do_QueryInterface(mEditor);
00296   if (inlineTableEditing)
00297   {
00298     nsCOMPtr<nsIDOMEventTarget> target;
00299     nsresult res = aMouseEvent->GetTarget(getter_AddRefs(target));
00300     if (NS_FAILED(res)) return res;
00301     if (!target) return NS_ERROR_NULL_POINTER;
00302     nsCOMPtr<nsIDOMElement> element = do_QueryInterface(target);
00303 
00304     inlineTableEditing->DoInlineTableEditingAction(element);
00305   }
00306 
00307   return nsTextEditorMouseListener::MouseClick(aMouseEvent);
00308 }
00309 
00310 nsresult
00311 NS_NewHTMLEditorMouseListener(nsIDOMEventListener ** aInstancePtrResult, 
00312                               nsHTMLEditor *aHTMLEditor)
00313 {
00314   nsHTMLEditorMouseListener* listener = new nsHTMLEditorMouseListener(aHTMLEditor);
00315   if (!listener)
00316     return NS_ERROR_OUT_OF_MEMORY;
00317 
00318   return listener->QueryInterface(NS_GET_IID(nsIDOMEventListener), (void **) aInstancePtrResult);   
00319 }