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 /* ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
16 * The Original Code is Mozilla.org code.
18 * The Initial Developer of the Original Code is
19 * Mozilla Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 2008
21 * the Initial Developer. All Rights Reserved.
24 * Josh Aas <josh@mozilla.com>
26 * Alternatively, the contents of this file may be used under the terms of
27 * either of the GNU General Public License Version 2 or later (the "GPL"),
28 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
40 #include "nsPluginUtilsOSX.h"
42 #import <Cocoa/Cocoa.h>
43 #include "nsObjCExceptions.h"
45 void NS_NPAPI_CarbonWindowFrame(WindowRef aWindow, nsRect& outRect)
51 ::GetWindowBounds(aWindow, kWindowStructureRgn, &windowRect);
52 outRect.x = windowRect.left;
53 outRect.y = windowRect.top;
54 outRect.width = windowRect.right - windowRect.left;
55 outRect.height = windowRect.bottom - windowRect.top;
58 void NS_NPAPI_CocoaWindowFrame(void* aWindow, nsRect& outRect)
60 NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
65 NSWindow* window = (NSWindow*)aWindow;
67 float menubarScreenHeight;
68 NSArray* allScreens = [NSScreen screens];
69 if ([allScreens count])
70 menubarScreenHeight = [[allScreens objectAtIndex:0] frame].size.height;
72 return; // If there are no screens, there's not much we can say.
74 NSRect frame = [window frame];
75 outRect.x = (nscoord)frame.origin.x;
76 outRect.y = (nscoord)(menubarScreenHeight - (frame.origin.y + frame.size.height));
77 outRect.width = (nscoord)frame.size.width;
78 outRect.height = (nscoord)frame.size.height;
80 NS_OBJC_END_TRY_ABORT_BLOCK;
83 NPError NS_NPAPI_ShowCocoaContextMenu(void* menu, nsIWidget* widget, NPCocoaEvent* event)
85 if (!menu || !widget || !event)
86 return NPERR_GENERIC_ERROR;
88 NSMenu* cocoaMenu = (NSMenu*)menu;
89 NSView* cocoaView = (NSView*)widget->GetNativeData(NS_NATIVE_WIDGET);
91 NSEventType cocoaEventType = NSRightMouseDown;
92 unsigned int cocoaModifierFlags = 0;
93 double x = 0.0; // Coordinates for the context menu in plugin terms, top-left origin.
96 NPCocoaEventType eventType = event->type;
97 if (eventType == NPCocoaEventMouseDown ||
98 eventType == NPCocoaEventMouseUp ||
99 eventType == NPCocoaEventMouseMoved ||
100 eventType == NPCocoaEventMouseEntered ||
101 eventType == NPCocoaEventMouseExited ||
102 eventType == NPCocoaEventMouseDragged) {
103 cocoaEventType = (NSEventType)event->data.mouse.buttonNumber;
104 cocoaModifierFlags = event->data.mouse.modifierFlags;
105 x = event->data.mouse.pluginX;
106 y = event->data.mouse.pluginY;
107 if ((x < 0.0) || (y < 0.0))
108 return NPERR_GENERIC_ERROR;
111 // Flip the coords to bottom-left origin.
112 NSRect viewFrame = [cocoaView frame];
114 double shiftedY = viewFrame.size.height - y;
115 // Shift to window coords.
116 shiftedX += viewFrame.origin.x;
117 shiftedY += [cocoaView convertPoint:NSMakePoint(0,0) toView:nil].y - viewFrame.size.height;
119 // Create an NSEvent we can use to show the context menu. Only the location
120 // is important here so just simulate a right mouse down. The coordinates
121 // must be in top-level window terms.
122 NSEvent* cocoaEvent = [NSEvent mouseEventWithType:cocoaEventType
123 location:NSMakePoint(shiftedX, shiftedY)
124 modifierFlags:cocoaModifierFlags
126 windowNumber:[[cocoaView window] windowNumber]
132 [NSMenu popUpContextMenu:cocoaMenu withEvent:cocoaEvent forView:cocoaView];
134 return NPERR_NO_ERROR;
137 NPBool NS_NPAPI_ConvertPointCocoa(void* inView,
138 double sourceX, double sourceY, NPCoordinateSpace sourceSpace,
139 double *destX, double *destY, NPCoordinateSpace destSpace)
141 NS_ASSERTION(inView, "Must have a native view to convert coordinates.");
143 // Caller has to want a result.
144 if (!destX && !destY)
147 if (sourceSpace == destSpace) {
155 NSView* view = (NSView*)inView;
156 NSWindow* window = [view window];
157 NSPoint sourcePoint = NSMakePoint(sourceX, sourceY);
159 // Convert to screen space.
161 switch (sourceSpace) {
162 case NPCoordinateSpacePlugin:
163 screenPoint = [view convertPoint:sourcePoint toView:nil];
164 screenPoint = [window convertBaseToScreen:screenPoint];
166 case NPCoordinateSpaceWindow:
167 screenPoint = [window convertBaseToScreen:sourcePoint];
169 case NPCoordinateSpaceFlippedWindow:
170 sourcePoint.y = [window frame].size.height - sourcePoint.y;
171 screenPoint = [window convertBaseToScreen:sourcePoint];
173 case NPCoordinateSpaceScreen:
174 screenPoint = sourcePoint;
176 case NPCoordinateSpaceFlippedScreen:
177 sourcePoint.y = [[[NSScreen screens] objectAtIndex:0] frame].size.height - sourcePoint.y;
178 screenPoint = sourcePoint;
184 // Convert from screen to dest space.
187 case NPCoordinateSpacePlugin:
188 destPoint = [window convertScreenToBase:screenPoint];
189 destPoint = [view convertPoint:destPoint fromView:nil];
191 case NPCoordinateSpaceWindow:
192 destPoint = [window convertScreenToBase:screenPoint];
194 case NPCoordinateSpaceFlippedWindow:
195 destPoint = [window convertScreenToBase:screenPoint];
196 destPoint.y = [window frame].size.height - destPoint.y;
198 case NPCoordinateSpaceScreen:
199 destPoint = screenPoint;
201 case NPCoordinateSpaceFlippedScreen:
202 destPoint = screenPoint;
203 destPoint.y = [[[NSScreen screens] objectAtIndex:0] frame].size.height - destPoint.y;
210 *destX = destPoint.x;
212 *destY = destPoint.y;