NPAPI drawPlugin从NPP实例中检索cgContext

时间:2011-10-11 13:41:58

标签: events cgcontext npapi

我试图根据basic-plugin在我的(NPAPI)Mac插件中绘制一个png。

我想在NPCocoaEventMouseDown上重新绘制插件,但我有麻烦来检索cgContextRef

下面的方法适用于NPCocoaEventDrawRect,但不适用于NPCocoaEventMouseDown,因为我无法使用event->data.draw.context。我尝试使用

检索cgContextRef


    CGContextRef cgContext = (NP_CGContext*)currentInstance->window.window


但这似乎不起作用。这是我的功能:



    void drawPlugin(NPP instance, NPCocoaEvent* event)
    {
        char* path = "/shot.png";
        if(!instance || !event)
            return;
        PluginInstance* currentInstance = (PluginInstance*)(instance->pdata);
        //CGContextRef cgContext = event->data.draw.context; //works with DrawRect
        CGContextRef cgContext = (NP_CGContext*)currentInstance->window.window;
        if (!cgContext) {
            return;
        }
        float windowWidth = currentInstance->window.width;
        float windowHeight = currentInstance->window.height;

        CGContextSaveGState(cgContext);
        //.....
        CGContextRestoreGState(cgContext);
    }

这里调用函数:



    int16_t NPP_HandleEvent(NPP instance, void* event)
    {
      NPCocoaEvent* cocoaEvent = (NPCocoaEvent*)event;
      if (cocoaEvent && (cocoaEvent->type == NPCocoaEventDrawRect)) {
          return 1;
        }

      if(cocoaEvent)
      {
        switch (cocoaEvent->type) {
            case NPCocoaEventDrawRect:
                drawPlugin(instance, (NPCocoaEvent*)event);
                break;
            case NPCocoaEventMouseDown:
                drawPlugin(instance, (NPCocoaEvent*)event);
                break;
            default:
                break;
        }  
          return 1;
      }
        return 0;
    }

如何检索cgContextRef中的NPCocoaEventMouseDown

1 个答案:

答案 0 :(得分:1)

  

我想在NPCocoaEventMouseDown

上重绘插件      

如何在NPCocoaEventMouseDown中检索cgContextRef?

你无法做到这两件事。您在鼠标按下处理程序中调用NPN_InvalidateRect,并等待绘制回调。

  

我试图用

检索cgContextRef      

CGContextRef cgContext =(NP_CGContext *)currentInstance-> window.window

     

但这似乎不起作用。

因为Cocoa事件模型下该字段始终为NULL,如Cocoa事件规范中所述。在绘制调用期间,您只是显式提供了CGContextRef,并且只需要在该调用期间有效。 (如果您正在考虑将其缓存以供以后使用:不要。结果将是完全未定义的行为,可能无法正常工作,当然不能依赖于工作,并且几乎肯定会在某些时候导致崩溃在某些浏览器中。)

相关问题