<?php if (!defined('PmWiki')) exit();
/*  
    ** GuiEdit GNU: **
    guiedit, copyright ?? pmwiki ??
    found on http://www.pmwiki.org/wiki/Cookbook/GuiEdit

    ** GuiButtons GNU: **
    guibuttons.php, copyright 2007 Hans Bracker, 
    this script is an adaptation of guiedit.php,
    Copyright 2004-2005 Patrick R. Michaud (pmichaud@pobox.com)
    This file is part of PmWiki; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published
    by the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.  See pmwiki.php for full details.

    This script adds a graphical button bar to a comment text form.
    Place (:guibuttons:) above the textarea box.
    Put inside the textarea form id=text like (:input textarea id=text ...
    This will provide a target for the guiedit javascript.
    Copy smiley gif images to GUIButton directory.

    ** Modifications & Merging of GuiEdit & GuiButtons **
    by Adam Overton, 2008-06-29
    note that this is my personal setup, so several buttons have been left out,
    of which there are many more to be found on the GuiEdit recipe page...

    a new variable $Smileys, has been added - set to 'true' or '1' if you'd 
    like the Smileys to show up... Smileys are turned off by default...

    ** Changes by SteP 2010-05-03 AIO
    * Test with pmwiki 2.2.15
    * Add $RecipeInfo
    * Fix use of % in color,bgcolor,left/right/center,image,table
    * Fix image markup issue
    * Fixes and a couple of string changes to enable effective translations ( XL )
    * Replace H1 button with preformatted text button, in line with the thinking that H1 is reserved for page title. Uncomment/comment 2/2 lines to get H1 back.
*/

/////////////////////////////////////////////////////////////////
//    SECTION TAKEN FROM GuiEdit.php RECIPE             //
//    http://www.pmwiki.org/wiki/Cookbook/GuiEdit        //
//    modified 2008-06-29 by Adam Overton                   //
//    modified 2010-05-03 by SteP //
/////////////////////////////////////////////////////////////////
$RecipeInfo['GuiEdit']['Version'] = '2010-05-03 AIO';

##  The following lines make additional editing buttons appear in the
##  edit page for subheadings, lists, tables, etc.

###### GUI Buttons ######
      $GUIButtonDirUrlFmt = $PubDirUrl.'/MyGuiEdit';
      $GUIButtonSpace1 = '$GUIButtonDirUrlFmt/space1.gif';
      $GUIButtonSpace2 = '$GUIButtonDirUrlFmt/space2.gif';
      $GUIButtonSpace3 = '$GUIButtonDirUrlFmt/space3.gif';
      $GUIButtonSpace4 = '$GUIButtonDirUrlFmt/space4.gif';
      $ArrayCount = 0;
      $SpaceCount = 0;

	$GUIButtons['strong']        = array($ArrayCount++, "'''", "'''", '$[Bold Text]',
							     '$GUIButtonDirUrlFmt/bold.gif"$[Bold]"');
	$GUIButtons['em']      = array($ArrayCount++, "''", "''", '$[Italic Text]',
								'$GUIButtonDirUrlFmt/italic.gif"$[Italic]"');
	$GUIButtons['underline']   = array($ArrayCount++, '{+', '+}', '$[Underlined Text]',
								'$GUIButtonDirUrlFmt/underline.gif"$[Underline]"');
	$GUIButtons['strikout']    = array($ArrayCount++, '{-', '-}', '$[Crossed Out Text]',
								'$GUIButtonDirUrlFmt/strikethrough.gif"$[Strike Thru]"');
	$GUIButtons ['sup']         = array($ArrayCount++, "'^", "^'", '$[Superscript]',
								'$GUIButtonDirUrlFmt/sup.gif"$[Superscript]"');
	$GUIButtons ['sub']         = array($ArrayCount++, "'_", "_'", '$[Subscript]',
								'$GUIButtonDirUrlFmt/sub.gif"$[Subscript]"');
	
	$GUIButtons [$SpaceCount++] = array($ArrayCount++, '', '', '', $GUIButtonSpace1);
	$GUIButtons ['left']        = array($ArrayCount++, '%left%', '', '',
								'$GUIButtonDirUrlFmt/left.gif"$[Align Left]"');
	$GUIButtons ['center']      = array($ArrayCount++, '%center%', '', '',
								'$GUIButtonDirUrlFmt/center.gif"$[Align Center]"');
	$GUIButtons ['right']       = array($ArrayCount++, '%right%', '', '',
								'$GUIButtonDirUrlFmt/right.gif"$[Align Right]"');
	$GUIButtons ['indent']      = array($ArrayCount++, '\\n->', '\\n', '$[Indented text]',
								'$GUIButtonDirUrlFmt/indent.gif"$[Indented text]"');
	$GUIButtons ['outdent']     = array($ArrayCount++, '\\n-<', '\\n', '$[Hanging indent]',
								'$GUIButtonDirUrlFmt/outdent.gif"$[Hanging indent]"');
	
	$GUIButtons [$SpaceCount++] = array($ArrayCount++, '', '', '', $GUIButtonSpace1);
	$GUIButtons ['big']         = array($ArrayCount++, "[+", "+]", '$[Big Text]',
								'$GUIButtonDirUrlFmt/big.gif"$[Big Text]"');
	$GUIButtons ['bigger']      = array($ArrayCount++, '[++', '++]', '$[Bigger Text]',
								'$GUIButtonDirUrlFmt/bigger.gif"$[Larger Text]"');
	$GUIButtons ['small']       = array($ArrayCount++, "[-", "-]", '$[small text]',
								'$GUIButtonDirUrlFmt/small.gif"$[small text]"');
	$GUIButtons ['smaller']     = array($ArrayCount++, '[--', '--]', '$[smaller text]',
								'$GUIButtonDirUrlFmt/smaller.gif"$[tiny text]"');
	
	$GUIButtons [$SpaceCount++] = array($ArrayCount++, '', '', '', $GUIButtonSpace1);
