/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* 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/. */ #ifndef nsCocoaUtils_h_ #define nsCocoaUtils_h_ #import typedef float CGFloat; #include "nsRect.h" #include "imgIContainer.h" #include "npapi.h" #include "nsTArray.h" #include "Units.h" // This must be the last include: #include "nsObjCExceptions.h" #include "mozilla/EventForwards.h" // Declare the backingScaleFactor method that we want to call // on NSView/Window/Screen objects, if they recognize it. @interface NSObject (BackingScaleFactorCategory) - (CGFloat)backingScaleFactor; @end // When building with a pre-10.7 SDK, NSEventPhase is not defined. #if !defined(MAC_OS_X_VERSION_10_7) || MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7 enum { NSEventPhaseNone = 0, NSEventPhaseBegan = 0x1 << 0, NSEventPhaseStationary = 0x1 << 1, NSEventPhaseChanged = 0x1 << 2, NSEventPhaseEnded = 0x1 << 3, NSEventPhaseCancelled = 0x1 << 4, }; typedef uint32_t NSEventPhase; #endif // #if !defined(MAC_OS_X_VERSION_10_7) || MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7 #if !defined(MAC_OS_X_VERSION_10_8) || MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_8 enum { NSEventPhaseMayBegin = 0x1 << 5 }; #endif // #if !defined(MAC_OS_X_VERSION_10_8) || MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_8 class nsIWidget; #if defined(MAC_OS_X_VERSION_10_5) && (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5) #define NS_LEOPARD_AND_LATER 1 #error The 10.4 SDK is the only supported target! #endif // "Borrowed" in part from the QTKit framework's QTKitDefines.h. This is // needed when building on OS X Tiger (10.4.X) or with a 10.4 SDK. It won't // be used when building on Leopard (10.5.X) or higher (or with a 10.5 or // higher SDK). // // These definitions for NSInteger and NSUInteger are the 32-bit ones -- since // we assume we'll always be building 32-bit binaries when building on Tiger // (or with a 10.4 SDK). #ifndef NSINTEGER_DEFINED typedef int NSInteger; typedef unsigned int NSUInteger; #define NSIntegerMax LONG_MAX #define NSIntegerMin LONG_MIN #define NSUIntegerMax ULONG_MAX #define NSINTEGER_DEFINED 1 #endif /* NSINTEGER_DEFINED */ #ifndef CGFLOAT_DEFINED typedef float CGFloat; # define CGFLOAT_MIN FLT_MIN # define CGFLOAT_MAX FLT_MAX # define CGFLOAT_IS_DOUBLE 0 # define CGFLOAT_DEFINED 1 #endif namespace mozilla { namespace gfx { class SourceSurface; } // namespace gfx } // namespace mozilla // Used to retain a Cocoa object for the remainder of a method's execution. class nsAutoRetainCocoaObject { public: explicit nsAutoRetainCocoaObject(id anObject) { mObject = NS_OBJC_TRY_EXPR_ABORT([anObject retain]); } ~nsAutoRetainCocoaObject() { NS_OBJC_TRY_ABORT([mObject release]); } private: id mObject; // [STRONG] }; // 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; }; @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). // Present in all versions of OS X from (at least) 10.2.8 through 10.5. - (void)_removeWindowFromCache:(NSWindow *)aWindow; // 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; // 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; @end struct KeyBindingsCommand { SEL selector; id data; }; @interface NativeKeyBindingsRecorder : NSResponder { @private nsTArray* mCommands; } - (void)startRecording:(nsTArray&)aCommands; - (void)doCommandBySelector:(SEL)aSelector; - (void)insertText:(id)aString; @end // NativeKeyBindingsRecorder class nsCocoaUtils { typedef mozilla::gfx::SourceSurface SourceSurface; typedef mozilla::LayoutDeviceIntPoint LayoutDeviceIntPoint; typedef mozilla::LayoutDeviceIntRect LayoutDeviceIntRect; public: #if(0) // 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 LayoutDeviceIntPoint CocoaPointsToDevPixels(const NSPoint& aPt, CGFloat aBackingScale) { return LayoutDeviceIntPoint(NSToIntRound(aPt.x * aBackingScale), NSToIntRound(aPt.y * aBackingScale)); } static LayoutDeviceIntRect CocoaPointsToDevPixels(const NSRect& aRect, CGFloat aBackingScale) { return LayoutDeviceIntRect(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 mozilla::LayoutDeviceIntPoint& aPt, CGFloat aBackingScale) { return NSMakePoint((CGFloat)aPt.x / aBackingScale, (CGFloat)aPt.y / aBackingScale); } // XXX: all calls to this function should eventually be replaced with calls // to DevPixelsToCocoaPoints(). static NSRect UntypedDevPixelsToCocoaPoints(const nsIntRect& aRect, CGFloat aBackingScale) { return NSMakeRect((CGFloat)aRect.x / aBackingScale, (CGFloat)aRect.y / aBackingScale, (CGFloat)aRect.width / aBackingScale, (CGFloat)aRect.height / aBackingScale); } static NSRect DevPixelsToCocoaPoints(const LayoutDeviceIntRect& aRect, CGFloat aBackingScale) { return NSMakeRect((CGFloat)aRect.x / aBackingScale, (CGFloat)aRect.y / aBackingScale, (CGFloat)aRect.width / aBackingScale, (CGFloat)aRect.height / aBackingScale); } #else // Conversions between Cocoa points and device pixels. static int32_t CocoaPointsToDevPixels(CGFloat aPts) { return NSToIntRound(aPts); } static LayoutDeviceIntPoint CocoaPointsToDevPixels(const NSPoint& aPt) { return LayoutDeviceIntPoint(NSToIntRound(aPt.x), NSToIntRound(aPt.y)); } static LayoutDeviceIntRect CocoaPointsToDevPixels(const NSRect& aRect) { return LayoutDeviceIntRect(NSToIntRound(aRect.origin.x), NSToIntRound(aRect.origin.y), NSToIntRound(aRect.size.width), NSToIntRound(aRect.size.height)); } static CGFloat DevPixelsToCocoaPoints(int32_t aPixels) { return (CGFloat)aPixels; } static NSPoint DevPixelsToCocoaPoints(const mozilla::LayoutDeviceIntPoint& aPt) { return NSMakePoint((CGFloat)aPt.x, (CGFloat)aPt.y); } // XXX: all calls to this function should eventually be replaced with calls // to DevPixelsToCocoaPoints(). static NSRect UntypedDevPixelsToCocoaPoints(const nsIntRect& aRect) { return NSMakeRect((CGFloat)aRect.x, (CGFloat)aRect.y, (CGFloat)aRect.width, (CGFloat)aRect.height); } static NSRect DevPixelsToCocoaPoints(const LayoutDeviceIntRect& aRect) { return NSMakeRect((CGFloat)aRect.x, (CGFloat)aRect.y, (CGFloat)aRect.width, (CGFloat)aRect.height); } #endif // 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); // 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. // 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. // This function does no scaling, so the Gecko coordinates are // expected to be CSS pixels, which we treat as equal to Cocoa points. static NSRect GeckoRectToCocoaRect(const nsIntRect &geckoRect); // Converts aGeckoRect in dev pixels to points in Cocoa coordinates static NSRect GeckoRectToCocoaRectDevPix(const nsIntRect &aGeckoRect); // See explanation for geckoRectToCocoaRect, guess what this does... static nsIntRect CocoaRectToGeckoRect(const NSRect &cocoaRect); static mozilla::LayoutDeviceIntRect CocoaRectToGeckoRectDevPix( const NSRect& aCocoaRect); // 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! // anEvent may be nil -- in that case the current mouse location is returned. 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); // 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); // Compatibility wrappers for the -[NSEvent phase], -[NSEvent momentumPhase], // -[NSEvent hasPreciseScrollingDeltas] and -[NSEvent scrollingDeltaX/Y] APIs // that became availaible starting with the 10.7 SDK. // All of these can be removed once we drop support for 10.6. static NSEventPhase EventPhase(NSEvent* aEvent); static NSEventPhase EventMomentumPhase(NSEvent* aEvent); static BOOL IsMomentumScrollEvent(NSEvent* aEvent); static BOOL HasPreciseScrollingDeltas(NSEvent* aEvent); static void GetScrollingDeltas(NSEvent* aEvent, CGFloat* aOutDeltaX, CGFloat* aOutDeltaY); static BOOL EventHasPhaseInformation(NSEvent* aEvent); // Hides the Menu bar and the Dock. Multiple hide/show requests can be nested. static void HideOSChromeOnScreen(bool aShouldHide, NSScreen* aScreen); static nsIWidget* GetHiddenWindowWidget(); static void PrepareForNativeAppModalDialog(); static void CleanUpAfterNativeAppModalDialog(); // Wrap calls to [theEvent keyCode] and [theEvent modifierFlags]. Needed to // work around an Apple bug (on OS X 10.4.X) that causes ctrl-ESC key events // sent via performKeyEquivalent: to return 0 on these calls. static unsigned short GetCocoaEventKeyCode(NSEvent *theEvent); static NSUInteger GetCocoaEventModifierFlags(NSEvent *theEvent); // 3 utility functions to go from a frame of imgIContainer to CGImage and then to NSImage // Convert imgIContainer -> CGImageRef, caller owns result /** Creates a CGImageRef from a frame contained in an imgIContainer. Copies the pixel data from the indicated frame of the imgIContainer into a new CGImageRef. The caller owns the CGImageRef. @param aFrame the frame to convert @param aResult the resulting CGImageRef @return NS_OK if the conversion worked, NS_ERROR_FAILURE otherwise */ static nsresult CreateCGImageFromSurface(SourceSurface* aSurface, CGImageRef* aResult); /** Creates a Cocoa NSImage from a CGImageRef. Copies the pixel data from the CGImageRef into a new NSImage. The caller owns the NSImage. @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 NSImage from a frame of an imgIContainer. Combines the two methods above. The caller owns the NSImage. @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 */ static nsresult CreateNSImageFromImageContainer(imgIContainer *aImage, uint32_t aWhichFrame, NSImage **aResult); /** * 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. * 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 GeckoRectToNSRect(const nsIntRect& aGeckoRect, 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); /** * Makes NSEvent instance for aEventTytpe and aEvent. */ static NSEvent* MakeNewCocoaEventWithType(NSEventType aEventType, NSEvent *aEvent); /** * Initializes aNPCocoaEvent. */ static void InitNPCocoaEvent(NPCocoaEvent* aNPCocoaEvent); /** * Initializes WidgetInputEvent for aNativeEvent or aModifiers. */ static void InitInputEvent(mozilla::WidgetInputEvent &aInputEvent, NSEvent* aNativeEvent); /** * Converts the native modifiers from aNativeEvent into WidgetMouseEvent * Modifiers. aNativeEvent can be null. */ static mozilla::Modifiers ModifiersForEvent(NSEvent* aNativeEvent); /** * ConvertToCarbonModifier() returns carbon modifier flags for the cocoa * modifier flags. * NOTE: The result never includes right*Key. */ static UInt32 ConvertToCarbonModifier(NSUInteger aCocoaModifier); /** * Whether to support HiDPI rendering. For testing purposes, to be removed * once we're comfortable with the HiDPI behavior. */ static bool HiDPIEnabled(); /** * Keys can optionally be bound by system or user key bindings to one or more * commands based on selectors. This collects any such commands in the * provided array. */ static void GetCommandsFromKeyEvent(NSEvent* aEvent, nsTArray& aCommands); /** * Converts the string name of a Gecko key (like "VK_HOME") to the * corresponding Cocoa Unicode character. */ static uint32_t ConvertGeckoNameToMacCharCode(const nsAString& aKeyCodeName); /** * Converts a Gecko key code (like NS_VK_HOME) to the corresponding Cocoa * Unicode character. */ static uint32_t ConvertGeckoKeyCodeToMacCharCode(uint32_t aKeyCode); }; #endif // nsCocoaUtils_h_