2011-08-11 24 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

相關問題