#	$GUIButtons ['h1']          = array($ArrayCount++, '\\n! ', '\\n', '$[Heading1]',
#								'$GUIButtonDirUrlFmt/h1.gif"$[Heading1]"');
	$GUIButtons ['code']        = array($ArrayCount++, '[@', '@]', '$[Text or Code]',
								'$GUIButtonDirUrlFmt/code.gif"$[Preformatted text]"');
	$GUIButtons ['h2']          = array($ArrayCount++, '\\n!! ', '\\n', '$[Heading2]',
								'$GUIButtonDirUrlFmt/h2.gif"$[Heading2]"');
	$GUIButtons ['h3']          = array($ArrayCount++, '\\n!!! ', '\\n', '$[Heading3]',
								'$GUIButtonDirUrlFmt/h3.gif"$[Heading3]"');
	$GUIButtons ['h4']          = array($ArrayCount++, '\\n!!!! ', '\\n', '$[Heading4]',
								'$GUIButtonDirUrlFmt/h4.gif"$[Heading4]"');
	
	$GUIButtons [$SpaceCount++] = array($ArrayCount++, '', '', '', $GUIButtonSpace1);
	$GUIButtons ['table']       = array($ArrayCount++, '||border=1 width=80%\\n||!Hdr ||!Hdr ||!Hdr ||\\n||     ||     ||     ||\\n||     ||     ||     ||\\n', '', '',
								 '$GUIButtonDirUrlFmt/table.gif"$[Table]"');
	
	#Must be changed to one line!
	$GUIButtons ['advtable']    = array($ArrayCount++, "(:table border=1 width=80%:)\\n(:cellnr:)'''Header'''\\n(:cell:)'''Header'''\\n(:cell:)'''Header'''\\n(:cellnr:)\\n(:cell:)\\n(:cell:)\\n(:tableend:)\\n",'', '','$GUIButtonDirUrlFmt/tableadv.gif"$[Advanced table]"');
	#Must be changed to one line!
	$GUIButtons ['sig']         = array($ArrayCount++, '\\n~~~~', '', '',
								'$GUIButtonDirUrlFmt/sig.gif"$[Stamp your name and current date]"');

	#Must be changed to one line - the $ must be escaped !
	
	$GUIButtons [$SpaceCount++] = array($ArrayCount++, '\\n->', '\\n', '$[2nd Line]', '<br \>');
	$GUIButtons ['pagelink']    = array($ArrayCount++, '[[',']]', '$[Page link]',
								'$GUIButtonDirUrlFmt/pagelink.gif"$[Link to internal page]"');
	$GUIButtons ['extlink']     = array($ArrayCount++, '[[',']]', '$[http:// | link text]',
								'$GUIButtonDirUrlFmt/extlink.gif"$[Link to external page]"');
	$GUIButtons ['anchor']      = array($ArrayCount++, '[[#', ']]', '$[AnchorName]',
								'$GUIButtonDirUrlFmt/anchor.gif"$[Invisible Anchor to Link To]"');
	$GUIButtons ['anchorlink']  = array($ArrayCount++, '[[#', '$[| text]]]', '$[AnchorName]',
								'$GUIButtonDirUrlFmt/anchorlink.gif"$[Link to Anchor in SAME page]"');
	$GUIButtons ['anchor2']     = array($ArrayCount++, '[[', '$[#AnchorName | text]]]', '$[PageName]',
								'$GUIButtonDirUrlFmt/anchor2.gif"$[Link to Anchor on DIFFERENT page]"');
	
	$GUIButtons [$SpaceCount++] = array($ArrayCount++, '', '', '', $GUIButtonSpace3);
	$GUIButtons ['attach']      = array($ArrayCount++, 'Attach:','', '$[file.ext]',
								'$GUIButtonDirUrlFmt/attach.gif"$[Attach File]"');
	$GUIButtons ['image']       = array($ArrayCount++, 
								'%rframe text-align=left height=50px margin-top=7px margin-left=10px% [[Attach:',
								" | Attach:$[SameImageName.jpg]&quot;$[MOUSE HOVER TEXT]&quot;]] | '$[-SUBTITLE-]'", '$[SameImageName.jpg]',
								'$GUIButtonDirUrlFmt/image.gif"$[Thumbnail of Image floating right]"');
	$GUIButtons ['ol']          = array($ArrayCount++, '\\n# ', '\\n', '$[Numbered list element]',
								'$GUIButtonDirUrlFmt/ol.gif"$[Ordered list]"');
	$GUIButtons ['ul']          = array($ArrayCount++, '\\n* ', '\\n', '$[Bullet list element]',
								'$GUIButtonDirUrlFmt/ul.gif"$[Unordered list]"');
	$GUIButtons ['hr']          = array($ArrayCount++, '\\n----\\n', '', '',
								'$GUIButtonDirUrlFmt/hr.gif"$[Horizontal rule]"');
	
	$GUIButtons [$SpaceCount++] = array($ArrayCount++, '', '', '', $GUIButtonSpace1);
	$GUIButtons ['textblue']    = array($ArrayCount++, '%blue%', '%%', '$[Blue Text]',
								'$GUIButtonDirUrlFmt/hightextblue.gif"$[Blue Text]"');
	$GUIButtons ['textgreen']   = array($ArrayCount++, '%green%', '%%', '$[Green Text]',
								'$GUIButtonDirUrlFmt/hightextgreen.gif"$[Green Text]"');
	$GUIButtons ['textred']     = array($ArrayCount++, '%red%', '%%', '$[Red Text]',
								'$GUIButtonDirUrlFmt/hightextred.gif"$[Red Text]"');
	$GUIButtons ['textyellow']  = array($ArrayCount++, '%yellow%', '%%', '$[Yellow Text]',
								'$GUIButtonDirUrlFmt/hightextyellow.gif"$[Yellow Text]"');
	
	$GUIButtons [$SpaceCount++] = array($ArrayCount++, '', '', '', $GUIButtonSpace1);
	$GUIButtons ['bgblue']      = array($ArrayCount++, '%bgcolor=blue%', '%%', '$[Background Blue]',
								'$GUIButtonDirUrlFmt/highbgblue.gif"$[Blue Background]"');
	$GUIButtons ['bggreen']     = array($ArrayCount++, '%bgcolor=green%', '%%', '$[Background Green]',
								'$GUIButtonDirUrlFmt/highbggreen.gif"$[Green Background]"');
	$GUIButtons ['bgred']       = array($ArrayCount++, '%bgcolor=red%', '%%', '$[Background Red]',
								'$GUIButtonDirUrlFmt/highbgred.gif"$[Red Background]"');
	$GUIButtons ['bgyellow']    = array($ArrayCount++, '%bgcolor=yellow%', '%%', '$[Background Yellow]',
								'$GUIButtonDirUrlFmt/highbgyellow.gif"$[Yellow Background]"');
	
	$GUIButtons [$SpaceCount++] = array($ArrayCount++, '', '', '',$GUIButtonSpace1);
	$GUIButtons ['nextline']    = array($ArrayCount++, '\\\\\\\\\\n', '', '',
								'$GUIButtonDirUrlFmt/nextline.gif"$[Line Break]"');
	$GUIButtons ['nextline2']   = array($ArrayCount++, '\\\\\\\\\\\\\\n', '', '',
								'$GUIButtonDirUrlFmt/nextline2.gif"$[2 Line Breaks]"');
	$GUIButtons ['return']      = array($ArrayCount++, '[[<<]] \\n', '', '',
								'$GUIButtonDirUrlFmt/return.gif"$[ Return]"');
	

