1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 // vim:set ts=2 sts=2 sw=2 et cin:
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "nsPluginUtilsOSX.h"
9 #import <Cocoa/Cocoa.h>
10 #import <QuartzCore/QuartzCore.h>
11 #include "nsObjCExceptions.h"
14 void NS_NPAPI_CarbonWindowFrame(WindowRef aWindow, nsRect& outRect)
20 ::GetWindowBounds(aWindow, kWindowStructureRgn, &windowRect);
21 outRect.x = windowRect.left;
22 outRect.y = windowRect.top;
23 outRect.width = windowRect.right - windowRect.left;
24 outRect.height = windowRect.bottom - windowRect.top;
28 void NS_NPAPI_CocoaWindowFrame(void* aWindow, nsRect& outRect)
30 NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
35 NSWindow* window = (NSWindow*)aWindow;
37 float menubarScreenHeight;
38 NSArray* allScreens = [NSScreen screens];
39 if ([allScreens count])
40 menubarScreenHeight = [[allScreens objectAtIndex:0] frame].size.height;
42 return; // If there are no screens, there's not much we can say.
44 NSRect frame = [window frame];
45 outRect.x = (nscoord)frame.origin.x;
46 outRect.y = (nscoord)(menubarScreenHeight - (frame.origin.y + frame.size.height));
47 outRect.width = (nscoord)frame.size.width;
48 outRect.height = (nscoord)frame.size.height;
50 NS_OBJC_END_TRY_ABORT_BLOCK;
53 bool NS_NPAPI_CocoaWindowIsMain(void* aWindow)
55 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN;
60 NSWindow* window = (NSWindow*)aWindow;
62 return (bool)[window isMainWindow];
64 NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(true);
67 NPError NS_NPAPI_ShowCocoaContextMenu(void* menu, nsIWidget* widget, NPCocoaEvent* event)
69 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN;
71 if (!menu || !widget || !event)
72 return NPERR_GENERIC_ERROR;
74 NSMenu* cocoaMenu = (NSMenu*)menu;
75 NSView* cocoaView = (NSView*)widget->GetNativeData(NS_NATIVE_WIDGET);
77 NSEventType cocoaEventType = NSRightMouseDown;
78 unsigned int cocoaModifierFlags = 0;
79 double x = 0.0; // Coordinates for the context menu in plugin terms, top-left origin.
82 NPCocoaEventType eventType = event->type;
83 if (eventType == NPCocoaEventMouseDown ||
84 eventType == NPCocoaEventMouseUp ||
85 eventType == NPCocoaEventMouseMoved ||
86 eventType == NPCocoaEventMouseEntered ||
87 eventType == NPCocoaEventMouseExited ||
88 eventType == NPCocoaEventMouseDragged) {
89 x = event->data.mouse.pluginX;
90 y = event->data.mouse.pluginY;
91 if ((x < 0.0) || (y < 0.0))
92 return NPERR_GENERIC_ERROR;
95 // Flip the coords to bottom-left origin.
96 NSRect viewFrame = [cocoaView frame];
98 double shiftedY = viewFrame.size.height - y;
99 // Shift to window coords.
100 shiftedX += viewFrame.origin.x;
101 shiftedY += [cocoaView convertPoint:NSMakePoint(0,0) toView:nil].y - viewFrame.size.height;
103 // Create an NSEvent we can use to show the context menu. Only the location
104 // is important here so just simulate a right mouse down. The coordinates
105 // must be in top-level window terms.
106 NSEvent* cocoaEvent = [NSEvent mouseEventWithType:cocoaEventType
107 location:NSMakePoint(shiftedX, shiftedY)
108 modifierFlags:cocoaModifierFlags
110 windowNumber:[[cocoaView window] windowNumber]
116 [NSMenu popUpContextMenu:cocoaMenu withEvent:cocoaEvent forView:cocoaView];
118 return NPERR_NO_ERROR;
120 NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(NPERR_GENERIC_ERROR);
123 NPBool NS_NPAPI_ConvertPointCocoa(void* inView,
124 double sourceX, double sourceY, NPCoordinateSpace sourceSpace,
125 double *destX, double *destY, NPCoordinateSpace destSpace)
127 // Plugins don't always have a view/frame. It would be odd to ask for a point conversion
128 // without a view, so we'll warn about it, but it's technically OK.
130 NS_WARNING("Must have a native view to convert coordinates.");
134 // Caller has to want a result.
135 if (!destX && !destY)
138 if (sourceSpace == destSpace) {
146 NSView* view = (NSView*)inView;
147 NSWindow* window = [view window];
148 NSPoint sourcePoint = NSMakePoint(sourceX, sourceY);
150 // Convert to screen space.
152 switch (sourceSpace) {
153 case NPCoordinateSpacePlugin:
154 screenPoint = [view convertPoint:sourcePoint toView:nil];
155 screenPoint = [window convertBaseToScreen:screenPoint];
157 case NPCoordinateSpaceWindow:
158 screenPoint = [window convertBaseToScreen:sourcePoint];
160 case NPCoordinateSpaceFlippedWindow:
161 sourcePoint.y = [window frame].size.height - sourcePoint.y;
162 screenPoint = [window convertBaseToScreen:sourcePoint];
164 case NPCoordinateSpaceScreen:
165 screenPoint = sourcePoint;
167 case NPCoordinateSpaceFlippedScreen:
168 sourcePoint.y = [[[NSScreen screens] objectAtIndex:0] frame].size.height - sourcePoint.y;
169 screenPoint = sourcePoint;
175 // Convert from screen to dest space.
178 case NPCoordinateSpacePlugin:
179 destPoint = [window convertScreenToBase:screenPoint];
180 destPoint = [view convertPoint:destPoint fromView:nil];
182 case NPCoordinateSpaceWindow:
183 destPoint = [window convertScreenToBase:screenPoint];
185 case NPCoordinateSpaceFlippedWindow:
186 destPoint = [window convertScreenToBase:screenPoint];
187 destPoint.y = [window frame].size.height - destPoint.y;
189 case NPCoordinateSpaceScreen:
190 destPoint = screenPoint;
192 case NPCoordinateSpaceFlippedScreen:
193 destPoint = screenPoint;
194 destPoint.y = [[[NSScreen screens] objectAtIndex:0] frame].size.height - destPoint.y;
201 *destX = destPoint.x;
203 *destY = destPoint.y;