1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
<!-- Copyright (c) 2000-2001 ActiveState Tool Corporation. -->
<!-- See the file LICENSE.txt for licensing information. -->
<center><b><font size=+2>Python Component Sample</font></b>
<p>
<br>
Last modified
<script>
document.write(document.lastModified);
</script>
</center>
<p>XPConnect allows JavaScript
to transparantly access and manipulate XPCOM objects;
<p>Big Deal, I hear you say! But it also works for Python!!!
<p>
This sample demonstrates accessing a XPCOM object through XPConnect.
The JavaScript executed when this page loads creates an instance
of the Python object by
using the <tt>Components</tt> object, then accesses it through
the <a href="py_test_component.idl">nsISample</a> interface by calling <tt>QueryInterface</tt>:
<br>
<pre>
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
var sample = Components.classes["component://mozilla/sample/sample-world"].createInstance();
sample = sample.QueryInterface(Components.interfaces.nsISample);
</pre>
<p>
The buttons on the form are connected to JavaScript event handlers which
call the methods defined in Python
<p><b><a name="Compiling">Compiling the idl</b>
<p>The XPIDL compiler (xpidl on Unix, xpidl.exe on Windows, and a CodeWarrior plugin on Mac)
is compiled at build time (except on Mac) thus
you will have to build mozilla in order to test this out. If you
have already built mozilla then the compiler will be located at <tt>mozilla\dist\WIN32_D.OBJ\bin\xpidl.exe</tt>.
<p>Once you have the XPIDL compiler enter the following command at your
prompt:
<br><tt>D:\whereever\xpcom\test\test_component>d:\mozilla\dist\WIN32_D.OBJ\bin\xpidl -I
d:\mozilla\dist\idl -m typelib py_test_component.idl</tt>. You must then copy the generated .xpt file
to the mozilla component directory.
<p>The <tt>-I d:\mozilla\dist\idl</tt> points the compiler to the folder
containing the other idl files, needed because nsISample.idl inherits from
nsISupports.idl. The <tt>-m typelib</tt> instruction tells the compiler
to build the .XPT typelib file.</tt>.
<p>
For more information on compilation see the <a href="http://www.mozilla.org/scriptable/xpidl/">xpidl
compiler page</a>.
<p><b>Running the sample</b>
<p><b>NOTE: This doesnt work for me - I get an access denied error using XPConnect!</b>
<p>Using Mozilla, load this file. Pay attention
to the console when clicking "write".
<!-- XXX keep in sync with stuff in pre tag below -->
<script>
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
var sample = Components.classes["Python.TestComponent"].createInstance();
sample = sample.QueryInterface(Components.interfaces.nsIPythonTestInterface);
dump("sample = " + sample + "\n");
function get()
{
var field = document.getElementById('Value');
field.value = sample.str_value;
}
function set()
{
var field = document.getElementById('Value');
sample.str_value = field.value;
}
function poke()
{
var field = document.getElementById('Value');
sample.poke(field.value);
}
function write()
{
sample.writeValue("here is what I'm writing: ");
}
</script>
<p>
<form name="form">
<input type="button" value="Get" onclick="get();">
<input type="button" value="Set" onclick="set();">
<input type="button" value="Poke" onclick="poke();">
<input type="text" id="Value">
<input type="button" value="Write" onclick="write();">
<form>
<hr>
<p>
JavaScript and form source:
<!-- XXX keep in sync with actual script -->
<pre>
<script>
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
var sample = Components.classes["component://Python.TestComponent"].createInstance();
sample = sample.QueryInterface(Components.interfaces.nsIPythonTestInterface);
dump("sample = " + sample + "\n");
function get()
{
var field = document.getElementById('Value');
field.value = sample.str_value;
}
function set()
{
var field = document.getElementById('Value');
sample.str_value = field.value;
}
function poke()
{
var field = document.getElementById('Value');
sample.poke(field.value);
}
function write()
{
sample.writeValue("here is what I'm writing: ");
}
</script>
<form name="form">
<input type="button" value="Get" onclick="get();">
<input type="button" value="Set" onclick="set();">
<input type="button" value="Poke" onclick="poke();">
<input type="text" id="Value">
<input type="button" value="Write" onclick="write();">
<form>
</pre>
|