|
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/. */ |
|
6 |
|
7 #include "nsPluginUtilsOSX.h" |
|
8 |
|
9 #import <Cocoa/Cocoa.h> |
|
10 #import <QuartzCore/QuartzCore.h> |
|
11 #include "nsObjCExceptions.h" |
|
12 |
|
13 #ifndef __LP64__ |
|
14 void NS_NPAPI_CarbonWindowFrame(WindowRef aWindow, nsRect& outRect) |
|
15 { |
|
16 if (!aWindow) |
|
17 return; |
|
18 |
|
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; |
|
25 } |
|
26 #endif |
|
27 |
|
28 void NS_NPAPI_CocoaWindowFrame(void* aWindow, nsRect& outRect) |
|
29 { |
|
30 NS_OBJC_BEGIN_TRY_ABORT_BLOCK; |
|
31 |
|
32 if (!aWindow) |
|
33 return; |
|
34 |
|
35 NSWindow* window = (NSWindow*)aWindow; |
|
36 |
|
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. |
|
43 |
|
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; |
|
49 |
|
50 NS_OBJC_END_TRY_ABORT_BLOCK; |
|
51 } |
|
52 |
|
53 bool NS_NPAPI_CocoaWindowIsMain(void* aWindow) |
|
54 { |
|
55 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN; |
|
56 |
|
57 if (!aWindow) |
|
58 return true; |
|
59 |
|
60 NSWindow* window = (NSWindow*)aWindow; |
|
61 |
|
62 return (bool)[window isMainWindow]; |
|
63 |
|
64 NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(true); |
|
65 } |
|
66 |
|
67 NPError NS_NPAPI_ShowCocoaContextMenu(void* menu, nsIWidget* widget, NPCocoaEvent* event) |
|
68 { |
|
69 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN; |
|
70 |
|
71 if (!menu || !widget || !event) |
|
72 return NPERR_GENERIC_ERROR; |
|
73 |
|
74 NSMenu* cocoaMenu = (NSMenu*)menu; |
|
75 NSView* cocoaView = (NSView*)widget->GetNativeData(NS_NATIVE_WIDGET); |
|
76 |
|
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; |
|
81 |
|
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 } |
|
94 |
|
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; |
|
102 |
|
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]; |
|
115 |
|
116 [NSMenu popUpContextMenu:cocoaMenu withEvent:cocoaEvent forView:cocoaView]; |
|
117 |
|
118 return NPERR_NO_ERROR; |
|
119 |
|
120 NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(NPERR_GENERIC_ERROR); |
|
121 } |
|
122 |
|
123 NPBool NS_NPAPI_ConvertPointCocoa(void* inView, |
|
124 double sourceX, double sourceY, NPCoordinateSpace sourceSpace, |
|
125 double *destX, double *destY, NPCoordinateSpace destSpace) |
|
126 { |
|
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 } |
|
133 |
|
134 // Caller has to want a result. |
|
135 if (!destX && !destY) |
|
136 return false; |
|
137 |
|
138 if (sourceSpace == destSpace) { |
|
139 if (destX) |
|
140 *destX = sourceX; |
|
141 if (destY) |
|
142 *destY = sourceY; |
|
143 return true; |
|
144 } |
|
145 |
|
146 NSView* view = (NSView*)inView; |
|
147 NSWindow* window = [view window]; |
|
148 NSPoint sourcePoint = NSMakePoint(sourceX, sourceY); |
|
149 |
|
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 } |
|
174 |
|
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 } |
|
199 |
|
200 if (destX) |
|
201 *destX = destPoint.x; |
|
202 if (destY) |
|
203 *destY = destPoint.y; |
|
204 |
|
205 return true; |
|
206 } |
|
207 |