/////////////////////////////////////////////////////////////////
//    SECTION TAKEN FROM GuiButtons.php RECIPE       //
//    http://www.pmwiki.org/wiki/Cookbook/GuiButtons  //
//    modified 2008-06-29 by Adam Overton                  //
/////////////////////////////////////////////////////////////////

$HTMLHeaderFmt[] = "<script language='javascript' type='text/javascript'
  src='\$FarmPubDirUrl/guiedit/guiedit.js'></script>\n";

SDV($SmileyButtonsDirUrlFmt,"{$GUIButtonDirUrlFmt}/smileys");

# display smilie images 
Markup("happy2","block","/\{happy\}/","<img src='$SmileyButtonsDirUrlFmt/happy.gif' border='0' width='15' height='15' alt='happy smiley' title='happy smiley' class='smiley' />");
Markup("wink2","block","/\{wink\}/","<img src='$SmileyButtonsDirUrlFmt/wink.gif' border='0' width='15' height='15' alt='winking smiley' title='winking smiley' class='smiley' />");
Markup("cool","block","/\{cool\}/","<img src='$SmileyButtonsDirUrlFmt/cool.gif' border='0' width='15' height='15' alt='cool smiley' title='cool smiley' class='smiley' />");
Markup("confused","block","/\{confused\}/","<img src='$SmileyButtonsDirUrlFmt/confused.gif' border='0' width='15' height='22' alt='confused smiley' title='confused smiley' class='smiley' />");
Markup("eek","block","/\{eek\}/","<img src='$SmileyButtonsDirUrlFmt/eek.gif' border='0' width='15' height='15' alt='shrieking smiley' title='shrieking smiley' class='smiley' />");
Markup("mad","block","/\{angry\}/","<img src='$SmileyButtonsDirUrlFmt/mad.gif' border='0' width='15' height='15' alt='angry smiley' title='angry smiley' class='smiley' />");
Markup("rolleyes","block","/\{rolleyes\}/","<img src='$SmileyButtonsDirUrlFmt/rolleyes.gif' border='0' width='15' height='15' alt='rolling eyes smiley' title='rolling eyes smiley' class='smiley' />");
Markup("sad2","block","/\{sad\}/","<img src='$SmileyButtonsDirUrlFmt/sad.gif' border='0' width='15' height='15' alt='frowning smiley' title='frowning smiley' class='smiley' />");
Markup("tongue2","block","/\{tongue\}/","<img src='$SmileyButtonsDirUrlFmt/tongue.gif' border='0' width='15' height='15' alt='laughing smiley' title='laughing smiley' class='smiley' />");
Markup("redface2","block","/\{redface\}/","<img src='$SmileyButtonsDirUrlFmt/redface.gif' border='0' width='15' height='15' alt='embarrassed smiley' title='embarrassed smiley' class='smiley' />");
Markup("biggrin2","block","/\{biggrin\}/","<img src='$SmileyButtonsDirUrlFmt/biggrin.gif' border='0' width='15' height='15' alt='grinning smiley' title='grinning smiley' class='smiley' />");
Markup("mad2","block","/\{mad\}/","<img src='$SmileyButtonsDirUrlFmt/mad.gif' border='0' width='15' height='15' alt='angry smiley' title='angry smiley' class='smiley' />");


