<?php if (!defined('PmWiki')) exit();
/*  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.

*/
$RecipeInfo['GuiButtons']['Version'] = '2007-04-23';

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

SDV($GuiButtonsDirUrlFmt,"{$FarmPubDirUrl}/guibuttons");
SDV($SmileysFolder, "{$FarmPubDirUrl}/guibuttons"); 

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


$GUIButtonSpace1 = '$GuiButtonsDirUrlFmt/space1.gif';
$GUIButtonSpace2 = '$GuiButtonsDirUrlFmt/space2.gif';
$GUIButtonSpace3 = '$GuiButtonsDirUrlFmt/space3.gif';
$GUIButtonSpace4 = '$GuiButtonsDirUrlFmt/space4.gif';
$ArrayCount = 0;
$SpaceCount = 0;

SDVA($c_GUIButtons, array(
  'italic'       => array($ArrayCount++, "''", "''", '$[Emphasized]',
                  '$GuiButtonsDirUrlFmt/italic.gif"$[Emphasized (italic)]"', 
                  '$[ak_em]'),
  'bold'   => array($ArrayCount++, "'''", "'''", '$[Strong]',
                  '$GuiButtonsDirUrlFmt/bold.gif"$[Strong (bold)]"',
                  '$[ak_strong]'),
   'underline' => array($ArrayCount++, '{+', '+}', '$[Underlined Text]',
                   '$GuiButtonsDirUrlFmt/underline.gif"$[Underline]"'),

//  'extlink'  => array($ArrayCount++, '[[', ']]', 'http:// | $[link text]',
//                  '$GuiButtonsDirUrlFmt/extlink.gif"$[Link to external page]"'),
  'big'      => array($ArrayCount++, "'+", "+'", '$[Big text]',
                  '$GuiButtonsDirUrlFmt/big.gif"$[Big text]"'),
  'small'    => array($ArrayCount++, "'-", "-'", '$[Small text]',
                  '$GuiButtonsDirUrlFmt/small.gif"$[Small text]"'),
  'sup'      => array($ArrayCount++, "'^", "^'", '$[Superscript]',
                  '$GuiButtonsDirUrlFmt/sup.gif"$[Superscript]"'),
  'sub'      => array($ArrayCount++, "'_", "_'", '$[Subscript]',
                  '$GuiButtonsDirUrlFmt/sub.gif"$[Subscript]"'),
  'h4'       => array($ArrayCount++, '\\n!!!! ', '\\n', '$[Heading]',
                  '$GuiButtonsDirUrlFmt/h.gif"$[Heading]"'),
  'center'   => array($ArrayCount++, '%25center%25', '', '',
                  '$GuiButtonsDirUrlFmt/center.gif"$[Center]"'),
  'pagelink' => array($ArrayCount++, '[[', ']]', '$[Page link]', 
                  '$GuiButtonsDirUrlFmt/pagelink.gif"$[Link to internal page]"'),
   'code'    => array($ArrayCount++, "[@", "@]", '$[Code]',
                  '$GuiButtonsDirUrlFmt/code.gif"$[Code]"'),
   'textblue'   => array($ArrayCount++, '%25blue%25', '%25%25', '$[Blue Text]',
                     '$GuiButtonsDirUrlFmt/hightextblue.gif"$[Blue Text]"'),
   'textgreen'  => array($ArrayCount++, '%25green%25', '%25%25', '$[Green Text]',
                     '$GuiButtonsDirUrlFmt/hightextgreen.gif"$[Green Text]"'),
   'textred'     => array($ArrayCount++, '%25red%25', '%25%25', '$[Red Text]',
                      '$GuiButtonsDirUrlFmt/hightextred.gif"$[Red Text]"'),
//   'textyellow'  => array($ArrayCount++, '%25yellow%25', '%25%25', '$[Yellow Text]',
//                      '$GuiButtonsDirUrlFmt/hightextyellow.gif"$[Yellow Text]"'),
                  
                  ));

# smiley gui-edit buttons
$c_GUIButtons [$SpaceCount++] = array($ArrayCount++, '', '', '',
     '$GuiButtonsDirUrlFmt/spacer.gif'); 
$c_GUIButtons['happy'] = array($ArrayCount++, '\{happy\}', '', '',
                  '$GuiButtonsDirUrlFmt/happy.gif"$[Happy]"');
$c_GUIButtons [$SpaceCount++] = array($ArrayCount++, '', '', '',
     '$GuiButtonsDirUrlFmt/pixspace.gif');                  
$c_GUIButtons['wink'] = array($ArrayCount++, '\{wink\}', '', '',
                  '$GuiButtonsDirUrlFmt/wink.gif"$[Wink]"'); 
$c_GUIButtons [$SpaceCount++] = array($ArrayCount++, '', '', '',
     '$GuiButtonsDirUrlFmt/pixspace.gif');                   
$c_GUIButtons['cool'] = array($ArrayCount++, '\{cool\}', '', '',
                  '$GuiButtonsDirUrlFmt/cool.gif"$[Cool]"'); 
$c_GUIButtons [$SpaceCount++] = array($ArrayCount++, '', '', '',
     '$GuiButtonsDirUrlFmt/pixspace.gif');                   
$c_GUIButtons['confused'] = array($ArrayCount++, '\{confused\}', '', '',
                  '$GuiButtonsDirUrlFmt/confused.gif"$[Confused]"'); 
$c_GUIButtons [$SpaceCount++] = array($ArrayCount++, '', '', '',
     '$GuiButtonsDirUrlFmt/pixspace.gif');                   
$c_GUIButtons['eek'] = array($ArrayCount++, '\{eek\}', '', '',
                  '$GuiButtonsDirUrlFmt/eek.gif"$[Eek]"');
$c_GUIButtons [$SpaceCount++] = array($ArrayCount++, '', '', '',
     '$GuiButtonsDirUrlFmt/pixspace.gif');                   
$c_GUIButtons['rolleyes'] = array($ArrayCount++, '\{rolleyes\}', '', '',
                  '$GuiButtonsDirUrlFmt/rolleyes.gif"$[Roll eyes]"'); 
$c_GUIButtons [$SpaceCount++] = array($ArrayCount++, '', '', '',
     '$GuiButtonsDirUrlFmt/pixspace.gif');                   
$c_GUIButtons['sad'] = array($ArrayCount++, '\{sad\}', '', '',
                  '$GuiButtonsDirUrlFmt/sad.gif"$[Sad]"'); 
$c_GUIButtons [$SpaceCount++] = array($ArrayCount++, '', '', '',
     '$GuiButtonsDirUrlFmt/pixspace.gif');                   
$c_GUIButtons['mad'] = array($ArrayCount++, '\{mad\}', '', '',
                  '$GuiButtonsDirUrlFmt/mad.gif"$[Mad]"'); 
$c_GUIButtons [$SpaceCount++] = array($ArrayCount++, '', '', '',
     '$GuiButtonsDirUrlFmt/pixspace.gif');                   
$c_GUIButtons['biggrin'] = array($ArrayCount++, '\{biggrin\}', '', '',
                  '$GuiButtonsDirUrlFmt/biggrin.gif"$[Big grin]"'); 


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

function GUIButtonsMarkup($pagename) {
  global $c_GUIButtons;
  $cmpfn = create_function('$a,$b', 'return $a[0]-$b[0];');
  usort($c_GUIButtons, $cmpfn);
  $out = "<script language='javascript' type='text/javascript'>\n";
  foreach ($c_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;
}