michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "imgIContainer.h" michael@0: #include "nsCocoaUtils.h" michael@0: #include "nsCursorManager.h" michael@0: #include "nsObjCExceptions.h" michael@0: #include michael@0: michael@0: static nsCursorManager *gInstance; michael@0: static CGFloat sCursorScaleFactor = 0.0f; michael@0: static imgIContainer *sCursorImgContainer = nullptr; michael@0: static const nsCursor sCustomCursor = eCursorCount; michael@0: michael@0: /*! @category nsCursorManager(PrivateMethods) michael@0: Private methods for the cursor manager class. michael@0: */ michael@0: @interface nsCursorManager(PrivateMethods) michael@0: /*! @method getCursor: michael@0: @abstract Get a reference to the native Mac representation of a cursor. michael@0: @discussion Gets a reference to the Mac native implementation of a cursor. michael@0: If the cursor has been requested before, it is retreived from the cursor cache, michael@0: otherwise it is created and cached. michael@0: @param aCursor the cursor to get michael@0: @result the Mac native implementation of the cursor michael@0: */ michael@0: - (nsMacCursor *) getCursor: (nsCursor) aCursor; michael@0: michael@0: /*! @method setMacCursor: michael@0: @abstract Set the current Mac native cursor michael@0: @discussion Sets the current cursor - this routine is what actually causes the cursor to change. michael@0: The argument is retained and the old cursor is released. michael@0: @param aMacCursor the cursor to set michael@0: @result NS_OK michael@0: */ michael@0: - (nsresult) setMacCursor: (nsMacCursor*) aMacCursor; michael@0: michael@0: /*! @method createCursor: michael@0: @abstract Create a Mac native representation of a cursor. michael@0: @discussion Creates a version of the Mac native representation of this cursor michael@0: @param aCursor the cursor to create michael@0: @result the Mac native implementation of the cursor michael@0: */ michael@0: + (nsMacCursor *) createCursor: (enum nsCursor) aCursor; michael@0: michael@0: @end michael@0: michael@0: @implementation nsCursorManager michael@0: michael@0: + (nsCursorManager *) sharedInstance michael@0: { michael@0: NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NIL; michael@0: michael@0: if (!gInstance) { michael@0: gInstance = [[nsCursorManager alloc] init]; michael@0: } michael@0: return gInstance; michael@0: michael@0: NS_OBJC_END_TRY_ABORT_BLOCK_NIL; michael@0: } michael@0: michael@0: + (void) dispose michael@0: { michael@0: NS_OBJC_BEGIN_TRY_ABORT_BLOCK; michael@0: michael@0: [gInstance release]; michael@0: gInstance = nil; michael@0: michael@0: NS_OBJC_END_TRY_ABORT_BLOCK; michael@0: } michael@0: michael@0: + (nsMacCursor *) createCursor: (enum nsCursor) aCursor michael@0: { michael@0: NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NIL; michael@0: michael@0: switch(aCursor) michael@0: { michael@0: SEL cursorSelector; michael@0: case eCursor_standard: michael@0: return [nsMacCursor cursorWithCursor:[NSCursor arrowCursor] type:aCursor]; michael@0: case eCursor_wait: michael@0: case eCursor_spinning: michael@0: { michael@0: return [nsMacCursor cursorWithCursor:[NSCursor busyButClickableCursor] type:aCursor]; michael@0: } michael@0: case eCursor_select: michael@0: return [nsMacCursor cursorWithCursor:[NSCursor IBeamCursor] type:aCursor]; michael@0: case eCursor_hyperlink: michael@0: return [nsMacCursor cursorWithCursor:[NSCursor pointingHandCursor] type:aCursor]; michael@0: case eCursor_crosshair: michael@0: return [nsMacCursor cursorWithCursor:[NSCursor crosshairCursor] type:aCursor]; michael@0: case eCursor_move: michael@0: return [nsMacCursor cursorWithImageNamed:@"move" hotSpot:NSMakePoint(12,12) type:aCursor]; michael@0: case eCursor_help: michael@0: return [nsMacCursor cursorWithImageNamed:@"help" hotSpot:NSMakePoint(12,12) type:aCursor]; michael@0: case eCursor_copy: michael@0: cursorSelector = @selector(dragCopyCursor); michael@0: return [nsMacCursor cursorWithCursor:[NSCursor respondsToSelector:cursorSelector] ? michael@0: [NSCursor performSelector:cursorSelector] : michael@0: [NSCursor arrowCursor] type:aCursor]; michael@0: case eCursor_alias: michael@0: cursorSelector = @selector(dragLinkCursor); michael@0: return [nsMacCursor cursorWithCursor:[NSCursor respondsToSelector:cursorSelector] ? michael@0: [NSCursor performSelector:cursorSelector] : michael@0: [NSCursor arrowCursor] type:aCursor]; michael@0: case eCursor_context_menu: michael@0: cursorSelector = @selector(contextualMenuCursor); michael@0: return [nsMacCursor cursorWithCursor:[NSCursor respondsToSelector:cursorSelector] ? michael@0: [NSCursor performSelector:cursorSelector] : michael@0: [NSCursor arrowCursor] type:aCursor]; michael@0: case eCursor_cell: michael@0: return [nsMacCursor cursorWithImageNamed:@"cell" hotSpot:NSMakePoint(12,12) type:aCursor]; michael@0: case eCursor_grab: michael@0: return [nsMacCursor cursorWithCursor:[NSCursor openHandCursor] type:aCursor]; michael@0: case eCursor_grabbing: michael@0: return [nsMacCursor cursorWithCursor:[NSCursor closedHandCursor] type:aCursor]; michael@0: case eCursor_zoom_in: michael@0: return [nsMacCursor cursorWithImageNamed:@"zoomIn" hotSpot:NSMakePoint(10,10) type:aCursor]; michael@0: case eCursor_zoom_out: michael@0: return [nsMacCursor cursorWithImageNamed:@"zoomOut" hotSpot:NSMakePoint(10,10) type:aCursor]; michael@0: case eCursor_vertical_text: michael@0: return [nsMacCursor cursorWithImageNamed:@"vtIBeam" hotSpot:NSMakePoint(12,11) type:aCursor]; michael@0: case eCursor_all_scroll: michael@0: return [nsMacCursor cursorWithCursor:[NSCursor openHandCursor] type:aCursor]; michael@0: case eCursor_not_allowed: michael@0: case eCursor_no_drop: michael@0: cursorSelector = @selector(operationNotAllowedCursor); michael@0: return [nsMacCursor cursorWithCursor:[NSCursor respondsToSelector:cursorSelector] ? michael@0: [NSCursor performSelector:cursorSelector] : michael@0: [NSCursor arrowCursor] type:aCursor]; michael@0: // Resize Cursors: michael@0: // North michael@0: case eCursor_n_resize: michael@0: return [nsMacCursor cursorWithCursor:[NSCursor resizeUpCursor] type:aCursor]; michael@0: // North East michael@0: case eCursor_ne_resize: michael@0: return [nsMacCursor cursorWithImageNamed:@"sizeNE" hotSpot:NSMakePoint(12,11) type:aCursor]; michael@0: // East michael@0: case eCursor_e_resize: michael@0: return [nsMacCursor cursorWithCursor:[NSCursor resizeRightCursor] type:aCursor]; michael@0: // South East michael@0: case eCursor_se_resize: michael@0: return [nsMacCursor cursorWithImageNamed:@"sizeSE" hotSpot:NSMakePoint(12,12) type:aCursor]; michael@0: // South michael@0: case eCursor_s_resize: michael@0: return [nsMacCursor cursorWithCursor:[NSCursor resizeDownCursor] type:aCursor]; michael@0: // South West michael@0: case eCursor_sw_resize: michael@0: return [nsMacCursor cursorWithImageNamed:@"sizeSW" hotSpot:NSMakePoint(10,12) type:aCursor]; michael@0: // West michael@0: case eCursor_w_resize: michael@0: return [nsMacCursor cursorWithCursor:[NSCursor resizeLeftCursor] type:aCursor]; michael@0: // North West michael@0: case eCursor_nw_resize: michael@0: return [nsMacCursor cursorWithImageNamed:@"sizeNW" hotSpot:NSMakePoint(11,11) type:aCursor]; michael@0: // North & South michael@0: case eCursor_ns_resize: michael@0: return [nsMacCursor cursorWithCursor:[NSCursor resizeUpDownCursor] type:aCursor]; michael@0: // East & West michael@0: case eCursor_ew_resize: michael@0: return [nsMacCursor cursorWithCursor:[NSCursor resizeLeftRightCursor] type:aCursor]; michael@0: // North East & South West michael@0: case eCursor_nesw_resize: michael@0: return [nsMacCursor cursorWithImageNamed:@"sizeNESW" hotSpot:NSMakePoint(12,12) type:aCursor]; michael@0: // North West & South East michael@0: case eCursor_nwse_resize: michael@0: return [nsMacCursor cursorWithImageNamed:@"sizeNWSE" hotSpot:NSMakePoint(12,12) type:aCursor]; michael@0: // Column Resize michael@0: case eCursor_col_resize: michael@0: return [nsMacCursor cursorWithImageNamed:@"colResize" hotSpot:NSMakePoint(12,12) type:aCursor]; michael@0: // Row Resize michael@0: case eCursor_row_resize: michael@0: return [nsMacCursor cursorWithImageNamed:@"rowResize" hotSpot:NSMakePoint(12,12) type:aCursor]; michael@0: default: michael@0: return [nsMacCursor cursorWithCursor:[NSCursor arrowCursor] type:aCursor]; michael@0: } michael@0: michael@0: NS_OBJC_END_TRY_ABORT_BLOCK_NIL; michael@0: } michael@0: michael@0: - (id) init michael@0: { michael@0: NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NIL; michael@0: michael@0: if ((self = [super init])) { michael@0: mCursors = [[NSMutableDictionary alloc] initWithCapacity:25]; michael@0: } michael@0: return self; michael@0: michael@0: NS_OBJC_END_TRY_ABORT_BLOCK_NIL; michael@0: } michael@0: michael@0: - (nsresult) setCursor: (enum nsCursor) aCursor michael@0: { michael@0: NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; michael@0: michael@0: // Some plugins mess with our cursors and set a cursor that even michael@0: // [NSCursor currentCursor] doesn't know about. In case that happens, just michael@0: // reset the state. michael@0: [[NSCursor currentCursor] set]; michael@0: michael@0: nsCursor oldType = [mCurrentMacCursor type]; michael@0: if (oldType != aCursor) { michael@0: if (aCursor == eCursor_none) { michael@0: [NSCursor hide]; michael@0: } else if (oldType == eCursor_none) { michael@0: [NSCursor unhide]; michael@0: } michael@0: } michael@0: [self setMacCursor:[self getCursor:aCursor]]; michael@0: michael@0: // if a custom cursor was previously set, release sCursorImgContainer michael@0: if (oldType == sCustomCursor) { michael@0: NS_IF_RELEASE(sCursorImgContainer); michael@0: } michael@0: return NS_OK; michael@0: michael@0: NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; michael@0: } michael@0: michael@0: - (nsresult) setMacCursor: (nsMacCursor*) aMacCursor michael@0: { michael@0: NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; michael@0: michael@0: if (mCurrentMacCursor != aMacCursor || ![mCurrentMacCursor isSet]) { michael@0: [aMacCursor retain]; michael@0: [mCurrentMacCursor unset]; michael@0: [aMacCursor set]; michael@0: [mCurrentMacCursor release]; michael@0: mCurrentMacCursor = aMacCursor; michael@0: } michael@0: michael@0: return NS_OK; michael@0: michael@0: NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; michael@0: } michael@0: michael@0: - (nsresult) setCursorWithImage: (imgIContainer*) aCursorImage hotSpotX: (uint32_t) aHotspotX hotSpotY: (uint32_t) aHotspotY scaleFactor: (CGFloat) scaleFactor michael@0: { michael@0: NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; michael@0: // As the user moves the mouse, this gets called repeatedly with the same aCursorImage michael@0: if (sCursorImgContainer == aCursorImage && sCursorScaleFactor == scaleFactor && mCurrentMacCursor) { michael@0: [self setMacCursor:mCurrentMacCursor]; michael@0: return NS_OK; michael@0: } michael@0: michael@0: [[NSCursor currentCursor] set]; michael@0: int32_t width = 0, height = 0; michael@0: aCursorImage->GetWidth(&width); michael@0: aCursorImage->GetHeight(&height); michael@0: // prevent DoS attacks michael@0: if (width > 128 || height > 128) { michael@0: return NS_OK; michael@0: } michael@0: michael@0: NSImage *cursorImage; michael@0: nsresult rv = nsCocoaUtils::CreateNSImageFromImageContainer(aCursorImage, imgIContainer::FRAME_FIRST, &cursorImage, scaleFactor); michael@0: if (NS_FAILED(rv) || !cursorImage) { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: // if the hotspot is nonsensical, make it 0,0 michael@0: aHotspotX = (aHotspotX > (uint32_t)width - 1) ? 0 : aHotspotX; michael@0: aHotspotY = (aHotspotY > (uint32_t)height - 1) ? 0 : aHotspotY; michael@0: michael@0: NSPoint hotSpot = ::NSMakePoint(aHotspotX, aHotspotY); michael@0: [self setMacCursor:[nsMacCursor cursorWithCursor:[[NSCursor alloc] initWithImage:cursorImage hotSpot:hotSpot] type:sCustomCursor]]; michael@0: [cursorImage release]; michael@0: michael@0: NS_IF_RELEASE(sCursorImgContainer); michael@0: sCursorImgContainer = aCursorImage; michael@0: sCursorScaleFactor = scaleFactor; michael@0: NS_ADDREF(sCursorImgContainer); michael@0: michael@0: return NS_OK; michael@0: michael@0: NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; michael@0: } michael@0: michael@0: - (nsMacCursor *) getCursor: (enum nsCursor) aCursor michael@0: { michael@0: NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NIL; michael@0: michael@0: nsMacCursor * result = [mCursors objectForKey:[NSNumber numberWithInt:aCursor]]; michael@0: if (!result) { michael@0: result = [nsCursorManager createCursor:aCursor]; michael@0: [mCursors setObject:result forKey:[NSNumber numberWithInt:aCursor]]; michael@0: } michael@0: return result; michael@0: michael@0: NS_OBJC_END_TRY_ABORT_BLOCK_NIL; michael@0: } michael@0: michael@0: - (void) dealloc michael@0: { michael@0: NS_OBJC_BEGIN_TRY_ABORT_BLOCK; michael@0: michael@0: [mCurrentMacCursor unset]; michael@0: [mCurrentMacCursor release]; michael@0: [mCursors release]; michael@0: NS_IF_RELEASE(sCursorImgContainer); michael@0: [super dealloc]; michael@0: michael@0: NS_OBJC_END_TRY_ABORT_BLOCK; michael@0: } michael@0: michael@0: @end