Plugins, events and scripting

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Plugins, events and scripting

Scott-114
 I am implementing a plugin for firefox, and am trying to do a couple of
things:

1. Implement an event handler in my c++ code and have it registered as the
event handler for a DOM element's event.


2. Expose a scriptable event from my c++ code, so that javascript in the
page can add and event listener.


Are there any examples? Or even pointers to get me started...


Thanks, Kevin


_______________________________________________
dev-tech-plugins mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-plugins
Reply | Threaded
Open this post in threaded view
|

Re: Plugins, events and scripting

Martin Husemann
On Mon, Sep 04, 2006 at 09:59:53PM +0800, Scott wrote:
> 2. Expose a scriptable event from my c++ code, so that javascript in the
> page can add and event listener.

Just a single event?

I had a whole bunch of events that my plugin could fire, so I defined
an interface for them (in the idl file), then have the page implement a
JS object with this interface and assign it to an "EventSink" attribute
of the plugin.

But this sounds like overkill if you want a single event.

Martin
_______________________________________________
dev-tech-plugins mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-plugins
Reply | Threaded
Open this post in threaded view
|

Re: Plugins, events and scripting

Scott-114
In reply to this post by Scott-114
Not just one, I am going to develop a plugins to control  some elements of
HTML(i.e, focus, left, right, up, down and enter) with a keyboad.
Can you tell me, how to do I launch on it.  Thanks.


"Martin Husemann" <[hidden email]>
??????:[hidden email]...

> On Mon, Sep 04, 2006 at 09:59:53PM +0800, Scott wrote:
>> 2. Expose a scriptable event from my c++ code, so that javascript in the
>> page can add and event listener.
>
> Just a single event?
>
> I had a whole bunch of events that my plugin could fire, so I defined
> an interface for them (in the idl file), then have the page implement a
> JS object with this interface and assign it to an "EventSink" attribute
> of the plugin.
>
> But this sounds like overkill if you want a single event.
>
> Martin


_______________________________________________
dev-tech-plugins mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-plugins
Reply | Threaded
Open this post in threaded view
|

Re: Plugins, events and scripting

Martin Husemann
On Tue, Sep 05, 2006 at 09:55:11AM +0800, Scott wrote:
> Can you tell me, how to do I launch on it.

I'm not sure if this fits your needs, but it works for me.
My customer has not published the plugin yet, so I'm leaving a few parts
blank...

I defined this interface in the idl file:

[scriptable, uuid(4E4AFB87-CC0D-11d2-AD31-00A0C9251384)]
interface nsIMyPluginEvents : nsISupports {
    void SubObjectPicked(in long Index, in string LocalName, in string DatabaseName, in string DisplayName);
    void Click(in long SubAnimIndex);
    void AnimationDone();
    void AnimationStarted(in long AnimationNo, in string Name, in boolean autoRepeat);
    void ObjectLoaded();
    void ThumbnailDone();
    void TextureChanged(in long SubObjectIndex, in long TextureIndex, in string NewTextureName);
    void MaterialChanged(in long SubObjectIndex, in long MaterialIndex, in string LoadingFileName);
    void ObjectMove(in long objOrGrpID, in long whichEvnt, in float x, in float y, in float z);
    void ObjectMoving(in long objOrGrpID, in float xMousePos, in float yMousePos, in float zMousePos, in float xCenterPos, in float yCenterPos, in float zCenterPos);
    void MouseReleased(in long event);
    void ClickWithKeys(in long flags, in long SubAnimIndex);
    void MouseDown(in long objID, in long whichMouseButton, in long flags);
};

and added an attribute:

attribute nsIMyPluginEvents EventSink;

Implementation is trivial, and to fire events I just call the coresponding
event sink function.

Now on the web page I do two thinks: first, create a JS object that
implements the nsIMyPluginEvents interface, and then, in the onload
event of the page, set this JS object as the event sink in my plugin.

That's all. (I'm a C++ programmer, not a JS guy, so I had a hard time
figuring out the strange initializer syntax for the interface implementation
in java script, but it actually is documented, and it works ;-})

Below is the relevant fragment from a sample page.

Martin


        <OBJECT DATA="test" TYPE="application/x-myplugin" WIDTH=400 HEIGHT=300 ID="thePlugin" VIEWASTEXT>
            <PARAM NAME=BorderStyle VALUE=1>
            <PARAM NAME=Appearance VALUE=1>
            <PARAM NAME=AnimPlaying VALUE=true>
            <PARAM NAME=BackColor VALUE=16777215>          
        </OBJECT>

<script Language="JavaScript">
       
        // Define an event object
        function jsMyEvents()
            {
            }
         
            // this is the required interface (note: the format is { , , , , }; - this is a static array of function
            // declarations added to the "prototype" property.
        jsMyEvents.prototype = {
       
                    QueryInterface : function(iid)
                    {
                            try {
                                    if (iid.equals(Components.interfaces.nsIMyPluginEvents) ||
                                    iid.equals(Components.interfaces.nsIClassInfo) ||
                                    iid.equals(Components.interfaces.nsISecurityCheckedComponent) ||
                                    iid.equals(Components.interfaces.nsISupports)) {
                                            return this;
                                    }
                                    throw Components.results.NS_ERROR_NO_INTERFACE;
                            } catch (se) {
                                    return this;    // this is wrong, but works around some browser bugs
                            }
                    },

                    Click : function(/*int*/ subAnimIndex)
                    {
                    },
                   
                    SubObjectPicked : function(/*int*/ Index, /*string*/ LocalName, /*string*/ DatabaseName, /*string*/ DisplayName)
                    {
                        onPick(Index, LocalName, DatabaseName);
                    },

            AnimationDone : function()
            {
                onAnimationDone();
            },

            AnimationStarted : function(/*int*/ AnimationNo, /*string*/ Name, /*boolean*/ autoRepeat) { },
            ObjectLoaded : function() {},
            ThumbnailDone : function() {},
            TextureChanged : function(/*long*/ SubObjectIndex, /*long*/ TextureIndex, /*string*/ NewTextureName) {},
            MaterialChanged : function(/*long*/ SubObjectIndex, /*long*/ MaterialIndex, /*string*/ LoadingFileName) {},
            ObjectMove : function(/*long*/ objOrGrpID, /*long*/ whichEvnt, /*float*/ x, /*float*/ y, /*float*/ z) {},
            ObjectMoving : function(/*long*/ objOrGrpID, /*float*/ xMousePos, /*float*/ yMousePos, /*float*/ zMousePos, /*float*/ xCenterPos, /*float*/ yCenterPos, /*float*/ zCenterPos) {},
            MouseReleased : function(/*long*/ event) {},
            ClickWithKeys : function(/*long*/ flags, /*long*/ SubAnimIndex) {},
            MouseDown : function(/*long*/ objID, /*long*/ whichMouseButton, /*long*/ flags) {}

            };
        // we can not connect the event sink object before the plugin is completely loaded
        window.onload = function (evt) {
                        thePlugin.EventSink = new jsMyEvents;
        }

</script>
_______________________________________________
dev-tech-plugins mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-plugins