2007-02-23 00:56:50 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 20; 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-02-23 00:56:50 +03:00
|
|
|
|
|
|
|
#ifndef nsCocoaUtils_h_
|
|
|
|
#define nsCocoaUtils_h_
|
|
|
|
|
|
|
|
#import <Cocoa/Cocoa.h>
|
|
|
|
|
|
|
|
#include "nsRect.h"
|
2010-03-11 19:44:31 +03:00
|
|
|
#include "imgIContainer.h"
|
2011-07-21 04:33:16 +04:00
|
|
|
#include "nsEvent.h"
|
|
|
|
#include "npapi.h"
|
2008-03-27 06:42:57 +03:00
|
|
|
|
2013-01-15 16:22:03 +04:00
|
|
|
// This must be the last include:
|
|
|
|
#include "nsObjCExceptions.h"
|
|
|
|
|
2012-09-29 15:36:09 +04:00
|
|
|
// Declare the backingScaleFactor method that we want to call
|
|
|
|
// on NSView/Window/Screen objects, if they recognize it.
|
|
|
|
@interface NSObject (BackingScaleFactorCategory)
|
|
|
|
- (CGFloat)backingScaleFactor;
|
|
|
|
@end
|
|
|
|
|
2009-04-02 01:59:02 +04:00
|
|
|
class nsIWidget;
|
|
|
|
|
2008-04-08 08:38:52 +04:00
|
|
|
// Used to retain a Cocoa object for the remainder of a method's execution.
|
|
|
|
class nsAutoRetainCocoaObject {
|
|
|
|
public:
|
|
|
|
nsAutoRetainCocoaObject(id anObject)
|
|
|
|
{
|
|
|
|
mObject = NS_OBJC_TRY_EXPR_ABORT([anObject retain]);
|
|
|
|
}
|
|
|
|
~nsAutoRetainCocoaObject()
|
|
|
|
{
|
|
|
|
NS_OBJC_TRY_ABORT([mObject release]);
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
id mObject; // [STRONG]
|
|
|
|
};
|
|
|
|
|
2010-04-14 00:36:03 +04:00
|
|
|
// Provide a local autorelease pool for the remainder of a method's execution.
|
|
|
|
class nsAutoreleasePool {
|
|
|
|
public:
|
|
|
|
nsAutoreleasePool()
|
|
|
|
{
|
|
|
|
mLocalPool = [[NSAutoreleasePool alloc] init];
|
|
|
|
}
|
|
|
|
~nsAutoreleasePool()
|
|
|
|
{
|
|
|
|
[mLocalPool release];
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
NSAutoreleasePool *mLocalPool;
|
|
|
|
};
|
|
|
|
|
2008-03-27 06:42:57 +03:00
|
|
|
@interface NSApplication (Undocumented)
|
|
|
|
|
|
|
|
// Present in all versions of OS X from (at least) 10.2.8 through 10.5.
|
|
|
|
- (BOOL)_isRunningModal;
|
|
|
|
- (BOOL)_isRunningAppModal;
|
|
|
|
|
|
|
|
// It's sometimes necessary to explicitly remove a window from the "window
|
|
|
|
// cache" in order to deactivate it. The "window cache" is an undocumented
|
|
|
|
// subsystem, all of whose methods are included in the NSWindowCache category
|
|
|
|
// of the NSApplication class (in header files generated using class-dump).
|
2008-10-18 01:58:47 +04:00
|
|
|
// Present in all versions of OS X from (at least) 10.2.8 through 10.5.
|
2008-03-27 06:42:57 +03:00
|
|
|
- (void)_removeWindowFromCache:(NSWindow *)aWindow;
|
|
|
|
|
2008-10-18 01:58:47 +04:00
|
|
|
// Send an event to the current Cocoa app-modal session. Present in all
|
|
|
|
// versions of OS X from (at least) 10.2.8 through 10.5.
|
|
|
|
- (void)_modalSession:(NSModalSession)aSession sendEvent:(NSEvent *)theEvent;
|
|
|
|
|
2010-01-09 01:34:51 +03:00
|
|
|
// Present (and documented) on OS X 10.6 and above. Not present before 10.6.
|
|
|
|
// This declaration needed to avoid compiler warnings when compiling on 10.5
|
|
|
|
// and below (or using the 10.5 SDK and below).
|
|
|
|
- (void)setHelpMenu:(NSMenu *)helpMenu;
|
|
|
|
|
2008-03-27 06:42:57 +03:00
|
|
|
@end
|
2007-02-23 00:56:50 +03:00
|
|
|
|
2007-12-06 02:17:08 +03:00
|
|
|
class nsCocoaUtils
|
|
|
|
{
|
|
|
|
public:
|
2012-09-29 15:36:09 +04:00
|
|
|
|
|
|
|
// Get the backing scale factor from an object that supports this selector
|
|
|
|
// (NSView/Window/Screen, on 10.7 or later), returning 1.0 if not supported
|
|
|
|
static CGFloat
|
|
|
|
GetBackingScaleFactor(id aObject)
|
|
|
|
{
|
|
|
|
if (HiDPIEnabled() &&
|
|
|
|
[aObject respondsToSelector:@selector(backingScaleFactor)]) {
|
|
|
|
return [aObject backingScaleFactor];
|
|
|
|
}
|
|
|
|
return 1.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Conversions between Cocoa points and device pixels, given the backing
|
|
|
|
// scale factor from a view/window/screen.
|
|
|
|
static int32_t
|
|
|
|
CocoaPointsToDevPixels(CGFloat aPts, CGFloat aBackingScale)
|
|
|
|
{
|
|
|
|
return NSToIntRound(aPts * aBackingScale);
|
|
|
|
}
|
|
|
|
|
|
|
|
static nsIntPoint
|
|
|
|
CocoaPointsToDevPixels(const NSPoint& aPt, CGFloat aBackingScale)
|
|
|
|
{
|
|
|
|
return nsIntPoint(NSToIntRound(aPt.x * aBackingScale),
|
|
|
|
NSToIntRound(aPt.y * aBackingScale));
|
|
|
|
}
|
|
|
|
|
|
|
|
static nsIntRect
|
|
|
|
CocoaPointsToDevPixels(const NSRect& aRect, CGFloat aBackingScale)
|
|
|
|
{
|
|
|
|
return nsIntRect(NSToIntRound(aRect.origin.x * aBackingScale),
|
|
|
|
NSToIntRound(aRect.origin.y * aBackingScale),
|
|
|
|
NSToIntRound(aRect.size.width * aBackingScale),
|
|
|
|
NSToIntRound(aRect.size.height * aBackingScale));
|
|
|
|
}
|
|
|
|
|
|
|
|
static CGFloat
|
|
|
|
DevPixelsToCocoaPoints(int32_t aPixels, CGFloat aBackingScale)
|
|
|
|
{
|
|
|
|
return (CGFloat)aPixels / aBackingScale;
|
|
|
|
}
|
|
|
|
|
|
|
|
static NSPoint
|
|
|
|
DevPixelsToCocoaPoints(const nsIntPoint& aPt, CGFloat aBackingScale)
|
|
|
|
{
|
|
|
|
return NSMakePoint((CGFloat)aPt.x / aBackingScale,
|
|
|
|
(CGFloat)aPt.y / aBackingScale);
|
|
|
|
}
|
|
|
|
|
|
|
|
static NSRect
|
|
|
|
DevPixelsToCocoaPoints(const nsIntRect& aRect, CGFloat aBackingScale)
|
|
|
|
{
|
|
|
|
return NSMakeRect((CGFloat)aRect.x / aBackingScale,
|
|
|
|
(CGFloat)aRect.y / aBackingScale,
|
|
|
|
(CGFloat)aRect.width / aBackingScale,
|
|
|
|
(CGFloat)aRect.height / aBackingScale);
|
|
|
|
}
|
2007-02-23 00:56:50 +03:00
|
|
|
|
2007-12-06 02:17:08 +03:00
|
|
|
// Returns the given y coordinate, which must be in screen coordinates,
|
|
|
|
// flipped from Gecko to Cocoa or Cocoa to Gecko.
|
|
|
|
static float FlippedScreenY(float y);
|
2012-09-29 15:36:09 +04:00
|
|
|
|
|
|
|
// The following functions come in "DevPix" variants that work with
|
|
|
|
// backing-store (device pixel) coordinates, as well as the original
|
|
|
|
// versions that expect coordinates in Cocoa points/CSS pixels.
|
|
|
|
// The difference becomes important in HiDPI display modes, where Cocoa
|
|
|
|
// points and backing-store pixels are no longer 1:1.
|
|
|
|
|
2007-12-06 02:17:08 +03:00
|
|
|
// Gecko rects (nsRect) contain an origin (x,y) in a coordinate
|
|
|
|
// system with (0,0) in the top-left of the primary screen. Cocoa rects
|
|
|
|
// (NSRect) contain an origin (x,y) in a coordinate system with (0,0)
|
|
|
|
// in the bottom-left of the primary screen. Both nsRect and NSRect
|
|
|
|
// contain width/height info, with no difference in their use.
|
2012-09-29 15:36:09 +04:00
|
|
|
// This function does no scaling, so the Gecko coordinates are
|
|
|
|
// expected to be CSS pixels, which we treat as equal to Cocoa points.
|
2009-01-15 06:27:09 +03:00
|
|
|
static NSRect GeckoRectToCocoaRect(const nsIntRect &geckoRect);
|
2012-09-29 15:36:09 +04:00
|
|
|
|
|
|
|
// Converts aGeckoRect in dev pixels to points in Cocoa coordinates
|
|
|
|
static NSRect GeckoRectToCocoaRectDevPix(const nsIntRect &aGeckoRect,
|
|
|
|
CGFloat aBackingScale);
|
|
|
|
|
2007-12-06 02:17:08 +03:00
|
|
|
// See explanation for geckoRectToCocoaRect, guess what this does...
|
2009-01-15 06:27:09 +03:00
|
|
|
static nsIntRect CocoaRectToGeckoRect(const NSRect &cocoaRect);
|
2012-09-29 15:36:09 +04:00
|
|
|
|
|
|
|
static nsIntRect CocoaRectToGeckoRectDevPix(const NSRect &aCocoaRect,
|
|
|
|
CGFloat aBackingScale);
|
|
|
|
|
2007-12-06 02:17:08 +03:00
|
|
|
// Gives the location for the event in screen coordinates. Do not call this
|
|
|
|
// unless the window the event was originally targeted at is still alive!
|
2009-12-12 00:56:59 +03:00
|
|
|
// anEvent may be nil -- in that case the current mouse location is returned.
|
2007-12-06 02:17:08 +03:00
|
|
|
static NSPoint ScreenLocationForEvent(NSEvent* anEvent);
|
|
|
|
|
|
|
|
// Determines if an event happened over a window, whether or not the event
|
|
|
|
// is for the window. Does not take window z-order into account.
|
|
|
|
static BOOL IsEventOverWindow(NSEvent* anEvent, NSWindow* aWindow);
|
2009-08-18 06:36:15 +04:00
|
|
|
|
2007-12-06 02:17:08 +03:00
|
|
|
// Events are set up so that their coordinates refer to the window to which they
|
|
|
|
// were originally sent. If we reroute the event somewhere else, we'll have
|
|
|
|
// to get the window coordinates this way. Do not call this unless the window
|
|
|
|
// the event was originally targeted at is still alive!
|
|
|
|
static NSPoint EventLocationForWindow(NSEvent* anEvent, NSWindow* aWindow);
|
2008-03-27 06:42:57 +03:00
|
|
|
|
2011-08-18 02:30:52 +04:00
|
|
|
static BOOL IsMomentumScrollEvent(NSEvent* aEvent);
|
|
|
|
|
2009-07-22 13:01:37 +04:00
|
|
|
// Hides the Menu bar and the Dock. Multiple hide/show requests can be nested.
|
2011-09-29 10:19:26 +04:00
|
|
|
static void HideOSChromeOnScreen(bool aShouldHide, NSScreen* aScreen);
|
2009-07-22 13:01:37 +04:00
|
|
|
|
2008-03-27 06:42:57 +03:00
|
|
|
static nsIWidget* GetHiddenWindowWidget();
|
|
|
|
|
|
|
|
static void PrepareForNativeAppModalDialog();
|
|
|
|
static void CleanUpAfterNativeAppModalDialog();
|
2010-03-11 19:44:31 +03:00
|
|
|
|
|
|
|
// 3 utility functions to go from a frame of imgIContainer to CGImage and then to NSImage
|
|
|
|
// Convert imgIContainer -> CGImageRef, caller owns result
|
|
|
|
|
|
|
|
/** Creates a <code>CGImageRef</code> from a frame contained in an <code>imgIContainer</code>.
|
|
|
|
Copies the pixel data from the indicated frame of the <code>imgIContainer</code> into a new <code>CGImageRef</code>.
|
|
|
|
The caller owns the <code>CGImageRef</code>.
|
2010-03-25 19:07:36 +03:00
|
|
|
@param aFrame the frame to convert
|
2010-03-11 19:44:31 +03:00
|
|
|
@param aResult the resulting CGImageRef
|
|
|
|
@return NS_OK if the conversion worked, NS_ERROR_FAILURE otherwise
|
|
|
|
*/
|
2010-03-25 19:07:36 +03:00
|
|
|
static nsresult CreateCGImageFromSurface(gfxImageSurface *aFrame, CGImageRef *aResult);
|
2010-03-11 19:44:31 +03:00
|
|
|
|
|
|
|
/** Creates a Cocoa <code>NSImage</code> from a <code>CGImageRef</code>.
|
|
|
|
Copies the pixel data from the <code>CGImageRef</code> into a new <code>NSImage</code>.
|
|
|
|
The caller owns the <code>NSImage</code>.
|
|
|
|
@param aInputImage the image to convert
|
|
|
|
@param aResult the resulting NSImage
|
|
|
|
@return NS_OK if the conversion worked, NS_ERROR_FAILURE otherwise
|
|
|
|
*/
|
|
|
|
static nsresult CreateNSImageFromCGImage(CGImageRef aInputImage, NSImage **aResult);
|
|
|
|
|
|
|
|
/** Creates a Cocoa <code>NSImage</code> from a frame of an <code>imgIContainer</code>.
|
|
|
|
Combines the two methods above. The caller owns the <code>NSImage</code>.
|
|
|
|
@param aImage the image to extract a frame from
|
|
|
|
@param aWhichFrame the frame to extract (see imgIContainer FRAME_*)
|
|
|
|
@param aResult the resulting NSImage
|
|
|
|
@return NS_OK if the conversion worked, NS_ERROR_FAILURE otherwise
|
|
|
|
*/
|
2012-08-22 19:56:38 +04:00
|
|
|
static nsresult CreateNSImageFromImageContainer(imgIContainer *aImage, uint32_t aWhichFrame, NSImage **aResult);
|
2011-07-21 04:33:16 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns nsAString for aSrc.
|
|
|
|
*/
|
|
|
|
static void GetStringForNSString(const NSString *aSrc, nsAString& aDist);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Makes NSString instance for aString.
|
|
|
|
*/
|
|
|
|
static NSString* ToNSString(const nsAString& aString);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns NSRect for aGeckoRect.
|
2012-09-29 15:36:09 +04:00
|
|
|
* Just copies values between the two types; it does no coordinate-system
|
|
|
|
* conversion, so both rects must have the same coordinate origin/direction.
|
2011-07-21 04:33:16 +04:00
|
|
|
*/
|
|
|
|
static void GeckoRectToNSRect(const nsIntRect& aGeckoRect,
|
2012-09-29 15:36:09 +04:00
|
|
|
NSRect& aOutCocoaRect);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns Gecko rect for aCocoaRect.
|
|
|
|
* Just copies values between the two types; it does no coordinate-system
|
|
|
|
* conversion, so both rects must have the same coordinate origin/direction.
|
|
|
|
*/
|
|
|
|
static void NSRectToGeckoRect(const NSRect& aCocoaRect,
|
|
|
|
nsIntRect& aOutGeckoRect);
|
2011-07-21 04:33:16 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Makes NSEvent instance for aEventTytpe and aEvent.
|
|
|
|
*/
|
|
|
|
static NSEvent* MakeNewCocoaEventWithType(NSEventType aEventType,
|
|
|
|
NSEvent *aEvent);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes aNPCocoaEvent.
|
|
|
|
*/
|
|
|
|
static void InitNPCocoaEvent(NPCocoaEvent* aNPCocoaEvent);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes aPluginEvent for aCocoaEvent.
|
|
|
|
*/
|
|
|
|
static void InitPluginEvent(nsPluginEvent &aPluginEvent,
|
|
|
|
NPCocoaEvent &aCocoaEvent);
|
2012-04-25 07:00:02 +04:00
|
|
|
/**
|
|
|
|
* Initializes nsInputEvent for aNativeEvent or aModifiers.
|
|
|
|
*/
|
|
|
|
static void InitInputEvent(nsInputEvent &aInputEvent,
|
|
|
|
NSEvent* aNativeEvent);
|
|
|
|
static void InitInputEvent(nsInputEvent &aInputEvent,
|
|
|
|
NSUInteger aModifiers);
|
2012-05-16 04:28:48 +04:00
|
|
|
|
2012-10-16 05:43:50 +04:00
|
|
|
/**
|
|
|
|
* ConvertToCarbonModifier() returns carbon modifier flags for the cocoa
|
|
|
|
* modifier flags.
|
|
|
|
* NOTE: The result never includes right*Key.
|
|
|
|
*/
|
|
|
|
static UInt32 ConvertToCarbonModifier(NSUInteger aCocoaModifier);
|
|
|
|
|
2012-09-29 15:36:09 +04:00
|
|
|
/**
|
|
|
|
* Whether to support HiDPI rendering. For testing purposes, to be removed
|
|
|
|
* once we're comfortable with the HiDPI behavior.
|
|
|
|
*/
|
|
|
|
static bool HiDPIEnabled();
|
2007-12-06 02:17:08 +03:00
|
|
|
};
|
2007-02-23 00:56:50 +03:00
|
|
|
|
|
|
|
#endif // nsCocoaUtils_h_
|