Bumping manifests a=b2g-bump
[gecko.git] / layout / generic / nsPluginUtilsOSX.mm
blob8f7f3214e6ce02114db535f330ae4b5a58f78311
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"
13 #ifndef __LP64__
14 void NS_NPAPI_CarbonWindowFrame(WindowRef aWindow, nsRect& outRect)
16   if (!aWindow)
17     return;
19   Rect windowRect;
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;
26 #endif
28 void NS_NPAPI_CocoaWindowFrame(void* aWindow, nsRect& outRect)
30   NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
32   if (!aWindow)
33     return;
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;
41   else
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;
57   if (!aWindow)
58     return true;
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.
80   double y = 0.0;
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;
93   }
95   // Flip the coords to bottom-left origin.
96   NSRect viewFrame = [cocoaView frame];
97   double shiftedX = x;
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
109                                           timestamp:0
110                                        windowNumber:[[cocoaView window] windowNumber]
111                                             context:nil
112                                         eventNumber:0
113                                          clickCount:1
114                                            pressure:0.0];
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.
129   if (!inView) {
130     NS_WARNING("Must have a native view to convert coordinates.");
131     return false;
132   }
134   // Caller has to want a result.
135   if (!destX && !destY)
136     return false;
138   if (sourceSpace == destSpace) {
139     if (destX)
140       *destX = sourceX;
141     if (destY)
142       *destY = sourceY;
143     return true;
144   }
146   NSView* view = (NSView*)inView;
147   NSWindow* window = [view window];
148   NSPoint sourcePoint = NSMakePoint(sourceX, sourceY);
150   // Convert to screen space.
151   NSPoint screenPoint;
152   switch (sourceSpace) {
153     case NPCoordinateSpacePlugin:
154       screenPoint = [view convertPoint:sourcePoint toView:nil];
155       screenPoint = [window convertBaseToScreen:screenPoint];
156       break;
157     case NPCoordinateSpaceWindow:
158       screenPoint = [window convertBaseToScreen:sourcePoint];
159       break;
160     case NPCoordinateSpaceFlippedWindow:
161       sourcePoint.y = [window frame].size.height - sourcePoint.y;
162       screenPoint = [window convertBaseToScreen:sourcePoint];
163       break;
164     case NPCoordinateSpaceScreen:
165       screenPoint = sourcePoint;
166       break;
167     case NPCoordinateSpaceFlippedScreen:
168       sourcePoint.y = [[[NSScreen screens] objectAtIndex:0] frame].size.height - sourcePoint.y;
169       screenPoint = sourcePoint;
170       break;
171     default:
172       return false;
173   }
175   // Convert from screen to dest space.
176   NSPoint destPoint;
177   switch (destSpace) {
178     case NPCoordinateSpacePlugin:
179       destPoint = [window convertScreenToBase:screenPoint];
180       destPoint = [view convertPoint:destPoint fromView:nil];
181       break;
182     case NPCoordinateSpaceWindow:
183       destPoint = [window convertScreenToBase:screenPoint];
184       break;
185     case NPCoordinateSpaceFlippedWindow:
186       destPoint = [window convertScreenToBase:screenPoint];
187       destPoint.y = [window frame].size.height - destPoint.y;
188       break;
189     case NPCoordinateSpaceScreen:
190       destPoint = screenPoint;
191       break;
192     case NPCoordinateSpaceFlippedScreen:
193       destPoint = screenPoint;
194       destPoint.y = [[[NSScreen screens] objectAtIndex:0] frame].size.height - destPoint.y;
195       break;
196     default:
197       return false;
198   }
200   if (destX)
201     *destX = destPoint.x;
202   if (destY)
203     *destY = destPoint.y;
205   return true;