// *** AO -- THIS ADDS SMILEYS TO THE $GuiButtons ARRAY *** //
if($Smileys) {
	# smiley gui-edit buttons
	$GUIButtons [$SpaceCount++] = array($ArrayCount++, '', '', '', $GUIButtonSpace1);
	$GUIButtons['happy'] = array($ArrayCount++, '\{happy\}', '', '',
				'$SmileyButtonsDirUrlFmt/happy.gif"$[Happy]"');
	$GUIButtons [$SpaceCount++] = array($ArrayCount++, '', '', '', "$SmileyButtonsDirUrlFmt/pixspace.gif");

	$GUIButtons['wink'] = array($ArrayCount++, '\{wink\}', '', '',
				'$SmileyButtonsDirUrlFmt/wink.gif"$[Wink]"'); 
	$GUIButtons [$SpaceCount++] = array($ArrayCount++, '', '', '', "$SmileyButtonsDirUrlFmt/pixspace.gif");

	$GUIButtons['cool'] = array($ArrayCount++, '\{cool\}', '', '',
				'$SmileyButtonsDirUrlFmt/cool.gif"$[Cool]"'); 
	$GUIButtons [$SpaceCount++] = array($ArrayCount++, '', '', '', "$SmileyButtonsDirUrlFmt/pixspace.gif");

	$GUIButtons['confused'] = array($ArrayCount++, '\{confused\}', '', '',
				'$SmileyButtonsDirUrlFmt/confused.gif"$[Confused]"'); 
	$GUIButtons [$SpaceCount++] = array($ArrayCount++, '', '', '', "$SmileyButtonsDirUrlFmt/pixspace.gif");

	$GUIButtons['eek'] = array($ArrayCount++, '\{eek\}', '', '',
				'$SmileyButtonsDirUrlFmt/eek.gif"$[Eek]"');
	$GUIButtons [$SpaceCount++] = array($ArrayCount++, '', '', '', "$SmileyButtonsDirUrlFmt/pixspace.gif");

	$GUIButtons['rolleyes'] = array($ArrayCount++, '\{rolleyes\}', '', '',
				'$SmileyButtonsDirUrlFmt/rolleyes.gif"$[Roll eyes]"'); 
	$GUIButtons [$SpaceCount++] = array($ArrayCount++, '', '', '', "$SmileyButtonsDirUrlFmt/pixspace.gif");

	$GUIButtons['sad'] = array($ArrayCount++, '\{sad\}', '', '',
				'$SmileyButtonsDirUrlFmt/sad.gif"$[Sad]"'); 
	$GUIButtons [$SpaceCount++] = array($ArrayCount++, '', '', '', "$SmileyButtonsDirUrlFmt/pixspace.gif");

	$GUIButtons['mad'] = array($ArrayCount++, '\{mad\}', '', '',
				'$SmileyButtonsDirUrlFmt/mad.gif"$[Mad]"'); 
	$GUIButtons [$SpaceCount++] = array($ArrayCount++, '', '', '', "$SmileyButtonsDirUrlFmt/pixspace.gif");

	$GUIButtons['biggrin'] = array($ArrayCount++, '\{biggrin\}', '', '',
				'$SmileyButtonsDirUrlFmt/biggrin.gif"$[Big grin]"'); 
}	


