2008-07-02 Geoffrey Garen <ggaren@apple.com>
[WebKit-https.git] / WebKitExamplePlugins / NetscapeCocoaPlugin / main.m
1 /*
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.
7  
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.
20  
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.
25  
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.
32  */
33
34 #import <WebKit/npapi.h>
35 #import <WebKit/npfunctions.h>
36 #import <WebKit/npruntime.h>
37
38 #import <Cocoa/Cocoa.h>
39
40 #import "MenuHandler.h"
41
42 // Browser function table
43 static NPNetscapeFuncs* browser;
44
45 // Structure for per-instance storage
46 typedef struct PluginObject
47 {
48     NPP npp;
49     
50     NPWindow window;
51     
52     NSString *string;
53     bool hasFocus;
54     bool mouseIsInsidePlugin;
55     
56     MenuHandler *menuHandler;
57 } PluginObject;
58
59 NPError NPP_New(NPMIMEType pluginType, NPP instance, uint16 mode, int16 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* stype);
63 NPError NPP_DestroyStream(NPP instance, NPStream* stream, NPReason reason);
64 int32 NPP_WriteReady(NPP instance, NPStream* stream);
65 int32 NPP_Write(NPP instance, NPStream* stream, int32 offset, int32 len, void* buffer);
66 void NPP_StreamAsFile(NPP instance, NPStream* stream, const char* fname);
67 void NPP_Print(NPP instance, NPPrint* platformPrint);
68 int16 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);
72
73 #pragma export on
74 // Mach-o entry points
75 NPError NP_Initialize(NPNetscapeFuncs *browserFuncs);
76 NPError NP_GetEntryPoints(NPPluginFuncs *pluginFuncs);
77 void NP_Shutdown(void);
78 #pragma export off
79
80 NPError NP_Initialize(NPNetscapeFuncs* browserFuncs)
81 {
82     browser = browserFuncs;
83     return NPERR_NO_ERROR;
84 }
85
86 NPError NP_GetEntryPoints(NPPluginFuncs* pluginFuncs)
87 {
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;
103     
104     return NPERR_NO_ERROR;
105 }
106
107 void NP_Shutdown(void)
108 {
109
110 }
111
112 NPError NPP_New(NPMIMEType pluginType, NPP instance, uint16 mode, int16 argc, char* argn[], char* argv[], NPSavedData* saved)
113 {
114     // Create per-instance storage
115     PluginObject *obj = (PluginObject *)malloc(sizeof(PluginObject));
116     bzero(obj, sizeof(PluginObject));
117     
118     obj->npp = instance;
119     instance->pdata = obj;
120     
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;
125     
126     if (!supportsCoreGraphics)
127         return NPERR_INCOMPATIBLE_VERSION_ERROR;
128     
129     // If the browser supports the CoreGraphics drawing model, enable it.
130     browser->setvalue(instance, NPPVpluginDrawingModel, (void *)NPDrawingModelCoreGraphics);
131
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;
136     
137     if (!supportsCocoa)
138         return NPERR_INCOMPATIBLE_VERSION_ERROR;
139     
140     browser->setvalue(instance, NPPVpluginEventModel, (void *)NPEventModelCocoa);
141     
142     return NPERR_NO_ERROR;
143 }
144
145 NPError NPP_Destroy(NPP instance, NPSavedData** save)
146 {
147     // Free per-instance storage
148     PluginObject *obj = instance->pdata;
149     
150     [obj->string release];
151     [obj->menuHandler release];
152     
153     free(obj);
154     
155     return NPERR_NO_ERROR;
156 }
157
158 NPError NPP_SetWindow(NPP instance, NPWindow* window)
159 {
160     PluginObject *obj = instance->pdata;
161     obj->window = *window;
162
163     return NPERR_NO_ERROR;
164 }
165  
166
167 NPError NPP_NewStream(NPP instance, NPMIMEType type, NPStream* stream, NPBool seekable, uint16* stype)
168 {
169     *stype = NP_ASFILEONLY;
170     return NPERR_NO_ERROR;
171 }
172
173 NPError NPP_DestroyStream(NPP instance, NPStream* stream, NPReason reason)
174 {
175     return NPERR_NO_ERROR;
176 }
177
178 int32 NPP_WriteReady(NPP instance, NPStream* stream)
179 {
180     return 0;
181 }
182
183 int32 NPP_Write(NPP instance, NPStream* stream, int32 offset, int32 len, void* buffer)
184 {
185     return 0;
186 }
187
188 void NPP_StreamAsFile(NPP instance, NPStream* stream, const char* fname)
189 {
190 }
191
192 void NPP_Print(NPP instance, NPPrint* platformPrint)
193 {
194
195 }
196
197 static void handleDraw(PluginObject *obj)
198 {
199     NSGraphicsContext *oldContext = [[NSGraphicsContext currentContext] retain];
200     
201     NSGraphicsContext *context = [NSGraphicsContext graphicsContextWithGraphicsPort:((NP_CGContext *)obj->window.window)->context
202                                                                             flipped:YES];
203
204
205     [NSGraphicsContext setCurrentContext:context];
206     
207     NSRect rect = NSMakeRect(0, 0, obj->window.width, obj->window.height);
208     
209     [[NSColor lightGrayColor] set];
210     [NSBezierPath fillRect:rect];
211
212     // If the plugin has focus, draw a focus indicator
213     if (obj->hasFocus) {
214         [[NSColor blackColor] set];
215         NSBezierPath *path = [NSBezierPath bezierPathWithRect:rect];
216         [path setLineWidth:5];
217         [path stroke];
218     }
219     
220     [obj->string drawAtPoint:NSMakePoint(10, 10) withAttributes:nil];
221     
222     [NSGraphicsContext setCurrentContext:oldContext];
223 }
224
225 static NSString *eventType(NPCocoaEventType type)
226 {
227     switch (type) {
228         case NPCocoaEventScrollWheel:
229             return @"NPCocoaEventScrollWheel";
230         case NPCocoaEventMouseDown:
231             return @"NPCocoaEventMouseDown";
232         case NPCocoaEventMouseUp:
233             return @"NPCocoaEventMouseUp";            
234         case NPCocoaEventMouseMoved:
235             return @"NPCocoaEventMouseMoved";            
236         case NPCocoaEventMouseDragged:
237             return @"NPCocoaEventMouseDragged";            
238         case NPCocoaEventMouseEntered:
239             return @"NPCocoaEventMouseEntered";            
240         case NPCocoaEventMouseExited:
241             return @"NPCocoaEventMouseExited";
242         case NPCocoaEventKeyDown:
243             return @"NPCocoaEventKeyDown";
244         case NPCocoaEventKeyUp:
245             return @"NPCocoaEventKeyUp";
246         case NPCocoaEventFlagsChanged:
247             return @"NPCocoaEventFlagsChanged";
248         default:
249             return @"unknown";
250     }    
251 }
252
253 static void invalidatePlugin(PluginObject *obj)
254 {
255     NPRect rect;
256     rect.left = 0;
257     rect.top = 0;
258     rect.right = obj->window.width;
259     rect.bottom = obj->window.height;
260     
261     browser->invalidaterect(obj->npp, &rect);    
262 }
263
264
265 static void handleMouseEvent(PluginObject *obj, NPCocoaEvent *event)
266 {
267     NSString *string = [NSString stringWithFormat:@"Type: %@\n"
268                                                    "Modifier flags: 0x%x\n"
269                                                    "Coordinates: (%g, %g)\n"
270                                                    "Button number: %d\n"
271                                                    "Click count: %d\n"
272                                                    "Delta: (%g, %g, %g)",
273                                                    eventType(event->type), 
274                                                    event->event.mouse.modifierFlags,
275                                                    event->event.mouse.pluginX,
276                                                    event->event.mouse.pluginY,
277                                                    event->event.mouse.buttonNumber,
278                                                    event->event.mouse.clickCount,
279                                                    event->event.mouse.deltaX, event->event.mouse.deltaY, event->event.mouse.deltaZ];
280     
281     
282     [obj->string release];
283     obj->string = [string retain];
284  
285     invalidatePlugin(obj);
286     
287     if (event->event.mouse.buttonNumber == 1) {
288         if (!obj->menuHandler)
289             obj->menuHandler = [[MenuHandler alloc] initWithBrowserFuncs:browser instance:obj->npp];
290         
291         browser->popupcontextmenu(obj->npp, (NPNSMenu *)[obj->menuHandler menu]);
292         NSLog(@"foo");
293     }
294 }
295
296 static void handleKeyboardEvent(PluginObject *obj, NPCocoaEvent *event)
297 {
298     NSString *string = [NSString stringWithFormat:@"Type: %@\n"
299                         "Modifier flags: 0x%x\n"
300                         "Characters: %@\n"
301                         "Characters ignoring modifiers: %@\n"
302                         "Is a repeat: %@\n"
303                         "Key code: %d",
304                         eventType(event->type), 
305                         event->event.key.modifierFlags,
306                         event->event.key.characters,
307                         event->event.key.charactersIgnoringModifiers,
308                         event->event.key.isARepeat ? @"YES" : @"NO",
309                         event->event.key.keyCode];
310     
311     
312     [obj->string release];
313     obj->string = [string retain];
314     
315     invalidatePlugin(obj);
316 }
317
318 int16 NPP_HandleEvent(NPP instance, void* event)
319 {
320     PluginObject *obj = instance->pdata;
321
322     NPCocoaEvent *cocoaEvent = event;
323     
324     switch(cocoaEvent->type) {
325         case NPCocoaEventFocusChanged:
326             obj->hasFocus = cocoaEvent->event.focus.hasFocus;
327             invalidatePlugin(obj);
328             return 1;
329             
330         case NPCocoaEventDrawRect:
331             handleDraw(obj);
332             return 1;
333         
334         case NPCocoaEventKeyDown:
335         case NPCocoaEventKeyUp:
336         case NPCocoaEventFlagsChanged:
337             handleKeyboardEvent(obj, cocoaEvent);
338             return 1;
339             
340         case NPCocoaEventMouseDown:
341         case NPCocoaEventMouseUp:
342             
343         // FIXME: NPCocoaEventMouseMoved is currently disabled in order to see other events more clearly
344         // without "drowning" in mouse moved events.
345 //        case NPCocoaEventMouseMoved:
346         case NPCocoaEventMouseEntered:
347         case NPCocoaEventMouseExited:
348         case NPCocoaEventMouseDragged:
349         case NPCocoaEventScrollWheel:
350             handleMouseEvent(obj, cocoaEvent);
351             return 1;
352     }
353     
354     return 0;
355 }
356
357 void NPP_URLNotify(NPP instance, const char* url, NPReason reason, void* notifyData)
358 {
359
360 }
361
362 NPError NPP_GetValue(NPP instance, NPPVariable variable, void *value)
363 {
364     return NPERR_GENERIC_ERROR;
365 }
366
367 NPError NPP_SetValue(NPP instance, NPNVariable variable, void *value)
368 {
369     return NPERR_GENERIC_ERROR;
370 }