2008-05-06 Anders Carlsson <andersca@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, NPNVpluginDrawingModel, (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, NPNVpluginEventModel, (void *)NPEventModelCocoa);
141     
142    
143     return NPERR_NO_ERROR;
144 }
145
146 NPError NPP_Destroy(NPP instance, NPSavedData** save)
147 {
148     // Free per-instance storage
149     PluginObject *obj = instance->pdata;
150     
151     [obj->string release];
152     [obj->menuHandler release];
153     
154     free(obj);
155     
156     return NPERR_NO_ERROR;
157 }
158
159 NPError NPP_SetWindow(NPP instance, NPWindow* window)
160 {
161     PluginObject *obj = instance->pdata;
162     obj->window = *window;
163
164     return NPERR_NO_ERROR;
165 }
166  
167
168 NPError NPP_NewStream(NPP instance, NPMIMEType type, NPStream* stream, NPBool seekable, uint16* stype)
169 {
170     *stype = NP_ASFILEONLY;
171     return NPERR_NO_ERROR;
172 }
173
174 NPError NPP_DestroyStream(NPP instance, NPStream* stream, NPReason reason)
175 {
176     return NPERR_NO_ERROR;
177 }
178
179 int32 NPP_WriteReady(NPP instance, NPStream* stream)
180 {
181     return 0;
182 }
183
184 int32 NPP_Write(NPP instance, NPStream* stream, int32 offset, int32 len, void* buffer)
185 {
186     return 0;
187 }
188
189 void NPP_StreamAsFile(NPP instance, NPStream* stream, const char* fname)
190 {
191 }
192
193 void NPP_Print(NPP instance, NPPrint* platformPrint)
194 {
195
196 }
197
198 static void handleDraw(PluginObject *obj)
199 {
200     NSGraphicsContext *oldContext = [[NSGraphicsContext currentContext] retain];
201     
202     NSGraphicsContext *context = [NSGraphicsContext graphicsContextWithGraphicsPort:((NP_CGContext *)obj->window.window)->context
203                                                                             flipped:YES];
204
205
206     [NSGraphicsContext setCurrentContext:context];
207     
208     NSRect rect = NSMakeRect(0, 0, obj->window.width, obj->window.height);
209     
210     [[NSColor lightGrayColor] set];
211     [NSBezierPath fillRect:rect];
212
213     // If the plugin has focus, draw a focus indicator
214     if (obj->hasFocus) {
215         [[NSColor blackColor] set];
216         NSBezierPath *path = [NSBezierPath bezierPathWithRect:rect];
217         [path setLineWidth:5];
218         [path stroke];
219     }
220     
221     [obj->string drawAtPoint:NSMakePoint(10, 10) withAttributes:nil];
222     
223     [NSGraphicsContext setCurrentContext:oldContext];
224 }
225
226 static NSString *eventType(NPCocoaEventType type)
227 {
228     switch (type) {
229         case NPCocoaEventScrollWheel:
230             return @"NPCocoaEventScrollWheel";
231         case NPCocoaEventMouseDown:
232             return @"NPCocoaEventMouseDown";
233         case NPCocoaEventMouseUp:
234             return @"NPCocoaEventMouseUp";            
235         case NPCocoaEventMouseMoved:
236             return @"NPCocoaEventMouseMoved";            
237         case NPCocoaEventMouseDragged:
238             return @"NPCocoaEventMouseDragged";            
239         case NPCocoaEventMouseEntered:
240             return @"NPCocoaEventMouseEntered";            
241         case NPCocoaEventMouseExited:
242             return @"NPCocoaEventMouseExited";
243         case NPCocoaEventKeyDown:
244             return @"NPCocoaEventKeyDown";
245         case NPCocoaEventKeyUp:
246             return @"NPCocoaEventKeyUp";
247         case NPCocoaEventFlagsChanged:
248             return @"NPCocoaEventFlagsChanged";
249         default:
250             return @"unknown";
251     }    
252 }
253
254 static void invalidatePlugin(PluginObject *obj)
255 {
256     NPRect rect;
257     rect.left = 0;
258     rect.top = 0;
259     rect.right = obj->window.width;
260     rect.bottom = obj->window.height;
261     
262     browser->invalidaterect(obj->npp, &rect);    
263 }
264
265
266 static void handleMouseEvent(PluginObject *obj, NPCocoaEvent *event)
267 {
268     NSString *string = [NSString stringWithFormat:@"Type: %@\n"
269                                                    "Modifier flags: 0x%x\n"
270                                                    "Coordinates: (%g, %g)\n"
271                                                    "Button number: %d\n"
272                                                    "Click count: %d\n"
273                                                    "Delta: (%g, %g, %g)",
274                                                    eventType(event->type), 
275                                                    event->event.mouse.modifierFlags,
276                                                    event->event.mouse.pluginX,
277                                                    event->event.mouse.pluginY,
278                                                    event->event.mouse.buttonNumber,
279                                                    event->event.mouse.clickCount,
280                                                    event->event.mouse.deltaX, event->event.mouse.deltaY, event->event.mouse.deltaZ];
281     
282     
283     [obj->string release];
284     obj->string = [string retain];
285  
286     invalidatePlugin(obj);
287     
288     if (event->event.mouse.buttonNumber == 1) {
289         if (!obj->menuHandler)
290             obj->menuHandler = [[MenuHandler alloc] initWithBrowserFuncs:browser instance:obj->npp];
291         
292         browser->popupcontextmenu(obj->npp, (NPNSMenu *)[obj->menuHandler menu]);
293         NSLog(@"foo");
294     }
295 }
296
297 static void handleKeyboardEvent(PluginObject *obj, NPCocoaEvent *event)
298 {
299     NSString *string = [NSString stringWithFormat:@"Type: %@\n"
300                         "Modifier flags: 0x%x\n"
301                         "Characters: %@\n"
302                         "Characters ignoring modifiers: %@\n"
303                         "Is a repeat: %@\n"
304                         "Key code: %d",
305                         eventType(event->type), 
306                         event->event.key.modifierFlags,
307                         event->event.key.characters,
308                         event->event.key.charactersIgnoringModifiers,
309                         event->event.key.isARepeat ? @"YES" : @"NO",
310                         event->event.key.keyCode];
311     
312     
313     [obj->string release];
314     obj->string = [string retain];
315     
316     invalidatePlugin(obj);
317 }
318
319 int16 NPP_HandleEvent(NPP instance, void* event)
320 {
321     PluginObject *obj = instance->pdata;
322
323     NPCocoaEvent *cocoaEvent = event;
324     
325     switch(cocoaEvent->type) {
326         case NPCocoaEventFocusChanged:
327             obj->hasFocus = cocoaEvent->event.focus.hasFocus;
328             invalidatePlugin(obj);
329             return 1;
330             
331         case NPCocoaEventDrawRect:
332             handleDraw(obj);
333             return 1;
334         
335         case NPCocoaEventKeyDown:
336         case NPCocoaEventKeyUp:
337         case NPCocoaEventFlagsChanged:
338             handleKeyboardEvent(obj, cocoaEvent);
339             return 1;
340             
341         case NPCocoaEventMouseDown:
342         case NPCocoaEventMouseUp:
343             
344         // FIXME: NPCocoaEventMouseMoved is currently disabled in order to see other events more clearly
345         // without "drowning" in mouse moved events.
346 //        case NPCocoaEventMouseMoved:
347         case NPCocoaEventMouseEntered:
348         case NPCocoaEventMouseExited:
349         case NPCocoaEventMouseDragged:
350         case NPCocoaEventScrollWheel:
351             handleMouseEvent(obj, cocoaEvent);
352             return 1;
353     }
354     
355     return 0;
356 }
357
358 void NPP_URLNotify(NPP instance, const char* url, NPReason reason, void* notifyData)
359 {
360
361 }
362
363 NPError NPP_GetValue(NPP instance, NPPVariable variable, void *value)
364 {
365     return NPERR_GENERIC_ERROR;
366 }
367
368 NPError NPP_SetValue(NPP instance, NPNVariable variable, void *value)
369 {
370     return NPERR_GENERIC_ERROR;
371 }