<?php if (!defined('PmWiki')) exit();
/*
  processing - Embed Processing java applets into PmWiki

  Copyright (C) 2011 Antony Templier

  This software is under WTFPL License:
  ------------------------------------------------------------------------------
              DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
                      Version 2, December 2004

   Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>

   Everyone is permitted to copy and distribute verbatim or modified
   copies of this license document, and changing it is allowed as long
   as the name is changed.

              DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
     TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

    0. You just DO WHAT THE FUCK YOU WANT TO.
  ------------------------------------------------------------------------------

    TOPIC:
    I used to display my processing applets (http://www.processing.org) with the
    applet Cookbook (http://www.pmwiki.org/wiki/Cookbook/Applet) but thing goes
    wrong on recent macos X since new java version 1.6.

    This cookbook is processing specifique, not java generic but more simple than
    applet.php.

    USAGE:
    Processing applet files are into pub/applets subdirectory.

    On a PmWiki page:
    (:processing codebase="SampleProcessing" code="SampleProcessing"
    archive="SampleProcessing.jar,core.jar" width="600" height="600":)

    For:
    codebase : the name of subdirectory where are located applet files.
    code : Entry point class.
    archive : The applet jar archive name.
    width & height : dimensions in pixel of the applet

    CREDITS:
    It is originally based on how processing 1.51  make his own index.html
    and on applet.php - see below :


 * applet - Embed java applets into PmWiki 2.0 pages
 * Copyright 2005-2007 by D.Faure (dfaure@cpan.org)
 * Thanks to Shayne Steele (steele <AT> cs <DOT> fsu <DOT> edu) for the
 * article at: http://ww2.cs.fsu.edu/~steele/XHTML/appletObject.html
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * See http://www.pmwiki.org/wiki/Cookbook/Applet for info.
 */

$RecipeInfo['Processing']['Version'] = '20150815';

if(function_exists('Markup_e')) {
  Markup_e('processing', 'directives',
      "/\\(:processing(\\s+.*)\\s*:\\)/", "ProcessingMarkup(\$m[1])");
} else {
  Markup('processing', 'directives',
      "/\\(:processing(\\s+.*)\\s*:\\)/e", "ProcessingMarkup(PSS('$1'))");
}

function ProcessingMarkup($args) {
    return PApplet(ParseArgs($args, '(?>([\\w\\.]+)[:=])'));
}

function PApplet($opt) {
    global $FarmPubDirUrl, $ProcessingRootCodebase;

    SDV($ProcessingRootCodebase, "$FarmPubDirUrl/applets");

    $opt = array_merge((array)$AppletParams, (array)$opt);
    unset($opt['#'], $opt['-'], $opt['+'], $opt['']);

    $code    = @$opt['code'];    unset($opt['code']);
    $archive = @$opt['archive']; unset($opt['archive']);
    $width   = @$opt['width'];   unset($opt['width']);
    $height  = @$opt['height'];  unset($opt['height']);

    $codebase = @preg_replace('!^/?!', "$ProcessingRootCodebase/", $opt['codebase']);
    unset($opt['codebase']);

    $out = '
    <!-- This version plays nicer with older browsers,
       but requires JavaScript to be enabled.
       http://java.sun.com/javase/6/docs/technotes/guides/jweb/deployment_advice.html
       http://download.oracle.com/javase/tutorial/deployment/deploymentInDepth/ -->
    <script type="text/javascript"
    src="http://www.java.com/js/deployJava.js"></script>
    <script type="text/javascript">
    /* <![CDATA[ */

    var attributes = {
            codebase: \''.$codebase.'\',
            code: \''.$code.'.class\',
            archive: \''.$archive.'\',
            width: '.$width.',
            height: '.$height.',
          };
          var parameters = {
            image: \'loading.gif\',
            centerimage: \'true\',
          };
          var version = \'1.5\';
          deployJava.runApplet(attributes, parameters, version);

          /* ]]> */
        </script>

    <noscript> <div>
    <!--[if !IE]> -->
    <object classid="java:'.$code.'.class"
                type="application/x-java-applet"
                codebase="'.$codebase.'"
                archive="'.$archive.'"
                width="'.$width.'" height="'.$height.'"
                standby="Loading Processing software..." >

      <param name="archive" value="'.$archive.'" />

      <param name="mayscript" value="true" />
      <param name="scriptable" value="true" />

      <param name="image" value="loading.gif" />
      <param name="boxmessage" value="Loading Processing software..." />
      <param name="boxbgcolor" value="#FFFFFF" />
    <!--<![endif]-->

      <!-- For more instructions on deployment,
     or to update the CAB file listed here, see:
     http://java.sun.com/javase/6/webnotes/family-clsid.html
     http://java.sun.com/javase/6/webnotes/install/jre/autodownload.html -->
      <object classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
        codebase="http://java.sun.com/update/1.6.0/jinstall-6u20-windows-i586.cab"
        width="'.$width.'" height="'.$height.'"
        standby="Loading Processing software..."  >


        <param name="code" value="'.$code.'" />
        <param name="codebase" value="'.$codebase.'" />
        <param name="archive" value="'.$archive.'" />

        <param name="mayscript" value="true" />
        <param name="scriptable" value="true" />

        <param name="image" value="loading.gif" />
        <param name="boxmessage" value="Loading Processing software..." />
        <param name="boxbgcolor" value="#FFFFFF" />

        <p>
    <strong>
      This browser does not have a Java Plug-in.
      <br />
      <a href="http://www.java.com/getjava" title="Download Java Plug-in">
        Get the latest Java Plug-in here.
      </a>
    </strong>
        </p>

      </object>

    <!--[if !IE]> -->
    </object>
    <!--<![endif]-->

    </div> </noscript>';

    // '<:block> évite de générer un <p></p> englobant autour du résultat
    return '<:block>'.Keep($out);
}

?>