16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync<!-- ***** BEGIN LICENSE BLOCK *****
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync - Version: MPL 1.1/GPL 2.0/LGPL 2.1
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync -
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync - The contents of this file are subject to the Mozilla Public License Version
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync - 1.1 (the "License"); you may not use this file except in compliance with
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync - the License. You may obtain a copy of the License at
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync - http://www.mozilla.org/MPL/
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync -
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync - Software distributed under the License is distributed on an "AS IS" basis,
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync - WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync - for the specific language governing rights and limitations under the
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync - License.
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync -
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync - The Original Code is PyXPCOM.
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync -
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync - The Initial Developer of the Original Code is
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync - ActiveState Tool Corporation.
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync - Portions created by the Initial Developer are Copyright (C) 2000-2001
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync - the Initial Developer. All Rights Reserved.
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync -
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync - Contributor(s):
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync -
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync - Alternatively, the contents of this file may be used under the terms of
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync - either the GNU General Public License Version 2 or later (the "GPL"), or
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync - the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync - in which case the provisions of the GPL or the LGPL are applicable instead
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync - of those above. If you wish to allow use of your version of this file only
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync - under the terms of either the GPL or the LGPL, and not to allow others to
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync - use your version of this file under the terms of the MPL, indicate your
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync - decision by deleting the provisions above and replace them with the notice
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync - and other provisions required by the LGPL or the GPL. If you do not delete
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync - the provisions above, a recipient may use your version of this file under
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync - the terms of any one of the MPL, the GPL or the LGPL.
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync -
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync - ***** END LICENSE BLOCK ***** -->
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync<center><b><font size=+2>Python Component Sample</font></b>
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync<p>
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync<br>
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsyncLast modified
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync<script>
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsyncdocument.write(document.lastModified);
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync</script>
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync</center>
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync<p>XPConnect allows JavaScript
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsyncto transparantly access and manipulate XPCOM objects;
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync<p>Big Deal, I hear you say! But it also works for Python!!!
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync<p>
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsyncThis sample demonstrates accessing a XPCOM object through XPConnect.
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsyncThe JavaScript executed when this page loads creates an instance
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsyncof the Python object by
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsyncusing the <tt>Components</tt> object, then accesses it through
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsyncthe <a href="py_test_component.idl">nsISample</a> interface by calling <tt>QueryInterface</tt>:
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync<br>
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync<pre>
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsyncnetscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsyncvar sample = Components.classes["component://mozilla/sample/sample-world"].createInstance();
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsyncsample = sample.QueryInterface(Components.interfaces.nsISample);
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync</pre>
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync<p>
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsyncThe buttons on the form are connected to JavaScript event handlers which
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsynccall the methods defined in Python
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync<p><b><a name="Compiling">Compiling the idl</b>
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync<p>The XPIDL compiler (xpidl on Unix, xpidl.exe on Windows, and a CodeWarrior plugin on Mac)
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsyncis compiled at build time (except on Mac) thus
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsyncyou will have to build mozilla in order to test this out. If you
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsynchave already built mozilla then the compiler will be located at <tt>mozilla\dist\WIN32_D.OBJ\bin\xpidl.exe</tt>.
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync<p>Once you have the XPIDL compiler enter the following command at your
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsyncprompt:
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync<br><tt>D:\whereever\xpcom\test\test_component>d:\mozilla\dist\WIN32_D.OBJ\bin\xpidl -I
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsyncd:\mozilla\dist\idl -m typelib py_test_component.idl</tt>. You must then copy the generated .xpt file
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsyncto the mozilla component directory.
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync<p>The <tt>-I d:\mozilla\dist\idl</tt> points the compiler to the folder
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsynccontaining the other idl files, needed because nsISample.idl inherits from
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsyncnsISupports.idl. The <tt>-m typelib</tt> instruction tells the compiler
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsyncto build the .XPT typelib file.</tt>.
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync<p>
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsyncFor more information on compilation see the <a href="http://www.mozilla.org/scriptable/xpidl/">xpidl
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsynccompiler page</a>.
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync<p><b>Running the sample</b>
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync<p><b>NOTE: This doesnt work for me - I get an access denied error using XPConnect!</b>
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync<p>Using Mozilla, load this file. Pay attention
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsyncto the console when clicking "write".
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync<!-- XXX keep in sync with stuff in pre tag below -->
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync<script>
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsyncnetscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsyncvar sample = Components.classes["Python.TestComponent"].createInstance();
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsyncsample = sample.QueryInterface(Components.interfaces.nsIPythonTestInterface);
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsyncdump("sample = " + sample + "\n");
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsyncfunction get()
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync{
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync var field = document.getElementById('Value');
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync field.value = sample.str_value;
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync}
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsyncfunction set()
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync{
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync var field = document.getElementById('Value');
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync sample.str_value = field.value;
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync}
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsyncfunction poke()
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync{
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync var field = document.getElementById('Value');
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync sample.poke(field.value);
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync}
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsyncfunction write()
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync{
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync sample.writeValue("here is what I'm writing: ");
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync}
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync</script>
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync<p>
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync<form name="form">
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync<input type="button" value="Get" onclick="get();">
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync<input type="button" value="Set" onclick="set();">
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync<input type="button" value="Poke" onclick="poke();">
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync<input type="text" id="Value">
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync<input type="button" value="Write" onclick="write();">
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync<form>
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync<hr>
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync<p>
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsyncJavaScript and form source:
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync<!-- XXX keep in sync with actual script -->
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync<pre>
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync&lt;script&gt;
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsyncnetscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsyncvar sample = Components.classes["component://Python.TestComponent"].createInstance();
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsyncsample = sample.QueryInterface(Components.interfaces.nsIPythonTestInterface);
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsyncdump("sample = " + sample + "\n");
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsyncfunction get()
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync{
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync var field = document.getElementById('Value');
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync field.value = sample.str_value;
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync}
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsyncfunction set()
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync{
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync var field = document.getElementById('Value');
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync sample.str_value = field.value;
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync}
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsyncfunction poke()
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync{
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync var field = document.getElementById('Value');
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync sample.poke(field.value);
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync}
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsyncfunction write()
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync{
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync sample.writeValue("here is what I'm writing: ");
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync}
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync&lt;/script&gt;
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync&lt;form name=&quot;form&quot;&gt;
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync&lt;input type=&quot;button&quot; value=&quot;Get&quot; onclick=&quot;get();&quot;&gt;
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync&lt;input type=&quot;button&quot; value=&quot;Set&quot; onclick=&quot;set();&quot;&gt;
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync&lt;input type=&quot;button&quot; value=&quot;Poke&quot; onclick=&quot;poke();&quot;&gt;
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync&lt;input type=&quot;text&quot; id=&quot;Value&quot;&gt;
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync&lt;input type=&quot;button&quot; value=&quot;Write&quot; onclick=&quot;write();&quot;&gt;
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync&lt;form>
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync
16a8d09569a2ebd598cef72fa605be6fb4563607vboxsync</pre>