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 #import <QuartzCore/QuartzCore.h>
44 #include "nsObjCExceptions.h"
47 void NS_NPAPI_CarbonWindowFrame(WindowRef aWindow, nsRect& outRect)
53 ::GetWindowBounds(aWindow, kWindowStructureRgn, &windowRect);
54 outRect.x = windowRect.left;
55 outRect.y = windowRect.top;
56 outRect.width = windowRect.right - windowRect.left;
57 outRect.height = windowRect.bottom - windowRect.top;
61 void NS_NPAPI_CocoaWindowFrame(void* aWindow, nsRect& outRect)
63 NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
68 NSWindow* window = (NSWindow*)aWindow;
70 float menubarScreenHeight;
71 NSArray* allScreens = [NSScreen screens];
72 if ([allScreens count])
73 menubarScreenHeight = [[allScreens objectAtIndex:0] frame].size.height;
75 return; // If there are no screens, there's not much we can say.
77 NSRect frame = [window frame];
78 outRect.x = (nscoord)frame.origin.x;
79 outRect.y = (nscoord)(menubarScreenHeight - (frame.origin.y + frame.size.height));
80 outRect.width = (nscoord)frame.size.width;
81 outRect.height = (nscoord)frame.size.height;
83 NS_OBJC_END_TRY_ABORT_BLOCK;
86 PRBool NS_NPAPI_CocoaWindowIsMain(void* aWindow)
88 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN;
93 NSWindow* window = (NSWindow*)aWindow;
95 return (PRBool)[window isMainWindow];
97 NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(PR_TRUE);
100 NPError NS_NPAPI_ShowCocoaContextMenu(void* menu, nsIWidget* widget, NPCocoaEvent* event)
102 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN;
104 if (!menu || !widget || !event)
105 return NPERR_GENERIC_ERROR;
107 NSMenu* cocoaMenu = (NSMenu*)menu;
108 NSView* cocoaView = (NSView*)widget->GetNativeData(NS_NATIVE_WIDGET);
110 NSEventType cocoaEventType = NSRightMouseDown;
111 unsigned int cocoaModifierFlags = 0;
112 double x = 0.0; // Coordinates for the context menu in plugin terms, top-left origin.
115 NPCocoaEventType eventType = event->type;
116 if (eventType == NPCocoaEventMouseDown ||
117 eventType == NPCocoaEventMouseUp ||
118 eventType == NPCocoaEventMouseMoved ||
119 eventType == NPCocoaEventMouseEntered ||
120 eventType == NPCocoaEventMouseExited ||
121 eventType == NPCocoaEventMouseDragged) {
122 cocoaEventType = (NSEventType)event->data.mouse.buttonNumber;
123 cocoaModifierFlags = event->data.mouse.modifierFlags;
124 x = event->data.mouse.pluginX;
125 y = event->data.mouse.pluginY;
126 if ((x < 0.0) || (y < 0.0))
127 return NPERR_GENERIC_ERROR;
130 // Flip the coords to bottom-left origin.
131 NSRect viewFrame = [cocoaView frame];
133 double shiftedY = viewFrame.size.height - y;
134 // Shift to window coords.
135 shiftedX += viewFrame.origin.x;
136 shiftedY += [cocoaView convertPoint:NSMakePoint(0,0) toView:nil].y - viewFrame.size.height;
138 // Create an NSEvent we can use to show the context menu. Only the location
139 // is important here so just simulate a right mouse down. The coordinates
140 // must be in top-level window terms.
141 NSEvent* cocoaEvent = [NSEvent mouseEventWithType:cocoaEventType
142 location:NSMakePoint(shiftedX, shiftedY)
143 modifierFlags:cocoaModifierFlags
145 windowNumber:[[cocoaView window] windowNumber]
151 [NSMenu popUpContextMenu:cocoaMenu withEvent:cocoaEvent forView:cocoaView];
153 return NPERR_NO_ERROR;
155 NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(NPERR_GENERIC_ERROR);
158 NPBool NS_NPAPI_ConvertPointCocoa(void* inView,
159 double sourceX, double sourceY, NPCoordinateSpace sourceSpace,
160 double *destX, double *destY, NPCoordinateSpace destSpace)
162 NS_ASSERTION(inView, "Must have a native view to convert coordinates.");
164 // Caller has to want a result.
165 if (!destX && !destY)
168 if (sourceSpace == destSpace) {
176 NSView* view = (NSView*)inView;
177 NSWindow* window = [view window];
178 NSPoint sourcePoint = NSMakePoint(sourceX, sourceY);
180 // Convert to screen space.
182 switch (sourceSpace) {
183 case NPCoordinateSpacePlugin:
184 screenPoint = [view convertPoint:sourcePoint toView:nil];
185 screenPoint = [window convertBaseToScreen:screenPoint];
187 case NPCoordinateSpaceWindow:
188 screenPoint = [window convertBaseToScreen:sourcePoint];
190 case NPCoordinateSpaceFlippedWindow:
191 sourcePoint.y = [window frame].size.height - sourcePoint.y;
192 screenPoint = [window convertBaseToScreen:sourcePoint];
194 case NPCoordinateSpaceScreen:
195 screenPoint = sourcePoint;
197 case NPCoordinateSpaceFlippedScreen:
198 sourcePoint.y = [[[NSScreen screens] objectAtIndex:0] frame].size.height - sourcePoint.y;
199 screenPoint = sourcePoint;
205 // Convert from screen to dest space.
208 case NPCoordinateSpacePlugin:
209 destPoint = [window convertScreenToBase:screenPoint];
210 destPoint = [view convertPoint:destPoint fromView:nil];
212 case NPCoordinateSpaceWindow:
213 destPoint = [window convertScreenToBase:screenPoint];
215 case NPCoordinateSpaceFlippedWindow:
216 destPoint = [window convertScreenToBase:screenPoint];
217 destPoint.y = [window frame].size.height - destPoint.y;
219 case NPCoordinateSpaceScreen:
220 destPoint = screenPoint;
222 case NPCoordinateSpaceFlippedScreen:
223 destPoint = screenPoint;
224 destPoint.y = [[[NSScreen screens] objectAtIndex:0] frame].size.height - destPoint.y;
231 *destX = destPoint.x;
233 *destY = destPoint.y;