Invoking the Tcl Plugin

revised 21 May 2006
Google site search

ContentsIntroductionBasicIntermediateAdvancedFuturePolicyInfrastructure
You need the Tcl Plugin to read this page. If this spot  is not green (v2) or cyan (v3)you may want to download the latest Tcl Plugin.

1   Invoking the Tcl Plugin        

Documentation is found at the Tcl Developer Exchange. This documentation does not tell you what you can do in the Tclet. Instead it talks a bout "Policies". Policies are the rules which determine what commands are allowed and not allowed to be used in a plugin. This is determined on the basis of classes of restrictive command sets, ranging from the most resticted home policy up to the unrestricted trusted policy. home is the default policy for plugins.

The home policy allows the tclet to access facilities on the home (host) which sent the tclet. This means that a Tclet on your webpage that you view on your own computer may not behave the same on someone elses computer. Interactions with the webpage-viewer's computer are prevented.

The Tclet cannot be used to save a file on the viewers computer (unless you view the tcllet on your own server), not even clipboard access etc. Basically all the tclet provides is a window by which the viewer can see whatever you want to show them from your computer. Thats it.

But for simple games, demonstrations, static illustrations or illustrations that respond to the user it is great.

In most browsers a simple <embed> tag is sufficient, as in:

<embed src='echoparams.tcl' width='140' height='30' type='application/x-tcl' />

But Wait ! Unfortunately not everyone is going to be able to see this tclet. If you are using Internet Explorer you are probably missing out.

1.1   Invoking the Tcl Plugin under Windows        

Microsoft is still engaged in legal disputes about its use of <embed> so the <object> tag has to be used instead. So the invocation above only produces a blank box. This situation is similar to that for flash and other plugins. The <object> tag is supposed to be different from <embed> which accounts for its clumsy syntax. The general format for invoking the plugin is:

<object
    id='PluginHostCtrl'
    classid='CLSID:14E78123-A693-4F27-B6EE-DDDE18F93D3A'
    width='400'
    height='120'
>
    <param name='myArg' value='itsValue' />
    <param name='src' value='echoparams.tcl' />
    <param name='type' value='application/x-tcl' />
    <param name='pluginspage' value='http://plugin.tcl.tk/' />

    <embed
        width='400'
        height='120'
        myArg='itsValue'
        src='echoparams.tcl'
        type='application/x-tcl'
        pluginspage='http://plugin.tcl.tk/'
    >
    </embed>
</object>

Notice the two different ways of specifying parameters - <param name='myArg' value='itsValue' /> in the <object> tag and the myArg='itsValue' in the <embed> tag. With this html you should be able to see the tclet displayed here.

Notice that "id" and "classid" do not appear in the embed_args array. This keeps the interface the same as for <embed>.

I use the following routines to create my <object/embed> html for my webpages.

proc plugin args {
    array set arg $args
    set object "\n<object"
    set embed "\n    <embed"
    foreach i {id ID classid CLASSID codebase CODEBASE } {
        if { [info exists arg($i)] } {
            append object "\n    $i='$arg($i)'"
            unset arg($i)  
        }
    }
    foreach i {width WIDTH height HEIGHT} {
        if { [info exists arg($i)] } {
            append object "\n    $i='$arg($i)'"
            append embed "\n        $i='$arg($i)'"
            unset arg($i)
        }
    }
    append object "\n>"
    foreach i [array names arg] {
        append object "\n    <param name='$i' value='$arg($i)' />"
        append embed "\n        $i='$arg($i)'"
    }
    return "$object\n$embed\n    >\n    </embed>\n</object>"
}
proc "plugin tcl" args {
    return [eval plugin id PluginHostCtrl \
        classid CLSID:14E78123-A693-4F27-B6EE-DDDE18F93D3A \
        type application/x-tcl pluginspage http://plugin.tcl.tk/ $args]
}

proc "plugin flash6" args {
    return [eval plugin classid "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" \
        codebase "http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" \
        quality high \
        type application/x-shockwave-flash    pluginspage http://www.macromedia.com/go/getflashplayer $args]
}

[plugin] is a generic procedure for all plugins, so it can do Flash as well as anything else.
["plugin tcl"] is the special form for a Tcl Plugin. Notice the quotes.

Where the height and width determine the size of the window on the webpage in pixels. The size of the window is not determined from the Tcl code, so it is up to you to match the dimensions.

1.2   Exercise        

The plugin procedure is for use in a server or some other program which is generating the html code for the webpage. There are two options here. If the webpage being created is static, or pseudo-static, for instance it is a cached template page which is only sometimes regenerated in response to a page request, then in this case the plugin procedure above needs to be used. But if the page is being dynamically generated in response to each user request then the plugin procedure can look at its environment and produce either the <object> or the <embed> html code required by the particular user.

Write the plugin procedure which does this. Within the Tclet the embed_args array contains an element for each parameter passed into the tclet from the html. This means that you can size your tclet based on the embed_args(width) and embed_args(height) values. The Tclet above echoes the embed_args values back. someArg becomes an array element variable known in the Tclet.

The height and width values passed into the tclet can be used there to scale the tclet if you wish or to determine if the tclet needs scrollbars to be displayed properly.

Notice that the embed_args array has an element embed_mode set to embed. This is a way you can tell that your tcl code is being used as a tclet without relying on the existence of the embed_args array which you may find convenient to have regardless of whether your Tcl is in a tclet or is just a wish script.

You need to click inside the plugin frame in order to get focus. Then you can move the scrollbar. The text widget containing the text has text editor functions - select ,insert and delete. Copy and Paste are not available as they access the system clipboard. The Tclet code is shown below:

frame .f  -borderwidth 10 -relief raised -background #ff0000
pack .f
text .f.t -yscrollcommand {.f.sy set} -width 60
pack .f.t -side left -fill both -expand true
scrollbar .f.sy -command {.f.t yview} -orient vertical 
pack .f.sy -side right -fill y

.f.t insert end "Echo Plugin Parameters"
foreach i [array names embed_args] {
     .f.t insert end "\nembed_args($i) = $embed_args($i)"
}


# Notes on this Tclet
#
# the frame .f enclosing the text and scrollbar widgets is given some borderwidth,
# relief and colour so that you can see it clearly.
#
# the frame height and width dimensions are in pixels 
# while the text height and width are in lines and characters respectively

2   The script option and other stuff        

The man-type webpage provides a detailed specification of the Plugin. A couple of things to note here are:

3   Other Policies        

Here is a rough guide to the Policies which a Tclet can ask to be allowed to use: There are additional policies that allow access to the guests computer, such as the tempfile policy. This policy is set up on the guests computer in the Plugin directories. It will probably be specially written to enable a specific application used within a company. It will limit file sizes and number of files that a Tclet can use, so as not to impact on the guests computer.

These additional policies are for the very advanced user writing a special applicatiopn for an intranet.

This and Tcl/Javascript are covered in the book "Web TCL Complete" by Steve Hall, McGraw-Hill. ISBN: 007913713X
Prev Next

©2000 - 2006 WEBSCOOL This page last updated 02 Jun 2006. All rights reserved - including copying or distribution of any portion of this document in any form or on any medium without authorisation. For more regarding the copyright.