Markup('guibuttons', 'directives',
  '/\\(:guibuttons:\\)/e',
  "Keep(FmtPageName(GUIButtonsMarkup(\$pagename), \$pagename))");

function GUIButtonsMarkup($pagename) {
  global $GUIButtons, $ArrayCount, $SpaceCount;  // *** AO *** //

  $cmpfn = create_function('$a,$b', 'return $a[0]-$b[0];');
  usort($GUIButtons, $cmpfn);
  $out = "<script language='javascript' type='text/javascript'>\n";
  foreach ($GUIButtons as $k => $g) {
    if (!$g) continue;
    @list($when, $mopen, $mclose, $mtext, $tag, $mkey) = $g;
    if ($tag{0} == '<') { 
        $out .= "document.write(\"$tag\");\n";
        continue; 
    }
    if (preg_match('/^(.*\\.(gif|jpg|png))("([^"]+)")?$/', $tag, $m)) {
      $title = (@$m[4] > '') ? "title='{$m[4]}'" : '';
      $tag = "<img src='{$m[1]}' $title style='border:0px;' />";
    }
    $mopen = str_replace(array('\\', "'"), array('\\\\', "\\\\'"), $mopen);
    $mclose = str_replace(array('\\', "'"), array('\\\\', "\\\\'"), $mclose);
    $mtext = str_replace(array('\\', "'"), array('\\\\', "\\\\'"), $mtext);
    $out .= 
      "insButton(\"$mopen\", \"$mclose\", '$mtext', \"$tag\", \"$mkey\");\n";
  }
  $out .= '</script>';
  return $out;
}