2002-03-17 23:39:16 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
2004-04-18 18:21:17 +04:00
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
2002-03-17 23:39:16 +03:00
|
|
|
*
|
2004-04-18 18:21:17 +04:00
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
2002-03-17 23:39:16 +03:00
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
2004-04-18 18:21:17 +04:00
|
|
|
* The Initial Developer of the Original Code is
|
2002-03-17 23:39:16 +03:00
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
2004-04-18 18:21:17 +04:00
|
|
|
* Charles Manske (cmanske@netscape.com)
|
|
|
|
* Daniel Glazman (glazman@netscape.com)
|
2002-03-17 23:39:16 +03:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
2004-04-18 18:21:17 +04:00
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
2002-03-17 23:39:16 +03:00
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
2004-04-18 18:21:17 +04:00
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
2002-03-17 23:39:16 +03:00
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
2004-04-18 18:21:17 +04:00
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
2002-03-17 23:39:16 +03:00
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
#include "nsHTMLEditorMouseListener.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
|
|
|
|
#include "nsIDOMEvent.h"
|
2003-02-22 11:12:11 +03:00
|
|
|
#include "nsIDOMNSEvent.h"
|
2002-03-17 23:39:16 +03:00
|
|
|
#include "nsIDOMElement.h"
|
|
|
|
#include "nsIDOMMouseEvent.h"
|
|
|
|
#include "nsISelection.h"
|
|
|
|
#include "nsIDOMRange.h"
|
|
|
|
#include "nsIDOMNSRange.h"
|
|
|
|
#include "nsIDOMEventTarget.h"
|
|
|
|
#include "nsIDOMNSUIEvent.h"
|
|
|
|
#include "nsIDOMHTMLTableElement.h"
|
|
|
|
#include "nsIDOMHTMLTableCellElement.h"
|
|
|
|
#include "nsIContent.h"
|
|
|
|
|
|
|
|
#include "nsIEditor.h"
|
|
|
|
#include "nsIHTMLEditor.h"
|
2003-02-24 12:37:18 +03:00
|
|
|
#include "nsIHTMLObjectResizer.h"
|
2003-04-17 09:42:33 +04:00
|
|
|
#include "nsEditProperty.h"
|
2002-03-17 23:39:16 +03:00
|
|
|
#include "nsTextEditUtils.h"
|
2003-07-15 17:47:15 +04:00
|
|
|
#include "nsHTMLEditUtils.h"
|
2003-06-25 12:50:48 +04:00
|
|
|
#include "nsIHTMLInlineTableEditor.h"
|
2002-03-17 23:39:16 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* nsHTMLEditorMouseListener implementation
|
|
|
|
*
|
|
|
|
* The only reason we need this is so a context mouse-click
|
|
|
|
* moves the caret or selects an element as it does for normal click
|
|
|
|
*/
|
|
|
|
|
2003-04-07 18:32:23 +04:00
|
|
|
nsHTMLEditorMouseListener::nsHTMLEditorMouseListener(nsHTMLEditor *aHTMLEditor)
|
|
|
|
: mHTMLEditor(aHTMLEditor)
|
2002-03-17 23:39:16 +03:00
|
|
|
{
|
2003-04-07 18:32:23 +04:00
|
|
|
SetEditor(mHTMLEditor); // Tell the base class about the editor.
|
2002-03-17 23:39:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsHTMLEditorMouseListener::~nsHTMLEditorMouseListener()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS_INHERITED1(nsHTMLEditorMouseListener, nsTextEditorMouseListener, nsIDOMMouseListener)
|
|
|
|
|
2003-02-24 12:37:18 +03:00
|
|
|
nsresult
|
|
|
|
nsHTMLEditorMouseListener::MouseUp(nsIDOMEvent* aMouseEvent)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMMouseEvent> mouseEvent ( do_QueryInterface(aMouseEvent) );
|
|
|
|
if (!mouseEvent) {
|
|
|
|
//non-ui event passed in. bad things.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't do anything special if not an HTML editor
|
|
|
|
nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(mEditor);
|
|
|
|
if (htmlEditor)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMEventTarget> target;
|
|
|
|
nsresult res = aMouseEvent->GetTarget(getter_AddRefs(target));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!target) return NS_ERROR_NULL_POINTER;
|
|
|
|
nsCOMPtr<nsIDOMElement> element = do_QueryInterface(target);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIHTMLObjectResizer> objectResizer = do_QueryInterface(htmlEditor);
|
|
|
|
PRInt32 clientX, clientY;
|
|
|
|
mouseEvent->GetClientX(&clientX);
|
|
|
|
mouseEvent->GetClientY(&clientY);
|
|
|
|
objectResizer->MouseUp(clientX, clientY, element);
|
|
|
|
}
|
2003-07-08 21:38:13 +04:00
|
|
|
|
|
|
|
return nsTextEditorMouseListener::MouseUp(aMouseEvent);
|
2003-02-24 12:37:18 +03:00
|
|
|
}
|
|
|
|
|
2002-03-17 23:39:16 +03:00
|
|
|
nsresult
|
|
|
|
nsHTMLEditorMouseListener::MouseDown(nsIDOMEvent* aMouseEvent)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMMouseEvent> mouseEvent ( do_QueryInterface(aMouseEvent) );
|
|
|
|
if (!mouseEvent) {
|
|
|
|
//non-ui event passed in. bad things.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't do anything special if not an HTML editor
|
|
|
|
nsCOMPtr<nsIHTMLEditor> htmlEditor = do_QueryInterface(mEditor);
|
|
|
|
if (htmlEditor)
|
|
|
|
{
|
|
|
|
// Detect only "context menu" click
|
|
|
|
//XXX This should be easier to do!
|
|
|
|
// But eDOMEvents_contextmenu and NS_CONTEXTMENU is not exposed in any event interface :-(
|
|
|
|
PRUint16 buttonNumber;
|
2003-06-25 12:50:48 +04:00
|
|
|
nsresult res = mouseEvent->GetButton(&buttonNumber);
|
2002-03-17 23:39:16 +03:00
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
PRBool isContextClick;
|
|
|
|
|
|
|
|
#if defined(XP_MAC) || defined(XP_MACOSX)
|
|
|
|
// Ctrl+Click for context menu
|
|
|
|
res = mouseEvent->GetCtrlKey(&isContextClick);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
#else
|
|
|
|
// Right mouse button for Windows, UNIX
|
|
|
|
isContextClick = buttonNumber == 2;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
PRInt32 clickCount;
|
|
|
|
res = mouseEvent->GetDetail(&clickCount);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
2003-02-24 12:37:18 +03:00
|
|
|
nsCOMPtr<nsIDOMEventTarget> target;
|
|
|
|
nsCOMPtr<nsIDOMNSEvent> internalEvent = do_QueryInterface(aMouseEvent);
|
|
|
|
res = internalEvent->GetExplicitOriginalTarget(getter_AddRefs(target));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!target) return NS_ERROR_NULL_POINTER;
|
|
|
|
nsCOMPtr<nsIDOMElement> element = do_QueryInterface(target);
|
|
|
|
|
2002-03-17 23:39:16 +03:00
|
|
|
if (isContextClick || (buttonNumber == 0 && clickCount == 2))
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsISelection> selection;
|
|
|
|
mEditor->GetSelection(getter_AddRefs(selection));
|
|
|
|
if (!selection) return NS_OK;
|
|
|
|
|
|
|
|
// Get location of mouse within target node
|
|
|
|
nsCOMPtr<nsIDOMNSUIEvent> uiEvent = do_QueryInterface(aMouseEvent);
|
|
|
|
if (!uiEvent) return NS_ERROR_FAILURE;
|
|
|
|
|
2003-09-03 17:56:21 +04:00
|
|
|
nsCOMPtr<nsIDOMNode> parent;
|
|
|
|
PRInt32 offset = 0;
|
|
|
|
|
2002-03-17 23:39:16 +03:00
|
|
|
res = uiEvent->GetRangeParent(getter_AddRefs(parent));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!parent) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
res = uiEvent->GetRangeOffset(&offset);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
// Detect if mouse point is within current selection for context click
|
|
|
|
PRBool nodeIsInSelection = PR_FALSE;
|
2002-03-21 05:47:52 +03:00
|
|
|
if (isContextClick)
|
2002-03-17 23:39:16 +03:00
|
|
|
{
|
|
|
|
PRBool isCollapsed;
|
|
|
|
selection->GetIsCollapsed(&isCollapsed);
|
|
|
|
if (!isCollapsed)
|
|
|
|
{
|
|
|
|
PRInt32 rangeCount;
|
|
|
|
res = selection->GetRangeCount(&rangeCount);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
for (PRInt32 i = 0; i < rangeCount; i++)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMRange> range;
|
|
|
|
|
|
|
|
res = selection->GetRangeAt(i, getter_AddRefs(range));
|
|
|
|
if (NS_FAILED(res) || !range)
|
2005-11-25 11:16:51 +03:00
|
|
|
continue;//don't bail yet, iterate through them all
|
2002-03-17 23:39:16 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNSRange> nsrange(do_QueryInterface(range));
|
|
|
|
if (NS_FAILED(res) || !nsrange)
|
2005-11-25 11:16:51 +03:00
|
|
|
continue;//don't bail yet, iterate through them all
|
2002-03-17 23:39:16 +03:00
|
|
|
|
|
|
|
res = nsrange->IsPointInRange(parent, offset, &nodeIsInSelection);
|
|
|
|
|
|
|
|
// Done when we find a range that we are in
|
|
|
|
if (nodeIsInSelection)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIDOMNode> node = do_QueryInterface(target);
|
|
|
|
if (node && !nodeIsInSelection)
|
|
|
|
{
|
2002-03-21 05:47:52 +03:00
|
|
|
if (!element)
|
2002-03-17 23:39:16 +03:00
|
|
|
{
|
2002-11-30 21:59:34 +03:00
|
|
|
if (isContextClick)
|
|
|
|
{
|
|
|
|
// Set the selection to the point under the mouse cursor:
|
|
|
|
selection->Collapse(parent, offset);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Get enclosing link if in text so we can select the link
|
|
|
|
nsCOMPtr<nsIDOMElement> linkElement;
|
|
|
|
res = htmlEditor->GetElementOrParentByTagName(NS_LITERAL_STRING("href"), node, getter_AddRefs(linkElement));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (linkElement)
|
|
|
|
element = linkElement;
|
|
|
|
}
|
2002-03-17 23:39:16 +03:00
|
|
|
}
|
|
|
|
// Select entire element clicked on if NOT within an existing selection
|
|
|
|
// and not the entire body, or table-related elements
|
2002-03-21 05:47:52 +03:00
|
|
|
if (element)
|
2002-03-17 23:39:16 +03:00
|
|
|
{
|
2003-09-03 17:56:21 +04:00
|
|
|
nsCOMPtr<nsIDOMNode> selectAllNode = mHTMLEditor->FindUserSelectAllNode(element);
|
2003-04-07 18:32:23 +04:00
|
|
|
|
2003-09-03 17:56:21 +04:00
|
|
|
if (selectAllNode)
|
2003-04-07 18:32:23 +04:00
|
|
|
{
|
2003-09-03 17:56:21 +04:00
|
|
|
nsCOMPtr<nsIDOMElement> newElement = do_QueryInterface(selectAllNode);
|
|
|
|
if (newElement)
|
2003-04-07 18:32:23 +04:00
|
|
|
{
|
2003-09-03 17:56:21 +04:00
|
|
|
node = selectAllNode;
|
|
|
|
element = newElement;
|
2003-04-07 18:32:23 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-07-15 17:47:15 +04:00
|
|
|
// XXX: should we call nsHTMLEditUtils::IsTableElement here?
|
|
|
|
// that also checks for thead, tbody, tfoot
|
|
|
|
if (nsTextEditUtils::IsBody(node) ||
|
|
|
|
nsHTMLEditUtils::IsTableCellOrCaption(node) ||
|
|
|
|
nsHTMLEditUtils::IsTableRow(node) ||
|
|
|
|
nsHTMLEditUtils::IsTable(node))
|
2002-03-21 05:47:52 +03:00
|
|
|
{
|
|
|
|
// This will place caret just inside table cell or at start of body
|
|
|
|
selection->Collapse(parent, offset);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
htmlEditor->SelectElement(element);
|
|
|
|
}
|
2002-03-17 23:39:16 +03:00
|
|
|
}
|
|
|
|
}
|
2003-02-24 12:37:18 +03:00
|
|
|
// HACK !!! Context click places the caret but the context menu consumes
|
|
|
|
// the event; so we need to check resizing state ourselves
|
2003-06-25 12:50:48 +04:00
|
|
|
htmlEditor->CheckSelectionStateForAnonymousButtons(selection);
|
2003-02-24 12:37:18 +03:00
|
|
|
|
2002-03-21 05:47:52 +03:00
|
|
|
// Prevent bubbling if we changed selection or
|
|
|
|
// for all context clicks
|
|
|
|
if (element || isContextClick)
|
|
|
|
{
|
2005-11-09 19:20:25 +03:00
|
|
|
#ifndef XP_OS2
|
2002-03-21 05:47:52 +03:00
|
|
|
mouseEvent->PreventDefault();
|
2005-11-09 19:20:25 +03:00
|
|
|
#endif
|
2002-03-21 05:47:52 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2002-03-17 23:39:16 +03:00
|
|
|
}
|
2003-04-07 18:32:23 +04:00
|
|
|
else if (!isContextClick && buttonNumber == 0 && clickCount == 1)
|
2003-02-24 12:37:18 +03:00
|
|
|
{
|
|
|
|
// if the target element is an image, we have to display resizers
|
|
|
|
nsCOMPtr<nsIHTMLObjectResizer> objectResizer = do_QueryInterface(htmlEditor);
|
|
|
|
PRInt32 clientX, clientY;
|
|
|
|
mouseEvent->GetClientX(&clientX);
|
|
|
|
mouseEvent->GetClientY(&clientY);
|
|
|
|
objectResizer->MouseDown(clientX, clientY, element);
|
|
|
|
}
|
2002-03-17 23:39:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nsTextEditorMouseListener::MouseDown(aMouseEvent);
|
|
|
|
}
|
|
|
|
|
2003-06-25 12:50:48 +04:00
|
|
|
nsresult
|
|
|
|
nsHTMLEditorMouseListener::MouseClick(nsIDOMEvent* aMouseEvent)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMMouseEvent> mouseEvent ( do_QueryInterface(aMouseEvent) );
|
|
|
|
if (!mouseEvent) {
|
|
|
|
//non-ui event passed in. bad things.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2003-09-03 17:56:21 +04:00
|
|
|
// Don't do anything special if not an HTML inline table editor
|
|
|
|
nsCOMPtr<nsIHTMLInlineTableEditor> inlineTableEditing = do_QueryInterface(mEditor);
|
|
|
|
if (inlineTableEditing)
|
2003-06-25 12:50:48 +04:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMEventTarget> target;
|
|
|
|
nsresult res = aMouseEvent->GetTarget(getter_AddRefs(target));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!target) return NS_ERROR_NULL_POINTER;
|
|
|
|
nsCOMPtr<nsIDOMElement> element = do_QueryInterface(target);
|
|
|
|
|
|
|
|
inlineTableEditing->DoInlineTableEditingAction(element);
|
|
|
|
}
|
2003-07-08 21:38:13 +04:00
|
|
|
|
|
|
|
return nsTextEditorMouseListener::MouseClick(aMouseEvent);
|
2003-06-25 12:50:48 +04:00
|
|
|
}
|
|
|
|
|
2002-03-17 23:39:16 +03:00
|
|
|
nsresult
|
|
|
|
NS_NewHTMLEditorMouseListener(nsIDOMEventListener ** aInstancePtrResult,
|
2003-04-07 18:32:23 +04:00
|
|
|
nsHTMLEditor *aHTMLEditor)
|
2002-03-17 23:39:16 +03:00
|
|
|
{
|
2003-04-07 18:32:23 +04:00
|
|
|
nsHTMLEditorMouseListener* listener = new nsHTMLEditorMouseListener(aHTMLEditor);
|
2002-03-17 23:39:16 +03:00
|
|
|
if (!listener)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
return listener->QueryInterface(NS_GET_IID(nsIDOMEventListener), (void **) aInstancePtrResult);
|
|
|
|
}
|