2011-08-11 18 views
1

我已经从夜间Webkit构建中跟踪了NetscapeCocoaPlugin示例,并且我能够构建使用Cocoa事件模型的NPAPI样式插件。我如何在可可事件模型中获得NSView NPAPI插件

我现在的问题是,我可以如何获得NPP_SetWindow内的NSView。

在此thread海报,说,这是可能使用[的NSView focusView],但我一直没能得到这个工作

我现在的功能是这样的:

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

你不能。曾经有一段时间你可以使用黑客来获取NSView,但它从来没有被支持,从来也不是一个好主意,也不再可能,因为所有三个浏览器都已经切换到使用进程外插件,这意味着你无法访问到NSView。

你可以得到一个CGContextRef,然后创建你自己的屏幕NSWindow和NSView并将它们渲染到CGContextRef中,但是你必须代理所有的事件。 FireBreath中有WebView wrapper这是实验仍然是这样做,但它是一个相当痛苦。最终我打算把它变成更通用的东西,以便NSView可以(有点)用在插件中,但是没有原生的方式来做。

有一个关于MAC绘制模型这里一个很好的博客文章:http://www.escapedthoughts.com/weblog/geek/P110308-mac-npapi-plugins.writeback

+0

呵呵,有意思!如果我想获得CGContextRef,我可以通过调用:NP_CGContext * npContext =(NP_CGContext *)window-> window; npContext->上下文? – CambridgeMike

+0

这取决于很多事情。首先,您需要选择CoreGraphics作为您的绘图模型(https://wiki.mozilla.org/NPAPI:Models和https://wiki.mozilla.org/NPAPI:CoreGraphicsDrawing)。然后,如果您协商了可能可行的Carbon事件模型,但如果您使用Cocoa(请参阅https://wiki.mozilla.org/NPAPI:CocoaEventModel),则无法保存CGContextRef,但需要从事件数据每次从NPAPI获得一个CocoaEvent。这些链接应该帮助你开始,或帮助你制定一个新的问题。 – taxilian

+0

如果这是答案,请将其标记为这样 – taxilian

相关问题