File: py_test_component.html

package info (click to toggle)
mozilla-firefox 1.0.4-2sarge17
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 255,356 kB
  • ctags: 267,207
  • sloc: cpp: 1,623,961; ansic: 792,828; xml: 85,380; makefile: 41,934; perl: 27,802; asm: 14,884; sh: 14,807; cs: 4,507; python: 4,398; java: 4,004; yacc: 1,380; lex: 409; pascal: 354; php: 244; csh: 132; objc: 73; ada: 44; sql: 4
file content (149 lines) | stat: -rw-r--r-- 4,593 bytes parent folder | download
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>
&lt;script&gt;
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: ");
}
&lt;/script&gt;

&lt;form name=&quot;form&quot;&gt;
&lt;input type=&quot;button&quot; value=&quot;Get&quot; onclick=&quot;get();&quot;&gt;
&lt;input type=&quot;button&quot; value=&quot;Set&quot; onclick=&quot;set();&quot;&gt;
&lt;input type=&quot;button&quot; value=&quot;Poke&quot; onclick=&quot;poke();&quot;&gt;
&lt;input type=&quot;text&quot; id=&quot;Value&quot;&gt;
&lt;input type=&quot;button&quot; value=&quot;Write&quot; onclick=&quot;write();&quot;&gt;
&lt;form>

</pre>