Bug 574454 - Cleanup nsNativeThemeWin's GetMinimumWidgetSize a bit. r=roc.
[mozilla-central.git] / layout / generic / nsPluginUtilsOSX.mm
blob86b547ad55a9f5401e9dd0412aedeac4fc0887de
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
5   *
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/
10   *
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
14   * License.
15   *
16   * The Original Code is Mozilla.org code.
17   *
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.
22   *
23   * Contributor(s):
24   *   Josh Aas <josh@mozilla.com>
25   *
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.
37   *
38   * ***** END LICENSE BLOCK ***** */
40 #include "nsPluginUtilsOSX.h"
42 #import <Cocoa/Cocoa.h>
43 #import <QuartzCore/QuartzCore.h>
44 #include "nsObjCExceptions.h"
46 #ifndef __LP64__
47 void NS_NPAPI_CarbonWindowFrame(WindowRef aWindow, nsRect& outRect)
49   if (!aWindow)
50     return;
52   Rect windowRect;
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;
59 #endif
61 void NS_NPAPI_CocoaWindowFrame(void* aWindow, nsRect& outRect)
63   NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
65   if (!aWindow)
66     return;
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;
74   else
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;
90   if (!aWindow)
91     return PR_TRUE;
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.
113   double y = 0.0;
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;
128   }
130   // Flip the coords to bottom-left origin.
131   NSRect viewFrame = [cocoaView frame];
132   double shiftedX = x;
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
144                                           timestamp:0
145                                        windowNumber:[[cocoaView window] windowNumber]
146                                             context:nil
147                                         eventNumber:0
148                                          clickCount:1
149                                            pressure:0.0];
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)
166     return PR_FALSE;
168   if (sourceSpace == destSpace) {
169     if (destX)
170       *destX = sourceX;
171     if (destY)
172       *destY = sourceY;
173     return PR_TRUE;
174   }
176   NSView* view = (NSView*)inView;
177   NSWindow* window = [view window];
178   NSPoint sourcePoint = NSMakePoint(sourceX, sourceY);
180   // Convert to screen space.
181   NSPoint screenPoint;
182   switch (sourceSpace) {
183     case NPCoordinateSpacePlugin:
184       screenPoint = [view convertPoint:sourcePoint toView:nil];
185       screenPoint = [window convertBaseToScreen:screenPoint];
186       break;
187     case NPCoordinateSpaceWindow:
188       screenPoint = [window convertBaseToScreen:sourcePoint];
189       break;
190     case NPCoordinateSpaceFlippedWindow:
191       sourcePoint.y = [window frame].size.height - sourcePoint.y;
192       screenPoint = [window convertBaseToScreen:sourcePoint];
193       break;
194     case NPCoordinateSpaceScreen:
195       screenPoint = sourcePoint;
196       break;
197     case NPCoordinateSpaceFlippedScreen:
198       sourcePoint.y = [[[NSScreen screens] objectAtIndex:0] frame].size.height - sourcePoint.y;
199       screenPoint = sourcePoint;
200       break;
201     default:
202       return PR_FALSE;
203   }
205   // Convert from screen to dest space.
206   NSPoint destPoint;
207   switch (destSpace) {
208     case NPCoordinateSpacePlugin:
209       destPoint = [window convertScreenToBase:screenPoint];
210       destPoint = [view convertPoint:destPoint fromView:nil];
211       break;
212     case NPCoordinateSpaceWindow:
213       destPoint = [window convertScreenToBase:screenPoint];
214       break;
215     case NPCoordinateSpaceFlippedWindow:
216       destPoint = [window convertScreenToBase:screenPoint];
217       destPoint.y = [window frame].size.height - destPoint.y;
218       break;
219     case NPCoordinateSpaceScreen:
220       destPoint = screenPoint;
221       break;
222     case NPCoordinateSpaceFlippedScreen:
223       destPoint = screenPoint;
224       destPoint.y = [[[NSScreen screens] objectAtIndex:0] frame].size.height - destPoint.y;
225       break;
226     default:
227       return PR_FALSE;
228   }
230   if (destX)
231     *destX = destPoint.x;
232   if (destY)
233     *destY = destPoint.y;
235   return PR_TRUE;