开发者

NPAPI drawPlugin retrieve cgContext from NPP instance

Im trying to draw a png in my (NPAPI)webplugin for Mac based on basic-plugin.

I would like to redraw the plugin on a NPCocoaEventMouseDown but I'm having trouwens to retrieve the cgContextRef.

The methode below works for the NPCocoaEventDrawRect but doesn't for the NPCocoaEventMouseDown because then I can't use event->data.draw.context. I tried to retrieve the cgContextRef with



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


but that didn't seem to work. Here's my function:



    void drawPlugin(NPP instance, NPCocoaEvent* event)
    {
        char* path = "/shot.png";
        if(!instance || !event)
            return;
        PluginInstance* currentInstance = (PluginInstance*)(instance->pdata);
        //CGConte开发者_StackOverflow社区xtRef 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);
    }

And the function is called here:



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

How can I retrieve the cgContextRef in a NPCocoaEventMouseDown?


I would like to redraw the plugin on a NPCocoaEventMouseDown

How can I retrieve the cgContextRef in a NPCocoaEventMouseDown?

You can't do either of those things. You call NPN_InvalidateRect in your mouse-down handler, and wait to get a draw call-back.

I tried to retrieve the cgContextRef with

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

but that didn't seem to work.

Because that field is always NULL under the Cocoa event model, as documented in the Cocoa event spec. You are explicitly only provided a CGContextRef during a paint call, and it is only required to be valid for the duration of that call. (In case you are thinking of caching it for later use: don't. The results will be totally undefined behavior, probably won't work, certainly can't be relied on to work, and will almost certainly cause crashes at some point in some browser.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