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
|
2019-01-21 20:18:16 +03:00
|
|
|
# ifndef __EXCEPTIONS
|
|
|
|
# define __EXCEPTIONS
|
|
|
|
# endif
|
2016-03-07 05:10:17 +03:00
|
|
|
#endif
|
2012-11-16 22:24:58 +04:00
|
|
|
|
|
|
|
#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"
|
2019-01-02 16:05:23 +03:00
|
|
|
#include "mozilla/dom/Document.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 "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"
|
2014-08-20 01:49:38 +04:00
|
|
|
#include "nsIContentPolicy.h"
|
2018-01-11 11:17:57 +03:00
|
|
|
#include "nsComputedDOMStyle.h"
|
2007-04-22 05:42:17 +04:00
|
|
|
|
2018-11-14 20:03:36 +03:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2015-10-27 00:37:32 +03:00
|
|
|
using mozilla::dom::Element;
|
2014-03-08 05:21:24 +04:00
|
|
|
using mozilla::gfx::SourceSurface;
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
static const uint32_t kIconWidth = 16;
|
|
|
|
static const uint32_t kIconHeight = 16;
|
2007-04-22 05:42:17 +04:00
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsMenuItemIconX, imgINotificationObserver)
|
2007-04-22 05:42:17 +04:00
|
|
|
|
2019-01-21 20:18:16 +03:00
|
|
|
nsMenuItemIconX::nsMenuItemIconX(nsMenuObjectX* aMenuItem, nsIContent* aContent,
|
|
|
|
NSMenuItem* aNativeMenuItem)
|
|
|
|
: mContent(aContent),
|
|
|
|
mTriggeringPrincipal(aContent->NodePrincipal()),
|
|
|
|
mContentType(nsIContentPolicy::TYPE_INTERNAL_IMAGE),
|
|
|
|
mMenuObject(aMenuItem),
|
|
|
|
mLoadedIcon(false),
|
|
|
|
mSetIcon(false),
|
|
|
|
mNativeMenuItem(aNativeMenuItem) {
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2019-01-21 20:18:16 +03:00
|
|
|
nsMenuItemIconX::~nsMenuItemIconX() {
|
|
|
|
if (mIconRequest) 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.
|
2019-01-21 20:18:16 +03:00
|
|
|
void nsMenuItemIconX::Destroy() {
|
2009-07-07 00:59:46 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-01-21 20:18:16 +03: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
|
|
|
}
|
|
|
|
|
2019-01-21 20:18:16 +03:00
|
|
|
nsresult nsMenuItemIconX::GetIconURI(nsIURI** aIconURI) {
|
|
|
|
if (!mMenuObject) return NS_ERROR_FAILURE;
|
2009-07-07 00:59:46 +04:00
|
|
|
|
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);
|
2019-01-21 20:18:16 +03:00
|
|
|
if (menuItem->GetMenuItemType() != eRegularMenuItemType) return NS_ERROR_FAILURE;
|
2007-04-22 05:42:17 +04:00
|
|
|
}
|
|
|
|
|
2019-01-21 20:18:16 +03: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;
|
2017-12-07 21:13:50 +03:00
|
|
|
bool hasImageAttr =
|
2019-01-21 20:18:16 +03:00
|
|
|
mContent->IsElement() &&
|
|
|
|
mContent->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::image, imageURIString);
|
2007-04-22 05:42:17 +04:00
|
|
|
|
|
|
|
nsresult rv;
|
2018-03-22 21:20:41 +03:00
|
|
|
RefPtr<ComputedStyle> sc;
|
2018-01-11 11:17:57 +03:00
|
|
|
nsCOMPtr<nsIURI> iconURI;
|
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.
|
2019-01-02 16:05:23 +03:00
|
|
|
RefPtr<mozilla::dom::Document> document = mContent->GetComposedDoc();
|
2018-01-11 11:17:57 +03:00
|
|
|
if (!document || !mContent->IsElement()) {
|
2011-04-24 10:54:25 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2018-01-11 11:17:57 +03:00
|
|
|
}
|
2007-04-22 05:42:17 +04:00
|
|
|
|
2018-03-22 21:20:41 +03:00
|
|
|
sc = nsComputedDOMStyle::GetComputedStyle(mContent->AsElement(), nullptr);
|
2018-01-11 11:17:57 +03:00
|
|
|
if (!sc) {
|
2015-10-27 00:37:32 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2018-01-11 11:17:57 +03:00
|
|
|
}
|
2007-04-22 05:42:17 +04:00
|
|
|
|
2018-01-11 11:17:57 +03:00
|
|
|
iconURI = sc->StyleList()->GetListStyleImageURI();
|
|
|
|
if (!iconURI) {
|
2007-04-22 05:42:17 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2018-01-11 11:17:57 +03:00
|
|
|
}
|
2016-12-14 23:52:57 +03:00
|
|
|
} else {
|
2017-09-21 03:09:00 +03:00
|
|
|
uint64_t dummy = 0;
|
2019-01-21 20:18:16 +03:00
|
|
|
nsContentUtils::GetContentPolicyTypeForUIImageLoading(
|
|
|
|
mContent, getter_AddRefs(mTriggeringPrincipal), mContentType, &dummy);
|
2018-01-11 11:17:57 +03:00
|
|
|
|
|
|
|
// If this menu item shouldn't have an icon, the string will be empty,
|
|
|
|
// and NS_NewURI will fail.
|
|
|
|
rv = NS_NewURI(getter_AddRefs(iconURI), imageURIString);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2007-04-22 05:42:17 +04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2018-01-11 11:17:57 +03:00
|
|
|
iconURI.forget(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.
|
2018-01-11 11:17:57 +03:00
|
|
|
const nsRect& r = sc->StyleList()->mImageRegion;
|
2009-12-12 01:09:11 +03:00
|
|
|
|
2018-01-11 11:17:57 +03:00
|
|
|
// Return NS_ERROR_FAILURE if the image region is invalid so the image
|
|
|
|
// is not drawn, and behavior is similar to XUL menus.
|
2018-01-12 08:39:49 +03:00
|
|
|
if (r.X() < 0 || r.Y() < 0 || r.Width() < 0 || r.Height() < 0) {
|
2018-01-11 11:17:57 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2009-12-12 01:09:11 +03:00
|
|
|
}
|
2018-01-11 11:17:57 +03:00
|
|
|
|
2018-01-12 08:39:49 +03:00
|
|
|
// 'auto' is represented by a [0, 0, 0, 0] rect. Only set mImageRegionRect
|
|
|
|
// if we have some other value.
|
|
|
|
if (!r.IsEmpty()) {
|
|
|
|
mImageRegionRect = r.ToNearestPixels(mozilla::AppUnitsPerCSSPixel());
|
|
|
|
}
|
2009-12-12 01:09:11 +03:00
|
|
|
}
|
|
|
|
|
2007-04-22 05:42:17 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-01-21 20:18:16 +03:00
|
|
|
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;
|
|
|
|
|
2019-01-02 16:05:23 +03:00
|
|
|
RefPtr<mozilla::dom::Document> document = mContent->OwnerDoc();
|
2007-04-22 05:42:17 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup = document->GetDocumentLoadGroup();
|
|
|
|
if (!loadGroup) return NS_ERROR_FAILURE;
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<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;
|
|
|
|
|
2019-01-21 20:18:16 +03:00
|
|
|
if (mNativeMenuItem) [mNativeMenuItem setImage:sPlaceholderIconImage];
|
2007-04-22 05:42:17 +04:00
|
|
|
}
|
|
|
|
|
2019-01-21 20:18:16 +03:00
|
|
|
nsresult rv = loader->LoadImage(
|
|
|
|
aIconURI, nullptr, nullptr, mozilla::net::RP_Unset, mTriggeringPrincipal, 0, loadGroup, this,
|
|
|
|
mContent, document, nsIRequest::LOAD_NORMAL, nullptr, mContentType, EmptyString(),
|
|
|
|
/* aUseUrgentStartForChannel */ false, getter_AddRefs(mIconRequest));
|
2007-04-22 05:42:17 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
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
|
2019-01-21 20:18:16 +03:00
|
|
|
nsMenuItemIconX::Notify(imgIRequest* aRequest, int32_t aType, const nsIntRect* aData) {
|
2015-01-20 02:46:55 +03:00
|
|
|
if (aType == imgINotificationObserver::LOAD_COMPLETE) {
|
|
|
|
// Make sure the image loaded successfully.
|
|
|
|
uint32_t status = imgIRequest::STATUS_ERROR;
|
2019-01-21 20:18:16 +03:00
|
|
|
if (NS_FAILED(aRequest->GetImageStatus(&status)) || (status & imgIRequest::STATUS_ERROR)) {
|
2015-01-20 02:46:55 +03:00
|
|
|
mIconRequest->Cancel(NS_BINDING_ABORTED);
|
|
|
|
mIconRequest = nullptr;
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<imgIContainer> image;
|
|
|
|
aRequest->GetImage(getter_AddRefs(image));
|
|
|
|
MOZ_ASSERT(image);
|
|
|
|
|
|
|
|
// Ask the image to decode at its intrinsic size.
|
|
|
|
int32_t width = 0, height = 0;
|
|
|
|
image->GetWidth(&width);
|
|
|
|
image->GetHeight(&height);
|
2019-01-21 20:18:16 +03:00
|
|
|
image->RequestDecodeForSize(nsIntSize(width, height), imgIContainer::FLAG_HIGH_QUALITY_SCALING);
|
2015-01-20 02:46:55 +03:00
|
|
|
}
|
|
|
|
|
2012-10-12 20:11:23 +04:00
|
|
|
if (aType == imgINotificationObserver::FRAME_COMPLETE) {
|
2014-11-18 01:29:56 +03:00
|
|
|
return OnFrameComplete(aRequest);
|
2012-10-12 20:11:22 +04:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2019-01-21 20:18:16 +03:00
|
|
|
nsresult nsMenuItemIconX::OnFrameComplete(imgIRequest* aRequest) {
|
2008-02-21 02:47:05 +03:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2019-01-21 20:18:16 +03:00
|
|
|
if (aRequest != mIconRequest) return NS_ERROR_FAILURE;
|
2007-04-22 05:42:17 +04:00
|
|
|
|
|
|
|
// Only support one frame.
|
2019-01-21 20:18:16 +03:00
|
|
|
if (mLoadedIcon) return NS_OK;
|
2007-04-22 05:42:17 +04:00
|
|
|
|
2019-01-21 20:18:16 +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);
|
2014-09-17 03:18:04 +04:00
|
|
|
|
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() &&
|
2019-01-21 20:18:16 +03:00
|
|
|
(mImageRegionRect.XMost() > origWidth || mImageRegionRect.YMost() > origHeight)) {
|
2009-12-12 01:09:11 +03:00
|
|
|
[mNativeMenuItem setImage:nil];
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mImageRegionRect.IsEmpty()) {
|
|
|
|
mImageRegionRect.SetRect(0, 0, origWidth, origHeight);
|
|
|
|
}
|
2014-04-15 22:02:23 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<SourceSurface> surface =
|
2019-01-21 20:18:16 +03:00
|
|
|
imageContainer->GetFrame(imgIContainer::FRAME_CURRENT, 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;
|
2018-04-16 22:23:23 +03:00
|
|
|
bool isEntirelyBlack = false;
|
2019-01-21 20:18:16 +03:00
|
|
|
nsresult rv = nsCocoaUtils::CreateCGImageFromSurface(surface, &origImage, &isEntirelyBlack);
|
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
|
|
|
|
2019-01-21 20:18:16 +03:00
|
|
|
bool createSubImage =
|
|
|
|
!(mImageRegionRect.x == 0 && mImageRegionRect.y == 0 && mImageRegionRect.width == origWidth &&
|
|
|
|
mImageRegionRect.height == origHeight);
|
2014-09-17 03:18:04 +04:00
|
|
|
|
2014-07-20 18:31:37 +04:00
|
|
|
CGImageRef finalImage = origImage;
|
2010-03-03 01:23:07 +03:00
|
|
|
if (createSubImage) {
|
2014-09-17 03:18:04 +04:00
|
|
|
// if mImageRegionRect is set using CSS, we need to slice a piece out of the overall
|
2010-03-13 17:39:55 +03:00
|
|
|
// image to use as the icon
|
2019-01-21 20:18:16 +03:00
|
|
|
finalImage = ::CGImageCreateWithImageInRect(
|
|
|
|
origImage, ::CGRectMake(mImageRegionRect.x, mImageRegionRect.y, mImageRegionRect.width,
|
|
|
|
mImageRegionRect.height));
|
2010-03-13 17:39:55 +03:00
|
|
|
::CGImageRelease(origImage);
|
|
|
|
if (!finalImage) {
|
2010-03-03 01:23:07 +03:00
|
|
|
[mNativeMenuItem setImage:nil];
|
2014-09-17 03:18:04 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2010-03-03 01:23:07 +03:00
|
|
|
}
|
2007-04-22 05:42:17 +04:00
|
|
|
}
|
|
|
|
|
2019-01-21 20:18:16 +03:00
|
|
|
NSImage* newImage = nil;
|
2014-07-20 18:31:37 +04:00
|
|
|
rv = nsCocoaUtils::CreateNSImageFromCGImage(finalImage, &newImage);
|
2014-09-17 03:18:04 +04:00
|
|
|
if (NS_FAILED(rv) || !newImage) {
|
2010-03-13 17:39:55 +03:00
|
|
|
[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;
|
|
|
|
}
|
|
|
|
|
2018-04-16 22:23:23 +03:00
|
|
|
// If all the color channels in the image are black, treat the image as a
|
|
|
|
// template. This will cause macOS to use the image's alpha channel as a mask
|
|
|
|
// and it will fill it with a color that looks good in the context that it's
|
|
|
|
// used in. For example, for regular menu items, the image will be black, but
|
|
|
|
// when the menu item is hovered (and its background is blue), it will be
|
|
|
|
// filled with white.
|
|
|
|
[newImage setTemplate:isEntirelyBlack];
|
|
|
|
|
2014-07-20 18:31:37 +04:00
|
|
|
[newImage setSize:NSMakeSize(kIconWidth, kIconHeight)];
|
2007-08-02 23:08:40 +04:00
|
|
|
[mNativeMenuItem setImage:newImage];
|
2014-09-17 03:18:04 +04:00
|
|
|
|
2007-08-02 23:08:40 +04:00
|
|
|
[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
|
|
|
}
|