% \iffalse meta-comment
%
% Copyright (C) 2010 by Robert Marik <marik@mendelu.cz>
% -----------------------------------------------------
% 
% This file may be distributed and/or modified under the conditions of
% the LaTeX Project Public License, either version 1.2 of this license
% or (at your option) any later version.  The latest version of this
% license is in:
%
%    http://www.latex-project.org/lppl.txt
%
% and version 1.2 or later is part of all distributions of LaTeX
% version 1999/12/01 or later.
%
% \fi
%
% \iffalse
%<*driver>
\ProvidesFile{ocgtools.dtx}
%</driver>
%<package>\NeedsTeXFormat{LaTeX2e}[1999/12/01]
%<package>\ProvidesPackage{ocgtools}
%<*package>
    [2010/04/01 v0.5 ocgtools.dtx  file]
%</package>
%
%<*driver>
\documentclass{ltxdoc}
\usepackage{a4wide,url,color}
\EnableCrossrefs         
\CodelineIndex
\RecordChanges

\begin{document}
  \DocInput{ocgtools.dtx}
  \PrintChanges
  \PrintIndex
\end{document}
%</driver>
% \fi
%
% \CheckSum{821}
%
% \CharacterTable
%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%   Digits        \0\1\2\3\4\5\6\7\8\9
%   Exclamation   \!     Double quote  \"     Hash (number) \#
%   Dollar        \$     Percent       \%     Ampersand     \&
%   Acute accent  \'     Left paren    \(     Right paren   \)
%   Asterisk      \*     Plus          \+     Comma         \,
%   Minus         \-     Point         \.     Solidus       \/
%   Colon         \:     Semicolon     \;     Less than     \<
%   Equals        \=     Greater than  \>     Question mark \?
%   Commercial at \@     Left bracket  \[     Backslash     \\
%   Right bracket \]     Circumflex    \^     Underscore    \_
%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%   Right brace   \}     Tilde         \~}
%
%
% \changes{v0.5}{2010/04/01}{First public version}
%
% \GetFileInfo{ocgtools.dtx}
%
% \DoNotIndex{\newcommand,\newenvironment,\def}
%
% \title{The \textsf{ocgtools} package\thanks{This document
%     corresponds to \textsf{ocgtools}~\fileversion, dated \filedate.}
%   \thanks{Supported by grant 131/2010 of Higher
%     Education Development Fund (FRV\v S) }} \author{Robert Ma\v r\'\i k \\
%   \texttt{marik@mendelu.cz}}
%
% \maketitle
%
% \section{Introduction}
%
% The package |ocgtools| is designed to insert OGC (Optional Group
% Content) into PDF presentations. From the user's point of view, the
% package allows to insert in an comfortable way any \TeX{} material
% into separate layer in PDF document and insert links which toggle
% this layer on/off. Hence parts of PDF document like formatted text,
% tables, math formulas or graphics can be turned to visible or
% invisible state by clicking active links or buttons.
%
% Similar packages are |cooltooltips|, |pdfcomment|, |AcroTeX| and
% |fancytooltips|. In contrast to |cooltooltips| and |pdfcomment|, we
% can work with any \TeX{} material, not only plain text. In contrast
% to |fancytooltips|, no external file is necessary and more
% minilayers (see below) can be opened simultaneously on one
% page. However, |fancytooltips| and |ocgtools| can be combined in the
% same document, even on the same page.  |AcroTeX| has far more
% possibilities than |ocgtools|, but has three disadvantages: (1)
% limited support for |pdftex| (no layers with |pdftex| driver) (2)
% PDF file needs postprocessing (3) for the post-processing the non-free
% Adobe Acrobat Professional is necessary.  However, the user of
% |ocgtools| must have |AcroTeX| installed, since we use its
% capability to insert buttons and JavaScripts into document (|eforms|
% and |insdljs| packages).
%
% Two types of OCG objects can be inserted 
% \begin{itemize} 
% \item \textit{layers}: OCG's which span across the whole size of paper
%   (scaled if necessary) 
% \item \textit{minilayers}: OCG's which have their natural size and
%   are placed somewhere close to the link which toggles them on/off.
% \end{itemize}
%
% The user should use viewer which allows to hide/reveal layers by
% JavaScript. This includes especially Adobe Reader. Some limited
% functionality is also in Foxit Reader (see the option |nobutton|
% below). In some other viewers (like |xpdf|) users see red warning on
% the first page (see |\ocgtools@msg|), all layers are visible and
% cannot be hidden. 
%
% \marginpar{\texttt{Important comment!}}\textbf{\color{red} Big
% warning:} Unfortunately, with this package you may make your PDF
% files \textbf{less portable} even if you use Adobe Reader! Till now,
% we observed the following problems:
% \begin{itemize}
% \item jpg pictures may look darker when using \texttt{transparent}
%   option and \textbf{Linux} version of Adobe Reader -- Adobe Reader
%   switches to different rendering method which seems to be system
%   dependent. Hope, Adobe fixes this problem soon.
% \item The layer with initial message on the first page which should
%   be hidden to the users of Adobe Reader is sometimes still visible,
%   if the PDF file is opened in Internet browser. From this reason,
%   layer \texttt{ocgtools0} is switched on and then off when
%   initializing PDF document. Hope, this workaround solves the
%   problem. If not, report the problem with a minimal example, please.
% \end{itemize}
%
% \section{Examples} Several examples are distributed with the
% package. We have one minimal example, one example which shows
% cooperation with |preview.sty| and mainly -- examples which show
% cooperation with three most popular packages to build
% presentations. This includes |web.sty|, |pdfscreen.sty| and
% |beamer.cls|. We have three demo files for each -- with no panel,
% with panel on the right and with panel on the left. All examples can
% be recompiled by running |ocgtools-test.bat| on Windows and
% |ocgtools-test.sh| on Linux.
%
% \section{Usage}
%
% All the packages we use are on CTAN or in \TeX live2009. The file
% |ocg.sty| is a part of |asymptote| package\footnote{Do not
% interchange with the |ocg.sty| provided by \"Osterreichische
% Computer Geselshafft. The correct file |ocg.sty| is at
% \url{http://www.tug.org/svn/texlive/trunk/Master/texmf/tex/latex/asymptote/ocg.sty}}.
% |AcroTeX| is quite old on CTAN but the version on CTAN works well.
% New version of |AcroTeX| is at |www.acrotex.net|, if necessary.
%
% \bigskip \textbf{Prerequisities}: Only |pdflatex| is supported. The
% route via |dvips| or |dvipdfm| is not (yet?) supported (this
% restriction follows from |ocg.sty| and |transparent.sty|
% packages). To work with the package load |color.sty| and
% |hyperref.sty| packages. (This is usually done automatically by most
% packages which are used to build PDF presentations. If not, these
% packages are loaded by |ocgtools| automatically as soon as
% |\definecolor| and |\href| remain undefined, respectively.) You may
% use also |xcolor.sty|, but this package is not compatible with
% |transparent| option. \bigskip
%
% You have to compile your |.tex| file \textit{three times} (!). If
% you change your document and create new layer, you have to compile
% three times again. After most changes (which include change in
% position of but not the number of layers) you have to compile twice
% to put layers on correct position. If you change the content of text
% layers or minilayers, one compilation is sufficient.
%
% \bigskip
% Load package |ocgtools.sty| as usual:
% \cs{usepackage}\oarg{options}|{ocgtools}|.  Options include:
% \begin{description}
% \item[\texttt{transparent}] Layers in fact do not span over the
%   whole page, but the material of layer is scaled to some reasonable
%   size (we keep aspect ratio). If the layer is activated, the page
%   is covered by a uniform color |ocgbg| and the layer is placed on
%   the top. The options |transparent| makes the color |ocgbg|
%   transparent. Looks cool, but it could be slow and could change
%   some colors of bitmap pictures (and this behavior seems to be
%   system dependent -- looks differently on Linux and differently on
%   Windows). The transparency is achieved by putting the command
%   |\ocgtools@transparent| on appropriate place.  The default setting
%   is |\def\ocgtools@transparent{\transparent{0.4}}|. Use something
%   like
%   like the following three lines to override:\\
%   |\makeatletter|\\
%   |\def\ocgtools@transparent{\transparent{0.6}}|\\
%   |\makeeatother|
% \item[\texttt{nobutton}] The pages with active layers have a
%   transparent button which can be used to hide this layer. The user
%   simply clicks anywhere and the layers become hidden. In some
%   viewers (like Foxit Reader) the button is not 100\% transparent.
%   This option allows not to include the big button to hide layers.
%   The layers can be closed by clicking the red cross
%   below.
% \item[\texttt{insertvisible}] In some rare cases problems with OCG's
%   occur on Windows installation of \TeX. Temporary (I hope)
%   workaround is to use this option, which inserts OCG's as visible.
%   The visibility is turned off when the PDF is opened on the first
%   page.
% \item[\texttt{mouseover}] Layers can be opened by MouseOver action.
%   The corresponding active area is an invisible square with side 8pt
%   placed on the right bottom corner of the text or picture which
%   opens the layer. More details: MouseOver opens the layer and
%   MouseExit hides this layer again. If mouse button is clicked, the
%   layer remains opened after exiting the button.
% \item[\texttt{minimouseover}] As mouseover, but restricted to
%   minilayers.
% \item[\texttt{noocg}] The package and its commands are ignored.
% \item[\texttt{noprogressmsg}] Opening PDF on the first page show the
%   message related to initial processing layers. This option turns
%   the message off.
% \item[\texttt{nopageclose}] By default, all layers become off and
%   all buttons become hidden if the page is changed. The option
%   \texttt{nopageclose} suppresses this behavior.
% \end{description}
%
% The macro |\ocgtext|\oarg{width}\marg{text1}\marg{text2}
% \DescribeMacro{\ocgtext} is used to create layer which contains any
% \LaTeX{} material. \meta{text1} is a text which is written in an
% ``usual'' way (in blue color which indicates that this text can be
% used to hide/unhide another object) and this text is used to
% hide/unhide layer with \meta{text2} (which could be text divided
% into more than one paragraph, figure created by |mfpic| or any
% \LaTeX{} material). \meta{text2} is either placed into |\hbox| (if
% \meta{width} is 0\,pt, which is default) or wrapped by |\vbox| with |\hsize|
% equal to \meta{width} (in the opposite case).
%
%
%
% \DescribeMacro{\ocgpicture} The macro
% |\ocgpicture|\oarg{params}\marg{picture} is used to insert a picture
% which is used as a link to layer with bigger version of this
% picture. The optional parameter \meta{params} is used by
% |\includegraphics| command to draw picture in text. The layer
% contains scaled version of the picture (aspect ratio is preserved).
%
% \DescribeMacro{\ocgminitext} The macro
% |\ocgminitext|\oarg{width}\marg{text1}\marg{text2} is used to create
% layer containing \meta{text2} which is placed near the right top
% corner of \meta{text1}. \meta{text2} is placed into |\hbox| or
% |vbox| with given |hsize| according to the value of \meta{width},
% as has been explained at |\ocgtext| macro. Macros |\ocgminitextrb|,
% |\ocgminitextlt| and |\ocgminitextlb| can be used to place the
% minilayer to the right bottom, left to and left bottom corner.
%
% \DescribeMacro{\ocgtextstart}\DescribeMacro{\ocgtextend} Macros
% |\ocgtextstart| and |\ocgtextend| are used to denote the start and
% the end of hyperlink which is used to hide/reveal layers. Default
% setting is |\def\ocgtextstart{\color{blue}}| and |\def\ocgtextend{}|
% and hence, the links are blue and there is no mark at the end of the
% link.
%
% \DescribeMacro{ocgpaper}\DescribeMacro{ocgbg} The colors |ocgbg| and
% |ocgpaper| are used to set the colors which are used to hide page
% and color which is used as background for text produced by
% |\ocgtext| and |\ocgminitext| commands. The default setting is
% |\definecolor{ocgpaper}{rgb}{1,1,0.2}| and
% |\definecolor{ocgbg}{rgb}{0,0,0}|.
%
% \DescribeMacro{\layerHshift} \DescribeMacro{\layerVshift} The
% dimensions |\layerHshift| and |\layerVshift| are used to place
% layers exactly on the top of the page. If presentation packages
% |web|, |beamer| or |pdfscreen| are used, then the length is set
% automatically.
%
% \DescribeMacro{\ocgtools@shipoutstart@hook}
% \DescribeMacro{\ocgtools@shipoutend@hook} Commands
% |\ocgtools@shipoutstart@hook| and |\ocgtools@shipoutend@hook| are
% introduced to insert some material at the begin and at the end of
% the box with the page contents. Can be used for example to put
% background to the presentation. Both commands should produce boxes
% of zero dimensions, i.e. use something like \begin{verbatim}
% \makeatletter \def\ocgtools@shipoutstart@hook{\hbox to 0 pt{\kern
% -1in \Huge A\hss}} \makeatother \end{verbatim} to insert letter "A"
% into the left bottom corner. Note that you will see this letter only
% if the background of the presentation is transparent. See also the
% files |ocgtools-example-web*.tex| and |ocgtools-example-web*.pdf|
% for slightly more complicated background.
%
% \DescribeMacro{\ocgclosechar} \DescribeMacro{\ocgtools@msg} Macro
% |\ocgclosechar| is used to print mark which is used to close layers
% and minilayers and |\ocgtools@msg| contains string for users of
% viewers which do not support layers (see the first few lines of the
% code for default settings).
%
% \section{Possible future development and known problems}
% \begin{enumerate}
% \item Test and support more packages for slides than current
%   web.sty, pdfscreen.sty and beamer.sty? Which ones?
% \item Using |preview| package it is possible to extract equations
%   and figures from the document and redefine |\ref| and |\eqref|
%   commands in such a way that clicking (or mouseover) opens on the
%   current page the layer with this equation (figure) and shift
%   clicking moves the user to the page with this equation
%   (figure). Or would |fancytooltips| produce better results in this
%   case (smaller PDF file)? See also the paper by Ross More at
%   \url{http://www.tug.org/TUGboat/Articles/tb29-3/tb93moore.pdf}. See
%   the |ocgtools-preview.*| files for some initial attempts in this
%   direction.
% \item Introduce draft mode, which prints all layers at the end of
%   document with links there and back?
% \item Improve placement of minitextlayers. Currently we have one the
%   reference point for both minitextlayers aligned to top and
%   bottom, which is far from optimal.
% \item In some rare cases the layers may be not inserted properly
%   when the \LaTeX{} file is compiled on \textbf{Windows} -- see the
%   option \texttt{insertvisible} which solves this problems in most
%   cases.  Since the author has limited access to \TeX{}
%   installations on Windows and hence reporting of problems with a
%   minimal example is highly appreciated.
% \item The package cannot be used to hide 3D graphics inserted by
%   |movie15| package. However, you can put this graphics into
%   floating window or full-screen using capabilities of Acrobat Reader
%   9.
% \item If you open and close layer and then use "Back"
%   (Alt+LeftArrow), the layer opens again, but the button to hide
%   this layer becomes unavailable. Possible solution is to define
%   open action and close action for each layer (is it possible in
%   current PDF specification?).
% \end{enumerate}
%
% \StopEventually{}
% 
% \section{Implementation}
% Initial settings
%    \begin{macrocode}
%<*package>
\def\ocgtools@msg{If message does not disappear after a short time, the
  author either did not compile the \LaTeX{} file three times, or your
  PDF viewer does not support OCG. Use Adobe Reader!}

\def\ocgtextstart{\color{blue}}
\def\ocgtextend{}
\ifx\definecolor\undefined\RequirePackage{color}\fi
\ifx\href\undefined\RequirePackage[pdftex]{hyperref}\fi
\definecolor{ocgpaper}{rgb}{1,1,0.2}
\definecolor{ocgbg}{rgb}{0,0,0}
\RequirePackage{graphicx}
\RequirePackage{pifont}
\RequirePackage{ocg}

\def\ocgclosechar{\ding{56}}
%    \end{macrocode}
% Make packages and options known.
%    \begin{macrocode}
\RequirePackage{atbegshi}
\RequirePackage[pdftex]{eforms}
\newif\if@ocgtools@transparent\@ocgtools@transparentfalse
\DeclareOption{transparent}{\@ocgtools@transparenttrue}
\newif\if@ocgtools@insertvisible\@ocgtools@insertvisiblefalse
\def\ocgtools@initialvisibility{0}
\DeclareOption{insertvisible}{\@ocgtools@insertvisibletrue
   \def\ocgtools@initialvisibility{1}}
\newif\ifocg@hide@button\ocg@hide@buttontrue
\DeclareOption{nobutton}{\ocg@hide@buttonfalse}
\newif\ifocgtools@noocg\ocgtools@noocgfalse
\DeclareOption{noocg}{\ocgtools@noocgtrue}
\def\ocgtools@progressmsg{\lower \layerVshift\hbox to 0 pt{ %space
  \textField[\V{OCGtools: processing OCG's ...}\BG{1} 
  \textColor{1 0 0} \textSize{10}]%
{ocgtoolsmsg}{6cm}{20pt}\hss}}%
\DeclareOption{noprogressmsg}{\let\ocgtools@progressmsg\relax}
\newif\if@ocgtools@minimouseover \@ocgtools@minimouseoverfalse
\DeclareOption{minimouseover}{\@ocgtools@minimouseovertrue}
\newif\if@ocgtools@mouseover \@ocgtools@mouseoverfalse
\DeclareOption{mouseover}{\@ocgtools@mouseovertrue \@ocgtools@minimouseovertrue}
\newif\if@ocgtools@pageclose \@ocgtools@pageclosetrue
\DeclareOption{nopageclose}{\@ocgtools@pageclosefalse}
\ProcessOptions
%    \end{macrocode}
% We insert JavaScripts which are evaluated when the file is opened at
% the first page.
%    \begin{macrocode}
\edef\ocgtools@pdfpageattr{
/AA << /O << /S /JavaScript /JS 
  (
%    \end{macrocode}
% We close layers, if the page is opened. If the document is opened
% and \texttt{ocgtoolsOCGs} is not initialized, we skip to
% \texttt{catch} part and initialize.
%    \begin{macrocode}
  try{
   var temp = ocgtoolsOCGs.length;
   \if@ocgtools@pageclose
   for(var i=0; ocgtoolsOCGs && i<ocgtoolsOCGs.length;i++)
     {
      ocgtoolsOCGs[i].state = false;
     }
   \fi
  }
%    \end{macrocode}
% We initialize document -- we find all layers, put into
% \texttt{ocgtoolsOCGs} field and make them hidden.
%    \begin{macrocode}
  catch (e){
%    \end{macrocode}
% No dotted rectangle for buttons which have focus.
%    \begin{macrocode}
    app.focusRectg = false;
%    \end{macrocode}
% The OCG objects inserted by ocgtools are stored in variable
% |ocgtoolsOCGs| when the PDF document is opened on the first page.
%    \begin{macrocode}
    var iniocgtoolsOCGs = this.getOCGs();
    var ocgtoolsOCGs = [];
    for(var i=0; iniocgtoolsOCGs && i<iniocgtoolsOCGs.length;i++)
     {
     if(iniocgtoolsOCGs[i].name.substr(0,8) == "ocgtools")
        {
          ocgtoolsOCGs.push(iniocgtoolsOCGs[i]);
          \if@ocgtools@insertvisible
          iniocgtoolsOCGs[i].state=false;
          \fi
        }  
     }
    iniocgtoolsOCGs[0].state=true;
    iniocgtoolsOCGs[0].state=false;
    \ifx\ocgtools@progressmsg\relax\relax\else
    this.getField("ocgtoolsmsg").hidden=true;
    \fi
    }
    \ifocg@hide@button 
     this.getField("OcgtoolsBtn.HideButton.main").hidden = true; 
    \fi
    )
    >> >>
}

\expandafter\global\expandafter\pdfpageattr\expandafter{\ocgtools@pdfpageattr}

\def\ocgtools@transparent{}
\if@ocgtools@transparent
\RequirePackage{transparent} \def\ocgtools@transparent{\transparent{0.4}}
\fi
%    \end{macrocode}
% Internal variables
%    \begin{macrocode}
\newif\ifocg@minitext@left
\newif\ifocg@minitext@bottom
\newcount\ocgtools@layercount
\newskip\ocgtools@left@skip
\newskip\ocgtools@bottom@skip
\newdimen\ocgtools@maxheight
\newdimen\ocgtools@maxwidth
%    \end{macrocode}
% We introduce lengths which can be used to finetune position of layers
% on the screen.  These lengths are not necessary for plain pages
% (like article class with geometry package) and beamer.
%    \begin{macrocode}
\newdimen\layerHshift \layerHshift=0pt
\newdimen\layerVshift \layerVshift=0pt
%    \end{macrocode}
% We need small shift for web.sty.
%    \begin{macrocode}
\ifx\@Rightmargin\undefined\else
  \layerVshift=3pt
\fi
%    \end{macrocode}
% We need another shift for pdfscreen.sty.
%    \begin{macrocode}
\ifx\PDFSCR@Warning\undefined\else
  \layerVshift=\@Bottommargin
\fi

\newtoks\ocgtools@layer@toks
\ocgtools@layer@toks{}
\newbox\ocgtools@box@a
\newbox\ocgtools@box@b
\newif\ifocg@page@contains@layer
%    \end{macrocode}
% We modify |shipout| routine and insert content of layers on the top of
% PDF pages.
%    \begin{macrocode}
\def\ocgtools@one{1}
\let\ocgtools@shipoutstart@hook\relax
\let\ocgtools@shipoutend@hook\relax
\AtBeginShipout{%
\setbox\AtBeginShipoutBox=\hbox{%
%    \end{macrocode}
% Hook which can be used to place backgound.
%    \begin{macrocode}
\ocgtools@shipoutstart@hook
%    \end{macrocode}
% We insert the page first.
%    \begin{macrocode}
\hbox to 0 pt{\box\AtBeginShipoutBox\hss}\kern -1in\kern \layerHshift
%    \end{macrocode}
% We insert the layers.
%    \begin{macrocode}
\ocg@page@contains@layerfalse
\lower \layerVshift \hbox{\the\ocgtools@layer@toks}%
%    \end{macrocode}
%    If at least one layer has been inserted, we insert button which
%    can be used to hide layers.
%    \begin{macrocode}
\ifocg@page@contains@layer
  \vbox to 0 pt{\kern -\paperheight \kern\layerVshift\hbox to 0 pt{\ocgtools@HideBtn\hss}\vss}%
\fi
%    \end{macrocode}
% We insert progress field on the first page.
%    \begin{macrocode}
\xdef\ocgtools@currpage{\thepage}%
\ifx\ocgtools@currpage\ocgtools@one
\ocgtools@progressmsg
\fi
%    \end{macrocode}
% We finish the box.
%    \begin{macrocode}
\hss \ocgtools@shipoutend@hook%
\kern 1in \kern -\layerHshift%
}%
}
%    \end{macrocode}
% We create buttons which are used to hide all layers (if we create
% buttons, we keep possibility to make them hidden, in contrast to
% links).
%    \begin{macrocode}
\def\ocgtools@HideBtn{\pushButton[\W{0}\BG{}\S{S}\BC{}\H{N}\F{\FHidden}\A{\JS{         
         for(var i=0; ocgtoolsOCGs && i<ocgtoolsOCGs.length;i++)
         {
         ocgtoolsOCGs[i].state = false;
         }
         \ifocg@hide@button this.getField("OcgtoolsBtn.HideButton").hidden = true; \fi
       }}]{OcgtoolsBtn.HideButton.main}{\paperwidth}{\paperheight}}
%    \end{macrocode}
% We create buttons which are attached to the top right corner 
% of each minilayer and can be used to hide the corresponding minilayer.
%    \begin{macrocode}
\newcommand\ocgtools@HideMiniLayer[1]
{\setbox\ocgtools@box@a=\hbox{{\color{red}\ocgclosechar}}%
\hbox to \wd\ocgtools@box@a%
{\hbox{{\color{red}\ocgclosechar}}\hss%
\hbox{\pushButton[\W{0}\S{S}\BG{}\BC{}\H{N}%
\F{\FHidden}\A{\JS{         
         for(var i=0; ocgtoolsOCGs && i<ocgtoolsOCGs.length;i++)
         {
         if(ocgtoolsOCGs[i].name == "ocgtools#1")
         ocgtoolsOCGs[i].state = false;
         }
         this.getField("OcgtoolsBtn.HideButton.mini.#1").hidden = true; 
       }}]%
     {OcgtoolsBtn.HideButton.mini.#1}{\wd\ocgtools@box@a}{\ht\ocgtools@box@a}}}}
%    \end{macrocode}
% We create buttons which are attached tothe bottom right corner 
% of the screen and can be used to hide all layers.
%    \begin{macrocode}
\newcommand\ocgtools@HideLayers[1]{%
\setbox\ocgtools@box@a=\hbox{{\color{red}\ocgclosechar}}%
\hbox to \wd\ocgtools@box@a%
{\hbox{{\color{red}\ocgclosechar}}\hss%
\hbox{\pushButton[\W{0}\S{S}\BG{}\BC{}\H{N}%
\F{\FHidden}\A{\JS{         
         for(var i=0; ocgtoolsOCGs && i<ocgtoolsOCGs.length;i++)
         {
         ocgtoolsOCGs[i].state = false;
         }
         \ifocg@hide@button 
         this.getField("OcgtoolsBtn.HideButton.main").hidden = true; 
         \fi
       }}]%
     {OcgtoolsBtn.HideButton.corner.#1}{\wd\ocgtools@box@a}{\ht\ocgtools@box@a}}}}
%    \end{macrocode}
% To hide/unhide layers and minilayers we use modified code from
% http://www.texample.net/weblog/2008/nov/02/creating-pdf-layers/
%    \begin{macrocode}
\newcommand{\ocgtools@ToggleLayer}[2]{%
  % #1: layer name,
  % #2: link text
  \leavevmode%
  \pdfstartlink user {
    /Subtype /Link
    /Border [0 0 0]%
    /H /N
    /A <<
      /S/JavaScript
      /JS (
         for(var i=0; ocgtoolsOCGs && i<ocgtoolsOCGs.length;i++)
         {
         if(ocgtoolsOCGs[i].name == "ocgtools#1")
         ocgtoolsOCGs[i].state = !ocgtoolsOCGs[i].state;
         else
         ocgtoolsOCGs[i].state = false;
         }
         \ifocg@hide@button this.getField("OcgtoolsBtn.HideButton.main").hidden = false;\fi
         this.getField("OcgtoolsBtn.HideButton.corner").hidden = false;
         this.getField("OcgtoolsBtn.HideButton.corner.#1").setFocus();
      )
    >>
  }#2%
    \if@ocgtools@mouseover
  \hbox to 0 pt{\hss\pushButton[\W{0}\S{S}\BG{}\BC{}\H{N}%
  \A{\JS{
        var OpenedByMouseEnter#1=false;
        \ifocg@hide@button this.getField("OcgtoolsBtn.HideButton.main").hidden = false;\fi
        this.getField("OcgtoolsBtn.HideButton.corner").hidden = false;
        this.getField("OcgtoolsBtn.HideButton.corner.#1").setFocus();
        }}
  \AA{\AAMouseEnter{\JS{         for(var i=0; ocgtoolsOCGs && i<ocgtoolsOCGs.length;i++)
         {
         var OpenedByMouseEnter#1=true;
         if(ocgtoolsOCGs[i].name == "ocgtools#1")
         ocgtoolsOCGs[i].state = true;
         else
         ocgtoolsOCGs[i].state = false;
         }
         }}
  \AAMouseExit{\JS{ 
        if (OpenedByMouseEnter#1)
         {for(var i=0; ocgtoolsOCGs && i<ocgtoolsOCGs.length;i++)
         {
         if(ocgtoolsOCGs[i].name == "ocgtools#1")
         ocgtoolsOCGs[i].state = false;
         }}
         }}}]
    {OcgtoolsBtn.MaxiButton.#1}{8pt}{8pt}}%
  \fi
  \pdfendlink%
}

\newcommand{\ocgtools@ToggleMiniLayer}[2]{%
  % #1: layer name,
  % #2: link text
  \leavevmode%
  \pdfstartlink user {
    /Subtype /Link
    /Border [0 0 0]%
    /H /N
    /A <<
      /S/JavaScript
      /JS (
         for(var i=0; ocgtoolsOCGs && i<ocgtoolsOCGs.length;i++)
         {
         if(ocgtoolsOCGs[i].name == "ocgtools#1")
         ocgtoolsOCGs[i].state = !ocgtoolsOCGs[i].state;
         }
         this.getField("OcgtoolsBtn.HideButton.mini.#1").hidden = 
         !this.getField("OcgtoolsBtn.HideButton.mini.#1").hidden; 
         if (!this.getField("OcgtoolsBtn.HideButton.mini.#1").hidden)
           {this.getField("OcgtoolsBtn.HideButton.mini.#1").setFocus();}
      )
    >>
  }#2%
  \if@ocgtools@minimouseover
  \hbox to 0 pt{\hss\pushButton[\W{0}\S{S}\BG{}\BC{}\H{N}%
  \A{\JS{
    var OpenedByMouseEnter#1=false;
    this.getField("OcgtoolsBtn.HideButton.mini.#1").setFocus(); 
   }}
  \AA{\AAMouseEnter{\JS{for(var i=0; ocgtoolsOCGs && i<ocgtoolsOCGs.length;i++)
         {
         var OpenedByMouseEnter#1=true;
         if(ocgtoolsOCGs[i].name == "ocgtools#1")
         ocgtoolsOCGs[i].state = true;
         }
         this.getField("OcgtoolsBtn.HideButton.mini.#1").hidden = false;
         }}
  \AAMouseExit{\JS{
       if (OpenedByMouseEnter#1)
       {
         for(var i=0; ocgtoolsOCGs && i<ocgtoolsOCGs.length;i++)
         {
         if(ocgtoolsOCGs[i].name == "ocgtools#1")
         ocgtoolsOCGs[i].state = false;
         }
         this.getField("OcgtoolsBtn.HideButton.mini.#1").hidden = true;
       }
       }}}]
    {OcgtoolsBtn.MiniButton.#1}{8pt}{8pt}}%
  \fi
  \pdfendlink%
}

%    \end{macrocode}
% Macros dealing with |\savepos| from Vit Zyka's paper in CSTUG
% Bulletin (ISSN 1211-6661, No. 2, 2007)
%    \begin{macrocode}
\newwrite\posHandle
\def\posFile{\jobname.pos}
\def\posOpen{\openout\posHandle=\posFile}
\def\posClose{\closeout\posHandle}
\ifocgtools@noocg
\else
\AtBeginDocument{\InputIfFileExists{\posFile}{}{}%
\ocgtools@maxheight=0.9\paperheight
\ocgtools@maxwidth=0.9\paperwidth
\expandafter\global\expandafter\def\csname ocgtools@textcontent@0\endcsname
{{\color{red}\bfseries\ocgtools@msg}}
\expandafter\ocgtools@layer@toks\expandafter{\the\ocgtools@layer@toks
\ocg@place@text{0}{1}{5cm}}
\posOpen}
\AtEndDocument{\posClose}
\fi

%    \end{macrocode}
% The following commands create links to hide/unhide layers and
% minilayers and write information on the layer number, page and layer
% content into the |pos| file. The content of textlayer is stored in
% command |\ocgtools@textcontent@<n>| where |<n>| is the number of the
% layer. Similarly, the content of the minitextlayer is stored in
% |\ocgtools@minitextcontent@<n>|.
%    \begin{macrocode}
\newcommand\ocgpicture[2][]{%
\global\advance\ocgtools@layercount by 1%
\ocgtools@ToggleLayer{\the\ocgtools@layercount}%
{\leavevmode\includegraphics[#1]{#2}}%
\expandafter\write\expandafter\posHandle\expandafter{%
\expandafter\string\expandafter\ocgtoolsplacepicture\expandafter{%
\the\ocgtools@layercount}{\thepage}{#2}}%
}

\newcommand\ocgtext[3][0pt]{%
\global\advance\ocgtools@layercount by 1%
\ocgtools@ToggleLayer{\the\ocgtools@layercount}%
{\leavevmode{\ocgtextstart #2\ocgtextend}}%
\expandafter\write\expandafter\posHandle\expandafter{%
\expandafter\string\expandafter\ocgtoolsplacetext\expandafter{%
\the\ocgtools@layercount}{\thepage}{#1}}%
\expandafter\global\expandafter\def
\csname ocgtools@textcontent@\the\ocgtools@layercount\endcsname{#3}%
}

\newcommand\ocgminitextlt[3][0pt]{%
\ocg@minitext@lefttrue\do@ocg@minitext{#1}{#2}{#3}{lt}}%
\newcommand\ocgminitextrt[3][0pt]{%
\ocg@minitext@leftfalse\do@ocg@minitext{#1}{#2}{#3}{rt}}%
\newcommand\ocgminitextlb[3][0pt]{%
\ocg@minitext@lefttrue\do@ocg@minitext{#1}{#2}{#3}{lb}}%
\newcommand\ocgminitextrb[3][0pt]{%
\ocg@minitext@leftfalse\do@ocg@minitext{#1}{#2}{#3}{rb}}%
\let\ocgminitext\ocgminitextrt

\long\def\do@ocg@minitext#1#2#3#4{%
\global\advance\ocgtools@layercount by 1\relax%
\leavevmode
\ifocg@minitext@left
  \vbox to 0 pt{\kern 0.5\baselineskip\hbox to 0 pt{\hss\pdfsavepos}\vss}%
\fi
\ocgtools@ToggleMiniLayer{\the\ocgtools@layercount}%
{{\ocgtextstart #2\ocgtextend}}%
\ifocg@minitext@left\else\pdfsavepos\fi
\expandafter\write\expandafter\posHandle\expandafter{%
\expandafter\string\expandafter\ocgtoolsplaceminitext\expandafter
{\the\ocgtools@layercount}{\thepage}{\the\pdflastxpos}%
{\the\pdflastypos}{}{#1}{#4}}%
\expandafter\global\expandafter\def
\csname ocgtools@minitextcontent@\the\ocgtools@layercount\endcsname{#3}%
}%

%    \end{macrocode}
% These commands appear in |pos| file and we read these commands at the
% begin of the document. Pictures are stored in token register, texts
% for layers and minilayers are stored in commands defined by
% |\csname|.
%    \begin{macrocode}
\newcommand\ocgtoolsplacepicture[3]{%
\expandafter\global\expandafter\ocgtools@layer@toks\expandafter
{\the\ocgtools@layer@toks \ocg@place@picture{#1}{#2}{#3}}}

\newcommand\ocgtoolsplacetext[3]{%
\expandafter\global\expandafter\ocgtools@layer@toks\expandafter
{\the\ocgtools@layer@toks\ocg@place@text{#1}{#2}{#3}}}

\newcommand\ocgtoolsplaceminitext[7]{%
\expandafter\global\expandafter\ocgtools@layer@toks\expandafter
{\the\ocgtools@layer@toks \ocg@place@minitext{#1}{#2}{#3}{#4}{#5}{#6}{#7}}}

%    \end{macrocode}
% These commands are called in output routine for each layer on each
% page. They put the layer on the page, if the layer should be here
% (i.e., if the second parameter equals |\thepage|).
%    \begin{macrocode}
\def\ocg@place@picture#1#2#3{\def\tempnuma{#2}\edef\tempnumb{\thepage}%
\ifx\tempnumb\tempnuma
  \global\ocg@page@contains@layertrue
  \vbox to 0 pt{\vss\hbox to 0pt%
    {\hbox{\begin{ocg}{ocgtools#1}{ocgtools#1}{\ocgtools@initialvisibility}%
          \ocgtools@drawpicture{#3}{#1}\end{ocg}}\hss}}%
\fi}

\def\ocg@place@text#1#2#3{\def\tempnuma{#2}\edef\tempnumb{\thepage}%
\ifx\tempnumb\tempnuma
  \global\ocg@page@contains@layertrue
  \vbox to 0 pt{\vss\hbox to 0 pt{\hbox{%
    \begin{ocg}{ocgtools#1}{ocgtools#1}{\ocgtools@initialvisibility}%
      \ocgtools@drawtext{#3}{\csname ocgtools@textcontent@#1\endcsname}{#1}%
    \end{ocg}}\hss}}%
\fi}

\long\def\ocg@place@minitext#1#2#3#4#5#6#7{%
\def\tempnuma{#2}\edef\tempnumb{\thepage}%
\def\ocg@placement{#7}%
\def\ocg@rb{rb}\def\ocg@lb{lb}\def\ocg@rt{rt}\def\ocg@lt{lt}%
\ocg@minitext@leftfalse\ocg@minitext@bottomfalse
\ifx\ocg@placement\ocg@lb 
  \ocg@minitext@lefttrue\ocg@minitext@bottomtrue
\fi
\ifx\ocg@placement\ocg@rb 
  \ocg@minitext@leftfalse\ocg@minitext@bottomtrue\fi
\ifx\ocg@placement\ocg@lt 
  \ocg@minitext@lefttrue\ocg@minitext@bottomfalse
\fi
\ifx\tempnumb\tempnuma
  \ocgtools@left@skip=#3sp minus #3sp%
  \ocgtools@bottom@skip=#4sp%
  \setbox\ocgtools@box@a=\hbox{\begin{ocg}{ocgtools#1}{ocgtools#1}{\ocgtools@initialvisibility}%
  \ocgtools@drawminitext{#6}{\csname ocgtools@minitextcontent@#1\endcsname}{#1}
  \end{ocg}}%
  \ifocg@minitext@left
    \ifdim \ocgtools@left@skip>\wd\ocgtools@box@a 
      \advance \ocgtools@left@skip by -\wd\ocgtools@box@a plus 0 pt minus -\wd\ocgtools@box@a
    \else
      \ocgtools@left@skip=0pt\relax
    \fi
  \fi
  \ifocg@minitext@bottom
    \advance \ocgtools@bottom@skip by -\ht\ocgtools@box@a
  \fi
  \hbox to 0 pt{\hbox to \paperwidth{\hskip \ocgtools@left@skip
  \vbox to 0 pt{\vss
  \vbox to \paperheight{\vskip 0 pt plus 1 fill
  \box\ocgtools@box@a\vskip \ocgtools@bottom@skip}
  }%
  \hskip 0 pt plus 1 fill}\hss}%
\fi
}

%    \end{macrocode}
% This code actually gives a graphical representation of the layers.
%    \begin{macrocode}
\def\ocgtools@drawpicture#1#2{\vbox to \paperheight{\vbox to 0 pt{{%
\ocgtools@transparent\color{ocgbg}%
\hrule width \paperwidth height \paperheight}\vss}\vss
\hbox to \paperwidth{\hss 
\setbox\ocgtools@box@a=\hbox{\includegraphics[height=\ocgtools@maxheight]{#1}}%
\ifdim\wd\ocgtools@box@a>\ocgtools@maxwidth
  \includegraphics[width=\ocgtools@maxwidth]{#1}\else \box\ocgtools@box@a
\fi
\hss}%
\vss
\hbox to \paperwidth {\hss\ocgtools@HideLayers{#2}}}}

\newdimen\ocg@textdimen

\long\def\ocgtools@drawtext#1#2#3{\vbox to \paperheight{%
\vbox to 0 pt{{\ocgtools@transparent\color{ocgbg}%
\hrule width \paperwidth height \paperheight}\vss}\vss\hbox to \paperwidth
{\hss 
\ocg@textdimen=#1
\ifdim\ocg@textdimen=0pt
  \setbox\ocgtools@box@a=\hbox{\colorbox{ocgpaper}{#2}}%
\else
  \setbox\ocgtools@box@a=\hbox{\colorbox{ocgpaper}%
    {\vbox{\hsize=#1\relax\rightskip 0 pt plus 1 fil\relax #2}}}%
\fi
\setbox\ocgtools@box@b\hbox{\resizebox{!}{\ocgtools@maxheight}{\copy\ocgtools@box@a}}%
\ifdim\wd\ocgtools@box@b>\ocgtools@maxwidth
  \resizebox{\ocgtools@maxwidth}{!}{\box\ocgtools@box@a}
\else 
  \box\ocgtools@box@b
\fi
\hss}%
\vss
\hbox to \paperwidth {\hss\ocgtools@HideLayers{#3}}}}

\def\ocg@empty{}

\newcommand\ocgtools@drawminitext[3]{\ocg@textdimen=#1
\ifdim\ocg@textdimen=0pt
  \setbox\ocgtools@box@a=\hbox{\colorbox{ocgpaper}{#2\ }}%
\else  \setbox\ocgtools@box@a=\hbox{\colorbox{ocgpaper}%
  {\vbox{\hsize=#1\relax\rightskip 0 pt plus 1 fil\relax #2}}}%
\fi
\def\temp{#3}%
\ifx\temp\ocg@empty
\box\ocgtools@box@a
\else
\hbox{\copy\ocgtools@box@a\raise\ht\ocgtools@box@a
\vbox to 0 pt{\hbox to 0 pt{\hss
\ocgtools@HideMiniLayer{#3}}\vss}}%
\fi
}

\ifocgtools@noocg
  \renewcommand\ocgpicture[2][]{\leavevmode\includegraphics[#1]{#2}}
  \renewcommand\ocgtext[3][0pt]{\leavevmode #2}
  \long\def\do@ocg@minitext#1#2#3#4{\leavevmode #2}%
  \renewcommand\ocgtoolsplacepicture[3]{}
  \renewcommand\ocgtoolsplacetext[3]{}
  \renewcommand\ocgtoolsplaceminitext[7]{}
\fi

%</package>
%    \end{macrocode}
%
% \Finale
\endinput



