pjs/layout/generic/nsImageMap.cpp

1086 строки
27 KiB
C++
Исходник Обычный вид История

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
1998-04-14 00:24:54 +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/
1998-04-14 00:24:54 +04: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.
1998-04-14 00:24:54 +04:00
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Mats Palmgren <mats.palmgren@bredband.net>
*
* Alternatively, the contents of this file may be used under the terms of
* 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"),
* 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
* use your version of this file under the terms of the MPL, indicate your
* 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
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "nsImageMap.h"
1998-04-14 00:24:54 +04:00
#include "nsString.h"
#include "nsReadableUtils.h"
1998-04-14 00:24:54 +04:00
#include "nsVoidArray.h"
#include "nsIRenderingContext.h"
#include "nsPresContext.h"
1998-04-14 00:24:54 +04:00
#include "nsIURL.h"
#include "nsIURL.h"
#include "nsIServiceManager.h"
#include "nsNetUtil.h"
1999-01-06 02:02:27 +03:00
#include "nsTextFragment.h"
#include "nsIContent.h"
#include "nsIStyledContent.h"
#include "nsIDOMHTMLElement.h"
#include "nsIDOMHTMLMapElement.h"
#include "nsIDOMHTMLAreaElement.h"
#include "nsIDOMHTMLAnchorElement.h"
#include "nsIDOMHTMLCollection.h"
#include "nsIDocument.h"
#include "nsINameSpaceManager.h"
#include "nsHTMLAtoms.h"
#include "nsIDOMEventReceiver.h"
#include "nsIPresShell.h"
#include "nsIFrame.h"
#include "nsFrameManager.h"
#include "nsIViewManager.h"
#include "nsCoord.h"
#include "nsIImageMap.h"
#include "nsIConsoleService.h"
#include "nsIScriptError.h"
#include "nsIStringBundle.h"
#include "nsIDocument.h"
#include "nsContentUtils.h"
static NS_DEFINE_CID(kCStringBundleServiceCID, NS_STRINGBUNDLESERVICE_CID);
1998-04-14 00:24:54 +04:00
class Area {
public:
Area(nsIContent* aArea);
1998-04-14 00:24:54 +04:00
virtual ~Area();
virtual void ParseCoords(const nsAString& aSpec);
1998-04-14 00:24:54 +04:00
virtual PRBool IsInside(nscoord x, nscoord y) const = 0;
virtual void Draw(nsPresContext* aCX,
1998-04-14 00:24:54 +04:00
nsIRenderingContext& aRC) = 0;
virtual void GetRect(nsPresContext* aCX, nsRect& aRect) = 0;
1998-04-14 00:24:54 +04:00
void HasFocus(PRBool aHasFocus);
1998-04-14 00:24:54 +04:00
void GetHREF(nsAString& aHref) const;
void GetArea(nsIContent** aArea) const;
1998-04-14 00:24:54 +04:00
nsCOMPtr<nsIContent> mArea;
1998-04-14 00:24:54 +04:00
nscoord* mCoords;
PRInt32 mNumCoords;
PRPackedBool mHasFocus;
1998-04-14 00:24:54 +04:00
};
MOZ_DECL_CTOR_COUNTER(Area)
Area::Area(nsIContent* aArea)
: mArea(aArea)
1998-04-14 00:24:54 +04:00
{
MOZ_COUNT_CTOR(Area);
1998-04-14 00:24:54 +04:00
mCoords = nsnull;
mNumCoords = 0;
mHasFocus = PR_FALSE;
1998-04-14 00:24:54 +04:00
}
Area::~Area()
{
MOZ_COUNT_DTOR(Area);
1998-04-14 00:24:54 +04:00
delete [] mCoords;
}
void
Area::GetHREF(nsAString& aHref) const
{
aHref.Truncate();
if (mArea) {
mArea->GetAttr(kNameSpaceID_None, nsHTMLAtoms::href, aHref);
}
}
void
Area::GetArea(nsIContent** aArea) const
{
*aArea = mArea;
NS_IF_ADDREF(*aArea);
}
1998-04-14 00:24:54 +04:00
#include <stdlib.h>
inline PRBool
is_space(char c)
{
return (c == ' ' ||
c == '\f' ||
c == '\n' ||
c == '\r' ||
c == '\t' ||
c == '\v');
}
1998-04-14 00:24:54 +04:00
static void logMessage(nsIContent* aContent,
const nsAString& aCoordsSpec,
PRInt32 aFlags,
const char* aMessageName) {
nsIURI* documentURI = nsnull;
nsIDocument* doc = aContent->GetOwnerDoc();
if (doc) {
documentURI = doc->GetDocumentURI();
}
nsContentUtils::ReportToConsole(
nsContentUtils::eLAYOUT_PROPERTIES,
aMessageName,
nsnull, /* params */
0, /* params length */
documentURI,
PromiseFlatString(NS_LITERAL_STRING("coords=\"") +
aCoordsSpec +
NS_LITERAL_STRING("\"")), /* source line */
0, /* line number */
0, /* column number */
aFlags,
"ImageMap");
}
1998-04-14 00:24:54 +04:00
// XXX straight copy from laymap.c
static nscoord* lo_parse_coord_list(char *str, PRInt32* value_cnt)
{
char *tptr;
char *n_str;
PRInt32 i, cnt;
PRInt32 *value_list;
/*
* Nothing in an empty list
*/
*value_cnt = 0;
if (!str || *str == '\0')
1998-04-14 00:24:54 +04:00
{
return nsnull;
1998-04-14 00:24:54 +04:00
}
/*
* Skip beginning whitespace, all whitespace is empty list.
*/
n_str = str;
while (is_space(*n_str))
1998-04-14 00:24:54 +04:00
{
n_str++;
}
if (*n_str == '\0')
{
return nsnull;
1998-04-14 00:24:54 +04:00
}
/*
* Make a pass where any two numbers separated by just whitespace
* are given a comma separator. Count entries while passing.
*/
cnt = 0;
while (*n_str != '\0')
{
PRBool has_comma;
/*
* Skip to a separator
*/
tptr = n_str;
while (!is_space(*tptr) && *tptr != ',' && *tptr != '\0')
1998-04-14 00:24:54 +04:00
{
tptr++;
}
n_str = tptr;
/*
* If no more entries, break out here
*/
if (*n_str == '\0')
{
break;
}
/*
* Skip to the end of the separator, noting if we have a
* comma.
*/
has_comma = PR_FALSE;
while (is_space(*tptr) || *tptr == ',')
1998-04-14 00:24:54 +04:00
{
if (*tptr == ',')
{
if (has_comma == PR_FALSE)
{
has_comma = PR_TRUE;
}
else
{
break;
}
}
tptr++;
}
/*
* If this was trailing whitespace we skipped, we are done.
*/
if ((*tptr == '\0')&&(has_comma == PR_FALSE))
{
break;
}
/*
* Else if the separator is all whitespace, and this is not the
* end of the string, add a comma to the separator.
*/
else if (has_comma == PR_FALSE)
{
*n_str = ',';
}
/*
* count the entry skipped.
*/
cnt++;
n_str = tptr;
}
/*
* count the last entry in the list.
*/
cnt++;
/*
* Allocate space for the coordinate array.
*/
value_list = new nscoord[cnt];
if (!value_list)
1998-04-14 00:24:54 +04:00
{
return nsnull;
1998-04-14 00:24:54 +04:00
}
/*
* Second pass to copy integer values into list.
*/
tptr = str;
for (i=0; i<cnt; i++)
{
char *ptr;
ptr = strchr(tptr, ',');
if (ptr)
1998-04-14 00:24:54 +04:00
{
*ptr = '\0';
}
/*
* Strip whitespace in front of number because I don't
* trust atoi to do it on all platforms.
*/
while (is_space(*tptr))
1998-04-14 00:24:54 +04:00
{
tptr++;
}
if (*tptr == '\0')
{
value_list[i] = 0;
}
else
{
value_list[i] = (nscoord) ::atoi(tptr);
1998-04-14 00:24:54 +04:00
}
if (ptr)
1998-04-14 00:24:54 +04:00
{
*ptr = ',';
tptr = ptr + 1;
1998-04-14 00:24:54 +04:00
}
}
*value_cnt = cnt;
return value_list;
1998-04-14 00:24:54 +04:00
}
void Area::ParseCoords(const nsAString& aSpec)
1998-04-14 00:24:54 +04:00
{
char* cp = ToNewCString(aSpec);
1999-10-28 18:33:34 +04:00
if (cp) {
mCoords = lo_parse_coord_list(cp, &mNumCoords);
nsCRT::free(cp);
}
1998-04-14 00:24:54 +04:00
}
void Area::HasFocus(PRBool aHasFocus)
{
mHasFocus = aHasFocus;
}
1998-04-14 00:24:54 +04:00
//----------------------------------------------------------------------
class DefaultArea : public Area {
public:
DefaultArea(nsIContent* aArea);
1998-04-14 00:24:54 +04:00
virtual PRBool IsInside(nscoord x, nscoord y) const;
virtual void Draw(nsPresContext* aCX,
1998-04-14 00:24:54 +04:00
nsIRenderingContext& aRC);
virtual void GetRect(nsPresContext* aCX, nsRect& aRect);
1998-04-14 00:24:54 +04:00
};
DefaultArea::DefaultArea(nsIContent* aArea)
: Area(aArea)
1998-04-14 00:24:54 +04:00
{
}
PRBool DefaultArea::IsInside(nscoord x, nscoord y) const
1998-04-14 00:24:54 +04:00
{
return PR_TRUE;
}
void DefaultArea::Draw(nsPresContext* aCX, nsIRenderingContext& aRC)
1998-04-14 00:24:54 +04:00
{
}
void DefaultArea::GetRect(nsPresContext* aCX, nsRect& aRect)
{
}
1998-04-14 00:24:54 +04:00
//----------------------------------------------------------------------
class RectArea : public Area {
public:
RectArea(nsIContent* aArea);
1998-04-14 00:24:54 +04:00
virtual void ParseCoords(const nsAString& aSpec);
virtual PRBool IsInside(nscoord x, nscoord y) const;
virtual void Draw(nsPresContext* aCX,
1998-04-14 00:24:54 +04:00
nsIRenderingContext& aRC);
virtual void GetRect(nsPresContext* aCX, nsRect& aRect);
1998-04-14 00:24:54 +04:00
};
RectArea::RectArea(nsIContent* aArea)
: Area(aArea)
1998-04-14 00:24:54 +04:00
{
}
void RectArea::ParseCoords(const nsAString& aSpec)
{
Area::ParseCoords(aSpec);
PRBool saneRect = PR_TRUE;
PRInt32 flag = nsIScriptError::warningFlag;
if (mNumCoords >= 4) {
if (mCoords[0] > mCoords[2]) {
// x-coords in reversed order
nscoord x = mCoords[2];
mCoords[2] = mCoords[0];
mCoords[0] = x;
saneRect = PR_FALSE;
}
if (mCoords[1] > mCoords[3]) {
// y-coords in reversed order
nscoord y = mCoords[3];
mCoords[3] = mCoords[1];
mCoords[1] = y;
saneRect = PR_FALSE;
}
if (mNumCoords > 4) {
// Someone missed the concept of a rect here
saneRect = PR_FALSE;
}
} else {
saneRect = PR_FALSE;
flag = nsIScriptError::errorFlag;
}
if (!saneRect) {
logMessage(mArea, aSpec, flag, "ImageMapRectBoundsError");
}
}
PRBool RectArea::IsInside(nscoord x, nscoord y) const
1998-04-14 00:24:54 +04:00
{
if (mNumCoords >= 4) { // Note: > is for nav compatabilty
nscoord x1 = mCoords[0];
nscoord y1 = mCoords[1];
nscoord x2 = mCoords[2];
nscoord y2 = mCoords[3];
NS_ASSERTION(x1 <= x2 && y1 <= y2,
"Someone screwed up RectArea::ParseCoords");
1998-04-14 00:24:54 +04:00
if ((x >= x1) && (x <= x2) && (y >= y1) && (y <= y2)) {
return PR_TRUE;
}
}
return PR_FALSE;
}
void RectArea::Draw(nsPresContext* aCX, nsIRenderingContext& aRC)
{
if (mHasFocus) {
if (mNumCoords >= 4) {
float p2t;
p2t = aCX->PixelsToTwips();
nscoord x1 = NSIntPixelsToTwips(mCoords[0], p2t);
nscoord y1 = NSIntPixelsToTwips(mCoords[1], p2t);
nscoord x2 = NSIntPixelsToTwips(mCoords[2], p2t);
nscoord y2 = NSIntPixelsToTwips(mCoords[3], p2t);
NS_ASSERTION(x1 <= x2 && y1 <= y2,
"Someone screwed up RectArea::ParseCoords");
aRC.DrawLine(x1, y1, x1, y2);
aRC.DrawLine(x1, y2, x2, y2);
aRC.DrawLine(x1, y1, x2, y1);
aRC.DrawLine(x2, y1, x2, y2);
}
}
}
void RectArea::GetRect(nsPresContext* aCX, nsRect& aRect)
1998-04-14 00:24:54 +04:00
{
if (mNumCoords >= 4) {
float p2t;
p2t = aCX->PixelsToTwips();
1998-08-01 03:34:45 +04:00
nscoord x1 = NSIntPixelsToTwips(mCoords[0], p2t);
nscoord y1 = NSIntPixelsToTwips(mCoords[1], p2t);
nscoord x2 = NSIntPixelsToTwips(mCoords[2], p2t);
nscoord y2 = NSIntPixelsToTwips(mCoords[3], p2t);
NS_ASSERTION(x1 <= x2 && y1 <= y2,
"Someone screwed up RectArea::ParseCoords");
aRect.SetRect(x1, y1, x2, y2);
1998-04-14 00:24:54 +04:00
}
}
//----------------------------------------------------------------------
class PolyArea : public Area {
public:
PolyArea(nsIContent* aArea);
1998-04-14 00:24:54 +04:00
virtual void ParseCoords(const nsAString& aSpec);
virtual PRBool IsInside(nscoord x, nscoord y) const;
virtual void Draw(nsPresContext* aCX,
1998-04-14 00:24:54 +04:00
nsIRenderingContext& aRC);
virtual void GetRect(nsPresContext* aCX, nsRect& aRect);
1998-04-14 00:24:54 +04:00
};
PolyArea::PolyArea(nsIContent* aArea)
: Area(aArea)
1998-04-14 00:24:54 +04:00
{
}
void PolyArea::ParseCoords(const nsAString& aSpec)
{
Area::ParseCoords(aSpec);
if (mNumCoords >= 2) {
if (mNumCoords & 1U) {
logMessage(mArea,
aSpec,
nsIScriptError::warningFlag,
"ImageMapPolyOddNumberOfCoords");
}
} else {
logMessage(mArea,
aSpec,
nsIScriptError::errorFlag,
"ImageMapPolyWrongNumberOfCoords");
}
}
PRBool PolyArea::IsInside(nscoord x, nscoord y) const
1998-04-14 00:24:54 +04:00
{
if (mNumCoords >= 6) {
PRInt32 intersects = 0;
nscoord wherex = x;
nscoord wherey = y;
PRInt32 totalv = mNumCoords / 2;
PRInt32 totalc = totalv * 2;
nscoord xval = mCoords[totalc - 2];
nscoord yval = mCoords[totalc - 1];
PRInt32 end = totalc;
PRInt32 pointer = 1;
if ((yval >= wherey) != (mCoords[pointer] >= wherey))
if ((xval >= wherex) == (mCoords[0] >= wherex))
intersects += (xval >= wherex) ? 1 : 0;
else
intersects += ((xval - (yval - wherey) *
(mCoords[0] - xval) /
(mCoords[pointer] - yval)) >= wherex) ? 1 : 0;
// XXX I wonder what this is doing; this is a translation of ptinpoly.c
while (pointer < end) {
yval = mCoords[pointer];
pointer += 2;
if (yval >= wherey) {
while((pointer < end) && (mCoords[pointer] >= wherey))
pointer+=2;
if (pointer >= end)
break;
if ((mCoords[pointer-3] >= wherex) ==
(mCoords[pointer-1] >= wherex)) {
intersects += (mCoords[pointer-3] >= wherex) ? 1 : 0;
} else {
intersects +=
((mCoords[pointer-3] - (mCoords[pointer-2] - wherey) *
(mCoords[pointer-1] - mCoords[pointer-3]) /
(mCoords[pointer] - mCoords[pointer - 2])) >= wherex) ? 1:0;
}
} else {
while((pointer < end) && (mCoords[pointer] < wherey))
pointer+=2;
if (pointer >= end)
break;
if ((mCoords[pointer-3] >= wherex) ==
(mCoords[pointer-1] >= wherex)) {
intersects += (mCoords[pointer-3] >= wherex) ? 1:0;
} else {
intersects +=
((mCoords[pointer-3] - (mCoords[pointer-2] - wherey) *
(mCoords[pointer-1] - mCoords[pointer-3]) /
(mCoords[pointer] - mCoords[pointer - 2])) >= wherex) ? 1:0;
}
}
}
if ((intersects & 1) != 0) {
return PR_TRUE;
}
}
return PR_FALSE;
}
void PolyArea::Draw(nsPresContext* aCX, nsIRenderingContext& aRC)
{
if (mHasFocus) {
if (mNumCoords >= 6) {
float p2t;
p2t = aCX->PixelsToTwips();
nscoord x0 = NSIntPixelsToTwips(mCoords[0], p2t);
nscoord y0 = NSIntPixelsToTwips(mCoords[1], p2t);
nscoord x1, y1;
for (PRInt32 i = 2; i < mNumCoords; i += 2) {
x1 = NSIntPixelsToTwips(mCoords[i], p2t);
y1 = NSIntPixelsToTwips(mCoords[i+1], p2t);
aRC.DrawLine(x0, y0, x1, y1);
x0 = x1;
y0 = y1;
}
x1 = NSIntPixelsToTwips(mCoords[0], p2t);
y1 = NSIntPixelsToTwips(mCoords[1], p2t);
aRC.DrawLine(x0, y0, x1, y1);
}
}
}
void PolyArea::GetRect(nsPresContext* aCX, nsRect& aRect)
1998-04-14 00:24:54 +04:00
{
if (mNumCoords >= 6) {
float p2t;
p2t = aCX->PixelsToTwips();
nscoord x1, x2, y1, y2, xtmp, ytmp;
x1 = x2 = NSIntPixelsToTwips(mCoords[0], p2t);
y1 = y2 = NSIntPixelsToTwips(mCoords[1], p2t);
1998-04-14 00:24:54 +04:00
for (PRInt32 i = 2; i < mNumCoords; i += 2) {
xtmp = NSIntPixelsToTwips(mCoords[i], p2t);
ytmp = NSIntPixelsToTwips(mCoords[i+1], p2t);
x1 = x1 < xtmp ? x1 : xtmp;
y1 = y1 < ytmp ? y1 : ytmp;
x2 = x2 > xtmp ? x2 : xtmp;
y2 = y2 > ytmp ? y2 : ytmp;
1998-04-14 00:24:54 +04:00
}
aRect.SetRect(x1, y1, x2, y2);
1998-04-14 00:24:54 +04:00
}
}
//----------------------------------------------------------------------
class CircleArea : public Area {
public:
CircleArea(nsIContent* aArea);
1998-04-14 00:24:54 +04:00
virtual void ParseCoords(const nsAString& aSpec);
virtual PRBool IsInside(nscoord x, nscoord y) const;
virtual void Draw(nsPresContext* aCX,
1998-04-14 00:24:54 +04:00
nsIRenderingContext& aRC);
virtual void GetRect(nsPresContext* aCX, nsRect& aRect);
1998-04-14 00:24:54 +04:00
};
CircleArea::CircleArea(nsIContent* aArea)
: Area(aArea)
1998-04-14 00:24:54 +04:00
{
}
void CircleArea::ParseCoords(const nsAString& aSpec)
{
Area::ParseCoords(aSpec);
PRBool wrongNumberOfCoords = PR_FALSE;
PRInt32 flag = nsIScriptError::warningFlag;
if (mNumCoords >= 3) {
if (mCoords[2] < 0) {
logMessage(mArea,
aSpec,
nsIScriptError::errorFlag,
"ImageMapCircleNegativeRadius");
}
if (mNumCoords > 3) {
wrongNumberOfCoords = PR_TRUE;
}
} else {
wrongNumberOfCoords = PR_TRUE;
flag = nsIScriptError::errorFlag;
}
if (wrongNumberOfCoords) {
logMessage(mArea,
aSpec,
flag,
"ImageMapCircleWrongNumberOfCoords");
}
}
PRBool CircleArea::IsInside(nscoord x, nscoord y) const
1998-04-14 00:24:54 +04:00
{
// Note: > is for nav compatabilty
if (mNumCoords >= 3) {
nscoord x1 = mCoords[0];
nscoord y1 = mCoords[1];
nscoord radius = mCoords[2];
if (radius < 0) {
return PR_FALSE;
}
nscoord dx = x1 - x;
nscoord dy = y1 - y;
nscoord dist = (dx * dx) + (dy * dy);
if (dist <= (radius * radius)) {
return PR_TRUE;
}
}
return PR_FALSE;
}
void CircleArea::Draw(nsPresContext* aCX, nsIRenderingContext& aRC)
{
if (mHasFocus) {
if (mNumCoords >= 3) {
float p2t;
p2t = aCX->PixelsToTwips();
nscoord x1 = NSIntPixelsToTwips(mCoords[0], p2t);
nscoord y1 = NSIntPixelsToTwips(mCoords[1], p2t);
nscoord radius = NSIntPixelsToTwips(mCoords[2], p2t);
if (radius < 0) {
return;
}
nscoord x = x1 - radius;
nscoord y = y1 - radius;
nscoord w = 2 * radius;
aRC.DrawEllipse(x, y, w, w);
}
}
}
void CircleArea::GetRect(nsPresContext* aCX, nsRect& aRect)
1998-04-14 00:24:54 +04:00
{
if (mNumCoords >= 3) {
float p2t;
p2t = aCX->PixelsToTwips();
1998-08-01 03:34:45 +04:00
nscoord x1 = NSIntPixelsToTwips(mCoords[0], p2t);
nscoord y1 = NSIntPixelsToTwips(mCoords[1], p2t);
nscoord radius = NSIntPixelsToTwips(mCoords[2], p2t);
1998-04-14 00:24:54 +04:00
if (radius < 0) {
return;
}
aRect.SetRect(x1 - radius, y1 - radius, x1 + radius, y1 + radius);
1998-04-14 00:24:54 +04:00
}
}
//----------------------------------------------------------------------
nsImageMap::nsImageMap() :
mPresShell(nsnull),
mImageFrame(nsnull),
mDocument(nsnull),
mContainsBlockContents(PR_FALSE)
{
}
1998-04-14 00:24:54 +04:00
nsImageMap::~nsImageMap()
1998-04-14 00:24:54 +04:00
{
NS_ASSERTION(mAreas.Count() == 0, "Destroy was not called");
1998-04-14 00:24:54 +04:00
}
NS_IMPL_ISUPPORTS4(nsImageMap,
nsIDocumentObserver,
nsIDOMFocusListener,
nsIDOMEventListener,
nsIImageMap)
NS_IMETHODIMP
nsImageMap::GetBoundsForAreaContent(nsIContent *aContent,
nsPresContext* aPresContext,
nsRect& aBounds)
{
// Find the Area struct associated with this content node, and return bounds
PRInt32 i, n = mAreas.Count();
for (i = 0; i < n; i++) {
Area* area = (Area*) mAreas.ElementAt(i);
if (area->mArea == aContent) {
area->GetRect(aPresContext, aBounds);
return NS_OK;
}
}
return NS_ERROR_FAILURE;
}
void
nsImageMap::FreeAreas()
1998-04-14 00:24:54 +04:00
{
nsFrameManager *frameManager = mPresShell->FrameManager();
1998-04-14 00:24:54 +04:00
PRInt32 i, n = mAreas.Count();
for (i = 0; i < n; i++) {
Area* area = (Area*) mAreas.ElementAt(i);
frameManager->SetPrimaryFrameFor(area->mArea, nsnull);
nsCOMPtr<nsIContent> areaContent;
area->GetArea(getter_AddRefs(areaContent));
if (areaContent) {
nsCOMPtr<nsIDOMEventReceiver> rec(do_QueryInterface(areaContent));
if (rec) {
rec->RemoveEventListenerByIID(this, NS_GET_IID(nsIDOMFocusListener));
}
}
1998-04-14 00:24:54 +04:00
delete area;
}
mAreas.Clear();
1998-04-14 00:24:54 +04:00
}
nsresult
nsImageMap::Init(nsIPresShell* aPresShell, nsIFrame* aImageFrame, nsIDOMHTMLMapElement* aMap)
1998-04-14 00:24:54 +04:00
{
NS_PRECONDITION(nsnull != aMap, "null ptr");
if (nsnull == aMap) {
return NS_ERROR_NULL_POINTER;
}
mPresShell = aPresShell;
mImageFrame = aImageFrame;
nsresult rv;
mMap = do_QueryInterface(aMap, &rv);
NS_ASSERTION(mMap, "aMap is not an nsIContent!");
mDocument = mMap->GetDocument();
if (mDocument) {
mDocument->AddObserver(this);
}
// "Compile" the areas in the map into faster access versions
rv = UpdateAreas();
return rv;
1998-04-14 00:24:54 +04:00
}
nsresult
nsImageMap::UpdateAreasForBlock(nsIContent* aParent, PRBool* aFoundAnchor)
{
nsresult rv = NS_OK;
PRUint32 i, n = aParent->GetChildCount();
for (i = 0; (i < n) && NS_SUCCEEDED(rv); i++) {
nsIContent *child = aParent->GetChildAt(i);
nsCOMPtr<nsIDOMHTMLAnchorElement> area = do_QueryInterface(child);
if (area) {
*aFoundAnchor = PR_TRUE;
rv = AddArea(child);
}
else {
rv = UpdateAreasForBlock(child, aFoundAnchor);
}
}
return rv;
}
nsresult
nsImageMap::UpdateAreas()
1998-04-14 00:24:54 +04:00
{
// Get rid of old area data
FreeAreas();
PRUint32 i, n = mMap->GetChildCount();
PRBool containsBlock = PR_FALSE, containsArea = PR_FALSE;
for (i = 0; i < n; i++) {
nsIContent *child = mMap->GetChildAt(i);
// Only look at elements and not text, comments, etc.
if (!child->IsContentOfType(nsIContent::eHTML))
continue;
// First check if this map element contains an AREA element.
// If so, we only look for AREA elements
if (!containsBlock) {
nsCOMPtr<nsIDOMHTMLAreaElement> area = do_QueryInterface(child);
if (area) {
containsArea = PR_TRUE;
AddArea(child);
}
}
// If we haven't determined that the map element contains an
// AREA element yet, the look for a block element with children
// that are anchors.
if (!containsArea) {
UpdateAreasForBlock(child, &containsBlock);
if (containsBlock)
mContainsBlockContents = PR_TRUE;
}
}
return NS_OK;
1998-04-14 00:24:54 +04:00
}
nsresult
nsImageMap::AddArea(nsIContent* aArea)
{
nsAutoString shape, coords;
aArea->GetAttr(kNameSpaceID_None, nsHTMLAtoms::shape, shape);
aArea->GetAttr(kNameSpaceID_None, nsHTMLAtoms::coords, coords);
1998-04-14 00:24:54 +04:00
Area* area;
if (shape.IsEmpty() ||
shape.LowerCaseEqualsLiteral("rect") ||
shape.LowerCaseEqualsLiteral("rectangle")) {
area = new RectArea(aArea);
}
else if (shape.LowerCaseEqualsLiteral("poly") ||
shape.LowerCaseEqualsLiteral("polygon")) {
area = new PolyArea(aArea);
}
else if (shape.LowerCaseEqualsLiteral("circle") ||
shape.LowerCaseEqualsLiteral("circ")) {
area = new CircleArea(aArea);
1998-04-14 00:24:54 +04:00
}
else if (shape.LowerCaseEqualsLiteral("default")) {
area = new DefaultArea(aArea);
1998-04-14 00:24:54 +04:00
}
else {
// Unknown area type; bail
return NS_OK;
}
if (!area)
return NS_ERROR_OUT_OF_MEMORY;
//Add focus listener to track area focus changes
nsCOMPtr<nsIDOMEventReceiver> rec(do_QueryInterface(aArea));
if (rec) {
rec->AddEventListenerByIID(this, NS_GET_IID(nsIDOMFocusListener));
}
mPresShell->FrameManager()->SetPrimaryFrameFor(aArea, mImageFrame);
aArea->SetMayHaveFrame(PR_TRUE);
NS_ASSERTION(aArea->MayHaveFrame(), "SetMayHaveFrame failed?");
area->ParseCoords(coords);
1998-04-14 00:24:54 +04:00
mAreas.AppendElement(area);
return NS_OK;
}
PRBool
nsImageMap::IsInside(nscoord aX, nscoord aY,
nsIContent** aContent) const
1998-04-14 00:24:54 +04:00
{
NS_ASSERTION(mMap, "Not initialized");
1998-04-14 00:24:54 +04:00
PRInt32 i, n = mAreas.Count();
for (i = 0; i < n; i++) {
Area* area = (Area*) mAreas.ElementAt(i);
if (area->IsInside(aX, aY)) {
area->GetArea(aContent);
return PR_TRUE;
1998-04-14 00:24:54 +04:00
}
}
return PR_FALSE;
1998-04-14 00:24:54 +04:00
}
PRBool
nsImageMap::IsInside(nscoord aX, nscoord aY) const
1998-04-14 00:24:54 +04:00
{
PRInt32 i, n = mAreas.Count();
for (i = 0; i < n; i++) {
Area* area = (Area*) mAreas.ElementAt(i);
if (area->IsInside(aX, aY)) {
nsAutoString href;
area->GetHREF(href);
if (!href.IsEmpty()) {
return PR_TRUE;
}
else {
//We need to return here so we don't hit an overlapping map area
return PR_FALSE;
}
1998-04-14 00:24:54 +04:00
}
}
return PR_FALSE;
1998-04-14 00:24:54 +04:00
}
void
nsImageMap::Draw(nsPresContext* aCX, nsIRenderingContext& aRC)
1998-04-14 00:24:54 +04:00
{
PRInt32 i, n = mAreas.Count();
for (i = 0; i < n; i++) {
Area* area = (Area*) mAreas.ElementAt(i);
area->Draw(aCX, aRC);
}
}
void
nsImageMap::MaybeUpdateAreas(nsIContent *aContent)
{
if (aContent && (aContent == mMap ||
(mContainsBlockContents &&
nsContentUtils::ContentIsDescendantOf(aContent, mMap)))) {
UpdateAreas();
}
}
void
nsImageMap::AttributeChanged(nsIDocument* aDocument,
nsIStyledContent* aContent,
PRInt32 aNameSpaceID,
nsIAtom* aAttribute,
PRInt32 aModType)
{
// If the parent of the changing content node is our map then update
// the map. But only do this if the node is an HTML <area> or <a>
// and the attribute that's changing is "shape" or "coords" -- those
// are the only cases we care about.
if (aContent->IsContentOfType(nsIContent::eHTML) &&
(aContent->NodeInfo()->Equals(nsHTMLAtoms::area) ||
aContent->NodeInfo()->Equals(nsHTMLAtoms::a)) &&
aNameSpaceID == kNameSpaceID_None &&
(aAttribute == nsHTMLAtoms::shape ||
aAttribute == nsHTMLAtoms::coords)) {
MaybeUpdateAreas(aContent->GetParent());
}
}
void
nsImageMap::ContentAppended(nsIDocument *aDocument,
nsIContent* aContainer,
PRInt32 aNewIndexInContainer)
{
MaybeUpdateAreas(aContainer);
}
void
nsImageMap::ContentInserted(nsIDocument *aDocument,
nsIContent* aContainer,
nsIContent* aChild,
PRInt32 aIndexInContainer)
{
MaybeUpdateAreas(aContainer);
}
void
nsImageMap::ContentRemoved(nsIDocument *aDocument,
nsIContent* aContainer,
nsIContent* aChild,
PRInt32 aIndexInContainer)
{
MaybeUpdateAreas(aContainer);
}
nsresult
nsImageMap::Focus(nsIDOMEvent* aEvent)
{
return ChangeFocus(aEvent, PR_TRUE);
}
nsresult
nsImageMap::Blur(nsIDOMEvent* aEvent)
{
return ChangeFocus(aEvent, PR_FALSE);
}
nsresult
nsImageMap::ChangeFocus(nsIDOMEvent* aEvent, PRBool aFocus) {
//Set which one of our areas changed focus
nsCOMPtr<nsIDOMEventTarget> target;
if (NS_SUCCEEDED(aEvent->GetTarget(getter_AddRefs(target))) && target) {
nsCOMPtr<nsIContent> targetContent(do_QueryInterface(target));
if (targetContent) {
PRInt32 i, n = mAreas.Count();
for (i = 0; i < n; i++) {
Area* area = (Area*) mAreas.ElementAt(i);
nsCOMPtr<nsIContent> areaContent;
area->GetArea(getter_AddRefs(areaContent));
if (areaContent) {
if (areaContent.get() == targetContent.get()) {
//Set or Remove internal focus
area->HasFocus(aFocus);
//Now invalidate the rect
nsCOMPtr<nsIDocument> doc = targetContent->GetDocument();
//This check is necessary to see if we're still attached to the doc
if (doc) {
nsIPresShell *presShell = doc->GetShellAt(0);
if (presShell) {
nsIFrame* imgFrame = presShell->GetPrimaryFrameFor(targetContent);
if (imgFrame) {
nsPresContext *presContext = presShell->GetPresContext();
if (presContext) {
nsRect dmgRect;
area->GetRect(presContext, dmgRect);
imgFrame->Invalidate(dmgRect, PR_TRUE);
}
}
}
}
}
}
}
}
}
return NS_OK;
}
nsresult
nsImageMap::HandleEvent(nsIDOMEvent* aEvent)
{
return NS_OK;
}
void
nsImageMap::Destroy(void)
{
FreeAreas();
if (mDocument) {
mDocument->RemoveObserver(this);
}
}