<?php /*>*/ if (!defined('PmWiki')) exit();
/*
 * applet - Embed java applets into PmWiki 2.0 pages
 * Copyright 2005-2014 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['Applet']['Version'] = '2014-08-25';

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

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

SDVA($Applet, array(
'classid' => "clsid:8AD9C840-044E-11D1-B3E9-00805F499D93",
'codebase' => "http://java.sun.com/update/1.5.0/jinstall-1_5_0-windows-i586.cab",
'not_handled' => "ERROR: Applet not displayed. 
Visit the <a href='http://www.java.com/en/download/' title='Java Software Website'>Java Software Website</a>
to check if Java is installed on your computer or try enabling the <i>ActiveX</i> if you are Internet Explorer user.",
));

SDV($HTMLStylesFmt['applet'], "
.applet {
  background-color: #cccccc;
}
.applet .not_handled {
  margin: 1em;
}
");

function Applet($opt) {
  global $Applet, $UrlScheme, $FarmPubDirUrl, $PubDirUrl;
  unset($opt['#'], $opt['-'], $opt['+'], $opt['']);
  $code	   = @$opt['code'];	 unset($opt['code']);
  $archive	= @$opt['archive'];  unset($opt['archive']);
  $codebase   = @$opt['codebase']; unset($opt['codebase']);
  $width	  = @$opt['width'];	unset($opt['width']);
  $height	 = @$opt['height'];   unset($opt['height']);
  $name	   = @$opt['name'];	 unset($opt['name']);
  $id		 = @$opt['id'];	   unset($opt['id']);
  $title	  = @$opt['title'];	unset($opt['title']);
  $baseurl = IsEnabled($FarmPubDirUrl, $PubDirUrl);
  if($archive) {
	if(!preg_match("/^($UrlScheme|\/)/i", $archive))
	  $archive = "$baseurl/$archive";
	$objparms[] = "<param name=\"archive\" value=\"$archive\" />";
	$archive = "archive=\"$archive\"";
  }
  if($codebase) {
	if(!preg_match("/^($UrlScheme|\/)/i", $codebase))
	  $codebase = "$baseurl/$codebase";
	$objparms[] = "<param name=\"codebase\" value=\"$codebase\" />";
  }
  if($name) {
	$objparms[] = "<param name=\"name\" value=\"$name\" />";
	$name = "name=\"$name\"";
  }
  if($id) $id = "id=\"$id\"";
  if($title) $title = "title=\"$title\"";
  foreach($opt as $n => $v)
	$objparms[] = "<param name=\"$n\" value=\"$v\" />";
  $objparms = implode("\n", $objparms);
  $out = "
<div class=\"applet\" style=\"width:{$width}px; height:{$height}px;\">
<!--[if !IE]> Hidden to IE -->
  <object classid=\"java:{$code}\" {$name} {$id} {$title}
		  type=\"application/x-java-applet\"
		  {$archive}
		  width=\"{$width}\" height=\"{$height}\">
{$objparms}
<!--<![endif]-->
	<object classid=\"{$Applet['classid']}\" {$name} {$id} {$title}
			codebase=\"{$Applet['codebase']}\"
			width=\"{$width}\" height=\"{$height}\">
	  <param name=\"type\" value=\"application/x-java-applet\" />
	  <param name=\"code\" value=\"{$code}\" />
{$objparms}
	  <div class=\"not_handled\">
{$Applet['not_handled']}
	  </div>
	</object>
<!--[if !IE]> Hidden to IE -->
  </object>
<!--<![endif]-->
</div>
";
#  StopWatch(TraceVars(array(
#  'out' => $out,
#  )));
  return $out;
}