2007-04-22 05:42:17 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-04-22 05:42:17 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Retrieves and displays icons in native menu items on Mac OS X.
|
|
|
|
*/
|
|
|
|
|
2012-11-16 22:24:58 +04:00
|
|
|
/* exception_defines.h defines 'try' to 'if (true)' which breaks objective-c
|
|
|
|
exceptions and produces errors like: error: unexpected '@' in program'.
|
|
|
|
If we define __EXCEPTIONS exception_defines.h will avoid doing this.
|
|
|
|
|
|
|
|
See bug 666609 for more information.
|
2007-04-22 05:42:17 +04:00
|
|
|
|
2012-11-16 22:24:58 +04:00
|
|
|
We use <limits> to get the libstdc++ version. */
|
|
|
|
#include <limits>
|
|
|
|
#if __GLIBCXX__ <= 20070719
|
|
|
|
#define __EXCEPTIONS
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "nsMenuItemIconX.h"
|
2008-02-21 02:47:05 +03:00
|
|
|
#include "nsObjCExceptions.h"
|
2007-04-22 05:42:17 +04:00
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsIDocument.h"
|
2014-02-28 03:04:46 +04:00
|
|
|
#include "nsNameSpaceManager.h"
|
2011-10-14 22:11:22 +04:00
|
|
|
#include "nsGkAtoms.h"
|
2007-04-22 05:42:17 +04:00
|
|
|
#include "nsIDOMElement.h"
|
|
|
|
#include "nsIDOMCSSStyleDeclaration.h"
|
|
|
|
#include "nsIDOMCSSValue.h"
|
|
|
|
#include "nsIDOMCSSPrimitiveValue.h"
|
2009-12-12 01:09:11 +03:00
|
|
|
#include "nsIDOMRect.h"
|
2007-04-22 05:42:17 +04:00
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "nsToolkit.h"
|
|
|
|
#include "nsNetUtil.h"
|
2012-11-16 22:24:58 +04:00
|
|
|
#include "imgLoader.h"
|
2012-10-12 16:43:01 +04:00
|
|
|
#include "imgRequestProxy.h"
|
2008-06-28 11:55:30 +04:00
|
|
|
#include "nsMenuItemX.h"
|
2014-03-08 05:21:24 +04:00
|
|
|
#include "gfxPlatform.h"
|
Bug 753 - Remove nsIImage, gfxIImageFrame, and their implementations, and expose an equivalent api on imgIContainer. r=roc,josh,bz,longsonr,vlad,karlt,jimm,bsmedberg,mfinkle,peterw,peterv sr=vlad,roc
--HG--
rename : gfx/src/shared/gfxImageFrame.cpp => modules/libpr0n/src/imgFrame.cpp
rename : gfx/src/shared/gfxImageFrame.h => modules/libpr0n/src/imgFrame.h
2009-07-21 05:50:15 +04:00
|
|
|
#include "imgIContainer.h"
|
2010-03-13 17:39:55 +03:00
|
|
|
#include "nsCocoaUtils.h"
|
2012-06-26 08:20:12 +04:00
|
|
|
#include "nsContentUtils.h"
|
2007-04-22 05:42:17 +04:00
|
|
|
|
2014-03-08 05:21:24 +04:00
|
|
|
using mozilla::gfx::SourceSurface;
|
2014-02-21 23:07:42 +04:00
|
|
|
using mozilla::RefPtr;
|
2014-03-08 05:21:24 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
static const uint32_t kIconWidth = 16;
|
|
|
|
static const uint32_t kIconHeight = 16;
|
|
|
|
static const uint32_t kIconBitsPerComponent = 8;
|
|
|
|
static const uint32_t kIconComponents = 4;
|
|
|
|
static const uint32_t kIconBitsPerPixel = kIconBitsPerComponent *
|
2007-04-22 05:42:17 +04:00
|
|
|
kIconComponents;
|
2012-08-22 19:56:38 +04:00
|
|
|
static const uint32_t kIconBytesPerRow = kIconWidth * kIconBitsPerPixel / 8;
|
|
|
|
static const uint32_t kIconBytes = kIconBytesPerRow * kIconHeight;
|
2007-04-22 05:42:17 +04:00
|
|
|
|
2011-04-14 23:54:58 +04:00
|
|
|
typedef NS_STDCALL_FUNCPROTO(nsresult, GetRectSideMethod, nsIDOMRect,
|
|
|
|
GetBottom, (nsIDOMCSSPrimitiveValue**));
|
2009-12-12 01:09:11 +03:00
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsMenuItemIconX, imgINotificationObserver)
|
2007-04-22 05:42:17 +04:00
|
|
|
|
2008-06-28 11:55:30 +04:00
|
|
|
nsMenuItemIconX::nsMenuItemIconX(nsMenuObjectX* aMenuItem,
|
|
|
|
nsIContent* aContent,
|
|
|
|
NSMenuItem* aNativeMenuItem)
|
2007-04-22 05:42:17 +04:00
|
|
|
: mContent(aContent)
|
2008-06-28 11:55:30 +04:00
|
|
|
, mMenuObject(aMenuItem)
|
2011-10-01 04:20:33 +04:00
|
|
|
, mLoadedIcon(false)
|
|
|
|
, mSetIcon(false)
|
2007-08-02 23:08:40 +04:00
|
|
|
, mNativeMenuItem(aNativeMenuItem)
|
2007-04-22 05:42:17 +04:00
|
|
|
{
|
2007-08-02 23:08:40 +04:00
|
|
|
// printf("Creating icon for menu item %d, menu %d, native item is %d\n", aMenuItem, aMenu, aNativeMenuItem);
|
2007-04-22 05:42:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsMenuItemIconX::~nsMenuItemIconX()
|
|
|
|
{
|
|
|
|
if (mIconRequest)
|
2008-12-20 01:35:50 +03:00
|
|
|
mIconRequest->CancelAndForgetObserver(NS_BINDING_ABORTED);
|
2007-04-22 05:42:17 +04:00
|
|
|
}
|
|
|
|
|
2009-07-07 00:59:46 +04:00
|
|
|
// Called from mMenuObjectX's destructor, to prevent us from outliving it
|
2012-12-18 20:37:15 +04:00
|
|
|
// (as might otherwise happen if calls to our imgINotificationObserver methods
|
2009-07-07 00:59:46 +04:00
|
|
|
// are still outstanding). mMenuObjectX owns our nNativeMenuItem.
|
|
|
|
void nsMenuItemIconX::Destroy()
|
|
|
|
{
|
|
|
|
if (mIconRequest) {
|
|
|
|
mIconRequest->CancelAndForgetObserver(NS_BINDING_ABORTED);
|
2012-07-30 18:20:58 +04:00
|
|
|
mIconRequest = nullptr;
|
2009-07-07 00:59:46 +04:00
|
|
|
}
|
2012-07-30 18:20:58 +04:00
|
|
|
mMenuObject = nullptr;
|
2009-07-07 00:59:46 +04:00
|
|
|
mNativeMenuItem = nil;
|
|
|
|
}
|
|
|
|
|
2007-04-22 05:42:17 +04:00
|
|
|
nsresult
|
|
|
|
nsMenuItemIconX::SetupIcon()
|
|
|
|
{
|
2008-02-21 02:47:05 +03:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2007-08-02 23:08:40 +04:00
|
|
|
// Still don't have one, then something is wrong, get out of here.
|
|
|
|
if (!mNativeMenuItem) {
|
2010-06-18 00:28:38 +04:00
|
|
|
NS_ERROR("No native menu item");
|
2007-08-02 23:08:40 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2007-04-22 05:42:17 +04:00
|
|
|
nsCOMPtr<nsIURI> iconURI;
|
2008-06-28 11:55:30 +04:00
|
|
|
nsresult rv = GetIconURI(getter_AddRefs(iconURI));
|
2007-04-22 05:42:17 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// There is no icon for this menu item. An icon might have been set
|
|
|
|
// earlier. Clear it.
|
2007-08-02 23:08:40 +04:00
|
|
|
[mNativeMenuItem setImage:nil];
|
2007-04-22 05:42:17 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-12-12 01:09:11 +03:00
|
|
|
rv = LoadIcon(iconURI);
|
|
|
|
if (NS_FAILED(rv)) {
|
2010-05-13 16:19:50 +04:00
|
|
|
// There is no icon for this menu item, as an error occurred while loading it.
|
2009-12-12 01:09:11 +03:00
|
|
|
// An icon might have been set earlier or the place holder icon may have
|
|
|
|
// been set. Clear it.
|
|
|
|
[mNativeMenuItem setImage:nil];
|
|
|
|
}
|
|
|
|
return rv;
|
2008-02-21 02:47:05 +03:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-04-22 05:42:17 +04:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
static int32_t
|
2009-12-12 01:09:11 +03:00
|
|
|
GetDOMRectSide(nsIDOMRect* aRect, GetRectSideMethod aMethod)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMCSSPrimitiveValue> dimensionValue;
|
|
|
|
(aRect->*aMethod)(getter_AddRefs(dimensionValue));
|
|
|
|
if (!dimensionValue)
|
|
|
|
return -1;
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint16_t primitiveType;
|
2009-12-12 01:09:11 +03:00
|
|
|
nsresult rv = dimensionValue->GetPrimitiveType(&primitiveType);
|
|
|
|
if (NS_FAILED(rv) || primitiveType != nsIDOMCSSPrimitiveValue::CSS_PX)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
float dimension = 0;
|
|
|
|
rv = dimensionValue->GetFloatValue(nsIDOMCSSPrimitiveValue::CSS_PX,
|
|
|
|
&dimension);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return NSToIntRound(dimension);
|
|
|
|
}
|
|
|
|
|
2007-04-22 05:42:17 +04:00
|
|
|
nsresult
|
|
|
|
nsMenuItemIconX::GetIconURI(nsIURI** aIconURI)
|
|
|
|
{
|
2009-07-07 00:59:46 +04:00
|
|
|
if (!mMenuObject)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2007-04-22 05:42:17 +04:00
|
|
|
// Mac native menu items support having both a checkmark and an icon
|
|
|
|
// simultaneously, but this is unheard of in the cross-platform toolkit,
|
|
|
|
// seemingly because the win32 theme is unable to cope with both at once.
|
|
|
|
// The downside is that it's possible to get a menu item marked with a
|
|
|
|
// native checkmark and a checkmark for an icon. Head off that possibility
|
|
|
|
// by pretending that no icon exists if this is a checkable menu item.
|
2008-06-28 11:55:30 +04:00
|
|
|
if (mMenuObject->MenuObjectType() == eMenuItemObjectType) {
|
|
|
|
nsMenuItemX* menuItem = static_cast<nsMenuItemX*>(mMenuObject);
|
|
|
|
if (menuItem->GetMenuItemType() != eRegularMenuItemType)
|
2007-04-22 05:42:17 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2008-06-28 11:55:30 +04:00
|
|
|
if (!mContent)
|
|
|
|
return NS_ERROR_FAILURE;
|
2007-04-22 05:42:17 +04:00
|
|
|
|
|
|
|
// First, look at the content node's "image" attribute.
|
|
|
|
nsAutoString imageURIString;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool hasImageAttr = mContent->GetAttr(kNameSpaceID_None,
|
2011-10-14 22:11:22 +04:00
|
|
|
nsGkAtoms::image,
|
|
|
|
imageURIString);
|
2007-04-22 05:42:17 +04:00
|
|
|
|
|
|
|
nsresult rv;
|
2009-12-12 01:09:11 +03:00
|
|
|
nsCOMPtr<nsIDOMCSSValue> cssValue;
|
|
|
|
nsCOMPtr<nsIDOMCSSStyleDeclaration> cssStyleDecl;
|
|
|
|
nsCOMPtr<nsIDOMCSSPrimitiveValue> primitiveValue;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint16_t primitiveType;
|
2007-04-22 05:42:17 +04:00
|
|
|
if (!hasImageAttr) {
|
|
|
|
// If the content node has no "image" attribute, get the
|
|
|
|
// "list-style-image" property from CSS.
|
2014-08-23 00:11:27 +04:00
|
|
|
nsCOMPtr<nsIDocument> document = mContent->GetComposedDoc();
|
2013-08-14 11:03:40 +04:00
|
|
|
if (!document)
|
2011-04-24 10:54:25 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2011-04-26 01:05:17 +04:00
|
|
|
|
2013-08-14 11:03:40 +04:00
|
|
|
nsCOMPtr<nsPIDOMWindow> window = document->GetWindow();
|
2011-04-24 10:54:25 +04:00
|
|
|
if (!window)
|
|
|
|
return NS_ERROR_FAILURE;
|
2007-04-22 05:42:17 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMElement> domElement = do_QueryInterface(mContent);
|
2011-04-24 10:54:25 +04:00
|
|
|
if (!domElement)
|
|
|
|
return NS_ERROR_FAILURE;
|
2007-04-22 05:42:17 +04:00
|
|
|
|
2010-05-18 11:50:14 +04:00
|
|
|
|
2011-04-24 10:54:25 +04:00
|
|
|
rv = window->GetComputedStyle(domElement, EmptyString(),
|
|
|
|
getter_AddRefs(cssStyleDecl));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2007-04-22 05:42:17 +04:00
|
|
|
|
|
|
|
NS_NAMED_LITERAL_STRING(listStyleImage, "list-style-image");
|
|
|
|
rv = cssStyleDecl->GetPropertyCSSValue(listStyleImage,
|
|
|
|
getter_AddRefs(cssValue));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2009-12-12 01:09:11 +03:00
|
|
|
primitiveValue = do_QueryInterface(cssValue);
|
2007-04-22 05:42:17 +04:00
|
|
|
if (!primitiveValue) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
rv = primitiveValue->GetPrimitiveType(&primitiveType);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
if (primitiveType != nsIDOMCSSPrimitiveValue::CSS_URI)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
rv = primitiveValue->GetStringValue(imageURIString);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
}
|
|
|
|
|
2009-12-12 01:09:11 +03:00
|
|
|
// Empty the mImageRegionRect initially as the image region CSS could
|
|
|
|
// have been changed and now have an error or have been removed since the
|
|
|
|
// last GetIconURI call.
|
2011-04-19 07:07:23 +04:00
|
|
|
mImageRegionRect.SetEmpty();
|
2009-12-12 01:09:11 +03:00
|
|
|
|
2007-04-22 05:42:17 +04:00
|
|
|
// If this menu item shouldn't have an icon, the string will be empty,
|
|
|
|
// and NS_NewURI will fail.
|
|
|
|
nsCOMPtr<nsIURI> iconURI;
|
|
|
|
rv = NS_NewURI(getter_AddRefs(iconURI), imageURIString);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
*aIconURI = iconURI;
|
|
|
|
NS_ADDREF(*aIconURI);
|
2009-12-12 01:09:11 +03:00
|
|
|
|
|
|
|
if (!hasImageAttr) {
|
|
|
|
// Check if the icon has a specified image region so that it can be
|
|
|
|
// cropped appropriately before being displayed.
|
|
|
|
NS_NAMED_LITERAL_STRING(imageRegion, "-moz-image-region");
|
|
|
|
rv = cssStyleDecl->GetPropertyCSSValue(imageRegion,
|
|
|
|
getter_AddRefs(cssValue));
|
|
|
|
// Just return NS_OK if there if there is a failure due to no
|
|
|
|
// moz-image region specified so the whole icon will be drawn anyway.
|
|
|
|
if (NS_FAILED(rv)) return NS_OK;
|
|
|
|
|
|
|
|
primitiveValue = do_QueryInterface(cssValue);
|
|
|
|
if (!primitiveValue) return NS_OK;
|
|
|
|
|
|
|
|
rv = primitiveValue->GetPrimitiveType(&primitiveType);
|
|
|
|
if (NS_FAILED(rv)) return NS_OK;
|
|
|
|
if (primitiveType != nsIDOMCSSPrimitiveValue::CSS_RECT)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMRect> imageRegionRect;
|
|
|
|
rv = primitiveValue->GetRectValue(getter_AddRefs(imageRegionRect));
|
|
|
|
if (NS_FAILED(rv)) return NS_OK;
|
|
|
|
|
|
|
|
if (imageRegionRect) {
|
|
|
|
// Return NS_ERROR_FAILURE if the image region is invalid so the image
|
|
|
|
// is not drawn, and behavior is similar to XUL menus.
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t bottom = GetDOMRectSide(imageRegionRect, &nsIDOMRect::GetBottom);
|
|
|
|
int32_t right = GetDOMRectSide(imageRegionRect, &nsIDOMRect::GetRight);
|
|
|
|
int32_t top = GetDOMRectSide(imageRegionRect, &nsIDOMRect::GetTop);
|
|
|
|
int32_t left = GetDOMRectSide(imageRegionRect, &nsIDOMRect::GetLeft);
|
2009-12-12 01:09:11 +03:00
|
|
|
|
|
|
|
if (top < 0 || left < 0 || bottom <= top || right <= left)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
mImageRegionRect.SetRect(left, top, right - left, bottom - top);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-22 05:42:17 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsMenuItemIconX::LoadIcon(nsIURI* aIconURI)
|
|
|
|
{
|
2008-02-21 02:47:05 +03:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2007-04-22 05:42:17 +04:00
|
|
|
if (mIconRequest) {
|
|
|
|
// Another icon request is already in flight. Kill it.
|
|
|
|
mIconRequest->Cancel(NS_BINDING_ABORTED);
|
2012-07-30 18:20:58 +04:00
|
|
|
mIconRequest = nullptr;
|
2007-04-22 05:42:17 +04:00
|
|
|
}
|
|
|
|
|
2011-10-01 04:20:33 +04:00
|
|
|
mLoadedIcon = false;
|
2007-04-22 05:42:17 +04:00
|
|
|
|
|
|
|
if (!mContent) return NS_ERROR_FAILURE;
|
|
|
|
|
2011-10-18 14:53:36 +04:00
|
|
|
nsCOMPtr<nsIDocument> document = mContent->OwnerDoc();
|
2007-04-22 05:42:17 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup = document->GetDocumentLoadGroup();
|
|
|
|
if (!loadGroup) return NS_ERROR_FAILURE;
|
|
|
|
|
2012-11-16 22:24:58 +04:00
|
|
|
nsRefPtr<imgLoader> loader = nsContentUtils::GetImgLoaderForDocument(document);
|
2012-06-26 08:20:12 +04:00
|
|
|
if (!loader) return NS_ERROR_FAILURE;
|
2007-04-22 05:42:17 +04:00
|
|
|
|
|
|
|
if (!mSetIcon) {
|
|
|
|
// Set a completely transparent 16x16 image as the icon on this menu item
|
|
|
|
// as a placeholder. This keeps the menu item text displayed in the same
|
|
|
|
// position that it will be displayed when the real icon is loaded, and
|
|
|
|
// prevents it from jumping around or looking misaligned.
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool sInitializedPlaceholder;
|
2007-08-02 23:08:40 +04:00
|
|
|
static NSImage* sPlaceholderIconImage;
|
2007-04-22 05:42:17 +04:00
|
|
|
if (!sInitializedPlaceholder) {
|
2011-10-01 04:20:33 +04:00
|
|
|
sInitializedPlaceholder = true;
|
2007-04-22 05:42:17 +04:00
|
|
|
|
2007-08-02 23:08:40 +04:00
|
|
|
// Note that we only create the one and reuse it forever, so this is not a leak.
|
|
|
|
sPlaceholderIconImage = [[NSImage alloc] initWithSize:NSMakeSize(kIconWidth, kIconHeight)];
|
2007-04-22 05:42:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!sPlaceholderIconImage) return NS_ERROR_FAILURE;
|
|
|
|
|
2007-08-02 23:08:40 +04:00
|
|
|
if (mNativeMenuItem)
|
|
|
|
[mNativeMenuItem setImage:sPlaceholderIconImage];
|
2007-04-22 05:42:17 +04:00
|
|
|
}
|
|
|
|
|
2010-05-21 00:08:02 +04:00
|
|
|
// Passing in null for channelPolicy here since nsMenuItemIconX::LoadIcon is
|
|
|
|
// not exposed to web content
|
2012-06-26 08:20:12 +04:00
|
|
|
nsresult rv = loader->LoadImage(aIconURI, nullptr, nullptr, nullptr, loadGroup, this,
|
2012-11-04 07:04:07 +04:00
|
|
|
nullptr, nsIRequest::LOAD_NORMAL, nullptr,
|
2013-10-16 05:35:44 +04:00
|
|
|
nullptr, EmptyString(), getter_AddRefs(mIconRequest));
|
2007-04-22 05:42:17 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2011-12-14 23:44:46 +04:00
|
|
|
// We need to request the icon be decoded (bug 573583, bug 705516).
|
2012-10-05 00:02:15 +04:00
|
|
|
mIconRequest->StartDecoding();
|
2011-12-14 23:44:46 +04:00
|
|
|
|
2007-04-22 05:42:17 +04:00
|
|
|
return NS_OK;
|
2008-02-21 02:47:05 +03:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-04-22 05:42:17 +04:00
|
|
|
}
|
|
|
|
|
2007-08-31 12:08:03 +04:00
|
|
|
//
|
2012-10-12 20:11:22 +04:00
|
|
|
// imgINotificationObserver
|
2007-08-31 12:08:03 +04:00
|
|
|
//
|
2007-04-22 05:42:17 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-10-12 20:11:22 +04:00
|
|
|
nsMenuItemIconX::Notify(imgIRequest *aRequest, int32_t aType, const nsIntRect* aData)
|
2007-04-22 05:42:17 +04:00
|
|
|
{
|
2012-10-12 20:11:23 +04:00
|
|
|
if (aType == imgINotificationObserver::FRAME_COMPLETE) {
|
2012-10-12 20:11:22 +04:00
|
|
|
return OnStopFrame(aRequest);
|
|
|
|
}
|
2012-10-12 05:58:24 +04:00
|
|
|
|
2013-03-22 07:09:16 +04:00
|
|
|
if (aType == imgINotificationObserver::DECODE_COMPLETE) {
|
2012-10-12 20:11:22 +04:00
|
|
|
if (mIconRequest && mIconRequest == aRequest) {
|
|
|
|
mIconRequest->Cancel(NS_BINDING_ABORTED);
|
|
|
|
mIconRequest = nullptr;
|
|
|
|
}
|
|
|
|
}
|
2012-10-12 05:58:24 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-10-12 20:11:22 +04:00
|
|
|
nsresult
|
|
|
|
nsMenuItemIconX::OnStopFrame(imgIRequest* aRequest)
|
2007-04-22 05:42:17 +04:00
|
|
|
{
|
2008-02-21 02:47:05 +03:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
Bug 753 - Remove nsIImage, gfxIImageFrame, and their implementations, and expose an equivalent api on imgIContainer. r=roc,josh,bz,longsonr,vlad,karlt,jimm,bsmedberg,mfinkle,peterw,peterv sr=vlad,roc
--HG--
rename : gfx/src/shared/gfxImageFrame.cpp => modules/libpr0n/src/imgFrame.cpp
rename : gfx/src/shared/gfxImageFrame.h => modules/libpr0n/src/imgFrame.h
2009-07-21 05:50:15 +04:00
|
|
|
if (aRequest != mIconRequest)
|
|
|
|
return NS_ERROR_FAILURE;
|
2007-04-22 05:42:17 +04:00
|
|
|
|
|
|
|
// Only support one frame.
|
|
|
|
if (mLoadedIcon)
|
|
|
|
return NS_OK;
|
|
|
|
|
2010-03-03 01:23:07 +03:00
|
|
|
if (!mNativeMenuItem)
|
|
|
|
return NS_ERROR_FAILURE;
|
2009-07-07 00:59:46 +04:00
|
|
|
|
Bug 753 - Remove nsIImage, gfxIImageFrame, and their implementations, and expose an equivalent api on imgIContainer. r=roc,josh,bz,longsonr,vlad,karlt,jimm,bsmedberg,mfinkle,peterw,peterv sr=vlad,roc
--HG--
rename : gfx/src/shared/gfxImageFrame.cpp => modules/libpr0n/src/imgFrame.cpp
rename : gfx/src/shared/gfxImageFrame.h => modules/libpr0n/src/imgFrame.h
2009-07-21 05:50:15 +04:00
|
|
|
nsCOMPtr<imgIContainer> imageContainer;
|
|
|
|
aRequest->GetImage(getter_AddRefs(imageContainer));
|
2009-12-12 01:09:11 +03:00
|
|
|
if (!imageContainer) {
|
|
|
|
[mNativeMenuItem setImage:nil];
|
Bug 753 - Remove nsIImage, gfxIImageFrame, and their implementations, and expose an equivalent api on imgIContainer. r=roc,josh,bz,longsonr,vlad,karlt,jimm,bsmedberg,mfinkle,peterw,peterv sr=vlad,roc
--HG--
rename : gfx/src/shared/gfxImageFrame.cpp => modules/libpr0n/src/imgFrame.cpp
rename : gfx/src/shared/gfxImageFrame.h => modules/libpr0n/src/imgFrame.h
2009-07-21 05:50:15 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2009-12-12 01:09:11 +03:00
|
|
|
}
|
2007-04-22 05:42:17 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t origWidth = 0, origHeight = 0;
|
2010-03-13 17:39:55 +03:00
|
|
|
imageContainer->GetWidth(&origWidth);
|
|
|
|
imageContainer->GetHeight(&origHeight);
|
|
|
|
|
2009-12-12 01:09:11 +03:00
|
|
|
// If the image region is invalid, don't draw the image to almost match
|
|
|
|
// the behavior of other platforms.
|
|
|
|
if (!mImageRegionRect.IsEmpty() &&
|
|
|
|
(mImageRegionRect.XMost() > origWidth ||
|
|
|
|
mImageRegionRect.YMost() > origHeight)) {
|
|
|
|
[mNativeMenuItem setImage:nil];
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mImageRegionRect.IsEmpty()) {
|
|
|
|
mImageRegionRect.SetRect(0, 0, origWidth, origHeight);
|
|
|
|
}
|
2014-04-15 22:02:23 +04:00
|
|
|
|
|
|
|
RefPtr<SourceSurface> surface =
|
2013-12-13 12:34:24 +04:00
|
|
|
imageContainer->GetFrame(imgIContainer::FRAME_CURRENT,
|
2014-07-20 18:31:37 +04:00
|
|
|
imgIContainer::FLAG_SYNC_DECODE);
|
2014-04-15 22:02:23 +04:00
|
|
|
if (!surface) {
|
2010-03-25 19:07:36 +03:00
|
|
|
[mNativeMenuItem setImage:nil];
|
|
|
|
return NS_ERROR_FAILURE;
|
2013-03-06 03:39:48 +04:00
|
|
|
}
|
|
|
|
|
2010-03-13 17:39:55 +03:00
|
|
|
CGImageRef origImage = NULL;
|
2014-03-08 05:21:24 +04:00
|
|
|
nsresult rv = nsCocoaUtils::CreateCGImageFromSurface(surface, &origImage);
|
2010-03-13 17:39:55 +03:00
|
|
|
if (NS_FAILED(rv) || !origImage) {
|
|
|
|
[mNativeMenuItem setImage:nil];
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2010-03-03 01:23:07 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool createSubImage = !(mImageRegionRect.x == 0 && mImageRegionRect.y == 0 &&
|
2010-03-03 01:23:07 +03:00
|
|
|
mImageRegionRect.width == origWidth && mImageRegionRect.height == origHeight);
|
2010-03-13 17:39:55 +03:00
|
|
|
|
2014-07-20 18:31:37 +04:00
|
|
|
CGImageRef finalImage = origImage;
|
2010-03-03 01:23:07 +03:00
|
|
|
if (createSubImage) {
|
2010-03-13 17:39:55 +03:00
|
|
|
// if mImageRegionRect is set using CSS, we need to slice a piece out of the overall
|
|
|
|
// image to use as the icon
|
|
|
|
finalImage = ::CGImageCreateWithImageInRect(origImage,
|
|
|
|
::CGRectMake(mImageRegionRect.x,
|
|
|
|
mImageRegionRect.y,
|
|
|
|
mImageRegionRect.width,
|
|
|
|
mImageRegionRect.height));
|
|
|
|
::CGImageRelease(origImage);
|
|
|
|
if (!finalImage) {
|
2010-03-03 01:23:07 +03:00
|
|
|
[mNativeMenuItem setImage:nil];
|
2010-03-13 17:39:55 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2010-03-03 01:23:07 +03:00
|
|
|
}
|
2007-04-22 05:42:17 +04:00
|
|
|
}
|
|
|
|
|
2010-03-13 17:39:55 +03:00
|
|
|
NSImage *newImage = nil;
|
2014-07-20 18:31:37 +04:00
|
|
|
rv = nsCocoaUtils::CreateNSImageFromCGImage(finalImage, &newImage);
|
2010-03-13 17:39:55 +03:00
|
|
|
if (NS_FAILED(rv) || !newImage) {
|
|
|
|
[mNativeMenuItem setImage:nil];
|
2014-07-20 18:31:37 +04:00
|
|
|
::CGImageRelease(finalImage);
|
2007-04-22 05:42:17 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2014-07-20 18:31:37 +04:00
|
|
|
[newImage setSize:NSMakeSize(kIconWidth, kIconHeight)];
|
2007-08-02 23:08:40 +04:00
|
|
|
[mNativeMenuItem setImage:newImage];
|
|
|
|
|
|
|
|
[newImage release];
|
2014-07-20 18:31:37 +04:00
|
|
|
::CGImageRelease(finalImage);
|
2007-04-22 05:42:17 +04:00
|
|
|
|
2011-10-01 04:20:33 +04:00
|
|
|
mLoadedIcon = true;
|
|
|
|
mSetIcon = true;
|
2007-04-22 05:42:17 +04:00
|
|
|
|
2014-07-18 23:07:02 +04:00
|
|
|
if (mMenuObject) {
|
|
|
|
mMenuObject->IconUpdated();
|
|
|
|
}
|
|
|
|
|
2007-04-22 05:42:17 +04:00
|
|
|
return NS_OK;
|
2008-02-21 02:47:05 +03:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-04-22 05:42:17 +04:00
|
|
|
}
|