1

I've followed the NetscapeCocoaPlugin example from the nightly Webkit build, and I'm able to build a NPAPI style plugin that uses the Cocoa Event Model.

My question now, is how I can get the NSView inside NPP_SetWindow.

A poster in this thread, says that it's possible using [NSView focusView], but I haven't been able to get this to work

My current function looks like this:

NPError NPP_SetWindow(NPP instance, NPWindow* window)
{
PluginObject *obj = instance->pdata;
obj->window = *window;

NSLog(@"Set Window called");

NSView* currentView = [NSView focusView];

[[NSColor redColor] set]; // Sets current drawing color.
NSRectFill(NSMakeRect(10, 10, 2, 20)); // Defines a rectangle and then fills it with the current drawing color.
[[NSColor colorWithCalibratedRed:0.7 green:0.9 blue:0.3 alpha:1.0] set]; // Sets a new color.
[[NSBezierPath bezierPathWithOvalInRect:NSMakeRect(5, 0, 10, 10)] fill]; // Draws a circle in the new color.

[currentView setNeedsDisplay:YES];

return NPERR_NO_ERROR;
}
Community
  • 1
  • 1
CambridgeMike
  • 4,562
  • 1
  • 28
  • 37

1 Answers1

1

You can't. There was a time when you could get the NSView using a hack, but it was never supported, never a good idea, and no longer possible because all three browsers have switched to using out of process plugins, which means you can't get access to the NSView.

You can get a CGContextRef and then create your own offscreen NSWindow and NSView and render them into the CGContextRef, but then you'd have to proxy all events as well. There is a WebView wrapper in FireBreath that is experimental still that does this, but it is quite a pain. Eventually I plan to turn it into something more generic so that an NSView can (kinda) be used in a plugin, but there is no native way to do so.

There is an excellent blog post about mac drawing models here: http://www.escapedthoughts.com/weblog/geek/P110308-mac-npapi-plugins.writeback

taxilian
  • 14,229
  • 4
  • 34
  • 73
  • Oh, interesting! If I wanted to get the CGContextRef, would I do it by calling:NP_CGContext* npContext = (NP_CGContext*) window->window; npContext->context ? – CambridgeMike Aug 11 '11 at 16:13
  • That depends on a lot of things. First, you need to choose CoreGraphics as your drawing model (https://wiki.mozilla.org/NPAPI:Models and https://wiki.mozilla.org/NPAPI:CoreGraphicsDrawing). Then if you negotiated the Carbon event model that will probably work, but if you're using Cocoa (see https://wiki.mozilla.org/NPAPI:CocoaEventModel) you can't save the CGContextRef but need to get it from the event data each time you get a CocoaEvent from NPAPI. those links should help get you started, or help you formulate a new question. – taxilian Aug 11 '11 at 16:18