2 IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc. ("Apple") in
3 consideration of your agreement to the following terms, and your use, installation,
4 modification or redistribution of this Apple software constitutes acceptance of these
5 terms. If you do not agree with these terms, please do not use, install, modify or
6 redistribute this Apple software.
8 In consideration of your agreement to abide by the following terms, and subject to these
9 terms, Apple grants you a personal, non-exclusive license, under Appleās copyrights in
10 this original Apple software (the "Apple Software"), to use, reproduce, modify and
11 redistribute the Apple Software, with or without modifications, in source and/or binary
12 forms; provided that if you redistribute the Apple Software in its entirety and without
13 modifications, you must retain this notice and the following text and disclaimers in all
14 such redistributions of the Apple Software. Neither the name, trademarks, service marks
15 or logos of Apple Computer, Inc. may be used to endorse or promote products derived from
16 the Apple Software without specific prior written permission from Apple. Except as expressly
17 stated in this notice, no other rights or licenses, express or implied, are granted by Apple
18 herein, including but not limited to any patent rights that may be infringed by your
19 derivative works or by other works in which the Apple Software may be incorporated.
21 The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO WARRANTIES,
22 EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF NON-INFRINGEMENT,
23 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS
24 USE AND OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
26 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR CONSEQUENTIAL
27 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28 OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE,
29 REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND
30 WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR
31 OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #import <WebKit/npapi.h>
35 #import <WebKit/npfunctions.h>
36 #import <WebKit/npruntime.h>
38 #import <Cocoa/Cocoa.h>
40 #import "MenuHandler.h"
42 // Browser function table
43 static NPNetscapeFuncs* browser;
45 // Structure for per-instance storage
46 typedef struct PluginObject
54 bool mouseIsInsidePlugin;
56 MenuHandler *menuHandler;
59 NPError NPP_New(NPMIMEType pluginType, NPP instance, uint16_t mode, int16_t argc, char* argn[], char* argv[], NPSavedData* saved);
60 NPError NPP_Destroy(NPP instance, NPSavedData** save);
61 NPError NPP_SetWindow(NPP instance, NPWindow* window);
62 NPError NPP_NewStream(NPP instance, NPMIMEType type, NPStream* stream, NPBool seekable, uint16_t* stype);
63 NPError NPP_DestroyStream(NPP instance, NPStream* stream, NPReason reason);
64 int32_t NPP_WriteReady(NPP instance, NPStream* stream);
65 int32_t NPP_Write(NPP instance, NPStream* stream, int32_t offset, int32_t len, void* buffer);
66 void NPP_StreamAsFile(NPP instance, NPStream* stream, const char* fname);
67 void NPP_Print(NPP instance, NPPrint* platformPrint);
68 int16_t NPP_HandleEvent(NPP instance, void* event);
69 void NPP_URLNotify(NPP instance, const char* URL, NPReason reason, void* notifyData);
70 NPError NPP_GetValue(NPP instance, NPPVariable variable, void *value);
71 NPError NPP_SetValue(NPP instance, NPNVariable variable, void *value);
74 // Mach-o entry points
75 NPError NP_Initialize(NPNetscapeFuncs *browserFuncs);
76 NPError NP_GetEntryPoints(NPPluginFuncs *pluginFuncs);
77 void NP_Shutdown(void);
80 NPError NP_Initialize(NPNetscapeFuncs* browserFuncs)
82 browser = browserFuncs;
83 return NPERR_NO_ERROR;
86 NPError NP_GetEntryPoints(NPPluginFuncs* pluginFuncs)
88 pluginFuncs->version = 11;
89 pluginFuncs->size = sizeof(pluginFuncs);
90 pluginFuncs->newp = NPP_New;
91 pluginFuncs->destroy = NPP_Destroy;
92 pluginFuncs->setwindow = NPP_SetWindow;
93 pluginFuncs->newstream = NPP_NewStream;
94 pluginFuncs->destroystream = NPP_DestroyStream;
95 pluginFuncs->asfile = NPP_StreamAsFile;
96 pluginFuncs->writeready = NPP_WriteReady;
97 pluginFuncs->write = (NPP_WriteProcPtr)NPP_Write;
98 pluginFuncs->print = NPP_Print;
99 pluginFuncs->event = NPP_HandleEvent;
100 pluginFuncs->urlnotify = NPP_URLNotify;
101 pluginFuncs->getvalue = NPP_GetValue;
102 pluginFuncs->setvalue = NPP_SetValue;
104 return NPERR_NO_ERROR;
107 void NP_Shutdown(void)
112 NPError NPP_New(NPMIMEType pluginType, NPP instance, uint16_t mode, int16_t argc, char* argn[], char* argv[], NPSavedData* saved)
114 // Create per-instance storage
115 PluginObject *obj = (PluginObject *)malloc(sizeof(PluginObject));
116 bzero(obj, sizeof(PluginObject));
119 instance->pdata = obj;
121 // Ask the browser if it supports the CoreGraphics drawing model
122 NPBool supportsCoreGraphics;
123 if (browser->getvalue(instance, NPNVsupportsCoreGraphicsBool, &supportsCoreGraphics) != NPERR_NO_ERROR)
124 supportsCoreGraphics = FALSE;
126 if (!supportsCoreGraphics)
127 return NPERR_INCOMPATIBLE_VERSION_ERROR;
129 // If the browser supports the CoreGraphics drawing model, enable it.
130 browser->setvalue(instance, NPPVpluginDrawingModel, (void *)NPDrawingModelCoreGraphics);
132 // If the browser supports the Cocoa event model, enable it.
133 NPBool supportsCocoa;
134 if (browser->getvalue(instance, NPNVsupportsCocoaBool, &supportsCocoa) != NPERR_NO_ERROR)
135 supportsCocoa = FALSE;
138 return NPERR_INCOMPATIBLE_VERSION_ERROR;
140 browser->setvalue(instance, NPPVpluginEventModel, (void *)NPEventModelCocoa);
142 return NPERR_NO_ERROR;
145 NPError NPP_Destroy(NPP instance, NPSavedData** save)
147 // Free per-instance storage
148 PluginObject *obj = instance->pdata;
150 [obj->string release];
151 [obj->menuHandler release];
155 return NPERR_NO_ERROR;
158 NPError NPP_SetWindow(NPP instance, NPWindow* window)
160 PluginObject *obj = instance->pdata;
161 obj->window = *window;
163 return NPERR_NO_ERROR;
167 NPError NPP_NewStream(NPP instance, NPMIMEType type, NPStream* stream, NPBool seekable, uint16_t* stype)
169 *stype = NP_ASFILEONLY;
170 return NPERR_NO_ERROR;
173 NPError NPP_DestroyStream(NPP instance, NPStream* stream, NPReason reason)
175 return NPERR_NO_ERROR;
178 int32_t NPP_WriteReady(NPP instance, NPStream* stream)
183 int32_t NPP_Write(NPP instance, NPStream* stream, int32_t offset, int32_t len, void* buffer)
188 void NPP_StreamAsFile(NPP instance, NPStream* stream, const char* fname)
192 void NPP_Print(NPP instance, NPPrint* platformPrint)
197 static void handleDraw(PluginObject *obj, NPCocoaEvent *event)
199 NSGraphicsContext *oldContext = [[NSGraphicsContext currentContext] retain];
201 NSGraphicsContext *context = [NSGraphicsContext graphicsContextWithGraphicsPort:event->data.draw.context
204 [NSGraphicsContext setCurrentContext:context];
206 NSRect rect = NSMakeRect(0, 0, obj->window.width, obj->window.height);
208 [[NSColor lightGrayColor] set];
209 [NSBezierPath fillRect:rect];
211 // If the plugin has focus, draw a focus indicator
213 [[NSColor blackColor] set];
214 NSBezierPath *path = [NSBezierPath bezierPathWithRect:rect];
215 [path setLineWidth:5];
219 [obj->string drawAtPoint:NSMakePoint(10, 10) withAttributes:nil];
221 [NSGraphicsContext setCurrentContext:oldContext];
224 static NSString *eventType(NPCocoaEventType type)
227 case NPCocoaEventScrollWheel:
228 return @"NPCocoaEventScrollWheel";
229 case NPCocoaEventMouseDown:
230 return @"NPCocoaEventMouseDown";
231 case NPCocoaEventMouseUp:
232 return @"NPCocoaEventMouseUp";
233 case NPCocoaEventMouseMoved:
234 return @"NPCocoaEventMouseMoved";
235 case NPCocoaEventMouseDragged:
236 return @"NPCocoaEventMouseDragged";
237 case NPCocoaEventMouseEntered:
238 return @"NPCocoaEventMouseEntered";
239 case NPCocoaEventMouseExited:
240 return @"NPCocoaEventMouseExited";
241 case NPCocoaEventKeyDown:
242 return @"NPCocoaEventKeyDown";
243 case NPCocoaEventKeyUp:
244 return @"NPCocoaEventKeyUp";
245 case NPCocoaEventFlagsChanged:
246 return @"NPCocoaEventFlagsChanged";
252 static void invalidatePlugin(PluginObject *obj)
257 rect.right = obj->window.width;
258 rect.bottom = obj->window.height;
260 browser->invalidaterect(obj->npp, &rect);
264 static void handleMouseEvent(PluginObject *obj, NPCocoaEvent *event)
266 NSString *string = [NSString stringWithFormat:@"Type: %@\n"
267 "Modifier flags: 0x%x\n"
268 "Coordinates: (%g, %g)\n"
269 "Button number: %d\n"
271 "Delta: (%g, %g, %g)",
272 eventType(event->type),
273 event->data.mouse.modifierFlags,
274 event->data.mouse.pluginX,
275 event->data.mouse.pluginY,
276 event->data.mouse.buttonNumber,
277 event->data.mouse.clickCount,
278 event->data.mouse.deltaX, event->data.mouse.deltaY, event->data.mouse.deltaZ];
281 [obj->string release];
282 obj->string = [string retain];
284 invalidatePlugin(obj);
286 if (event->data.mouse.buttonNumber == 1) {
287 if (!obj->menuHandler)
288 obj->menuHandler = [[MenuHandler alloc] initWithBrowserFuncs:browser instance:obj->npp];
290 browser->popupcontextmenu(obj->npp, (NPNSMenu *)[obj->menuHandler menu]);
294 static void handleKeyboardEvent(PluginObject *obj, NPCocoaEvent *event)
296 NSString *string = [NSString stringWithFormat:@"Type: %@\n"
297 "Modifier flags: 0x%x\n"
299 "Characters ignoring modifiers: %@\n"
302 eventType(event->type),
303 event->data.key.modifierFlags,
304 event->data.key.characters,
305 event->data.key.charactersIgnoringModifiers,
306 event->data.key.isARepeat ? @"YES" : @"NO",
307 event->data.key.keyCode];
310 [obj->string release];
311 obj->string = [string retain];
313 invalidatePlugin(obj);
316 int16_t NPP_HandleEvent(NPP instance, void* event)
318 PluginObject *obj = instance->pdata;
320 NPCocoaEvent *cocoaEvent = event;
322 switch(cocoaEvent->type) {
323 case NPCocoaEventFocusChanged:
324 obj->hasFocus = cocoaEvent->data.focus.hasFocus;
325 invalidatePlugin(obj);
328 case NPCocoaEventDrawRect:
329 handleDraw(obj, cocoaEvent);
332 case NPCocoaEventKeyDown:
333 case NPCocoaEventKeyUp:
334 case NPCocoaEventFlagsChanged:
335 handleKeyboardEvent(obj, cocoaEvent);
338 case NPCocoaEventMouseDown:
339 case NPCocoaEventMouseUp:
341 // FIXME: NPCocoaEventMouseMoved is currently disabled in order to see other events more clearly
342 // without "drowning" in mouse moved events.
343 // case NPCocoaEventMouseMoved:
344 case NPCocoaEventMouseEntered:
345 case NPCocoaEventMouseExited:
346 case NPCocoaEventMouseDragged:
347 case NPCocoaEventScrollWheel:
348 handleMouseEvent(obj, cocoaEvent);
355 void NPP_URLNotify(NPP instance, const char* url, NPReason reason, void* notifyData)
360 NPError NPP_GetValue(NPP instance, NPPVariable variable, void *value)
362 return NPERR_GENERIC_ERROR;
365 NPError NPP_SetValue(NPP instance, NPNVariable variable, void *value)
367 return NPERR_GENERIC_ERROR;