1563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark/*
2563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc. ("Apple") in
3563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark consideration of your agreement to the following terms, and your use, installation,
4563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark modification or redistribution of this Apple software constitutes acceptance of these
5563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark terms.  If you do not agree with these terms, please do not use, install, modify or
6563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark redistribute this Apple software.
7563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
8563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark In consideration of your agreement to abide by the following terms, and subject to these
9563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark terms, Apple grants you a personal, non-exclusive license, under Apple’s copyrights in
10563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark this original Apple software (the "Apple Software"), to use, reproduce, modify and
11563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark redistribute the Apple Software, with or without modifications, in source and/or binary
12563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark forms; provided that if you redistribute the Apple Software in its entirety and without
13563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark modifications, you must retain this notice and the following text and disclaimers in all
14563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark such redistributions of the Apple Software.  Neither the name, trademarks, service marks
15563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark or logos of Apple Computer, Inc. may be used to endorse or promote products derived from
16563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark the Apple Software without specific prior written permission from Apple. Except as expressly
17563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark stated in this notice, no other rights or licenses, express or implied, are granted by Apple
18563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark herein, including but not limited to any patent rights that may be infringed by your
19563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark derivative works or by other works in which the Apple Software may be incorporated.
20563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
21563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO WARRANTIES,
22563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF NON-INFRINGEMENT,
23563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS
24563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark USE AND OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
25563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
26563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR CONSEQUENTIAL
27563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE,
29563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND
30563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR
31563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark */
33563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
34563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark#import <WebKit/npapi.h>
35563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark#import <WebKit/npfunctions.h>
36563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark#import <WebKit/npruntime.h>
37563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
38563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark#import <Cocoa/Cocoa.h>
39563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
40563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark#import "MenuHandler.h"
41563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
42563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark// Browser function table
43563af33bc48281d19dce701398dbb88cb54fd7ecCary Clarkstatic NPNetscapeFuncs* browser;
44563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
45563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark// Structure for per-instance storage
46563af33bc48281d19dce701398dbb88cb54fd7ecCary Clarktypedef struct PluginObject
47563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark{
48563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    NPP npp;
49563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
50563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    NPWindow window;
51563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
52563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    NSString *string;
53563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    bool hasFocus;
54563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    bool mouseIsInsidePlugin;
55563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
56563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    MenuHandler *menuHandler;
57563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark} PluginObject;
58563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
59545e470e52f0ac6a3a072bf559c796b42c6066b6Ben MurdochNPError NPP_New(NPMIMEType pluginType, NPP instance, uint16_t mode, int16_t argc, char* argn[], char* argv[], NPSavedData* saved);
60563af33bc48281d19dce701398dbb88cb54fd7ecCary ClarkNPError NPP_Destroy(NPP instance, NPSavedData** save);
61563af33bc48281d19dce701398dbb88cb54fd7ecCary ClarkNPError NPP_SetWindow(NPP instance, NPWindow* window);
62545e470e52f0ac6a3a072bf559c796b42c6066b6Ben MurdochNPError NPP_NewStream(NPP instance, NPMIMEType type, NPStream* stream, NPBool seekable, uint16_t* stype);
63563af33bc48281d19dce701398dbb88cb54fd7ecCary ClarkNPError NPP_DestroyStream(NPP instance, NPStream* stream, NPReason reason);
64545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdochint32_t NPP_WriteReady(NPP instance, NPStream* stream);
65545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdochint32_t NPP_Write(NPP instance, NPStream* stream, int32_t offset, int32_t len, void* buffer);
66563af33bc48281d19dce701398dbb88cb54fd7ecCary Clarkvoid NPP_StreamAsFile(NPP instance, NPStream* stream, const char* fname);
67563af33bc48281d19dce701398dbb88cb54fd7ecCary Clarkvoid NPP_Print(NPP instance, NPPrint* platformPrint);
68545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdochint16_t NPP_HandleEvent(NPP instance, void* event);
69563af33bc48281d19dce701398dbb88cb54fd7ecCary Clarkvoid NPP_URLNotify(NPP instance, const char* URL, NPReason reason, void* notifyData);
70563af33bc48281d19dce701398dbb88cb54fd7ecCary ClarkNPError NPP_GetValue(NPP instance, NPPVariable variable, void *value);
71563af33bc48281d19dce701398dbb88cb54fd7ecCary ClarkNPError NPP_SetValue(NPP instance, NPNVariable variable, void *value);
72563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
73563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark#pragma export on
74563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark// Mach-o entry points
75563af33bc48281d19dce701398dbb88cb54fd7ecCary ClarkNPError NP_Initialize(NPNetscapeFuncs *browserFuncs);
76563af33bc48281d19dce701398dbb88cb54fd7ecCary ClarkNPError NP_GetEntryPoints(NPPluginFuncs *pluginFuncs);
77563af33bc48281d19dce701398dbb88cb54fd7ecCary Clarkvoid NP_Shutdown(void);
78563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark#pragma export off
79563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
80563af33bc48281d19dce701398dbb88cb54fd7ecCary ClarkNPError NP_Initialize(NPNetscapeFuncs* browserFuncs)
81563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark{
82563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    browser = browserFuncs;
83563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    return NPERR_NO_ERROR;
84563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark}
85563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
86563af33bc48281d19dce701398dbb88cb54fd7ecCary ClarkNPError NP_GetEntryPoints(NPPluginFuncs* pluginFuncs)
87563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark{
88563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    pluginFuncs->version = 11;
89563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    pluginFuncs->size = sizeof(pluginFuncs);
90563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    pluginFuncs->newp = NPP_New;
91563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    pluginFuncs->destroy = NPP_Destroy;
92563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    pluginFuncs->setwindow = NPP_SetWindow;
93563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    pluginFuncs->newstream = NPP_NewStream;
94563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    pluginFuncs->destroystream = NPP_DestroyStream;
95563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    pluginFuncs->asfile = NPP_StreamAsFile;
96563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    pluginFuncs->writeready = NPP_WriteReady;
97563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    pluginFuncs->write = (NPP_WriteProcPtr)NPP_Write;
98563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    pluginFuncs->print = NPP_Print;
99563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    pluginFuncs->event = NPP_HandleEvent;
100563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    pluginFuncs->urlnotify = NPP_URLNotify;
101563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    pluginFuncs->getvalue = NPP_GetValue;
102563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    pluginFuncs->setvalue = NPP_SetValue;
103563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
104563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    return NPERR_NO_ERROR;
105563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark}
106563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
107563af33bc48281d19dce701398dbb88cb54fd7ecCary Clarkvoid NP_Shutdown(void)
108563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark{
109563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
110563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark}
111563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
112545e470e52f0ac6a3a072bf559c796b42c6066b6Ben MurdochNPError NPP_New(NPMIMEType pluginType, NPP instance, uint16_t mode, int16_t argc, char* argn[], char* argv[], NPSavedData* saved)
113563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark{
114563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    // Create per-instance storage
115563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    PluginObject *obj = (PluginObject *)malloc(sizeof(PluginObject));
116563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    bzero(obj, sizeof(PluginObject));
117563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
118563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    obj->npp = instance;
119563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    instance->pdata = obj;
120563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
121563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    // Ask the browser if it supports the CoreGraphics drawing model
122563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    NPBool supportsCoreGraphics;
123563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    if (browser->getvalue(instance, NPNVsupportsCoreGraphicsBool, &supportsCoreGraphics) != NPERR_NO_ERROR)
124563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark        supportsCoreGraphics = FALSE;
125563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
126563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    if (!supportsCoreGraphics)
127563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark        return NPERR_INCOMPATIBLE_VERSION_ERROR;
128563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
129563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    // If the browser supports the CoreGraphics drawing model, enable it.
130563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    browser->setvalue(instance, NPPVpluginDrawingModel, (void *)NPDrawingModelCoreGraphics);
131563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
132563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    // If the browser supports the Cocoa event model, enable it.
133563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    NPBool supportsCocoa;
134563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    if (browser->getvalue(instance, NPNVsupportsCocoaBool, &supportsCocoa) != NPERR_NO_ERROR)
135563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark        supportsCocoa = FALSE;
136563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
137563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    if (!supportsCocoa)
138563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark        return NPERR_INCOMPATIBLE_VERSION_ERROR;
139563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
140563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    browser->setvalue(instance, NPPVpluginEventModel, (void *)NPEventModelCocoa);
141563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
142563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    return NPERR_NO_ERROR;
143563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark}
144563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
145563af33bc48281d19dce701398dbb88cb54fd7ecCary ClarkNPError NPP_Destroy(NPP instance, NPSavedData** save)
146563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark{
147563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    // Free per-instance storage
148563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    PluginObject *obj = instance->pdata;
149563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
150563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    [obj->string release];
151563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    [obj->menuHandler release];
152563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
153563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    free(obj);
154563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
155563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    return NPERR_NO_ERROR;
156563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark}
157563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
158563af33bc48281d19dce701398dbb88cb54fd7ecCary ClarkNPError NPP_SetWindow(NPP instance, NPWindow* window)
159563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark{
160563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    PluginObject *obj = instance->pdata;
161563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    obj->window = *window;
162563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
163563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    return NPERR_NO_ERROR;
164563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark}
165563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
166563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
167545e470e52f0ac6a3a072bf559c796b42c6066b6Ben MurdochNPError NPP_NewStream(NPP instance, NPMIMEType type, NPStream* stream, NPBool seekable, uint16_t* stype)
168563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark{
169563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    *stype = NP_ASFILEONLY;
170563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    return NPERR_NO_ERROR;
171563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark}
172563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
173563af33bc48281d19dce701398dbb88cb54fd7ecCary ClarkNPError NPP_DestroyStream(NPP instance, NPStream* stream, NPReason reason)
174563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark{
175563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    return NPERR_NO_ERROR;
176563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark}
177563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
178545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdochint32_t NPP_WriteReady(NPP instance, NPStream* stream)
179563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark{
180563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    return 0;
181563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark}
182563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
183545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdochint32_t NPP_Write(NPP instance, NPStream* stream, int32_t offset, int32_t len, void* buffer)
184563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark{
185563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    return 0;
186563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark}
187563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
188563af33bc48281d19dce701398dbb88cb54fd7ecCary Clarkvoid NPP_StreamAsFile(NPP instance, NPStream* stream, const char* fname)
189563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark{
190563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark}
191563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
192563af33bc48281d19dce701398dbb88cb54fd7ecCary Clarkvoid NPP_Print(NPP instance, NPPrint* platformPrint)
193563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark{
194563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
195563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark}
196563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
1970bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdochstatic void handleDraw(PluginObject *obj, NPCocoaEvent *event)
198563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark{
199563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    NSGraphicsContext *oldContext = [[NSGraphicsContext currentContext] retain];
200563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
2010bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch    NSGraphicsContext *context = [NSGraphicsContext graphicsContextWithGraphicsPort:event->data.draw.context
202563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark                                                                            flipped:YES];
203563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
204563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    [NSGraphicsContext setCurrentContext:context];
205563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
206563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    NSRect rect = NSMakeRect(0, 0, obj->window.width, obj->window.height);
207563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
208563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    [[NSColor lightGrayColor] set];
209563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    [NSBezierPath fillRect:rect];
210563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
211563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    // If the plugin has focus, draw a focus indicator
212563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    if (obj->hasFocus) {
213563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark        [[NSColor blackColor] set];
214563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark        NSBezierPath *path = [NSBezierPath bezierPathWithRect:rect];
215563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark        [path setLineWidth:5];
216563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark        [path stroke];
217563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    }
218563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
219563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    [obj->string drawAtPoint:NSMakePoint(10, 10) withAttributes:nil];
220563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
221563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    [NSGraphicsContext setCurrentContext:oldContext];
222563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark}
223563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
224563af33bc48281d19dce701398dbb88cb54fd7ecCary Clarkstatic NSString *eventType(NPCocoaEventType type)
225563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark{
226563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    switch (type) {
227563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark        case NPCocoaEventScrollWheel:
228563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark            return @"NPCocoaEventScrollWheel";
229563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark        case NPCocoaEventMouseDown:
230563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark            return @"NPCocoaEventMouseDown";
231563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark        case NPCocoaEventMouseUp:
232563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark            return @"NPCocoaEventMouseUp";
233563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark        case NPCocoaEventMouseMoved:
234563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark            return @"NPCocoaEventMouseMoved";
235563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark        case NPCocoaEventMouseDragged:
236563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark            return @"NPCocoaEventMouseDragged";
237563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark        case NPCocoaEventMouseEntered:
238563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark            return @"NPCocoaEventMouseEntered";
239563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark        case NPCocoaEventMouseExited:
240563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark            return @"NPCocoaEventMouseExited";
241563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark        case NPCocoaEventKeyDown:
242563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark            return @"NPCocoaEventKeyDown";
243563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark        case NPCocoaEventKeyUp:
244563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark            return @"NPCocoaEventKeyUp";
245563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark        case NPCocoaEventFlagsChanged:
246563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark            return @"NPCocoaEventFlagsChanged";
247563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark        default:
248563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark            return @"unknown";
249563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    }
250563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark}
251563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
252563af33bc48281d19dce701398dbb88cb54fd7ecCary Clarkstatic void invalidatePlugin(PluginObject *obj)
253563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark{
254563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    NPRect rect;
255563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    rect.left = 0;
256563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    rect.top = 0;
257563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    rect.right = obj->window.width;
258563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    rect.bottom = obj->window.height;
259563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
260563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    browser->invalidaterect(obj->npp, &rect);
261563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark}
262563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
263563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
264563af33bc48281d19dce701398dbb88cb54fd7ecCary Clarkstatic void handleMouseEvent(PluginObject *obj, NPCocoaEvent *event)
265563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark{
266563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    NSString *string = [NSString stringWithFormat:@"Type: %@\n"
267563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark                                                   "Modifier flags: 0x%x\n"
268563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark                                                   "Coordinates: (%g, %g)\n"
269563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark                                                   "Button number: %d\n"
270563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark                                                   "Click count: %d\n"
271563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark                                                   "Delta: (%g, %g, %g)",
272563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark                                                   eventType(event->type),
273563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark                                                   event->data.mouse.modifierFlags,
274563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark                                                   event->data.mouse.pluginX,
275563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark                                                   event->data.mouse.pluginY,
276563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark                                                   event->data.mouse.buttonNumber,
277563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark                                                   event->data.mouse.clickCount,
278563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark                                                   event->data.mouse.deltaX, event->data.mouse.deltaY, event->data.mouse.deltaZ];
279563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
280563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
281563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    [obj->string release];
282563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    obj->string = [string retain];
283563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
284563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    invalidatePlugin(obj);
285563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
286563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    if (event->data.mouse.buttonNumber == 1) {
287563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark        if (!obj->menuHandler)
288563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark            obj->menuHandler = [[MenuHandler alloc] initWithBrowserFuncs:browser instance:obj->npp];
289563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
290563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark        browser->popupcontextmenu(obj->npp, (NPNSMenu *)[obj->menuHandler menu]);
291563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    }
292563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark}
293563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
294563af33bc48281d19dce701398dbb88cb54fd7ecCary Clarkstatic void handleKeyboardEvent(PluginObject *obj, NPCocoaEvent *event)
295563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark{
296563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    NSString *string = [NSString stringWithFormat:@"Type: %@\n"
297563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark                        "Modifier flags: 0x%x\n"
298563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark                        "Characters: %@\n"
299563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark                        "Characters ignoring modifiers: %@\n"
300563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark                        "Is a repeat: %@\n"
301563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark                        "Key code: %d",
302563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark                        eventType(event->type),
303563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark                        event->data.key.modifierFlags,
304563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark                        event->data.key.characters,
305563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark                        event->data.key.charactersIgnoringModifiers,
306563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark                        event->data.key.isARepeat ? @"YES" : @"NO",
307563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark                        event->data.key.keyCode];
308563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
309563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
310563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    [obj->string release];
311563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    obj->string = [string retain];
312563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
313563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    invalidatePlugin(obj);
314563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark}
315563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
316545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdochint16_t NPP_HandleEvent(NPP instance, void* event)
317563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark{
318563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    PluginObject *obj = instance->pdata;
319563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
320563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    NPCocoaEvent *cocoaEvent = event;
321563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
322563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    switch(cocoaEvent->type) {
323563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark        case NPCocoaEventFocusChanged:
324563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark            obj->hasFocus = cocoaEvent->data.focus.hasFocus;
325563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark            invalidatePlugin(obj);
326563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark            return 1;
327563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
328563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark        case NPCocoaEventDrawRect:
3290bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch            handleDraw(obj, cocoaEvent);
330563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark            return 1;
331563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
332563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark        case NPCocoaEventKeyDown:
333563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark        case NPCocoaEventKeyUp:
334563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark        case NPCocoaEventFlagsChanged:
335563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark            handleKeyboardEvent(obj, cocoaEvent);
336563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark            return 1;
337563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
338563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark        case NPCocoaEventMouseDown:
339563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark        case NPCocoaEventMouseUp:
340563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
341563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark        // FIXME: NPCocoaEventMouseMoved is currently disabled in order to see other events more clearly
342563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark        // without "drowning" in mouse moved events.
343563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark//        case NPCocoaEventMouseMoved:
344563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark        case NPCocoaEventMouseEntered:
345563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark        case NPCocoaEventMouseExited:
346563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark        case NPCocoaEventMouseDragged:
347563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark        case NPCocoaEventScrollWheel:
348563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark            handleMouseEvent(obj, cocoaEvent);
349563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark            return 1;
350563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    }
351563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
352563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    return 0;
353563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark}
354563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
355563af33bc48281d19dce701398dbb88cb54fd7ecCary Clarkvoid NPP_URLNotify(NPP instance, const char* url, NPReason reason, void* notifyData)
356563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark{
357563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
358563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark}
359563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
360563af33bc48281d19dce701398dbb88cb54fd7ecCary ClarkNPError NPP_GetValue(NPP instance, NPPVariable variable, void *value)
361563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark{
362563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    return NPERR_GENERIC_ERROR;
363563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark}
364563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
365563af33bc48281d19dce701398dbb88cb54fd7ecCary ClarkNPError NPP_SetValue(NPP instance, NPNVariable variable, void *value)
366563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark{
367563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    return NPERR_GENERIC_ERROR;
368563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark}
369