How do I get an NSView in a cocoa event model NPAPI plugin

旧城冷巷雨未停 提交于 2019-12-11 13:02:31

问题


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;
}

回答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



来源:https://stackoverflow.com/questions/7028596/how-do-i-get-an-nsview-in-a-cocoa-event-model-npapi-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!