michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: // vim:set ts=2 sts=2 sw=2 et cin: 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 "nsPluginUtilsOSX.h" michael@0: michael@0: #import michael@0: #import michael@0: #include "nsObjCExceptions.h" michael@0: michael@0: #ifndef __LP64__ michael@0: void NS_NPAPI_CarbonWindowFrame(WindowRef aWindow, nsRect& outRect) michael@0: { michael@0: if (!aWindow) michael@0: return; michael@0: michael@0: Rect windowRect; michael@0: ::GetWindowBounds(aWindow, kWindowStructureRgn, &windowRect); michael@0: outRect.x = windowRect.left; michael@0: outRect.y = windowRect.top; michael@0: outRect.width = windowRect.right - windowRect.left; michael@0: outRect.height = windowRect.bottom - windowRect.top; michael@0: } michael@0: #endif michael@0: michael@0: void NS_NPAPI_CocoaWindowFrame(void* aWindow, nsRect& outRect) michael@0: { michael@0: NS_OBJC_BEGIN_TRY_ABORT_BLOCK; michael@0: michael@0: if (!aWindow) michael@0: return; michael@0: michael@0: NSWindow* window = (NSWindow*)aWindow; michael@0: michael@0: float menubarScreenHeight; michael@0: NSArray* allScreens = [NSScreen screens]; michael@0: if ([allScreens count]) michael@0: menubarScreenHeight = [[allScreens objectAtIndex:0] frame].size.height; michael@0: else michael@0: return; // If there are no screens, there's not much we can say. michael@0: michael@0: NSRect frame = [window frame]; michael@0: outRect.x = (nscoord)frame.origin.x; michael@0: outRect.y = (nscoord)(menubarScreenHeight - (frame.origin.y + frame.size.height)); michael@0: outRect.width = (nscoord)frame.size.width; michael@0: outRect.height = (nscoord)frame.size.height; michael@0: michael@0: NS_OBJC_END_TRY_ABORT_BLOCK; michael@0: } michael@0: michael@0: bool NS_NPAPI_CocoaWindowIsMain(void* aWindow) michael@0: { michael@0: NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN; michael@0: michael@0: if (!aWindow) michael@0: return true; michael@0: michael@0: NSWindow* window = (NSWindow*)aWindow; michael@0: michael@0: return (bool)[window isMainWindow]; michael@0: michael@0: NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(true); michael@0: } michael@0: michael@0: NPError NS_NPAPI_ShowCocoaContextMenu(void* menu, nsIWidget* widget, NPCocoaEvent* event) michael@0: { michael@0: NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN; michael@0: michael@0: if (!menu || !widget || !event) michael@0: return NPERR_GENERIC_ERROR; michael@0: michael@0: NSMenu* cocoaMenu = (NSMenu*)menu; michael@0: NSView* cocoaView = (NSView*)widget->GetNativeData(NS_NATIVE_WIDGET); michael@0: michael@0: NSEventType cocoaEventType = NSRightMouseDown; michael@0: unsigned int cocoaModifierFlags = 0; michael@0: double x = 0.0; // Coordinates for the context menu in plugin terms, top-left origin. michael@0: double y = 0.0; michael@0: michael@0: NPCocoaEventType eventType = event->type; michael@0: if (eventType == NPCocoaEventMouseDown || michael@0: eventType == NPCocoaEventMouseUp || michael@0: eventType == NPCocoaEventMouseMoved || michael@0: eventType == NPCocoaEventMouseEntered || michael@0: eventType == NPCocoaEventMouseExited || michael@0: eventType == NPCocoaEventMouseDragged) { michael@0: x = event->data.mouse.pluginX; michael@0: y = event->data.mouse.pluginY; michael@0: if ((x < 0.0) || (y < 0.0)) michael@0: return NPERR_GENERIC_ERROR; michael@0: } michael@0: michael@0: // Flip the coords to bottom-left origin. michael@0: NSRect viewFrame = [cocoaView frame]; michael@0: double shiftedX = x; michael@0: double shiftedY = viewFrame.size.height - y; michael@0: // Shift to window coords. michael@0: shiftedX += viewFrame.origin.x; michael@0: shiftedY += [cocoaView convertPoint:NSMakePoint(0,0) toView:nil].y - viewFrame.size.height; michael@0: michael@0: // Create an NSEvent we can use to show the context menu. Only the location michael@0: // is important here so just simulate a right mouse down. The coordinates michael@0: // must be in top-level window terms. michael@0: NSEvent* cocoaEvent = [NSEvent mouseEventWithType:cocoaEventType michael@0: location:NSMakePoint(shiftedX, shiftedY) michael@0: modifierFlags:cocoaModifierFlags michael@0: timestamp:0 michael@0: windowNumber:[[cocoaView window] windowNumber] michael@0: context:nil michael@0: eventNumber:0 michael@0: clickCount:1 michael@0: pressure:0.0]; michael@0: michael@0: [NSMenu popUpContextMenu:cocoaMenu withEvent:cocoaEvent forView:cocoaView]; michael@0: michael@0: return NPERR_NO_ERROR; michael@0: michael@0: NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(NPERR_GENERIC_ERROR); michael@0: } michael@0: michael@0: NPBool NS_NPAPI_ConvertPointCocoa(void* inView, michael@0: double sourceX, double sourceY, NPCoordinateSpace sourceSpace, michael@0: double *destX, double *destY, NPCoordinateSpace destSpace) michael@0: { michael@0: // Plugins don't always have a view/frame. It would be odd to ask for a point conversion michael@0: // without a view, so we'll warn about it, but it's technically OK. michael@0: if (!inView) { michael@0: NS_WARNING("Must have a native view to convert coordinates."); michael@0: return false; michael@0: } michael@0: michael@0: // Caller has to want a result. michael@0: if (!destX && !destY) michael@0: return false; michael@0: michael@0: if (sourceSpace == destSpace) { michael@0: if (destX) michael@0: *destX = sourceX; michael@0: if (destY) michael@0: *destY = sourceY; michael@0: return true; michael@0: } michael@0: michael@0: NSView* view = (NSView*)inView; michael@0: NSWindow* window = [view window]; michael@0: NSPoint sourcePoint = NSMakePoint(sourceX, sourceY); michael@0: michael@0: // Convert to screen space. michael@0: NSPoint screenPoint; michael@0: switch (sourceSpace) { michael@0: case NPCoordinateSpacePlugin: michael@0: screenPoint = [view convertPoint:sourcePoint toView:nil]; michael@0: screenPoint = [window convertBaseToScreen:screenPoint]; michael@0: break; michael@0: case NPCoordinateSpaceWindow: michael@0: screenPoint = [window convertBaseToScreen:sourcePoint]; michael@0: break; michael@0: case NPCoordinateSpaceFlippedWindow: michael@0: sourcePoint.y = [window frame].size.height - sourcePoint.y; michael@0: screenPoint = [window convertBaseToScreen:sourcePoint]; michael@0: break; michael@0: case NPCoordinateSpaceScreen: michael@0: screenPoint = sourcePoint; michael@0: break; michael@0: case NPCoordinateSpaceFlippedScreen: michael@0: sourcePoint.y = [[[NSScreen screens] objectAtIndex:0] frame].size.height - sourcePoint.y; michael@0: screenPoint = sourcePoint; michael@0: break; michael@0: default: michael@0: return false; michael@0: } michael@0: michael@0: // Convert from screen to dest space. michael@0: NSPoint destPoint; michael@0: switch (destSpace) { michael@0: case NPCoordinateSpacePlugin: michael@0: destPoint = [window convertScreenToBase:screenPoint]; michael@0: destPoint = [view convertPoint:destPoint fromView:nil]; michael@0: break; michael@0: case NPCoordinateSpaceWindow: michael@0: destPoint = [window convertScreenToBase:screenPoint]; michael@0: break; michael@0: case NPCoordinateSpaceFlippedWindow: michael@0: destPoint = [window convertScreenToBase:screenPoint]; michael@0: destPoint.y = [window frame].size.height - destPoint.y; michael@0: break; michael@0: case NPCoordinateSpaceScreen: michael@0: destPoint = screenPoint; michael@0: break; michael@0: case NPCoordinateSpaceFlippedScreen: michael@0: destPoint = screenPoint; michael@0: destPoint.y = [[[NSScreen screens] objectAtIndex:0] frame].size.height - destPoint.y; michael@0: break; michael@0: default: michael@0: return false; michael@0: } michael@0: michael@0: if (destX) michael@0: *destX = destPoint.x; michael@0: if (destY) michael@0: *destY = destPoint.y; michael@0: michael@0: return true; michael@0: } michael@0: